搜索

[硬件] STM32f103+mrf24J40

[复制链接]
3537|3
 楼主 | 2021-1-3 12:33 | 显示全部楼层 |阅读模式
本帖最后由 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.这与我的操作相矛盾,我不知道是哪里出了问题,希望大家可以看一下。
  1. void MRF24J40Init(void)
  2. {
  3.         uint8_t i;
  4.         uint16_t j;
  5.         //reset the MRF24J40 by pulling the RESET pin low
  6.         RESET_LOW;
  7.         for(j=0;j<(uint8_t)300;j++){}
  8.         RESET_HIGH;
  9.         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.
  10.                
  11.         //do a soft reset
  12.                 //bit7-3 reserver :maintain as '0'
  13.                 //bit2 - 1 reset power managerment circuitry (bit is automatically cleared to ‘0’ by hardware)
  14.                 //bit1 - 1 Reset baseband circuitry (bit is automatically cleared to ‘0’ by hardware)
  15.                 //bit0 - 1 Reset MAC circuitry (bit is automatically cleared to ‘0’ by hardware)
  16.         MRF24_SetShortRAMAddr(WRITE_SOFTRST,0x07);

  17.                 //wait for bit0-2 clear to '0'
  18.         do{
  19.                 i = MRF24_GetShortRAMAddr(READ_SOFTRST);
  20.         }while((i&0x07) != (uint8_t)0x00);       

  21.         i=MRF24_GetShortRAMAddr(READ_ORDER);
  22.         if(i != 0xFF){       
  23.                 printf("MRF24J40 Daughter card not inserted\t\n,0x%02x \t\n",i);
  24.                 return ;//error
  25.         }
  26.         MRF24_SetShortRAMAddr(WRITE_PACON2,0x98);//PACON2 = 0x98, Initialize FFOEN=1 and TXONTS = 0x6
  27.         MRF24_SetShortRAMAddr(WRITE_TXSTBL,0x95);//Initialize RFSTBL = 0x9
  28.         MRF24_SetLongRAMAddr(RFCON0,0x03);//Initialize RFOPT = 0x03, not use
  29.         MRF24_SetLongRAMAddr(RFCON1,0x01);//Initialize VCOOPT=0x02,not use
  30.         MRF24_SetLongRAMAddr(RFCON2,0x80);//Enable PLL
  31.         MRF24_SetLongRAMAddr(RFCON6,0x90);//Initialize TXFIL=1, 20MRECVR=1
  32.         MRF24_SetLongRAMAddr(RFCON7,0x80);//Initialize SLPCLKSEL = 0x2 (100KHz internal oscialltor)
  33.         MRF24_SetLongRAMAddr(RFCON8,0x10);//Initialize RFVCO =1
  34.         MRF24_SetLongRAMAddr(SLPCON0,0x02);//interrupt rising edge
  35.         MRF24_SetLongRAMAddr(SLPCON1,0x21);//Initialize CLKOUTEN=1 and SLPCLKDIV = 0x01
  36.         //deafult Nonbeacon_enabled device,disable slotted csma-ca mode
  37.         //Configuration for nonbeacon-enabled devices
  38.         MRF24_SetShortRAMAddr(WRITE_BBREG2,0x80);//Set CCA mode to ED
  39.         MRF24_SetShortRAMAddr(WRITE_CCAEDTH,0x60);//Set CCA-ED Threshold
  40.         MRF24_SetShortRAMAddr(WRITE_BBREG6,0x40);//Set appended RSSI value to RX FIFO
  41.         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
  42.         //MRF24_SetLongRAMAddr(RFCON3,0x48);//Large Scale Control for TX Power=-10dB,Small Scale Control for TX Power=-0.5dB
  43.         MRF24_SetChannel(CHANNEL_15);//set operating channel as channel 11
  44.        
  45.                 //delay 192us after RF State Machine Reset
  46.    drv_delay_us(200);
  47.          
  48.         do{ //Wait until th RFSTATE machine indicates RX state
  49.                         i = MRF24_GetLongRAMAddr(RFSTATE);
  50.         }while((i&0xA0) != 0xA0);

  51. //Program the short MAC address ,x0ffff
  52.         MRF24_SetShortRAMAddr(WRITE_SADRL,0xFF);
  53.         MRF24_SetShortRAMAddr(WRITE_SADRH,0xFF);
  54.         // Load the short address of the device with 0xffff which means that it will be ignore upon receipt
  55.        
  56.         //Load the PAN address also with 0xfff
  57.         MRF24_SetShortRAMAddr(WRITE_PANIDL,0xFF);
  58.         MRF24_SetShortRAMAddr(WRITE_PANIDH,0xFF);

  59.         //program Long MAC address
  60.         for(i=0;i<(BYTE)8;i++){
  61.                 //load the long address as 07,06,05,04,03,02,01,00
  62.                 MRF24_SetShortRAMAddr(WRITE_EADR0+i*2,myLongAddress[i]);
  63.         }
  64.         // Automatic acknowledgement is not transmitted for received packets by TXMAC
  65.         //ERRpkt mode bit is set to 0,Disables automatic Acknowledgement response
  66.   //Promiscuous packet mode bit is set to 1,Receive all packet types with good CRC
  67.         MRF24_SetShortRAMAddr(WRITE_RXMCR,0x21);
  68. }
复制代码
  1. void MRF24_SendPacket(BYTE header_len, BYTE Frame_len, BYTE* packet){
  2.                 BYTE i;
  3.                 MRF24_SetLongRAMAddr(TX_NORMAL_FIFO,header_len);
  4.                 MRF24_SetLongRAMAddr(TX_NORMAL_FIFO+1,Frame_len);
  5.        
  6.                 for(i=2;i<Frame_len+2;i++){
  7.                         MRF24_SetLongRAMAddr(TX_NORMAL_FIFO+i,packet[i-2]);
  8.                 }printf("--0x%02x",MRF24_GetLongRAMAddr(TX_NORMAL_FIFO+2));
  9.                 MRF24_SetShortRAMAddr(WRITE_TXNCON,0x01);//Transmit the frame in the TX Normal FIFO; bit is automatically cleared by hardware
  10.                
  11. }
复制代码
  1. void radio(){//int main()中执行的函数
  2. //mrf24j40 signal pin configure
  3.         GPIO_InitTypeDef InitStructure;
  4.         //enable pin configure
  5.         RCC_APB2PeriphClockCmd(RESET_CLK|INT_CLK|WAKE_CLK,ENABLE);
  6.         //interrupt pin configure input
  7.         InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  8.         InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  9.         InitStructure.GPIO_Pin         = INT_PIN;
  10.         GPIO_Init(INT_PORT,&InitStructure);
  11. //---out pin
  12.         //RESET pin
  13.         InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  14.         InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  15.         InitStructure.GPIO_Pin  = RESET_PIN;
  16.         GPIO_Init(RESET_PORT,&InitStructure);
  17.        
  18.         //WAKE pin
  19.         InitStructure.GPIO_Pin  = WAKE_PIN;
  20.         GPIO_Init(WAKE_PORT,&InitStructure);
  21.        
  22.         //NVIC  init
  23.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  24.         NVIC_InitTypeDef NVIC_InitStructure;
  25.         NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
  26.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  27.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  28.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  29.         NVIC_Init(&NVIC_InitStructure);
  30.         //EXTI
  31.         GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource5);
  32.         EXTI_InitTypeDef Exti_initstructure;
  33.         Exti_initstructure.EXTI_Line = EXTI_Line5;
  34.         Exti_initstructure.EXTI_Mode = EXTI_Mode_Interrupt;
  35.         Exti_initstructure.EXTI_Trigger = EXTI_Trigger_Rising;
  36.         Exti_initstructure.EXTI_LineCmd = ENABLE;
  37.         EXTI_Init(&Exti_initstructure);
  38.        
  39.         MRF24J40Init();//channel ,rx/tx interrupt and nobeacon-enable device, disable slotted-enable
  40.         LED1_ON;
  41.         drv_delay_500Ms(10);
  42.         LED1_OFF;
  43.         printf("state:0x%02x\t\n",MRF24_GetLongRAMAddr(RFSTATE));
  44.        
  45. #ifdef MRF_TX       
  46.         printf("send start\t");
  47.         while(1){
  48.                 if(MRF24_GetShortRAMAddr(READ_TXSTAT) & 0x20){printf("Channel busy\t\n");continue;}
  49.                 drv_delay_ms(20);
  50.         //        printf("~~0x%02x\t\n",MRF24_GetShortRAMAddr(READ_TXNCON));
  51.         //        MRF24_SendPacket(9,sizeof(txbuff),(BYTE*)txbuff);
  52.                 MRF24_SetLongRAMAddr(TX_NORMAL_FIFO,0x0D);
  53.                 MRF24_SetLongRAMAddr(0x001,0x0D);
  54.                 MRF24_SetLongRAMAddr(0x002,0x01);
  55.                 MRF24_SetLongRAMAddr(0x003,0x04);
  56.                 MRF24_SetLongRAMAddr(0x004,0xC4);
  57.                 MRF24_SetLongRAMAddr(0x005,0xFF);
  58.                 MRF24_SetLongRAMAddr(0x006,0xFF);
  59.                 MRF24_SetLongRAMAddr(0x007,0xFF);
  60.                 MRF24_SetLongRAMAddr(0x008,0xFF);
  61.                 MRF24_SetLongRAMAddr(0x009,0x07);
  62.                 MRF24_SetLongRAMAddr(0x00A,0x00);
  63.                 MRF24_SetLongRAMAddr(0x00B,0x00);
  64.                 MRF24_SetLongRAMAddr(0x00C,0x00);
  65.                 MRF24_SetLongRAMAddr(0x00D,0x00);
  66.                 MRF24_SetLongRAMAddr(0x00E,0xAA);
  67. <font color="#ff0000">                MRF24_SetShortRAMAddr(WRITE_TXNCON,0b00000011);// 本应只设置0b00000001即可实现发送,但现在需要设置两位模块才会产生发送中断</font>
  68.                
  69.                
  70.                 if((MRF24_GetShortRAMAddr(READ_TXSTAT)&0x01)==0){
  71.                 drv_delay_500Ms(2);
  72.                 LED2_TOGGLE;
  73.                 }else{//transmit failed
  74.                         printf("retry:0x%02x---0x%02x\t",MRF24_GetShortRAMAddr(READ_TXSTAT)&0x01,MRF24_GetShortRAMAddr(READ_TXSTAT)&0x60);
  75.                         drv_delay_500Ms(2);
  76.                  LED3_TOGGLE;
  77.                 }
  78.         }
  79. }
复制代码


使用特权

评论回复

相关帖子

| 2021-1-3 23:32 | 显示全部楼层
楼主是否放一下error信息和码会好一点

使用特权

评论回复
 楼主 | 2021-1-4 11:28 | 显示全部楼层
正圆椭圆 发表于 2021-1-3 23:32
楼主是否放一下error信息和码会好一点

我将相关代码题贴上去了,您可以看一下, 刚才的问题已经更新。

使用特权

评论回复
| 2021-2-26 15:45 | 显示全部楼层
建议楼主去st的版块问问,这里是ti 无线相关产品的板块

使用特权

评论回复
扫描二维码,随时随地手机跟帖
您需要登录后才可以回帖 登录 | 注册

本版积分规则

我要发帖 我要提问 投诉建议 申请版主

快速回复

您需要登录后才可以回帖
登录 | 注册
高级模式

论坛热帖

在线客服 快速回复 返回顶部 返回列表