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

Merged in mindnever/librepilot/LP-270-Implement_a_PIOS_COM_ChangeConfig (pull request #386)

LP-270 implement a PIOS_COM_ChangeConfig API
This commit is contained in:
Philippe Renon 2017-01-27 19:56:38 +00:00
commit 20454c53a0
5 changed files with 359 additions and 36 deletions

View File

@ -7,7 +7,8 @@
* @{ * @{
* *
* @file pios_com.c * @file pios_com.c
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. * @author The LibrePilot Project, http://www.librepilot.org, Copyright (c) 2015-2017.
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @brief COM layer functions * @brief COM layer functions
* @see The GNU Public License (GPL) Version 3 * @see The GNU Public License (GPL) Version 3
* *
@ -281,6 +282,23 @@ int32_t PIOS_COM_ChangeBaud(uint32_t com_id, uint32_t baud)
return 0; 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, uint32_t baud_rate, enum PIOS_COM_Mode mode)
{
struct pios_com_dev *com_dev = (struct pios_com_dev *)com_id;
if (!PIOS_COM_validate(com_dev)) {
/* Undefined COM port for this board (see pios_board.c) */
return -1;
}
/* 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, baud_rate, mode);
}
return 0;
}
/** /**
* Set control lines associated with the port * Set control lines associated with the port
* \param[in] port COM port * \param[in] port COM port

View File

@ -7,7 +7,8 @@
* @{ * @{
* *
* @file pios_com.h * @file pios_com.h
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. * @author The LibrePilot Project, http://www.librepilot.org, Copyright (c) 2017.
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* Parts by Thorsten Klose (tk@midibox.org) * Parts by Thorsten Klose (tk@midibox.org)
* @brief COM layer functions header * @brief COM layer functions header
* @see The GNU Public License (GPL) Version 3 * @see The GNU Public License (GPL) Version 3
@ -40,9 +41,38 @@ typedef void (*pios_com_callback_ctrl_line)(uint32_t context, uint32_t mask, uin
typedef void (*pios_com_callback_baud_rate)(uint32_t context, uint32_t baud); typedef void (*pios_com_callback_baud_rate)(uint32_t context, uint32_t baud);
typedef void (*pios_com_callback_available)(uint32_t context, uint32_t available); typedef void (*pios_com_callback_available)(uint32_t context, uint32_t available);
enum PIOS_COM_Word_Length {
PIOS_COM_Word_length_Unchanged = 0,
PIOS_COM_Word_length_8b,
PIOS_COM_Word_length_9b,
};
enum PIOS_COM_StopBits {
PIOS_COM_StopBits_Unchanged = 0,
PIOS_COM_StopBits_0_5,
PIOS_COM_StopBits_1,
PIOS_COM_StopBits_1_5,
PIOS_COM_StopBits_2,
};
enum PIOS_COM_Parity {
PIOS_COM_Parity_Unchanged = 0,
PIOS_COM_Parity_No,
PIOS_COM_Parity_Even,
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 { struct pios_com_driver {
void (*init)(uint32_t id); void (*init)(uint32_t id);
void (*set_baud)(uint32_t id, uint32_t baud); 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, uint32_t baud_rate, enum PIOS_COM_Mode mode);
void (*set_ctrl_line)(uint32_t id, uint32_t mask, uint32_t state); 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 (*tx_start)(uint32_t id, uint16_t tx_bytes_avail);
void (*rx_start)(uint32_t id, uint16_t rx_bytes_avail); void (*rx_start)(uint32_t id, uint16_t rx_bytes_avail);
@ -61,6 +91,7 @@ struct pios_com_driver {
/* Public Functions */ /* 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_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_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, 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_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_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); extern int32_t PIOS_COM_RegisterBaudRateCallback(uint32_t usart_id, pios_com_callback_baud_rate baud_rate_cb, uint32_t context);

View File

@ -7,7 +7,8 @@
* @{ * @{
* *
* @file pios_usart.c * @file pios_usart.c
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. * @author The LibrePilot Project, http://www.librepilot.org, Copyright (c) 2016-2017.
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @brief USART commands. Inits USARTs, controls USARTs & Interupt handlers. (STM32 dependent) * @brief USART commands. Inits USARTs, controls USARTs & Interupt handlers. (STM32 dependent)
* @see The GNU Public License (GPL) Version 3 * @see The GNU Public License (GPL) Version 3
* *
@ -37,6 +38,7 @@
#define PIOS_UART_TX_BUFFER 10 #define PIOS_UART_TX_BUFFER 10
/* Provide a COM driver */ /* Provide a COM driver */
static void PIOS_USART_ChangeBaud(uint32_t usart_id, uint32_t baud); 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, 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_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_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); static void PIOS_USART_TxStart(uint32_t usart_id, uint16_t tx_bytes_avail);
@ -44,6 +46,7 @@ static void PIOS_USART_RxStart(uint32_t usart_id, uint16_t rx_bytes_avail);
const struct pios_com_driver pios_usart_com_driver = { const struct pios_com_driver pios_usart_com_driver = {
.set_baud = PIOS_USART_ChangeBaud, .set_baud = PIOS_USART_ChangeBaud,
.set_config = PIOS_USART_ChangeConfig,
.tx_start = PIOS_USART_TxStart, .tx_start = PIOS_USART_TxStart,
.rx_start = PIOS_USART_RxStart, .rx_start = PIOS_USART_RxStart,
.bind_tx_cb = PIOS_USART_RegisterTxCallback, .bind_tx_cb = PIOS_USART_RegisterTxCallback,
@ -57,7 +60,7 @@ enum pios_usart_dev_magic {
struct pios_usart_dev { struct pios_usart_dev {
enum pios_usart_dev_magic magic; enum pios_usart_dev_magic magic;
const struct pios_usart_cfg *cfg; const struct pios_usart_cfg *cfg;
USART_InitTypeDef init;
pios_com_callback rx_in_cb; pios_com_callback rx_in_cb;
uint32_t rx_in_context; uint32_t rx_in_context;
pios_com_callback tx_out_cb; pios_com_callback tx_out_cb;
@ -159,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 */ /* 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 */ /* Enable the USART Pins Software Remapping */
if (usart_dev->cfg->remap) { if (usart_dev->cfg->remap) {
@ -188,7 +194,7 @@ int32_t PIOS_USART_Init(uint32_t *usart_id, const struct pios_usart_cfg *cfg)
} }
/* Configure the USART */ /* 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; *usart_id = (uint32_t)usart_dev;
NVIC_Init((NVIC_InitTypeDef *)&cfg->irq.init); NVIC_Init((NVIC_InitTypeDef *)&cfg->irq.init);
@ -222,19 +228,99 @@ static void PIOS_USART_TxStart(uint32_t usart_id, __attribute__((unused)) uint16
*/ */
static void PIOS_USART_ChangeBaud(uint32_t usart_id, uint32_t baud) 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 */ /* Write back the modified configuration */
USART_InitStructure = usart_dev->cfg->init; USART_Init(usart_dev->cfg->regs, &usart_dev->init);
/* Adjust the baud rate */ USART_Cmd(usart_dev->cfg->regs, ENABLE);
USART_InitStructure.USART_BaudRate = baud; }
/* Write back the new configuration */ /**
USART_Init(usart_dev->cfg->regs, &USART_InitStructure); * Changes configuration of the USART peripheral without re-initialising.
* \param[in] usart_id USART name (GPS, TELEM, AUX)
* \param[in] word_len Requested word length
* \param[in] stop_bits Requested stop bits
* \param[in] parity Requested parity
* \param[in] baud_rate Requested baud rate
* \param[in] mode Requested mode
*
*/
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)
{
struct pios_usart_dev *usart_dev = PIOS_USART_validate(usart_id);
switch (word_len) {
case PIOS_COM_Word_length_8b:
usart_dev->init.USART_WordLength = USART_WordLength_8b;
break;
case PIOS_COM_Word_length_9b:
usart_dev->init.USART_WordLength = USART_WordLength_9b;
break;
default:
break;
}
switch (stop_bits) {
case PIOS_COM_StopBits_1:
usart_dev->init.USART_StopBits = USART_StopBits_1;
break;
case PIOS_COM_StopBits_1_5:
usart_dev->init.USART_StopBits = USART_StopBits_1_5;
break;
case PIOS_COM_StopBits_2:
usart_dev->init.USART_StopBits = USART_StopBits_2;
break;
default:
break;
}
switch (parity) {
case PIOS_COM_Parity_No:
usart_dev->init.USART_Parity = USART_Parity_No;
break;
case PIOS_COM_Parity_Even:
usart_dev->init.USART_Parity = USART_Parity_Even;
break;
case PIOS_COM_Parity_Odd:
usart_dev->init.USART_Parity = USART_Parity_Odd;
break;
default:
break;
}
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); USART_Cmd(usart_dev->cfg->regs, ENABLE);
} }

View File

@ -7,7 +7,8 @@
* @{ * @{
* *
* @file pios_usart.c * @file pios_usart.c
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. * @author The LibrePilot Project, http://www.librepilot.org, Copyright (c) 2016-2017.
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @brief USART commands. Inits USARTs, controls USARTs & Interupt handlers. (STM32 dependent) * @brief USART commands. Inits USARTs, controls USARTs & Interupt handlers. (STM32 dependent)
* @see The GNU Public License (GPL) Version 3 * @see The GNU Public License (GPL) Version 3
* *
@ -33,8 +34,10 @@
#ifdef PIOS_INCLUDE_USART #ifdef PIOS_INCLUDE_USART
#include <pios_usart_priv.h> #include <pios_usart_priv.h>
#include <pios_usart.h>
/* Provide a COM driver */ /* 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, 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_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_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_RegisterTxCallback(uint32_t usart_id, pios_com_callback tx_out_cb, uint32_t context);
@ -43,6 +46,7 @@ static void PIOS_USART_RxStart(uint32_t usart_id, uint16_t rx_bytes_avail);
const struct pios_com_driver pios_usart_com_driver = { const struct pios_com_driver pios_usart_com_driver = {
.set_baud = PIOS_USART_ChangeBaud, .set_baud = PIOS_USART_ChangeBaud,
.set_config = PIOS_USART_ChangeConfig,
.tx_start = PIOS_USART_TxStart, .tx_start = PIOS_USART_TxStart,
.rx_start = PIOS_USART_RxStart, .rx_start = PIOS_USART_RxStart,
.bind_tx_cb = PIOS_USART_RegisterTxCallback, .bind_tx_cb = PIOS_USART_RegisterTxCallback,
@ -56,7 +60,7 @@ enum pios_usart_dev_magic {
struct pios_usart_dev { struct pios_usart_dev {
enum pios_usart_dev_magic magic; enum pios_usart_dev_magic magic;
const struct pios_usart_cfg *cfg; const struct pios_usart_cfg *cfg;
USART_InitTypeDef init;
pios_com_callback rx_in_cb; pios_com_callback rx_in_cb;
uint32_t rx_in_context; uint32_t rx_in_context;
pios_com_callback tx_out_cb; pios_com_callback tx_out_cb;
@ -149,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 */ /* 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 */ /* Enable the USART Pins Software Remapping */
if (usart_dev->cfg->remap) { if (usart_dev->cfg->remap) {
@ -174,7 +181,7 @@ int32_t PIOS_USART_Init(uint32_t *usart_id, const struct pios_usart_cfg *cfg)
} }
/* Configure the USART */ /* 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; *usart_id = (uint32_t)usart_dev;
@ -237,16 +244,103 @@ static void PIOS_USART_ChangeBaud(uint32_t usart_id, uint32_t baud)
PIOS_Assert(valid); 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 */ /* Write back the modified configuration */
USART_InitStructure = usart_dev->cfg->init; USART_Init(usart_dev->cfg->regs, &usart_dev->init);
}
/* Adjust the baud rate */ /**
USART_InitStructure.USART_BaudRate = baud; * Changes configuration of the USART peripheral without re-initialising.
* \param[in] usart_id USART name (GPS, TELEM, AUX)
* \param[in] word_len Requested word length
* \param[in] stop_bits Requested stop bits
* \param[in] parity Requested parity
* \param[in] baud_rate Requested baud rate
* \param[in] mode Requested mode
*
*/
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)
{
struct pios_usart_dev *usart_dev = (struct pios_usart_dev *)usart_id;
/* Write back the new configuration */ bool valid = PIOS_USART_validate(usart_dev);
USART_Init(usart_dev->cfg->regs, &USART_InitStructure);
PIOS_Assert(valid);
switch (word_len) {
case PIOS_COM_Word_length_8b:
usart_dev->init.USART_WordLength = USART_WordLength_8b;
break;
case PIOS_COM_Word_length_9b:
usart_dev->init.USART_WordLength = USART_WordLength_9b;
break;
default:
break;
}
switch (stop_bits) {
case PIOS_COM_StopBits_0_5:
usart_dev->init.USART_StopBits = USART_StopBits_0_5;
break;
case PIOS_COM_StopBits_1:
usart_dev->init.USART_StopBits = USART_StopBits_1;
break;
case PIOS_COM_StopBits_1_5:
usart_dev->init.USART_StopBits = USART_StopBits_1_5;
break;
case PIOS_COM_StopBits_2:
usart_dev->init.USART_StopBits = USART_StopBits_2;
break;
default:
break;
}
switch (parity) {
case PIOS_COM_Parity_No:
usart_dev->init.USART_Parity = USART_Parity_No;
break;
case PIOS_COM_Parity_Even:
usart_dev->init.USART_Parity = USART_Parity_Even;
break;
case PIOS_COM_Parity_Odd:
usart_dev->init.USART_Parity = USART_Parity_Odd;
break;
default:
break;
}
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) static void PIOS_USART_RegisterRxCallback(uint32_t usart_id, pios_com_callback rx_in_cb, uint32_t context)

View File

@ -7,7 +7,8 @@
* @{ * @{
* *
* @file pios_usart.c * @file pios_usart.c
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012. * @author The LibrePilot Project, http://www.librepilot.org, Copyright (c) 2016-2017.
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @brief USART commands. Inits USARTs, controls USARTs & Interupt handlers. (STM32 dependent) * @brief USART commands. Inits USARTs, controls USARTs & Interupt handlers. (STM32 dependent)
* @see The GNU Public License (GPL) Version 3 * @see The GNU Public License (GPL) Version 3
* *
@ -40,6 +41,7 @@
/* Provide a COM driver */ /* Provide a COM driver */
static void PIOS_USART_ChangeBaud(uint32_t usart_id, uint32_t baud); 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, 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_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_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_RegisterTxCallback(uint32_t usart_id, pios_com_callback tx_out_cb, uint32_t context);
@ -48,6 +50,7 @@ static void PIOS_USART_RxStart(uint32_t usart_id, uint16_t rx_bytes_avail);
const struct pios_com_driver pios_usart_com_driver = { const struct pios_com_driver pios_usart_com_driver = {
.set_baud = PIOS_USART_ChangeBaud, .set_baud = PIOS_USART_ChangeBaud,
.set_config = PIOS_USART_ChangeConfig,
.set_ctrl_line = PIOS_USART_SetCtrlLine, .set_ctrl_line = PIOS_USART_SetCtrlLine,
.tx_start = PIOS_USART_TxStart, .tx_start = PIOS_USART_TxStart,
.rx_start = PIOS_USART_RxStart, .rx_start = PIOS_USART_RxStart,
@ -62,7 +65,7 @@ enum pios_usart_dev_magic {
struct pios_usart_dev { struct pios_usart_dev {
enum pios_usart_dev_magic magic; enum pios_usart_dev_magic magic;
const struct pios_usart_cfg *cfg; const struct pios_usart_cfg *cfg;
USART_InitTypeDef init;
pios_com_callback rx_in_cb; pios_com_callback rx_in_cb;
uint32_t rx_in_context; uint32_t rx_in_context;
pios_com_callback tx_out_cb; pios_com_callback tx_out_cb;
@ -174,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 */ /* 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 */ /* Map pins to USART function */
/* note __builtin_ctz() due to the difference between GPIO_PinX and GPIO_PinSourceX */ /* note __builtin_ctz() due to the difference between GPIO_PinX and GPIO_PinSourceX */
@ -198,7 +204,7 @@ int32_t PIOS_USART_Init(uint32_t *usart_id, const struct pios_usart_cfg *cfg)
} }
/* Configure the USART */ /* 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; *usart_id = (uint32_t)usart_dev;
@ -272,16 +278,104 @@ static void PIOS_USART_ChangeBaud(uint32_t usart_id, uint32_t baud)
PIOS_Assert(valid); PIOS_Assert(valid);
USART_InitTypeDef USART_InitStructure;
/* Start with a copy of the default configuration for the peripheral */ /* Use our working copy of the usart init structure */
USART_InitStructure = usart_dev->cfg->init; usart_dev->init.USART_BaudRate = baud;
/* Adjust the baud rate */ /* Write back the modified configuration */
USART_InitStructure.USART_BaudRate = baud; USART_Init(usart_dev->cfg->regs, &usart_dev->init);
}
/* Write back the new configuration */ /**
USART_Init(usart_dev->cfg->regs, &USART_InitStructure); * Changes configuration of the USART peripheral without re-initialising.
* \param[in] usart_id USART name (GPS, TELEM, AUX)
* \param[in] word_len Requested word length
* \param[in] stop_bits Requested stop bits
* \param[in] parity Requested parity
* \param[in] baud_rate Requested baud rate
* \param[in] mode Requested mode
*
*/
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)
{
struct pios_usart_dev *usart_dev = (struct pios_usart_dev *)usart_id;
bool valid = PIOS_USART_validate(usart_dev);
PIOS_Assert(valid);
switch (word_len) {
case PIOS_COM_Word_length_8b:
usart_dev->init.USART_WordLength = USART_WordLength_8b;
break;
case PIOS_COM_Word_length_9b:
usart_dev->init.USART_WordLength = USART_WordLength_9b;
break;
default:
break;
}
switch (stop_bits) {
case PIOS_COM_StopBits_0_5:
usart_dev->init.USART_StopBits = USART_StopBits_0_5;
break;
case PIOS_COM_StopBits_1:
usart_dev->init.USART_StopBits = USART_StopBits_1;
break;
case PIOS_COM_StopBits_1_5:
usart_dev->init.USART_StopBits = USART_StopBits_1_5;
break;
case PIOS_COM_StopBits_2:
usart_dev->init.USART_StopBits = USART_StopBits_2;
break;
default:
break;
}
switch (parity) {
case PIOS_COM_Parity_No:
usart_dev->init.USART_Parity = USART_Parity_No;
break;
case PIOS_COM_Parity_Even:
usart_dev->init.USART_Parity = USART_Parity_Even;
break;
case PIOS_COM_Parity_Odd:
usart_dev->init.USART_Parity = USART_Parity_Odd;
break;
default:
break;
}
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) static void PIOS_USART_SetCtrlLine(uint32_t usart_id, uint32_t mask, uint32_t state)