drv_usart.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "common.h"
  2. #include "drv_usart.h"
  3. /* transmit buffer and receive buffer */
  4. static uint8_t tx_buffer[TX_BUFFER_SIZE];
  5. static uint8_t rx_buffer[RX_BUFFER_SIZE];
  6. /* counter of transmit buffer and receive buffer */
  7. static uint16_t tx_count = 0, rx_count = 0;
  8. /* size of transmit buffer and receive buffer */
  9. static uint32_t /*rx_buffer_size ,*/ tx_buffer_size;
  10. DELAY_COMMON uart1_tout;
  11. void Enable_Uart1_Timer(uint8_t enable,uint16_t timeout)
  12. {
  13. if(enable)
  14. {
  15. uart1_tout.set = 1;
  16. uart1_tout.count = timeout;
  17. }
  18. else
  19. memset(&uart1_tout,0x00,sizeof(uart1_tout));
  20. }
  21. static void Reset_RX_Buffer(void)
  22. {
  23. memset(rx_buffer,0x00,sizeof(rx_buffer));
  24. rx_count = 0;
  25. }
  26. uint16_t Get_RS485_Data(uint8_t * dbuf,uint16_t dbuf_len)
  27. {
  28. if(dbuf == NULL || dbuf_len < rx_count)
  29. return 0;
  30. memcpy(dbuf,rx_buffer,rx_count);
  31. dbuf_len = rx_count;
  32. Reset_RX_Buffer();
  33. return dbuf_len;
  34. }
  35. int8_t Send_Data_RS485(uint8_t*data,uint16_t size)
  36. {
  37. if(data == NULL || size == 0 || size > sizeof(tx_buffer))
  38. return 0;
  39. memcpy(tx_buffer,data,size);
  40. tx_count = 0;
  41. tx_buffer_size = size;
  42. //
  43. #if 0
  44. do
  45. {
  46. while(RESET == usart_flag_get(USART0, USART_FLAG_TC));
  47. usart_data_transmit(USART0,tx_buffer[tx_count++]);
  48. }while(tx_count < tx_buffer_size);
  49. #else
  50. //while(RESET == USART_GetFlagStatus(USART1, USART_FLAG_TC));
  51. //USART_SendData(USART1,tx_buffer[tx_count++]);
  52. usart_interrupt_enable(USART0, USART_INT_TC);
  53. usart_interrupt_flag_get(USART0, USART_INT_TC);
  54. #endif
  55. return 1;
  56. }
  57. /*
  58. * 函数名:USART1_Config
  59. * 描述 :USART1 GPIO 配置,工作模式配置。9600 8-N-1
  60. * 输入 :无
  61. * 输出 : 无
  62. * 调用 :外部调用
  63. */
  64. void Usart1_Initial(void)
  65. {
  66. /* enable GPIO clock */
  67. rcu_periph_clock_enable(RCU_GPIOA);
  68. rcu_periph_clock_enable(RCU_AF);
  69. rcu_periph_clock_enable(RCU_USART0);
  70. /* connect port to USARTx_Tx */
  71. gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
  72. /* connect port to USARTx_Rx */
  73. gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
  74. /* USART configure */
  75. /* enable USART clock */
  76. usart_deinit(USART0);
  77. usart_baudrate_set(USART0, USART_0_BAUND);
  78. usart_word_length_set(USART0, USART_WL_8BIT);
  79. usart_stop_bit_set(USART0, USART_STB_1BIT);
  80. usart_parity_config(USART0, USART_PM_NONE);
  81. usart_hardware_flow_rts_config(USART0, USART_RTS_DISABLE);
  82. usart_hardware_flow_cts_config(USART0, USART_CTS_DISABLE);
  83. usart_receive_config(USART0, USART_RECEIVE_ENABLE);
  84. usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
  85. nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);
  86. nvic_irq_enable(USART0_IRQn, 1, 0);
  87. usart_interrupt_enable(USART0, USART_INT_RBNE);
  88. #if UART_0_IDAR
  89. /*USART_ClockInitTypeDef USART_ClockInitStruct;
  90. USART_ClockInitStruct.USART_Clock = USART_Clock_Disable;
  91. USART_ClockInitStruct.USART_CPOL = USART_CPOL_Low;
  92. USART_ClockInitStruct.USART_CPHA = USART_CPHA_2Edge;
  93. USART_ClockInitStruct.USART_LastBit = USART_LastBit_Disable;
  94. USART_ClockInit(USART1, &USART_ClockInitStruct);*/
  95. usart_enable(USART0);
  96. usart_prescaler_config(USART0, 0x01);
  97. usart_irda_lowpower_config(USART0, USART_IRLP_NORMAL);
  98. usart_irda_mode_enable(USART0);
  99. #else
  100. usart_enable(USART0);
  101. #endif
  102. }
  103. /*******************************************************************************
  104. * Function Name : USART1_IRQHandler
  105. * Description : This function handles USART1 global interrupt request.
  106. * Input : None
  107. * Output : None
  108. * Return : None
  109. *******************************************************************************/
  110. void USART0_IRQHandler(void)
  111. {
  112. uint8_t c;
  113. /* 串口接收 */
  114. //if(usart_interrupt_flag_get(USART0, USART_FLAG_RBNE) == SET)
  115. if(usart_flag_get(USART0, USART_FLAG_RBNE) == SET)
  116. {
  117. c = usart_data_receive(USART0);
  118. if (rx_count >= sizeof(rx_buffer))
  119. {
  120. return;
  121. }
  122. //USART_ReceiverTimeOutCmd(USART1,DISABLE);
  123. Enable_Uart1_Timer(0,0);
  124. /* Read one byte from the receive data register */
  125. rx_buffer[rx_count++] = c;
  126. Enable_Uart1_Timer(1,5);
  127. }
  128. /* 发送 */
  129. //if(usart_interrupt_flag_get(USART0, USART_FLAG_TC) == SET)
  130. if(usart_flag_get(USART0, USART_FLAG_TC) == SET)
  131. {
  132. //USART_ClearFlag(USART1, USART_FLAG_TC);
  133. if (tx_count >= tx_buffer_size)
  134. {
  135. /* Disable the EVAL_COM1 Transmit interrupt */
  136. usart_interrupt_disable(USART0,USART_INT_TC);
  137. //USART_ClearFlag(USART1, USART_FLAG_TC);
  138. return;
  139. }
  140. /* Write one byte to the transmit data register */
  141. usart_data_transmit(USART0,tx_buffer[tx_count++]);
  142. }
  143. }