serial.c 9.8 KB

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