fml_single_line_simplex.c 28 KB

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