uarths.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* Copyright 2018 Canaan Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include <stdint.h>
  16. #include <stdio.h>
  17. #include "encoding.h"
  18. #include "sysctl.h"
  19. #include "uarths.h"
  20. volatile uarths_t *const uarths = (volatile uarths_t *)UARTHS_BASE_ADDR;
  21. typedef struct _uarths_instance
  22. {
  23. plic_irq_callback_t callback;
  24. void *ctx;
  25. uarths_interrupt_mode_t uarths_interrupt_mode;
  26. } uarths_instance_t;
  27. uarths_instance_t g_uarths_instance;
  28. uarths_interrupt_mode_t uarths_get_interrupt_mode(void)
  29. {
  30. uint32_t v_rx_interrupt = uarths->ip.rxwm;
  31. uint32_t v_tx_interrupt = uarths->ip.txwm;
  32. return (v_rx_interrupt << 1) | v_tx_interrupt;
  33. }
  34. int uarths_irq_callback(void *ctx)
  35. {
  36. uarths_instance_t *uart_context = (uarths_instance_t *)ctx;
  37. if(uart_context->callback)
  38. uart_context->callback(uart_context->ctx);
  39. return 0;
  40. }
  41. void uarths_set_interrupt_cnt(uarths_interrupt_mode_t interrupt_mode, uint8_t cnt)
  42. {
  43. switch(interrupt_mode)
  44. {
  45. case UARTHS_SEND:
  46. uarths->txctrl.txcnt = cnt;
  47. break;
  48. case UARTHS_RECEIVE:
  49. uarths->rxctrl.rxcnt = cnt;
  50. break;
  51. case UARTHS_SEND_RECEIVE:
  52. default:
  53. uarths->txctrl.txcnt = cnt;
  54. uarths->rxctrl.rxcnt = cnt;
  55. break;
  56. }
  57. }
  58. void uarths_set_irq(uarths_interrupt_mode_t interrupt_mode, plic_irq_callback_t uarths_callback, void *ctx, uint32_t priority)
  59. {
  60. g_uarths_instance.callback = uarths_callback;
  61. g_uarths_instance.ctx = ctx;
  62. switch(interrupt_mode)
  63. {
  64. case UARTHS_SEND:
  65. uarths->ie.txwm = 1;
  66. uarths->ie.rxwm = 0;
  67. break;
  68. case UARTHS_RECEIVE:
  69. uarths->ie.txwm = 0;
  70. uarths->ie.rxwm = 1;
  71. break;
  72. default:
  73. uarths->ie.txwm = 1;
  74. uarths->ie.rxwm = 1;
  75. break;
  76. }
  77. g_uarths_instance.uarths_interrupt_mode = interrupt_mode;
  78. plic_set_priority(IRQN_UARTHS_INTERRUPT, priority);
  79. plic_irq_register(IRQN_UARTHS_INTERRUPT, uarths_irq_callback, &g_uarths_instance);
  80. plic_irq_enable(IRQN_UARTHS_INTERRUPT);
  81. }
  82. int uarths_putchar(char c)
  83. {
  84. while(uarths->txdata.full)
  85. continue;
  86. uarths->txdata.data = (uint8_t)c;
  87. return (c & 0xff);
  88. }
  89. int uarths_getchar(void)
  90. {
  91. /* while not empty */
  92. uarths_rxdata_t recv = uarths->rxdata;
  93. if(recv.empty)
  94. return EOF;
  95. else
  96. return (recv.data & 0xff);
  97. }
  98. /* [Deprecated] this function will remove in future */
  99. int uarths_getc(void) __attribute__((weak, alias("uarths_getchar")));
  100. size_t uarths_receive_data(uint8_t *buf, size_t buf_len)
  101. {
  102. size_t i;
  103. for(i = 0; i < buf_len; i++)
  104. {
  105. uarths_rxdata_t recv = uarths->rxdata;
  106. if(recv.empty)
  107. break;
  108. else
  109. buf[i] = (recv.data & 0xFF);
  110. }
  111. return i;
  112. }
  113. size_t uarths_send_data(const uint8_t *buf, size_t buf_len)
  114. {
  115. size_t write = 0;
  116. while(write < buf_len)
  117. {
  118. uarths_putchar(*buf++);
  119. write++;
  120. }
  121. return write;
  122. }
  123. int uarths_puts(const char *s)
  124. {
  125. while(*s)
  126. if(uarths_putchar(*s++) != 0)
  127. return -1;
  128. return 0;
  129. }
  130. void uarths_init(void)
  131. {
  132. uint32_t freq = sysctl_clock_get_freq(SYSCTL_CLOCK_CPU);
  133. uint16_t div = freq / 115200 - 1;
  134. /* Set UART registers */
  135. uarths->div.div = div;
  136. uarths->txctrl.txen = 1;
  137. uarths->rxctrl.rxen = 1;
  138. uarths->txctrl.txcnt = 0;
  139. uarths->rxctrl.rxcnt = 0;
  140. uarths->ip.txwm = 1;
  141. uarths->ip.rxwm = 1;
  142. uarths->ie.txwm = 0;
  143. uarths->ie.rxwm = 1;
  144. }
  145. void uarths_config(uint32_t baud_rate, uarths_stopbit_t stopbit)
  146. {
  147. uint32_t freq = sysctl_clock_get_freq(SYSCTL_CLOCK_CPU);
  148. uint16_t div = freq / baud_rate - 1;
  149. uarths->div.div = div;
  150. uarths->txctrl.nstop = stopbit;
  151. }