uart.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Copyright 2023 Dual Tachyon
  2. * https://github.com/DualTachyon
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdbool.h>
  17. #include "bsp/dp32g030/dma.h"
  18. #include "bsp/dp32g030/syscon.h"
  19. #include "bsp/dp32g030/uart.h"
  20. #include "driver/uart.h"
  21. #include "external/printf/printf.h"
  22. static bool UART_IsLogEnabled;
  23. uint8_t UART_DMA_Buffer[256];
  24. void UART_Init(void)
  25. {
  26. uint32_t Delta;
  27. uint32_t Positive;
  28. uint32_t Frequency;
  29. UART1->CTRL = (UART1->CTRL & ~UART_CTRL_UARTEN_MASK) | UART_CTRL_UARTEN_BITS_DISABLE;
  30. Delta = SYSCON_RC_FREQ_DELTA;
  31. Positive = (Delta & SYSCON_RC_FREQ_DELTA_RCHF_SIG_MASK) >> SYSCON_RC_FREQ_DELTA_RCHF_SIG_SHIFT;
  32. Frequency = (Delta & SYSCON_RC_FREQ_DELTA_RCHF_DELTA_MASK) >> SYSCON_RC_FREQ_DELTA_RCHF_DELTA_SHIFT;
  33. if (Positive) {
  34. Frequency += 48000000U;
  35. } else {
  36. Frequency = 48000000U - Frequency;
  37. }
  38. // 48M, the baud rate is set to 115200, then UARTDIV=48000000/115200=416.6, 417 can be selected based on rounding.
  39. UART1->BAUD = Frequency / 39053U;
  40. //UART1->BAUD = 48000000U / 38400U;
  41. //UART1->BAUD = Frequency / 115200;
  42. //UART1->BAUD = 48000000U / 128000;
  43. UART1->CTRL = UART_CTRL_RXEN_BITS_ENABLE | UART_CTRL_TXEN_BITS_ENABLE | UART_CTRL_RXDMAEN_BITS_ENABLE;
  44. UART1->RXTO = 4;
  45. UART1->FC = 0;
  46. UART1->FIFO = UART_FIFO_RF_LEVEL_BITS_8_BYTE | UART_FIFO_RF_CLR_BITS_ENABLE | UART_FIFO_TF_CLR_BITS_ENABLE;
  47. UART1->IE = 0;
  48. DMA_CTR = (DMA_CTR & ~DMA_CTR_DMAEN_MASK) | DMA_CTR_DMAEN_BITS_DISABLE;
  49. DMA_CH0->MSADDR = (uint32_t)(uintptr_t)&UART1->RDR;
  50. DMA_CH0->MDADDR = (uint32_t)(uintptr_t)UART_DMA_Buffer;
  51. DMA_CH0->MOD = 0
  52. // Source
  53. | DMA_CH_MOD_MS_ADDMOD_BITS_NONE
  54. | DMA_CH_MOD_MS_SIZE_BITS_8BIT
  55. | DMA_CH_MOD_MS_SEL_BITS_HSREQ_MS1
  56. // Destination
  57. | DMA_CH_MOD_MD_ADDMOD_BITS_INCREMENT
  58. | DMA_CH_MOD_MD_SIZE_BITS_8BIT
  59. | DMA_CH_MOD_MD_SEL_BITS_SRAM
  60. ;
  61. DMA_INTEN = 0;
  62. DMA_INTST = 0
  63. | DMA_INTST_CH0_TC_INTST_BITS_SET
  64. | DMA_INTST_CH1_TC_INTST_BITS_SET
  65. | DMA_INTST_CH2_TC_INTST_BITS_SET
  66. | DMA_INTST_CH3_TC_INTST_BITS_SET
  67. | DMA_INTST_CH0_THC_INTST_BITS_SET
  68. | DMA_INTST_CH1_THC_INTST_BITS_SET
  69. | DMA_INTST_CH2_THC_INTST_BITS_SET
  70. | DMA_INTST_CH3_THC_INTST_BITS_SET
  71. ;
  72. DMA_CH0->CTR = 0
  73. | DMA_CH_CTR_CH_EN_BITS_ENABLE
  74. | ((0xFF << DMA_CH_CTR_LENGTH_SHIFT) & DMA_CH_CTR_LENGTH_MASK)
  75. | DMA_CH_CTR_LOOP_BITS_ENABLE
  76. | DMA_CH_CTR_PRI_BITS_MEDIUM
  77. ;
  78. UART1->IF = UART_IF_RXTO_BITS_SET;
  79. DMA_CTR = (DMA_CTR & ~DMA_CTR_DMAEN_MASK) | DMA_CTR_DMAEN_BITS_ENABLE;
  80. UART1->CTRL |= UART_CTRL_UARTEN_BITS_ENABLE;
  81. }
  82. void UART_Send(const void *pBuffer, uint32_t Size)
  83. {
  84. const uint8_t *pData = (const uint8_t *)pBuffer;
  85. uint32_t i;
  86. for (i = 0; i < Size; i++) {
  87. UART1->TDR = pData[i];
  88. while ((UART1->IF & UART_IF_TXFIFO_FULL_MASK) != UART_IF_TXFIFO_FULL_BITS_NOT_SET) {
  89. }
  90. }
  91. }
  92. void UART_LogSend(const void *pBuffer, uint32_t Size)
  93. {
  94. if (UART_IsLogEnabled) {
  95. UART_Send(pBuffer, Size);
  96. }
  97. }
  98. void UART_printf(const char *str, ...)
  99. {
  100. char text[256];
  101. int len;
  102. va_list va;
  103. va_start(va, str);
  104. len = vsnprintf(text, sizeof(text), str, va);
  105. va_end(va);
  106. UART_Send(text, len);
  107. //UART_Send(text, strlen(text));
  108. }