From 2d27c54d4805a62e51321d390998978090ec2e68 Mon Sep 17 00:00:00 2001 From: Stacey Sheldon Date: Thu, 12 Jan 2012 19:53:52 -0500 Subject: [PATCH] com_msg: Create new message based COM layer for bootloaders The main purpose of this new COM implementation is that it is much simpler, and requires less code space. This takes a bit of the pressure off of the CC bootloader which was right at the limit of available code space in the bootloader partition. This is not intended to ever be used by the application. This driver also formalizes the assumptions in the bootloader's usage of the COM layer. All messages are assumed to arrive in atomic chunks from the HID layer. --- flight/Bootloaders/CopterControl/Makefile | 2 +- .../CopterControl/inc/pios_config.h | 2 +- flight/Bootloaders/CopterControl/main.c | 5 +- flight/Bootloaders/CopterControl/op_dfu.c | 5 +- flight/Bootloaders/CopterControl/pios_board.c | 20 +- flight/Bootloaders/OpenPilot/Makefile | 1 + .../Bootloaders/OpenPilot/inc/pios_config.h | 1 + flight/Bootloaders/OpenPilot/main.c | 5 +- flight/Bootloaders/OpenPilot/op_dfu.c | 9 +- flight/Bootloaders/OpenPilot/pios_board.c | 24 +-- flight/Bootloaders/PipXtreme/Makefile | 2 +- .../Bootloaders/PipXtreme/inc/pios_config.h | 2 +- flight/Bootloaders/PipXtreme/main.c | 5 +- flight/Bootloaders/PipXtreme/op_dfu.c | 30 +-- flight/Bootloaders/PipXtreme/pios_board.c | 25 +-- flight/PiOS/Common/pios_com_msg.c | 185 ++++++++++++++++++ flight/PiOS/STM32F10x/pios_usb_hid.c | 18 +- flight/PiOS/inc/pios_com.h | 4 +- flight/PiOS/inc/pios_com_msg.h | 45 +++++ flight/PiOS/inc/pios_com_msg_priv.h | 44 +++++ flight/PiOS/inc/pios_com_priv.h | 2 + 21 files changed, 345 insertions(+), 91 deletions(-) create mode 100644 flight/PiOS/Common/pios_com_msg.c create mode 100644 flight/PiOS/inc/pios_com_msg.h create mode 100644 flight/PiOS/inc/pios_com_msg_priv.h diff --git a/flight/Bootloaders/CopterControl/Makefile b/flight/Bootloaders/CopterControl/Makefile index 1257ed579..a5effde9c 100644 --- a/flight/Bootloaders/CopterControl/Makefile +++ b/flight/Bootloaders/CopterControl/Makefile @@ -116,7 +116,7 @@ SRC += $(PIOSCOMMON)/pios_usb_desc_hid_only.c ## PIOS Hardware (Common) SRC += $(PIOSCOMMON)/pios_board_info.c -SRC += $(PIOSCOMMON)/pios_com.c +SRC += $(PIOSCOMMON)/pios_com_msg.c SRC += $(PIOSCOMMON)/pios_bl_helper.c SRC += $(PIOSCOMMON)/pios_iap.c SRC += $(PIOSCOMMON)/printf-stdarg.c diff --git a/flight/Bootloaders/CopterControl/inc/pios_config.h b/flight/Bootloaders/CopterControl/inc/pios_config.h index 081ed1b0f..864ed4665 100644 --- a/flight/Bootloaders/CopterControl/inc/pios_config.h +++ b/flight/Bootloaders/CopterControl/inc/pios_config.h @@ -39,7 +39,7 @@ #define PIOS_INCLUDE_SYS #define PIOS_INCLUDE_USB #define PIOS_INCLUDE_USB_HID -#define PIOS_INCLUDE_COM +#define PIOS_INCLUDE_COM_MSG #define PIOS_INCLUDE_GPIO #define PIOS_NO_GPS diff --git a/flight/Bootloaders/CopterControl/main.c b/flight/Bootloaders/CopterControl/main.c index c1def4a46..7a61ae9e9 100644 --- a/flight/Bootloaders/CopterControl/main.c +++ b/flight/Bootloaders/CopterControl/main.c @@ -32,6 +32,7 @@ #include "usb_lib.h" #include "pios_iap.h" #include "fifo_buffer.h" +#include "pios_com_msg.h" /* Prototype of PIOS_Board_Init() function */ extern void PIOS_Board_Init(void); extern void FLASH_Download(); @@ -62,7 +63,7 @@ uint8_t JumpToApp = FALSE; uint8_t GO_dfu = FALSE; uint8_t USB_connected = FALSE; uint8_t User_DFU_request = FALSE; -static uint8_t mReceive_Buffer[64]; +static uint8_t mReceive_Buffer[63]; /* Private function prototypes -----------------------------------------------*/ uint32_t LedPWM(uint32_t pwm_period, uint32_t pwm_sweep_steps, uint32_t count); uint8_t processRX(); @@ -198,7 +199,7 @@ uint32_t LedPWM(uint32_t pwm_period, uint32_t pwm_sweep_steps, uint32_t count) { } uint8_t processRX() { - if (PIOS_COM_ReceiveBuffer(PIOS_COM_TELEM_USB, mReceive_Buffer, 63, 0) == 63) { + if (PIOS_COM_MSG_Receive(PIOS_COM_TELEM_USB, mReceive_Buffer, sizeof(mReceive_Buffer))) { processComand(mReceive_Buffer); } return TRUE; diff --git a/flight/Bootloaders/CopterControl/op_dfu.c b/flight/Bootloaders/CopterControl/op_dfu.c index ae85160c2..9f98d3394 100644 --- a/flight/Bootloaders/CopterControl/op_dfu.c +++ b/flight/Bootloaders/CopterControl/op_dfu.c @@ -30,6 +30,7 @@ #include "pios.h" #include "op_dfu.h" #include "pios_bl_helper.h" +#include "pios_com_msg.h" #include //programmable devices Device devicesTable[10]; @@ -446,9 +447,7 @@ uint32_t CalcFirmCRC() { } void sendData(uint8_t * buf, uint16_t size) { - PIOS_COM_SendBuffer(PIOS_COM_TELEM_USB, buf, size); - if (DeviceState == downloading) - PIOS_DELAY_WaitmS(20);//this is an hack, we should check wtf is wrong with hid + PIOS_COM_MSG_Send(PIOS_COM_TELEM_USB, buf, size); } bool flash_read(uint8_t * buffer, uint32_t adr, DFUProgType type) { diff --git a/flight/Bootloaders/CopterControl/pios_board.c b/flight/Bootloaders/CopterControl/pios_board.c index 1b3c54af3..972a85be0 100644 --- a/flight/Bootloaders/CopterControl/pios_board.c +++ b/flight/Bootloaders/CopterControl/pios_board.c @@ -27,17 +27,11 @@ // *********************************************************************************** -#if defined(PIOS_INCLUDE_COM) +#if defined(PIOS_INCLUDE_COM_MSG) -#include +#include -#define PIOS_COM_TELEM_USB_RX_BUF_LEN 192 -#define PIOS_COM_TELEM_USB_TX_BUF_LEN 192 - -static uint8_t pios_com_telem_usb_rx_buffer[PIOS_COM_TELEM_USB_RX_BUF_LEN]; -static uint8_t pios_com_telem_usb_tx_buffer[PIOS_COM_TELEM_USB_TX_BUF_LEN]; - -#endif /* PIOS_INCLUDE_COM */ +#endif /* PIOS_INCLUDE_COM_MSG */ // *********************************************************************************** @@ -97,17 +91,15 @@ void PIOS_Board_Init(void) { if (PIOS_USB_Init(&pios_usb_id, &pios_usb_main_cfg)) { PIOS_Assert(0); } -#if defined(PIOS_INCLUDE_USB_HID) && defined(PIOS_INCLUDE_COM) +#if defined(PIOS_INCLUDE_USB_HID) && defined(PIOS_INCLUDE_COM_MSG) uint32_t pios_usb_hid_id; if (PIOS_USB_HID_Init(&pios_usb_hid_id, &pios_usb_hid_cfg, pios_usb_id)) { PIOS_Assert(0); } - if (PIOS_COM_Init(&pios_com_telem_usb_id, &pios_usb_hid_com_driver, pios_usb_hid_id, - pios_com_telem_usb_rx_buffer, sizeof(pios_com_telem_usb_rx_buffer), - pios_com_telem_usb_tx_buffer, sizeof(pios_com_telem_usb_tx_buffer))) { + if (PIOS_COM_MSG_Init(&pios_com_telem_usb_id, &pios_usb_hid_com_driver, pios_usb_hid_id)) { PIOS_Assert(0); } -#endif /* PIOS_INCLUDE_USB_HID && PIOS_INCLUDE_COM */ +#endif /* PIOS_INCLUDE_USB_HID && PIOS_INCLUDE_COM_MSG */ #endif /* PIOS_INCLUDE_USB */ diff --git a/flight/Bootloaders/OpenPilot/Makefile b/flight/Bootloaders/OpenPilot/Makefile index e9b55e134..6c3888797 100644 --- a/flight/Bootloaders/OpenPilot/Makefile +++ b/flight/Bootloaders/OpenPilot/Makefile @@ -119,6 +119,7 @@ SRC += $(PIOSCOMMON)/pios_usb_desc_hid_only.c ## PIOS Hardware (Common) SRC += $(PIOSCOMMON)/pios_board_info.c +SRC += $(PIOSCOMMON)/pios_com_msg.c SRC += $(PIOSCOMMON)/pios_com.c SRC += $(PIOSCOMMON)/pios_opahrs_v0.c SRC += $(PIOSCOMMON)/pios_bl_helper.c diff --git a/flight/Bootloaders/OpenPilot/inc/pios_config.h b/flight/Bootloaders/OpenPilot/inc/pios_config.h index 024bcae9a..cf8981339 100644 --- a/flight/Bootloaders/OpenPilot/inc/pios_config.h +++ b/flight/Bootloaders/OpenPilot/inc/pios_config.h @@ -42,6 +42,7 @@ #define PIOS_INCLUDE_USB_HID #define PIOS_INCLUDE_OPAHRS #define PIOS_INCLUDE_COM +#define PIOS_INCLUDE_COM_MSG #define PIOS_INCLUDE_GPIO //#define DEBUG_SSP diff --git a/flight/Bootloaders/OpenPilot/main.c b/flight/Bootloaders/OpenPilot/main.c index 48056cff6..8a9e554ef 100644 --- a/flight/Bootloaders/OpenPilot/main.c +++ b/flight/Bootloaders/OpenPilot/main.c @@ -35,6 +35,7 @@ #include "pios_iap.h" #include "ssp.h" #include "fifo_buffer.h" +#include "pios_com_msg.h" /* Prototype of PIOS_Board_Init() function */ extern void PIOS_Board_Init(void); extern void FLASH_Download(); @@ -90,7 +91,7 @@ uint8_t JumpToApp = FALSE; uint8_t GO_dfu = FALSE; uint8_t USB_connected = FALSE; uint8_t User_DFU_request = FALSE; -static uint8_t mReceive_Buffer[64]; +static uint8_t mReceive_Buffer[63]; /* Private function prototypes -----------------------------------------------*/ uint32_t LedPWM(uint32_t pwm_period, uint32_t pwm_sweep_steps, uint32_t count); uint8_t processRX(); @@ -249,7 +250,7 @@ uint32_t LedPWM(uint32_t pwm_period, uint32_t pwm_sweep_steps, uint32_t count) { uint8_t processRX() { if (ProgPort == Usb) { - if (PIOS_COM_ReceiveBuffer(PIOS_COM_TELEM_USB, mReceive_Buffer, 63, 0) == 63) { + if (PIOS_COM_MSG_Receive(PIOS_COM_TELEM_USB, mReceive_Buffer, sizeof(mReceive_Buffer))) { processComand(mReceive_Buffer); } } else if (ProgPort == Serial) { diff --git a/flight/Bootloaders/OpenPilot/op_dfu.c b/flight/Bootloaders/OpenPilot/op_dfu.c index bc62fff6c..ad5836ad7 100644 --- a/flight/Bootloaders/OpenPilot/op_dfu.c +++ b/flight/Bootloaders/OpenPilot/op_dfu.c @@ -30,6 +30,7 @@ #include "pios.h" #include "op_dfu.h" #include "pios_bl_helper.h" +#include "pios_com_msg.h" #include #include "pios_opahrs.h" #include "ssp.h" @@ -299,7 +300,6 @@ void processComand(uint8_t *xReceive_Buffer) { ++Next_Packet; } else { - DeviceState = wrong_packet_received; Aditionals = Count; } @@ -508,6 +508,7 @@ uint32_t baseOfAdressType(DFUTransfer type) { return currentDevice.startOfUserCode + currentDevice.sizeOfCode; break; default: + return 0; } } @@ -550,11 +551,7 @@ uint32_t CalcFirmCRC() { } void sendData(uint8_t * buf, uint16_t size) { if (ProgPort == Usb) { - - PIOS_COM_SendBuffer(PIOS_COM_TELEM_USB, buf, size); - if (DeviceState == downloading) - PIOS_DELAY_WaitmS(10); - + PIOS_COM_MSG_Send(PIOS_COM_TELEM_USB, buf, size); } else if (ProgPort == Serial) { ssp_SendData(&ssp_port, buf, size); } diff --git a/flight/Bootloaders/OpenPilot/pios_board.c b/flight/Bootloaders/OpenPilot/pios_board.c index a2630815a..707600b4d 100644 --- a/flight/Bootloaders/OpenPilot/pios_board.c +++ b/flight/Bootloaders/OpenPilot/pios_board.c @@ -184,12 +184,6 @@ const struct pios_usart_cfg pios_usart_telem_cfg = { #include "pios_com_priv.h" -#define PIOS_COM_TELEM_USB_RX_BUF_LEN 192 -#define PIOS_COM_TELEM_USB_TX_BUF_LEN 192 - -static uint8_t pios_com_telem_usb_rx_buffer[PIOS_COM_TELEM_USB_RX_BUF_LEN]; -static uint8_t pios_com_telem_usb_tx_buffer[PIOS_COM_TELEM_USB_TX_BUF_LEN]; - #define PIOS_COM_TELEM_RF_RX_BUF_LEN 192 #define PIOS_COM_TELEM_RF_TX_BUF_LEN 192 @@ -198,6 +192,16 @@ static uint8_t pios_com_telem_rf_tx_buffer[PIOS_COM_TELEM_RF_TX_BUF_LEN]; #endif /* PIOS_INCLUDE_COM */ +// *********************************************************************************** + +#if defined(PIOS_INCLUDE_COM_MSG) + +#include + +#endif /* PIOS_INCLUDE_COM_MSG */ + +// *********************************************************************************** + #if defined(PIOS_INCLUDE_USB) #include "pios_usb_priv.h" @@ -270,17 +274,15 @@ void PIOS_Board_Init(void) { if (PIOS_USB_Init(&pios_usb_id, &pios_usb_main_cfg)) { PIOS_Assert(0); } -#if defined(PIOS_INCLUDE_USB_HID) && defined(PIOS_INCLUDE_COM) +#if defined(PIOS_INCLUDE_USB_HID) && defined(PIOS_INCLUDE_COM_MSG) uint32_t pios_usb_hid_id; if (PIOS_USB_HID_Init(&pios_usb_hid_id, &pios_usb_hid_cfg, pios_usb_id)) { PIOS_Assert(0); } - if (PIOS_COM_Init(&pios_com_telem_usb_id, &pios_usb_hid_com_driver, pios_usb_hid_id, - pios_com_telem_usb_rx_buffer, sizeof(pios_com_telem_usb_rx_buffer), - pios_com_telem_usb_tx_buffer, sizeof(pios_com_telem_usb_tx_buffer))) { + if (PIOS_COM_MSG_Init(&pios_com_telem_usb_id, &pios_usb_hid_com_driver, pios_usb_hid_id)) { PIOS_Assert(0); } -#endif /* PIOS_INCLUDE_USB_HID && PIOS_INCLUDE_COM */ +#endif /* PIOS_INCLUDE_USB_HID && PIOS_INCLUDE_COM_MSG */ #endif /* PIOS_INCLUDE_USB */ diff --git a/flight/Bootloaders/PipXtreme/Makefile b/flight/Bootloaders/PipXtreme/Makefile index af6469ae0..9bad695f7 100644 --- a/flight/Bootloaders/PipXtreme/Makefile +++ b/flight/Bootloaders/PipXtreme/Makefile @@ -115,7 +115,7 @@ SRC += $(PIOSCOMMON)/pios_usb_desc_hid_only.c ## PIOS Hardware (Common) SRC += $(PIOSCOMMON)/pios_board_info.c -SRC += $(PIOSCOMMON)/pios_com.c +SRC += $(PIOSCOMMON)/pios_com_msg.c SRC += $(PIOSCOMMON)/pios_bl_helper.c SRC += $(PIOSCOMMON)/pios_iap.c SRC += $(PIOSCOMMON)/printf-stdarg.c diff --git a/flight/Bootloaders/PipXtreme/inc/pios_config.h b/flight/Bootloaders/PipXtreme/inc/pios_config.h index 0c2cc7877..8084f9c0a 100644 --- a/flight/Bootloaders/PipXtreme/inc/pios_config.h +++ b/flight/Bootloaders/PipXtreme/inc/pios_config.h @@ -38,7 +38,7 @@ #define PIOS_INCLUDE_SYS #define PIOS_INCLUDE_USB #define PIOS_INCLUDE_USB_HID -#define PIOS_INCLUDE_COM +#define PIOS_INCLUDE_COM_MSG #define PIOS_INCLUDE_GPIO //#define DEBUG_SSP diff --git a/flight/Bootloaders/PipXtreme/main.c b/flight/Bootloaders/PipXtreme/main.c index f09881d42..4923f8fc1 100644 --- a/flight/Bootloaders/PipXtreme/main.c +++ b/flight/Bootloaders/PipXtreme/main.c @@ -32,6 +32,7 @@ #include "usb_lib.h" #include "pios_iap.h" #include "fifo_buffer.h" +#include "pios_com_msg.h" /* Prototype of PIOS_Board_Init() function */ extern void PIOS_Board_Init(void); extern void FLASH_Download(); @@ -62,7 +63,7 @@ uint8_t JumpToApp = FALSE; uint8_t GO_dfu = FALSE; uint8_t USB_connected = FALSE; uint8_t User_DFU_request = FALSE; -static uint8_t mReceive_Buffer[64]; +static uint8_t mReceive_Buffer[63]; /* Private function prototypes -----------------------------------------------*/ uint32_t LedPWM(uint32_t pwm_period, uint32_t pwm_sweep_steps, uint32_t count); uint8_t processRX(); @@ -204,7 +205,7 @@ uint32_t LedPWM(uint32_t pwm_period, uint32_t pwm_sweep_steps, uint32_t count) { } uint8_t processRX() { - if (PIOS_COM_ReceiveBuffer(PIOS_COM_TELEM_USB, mReceive_Buffer, 63, 0) == 63) { + if (PIOS_COM_MSG_Receive(PIOS_COM_TELEM_USB, mReceive_Buffer, sizeof(mReceive_Buffer))) { processComand(mReceive_Buffer); } return TRUE; diff --git a/flight/Bootloaders/PipXtreme/op_dfu.c b/flight/Bootloaders/PipXtreme/op_dfu.c index 770ce8237..cdc2d82b8 100644 --- a/flight/Bootloaders/PipXtreme/op_dfu.c +++ b/flight/Bootloaders/PipXtreme/op_dfu.c @@ -30,6 +30,7 @@ #include "pios.h" #include "op_dfu.h" #include "pios_bl_helper.h" +#include "pios_com_msg.h" #include /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ @@ -102,33 +103,9 @@ void DataDownload(DownloadAction action) { currentProgrammingDestination)) { DeviceState = Last_operation_failed; } - /* - switch (currentProgrammingDestination) { - case Remote_flash_via_spi: - if (downType == Descript) { - SendBuffer[6 + (x * 4)] - = spi_dev_desc[(uint8_t) partoffset]; - SendBuffer[7 + (x * 4)] = spi_dev_desc[(uint8_t) partoffset - + 1]; - SendBuffer[8 + (x * 4)] = spi_dev_desc[(uint8_t) partoffset - + 2]; - SendBuffer[9 + (x * 4)] = spi_dev_desc[(uint8_t) partoffset - + 3]; - } - break; - case Self_flash: - SendBuffer[6 + (x * 4)] = *PIOS_BL_HELPER_FLASH_If_Read(offset); - SendBuffer[7 + (x * 4)] = *PIOS_BL_HELPER_FLASH_If_Read(offset + 1); - SendBuffer[8 + (x * 4)] = *PIOS_BL_HELPER_FLASH_If_Read(offset + 2); - SendBuffer[9 + (x * 4)] = *PIOS_BL_HELPER_FLASH_If_Read(offset + 3); - break; - } - */ } - //PIOS USB_SIL_Write(EP1_IN, (uint8_t*) SendBuffer, 64); downPacketCurrent = downPacketCurrent + 1; if (downPacketCurrent > downPacketTotal - 1) { - // STM_EVAL_LEDOn(LED2); DeviceState = Last_operation_Success; Aditionals = (uint32_t) Download; } @@ -322,7 +299,6 @@ void processComand(uint8_t *xReceive_Buffer) { Buffer[15] = devicesTable[Data0 - 1].devID; } sendData(Buffer + 1, 63); - //PIOS_COM_SendBuffer(PIOS_COM_TELEM_USB, Buffer + 1, 63);//FIX+1 break; case JumpFW: FLASH_Lock(); @@ -474,9 +450,7 @@ uint32_t CalcFirmCRC() { } void sendData(uint8_t * buf, uint16_t size) { - PIOS_COM_SendBuffer(PIOS_COM_TELEM_USB, buf, size); - if (DeviceState == downloading) - PIOS_DELAY_WaitmS(10); + PIOS_COM_MSG_Send(PIOS_COM_TELEM_USB, buf, size); } bool flash_read(uint8_t * buffer, uint32_t adr, DFUProgType type) { diff --git a/flight/Bootloaders/PipXtreme/pios_board.c b/flight/Bootloaders/PipXtreme/pios_board.c index abf079340..3c06d820b 100644 --- a/flight/Bootloaders/PipXtreme/pios_board.c +++ b/flight/Bootloaders/PipXtreme/pios_board.c @@ -27,17 +27,11 @@ // *********************************************************************************** -#if defined(PIOS_INCLUDE_COM) +#if defined(PIOS_INCLUDE_COM_MSG) -#include +#include -#define PIOS_COM_TELEM_USB_RX_BUF_LEN 192 -#define PIOS_COM_TELEM_USB_TX_BUF_LEN 192 - -static uint8_t pios_com_telem_usb_rx_buffer[PIOS_COM_TELEM_USB_RX_BUF_LEN]; -static uint8_t pios_com_telem_usb_tx_buffer[PIOS_COM_TELEM_USB_TX_BUF_LEN]; - -#endif /* PIOS_INCLUDE_COM */ +#endif /* PIOS_INCLUDE_COM_MSG */ // *********************************************************************************** @@ -98,17 +92,16 @@ void PIOS_Board_Init(void) { if (PIOS_USB_Init(&pios_usb_id, &pios_usb_main_cfg)) { PIOS_Assert(0); } -#if defined(PIOS_INCLUDE_USB_HID) && defined(PIOS_INCLUDE_COM) - uint32_t pios_usb_com_id; - if (PIOS_USB_HID_Init(&pios_usb_com_id, &pios_usb_hid_cfg, pios_usb_id)) { +#if defined(PIOS_INCLUDE_USB_HID) && defined(PIOS_INCLUDE_COM_MSG) + uint32_t pios_usb_hid_id; + if (PIOS_USB_HID_Init(&pios_usb_hid_id, &pios_usb_hid_cfg, pios_usb_id)) { PIOS_Assert(0); } - if (PIOS_COM_Init(&pios_com_telem_usb_id, &pios_usb_hid_com_driver, pios_usb_com_id, - pios_com_telem_usb_rx_buffer, sizeof(pios_com_telem_usb_rx_buffer), - pios_com_telem_usb_tx_buffer, sizeof(pios_com_telem_usb_tx_buffer))) { + if (PIOS_COM_MSG_Init(&pios_com_telem_usb_id, &pios_usb_hid_com_driver, pios_usb_hid_id)) { PIOS_Assert(0); } -#endif /* PIOS_INCLUDE_USB_HID && PIOS_INCLUDE_COM */ +#endif /* PIOS_INCLUDE_USB_HID && PIOS_INCLUDE_COM_MSG */ + #endif /* PIOS_INCLUDE_USB */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE);//TODO Tirar diff --git a/flight/PiOS/Common/pios_com_msg.c b/flight/PiOS/Common/pios_com_msg.c new file mode 100644 index 000000000..c1cebe1d9 --- /dev/null +++ b/flight/PiOS/Common/pios_com_msg.c @@ -0,0 +1,185 @@ +/** + ****************************************************************************** + * @addtogroup PIOS PIOS Core hardware abstraction layer + * @{ + * @addtogroup PIOS_COM COM MSG layer functions + * @brief Hardware communication layer + * @{ + * + * @file pios_com_msg.c + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief COM MSG layer functions + * @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 + */ + +/* Project Includes */ +#include "pios.h" + +#if defined(PIOS_INCLUDE_COM_MSG) + +#include "pios_com.h" + +#define PIOS_COM_MSG_MAX_LEN 63 + +struct pios_com_msg_dev { + uint32_t lower_id; + const struct pios_com_driver * driver; + + uint8_t rx_msg_buffer[PIOS_COM_MSG_MAX_LEN]; + volatile bool rx_msg_full; + + uint8_t tx_msg_buffer[PIOS_COM_MSG_MAX_LEN]; + volatile bool tx_msg_full; +}; + +static struct pios_com_msg_dev com_msg_dev; + +static uint16_t PIOS_COM_MSG_TxOutCallback(uint32_t context, uint8_t * buf, uint16_t buf_len, uint16_t * headroom, bool * need_yield); +static uint16_t PIOS_COM_MSG_RxInCallback(uint32_t context, uint8_t * buf, uint16_t buf_len, uint16_t * headroom, bool * need_yield); + +int32_t PIOS_COM_MSG_Init(uint32_t * com_id, const struct pios_com_driver * driver, uint32_t lower_id) +{ + PIOS_Assert(com_id); + PIOS_Assert(driver); + + PIOS_Assert(driver->bind_tx_cb); + PIOS_Assert(driver->bind_rx_cb); + + struct pios_com_msg_dev * com_dev = &com_msg_dev; + + com_dev->driver = driver; + com_dev->lower_id = lower_id; + + com_dev->rx_msg_full = false; + (com_dev->driver->bind_rx_cb)(lower_id, PIOS_COM_MSG_RxInCallback, (uint32_t)com_dev); + (com_dev->driver->rx_start)(com_dev->lower_id, sizeof(com_dev->rx_msg_buffer)); + + com_dev->tx_msg_full = false; + (com_dev->driver->bind_tx_cb)(lower_id, PIOS_COM_MSG_TxOutCallback, (uint32_t)com_dev); + + *com_id = (uint32_t)com_dev; + return(0); +} + +static uint16_t PIOS_COM_MSG_TxOutCallback(uint32_t context, uint8_t * buf, uint16_t buf_len, uint16_t * headroom, bool * need_yield) +{ + struct pios_com_msg_dev * com_dev = (struct pios_com_msg_dev *)context; + + PIOS_Assert(buf); + PIOS_Assert(buf_len); + + uint16_t bytes_from_fifo = 0; + + if (com_dev->tx_msg_full && (buf_len >= sizeof(com_dev->tx_msg_buffer))) { + /* Room for an entire message, send it */ + memcpy(buf, com_dev->tx_msg_buffer, sizeof(com_dev->tx_msg_buffer)); + bytes_from_fifo = sizeof(com_dev->tx_msg_buffer); + com_dev->tx_msg_full = false; + } + + if (headroom) { + if (com_dev->tx_msg_full) { + *headroom = sizeof(com_dev->tx_msg_buffer); + } else { + *headroom = 0; + } + } + + return (bytes_from_fifo); +} + +static uint16_t PIOS_COM_MSG_RxInCallback(uint32_t context, uint8_t * buf, uint16_t buf_len, uint16_t * headroom, bool * need_yield) +{ + struct pios_com_msg_dev * com_dev = (struct pios_com_msg_dev *)context; + + uint16_t bytes_into_fifo = 0; + + if (!com_dev->rx_msg_full && (buf_len >= sizeof(com_dev->rx_msg_buffer))) { + memcpy(com_dev->rx_msg_buffer, buf, sizeof(com_dev->rx_msg_buffer)); + bytes_into_fifo = sizeof(com_dev->rx_msg_buffer); + com_dev->rx_msg_full = true; + } + + if (headroom) { + if (!com_dev->rx_msg_full) { + *headroom = sizeof(com_dev->rx_msg_buffer); + } else { + *headroom = 0; + } + } + + return (bytes_into_fifo); +} + +int32_t PIOS_COM_MSG_Send(uint32_t com_id, const uint8_t *msg, uint16_t msg_len) +{ + PIOS_Assert(msg); + PIOS_Assert(msg_len); + + struct pios_com_msg_dev * com_dev = (struct pios_com_msg_dev *)com_id; + + PIOS_Assert(msg_len == sizeof(com_dev->tx_msg_buffer)); + + /* Wait forever for room in the tx buffer */ + while (com_dev->tx_msg_full) { + /* Kick the transmitter while we wait */ + if (com_dev->driver->tx_start) { + (com_dev->driver->tx_start)(com_dev->lower_id, sizeof(com_dev->tx_msg_buffer)); + } + } + + memcpy((void *) com_dev->tx_msg_buffer, msg, msg_len); + com_dev->tx_msg_full = true; + + /* Kick the transmitter now that we've queued our message */ + if (com_dev->driver->tx_start) { + (com_dev->driver->tx_start)(com_dev->lower_id, sizeof(com_dev->tx_msg_buffer)); + } + + return 0; +} + +uint16_t PIOS_COM_MSG_Receive(uint32_t com_id, uint8_t * msg, uint16_t msg_len) +{ + PIOS_Assert(msg); + PIOS_Assert(msg_len); + + struct pios_com_msg_dev * com_dev = (struct pios_com_msg_dev *)com_id; + + PIOS_Assert(msg_len == sizeof(com_dev->rx_msg_buffer)); + + if (!com_dev->rx_msg_full) { + /* There's room in our buffer, kick the receiver */ + (com_dev->driver->rx_start)(com_dev->lower_id, sizeof(com_dev->rx_msg_buffer)); + } else { + memcpy(msg, com_dev->rx_msg_buffer, msg_len); + com_dev->rx_msg_full = false; + + return msg_len; + } + + return 0; +} + +#endif /* PIOS_INCLUDE_COM_MSG */ + +/** + * @} + * @} + */ diff --git a/flight/PiOS/STM32F10x/pios_usb_hid.c b/flight/PiOS/STM32F10x/pios_usb_hid.c index 11eb988c1..e70c93046 100644 --- a/flight/PiOS/STM32F10x/pios_usb_hid.c +++ b/flight/PiOS/STM32F10x/pios_usb_hid.c @@ -207,9 +207,15 @@ static void PIOS_USB_HID_RxStart(uint32_t usbhid_id, uint16_t rx_bytes_avail) { } // If endpoint was stalled and there is now space make it valid +#ifdef PIOS_USB_BOARD_BL_HID_HAS_NO_LENGTH_BYTE + uint16_t max_payload_length = PIOS_USB_BOARD_HID_DATA_LENGTH - 1; +#else + uint16_t max_payload_length = PIOS_USB_BOARD_HID_DATA_LENGTH - 2; +#endif + PIOS_IRQ_Disable(); if ((GetEPRxStatus(usb_hid_dev->cfg->data_rx_ep) != EP_RX_VALID) && - (rx_bytes_avail >= PIOS_USB_BOARD_HID_DATA_LENGTH)) { + (rx_bytes_avail >= max_payload_length)) { SetEPRxStatus(usb_hid_dev->cfg->data_rx_ep, EP_RX_VALID); } PIOS_IRQ_Enable(); @@ -328,7 +334,15 @@ static void PIOS_USB_HID_EP_OUT_Callback(void) &headroom, &need_yield); #endif - if (headroom >= PIOS_USB_BOARD_HID_DATA_LENGTH) { + +#ifdef PIOS_USB_BOARD_BL_HID_HAS_NO_LENGTH_BYTE + uint16_t max_payload_length = PIOS_USB_BOARD_HID_DATA_LENGTH - 1; +#else + uint16_t max_payload_length = PIOS_USB_BOARD_HID_DATA_LENGTH - 2; +#endif + + if (headroom >= max_payload_length) { + /* We have room for a maximum length message */ SetEPRxStatus(usb_hid_dev->cfg->data_rx_ep, EP_RX_VALID); } else { diff --git a/flight/PiOS/inc/pios_com.h b/flight/PiOS/inc/pios_com.h index 19abb2f27..10591f116 100644 --- a/flight/PiOS/inc/pios_com.h +++ b/flight/PiOS/inc/pios_com.h @@ -31,6 +31,9 @@ #ifndef PIOS_COM_H #define PIOS_COM_H +#include /* uint*_t */ +#include /* bool */ + typedef uint16_t (*pios_com_callback)(uint32_t context, uint8_t * buf, uint16_t buf_len, uint16_t * headroom, bool * task_woken); struct pios_com_driver { @@ -43,7 +46,6 @@ struct pios_com_driver { }; /* Public Functions */ -extern int32_t PIOS_COM_Init(uint32_t * com_id, const struct pios_com_driver * driver, uint32_t lower_id, uint8_t * rx_buffer, uint16_t rx_buffer_len, uint8_t * tx_buffer, uint16_t tx_buffer_len); extern int32_t PIOS_COM_ChangeBaud(uint32_t com_id, uint32_t baud); extern int32_t PIOS_COM_SendCharNonBlocking(uint32_t com_id, char c); extern int32_t PIOS_COM_SendChar(uint32_t com_id, char c); diff --git a/flight/PiOS/inc/pios_com_msg.h b/flight/PiOS/inc/pios_com_msg.h new file mode 100644 index 000000000..1caaeabeb --- /dev/null +++ b/flight/PiOS/inc/pios_com_msg.h @@ -0,0 +1,45 @@ +/** + ****************************************************************************** + * @addtogroup PIOS PIOS Core hardware abstraction layer + * @{ + * @addtogroup PIOS_COM COM MSG layer functions + * @brief Hardware communication layer + * @{ + * + * @file pios_com_msg.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief COM MSG layer functions header + * @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_MSG_H +#define PIOS_COM_MSG_H + +#include /* uint*_t */ + +/* Public Functions */ +extern int32_t PIOS_COM_MSG_Send(uint32_t com_id, const uint8_t *msg, uint16_t msg_len); +extern uint16_t PIOS_COM_MSG_Receive(uint32_t com_id, uint8_t * buf, uint16_t buf_len); + +#endif /* PIOS_COM_MSG_H */ + +/** + * @} + * @} + */ diff --git a/flight/PiOS/inc/pios_com_msg_priv.h b/flight/PiOS/inc/pios_com_msg_priv.h new file mode 100644 index 000000000..20992de47 --- /dev/null +++ b/flight/PiOS/inc/pios_com_msg_priv.h @@ -0,0 +1,44 @@ +/** + ****************************************************************************** + * @addtogroup PIOS PIOS Core hardware abstraction layer + * @{ + * @addtogroup PIOS_COM COM MSG layer functions + * @brief Hardware communication layer + * @{ + * + * @file pios_com_msg_priv.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief COM MSG 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_MSG_PRIV_H +#define PIOS_COM_MSG_PRIV_H + +#include /* uint*_t */ +#include "pios_com_priv.h" /* struct pios_com_driver */ + +extern int32_t PIOS_COM_MSG_Init(uint32_t * com_id, const struct pios_com_driver * driver, uint32_t lower_id); + +#endif /* PIOS_COM_MSG_PRIV_H */ + +/** + * @} + * @} + */ diff --git a/flight/PiOS/inc/pios_com_priv.h b/flight/PiOS/inc/pios_com_priv.h index 7bd9a9be0..c39522f79 100644 --- a/flight/PiOS/inc/pios_com_priv.h +++ b/flight/PiOS/inc/pios_com_priv.h @@ -31,6 +31,8 @@ #ifndef PIOS_COM_PRIV_H #define PIOS_COM_PRIV_H +extern int32_t PIOS_COM_Init(uint32_t * com_id, const struct pios_com_driver * driver, uint32_t lower_id, uint8_t * rx_buffer, uint16_t rx_buffer_len, uint8_t * tx_buffer, uint16_t tx_buffer_len); + #endif /* PIOS_COM_PRIV_H */ /**