board.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 "board.h"
  11. #include "inc/arc/arc_timer.h"
  12. #include "inc/arc/arc_exception.h"
  13. #include "inc/embARC_error.h"
  14. #include "mux.h"
  15. #include "dw_uart_obj.h"
  16. static void rt_hw_timer_isr(int vector, void *param)
  17. {
  18. arc_timer_int_clear(BOARD_OS_TIMER_ID);
  19. rt_tick_increase();
  20. }
  21. static void emsk_mux_init(void)
  22. {
  23. MUX_REG *mux_regs;
  24. mux_regs = (MUX_REG *)(PERIPHERAL_BASE|REL_REGBASE_PINMUX);
  25. mux_init(mux_regs);
  26. /**
  27. * + Please refer to corresponding EMSK User Guide for detailed information
  28. * -> Appendix: A Hardware Functional Description
  29. * -> Pmods Configuration summary
  30. * + Set up pin-multiplexer of all PMOD connections
  31. * - PM1 J1: Upper row as UART 0, lower row as SPI Slave
  32. * - PM2 J2: IIC 0 and run/halt signals
  33. * - PM3 J3: GPIO Port A and Port C
  34. * - PM4 J4: IIC 1 and Port D
  35. * - PM5 J5: Upper row as SPI Master, lower row as Port A
  36. * - PM6 J6: Upper row as SPI Master, lower row as Port A
  37. */
  38. set_pmod_mux(mux_regs, PM1_UR_UART_0 | PM1_LR_SPI_S \
  39. | PM2_I2C_HRI \
  40. | PM3_GPIO_AC \
  41. | PM4_I2C_GPIO_D \
  42. | PM5_UR_SPI_M1 | PM5_LR_GPIO_A \
  43. | PM6_UR_SPI_M0 | PM6_LR_GPIO_A );
  44. /**
  45. * PM1 upper row as UART
  46. * UM4:RXD, UM3:TXD
  47. * UM2:RTS_N, UM1:CTS_N
  48. */
  49. set_uart_map(mux_regs, 0xe4);
  50. }
  51. static struct rt_serial_device _emsk_uart0; //abstracted serial for RTT
  52. static struct rt_serial_device _emsk_uart1;
  53. static rt_err_t _configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  54. {
  55. DEV_UART_PTR uart;
  56. unsigned int id;
  57. int ret;
  58. id = (unsigned int)(serial->parent.user_data);
  59. uart = uart_get_dev(id);
  60. ret = uart->uart_control(UART_CMD_SET_BAUD, (void *)(cfg->baud_rate));
  61. if (ret != E_OK) {
  62. return RT_ERROR;
  63. }
  64. return RT_EOK;
  65. }
  66. static rt_err_t _control(struct rt_serial_device *serial, int cmd, void *arg)
  67. {
  68. DEV_UART_PTR uart;
  69. unsigned int id;
  70. id = (unsigned int)(serial->parent.user_data);
  71. uart = uart_get_dev(id);
  72. switch (cmd) {
  73. case RT_DEVICE_CTRL_CLR_INT:
  74. uart->uart_control(UART_CMD_SET_RXINT, (void *)0);
  75. break;
  76. case RT_DEVICE_CTRL_SET_INT:
  77. uart->uart_control(UART_CMD_SET_RXINT, (void *)1);
  78. break;
  79. case RT_DEVICE_CTRL_SUSPEND:
  80. uart->uart_control(UART_CMD_DIS_DEV, (void *)0);
  81. break;
  82. case RT_DEVICE_CTRL_RESUME:
  83. uart->uart_control(UART_CMD_ENA_DEV, (void *)0);
  84. break;
  85. default:
  86. return RT_ERROR;
  87. break;
  88. }
  89. return RT_EOK;
  90. }
  91. static int _putc(struct rt_serial_device *serial, char c)
  92. {
  93. DEV_UART_PTR uart;
  94. unsigned int id;
  95. int ret;
  96. id = (unsigned int)(serial->parent.user_data);
  97. uart = uart_get_dev(id);
  98. ret = uart->uart_write(&c, 1);
  99. if (ret < 0) {
  100. return -1;
  101. } else {
  102. return 1;
  103. }
  104. }
  105. static int _getc(struct rt_serial_device *serial)
  106. {
  107. DEV_UART_PTR uart;
  108. unsigned int id;
  109. unsigned int data;
  110. int ret;
  111. int rd_avail = 0;
  112. id = (unsigned int)(serial->parent.user_data);
  113. uart = uart_get_dev(id);
  114. uart->uart_control(UART_CMD_GET_RXAVAIL, (void *)(&rd_avail));
  115. if (rd_avail > 0) {
  116. ret = uart->uart_read(&data, 1);
  117. } else {
  118. return -1;
  119. }
  120. if (ret < 0) {
  121. return -1;
  122. } else {
  123. return data;
  124. }
  125. }
  126. static void _emsk_uart0_isr(void *ptr)
  127. {
  128. rt_hw_serial_isr((struct rt_serial_device*)&_emsk_uart0, RT_SERIAL_EVENT_RX_IND);
  129. }
  130. static const struct rt_uart_ops _emsk_uart0_ops =
  131. {
  132. _configure,
  133. _control,
  134. _putc,
  135. _getc,
  136. };
  137. static void _emsk_uart1_isr(void *ptr)
  138. {
  139. rt_hw_serial_isr((struct rt_serial_device*)&_emsk_uart1, RT_SERIAL_EVENT_RX_IND);
  140. }
  141. static const struct rt_uart_ops _emsk_uart1_ops =
  142. {
  143. _configure,
  144. _control,
  145. _putc,
  146. _getc,
  147. };
  148. int rt_hw_uart_init(void)
  149. {
  150. DEV_UART_PTR uart;
  151. struct serial_configure config;
  152. int ret;
  153. config.baud_rate = BAUD_RATE_115200;
  154. config.bit_order = BIT_ORDER_LSB;
  155. config.data_bits = DATA_BITS_8;
  156. config.parity = PARITY_NONE;
  157. config.stop_bits = STOP_BITS_1;
  158. config.invert = NRZ_NORMAL;
  159. config.bufsz = RT_SERIAL_RB_BUFSZ;
  160. _emsk_uart0.ops = &_emsk_uart0_ops;
  161. _emsk_uart0.config = config;
  162. _emsk_uart1.ops = &_emsk_uart1_ops;
  163. _emsk_uart1.config = config;
  164. /* open UART1 for USB-UART interface */
  165. uart = uart_get_dev(DW_UART_1_ID);
  166. /* default format: 8bits, no parity, 1 stop bits */
  167. ret = uart->uart_open(config.baud_rate);
  168. if (ret != E_OPNED && ret != E_OK) {
  169. return RT_ERROR;
  170. }
  171. /* enable rx int */
  172. uart->uart_control(UART_CMD_SET_RXINT, (void *)1);
  173. /* use customized int isr */
  174. uart->uart_control(UART_CMD_SET_RXCB, _emsk_uart1_isr);
  175. uart->uart_control(UART_CMD_SET_RXINT_BUF, NULL);
  176. rt_hw_serial_register(&_emsk_uart1, "uart1",
  177. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  178. (void *)DW_UART_1_ID);
  179. /* open UART0 in PMOD A*/
  180. uart = uart_get_dev(DW_UART_0_ID);
  181. /* default format: 8bits, no parity, 1 stop bits */
  182. ret = uart->uart_open(config.baud_rate);
  183. if (ret != E_OPNED && ret != E_OK) {
  184. return RT_ERROR;
  185. }
  186. /* enable rx int */
  187. uart->uart_control(UART_CMD_SET_RXINT, (void *)1);
  188. /* use customized int isr */
  189. uart->uart_control(UART_CMD_SET_RXCB, _emsk_uart0_isr);
  190. uart->uart_control(UART_CMD_SET_RXINT_BUF, NULL);
  191. rt_hw_serial_register(&_emsk_uart0, "uart0",
  192. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  193. (void *)DW_UART_0_ID);
  194. return RT_EOK;
  195. }
  196. void rt_hw_console_output(const char *str)
  197. {
  198. while(*str != '\0')
  199. {
  200. if (*str == '\n') {
  201. _putc(&_emsk_uart1,'\r');
  202. }
  203. _putc(&_emsk_uart1,*str);
  204. str++;
  205. }
  206. }
  207. int rt_hw_timer_init(void)
  208. {
  209. unsigned int cyc = BOARD_CPU_CLOCK / RT_TICK_PER_SECOND;
  210. int_disable(BOARD_OS_TIMER_INTNO); /* disable os timer interrupt */
  211. arc_timer_stop(BOARD_OS_TIMER_ID);
  212. arc_timer_start(BOARD_OS_TIMER_ID, TIMER_CTRL_IE | TIMER_CTRL_NH, cyc);
  213. int_handler_install(BOARD_OS_TIMER_INTNO, (INT_HANDLER)rt_hw_timer_isr);
  214. int_enable(BOARD_OS_TIMER_INTNO);
  215. return 0;
  216. }
  217. INIT_BOARD_EXPORT(rt_hw_timer_init);
  218. void rt_hw_board_init()
  219. {
  220. emsk_mux_init();
  221. rt_hw_uart_init();
  222. rt_components_board_init();
  223. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  224. }