uart.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #include <GenericTypeDefs.h>
  2. #include <plib.h>
  3. #include <rtthread.h>
  4. #include "uart.h"
  5. #define GetSystemClock() (80000000ul)
  6. #define GetPeripheralClock() (GetSystemClock()/(1 << OSCCONbits.PBDIV))
  7. #define GetInstructionClock() (GetSystemClock())
  8. struct rt_uart_pic32
  9. {
  10. struct rt_device parent;
  11. int uart; /* UART Module ID. */
  12. /* buffer for reception */
  13. rt_uint8_t read_index, save_index;
  14. rt_uint8_t rx_buffer[RT_UART_RX_BUFFER_SIZE];
  15. };
  16. #ifdef RT_USING_UART1
  17. static struct rt_uart_pic32 uart1_device;
  18. #endif
  19. #ifdef RT_USING_UART2
  20. static struct rt_uart_pic32 uart2_device;
  21. #endif
  22. static rt_err_t rt_uart_init (rt_device_t dev)
  23. {
  24. struct rt_uart_pic32 *uart_device = (struct rt_uart_pic32*)dev;
  25. UARTConfigure(uart_device->uart, UART_ENABLE_PINS_TX_RX_ONLY);
  26. UARTSetFifoMode(uart_device->uart, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
  27. UARTSetLineControl(uart_device->uart, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
  28. UARTSetDataRate(uart_device->uart, GetPeripheralClock(), 115200);
  29. UARTEnable(uart_device->uart, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));
  30. // Configure UART RX Interrupt
  31. INTEnable(INT_SOURCE_UART_RX(uart_device->uart), INT_ENABLED);
  32. INTSetVectorPriority(INT_VECTOR_UART(uart_device->uart), INT_PRIORITY_LEVEL_2);
  33. INTSetVectorSubPriority(INT_VECTOR_UART(uart_device->uart), INT_SUB_PRIORITY_LEVEL_0);
  34. return RT_EOK;
  35. }
  36. static rt_err_t rt_uart_open(rt_device_t dev, rt_uint16_t oflag)
  37. {
  38. return RT_EOK;
  39. }
  40. static rt_err_t rt_uart_close(rt_device_t dev)
  41. {
  42. return RT_EOK;
  43. }
  44. static rt_size_t rt_uart_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  45. {
  46. rt_uint8_t* ptr;
  47. struct rt_uart_pic32 *uart_device = (struct rt_uart_pic32*)dev;
  48. RT_ASSERT(uart_device != RT_NULL);
  49. /* point to buffer */
  50. ptr = (rt_uint8_t*) buffer;
  51. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  52. {
  53. while (size)
  54. {
  55. /* interrupt receive */
  56. rt_base_t level;
  57. /* disable interrupt */
  58. level = rt_hw_interrupt_disable();
  59. if (uart_device->read_index != uart_device->save_index)
  60. {
  61. *ptr = uart_device->rx_buffer[uart_device->read_index];
  62. uart_device->read_index ++;
  63. if (uart_device->read_index >= RT_UART_RX_BUFFER_SIZE)
  64. uart_device->read_index = 0;
  65. }
  66. else
  67. {
  68. /* no data in rx buffer */
  69. /* enable interrupt */
  70. rt_hw_interrupt_enable(level);
  71. break;
  72. }
  73. /* enable interrupt */
  74. rt_hw_interrupt_enable(level);
  75. ptr ++;
  76. size --;
  77. }
  78. return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  79. }
  80. return 0;
  81. }
  82. static rt_size_t rt_uart_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  83. {
  84. struct rt_uart_pic32 *uart_device = (struct rt_uart_pic32*)dev;
  85. char *ptr;
  86. ptr = (char*)buffer;
  87. if (dev->flag & RT_DEVICE_FLAG_STREAM)
  88. {
  89. /* stream mode */
  90. while (size)
  91. {
  92. if (*ptr == '\n')
  93. {
  94. while(!UARTTransmitterIsReady(uart_device->uart));
  95. UARTSendDataByte(uart_device->uart,'\r' );
  96. while(!UARTTransmissionHasCompleted(uart_device->uart));
  97. }
  98. while(!UARTTransmitterIsReady(uart_device->uart));
  99. UARTSendDataByte(uart_device->uart, *ptr);
  100. while(!UARTTransmissionHasCompleted(uart_device->uart));
  101. ptr ++;
  102. size --;
  103. }
  104. }
  105. else
  106. {
  107. while ( size != 0 )
  108. {
  109. while(!UARTTransmitterIsReady(uart_device->uart));
  110. UARTSendDataByte(uart_device->uart, *ptr);
  111. while(!UARTTransmissionHasCompleted(uart_device->uart));
  112. ptr++;
  113. size--;
  114. }
  115. }
  116. return (rt_size_t) ptr - (rt_size_t) buffer;
  117. }
  118. #ifdef RT_USING_UART1
  119. // UART 1 interrupt handler
  120. // it is set at priority level 2
  121. void __ISR(_UART1_VECTOR, ipl2) IntUART1Handler(void)
  122. {
  123. struct rt_uart_pic32 *uart_device = &uart1_device;
  124. // Is this an RX interrupt?
  125. if(INTGetFlag(INT_SOURCE_UART_RX(uart_device->uart)))
  126. {
  127. while( U1STAbits.URXDA )
  128. {
  129. /* Receive Data Available */
  130. uart_device->rx_buffer[uart_device->save_index] = UARTGetDataByte(uart_device->uart);//UARTGetDataByte(UART1);
  131. uart_device->save_index ++;
  132. if (uart_device->save_index >= RT_UART_RX_BUFFER_SIZE)
  133. {
  134. uart_device->save_index = 0;
  135. }
  136. }
  137. /* invoke callback */
  138. if(uart_device->parent.rx_indicate != RT_NULL)
  139. {
  140. rt_size_t length;
  141. if (uart_device->read_index > uart_device->save_index)
  142. {
  143. length = RT_UART_RX_BUFFER_SIZE - uart_device->read_index + uart_device->save_index;
  144. }
  145. else
  146. {
  147. length = uart_device->save_index - uart_device->read_index;
  148. }
  149. if( length )
  150. {
  151. uart_device->parent.rx_indicate(&uart_device->parent, length);
  152. }
  153. }
  154. // Clear the RX interrupt Flag
  155. INTClearFlag(INT_SOURCE_UART_RX(uart_device->uart));
  156. } // Is this an RX interrupt?
  157. // We don't care about TX interrupt
  158. if ( INTGetFlag(INT_SOURCE_UART_TX(uart_device->uart)) )
  159. {
  160. INTClearFlag(INT_SOURCE_UART_TX(uart_device->uart));
  161. }
  162. }
  163. #endif
  164. #ifdef RT_USING_UART2
  165. // UART 2 interrupt handler
  166. // it is set at priority level 2
  167. void __ISR(_UART2_VECTOR, ipl2) IntUART2Handler(void)
  168. {
  169. struct rt_uart_pic32 *uart_device = &uart2_device;
  170. // Is this an RX interrupt?
  171. if(INTGetFlag(INT_SOURCE_UART_RX(uart_device->uart)))
  172. {
  173. while( U2STAbits.URXDA )
  174. {
  175. /* Receive Data Available */
  176. uart_device->rx_buffer[uart_device->save_index] = UARTGetDataByte(uart_device->uart);//UARTGetDataByte(UART1);
  177. uart_device->save_index ++;
  178. if (uart_device->save_index >= RT_UART_RX_BUFFER_SIZE)
  179. {
  180. uart_device->save_index = 0;
  181. }
  182. }
  183. /* invoke callback */
  184. if(uart_device->parent.rx_indicate != RT_NULL)
  185. {
  186. rt_size_t length;
  187. if (uart_device->read_index > uart_device->save_index)
  188. {
  189. length = RT_UART_RX_BUFFER_SIZE - uart_device->read_index + uart_device->save_index;
  190. }
  191. else
  192. {
  193. length = uart_device->save_index - uart_device->read_index;
  194. }
  195. if( length )
  196. {
  197. uart_device->parent.rx_indicate(&uart_device->parent, length);
  198. }
  199. }
  200. // Clear the RX interrupt Flag
  201. INTClearFlag(INT_SOURCE_UART_RX(uart_device->uart));
  202. } // Is this an RX interrupt?
  203. // We don't care about TX interrupt
  204. if ( INTGetFlag(INT_SOURCE_UART_TX(uart_device->uart)) )
  205. {
  206. INTClearFlag(INT_SOURCE_UART_TX(uart_device->uart));
  207. }
  208. }
  209. #endif
  210. void rt_hw_usart_init(void)
  211. {
  212. struct rt_uart_pic32 *uart_device;
  213. #ifdef RT_USING_UART1
  214. /* device initialization */
  215. uart_device = &uart1_device;
  216. rt_memset(uart_device,0,sizeof(struct rt_uart_pic32));
  217. uart_device->uart = UART1;
  218. uart_device->parent.type = RT_Device_Class_Char;
  219. /* device interface */
  220. uart_device->parent.init = rt_uart_init;
  221. uart_device->parent.open = rt_uart_open;
  222. uart_device->parent.close = rt_uart_close;
  223. uart_device->parent.read = rt_uart_read;
  224. uart_device->parent.write = rt_uart_write;
  225. uart_device->parent.control = RT_NULL;
  226. uart_device->parent.user_data = RT_NULL;
  227. rt_device_register(&uart_device->parent,
  228. "uart1", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_INT_RX);
  229. #endif
  230. #ifdef RT_USING_UART2
  231. /* device initialization */
  232. uart_device = &uart2_device;
  233. rt_memset(uart_device,0,sizeof(struct rt_uart_pic32));
  234. uart_device->uart = UART2;
  235. uart_device->parent.type = RT_Device_Class_Char;
  236. /* device interface */
  237. uart_device->parent.init = rt_uart_init;
  238. uart_device->parent.open = rt_uart_open;
  239. uart_device->parent.close = rt_uart_close;
  240. uart_device->parent.read = rt_uart_read;
  241. uart_device->parent.write = rt_uart_write;
  242. uart_device->parent.control = RT_NULL;
  243. uart_device->parent.user_data = RT_NULL;
  244. rt_device_register(&uart_device->parent,
  245. "uart2", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_INT_RX);
  246. #endif
  247. }