void rs422_uart_init(uint32_t baudrate, uint16_t data_bits,uint16_t parity, uint16_t stopbits)
{
rcu_periph_clock_enable(AS32_GPIO_RX_RCC);
rcu_periph_clock_enable(AS32_GPIO_TX_RCC);
rcu_periph_clock_enable(AS32_UART_RCC);
/* connect port to USARTx_Tx */
gpio_af_set(AS32_GPIO_TX_PORT, AS32_UART_AF, AS32_GPIO_TX_PIN);
/* connect port to USARTx_Rx */
gpio_af_set(AS32_GPIO_RX_PORT, AS32_UART_AF, AS32_GPIO_RX_PIN);
/* configure USART Tx as alternate function push-pull */
gpio_mode_set(AS32_GPIO_TX_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, AS32_GPIO_TX_PIN);
gpio_output_options_set(AS32_GPIO_TX_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, AS32_GPIO_TX_PIN);
/* configure USART Rx as alternate function push-pull */
gpio_mode_set(AS32_GPIO_RX_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, AS32_GPIO_RX_PIN);
gpio_output_options_set(AS32_GPIO_RX_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, AS32_GPIO_RX_PIN);
usart_deinit(AS32_UART);
usart_baudrate_set(AS32_UART, baudrate);
usart_transmit_config(AS32_UART, USART_TRANSMIT_ENABLE);
//nvic_irq_enable(AS32_UART_IRQ, 1, 1);
#ifdef AS32_DIR_PORT
rcu_periph_clock_enable(AS32_DIR_RCC);
gpio_output_options_set(AS32_DIR_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, AS32_DIR_PIN);
gpio_mode_set(AS32_DIR_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, AS32_DIR_PIN);
gpio_bit_reset(AS32_DIR_PORT, AS32_DIR_PIN);
#endif
usart_interrupt_flag_clear(RS485_UART,USART_INT_FLAG_RBNE_ORERR);
usart_interrupt_enable(AS32_UART,USART_INT_RBNE);
usart_receive_config(AS32_UART, USART_RECEIVE_ENABLE);
usart_enable(AS32_UART);
while(usart_flag_get(AS32_UART,USART_FLAG_REA) != SET);
}
#define AS32_UART USART1
#define AS32_UART_IRQ USART1_IRQn
#define AS32_UART_RCC RCU_USART1
#define AS32_UART_AF GPIO_AF_1
#define AS32_GPIO_TX_RCC RCU_GPIOA
#define AS32_GPIO_TX_PORT GPIOA
#define AS32_GPIO_TX_PIN GPIO_PIN_2
#define AS32_GPIO_RX_RCC RCU_GPIOA
#define AS32_GPIO_RX_PORT GPIOA
#define AS32_GPIO_RX_PIN GPIO_PIN_3
本来想好好支持一下国产的, 难道真的还是要放弃? |