uart.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. ////////////////////////////////////////////////////////////////////////////////////////
  2. /* enter necessary header files for proper interrupt vector and UART/USART visibility */
  3. ////////////////////////////////////////////////////////////////////////////////////////
  4. #include<stdio.h>
  5. #include "uart.h"
  6. #include "stm32f1xx_hal.h"
  7. static void usartRxIntHandler();
  8. static void usartTxIntHandler();
  9. volatile uint8_t uart_rx_fifo_not_empty_flag = 0;
  10. volatile uint8_t uart_rx_fifo_full_flag = 0;
  11. volatile uint8_t uart_rx_fifo_ovf_flag = 0;
  12. volatile uint8_t uart_tx_fifo_full_flag = 0;
  13. volatile uint8_t uart_tx_fifo_ovf_flag = 0;
  14. volatile uint8_t uart_tx_fifo_not_empty_flag = 0;
  15. // ********************************************** USART ************************************************
  16. extern UART_HandleTypeDef huart2;
  17. #ifdef __GNUC__
  18. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  19. #else
  20. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  21. #endif
  22. PUTCHAR_PROTOTYPE
  23. {
  24. unsigned char c=ch;
  25. while(uart_tx_fifo_full_flag);
  26. uart_send_byte(c);
  27. //HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xFFFF);
  28. }
  29. void usartInterrupt(void) {
  30. if( USART2->SR & USART_SR_TXE ) {
  31. usartTxIntHandler();
  32. }
  33. if( USART2->SR & USART_SR_RXNE ) {
  34. usartRxIntHandler();
  35. }
  36. }
  37. /*int fputc(int iChar, FILE *f)
  38. {
  39. unsigned char c=iChar;
  40. while(uart_tx_fifo_full_flag);
  41. uart_send_byte(c);
  42. return iChar;
  43. }
  44. int fgetc(FILE *f)
  45. {
  46. __IO int i=0;
  47. i++;
  48. return 1;
  49. }*/
  50. // ********************************************** USART ************************************************
  51. typedef struct {
  52. uint8_t data_buf[FIFO_BUFFER_SIZE]; // FIFO buffer
  53. uint16_t i_first; // index of oldest data byte in buffer
  54. uint16_t i_last; // index of newest data byte in buffer
  55. uint16_t num_bytes; // number of bytes currently in buffer
  56. }sw_fifo_typedef;
  57. sw_fifo_typedef rx_fifo = { {0}, 0, 0, 0 }; // declare a receive software buffer
  58. sw_fifo_typedef tx_fifo = { {0}, 0, 0, 0 }; // declare a transmit software buffer
  59. /***************************************************************************************************************/
  60. // UART receive interrupt sub-routine
  61. // - interrupts when valid data exists in rx hardware buffer
  62. // - checks if there's room in the rx software buffer
  63. // - if there's room, it transfers the received data into the sw buffer
  64. // - automatically handles "uart_rx_buffer_full_flag"
  65. // - sets overflow flag upon software buffer overflow (doesn't overwrite existing data)
  66. //////////////////////////////////////////////
  67. static void usartRxIntHandler() {
  68. //////////////////////////////////////////////
  69. if(rx_fifo.num_bytes == FIFO_BUFFER_SIZE) { // if the sw buffer is full
  70. uart_rx_fifo_ovf_flag = 1; // set the overflow flag
  71. }else if(rx_fifo.num_bytes < FIFO_BUFFER_SIZE) { // if there's room in the sw buffer
  72. rx_fifo.data_buf[rx_fifo.i_last] = USART2->DR; /* enter pointer to UART rx hardware buffer here */ // store the received data as the newest data element in the sw buffer
  73. rx_fifo.i_last++; // increment the index of the most recently added element
  74. rx_fifo.num_bytes++; // increment the bytes counter
  75. }
  76. if(rx_fifo.num_bytes == FIFO_BUFFER_SIZE) { // if sw buffer just filled up
  77. uart_rx_fifo_full_flag = 1; // set the RX FIFO full flag
  78. }
  79. if(rx_fifo.i_last == FIFO_BUFFER_SIZE) { // if the index has reached the end of the buffer,
  80. rx_fifo.i_last = 0; // roll over the index counter
  81. }
  82. uart_rx_fifo_not_empty_flag = 1; // set received-data flag
  83. }
  84. /***************************************************************************************************************/
  85. /***************************************************************************************************************/
  86. // UART transmit interrupt sub-routine
  87. // - interrupts when the tx hardware buffer is empty
  88. // - checks if data exists in the tx software buffer
  89. // - if data exists, it places the oldest element of the sw buffer into the tx hardware buffer
  90. // - if the sw buffer is emptied, it disables the "hw buffer empty" interrupt
  91. // - automatically handles "uart_tx_buffer_full_flag"
  92. //////////////////////////////////////////////
  93. static void usartTxIntHandler() {
  94. if(tx_fifo.num_bytes == FIFO_BUFFER_SIZE) { // if the sw buffer is full
  95. uart_tx_fifo_full_flag = 0; // clear the buffer full flag because we are about to make room
  96. }
  97. if(tx_fifo.num_bytes > 0) { // if data exists in the sw buffer
  98. USART2->DR = tx_fifo.data_buf[tx_fifo.i_first]; // place oldest data element in the TX hardware buffer
  99. tx_fifo.i_first++; // increment the index of the oldest element
  100. tx_fifo.num_bytes--; // decrement the bytes counter
  101. }
  102. if(tx_fifo.i_first == FIFO_BUFFER_SIZE) { // if the index has reached the end of the buffer,
  103. tx_fifo.i_first = 0; // roll over the index counter
  104. }
  105. if(tx_fifo.num_bytes == 0) { // if no more data exists
  106. uart_tx_fifo_not_empty_flag = 0; // clear flag
  107. USART2->CR1 &= ~(USART_CR1_TXEIE); // Disable TX Empty Interrupt
  108. }
  109. }
  110. /***************************************************************************************************************/
  111. /***************************************************************************************************************/
  112. // UART data transmit function
  113. // - checks if there's room in the transmit sw buffer
  114. // - if there's room, it transfers data byte to sw buffer
  115. // - automatically handles "uart_tx_buffer_full_flag"
  116. // - sets the overflow flag upon software buffer overflow (doesn't overwrite existing data)
  117. // - if this is the first data byte in the buffer, it enables the "hw buffer empty" interrupt
  118. void uart_send_byte(uint8_t byte) {
  119. ///////////////////////////////////////////////////////////
  120. /* disable interrupts while manipulating buffer pointers */
  121. ///////////////////////////////////////////////////////////
  122. unsigned int istate = USART2->CR1 & USART_CR1_TXEIE;
  123. USART2->CR1 &= ~(USART_CR1_TXEIE); // Disable TX Empty Interrupt
  124. if(tx_fifo.num_bytes == FIFO_BUFFER_SIZE) { // no room in the sw buffer
  125. uart_tx_fifo_ovf_flag = 1; // set the overflow flag
  126. }else if(tx_fifo.num_bytes < FIFO_BUFFER_SIZE) { // if there's room in the sw buffer
  127. tx_fifo.data_buf[tx_fifo.i_last] = byte; // transfer data byte to sw buffer
  128. tx_fifo.i_last++; // increment the index of the most recently added element
  129. tx_fifo.num_bytes++; // increment the bytes counter
  130. }
  131. if(tx_fifo.num_bytes == FIFO_BUFFER_SIZE) { // if sw buffer is full
  132. uart_tx_fifo_full_flag = 1; // set the TX FIFO full flag
  133. }
  134. if(tx_fifo.i_last == FIFO_BUFFER_SIZE) { // if the "new data" index has reached the end of the buffer,
  135. tx_fifo.i_last = 0; // roll over the index counter
  136. }
  137. ///////////////////////
  138. /* enable interrupts */
  139. ///////////////////////
  140. if( istate )
  141. USART2->CR1 |= USART_CR1_TXEIE;
  142. if(tx_fifo.num_bytes > 0) { // if there is data in the buffer
  143. uart_tx_fifo_not_empty_flag = 1; // set flag
  144. USART2->CR1 |= USART_CR1_TXEIE; // Enable TX Empty Interrupt
  145. }
  146. }
  147. int serialAvailable() {
  148. return rx_fifo.num_bytes;
  149. }
  150. /***************************************************************************************************************/
  151. // UART data receive function
  152. // - checks if data exists in the receive sw buffer
  153. // - if data exists, it returns the oldest element contained in the buffer
  154. // - automatically handles "uart_rx_buffer_full_flag"
  155. // - if no data exists, it clears the uart_rx_flag
  156. unsigned char serialRead(void) {
  157. ///////////////////////////////////////////////////////////
  158. /* disable interrupts while manipulating buffer pointers */
  159. ///////////////////////////////////////////////////////////
  160. USART2->CR1 &= ~(USART_CR1_RXNEIE); // Disable RX Empty Interrupt
  161. uint8_t byte = 0;
  162. if(rx_fifo.num_bytes == FIFO_BUFFER_SIZE) { // if the sw buffer is full
  163. uart_rx_fifo_full_flag = 0; // clear the buffer full flag because we are about to make room
  164. }
  165. if(rx_fifo.num_bytes > 0) { // if data exists in the sw buffer
  166. byte = rx_fifo.data_buf[rx_fifo.i_first]; // grab the oldest element in the buffer
  167. rx_fifo.i_first++; // increment the index of the oldest element
  168. rx_fifo.num_bytes--; // decrement the bytes counter
  169. }else{ // RX sw buffer is empty
  170. uart_rx_fifo_not_empty_flag = 0; // clear the rx flag
  171. }
  172. if(rx_fifo.i_first == FIFO_BUFFER_SIZE) { // if the index has reached the end of the buffer,
  173. rx_fifo.i_first = 0; // roll over the index counter
  174. }
  175. ///////////////////////
  176. /* enable interrupts */
  177. ///////////////////////
  178. USART2->CR1 |= USART_CR1_RXNEIE;
  179. return (unsigned char)byte; // return the data byte
  180. }