本帖最后由 kyh咚疯 于 2021-1-4 11:26 编辑
采用stm32f103通过spi与mrf24J40MA进行通信,如何实现无线收发,我用的mrf24j40的驱动代码,在发送时,发现TXNCON的第0位一直为1并未主动清零,且未触发发送中断,只有同时设置TXNCON的TXNTRIG(0x1B<0>)和TXNSECEN(0x1B<1>)这两位时才会触发发送中断及TXNCON位清0操作,且传输一段时间后会放弃传输,按照数据手册P112介绍:Transmit the packet by setting the TXNTRIG (TXNCON 0x1B<0>) bit = 1. The bit will be automatically cleared by hardware.这与我的操作相矛盾,我不知道是哪里出了问题,希望大家可以看一下。void MRF24J40Init(void)
{
uint8_t i;
uint16_t j;
//reset the MRF24J40 by pulling the RESET pin low
RESET_LOW;
for(j=0;j<(uint8_t)300;j++){}
RESET_HIGH;
drv_delay_ms(3);//delay 2-3 ms after a Reset before accessing the MRF24J40 to allow the RF circuitry to start up and stabilize.
//do a soft reset
//bit7-3 reserver :maintain as '0'
//bit2 - 1 reset power managerment circuitry (bit is automatically cleared to ‘0’ by hardware)
//bit1 - 1 Reset baseband circuitry (bit is automatically cleared to ‘0’ by hardware)
//bit0 - 1 Reset MAC circuitry (bit is automatically cleared to ‘0’ by hardware)
MRF24_SetShortRAMAddr(WRITE_SOFTRST,0x07);
//wait for bit0-2 clear to '0'
do{
i = MRF24_GetShortRAMAddr(READ_SOFTRST);
}while((i&0x07) != (uint8_t)0x00);
i=MRF24_GetShortRAMAddr(READ_ORDER);
if(i != 0xFF){
printf("MRF24J40 Daughter card not inserted\t\n,0x%02x \t\n",i);
return ;//error
}
MRF24_SetShortRAMAddr(WRITE_PACON2,0x98);//PACON2 = 0x98, Initialize FFOEN=1 and TXONTS = 0x6
MRF24_SetShortRAMAddr(WRITE_TXSTBL,0x95);//Initialize RFSTBL = 0x9
MRF24_SetLongRAMAddr(RFCON0,0x03);//Initialize RFOPT = 0x03, not use
MRF24_SetLongRAMAddr(RFCON1,0x01);//Initialize VCOOPT=0x02,not use
MRF24_SetLongRAMAddr(RFCON2,0x80);//Enable PLL
MRF24_SetLongRAMAddr(RFCON6,0x90);//Initialize TXFIL=1, 20MRECVR=1
MRF24_SetLongRAMAddr(RFCON7,0x80);//Initialize SLPCLKSEL = 0x2 (100KHz internal oscialltor)
MRF24_SetLongRAMAddr(RFCON8,0x10);//Initialize RFVCO =1
MRF24_SetLongRAMAddr(SLPCON0,0x02);//interrupt rising edge
MRF24_SetLongRAMAddr(SLPCON1,0x21);//Initialize CLKOUTEN=1 and SLPCLKDIV = 0x01
//deafult Nonbeacon_enabled device,disable slotted csma-ca mode
//Configuration for nonbeacon-enabled devices
MRF24_SetShortRAMAddr(WRITE_BBREG2,0x80);//Set CCA mode to ED
MRF24_SetShortRAMAddr(WRITE_CCAEDTH,0x60);//Set CCA-ED Threshold
MRF24_SetShortRAMAddr(WRITE_BBREG6,0x40);//Set appended RSSI value to RX FIFO
MRF24_SetShortRAMAddr(WRITE_INTCON,0b11110110);//INTCON (0x32) = 0xF6 - Enables only TXNIF and RXIF interrupts,Enables the RX/TX FIFO reception interrupt, refer to datasheet P.54
//MRF24_SetLongRAMAddr(RFCON3,0x48);//Large Scale Control for TX Power=-10dB,Small Scale Control for TX Power=-0.5dB
MRF24_SetChannel(CHANNEL_15);//set operating channel as channel 11
//delay 192us after RF State Machine Reset
drv_delay_us(200);
do{ //Wait until th RFSTATE machine indicates RX state
i = MRF24_GetLongRAMAddr(RFSTATE);
}while((i&0xA0) != 0xA0);
//Program the short MAC address ,x0ffff
MRF24_SetShortRAMAddr(WRITE_SADRL,0xFF);
MRF24_SetShortRAMAddr(WRITE_SADRH,0xFF);
// Load the short address of the device with 0xffff which means that it will be ignore upon receipt
//Load the PAN address also with 0xfff
MRF24_SetShortRAMAddr(WRITE_PANIDL,0xFF);
MRF24_SetShortRAMAddr(WRITE_PANIDH,0xFF);
//program Long MAC address
for(i=0;i<(BYTE)8;i++){
//load the long address as 07,06,05,04,03,02,01,00
MRF24_SetShortRAMAddr(WRITE_EADR0+i*2,myLongAddress[i]);
}
// Automatic acknowledgement is not transmitted for received packets by TXMAC
//ERRpkt mode bit is set to 0,Disables automatic Acknowledgement response
//Promiscuous packet mode bit is set to 1,Receive all packet types with good CRC
MRF24_SetShortRAMAddr(WRITE_RXMCR,0x21);
}
void MRF24_SendPacket(BYTE header_len, BYTE Frame_len, BYTE* packet){
BYTE i;
MRF24_SetLongRAMAddr(TX_NORMAL_FIFO,header_len);
MRF24_SetLongRAMAddr(TX_NORMAL_FIFO+1,Frame_len);
for(i=2;i<Frame_len+2;i++){
MRF24_SetLongRAMAddr(TX_NORMAL_FIFO+i,packet[i-2]);
}printf("--0x%02x",MRF24_GetLongRAMAddr(TX_NORMAL_FIFO+2));
MRF24_SetShortRAMAddr(WRITE_TXNCON,0x01);//Transmit the frame in the TX Normal FIFO; bit is automatically cleared by hardware
}
void radio(){//int main()中执行的函数
//mrf24j40 signal pin configure
GPIO_InitTypeDef InitStructure;
//enable pin configure
RCC_APB2PeriphClockCmd(RESET_CLK|INT_CLK|WAKE_CLK,ENABLE);
//interrupt pin configure input
InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
InitStructure.GPIO_Pin = INT_PIN;
GPIO_Init(INT_PORT,&InitStructure);
//---out pin
//RESET pin
InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
InitStructure.GPIO_Pin = RESET_PIN;
GPIO_Init(RESET_PORT,&InitStructure);
//WAKE pin
InitStructure.GPIO_Pin = WAKE_PIN;
GPIO_Init(WAKE_PORT,&InitStructure);
//NVIC init
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//EXTI
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource5);
EXTI_InitTypeDef Exti_initstructure;
Exti_initstructure.EXTI_Line = EXTI_Line5;
Exti_initstructure.EXTI_Mode = EXTI_Mode_Interrupt;
Exti_initstructure.EXTI_Trigger = EXTI_Trigger_Rising;
Exti_initstructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&Exti_initstructure);
MRF24J40Init();//channel ,rx/tx interrupt and nobeacon-enable device, disable slotted-enable
LED1_ON;
drv_delay_500Ms(10);
LED1_OFF;
printf("state:0x%02x\t\n",MRF24_GetLongRAMAddr(RFSTATE));
#ifdef MRF_TX
printf("send start\t");
while(1){
if(MRF24_GetShortRAMAddr(READ_TXSTAT) & 0x20){printf("Channel busy\t\n");continue;}
drv_delay_ms(20);
// printf("~~0x%02x\t\n",MRF24_GetShortRAMAddr(READ_TXNCON));
// MRF24_SendPacket(9,sizeof(txbuff),(BYTE*)txbuff);
MRF24_SetLongRAMAddr(TX_NORMAL_FIFO,0x0D);
MRF24_SetLongRAMAddr(0x001,0x0D);
MRF24_SetLongRAMAddr(0x002,0x01);
MRF24_SetLongRAMAddr(0x003,0x04);
MRF24_SetLongRAMAddr(0x004,0xC4);
MRF24_SetLongRAMAddr(0x005,0xFF);
MRF24_SetLongRAMAddr(0x006,0xFF);
MRF24_SetLongRAMAddr(0x007,0xFF);
MRF24_SetLongRAMAddr(0x008,0xFF);
MRF24_SetLongRAMAddr(0x009,0x07);
MRF24_SetLongRAMAddr(0x00A,0x00);
MRF24_SetLongRAMAddr(0x00B,0x00);
MRF24_SetLongRAMAddr(0x00C,0x00);
MRF24_SetLongRAMAddr(0x00D,0x00);
MRF24_SetLongRAMAddr(0x00E,0xAA);
<font color="#ff0000"> MRF24_SetShortRAMAddr(WRITE_TXNCON,0b00000011);// 本应只设置0b00000001即可实现发送,但现在需要设置两位模块才会产生发送中断</font>
if((MRF24_GetShortRAMAddr(READ_TXSTAT)&0x01)==0){
drv_delay_500Ms(2);
LED2_TOGGLE;
}else{//transmit failed
printf("retry:0x%02x---0x%02x\t",MRF24_GetShortRAMAddr(READ_TXSTAT)&0x01,MRF24_GetShortRAMAddr(READ_TXSTAT)&0x60);
drv_delay_500Ms(2);
LED3_TOGGLE;
}
}
}
|
|