myUart.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #include "myUart.h"
  2. #include "stm32f10x_rcc.h"
  3. #include "stm32f10x_usart.h"
  4. #include "stm32f10x_it.h"
  5. #include "misc.h"
  6. uint8_t printf_uartX = US_UART1;
  7. UART_CALLBACK uartCallBack;
  8. uint8_t USART_RX_BUF[USART_REC_LEN]; //
  9. uint16_t USART_RX_STA=0; //接收状态标记
  10. static irqCallback_ts myIrqCallback_uart1;
  11. void uart1_callback(uint8_t status, uint32_t param)
  12. {
  13. uint8_t Res;
  14. switch (status)
  15. {
  16. case 0:
  17. {
  18. uartCallBack(USART_RX_BUF, USART_RX_STA);
  19. memset(USART_RX_BUF, 0, sizeof(USART_RX_BUF));
  20. USART_RX_STA=0;
  21. }
  22. break;
  23. case 1:
  24. {
  25. //读取接收到的数据
  26. USART_RX_BUF[USART_RX_STA] = param;
  27. USART_RX_STA++;
  28. if(USART_RX_STA > (USART_REC_LEN - 1))
  29. {
  30. USART_RX_STA=0;//接收数据错误,重新开始接收
  31. }
  32. }
  33. break;
  34. default:
  35. break;
  36. }
  37. }
  38. void myUart1_init(uint32_t baudrate, UART_CALLBACK cb)
  39. {
  40. GPIO_InitTypeDef GPIO_InitStructure;
  41. USART_InitTypeDef USART_InitStructure;
  42. NVIC_InitTypeDef NVIC_InitStructure;
  43. myIrqCallback_uart1.thisCb = uart1_callback;
  44. USART1_callbackRegiste(&myIrqCallback_uart1);
  45. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //使能USART1,GPIOA时钟
  46. //USART1_TX GPIOA.9
  47. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
  48. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  49. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
  50. GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
  51. //USART1_RX GPIOA.10初始化
  52. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
  53. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
  54. GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
  55. //Usart1 NVIC 配置
  56. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  57. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
  58. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
  59. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
  60. NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
  61. //USART 初始化设置
  62. USART_InitStructure.USART_BaudRate = baudrate;//串口波特率
  63. USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
  64. USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
  65. USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
  66. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
  67. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
  68. USART_Init(USART1, &USART_InitStructure); //初始化串口1
  69. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断
  70. USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);//开启串口接受中断
  71. // USART_ITConfig(USART1, USART_IT_TC, ENABLE);// 使能串口发送完成中断
  72. USART_Cmd(USART1, ENABLE); //使能串口1
  73. uartCallBack = cb;
  74. }
  75. void myUart1_sendByte(uint8_t src)
  76. {
  77. while(!USART_GetFlagStatus(USART1, USART_FLAG_TC));
  78. USART_SendData(USART1, src);
  79. }
  80. void myUart1_sendArray(uint8_t *src, uint16_t srclen)
  81. {
  82. while(srclen --)
  83. {
  84. myUart1_sendByte(*src);
  85. src ++;
  86. }
  87. }
  88. static uint16_t USART3_RX_STA; //接收状态标记
  89. static UART_CALLBACK uart3RxCallBack;
  90. static uint8_t USART3_RX_BUF[USART_REC_LEN];
  91. static irqCallback_ts myIrqCallback_uart3;
  92. static irqCallback_ts myIrqCallback_uart3Timeout;
  93. static uint32_t timeout;
  94. /**
  95. * @brief uart3中断函数
  96. * 通过API@USART1_callbackRegiste注册到@stm32f10x_it.c中的真正的中断函数
  97. *
  98. * @param status 串口接收状态,
  99. * =0,接收超时完成
  100. * =1,接收到一个字节数据
  101. * @param param
  102. */
  103. void uart3_callback(uint8_t status, uint32_t param)
  104. {
  105. uint8_t Res;
  106. switch (status)
  107. {
  108. // case 0:
  109. // {
  110. // uart3RxCallBack(USART3_RX_BUF, USART3_RX_STA);
  111. // memset(USART3_RX_BUF, 0, sizeof(USART3_RX_BUF));
  112. // USART3_RX_STA=0;
  113. // }
  114. // break;
  115. case 1:
  116. {
  117. //读取接收到的数据
  118. USART3_RX_BUF[USART3_RX_STA] = param;
  119. USART3_RX_STA++;
  120. if(USART3_RX_STA > (USART_REC_LEN - 1))
  121. {
  122. USART3_RX_STA=0;//接收数据错误,重新开始接收
  123. }
  124. timeout = 5*100;
  125. }
  126. break;
  127. default:
  128. break;
  129. }
  130. }
  131. void uart3Timeout_callback(uint8_t status, uint32_t param)
  132. {
  133. if (timeout)
  134. {
  135. if (--timeout == 0)
  136. {
  137. uart3RxCallBack(USART3_RX_BUF, USART3_RX_STA);
  138. memset(USART3_RX_BUF, 0, sizeof(USART3_RX_BUF));
  139. USART3_RX_STA=0;
  140. }
  141. }
  142. }
  143. void myUart3_init(uint32_t baudrate, UART_CALLBACK cb)
  144. {
  145. //GPIO端口设置
  146. GPIO_InitTypeDef GPIO_InitStructure;
  147. USART_InitTypeDef USART_InitStructure;
  148. NVIC_InitTypeDef NVIC_InitStructure;
  149. myIrqCallback_uart3.thisCb = uart3_callback;
  150. USART3_callbackRegiste(&myIrqCallback_uart3);
  151. myIrqCallback_uart3Timeout.thisCb = uart3Timeout_callback;
  152. TIM1_callbackRegiste(&myIrqCallback_uart3Timeout);
  153. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  154. //USART3_TX
  155. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //
  156. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  157. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
  158. GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
  159. //USART3_RX初始化
  160. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//
  161. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
  162. GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
  163. //Usart3 NVIC 配置
  164. NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
  165. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
  166. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
  167. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
  168. NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
  169. //USART 初始化设置
  170. USART_InitStructure.USART_BaudRate = baudrate;//串口波特率
  171. USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
  172. USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
  173. USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
  174. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
  175. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
  176. USART_Init(USART3, &USART_InitStructure); //初始化串口
  177. USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启串口接受中断
  178. USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);//开启串口接受中断
  179. // USART_ITConfig(USART3, USART_IT_TC, ENABLE);// 使能串口发送完成中断
  180. uart3RxCallBack = cb;
  181. USART_Cmd(USART3, ENABLE); //使能串口
  182. }
  183. void myUart3_sendByte(uint8_t sendByte)
  184. {
  185. while(!USART_GetFlagStatus(USART3, USART_FLAG_TC))
  186. {
  187. ;
  188. }
  189. USART_SendData(USART3, sendByte);
  190. }
  191. void myUart3_sendArray(uint8_t *buffer, uint16_t sendLen)
  192. {
  193. int i = 0;
  194. for (i = 0; i < sendLen; i++)
  195. {
  196. myUart3_sendByte(*buffer);
  197. buffer ++;
  198. }
  199. }
  200. //////////////////////////////////////////////////////////////////
  201. //加入以下代码,支持printf函数,而不需要选择use MicroLIB
  202. #ifdef __GNUC__
  203. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  204. #else
  205. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  206. #endif
  207. #ifdef USE_IAR
  208. PUTCHAR_PROTOTYPE
  209. {
  210. myUart_sendByte(ch);
  211. return ch;
  212. }
  213. #else
  214. #pragma import(__use_no_semihosting)
  215. //标准库需要的支持函数
  216. struct __FILE
  217. {
  218. int handle;
  219. };
  220. FILE __stdout;
  221. //定义_sys_exit()以避免使用半主机模式
  222. void _sys_exit(int x)
  223. {
  224. x = x;
  225. }
  226. //redefine fputcfunction
  227. s32 fputc(s32 ch, FILE* f)
  228. {
  229. switch (printf_uartX)
  230. {
  231. case US_UART1:
  232. myUart1_sendByte(ch);
  233. break;
  234. case US_UART3:
  235. myUart3_sendByte(ch);
  236. break;
  237. default:
  238. break;
  239. }
  240. return ch;
  241. }
  242. #endif