SX126x_board.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*!
  2. * \file sx1262mbxcas-board.c
  3. *
  4. * \brief Target board SX1262MBXCAS shield driver implementation
  5. *
  6. * \copyright Revised BSD License, see section \ref LICENSE.
  7. *
  8. * \code
  9. * ______ _
  10. * / _____) _ | |
  11. * ( (____ _____ ____ _| |_ _____ ____| |__
  12. * \____ \| ___ | (_ _) ___ |/ ___) _ \
  13. * _____) ) ____| | | || |_| ____( (___| | | |
  14. * (______/|_____)_|_|_| \__)_____)\____)_| |_|
  15. * (C)2013-2017 Semtech
  16. *
  17. * \endcode
  18. *
  19. * \author Miguel Luis ( Semtech )
  20. *
  21. * \author Gregory Cristian ( Semtech )
  22. */
  23. #include <stdlib.h>
  24. #include "project_config.h"
  25. #include "delay.h"
  26. #include "radio.h"
  27. #include "SX126x_board.h"
  28. #include "SX126x_SPI.h"
  29. #include "stdio.h"
  30. extern volatile uint32_t TickCounter;
  31. typedef struct __softTimer_s{
  32. uint8_t isRun; //0停止,1运行
  33. uint32_t delayMs; //定时器需要多长时间后唤醒
  34. uint32_t startMs; //定时器开始时间
  35. }SoftTimer_t;
  36. //static DioIrqHandler *dio1IrqCallback=NULL; //记录DIO1的回调函数句柄
  37. static uint32_t timer_count=0;
  38. static SoftTimer_t txTimerHandle,rxTimerHandle;
  39. /* 初始化sx126x需要用到的GPIO初始化,将 BUSY 引脚设置为输入模式
  40. */
  41. void SX126xIoInit( void )
  42. {
  43. uint8_t u8_ret=255;
  44. GPIO_InitTypeDef GPIO_InitStructure;
  45. //初始化BUSY为上拉输入模式
  46. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOE, ENABLE); //使能指定端口时钟
  47. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz,这里不用传参,直接写死用最大速度50MHZ
  48. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;//上拉输入
  49. GPIO_InitStructure.GPIO_Pin = RADIO_DIO4_BUSY_PIN;
  50. GPIO_Init(RADIO_DIO4_BUSY_PORT, &GPIO_InitStructure); //初始化GPIO
  51. //下面这几个引脚没用用到,设置为浮空输入模式
  52. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//浮空输入
  53. GPIO_InitStructure.GPIO_Pin = RADIO_DIO0_TXEN_PIN;
  54. GPIO_Init(RADIO_DIO0_TXEN_PORT, &GPIO_InitStructure); //初始化GPIO
  55. GPIO_InitStructure.GPIO_Pin = RADIO_DIO2_PIN;
  56. GPIO_Init(RADIO_DIO2_PORT, &GPIO_InitStructure); //初始化GPIO
  57. GPIO_InitStructure.GPIO_Pin = RADIO_DIO3_PIN;
  58. GPIO_Init(RADIO_DIO3_PORT, &GPIO_InitStructure); //初始化GPIO
  59. GPIO_InitStructure.GPIO_Pin = RADIO_DIO5_RXEN_PIN;
  60. GPIO_Init(RADIO_DIO5_RXEN_PORT, &GPIO_InitStructure); //初始化GPIO
  61. u8_ret=Spi1Init();
  62. if(u8_ret){
  63. printf("spi1 init error\r\n");
  64. }else{
  65. printf("SX126xIoInit init sucess\r\n");
  66. }
  67. }
  68. /* 初始化 DIO1
  69. * 将DIO1设置为外部中断(上升沿触发),并且回调函数为 dioIrq 函数原型 void RadioOnDioIrq( void* context )
  70. */
  71. void SX126xIoIrqInit( DioIrqHandler dioIrq )
  72. {
  73. GPIO_InitTypeDef GPIO_InitStructure;
  74. EXTI_InitTypeDef EXTI_InitStructure;
  75. NVIC_InitTypeDef NVIC_InitStructure;
  76. // dio1IrqCallback=dioIrq;
  77. //gpio(DIO1)初始化为下拉输入模式
  78. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOB, ENABLE); //使能指定端口时钟
  79. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz,这里不用传参,直接写死用最大速度50MHZ
  80. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;//下拉输入
  81. GPIO_InitStructure.GPIO_Pin = RADIO_DIO1_PIN;
  82. GPIO_Init(RADIO_DIO1_PORT, &GPIO_InitStructure); //初始化GPIO
  83. //中断配置
  84. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //使能复用功能时钟
  85. //GPIOE.2 中断线以及中断初始化配置 上升沿触发
  86. GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource8);
  87. EXTI_InitStructure.EXTI_Line=EXTI_Line8;
  88. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  89. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //上升沿触发
  90. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  91. EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
  92. //设置中断优先级
  93. NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //使能按键WK_UP所在的外部中断通道
  94. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00; //抢占优先级2,
  95. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; //子优先级3
  96. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
  97. NVIC_Init(&NVIC_InitStructure);
  98. EXTI_ClearITPendingBit(EXTI_Line8); //清除LINE11上的中断标志位
  99. }
  100. void SX126xIoDeInit( void )
  101. {
  102. //GPIO去初始化代码
  103. }
  104. //复位按键功能
  105. void SX126xReset( void )
  106. {
  107. GPIO_InitTypeDef GPIO_InitStructure;
  108. SX126xDelayMs( 10 );
  109. //将RST输出低电平
  110. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能指定端口时钟
  111. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz,这里不用传参,直接写死用最大速度50MHZ
  112. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //推挽输出
  113. GPIO_InitStructure.GPIO_Pin = RADIO_nRESET_PIN;
  114. GPIO_Init(RADIO_nRESET_PORT, &GPIO_InitStructure); //初始化GPIO
  115. GPIO_WriteBit( RADIO_nRESET_PORT, RADIO_nRESET_PIN,Bit_RESET); //RST设为低电平输出
  116. SX126xDelayMs( 20 );
  117. //将RST设置为上拉输入模式
  118. GPIO_WriteBit( RADIO_nRESET_PORT, RADIO_nRESET_PIN,Bit_SET); //RST设为低电平输出
  119. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
  120. GPIO_Init(RADIO_nRESET_PORT, &GPIO_InitStructure); //初始化GPIO
  121. SX126xDelayMs( 10 );
  122. }
  123. //读取Busy引脚电平状态
  124. void SX126xWaitOnBusy( void )
  125. {
  126. uint32_t u32_count=0;
  127. while( GPIO_ReadInputDataBit(RADIO_DIO4_BUSY_PORT,RADIO_DIO4_BUSY_PIN) == 1 ){
  128. if(u32_count++>1000){
  129. printf("wait busy pin timeout\r\n");
  130. u32_count=0;
  131. }
  132. SX126xDelayMs(1);
  133. }
  134. }
  135. //设置SPI的NSS引脚电平,0低电平,非零高电平
  136. void SX126xSetNss(uint8_t lev ){
  137. if(lev){
  138. GPIO_SetBits(RADIO_NSS_PORT,RADIO_NSS_PIN); //输出高电平
  139. }else{
  140. GPIO_ResetBits(RADIO_NSS_PORT,RADIO_NSS_PIN); //输出低电平
  141. }
  142. }
  143. //spi传输一个字节的数据
  144. uint8_t SX126xSpiInOut(uint8_t data){
  145. return HALSpi1InOut(data);
  146. }
  147. //检查频率是否符合要求,如果不需要判断则可以直接返回true
  148. bool SX126xCheckRfFrequency( uint32_t frequency )
  149. {
  150. // Implement check. Currently all frequencies are supported
  151. return true;
  152. }
  153. //毫秒延时
  154. void SX126xDelayMs(uint32_t ms){
  155. delay_ms(ms);
  156. }
  157. //tx/rx定时器操作
  158. //定时器3中断服务程序
  159. //tx定时结束需要回调 RadioOnTxTimeoutIrq
  160. //rx定时结束需要回调 RadioOnRxTimeoutIrq
  161. void TIM3_IRQHandler(void) //TIM3中断
  162. {
  163. uint32_t diffMs=0;
  164. if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET){
  165. timer_count++;
  166. TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIMx更新中断标志
  167. //printf("timer_count=%d\r\n",timer_count);
  168. //处理tx定时器
  169. if(txTimerHandle.isRun){
  170. if(timer_count>=txTimerHandle.startMs){
  171. diffMs=timer_count-txTimerHandle.startMs;
  172. }else{//溢出了
  173. diffMs=0xffffffff - txTimerHandle.startMs + timer_count;
  174. }
  175. if(diffMs>txTimerHandle.delayMs){
  176. //计时结束
  177. //printf("----------------- tx timer irq ---------------\r\n");
  178. SX126xTxTimerStop();
  179. txTimerHandle.startMs=timer_count;
  180. RadioOnTxTimeoutIrq(NULL); //tx定时结束需要回调 RadioOnTxTimeoutIrq
  181. }
  182. }
  183. //处理rx定时器
  184. if(rxTimerHandle.isRun){
  185. if(timer_count>=rxTimerHandle.startMs){
  186. diffMs=timer_count-rxTimerHandle.startMs;
  187. }else{//溢出了
  188. diffMs=0xffffffff - rxTimerHandle.startMs + timer_count;
  189. }
  190. if(diffMs>rxTimerHandle.delayMs){
  191. //计时结束
  192. //printf("----------------- rx timer irq ---------------\r\n");
  193. SX126xRxTimerStop();
  194. rxTimerHandle.startMs=timer_count;
  195. RadioOnRxTimeoutIrq(NULL); //rx定时结束需要回调 RadioOnRxTimeoutIrq
  196. }
  197. }
  198. }
  199. }
  200. //初始化定时器(这里用TIM3定时器模拟出了两个ms定时器)
  201. //tx定时结束需要回调 RadioOnTxTimeoutIrq
  202. //rx定时结束需要回调 RadioOnRxTimeoutIrq
  203. void SX126xTimerInit(void){
  204. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  205. NVIC_InitTypeDef NVIC_InitStructure;
  206. //使能TIM3
  207. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能
  208. //TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断
  209. //中断优先级NVIC设置
  210. NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
  211. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //先占优先级0级
  212. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
  213. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
  214. NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
  215. //定时器TIM3初始化
  216. TIM_TimeBaseStructure.TIM_Period = 10; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
  217. TIM_TimeBaseStructure.TIM_Prescaler =SystemCoreClock/10000; //设置用来作为TIMx时钟频率除数的预分频值(1s10000次,也就是1次0.1ms)
  218. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
  219. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
  220. TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
  221. //关闭软件定时器
  222. txTimerHandle.isRun=0;
  223. rxTimerHandle.isRun=0;
  224. TIM_Cmd(TIM3, ENABLE); //使能TIMx
  225. TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIMx更新中断标志
  226. TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM2中断,允许更新中断
  227. }
  228. void SX126xSetTxTimerValue(uint32_t nMs){
  229. //printf("[%s()-%d]set timer out %d ms\r\n",__func__,__LINE__,nMs);
  230. txTimerHandle.delayMs=nMs;
  231. }
  232. void SX126xTxTimerStart(void){
  233. //printf("[%s()-%d]start timer\r\n",__func__,__LINE__);
  234. txTimerHandle.startMs=timer_count;
  235. txTimerHandle.isRun=1;
  236. }
  237. void SX126xTxTimerStop(void){
  238. //printf("[%s()-%d]stop timer\r\n",__func__,__LINE__);
  239. txTimerHandle.isRun=0;
  240. }
  241. void SX126xSetRxTimerValue(uint32_t nMs){
  242. //printf("[%s()-%d]set timer out %d ms\r\n",__func__,__LINE__,nMs);
  243. rxTimerHandle.delayMs=nMs;
  244. }
  245. void SX126xRxTimerStart(void){
  246. //printf("[%s()-%d]start timer\r\n",__func__,__LINE__);
  247. rxTimerHandle.startMs=timer_count;
  248. rxTimerHandle.isRun=1;
  249. }
  250. void SX126xRxTimerStop(void){
  251. //printf("[%s()-%d]stop timer\r\n",__func__,__LINE__);
  252. rxTimerHandle.isRun=0;
  253. }