1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-29 14:52:12 +01:00

OP-21/Flight Bootloader - Complete recoding. WARNING- As with all flash memories, the one in your OP HW as limited write cycles. Errors in this bootloader may damage your HW if it falls under a write or erase dead loop. This is still under development, for devs only.

git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@1507 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
zedamota 2010-09-02 20:13:41 +00:00 committed by zedamota
parent f8873b9a18
commit 7a66232be0
10 changed files with 661 additions and 377 deletions

View File

@ -93,6 +93,8 @@ SRC += $(HIDSYSTEM)/usb_prop.c
SRC += $(HIDSYSTEM)/usb_pwr.c
SRC += $(HIDSYSTEM)/stm3210e_eval.c
SRC += $(HIDSYSTEM)/stopwatch.c
SRC += $(HIDSYSTEM)/op_dfu.c
SRC += $(HIDSYSTEM)/flash_dfu.c
## CMSIS for STM32
SRC += $(CMSISDIR)/core_cm3.c

View File

@ -0,0 +1,90 @@
/******************** (C) COPYRIGHT 2010 STMicroelectronics ********************
* File Name : usb_endp.c
* Author : MCD Application Team
* Version : V3.2.1
* Date : 07/05/2010
* Description : Endpoint routines
********************************************************************************
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "platform_config.h"
#include "stm32f10x.h"
#include "usb_lib.h"
#include "usb_istr.h"
#include "stm32_eval.h"
#include "stm32f10x_flash.h"
#include "common.h"
#include "hw_config.h"
#include <string.h>
#include "op_dfu.h"
//extern uint32_t baseOfAdressType(DFUTransfer type);
//extern uint8_t* SendBuffer;
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
//uint32_t downPacketCurrent = 0;
//uint8_t downType = 0;
//uint32_t downSizeOfLastPacket = 0;
//uint32_t downPacketTotal = 0;
//extern DFUStates DeviceState;
//extern uint32_t Aditionals;
uint8_t FLASH_Ini() {
FLASH_Unlock();
return 1;
}
uint8_t *FLASH_If_Read(uint32_t SectorAddress, uint32_t DataLength) {
return (uint8_t*) (SectorAddress);
}
uint8_t FLASH_Start(uint32_t size) {
FLASH_ErasePage(0x08008800);
uint32_t pageAdress;
pageAdress = StartOfUserCode;
uint8_t fail = FALSE;
while ((pageAdress < StartOfUserCode + size) || (fail == TRUE)) {
for (int retry = 0; retry < MAX_DEL_RETRYS; ++retry) {
if (FLASH_ErasePage(pageAdress) == FLASH_COMPLETE) {
fail = FALSE;
break;
} else {
fail = TRUE;
}
}
#ifdef STM32F10X_HD
pageAdress += 2048;
#endif
#ifdef STM32F10X_MD
pageAdress += 1024;
#endif
}
if (fail == FALSE) {
#ifdef STM32F10X_HD
pageAdress = StartOfUserCode + SizeOfHash + SizeOfDescription
+ SizeOfCode - 2048;
#endif
#ifdef STM32F10X_MD
pageAdress = StartOfUserCode+SizeOfHash+SizeOfDescription+SizeOfCode-1024;
#endif
for (int retry = 0; retry < MAX_DEL_RETRYS; ++retry) {
if (FLASH_ErasePage(pageAdress) == FLASH_COMPLETE) {
fail = FALSE;
break;
} else
fail = TRUE;
}
}
return (fail == TRUE) ? 0 : 1;
}
/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/

View File

@ -0,0 +1,31 @@
/*
* board.h
*
* Created on: 2010/08/31
* Author: Programacao
*/
#ifndef BOARD_H_
#define BOARD_H_
#define deviceID 69
#define board_can_read 1
#define board_can_write 1
/**************************************************/
/* OP_DFU Memory locations */
/**************************************************/
#define StartOfUserCode 0x08006000
/**************************************************/
/* OP_DFU Mem Sizes */
/**************************************************/
#define SizeOfHash 20
#define SizeOfDescription 100
#define SizeOfCode 499712-SizeOfHash-SizeOfDescription //488K
#endif /* BOARD_H_ */

View File

@ -8,63 +8,71 @@
#ifndef COMMON_H_
#define COMMON_H_
/**************************************************/
/* OP_DFU Memory locations */
/**************************************************/
#define StartOfUserCode 0x08006000
/**************************************************/
/* OP_DFU Mem Sizes */
/**************************************************/
#define SizeOfHash 60
#define SizeOfDescription 100
#define SizoOfCode 100
#include "board.h"
/**************************************************/
/* OP_DFU states */
/**************************************************/
#define DFUidle 0
#define uploading 1
#define wrong_packet_received 2
#define too_many_packets 3
#define too_few_packets 4
#define Last_operation_Success 5
#define downloading 6
#define idle 7
#define Last_operation_failed 8
#define uploadingStarting 9
typedef enum
{DFUidle, //0
uploading, //1
wrong_packet_received, //2
too_many_packets, //3
too_few_packets, //4
Last_operation_Success, //5
downloading, //6
idle, //7
Last_operation_failed, //8
uploadingStarting, //9
outsideDevCapabilities, //10
test
}DFUStates;
/**************************************************/
/* OP_DFU commands */
/**************************************************/
#define Reserved 0
#define Req_Capabilities 1
#define Rep_Capabilities 2
#define EnterDFU 3
#define JumpFW 4
#define Reset 5
#define Abort_Operation 6
#define Upload 7
#define Op_END 8
#define Download_Req 9
#define Download 10
#define Status_Request 11
#define Status_Rep 12
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;
/**************************************************/
/* OP_DFU transfer types */
/**************************************************/
#define FW 0
#define Hash 1
#define Descript 2
typedef enum
{
FW, //0
Hash, //1
Descript //2
}DFUTransfer;
/**************************************************/
/* 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_ */

View File

@ -0,0 +1,15 @@
/*
* flash_dfu.h
*
* Created on: 2010/08/31
* Author: Programacao
*/
#ifndef FLASH_DFU_H_
#define FLASH_DFU_H_
uint8_t FLASH_Ini();
uint8_t FLASH_Start(uint32_t size);
uint8_t *FLASH_If_Read(uint32_t SectorAddress, uint32_t DataLength);
#endif /* FLASH_DFU_H_ */

View File

@ -0,0 +1,48 @@
/******************** (C) COPYRIGHT 2010 STMicroelectronics ********************
* File Name : usb_desc.h
* Author : MCD Application Team
* Version : V3.2.1
* Date : 07/05/2010
* Description : Descriptor Header for Custom HID Demo
********************************************************************************
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __OP_DFU_H
#define __OP_DFU_H
/* Includes ------------------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
typedef struct
{
uint8_t programmingType;
uint8_t readWriteFlags;
uint32_t startOfUserCode;
uint32_t sizeOfCode;
uint8_t sizeOfDescription;
uint8_t sizeOfHash;
uint8_t devID;
}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(void);
void DataDownload();
#endif /* __OP_DFU_H */
/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/

View File

@ -14,13 +14,16 @@
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "usb_lib.h"
#include "hw_config.h"
#include "stm32_eval.h"
#include "common.h"
#include "platform_config.h"
#include "stopwatch.h"
#include "op_dfu.h"
extern void FLASH_Download();
#define BSL_HOLD_STATE ((USB_DISCONNECT->IDR & USB_DISCONNECT_PIN) ? 0 : 1)
@ -39,7 +42,7 @@ uint32_t period2 = 50; // *100 uS -> 5 mS
uint32_t sweep_steps2 = 100; // * 5 mS -> 500 mS
/* Extern variables ----------------------------------------------------------*/
uint8_t DeviceState;
DFUStates DeviceState;
uint8_t JumpToApp = 0;
/* Private function prototypes -----------------------------------------------*/
void Delay(__IO uint32_t nCount);
@ -58,7 +61,7 @@ int main(void) {
Set_System();
if (BSL_HOLD_STATE == 0) {
OPDfuIni();
USB_Interrupts_Config();
Set_USBClock();
USB_Init();
@ -124,9 +127,9 @@ int main(void) {
if (STOPWATCH_ValueGet() > 100 * 50 * 100)
STOPWATCH_Reset();
if ((STOPWATCH_ValueGet() > 60000) && (DeviceState == idle))
if ((STOPWATCH_ValueGet() > 70000) && (DeviceState == idle))
JumpToApp = TRUE;
FLASH_Download();
DataDownload();
//DelayWithDown(10);//1000000);
}
if (((*(__IO uint32_t*) StartOfUserCode) & 0x2FFE0000) == 0x20000000) { /* Jump to user application */
@ -146,6 +149,9 @@ int main(void) {
}
while (1) {
// STM_EVAL_LEDOff(LED1);
// STM_EVAL_LEDOff(LED2);
// while(1){}
if (LedPWM(50, 100, STOPWATCH_ValueGet())) {
STM_EVAL_LEDOn(LED2);
STM_EVAL_LEDOff(LED1);
@ -155,7 +161,7 @@ int main(void) {
}
if (STOPWATCH_ValueGet() > 2*50 * 100)
STOPWATCH_Reset();
FLASH_Download();
DataDownload();
}
}
@ -179,20 +185,7 @@ void Delay(__IO uint32_t nCount) {
}
}
/*******************************************************************************
* Function Name : Delay
* Description : Inserts a delay time.
* Input : nCount: specifies the delay time length.
* Output : None
* Return : None
*******************************************************************************/
void DelayWithDown(__IO uint32_t nCount) {
for (; nCount != 0; nCount--) {
for (__IO uint32_t delay = DownloadDelay; delay != 0; delay--) {
}
FLASH_Download();
}
}
#ifdef USE_FULL_ASSERT
/*******************************************************************************
* Function Name : assert_failed

View File

@ -0,0 +1,406 @@
/******************** (C) COPYRIGHT 2010 STMicroelectronics ********************
* File Name : usb_endp.c
* Author : MCD Application Team
* Version : V3.2.1
* Date : 07/05/2010
* Description : Endpoint routines
********************************************************************************
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "platform_config.h"
#include "stm32f10x.h"
#include "usb_lib.h"
#include "usb_istr.h"
#include "stm32_eval.h"
#include "stm32f10x_flash.h"
#include "common.h"
#include "hw_config.h"
#include <string.h>
#include "op_dfu.h"
#include "flash_dfu.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
//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;
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 DataDownload() {
if ((DeviceState == downloading) && (GetEPTxStatus(ENDP1) == EP_TX_NAK)) {
uint8_t packetSize;
SendBuffer[0] = 0x01;
SendBuffer[1] = Download;
SendBuffer[2] = downPacketCurrent >> 24;
SendBuffer[3] = downPacketCurrent >> 16;
SendBuffer[4] = downPacketCurrent >> 8;
SendBuffer[5] = downPacketCurrent;
if (downPacketCurrent == downPacketTotal) {
packetSize = downSizeOfLastPacket;
} else {
packetSize = 14;
}
for (int x = 0; x < packetSize; ++x) {
switch (currentProgrammingDestination) {
case Self_flash:
SendBuffer[6 + x * 4] = *FLASH_If_Read(baseOfAdressType(
downType) + downPacketCurrent * 4 + x * 4, 0);
SendBuffer[7 + x * 4] = *FLASH_If_Read(baseOfAdressType(
downType) + 1 + downPacketCurrent * 4 + x * 4, 0);
SendBuffer[8 + x * 4] = *FLASH_If_Read(baseOfAdressType(
downType) + 2 + downPacketCurrent * 4 + x * 4, 0);
SendBuffer[9 + x * 4] = *FLASH_If_Read(baseOfAdressType(
downType) + 3 + downPacketCurrent * 4 + x * 4, 0);
break;
case Remote_flash_via_spi:
//TODO result=SPI_FLASH();
break;
}
}
USB_SIL_Write(EP1_IN, (uint8_t*) SendBuffer, 64);
downPacketCurrent = downPacketCurrent + 1;
if (downPacketCurrent > downPacketTotal) {
// STM_EVAL_LEDOn(LED2);
DeviceState = Last_operation_Success;
Aditionals = (uint32_t) Download;
}
SetEPTxValid(ENDP1);
}
}
void processComand(uint8_t *Receive_Buffer) {
Command = Receive_Buffer[COMMAND];
EchoReqFlag = (Command >> 7);
EchoAnsFlag = (Command >> 6) & 0x01;
StartFlag = (Command >> 5) & 0x01;
Count = Receive_Buffer[COUNT] << 24;
Count += Receive_Buffer[COUNT + 1] << 16;
Count += Receive_Buffer[COUNT + 2] << 8;
Count += Receive_Buffer[COUNT + 3];
Data = Receive_Buffer[DATA] << 24;
Data += Receive_Buffer[DATA + 1] << 16;
Data += Receive_Buffer[DATA + 2] << 8;
Data += Receive_Buffer[DATA + 3];
Data0 = Receive_Buffer[DATA];
Data1 = Receive_Buffer[DATA + 1];
Data2 = Receive_Buffer[DATA + 2];
Data3 = Receive_Buffer[DATA + 3];
Command = Command & 0b00011111;
if (EchoReqFlag == 1) {
memcpy(echoBuffer, Buffer, 64);
}
switch (Command) {
case EnterDFU:
if ((DeviceState == idle) && (Data0 < numberOfDevices)) {
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 = FLASH_Ini();
break;
case Remote_flash_via_spi:
//TODO result=SPI_FLASH();
break;
default:
DeviceState = Last_operation_failed;
Aditionals = (uint16_t) Command;
}
if (result != 1) {
DeviceState = Last_operation_failed;
Aditionals = (uint32_t) Command;
}
} else {
DeviceState = outsideDevCapabilities;
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;
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) {
uint32_t size = (SizeOfTransfer - 1) * 14 * 4
+ SizeOfLastPacket * 4;
result = FLASH_Start(size);
}
if (result != 1) {
DeviceState = Last_operation_failed;//ok
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)//is this the last packet?
{
numberOfWords = SizeOfLastPacket;
}
for (uint8_t x = 0; x < numberOfWords; ++x) {
offset = 4 * x;
Data = Receive_Buffer[DATA + offset] << 24;
Data += Receive_Buffer[DATA + 1 + offset] << 16;
Data += Receive_Buffer[DATA + 2 + offset] << 8;
Data += Receive_Buffer[DATA + 3 + offset];
aux = baseOfAdressType(TransferType) + (uint32_t)(Count
* 14 * 4 + x * 4);
uint8_t result = 0;
//uint8_t lol=0;
switch (currentProgrammingDestination) {
case Self_flash:
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:
//TODO result=SPI_FLASH_ProgramWord(aux,Data);
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;
}
// FLASH_ProgramWord(MemLocations[TransferType]+4,++Next_Packet);//+Count,Data);
} else {
DeviceState = Last_operation_failed;
Aditionals = (uint32_t) Command;
}
}
break;
case Req_Capabilities:
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].sizeOfCode >> 24;
Buffer[3] = devicesTable[Data0].sizeOfCode >> 16;
Buffer[4] = devicesTable[Data0].sizeOfCode >> 8;
Buffer[5] = devicesTable[Data0].sizeOfCode;
Buffer[6] = Data0;
Buffer[7] = devicesTable[Data0].sizeOfHash;
Buffer[8] = devicesTable[Data0].sizeOfDescription;
Buffer[8] = devicesTable[Data0].devID;
}
USB_SIL_Write(EP1_IN, (uint8_t*) Buffer, 64);
SetEPTxValid(ENDP1);
break;
case Rep_Capabilities:
break;
case JumpFW:
FLASH_Lock();
JumpToApp = 1;
break;
case Reset:
Reset_Device();
break;
case Abort_Operation:
Next_Packet=0;
DeviceState=DFUidle;
break;
case Op_END:
if (DeviceState == uploading) {
if (Next_Packet - 1 == SizeOfTransfer) {
Next_Packet=0;
DeviceState = Last_operation_Success;
}
if (Next_Packet - 1 < SizeOfTransfer) {
Next_Packet=0;
DeviceState = too_few_packets;
}
}
break;
case Download_Req:
if (DeviceState == DFUidle) {
downType = Data0;
downPacketTotal = Count;
downSizeOfLastPacket = Data1;
if (isBiggerThanAvailable(downType, downPacketTotal - 1 * 14
+ downSizeOfLastPacket) == 1) {
DeviceState = outsideDevCapabilities;
Aditionals = (uint32_t) Command;
} else
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;
USB_SIL_Write(EP1_IN, (uint8_t*) Buffer, 64);
SetEPTxValid(ENDP1);
if (DeviceState == Last_operation_Success) {
DeviceState = DFUidle;
}
break;
case Status_Rep:
break;
}
if (EchoReqFlag == 1) {
echoBuffer[1] = echoBuffer[1] | EchoAnsFlag;
USB_SIL_Write(EP1_IN, (uint8_t*) echoBuffer, 64);
SetEPTxValid(ENDP1);
}
return;
}
void OPDfuIni(void) {
Device dev;
dev.programmingType = Self_flash;
dev.readWriteFlags = (board_can_read | (board_can_write << 1));
dev.startOfUserCode = StartOfUserCode;
dev.sizeOfCode = SizeOfCode;
dev.sizeOfDescription = SizeOfDescription;
dev.sizeOfHash = SizeOfHash;
dev.devID = deviceID;
numberOfDevices = 1;
devicesTable[0] = dev;
//TODO check other devices trough spi or whatever
}
uint32_t baseOfAdressType(DFUTransfer type) {
switch (type) {
case FW:
return currentDevice.startOfUserCode;
break;
case Hash:
return currentDevice.startOfUserCode + currentDevice.sizeOfCode;
break;
case Descript:
return currentDevice.startOfUserCode + currentDevice.sizeOfCode
+ currentDevice.sizeOfHash;
;
break;
default:
return 0;
}
}
uint8_t isBiggerThanAvailable(DFUTransfer type, uint32_t size) {
switch (type) {
case FW:
return (size > currentDevice.sizeOfCode) ? 1 : 0;
break;
case Hash:
return (size > currentDevice.sizeOfHash) ? 1 : 0;
break;
case Descript:
return (size > currentDevice.sizeOfDescription) ? 1 : 0;
break;
default:
return TRUE;
}
}

View File

@ -16,7 +16,7 @@
/* Includes ------------------------------------------------------------------*/
#include "usb_lib.h"
#include "usb_desc.h"
#include <stdio.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/

View File

@ -22,86 +22,13 @@
#include "stm32f10x_flash.h"
#include "common.h"
#include "hw_config.h"
#include "op_dfu.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint8_t Receive_Buffer[64];
uint8_t Buffer[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;
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
volatile uint32_t downPacketCurrent=0;
uint32_t downPacketTotal=0;
uint8_t downType=0;
uint32_t downSizeOfLastPacket=0;
uint32_t MemLocations[3]=
{
StartOfUserCode, StartOfUserCode-SizeOfHash, StartOfUserCode-SizeOfHash-SizeOfDescription
};
/* Extern variables ----------------------------------------------------------*/
extern uint8_t DeviceState ;
extern uint8_t JumpToApp;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
uint8_t *FLASH_If_Read (uint32_t SectorAddress, uint32_t DataLength)
{
return (uint8_t*)(SectorAddress);
}
void FLASH_Download() {
if ((DeviceState == downloading) && (GetEPTxStatus(ENDP1)==EP_TX_NAK)) {
uint8_t packetSize;
Buffer[0] = 0x01;
Buffer[1] = Download;
Buffer[2] = downPacketCurrent >> 24;
Buffer[3] = downPacketCurrent >> 16;
Buffer[4] = downPacketCurrent >> 8;
Buffer[5] = downPacketCurrent;
if(downPacketCurrent==downPacketTotal)
{
packetSize=downSizeOfLastPacket;
}
else
{
packetSize=14;
}
for(int x=0;x<packetSize;++x)
{
Buffer[6+x*4] = *FLASH_If_Read(MemLocations[downType]+ downPacketCurrent*4+x*4, 0);
Buffer[7+x*4] = *FLASH_If_Read(MemLocations[downType] + 1+downPacketCurrent*4+x*4, 0);
Buffer[8+x*4] = *FLASH_If_Read(MemLocations[downType] + 2+downPacketCurrent*4+x*4, 0);
Buffer[9+x*4] = *FLASH_If_Read(MemLocations[downType] + 3+downPacketCurrent*4+x*4, 0);
}
USB_SIL_Write(EP1_IN, (uint8_t*) Buffer, 64);
downPacketCurrent=downPacketCurrent+1;
if(downPacketCurrent>downPacketTotal)
{
// STM_EVAL_LEDOn(LED2);
DeviceState=Last_operation_Success;
Aditionals=(uint32_t)Download;
}
SetEPTxValid(ENDP1);
}
}
/*******************************************************************************
* Function Name : EP1_OUT_Callback.
* Description : EP1 OUT Callback Routine.
@ -111,246 +38,10 @@ void FLASH_Download() {
*******************************************************************************/
void EP1_OUT_Callback(void)
{
BitAction Led_State;
/* Read received data (2 bytes) */
USB_SIL_Read(EP1_OUT, Receive_Buffer);
Command=Receive_Buffer[1];
EchoReqFlag=(Command>>7);
EchoAnsFlag=(Command>>6) & 0x01;
StartFlag=(Command>>5) & 0x01;
Count=Receive_Buffer[2]<<24;
Count+=Receive_Buffer[3]<<16;
Count+=Receive_Buffer[4]<<8;
Count+=Receive_Buffer[5];
Data=Receive_Buffer[6]<<24;
Data+=Receive_Buffer[7]<<16;
Data+=Receive_Buffer[8]<<8;
Data+=Receive_Buffer[9];
Data0=Receive_Buffer[6];
Data1=Receive_Buffer[7];
Data2=Receive_Buffer[8];
Data3=Receive_Buffer[9];
Command=Command & 0b00011111;
switch(Command)
{
case EnterDFU:
if (DeviceState==idle)
{
DeviceState=DFUidle;
FLASH_Unlock();
}
else
{
DeviceState=Last_operation_failed;
Aditionals=(uint16_t) Command;
}
break;
case Upload:
if((DeviceState==DFUidle) || (DeviceState==uploading))
{
if((StartFlag==1) && (Next_Packet==0))
{
TransferType=Data0;
SizeOfTransfer=Count;
Next_Packet=1;
DeviceState=uploading;
SizeOfLastPacket=Data1;
}
else if((StartFlag!=1) && (Next_Packet!=0))
{
// STM_EVAL_LEDOn(LED2);
if(Count==Next_Packet-1)
{
uint8_t numberOfWords=14;
if(Count==SizeOfTransfer)//is this the last packet?
{
numberOfWords=SizeOfLastPacket;
}
for(uint8_t x=0;x<numberOfWords;++x)
{
offset=4*x;
Data=Receive_Buffer[6+offset]<<24;
Data+=Receive_Buffer[7+offset]<<16;
Data+=Receive_Buffer[8+offset]<<8;
Data+=Receive_Buffer[9+offset];
aux=MemLocations[TransferType]+(uint32_t)(Count*14*4+x*4);
FLASH_ProgramWord(aux,Data);
}
++Next_Packet;
}
else
{
DeviceState=wrong_packet_received;
Aditionals=Count;
}
// FLASH_ProgramWord(MemLocations[TransferType]+4,++Next_Packet);//+Count,Data);
}
else
{
STM_EVAL_LEDOn(LED2);
DeviceState=Last_operation_failed;
Aditionals=(uint32_t) Command;
}
}
break;
case Req_Capabilities:
break;
case Rep_Capabilities:
break;
case JumpFW:
JumpToApp=1;
break;
case Reset:
Reset_Device();
break;
case Abort_Operation:
break;
case Op_END:
if(DeviceState==uploading)
{
if(Next_Packet-1==SizeOfTransfer)
{
DeviceState=Last_operation_Success;
}
if(Next_Packet-1<SizeOfTransfer)
{
DeviceState=too_few_packets;
}
}
break;
case Download_Req:
if(DeviceState==DFUidle)
{
downType=Data0;
downPacketCurrent=0;
downPacketTotal=Count;
DeviceState=downloading;
downSizeOfLastPacket=Data1;
}
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[4]=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;
USB_SIL_Write(EP1_IN, (uint8_t*)Buffer,64);
SetEPTxValid(ENDP1);
if(DeviceState==Last_operation_Success)
{
DeviceState=DFUidle;
}
break;
case Status_Rep:
break;
}
processComand(Receive_Buffer+1);
SetEPRxStatus(ENDP1, EP_RX_VALID);
//uint8_t Buffer[2];
Buffer[0]=0x07;
Buffer[1]=2;
//USB_SIL_Write(EP1_IN, (uint8_t*)Buffer,2);
//SetEPTxValid(ENDP1);
return;
if (Receive_Buffer[1] == 0)
{
Led_State = Bit_RESET;
}
else
{
Led_State = Bit_SET;
}
switch (Receive_Buffer[0])
{
case 1: /* Led 1 */
if (Led_State != Bit_RESET)
{
STM_EVAL_LEDOn(LED1);
}
else
{
STM_EVAL_LEDOff(LED1);
}
break;
case 2: /* Led 2 */
if (Led_State != Bit_RESET)
{
STM_EVAL_LEDOn(LED2);
}
else
{
STM_EVAL_LEDOff(LED2);
}
break;
case 3: /* Led 3 */
if (Led_State != Bit_RESET)
{
STM_EVAL_LEDOn(LED3);
}
else
{
STM_EVAL_LEDOff(LED3);
}
break;
case 4: /* Led 4 */
if (Led_State != Bit_RESET)
{
STM_EVAL_LEDOn(LED4);
}
else
{
STM_EVAL_LEDOff(LED4);
}
break;
default:
STM_EVAL_LEDOff(LED1);
STM_EVAL_LEDOff(LED2);
STM_EVAL_LEDOff(LED3);
STM_EVAL_LEDOff(LED4);
break;
}
#ifndef STM32F10X_CL
SetEPRxStatus(ENDP1, EP_RX_VALID);
#endif /* STM32F10X_CL */
}
/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/