rt_board.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (c) 2018, Synopsys, Inc.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <rthw.h>
  7. #include <rtthread.h>
  8. #include <rtdevice.h>
  9. #include <drivers/serial.h>
  10. #include "rt_board.h"
  11. #include "arc/arc_timer.h"
  12. #include "arc/arc_exception.h"
  13. #include "embARC_error.h"
  14. static rt_err_t _configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  15. {
  16. DEV_UART_PTR uart;
  17. unsigned int id;
  18. int ret;
  19. id = (unsigned int)(serial->parent.user_data);
  20. uart = uart_get_dev(id);
  21. ret = uart->uart_control(UART_CMD_SET_BAUD, (void *)(cfg->baud_rate));
  22. if (ret != E_OK) {
  23. return RT_ERROR;
  24. }
  25. return RT_EOK;
  26. }
  27. static rt_err_t _control(struct rt_serial_device *serial, int cmd, void *arg)
  28. {
  29. DEV_UART_PTR uart;
  30. unsigned int id;
  31. id = (unsigned int)(serial->parent.user_data);
  32. uart = uart_get_dev(id);
  33. switch (cmd) {
  34. case RT_DEVICE_CTRL_CLR_INT:
  35. uart->uart_control(UART_CMD_SET_RXINT, (void *)0);
  36. break;
  37. case RT_DEVICE_CTRL_SET_INT:
  38. uart->uart_control(UART_CMD_SET_RXINT, (void *)1);
  39. break;
  40. case RT_DEVICE_CTRL_SUSPEND:
  41. uart->uart_control(UART_CMD_DIS_DEV, (void *)0);
  42. break;
  43. case RT_DEVICE_CTRL_RESUME:
  44. uart->uart_control(UART_CMD_ENA_DEV, (void *)0);
  45. break;
  46. default:
  47. return RT_ERROR;
  48. break;
  49. }
  50. return RT_EOK;
  51. }
  52. static int _putc(struct rt_serial_device *serial, char c)
  53. {
  54. DEV_UART_PTR uart;
  55. unsigned int id;
  56. int ret;
  57. id = (unsigned int)(serial->parent.user_data);
  58. uart = uart_get_dev(id);
  59. ret = uart->uart_write(&c, 1);
  60. if (ret < 0) {
  61. return -1;
  62. } else {
  63. return 1;
  64. }
  65. }
  66. static int _getc(struct rt_serial_device *serial)
  67. {
  68. DEV_UART_PTR uart;
  69. unsigned int id;
  70. unsigned int data;
  71. int ret;
  72. int rd_avail = 0;
  73. id = (unsigned int)(serial->parent.user_data);
  74. uart = uart_get_dev(id);
  75. uart->uart_control(UART_CMD_GET_RXAVAIL, (void *)(&rd_avail));
  76. if (rd_avail > 0) {
  77. ret = uart->uart_read(&data, 1);
  78. } else {
  79. return -1;
  80. }
  81. if (ret < 0) {
  82. return -1;
  83. } else {
  84. return data;
  85. }
  86. }
  87. static const struct rt_uart_ops uart_ops =
  88. {
  89. _configure,
  90. _control,
  91. _putc,
  92. _getc,
  93. };
  94. static struct rt_serial_device uart0;
  95. static struct rt_serial_device uart1;
  96. static struct rt_serial_device uart2;
  97. static struct rt_serial_device uart3;
  98. static void uart0_isr(void *ptr)
  99. {
  100. rt_hw_serial_isr((struct rt_serial_device*)&uart0, RT_SERIAL_EVENT_RX_IND);
  101. }
  102. static void uart1_isr(void *ptr)
  103. {
  104. rt_hw_serial_isr((struct rt_serial_device*)&uart1, RT_SERIAL_EVENT_RX_IND);
  105. }
  106. static void uart2_isr(void *ptr)
  107. {
  108. rt_hw_serial_isr((struct rt_serial_device*)&uart2, RT_SERIAL_EVENT_RX_IND);
  109. }
  110. static void uart3_isr(void *ptr)
  111. {
  112. rt_hw_serial_isr((struct rt_serial_device*)&uart3, RT_SERIAL_EVENT_RX_IND);
  113. }
  114. int rt_hw_uart_init(void)
  115. {
  116. DEV_UART_PTR uart;
  117. struct serial_configure config;
  118. int ret;
  119. config.baud_rate = BAUD_RATE_115200;
  120. config.bit_order = BIT_ORDER_LSB;
  121. config.data_bits = DATA_BITS_8;
  122. config.parity = PARITY_NONE;
  123. config.stop_bits = STOP_BITS_1;
  124. config.invert = NRZ_NORMAL;
  125. config.bufsz = RT_SERIAL_RB_BUFSZ;
  126. uart0.ops = &uart_ops;
  127. uart0.config = config;
  128. uart1.ops = &uart_ops;
  129. uart1.config = config;
  130. uart2.ops = &uart_ops;
  131. uart2.config = config;
  132. uart3.ops = &uart_ops;
  133. uart3.config = config;
  134. /* uart0 init */
  135. uart = uart_get_dev(0);
  136. if (uart != NULL) {
  137. /* default format: 8bits, no parity, 1 stop bits */
  138. ret = uart->uart_open(config.baud_rate);
  139. if (ret != E_OPNED && ret != E_OK) {
  140. return RT_ERROR;
  141. }
  142. /* enable rx int */
  143. uart->uart_control(UART_CMD_SET_RXINT, (void *)0);
  144. /* use customized int isr */
  145. uart->uart_control(UART_CMD_SET_RXCB, uart0_isr);
  146. uart->uart_control(UART_CMD_SET_RXINT_BUF, NULL);
  147. rt_hw_serial_register(&uart0, "uart0",
  148. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  149. (void *)0);
  150. }
  151. /* uart1 init */
  152. uart = uart_get_dev(1);
  153. if (uart != NULL) {
  154. /* default format: 8bits, no parity, 1 stop bits */
  155. ret = uart->uart_open(config.baud_rate);
  156. if (ret != E_OPNED && ret != E_OK) {
  157. return RT_ERROR;
  158. }
  159. /* enable rx int */
  160. uart->uart_control(UART_CMD_SET_RXINT, (void *)0);
  161. /* use customized int isr */
  162. uart->uart_control(UART_CMD_SET_RXCB, uart1_isr);
  163. uart->uart_control(UART_CMD_SET_RXINT_BUF, NULL);
  164. rt_hw_serial_register(&uart1, "uart1",
  165. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  166. (void *)1);
  167. }
  168. /* uart2 init */
  169. uart = uart_get_dev(2);
  170. if (uart != NULL) {
  171. /* default format: 8bits, no parity, 1 stop bits */
  172. ret = uart->uart_open(config.baud_rate);
  173. if (ret != E_OPNED && ret != E_OK) {
  174. return RT_ERROR;
  175. }
  176. /* enable rx int */
  177. uart->uart_control(UART_CMD_SET_RXINT, (void *)0);
  178. /* use customized int isr */
  179. uart->uart_control(UART_CMD_SET_RXCB, uart2_isr);
  180. uart->uart_control(UART_CMD_SET_RXINT_BUF, NULL);
  181. rt_hw_serial_register(&uart2, "uart2",
  182. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  183. (void *)2);
  184. }
  185. /* uart3 init */
  186. uart = uart_get_dev(3);
  187. if (uart != NULL) {
  188. /* default format: 8bits, no parity, 1 stop bits */
  189. ret = uart->uart_open(config.baud_rate);
  190. if (ret != E_OPNED && ret != E_OK) {
  191. return RT_ERROR;
  192. }
  193. /* enable rx int */
  194. uart->uart_control(UART_CMD_SET_RXINT, (void *)0);
  195. /* use customized int isr */
  196. uart->uart_control(UART_CMD_SET_RXCB, uart3_isr);
  197. uart->uart_control(UART_CMD_SET_RXINT_BUF, NULL);
  198. rt_hw_serial_register(&uart3, "uart3",
  199. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  200. (void *)3);
  201. }
  202. return RT_EOK;
  203. }
  204. #if defined BOARD_EMSK
  205. #define CONSOLE_UART "uart1"
  206. struct rt_serial_device *console_uart = &uart1;
  207. #elif defined BOARD_IOTDK || defined BOARD_EMSDP || defined BOARD_NSIM
  208. #define CONSOLE_UART "uart0"
  209. struct rt_serial_device *console_uart = &uart0;
  210. #elif defined BOARD_AXS
  211. #define CONSOLE_UART "uart2"
  212. struct rt_serial_device *console_uart = &uart2;
  213. #elif defined BOARD_HSDK
  214. #define CONSOLE_UART "uart3"
  215. struct rt_serial_device *console_uart = &uart3;
  216. #else
  217. #error "no supported board selected!"
  218. #endif
  219. void rt_hw_console_output(const char *str)
  220. {
  221. while(*str != '\0')
  222. {
  223. if (*str == '\n') {
  224. _putc(console_uart,'\r');
  225. }
  226. _putc(console_uart,*str);
  227. str++;
  228. }
  229. }
  230. void rt_hw_board_init()
  231. {
  232. rt_hw_uart_init();
  233. rt_components_board_init();
  234. rt_console_set_device(CONSOLE_UART);
  235. }
  236. static void rt_hw_timer_isr(int vector, void *param)
  237. {
  238. timer_int_clear(BOARD_OS_TIMER_ID);
  239. rt_tick_increase();
  240. }
  241. int rt_hw_timer_init(void)
  242. {
  243. unsigned int cyc = BOARD_CPU_CLOCK / RT_TICK_PER_SECOND;
  244. int_disable(BOARD_OS_TIMER_INTNO); /* disable os timer interrupt */
  245. timer_stop(BOARD_OS_TIMER_ID);
  246. timer_start(BOARD_OS_TIMER_ID, TIMER_CTRL_IE | TIMER_CTRL_NH, cyc);
  247. int_handler_install(BOARD_OS_TIMER_INTNO, (INT_HANDLER_T)rt_hw_timer_isr);
  248. int_pri_set(BOARD_OS_TIMER_INTNO, INT_PRI_MIN + 1); /* currently, firq(INT_PRI_MIN) not supported*/
  249. int_enable(BOARD_OS_TIMER_INTNO);
  250. return 0;
  251. }
  252. INIT_BOARD_EXPORT(rt_hw_timer_init);