uart.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. #include "uart.h"
  2. #include "systick.h"
  3. #include <stdio.h>
  4. #include <sys/unistd.h>
  5. static uint8_t usart1_tx_buf[256];
  6. static uint8_t usart1_rx_buf[256];
  7. static uint8_t usart2_tx_buf[256];
  8. static uint8_t usart2_rx_buf[256];
  9. static uint8_t usart3_tx_buf[256];
  10. static uint8_t usart3_rx_buf[256];
  11. #define UART1_TX_PORT GPIOA
  12. #define UART1_TX_PIN GPIO_Pin_9
  13. #define UART1_TX_SOURCE GPIO_PinSource9
  14. #define UART1_RX_PORT GPIOA
  15. #define UART1_RX_PIN GPIO_Pin_10
  16. #define UART1_RX_SOURCE GPIO_PinSource10
  17. #define UART2_TX_PORT GPIOA
  18. #define UART2_TX_PIN GPIO_Pin_2
  19. #define UART2_TX_SOURCE GPIO_PinSource2
  20. #define UART2_RX_PORT GPIOA
  21. #define UART2_RX_PIN GPIO_Pin_3
  22. #define UART2_RX_SOURCE GPIO_PinSource3
  23. #define UART3_TX_PORT GPIOB
  24. #define UART3_TX_PIN GPIO_Pin_10
  25. #define UART3_TX_SOURCE GPIO_PinSource2
  26. #define UART3_RX_PORT GPIOB
  27. #define UART3_RX_PIN GPIO_Pin_11
  28. #define UART3_RX_SOURCE GPIO_PinSource3
  29. int usart1_snd_tx_cnt = 0;
  30. // 加入以下代码,支持printf函数,而不需要选择use MicroLIB
  31. // 确保没有从 C 库链接使用半主机的函数
  32. #pragma import(__use_no_semihosting)
  33. #pragma import(__use_no_semihosting_swi)
  34. // 定义_sys_exit() 以避免使用半主机模式
  35. void _sys_exit(int x)
  36. {
  37. x = x;
  38. }
  39. // 加入以下代码,支持printf函数,而不需要选择use MicroLIB
  40. // 标准库需要的支持函数
  41. struct __FILE
  42. {
  43. int handle;
  44. };
  45. /* FILE is typedef ’ d in stdio.h. */
  46. FILE __stdout;
  47. FILE __stdin;
  48. int _ttywrch(int ch)
  49. {
  50. ch = ch;
  51. return ch;
  52. }
  53. // && !defined(__clang__)
  54. #if defined(__GNUC__)
  55. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  56. #define GETCHAR_PROTOTYPE int __io_getchar(void)
  57. #else
  58. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  59. #define GETCHAR_PROTOTYPE int fgetc(FILE *f)
  60. #endif
  61. PUTCHAR_PROTOTYPE
  62. {
  63. uint32_t tick;
  64. usart1_snd_tx_cnt++;
  65. usart_send_it(&usart1_context, &ch, 1);
  66. tick = get_systick_ms();
  67. while (RESET == USART_GetFlagStatus(USART1, USART_FLAG_TXE))
  68. {
  69. if (get_systick_ms() - tick > 10)
  70. break;
  71. }
  72. return ch;
  73. }
  74. GETCHAR_PROTOTYPE
  75. {
  76. uint8_t ch;
  77. if (0 != usart_receive_read(&usart1_context, &ch, 1))
  78. {
  79. return ch;
  80. }
  81. return -1;
  82. }
  83. // &&!defined(__clang__)
  84. #if defined(__GNUC__)
  85. __attribute__((weak)) int _write(int file, char *ptr, int len)
  86. {
  87. (void)file;
  88. if ((file != STDOUT_FILENO) && (file != STDERR_FILENO))
  89. {
  90. return -1;
  91. }
  92. for (int index = 0; index < len; index++)
  93. __io_putchar(*ptr++);
  94. return len;
  95. }
  96. __attribute__((weak)) int _read(int file, char *ptr, int len)
  97. {
  98. (void)file;
  99. if (file != STDIN_FILENO)
  100. {
  101. return -1;
  102. }
  103. uint8_t ch;
  104. for (int index = 0; index < len; index++)
  105. {
  106. // *ptr++ = __io_getchar();
  107. if (0 != usart_receive_read(&usart1_context, &ch, 1))
  108. {
  109. return ch;
  110. }
  111. return -1;
  112. }
  113. return len;
  114. }
  115. #endif
  116. #if 0
  117. int getchar(void)
  118. {
  119. char c = 0;
  120. // ssize_t
  121. int bytes_read = read(STDIN_FILENO, &c, sizeof(char));
  122. if (bytes_read == -1)
  123. {
  124. // 读取错误
  125. return EOF;
  126. }
  127. else if (bytes_read == 0)
  128. {
  129. // 读取结束
  130. return EOF;
  131. }
  132. else
  133. {
  134. // 返回读取的字符
  135. return (int)c;
  136. }
  137. }
  138. #else
  139. int getchar(void)
  140. {
  141. uint8_t ch;
  142. if (0 != usart_receive_read(&usart1_context, &ch, 1))
  143. {
  144. return ch;
  145. }
  146. return -1;
  147. }
  148. #endif
  149. static void usart1_config(void)
  150. {
  151. GPIO_InitTypeDef GPIO_StructInit;
  152. NVIC_InitTypeDef NVIC_InitStructure;
  153. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  154. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  155. GPIO_StructInit.GPIO_Mode = GPIO_Mode_AF;
  156. GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
  157. GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
  158. GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_UP;
  159. GPIO_StructInit.GPIO_Pin = UART1_TX_PIN;
  160. GPIO_PinAFConfig(UART1_TX_PORT, UART1_TX_SOURCE, GPIO_AF_USART1);
  161. GPIO_Init(UART1_TX_PORT, &GPIO_StructInit);
  162. GPIO_StructInit.GPIO_Pin = UART1_RX_PIN;
  163. GPIO_PinAFConfig(UART1_RX_PORT, UART1_RX_SOURCE, GPIO_AF_USART1);
  164. GPIO_Init(UART1_RX_PORT, &GPIO_StructInit);
  165. // Usart1 NVIC 配置
  166. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; // 串口1中断通道
  167. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 抢占优先级
  168. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 子优先级
  169. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQ通道使能
  170. NVIC_Init(&NVIC_InitStructure);
  171. }
  172. static void usart1_deconfig(void)
  173. {
  174. }
  175. static void usart2_config(void)
  176. {
  177. GPIO_InitTypeDef GPIO_StructInit;
  178. NVIC_InitTypeDef NVIC_InitStructure;
  179. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  180. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  181. GPIO_StructInit.GPIO_Mode = GPIO_Mode_AF;
  182. GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
  183. GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
  184. GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_UP;
  185. GPIO_StructInit.GPIO_Pin = UART2_TX_PIN;
  186. GPIO_PinAFConfig(UART2_TX_PORT, UART2_TX_SOURCE, GPIO_AF_USART2);
  187. GPIO_Init(UART2_TX_PORT, &GPIO_StructInit);
  188. GPIO_StructInit.GPIO_Pin = UART2_RX_PIN;
  189. GPIO_PinAFConfig(UART2_RX_PORT, UART2_RX_SOURCE, GPIO_AF_USART2);
  190. GPIO_Init(UART2_RX_PORT, &GPIO_StructInit);
  191. // Usart1 NVIC 配置
  192. NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; // 串口1中断通道
  193. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 抢占优先级
  194. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 子优先级
  195. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQ通道使能
  196. NVIC_Init(&NVIC_InitStructure);
  197. }
  198. static void usart2_deconfig(void)
  199. {
  200. }
  201. static void usart3_config(void)
  202. {
  203. GPIO_InitTypeDef GPIO_StructInit;
  204. NVIC_InitTypeDef NVIC_InitStructure;
  205. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  206. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  207. GPIO_StructInit.GPIO_Mode = GPIO_Mode_AF;
  208. GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
  209. GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
  210. GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_UP;
  211. GPIO_StructInit.GPIO_Pin = UART3_TX_PIN;
  212. GPIO_PinAFConfig(UART3_TX_PORT, UART3_TX_SOURCE, GPIO_AF_USART3);
  213. GPIO_Init(UART3_TX_PORT, &GPIO_StructInit);
  214. GPIO_StructInit.GPIO_Pin = UART3_RX_PIN;
  215. GPIO_PinAFConfig(UART3_RX_PORT, UART3_RX_SOURCE, GPIO_AF_USART3);
  216. GPIO_Init(UART3_RX_PORT, &GPIO_StructInit);
  217. // Usart1 NVIC 配置
  218. NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; // 串口1中断通道
  219. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 抢占优先级
  220. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 子优先级
  221. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQ通道使能
  222. NVIC_Init(&NVIC_InitStructure);
  223. }
  224. static void usart3_deconfig(void)
  225. {
  226. }
  227. usart_context_t usart1_context = {
  228. USART1,
  229. usart1_tx_buf,
  230. sizeof(usart1_tx_buf),
  231. 0,
  232. 0,
  233. 0,
  234. usart1_rx_buf,
  235. sizeof(usart1_rx_buf),
  236. 0,
  237. 0,
  238. 0,
  239. usart1_config,
  240. usart1_deconfig};
  241. usart_context_t usart2_context = {
  242. USART2,
  243. usart2_tx_buf,
  244. sizeof(usart2_tx_buf),
  245. 0,
  246. 0,
  247. 0,
  248. usart2_rx_buf,
  249. sizeof(usart2_rx_buf),
  250. 0,
  251. 0,
  252. 0,
  253. usart2_config,
  254. usart2_deconfig};
  255. usart_context_t usart3_context = {
  256. USART3,
  257. usart3_tx_buf,
  258. sizeof(usart3_tx_buf),
  259. 0,
  260. 0,
  261. 0,
  262. usart3_rx_buf,
  263. sizeof(usart3_rx_buf),
  264. 0,
  265. 0,
  266. 0,
  267. usart3_config,
  268. usart3_deconfig};
  269. void usart_config_init(usart_context_t *p_usart_content, uint32_t band_rate)
  270. {
  271. p_usart_content->config();
  272. USART_InitTypeDef USART_InitStructure;
  273. USART_InitStructure.USART_BaudRate = band_rate;
  274. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  275. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  276. USART_InitStructure.USART_Parity = USART_Parity_No;
  277. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  278. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  279. USART_Init(p_usart_content->usart_periph, &USART_InitStructure); // 初始化串口
  280. USART_ITConfig(p_usart_content->usart_periph, USART_IT_RXNE, ENABLE);
  281. USART_Cmd(p_usart_content->usart_periph, ENABLE); // 使能串口
  282. }
  283. void usart_config_deinit(usart_context_t *p_usart_content)
  284. {
  285. USART_DeInit(p_usart_content->usart_periph);
  286. p_usart_content->deconfig();
  287. }
  288. void usart_send(usart_context_t *p_usart_content, const void *_send_buf, const uint16_t send_count)
  289. {
  290. const uint8_t *send_buf = (const uint8_t *)_send_buf;
  291. uint16_t i;
  292. for (i = 0; i < send_count; i++)
  293. {
  294. while (RESET == USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_TXE))
  295. ;
  296. USART_SendData(p_usart_content->usart_periph, (uint8_t)send_buf[i]);
  297. }
  298. while (RESET == USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_TC))
  299. ;
  300. }
  301. int32_t usart_read_until_char(usart_context_t *p_usart_content, char c)
  302. {
  303. uint32_t i;
  304. for (i = p_usart_content->rx_rd; i < p_usart_content->rx_wr; i++)
  305. {
  306. if (p_usart_content->rx_buf[i % p_usart_content->rx_buf_size] == c)
  307. {
  308. return i;
  309. }
  310. }
  311. return -1;
  312. }
  313. // read until memory
  314. // return:第一次出现mem位置, -1 没找到
  315. int32_t usart_read_until_mem(usart_context_t *p_usart_content, char *mem, int len)
  316. {
  317. int32_t i, j, pos;
  318. for (i = p_usart_content->rx_rd; i < p_usart_content->rx_wr; i++)
  319. {
  320. // overflow
  321. for (j = 0; j < len; j++)
  322. {
  323. pos = i + j;
  324. if (p_usart_content->rx_buf[pos % p_usart_content->rx_buf_size] != mem[j])
  325. {
  326. break;
  327. }
  328. }
  329. // 找到内存快
  330. if (j == len)
  331. {
  332. return i;
  333. }
  334. }
  335. return -1;
  336. }
  337. // read ble format 04 FC code len data
  338. // return 满足format条件结束位置, -1 没找到
  339. int32_t usart_read_ble_format(usart_context_t *p_usart_content)
  340. {
  341. uint16_t rx_wr = p_usart_content->rx_wr;
  342. uint16_t rx_rd = p_usart_content->rx_rd;
  343. if (rx_wr < rx_rd)
  344. {
  345. rx_wr += p_usart_content->rx_buf_size;
  346. }
  347. if (rx_wr - rx_rd < 4)
  348. {
  349. return -1;
  350. }
  351. // 第一次开机跳过特殊桢处理 00 04,跳过00
  352. if (p_usart_content->rx_buf[rx_rd % p_usart_content->rx_buf_size] == 0x00 || p_usart_content->rx_buf[(rx_rd + 1) % p_usart_content->rx_buf_size] == 0x04)
  353. {
  354. p_usart_content->rx_rd++;
  355. p_usart_content->rx_rd %= p_usart_content->rx_buf_size;
  356. return -4;
  357. }
  358. if (p_usart_content->rx_buf[rx_rd % p_usart_content->rx_buf_size] != 0x04 || p_usart_content->rx_buf[(rx_rd + 1) % p_usart_content->rx_buf_size] != 0xFC)
  359. {
  360. return -2;
  361. }
  362. // 蓝牙长度不够
  363. if (rx_wr - rx_rd < p_usart_content->rx_buf[(rx_rd + 3) % p_usart_content->rx_buf_size] + 4)
  364. {
  365. return -3;
  366. }
  367. return p_usart_content->rx_buf[(rx_rd + 3) % p_usart_content->rx_buf_size] + 4;
  368. }
  369. int32_t usart_copy_data(usart_context_t *p_usart_content, int rlen, char *data)
  370. {
  371. int32_t i, p;
  372. // 拷贝rlen + 1个数据
  373. for (i = 0; i < rlen; i++)
  374. {
  375. p = (p_usart_content->rx_rd + i) % p_usart_content->rx_buf_size;
  376. data[i] = p_usart_content->rx_buf[p];
  377. }
  378. return 0;
  379. }
  380. int32_t usart_read_update(usart_context_t *p_usart_content, int rlen, char *data)
  381. {
  382. int32_t i, p;
  383. // 拷贝rlen + 1个数据
  384. for (i = 0; i < rlen; i++)
  385. {
  386. p = (p_usart_content->rx_rd + i) % p_usart_content->rx_buf_size;
  387. data[i] = p_usart_content->rx_buf[p];
  388. }
  389. // 指定pos下一个位置
  390. // return p_usart_content->rx_rd = (p_usart_content->rx_rd + rlen) % p_usart_content->rx_buf_size;
  391. p = p_usart_content->rx_rd = (p_usart_content->rx_rd + rlen) % p_usart_content->rx_buf_size;
  392. return p;
  393. }
  394. void usart_send_it(usart_context_t *p_usart_content, const void *_send_buf, const uint16_t send_count)
  395. {
  396. const uint8_t *send_buf = (const uint8_t *)_send_buf;
  397. uint16_t i;
  398. uint16_t left;
  399. // 空间不够直接返回
  400. if (p_usart_content->tx_wr >= p_usart_content->tx_rd)
  401. {
  402. left = p_usart_content->tx_buf_size - (p_usart_content->tx_wr - p_usart_content->tx_rd);
  403. }
  404. else
  405. {
  406. left = p_usart_content->tx_rd - p_usart_content->tx_wr;
  407. if (send_count > left)
  408. {
  409. p_usart_content->tx_err++;
  410. return;
  411. }
  412. }
  413. // 内存拷贝
  414. for (i = 0; i < send_count; i++)
  415. {
  416. // send buffer overflow just wait
  417. p_usart_content->tx_buf[p_usart_content->tx_wr++] = send_buf[i];
  418. p_usart_content->tx_wr %= p_usart_content->tx_buf_size;
  419. }
  420. // 使能发送缓存空中断
  421. USART_ITConfig(p_usart_content->usart_periph, USART_IT_TXE, ENABLE);
  422. }
  423. void usart_wait_send_finished(usart_context_t *p_usart_content)
  424. {
  425. while (p_usart_content->tx_wr != p_usart_content->tx_rd)
  426. ;
  427. while (RESET == USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_TC))
  428. ;
  429. }
  430. int usart_receive_read(usart_context_t *p_usart_context, void *_receive_buf, const int receive_count)
  431. {
  432. uint8_t *receive_buf = (uint8_t *)_receive_buf;
  433. int i, receive_count_real;
  434. /* Read data from receive buffer.
  435. *The buffer have data that received from usart.
  436. */
  437. for (i = 0, receive_count_real = 0; i < receive_count; i++)
  438. {
  439. if (p_usart_context->rx_rd == p_usart_context->rx_wr)
  440. {
  441. return receive_count_real;
  442. }
  443. else
  444. {
  445. receive_buf[i] = p_usart_context->rx_buf[p_usart_context->rx_rd++];
  446. p_usart_context->rx_rd %= p_usart_context->rx_buf_size;
  447. receive_count_real++;
  448. }
  449. }
  450. return receive_count_real;
  451. }
  452. static void usart_rxne_it(usart_context_t *p_usart_content)
  453. {
  454. p_usart_content->rx_buf[p_usart_content->rx_wr++] = USART_ReceiveData(p_usart_content->usart_periph);
  455. p_usart_content->rx_wr %= p_usart_content->rx_buf_size;
  456. // overflow handle
  457. if (p_usart_content->rx_wr == p_usart_content->rx_rd)
  458. {
  459. p_usart_content->rx_rd++;
  460. p_usart_content->rx_rd %= p_usart_content->rx_buf_size;
  461. }
  462. }
  463. static void usart_txe_it(usart_context_t *p_usart_content)
  464. {
  465. if (p_usart_content->tx_rd != p_usart_content->tx_wr)
  466. {
  467. USART_SendData(p_usart_content->usart_periph, p_usart_content->tx_buf[p_usart_content->tx_rd++]);
  468. p_usart_content->tx_rd %= p_usart_content->tx_buf_size;
  469. }
  470. else
  471. {
  472. USART_ITConfig(p_usart_content->usart_periph, USART_IT_TXE, DISABLE);
  473. }
  474. }
  475. void usart_it(usart_context_t *p_usart_content)
  476. {
  477. // 读取缓沖区非空中断
  478. if (SET == USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_RXNE))
  479. {
  480. USART_ClearFlag(p_usart_content->usart_periph, USART_FLAG_RXNE);
  481. usart_rxne_it(p_usart_content);
  482. }
  483. // 发送缓沖区空中断
  484. if (SET == USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_TXE))
  485. {
  486. USART_ClearFlag(p_usart_content->usart_periph, USART_FLAG_TXE);
  487. usart_txe_it(p_usart_content);
  488. }
  489. // // 发送完成中断
  490. // if (RESET != USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_TC))
  491. // {
  492. // USART_ITConfig(p_usart_content->usart_periph, USART_FLAG_TC, DISABLE);
  493. // USART_ITConfig(p_usart_content->usart_periph, USART_FLAG_RXNE, ENABLE);
  494. // }
  495. // 发送错误
  496. if (RESET != USART_GetFlagStatus(p_usart_content->usart_periph, USART_FLAG_ORE))
  497. USART_ClearFlag(p_usart_content->usart_periph, USART_FLAG_ORE);
  498. }
  499. void USART1_IRQHandler(void)
  500. {
  501. usart_it(&usart1_context);
  502. }
  503. void USART2_IRQHandler(void)
  504. {
  505. usart_it(&usart2_context);
  506. }
  507. void USART3_IRQHandler(void)
  508. {
  509. usart_it(&usart3_context);
  510. }