Added serial config parameter support to HardwareSerial :: begin(), so that word length (of 8 or 9 bits), parity (None, Even or Odd) and 1 or 2 stop bits can now be set. Note the STM32 doesnt support all the word lengths supprted by AVR or SAM e.g. the hardware doesnt support 5,6 or 7 bit word lengths.

This commit is contained in:
Roger Clark 2015-03-04 22:59:11 +11:00
parent 29c5d538da
commit f21592574f
4 changed files with 64 additions and 27 deletions

View File

@ -135,7 +135,7 @@ void HardwareSerial::begin(uint32 baud, uint8_t config)
usart_config_gpios_async(this->usart_device,
rxi->gpio_device, rxi->gpio_bit,
txi->gpio_device, txi->gpio_bit,
0);
config);
usart_init(this->usart_device);
usart_set_baud_rate(this->usart_device, USART_USE_PCLK, baud);
usart_enable(this->usart_device);

View File

@ -82,30 +82,24 @@ struct usart_dev;
* Note. The values will need to be changed to match STM32 USART config register values, these are just place holders.
*/
// Define config for Serial.begin(baud, config);
#define SERIAL_5N1 0x00
#define SERIAL_6N1 0x02
#define SERIAL_7N1 0x04
#define SERIAL_8N1 0x06
#define SERIAL_5N2 0x08
#define SERIAL_6N2 0x0A
#define SERIAL_7N2 0x0C
#define SERIAL_8N2 0x0E
#define SERIAL_5E1 0x20
#define SERIAL_6E1 0x22
#define SERIAL_7E1 0x24
#define SERIAL_8E1 0x26
#define SERIAL_5E2 0x28
#define SERIAL_6E2 0x2A
#define SERIAL_7E2 0x2C
#define SERIAL_8E2 0x2E
#define SERIAL_5O1 0x30
#define SERIAL_6O1 0x32
#define SERIAL_7O1 0x34
#define SERIAL_8O1 0x36
#define SERIAL_5O2 0x38
#define SERIAL_6O2 0x3A
#define SERIAL_7O2 0x3C
#define SERIAL_8O2 0x3E
// Note. STM32 doesn't support as many different Serial modes as AVR or SAM cores.
#define SERIAL_8N1 0B00000000
#define SERIAL_8N2 0B00100000
#define SERIAL_9N1 0B00001000
#define SERIAL_9N2 0B00101000
#define SERIAL_8E1 0B00000010
#define SERIAL_8E2 0B00100010
#define SERIAL_9E1 0B00001010
#define SERIAL_9E2 0B00101010
#define SERIAL_8O1 0B00000011
#define SERIAL_8O2 0B00100011
#define SERIAL_9O1 0B00001011
#define SERIAL_9O2 0B00101011
/* Roger clark. Changed class inheritance from Print to Stream.
* Also added new functions for peek() and availableForWrite()

View File

@ -57,8 +57,7 @@ void usart_init(usart_dev *dev) {
*/
void usart_enable(usart_dev *dev) {
usart_reg_map *regs = dev->regs;
regs->CR1 = (USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE |
USART_CR1_M_8N1);
regs->CR1 |= (USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE);// don't change the word length etc, and 'or' in the patten not overwrite |USART_CR1_M_8N1);
regs->CR1 |= USART_CR1_UE;
}

View File

@ -107,6 +107,50 @@ void usart_config_gpios_async(usart_dev *udev,
unsigned flags) {
gpio_set_mode(rx_dev, rx, GPIO_INPUT_FLOATING);
gpio_set_mode(tx_dev, tx, GPIO_AF_OUTPUT_PP);
/*
CR1 bit 12 Word length 0=8 1=9
CR1 bit 11 wake (default value is 0) we can safely set this value to 0 (zero) each time
CR1 bit 10 parity enable (1 = enabled)
CR1 bit 9 Parity selection 0 = Even 1 = Odd
CR2 bits 13 and 12 00 = 1 01 = 0.5 10 = 2 11 = 1.5
Not all USARTs support 1.5 or 0.5 bits so its best to avoid them.
CR2 CR1
0B00 0000
0B10 0000
0B00 1000
0B10 1000
0B00 0010
0B10 0010
0B00 1010
0B10 1010
0B00 0011
0B10 0011
0B00 1011
0B10 1011
#define SERIAL_8N1 0B 0000 0000
#define SERIAL_8N2 0B 0010 0000
#define SERIAL_9N1 0B 0000 1000
#define SERIAL_9N2 0B 0010 1000
#define SERIAL_8E1 0B 0000 0010
#define SERIAL_8E2 0B 0010 0010
#define SERIAL_9E1 0B 0000 1010
#define SERIAL_9E2 0B 0010 1010
#define SERIAL_8O1 0B 0000 0011
#define SERIAL_8O2 0B 0010 0011
#define SERIAL_9O1 0B 0000 1011
#define SERIAL_9O2 0B 0010 1011
*/
udev->regs->CR1 = udev->regs->CR1 ^ ((udev->regs->CR1 ^ (flags&0x0F)<<9 ) & 0B0001111000000000);
udev->regs->CR2 = udev->regs->CR2 ^ ((udev->regs->CR2 ^ (flags&0xF0)<<8 ) & 0B0011000000000000);
}
void usart_set_baud_rate(usart_dev *dev, uint32 clock_speed, uint32 baud) {