fml_single_line_simplex.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. /**
  2. 通讯线平时空闲时处于低电平,由一个100MS的高电平作为引导码来唤醒从机。然后发送一个字节串行数据。完毕后通讯线恢复到空闲状态的低电平。
  3. 对于从机来说,接受完8位数据,或检测到一个连续5t的低电平(结束码)即可认为通讯结束。
  4. 在引导码之后,数据码之前,增加一位波特率校准位。该校准位由一个低电平和高电平构成,低电平和高电平分别 =1t。在从机被唤醒后,检测到引导码结束后,先
  5. 对校准位进行时间测量。为后续数据码的识别提供标准时间(“t”值)。
  6. 数据码共8位(低位bit0先发);每位数据码由一个低电平和一个高电平组成;低电平时间固定=1t;高电平时间=1t代表逻辑‘0’,=3t代表逻辑‘1’;
  7. 结束码为一个持续时间 >= 5t的低电平。检测到结束码后通讯结束。
  8. */
  9. /*Includes ----------------------------------------------*/
  10. #include "tool.h"
  11. #include "bsp_pwm.h"
  12. #include "fml_single_line_simplex.h"
  13. /*Private macro ------------------------------------------------*/
  14. #define FML_SINGLE_LINE_SIMPLEX_CONFIG_PIN_OUTPUT do \
  15. { \
  16. nrfx_gpiote_out_uninit(FML_SINGLE_LINE_SIMPLEX_PIN); \
  17. nrfx_gpiote_in_uninit(FML_SINGLE_LINE_SIMPLEX_PIN); \
  18. nrfx_gpiote_out_config_t out_config = GPIOTE_CONFIG_OUT_SIMPLE(0); \
  19. APP_ERROR_CHECK(nrfx_gpiote_out_init(FML_SINGLE_LINE_SIMPLEX_PIN, &out_config)); \
  20. nrf_gpio_cfg_watcher(FML_SINGLE_LINE_SIMPLEX_PIN); \
  21. }while(0) //配置引脚为输出,且初始化为低电平
  22. #define FML_SINGLE_LINE_SIMPLEX_SET_PIN nrfx_gpiote_out_set(FML_SINGLE_LINE_SIMPLEX_PIN) //设置IO引脚高电平
  23. #define FML_SINGLE_LINE_SIMPLEX_CLEAR_PIN nrfx_gpiote_out_clear(FML_SINGLE_LINE_SIMPLEX_PIN) //设置IO引脚低电平
  24. #define FML_SINGLE_LINE_SIMPLEX_GET_PIN(X) do \
  25. { \
  26. if(nrfx_gpiote_in_is_set(FML_SINGLE_LINE_SIMPLEX_PIN)) \
  27. X = 1; \
  28. else \
  29. X = 0; \
  30. }while(0) //读取IO引脚高低电平
  31. #define FML_SINGLE_LINE_SIMPLEX_CONFIG_PIN_INPUT_INTERRUPT_H_PULL 0 //主机中断接收上下拉配置
  32. #define FML_SINGLE_LINE_SIMPLEX_CONFIG_PIN_INPUT_INTERRUPT_S_PULL 1 //从机中断接收上下拉配置
  33. #define FML_SINGLE_LINE_SIMPLEX_CONFIG_PIN_INPUT_INTERRUPT(HANDLER,X) do \
  34. { \
  35. nrfx_gpiote_out_uninit(FML_SINGLE_LINE_SIMPLEX_PIN); \
  36. nrfx_gpiote_in_uninit(FML_SINGLE_LINE_SIMPLEX_PIN); \
  37. nrfx_gpiote_in_config_t in_config; \
  38. in_config.sense = NRF_GPIOTE_POLARITY_TOGGLE; \
  39. if(X == 0)in_config.pull = NRF_GPIO_PIN_NOPULL; \
  40. if(X == 1)in_config.pull = NRF_GPIO_PIN_PULLUP; \
  41. if(X == 2)in_config.pull = NRF_GPIO_PIN_PULLDOWN; \
  42. in_config.is_watcher = false; \
  43. in_config.hi_accuracy = true; \
  44. in_config.skip_gpio_setup = false; \
  45. APP_ERROR_CHECK(nrfx_gpiote_in_init(FML_SINGLE_LINE_SIMPLEX_PIN, &in_config, HANDLER)); \
  46. nrfx_gpiote_in_event_enable(FML_SINGLE_LINE_SIMPLEX_PIN, true); \
  47. }while(0) //设置IO引脚为输入高低电平中断
  48. #define FML_SINGLE_LINE_SIMPLEX_RECEIVE_MAX_LEN 1024 //接收缓存的最大字节长度
  49. #define FML_SINGLE_LINE_SIMPLEX_APPROX_EQUAL_TO(A,B,ERROR,BOOL) do \
  50. { \
  51. uint32_t error; \
  52. if(A > B){error = A - B;if(error <= ERROR)BOOL = true;else BOOL = false;} \
  53. else {error = B - A;if(error <= ERROR)BOOL = true;else BOOL = false;} \
  54. }while(0) //判断俩个数是否约等于
  55. #define FML_SINGLE_LINE_SIMPLEX_WAIT_TIMES(US) do \
  56. { \
  57. uint32_t tim1; \
  58. uint32_t tim2; \
  59. uint32_t distance; \
  60. tim1 = NRF_RTC0->COUNTER; \
  61. do \
  62. { \
  63. tim2 = NRF_RTC0->COUNTER; \
  64. if(tim2 < tim1)tim2 += 16777216; \
  65. distance = (tim2 - tim1)/32.768 * 1000; \
  66. }while(distance < US); \
  67. }while(0) //等待时间 单位:us
  68. #define FML_SINGLE_LINE_SIMPLEX_SEND_ONE_BIT(X) do \
  69. { \
  70. FML_SINGLE_LINE_SIMPLEX_CLEAR_PIN; \
  71. FML_SINGLE_LINE_SIMPLEX_WAIT_TIMES(FML_SINGLE_LINE_SIMPLEX_CALIBRATION_CODE_T); \
  72. if(X == 1) \
  73. FML_SINGLE_LINE_SIMPLEX_SET_PIN; \
  74. else \
  75. FML_SINGLE_LINE_SIMPLEX_CLEAR_PIN; \
  76. }while(0) //发送一个位
  77. #define FML_SINGLE_LINE_SIMPLEX_GET_TIMESTAMP(X) X = NRF_RTC0->COUNTER //获取时间戳
  78. #define FML_SINGLE_LINE_SIMPLEX_GET_DISTANCE_FOR_TIMESTAMP(TIMESTAMP,US) do \
  79. { \
  80. uint32_t tim = NRF_RTC0->COUNTER; \
  81. if(tim < TIMESTAMP) tim += 16777216; \
  82. US = (tim - TIMESTAMP)/32.768 * 1000; \
  83. }while(0) //获取与时间戳的差 单位:us
  84. #define FML_SINGLE_LINE_SIMPLEX_PWM_SEND_ONE_BIT_TIEMS (FML_SINGLE_LINE_SIMPLEX_SEND_ONE_BIT_TIEMS/8) //PWM发送一位的时间 单位:us 1 -> 8us 125 -> 1ms 3750 -> 30ms
  85. #define FML_SINGLE_LINE_SIMPLEX_PWM_SEQ_VALUES_LEN ((FML_SINGLE_LINE_SIMPLEX_CALIBRATION_CODE_T + FML_SINGLE_LINE_SIMPLEX_BOOT_CODE_T + \
  86. 2*FML_SINGLE_LINE_SIMPLEX_CALIBRATION_CODE_T + \
  87. 8*FML_SINGLE_LINE_SIMPLEX_CALIBRATION_CODE_T + 8*FML_SINGLE_LINE_SIMPLEX_HIGHT_LEVEL_T + \
  88. FML_SINGLE_LINE_SIMPLEX_CALIBRATION_CODE_T + FML_SINGLE_LINE_SIMPLEX_HIGHT_LEVEL_T + \
  89. FML_SINGLE_LINE_SIMPLEX_END_CODE_T*2) / FML_SINGLE_LINE_SIMPLEX_SEND_ONE_BIT_TIEMS \
  90. ) //PWM发送一个字节的序列大小,END_CODE*2是为了余量。
  91. #define FML_SINGLE_LINE_SIMPLEX_PWM_LEVEL_HIGHT 0 //PWM高电平
  92. #define FML_SINGLE_LINE_SIMPLEX_PWM_LEVEL_LOW 0x8000 //PWM低电平
  93. #define FML_SINGLE_LINE_SIMPLEX_PWM_WAIT_TIMES(SEQ_VALUES,INDEX,COUNT,X) do \
  94. { \
  95. uint16_t level; \
  96. if(X==1)level = FML_SINGLE_LINE_SIMPLEX_PWM_LEVEL_HIGHT;else level = FML_SINGLE_LINE_SIMPLEX_PWM_LEVEL_LOW; \
  97. for(int i = 0; i < COUNT; i++)SEQ_VALUES[INDEX++] =level; \
  98. }while(0) //PWM等待时间转为序列 单位:us
  99. /*STRUCTION -----------------------------------------------------*/
  100. typedef struct _fml_single_line_simplex
  101. {
  102. FML_SINGLE_LINE_SIMPLEX_ROLE_e role; //角色
  103. bool start_receive; //开始接收标志位
  104. uint8_t receive_bit_num; //接收到的字节当前位
  105. volatile unsigned char* RxW; //接收缓存区写指针
  106. volatile unsigned char* RxR; //接收缓存区读指针
  107. uint8_t receive_buff[FML_SINGLE_LINE_SIMPLEX_RECEIVE_MAX_LEN]; //接收缓存区
  108. fml_single_line_half_duplex_receive_cb receive_cb; //接收回调
  109. uint32_t pwm_pin_channel[4]; //PWM通道引脚,每个PWM模块只支持4个通道
  110. uint16_t pwm_cycle_time; //PWM周期时间
  111. uint32_t pwm_mode; //PWM模式
  112. pwm_values_common_t fml_single_line_simplex_pwm_seq_values[FML_SINGLE_LINE_SIMPLEX_PWM_SEQ_VALUES_LEN]; //PWM序列值
  113. nrf_pwm_sequence_t *fml_single_line_simplex_pwm_seq; //PWM序列指针
  114. bool pwm_transfer_is_done; //PWM传输是否完成标志
  115. } Fml_Single_Line_Simplex_t;
  116. /*Local Variable ----------------------------------------------*/
  117. static Fml_Single_Line_Simplex_t ob_fml_single_line_simplex;
  118. /*Local Functions ----------------------------------------------*/
  119. static void fml_single_line_receive_buff_push(uint8_t *p, int len)
  120. {
  121. volatile unsigned char *W=ob_fml_single_line_simplex.RxW; //这里要与上面指针相同
  122. if(len<=0) return;
  123. for(int i=0;i<len;i++){
  124. W=ob_fml_single_line_simplex.RxW+1;
  125. if(W>=ob_fml_single_line_simplex.receive_buff+FML_SINGLE_LINE_SIMPLEX_RECEIVE_MAX_LEN) W=ob_fml_single_line_simplex.receive_buff; //取下一位置(到顶转到底)
  126. if(W!=ob_fml_single_line_simplex.RxR){*ob_fml_single_line_simplex.RxW=*(p+i); ob_fml_single_line_simplex.RxW=W;}
  127. else break;
  128. }
  129. }
  130. static unsigned int fml_single_line_receive_CheckLen(void) //检查RX接收了多少数据
  131. {
  132. unsigned int Len; //short
  133. volatile unsigned char *W=ob_fml_single_line_simplex.RxW;
  134. volatile unsigned char *R=ob_fml_single_line_simplex.RxR;
  135. if(W>=R)Len=W-R;else Len=(W+FML_SINGLE_LINE_SIMPLEX_RECEIVE_MAX_LEN)-R; //这样正确(中途中断改变也变不了结果)
  136. return Len;
  137. }
  138. static uint8_t fml_single_line_receive_buff_pop(void)
  139. {
  140. unsigned char R=*ob_fml_single_line_simplex.RxR; //读数
  141. if(ob_fml_single_line_simplex.RxR!=ob_fml_single_line_simplex.RxW)
  142. {
  143. if(ob_fml_single_line_simplex.RxR+1>=(ob_fml_single_line_simplex.receive_buff+FML_SINGLE_LINE_SIMPLEX_RECEIVE_MAX_LEN))
  144. ob_fml_single_line_simplex.RxR = ob_fml_single_line_simplex.receive_buff;
  145. else
  146. ob_fml_single_line_simplex.RxR++;
  147. }//下标
  148. return R;
  149. }
  150. static void fml_single_line_in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
  151. {
  152. static uint8_t receive_byte = 0;
  153. static uint8_t flow = 0;
  154. static uint32_t timestamp,last_timestamp;
  155. static uint32_t distance;
  156. static uint32_t calibration_compensate = FML_SINGLE_LINE_SIMPLEX_CALIBRATION_CODE_T / 2; //若线太长,拉高拉低时间过长,导致校验码时间与设定好的校验码时间不一致,初始设定能接受的延迟为校准码的一半
  157. static int Parity_Check;
  158. uint32_t level;
  159. bool condition;
  160. FML_SINGLE_LINE_SIMPLEX_GET_PIN(level);
  161. // //test-----------------
  162. // JS_RTT_Print(level,0,0);
  163. // return;
  164. // //test-----------------
  165. // SEGGER_RTT_printf(0,"level:%d\n",level);
  166. switch(flow)
  167. {
  168. case 0: //接收引导码阶段
  169. if(level == 1)
  170. {
  171. //这样子读取减少误差产生---------->
  172. last_timestamp = timestamp;
  173. FML_SINGLE_LINE_SIMPLEX_GET_TIMESTAMP(timestamp);
  174. FML_SINGLE_LINE_SIMPLEX_GET_DISTANCE_FOR_TIMESTAMP(last_timestamp,distance);
  175. //<----------这样子读取减少误差产生
  176. if(timestamp !=0 && last_timestamp != 0)
  177. {
  178. if(distance >= FML_SINGLE_LINE_SIMPLEX_END_CODE_T) //结束码错误
  179. {
  180. flow = 1;
  181. }
  182. else
  183. {
  184. flow = 0;
  185. }
  186. }
  187. else
  188. {
  189. flow = 1; //第一次运行
  190. }
  191. ob_fml_single_line_simplex.start_receive = false;
  192. ob_fml_single_line_simplex.receive_bit_num = 0;
  193. receive_byte = 0;
  194. }
  195. break;
  196. case 1: //接收校准码阶段
  197. if(level == 0)
  198. {
  199. //这样子读取减少误差产生---------->
  200. last_timestamp = timestamp;
  201. FML_SINGLE_LINE_SIMPLEX_GET_TIMESTAMP(timestamp);
  202. FML_SINGLE_LINE_SIMPLEX_GET_DISTANCE_FOR_TIMESTAMP(last_timestamp,distance);
  203. //<----------这样子读取减少误差产生
  204. FML_SINGLE_LINE_SIMPLEX_APPROX_EQUAL_TO(distance,FML_SINGLE_LINE_SIMPLEX_BOOT_CODE_T,calibration_compensate,condition);
  205. if(!condition)
  206. {
  207. flow = 0; //引导码错误
  208. }
  209. }
  210. else
  211. {
  212. FML_SINGLE_LINE_SIMPLEX_GET_TIMESTAMP(timestamp);
  213. flow = 2;
  214. }
  215. break;
  216. case 2: //接收数据码阶段
  217. if(level == 0)
  218. {
  219. //这样子读取减少误差产生---------->
  220. last_timestamp = timestamp;
  221. FML_SINGLE_LINE_SIMPLEX_GET_TIMESTAMP(timestamp);
  222. FML_SINGLE_LINE_SIMPLEX_GET_DISTANCE_FOR_TIMESTAMP(last_timestamp,distance);
  223. //<----------这样子读取减少误差产生
  224. if(ob_fml_single_line_simplex.start_receive == false)
  225. {
  226. FML_SINGLE_LINE_SIMPLEX_APPROX_EQUAL_TO(distance,FML_SINGLE_LINE_SIMPLEX_CALIBRATION_CODE_T,calibration_compensate,condition);
  227. if(condition)
  228. {
  229. ob_fml_single_line_simplex.start_receive = true; //校准码无误
  230. Parity_Check = 0;
  231. }
  232. else
  233. {
  234. flow = 0;
  235. }
  236. }
  237. else
  238. {
  239. FML_SINGLE_LINE_SIMPLEX_APPROX_EQUAL_TO(distance,FML_SINGLE_LINE_SIMPLEX_HIGHT_LEVEL_T,calibration_compensate,condition);
  240. if(condition){receive_byte |= (0x01 << ob_fml_single_line_simplex.receive_bit_num);Parity_Check++;}
  241. else receive_byte &= ~(0x01 << ob_fml_single_line_simplex.receive_bit_num);
  242. ob_fml_single_line_simplex.receive_bit_num++;
  243. if(ob_fml_single_line_simplex.receive_bit_num == 8)
  244. {
  245. flow = 3;
  246. }
  247. }
  248. }
  249. else
  250. {
  251. FML_SINGLE_LINE_SIMPLEX_GET_TIMESTAMP(timestamp);
  252. }
  253. break;
  254. case 3: //接收奇偶校验码阶段
  255. if(level == 1)
  256. {
  257. FML_SINGLE_LINE_SIMPLEX_GET_TIMESTAMP(timestamp);
  258. }
  259. else
  260. {
  261. //这样子读取减少误差产生---------->
  262. last_timestamp = timestamp;
  263. FML_SINGLE_LINE_SIMPLEX_GET_TIMESTAMP(timestamp);
  264. FML_SINGLE_LINE_SIMPLEX_GET_DISTANCE_FOR_TIMESTAMP(last_timestamp,distance);
  265. //<----------这样子读取减少误差产生
  266. if(ob_fml_single_line_simplex.start_receive == true && ob_fml_single_line_simplex.receive_bit_num == 8) //接收结束码阶段
  267. {
  268. FML_SINGLE_LINE_SIMPLEX_APPROX_EQUAL_TO(distance,FML_SINGLE_LINE_SIMPLEX_HIGHT_LEVEL_T,calibration_compensate,condition);
  269. if(condition)
  270. {
  271. if((Parity_Check % 2) != 0) //奇校验通过
  272. {
  273. //接收字节
  274. fml_single_line_receive_buff_push(&receive_byte, 1);
  275. //接收回调
  276. if(ob_fml_single_line_simplex.receive_cb != NULL)ob_fml_single_line_simplex.receive_cb();
  277. }
  278. }
  279. else
  280. {
  281. if((Parity_Check % 2) == 0) //偶校验通过
  282. {
  283. //接收字节
  284. fml_single_line_receive_buff_push(&receive_byte, 1);
  285. //接收回调
  286. if(ob_fml_single_line_simplex.receive_cb != NULL)ob_fml_single_line_simplex.receive_cb();
  287. }
  288. }
  289. }
  290. flow = 0;
  291. }
  292. break;
  293. }
  294. }
  295. static int fml_single_line_transfer_one_byte(uint8_t value)
  296. {
  297. uint8_t bit;
  298. int Parity_Check;
  299. //发送引导码
  300. FML_SINGLE_LINE_SIMPLEX_CLEAR_PIN;
  301. FML_SINGLE_LINE_SIMPLEX_WAIT_TIMES(FML_SINGLE_LINE_SIMPLEX_CALIBRATION_CODE_T);
  302. FML_SINGLE_LINE_SIMPLEX_SET_PIN;
  303. FML_SINGLE_LINE_SIMPLEX_WAIT_TIMES(FML_SINGLE_LINE_SIMPLEX_BOOT_CODE_T);
  304. //发送校准码
  305. FML_SINGLE_LINE_SIMPLEX_SEND_ONE_BIT(1);
  306. FML_SINGLE_LINE_SIMPLEX_WAIT_TIMES(FML_SINGLE_LINE_SIMPLEX_CALIBRATION_CODE_T);
  307. //发送数据码
  308. Parity_Check = 0;
  309. for(int i=0; i<8;i++)
  310. {
  311. bit = ((value >> i)&0x01);
  312. FML_SINGLE_LINE_SIMPLEX_SEND_ONE_BIT(1);
  313. if(bit == 1){FML_SINGLE_LINE_SIMPLEX_WAIT_TIMES(FML_SINGLE_LINE_SIMPLEX_HIGHT_LEVEL_T);Parity_Check++;}
  314. else FML_SINGLE_LINE_SIMPLEX_WAIT_TIMES(FML_SINGLE_LINE_SIMPLEX_LOW_LEVEL_T);
  315. }
  316. //发送奇偶校验码
  317. FML_SINGLE_LINE_SIMPLEX_SEND_ONE_BIT(1);
  318. if(Parity_Check % 2 == 0)FML_SINGLE_LINE_SIMPLEX_WAIT_TIMES(FML_SINGLE_LINE_SIMPLEX_LOW_LEVEL_T);
  319. else FML_SINGLE_LINE_SIMPLEX_WAIT_TIMES(FML_SINGLE_LINE_SIMPLEX_HIGHT_LEVEL_T);
  320. //发送结束码
  321. FML_SINGLE_LINE_SIMPLEX_CLEAR_PIN;
  322. FML_SINGLE_LINE_SIMPLEX_WAIT_TIMES(FML_SINGLE_LINE_SIMPLEX_END_CODE_T);
  323. return 0;
  324. }
  325. static int fml_single_line_pwm_transfer_one_byte(uint8_t value)
  326. {
  327. uint8_t bit;
  328. int Parity_Check;
  329. int index;
  330. //清空序列值
  331. for(int i=0; i<FML_SINGLE_LINE_SIMPLEX_PWM_SEQ_VALUES_LEN; i++)ob_fml_single_line_simplex.fml_single_line_simplex_pwm_seq_values[i] = FML_SINGLE_LINE_SIMPLEX_PWM_LEVEL_LOW;
  332. //发送引导码
  333. index = 1;
  334. FML_SINGLE_LINE_SIMPLEX_PWM_WAIT_TIMES(ob_fml_single_line_simplex.fml_single_line_simplex_pwm_seq_values, \
  335. index, \
  336. (FML_SINGLE_LINE_SIMPLEX_BOOT_CODE_T/FML_SINGLE_LINE_SIMPLEX_SEND_ONE_BIT_TIEMS), \
  337. 1);
  338. //发送校准码
  339. FML_SINGLE_LINE_SIMPLEX_PWM_WAIT_TIMES(ob_fml_single_line_simplex.fml_single_line_simplex_pwm_seq_values, \
  340. index, \
  341. (FML_SINGLE_LINE_SIMPLEX_CALIBRATION_CODE_T/FML_SINGLE_LINE_SIMPLEX_SEND_ONE_BIT_TIEMS), \
  342. 0);
  343. FML_SINGLE_LINE_SIMPLEX_PWM_WAIT_TIMES(ob_fml_single_line_simplex.fml_single_line_simplex_pwm_seq_values, \
  344. index, \
  345. (FML_SINGLE_LINE_SIMPLEX_CALIBRATION_CODE_T/FML_SINGLE_LINE_SIMPLEX_SEND_ONE_BIT_TIEMS), \
  346. 1);
  347. //发送数据码
  348. Parity_Check = 0;
  349. for(int i=0; i<8;i++)
  350. {
  351. bit = ((value >> i)&0x01);
  352. FML_SINGLE_LINE_SIMPLEX_PWM_WAIT_TIMES(ob_fml_single_line_simplex.fml_single_line_simplex_pwm_seq_values, \
  353. index, \
  354. (FML_SINGLE_LINE_SIMPLEX_CALIBRATION_CODE_T/FML_SINGLE_LINE_SIMPLEX_SEND_ONE_BIT_TIEMS), \
  355. 0);
  356. if(bit == 1)
  357. {
  358. FML_SINGLE_LINE_SIMPLEX_PWM_WAIT_TIMES(ob_fml_single_line_simplex.fml_single_line_simplex_pwm_seq_values, \
  359. index, \
  360. (FML_SINGLE_LINE_SIMPLEX_HIGHT_LEVEL_T/FML_SINGLE_LINE_SIMPLEX_SEND_ONE_BIT_TIEMS), \
  361. 1);
  362. Parity_Check++;
  363. }
  364. else
  365. {
  366. FML_SINGLE_LINE_SIMPLEX_PWM_WAIT_TIMES(ob_fml_single_line_simplex.fml_single_line_simplex_pwm_seq_values, \
  367. index, \
  368. (FML_SINGLE_LINE_SIMPLEX_LOW_LEVEL_T/FML_SINGLE_LINE_SIMPLEX_SEND_ONE_BIT_TIEMS), \
  369. 1);
  370. }
  371. }
  372. //发送奇偶校验码
  373. FML_SINGLE_LINE_SIMPLEX_PWM_WAIT_TIMES(ob_fml_single_line_simplex.fml_single_line_simplex_pwm_seq_values, \
  374. index, \
  375. (FML_SINGLE_LINE_SIMPLEX_CALIBRATION_CODE_T/FML_SINGLE_LINE_SIMPLEX_SEND_ONE_BIT_TIEMS), \
  376. 0);
  377. if(Parity_Check % 2 == 0)
  378. {
  379. FML_SINGLE_LINE_SIMPLEX_PWM_WAIT_TIMES(ob_fml_single_line_simplex.fml_single_line_simplex_pwm_seq_values, \
  380. index, \
  381. (FML_SINGLE_LINE_SIMPLEX_LOW_LEVEL_T/FML_SINGLE_LINE_SIMPLEX_SEND_ONE_BIT_TIEMS), \
  382. 1);
  383. }
  384. else
  385. {
  386. FML_SINGLE_LINE_SIMPLEX_PWM_WAIT_TIMES(ob_fml_single_line_simplex.fml_single_line_simplex_pwm_seq_values, \
  387. index, \
  388. (FML_SINGLE_LINE_SIMPLEX_HIGHT_LEVEL_T/FML_SINGLE_LINE_SIMPLEX_SEND_ONE_BIT_TIEMS), \
  389. 1);
  390. }
  391. //发送结束码
  392. FML_SINGLE_LINE_SIMPLEX_PWM_WAIT_TIMES(ob_fml_single_line_simplex.fml_single_line_simplex_pwm_seq_values, \
  393. index, \
  394. (FML_SINGLE_LINE_SIMPLEX_END_CODE_T/FML_SINGLE_LINE_SIMPLEX_SEND_ONE_BIT_TIEMS), \
  395. 0);
  396. return index;
  397. }
  398. static void fml_single_line_pwm_transfer_callback(nrfx_pwm_evt_type_t event_type)
  399. {
  400. uint32_t err_code;
  401. switch(event_type)
  402. {
  403. case NRFX_PWM_EVT_FINISHED: //Sequence playback finished.
  404. //取消pwm
  405. Pwm1_UnInitialize();
  406. //重新GPIOTE 驱动初始化:
  407. err_code = nrf_drv_gpiote_init();
  408. APP_ERROR_CHECK(err_code);
  409. //配置接收中断
  410. if(ob_fml_single_line_simplex.role == FML_SINGLE_LINE_SIMPLEX_ROLE_HOST)
  411. FML_SINGLE_LINE_SIMPLEX_CONFIG_PIN_INPUT_INTERRUPT(fml_single_line_in_pin_handler,FML_SINGLE_LINE_SIMPLEX_CONFIG_PIN_INPUT_INTERRUPT_H_PULL);
  412. else
  413. FML_SINGLE_LINE_SIMPLEX_CONFIG_PIN_INPUT_INTERRUPT(fml_single_line_in_pin_handler,FML_SINGLE_LINE_SIMPLEX_CONFIG_PIN_INPUT_INTERRUPT_S_PULL);
  414. //修改状态
  415. ob_fml_single_line_simplex.pwm_transfer_is_done = true;
  416. break;
  417. default:
  418. break;
  419. }
  420. }
  421. /*API ----------------------------------------------*/
  422. /**
  423. @brief 初始化单线单工驱动
  424. @param role - [in] 角色
  425. @return 错误代码 - [out] -1失败,0成功
  426. */
  427. int fml_single_line_simplex_Init(FML_SINGLE_LINE_SIMPLEX_ROLE_e role)
  428. {
  429. uint32_t err_code;
  430. // GPIOTE 驱动初始化:
  431. err_code = nrf_drv_gpiote_init();
  432. APP_ERROR_CHECK(err_code);
  433. //重置
  434. memset(&ob_fml_single_line_simplex,0,sizeof(ob_fml_single_line_simplex));
  435. //初始化结构体
  436. ob_fml_single_line_simplex.start_receive = false;
  437. ob_fml_single_line_simplex.RxW = ob_fml_single_line_simplex.receive_buff;
  438. ob_fml_single_line_simplex.RxR = ob_fml_single_line_simplex.receive_buff;
  439. ob_fml_single_line_simplex.pwm_pin_channel[0] = FML_SINGLE_LINE_SIMPLEX_PIN;
  440. ob_fml_single_line_simplex.pwm_pin_channel[1] = NRF_DRV_PWM_PIN_NOT_USED;
  441. ob_fml_single_line_simplex.pwm_pin_channel[2] = NRF_DRV_PWM_PIN_NOT_USED;
  442. ob_fml_single_line_simplex.pwm_pin_channel[3] = NRF_DRV_PWM_PIN_NOT_USED;
  443. ob_fml_single_line_simplex.pwm_cycle_time = FML_SINGLE_LINE_SIMPLEX_PWM_SEND_ONE_BIT_TIEMS;
  444. ob_fml_single_line_simplex.pwm_mode = PWM_FLAG_STOP;
  445. ob_fml_single_line_simplex.pwm_transfer_is_done = true;
  446. ob_fml_single_line_simplex.role = role;
  447. //配置接收中断
  448. if(ob_fml_single_line_simplex.role == FML_SINGLE_LINE_SIMPLEX_ROLE_HOST)
  449. FML_SINGLE_LINE_SIMPLEX_CONFIG_PIN_INPUT_INTERRUPT(fml_single_line_in_pin_handler,FML_SINGLE_LINE_SIMPLEX_CONFIG_PIN_INPUT_INTERRUPT_H_PULL);
  450. else
  451. FML_SINGLE_LINE_SIMPLEX_CONFIG_PIN_INPUT_INTERRUPT(fml_single_line_in_pin_handler,FML_SINGLE_LINE_SIMPLEX_CONFIG_PIN_INPUT_INTERRUPT_S_PULL);
  452. return 0;
  453. }
  454. /**
  455. @brief 取消初始化单线单工驱动
  456. @param 无
  457. @return 错误代码 - [out] -1失败,0成功
  458. */
  459. int fml_single_line_simplex_UnInit(void)
  460. {
  461. //取消pwm
  462. Pwm1_UnInitialize();
  463. //取消gpiote
  464. nrf_drv_gpiote_uninit();
  465. return 0;
  466. }
  467. /**
  468. @brief 单线单工传输 —— 发送
  469. @param p_send - [in] 需要发送数据的指向地址
  470. @param len - [in] 数据长度
  471. @return 错误代码 - [out] -1失败,0成功
  472. */
  473. int fml_single_line_simplex_transfer(char *p_send, int len)
  474. {
  475. uint32_t err_code;
  476. if(p_send == NULL)return -1;
  477. if(len < 0)return -1;
  478. //取消pwm
  479. Pwm1_UnInitialize();
  480. //取消gpiote
  481. nrf_drv_gpiote_uninit();
  482. //重新GPIOTE 驱动初始化:
  483. err_code = nrf_drv_gpiote_init();
  484. APP_ERROR_CHECK(err_code);
  485. //配置发送
  486. FML_SINGLE_LINE_SIMPLEX_CONFIG_PIN_OUTPUT;
  487. for(int i=0; i<len; i++)
  488. {
  489. //发送一个字节
  490. fml_single_line_transfer_one_byte(p_send[i]);
  491. }
  492. //配置接收中断
  493. if(ob_fml_single_line_simplex.role == FML_SINGLE_LINE_SIMPLEX_ROLE_HOST)
  494. FML_SINGLE_LINE_SIMPLEX_CONFIG_PIN_INPUT_INTERRUPT(fml_single_line_in_pin_handler,FML_SINGLE_LINE_SIMPLEX_CONFIG_PIN_INPUT_INTERRUPT_H_PULL);
  495. else
  496. FML_SINGLE_LINE_SIMPLEX_CONFIG_PIN_INPUT_INTERRUPT(fml_single_line_in_pin_handler,FML_SINGLE_LINE_SIMPLEX_CONFIG_PIN_INPUT_INTERRUPT_S_PULL);
  497. return 0;
  498. }
  499. /**
  500. @brief 单线单工PWM传输 —— 发送
  501. @param byte - [in] 发送的字节
  502. @return 错误代码 - [out] -1失败,0成功
  503. */
  504. int fml_single_line_simplex_pwm_transfer_onebyte(char byte)
  505. {
  506. int len = 0;
  507. if(ob_fml_single_line_simplex.pwm_transfer_is_done == false)return -1;
  508. //取消gpiote
  509. nrf_drv_gpiote_uninit();
  510. //取消pwm
  511. Pwm1_UnInitialize();
  512. //重新初始化PWM
  513. SetPwm1_BaseClock(NRF_PWM_CLK_125kHz);
  514. SetPwm1_Channels(ob_fml_single_line_simplex.pwm_pin_channel[0], ob_fml_single_line_simplex.pwm_pin_channel[1], ob_fml_single_line_simplex.pwm_pin_channel[2], ob_fml_single_line_simplex.pwm_pin_channel[3]);
  515. SetPwm1_DutyCycleThreshold(ob_fml_single_line_simplex.pwm_cycle_time);
  516. SetPwm1_IRQ(fml_single_line_pwm_transfer_callback, PWM0_IRQ_PRIORITY);
  517. Pwm1_Initialize();
  518. //将需要发送的字节转换对应的序列值
  519. len = fml_single_line_pwm_transfer_one_byte(byte);
  520. ob_fml_single_line_simplex.fml_single_line_simplex_pwm_seq = Pwm1_SetComSequence(ob_fml_single_line_simplex.fml_single_line_simplex_pwm_seq_values, len,0,0);
  521. //修改状态
  522. ob_fml_single_line_simplex.pwm_transfer_is_done = false;
  523. //播放序列
  524. if(SetSimplePwm1PlayBack(ob_fml_single_line_simplex.fml_single_line_simplex_pwm_seq, 1, ob_fml_single_line_simplex.pwm_mode) != NRF_SUCCESS)
  525. {
  526. return -1;
  527. }
  528. return 0;
  529. }
  530. /**
  531. @brief 单线单工PWM传输是否完成 —— 发送
  532. @param 无
  533. @return 错误代码 - [out] -1失败,0成功
  534. */
  535. int fml_single_line_simplex_pwm_transfer_is_done(void)
  536. {
  537. return (ob_fml_single_line_simplex.pwm_transfer_is_done == true)?0:-1;
  538. }
  539. /**
  540. @brief 单线单工传输 —— 接收
  541. @param p_receive - [in] 需要接收数据的指向地址
  542. @param len - [in] 数据长度
  543. @return 返回的长度
  544. */
  545. int fml_single_line_simplex_receive(char *p_receive, uint32_t len)
  546. {
  547. uint8_t read_len;
  548. if(p_receive == NULL)return 0;
  549. read_len = fml_single_line_receive_CheckLen();
  550. if(read_len >= len)read_len = len;
  551. for(int i=0; i<read_len; i++)
  552. {
  553. p_receive[i] = fml_single_line_receive_buff_pop();
  554. }
  555. return read_len;
  556. }
  557. /**
  558. @brief 单线单工传输 —— 接收回调
  559. @param cb - [in] 回调地址
  560. @return 错误代码 - [out] -1失败,0成功
  561. */
  562. int fml_single_line_simplex_receive_time_callback(fml_single_line_half_duplex_receive_cb cb)
  563. {
  564. if(cb == NULL)return -1;
  565. ob_fml_single_line_simplex.receive_cb = cb;
  566. return 0;
  567. }