sw_fifo_2.c 9.5 KB

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