From 771a56ca9da8ad194fe7f443532ad895bce1b2ff Mon Sep 17 00:00:00 2001 From: stac Date: Sun, 13 Jun 2010 04:24:26 +0000 Subject: [PATCH] com/usart: generalize com driver API - Created a pluggable COM layer - Converted COM + USART init into static initializers rather than typedefs - Generalized the USB HID COM API to match the USART API. - Changed USART and COM layers to be data driven rather than #ifdef'ing/switching on the specifics of each port git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@760 ebee16cc-31ac-478f-84a7-5cbb03baadba --- flight/AHRS/ahrs.c | 6 +- flight/AHRS/inc/pios_board.h | 19 +- flight/AHRS/pios_board.c | 90 ++- flight/OpenPilot/Modules/GPS/GPS.c | 4 +- .../OpenPilot/Modules/Telemetry/telemetry.c | 10 +- flight/OpenPilot/System/inc/pios_board.h | 60 +- flight/OpenPilot/System/openpilot.c | 2 +- flight/OpenPilot/System/pios_board.c | 204 ++++++ flight/PiOS/Common/pios_com.c | 271 +++---- flight/PiOS/Common/printf-stdarg.c | 7 +- flight/PiOS/STM32F10x/pios_debug.c | 5 +- flight/PiOS/STM32F10x/pios_usart.c | 680 ++++++++---------- flight/PiOS/STM32F10x/pios_usb_hid.c | 18 +- flight/PiOS/inc/pios_com.h | 40 +- flight/PiOS/inc/pios_com_priv.h | 41 ++ flight/PiOS/inc/pios_usart.h | 26 +- flight/PiOS/inc/pios_usart_priv.h | 58 ++ flight/PiOS/inc/pios_usb_hid.h | 10 +- 18 files changed, 870 insertions(+), 681 deletions(-) create mode 100644 flight/PiOS/inc/pios_com_priv.h create mode 100644 flight/PiOS/inc/pios_usart_priv.h diff --git a/flight/AHRS/ahrs.c b/flight/AHRS/ahrs.c index 270fd0e2d..aa4572079 100644 --- a/flight/AHRS/ahrs.c +++ b/flight/AHRS/ahrs.c @@ -67,7 +67,7 @@ int main() HMC5843_InitStructure.Mode = PIOS_HMC5843_MODE_CONTINUOS; PIOS_HMC5843_Config(&HMC5843_InitStructure); - uint8_t id[3] = {0}; + uint8_t id[4] = {0}; int16_t data[3] = {0}; int32_t heading = 0; @@ -90,10 +90,10 @@ int main() if(heading < 0) heading += 360; // Output Heading data to - PIOS_COM_SendFormattedString(COM_USART1, "HMC5843 Chip ID: %s\rHeading: %d\rRaw Mag Values: X=%d Y=%d Y=%d\r", id, heading, data[0], data[1], data[2]); + PIOS_COM_SendFormattedString(PIOS_COM_AUX, "HMC5843 Chip ID: %s Heading: %d Raw Mag Values: X=%d Y=%d Z=%d ", id, heading, data[0], data[1], data[2]); // Test ADC - PIOS_COM_SendFormattedString(COM_USART1, "ADC Values: %d,%d,%d,%d,%d,%d\r\n", PIOS_ADC_PinGet(0), PIOS_ADC_PinGet(1), PIOS_ADC_PinGet(2), PIOS_ADC_PinGet(3), PIOS_ADC_PinGet(4), PIOS_ADC_PinGet(5)); + PIOS_COM_SendFormattedString(PIOS_COM_AUX, "ADC Values: %d,%d,%d,%d,%d,%d\r\n", PIOS_ADC_PinGet(0), PIOS_ADC_PinGet(1), PIOS_ADC_PinGet(2), PIOS_ADC_PinGet(3), PIOS_ADC_PinGet(4), PIOS_ADC_PinGet(5)); // Delay until next reading PIOS_DELAY_WaitmS(500); diff --git a/flight/AHRS/inc/pios_board.h b/flight/AHRS/inc/pios_board.h index 59a707e61..b05ca443d 100644 --- a/flight/AHRS/inc/pios_board.h +++ b/flight/AHRS/inc/pios_board.h @@ -122,28 +122,13 @@ TIM8 | | | | //------------------------- #define PIOS_OP_SPI 0 -//------------------------- -// PIOS_USART1 -//------------------------- -#define PIOS_USART1_ENABLED 1 -#define PIOS_USART1_USART USART3 -#define PIOS_USART1_GPIO_PORT GPIOB -#define PIOS_USART1_RX_PIN GPIO_Pin_11 -#define PIOS_USART1_TX_PIN GPIO_Pin_10 -#define PIOS_USART1_REMAP_FUNC { } -#define PIOS_USART1_IRQ_CHANNEL USART3_IRQn -#define PIOS_USART1_IRQHANDLER_FUNC void USART3_IRQHandler(void) -#define PIOS_USART1_CLK_FUNC RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE) -#define PIOS_USART1_NVIC_PRIO PIOS_IRQ_PRIO_HIGH -#define PIOS_USART1_BAUDRATE 57600 - //------------------------- // PIOS_USART //------------------------- -#define PIOS_USART_NUM 1 #define PIOS_USART_RX_BUFFER_SIZE 256 #define PIOS_USART_TX_BUFFER_SIZE 256 -#define PIOS_COM_DEBUG_PORT USART_1 +#define PIOS_COM_AUX 0 +#define PIOS_COM_DEBUG PIOS_COM_AUX #if 0 //------------------------- diff --git a/flight/AHRS/pios_board.c b/flight/AHRS/pios_board.c index c7a952a6a..99c1ca7c1 100644 --- a/flight/AHRS/pios_board.c +++ b/flight/AHRS/pios_board.c @@ -141,4 +141,92 @@ void PIOS_SPI_op_irq_handler(void) PIOS_SPI_IRQ_Handler(PIOS_OP_SPI); } -#endif +#endif /* PIOS_INCLUDE_SPI */ + +#if defined(PIOS_INCLUDE_USART) +#include + +/* + * AUX USART + */ +void PIOS_USART_aux_irq_handler(void); +void USART3_IRQHandler() __attribute__ ((alias ("PIOS_USART_aux_irq_handler"))); +const struct pios_usart_cfg pios_usart_aux_cfg = { + .regs = USART3, + .init = { + .USART_BaudRate = 57600, + .USART_WordLength = USART_WordLength_8b, + .USART_Parity = USART_Parity_No, + .USART_StopBits = USART_StopBits_1, + .USART_HardwareFlowControl = USART_HardwareFlowControl_None, + .USART_Mode = USART_Mode_Rx | USART_Mode_Tx, + }, + .irq = { + .handler = PIOS_USART_aux_irq_handler, + .init = { + .NVIC_IRQChannel = USART3_IRQn, + .NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_HIGH, + .NVIC_IRQChannelSubPriority = 0, + .NVIC_IRQChannelCmd = ENABLE, + }, + }, + .rx = { + .gpio = GPIOB, + .init = { + .GPIO_Pin = GPIO_Pin_11, + .GPIO_Speed = GPIO_Speed_2MHz, + .GPIO_Mode = GPIO_Mode_IPU, + }, + }, + .tx = { + .gpio = GPIOB, + .init = { + .GPIO_Pin = GPIO_Pin_10, + .GPIO_Speed = GPIO_Speed_2MHz, + .GPIO_Mode = GPIO_Mode_AF_PP, + }, + }, +}; + +/* + * Board specific number of devices. + */ +struct pios_usart_dev pios_usart_devs[] = { +#define PIOS_USART_AUX 0 + { + .cfg = &pios_usart_aux_cfg, + }, +}; + +uint8_t pios_usart_num_devices = NELEMENTS(pios_usart_devs); + +void PIOS_USART_aux_irq_handler(void) +{ + PIOS_USART_IRQ_Handler(PIOS_USART_AUX); +} + +#endif /* PIOS_INCLUDE_USART */ + +#if defined(PIOS_INCLUDE_COM) +#include + +/* + * COM devices + */ + +/* + * Board specific number of devices. + */ +extern const struct pios_com_driver pios_usart_com_driver; + +struct pios_com_dev pios_com_devs[] = { + { + .id = PIOS_USART_AUX, + .driver = &pios_usart_com_driver, + }, +}; + +uint8_t pios_com_num_devices = NELEMENTS(pios_com_devs); + +#endif /* PIOS_INCLUDE_COM */ + diff --git a/flight/OpenPilot/Modules/GPS/GPS.c b/flight/OpenPilot/Modules/GPS/GPS.c index b9b6d2a59..beb7a9ba6 100644 --- a/flight/OpenPilot/Modules/GPS/GPS.c +++ b/flight/OpenPilot/Modules/GPS/GPS.c @@ -76,7 +76,7 @@ void nmeaProcessGPGSA(char* packet); // Private types // Private variables -static COMPortTypeDef gpsPort; +static uint8_t gpsPort; static xTaskHandle gpsTaskHandle; cBuffer gpsRxBuffer; static char gpsRxData[512]; @@ -100,7 +100,7 @@ int32_t GPSInitialize(void) timeOfLastUpdateMs = 0; // TODO: Get gps settings object - gpsPort = COM_USART2; + gpsPort = PIOS_COM_GPS; // Init input buffer size 512 bufferInit(&gpsRxBuffer, (unsigned char *)gpsRxData, 512); diff --git a/flight/OpenPilot/Modules/Telemetry/telemetry.c b/flight/OpenPilot/Modules/Telemetry/telemetry.c index 5cabfc1bd..36d60bf96 100644 --- a/flight/OpenPilot/Modules/Telemetry/telemetry.c +++ b/flight/OpenPilot/Modules/Telemetry/telemetry.c @@ -45,7 +45,7 @@ // Private types // Private variables -static COMPortTypeDef telemetryPort; +static uint8_t telemetryPort; static xQueueHandle queue; static xQueueHandle priorityQueue; static xTaskHandle telemetryTxTaskHandle; @@ -300,7 +300,7 @@ static void telemetryTxPriTask(void* parameters) */ static void telemetryRxTask(void* parameters) { - COMPortTypeDef inputPort; + uint8_t inputPort; int32_t len; // Task loop @@ -310,7 +310,7 @@ static void telemetryRxTask(void* parameters) // Determine input port (USB takes priority over telemetry port) if(PIOS_USB_HID_CheckAvailable()) { - inputPort = COM_USB_HID; + inputPort = PIOS_COM_TELEM_USB; } else #endif /* ALLOW_HID_TELEMETRY */ @@ -339,7 +339,7 @@ static void telemetryRxTask(void* parameters) */ static int32_t transmitData(uint8_t* data, int32_t length) { - COMPortTypeDef outputPort; + uint8_t outputPort; // Determine input port (USB takes priority over telemetry port) #if ALLOW_HID_TELEMETRY @@ -533,7 +533,7 @@ static void updateTelemetryStats() static void updateSettings() { // Set port - telemetryPort = COM_USART1; + telemetryPort = PIOS_COM_TELEM_RF; // Retrieve settings TelemetrySettingsGet(&settings); // Set port speed diff --git a/flight/OpenPilot/System/inc/pios_board.h b/flight/OpenPilot/System/inc/pios_board.h index 13e0640d7..e771c3f17 100644 --- a/flight/OpenPilot/System/inc/pios_board.h +++ b/flight/OpenPilot/System/inc/pios_board.h @@ -108,57 +108,21 @@ TIM8 | Servo 5 | Servo 6 | Servo 7 | Servo 8 #define PIOS_BMP085_OVERSAMPLING 2 //------------------------- -// PIOS_USART1 (TELEM) +// USART +// +// See also pios_board.c //------------------------- -#define PIOS_USART1_ENABLED 1 -#define PIOS_USART1_USART USART2 -#define PIOS_USART1_GPIO_PORT GPIOA -#define PIOS_USART1_RX_PIN GPIO_Pin_3 -#define PIOS_USART1_TX_PIN GPIO_Pin_2 -#define PIOS_USART1_REMAP_FUNC { } -#define PIOS_USART1_IRQ_CHANNEL USART2_IRQn -#define PIOS_USART1_IRQHANDLER_FUNC void USART2_IRQHandler(void) -#define PIOS_USART1_CLK_FUNC RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE) -#define PIOS_USART1_NVIC_PRIO PIOS_IRQ_PRIO_HIGHEST -#define PIOS_USART1_BAUDRATE 57600 - -//------------------------- -// PIOS_USART2 (GPS) -//------------------------- -#define PIOS_USART2_ENABLED 1 -#define PIOS_USART2_USART USART3 -#define PIOS_USART2_GPIO_PORT GPIOC -#define PIOS_USART2_RX_PIN GPIO_Pin_11 -#define PIOS_USART2_TX_PIN GPIO_Pin_10 -#define PIOS_USART2_REMAP_FUNC { GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE); } -#define PIOS_USART2_IRQ_CHANNEL USART3_IRQn -#define PIOS_USART2_IRQHANDLER_FUNC void USART3_IRQHandler(void) -#define PIOS_USART2_CLK_FUNC RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE) -#define PIOS_USART2_NVIC_PRIO PIOS_IRQ_PRIO_HIGH -#define PIOS_USART2_BAUDRATE 57600 - -//------------------------- -// PIOS_USART3 (AUX) (RX5/RX6) -//------------------------- -#define PIOS_USART3_ENABLED 0 -#define PIOS_USART3_USART USART1 -#define PIOS_USART3_GPIO_PORT GPIOA -#define PIOS_USART3_RX_PIN GPIO_Pin_10 -#define PIOS_USART3_TX_PIN GPIO_Pin_9 -#define PIOS_USART3_REMAP_FUNC { } -#define PIOS_USART3_IRQ_CHANNEL USART1_IRQn -#define PIOS_USART3_IRQHANDLER_FUNC void USART1_IRQHandler(void) -#define PIOS_USART3_CLK_FUNC RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE) -#define PIOS_USART3_NVIC_PRIO PIOS_IRQ_PRIO_HIGH -#define PIOS_USART3_BAUDRATE 57600 - -//------------------------- -// PIOS_USART -//------------------------- -#define PIOS_USART_NUM 3 #define PIOS_USART_RX_BUFFER_SIZE 1024 #define PIOS_USART_TX_BUFFER_SIZE 256 -#define PIOS_COM_DEBUG_PORT USART_1 + +#define PIOS_COM_TELEM_RF 0 +#define PIOS_COM_GPS 1 +#define PIOS_COM_TELEM_USB 2 + +#if 0 +#define PIOS_COM_AUX 3 +#define PIOS_COM_DEBUG PIOS_COM_AUX +#endif //------------------------- // SPI diff --git a/flight/OpenPilot/System/openpilot.c b/flight/OpenPilot/System/openpilot.c index 818aac7f5..7f709202a 100644 --- a/flight/OpenPilot/System/openpilot.c +++ b/flight/OpenPilot/System/openpilot.c @@ -53,7 +53,7 @@ static void TaskHIDTest(void *pvParameters); static void TaskServos(void *pvParameters); static void TaskSDCard(void *pvParameters); #endif -int32_t CONSOLE_Parse(COMPortTypeDef port, char c); +int32_t CONSOLE_Parse(uint8_t port, char c); void OP_ADC_NotifyChange(uint32_t pin, uint32_t pin_value); /* Prototype of generated InitModules() function */ diff --git a/flight/OpenPilot/System/pios_board.c b/flight/OpenPilot/System/pios_board.c index ab8b1265f..9257c7361 100644 --- a/flight/OpenPilot/System/pios_board.c +++ b/flight/OpenPilot/System/pios_board.c @@ -25,6 +25,8 @@ #include #include +#include +#include /* MicroSD Interface * @@ -243,3 +245,205 @@ void PIOS_SPI_ahrs_irq_handler(void) /* Call into the generic code to handle the IRQ for this specific device */ PIOS_SPI_IRQ_Handler(PIOS_OPAHRS_SPI); } + +/* + * Telemetry USART + */ +void PIOS_USART_telem_irq_handler(void); +void USART2_IRQHandler() __attribute__ ((alias ("PIOS_USART_telem_irq_handler"))); +const struct pios_usart_cfg pios_usart_telem_cfg = { + .regs = USART2, + .init = { + .USART_BaudRate = 57600, + .USART_WordLength = USART_WordLength_8b, + .USART_Parity = USART_Parity_No, + .USART_StopBits = USART_StopBits_1, + .USART_HardwareFlowControl = USART_HardwareFlowControl_None, + .USART_Mode = USART_Mode_Rx | USART_Mode_Tx, + }, + .irq = { + .handler = PIOS_USART_telem_irq_handler, + .init = { + .NVIC_IRQChannel = USART2_IRQn, + .NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_HIGHEST, + .NVIC_IRQChannelSubPriority = 0, + .NVIC_IRQChannelCmd = ENABLE, + }, + }, + .rx = { + .gpio = GPIOA, + .init = { + .GPIO_Pin = GPIO_Pin_3, + .GPIO_Speed = GPIO_Speed_2MHz, + .GPIO_Mode = GPIO_Mode_IPU, + }, + }, + .tx = { + .gpio = GPIOA, + .init = { + .GPIO_Pin = GPIO_Pin_2, + .GPIO_Speed = GPIO_Speed_2MHz, + .GPIO_Mode = GPIO_Mode_AF_PP, + }, + }, +}; + +/* + * GPS USART + */ +void PIOS_USART_gps_irq_handler(void); +void USART3_IRQHandler() __attribute__ ((alias ("PIOS_USART_gps_irq_handler"))); +const struct pios_usart_cfg pios_usart_gps_cfg = { + .regs = USART3, + .remap = GPIO_PartialRemap_USART3, + .init = { + .USART_BaudRate = 57600, + .USART_WordLength = USART_WordLength_8b, + .USART_Parity = USART_Parity_No, + .USART_StopBits = USART_StopBits_1, + .USART_HardwareFlowControl = USART_HardwareFlowControl_None, + .USART_Mode = USART_Mode_Rx | USART_Mode_Tx, + }, + .irq = { + .handler = PIOS_USART_gps_irq_handler, + .init = { + .NVIC_IRQChannel = USART3_IRQn, + .NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_HIGH, + .NVIC_IRQChannelSubPriority = 0, + .NVIC_IRQChannelCmd = ENABLE, + }, + }, + .rx = { + .gpio = GPIOC, + .init = { + .GPIO_Pin = GPIO_Pin_11, + .GPIO_Speed = GPIO_Speed_2MHz, + .GPIO_Mode = GPIO_Mode_IPU, + }, + }, + .tx = { + .gpio = GPIOC, + .init = { + .GPIO_Pin = GPIO_Pin_10, + .GPIO_Speed = GPIO_Speed_2MHz, + .GPIO_Mode = GPIO_Mode_AF_PP, + }, + }, +}; + +#ifdef PIOS_COM_AUX +/* + * AUX USART + */ +void PIOS_USART_aux_irq_handler(void); +void USART1_IRQHandler() __attribute__ ((alias ("PIOS_USART_aux_irq_handler"))); +const struct pios_usart_cfg pios_usart_aux_cfg = { + .regs = USART1, + .init = { + .USART_BaudRate = 57600, + .USART_WordLength = USART_WordLength_8b, + .USART_Parity = USART_Parity_No, + .USART_StopBits = USART_StopBits_1, + .USART_HardwareFlowControl = USART_HardwareFlowControl_None, + .USART_Mode = USART_Mode_Rx | USART_Mode_Tx, + }, + .irq = { + .handler = PIOS_USART_aux_irq_handler, + .init = { + .NVIC_IRQChannel = USART1_IRQn, + .NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_HIGH, + .NVIC_IRQChannelSubPriority = 0, + .NVIC_IRQChannelCmd = ENABLE, + }, + }, + .rx = { + .gpio = GPIOA, + .init = { + .GPIO_Pin = GPIO_Pin_10, + .GPIO_Speed = GPIO_Speed_2MHz, + .GPIO_Mode = GPIO_Mode_IPU, + }, + }, + .tx = { + .gpio = GPIOA, + .init = { + .GPIO_Pin = GPIO_Pin_9, + .GPIO_Speed = GPIO_Speed_2MHz, + .GPIO_Mode = GPIO_Mode_AF_PP, + }, + }, +}; +#endif + +/* + * Board specific number of devices. + */ +struct pios_usart_dev pios_usart_devs[] = { +#define PIOS_USART_TELEM 0 + { + .cfg = &pios_usart_telem_cfg, + }, +#define PIOS_USART_GPS 1 + { + .cfg = &pios_usart_gps_cfg, + }, +#ifdef PIOS_COM_AUX +#define PIOS_USART_AUX 2 + { + .cfg = &pios_usart_aux_cfg, + }, +#endif +}; + +uint8_t pios_usart_num_devices = NELEMENTS(pios_usart_devs); + +void PIOS_USART_telem_irq_handler(void) +{ + PIOS_USART_IRQ_Handler(PIOS_USART_TELEM); +} + +void PIOS_USART_gps_irq_handler(void) +{ + PIOS_USART_IRQ_Handler(PIOS_USART_GPS); +} + +#ifdef PIOS_COM_AUX +void PIOS_USART_aux_irq_handler(void) +{ + PIOS_USART_IRQ_Handler(PIOS_USART_AUX); +} +#endif + +/* + * COM devices + */ + +/* + * Board specific number of devices. + */ +extern const struct pios_com_driver pios_usart_com_driver; +extern const struct pios_com_driver pios_usb_com_driver; + +struct pios_com_dev pios_com_devs[] = { + { + .id = PIOS_USART_TELEM, + .driver = &pios_usart_com_driver, + }, + { + .id = PIOS_USART_GPS, + .driver = &pios_usart_com_driver, + }, + { + .id = 0, + .driver = &pios_usb_com_driver, + }, +#ifdef PIOS_COM_AUX + { + .id = PIOS_USART_AUX, + .driver = &pios_usart_com_driver, + }, +#endif +}; + +uint8_t pios_com_num_devices = NELEMENTS(pios_com_devs); + diff --git a/flight/PiOS/Common/pios_com.c b/flight/PiOS/Common/pios_com.c index 5dfacfc01..308af08ce 100644 --- a/flight/PiOS/Common/pios_com.c +++ b/flight/PiOS/Common/pios_com.c @@ -28,17 +28,23 @@ /* Project Includes */ -#include "pios.h" - -#if defined(PIOS_INCLUDE_COM) - - -/* Global Variables */ - -/* Local Variables */ -static int32_t (*receive_callback_func)(COMPortTypeDef port, char c); - - +#include "pios.h" + +#if defined(PIOS_INCLUDE_COM) + +#include + +static struct pios_com_dev * find_com_dev_by_id (uint8_t port) +{ + if (port >= pios_com_num_devices) { + /* Undefined COM port for this board (see pios_board.c) */ + return NULL; + } + + /* Get a handle for the device configuration */ + return &(pios_com_devs[port]); +} + /** * Initialises COM layer * \param[in] mode currently only mode 0 supported @@ -48,9 +54,6 @@ int32_t PIOS_COM_Init(void) { int32_t ret = 0; - /* Disable callback by default */ - receive_callback_func = NULL; - /* If any COM assignment: */ #if defined(PIOS_INCLUDE_USART) PIOS_USART_Init(); @@ -70,31 +73,24 @@ int32_t PIOS_COM_Init(void) * \return -1 if port not available * \return 0 on success */ -int32_t PIOS_COM_ChangeBaud(COMPortTypeDef port, uint32_t baud) -{ - /* Branch depending on selected port */ - switch(port) { -#if defined(PIOS_INCLUDE_USART) - case COM_DEBUG_USART: - PIOS_USART_ChangeBaud(PIOS_COM_DEBUG_PORT, baud); - return 0; - case COM_USART1: - PIOS_USART_ChangeBaud(USART_1, baud); - return 0; - case COM_USART2: - PIOS_USART_ChangeBaud(USART_2, baud); - return 0; - case COM_USART3: - PIOS_USART_ChangeBaud(USART_3, baud); - return 0; -#endif - case COM_USB_HID: - return 0; - default: - /* Invalid port */ - return -1; - } -} +int32_t PIOS_COM_ChangeBaud(uint8_t port, uint32_t baud) +{ + struct pios_com_dev * com_dev; + + com_dev = find_com_dev_by_id (port); + + if (!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_baud) { + com_dev->driver->set_baud(com_dev->id, baud); + } + + return 0; +} /** * Sends a package over given port @@ -106,37 +102,25 @@ int32_t PIOS_COM_ChangeBaud(COMPortTypeDef port, uint32_t baud) * caller should retry until buffer is free again * \return 0 on success */ -int32_t PIOS_COM_SendBufferNonBlocking(COMPortTypeDef port, uint8_t *buffer, uint16_t len) -{ - /* Branch depending on selected port */ - switch(port) { -#if defined(PIOS_INCLUDE_USART) - case COM_DEBUG_USART: - return PIOS_USART_TxBufferPutMoreNonBlocking(PIOS_COM_DEBUG_PORT, buffer, len); -#endif -#if PIOS_USART1_ENABLED - case COM_USART1: - return PIOS_USART_TxBufferPutMoreNonBlocking(USART_1, buffer, len); -#endif -#if PIOS_USART2_ENABLED - case COM_USART2: - return PIOS_USART_TxBufferPutMoreNonBlocking(USART_2, buffer, len); -#endif -#if PIOS_USART3_ENABLED - case COM_USART3: - return PIOS_USART_TxBufferPutMoreNonBlocking(USART_3, buffer, len); -#endif - -#if defined(PIOS_INCLUDE_USB_HID) - case COM_USB_HID: - return PIOS_USB_HID_TxBufferPutMoreNonBlocking(buffer, len); -#endif - default: - /* Invalid port */ - return -1; - } -} - +int32_t PIOS_COM_SendBufferNonBlocking(uint8_t port, uint8_t *buffer, uint16_t len) +{ + struct pios_com_dev * com_dev; + + com_dev = find_com_dev_by_id (port); + + if (!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->tx_nb) { + return com_dev->driver->tx_nb(com_dev->id, buffer, len); + } + + return 0; +} + /** * Sends a package over given port * (blocking function) @@ -146,37 +130,25 @@ int32_t PIOS_COM_SendBufferNonBlocking(COMPortTypeDef port, uint8_t *buffer, uin * \return -1 if port not available * \return 0 on success */ -int32_t PIOS_COM_SendBuffer(COMPortTypeDef port, uint8_t *buffer, uint16_t len) -{ - /* Branch depending on selected port */ - switch(port) { -#if defined(PIOS_INCLUDE_USART) - case COM_DEBUG_USART: - return PIOS_USART_TxBufferPutMore(PIOS_COM_DEBUG_PORT, buffer, len); -#endif -#if PIOS_USART1_ENABLED - case COM_USART1: - return PIOS_USART_TxBufferPutMore(USART_1, buffer, len); -#endif -#if PIOS_USART2_ENABLED - case COM_USART2: - return PIOS_USART_TxBufferPutMore(USART_2, buffer, len); -#endif -#if PIOS_USART3_ENABLED - case COM_USART3: - return PIOS_USART_TxBufferPutMore(USART_3, buffer, len); -#endif - -#if defined(PIOS_INCLUDE_USB_HID) - case COM_USB_HID: - return PIOS_USB_HID_TxBufferPutMore(buffer, len); -#endif - default: - /* Invalid port */ - return -1; - } -} - +int32_t PIOS_COM_SendBuffer(uint8_t port, uint8_t *buffer, uint16_t len) +{ + struct pios_com_dev * com_dev; + + com_dev = find_com_dev_by_id (port); + + if (!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->tx) { + return com_dev->driver->tx(com_dev->id, buffer, len); + } + + return 0; +} + /** * Sends a single character over given port * \param[in] port COM port @@ -186,7 +158,7 @@ int32_t PIOS_COM_SendBuffer(COMPortTypeDef port, uint8_t *buffer, uint16_t len) * caller should retry until buffer is free again * \return 0 on success */ -int32_t PIOS_COM_SendCharNonBlocking(COMPortTypeDef port, char c) +int32_t PIOS_COM_SendCharNonBlocking(uint8_t port, char c) { return PIOS_COM_SendBufferNonBlocking(port, (uint8_t *)&c, 1); } @@ -199,7 +171,7 @@ int32_t PIOS_COM_SendCharNonBlocking(COMPortTypeDef port, char c) * \return -1 if port not available * \return 0 on success */ -int32_t PIOS_COM_SendChar(COMPortTypeDef port, char c) +int32_t PIOS_COM_SendChar(uint8_t port, char c) { return PIOS_COM_SendBuffer(port, (uint8_t *)&c, 1); } @@ -213,7 +185,7 @@ int32_t PIOS_COM_SendChar(COMPortTypeDef port, char c) * caller should retry until buffer is free again * \return 0 on success */ -int32_t PIOS_COM_SendStringNonBlocking(COMPortTypeDef port, char *str) +int32_t PIOS_COM_SendStringNonBlocking(uint8_t port, char *str) { return PIOS_COM_SendBufferNonBlocking(port, (uint8_t *)str, (uint16_t)strlen(str)); } @@ -226,7 +198,7 @@ int32_t PIOS_COM_SendStringNonBlocking(COMPortTypeDef port, char *str) * \return -1 if port not available * \return 0 on success */ -int32_t PIOS_COM_SendString(COMPortTypeDef port, char *str) +int32_t PIOS_COM_SendString(uint8_t port, char *str) { return PIOS_COM_SendBuffer(port, (uint8_t *)str, strlen(str)); } @@ -241,7 +213,7 @@ int32_t PIOS_COM_SendString(COMPortTypeDef port, char *str) * caller should retry until buffer is free again * \return 0 on success */ -int32_t PIOS_COM_SendFormattedStringNonBlocking(COMPortTypeDef port, char *format, ...) +int32_t PIOS_COM_SendFormattedStringNonBlocking(uint8_t port, char *format, ...) { uint8_t buffer[128]; // TODO: tmp!!! Provide a streamed COM method later! @@ -261,7 +233,7 @@ int32_t PIOS_COM_SendFormattedStringNonBlocking(COMPortTypeDef port, char *forma * \return -1 if port not available * \return 0 on success */ -int32_t PIOS_COM_SendFormattedString(COMPortTypeDef port, char *format, ...) +int32_t PIOS_COM_SendFormattedString(uint8_t port, char *format, ...) { uint8_t buffer[128]; // TODO: tmp!!! Provide a streamed COM method later! va_list args; @@ -276,69 +248,38 @@ int32_t PIOS_COM_SendFormattedString(COMPortTypeDef port, char *format, ...) * \param[in] port COM port * \returns Byte from buffer */ -uint8_t PIOS_COM_ReceiveBuffer(COMPortTypeDef port) +uint8_t PIOS_COM_ReceiveBuffer(uint8_t port) { - switch(port) { -#if defined(PIOS_INCLUDE_USART) - case COM_DEBUG_USART: - return PIOS_USART_RxBufferGet(PIOS_COM_DEBUG_PORT); -#endif -#if PIOS_USART1_ENABLED - case COM_USART1: - return PIOS_USART_RxBufferGet(USART_1); -#endif -#if PIOS_USART2_ENABLED - case COM_USART2: - return PIOS_USART_RxBufferGet(USART_2); -#endif -#if PIOS_USART3_ENABLED - case COM_USART3: - return PIOS_USART_RxBufferGet(USART_3); -#endif - -#if defined(PIOS_INCLUDE_USB_HID) - case COM_USB_HID: - return PIOS_USB_HID_RxBufferGet(); -#endif - /* To suppress warnings */ - default: - return 0; - } -} - + struct pios_com_dev * com_dev; + + com_dev = find_com_dev_by_id (port); + PIOS_DEBUG_Assert(com_dev); + PIOS_DEBUG_Assert(com_dev->driver->rx); + + return com_dev->driver->rx(com_dev->id); +} + /** * Get the number of bytes waiting in the buffer * \param[in] port COM port * \return Number of bytes used in buffer */ -int32_t PIOS_COM_ReceiveBufferUsed(COMPortTypeDef port) -{ - switch(port) { - -#if defined(PIOS_INCLUDE_USART) - case COM_DEBUG_USART: - return PIOS_USART_RxBufferUsed(PIOS_COM_DEBUG_PORT); -#if PIOS_USART1_ENABLED - case COM_USART1: - return PIOS_USART_RxBufferUsed(USART_1); -#endif -#if PIOS_USART2_ENABLED - case COM_USART2: - return PIOS_USART_RxBufferUsed(USART_2); -#endif -#if PIOS_USART3_ENABLED - case COM_USART3: - return PIOS_USART_RxBufferUsed(USART_3); -#endif -#endif -#if defined(PIOS_INCLUDE_USB_HID) - case COM_USB_HID: - return PIOS_USB_HID_RxBufferUsed(); -#endif - /* To suppress warnings */ - default: - return 0; - } -} +int32_t PIOS_COM_ReceiveBufferUsed(uint8_t port) +{ + struct pios_com_dev * com_dev; + + com_dev = find_com_dev_by_id (port); + + if (!com_dev) { + /* Undefined COM port for this board (see pios_board.c) */ + return 0; + } + + if (!com_dev->driver->rx_avail) { + return 0; + } + + return com_dev->driver->rx_avail(com_dev->id); +} #endif diff --git a/flight/PiOS/Common/printf-stdarg.c b/flight/PiOS/Common/printf-stdarg.c index 9de19a054..e89d87cb5 100644 --- a/flight/PiOS/Common/printf-stdarg.c +++ b/flight/PiOS/Common/printf-stdarg.c @@ -39,14 +39,13 @@ static void printchar(char **str, int c) { - // extern int putchar(int c); - if (str) { **str = c; ++(*str); } - - else PIOS_COM_SendChar(COM_DEBUG_USART, c); // (void)putchar(c); +#ifdef PIOS_COM_DEBUG + else PIOS_COM_SendChar(PIOS_COM_DEBUG, c); +#endif } diff --git a/flight/PiOS/STM32F10x/pios_debug.c b/flight/PiOS/STM32F10x/pios_debug.c index ecc93fad8..df4705b0d 100644 --- a/flight/PiOS/STM32F10x/pios_debug.c +++ b/flight/PiOS/STM32F10x/pios_debug.c @@ -91,9 +91,10 @@ void PIOS_DEBUG_PinLow(uint8_t Pin) */ void PIOS_DEBUG_Panic(const char* msg) { +#ifdef PIOS_COM_DEBUG register int *lr asm ("lr"); // Link-register holds the PC of the caller - - PIOS_COM_SendFormattedStringNonBlocking(COM_DEBUG_USART, "\r%s @0x%x\r", msg, lr); + PIOS_COM_SendFormattedStringNonBlocking(PIOS_COM_DEBUG, "\r%s @0x%x\r", msg, lr); +#endif // Stay put while(1); diff --git a/flight/PiOS/STM32F10x/pios_usart.c b/flight/PiOS/STM32F10x/pios_usart.c index b77566ee7..2b10c2328 100644 --- a/flight/PiOS/STM32F10x/pios_usart.c +++ b/flight/PiOS/STM32F10x/pios_usart.c @@ -32,151 +32,78 @@ #if defined(PIOS_INCLUDE_USART) +#include -/* Global Variables */ +/* Provide a COM driver */ +const struct pios_com_driver pios_usart_com_driver = { + .set_baud = PIOS_USART_ChangeBaud, + .tx_nb = PIOS_USART_TxBufferPutMoreNonBlocking, + .tx = PIOS_USART_TxBufferPutMore, + .rx = PIOS_USART_RxBufferGet, + .rx_avail = PIOS_USART_RxBufferUsed, +}; +static struct pios_usart_dev * find_usart_dev_by_id (uint8_t usart) +{ + if (usart >= pios_usart_num_devices) { + /* Undefined USART port for this board (see pios_board.c) */ + return NULL; + } -/* Local Variables */ -static uint8_t rx_buffer[PIOS_USART_NUM][PIOS_USART_RX_BUFFER_SIZE]; -static volatile uint8_t rx_buffer_tail[PIOS_USART_NUM]; -static volatile uint8_t rx_buffer_head[PIOS_USART_NUM]; -static volatile uint8_t rx_buffer_size[PIOS_USART_NUM]; - -static uint8_t tx_buffer[PIOS_USART_NUM][PIOS_USART_TX_BUFFER_SIZE]; -static volatile uint8_t tx_buffer_tail[PIOS_USART_NUM]; -static volatile uint8_t tx_buffer_head[PIOS_USART_NUM]; -static volatile uint8_t tx_buffer_size[PIOS_USART_NUM]; - + /* Get a handle for the device configuration */ + return &(pios_usart_devs[usart]); +} /** * Initialise the onboard USARTs */ void PIOS_USART_Init(void) { - /* Clear buffer counters */ - uint8_t i; - for(i = 0; i < PIOS_USART_NUM; ++i) { - rx_buffer_tail[i] = rx_buffer_head[i] = rx_buffer_size[i] = 0; - tx_buffer_tail[i] = tx_buffer_head[i] = tx_buffer_size[i] = 0; - } - - /* Configure USART Pins */ - GPIO_InitTypeDef GPIO_InitStructure; - GPIO_StructInit(&GPIO_InitStructure); - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; + struct pios_usart_dev * usart_dev; + uint8_t i; - /* Configure and Init USARTs */ - USART_InitTypeDef USART_InitStructure; - USART_InitStructure.USART_WordLength = USART_WordLength_8b; - USART_InitStructure.USART_StopBits = USART_StopBits_1; - USART_InitStructure.USART_Parity = USART_Parity_No; - USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; - USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; + for (i = 0; i < pios_usart_num_devices; i++) { + /* Get a handle for the device configuration */ + usart_dev = find_usart_dev_by_id(i); + PIOS_DEBUG_Assert(usart_dev); - /* Configure the USART Interrupts */ - NVIC_InitTypeDef NVIC_InitStructure; - NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; - NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; + /* Clear buffer counters */ + usart_dev->rx.head = usart_dev->rx.tail = usart_dev->rx.size = 0; + usart_dev->tx.head = usart_dev->tx.tail = usart_dev->tx.size = 0; -#if (PIOS_USART1_ENABLED) - /* Enable the USART Pins Software Remapping */ - PIOS_USART1_REMAP_FUNC; + /* Enable the USART Pins Software Remapping */ + if (usart_dev->cfg->remap) { + GPIO_PinRemapConfig(usart_dev->cfg->remap, ENABLE); + } - /* Configure and Init USART Tx as alternate function push-pull */ - GPIO_InitStructure.GPIO_Pin = PIOS_USART1_TX_PIN; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; - GPIO_Init(PIOS_USART1_GPIO_PORT, &GPIO_InitStructure); - - /* Configure and Init USART Rx input with internal pull-ups */ - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; - GPIO_InitStructure.GPIO_Pin = PIOS_USART1_RX_PIN; - GPIO_Init(PIOS_USART1_GPIO_PORT, &GPIO_InitStructure); + /* Initialize the USART Rx and Tx pins */ + GPIO_Init(usart_dev->cfg->rx.gpio, &usart_dev->cfg->rx.init); + GPIO_Init(usart_dev->cfg->tx.gpio, &usart_dev->cfg->tx.init); - /* Enable USART clock */ - PIOS_USART1_CLK_FUNC; + /* Enable USART clock */ + switch ((uint32_t)usart_dev->cfg->regs) { + case (uint32_t)USART1: + RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); + break; + case (uint32_t)USART2: + RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); + break; + case (uint32_t)USART3: + RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); + break; + } - /* Enable USART Receive and Transmit interrupts */ - USART_InitStructure.USART_BaudRate = PIOS_USART1_BAUDRATE; - USART_Init(PIOS_USART1_USART, &USART_InitStructure); - USART_ITConfig(PIOS_USART1_USART, USART_IT_RXNE, ENABLE); - USART_ITConfig(PIOS_USART1_USART, USART_IT_TXE, ENABLE); + /* Enable USART */ + USART_Init(usart_dev->cfg->regs, &usart_dev->cfg->init); - /* Configure the USART Interrupts */ - NVIC_InitStructure.NVIC_IRQChannel = PIOS_USART1_IRQ_CHANNEL; - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PIOS_USART1_NVIC_PRIO; - NVIC_Init(&NVIC_InitStructure); - USART_ITConfig(PIOS_USART1_USART, USART_IT_RXNE, ENABLE); - - /* Enable USART */ - USART_Cmd(PIOS_USART1_USART, ENABLE); -#endif - -#if (PIOS_USART2_ENABLED) - /* Enable the USART Pins Software Remapping */ - PIOS_USART2_REMAP_FUNC; - - /* Configure and Init USART Tx as alternate function push-pull */ - GPIO_InitStructure.GPIO_Pin = PIOS_USART2_TX_PIN; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; - GPIO_Init(PIOS_USART2_GPIO_PORT, &GPIO_InitStructure); - - /* Configure and Init USART Rx input with internal pull-ups */ - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; - GPIO_InitStructure.GPIO_Pin = PIOS_USART2_RX_PIN; - GPIO_Init(PIOS_USART2_GPIO_PORT, &GPIO_InitStructure); - - /* Enable USART clock */ - PIOS_USART2_CLK_FUNC; - - /* Enable USART Receive and Transmit interrupts */ - USART_InitStructure.USART_BaudRate = PIOS_USART2_BAUDRATE; - USART_Init(PIOS_USART2_USART, &USART_InitStructure); - USART_ITConfig(PIOS_USART2_USART, USART_IT_RXNE, ENABLE); - USART_ITConfig(PIOS_USART2_USART, USART_IT_TXE, ENABLE); - - /* Configure the USART Interrupts */ - NVIC_InitStructure.NVIC_IRQChannel = PIOS_USART2_IRQ_CHANNEL; - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PIOS_USART2_NVIC_PRIO; - NVIC_Init(&NVIC_InitStructure); - USART_ITConfig(PIOS_USART2_USART, USART_IT_RXNE, ENABLE); - - /* Enable USART */ - USART_Cmd(PIOS_USART2_USART, ENABLE); -#endif - -#if (PIOS_USART3_ENABLED) - /* Enable the USART Pins Software Remapping */ - PIOS_USART3_REMAP_FUNC; - - /* Configure and Init USART Tx as alternate function push-pull */ - GPIO_InitStructure.GPIO_Pin = PIOS_USART3_TX_PIN; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; - GPIO_Init(PIOS_USART3_GPIO_PORT, &GPIO_InitStructure); - - /* Configure and Init USART Rx input with internal pull-ups */ - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; - GPIO_InitStructure.GPIO_Pin = PIOS_USART3_RX_PIN; - GPIO_Init(PIOS_USART3_GPIO_PORT, &GPIO_InitStructure); - - /* Enable USART clock */ - PIOS_USART3_CLK_FUNC; - - /* Enable USART Receive and Transmit interrupts */ - USART_InitStructure.USART_BaudRate = PIOS_USART3_BAUDRATE; - USART_Init(PIOS_USART3_USART, &USART_InitStructure); - USART_ITConfig(PIOS_USART3_USART, USART_IT_RXNE, ENABLE); - USART_ITConfig(PIOS_USART3_USART, USART_IT_TXE, ENABLE); - - /* Configure the USART Interrupts */ - NVIC_InitStructure.NVIC_IRQChannel = PIOS_USART3_IRQ_CHANNEL; - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PIOS_USART3_NVIC_PRIO; - NVIC_Init(&NVIC_InitStructure); - USART_ITConfig(PIOS_USART3_USART, USART_IT_RXNE, ENABLE); - - /* Enable USART */ - USART_Cmd(PIOS_USART3_USART, ENABLE); -#endif + /* Configure USART Interrupts */ + NVIC_Init(&usart_dev->cfg->irq.init); + USART_ITConfig(usart_dev->cfg->regs, USART_IT_RXNE, ENABLE); + USART_ITConfig(usart_dev->cfg->regs, USART_IT_TXE, ENABLE); + /* Enable USART */ + USART_Cmd(usart_dev->cfg->regs, ENABLE); + } } /** @@ -184,26 +111,29 @@ void PIOS_USART_Init(void) * \param[in] usart USART name (GPS, TELEM, AUX) * \param[in] baud Requested baud rate */ -void PIOS_USART_ChangeBaud(USARTNumTypeDef usart, uint32_t baud) +void PIOS_USART_ChangeBaud(uint8_t usart, uint32_t baud) { - USART_InitTypeDef USART_InitStructure; - USART_InitStructure.USART_WordLength = USART_WordLength_8b; - USART_InitStructure.USART_StopBits = USART_StopBits_1; - USART_InitStructure.USART_Parity = USART_Parity_No; - USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; - USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; - USART_InitStructure.USART_BaudRate = baud; - if (usart == USART_1) { - USART_Init(PIOS_USART1_USART, &USART_InitStructure); - } else if (usart == USART_2) { -#if (PIOS_USART2_ENABLED) - USART_Init(PIOS_USART2_USART, &USART_InitStructure); + struct pios_usart_dev * usart_dev; + USART_InitTypeDef USART_InitStructure; + + /* Get a handle for the device configuration */ + usart_dev = find_usart_dev_by_id(usart); + +#if 0 + if (!usart_dev) { + /* Undefined USART port for this board (see pios_board.c) */ + return -2; + } #endif - } else if (usart == USART_3) { -#if (PIOS_USART3_ENABLED) - USART_Init(PIOS_USART3_USART, &USART_InitStructure); -#endif - } + + /* 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); } /** @@ -213,13 +143,19 @@ void PIOS_USART_ChangeBaud(USARTNumTypeDef usart, uint32_t baud) * \return 1: USART available * \return 0: USART not available */ -int32_t PIOS_USART_RxBufferFree(USARTNumTypeDef usart) +int32_t PIOS_USART_RxBufferFree(uint8_t usart) { - if(usart >= PIOS_USART_NUM) { - return 0; - } else { - return PIOS_USART_RX_BUFFER_SIZE - rx_buffer_size[usart]; - } + struct pios_usart_dev * usart_dev; + + /* Get a handle for the device configuration */ + usart_dev = find_usart_dev_by_id(usart); + + if (!usart_dev) { + /* Undefined USART port for this board (see pios_board.c) */ + return -2; + } + + return (sizeof(usart_dev->rx.buf) - usart_dev->rx.size); } /** @@ -229,13 +165,19 @@ int32_t PIOS_USART_RxBufferFree(USARTNumTypeDef usart) * \return 0 if USART not available * \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions */ -int32_t PIOS_USART_RxBufferUsed(USARTNumTypeDef usart) +int32_t PIOS_USART_RxBufferUsed(uint8_t usart) { - if(usart >= PIOS_USART_NUM) { - return 0; - } else { - return rx_buffer_size[usart]; - } + struct pios_usart_dev * usart_dev; + + /* Get a handle for the device configuration */ + usart_dev = find_usart_dev_by_id(usart); + + if (!usart_dev) { + /* Undefined USART port for this board (see pios_board.c) */ + return -2; + } + + return (usart_dev->rx.size); } /** @@ -246,28 +188,34 @@ int32_t PIOS_USART_RxBufferUsed(USARTNumTypeDef usart) * \return >= 0: number of received bytes * \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions */ -int32_t PIOS_USART_RxBufferGet(USARTNumTypeDef usart) +int32_t PIOS_USART_RxBufferGet(uint8_t usart) { - if(usart >= PIOS_USART_NUM) { - /* USART not available */ - return -1; - } - if(!rx_buffer_size[usart]) { - /* nothing new in buffer */ - return -2; - } - - /* get byte - this operation should be atomic! */ - PIOS_IRQ_Disable(); - uint8_t b = rx_buffer[usart][rx_buffer_tail[usart]]; - if(++rx_buffer_tail[usart] >= PIOS_USART_RX_BUFFER_SIZE) { - rx_buffer_tail[usart] = 0; - } - --rx_buffer_size[usart]; - PIOS_IRQ_Enable(); - - /* Return received byte */ - return b; + struct pios_usart_dev * usart_dev; + + /* Get a handle for the device configuration */ + usart_dev = find_usart_dev_by_id(usart); + + if (!usart_dev) { + /* Undefined USART port for this board (see pios_board.c) */ + return -2; + } + + if (!usart_dev->rx.size) { + /* Nothing new in the buffer */ + return -1; + } + + /* get byte - this operation should be atomic! */ + PIOS_IRQ_Disable(); + uint8_t b = usart_dev->rx.buf[usart_dev->rx.tail++]; + if (usart_dev->rx.tail >= sizeof(usart_dev->rx.buf)) { + usart_dev->rx.tail = 0; + } + usart_dev->rx.size--; + PIOS_IRQ_Enable(); + + /* Return received byte */ + return b; } /** @@ -278,25 +226,30 @@ int32_t PIOS_USART_RxBufferGet(USARTNumTypeDef usart) * \return >= 0: number of received bytes * \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions */ -int32_t PIOS_USART_RxBufferPeek(USARTNumTypeDef usart) +int32_t PIOS_USART_RxBufferPeek(uint8_t usart) { - if(usart >= PIOS_USART_NUM) { - /* USART not available */ - return -1; - } + struct pios_usart_dev * usart_dev; - if(!rx_buffer_size[usart]) { - /* Nothing new in buffer */ - return -2; - } + /* Get a handle for the device configuration */ + usart_dev = find_usart_dev_by_id(usart); - /* Get byte - this operation should be atomic! */ - PIOS_IRQ_Disable(); - uint8_t b = rx_buffer[usart][rx_buffer_tail[usart]]; - PIOS_IRQ_Enable(); + if (!usart_dev) { + /* Undefined USART port for this board (see pios_board.c) */ + return -2; + } - /* Return received byte */ - return b; + if (!usart_dev->rx.size) { + /* Nothing new in the buffer */ + return -1; + } + + /* get byte - this operation should be atomic! */ + PIOS_IRQ_Disable(); + uint8_t b = usart_dev->rx.buf[usart_dev->rx.tail]; + PIOS_IRQ_Enable(); + + /* Return received byte */ + return b; } /** @@ -308,30 +261,35 @@ int32_t PIOS_USART_RxBufferPeek(USARTNumTypeDef usart) * \return -2 if buffer full (retry) * \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions */ -int32_t PIOS_USART_RxBufferPut(USARTNumTypeDef usart, uint8_t b) +int32_t PIOS_USART_RxBufferPut(uint8_t usart, uint8_t b) { - if(usart >= PIOS_USART_NUM) { - /* USART not available */ - return -1; - } + struct pios_usart_dev * usart_dev; - if(rx_buffer_size[usart] >= PIOS_USART_RX_BUFFER_SIZE) { - /* Buffer full (retry) */ - return -2; - } + /* Get a handle for the device configuration */ + usart_dev = find_usart_dev_by_id(usart); - /* Copy received byte into receive buffer */ - /* This operation should be atomic! */ - PIOS_IRQ_Disable(); - rx_buffer[usart][rx_buffer_head[usart]] = b; - if(++rx_buffer_head[usart] >= PIOS_USART_RX_BUFFER_SIZE) { - rx_buffer_head[usart] = 0; - } - ++rx_buffer_size[usart]; - PIOS_IRQ_Enable(); + if (!usart_dev) { + /* Undefined USART port for this board (see pios_board.c) */ + return -1; + } - /* No error */ - return 0; + if (usart_dev->rx.size >= sizeof(usart_dev->rx.buf)) { + /* Buffer full (retry) */ + return -2; + } + + /* Copy received byte into receive buffer */ + /* This operation should be atomic! */ + PIOS_IRQ_Disable(); + usart_dev->rx.buf[usart_dev->rx.head++] = b; + if (usart_dev->rx.head >= sizeof(usart_dev->rx.buf)) { + usart_dev->rx.head = 0; + } + usart_dev->rx.size++; + PIOS_IRQ_Enable(); + + /* No error */ + return 0; } /** @@ -341,13 +299,19 @@ int32_t PIOS_USART_RxBufferPut(USARTNumTypeDef usart, uint8_t b) * \return 0 if USART not available * \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions */ -int32_t PIOS_USART_TxBufferFree(USARTNumTypeDef usart) +int32_t PIOS_USART_TxBufferFree(uint8_t usart) { - if(usart >= PIOS_USART_NUM) { - return 0; - } else { - return PIOS_USART_TX_BUFFER_SIZE - tx_buffer_size[usart]; - } + struct pios_usart_dev * usart_dev; + + /* Get a handle for the device configuration */ + usart_dev = find_usart_dev_by_id(usart); + + if (!usart_dev) { + /* Undefined USART port for this board (see pios_board.c) */ + return 0; + } + + return (sizeof(usart_dev->tx.buf) - usart_dev->tx.size); } /** @@ -357,13 +321,19 @@ int32_t PIOS_USART_TxBufferFree(USARTNumTypeDef usart) * \return 0 if USART not available * \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions */ -int32_t PIOS_USART_TxBufferUsed(USARTNumTypeDef usart) +int32_t PIOS_USART_TxBufferUsed(uint8_t usart) { - if(usart >= PIOS_USART_NUM) { - return 0; - } else { - return tx_buffer_size[usart]; - } + struct pios_usart_dev * usart_dev; + + /* Get a handle for the device configuration */ + usart_dev = find_usart_dev_by_id(usart); + + if (!usart_dev) { + /* Undefined USART port for this board (see pios_board.c) */ + return 0; + } + + return (usart_dev->tx.size); } /** @@ -374,29 +344,34 @@ int32_t PIOS_USART_TxBufferUsed(USARTNumTypeDef usart) * \return >= 0: transmitted byte * \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions */ -int32_t PIOS_USART_TxBufferGet(USARTNumTypeDef usart) +int32_t PIOS_USART_TxBufferGet(uint8_t usart) { - if(usart >= PIOS_USART_NUM) { - /* USART not available */ - return -1; - } + struct pios_usart_dev * usart_dev; - if(!tx_buffer_size[usart]) { - /* Nothing new in buffer */ - return -2; - } + /* Get a handle for the device configuration */ + usart_dev = find_usart_dev_by_id(usart); - /* Get byte - this operation should be atomic! */ - PIOS_IRQ_Disable(); - uint8_t b = tx_buffer[usart][tx_buffer_tail[usart]]; - if(++tx_buffer_tail[usart] >= PIOS_USART_TX_BUFFER_SIZE) { - tx_buffer_tail[usart] = 0; - } - --tx_buffer_size[usart]; - PIOS_IRQ_Enable(); + if (!usart_dev) { + /* Undefined USART port for this board (see pios_board.c) */ + return -1; + } - /* Return transmitted byte */ - return b; + if (!usart_dev->tx.size) { + /* Nothing new in the buffer */ + return -2; + } + + /* get byte - this operation should be atomic! */ + PIOS_IRQ_Disable(); + uint8_t b = usart_dev->tx.buf[usart_dev->tx.tail++]; + if (usart_dev->tx.tail >= sizeof(usart_dev->tx.buf)) { + usart_dev->tx.tail = 0; + } + usart_dev->tx.size--; + PIOS_IRQ_Enable(); + + /* Return received byte */ + return b; } /** @@ -410,55 +385,45 @@ int32_t PIOS_USART_TxBufferGet(USARTNumTypeDef usart) * \return -3 if USART not supported by USARTTxBufferPut Routine * \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions */ -int32_t PIOS_USART_TxBufferPutMoreNonBlocking(USARTNumTypeDef usart, uint8_t *buffer, uint16_t len) +int32_t PIOS_USART_TxBufferPutMoreNonBlocking(uint8_t usart, uint8_t *buffer, uint16_t len) { - if(usart >= PIOS_USART_NUM) { - /* USART not available */ - return -1; - } + struct pios_usart_dev * usart_dev; - if((tx_buffer_size[usart]+len) >= PIOS_USART_TX_BUFFER_SIZE) { - /* Buffer full or cannot get all requested bytes (retry) */ - return -2; - } + /* Get a handle for the device configuration */ + usart_dev = find_usart_dev_by_id(usart); - /* Copy bytes to be transmitted into transmit buffer */ - /* This operation should be atomic! */ - PIOS_IRQ_Disable(); + if (!usart_dev) { + /* Undefined USART port for this board (see pios_board.c) */ + return -1; + } - uint16_t i; - for(i = 0; i < len; ++i) { - tx_buffer[usart][tx_buffer_head[usart]] = *buffer++; - - if(++tx_buffer_head[usart] >= PIOS_USART_TX_BUFFER_SIZE) { - tx_buffer_head[usart] = 0; - } - - /* Enable Tx interrupt if buffer was empty */ - if(++tx_buffer_size[usart] == 1) { - switch(usart) { - #if (PIOS_USART1_ENABLED) - /* Enable TXE interrupt (TXEIE=1) */ - case 0: PIOS_USART1_USART->CR1 |= (1 << 7); break; - #endif - #if (PIOS_USART2_ENABLED) - /* Enable TXE interrupt (TXEIE=1) */ - case 1: PIOS_USART2_USART->CR1 |= (1 << 7); break; - #endif - #if (PIOS_USART3_ENABLED) - /* Enable TXE interrupt (TXEIE=1) */ - case 2: PIOS_USART3_USART->CR1 |= (1 << 7); break; - /* USART not supported by routine (yet) */ - #endif - default: PIOS_IRQ_Enable(); return -3; - } - } - } + if (usart_dev->tx.size + len >= sizeof(usart_dev->tx.buf)) { + /* Buffer cannot accept all requested bytes (retry) */ + return -2; + } - PIOS_IRQ_Enable(); + /* Copy bytes to be transmitted into transmit buffer */ + /* This operation should be atomic! */ + PIOS_IRQ_Disable(); - /* No error */ - return 0; + uint16_t i; + for(i = 0; i < len; ++i) { + usart_dev->tx.buf[usart_dev->tx.head++] = *buffer++; + if (usart_dev->tx.head >= sizeof(usart_dev->tx.buf)) { + usart_dev->tx.head = 0; + } + + usart_dev->tx.size++; + if (usart_dev->tx.size == 1) { + /* Buffer has just become non-empty, enable tx interrupt. */ + USART_ITConfig(usart_dev->cfg->regs, USART_IT_TXE, ENABLE); + } + } + + PIOS_IRQ_Enable(); + + /* No error */ + return 0; } /** @@ -472,13 +437,13 @@ int32_t PIOS_USART_TxBufferPutMoreNonBlocking(USARTNumTypeDef usart, uint8_t *bu * \return -3 if USART not supported by USARTTxBufferPut Routine * \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions */ -int32_t PIOS_USART_TxBufferPutMore(USARTNumTypeDef usart, uint8_t *buffer, uint16_t len) +int32_t PIOS_USART_TxBufferPutMore(uint8_t usart, uint8_t *buffer, uint16_t len) { - int error; + int32_t rc; - while((error = PIOS_USART_TxBufferPutMoreNonBlocking(usart, buffer, len)) == -2); + while((rc = PIOS_USART_TxBufferPutMoreNonBlocking(usart, buffer, len)) == -2); - return error; + return rc; } /** @@ -491,11 +456,9 @@ int32_t PIOS_USART_TxBufferPutMore(USARTNumTypeDef usart, uint8_t *buffer, uint1 * \return -3 if USART not supported by USARTTxBufferPut Routine * \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions */ -int32_t PIOS_USART_TxBufferPut_NonBlocking(USARTNumTypeDef usart, uint8_t b) +int32_t PIOS_USART_TxBufferPut_NonBlocking(uint8_t usart, uint8_t b) { - /* For more comfortable usage... */ - /* -> Just forward to USARTTxBufferPutMore */ - return PIOS_USART_TxBufferPutMore(usart, &b, 1); + return PIOS_USART_TxBufferPutMoreNonBlocking(usart, &b, 1); } /** @@ -508,105 +471,44 @@ int32_t PIOS_USART_TxBufferPut_NonBlocking(USARTNumTypeDef usart, uint8_t b) * \return -3 if USART not supported by USARTTxBufferPut Routine * \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions */ -int32_t PIOS_USART_TxBufferPut(USARTNumTypeDef usart, uint8_t b) +int32_t PIOS_USART_TxBufferPut(uint8_t usart, uint8_t b) { - int error; - - while((error = PIOS_USART_TxBufferPutMore(usart, &b, 1)) == -2); - - return error; + return PIOS_USART_TxBufferPutMore(usart, &b, 1); } -#if (PIOS_USART1_ENABLED) -/* Interrupt handler for USART1 */ -PIOS_USART1_IRQHANDLER_FUNC +void PIOS_USART_IRQ_Handler(uint8_t usart) { - /* Check if RXNE flag is set */ - if(PIOS_USART1_USART->SR & USART_SR_RXNE) { - uint8_t b = PIOS_USART1_USART->DR; + struct pios_usart_dev * usart_dev; + + /* Get a handle for the device configuration */ + usart_dev = find_usart_dev_by_id(usart); + PIOS_DEBUG_Assert(usart_dev); + + /* Check if RXNE flag is set */ + if (usart_dev->cfg->regs->SR & USART_SR_RXNE) { + uint8_t b = usart_dev->cfg->regs->DR; - if(PIOS_USART_RxBufferPut(USART_1, b) < 0) { - /* Here we could add some error handling */ - } - } + if (PIOS_USART_RxBufferPut(usart, b) < 0) { + /* Here we could add some error handling */ + } + } - /* Check if TXE flag is set */ - if(PIOS_USART1_USART->SR & USART_SR_TXE) { - if(PIOS_USART_TxBufferUsed(USART_1) > 0) { - int b = PIOS_USART_TxBufferGet(USART_1); - if(b < 0) { - /* Here we could add some error handling */ - PIOS_USART1_USART->DR = 0xff; - } else { - PIOS_USART1_USART->DR = b; - } - } else { - /* Disable TXE interrupt (TXEIE=0) */ - PIOS_USART1_USART->CR1 &= ~(USART_CR1_TXEIE); - } - } + /* Check if TXE flag is set */ + if (usart_dev->cfg->regs->SR & USART_SR_TXE) { + if (PIOS_USART_TxBufferUsed(usart) > 0) { + int32_t b = PIOS_USART_TxBufferGet(usart); + + if(b < 0) { + /* Here we could add some error handling */ + usart_dev->cfg->regs->DR = 0xff; + } else { + usart_dev->cfg->regs->DR = b & 0xff; + } + } else { + /* Disable TXE interrupt (TXEIE=0) */ + USART_ITConfig(usart_dev->cfg->regs, USART_IT_TXE, DISABLE); + } + } } -#endif - -#if (PIOS_USART2_ENABLED) -/* Interrupt handler for USART2 */ -PIOS_USART2_IRQHANDLER_FUNC -{ - /* check if RXNE flag is set */ - if(PIOS_USART2_USART->SR & USART_SR_RXNE) { - uint8_t b = PIOS_USART2_USART->DR; - - if(PIOS_USART_RxBufferPut(USART_2, b) < 0) { - /* Here we could add some error handling */ - } - } - - /* Check if TXE flag is set */ - if(PIOS_USART2_USART->SR & USART_SR_TXE) { - if(PIOS_USART_TxBufferUsed(USART_2) > 0) { - int b = PIOS_USART_TxBufferGet(USART_2); - if(b < 0) { - /* Here we could add some error handling */ - PIOS_USART2_USART->DR = 0xff; - } else { - PIOS_USART2_USART->DR = b; - } - } else { - /* Disable TXE interrupt (TXEIE=0) */ - PIOS_USART2_USART->CR1 &= ~(USART_CR1_TXEIE); - } - } -} -#endif - -#if (PIOS_USART3_ENABLED) -/* Interrupt handler for USART3 */ -PIOS_USART3_IRQHANDLER_FUNC -{ - /* check if RXNE flag is set */ - if(PIOS_USART3_USART->SR & USART_SR_RXNE) { - uint8_t b = PIOS_USART3_USART->DR; - - if(PIOS_USART_RxBufferPut(USART_3, b) < 0) { - /* Here we could add some error handling */ - } - } - - if(PIOS_USART3_USART->SR & USART_SR_TXE) { // check if TXE flag is set - if(PIOS_USART_TxBufferUsed(USART_3) > 0) { - int b = PIOS_USART_TxBufferGet(USART_3); - if(b < 0) { - /* Here we could add some error handling */ - PIOS_USART3_USART->DR = 0xff; - } else { - PIOS_USART3_USART->DR = b; - } - } else { - /* Disable TXE interrupt (TXEIE=0) */ - PIOS_USART3_USART->CR1 &= ~(USART_CR1_TXEIE); - } - } -} -#endif #endif diff --git a/flight/PiOS/STM32F10x/pios_usb_hid.c b/flight/PiOS/STM32F10x/pios_usb_hid.c index 2674cbc40..f5aaed03e 100644 --- a/flight/PiOS/STM32F10x/pios_usb_hid.c +++ b/flight/PiOS/STM32F10x/pios_usb_hid.c @@ -33,6 +33,12 @@ #if defined(PIOS_INCLUDE_USB_HID) +const struct pios_com_driver pios_usb_com_driver = { + .tx_nb = PIOS_USB_HID_TxBufferPutMoreNonBlocking, + .tx = PIOS_USB_HID_TxBufferPutMore, + .rx = PIOS_USB_HID_RxBufferGet, + .rx_avail = PIOS_USB_HID_RxBufferUsed, +}; /* Local types */ typedef enum _HID_REQUESTS { @@ -127,7 +133,7 @@ int32_t PIOS_USB_HID_ChangeConnectionState(uint32_t Connected) * \return 0: interface not available * \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions */ -int32_t PIOS_USB_HID_CheckAvailable(void) +int32_t PIOS_USB_HID_CheckAvailable(uint8_t id) { return transfer_possible ? 1 : 0; } @@ -140,7 +146,7 @@ int32_t PIOS_USB_HID_CheckAvailable(void) * \return -1 if too many bytes to be send * \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions */ -int32_t PIOS_USB_HID_TxBufferPutMoreNonBlocking(uint8_t *buffer, uint16_t len) +int32_t PIOS_USB_HID_TxBufferPutMoreNonBlocking(uint8_t id, uint8_t *buffer, uint16_t len) { if(len > PIOS_USB_HID_DATA_LENGTH) { /* Cannot send all requested bytes */ @@ -167,11 +173,11 @@ int32_t PIOS_USB_HID_TxBufferPutMoreNonBlocking(uint8_t *buffer, uint16_t len) * \return -1 if too many bytes to be send * \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions */ -int32_t PIOS_USB_HID_TxBufferPutMore(uint8_t *buffer, uint16_t len) +int32_t PIOS_USB_HID_TxBufferPutMore(uint8_t id, uint8_t *buffer, uint16_t len) { int32_t error; - while((error = PIOS_USB_HID_TxBufferPutMoreNonBlocking(buffer, len)) == -2); + while((error = PIOS_USB_HID_TxBufferPutMoreNonBlocking(id, buffer, len)) == -2); return error; } @@ -182,7 +188,7 @@ int32_t PIOS_USB_HID_TxBufferPutMore(uint8_t *buffer, uint16_t len) * \return >= 0: received byte * \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions */ -uint8_t PIOS_USB_HID_RxBufferGet(void) +int32_t PIOS_USB_HID_RxBufferGet(uint8_t id) { if(rx_buffer_new_data_ctr == 0) { /* Nothing new in buffer */ @@ -205,7 +211,7 @@ uint8_t PIOS_USB_HID_RxBufferGet(void) * \return 0 nothing available * \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions */ -int32_t PIOS_USB_HID_RxBufferUsed(void) +int32_t PIOS_USB_HID_RxBufferUsed(uint8_t id) { return rx_buffer_new_data_ctr; } diff --git a/flight/PiOS/inc/pios_com.h b/flight/PiOS/inc/pios_com.h index 60554b8e8..361e820d5 100644 --- a/flight/PiOS/inc/pios_com.h +++ b/flight/PiOS/inc/pios_com.h @@ -27,29 +27,29 @@ #ifndef PIOS_COM_H #define PIOS_COM_H -/* Global Types */ -typedef enum { - COM_DEBUG_USART = 0, - COM_USART1 = 1, - COM_USART2 = 2, - COM_USART3 = 3, - COM_USB_HID = 4 -} COMPortTypeDef; - /* Public Functions */ extern int32_t PIOS_COM_Init(void); -extern int32_t PIOS_COM_ChangeBaud(COMPortTypeDef port, uint32_t baud); -extern int32_t PIOS_COM_SendCharNonBlocking(COMPortTypeDef port, char c); -extern int32_t PIOS_COM_SendChar(COMPortTypeDef port, char c); -extern int32_t PIOS_COM_SendBufferNonBlocking(COMPortTypeDef port, uint8_t *buffer, uint16_t len); -extern int32_t PIOS_COM_SendBuffer(COMPortTypeDef port, uint8_t *buffer, uint16_t len); -extern int32_t PIOS_COM_SendStringNonBlocking(COMPortTypeDef port, char *str); -extern int32_t PIOS_COM_SendString(COMPortTypeDef port, char *str); -extern int32_t PIOS_COM_SendFormattedStringNonBlocking(COMPortTypeDef port, char *format, ...); -extern int32_t PIOS_COM_SendFormattedString(COMPortTypeDef port, char *format, ...); -extern uint8_t PIOS_COM_ReceiveBuffer(COMPortTypeDef port); -extern int32_t PIOS_COM_ReceiveBufferUsed(COMPortTypeDef port); +extern int32_t PIOS_COM_ChangeBaud(uint8_t port, uint32_t baud); +extern int32_t PIOS_COM_SendCharNonBlocking(uint8_t port, char c); +extern int32_t PIOS_COM_SendChar(uint8_t port, char c); +extern int32_t PIOS_COM_SendBufferNonBlocking(uint8_t port, uint8_t *buffer, uint16_t len); +extern int32_t PIOS_COM_SendBuffer(uint8_t port, uint8_t *buffer, uint16_t len); +extern int32_t PIOS_COM_SendStringNonBlocking(uint8_t port, char *str); +extern int32_t PIOS_COM_SendString(uint8_t port, char *str); +extern int32_t PIOS_COM_SendFormattedStringNonBlocking(uint8_t port, char *format, ...); +extern int32_t PIOS_COM_SendFormattedString(uint8_t port, char *format, ...); +extern uint8_t PIOS_COM_ReceiveBuffer(uint8_t port); +extern int32_t PIOS_COM_ReceiveBufferUsed(uint8_t port); extern int32_t PIOS_COM_ReceiveHandler(void); +struct pios_com_driver { + void (*init)(uint8_t id); + void (*set_baud)(uint8_t id, uint32_t baud); + int32_t (*tx_nb)(uint8_t id, uint8_t *buffer, uint16_t len); + int32_t (*tx)(uint8_t id, uint8_t *buffer, uint16_t len); + int32_t (*rx)(uint8_t id); + int32_t (*rx_avail)(uint8_t id); +}; + #endif /* PIOS_COM_H */ diff --git a/flight/PiOS/inc/pios_com_priv.h b/flight/PiOS/inc/pios_com_priv.h new file mode 100644 index 000000000..8f91215e4 --- /dev/null +++ b/flight/PiOS/inc/pios_com_priv.h @@ -0,0 +1,41 @@ +/** + ****************************************************************************** + * + * @file pios_com_priv.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * Parts by Thorsten Klose (tk@midibox.org) + * @brief COM private definitions. + * @see The GNU Public License (GPL) Version 3 + * + *****************************************************************************/ +/* + * 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 + * (at your option) any later version. + * + * 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 + * for more details. + * + * 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., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PIOS_COM_PRIV_H +#define PIOS_COM_PRIV_H + +#include + +struct pios_com_dev { + uint8_t id; + const struct pios_com_driver * const driver; +}; + +extern struct pios_com_dev pios_com_devs[]; +extern uint8_t pios_com_num_devices; + +#endif /* PIOS_COM_PRIV_H */ + diff --git a/flight/PiOS/inc/pios_usart.h b/flight/PiOS/inc/pios_usart.h index f2734efd5..474b7e95c 100644 --- a/flight/PiOS/inc/pios_usart.h +++ b/flight/PiOS/inc/pios_usart.h @@ -29,23 +29,23 @@ /* Global Types */ -typedef enum {USART_1 = 0, USART_2 = 1, USART_3 = 2} USARTNumTypeDef; /* Public Functions */ extern void PIOS_USART_Init(void); -extern void PIOS_USART_ChangeBaud(USARTNumTypeDef usart, uint32_t baud); +extern void PIOS_USART_ChangeBaud(uint8_t usart, uint32_t baud); -extern int32_t PIOS_USART_RxBufferFree(USARTNumTypeDef uart); -extern int32_t PIOS_USART_RxBufferUsed(USARTNumTypeDef uart); -extern int32_t PIOS_USART_RxBufferGet(USARTNumTypeDef uart); -extern int32_t PIOS_USART_RxBufferPeek(USARTNumTypeDef uart); -extern int32_t PIOS_USART_RxBufferPut(USARTNumTypeDef uart, uint8_t b); +extern int32_t PIOS_USART_RxBufferFree(uint8_t usart); +extern int32_t PIOS_USART_RxBufferUsed(uint8_t usart); +extern int32_t PIOS_USART_RxBufferGet(uint8_t usart); +extern int32_t PIOS_USART_RxBufferPeek(uint8_t usart); +extern int32_t PIOS_USART_RxBufferPut(uint8_t usart, uint8_t b); -extern int32_t PIOS_USART_TxBufferFree(USARTNumTypeDef uart); -extern int32_t PIOS_USART_TxBufferGet(USARTNumTypeDef uart); -extern int32_t PIOS_USART_TxBufferPutMoreNonBlocking(USARTNumTypeDef uart, uint8_t *buffer, uint16_t len); -extern int32_t PIOS_USART_TxBufferPutMore(USARTNumTypeDef uart, uint8_t *buffer, uint16_t len); -extern int32_t PIOS_USART_TxBufferPutNonBlocking(uint8_t uart, uint8_t b); -extern int32_t PIOS_USART_TxBufferPut(USARTNumTypeDef uart, uint8_t b); +extern int32_t PIOS_USART_TxBufferFree(uint8_t usart); +extern int32_t PIOS_USART_TxBufferGet(uint8_t usart); +extern int32_t PIOS_USART_TxBufferPutMoreNonBlocking(uint8_t usart, uint8_t *buffer, uint16_t len); +extern int32_t PIOS_USART_TxBufferPutMore(uint8_t usart, uint8_t *buffer, uint16_t len); +extern int32_t PIOS_USART_TxBufferPutNonBlocking(uint8_t usart, uint8_t b); +extern int32_t PIOS_USART_TxBufferPut(uint8_t usart, uint8_t b); +extern void PIOS_USART_IRQ_Handler(uint8_t usart); #endif /* PIOS_USART_H */ diff --git a/flight/PiOS/inc/pios_usart_priv.h b/flight/PiOS/inc/pios_usart_priv.h new file mode 100644 index 000000000..6a0ba9db6 --- /dev/null +++ b/flight/PiOS/inc/pios_usart_priv.h @@ -0,0 +1,58 @@ +/** + ****************************************************************************** + * + * @file pios_usart_priv.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * Parts by Thorsten Klose (tk@midibox.org) + * @brief USART private definitions. + * @see The GNU Public License (GPL) Version 3 + * + *****************************************************************************/ +/* + * 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 + * (at your option) any later version. + * + * 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 + * for more details. + * + * 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., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PIOS_USART_PRIV_H +#define PIOS_USART_PRIV_H + +#include +#include + +struct pios_usart_cfg { + USART_TypeDef * regs; + uint32_t remap; /* GPIO_Remap_* */ + USART_InitTypeDef init; + struct stm32_gpio rx; + struct stm32_gpio tx; + struct stm32_irq irq; +}; + +struct pios_usart_buffer { + uint8_t buf[PIOS_USART_RX_BUFFER_SIZE]; + uint16_t head; + uint16_t tail; + uint16_t size; +}; + +struct pios_usart_dev { + const struct pios_usart_cfg * const cfg; + struct pios_usart_buffer rx; + struct pios_usart_buffer tx; +}; + +extern struct pios_usart_dev pios_usart_devs[]; +extern uint8_t pios_usart_num_devices; + +#endif /* PIOS_USART_PRIV_H */ diff --git a/flight/PiOS/inc/pios_usb_hid.h b/flight/PiOS/inc/pios_usb_hid.h index 1e7bdb87b..b715086a7 100644 --- a/flight/PiOS/inc/pios_usb_hid.h +++ b/flight/PiOS/inc/pios_usb_hid.h @@ -39,11 +39,11 @@ /* Global functions */ extern int32_t PIOS_USB_HID_Init(uint32_t mode); extern int32_t PIOS_USB_HID_ChangeConnectionState(uint32_t Connected); -extern int32_t PIOS_USB_HID_CheckAvailable(void); -extern int32_t PIOS_USB_HID_TxBufferPutMoreNonBlocking(uint8_t *buffer, uint16_t len); -extern int32_t PIOS_USB_HID_TxBufferPutMore(uint8_t *buffer, uint16_t len); -extern uint8_t PIOS_USB_HID_RxBufferGet(void); -extern int32_t PIOS_USB_HID_RxBufferUsed(void); +extern int32_t PIOS_USB_HID_CheckAvailable(uint8_t id); +extern int32_t PIOS_USB_HID_TxBufferPutMoreNonBlocking(uint8_t id, uint8_t *buffer, uint16_t len); +extern int32_t PIOS_USB_HID_TxBufferPutMore(uint8_t id, uint8_t *buffer, uint16_t len); +extern int32_t PIOS_USB_HID_RxBufferGet(uint8_t id); +extern int32_t PIOS_USB_HID_RxBufferUsed(uint8_t id); extern int32_t PIOS_USB_HID_CB_Data_Setup(uint8_t RequestNo); extern int32_t PIOS_USB_HID_CB_NoData_Setup(uint8_t RequestNo); extern void PIOS_USB_HID_EP1_OUT_Callback(void);