sw_fifo.c 9.0 KB

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