From 2fe266650fbfcfc01a147bfcc1790dc917287332 Mon Sep 17 00:00:00 2001 From: kokomojoe Date: Wed, 6 Oct 2010 02:06:13 +0000 Subject: [PATCH] OP-185 Flight - support files for Firmware IAP object git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@1891 ebee16cc-31ac-478f-84a7-5cbb03baadba --- .../Modules/FirmwareIAP/firmwareiap.c | 203 ++++++++++++++++++ .../Modules/FirmwareIAP/inc/firmwareiap.h | 32 +++ flight/OpenPilot/UAVObjects/firmwareiapobj.c | 111 ++++++++++ .../OpenPilot/UAVObjects/inc/firmwareiapobj.h | 101 +++++++++ flight/PiOS/pios.h | 1 + 5 files changed, 448 insertions(+) create mode 100644 flight/OpenPilot/Modules/FirmwareIAP/firmwareiap.c create mode 100644 flight/OpenPilot/Modules/FirmwareIAP/inc/firmwareiap.h create mode 100644 flight/OpenPilot/UAVObjects/firmwareiapobj.c create mode 100644 flight/OpenPilot/UAVObjects/inc/firmwareiapobj.h diff --git a/flight/OpenPilot/Modules/FirmwareIAP/firmwareiap.c b/flight/OpenPilot/Modules/FirmwareIAP/firmwareiap.c new file mode 100644 index 000000000..97ba2ed57 --- /dev/null +++ b/flight/OpenPilot/Modules/FirmwareIAP/firmwareiap.c @@ -0,0 +1,203 @@ +/** + ****************************************************************************** + * + * @file firmwareiap.c + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief In Application Programming module to support firmware upgrades by + * providing a means to enter 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 + */ +#include + +#include "openpilot.h" +#include "firmwareiap.h" +#include "firmwareiapobj.h" +#include "pios_iap.h" + +#include "STM3210E_OP.h" + +// Private constants +#define IAP_CMD_STEP_1 1122 +#define IAP_CMD_STEP_2 2233 +#define IAP_CMD_STEP_3 3344 + +#define IAP_CMD_CRC 100 +#define IAP_CMD_VERIFY 101 +#define IAP_CMD_VERSION 102 + +#define IAP_STATE_READY 0 +#define IAP_STATE_STEP_1 1 +#define IAP_STATE_STEP_2 2 + + +#define TICKS2MS(t) ((t)/portTICK_RATE_MS) +#define MS2TICKS(m) ((m)*portTICK_RATE_MS) + +const uint32_t iap_time_2_low_end = 500; +const uint32_t iap_time_2_high_end = 5000; +const uint32_t iap_time_3_low_end = 500; +const uint32_t iap_time_3_high_end = 5000; + +// Private types + +// Private variables +const static uint8_t version[] = { 0, 0, 1 }; +const static uint16_t SVN = 12345; + +// Private functions +static void FirmwareIAPCallback(UAVObjEvent* ev); + +static uint32_t iap_calc_crc(void); + +FirmwareIAPObjData data; + +static uint32_t get_time(void); +/** + * Initialise the module, called on startup. + * \returns 0 on success or -1 if initialisation failed + */ + +/*! + * \brief Performs object initialization functions. + * \param None. + * \return 0 - under all cases + * + * \note + * + */ + +int32_t FirmwareIAPInitialize() +{ + data.Version[0] = version[0]; + data.Version[1] = version[1]; + data.Version[2] = version[2]; + data.SVN = SVN; + data.crc = iap_calc_crc(); + FirmwareIAPObjSet( &data ); + FirmwareIAPObjConnectCallback( &FirmwareIAPCallback ); + return 0; +} + +/*! + * \brief FirmwareIAPCallback - callback function for firmware IAP requests + * \param[in] ev - pointer objevent + * \retval None. + * + * \note + * + */ +static void FirmwareIAPCallback(UAVObjEvent* ev) +{ + static uint32_t last_time = 0; + static uint16_t iap_state = IAP_STATE_READY; + uint32_t this_time; + uint32_t delta; + + if ( ev->obj == FirmwareIAPObjHandle() ) { + // Get the input object data + FirmwareIAPObjGet(&data); + this_time = get_time(); + delta = this_time - last_time; + last_time = this_time; + + switch(iap_state) { + case IAP_STATE_READY: + if( data.Command == IAP_CMD_STEP_1 ) { + iap_state = IAP_STATE_STEP_1; + } break; + case IAP_STATE_STEP_1: + if( data.Command == IAP_CMD_STEP_2 ) { + if( delta > iap_time_2_low_end && delta < iap_time_2_high_end ) { + iap_state = IAP_STATE_STEP_2; + } else { + iap_state = IAP_STATE_READY; + } + } else { + iap_state = IAP_STATE_READY; + } + break; + case IAP_STATE_STEP_2: + if( data.Command == IAP_CMD_STEP_3 ) { + if( delta > iap_time_3_low_end && delta < iap_time_3_high_end ) { + // we've met the three sequence of command numbers + // we've met the time requirements. + PIOS_IAP_SetRequest1(); + PIOS_IAP_SetRequest2(); + // goodbye cruel world... + PIOS_SYS_Reset(); + } else { + iap_state = IAP_STATE_READY; + } + } else { + iap_state = IAP_STATE_READY; + } + break; + default: + iap_state = IAP_STATE_READY; + break; + } + } +} + + + +// Returns number of milliseconds from the start of the kernel. + +/*! + * \brief Returns number of milliseconds from the start of the kernel + * \param None. + * \return number of milliseconds from the start of the kernel. + * + * \note + * + */ + +static uint32_t get_time(void) +{ + portTickType ticks; + + ticks = xTaskGetTickCount(); + + return TICKS2MS(ticks); +} + + + +/*! + * \brief Calculate the CRC value of the code in flash. + * \param None + * \return calculated CRC value using STM32's builtin CRC hardware + * + * \note + * I copied this function as the function crc calc function in pios_bl_helper.c + * is only included when the PIOS_BL_HELPER is defined, but this also includes + * the flash unlock and erase functions. It is safer to only have the flash + * functions in the bootloader. + * + */ + +static uint32_t iap_calc_crc(void) +{ + RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE); + CRC_ResetDR(); + CRC_CalcBlockCRC((uint32_t *) START_OF_USER_CODE, (SIZE_OF_CODE) >> 2); + return CRC_GetCRC(); +} + diff --git a/flight/OpenPilot/Modules/FirmwareIAP/inc/firmwareiap.h b/flight/OpenPilot/Modules/FirmwareIAP/inc/firmwareiap.h new file mode 100644 index 000000000..0266f7450 --- /dev/null +++ b/flight/OpenPilot/Modules/FirmwareIAP/inc/firmwareiap.h @@ -0,0 +1,32 @@ +/** + ****************************************************************************** + * + * @file firmwareiap.c + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief Example module to be used as a template for actual modules. + * + * @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 FIRMWAREIAP_H +#define FIRMWAREIAP_H + +int32_t FirmwareIAPInitialize(); + +#endif // FIRMWAREIAP_H + diff --git a/flight/OpenPilot/UAVObjects/firmwareiapobj.c b/flight/OpenPilot/UAVObjects/firmwareiapobj.c new file mode 100644 index 000000000..84c802e25 --- /dev/null +++ b/flight/OpenPilot/UAVObjects/firmwareiapobj.c @@ -0,0 +1,111 @@ +/** + ****************************************************************************** + * @addtogroup UAVObjects OpenPilot UAVObjects + * @{ + * @addtogroup FirmwareIAPObj FirmwareIAPObj + * @brief Firmware IAP + * + * Autogenerated files and functions for FirmwareIAPObj Object + * @{ + * + * @file firmwareiapobj.c + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief Implementation of the FirmwareIAPObj object. This file has been + * automatically generated by the UAVObjectGenerator. + * + * @note Object definition file: firmwareiap.xml. + * This is an automatically generated file. + * DO NOT modify manually. + * + * @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 + */ + +#include "openpilot.h" +#include "firmwareiapobj.h" + +// Private variables +static UAVObjHandle handle; + +// Private functions +static void setDefaults(UAVObjHandle obj, uint16_t instId); + +/** + * Initialize object. + * \return 0 Success + * \return -1 Failure + */ +int32_t FirmwareIAPObjInitialize() +{ + // Register object with the object manager + handle = UAVObjRegister(FIRMWAREIAPOBJ_OBJID, FIRMWAREIAPOBJ_NAME, FIRMWAREIAPOBJ_METANAME, 0, + FIRMWAREIAPOBJ_ISSINGLEINST, FIRMWAREIAPOBJ_ISSETTINGS, FIRMWAREIAPOBJ_NUMBYTES, &setDefaults); + + // Done + if (handle != 0) + { + return 0; + } + else + { + return -1; + } +} + +/** + * Initialize object fields and metadata with the default values. + * If a default value is not specified the object fields + * will be initialized to zero. + */ +static void setDefaults(UAVObjHandle obj, uint16_t instId) +{ + FirmwareIAPObjData data; + UAVObjMetadata metadata; + + // Initialize object fields to their default values + UAVObjGetInstanceData(obj, instId, &data); + memset(&data, 0, sizeof(FirmwareIAPObjData)); + + UAVObjSetInstanceData(obj, instId, &data); + + // Initialize object metadata to their default values + metadata.access = ACCESS_READWRITE; + metadata.gcsAccess = ACCESS_READWRITE; + metadata.telemetryAcked = 1; + metadata.telemetryUpdateMode = UPDATEMODE_MANUAL; + metadata.telemetryUpdatePeriod = 0; + metadata.gcsTelemetryAcked = 1; + metadata.gcsTelemetryUpdateMode = UPDATEMODE_MANUAL; + metadata.gcsTelemetryUpdatePeriod = 0; + metadata.loggingUpdateMode = UPDATEMODE_NEVER; + metadata.loggingUpdatePeriod = 0; + UAVObjSetMetadata(obj, &metadata); +} + +/** + * Get object handle + */ +UAVObjHandle FirmwareIAPObjHandle() +{ + return handle; +} + +/** + * @} + */ + diff --git a/flight/OpenPilot/UAVObjects/inc/firmwareiapobj.h b/flight/OpenPilot/UAVObjects/inc/firmwareiapobj.h new file mode 100644 index 000000000..adc2ad030 --- /dev/null +++ b/flight/OpenPilot/UAVObjects/inc/firmwareiapobj.h @@ -0,0 +1,101 @@ +/** + ****************************************************************************** + * @addtogroup UAVObjects OpenPilot UAVObjects + * @{ + * @addtogroup FirmwareIAPObj FirmwareIAPObj + * @brief Firmware IAP + * + * Autogenerated files and functions for FirmwareIAPObj Object + + * @{ + * + * @file firmwareiapobj.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief Implementation of the FirmwareIAPObj object. This file has been + * automatically generated by the UAVObjectGenerator. + * + * @note Object definition file: firmwareiap.xml. + * This is an automatically generated file. + * DO NOT modify manually. + * + * @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 FIRMWAREIAPOBJ_H +#define FIRMWAREIAPOBJ_H + +// Object constants +#define FIRMWAREIAPOBJ_OBJID 1075803696U +#define FIRMWAREIAPOBJ_NAME "FirmwareIAPObj" +#define FIRMWAREIAPOBJ_METANAME "FirmwareIAPObjMeta" +#define FIRMWAREIAPOBJ_ISSINGLEINST 1 +#define FIRMWAREIAPOBJ_ISSETTINGS 0 +#define FIRMWAREIAPOBJ_NUMBYTES sizeof(FirmwareIAPObjData) + +// Object access macros +/** + * @function FirmwareIAPObjGet(dataOut) + * @brief Populate a FirmwareIAPObjData object + * @param[out] dataOut + */ +#define FirmwareIAPObjGet(dataOut) UAVObjGetData(FirmwareIAPObjHandle(), dataOut) +#define FirmwareIAPObjSet(dataIn) UAVObjSetData(FirmwareIAPObjHandle(), dataIn) +#define FirmwareIAPObjInstGet(instId, dataOut) UAVObjGetInstanceData(FirmwareIAPObjHandle(), instId, dataOut) +#define FirmwareIAPObjInstSet(instId, dataIn) UAVObjSetInstanceData(FirmwareIAPObjHandle(), instId, dataIn) +#define FirmwareIAPObjConnectQueue(queue) UAVObjConnectQueue(FirmwareIAPObjHandle(), queue, EV_MASK_ALL_UPDATES) +#define FirmwareIAPObjConnectCallback(cb) UAVObjConnectCallback(FirmwareIAPObjHandle(), cb, EV_MASK_ALL_UPDATES) +#define FirmwareIAPObjCreateInstance() UAVObjCreateInstance(FirmwareIAPObjHandle()) +#define FirmwareIAPObjRequestUpdate() UAVObjRequestUpdate(FirmwareIAPObjHandle()) +#define FirmwareIAPObjRequestInstUpdate(instId) UAVObjRequestInstanceUpdate(FirmwareIAPObjHandle(), instId) +#define FirmwareIAPObjUpdated() UAVObjUpdated(FirmwareIAPObjHandle()) +#define FirmwareIAPObjInstUpdated(instId) UAVObjUpdated(FirmwareIAPObjHandle(), instId) +#define FirmwareIAPObjGetMetadata(dataOut) UAVObjGetMetadata(FirmwareIAPObjHandle(), dataOut) +#define FirmwareIAPObjSetMetadata(dataIn) UAVObjSetMetadata(FirmwareIAPObjHandle(), dataIn) +#define FirmwareIAPObjReadOnly(dataIn) UAVObjReadOnly(FirmwareIAPObjHandle()) + +// Object data +typedef struct { + uint16_t Command; + uint32_t Port; + uint8_t Version[3]; + uint16_t SVN; + uint32_t crc; + +} __attribute__((packed)) FirmwareIAPObjData; + +// Field information +// Field Command information +// Field Port information +// Field Version information +/* Number of elements for field Version */ +#define FIRMWAREIAPOBJ_VERSION_NUMELEM 3 +// Field SVN information +// Field crc information + + +// Generic interface functions +int32_t FirmwareIAPObjInitialize(); +UAVObjHandle FirmwareIAPObjHandle(); + +#endif // FIRMWAREIAPOBJ_H + +/** + * @} + * @} + */ diff --git a/flight/PiOS/pios.h b/flight/PiOS/pios.h index f556b7df0..f310b095c 100644 --- a/flight/PiOS/pios.h +++ b/flight/PiOS/pios.h @@ -92,6 +92,7 @@ #if defined(PIOS_INCLUDE_HMC5843) #include #endif +#include #if defined(PIOS_INCLUDE_BL_HELPER) #include