mcu_usart.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * File: mcu_usart.c
  3. * Date: 26.05.2011
  4. * Denis Zheleznjakov @ ZiBlog.ru
  5. */
  6. #include "main.h"
  7. struct
  8. {
  9. volatile uint8_t rx_buffer[USART_RX_BUFFER_SIZE];
  10. volatile uint8_t rx_buffer_head;
  11. volatile uint8_t rx_buffer_tail;
  12. volatile uint8_t tx_buffer[USART_TX_BUFFER_SIZE];
  13. volatile uint8_t tx_buffer_head;
  14. volatile uint8_t tx_buffer_tail;
  15. } usart_fifo;
  16. void mcu_usart_init(uint32_t baud_rate)
  17. {
  18. usart_fifo.rx_buffer_head = 0;
  19. usart_fifo.rx_buffer_tail = 0;
  20. usart_fifo.tx_buffer_head = 0;
  21. usart_fifo.tx_buffer_tail = 0;
  22. CLK_PeripheralClockConfig(CLK_Peripheral_USART1, ENABLE);
  23. USART_DeInit(USART1);
  24. USART_Init(USART1, baud_rate, USART_WordLength_8b, USART_StopBits_2, USART_Parity_No, (USART_Mode_TypeDef) (USART_Mode_Tx | USART_Mode_Rx));
  25. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  26. USART_Cmd(USART1, ENABLE);
  27. }
  28. _Bool mcu_usart_fifo_receive(uint8_t * data)
  29. {
  30. uint8_t rx_head;
  31. uint8_t rx_tail;
  32. rx_head = usart_fifo.rx_buffer_head;
  33. if (rx_head == usart_fifo.rx_buffer_tail)
  34. return FALSE;
  35. rx_tail = (uint8_t) ((usart_fifo.rx_buffer_tail + 1) & (uint8_t) (USART_RX_BUFFER_SIZE - 1));
  36. usart_fifo.rx_buffer_tail = rx_tail;
  37. *data = usart_fifo.rx_buffer[rx_tail];
  38. return TRUE;
  39. }
  40. void mcu_usart_fifo_transmit(uint8_t data)
  41. {
  42. uint8_t tx_head;
  43. tx_head = (uint8_t) ((usart_fifo.tx_buffer_head + 1) & (uint8_t) (USART_TX_BUFFER_SIZE - 1));
  44. while (tx_head == usart_fifo.tx_buffer_tail)
  45. {
  46. }
  47. usart_fifo.tx_buffer[tx_head] = data;
  48. usart_fifo.tx_buffer_head = tx_head;
  49. USART1->CR2 |= USART_CR2_TIEN;
  50. //USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
  51. }
  52. void mcu_usart_fifo_tx_handler(void)
  53. {
  54. uint8_t status;
  55. uint8_t tx_tail;
  56. uint8_t tx_head;
  57. status = USART1->SR;
  58. if (status & USART_SR_TC)
  59. {
  60. USART1->SR &= (uint8_t) (~USART_SR_TC);
  61. }
  62. if (status & USART_SR_TXE)
  63. {
  64. tx_head = usart_fifo.tx_buffer_head;
  65. tx_tail = usart_fifo.tx_buffer_tail;
  66. if (tx_head != tx_tail++)
  67. {
  68. tx_tail &= USART_TX_BUFFER_SIZE - 1;
  69. usart_fifo.tx_buffer_tail = tx_tail;
  70. USART1->DR = usart_fifo.tx_buffer[tx_tail];
  71. }
  72. else
  73. {
  74. USART1->CR2 &= (uint8_t) (~USART_CR2_TIEN);
  75. }
  76. }
  77. }
  78. void mcu_usart_fifo_rx_handler(void)
  79. {
  80. uint8_t rx_data;
  81. uint8_t rx_head;
  82. (void) USART1->SR;
  83. rx_data = USART1->DR;
  84. rx_head = (uint8_t) ((usart_fifo.rx_buffer_head + 1) & (uint8_t) (USART_RX_BUFFER_SIZE - 1));
  85. if (rx_head != usart_fifo.rx_buffer_tail)
  86. {
  87. usart_fifo.rx_buffer_head = rx_head;
  88. usart_fifo.rx_buffer[rx_head] = rx_data;
  89. }
  90. }