uart1_timer2_rx.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2021 IOsetting <iosetting(at)outlook.com>
  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. #include "fw_hal.h"
  15. #define RING_BUFFER_SIZE 0x20
  16. __IDATA uint8_t ring_buffer[RING_BUFFER_SIZE];
  17. static volatile uint16_t from, to;
  18. void ring_buffer_reset(void);
  19. void ring_buffer_push(uint8_t c);
  20. uint8_t ring_buffer_pop(uint8_t *c);
  21. uint16_t ring_buffer_size(void);
  22. uint16_t ring_buffer_read(uint8_t *buf);
  23. INTERRUPT(UART1_Routine, EXTI_VectUART1)
  24. {
  25. uint8_t c;
  26. if (RI)
  27. {
  28. UART1_ClearRxInterrupt();
  29. c = SBUF;
  30. if (c == '\r' || c == '\n')
  31. {
  32. c = '\0';
  33. ring_buffer_push(c);
  34. }
  35. else if (c == 0x7F)
  36. {
  37. ring_buffer_pop(&c);
  38. }
  39. else
  40. {
  41. ring_buffer_push(c);
  42. }
  43. }
  44. }
  45. void main(void)
  46. {
  47. uint8_t msg[RING_BUFFER_SIZE], size;
  48. SYS_SetClock();
  49. // UART1, baud 115200, baud source Timer2, 1T mode, interrupt on
  50. UART1_Config8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200);
  51. UART1_SetRxState(HAL_State_ON);
  52. // Enable UART1 interrupt
  53. EXTI_Global_SetIntState(HAL_State_ON);
  54. EXTI_UART1_SetIntState(HAL_State_ON);
  55. // Initialize ring buffer
  56. ring_buffer_reset();
  57. while(1)
  58. {
  59. size = ring_buffer_read((uint8_t *)msg);
  60. if (size > 0)
  61. {
  62. ring_buffer_reset();
  63. UART1_TxString(msg);
  64. UART1_TxString("\r\n");
  65. }
  66. SYS_Delay(500);
  67. }
  68. }
  69. /**
  70. * Ring buffer methods
  71. */
  72. void ring_buffer_reset(void)
  73. {
  74. from = 0;
  75. to = 0;
  76. }
  77. void ring_buffer_push(uint8_t c)
  78. {
  79. ring_buffer[to++] = c;
  80. if (to >= RING_BUFFER_SIZE)
  81. {
  82. to = 0;
  83. }
  84. if (to == from)
  85. {
  86. from++;
  87. if (from >= RING_BUFFER_SIZE)
  88. {
  89. from = 0;
  90. }
  91. }
  92. }
  93. uint8_t ring_buffer_pop(uint8_t *c)
  94. {
  95. if (to != from)
  96. {
  97. to = (to > 0)? to - 1 : RING_BUFFER_SIZE - 1;
  98. *c = ring_buffer[to];
  99. return 1;
  100. }
  101. else
  102. {
  103. return 0;
  104. }
  105. }
  106. uint16_t ring_buffer_size(void)
  107. {
  108. if (to >= from)
  109. {
  110. return to - from;
  111. }
  112. else
  113. {
  114. return RING_BUFFER_SIZE - from + to;
  115. }
  116. }
  117. uint16_t ring_buffer_read(uint8_t *buf)
  118. {
  119. uint16_t pos;
  120. if (to >= from)
  121. {
  122. pos = from;
  123. while(pos < to)
  124. {
  125. *buf++ = ring_buffer[pos++];
  126. }
  127. return to - from;
  128. }
  129. else
  130. {
  131. pos = from;
  132. while(pos < RING_BUFFER_SIZE)
  133. {
  134. *buf++ = ring_buffer[pos++];
  135. }
  136. pos = 0;
  137. while(pos < to)
  138. {
  139. *buf++ = ring_buffer[pos++];
  140. }
  141. return RING_BUFFER_SIZE - from + to;
  142. }
  143. }