Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

USART configuration fixes #997

Merged
merged 10 commits into from
Jun 10, 2016
77 changes: 65 additions & 12 deletions docs/reference/firmware.md
Original file line number Diff line number Diff line change
Expand Up @@ -2989,18 +2989,67 @@ _Since 0.5.0_

As of 0.5.0 firmware, 28800 baud set on the Host will put the device in Listening Mode, where a YMODEM download can be started by additionally sending an `f` character.

The configuration of the serial channel may also specify the number of data bits, stop bits and parity. The default is SERIAL_8N1 (8 data bits, no parity and 1 stop bit) and does not need to be specified to achieve this configuration. To specify one of the following configurations, add one of these defines as the second parameter in the `begin()` function, e.g. `Serial.begin(9600, SERIAL_8E1);` for 8 data bits, even parity and 1 stop bit.
The configuration of the serial channel may also specify the number of data bits, stop bits, parity, flow control and other settings. The default is SERIAL_8N1 (8 data bits, no parity and 1 stop bit) and does not need to be specified to achieve this configuration. To specify one of the following configurations, add one of these defines as the second parameter in the `begin()` function, e.g. `Serial.begin(9600, SERIAL_8E1);` for 8 data bits, even parity and 1 stop bit.

Serial configurations available:
Pre-defined Serial configurations available:

- `SERIAL_8N1` - 8 data bits, no parity, 1 stop bit (default)
- `SERIAL_8N2` - 8 data bits, no parity, 2 stop bits
- `SERIAL_8E1` - 8 data bits, even parity, 1 stop bit
- `SERIAL_8E2` - 8 data bits, even parity, 2 stop bits
- `SERIAL_8O1` - 8 data bits, odd parity, 1 stop bit
- `SERIAL_8O2` - 8 data bits, odd parity, 2 stop bits
- `SERIAL_9N1` - 9 data bits, no parity, 1 stop bit
- `SERIAL_9N2` - 9 data bits, no parity, 2 stop bits

_Since 0.6.0_

- `SERIAL_7O1` - 7 data bits, odd parity, 1 stop bit
- `SERIAL_7O2` - 7 data bits, odd parity, 1 stop bit
- `SERIAL_7E1` - 7 data bits, odd parity, 1 stop bit
- `SERIAL_7E2` - 7 data bits, odd parity, 1 stop bit
- `LIN_MASTER_13B` - 8 data bits, no parity, 1 stop bit, LIN Master mode with 13-bit break generation
- `LIN_SLAVE_10B` - 8 data bits, no parity, 1 stop bit, LIN Slave mode with 10-bit break detection
- `LIN_SLAVE_11B` - 8 data bits, no parity, 1 stop bit, LIN Slave mode with 11-bit break detection

Alternatively, configuration may be constructed manually by ORing (`|`) the following configuration constants:

Data bits:
- `SERIAL_DATA_BITS_7` - 7 data bits
- `SERIAL_DATA_BITS_8` - 8 data bits
- `SERIAL_DATA_BITS_9` - 9 data bits

Stop bits:
- `SERIAL_STOP_BITS_1` - 1 stop bit
- `SERIAL_STOP_BITS_2` - 2 stop bits
- `SERIAL_STOP_BITS_0_5` - 0.5 stop bits
- `SERIAL_STOP_BITS_1_5` - 1.5 stop bits

Parity:
- `SERIAL_PARITY_NO` - no parity
- `SERIAL_PARITY_EVEN` - even parity
- `SERIAL_PARITY_ODD` - odd parity

{{#if core}}
Hardware flow control, available only on Serial1 (`CTS` - `A0`, `RTS` - `A1`):
{{/if}}
{{#unless core}}
Hardware flow control, available only on Serial2 (`CTS` - `A7`, `RTS` - `RGBR` ):
{{/unless}}
- `SERIAL_FLOW_CONTROL_NONE` - no flow control
- `SERIAL_FLOW_CONTROL_RTS` - RTS flow control
- `SERIAL_FLOW_CONTROL_CTS` - CTS flow control
- `SERIAL_FLOW_CONTROL_RTS_CTS` - RTS/CTS flow control

LIN configuration:
- `LIN_MODE_MASTER` - LIN Master
- `LIN_MODE_SLAVE` - LIN Slave
- `LIN_BREAK_13B` - 13-bit break generation
- `LIN_BREAK_10B` - 10-bit break detection
- `LIN_BREAK_11B` - 10-bit break detection

**NOTE:** LIN break detection may be enabled in both Master and Slave modes.

`SERIAL_8N1` - 8 data bits, no parity, 1 stop bit (default)
`SERIAL_8N2` - 8 data bits, no parity, 2 stop bits
`SERIAL_8E1` - 8 data bits, even parity, 1 stop bit
`SERIAL_8E2` - 8 data bits, even parity, 2 stop bits
`SERIAL_8O1` - 8 data bits, odd parity, 1 stop bit
`SERIAL_8O2` - 8 data bits, odd parity, 2 stop bits
`SERIAL_9N1` - 9 data bits, no parity, 1 stop bit
`SERIAL_9N2` - 9 data bits, no parity, 2 stop bits

```C++
// SYNTAX
Expand All @@ -3016,6 +3065,9 @@ Serial2.begin(speed); // on Core via
// RGB-LED green(TX) and
// RGB-LED blue (RX) pins
Serial2.begin(speed, config); // "

Serial1.begin(9600, SERIAL_9N1); // via TX/RX pins, 9600 9N1 mode
Serial2.begin(9600, SERIAL_DATA_BITS_8 | SERIAL_STOP_BITS_1_5 | SERIAL_PARITY_EVEN); // via TX/RX pins, 9600 8E1.5
{{#if electron}}

Serial4.begin(speed); // via C3(TX)/C2(RX) pins
Expand All @@ -3026,8 +3078,9 @@ Serial5.begin(speed, config); // "
{{/if}}
```

`speed`: parameter that specifies the baud rate *(long)*
`config`: parameter that specifies the number of data bits used, parity and stop bits *(long)*
Parameters:
- `speed`: parameter that specifies the baud rate *(long)*
- `config`: parameter that specifies the number of data bits used, parity and stop bits *(long)*

`begin()` does not return anything

Expand Down
7 changes: 4 additions & 3 deletions hal/inc/hal_dynalib_usart.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ DYNALIB_FN(BASE_IDX + 12, hal_usart, USB_USART_Flush_Data, void(void))
#define BASE_IDX2 (BASE_IDX+11)
#endif

DYNALIB_FN(BASE_IDX2 + 0, hal_usart,HAL_USART_BeginConfig,void(HAL_USART_Serial serial, uint32_t baud, uint32_t config, void *ptr))
DYNALIB_FN(BASE_IDX2 + 1, hal_usart,HAL_USART_Write_NineBitData, uint32_t(HAL_USART_Serial serial, uint16_t data))

DYNALIB_FN(BASE_IDX2 + 0, hal_usart, HAL_USART_BeginConfig, void(HAL_USART_Serial serial, uint32_t baud, uint32_t config, void *ptr))
DYNALIB_FN(BASE_IDX2 + 1, hal_usart, HAL_USART_Write_NineBitData, uint32_t(HAL_USART_Serial serial, uint16_t data))
DYNALIB_FN(BASE_IDX2 + 2, hal_usart, HAL_USART_Send_Break, void(HAL_USART_Serial, void*))
DYNALIB_FN(BASE_IDX2 + 3, hal_usart, HAL_USART_Break_Detected, uint8_t(HAL_USART_Serial))

DYNALIB_END(hal_usart)

Expand Down
75 changes: 62 additions & 13 deletions hal/inc/usart_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,68 @@
#endif
#define SERIAL_BUFFER_SIZE 64

// Available Serial Configurations for C
#define SERIAL_8N1 (uint8_t)0b00000000
#define SERIAL_8N2 (uint8_t)0b00000001
#define SERIAL_8E1 (uint8_t)0b00000100
#define SERIAL_8E2 (uint8_t)0b00000101
#define SERIAL_8O1 (uint8_t)0b00001000
#define SERIAL_8O2 (uint8_t)0b00001001
#define SERIAL_9N1 (uint8_t)0b00010000
#define SERIAL_9N2 (uint8_t)0b00010001
// Pre-defined USART configurations
#define SERIAL_7E1 (uint32_t)(SERIAL_DATA_BITS_7 | SERIAL_PARITY_EVEN | SERIAL_STOP_BITS_1)
#define SERIAL_7E2 (uint32_t)(SERIAL_DATA_BITS_7 | SERIAL_PARITY_EVEN | SERIAL_STOP_BITS_2)
#define SERIAL_7O1 (uint32_t)(SERIAL_DATA_BITS_7 | SERIAL_PARITY_ODD | SERIAL_STOP_BITS_1)
#define SERIAL_7O2 (uint32_t)(SERIAL_DATA_BITS_7 | SERIAL_PARITY_ODD | SERIAL_STOP_BITS_2)
#define SERIAL_8N1 (uint32_t)(SERIAL_DATA_BITS_8 | SERIAL_PARITY_NO | SERIAL_STOP_BITS_1)
#define SERIAL_8N2 (uint32_t)(SERIAL_DATA_BITS_8 | SERIAL_PARITY_NO | SERIAL_STOP_BITS_2)
#define SERIAL_8E1 (uint32_t)(SERIAL_DATA_BITS_8 | SERIAL_PARITY_EVEN | SERIAL_STOP_BITS_1)
#define SERIAL_8E2 (uint32_t)(SERIAL_DATA_BITS_8 | SERIAL_PARITY_EVEN | SERIAL_STOP_BITS_2)
#define SERIAL_8O1 (uint32_t)(SERIAL_DATA_BITS_8 | SERIAL_PARITY_ODD | SERIAL_STOP_BITS_1)
#define SERIAL_8O2 (uint32_t)(SERIAL_DATA_BITS_8 | SERIAL_PARITY_ODD | SERIAL_STOP_BITS_2)
#define SERIAL_9N1 (uint32_t)(SERIAL_DATA_BITS_9 | SERIAL_PARITY_NO | SERIAL_STOP_BITS_1)
#define SERIAL_9N2 (uint32_t)(SERIAL_DATA_BITS_9 | SERIAL_PARITY_NO | SERIAL_STOP_BITS_2)

// Serial Configuration masks
#define SERIAL_VALID_CONFIG (uint8_t)0b00001100
#define SERIAL_STOP_BITS (uint8_t)0b00000011
#define SERIAL_PARITY_BITS (uint8_t)0b00001100
#define SERIAL_NINE_BITS (uint8_t)0b00010000
#define SERIAL_STOP_BITS ((uint32_t)0b00000011)
#define SERIAL_PARITY ((uint32_t)0b00001100)
#define SERIAL_DATA_BITS ((uint32_t)0b00110000)
#define SERIAL_FLOW_CONTROL ((uint32_t)0b11000000)

// Stop bits
#define SERIAL_STOP_BITS_1 ((uint32_t)0b00000000)
#define SERIAL_STOP_BITS_2 ((uint32_t)0b00000001)
#define SERIAL_STOP_BITS_0_5 ((uint32_t)0b00000010)
#define SERIAL_STOP_BITS_1_5 ((uint32_t)0b00000011)

// Parity
#define SERIAL_PARITY_NO ((uint32_t)0b00000000)
#define SERIAL_PARITY_EVEN ((uint32_t)0b00000100)
#define SERIAL_PARITY_ODD ((uint32_t)0b00001000)

// Data bits
#define SERIAL_DATA_BITS_8 ((uint32_t)0b00000000)
#define SERIAL_DATA_BITS_9 ((uint32_t)0b00010000)
#define SERIAL_DATA_BITS_7 ((uint32_t)0b00100000)

// Flow control settings
#define SERIAL_FLOW_CONTROL_NONE ((uint32_t)0b00000000)
#define SERIAL_FLOW_CONTROL_RTS ((uint32_t)0b01000000)
#define SERIAL_FLOW_CONTROL_CTS ((uint32_t)0b10000000)
#define SERIAL_FLOW_CONTROL_RTS_CTS ((uint32_t)0b11000000)

// LIN Configuration masks
#define LIN_MODE ((uint32_t)0x0300)
#define LIN_BREAK_BITS ((uint32_t)0x0C00)

// LIN modes
#define LIN_MODE_MASTER ((uint32_t)0x0100)
#define LIN_MODE_SLAVE ((uint32_t)0x0200)

// LIN break settings
// Supported only in Master mode
#define LIN_BREAK_13B ((uint32_t)0x0000)
// Supported only in Slave mode
#define LIN_BREAK_10B ((uint32_t)0x0800)
#define LIN_BREAK_11B ((uint32_t)0x0C00)

// Pre-defined LIN configurations
#define LIN_MASTER_13B (uint32_t)(((uint32_t)SERIAL_8N1) | LIN_MODE_MASTER | LIN_BREAK_13B)
#define LIN_SLAVE_10B (uint32_t)(((uint32_t)SERIAL_8N1) | LIN_MODE_SLAVE | LIN_BREAK_10B)
#define LIN_SLAVE_11B (uint32_t)(((uint32_t)SERIAL_8N1) | LIN_MODE_SLAVE | LIN_BREAK_11B)


/* Exported types ------------------------------------------------------------*/
typedef struct Ring_Buffer
Expand Down Expand Up @@ -97,6 +144,8 @@ bool HAL_USART_Is_Enabled(HAL_USART_Serial serial);
void HAL_USART_Half_Duplex(HAL_USART_Serial serial, bool Enable);
void HAL_USART_BeginConfig(HAL_USART_Serial serial, uint32_t baud, uint32_t config, void*);
uint32_t HAL_USART_Write_NineBitData(HAL_USART_Serial serial, uint16_t data);
void HAL_USART_Send_Break(HAL_USART_Serial serial, void* reserved);
uint8_t HAL_USART_Break_Detected(HAL_USART_Serial serial);

#ifdef __cplusplus
}
Expand Down
Loading