1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-02-20 10:54:14 +01:00

LP-270 - Add baud rate and mode parameters. Store working copy of usart init structure for private use, so we do not revert to original config before every change. Re-enable usart after changing parameters.

This commit is contained in:
Vladimir Zidar 2017-01-23 13:38:05 +01:00
parent 9750879396
commit 4bb5bee32b
5 changed files with 159 additions and 92 deletions

View File

@ -282,7 +282,7 @@ int32_t PIOS_COM_ChangeBaud(uint32_t com_id, uint32_t baud)
return 0;
}
int32_t PIOS_COM_ChangeConfig(uint32_t com_id, enum PIOS_COM_Word_Length word_len, enum PIOS_COM_StopBits stop_bits, enum PIOS_COM_Parity parity)
int32_t PIOS_COM_ChangeConfig(uint32_t com_id, enum PIOS_COM_Word_Length word_len, enum PIOS_COM_StopBits stop_bits, enum PIOS_COM_Parity parity, uint32_t baud_rate, enum PIOS_COM_Mode mode)
{
struct pios_com_dev *com_dev = (struct pios_com_dev *)com_id;
@ -293,13 +293,12 @@ int32_t PIOS_COM_ChangeConfig(uint32_t com_id, enum PIOS_COM_Word_Length word_le
/* Invoke the driver function if it exists */
if (com_dev->driver->set_config) {
com_dev->driver->set_config(com_dev->lower_id, word_len, stop_bits, parity);
com_dev->driver->set_config(com_dev->lower_id, word_len, stop_bits, parity, baud_rate, mode);
}
return 0;
}
/**
* Set control lines associated with the port
* \param[in] port COM port

View File

@ -61,10 +61,17 @@ enum PIOS_COM_Parity {
PIOS_COM_Parity_Odd,
};
enum PIOS_COM_Mode {
PIOS_COM_Mode_Unchanged = 0,
PIOS_COM_Mode_Rx = (1 << 0),
PIOS_COM_Mode_Tx = (1 << 1),
PIOS_COM_Mode_RxTx = (PIOS_COM_Mode_Rx | PIOS_COM_Mode_Tx),
};
struct pios_com_driver {
void (*init)(uint32_t id);
void (*set_baud)(uint32_t id, uint32_t baud);
void (*set_config)(uint32_t usart_id, enum PIOS_COM_Word_Length word_len, enum PIOS_COM_StopBits stop_bits, enum PIOS_COM_Parity parity);
void (*set_config)(uint32_t usart_id, enum PIOS_COM_Word_Length word_len, enum PIOS_COM_StopBits stop_bits, enum PIOS_COM_Parity parity, uint32_t baud_rate, enum PIOS_COM_Mode mode);
void (*set_ctrl_line)(uint32_t id, uint32_t mask, uint32_t state);
void (*tx_start)(uint32_t id, uint16_t tx_bytes_avail);
void (*rx_start)(uint32_t id, uint16_t rx_bytes_avail);
@ -83,7 +90,7 @@ struct pios_com_driver {
/* Public Functions */
extern int32_t PIOS_COM_Init(uint32_t *com_id, const struct pios_com_driver *driver, uint32_t lower_id, uint8_t *rx_buffer, uint16_t rx_buffer_len, uint8_t *tx_buffer, uint16_t tx_buffer_len);
extern int32_t PIOS_COM_ChangeBaud(uint32_t com_id, uint32_t baud);
extern int32_t PIOS_COM_ChangeConfig(uint32_t com_id, enum PIOS_COM_Word_Length word_len, enum PIOS_COM_StopBits stop_bits, enum PIOS_COM_Parity parity);
extern int32_t PIOS_COM_ChangeConfig(uint32_t com_id, enum PIOS_COM_Word_Length word_len, enum PIOS_COM_StopBits stop_bits, enum PIOS_COM_Parity parity, uint32_t baud_rate, enum PIOS_COM_Mode mode);
extern int32_t PIOS_COM_SetCtrlLine(uint32_t com_id, uint32_t mask, uint32_t state);
extern int32_t PIOS_COM_RegisterCtrlLineCallback(uint32_t usart_id, pios_com_callback_ctrl_line ctrl_line_cb, uint32_t context);
extern int32_t PIOS_COM_RegisterBaudRateCallback(uint32_t usart_id, pios_com_callback_baud_rate baud_rate_cb, uint32_t context);

View File

@ -38,7 +38,7 @@
#define PIOS_UART_TX_BUFFER 10
/* Provide a COM driver */
static void PIOS_USART_ChangeBaud(uint32_t usart_id, uint32_t baud);
static void PIOS_USART_ChangeConfig(uint32_t usart_id, enum PIOS_COM_Word_Length word_len, enum PIOS_COM_StopBits stop_bits, enum PIOS_COM_Parity parity);
static void PIOS_USART_ChangeConfig(uint32_t usart_id, enum PIOS_COM_Word_Length word_len, enum PIOS_COM_StopBits stop_bits, enum PIOS_COM_Parity parity, uint32_t baud_rate, enum PIOS_COM_Mode mode);
static void PIOS_USART_RegisterRxCallback(uint32_t usart_id, pios_com_callback rx_in_cb, uint32_t context);
static void PIOS_USART_RegisterTxCallback(uint32_t usart_id, pios_com_callback tx_out_cb, uint32_t context);
static void PIOS_USART_TxStart(uint32_t usart_id, uint16_t tx_bytes_avail);
@ -60,7 +60,7 @@ enum pios_usart_dev_magic {
struct pios_usart_dev {
enum pios_usart_dev_magic magic;
const struct pios_usart_cfg *cfg;
USART_InitTypeDef init;
pios_com_callback rx_in_cb;
uint32_t rx_in_context;
pios_com_callback tx_out_cb;
@ -162,7 +162,10 @@ int32_t PIOS_USART_Init(uint32_t *usart_id, const struct pios_usart_cfg *cfg)
}
/* Bind the configuration to the device instance */
usart_dev->cfg = cfg;
usart_dev->cfg = cfg;
/* Copy the comm parameter structure */
usart_dev->init = cfg->init;
/* Enable the USART Pins Software Remapping */
if (usart_dev->cfg->remap) {
@ -191,7 +194,7 @@ int32_t PIOS_USART_Init(uint32_t *usart_id, const struct pios_usart_cfg *cfg)
}
/* Configure the USART */
USART_Init(cfg->regs, (USART_InitTypeDef *)&cfg->init);
USART_Init(cfg->regs, (USART_InitTypeDef *)&usart_dev->init);
*usart_id = (uint32_t)usart_dev;
NVIC_Init((NVIC_InitTypeDef *)&cfg->irq.init);
@ -225,18 +228,13 @@ static void PIOS_USART_TxStart(uint32_t usart_id, __attribute__((unused)) uint16
*/
static void PIOS_USART_ChangeBaud(uint32_t usart_id, uint32_t baud)
{
const struct pios_usart_dev *usart_dev = PIOS_USART_validate(usart_id);
struct pios_usart_dev *usart_dev = PIOS_USART_validate(usart_id);
USART_InitTypeDef USART_InitStructure;
/* Use our working copy of the usart init structure */
usart_dev->init.USART_BaudRate = baud;
/* Start with a copy of the default configuration for the peripheral */
USART_InitStructure = usart_dev->cfg->init;
/* Adjust the baud rate */
USART_InitStructure.USART_BaudRate = baud;
/* Write back the new configuration */
USART_Init(usart_dev->cfg->regs, &USART_InitStructure);
/* Write back the modified configuration */
USART_Init(usart_dev->cfg->regs, &usart_dev->init);
USART_Cmd(usart_dev->cfg->regs, ENABLE);
}
@ -252,20 +250,18 @@ static void PIOS_USART_ChangeBaud(uint32_t usart_id, uint32_t baud)
static void PIOS_USART_ChangeConfig(uint32_t usart_id,
enum PIOS_COM_Word_Length word_len,
enum PIOS_COM_StopBits stop_bits,
enum PIOS_COM_Parity parity)
enum PIOS_COM_Parity parity,
uint32_t baud_rate,
enum PIOS_COM_Mode mode)
{
const struct pios_usart_dev *usart_dev = PIOS_USART_validate(usart_id);
struct pios_usart_dev *usart_dev = PIOS_USART_validate(usart_id);
USART_InitTypeDef USART_InitStructure;
/* Start with a copy of the default configuration for the peripheral */
USART_InitStructure = usart_dev->cfg->init;
switch (word_len) {
case PIOS_COM_Word_length_8b:
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
usart_dev->init.USART_WordLength = USART_WordLength_8b;
break;
case PIOS_COM_Word_length_9b:
USART_InitStructure.USART_WordLength = USART_WordLength_9b;
usart_dev->init.USART_WordLength = USART_WordLength_9b;
break;
default:
break;
@ -273,13 +269,13 @@ static void PIOS_USART_ChangeConfig(uint32_t usart_id,
switch (stop_bits) {
case PIOS_COM_StopBits_1:
USART_InitStructure.USART_StopBits = USART_StopBits_1;
usart_dev->init.USART_StopBits = USART_StopBits_1;
break;
case PIOS_COM_StopBits_1_5:
USART_InitStructure.USART_StopBits = USART_StopBits_1_5;
usart_dev->init.USART_StopBits = USART_StopBits_1_5;
break;
case PIOS_COM_StopBits_2:
USART_InitStructure.USART_StopBits = USART_StopBits_2;
usart_dev->init.USART_StopBits = USART_StopBits_2;
break;
default:
break;
@ -287,19 +283,43 @@ static void PIOS_USART_ChangeConfig(uint32_t usart_id,
switch (parity) {
case PIOS_COM_Parity_No:
USART_InitStructure.USART_Parity = USART_Parity_No;
usart_dev->init.USART_Parity = USART_Parity_No;
break;
case PIOS_COM_Parity_Even:
USART_InitStructure.USART_Parity = USART_Parity_Even;
usart_dev->init.USART_Parity = USART_Parity_Even;
break;
case PIOS_COM_Parity_Odd:
USART_InitStructure.USART_Parity = USART_Parity_Odd;
usart_dev->init.USART_Parity = USART_Parity_Odd;
break;
default:
break;
}
/* Write back the new configuration */
USART_Init(usart_dev->cfg->regs, &USART_InitStructure);
if (baud_rate) {
usart_dev->init.USART_BaudRate = baud_rate;
}
switch (mode) {
case PIOS_COM_Mode_Rx:
usart_dev->init.USART_Mode = USART_Mode_Rx;
break;
case PIOS_COM_Mode_Tx:
usart_dev->init.USART_Mode = USART_Mode_Tx;
break;
case PIOS_COM_Mode_RxTx:
usart_dev->init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
break;
default:
break;
}
/* Write back the modified configuration */
USART_Init(usart_dev->cfg->regs, &usart_dev->init);
/*
* Re enable USART.
*/
USART_Cmd(usart_dev->cfg->regs, ENABLE);
}
static void PIOS_USART_RegisterRxCallback(uint32_t usart_id, pios_com_callback rx_in_cb, uint32_t context)

View File

@ -37,7 +37,7 @@
#include <pios_usart.h>
/* Provide a COM driver */
static void PIOS_USART_ChangeConfig(uint32_t usart_id, enum PIOS_COM_Word_Length word_len, enum PIOS_COM_StopBits stop_bits, enum PIOS_COM_Parity parity);
static void PIOS_USART_ChangeConfig(uint32_t usart_id, enum PIOS_COM_Word_Length word_len, enum PIOS_COM_StopBits stop_bits, enum PIOS_COM_Parity parity, uint32_t baud_rate, enum PIOS_COM_Mode mode);
static void PIOS_USART_ChangeBaud(uint32_t usart_id, uint32_t baud);
static void PIOS_USART_RegisterRxCallback(uint32_t usart_id, pios_com_callback rx_in_cb, uint32_t context);
static void PIOS_USART_RegisterTxCallback(uint32_t usart_id, pios_com_callback tx_out_cb, uint32_t context);
@ -60,7 +60,7 @@ enum pios_usart_dev_magic {
struct pios_usart_dev {
enum pios_usart_dev_magic magic;
const struct pios_usart_cfg *cfg;
USART_InitTypeDef init;
pios_com_callback rx_in_cb;
uint32_t rx_in_context;
pios_com_callback tx_out_cb;
@ -153,7 +153,10 @@ int32_t PIOS_USART_Init(uint32_t *usart_id, const struct pios_usart_cfg *cfg)
}
/* Bind the configuration to the device instance */
usart_dev->cfg = cfg;
usart_dev->cfg = cfg;
/* Copy the comm parameter structure */
usart_dev->init = cfg->init;
/* Enable the USART Pins Software Remapping */
if (usart_dev->cfg->remap) {
@ -178,7 +181,7 @@ int32_t PIOS_USART_Init(uint32_t *usart_id, const struct pios_usart_cfg *cfg)
}
/* Configure the USART */
USART_Init(usart_dev->cfg->regs, &usart_dev->cfg->init);
USART_Init(usart_dev->cfg->regs, &usart_dev->init);
*usart_id = (uint32_t)usart_dev;
@ -241,16 +244,11 @@ static void PIOS_USART_ChangeBaud(uint32_t usart_id, uint32_t baud)
PIOS_Assert(valid);
USART_InitTypeDef USART_InitStructure;
/* Use our working copy of the usart init structure */
usart_dev->init.USART_BaudRate = baud;
/* Start with a copy of the default configuration for the peripheral */
USART_InitStructure = usart_dev->cfg->init;
/* Adjust the baud rate */
USART_InitStructure.USART_BaudRate = baud;
/* Write back the new configuration */
USART_Init(usart_dev->cfg->regs, &USART_InitStructure);
/* Write back the modified configuration */
USART_Init(usart_dev->cfg->regs, &usart_dev->init);
}
/**
@ -264,7 +262,9 @@ static void PIOS_USART_ChangeBaud(uint32_t usart_id, uint32_t baud)
static void PIOS_USART_ChangeConfig(uint32_t usart_id,
enum PIOS_COM_Word_Length word_len,
enum PIOS_COM_StopBits stop_bits,
enum PIOS_COM_Parity parity)
enum PIOS_COM_Parity parity,
uint32_t baud_rate,
enum PIOS_COM_Mode mode)
{
struct pios_usart_dev *usart_dev = (struct pios_usart_dev *)usart_id;
@ -272,16 +272,12 @@ static void PIOS_USART_ChangeConfig(uint32_t usart_id,
PIOS_Assert(valid);
USART_InitTypeDef USART_InitStructure;
/* Start with a copy of the default configuration for the peripheral */
USART_InitStructure = usart_dev->cfg->init;
switch (word_len) {
case PIOS_COM_Word_length_8b:
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
usart_dev->init.USART_WordLength = USART_WordLength_8b;
break;
case PIOS_COM_Word_length_9b:
USART_InitStructure.USART_WordLength = USART_WordLength_9b;
usart_dev->init.USART_WordLength = USART_WordLength_9b;
break;
default:
break;
@ -289,16 +285,16 @@ static void PIOS_USART_ChangeConfig(uint32_t usart_id,
switch (stop_bits) {
case PIOS_COM_StopBits_0_5:
USART_InitStructure.USART_StopBits = USART_StopBits_0_5;
usart_dev->init.USART_StopBits = USART_StopBits_0_5;
break;
case PIOS_COM_StopBits_1:
USART_InitStructure.USART_StopBits = USART_StopBits_1;
usart_dev->init.USART_StopBits = USART_StopBits_1;
break;
case PIOS_COM_StopBits_1_5:
USART_InitStructure.USART_StopBits = USART_StopBits_1_5;
usart_dev->init.USART_StopBits = USART_StopBits_1_5;
break;
case PIOS_COM_StopBits_2:
USART_InitStructure.USART_StopBits = USART_StopBits_2;
usart_dev->init.USART_StopBits = USART_StopBits_2;
break;
default:
break;
@ -306,19 +302,43 @@ static void PIOS_USART_ChangeConfig(uint32_t usart_id,
switch (parity) {
case PIOS_COM_Parity_No:
USART_InitStructure.USART_Parity = USART_Parity_No;
usart_dev->init.USART_Parity = USART_Parity_No;
break;
case PIOS_COM_Parity_Even:
USART_InitStructure.USART_Parity = USART_Parity_Even;
usart_dev->init.USART_Parity = USART_Parity_Even;
break;
case PIOS_COM_Parity_Odd:
USART_InitStructure.USART_Parity = USART_Parity_Odd;
usart_dev->init.USART_Parity = USART_Parity_Odd;
break;
default:
break;
}
/* Write back the new configuration */
USART_Init(usart_dev->cfg->regs, &USART_InitStructure);
if (baud_rate) {
usart_dev->init.USART_BaudRate = baud_rate;
}
switch (mode) {
case PIOS_COM_Mode_Rx:
usart_dev->init.USART_Mode = USART_Mode_Rx;
break;
case PIOS_COM_Mode_Tx:
usart_dev->init.USART_Mode = USART_Mode_Tx;
break;
case PIOS_COM_Mode_RxTx:
usart_dev->init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
break;
default:
break;
}
/* Write back the modified configuration */
USART_Init(usart_dev->cfg->regs, &usart_dev->init);
/*
* Re enable USART.
*/
USART_Cmd(usart_dev->cfg->regs, ENABLE);
}
static void PIOS_USART_RegisterRxCallback(uint32_t usart_id, pios_com_callback rx_in_cb, uint32_t context)

View File

@ -41,7 +41,7 @@
/* Provide a COM driver */
static void PIOS_USART_ChangeBaud(uint32_t usart_id, uint32_t baud);
static void PIOS_USART_ChangeConfig(uint32_t usart_id, enum PIOS_COM_Word_Length word_len, enum PIOS_COM_StopBits stop_bits, enum PIOS_COM_Parity parity);
static void PIOS_USART_ChangeConfig(uint32_t usart_id, enum PIOS_COM_Word_Length word_len, enum PIOS_COM_StopBits stop_bits, enum PIOS_COM_Parity parity, uint32_t baud_rate, enum PIOS_COM_Mode mode);
static void PIOS_USART_SetCtrlLine(uint32_t usart_id, uint32_t mask, uint32_t state);
static void PIOS_USART_RegisterRxCallback(uint32_t usart_id, pios_com_callback rx_in_cb, uint32_t context);
static void PIOS_USART_RegisterTxCallback(uint32_t usart_id, pios_com_callback tx_out_cb, uint32_t context);
@ -65,7 +65,7 @@ enum pios_usart_dev_magic {
struct pios_usart_dev {
enum pios_usart_dev_magic magic;
const struct pios_usart_cfg *cfg;
USART_InitTypeDef init;
pios_com_callback rx_in_cb;
uint32_t rx_in_context;
pios_com_callback tx_out_cb;
@ -177,7 +177,10 @@ int32_t PIOS_USART_Init(uint32_t *usart_id, const struct pios_usart_cfg *cfg)
}
/* Bind the configuration to the device instance */
usart_dev->cfg = cfg;
usart_dev->cfg = cfg;
/* Copy the comm parameter structure */
usart_dev->init = cfg->init;
/* Map pins to USART function */
/* note __builtin_ctz() due to the difference between GPIO_PinX and GPIO_PinSourceX */
@ -201,7 +204,7 @@ int32_t PIOS_USART_Init(uint32_t *usart_id, const struct pios_usart_cfg *cfg)
}
/* Configure the USART */
USART_Init(usart_dev->cfg->regs, (USART_InitTypeDef *)&usart_dev->cfg->init);
USART_Init(usart_dev->cfg->regs, (USART_InitTypeDef *)&usart_dev->init);
*usart_id = (uint32_t)usart_dev;
@ -275,16 +278,12 @@ static void PIOS_USART_ChangeBaud(uint32_t usart_id, uint32_t baud)
PIOS_Assert(valid);
USART_InitTypeDef USART_InitStructure;
/* Start with a copy of the default configuration for the peripheral */
USART_InitStructure = usart_dev->cfg->init;
/* Use our working copy of the usart init structure */
usart_dev->init.USART_BaudRate = baud;
/* Adjust the baud rate */
USART_InitStructure.USART_BaudRate = baud;
/* Write back the new configuration */
USART_Init(usart_dev->cfg->regs, &USART_InitStructure);
/* Write back the modified configuration */
USART_Init(usart_dev->cfg->regs, &usart_dev->init);
}
/**
@ -298,7 +297,9 @@ static void PIOS_USART_ChangeBaud(uint32_t usart_id, uint32_t baud)
static void PIOS_USART_ChangeConfig(uint32_t usart_id,
enum PIOS_COM_Word_Length word_len,
enum PIOS_COM_StopBits stop_bits,
enum PIOS_COM_Parity parity)
enum PIOS_COM_Parity parity,
uint32_t baud_rate,
enum PIOS_COM_Mode mode)
{
struct pios_usart_dev *usart_dev = (struct pios_usart_dev *)usart_id;
@ -306,16 +307,12 @@ static void PIOS_USART_ChangeConfig(uint32_t usart_id,
PIOS_Assert(valid);
USART_InitTypeDef USART_InitStructure;
/* Start with a copy of the default configuration for the peripheral */
USART_InitStructure = usart_dev->cfg->init;
switch (word_len) {
case PIOS_COM_Word_length_8b:
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
usart_dev->init.USART_WordLength = USART_WordLength_8b;
break;
case PIOS_COM_Word_length_9b:
USART_InitStructure.USART_WordLength = USART_WordLength_9b;
usart_dev->init.USART_WordLength = USART_WordLength_9b;
break;
default:
break;
@ -323,16 +320,16 @@ static void PIOS_USART_ChangeConfig(uint32_t usart_id,
switch (stop_bits) {
case PIOS_COM_StopBits_0_5:
USART_InitStructure.USART_StopBits = USART_StopBits_0_5;
usart_dev->init.USART_StopBits = USART_StopBits_0_5;
break;
case PIOS_COM_StopBits_1:
USART_InitStructure.USART_StopBits = USART_StopBits_1;
usart_dev->init.USART_StopBits = USART_StopBits_1;
break;
case PIOS_COM_StopBits_1_5:
USART_InitStructure.USART_StopBits = USART_StopBits_1_5;
usart_dev->init.USART_StopBits = USART_StopBits_1_5;
break;
case PIOS_COM_StopBits_2:
USART_InitStructure.USART_StopBits = USART_StopBits_2;
usart_dev->init.USART_StopBits = USART_StopBits_2;
break;
default:
break;
@ -340,19 +337,43 @@ static void PIOS_USART_ChangeConfig(uint32_t usart_id,
switch (parity) {
case PIOS_COM_Parity_No:
USART_InitStructure.USART_Parity = USART_Parity_No;
usart_dev->init.USART_Parity = USART_Parity_No;
break;
case PIOS_COM_Parity_Even:
USART_InitStructure.USART_Parity = USART_Parity_Even;
usart_dev->init.USART_Parity = USART_Parity_Even;
break;
case PIOS_COM_Parity_Odd:
USART_InitStructure.USART_Parity = USART_Parity_Odd;
usart_dev->init.USART_Parity = USART_Parity_Odd;
break;
default:
break;
}
/* Write back the new configuration */
USART_Init(usart_dev->cfg->regs, &USART_InitStructure);
if (baud_rate) {
usart_dev->init.USART_BaudRate = baud_rate;
}
switch (mode) {
case PIOS_COM_Mode_Rx:
usart_dev->init.USART_Mode = USART_Mode_Rx;
break;
case PIOS_COM_Mode_Tx:
usart_dev->init.USART_Mode = USART_Mode_Tx;
break;
case PIOS_COM_Mode_RxTx:
usart_dev->init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
break;
default:
break;
}
/* Write back the modified configuration */
USART_Init(usart_dev->cfg->regs, &usart_dev->init);
/*
* Re enable USART.
*/
USART_Cmd(usart_dev->cfg->regs, ENABLE);
}
static void PIOS_USART_SetCtrlLine(uint32_t usart_id, uint32_t mask, uint32_t state)