From 6d80b55c601394fba66cbe8966ec85d8e80c48fc Mon Sep 17 00:00:00 2001 From: Alessio Morale Date: Mon, 28 Mar 2016 15:11:56 +0200 Subject: [PATCH 1/4] LP-270 - Implement PIOS_COM_ChangeConfig API (F1) --- flight/pios/common/pios_com.c | 21 ++++++++- flight/pios/inc/pios_com.h | 23 ++++++++++ flight/pios/stm32f10x/pios_usart.c | 74 +++++++++++++++++++++++++++++- 3 files changed, 116 insertions(+), 2 deletions(-) diff --git a/flight/pios/common/pios_com.c b/flight/pios/common/pios_com.c index 820595d11..32e8de032 100644 --- a/flight/pios/common/pios_com.c +++ b/flight/pios/common/pios_com.c @@ -7,7 +7,8 @@ * @{ * * @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 + * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. * @brief COM layer functions * @see The GNU Public License (GPL) Version 3 * @@ -281,6 +282,24 @@ 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) +{ + 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); + } + + return 0; +} + + /** * Set control lines associated with the port * \param[in] port COM port diff --git a/flight/pios/inc/pios_com.h b/flight/pios/inc/pios_com.h index 275054d26..23764e384 100644 --- a/flight/pios/inc/pios_com.h +++ b/flight/pios/inc/pios_com.h @@ -40,9 +40,31 @@ 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_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, +}; + 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_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); @@ -61,6 +83,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_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); diff --git a/flight/pios/stm32f10x/pios_usart.c b/flight/pios/stm32f10x/pios_usart.c index ff3286cfa..956dd8da9 100644 --- a/flight/pios/stm32f10x/pios_usart.c +++ b/flight/pios/stm32f10x/pios_usart.c @@ -7,7 +7,8 @@ * @{ * * @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 + * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. * @brief USART commands. Inits USARTs, controls USARTs & Interupt handlers. (STM32 dependent) * @see The GNU Public License (GPL) Version 3 * @@ -33,8 +34,10 @@ #ifdef PIOS_INCLUDE_USART #include +#include /* 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_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); @@ -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 = { .set_baud = PIOS_USART_ChangeBaud, + .set_config = PIOS_USART_ChangeConfig, .tx_start = PIOS_USART_TxStart, .rx_start = PIOS_USART_RxStart, .bind_tx_cb = PIOS_USART_RegisterTxCallback, @@ -249,6 +253,74 @@ static void PIOS_USART_ChangeBaud(uint32_t usart_id, uint32_t baud) 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 + * + */ +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) +{ + struct pios_usart_dev *usart_dev = (struct pios_usart_dev *)usart_id; + + bool valid = PIOS_USART_validate(usart_dev); + + 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; + break; + case PIOS_COM_Word_length_9b: + USART_InitStructure.USART_WordLength = USART_WordLength_9b; + break; + default: + break; + } + + switch (stop_bits) { + case PIOS_COM_StopBits_0_5: + USART_InitStructure.USART_StopBits = USART_StopBits_0_5; + break; + case PIOS_COM_StopBits_1: + USART_InitStructure.USART_StopBits = USART_StopBits_1; + break; + case PIOS_COM_StopBits_1_5: + USART_InitStructure.USART_StopBits = USART_StopBits_1_5; + break; + case PIOS_COM_StopBits_2: + USART_InitStructure.USART_StopBits = USART_StopBits_2; + break; + default: + break; + } + + switch (parity) { + case PIOS_COM_Parity_No: + USART_InitStructure.USART_Parity = USART_Parity_No; + break; + case PIOS_COM_Parity_Even: + USART_InitStructure.USART_Parity = USART_Parity_Even; + break; + case PIOS_COM_Parity_Odd: + USART_InitStructure.USART_Parity = USART_Parity_Odd; + break; + default: + break; + } + /* Write back the new configuration */ + USART_Init(usart_dev->cfg->regs, &USART_InitStructure); +} + static void PIOS_USART_RegisterRxCallback(uint32_t usart_id, pios_com_callback rx_in_cb, uint32_t context) { struct pios_usart_dev *usart_dev = (struct pios_usart_dev *)usart_id; From 97508793965eddd85071992f4bf1ea2548bab9e1 Mon Sep 17 00:00:00 2001 From: Alessio Morale Date: Mon, 28 Mar 2016 14:43:56 +0200 Subject: [PATCH 2/4] LP-270 - F0 and F4 Implementation --- flight/pios/stm32f0x/pios_usart.c | 66 ++++++++++++++++++++++++++- flight/pios/stm32f4xx/pios_usart.c | 73 +++++++++++++++++++++++++++++- 2 files changed, 137 insertions(+), 2 deletions(-) diff --git a/flight/pios/stm32f0x/pios_usart.c b/flight/pios/stm32f0x/pios_usart.c index 4239f31d5..e79b6cf4e 100644 --- a/flight/pios/stm32f0x/pios_usart.c +++ b/flight/pios/stm32f0x/pios_usart.c @@ -7,7 +7,8 @@ * @{ * * @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 + * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. * @brief USART commands. Inits USARTs, controls USARTs & Interupt handlers. (STM32 dependent) * @see The GNU Public License (GPL) Version 3 * @@ -37,6 +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_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); @@ -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 = { .set_baud = PIOS_USART_ChangeBaud, + .set_config = PIOS_USART_ChangeConfig, .tx_start = PIOS_USART_TxStart, .rx_start = PIOS_USART_RxStart, .bind_tx_cb = PIOS_USART_RegisterTxCallback, @@ -238,6 +241,67 @@ static void PIOS_USART_ChangeBaud(uint32_t usart_id, uint32_t baud) USART_Cmd(usart_dev->cfg->regs, ENABLE); } +/** + * 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 + * + */ +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) +{ + const 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; + break; + case PIOS_COM_Word_length_9b: + USART_InitStructure.USART_WordLength = USART_WordLength_9b; + break; + default: + break; + } + + switch (stop_bits) { + case PIOS_COM_StopBits_1: + USART_InitStructure.USART_StopBits = USART_StopBits_1; + break; + case PIOS_COM_StopBits_1_5: + USART_InitStructure.USART_StopBits = USART_StopBits_1_5; + break; + case PIOS_COM_StopBits_2: + USART_InitStructure.USART_StopBits = USART_StopBits_2; + break; + default: + break; + } + + switch (parity) { + case PIOS_COM_Parity_No: + USART_InitStructure.USART_Parity = USART_Parity_No; + break; + case PIOS_COM_Parity_Even: + USART_InitStructure.USART_Parity = USART_Parity_Even; + break; + case PIOS_COM_Parity_Odd: + USART_InitStructure.USART_Parity = USART_Parity_Odd; + break; + default: + break; + } + /* Write back the new configuration */ + USART_Init(usart_dev->cfg->regs, &USART_InitStructure); +} + static void PIOS_USART_RegisterRxCallback(uint32_t usart_id, pios_com_callback rx_in_cb, uint32_t context) { struct pios_usart_dev *usart_dev = PIOS_USART_validate(usart_id); diff --git a/flight/pios/stm32f4xx/pios_usart.c b/flight/pios/stm32f4xx/pios_usart.c index bcc26cacf..4f43d27da 100644 --- a/flight/pios/stm32f4xx/pios_usart.c +++ b/flight/pios/stm32f4xx/pios_usart.c @@ -7,7 +7,8 @@ * @{ * * @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 + * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. * @brief USART commands. Inits USARTs, controls USARTs & Interupt handlers. (STM32 dependent) * @see The GNU Public License (GPL) Version 3 * @@ -40,6 +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_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); @@ -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 = { .set_baud = PIOS_USART_ChangeBaud, + .set_config = PIOS_USART_ChangeConfig, .set_ctrl_line = PIOS_USART_SetCtrlLine, .tx_start = PIOS_USART_TxStart, .rx_start = PIOS_USART_RxStart, @@ -284,6 +287,74 @@ static void PIOS_USART_ChangeBaud(uint32_t usart_id, uint32_t baud) 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 + * + */ +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) +{ + struct pios_usart_dev *usart_dev = (struct pios_usart_dev *)usart_id; + + bool valid = PIOS_USART_validate(usart_dev); + + 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; + break; + case PIOS_COM_Word_length_9b: + USART_InitStructure.USART_WordLength = USART_WordLength_9b; + break; + default: + break; + } + + switch (stop_bits) { + case PIOS_COM_StopBits_0_5: + USART_InitStructure.USART_StopBits = USART_StopBits_0_5; + break; + case PIOS_COM_StopBits_1: + USART_InitStructure.USART_StopBits = USART_StopBits_1; + break; + case PIOS_COM_StopBits_1_5: + USART_InitStructure.USART_StopBits = USART_StopBits_1_5; + break; + case PIOS_COM_StopBits_2: + USART_InitStructure.USART_StopBits = USART_StopBits_2; + break; + default: + break; + } + + switch (parity) { + case PIOS_COM_Parity_No: + USART_InitStructure.USART_Parity = USART_Parity_No; + break; + case PIOS_COM_Parity_Even: + USART_InitStructure.USART_Parity = USART_Parity_Even; + break; + case PIOS_COM_Parity_Odd: + USART_InitStructure.USART_Parity = USART_Parity_Odd; + break; + default: + break; + } + /* Write back the new configuration */ + USART_Init(usart_dev->cfg->regs, &USART_InitStructure); +} + static void PIOS_USART_SetCtrlLine(uint32_t usart_id, uint32_t mask, uint32_t state) { struct pios_usart_dev *usart_dev = (struct pios_usart_dev *)usart_id; From 4bb5bee32b1b799bb1ec39374d1eaf39efb08c8d Mon Sep 17 00:00:00 2001 From: Vladimir Zidar Date: Mon, 23 Jan 2017 13:38:05 +0100 Subject: [PATCH 3/4] 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. --- flight/pios/common/pios_com.c | 5 +- flight/pios/inc/pios_com.h | 11 +++- flight/pios/stm32f0x/pios_usart.c | 80 +++++++++++++++++++----------- flight/pios/stm32f10x/pios_usart.c | 78 ++++++++++++++++++----------- flight/pios/stm32f4xx/pios_usart.c | 77 +++++++++++++++++----------- 5 files changed, 159 insertions(+), 92 deletions(-) diff --git a/flight/pios/common/pios_com.c b/flight/pios/common/pios_com.c index 32e8de032..927f477a5 100644 --- a/flight/pios/common/pios_com.c +++ b/flight/pios/common/pios_com.c @@ -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 diff --git a/flight/pios/inc/pios_com.h b/flight/pios/inc/pios_com.h index 23764e384..d74feb35d 100644 --- a/flight/pios/inc/pios_com.h +++ b/flight/pios/inc/pios_com.h @@ -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); diff --git a/flight/pios/stm32f0x/pios_usart.c b/flight/pios/stm32f0x/pios_usart.c index e79b6cf4e..5ff89c1d6 100644 --- a/flight/pios/stm32f0x/pios_usart.c +++ b/flight/pios/stm32f0x/pios_usart.c @@ -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) diff --git a/flight/pios/stm32f10x/pios_usart.c b/flight/pios/stm32f10x/pios_usart.c index 956dd8da9..f0d0fcc98 100644 --- a/flight/pios/stm32f10x/pios_usart.c +++ b/flight/pios/stm32f10x/pios_usart.c @@ -37,7 +37,7 @@ #include /* 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) diff --git a/flight/pios/stm32f4xx/pios_usart.c b/flight/pios/stm32f4xx/pios_usart.c index 4f43d27da..c7896f102 100644 --- a/flight/pios/stm32f4xx/pios_usart.c +++ b/flight/pios/stm32f4xx/pios_usart.c @@ -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) From 191f92ce91e415ce896324a5d626097592a7e96b Mon Sep 17 00:00:00 2001 From: Vladimir Zidar Date: Mon, 23 Jan 2017 13:47:20 +0100 Subject: [PATCH 4/4] LP-270 - Updated copyright years --- flight/pios/common/pios_com.c | 2 +- flight/pios/inc/pios_com.h | 3 ++- flight/pios/stm32f0x/pios_usart.c | 4 +++- flight/pios/stm32f10x/pios_usart.c | 4 +++- flight/pios/stm32f4xx/pios_usart.c | 4 +++- 5 files changed, 12 insertions(+), 5 deletions(-) diff --git a/flight/pios/common/pios_com.c b/flight/pios/common/pios_com.c index 927f477a5..d13a7ca89 100644 --- a/flight/pios/common/pios_com.c +++ b/flight/pios/common/pios_com.c @@ -7,7 +7,7 @@ * @{ * * @file pios_com.c - * @author The LibrePilot Project, http://www.librepilot.org, Copyright (c) 2015 + * @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 * @see The GNU Public License (GPL) Version 3 diff --git a/flight/pios/inc/pios_com.h b/flight/pios/inc/pios_com.h index d74feb35d..9364e5942 100644 --- a/flight/pios/inc/pios_com.h +++ b/flight/pios/inc/pios_com.h @@ -7,7 +7,8 @@ * @{ * * @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) * @brief COM layer functions header * @see The GNU Public License (GPL) Version 3 diff --git a/flight/pios/stm32f0x/pios_usart.c b/flight/pios/stm32f0x/pios_usart.c index 5ff89c1d6..0ce5de3cc 100644 --- a/flight/pios/stm32f0x/pios_usart.c +++ b/flight/pios/stm32f0x/pios_usart.c @@ -7,7 +7,7 @@ * @{ * * @file pios_usart.c - * @author The LibrePilot Project, http://www.librepilot.org, Copyright (c) 2016 + * @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) * @see The GNU Public License (GPL) Version 3 @@ -245,6 +245,8 @@ static void PIOS_USART_ChangeBaud(uint32_t usart_id, uint32_t baud) * \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, diff --git a/flight/pios/stm32f10x/pios_usart.c b/flight/pios/stm32f10x/pios_usart.c index f0d0fcc98..82cd577cd 100644 --- a/flight/pios/stm32f10x/pios_usart.c +++ b/flight/pios/stm32f10x/pios_usart.c @@ -7,7 +7,7 @@ * @{ * * @file pios_usart.c - * @author The LibrePilot Project, http://www.librepilot.org, Copyright (c) 2016 + * @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) * @see The GNU Public License (GPL) Version 3 @@ -257,6 +257,8 @@ static void PIOS_USART_ChangeBaud(uint32_t usart_id, uint32_t baud) * \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, diff --git a/flight/pios/stm32f4xx/pios_usart.c b/flight/pios/stm32f4xx/pios_usart.c index c7896f102..01d790963 100644 --- a/flight/pios/stm32f4xx/pios_usart.c +++ b/flight/pios/stm32f4xx/pios_usart.c @@ -7,7 +7,7 @@ * @{ * * @file pios_usart.c - * @author The LibrePilot Project, http://www.librepilot.org, Copyright (c) 2016 + * @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) * @see The GNU Public License (GPL) Version 3 @@ -292,6 +292,8 @@ static void PIOS_USART_ChangeBaud(uint32_t usart_id, uint32_t baud) * \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,