serial.c 9.7 KB

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