uart.c 9.3 KB

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