serial.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * File : serial.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://openlab.rt-thread.com/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2006-08-23 Bernard first version
  13. * 2009-05-14 Bernard add RT-THread device interface
  14. */
  15. #include <rthw.h>
  16. #include <rtthread.h>
  17. #include "AT91SAM7S.h"
  18. #include "serial.h"
  19. /**
  20. * @addtogroup AT91SAM7
  21. */
  22. /*@{*/
  23. typedef volatile rt_uint32_t REG32;
  24. struct rt_at91serial_hw
  25. {
  26. REG32 US_CR; // Control Register
  27. REG32 US_MR; // Mode Register
  28. REG32 US_IER; // Interrupt Enable Register
  29. REG32 US_IDR; // Interrupt Disable Register
  30. REG32 US_IMR; // Interrupt Mask Register
  31. REG32 US_CSR; // Channel Status Register
  32. REG32 US_RHR; // Receiver Holding Register
  33. REG32 US_THR; // Transmitter Holding Register
  34. REG32 US_BRGR; // Baud Rate Generator Register
  35. REG32 US_RTOR; // Receiver Time-out Register
  36. REG32 US_TTGR; // Transmitter Time-guard Register
  37. REG32 Reserved0[5]; //
  38. REG32 US_FIDI; // FI_DI_Ratio Register
  39. REG32 US_NER; // Nb Errors Register
  40. REG32 Reserved1[1]; //
  41. REG32 US_IF; // IRDA_FILTER Register
  42. REG32 Reserved2[44]; //
  43. REG32 US_RPR; // Receive Pointer Register
  44. REG32 US_RCR; // Receive Counter Register
  45. REG32 US_TPR; // Transmit Pointer Register
  46. REG32 US_TCR; // Transmit Counter Register
  47. REG32 US_RNPR; // Receive Next Pointer Register
  48. REG32 US_RNCR; // Receive Next Counter Register
  49. REG32 US_TNPR; // Transmit Next Pointer Register
  50. REG32 US_TNCR; // Transmit Next Counter Register
  51. REG32 US_PTCR; // PDC Transfer Control Register
  52. REG32 US_PTSR; // PDC Transfer Status Register
  53. };
  54. struct rt_at91serial
  55. {
  56. struct rt_device parent;
  57. struct rt_at91serial_hw* hw_base;
  58. rt_uint16_t peripheral_id;
  59. rt_uint32_t baudrate;
  60. /* reception field */
  61. rt_uint16_t save_index, read_index;
  62. rt_uint8_t rx_buffer[RT_UART_RX_BUFFER_SIZE];
  63. };
  64. #ifdef RT_USING_UART1
  65. struct rt_at91serial serial1;
  66. #endif
  67. #ifdef RT_USING_UART2
  68. struct rt_at91serial serial2;
  69. #endif
  70. static void rt_hw_serial_isr(int irqno)
  71. {
  72. rt_base_t level;
  73. struct rt_device* device;
  74. struct rt_at91serial* serial = RT_NULL;
  75. if (irqno == AT91C_ID_US0)
  76. {
  77. #ifdef RT_USING_UART1
  78. /* serial 1 */
  79. serial = &serial1;
  80. #endif
  81. }
  82. else if (irqno == AT91C_ID_US1)
  83. {
  84. #ifdef RT_USING_UART2
  85. /* serial 2 */
  86. serial = &serial2;
  87. #endif
  88. }
  89. RT_ASSERT(serial != RT_NULL);
  90. /* get generic device object */
  91. device = (rt_device_t)serial;
  92. /* disable interrupt */
  93. level = rt_hw_interrupt_disable();
  94. /* get received character */
  95. serial->rx_buffer[serial->save_index] = serial->hw_base->US_RHR;
  96. /* move to next position */
  97. serial->save_index ++;
  98. if (serial->save_index >= RT_UART_RX_BUFFER_SIZE)
  99. serial->save_index = 0;
  100. /* if the next position is read index, discard this 'read char' */
  101. if (serial->save_index == serial->read_index)
  102. {
  103. serial->read_index ++;
  104. if (serial->read_index >= RT_UART_RX_BUFFER_SIZE)
  105. serial->read_index = 0;
  106. }
  107. /* enable interrupt */
  108. rt_hw_interrupt_enable(level);
  109. /* indicate to upper layer application */
  110. if (device->rx_indicate != RT_NULL)
  111. device->rx_indicate(device, 1);
  112. /* ack interrupt */
  113. AT91C_AIC_EOICR = 1;
  114. }
  115. static rt_err_t rt_serial_init (rt_device_t dev)
  116. {
  117. rt_uint32_t bd;
  118. struct rt_at91serial* serial = (struct rt_at91serial*) dev;
  119. RT_ASSERT(serial != RT_NULL);
  120. /* must be US0 or US1 */
  121. RT_ASSERT((serial->peripheral_id != AT91C_ID_US0) &&
  122. (serial->peripheral_id != AT91C_ID_US1));
  123. /* Enable Clock for USART */
  124. AT91C_PMC_PCER = 1 << serial->peripheral_id;
  125. /* Enable RxD0 and TxDO Pin */
  126. if (serial->peripheral_id == AT91C_ID_US0)
  127. {
  128. /* set pinmux */
  129. AT91C_PIO_PDR = (1 << 5) | (1 << 6);
  130. }
  131. else if (serial->peripheral_id == AT91C_ID_US1)
  132. {
  133. /* set pinmux */
  134. AT91C_PIO_PDR = (1 << 21) | (1 << 22);
  135. }
  136. serial->hw_base->US_CR = AT91C_US_RSTRX | /* Reset Receiver */
  137. AT91C_US_RSTTX | /* Reset Transmitter */
  138. AT91C_US_RXDIS | /* Receiver Disable */
  139. AT91C_US_TXDIS; /* Transmitter Disable */
  140. serial->hw_base->US_MR = AT91C_US_USMODE_NORMAL | /* Normal Mode */
  141. AT91C_US_CLKS_CLOCK | /* Clock = MCK */
  142. AT91C_US_CHRL_8_BITS | /* 8-bit Data */
  143. AT91C_US_PAR_NONE | /* No Parity */
  144. AT91C_US_NBSTOP_1_BIT; /* 1 Stop Bit */
  145. /* set baud rate divisor */
  146. bd = ((MCK*10)/(serial->baudrate * 16));
  147. if ((bd % 10) >= 5) bd = (bd / 10) + 1;
  148. else bd /= 10;
  149. serial->hw_base->US_BRGR = bd;
  150. serial->hw_base->US_CR = AT91C_US_RXEN | /* Receiver Enable */
  151. AT91C_US_TXEN; /* Transmitter Enable */
  152. /* reset rx index */
  153. serial->save_index = 0;
  154. serial->read_index = 0;
  155. /* reset rx buffer */
  156. rt_memset(serial->rx_buffer, 0, RT_UART_RX_BUFFER_SIZE);
  157. return RT_EOK;
  158. }
  159. static rt_err_t rt_serial_open(rt_device_t dev, rt_uint16_t oflag)
  160. {
  161. struct rt_at91serial *serial = (struct rt_at91serial*)dev;
  162. RT_ASSERT(serial != RT_NULL);
  163. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  164. {
  165. /* enable UART rx interrupt */
  166. serial->hw_base->US_IER = 1 << 0; /* RxReady interrupt */
  167. serial->hw_base->US_IMR |= 1 << 0; /* umask RxReady interrupt */
  168. /* install UART handler */
  169. rt_hw_interrupt_install(serial->peripheral_id, rt_hw_serial_isr, RT_NULL);
  170. AT91C_AIC_SMR(serial->peripheral_id) = 5 | (0x01 << 5);
  171. rt_hw_interrupt_umask(serial->peripheral_id);
  172. }
  173. return RT_EOK;
  174. }
  175. static rt_err_t rt_serial_close(rt_device_t dev)
  176. {
  177. struct rt_at91serial *serial = (struct rt_at91serial*)dev;
  178. RT_ASSERT(serial != RT_NULL);
  179. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  180. {
  181. /* disable interrupt */
  182. serial->hw_base->US_IDR = 1 << 0; /* RxReady interrupt */
  183. serial->hw_base->US_IMR &= ~(1 << 0); /* mask RxReady interrupt */
  184. }
  185. return RT_EOK;
  186. }
  187. static rt_size_t rt_serial_read (rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  188. {
  189. rt_uint8_t* ptr;
  190. struct rt_at91serial *serial = (struct rt_at91serial*)dev;
  191. RT_ASSERT(serial != RT_NULL);
  192. /* point to buffer */
  193. ptr = (rt_uint8_t*) buffer;
  194. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  195. {
  196. while (size)
  197. {
  198. /* interrupt receive */
  199. rt_base_t level;
  200. /* disable interrupt */
  201. level = rt_hw_interrupt_disable();
  202. if (serial->read_index != serial->save_index)
  203. {
  204. *ptr = serial->rx_buffer[serial->read_index];
  205. serial->read_index ++;
  206. if (serial->read_index >= RT_UART_RX_BUFFER_SIZE)
  207. serial->read_index = 0;
  208. }
  209. else
  210. {
  211. /* no data in rx buffer */
  212. /* enable interrupt */
  213. rt_hw_interrupt_enable(level);
  214. break;
  215. }
  216. /* enable interrupt */
  217. rt_hw_interrupt_enable(level);
  218. ptr ++; size --;
  219. }
  220. return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  221. }
  222. else if (dev->flag & RT_DEVICE_FLAG_DMA_RX)
  223. {
  224. /* not support right now */
  225. RT_ASSERT(0);
  226. }
  227. else
  228. {
  229. /* poll mode */
  230. while (size)
  231. {
  232. /* Wait for Full Rx Buffer */
  233. while (!(serial->hw_base->US_CSR & AT91C_US_RXRDY));
  234. /* Read Character */
  235. *ptr = serial->hw_base->US_RHR;
  236. ptr ++;
  237. size --;
  238. }
  239. return (rt_size_t)ptr - (rt_size_t)buffer;
  240. }
  241. return 0;
  242. }
  243. static rt_size_t rt_serial_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  244. {
  245. rt_uint8_t* ptr;
  246. struct rt_at91serial *serial = (struct rt_at91serial*)dev;
  247. RT_ASSERT(serial != RT_NULL);
  248. ptr = (rt_uint8_t*) buffer;
  249. if (dev->open_flag & RT_DEVICE_OFLAG_WRONLY)
  250. {
  251. if (dev->flag & RT_DEVICE_FLAG_STREAM)
  252. {
  253. /* it's a stream mode device */
  254. while (size)
  255. {
  256. /* stream mode */
  257. if (*ptr == '\n')
  258. {
  259. while (!(serial->hw_base->US_CSR & AT91C_US_TXRDY));
  260. serial->hw_base->US_THR = '\r';
  261. }
  262. /* Wait for Empty Tx Buffer */
  263. while (!(serial->hw_base->US_CSR & AT91C_US_TXRDY));
  264. /* Transmit Character */
  265. serial->hw_base->US_THR = *ptr;
  266. ptr ++; size --;
  267. }
  268. }
  269. else
  270. {
  271. while (size)
  272. {
  273. /* Wait for Empty Tx Buffer */
  274. while (!(serial->hw_base->US_CSR & AT91C_US_TXRDY));
  275. /* Transmit Character */
  276. serial->hw_base->US_THR = *ptr;
  277. ptr ++; size --;
  278. }
  279. }
  280. }
  281. return (rt_size_t)ptr - (rt_size_t)buffer;
  282. }
  283. static rt_err_t rt_serial_control (rt_device_t dev, rt_uint8_t cmd, void *args)
  284. {
  285. return RT_EOK;
  286. }
  287. rt_err_t rt_hw_serial_init()
  288. {
  289. rt_device_t device;
  290. #ifdef RT_USING_UART1
  291. device = (rt_device_t) &serial1;
  292. /* init serial device private data */
  293. serial1.hw_base = (struct rt_at91serial_hw*)AT91C_BASE_US0;
  294. serial1.peripheral_id = AT91C_ID_US0;
  295. serial1.baudrate = 115200;
  296. /* set device virtual interface */
  297. device->init = rt_serial_init;
  298. device->open = rt_serial_open;
  299. device->close = rt_serial_close;
  300. device->read = rt_serial_read;
  301. device->write = rt_serial_write;
  302. device->control = rt_serial_control;
  303. /* register uart1 on device subsystem */
  304. rt_device_register(device, "uart1", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
  305. #endif
  306. #ifdef RT_USING_UART2
  307. device = (rt_device_t) &serial2;
  308. serial2.hw_base = (struct rt_at91serial_hw*)AT91C_BASE_US1;
  309. serial2.peripheral_id = AT91C_ID_US1;
  310. serial2.baudrate = 115200;
  311. /* set device virtual interface */
  312. device->init = rt_serial_init;
  313. device->open = rt_serial_open;
  314. device->close = rt_serial_close;
  315. device->read = rt_serial_read;
  316. device->write = rt_serial_write;
  317. device->control = rt_serial_control;
  318. /* register uart2 on device subsystem */
  319. rt_device_register(device, "uart2", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
  320. #endif
  321. return RT_EOK;
  322. }
  323. /*@}*/