usart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * File : usart.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2011-01-13 weety first version
  23. * 2013-07-21 weety using serial component
  24. */
  25. #include <rtthread.h>
  26. #include <rthw.h>
  27. #include <at91sam926x.h>
  28. #include <rtdevice.h>
  29. #define RXRDY 0x01
  30. #define TXRDY (1 << 1)
  31. typedef struct uartport
  32. {
  33. volatile rt_uint32_t CR;
  34. volatile rt_uint32_t MR;
  35. volatile rt_uint32_t IER;
  36. volatile rt_uint32_t IDR;
  37. volatile rt_uint32_t IMR;
  38. volatile rt_uint32_t CSR;
  39. volatile rt_uint32_t RHR;
  40. volatile rt_uint32_t THR;
  41. volatile rt_uint32_t BRGR;
  42. volatile rt_uint32_t RTOR;
  43. volatile rt_uint32_t TTGR;
  44. volatile rt_uint32_t reserved0[5];
  45. volatile rt_uint32_t FIDI;
  46. volatile rt_uint32_t NER;
  47. volatile rt_uint32_t reserved1;
  48. volatile rt_uint32_t IFR;
  49. volatile rt_uint32_t reserved2[44];
  50. volatile rt_uint32_t RPR;
  51. volatile rt_uint32_t RCR;
  52. volatile rt_uint32_t TPR;
  53. volatile rt_uint32_t TCR;
  54. volatile rt_uint32_t RNPR;
  55. volatile rt_uint32_t RNCR;
  56. volatile rt_uint32_t TNPR;
  57. volatile rt_uint32_t TNCR;
  58. volatile rt_uint32_t PTCR;
  59. volatile rt_uint32_t PTSR;
  60. }uartport;
  61. #define CIDR FIDI
  62. #define EXID NER
  63. #define FNR reserved1
  64. #define DBGU ((struct uartport *)AT91SAM9260_BASE_DBGU)
  65. #define UART0 ((struct uartport *)AT91SAM9260_BASE_US0)
  66. #define UART1 ((struct uartport *)AT91SAM9260_BASE_US1)
  67. #define UART2 ((struct uartport *)AT91SAM9260_BASE_US2)
  68. #define UART3 ((struct uartport *)AT91SAM9260_BASE_US3)
  69. struct at91_uart {
  70. uartport *port;
  71. int irq;
  72. };
  73. /**
  74. * This function will handle serial
  75. */
  76. void rt_at91_usart_handler(int vector, void *param)
  77. {
  78. int status;
  79. struct at91_uart *uart;
  80. rt_device_t dev = (rt_device_t)param;
  81. uart = (struct at91_uart *)dev->user_data;
  82. status = uart->port->CSR;
  83. if (!(status & uart->port->IMR))
  84. {
  85. return;
  86. }
  87. rt_interrupt_enter();
  88. rt_hw_serial_isr(dev);
  89. rt_interrupt_leave();
  90. }
  91. /**
  92. * UART device in RT-Thread
  93. */
  94. static rt_err_t at91_usart_configure(struct rt_serial_device *serial,
  95. struct serial_configure *cfg)
  96. {
  97. int div;
  98. int mode = 0;
  99. struct at91_uart *uart;
  100. RT_ASSERT(serial != RT_NULL);
  101. RT_ASSERT(cfg != RT_NULL);
  102. uart = (struct at91_uart *)serial->parent.user_data;
  103. uart->port->CR = AT91_US_RSTTX | AT91_US_RSTRX |
  104. AT91_US_RXDIS | AT91_US_TXDIS;
  105. mode |= AT91_US_USMODE_NORMAL | AT91_US_USCLKS_MCK |
  106. AT91_US_CHMODE_NORMAL;
  107. switch (cfg->data_bits)
  108. {
  109. case DATA_BITS_8:
  110. mode |= AT91_US_CHRL_8;
  111. break;
  112. case DATA_BITS_7:
  113. mode |= AT91_US_CHRL_7;
  114. break;
  115. case DATA_BITS_6:
  116. mode |= AT91_US_CHRL_6;
  117. break;
  118. case DATA_BITS_5:
  119. mode |= AT91_US_CHRL_5;
  120. break;
  121. default:
  122. mode |= AT91_US_CHRL_8;
  123. break;
  124. }
  125. switch (cfg->stop_bits)
  126. {
  127. case STOP_BITS_2:
  128. mode |= AT91_US_NBSTOP_2;
  129. break;
  130. case STOP_BITS_1:
  131. default:
  132. mode |= AT91_US_NBSTOP_1;
  133. break;
  134. }
  135. switch (cfg->parity)
  136. {
  137. case PARITY_ODD:
  138. mode |= AT91_US_PAR_ODD;
  139. break;
  140. case PARITY_EVEN:
  141. mode |= AT91_US_PAR_EVEN;
  142. break;
  143. case PARITY_NONE:
  144. default:
  145. mode |= AT91_US_PAR_NONE;
  146. break;
  147. }
  148. uart->port->MR = mode;
  149. div = (clk_get_rate(clk_get("mck")) / 16 + cfg->baud_rate/2) / cfg->baud_rate;
  150. uart->port->BRGR = div;
  151. uart->port->CR = AT91_US_RXEN | AT91_US_TXEN;
  152. uart->port->IER = 0x01;
  153. return RT_EOK;
  154. }
  155. static rt_err_t at91_usart_control(struct rt_serial_device *serial,
  156. int cmd, void *arg)
  157. {
  158. struct at91_uart* uart;
  159. RT_ASSERT(serial != RT_NULL);
  160. uart = (struct at91_uart *)serial->parent.user_data;
  161. switch (cmd)
  162. {
  163. case RT_DEVICE_CTRL_CLR_INT:
  164. /* disable rx irq */
  165. rt_hw_interrupt_mask(uart->irq);
  166. break;
  167. case RT_DEVICE_CTRL_SET_INT:
  168. /* enable rx irq */
  169. rt_hw_interrupt_umask(uart->irq);
  170. break;
  171. }
  172. return RT_EOK;
  173. }
  174. static int at91_usart_putc(struct rt_serial_device *serial, char c)
  175. {
  176. rt_uint32_t level;
  177. struct at91_uart *uart = serial->parent.user_data;
  178. while (!(uart->port->CSR & TXRDY));
  179. uart->port->THR = c;
  180. return 1;
  181. }
  182. static int at91_usart_getc(struct rt_serial_device *serial)
  183. {
  184. int result;
  185. struct at91_uart *uart = serial->parent.user_data;
  186. if (uart->port->CSR & RXRDY)
  187. {
  188. result = uart->port->RHR & 0xff;
  189. }
  190. else
  191. {
  192. result = -1;
  193. }
  194. return result;
  195. }
  196. static const struct rt_uart_ops at91_usart_ops =
  197. {
  198. at91_usart_configure,
  199. at91_usart_control,
  200. at91_usart_putc,
  201. at91_usart_getc,
  202. };
  203. #if defined(RT_USING_DBGU)
  204. static struct rt_serial_device serial_dbgu;
  205. static struct serial_ringbuffer dbgu_int_rx;
  206. struct at91_uart dbgu = {
  207. DBGU,
  208. AT91_ID_SYS
  209. };
  210. #endif
  211. #if defined(RT_USING_UART0)
  212. static struct rt_serial_device serial0;
  213. static struct serial_ringbuffer uart0_int_rx;
  214. struct at91_uart uart0 = {
  215. UART0,
  216. AT91SAM9260_ID_US0
  217. };
  218. #endif
  219. #if defined(RT_USING_UART1)
  220. static struct rt_serial_device serial1;
  221. static struct serial_ringbuffer uart1_int_rx;
  222. struct at91_uart uart1 = {
  223. UART1,
  224. AT91SAM9260_ID_US1
  225. };
  226. #endif
  227. #if defined(RT_USING_UART2)
  228. static struct rt_serial_device serial2;
  229. static struct serial_ringbuffer uart2_int_rx;
  230. struct at91_uart uart2 = {
  231. UART2,
  232. AT91SAM9260_ID_US2
  233. };
  234. #endif
  235. #if defined(RT_USING_UART3)
  236. static struct rt_serial_device serial3;
  237. static struct serial_ringbuffer uart3_int_rx;
  238. struct at91_uart uart3 = {
  239. UART3,
  240. AT91SAM9260_ID_US3
  241. };
  242. #endif
  243. void at91_usart_gpio_init(void)
  244. {
  245. rt_uint32_t val;
  246. #ifdef RT_USING_DBGU
  247. at91_sys_write(AT91_PIOB + PIO_IDR, (1<<14)|(1<<15));
  248. //at91_sys_write(AT91_PIOB + PIO_PUER, (1<<6));
  249. at91_sys_write(AT91_PIOB + PIO_PUDR, (1<<14)|(1<<15));
  250. at91_sys_write(AT91_PIOB + PIO_ASR, (1<<14)|(1<<15));
  251. at91_sys_write(AT91_PIOB + PIO_PDR, (1<<14)|(1<<15));
  252. at91_sys_write(AT91_PMC_PCER, 1 << AT91_ID_SYS);
  253. #endif
  254. #ifdef RT_USING_UART0
  255. at91_sys_write(AT91_PMC_PCER, 1 << AT91SAM9260_ID_US0);
  256. at91_sys_write(AT91_PIOB + PIO_IDR, (1<<4)|(1<<5));
  257. at91_sys_write(AT91_PIOB + PIO_PUER, (1<<4));
  258. at91_sys_write(AT91_PIOB + PIO_PUDR, (1<<5));
  259. at91_sys_write(AT91_PIOB + PIO_ASR, (1<<4)|(1<<5));
  260. at91_sys_write(AT91_PIOB + PIO_PDR, (1<<4)|(1<<5));
  261. #endif
  262. #ifdef RT_USING_UART1
  263. at91_sys_write(AT91_PMC_PCER, 1 << AT91SAM9260_ID_US1);
  264. at91_sys_write(AT91_PIOB + PIO_IDR, (1<<6)|(1<<7));
  265. at91_sys_write(AT91_PIOB + PIO_PUER, (1<<6));
  266. at91_sys_write(AT91_PIOB + PIO_PUDR, (1<<7));
  267. at91_sys_write(AT91_PIOB + PIO_ASR, (1<<6)|(1<<7));
  268. at91_sys_write(AT91_PIOB + PIO_PDR, (1<<6)|(1<<7));
  269. #endif
  270. #ifdef RT_USING_UART2
  271. at91_sys_write(AT91_PMC_PCER, 1 << AT91SAM9260_ID_US2);
  272. at91_sys_write(AT91_PIOB + PIO_IDR, (1<<8)|(1<<9));
  273. at91_sys_write(AT91_PIOB + PIO_PUER, (1<<8));
  274. at91_sys_write(AT91_PIOB + PIO_PUDR, (1<<9));
  275. at91_sys_write(AT91_PIOB + PIO_ASR, (1<<8)|(1<<9));
  276. at91_sys_write(AT91_PIOB + PIO_PDR, (1<<8)|(1<<9));
  277. #endif
  278. #ifdef RT_USING_UART3
  279. at91_sys_write(AT91_PMC_PCER, 1<<AT91SAM9260_ID_US3);
  280. at91_sys_write(AT91_PIOB + PIO_IDR, (1<<10)|(1<<11));
  281. at91_sys_write(AT91_PIOB + PIO_PUER, (1<<10));
  282. at91_sys_write(AT91_PIOB + PIO_PUDR, (1<<11));
  283. at91_sys_write(AT91_PIOB + PIO_ASR, (1<<10)|(1<<11));
  284. at91_sys_write(AT91_PIOB + PIO_PDR, (1<<10)|(1<<11));
  285. #endif
  286. }
  287. /**
  288. * This function will handle init uart
  289. */
  290. void rt_hw_uart_init(void)
  291. {
  292. at91_usart_gpio_init();
  293. #if defined(RT_USING_DBGU)
  294. serial_dbgu.ops = &at91_usart_ops;
  295. serial_dbgu.int_rx = &dbgu_int_rx;
  296. serial_dbgu.config.baud_rate = BAUD_RATE_115200;
  297. serial_dbgu.config.bit_order = BIT_ORDER_LSB;
  298. serial_dbgu.config.data_bits = DATA_BITS_8;
  299. serial_dbgu.config.parity = PARITY_NONE;
  300. serial_dbgu.config.stop_bits = STOP_BITS_1;
  301. serial_dbgu.config.invert = NRZ_NORMAL;
  302. /* register vcom device */
  303. rt_hw_serial_register(&serial_dbgu, "dbgu",
  304. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  305. &dbgu);
  306. #endif
  307. #if defined(RT_USING_UART0)
  308. serial0.ops = &at91_usart_ops;
  309. serial0.int_rx = &uart0_int_rx;
  310. serial0.config.baud_rate = BAUD_RATE_115200;
  311. serial0.config.bit_order = BIT_ORDER_LSB;
  312. serial0.config.data_bits = DATA_BITS_8;
  313. serial0.config.parity = PARITY_NONE;
  314. serial0.config.stop_bits = STOP_BITS_1;
  315. serial0.config.invert = NRZ_NORMAL;
  316. /* register vcom device */
  317. rt_hw_serial_register(&serial0, "uart0",
  318. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  319. &uart0);
  320. rt_hw_interrupt_install(uart0.irq, rt_at91_usart_handler,
  321. (void *)&(serial0.parent), "UART0");
  322. rt_hw_interrupt_umask(uart0.irq);
  323. #endif
  324. #if defined(RT_USING_UART1)
  325. serial1.ops = &at91_usart_ops;
  326. serial1.int_rx = &uart1_int_rx;
  327. serial1.config.baud_rate = BAUD_RATE_115200;
  328. serial1.config.bit_order = BIT_ORDER_LSB;
  329. serial1.config.data_bits = DATA_BITS_8;
  330. serial1.config.parity = PARITY_NONE;
  331. serial1.config.stop_bits = STOP_BITS_1;
  332. serial1.config.invert = NRZ_NORMAL;
  333. /* register vcom device */
  334. rt_hw_serial_register(&serial1, "uart1",
  335. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  336. &uart1);
  337. rt_hw_interrupt_install(uart1.irq, rt_at91_usart_handler,
  338. (void *)&(serial1.parent), "UART1");
  339. rt_hw_interrupt_umask(uart1.irq);
  340. #endif
  341. #if defined(RT_USING_UART2)
  342. serial2.ops = &at91_usart_ops;
  343. serial2.int_rx = &uart2_int_rx;
  344. serial2.config.baud_rate = BAUD_RATE_115200;
  345. serial2.config.bit_order = BIT_ORDER_LSB;
  346. serial2.config.data_bits = DATA_BITS_8;
  347. serial2.config.parity = PARITY_NONE;
  348. serial2.config.stop_bits = STOP_BITS_1;
  349. serial2.config.invert = NRZ_NORMAL;
  350. /* register vcom device */
  351. rt_hw_serial_register(&serial2, "uart2",
  352. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  353. &uart2);
  354. rt_hw_interrupt_install(uart2.irq, rt_at91_usart_handler,
  355. (void *)&(serial2.parent), "UART2");
  356. rt_hw_interrupt_umask(uart2.irq);
  357. #endif
  358. #if defined(RT_USING_UART3)
  359. serial3.ops = &at91_usart_ops;
  360. serial3.int_rx = &uart3_int_rx;
  361. serial3.config.baud_rate = BAUD_RATE_115200;
  362. serial3.config.bit_order = BIT_ORDER_LSB;
  363. serial3.config.data_bits = DATA_BITS_8;
  364. serial3.config.parity = PARITY_NONE;
  365. serial3.config.stop_bits = STOP_BITS_1;
  366. serial3.config.invert = NRZ_NORMAL;
  367. /* register vcom device */
  368. rt_hw_serial_register(&serial3, "uart3",
  369. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  370. &uart3);
  371. rt_hw_interrupt_install(uart3.irq, rt_at91_usart_handler,
  372. (void *)&(serial3.parent), "UART3");
  373. rt_hw_interrupt_umask(uart3.irq);
  374. #endif
  375. }
  376. #ifdef RT_USING_DBGU
  377. void rt_dbgu_isr(void)
  378. {
  379. rt_at91_usart_handler(dbgu.irq, &(serial_dbgu.parent));
  380. }
  381. #endif