diff --git a/flight/Bootloaders/Revolution/Makefile b/flight/Bootloaders/Revolution/Makefile index f1fd72830..070d1dfea 100644 --- a/flight/Bootloaders/Revolution/Makefile +++ b/flight/Bootloaders/Revolution/Makefile @@ -71,6 +71,7 @@ HWDEFSINC = ../../board_hw_defs/$(BOARD_NAME) SRC += main.c SRC += pios_board.c SRC += pios_usb_board_data.c +SRC += op_dfu.c ## PIOS Hardware (STM32F4xx) include $(PIOS)/STM32F4xx/library.mk diff --git a/flight/Bootloaders/Revolution/inc/common.h b/flight/Bootloaders/Revolution/inc/common.h new file mode 100644 index 000000000..9ecff4079 --- /dev/null +++ b/flight/Bootloaders/Revolution/inc/common.h @@ -0,0 +1,115 @@ +/** + ****************************************************************************** + * @addtogroup CopterControlBL CopterControl BootLoader + * @brief These files contain the code to the CopterControl Bootloader. + * + * @{ + * @file common.c + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief This file contains various common defines for the BootLoader + * @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 COMMON_H_ +#define COMMON_H_ + +//#include "board.h" + +typedef enum { + start, keepgoing, +} DownloadAction; + +/**************************************************/ +/* OP_DFU states */ +/**************************************************/ + +typedef enum { + DFUidle, //0 + uploading, //1 + wrong_packet_received, //2 + too_many_packets, //3 + too_few_packets, //4 + Last_operation_Success, //5 + downloading, //6 + BLidle, //7 + Last_operation_failed, //8 + uploadingStarting, //9 + outsideDevCapabilities, //10 + CRC_Fail,//11 + failed_jump, +//12 +} DFUStates; +/**************************************************/ +/* OP_DFU commands */ +/**************************************************/ +typedef enum { + Reserved, //0 + Req_Capabilities, //1 + Rep_Capabilities, //2 + EnterDFU, //3 + JumpFW, //4 + Reset, //5 + Abort_Operation, //6 + Upload, //7 + Op_END, //8 + Download_Req, //9 + Download, //10 + Status_Request, //11 + Status_Rep +//12 +} DFUCommands; + +typedef enum { + High_Density, Medium_Density +} DeviceType; +/**************************************************/ +/* OP_DFU transfer types */ +/**************************************************/ +typedef enum { + FW, //0 + Descript +//2 +} DFUTransfer; +/**************************************************/ +/* OP_DFU transfer port */ +/**************************************************/ +typedef enum { + Usb, //0 + Serial +//2 +} DFUPort; +/**************************************************/ +/* OP_DFU programable programable HW types */ +/**************************************************/ +typedef enum { + Self_flash, //0 + Remote_flash_via_spi +//1 +} DFUProgType; +/**************************************************/ +/* OP_DFU programable sources */ +/**************************************************/ +#define USB 0 +#define SPI 1 + +#define DownloadDelay 100000 + +#define MAX_DEL_RETRYS 3 +#define MAX_WRI_RETRYS 3 + +#endif /* COMMON_H_ */ diff --git a/flight/Bootloaders/Revolution/inc/op_dfu.h b/flight/Bootloaders/Revolution/inc/op_dfu.h new file mode 100644 index 000000000..e031c3364 --- /dev/null +++ b/flight/Bootloaders/Revolution/inc/op_dfu.h @@ -0,0 +1,60 @@ +/** + ****************************************************************************** + * + * @file op_dfu.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief + * @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 + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __OP_DFU_H +#define __OP_DFU_H +#include "common.h" +/* Includes ------------------------------------------------------------------*/ +/* Exported types ------------------------------------------------------------*/ +typedef struct { + uint8_t programmingType; + uint8_t readWriteFlags; + uint32_t startOfUserCode; + uint32_t sizeOfCode; + uint8_t sizeOfDescription; + uint8_t BL_Version; + uint16_t devID; + DeviceType devType; + uint32_t FW_Crc; +} Device; + +/* Exported constants --------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +/* Exported define -----------------------------------------------------------*/ +#define COMMAND 0 +#define COUNT 1 +#define DATA 5 + +/* Exported functions ------------------------------------------------------- */ +void processComand(uint8_t *Receive_Buffer); +uint32_t baseOfAdressType(uint8_t type); +uint8_t isBiggerThanAvailable(uint8_t type, uint32_t size); +void OPDfuIni(uint8_t discover); +void DataDownload(DownloadAction); +bool flash_read(uint8_t * buffer, uint32_t adr, DFUProgType type); +#endif /* __OP_DFU_H */ + +/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/ diff --git a/flight/Bootloaders/Revolution/inc/pios_config.h b/flight/Bootloaders/Revolution/inc/pios_config.h index 469ea9035..dc71a1556 100644 --- a/flight/Bootloaders/Revolution/inc/pios_config.h +++ b/flight/Bootloaders/Revolution/inc/pios_config.h @@ -37,7 +37,7 @@ #define PIOS_INCLUDE_USB #define PIOS_INCLUDE_USB_HID #define PIOS_INCLUDE_COM_MSG -//#define PIOS_INCLUDE_BL_HELPER -//#define PIOS_INCLUDE_BL_HELPER_WRITE_SUPPORT +#define PIOS_INCLUDE_BL_HELPER +#define PIOS_INCLUDE_BL_HELPER_WRITE_SUPPORT #endif /* PIOS_CONFIG_H */ diff --git a/flight/Bootloaders/Revolution/main.c b/flight/Bootloaders/Revolution/main.c index e3748d3d8..b64ebc96a 100644 --- a/flight/Bootloaders/Revolution/main.c +++ b/flight/Bootloaders/Revolution/main.c @@ -1,17 +1,12 @@ /** ****************************************************************************** - * @addtogroup AHRS BOOTLOADER - * @brief The AHRS Modules perform - * - * @{ - * @addtogroup AHRS_BOOTLOADER_Main - * @brief Main function which does the hardware dependent stuff - * @{ - * + * @addtogroup RevolutionBL Revolution BootLoader + * @brief These files contain the code to the Revolution Bootloader. * + * @{ * @file main.c * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. - * @brief + * @brief This is the file with the main function of the Revolution BootLoader * @see The GNU Public License (GPL) Version 3 * *****************************************************************************/ @@ -30,42 +25,146 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ - -/* OpenPilot Includes */ -#include "ahrs_bl.h" +/* Bootloader Includes */ +#include #include -#include "pios_opahrs_proto.h" -#include "bl_fsm.h" /* lfsm_state */ -//#include "stm32f2xx_flash.h" - +#include +#include "op_dfu.h" +#include "pios_iap.h" +#include "fifo_buffer.h" +#include "pios_com_msg.h" +#include "pios_usbhook.h" /* PIOS_USBHOOK_* */ +/* Prototype of PIOS_Board_Init() function */ extern void PIOS_Board_Init(void); +extern void FLASH_Download(); +#define BSL_HOLD_STATE ((PIOS_USB_DETECT_GPIO_PORT->IDR & PIOS_USB_DETECT_GPIO_PIN) ? 0 : 1) -#define NSS_HOLD_STATE ((GPIOB->IDR & GPIO_Pin_12) ? 0 : 1) -enum bootloader_status boot_status; /* Private typedef -----------------------------------------------------------*/ -typedef void -(*pFunction)(void); +typedef void (*pFunction)(void); +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ pFunction Jump_To_Application; uint32_t JumpAddress; -/* Function Prototypes */ -//void -//process_spi_request(void); -void -jump_to_app(); -uint32_t Fw_crc; -/** - * @brief Bootloader Main function - */ + +/// LEDs PWM +uint32_t period1 = 5000; // 5 mS +uint32_t sweep_steps1 = 100; // * 5 mS -> 500 mS +uint32_t period2 = 5000; // 5 mS +uint32_t sweep_steps2 = 100; // * 5 mS -> 500 mS + + +//////////////////////////////////////// +uint8_t tempcount = 0; + +/* Extern variables ----------------------------------------------------------*/ +DFUStates DeviceState; +int16_t status = 0; +bool JumpToApp = false; +bool GO_dfu = false; +bool USB_connected = false; +bool User_DFU_request = false; +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(); +void jump_to_app(); + int main() { - /* Brings up System using CMSIS functions, enables the LEDs. */ - PIOS_SYS_Init(); + PIOS_SYS_Init(); PIOS_Board_Init(); + PIOS_IAP_Init(); - jump_to_app(); - return 0; + USB_connected = PIOS_USB_CheckAvailable(0); + + if (PIOS_IAP_CheckRequest() == true) { + PIOS_DELAY_WaitmS(1000); + User_DFU_request = true; + PIOS_IAP_ClearRequest(); + } + + GO_dfu = (USB_connected == true) || (User_DFU_request == true); + + if (GO_dfu == true) { + PIOS_Board_Init(); + if (User_DFU_request == true) + DeviceState = DFUidle; + else + DeviceState = BLidle; + } else + JumpToApp = true; + + uint32_t stopwatch = 0; + uint32_t prev_ticks = PIOS_DELAY_GetuS(); + while (true) { + /* Update the stopwatch */ + uint32_t elapsed_ticks = PIOS_DELAY_GetuSSince(prev_ticks); + prev_ticks += elapsed_ticks; + stopwatch += elapsed_ticks; + + if (JumpToApp == true) + jump_to_app(); + + switch (DeviceState) { + case Last_operation_Success: + case uploadingStarting: + case DFUidle: + period1 = 5000; + sweep_steps1 = 100; + PIOS_LED_Off(PIOS_LED_HEARTBEAT); + period2 = 0; + break; + case uploading: + period1 = 5000; + sweep_steps1 = 100; + period2 = 2500; + sweep_steps2 = 50; + break; + case downloading: + period1 = 2500; + sweep_steps1 = 50; + PIOS_LED_Off(PIOS_LED_HEARTBEAT); + period2 = 0; + break; + case BLidle: + period1 = 0; + PIOS_LED_On(PIOS_LED_HEARTBEAT); + period2 = 0; + break; + default://error + period1 = 5000; + sweep_steps1 = 100; + period2 = 5000; + sweep_steps2 = 100; + } + + if (period1 != 0) { + if (LedPWM(period1, sweep_steps1, stopwatch)) + PIOS_LED_On(PIOS_LED_HEARTBEAT); + else + PIOS_LED_Off(PIOS_LED_HEARTBEAT); + } else + PIOS_LED_On(PIOS_LED_HEARTBEAT); + + if (period2 != 0) { + if (LedPWM(period2, sweep_steps2, stopwatch)) + PIOS_LED_On(PIOS_LED_HEARTBEAT); + else + PIOS_LED_Off(PIOS_LED_HEARTBEAT); + } else + PIOS_LED_Off(PIOS_LED_HEARTBEAT); + + if (stopwatch > 50 * 1000 * 1000) + stopwatch = 0; + if ((stopwatch > 6 * 1000 * 1000) && (DeviceState + == BLidle)) + JumpToApp = true; + + processRX(); + DataDownload(start); + } } - void jump_to_app() { const struct pios_board_info * bdinfo = &pios_board_info_blob; @@ -76,8 +175,8 @@ void jump_to_app() { RCC_APB1PeriphResetCmd(0xffffffff, ENABLE); RCC_APB2PeriphResetCmd(0xffffffff, DISABLE); RCC_APB1PeriphResetCmd(0xffffffff, DISABLE); - //_SetCNTR(0); // clear interrupt mask - //_SetISTR(0); // clear all requests + + PIOS_USBHOOK_Deactivate(); JumpAddress = *(__IO uint32_t*) (bdinfo->fw_base + 4); Jump_To_Application = (pFunction) JumpAddress; @@ -85,7 +184,25 @@ void jump_to_app() { __set_MSP(*(__IO uint32_t*) bdinfo->fw_base); Jump_To_Application(); } else { - boot_status = jump_failed; + DeviceState = failed_jump; return; } } +uint32_t LedPWM(uint32_t pwm_period, uint32_t pwm_sweep_steps, uint32_t count) { + uint32_t curr_step = (count / pwm_period) % pwm_sweep_steps; /* 0 - pwm_sweep_steps */ + uint32_t pwm_duty = pwm_period * curr_step / pwm_sweep_steps; /* fraction of pwm_period */ + + uint32_t curr_sweep = (count / (pwm_period * pwm_sweep_steps)); /* ticks once per full sweep */ + if (curr_sweep & 1) { + pwm_duty = pwm_period - pwm_duty; /* reverse direction in odd sweeps */ + } + return ((count % pwm_period) > pwm_duty) ? 1 : 0; +} + +uint8_t processRX() { + if (PIOS_COM_MSG_Receive(PIOS_COM_TELEM_USB, mReceive_Buffer, sizeof(mReceive_Buffer))) { + processComand(mReceive_Buffer); + } + return true; +} + diff --git a/flight/Bootloaders/Revolution/op_dfu.c b/flight/Bootloaders/Revolution/op_dfu.c new file mode 100644 index 000000000..3aceedcfb --- /dev/null +++ b/flight/Bootloaders/Revolution/op_dfu.c @@ -0,0 +1,468 @@ +/** + ****************************************************************************** + * @addtogroup CopterControlBL CopterControl BootLoader + * @brief These files contain the code to the CopterControl Bootloader. + * + * @{ + * @file op_dfu.c + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief This file contains the DFU commands handling code + * @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 + */ + +/* Includes ------------------------------------------------------------------*/ +#include "pios.h" +#include +#include "op_dfu.h" +#include "pios_bl_helper.h" +#include "pios_com_msg.h" +#include +//programmable devices +Device devicesTable[10]; +uint8_t numberOfDevices = 0; + +DFUProgType currentProgrammingDestination; //flash, flash_trough spi +uint8_t currentDeviceCanRead; +uint8_t currentDeviceCanWrite; +Device currentDevice; + +uint8_t Buffer[64]; +uint8_t echoBuffer[64]; +uint8_t SendBuffer[64]; +uint8_t Command = 0; +uint8_t EchoReqFlag = 0; +uint8_t EchoAnsFlag = 0; +uint8_t StartFlag = 0; +uint32_t Aditionals = 0; +uint32_t SizeOfTransfer = 0; +uint32_t Expected_CRC = 0; +uint8_t SizeOfLastPacket = 0; +uint32_t Next_Packet = 0; +uint8_t TransferType; +uint32_t Count = 0; +uint32_t Data; +uint8_t Data0; +uint8_t Data1; +uint8_t Data2; +uint8_t Data3; +uint8_t offset = 0; +uint32_t aux; +//Download vars +uint32_t downSizeOfLastPacket = 0; +uint32_t downPacketTotal = 0; +uint32_t downPacketCurrent = 0; +DFUTransfer downType = 0; +/* Extern variables ----------------------------------------------------------*/ +extern DFUStates DeviceState; +extern uint8_t JumpToApp; +/* Private function prototypes -----------------------------------------------*/ +/* Private functions ---------------------------------------------------------*/ +void sendData(uint8_t * buf, uint16_t size); +uint32_t CalcFirmCRC(void); + +void DataDownload(DownloadAction action) { + if ((DeviceState == downloading)) { + + uint8_t packetSize; + uint32_t offset; + uint32_t partoffset; + SendBuffer[0] = 0x01; + SendBuffer[1] = Download; + SendBuffer[2] = downPacketCurrent >> 24; + SendBuffer[3] = downPacketCurrent >> 16; + SendBuffer[4] = downPacketCurrent >> 8; + SendBuffer[5] = downPacketCurrent; + if (downPacketCurrent == downPacketTotal - 1) { + packetSize = downSizeOfLastPacket; + } else { + packetSize = 14; + } + for (uint8_t x = 0; x < packetSize; ++x) { + partoffset = (downPacketCurrent * 14 * 4) + (x * 4); + offset = baseOfAdressType(downType) + partoffset; + if (!flash_read(SendBuffer + (6 + x * 4), offset, + currentProgrammingDestination)) { + DeviceState = Last_operation_failed; + } + } + downPacketCurrent = downPacketCurrent + 1; + if (downPacketCurrent > downPacketTotal - 1) { + DeviceState = Last_operation_Success; + Aditionals = (uint32_t) Download; + } + sendData(SendBuffer + 1, 63); + } +} +void processComand(uint8_t *xReceive_Buffer) { + + Command = xReceive_Buffer[COMMAND]; +#ifdef DEBUG_SSP + char str[63]= {0}; + sprintf(str,"Received COMMAND:%d|",Command); + PIOS_COM_SendString(PIOS_COM_TELEM_USB,str); +#endif + EchoReqFlag = (Command >> 7); + EchoAnsFlag = (Command >> 6) & 0x01; + StartFlag = (Command >> 5) & 0x01; + Count = xReceive_Buffer[COUNT] << 24; + Count += xReceive_Buffer[COUNT + 1] << 16; + Count += xReceive_Buffer[COUNT + 2] << 8; + Count += xReceive_Buffer[COUNT + 3]; + + Data = xReceive_Buffer[DATA] << 24; + Data += xReceive_Buffer[DATA + 1] << 16; + Data += xReceive_Buffer[DATA + 2] << 8; + Data += xReceive_Buffer[DATA + 3]; + Data0 = xReceive_Buffer[DATA]; + Data1 = xReceive_Buffer[DATA + 1]; + Data2 = xReceive_Buffer[DATA + 2]; + Data3 = xReceive_Buffer[DATA + 3]; + Command = Command & 0b00011111; + + if (EchoReqFlag == 1) { + memcpy(echoBuffer, xReceive_Buffer, 64); + } + switch (Command) { + case EnterDFU: + if (((DeviceState == BLidle) && (Data0 < numberOfDevices)) + || (DeviceState == DFUidle)) { + if (Data0 > 0) + OPDfuIni(true); + DeviceState = DFUidle; + currentProgrammingDestination = devicesTable[Data0].programmingType; + currentDeviceCanRead = devicesTable[Data0].readWriteFlags & 0x01; + currentDeviceCanWrite = devicesTable[Data0].readWriteFlags >> 1 + & 0x01; + currentDevice = devicesTable[Data0]; + uint8_t result = 0; + switch (currentProgrammingDestination) { + case Self_flash: + result = PIOS_BL_HELPER_FLASH_Ini(); + break; + case Remote_flash_via_spi: + result = true; + break; + default: + DeviceState = Last_operation_failed; + Aditionals = (uint16_t) Command; + } + if (result != 1) { + DeviceState = Last_operation_failed; + Aditionals = (uint32_t) Command; + } + } + break; + case Upload: + if ((DeviceState == DFUidle) || (DeviceState == uploading)) { + if ((StartFlag == 1) && (Next_Packet == 0)) { + TransferType = Data0; + SizeOfTransfer = Count; + Next_Packet = 1; + Expected_CRC = Data2 << 24; + Expected_CRC += Data3 << 16; + Expected_CRC += xReceive_Buffer[DATA + 4] << 8; + Expected_CRC += xReceive_Buffer[DATA + 5]; + SizeOfLastPacket = Data1; + + if (isBiggerThanAvailable(TransferType, (SizeOfTransfer - 1) + * 14 * 4 + SizeOfLastPacket * 4) == true) { + DeviceState = outsideDevCapabilities; + Aditionals = (uint32_t) Command; + } else { + uint8_t result = 1; + if (TransferType == FW) { + switch (currentProgrammingDestination) { + case Self_flash: + result = PIOS_BL_HELPER_FLASH_Start(); + break; + case Remote_flash_via_spi: + result = false; + break; + default: + break; + } + } + if (result != 1) { + DeviceState = Last_operation_failed; + Aditionals = (uint32_t) Command; + } else { + + DeviceState = uploading; + } + } + } else if ((StartFlag != 1) && (Next_Packet != 0)) { + if (Count > SizeOfTransfer) { + DeviceState = too_many_packets; + Aditionals = Count; + } else if (Count == Next_Packet - 1) { + uint8_t numberOfWords = 14; + if (Count == SizeOfTransfer - 1)//is this the last packet? + { + numberOfWords = SizeOfLastPacket; + } + uint8_t result = 0; + switch (currentProgrammingDestination) { + case Self_flash: + for (uint8_t x = 0; x < numberOfWords; ++x) { + offset = 4 * x; + Data = xReceive_Buffer[DATA + offset] << 24; + Data += xReceive_Buffer[DATA + 1 + offset] << 16; + Data += xReceive_Buffer[DATA + 2 + offset] << 8; + Data += xReceive_Buffer[DATA + 3 + offset]; + aux = baseOfAdressType(TransferType) + (uint32_t)( + Count * 14 * 4 + x * 4); + result = 0; + for (int retry = 0; retry < MAX_WRI_RETRYS; ++retry) { + if (result == 0) { + result = (FLASH_ProgramWord(aux, Data) + == FLASH_COMPLETE) ? 1 : 0; + } + } + } + break; + case Remote_flash_via_spi: + result = false; // No support for this for the PipX + break; + default: + result = 0; + break; + } + if (result != 1) { + DeviceState = Last_operation_failed; + Aditionals = (uint32_t) Command; + } + + ++Next_Packet; + } else { + DeviceState = wrong_packet_received; + Aditionals = Count; + } + } else { + DeviceState = Last_operation_failed; + Aditionals = (uint32_t) Command; + } + } + break; + case Req_Capabilities: + OPDfuIni(true); + Buffer[0] = 0x01; + Buffer[1] = Rep_Capabilities; + if (Data0 == 0) { + Buffer[2] = 0; + Buffer[3] = 0; + Buffer[4] = 0; + Buffer[5] = 0; + Buffer[6] = 0; + Buffer[7] = numberOfDevices; + uint16_t WRFlags = 0; + for (int x = 0; x < numberOfDevices; ++x) { + WRFlags = ((devicesTable[x].readWriteFlags << (x * 2)) + | WRFlags); + } + Buffer[8] = WRFlags >> 8; + Buffer[9] = WRFlags; + } else { + Buffer[2] = devicesTable[Data0 - 1].sizeOfCode >> 24; + Buffer[3] = devicesTable[Data0 - 1].sizeOfCode >> 16; + Buffer[4] = devicesTable[Data0 - 1].sizeOfCode >> 8; + Buffer[5] = devicesTable[Data0 - 1].sizeOfCode; + Buffer[6] = Data0; + Buffer[7] = devicesTable[Data0 - 1].BL_Version; + Buffer[8] = devicesTable[Data0 - 1].sizeOfDescription; + Buffer[9] = devicesTable[Data0 - 1].devID; + Buffer[10] = devicesTable[Data0 - 1].FW_Crc >> 24; + Buffer[11] = devicesTable[Data0 - 1].FW_Crc >> 16; + Buffer[12] = devicesTable[Data0 - 1].FW_Crc >> 8; + Buffer[13] = devicesTable[Data0 - 1].FW_Crc; + Buffer[14] = devicesTable[Data0 - 1].devID >> 8; + Buffer[15] = devicesTable[Data0 - 1].devID; + } + sendData(Buffer + 1, 63); + break; + case JumpFW: + if (Data == 0x5AFE) { + /* Force board into safe mode */ + PIOS_IAP_WriteBootCount(0xFFFF); + } + FLASH_Lock(); + JumpToApp = 1; + break; + case Reset: + PIOS_SYS_Reset(); + break; + case Abort_Operation: + Next_Packet = 0; + DeviceState = DFUidle; + break; + + case Op_END: + if (DeviceState == uploading) { + if (Next_Packet - 1 == SizeOfTransfer) { + Next_Packet = 0; + if ((TransferType != FW) || (Expected_CRC == CalcFirmCRC())) { + DeviceState = Last_operation_Success; + } else { + DeviceState = CRC_Fail; + } + } + if (Next_Packet - 1 < SizeOfTransfer) { + Next_Packet = 0; + DeviceState = too_few_packets; + } + } + break; + case Download_Req: +#ifdef DEBUG_SSP + sprintf(str,"COMMAND:DOWNLOAD_REQ 1 Status=%d|",DeviceState); + PIOS_COM_SendString(PIOS_COM_TELEM_USB,str); +#endif + if (DeviceState == DFUidle) { +#ifdef DEBUG_SSP + PIOS_COM_SendString(PIOS_COM_TELEM_USB,"COMMAND:DOWNLOAD_REQ 1|"); +#endif + downType = Data0; + downPacketTotal = Count; + downSizeOfLastPacket = Data1; + if (isBiggerThanAvailable(downType, (downPacketTotal - 1) * 14 * 4 + + downSizeOfLastPacket * 4) == 1) { + DeviceState = outsideDevCapabilities; + Aditionals = (uint32_t) Command; + + } else { + downPacketCurrent = 0; + DeviceState = downloading; + } + } else { + DeviceState = Last_operation_failed; + Aditionals = (uint32_t) Command; + } + break; + + case Status_Request: + Buffer[0] = 0x01; + Buffer[1] = Status_Rep; + if (DeviceState == wrong_packet_received) { + Buffer[2] = Aditionals >> 24; + Buffer[3] = Aditionals >> 16; + Buffer[4] = Aditionals >> 8; + Buffer[5] = Aditionals; + } else { + Buffer[2] = 0; + Buffer[3] = ((uint16_t) Aditionals) >> 8; + Buffer[4] = ((uint16_t) Aditionals); + Buffer[5] = 0; + } + Buffer[6] = DeviceState; + Buffer[7] = 0; + Buffer[8] = 0; + Buffer[9] = 0; + sendData(Buffer + 1, 63); + if (DeviceState == Last_operation_Success) { + DeviceState = DFUidle; + } + break; + case Status_Rep: + + break; + + } + if (EchoReqFlag == 1) { + echoBuffer[0] = echoBuffer[0] | (1 << 6); + sendData(echoBuffer, 63); + } + return; +} +void OPDfuIni(uint8_t discover) { + const struct pios_board_info * bdinfo = &pios_board_info_blob; + Device dev; + + dev.programmingType = Self_flash; + dev.readWriteFlags = (BOARD_READABLE | (BOARD_WRITABLE << 1)); + dev.startOfUserCode = bdinfo->fw_base; + dev.sizeOfCode = bdinfo->fw_size; + dev.sizeOfDescription = bdinfo->desc_size; + dev.BL_Version = bdinfo->bl_rev; + dev.FW_Crc = CalcFirmCRC(); + dev.devID = (bdinfo->board_type << 8) | (bdinfo->board_rev); + dev.devType = bdinfo->hw_type; + numberOfDevices = 1; + devicesTable[0] = dev; + if (discover) { + //TODO check other devices trough spi or whatever + } +} +uint32_t baseOfAdressType(DFUTransfer type) { + switch (type) { + case FW: + return currentDevice.startOfUserCode; + break; + case Descript: + return currentDevice.startOfUserCode + currentDevice.sizeOfCode; + break; + default: + + return 0; + } +} +uint8_t isBiggerThanAvailable(DFUTransfer type, uint32_t size) { + switch (type) { + case FW: + return (size > currentDevice.sizeOfCode) ? 1 : 0; + break; + case Descript: + return (size > currentDevice.sizeOfDescription) ? 1 : 0; + break; + default: + return true; + } +} + +uint32_t CalcFirmCRC() { + switch (currentProgrammingDestination) { + case Self_flash: + return PIOS_BL_HELPER_CRC_Memory_Calc(); + break; + case Remote_flash_via_spi: + return 0; + break; + default: + return 0; + break; + } + +} +void sendData(uint8_t * buf, uint16_t size) { + PIOS_COM_MSG_Send(PIOS_COM_TELEM_USB, buf, size); +} + +bool flash_read(uint8_t * buffer, uint32_t adr, DFUProgType type) { + switch (type) { + case Remote_flash_via_spi: + return false; // We should not get this for the PipX + break; + case Self_flash: + for (uint8_t x = 0; x < 4; ++x) { + buffer[x] = *PIOS_BL_HELPER_FLASH_If_Read(adr + x); + } + return true; + break; + default: + return false; + } +} diff --git a/flight/Bootloaders/Revolution/pios_board.c b/flight/Bootloaders/Revolution/pios_board.c index a89f83103..143124ade 100644 --- a/flight/Bootloaders/Revolution/pios_board.c +++ b/flight/Bootloaders/Revolution/pios_board.c @@ -23,6 +23,13 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +/* Pull in the board-specific static HW definitions. + * Including .c files is a bit ugly but this allows all of + * the HW definitions to be const and static to limit their + * scope. + * + * NOTE: THIS IS THE ONLY PLACE THAT SHOULD EVER INCLUDE THIS FILE + */ #include "board_hw_defs.c" #include @@ -39,10 +46,11 @@ void PIOS_Board_Init() { /* Delay system */ PIOS_DELAY_Init(); +#if defined(PIOS_INCLUDE_LED) PIOS_LED_Init(&pios_led_cfg); +#endif /* PIOS_INCLUDE_LED */ - -#if 0 && defined(PIOS_INCLUDE_USB) +#if defined(PIOS_INCLUDE_USB) /* Initialize board specific USB data */ PIOS_USB_BOARD_DATA_Init(); diff --git a/flight/PiOS/Boards/STM32F4xx_Revolution.h b/flight/PiOS/Boards/STM32F4xx_Revolution.h index 098fead57..000cc8d96 100644 --- a/flight/PiOS/Boards/STM32F4xx_Revolution.h +++ b/flight/PiOS/Boards/STM32F4xx_Revolution.h @@ -30,7 +30,7 @@ #ifndef STM3210E_INS_H_ #define STM3210E_INS_H_ - +#include //------------------------ // Timers and Channels Used @@ -68,8 +68,8 @@ TIM8 | | | | //------------------------ // BOOTLOADER_SETTINGS //------------------------ -#define BOARD_READABLE TRUE -#define BOARD_WRITABLE TRUE +#define BOARD_READABLE true +#define BOARD_WRITABLE true #define MAX_DEL_RETRYS 3