123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- #include "usart.h"
- void nvic_config(void);
- void gd_485_DE_pin_init(void)
- {
- /* enable the 485 DE pin clock */
- rcu_periph_clock_enable(DE485_GPIO_CLK);
- /* configure 485 DE GPIO port */
- gpio_init(DE485_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, DE485_PIN);
- GPIO_BC(DE485_GPIO_PORT) = DE485_PIN;
- }
- /*!
- \brief turn 485 to tx
- \param[out] none
- \retval none
- */
- void gd_485_DE_tx(void)
- {
- GPIO_BOP(DE485_GPIO_PORT) = DE485_PIN;
- }
- /*!
- \brief turn 485 to rx
- \param[out] none
- \retval none
- */
- void gd_485_DE_rx(void)
- {
- GPIO_BC(DE485_GPIO_PORT) = DE485_PIN;
- }
- /*!
- \brief COM port to send buff
- \param[in] com: COM on the board
- \arg COM_485
- \param[out] none
- \retval none
- */
- void gd_com_485_send(uint8_t *message,uint16_t size){
- uint16_t i=0;
- gd_485_DE_tx();
- for (i = 0; i < size; i++)
- {
- usart_data_transmit(COM_485, message[i]);
- while (RESET == usart_flag_get(COM_485, USART_FLAG_TC));
- }
- gd_485_DE_rx();
- }
- /*
- * 函数名:void config_485_port(uint32_t com,uint32_t baudrate, uint8_t databits, uint8_t stopbits, uint8_t parity, uint8_t flowcontrol)
- * 输入参数:com 串口,baudrate,databits,stopbits,parity,flowcontol 串口的配置参数
- * 输出参数:无
- * 返回值:无
- * 函数作用:配置485串口参数
- */
- void config_485_port(uint32_t com,uint32_t baudrate, uint8_t databits, uint8_t stopbits, uint8_t parity)
- {
- uint32_t com_id = 0U;
- uint8_t wordLength;
- if(parity!=0)
- {
- wordLength=databits+1;
- }else wordLength=databits;
- nvic_irq_enable(COM_485_IT_HANDLER, 0, 0);
- /* enable GPIO clock */
- rcu_periph_clock_enable(COM_485_GPIO_CLK);
- /* enable USART clock */
- rcu_periph_clock_enable(COM_485_CLK);
- /* connect port to USARTx_Tx */
- gpio_init(COM_485_GPIO_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, COM_485_TX_PIN);
- /* connect port to USARTx_Rx */
- gpio_init(COM_485_GPIO_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, COM_485_RX_PIN);
- //usart_deinit(com);
- usart_baudrate_set(com, baudrate);
- usart_word_length_set(com, (wordLength == 9) ? USART_WL_9BIT : USART_WL_8BIT);
- usart_stop_bit_set(com, (stopbits == 2) ? USART_STB_2BIT : USART_STB_1BIT);
- usart_parity_config(com, parity == 1 ? (USART_PM_ODD) : (parity == 2 ? USART_PM_EVEN : USART_PM_NONE));
- usart_hardware_flow_rts_config(com, USART_RTS_DISABLE);
- usart_hardware_flow_cts_config(com, USART_CTS_DISABLE);
- usart_receive_config(com, USART_RECEIVE_ENABLE);
- usart_transmit_config(com, USART_TRANSMIT_ENABLE);
- /* 使能串口 */
- usart_enable(com);
- // nvic_config();
- // usart_interrupt_enable(com, USART_INT_RBNE);
- // usart_interrupt_enable(com, USART_INT_IDLE);
- //解决485第一个帧数据第一个字节丢失问题
- usart_flag_clear(COM_485,USART_FLAG_TC);
- }
- void nvic_config(void)
- {
- nvic_irq_enable(USART0_IRQn, 0, 0);
- }
- void USART0_IRQHandler(void)
- {
- if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE))
- {
- usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE);
- }
- if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE))
- {
- usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE);
- }
- else
- {
- usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE);
- }
- }
|