/** Configure the format. Set the number of bits, parity and the number of stop bits * * @param obj The serial object * @param data_bits The number of data bits * @param parity The parity * @param stop_bits The number of stop bits */ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { uint16_t uen_flag = 0U; struct serial_s *p_obj = GET_SERIAL_S(obj); /* store the UEN flag */ uen_flag = USART_CTL0(p_obj->uart) & USART_CTL0_UEN; /* disable the UART clock first */ usart_disable(p_obj->uart); /* configurate the UART parity */ switch (parity) { case ParityOdd: p_obj->parity = USART_PM_ODD; usart_parity_config(p_obj->uart, USART_PM_ODD); break; case ParityEven: p_obj->parity = USART_PM_EVEN; usart_parity_config(p_obj->uart, USART_PM_EVEN); break; case ParityForced0: case ParityForced1: default: p_obj->parity = USART_PM_NONE; usart_parity_config(p_obj->uart, USART_PM_NONE); break; } if (p_obj->parity == USART_PM_NONE) { if (data_bits == 9) { usart_word_length_set(p_obj->uart, USART_WL_9BIT); } else if (data_bits == 8) { usart_word_length_set(p_obj->uart, USART_WL_8BIT); } else if (data_bits == 7) { return; } } else { if (data_bits == 9) { return; } else if (data_bits == 8) { usart_word_length_set(p_obj->uart, USART_WL_9BIT); } else if (data_bits == 7) { usart_word_length_set(p_obj->uart, USART_WL_8BIT); } } if (stop_bits == 2) { usart_stop_bit_set(p_obj->uart, USART_STB_2BIT); } else { usart_stop_bit_set(p_obj->uart, USART_STB_1BIT); } /* restore the UEN flag */ if (RESET != uen_flag) { usart_enable(p_obj->uart); } }
static rt_err_t gd32_configure(struct rt_serial_device *serial, struct serial_configure *cfg) { struct gd32_uart *uart; RT_ASSERT(serial != RT_NULL); RT_ASSERT(cfg != RT_NULL); uart = (struct gd32_uart *)serial->parent.user_data; gd32_uart_gpio_init(uart); usart_baudrate_set(uart->uart_periph, cfg->baud_rate); switch (cfg->data_bits) { case DATA_BITS_9: usart_word_length_set(uart->uart_periph, USART_WL_9BIT); break; default: usart_word_length_set(uart->uart_periph, USART_WL_8BIT); break; } switch (cfg->stop_bits) { case STOP_BITS_2: usart_stop_bit_set(uart->uart_periph, USART_STB_2BIT); break; default: usart_stop_bit_set(uart->uart_periph, USART_STB_1BIT); break; } switch (cfg->parity) { case PARITY_ODD: usart_parity_config(uart->uart_periph, USART_PM_ODD); break; case PARITY_EVEN: usart_parity_config(uart->uart_periph, USART_PM_EVEN); break; default: usart_parity_config(uart->uart_periph, USART_PM_NONE); break; } usart_receive_config(uart->uart_periph, USART_RECEIVE_ENABLE); usart_transmit_config(uart->uart_periph, USART_TRANSMIT_ENABLE); usart_enable(uart->uart_periph); return RT_EOK; }
/** Initialize the USART peripheral. * * @param obj_s The serial object */ static void usart_init(struct serial_s *obj_s) { if (obj_s->index >= USART_NUM) { return; } /* USART configuration */ usart_deinit(obj_s->uart); usart_word_length_set(obj_s->uart, obj_s->databits); usart_baudrate_set(obj_s->uart, obj_s->baudrate); usart_stop_bit_set(obj_s->uart, obj_s->stopbits); usart_parity_config(obj_s->uart, obj_s->parity); #if DEVICE_SERIAL_FC if (obj_s->hw_flow_ctl == USART_HWCONTROL_NONE) { usart_hardware_flow_cts_config(obj_s->uart, USART_CTS_DISABLE); usart_hardware_flow_rts_config(obj_s->uart, USART_RTS_DISABLE); } else if (obj_s->hw_flow_ctl == USART_HWCONTROL_RTS) { usart_hardware_flow_cts_config(obj_s->uart, USART_CTS_DISABLE); usart_hardware_flow_rts_config(obj_s->uart, USART_RTS_ENABLE); } else if (obj_s->hw_flow_ctl == USART_HWCONTROL_CTS) { usart_hardware_flow_cts_config(obj_s->uart, USART_CTS_ENABLE); usart_hardware_flow_rts_config(obj_s->uart, USART_RTS_DISABLE); } else if (obj_s->hw_flow_ctl == USART_HWCONTROL_RTS_CTS) { usart_hardware_flow_cts_config(obj_s->uart, USART_CTS_ENABLE); usart_hardware_flow_rts_config(obj_s->uart, USART_RTS_ENABLE); } #endif /* DEVICE_SERIAL_FC */ usart_receive_config(obj_s->uart, USART_RECEIVE_ENABLE); usart_transmit_config(obj_s->uart, USART_TRANSMIT_ENABLE); usart_enable(obj_s->uart); }