2010-09-27 09:28:45 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup PIOS PIOS Core hardware abstraction layer
|
|
|
|
* @{
|
|
|
|
* @addtogroup PIOS_USART USART Functions
|
|
|
|
* @brief PIOS interface for USART port
|
|
|
|
* @{
|
|
|
|
*
|
2013-05-19 16:37:30 +02:00
|
|
|
* @file pios_usart.c
|
2017-01-23 13:47:20 +01:00
|
|
|
* @author The LibrePilot Project, http://www.librepilot.org, Copyright (c) 2016-2017.
|
2016-03-28 15:11:56 +02:00
|
|
|
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
2010-09-27 09:28:45 +02:00
|
|
|
* @brief USART commands. Inits USARTs, controls USARTs & Interupt handlers. (STM32 dependent)
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
2013-05-19 16:37:30 +02:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2010-09-27 09:28:45 +02:00
|
|
|
* (at your option) any later version.
|
2013-05-19 16:37:30 +02:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
2010-09-27 09:28:45 +02:00
|
|
|
* for more details.
|
2013-05-19 16:37:30 +02:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2010-09-27 09:28:45 +02:00
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pios.h"
|
|
|
|
|
2013-03-15 19:25:30 +01:00
|
|
|
#ifdef PIOS_INCLUDE_USART
|
2010-09-27 09:28:45 +02:00
|
|
|
|
|
|
|
#include <pios_usart_priv.h>
|
2016-03-28 15:11:56 +02:00
|
|
|
#include <pios_usart.h>
|
2010-09-27 09:28:45 +02:00
|
|
|
|
|
|
|
/* Provide a COM driver */
|
2017-04-17 00:50:08 +02:00
|
|
|
static void PIOS_USART_ChangeConfig(uint32_t usart_id, enum PIOS_COM_Word_Length word_len, enum PIOS_COM_Parity parity, enum PIOS_COM_StopBits stop_bits, uint32_t baud_rate);
|
2011-02-12 23:19:43 +01:00
|
|
|
static void PIOS_USART_ChangeBaud(uint32_t usart_id, uint32_t baud);
|
2011-07-26 06:27:03 +02:00
|
|
|
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);
|
|
|
|
static void PIOS_USART_RxStart(uint32_t usart_id, uint16_t rx_bytes_avail);
|
2017-04-19 15:57:34 +02:00
|
|
|
static int32_t PIOS_USART_Ioctl(uint32_t usart_id, uint32_t ctl, void *param);
|
2011-02-12 23:19:43 +01:00
|
|
|
|
2010-09-27 09:28:45 +02:00
|
|
|
const struct pios_com_driver pios_usart_com_driver = {
|
2013-05-19 16:37:30 +02:00
|
|
|
.set_baud = PIOS_USART_ChangeBaud,
|
2016-03-28 15:11:56 +02:00
|
|
|
.set_config = PIOS_USART_ChangeConfig,
|
2013-05-19 16:37:30 +02:00
|
|
|
.tx_start = PIOS_USART_TxStart,
|
|
|
|
.rx_start = PIOS_USART_RxStart,
|
|
|
|
.bind_tx_cb = PIOS_USART_RegisterTxCallback,
|
|
|
|
.bind_rx_cb = PIOS_USART_RegisterRxCallback,
|
2017-04-19 15:58:31 +02:00
|
|
|
.ioctl = PIOS_USART_Ioctl,
|
2010-09-27 09:28:45 +02:00
|
|
|
};
|
|
|
|
|
bootcfg: use UAVobj to control boot-time HW config
This should mark an end to the compile-time selection of HW
configurations.
Minor changes in board initialization for all platforms:
- Most config structs are marked static to prevent badly written
drivers from directly referring to config data.
- Adapt to changes in .irq fields in config data.
- Adapt to changes in USART IRQ handling.
Major changes in board initialization for CC:
- Use HwSettings UAVObj to decide which drivers to attach to
the "main" port and the flexi port, and select the appropriate
device configuration data.
- HwSettings allows choosing between Disabled, Telemetry, SBUS,
Spektrum,GPS, and I2C for each of the two ports.
- Use ManualControlSettings.InputMode to init/configure the
appropriate receiver module, and register its available rx channels
with the PIOS_RCVR layer. Can choose between PWM, Spektrum and PPM
at board init time. PPM driver is broken, and SBUS will work once
it is added to this UAVObj as an option.
- CC build now includes code for SBUS, Spektrum and PWM receivers in
every firmware image.
PIOS_USART driver:
- Now handles its own low-level IRQs internally
- If NULL upper-level IRQ handler is bound in at board init time
then rx/tx is satisfied by internal PIOS_USART buffered IO routines
which are (typically) attached to the COM layer.
- If an alternate upper-level IRQ handler is bound in at board init
then that handler is called and expected to clear down the USART
IRQ sources. This is used by Spektrum and SBUS drivers.
PIOS_SBUS and PIOS_SPEKTRUM drivers:
- Improved data/API hiding
- No longer assume they know where their config data is stored which
allows for boot-time alternate configurations for the driver.
- Now registers an upper-level IRQ handlerwith the USART layer to
decouple the driver from which USART it is actually attached to.
2011-07-06 02:21:00 +02:00
|
|
|
enum pios_usart_dev_magic {
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_USART_DEV_MAGIC = 0x11223344,
|
bootcfg: use UAVobj to control boot-time HW config
This should mark an end to the compile-time selection of HW
configurations.
Minor changes in board initialization for all platforms:
- Most config structs are marked static to prevent badly written
drivers from directly referring to config data.
- Adapt to changes in .irq fields in config data.
- Adapt to changes in USART IRQ handling.
Major changes in board initialization for CC:
- Use HwSettings UAVObj to decide which drivers to attach to
the "main" port and the flexi port, and select the appropriate
device configuration data.
- HwSettings allows choosing between Disabled, Telemetry, SBUS,
Spektrum,GPS, and I2C for each of the two ports.
- Use ManualControlSettings.InputMode to init/configure the
appropriate receiver module, and register its available rx channels
with the PIOS_RCVR layer. Can choose between PWM, Spektrum and PPM
at board init time. PPM driver is broken, and SBUS will work once
it is added to this UAVObj as an option.
- CC build now includes code for SBUS, Spektrum and PWM receivers in
every firmware image.
PIOS_USART driver:
- Now handles its own low-level IRQs internally
- If NULL upper-level IRQ handler is bound in at board init time
then rx/tx is satisfied by internal PIOS_USART buffered IO routines
which are (typically) attached to the COM layer.
- If an alternate upper-level IRQ handler is bound in at board init
then that handler is called and expected to clear down the USART
IRQ sources. This is used by Spektrum and SBUS drivers.
PIOS_SBUS and PIOS_SPEKTRUM drivers:
- Improved data/API hiding
- No longer assume they know where their config data is stored which
allows for boot-time alternate configurations for the driver.
- Now registers an upper-level IRQ handlerwith the USART layer to
decouple the driver from which USART it is actually attached to.
2011-07-06 02:21:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct pios_usart_dev {
|
2013-05-19 16:37:30 +02:00
|
|
|
enum pios_usart_dev_magic magic;
|
|
|
|
const struct pios_usart_cfg *cfg;
|
2017-01-23 13:38:05 +01:00
|
|
|
USART_InitTypeDef init;
|
2013-05-19 16:37:30 +02:00
|
|
|
pios_com_callback rx_in_cb;
|
|
|
|
uint32_t rx_in_context;
|
|
|
|
pios_com_callback tx_out_cb;
|
|
|
|
uint32_t tx_out_context;
|
2017-05-30 14:50:46 +02:00
|
|
|
bool config_locked;
|
2013-05-19 16:37:30 +02:00
|
|
|
uint32_t rx_dropped;
|
2017-04-19 15:58:31 +02:00
|
|
|
uint8_t irq_channel;
|
bootcfg: use UAVobj to control boot-time HW config
This should mark an end to the compile-time selection of HW
configurations.
Minor changes in board initialization for all platforms:
- Most config structs are marked static to prevent badly written
drivers from directly referring to config data.
- Adapt to changes in .irq fields in config data.
- Adapt to changes in USART IRQ handling.
Major changes in board initialization for CC:
- Use HwSettings UAVObj to decide which drivers to attach to
the "main" port and the flexi port, and select the appropriate
device configuration data.
- HwSettings allows choosing between Disabled, Telemetry, SBUS,
Spektrum,GPS, and I2C for each of the two ports.
- Use ManualControlSettings.InputMode to init/configure the
appropriate receiver module, and register its available rx channels
with the PIOS_RCVR layer. Can choose between PWM, Spektrum and PPM
at board init time. PPM driver is broken, and SBUS will work once
it is added to this UAVObj as an option.
- CC build now includes code for SBUS, Spektrum and PWM receivers in
every firmware image.
PIOS_USART driver:
- Now handles its own low-level IRQs internally
- If NULL upper-level IRQ handler is bound in at board init time
then rx/tx is satisfied by internal PIOS_USART buffered IO routines
which are (typically) attached to the COM layer.
- If an alternate upper-level IRQ handler is bound in at board init
then that handler is called and expected to clear down the USART
IRQ sources. This is used by Spektrum and SBUS drivers.
PIOS_SBUS and PIOS_SPEKTRUM drivers:
- Improved data/API hiding
- No longer assume they know where their config data is stored which
allows for boot-time alternate configurations for the driver.
- Now registers an upper-level IRQ handlerwith the USART layer to
decouple the driver from which USART it is actually attached to.
2011-07-06 02:21:00 +02:00
|
|
|
};
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
static bool PIOS_USART_validate(struct pios_usart_dev *usart_dev)
|
2011-02-12 23:19:43 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
return usart_dev->magic == PIOS_USART_DEV_MAGIC;
|
2011-02-12 23:19:43 +01:00
|
|
|
}
|
|
|
|
|
2017-04-19 15:57:34 +02:00
|
|
|
const struct pios_usart_cfg *PIOS_USART_GetConfig(uint32_t usart_id)
|
|
|
|
{
|
|
|
|
struct pios_usart_dev *usart_dev = (struct pios_usart_dev *)usart_id;
|
2017-04-19 15:58:31 +02:00
|
|
|
|
2017-04-19 15:57:34 +02:00
|
|
|
bool valid = PIOS_USART_validate(usart_dev);
|
2017-04-19 15:58:31 +02:00
|
|
|
|
2017-04-19 15:57:34 +02:00
|
|
|
PIOS_Assert(valid);
|
2017-04-19 15:58:31 +02:00
|
|
|
|
2017-04-19 15:57:34 +02:00
|
|
|
return usart_dev->cfg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t PIOS_USART_SetIrqPrio(struct pios_usart_dev *usart_dev, uint8_t irq_prio)
|
|
|
|
{
|
|
|
|
NVIC_InitTypeDef init = {
|
|
|
|
.NVIC_IRQChannel = usart_dev->irq_channel,
|
|
|
|
.NVIC_IRQChannelPreemptionPriority = irq_prio,
|
|
|
|
.NVIC_IRQChannelCmd = ENABLE,
|
|
|
|
};
|
2017-04-19 15:58:31 +02:00
|
|
|
|
2017-04-19 15:57:34 +02:00
|
|
|
NVIC_Init(&init);
|
2017-04-19 15:58:31 +02:00
|
|
|
|
2017-04-19 15:57:34 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-01 04:35:03 +02:00
|
|
|
#if defined(PIOS_INCLUDE_FREERTOS)
|
2013-05-19 16:37:30 +02:00
|
|
|
static struct pios_usart_dev *PIOS_USART_alloc(void)
|
2010-09-27 09:28:45 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_usart_dev *usart_dev;
|
2011-02-12 23:19:43 +01:00
|
|
|
|
2014-06-11 20:11:48 +02:00
|
|
|
usart_dev = (struct pios_usart_dev *)pios_malloc(sizeof(struct pios_usart_dev));
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!usart_dev) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-02-12 23:19:43 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
memset(usart_dev, 0, sizeof(struct pios_usart_dev));
|
|
|
|
usart_dev->magic = PIOS_USART_DEV_MAGIC;
|
|
|
|
return usart_dev;
|
2011-02-12 23:19:43 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
static struct pios_usart_dev pios_usart_devs[PIOS_USART_MAX_DEVS];
|
|
|
|
static uint8_t pios_usart_num_devs;
|
2013-05-19 16:37:30 +02:00
|
|
|
static struct pios_usart_dev *PIOS_USART_alloc(void)
|
2011-02-12 23:19:43 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_usart_dev *usart_dev;
|
2011-02-12 23:19:43 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (pios_usart_num_devs >= PIOS_USART_MAX_DEVS) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-09-27 09:28:45 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
usart_dev = &pios_usart_devs[pios_usart_num_devs++];
|
2013-03-15 03:57:57 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
memset(usart_dev, 0, sizeof(struct pios_usart_dev));
|
|
|
|
usart_dev->magic = PIOS_USART_DEV_MAGIC;
|
2011-02-12 23:19:43 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return usart_dev;
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
2013-05-19 16:37:30 +02:00
|
|
|
#endif /* if defined(PIOS_INCLUDE_FREERTOS) */
|
2011-02-12 23:19:43 +01:00
|
|
|
|
bootcfg: use UAVobj to control boot-time HW config
This should mark an end to the compile-time selection of HW
configurations.
Minor changes in board initialization for all platforms:
- Most config structs are marked static to prevent badly written
drivers from directly referring to config data.
- Adapt to changes in .irq fields in config data.
- Adapt to changes in USART IRQ handling.
Major changes in board initialization for CC:
- Use HwSettings UAVObj to decide which drivers to attach to
the "main" port and the flexi port, and select the appropriate
device configuration data.
- HwSettings allows choosing between Disabled, Telemetry, SBUS,
Spektrum,GPS, and I2C for each of the two ports.
- Use ManualControlSettings.InputMode to init/configure the
appropriate receiver module, and register its available rx channels
with the PIOS_RCVR layer. Can choose between PWM, Spektrum and PPM
at board init time. PPM driver is broken, and SBUS will work once
it is added to this UAVObj as an option.
- CC build now includes code for SBUS, Spektrum and PWM receivers in
every firmware image.
PIOS_USART driver:
- Now handles its own low-level IRQs internally
- If NULL upper-level IRQ handler is bound in at board init time
then rx/tx is satisfied by internal PIOS_USART buffered IO routines
which are (typically) attached to the COM layer.
- If an alternate upper-level IRQ handler is bound in at board init
then that handler is called and expected to clear down the USART
IRQ sources. This is used by Spektrum and SBUS drivers.
PIOS_SBUS and PIOS_SPEKTRUM drivers:
- Improved data/API hiding
- No longer assume they know where their config data is stored which
allows for boot-time alternate configurations for the driver.
- Now registers an upper-level IRQ handlerwith the USART layer to
decouple the driver from which USART it is actually attached to.
2011-07-06 02:21:00 +02:00
|
|
|
/* Bind Interrupt Handlers
|
|
|
|
*
|
|
|
|
* Map all valid USART IRQs to the common interrupt handler
|
|
|
|
* and provide storage for a 32-bit device id IRQ to map
|
|
|
|
* each physical IRQ to a specific registered device instance.
|
|
|
|
*/
|
|
|
|
static void PIOS_USART_generic_irq_handler(uint32_t usart_id);
|
|
|
|
|
|
|
|
static uint32_t PIOS_USART_1_id;
|
2013-05-19 16:37:30 +02:00
|
|
|
void USART1_IRQHandler(void) __attribute__((alias("PIOS_USART_1_irq_handler")));
|
|
|
|
static void PIOS_USART_1_irq_handler(void)
|
bootcfg: use UAVobj to control boot-time HW config
This should mark an end to the compile-time selection of HW
configurations.
Minor changes in board initialization for all platforms:
- Most config structs are marked static to prevent badly written
drivers from directly referring to config data.
- Adapt to changes in .irq fields in config data.
- Adapt to changes in USART IRQ handling.
Major changes in board initialization for CC:
- Use HwSettings UAVObj to decide which drivers to attach to
the "main" port and the flexi port, and select the appropriate
device configuration data.
- HwSettings allows choosing between Disabled, Telemetry, SBUS,
Spektrum,GPS, and I2C for each of the two ports.
- Use ManualControlSettings.InputMode to init/configure the
appropriate receiver module, and register its available rx channels
with the PIOS_RCVR layer. Can choose between PWM, Spektrum and PPM
at board init time. PPM driver is broken, and SBUS will work once
it is added to this UAVObj as an option.
- CC build now includes code for SBUS, Spektrum and PWM receivers in
every firmware image.
PIOS_USART driver:
- Now handles its own low-level IRQs internally
- If NULL upper-level IRQ handler is bound in at board init time
then rx/tx is satisfied by internal PIOS_USART buffered IO routines
which are (typically) attached to the COM layer.
- If an alternate upper-level IRQ handler is bound in at board init
then that handler is called and expected to clear down the USART
IRQ sources. This is used by Spektrum and SBUS drivers.
PIOS_SBUS and PIOS_SPEKTRUM drivers:
- Improved data/API hiding
- No longer assume they know where their config data is stored which
allows for boot-time alternate configurations for the driver.
- Now registers an upper-level IRQ handlerwith the USART layer to
decouple the driver from which USART it is actually attached to.
2011-07-06 02:21:00 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_USART_generic_irq_handler(PIOS_USART_1_id);
|
bootcfg: use UAVobj to control boot-time HW config
This should mark an end to the compile-time selection of HW
configurations.
Minor changes in board initialization for all platforms:
- Most config structs are marked static to prevent badly written
drivers from directly referring to config data.
- Adapt to changes in .irq fields in config data.
- Adapt to changes in USART IRQ handling.
Major changes in board initialization for CC:
- Use HwSettings UAVObj to decide which drivers to attach to
the "main" port and the flexi port, and select the appropriate
device configuration data.
- HwSettings allows choosing between Disabled, Telemetry, SBUS,
Spektrum,GPS, and I2C for each of the two ports.
- Use ManualControlSettings.InputMode to init/configure the
appropriate receiver module, and register its available rx channels
with the PIOS_RCVR layer. Can choose between PWM, Spektrum and PPM
at board init time. PPM driver is broken, and SBUS will work once
it is added to this UAVObj as an option.
- CC build now includes code for SBUS, Spektrum and PWM receivers in
every firmware image.
PIOS_USART driver:
- Now handles its own low-level IRQs internally
- If NULL upper-level IRQ handler is bound in at board init time
then rx/tx is satisfied by internal PIOS_USART buffered IO routines
which are (typically) attached to the COM layer.
- If an alternate upper-level IRQ handler is bound in at board init
then that handler is called and expected to clear down the USART
IRQ sources. This is used by Spektrum and SBUS drivers.
PIOS_SBUS and PIOS_SPEKTRUM drivers:
- Improved data/API hiding
- No longer assume they know where their config data is stored which
allows for boot-time alternate configurations for the driver.
- Now registers an upper-level IRQ handlerwith the USART layer to
decouple the driver from which USART it is actually attached to.
2011-07-06 02:21:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t PIOS_USART_2_id;
|
2013-05-19 16:37:30 +02:00
|
|
|
void USART2_IRQHandler(void) __attribute__((alias("PIOS_USART_2_irq_handler")));
|
|
|
|
static void PIOS_USART_2_irq_handler(void)
|
bootcfg: use UAVobj to control boot-time HW config
This should mark an end to the compile-time selection of HW
configurations.
Minor changes in board initialization for all platforms:
- Most config structs are marked static to prevent badly written
drivers from directly referring to config data.
- Adapt to changes in .irq fields in config data.
- Adapt to changes in USART IRQ handling.
Major changes in board initialization for CC:
- Use HwSettings UAVObj to decide which drivers to attach to
the "main" port and the flexi port, and select the appropriate
device configuration data.
- HwSettings allows choosing between Disabled, Telemetry, SBUS,
Spektrum,GPS, and I2C for each of the two ports.
- Use ManualControlSettings.InputMode to init/configure the
appropriate receiver module, and register its available rx channels
with the PIOS_RCVR layer. Can choose between PWM, Spektrum and PPM
at board init time. PPM driver is broken, and SBUS will work once
it is added to this UAVObj as an option.
- CC build now includes code for SBUS, Spektrum and PWM receivers in
every firmware image.
PIOS_USART driver:
- Now handles its own low-level IRQs internally
- If NULL upper-level IRQ handler is bound in at board init time
then rx/tx is satisfied by internal PIOS_USART buffered IO routines
which are (typically) attached to the COM layer.
- If an alternate upper-level IRQ handler is bound in at board init
then that handler is called and expected to clear down the USART
IRQ sources. This is used by Spektrum and SBUS drivers.
PIOS_SBUS and PIOS_SPEKTRUM drivers:
- Improved data/API hiding
- No longer assume they know where their config data is stored which
allows for boot-time alternate configurations for the driver.
- Now registers an upper-level IRQ handlerwith the USART layer to
decouple the driver from which USART it is actually attached to.
2011-07-06 02:21:00 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_USART_generic_irq_handler(PIOS_USART_2_id);
|
bootcfg: use UAVobj to control boot-time HW config
This should mark an end to the compile-time selection of HW
configurations.
Minor changes in board initialization for all platforms:
- Most config structs are marked static to prevent badly written
drivers from directly referring to config data.
- Adapt to changes in .irq fields in config data.
- Adapt to changes in USART IRQ handling.
Major changes in board initialization for CC:
- Use HwSettings UAVObj to decide which drivers to attach to
the "main" port and the flexi port, and select the appropriate
device configuration data.
- HwSettings allows choosing between Disabled, Telemetry, SBUS,
Spektrum,GPS, and I2C for each of the two ports.
- Use ManualControlSettings.InputMode to init/configure the
appropriate receiver module, and register its available rx channels
with the PIOS_RCVR layer. Can choose between PWM, Spektrum and PPM
at board init time. PPM driver is broken, and SBUS will work once
it is added to this UAVObj as an option.
- CC build now includes code for SBUS, Spektrum and PWM receivers in
every firmware image.
PIOS_USART driver:
- Now handles its own low-level IRQs internally
- If NULL upper-level IRQ handler is bound in at board init time
then rx/tx is satisfied by internal PIOS_USART buffered IO routines
which are (typically) attached to the COM layer.
- If an alternate upper-level IRQ handler is bound in at board init
then that handler is called and expected to clear down the USART
IRQ sources. This is used by Spektrum and SBUS drivers.
PIOS_SBUS and PIOS_SPEKTRUM drivers:
- Improved data/API hiding
- No longer assume they know where their config data is stored which
allows for boot-time alternate configurations for the driver.
- Now registers an upper-level IRQ handlerwith the USART layer to
decouple the driver from which USART it is actually attached to.
2011-07-06 02:21:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t PIOS_USART_3_id;
|
2013-05-19 16:37:30 +02:00
|
|
|
void USART3_IRQHandler(void) __attribute__((alias("PIOS_USART_3_irq_handler")));
|
|
|
|
static void PIOS_USART_3_irq_handler(void)
|
bootcfg: use UAVobj to control boot-time HW config
This should mark an end to the compile-time selection of HW
configurations.
Minor changes in board initialization for all platforms:
- Most config structs are marked static to prevent badly written
drivers from directly referring to config data.
- Adapt to changes in .irq fields in config data.
- Adapt to changes in USART IRQ handling.
Major changes in board initialization for CC:
- Use HwSettings UAVObj to decide which drivers to attach to
the "main" port and the flexi port, and select the appropriate
device configuration data.
- HwSettings allows choosing between Disabled, Telemetry, SBUS,
Spektrum,GPS, and I2C for each of the two ports.
- Use ManualControlSettings.InputMode to init/configure the
appropriate receiver module, and register its available rx channels
with the PIOS_RCVR layer. Can choose between PWM, Spektrum and PPM
at board init time. PPM driver is broken, and SBUS will work once
it is added to this UAVObj as an option.
- CC build now includes code for SBUS, Spektrum and PWM receivers in
every firmware image.
PIOS_USART driver:
- Now handles its own low-level IRQs internally
- If NULL upper-level IRQ handler is bound in at board init time
then rx/tx is satisfied by internal PIOS_USART buffered IO routines
which are (typically) attached to the COM layer.
- If an alternate upper-level IRQ handler is bound in at board init
then that handler is called and expected to clear down the USART
IRQ sources. This is used by Spektrum and SBUS drivers.
PIOS_SBUS and PIOS_SPEKTRUM drivers:
- Improved data/API hiding
- No longer assume they know where their config data is stored which
allows for boot-time alternate configurations for the driver.
- Now registers an upper-level IRQ handlerwith the USART layer to
decouple the driver from which USART it is actually attached to.
2011-07-06 02:21:00 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_USART_generic_irq_handler(PIOS_USART_3_id);
|
bootcfg: use UAVobj to control boot-time HW config
This should mark an end to the compile-time selection of HW
configurations.
Minor changes in board initialization for all platforms:
- Most config structs are marked static to prevent badly written
drivers from directly referring to config data.
- Adapt to changes in .irq fields in config data.
- Adapt to changes in USART IRQ handling.
Major changes in board initialization for CC:
- Use HwSettings UAVObj to decide which drivers to attach to
the "main" port and the flexi port, and select the appropriate
device configuration data.
- HwSettings allows choosing between Disabled, Telemetry, SBUS,
Spektrum,GPS, and I2C for each of the two ports.
- Use ManualControlSettings.InputMode to init/configure the
appropriate receiver module, and register its available rx channels
with the PIOS_RCVR layer. Can choose between PWM, Spektrum and PPM
at board init time. PPM driver is broken, and SBUS will work once
it is added to this UAVObj as an option.
- CC build now includes code for SBUS, Spektrum and PWM receivers in
every firmware image.
PIOS_USART driver:
- Now handles its own low-level IRQs internally
- If NULL upper-level IRQ handler is bound in at board init time
then rx/tx is satisfied by internal PIOS_USART buffered IO routines
which are (typically) attached to the COM layer.
- If an alternate upper-level IRQ handler is bound in at board init
then that handler is called and expected to clear down the USART
IRQ sources. This is used by Spektrum and SBUS drivers.
PIOS_SBUS and PIOS_SPEKTRUM drivers:
- Improved data/API hiding
- No longer assume they know where their config data is stored which
allows for boot-time alternate configurations for the driver.
- Now registers an upper-level IRQ handlerwith the USART layer to
decouple the driver from which USART it is actually attached to.
2011-07-06 02:21:00 +02:00
|
|
|
}
|
2010-09-27 09:28:45 +02:00
|
|
|
|
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* Initialise a single USART device
|
|
|
|
*/
|
|
|
|
int32_t PIOS_USART_Init(uint32_t *usart_id, const struct pios_usart_cfg *cfg)
|
2010-09-27 09:28:45 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_DEBUG_Assert(usart_id);
|
|
|
|
PIOS_DEBUG_Assert(cfg);
|
|
|
|
|
2017-05-30 14:50:46 +02:00
|
|
|
uint32_t *local_id;
|
|
|
|
uint8_t irq_channel;
|
|
|
|
|
|
|
|
switch ((uint32_t)cfg->regs) {
|
|
|
|
case (uint32_t)USART1:
|
|
|
|
local_id = &PIOS_USART_1_id;
|
|
|
|
irq_channel = USART1_IRQn;
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
|
|
|
|
break;
|
|
|
|
case (uint32_t)USART2:
|
|
|
|
local_id = &PIOS_USART_2_id;
|
|
|
|
irq_channel = USART2_IRQn;
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
|
|
|
break;
|
|
|
|
case (uint32_t)USART3:
|
|
|
|
local_id = &PIOS_USART_3_id;
|
|
|
|
irq_channel = USART3_IRQn;
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto out_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*local_id) {
|
|
|
|
/* this port is already open */
|
|
|
|
*usart_id = *local_id;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_usart_dev *usart_dev;
|
|
|
|
|
|
|
|
usart_dev = (struct pios_usart_dev *)PIOS_USART_alloc();
|
|
|
|
if (!usart_dev) {
|
|
|
|
goto out_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bind the configuration to the device instance */
|
2017-04-19 15:58:31 +02:00
|
|
|
usart_dev->cfg = cfg;
|
2017-05-30 14:50:46 +02:00
|
|
|
usart_dev->irq_channel = irq_channel;
|
2017-01-23 13:38:05 +01:00
|
|
|
|
2017-04-19 15:57:34 +02:00
|
|
|
/* Initialize the comm parameter structure */
|
|
|
|
USART_StructInit(&usart_dev->init); // 9600 8n1
|
2013-05-19 16:37:30 +02:00
|
|
|
|
2017-04-17 00:50:08 +02:00
|
|
|
/* We will set modes later, depending on installed callbacks */
|
|
|
|
usart_dev->init.USART_Mode = 0;
|
2013-05-19 16:37:30 +02:00
|
|
|
|
2017-04-17 00:50:08 +02:00
|
|
|
/* DTR handling? */
|
2013-05-19 16:37:30 +02:00
|
|
|
|
2017-04-25 13:11:37 +02:00
|
|
|
#ifdef PIOS_USART_INVERTER_PORT
|
|
|
|
/* Initialize inverter gpio and set it to off */
|
|
|
|
if (usart_dev->cfg->regs == PIOS_USART_INVERTER_PORT) {
|
|
|
|
GPIO_InitTypeDef inverterGPIOInit = {
|
|
|
|
.GPIO_Pin = PIOS_USART_INVERTER_PIN,
|
|
|
|
.GPIO_Mode = GPIO_Mode_Out_PP,
|
|
|
|
.GPIO_Speed = GPIO_Speed_2MHz,
|
|
|
|
};
|
|
|
|
GPIO_Init(PIOS_USART_INVERTER_GPIO, &inverterGPIOInit);
|
|
|
|
|
|
|
|
GPIO_WriteBit(PIOS_USART_INVERTER_GPIO,
|
|
|
|
PIOS_USART_INVERTER_PIN,
|
|
|
|
PIOS_USART_INVERTER_DISABLE);
|
|
|
|
}
|
|
|
|
#endif
|
2013-05-19 16:37:30 +02:00
|
|
|
|
2017-05-30 14:50:46 +02:00
|
|
|
*usart_id = (uint32_t)usart_dev;
|
|
|
|
*local_id = (uint32_t)usart_dev;
|
2017-04-19 15:58:31 +02:00
|
|
|
|
2017-04-19 15:57:34 +02:00
|
|
|
PIOS_USART_SetIrqPrio(usart_dev, PIOS_IRQ_PRIO_MID);
|
2017-04-19 15:58:31 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return 0;
|
2011-02-12 23:19:43 +01:00
|
|
|
|
|
|
|
out_fail:
|
2013-05-19 16:37:30 +02:00
|
|
|
return -1;
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
2017-04-17 00:50:08 +02:00
|
|
|
static void PIOS_USART_Setup(struct pios_usart_dev *usart_dev)
|
|
|
|
{
|
|
|
|
/* Configure RX GPIO */
|
|
|
|
if ((usart_dev->init.USART_Mode & USART_Mode_Rx) && (usart_dev->cfg->rx.gpio)) {
|
|
|
|
if (usart_dev->cfg->remap) {
|
|
|
|
GPIO_PinRemapConfig(usart_dev->cfg->remap, ENABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
GPIO_Init(usart_dev->cfg->rx.gpio, (GPIO_InitTypeDef *)&usart_dev->cfg->rx.init);
|
|
|
|
|
|
|
|
USART_ITConfig(usart_dev->cfg->regs, USART_IT_RXNE, ENABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Configure TX GPIO */
|
|
|
|
if ((usart_dev->init.USART_Mode & USART_Mode_Tx) && usart_dev->cfg->tx.gpio) {
|
|
|
|
if (usart_dev->cfg->remap) {
|
|
|
|
GPIO_PinRemapConfig(usart_dev->cfg->remap, ENABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
GPIO_Init(usart_dev->cfg->tx.gpio, (GPIO_InitTypeDef *)&usart_dev->cfg->tx.init);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write new configuration */
|
2017-05-30 14:50:46 +02:00
|
|
|
{ // fix parity stuff
|
|
|
|
USART_InitTypeDef init = usart_dev->init;
|
|
|
|
|
|
|
|
if ((init.USART_Parity != USART_Parity_No) && (init.USART_WordLength == USART_WordLength_8b)) {
|
|
|
|
init.USART_WordLength = USART_WordLength_9b;
|
|
|
|
}
|
|
|
|
|
|
|
|
USART_Init(usart_dev->cfg->regs, &init);
|
|
|
|
}
|
|
|
|
|
2017-04-17 00:50:08 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Re enable USART.
|
|
|
|
*/
|
|
|
|
USART_Cmd(usart_dev->cfg->regs, ENABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-05 09:02:24 +02:00
|
|
|
static void PIOS_USART_RxStart(uint32_t usart_id, __attribute__((unused)) uint16_t rx_bytes_avail)
|
bootcfg: use UAVobj to control boot-time HW config
This should mark an end to the compile-time selection of HW
configurations.
Minor changes in board initialization for all platforms:
- Most config structs are marked static to prevent badly written
drivers from directly referring to config data.
- Adapt to changes in .irq fields in config data.
- Adapt to changes in USART IRQ handling.
Major changes in board initialization for CC:
- Use HwSettings UAVObj to decide which drivers to attach to
the "main" port and the flexi port, and select the appropriate
device configuration data.
- HwSettings allows choosing between Disabled, Telemetry, SBUS,
Spektrum,GPS, and I2C for each of the two ports.
- Use ManualControlSettings.InputMode to init/configure the
appropriate receiver module, and register its available rx channels
with the PIOS_RCVR layer. Can choose between PWM, Spektrum and PPM
at board init time. PPM driver is broken, and SBUS will work once
it is added to this UAVObj as an option.
- CC build now includes code for SBUS, Spektrum and PWM receivers in
every firmware image.
PIOS_USART driver:
- Now handles its own low-level IRQs internally
- If NULL upper-level IRQ handler is bound in at board init time
then rx/tx is satisfied by internal PIOS_USART buffered IO routines
which are (typically) attached to the COM layer.
- If an alternate upper-level IRQ handler is bound in at board init
then that handler is called and expected to clear down the USART
IRQ sources. This is used by Spektrum and SBUS drivers.
PIOS_SBUS and PIOS_SPEKTRUM drivers:
- Improved data/API hiding
- No longer assume they know where their config data is stored which
allows for boot-time alternate configurations for the driver.
- Now registers an upper-level IRQ handlerwith the USART layer to
decouple the driver from which USART it is actually attached to.
2011-07-06 02:21:00 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_usart_dev *usart_dev = (struct pios_usart_dev *)usart_id;
|
bootcfg: use UAVobj to control boot-time HW config
This should mark an end to the compile-time selection of HW
configurations.
Minor changes in board initialization for all platforms:
- Most config structs are marked static to prevent badly written
drivers from directly referring to config data.
- Adapt to changes in .irq fields in config data.
- Adapt to changes in USART IRQ handling.
Major changes in board initialization for CC:
- Use HwSettings UAVObj to decide which drivers to attach to
the "main" port and the flexi port, and select the appropriate
device configuration data.
- HwSettings allows choosing between Disabled, Telemetry, SBUS,
Spektrum,GPS, and I2C for each of the two ports.
- Use ManualControlSettings.InputMode to init/configure the
appropriate receiver module, and register its available rx channels
with the PIOS_RCVR layer. Can choose between PWM, Spektrum and PPM
at board init time. PPM driver is broken, and SBUS will work once
it is added to this UAVObj as an option.
- CC build now includes code for SBUS, Spektrum and PWM receivers in
every firmware image.
PIOS_USART driver:
- Now handles its own low-level IRQs internally
- If NULL upper-level IRQ handler is bound in at board init time
then rx/tx is satisfied by internal PIOS_USART buffered IO routines
which are (typically) attached to the COM layer.
- If an alternate upper-level IRQ handler is bound in at board init
then that handler is called and expected to clear down the USART
IRQ sources. This is used by Spektrum and SBUS drivers.
PIOS_SBUS and PIOS_SPEKTRUM drivers:
- Improved data/API hiding
- No longer assume they know where their config data is stored which
allows for boot-time alternate configurations for the driver.
- Now registers an upper-level IRQ handlerwith the USART layer to
decouple the driver from which USART it is actually attached to.
2011-07-06 02:21:00 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
bool valid = PIOS_USART_validate(usart_dev);
|
bootcfg: use UAVobj to control boot-time HW config
This should mark an end to the compile-time selection of HW
configurations.
Minor changes in board initialization for all platforms:
- Most config structs are marked static to prevent badly written
drivers from directly referring to config data.
- Adapt to changes in .irq fields in config data.
- Adapt to changes in USART IRQ handling.
Major changes in board initialization for CC:
- Use HwSettings UAVObj to decide which drivers to attach to
the "main" port and the flexi port, and select the appropriate
device configuration data.
- HwSettings allows choosing between Disabled, Telemetry, SBUS,
Spektrum,GPS, and I2C for each of the two ports.
- Use ManualControlSettings.InputMode to init/configure the
appropriate receiver module, and register its available rx channels
with the PIOS_RCVR layer. Can choose between PWM, Spektrum and PPM
at board init time. PPM driver is broken, and SBUS will work once
it is added to this UAVObj as an option.
- CC build now includes code for SBUS, Spektrum and PWM receivers in
every firmware image.
PIOS_USART driver:
- Now handles its own low-level IRQs internally
- If NULL upper-level IRQ handler is bound in at board init time
then rx/tx is satisfied by internal PIOS_USART buffered IO routines
which are (typically) attached to the COM layer.
- If an alternate upper-level IRQ handler is bound in at board init
then that handler is called and expected to clear down the USART
IRQ sources. This is used by Spektrum and SBUS drivers.
PIOS_SBUS and PIOS_SPEKTRUM drivers:
- Improved data/API hiding
- No longer assume they know where their config data is stored which
allows for boot-time alternate configurations for the driver.
- Now registers an upper-level IRQ handlerwith the USART layer to
decouple the driver from which USART it is actually attached to.
2011-07-06 02:21:00 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(valid);
|
|
|
|
|
|
|
|
USART_ITConfig(usart_dev->cfg->regs, USART_IT_RXNE, ENABLE);
|
2011-07-26 06:27:03 +02:00
|
|
|
}
|
2013-05-05 09:02:24 +02:00
|
|
|
static void PIOS_USART_TxStart(uint32_t usart_id, __attribute__((unused)) uint16_t tx_bytes_avail)
|
2011-07-26 06:27:03 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_usart_dev *usart_dev = (struct pios_usart_dev *)usart_id;
|
|
|
|
|
|
|
|
bool valid = PIOS_USART_validate(usart_dev);
|
bootcfg: use UAVobj to control boot-time HW config
This should mark an end to the compile-time selection of HW
configurations.
Minor changes in board initialization for all platforms:
- Most config structs are marked static to prevent badly written
drivers from directly referring to config data.
- Adapt to changes in .irq fields in config data.
- Adapt to changes in USART IRQ handling.
Major changes in board initialization for CC:
- Use HwSettings UAVObj to decide which drivers to attach to
the "main" port and the flexi port, and select the appropriate
device configuration data.
- HwSettings allows choosing between Disabled, Telemetry, SBUS,
Spektrum,GPS, and I2C for each of the two ports.
- Use ManualControlSettings.InputMode to init/configure the
appropriate receiver module, and register its available rx channels
with the PIOS_RCVR layer. Can choose between PWM, Spektrum and PPM
at board init time. PPM driver is broken, and SBUS will work once
it is added to this UAVObj as an option.
- CC build now includes code for SBUS, Spektrum and PWM receivers in
every firmware image.
PIOS_USART driver:
- Now handles its own low-level IRQs internally
- If NULL upper-level IRQ handler is bound in at board init time
then rx/tx is satisfied by internal PIOS_USART buffered IO routines
which are (typically) attached to the COM layer.
- If an alternate upper-level IRQ handler is bound in at board init
then that handler is called and expected to clear down the USART
IRQ sources. This is used by Spektrum and SBUS drivers.
PIOS_SBUS and PIOS_SPEKTRUM drivers:
- Improved data/API hiding
- No longer assume they know where their config data is stored which
allows for boot-time alternate configurations for the driver.
- Now registers an upper-level IRQ handlerwith the USART layer to
decouple the driver from which USART it is actually attached to.
2011-07-06 02:21:00 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(valid);
|
2011-07-26 06:27:03 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
USART_ITConfig(usart_dev->cfg->regs, USART_IT_TXE, ENABLE);
|
bootcfg: use UAVobj to control boot-time HW config
This should mark an end to the compile-time selection of HW
configurations.
Minor changes in board initialization for all platforms:
- Most config structs are marked static to prevent badly written
drivers from directly referring to config data.
- Adapt to changes in .irq fields in config data.
- Adapt to changes in USART IRQ handling.
Major changes in board initialization for CC:
- Use HwSettings UAVObj to decide which drivers to attach to
the "main" port and the flexi port, and select the appropriate
device configuration data.
- HwSettings allows choosing between Disabled, Telemetry, SBUS,
Spektrum,GPS, and I2C for each of the two ports.
- Use ManualControlSettings.InputMode to init/configure the
appropriate receiver module, and register its available rx channels
with the PIOS_RCVR layer. Can choose between PWM, Spektrum and PPM
at board init time. PPM driver is broken, and SBUS will work once
it is added to this UAVObj as an option.
- CC build now includes code for SBUS, Spektrum and PWM receivers in
every firmware image.
PIOS_USART driver:
- Now handles its own low-level IRQs internally
- If NULL upper-level IRQ handler is bound in at board init time
then rx/tx is satisfied by internal PIOS_USART buffered IO routines
which are (typically) attached to the COM layer.
- If an alternate upper-level IRQ handler is bound in at board init
then that handler is called and expected to clear down the USART
IRQ sources. This is used by Spektrum and SBUS drivers.
PIOS_SBUS and PIOS_SPEKTRUM drivers:
- Improved data/API hiding
- No longer assume they know where their config data is stored which
allows for boot-time alternate configurations for the driver.
- Now registers an upper-level IRQ handlerwith the USART layer to
decouple the driver from which USART it is actually attached to.
2011-07-06 02:21:00 +02:00
|
|
|
}
|
|
|
|
|
2010-09-27 09:28:45 +02:00
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* Changes the baud rate of the USART peripheral without re-initialising.
|
|
|
|
* \param[in] usart_id USART name (GPS, TELEM, AUX)
|
|
|
|
* \param[in] baud Requested baud rate
|
|
|
|
*/
|
2011-02-12 23:19:43 +01:00
|
|
|
static void PIOS_USART_ChangeBaud(uint32_t usart_id, uint32_t baud)
|
2010-09-27 09:28:45 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_usart_dev *usart_dev = (struct pios_usart_dev *)usart_id;
|
|
|
|
|
|
|
|
bool valid = PIOS_USART_validate(usart_dev);
|
2010-09-27 09:28:45 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(valid);
|
2010-09-27 09:28:45 +02:00
|
|
|
|
2017-05-30 14:50:46 +02:00
|
|
|
if (usart_dev->config_locked) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-23 13:38:05 +01:00
|
|
|
/* Use our working copy of the usart init structure */
|
|
|
|
usart_dev->init.USART_BaudRate = baud;
|
2010-09-27 09:28:45 +02:00
|
|
|
|
2017-04-17 00:50:08 +02:00
|
|
|
PIOS_USART_Setup(usart_dev);
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
2016-03-28 15:11:56 +02:00
|
|
|
/**
|
|
|
|
* 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
|
2017-01-23 13:47:20 +01:00
|
|
|
* \param[in] baud_rate Requested baud rate
|
|
|
|
* \param[in] mode Requested mode
|
2016-03-28 15:11:56 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static void PIOS_USART_ChangeConfig(uint32_t usart_id,
|
|
|
|
enum PIOS_COM_Word_Length word_len,
|
2017-01-23 13:38:05 +01:00
|
|
|
enum PIOS_COM_Parity parity,
|
2017-04-17 00:50:08 +02:00
|
|
|
enum PIOS_COM_StopBits stop_bits,
|
|
|
|
uint32_t baud_rate)
|
2016-03-28 15:11:56 +02:00
|
|
|
{
|
|
|
|
struct pios_usart_dev *usart_dev = (struct pios_usart_dev *)usart_id;
|
|
|
|
|
|
|
|
bool valid = PIOS_USART_validate(usart_dev);
|
|
|
|
|
|
|
|
PIOS_Assert(valid);
|
|
|
|
|
2017-05-30 14:50:46 +02:00
|
|
|
if (usart_dev->config_locked) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-28 15:11:56 +02:00
|
|
|
switch (word_len) {
|
|
|
|
case PIOS_COM_Word_length_8b:
|
2017-01-23 13:38:05 +01:00
|
|
|
usart_dev->init.USART_WordLength = USART_WordLength_8b;
|
2016-03-28 15:11:56 +02:00
|
|
|
break;
|
|
|
|
case PIOS_COM_Word_length_9b:
|
2017-01-23 13:38:05 +01:00
|
|
|
usart_dev->init.USART_WordLength = USART_WordLength_9b;
|
2016-03-28 15:11:56 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (stop_bits) {
|
|
|
|
case PIOS_COM_StopBits_0_5:
|
2017-01-23 13:38:05 +01:00
|
|
|
usart_dev->init.USART_StopBits = USART_StopBits_0_5;
|
2016-03-28 15:11:56 +02:00
|
|
|
break;
|
|
|
|
case PIOS_COM_StopBits_1:
|
2017-01-23 13:38:05 +01:00
|
|
|
usart_dev->init.USART_StopBits = USART_StopBits_1;
|
2016-03-28 15:11:56 +02:00
|
|
|
break;
|
|
|
|
case PIOS_COM_StopBits_1_5:
|
2017-01-23 13:38:05 +01:00
|
|
|
usart_dev->init.USART_StopBits = USART_StopBits_1_5;
|
2016-03-28 15:11:56 +02:00
|
|
|
break;
|
|
|
|
case PIOS_COM_StopBits_2:
|
2017-01-23 13:38:05 +01:00
|
|
|
usart_dev->init.USART_StopBits = USART_StopBits_2;
|
2016-03-28 15:11:56 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (parity) {
|
|
|
|
case PIOS_COM_Parity_No:
|
2017-01-23 13:38:05 +01:00
|
|
|
usart_dev->init.USART_Parity = USART_Parity_No;
|
2016-03-28 15:11:56 +02:00
|
|
|
break;
|
|
|
|
case PIOS_COM_Parity_Even:
|
2017-01-23 13:38:05 +01:00
|
|
|
usart_dev->init.USART_Parity = USART_Parity_Even;
|
2016-03-28 15:11:56 +02:00
|
|
|
break;
|
|
|
|
case PIOS_COM_Parity_Odd:
|
2017-01-23 13:38:05 +01:00
|
|
|
usart_dev->init.USART_Parity = USART_Parity_Odd;
|
2016-03-28 15:11:56 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-01-23 13:38:05 +01:00
|
|
|
|
|
|
|
if (baud_rate) {
|
|
|
|
usart_dev->init.USART_BaudRate = baud_rate;
|
|
|
|
}
|
|
|
|
|
2017-04-17 00:50:08 +02:00
|
|
|
PIOS_USART_Setup(usart_dev);
|
2016-03-28 15:11:56 +02:00
|
|
|
}
|
|
|
|
|
2011-07-26 06:27:03 +02:00
|
|
|
static void PIOS_USART_RegisterRxCallback(uint32_t usart_id, pios_com_callback rx_in_cb, uint32_t context)
|
2010-09-27 09:28:45 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_usart_dev *usart_dev = (struct pios_usart_dev *)usart_id;
|
|
|
|
|
|
|
|
bool valid = PIOS_USART_validate(usart_dev);
|
2010-09-27 09:28:45 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(valid);
|
2010-09-27 09:28:45 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/*
|
|
|
|
* Order is important in these assignments since ISR uses _cb
|
|
|
|
* field to determine if it's ok to dereference _cb and _context
|
|
|
|
*/
|
2017-04-17 00:50:08 +02:00
|
|
|
usart_dev->rx_in_context = context;
|
2013-05-19 16:37:30 +02:00
|
|
|
usart_dev->rx_in_cb = rx_in_cb;
|
2017-04-17 00:50:08 +02:00
|
|
|
|
|
|
|
usart_dev->init.USART_Mode |= USART_Mode_Rx;
|
|
|
|
|
|
|
|
PIOS_USART_Setup(usart_dev);
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
2011-07-26 06:27:03 +02:00
|
|
|
static void PIOS_USART_RegisterTxCallback(uint32_t usart_id, pios_com_callback tx_out_cb, uint32_t context)
|
2010-09-27 09:28:45 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_usart_dev *usart_dev = (struct pios_usart_dev *)usart_id;
|
2010-09-27 09:28:45 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
bool valid = PIOS_USART_validate(usart_dev);
|
2010-09-27 09:28:45 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(valid);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Order is important in these assignments since ISR uses _cb
|
|
|
|
* field to determine if it's ok to dereference _cb and _context
|
|
|
|
*/
|
2017-04-17 00:50:08 +02:00
|
|
|
usart_dev->tx_out_context = context;
|
2013-05-19 16:37:30 +02:00
|
|
|
usart_dev->tx_out_cb = tx_out_cb;
|
2017-04-17 00:50:08 +02:00
|
|
|
|
|
|
|
usart_dev->init.USART_Mode |= USART_Mode_Tx;
|
|
|
|
|
|
|
|
PIOS_USART_Setup(usart_dev);
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
bootcfg: use UAVobj to control boot-time HW config
This should mark an end to the compile-time selection of HW
configurations.
Minor changes in board initialization for all platforms:
- Most config structs are marked static to prevent badly written
drivers from directly referring to config data.
- Adapt to changes in .irq fields in config data.
- Adapt to changes in USART IRQ handling.
Major changes in board initialization for CC:
- Use HwSettings UAVObj to decide which drivers to attach to
the "main" port and the flexi port, and select the appropriate
device configuration data.
- HwSettings allows choosing between Disabled, Telemetry, SBUS,
Spektrum,GPS, and I2C for each of the two ports.
- Use ManualControlSettings.InputMode to init/configure the
appropriate receiver module, and register its available rx channels
with the PIOS_RCVR layer. Can choose between PWM, Spektrum and PPM
at board init time. PPM driver is broken, and SBUS will work once
it is added to this UAVObj as an option.
- CC build now includes code for SBUS, Spektrum and PWM receivers in
every firmware image.
PIOS_USART driver:
- Now handles its own low-level IRQs internally
- If NULL upper-level IRQ handler is bound in at board init time
then rx/tx is satisfied by internal PIOS_USART buffered IO routines
which are (typically) attached to the COM layer.
- If an alternate upper-level IRQ handler is bound in at board init
then that handler is called and expected to clear down the USART
IRQ sources. This is used by Spektrum and SBUS drivers.
PIOS_SBUS and PIOS_SPEKTRUM drivers:
- Improved data/API hiding
- No longer assume they know where their config data is stored which
allows for boot-time alternate configurations for the driver.
- Now registers an upper-level IRQ handlerwith the USART layer to
decouple the driver from which USART it is actually attached to.
2011-07-06 02:21:00 +02:00
|
|
|
static void PIOS_USART_generic_irq_handler(uint32_t usart_id)
|
2010-09-27 09:28:45 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_usart_dev *usart_dev = (struct pios_usart_dev *)usart_id;
|
|
|
|
|
|
|
|
bool valid = PIOS_USART_validate(usart_dev);
|
|
|
|
|
|
|
|
PIOS_Assert(valid);
|
|
|
|
|
|
|
|
/* Force read of dr after sr to make sure to clear error flags */
|
|
|
|
volatile uint16_t sr = usart_dev->cfg->regs->SR;
|
|
|
|
volatile uint8_t dr = usart_dev->cfg->regs->DR;
|
|
|
|
|
|
|
|
/* Check if RXNE flag is set */
|
|
|
|
bool rx_need_yield = false;
|
|
|
|
if (sr & USART_SR_RXNE) {
|
|
|
|
uint8_t byte = dr;
|
|
|
|
if (usart_dev->rx_in_cb) {
|
|
|
|
uint16_t rc;
|
|
|
|
rc = (usart_dev->rx_in_cb)(usart_dev->rx_in_context, &byte, 1, NULL, &rx_need_yield);
|
|
|
|
if (rc < 1) {
|
|
|
|
/* Lost bytes on rx */
|
|
|
|
usart_dev->rx_dropped += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if TXE flag is set */
|
|
|
|
bool tx_need_yield = false;
|
|
|
|
if (sr & USART_SR_TXE) {
|
|
|
|
if (usart_dev->tx_out_cb) {
|
|
|
|
uint8_t b;
|
|
|
|
uint16_t bytes_to_send;
|
|
|
|
|
|
|
|
bytes_to_send = (usart_dev->tx_out_cb)(usart_dev->tx_out_context, &b, 1, NULL, &tx_need_yield);
|
|
|
|
|
|
|
|
if (bytes_to_send > 0) {
|
|
|
|
/* Send the byte we've been given */
|
|
|
|
usart_dev->cfg->regs->DR = b;
|
|
|
|
} else {
|
|
|
|
/* No bytes to send, disable TXE interrupt */
|
|
|
|
USART_ITConfig(usart_dev->cfg->regs, USART_IT_TXE, DISABLE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* No bytes to send, disable TXE interrupt */
|
|
|
|
USART_ITConfig(usart_dev->cfg->regs, USART_IT_TXE, DISABLE);
|
|
|
|
}
|
|
|
|
}
|
2011-07-26 06:27:03 +02:00
|
|
|
|
|
|
|
#if defined(PIOS_INCLUDE_FREERTOS)
|
2013-05-19 16:37:30 +02:00
|
|
|
if (rx_need_yield || tx_need_yield) {
|
2013-09-05 05:26:23 +02:00
|
|
|
vPortYield();
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
#endif /* PIOS_INCLUDE_FREERTOS */
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
2017-04-19 15:57:34 +02:00
|
|
|
static int32_t PIOS_USART_Ioctl(uint32_t usart_id, uint32_t ctl, void *param)
|
|
|
|
{
|
|
|
|
struct pios_usart_dev *usart_dev = (struct pios_usart_dev *)usart_id;
|
2017-04-19 15:58:31 +02:00
|
|
|
|
2017-04-19 15:57:34 +02:00
|
|
|
bool valid = PIOS_USART_validate(usart_dev);
|
2017-04-19 15:58:31 +02:00
|
|
|
|
2017-04-19 15:57:34 +02:00
|
|
|
PIOS_Assert(valid);
|
2017-04-19 15:58:31 +02:00
|
|
|
|
2017-04-25 13:11:37 +02:00
|
|
|
/* First try board specific IOCTL to allow overriding default functions */
|
|
|
|
if (usart_dev->cfg->ioctl) {
|
|
|
|
int32_t ret = usart_dev->cfg->ioctl(usart_id, ctl, param);
|
|
|
|
if (ret != COM_IOCTL_ENOSYS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 15:57:34 +02:00
|
|
|
switch (ctl) {
|
2017-04-19 15:58:31 +02:00
|
|
|
case PIOS_IOCTL_USART_SET_IRQ_PRIO:
|
|
|
|
return PIOS_USART_SetIrqPrio(usart_dev, *(uint8_t *)param);
|
|
|
|
|
2017-04-25 13:11:37 +02:00
|
|
|
#ifdef PIOS_USART_INVERTER_PORT
|
|
|
|
case PIOS_IOCTL_USART_SET_INVERTED:
|
|
|
|
if (usart_dev->cfg->regs != PIOS_USART_INVERTER_PORT) {
|
|
|
|
return COM_IOCTL_ENOSYS; /* don't know how */
|
|
|
|
}
|
|
|
|
GPIO_WriteBit(PIOS_USART_INVERTER_GPIO,
|
|
|
|
PIOS_USART_INVERTER_PIN,
|
|
|
|
(*(enum PIOS_USART_Inverted *)param & PIOS_USART_Inverted_Rx) ? PIOS_USART_INVERTER_ENABLE : PIOS_USART_INVERTER_DISABLE);
|
|
|
|
|
|
|
|
break;
|
|
|
|
#endif /* PIOS_USART_INVERTER_PORT */
|
|
|
|
case PIOS_IOCTL_USART_GET_DSMBIND:
|
|
|
|
#ifdef PIOS_USART_INVERTER_PORT
|
|
|
|
if (usart_dev->cfg->regs == PIOS_USART_INVERTER_PORT) {
|
|
|
|
return -2; /* do not allow dsm bind on port with inverter */
|
|
|
|
}
|
|
|
|
#endif /* otherwise, return RXGPIO */
|
2017-04-19 15:58:31 +02:00
|
|
|
case PIOS_IOCTL_USART_GET_RXGPIO:
|
|
|
|
*(struct stm32_gpio *)param = usart_dev->cfg->rx;
|
|
|
|
break;
|
|
|
|
case PIOS_IOCTL_USART_GET_TXGPIO:
|
|
|
|
*(struct stm32_gpio *)param = usart_dev->cfg->tx;
|
|
|
|
break;
|
2017-04-17 00:50:08 +02:00
|
|
|
case PIOS_IOCTL_USART_SET_HALFDUPLEX:
|
|
|
|
USART_HalfDuplexCmd(usart_dev->cfg->regs, *(bool *)param ? ENABLE : DISABLE);
|
|
|
|
break;
|
2017-05-30 14:50:46 +02:00
|
|
|
case PIOS_IOCTL_USART_LOCK_CONFIG:
|
|
|
|
usart_dev->config_locked = *(bool *)param;
|
|
|
|
break;
|
2017-04-19 15:58:31 +02:00
|
|
|
default:
|
2017-04-25 13:11:37 +02:00
|
|
|
return COM_IOCTL_ENOSYS; /* unknown ioctl */
|
2017-04-19 15:57:34 +02:00
|
|
|
}
|
2017-04-19 15:58:31 +02:00
|
|
|
|
2017-04-19 15:57:34 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-15 19:25:30 +01:00
|
|
|
#endif /* PIOS_INCLUDE_USART */
|
2010-09-27 09:28:45 +02:00
|
|
|
|
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|