[Horus] S.PORT devices update should work (not tested)

[Horus] SD Manager files renaming fixed (quick & dirty, I would prefer a popup)
This commit is contained in:
Bertrand Songis
2016-09-05 19:33:06 +02:00
parent fd138cf22a
commit cc2c2b094e
22 changed files with 585 additions and 500 deletions
+76
View File
@@ -0,0 +1,76 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*/
#include "opentx.h"
bool isBootloader(const char * filename)
{
FIL file;
f_open(&file, filename, FA_READ);
uint8_t buffer[1024];
UINT count;
if (f_read(&file, buffer, 1024, &count) != FR_OK || count != 1024) {
return false;
}
return isBootloaderStart(buffer);
}
void bootloaderFlash(const char * filename)
{
FIL file;
f_open(&file, filename, FA_READ);
uint8_t buffer[1024];
UINT count;
lcdClear();
drawProgressBar(STR_WRITING);
static uint8_t unlocked = 0;
if (!unlocked) {
unlocked = 1;
unlockFlash();
}
for (int i=0; i<BOOTLOADER_SIZE; i+=1024) {
watchdogSuspend(100/*1s*/);
if (f_read(&file, buffer, 1024, &count) != FR_OK || count != 1024) {
POPUP_WARNING(STR_SDCARD_ERROR);
break;
}
if (i==0 && !isBootloaderStart(buffer)) {
POPUP_WARNING(STR_INCOMPATIBLE);
break;
}
for (int j=0; j<1024; j+=FLASH_PAGESIZE) {
flashWrite(CONVERT_UINT_PTR(FIRMWARE_ADDRESS+i+j), (uint32_t *)(buffer+j));
}
updateProgressBar(i, BOOTLOADER_SIZE);
SIMU_SLEEP(30/*ms*/);
}
if (unlocked) {
lockFlash();
unlocked = 0;
}
f_close(&file);
}
+338
View File
@@ -0,0 +1,338 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*/
#include "opentx.h"
#if defined(STM32)
#define PRIM_REQ_POWERUP (0)
#define PRIM_REQ_VERSION (1)
#define PRIM_CMD_DOWNLOAD (3)
#define PRIM_DATA_WORD (4)
#define PRIM_DATA_EOF (5)
#define PRIM_ACK_POWERUP (0x80)
#define PRIM_ACK_VERSION (0x81)
#define PRIM_REQ_DATA_ADDR (0x82)
#define PRIM_END_DOWNLOAD (0x83)
#define PRIM_DATA_CRC_ERR (0x84)
enum SportUpdateState {
SPORT_IDLE,
SPORT_POWERUP_REQ,
SPORT_POWERUP_ACK,
SPORT_VERSION_REQ,
SPORT_VERSION_ACK,
SPORT_DATA_TRANSFER,
SPORT_DATA_REQ,
SPORT_COMPLETE,
SPORT_FAIL
};
uint8_t sportUpdateState = SPORT_IDLE;
uint32_t sportUpdateAddr = 0;
void sportOutputPushByte(uint8_t byte)
{
if (byte == 0x7E || byte == 0x7D) {
telemetryOutputPushByte(0x7D);
telemetryOutputPushByte(0x20 ^ byte);
}
else {
telemetryOutputPushByte(byte);
}
}
bool isSportOutputBufferAvailable()
{
return (outputTelemetryBufferSize == 0 && outputTelemetryBufferTrigger == 0x7E);
}
// TODO merge it with S.PORT update function when finished
void sportOutputPushPacket(SportTelemetryPacket * packet)
{
uint16_t crc = 0;
for (uint8_t i=1; i<sizeof(SportTelemetryPacket); i++) {
uint8_t byte = packet->raw[i];
sportOutputPushByte(byte);
crc += byte; // 0-1FF
crc += crc >> 8; // 0-100
crc &= 0x00ff;
}
telemetryOutputPushByte(0xFF-crc);
telemetryOutputSetTrigger(packet->raw[0]); // physicalId
}
void sportProcessUpdatePacket(uint8_t * packet)
{
if (packet[0]==0x5E && packet[1]==0x50) {
switch (packet[2]) {
case PRIM_ACK_POWERUP :
if (sportUpdateState == SPORT_POWERUP_REQ) {
sportUpdateState = SPORT_POWERUP_ACK;
}
break;
case PRIM_ACK_VERSION:
if (sportUpdateState == SPORT_VERSION_REQ) {
sportUpdateState = SPORT_VERSION_ACK;
// SportVersion[0] = packet[3] ;
// SportVersion[1] = packet[4] ;
// SportVersion[2] = packet[5] ;
// SportVersion[3] = packet[6] ;
// SportVerValid = 1 ;
}
break;
case PRIM_REQ_DATA_ADDR :
if (sportUpdateState == SPORT_DATA_TRANSFER) {
sportUpdateAddr = *((uint32_t *)(&packet[3]));
sportUpdateState = SPORT_DATA_REQ;
}
break;
case PRIM_END_DOWNLOAD :
sportUpdateState = SPORT_COMPLETE ;
break;
case PRIM_DATA_CRC_ERR :
sportUpdateState = SPORT_FAIL ;
break;
}
}
}
bool sportWaitState(SportUpdateState state, int timeout)
{
#if defined(SIMU)
SIMU_SLEEP_NORET(1);
return true;
#else
watchdogSuspend(timeout / 10);
for (int i=timeout/2; i>=0; i--) {
uint8_t byte ;
while (telemetryGetByte(&byte)) {
processFrskyTelemetryData(byte);
}
if (sportUpdateState == state) {
return true;
}
else if (sportUpdateState == SPORT_FAIL) {
return false;
}
CoTickDelay(1);
}
return false;
#endif
}
void sportClearPacket(uint8_t * packet)
{
memset(packet+2, 0, 6);
}
// TODO merge this function
void sportWritePacket(uint8_t * packet)
{
uint8_t * ptr = outputTelemetryBuffer;
*ptr++ = 0x7E;
*ptr++ = 0xFF;
packet[7] = crc16(packet, 7);
for (int i=0; i<8; i++) {
if (packet[i] == 0x7E || packet[i] == 0x7D) {
*ptr++ = 0x7D;
*ptr++ = 0x20 ^ packet[i];
}
else {
*ptr++ = packet[i];
}
}
sportSendBuffer(outputTelemetryBuffer, ptr-outputTelemetryBuffer);
}
bool sportUpdatePowerOn(ModuleIndex module)
{
uint8_t packet[8];
sportUpdateState = SPORT_POWERUP_REQ;
sportWaitState(SPORT_IDLE, 500); // Clear the fifo
telemetryPortInit(FRSKY_SPORT_BAUDRATE, TELEMETRY_SERIAL_8N1);
#if defined(PCBTARANIS) || defined(PCBHORUS)
if (module == INTERNAL_MODULE)
INTERNAL_MODULE_ON();
else
EXTERNAL_MODULE_ON();
#endif
sportWaitState(SPORT_IDLE, 50); // Clear the fifo
for (int i=0; i<10; i++) {
// max 10 attempts
sportClearPacket(packet);
packet[0] = 0x50 ;
packet[1] = PRIM_REQ_POWERUP;
sportWritePacket(packet);
if (sportWaitState(SPORT_POWERUP_ACK, 100))
return true;
}
return false;
}
bool sportUpdateReqVersion()
{
uint8_t packet[8];
sportWaitState(SPORT_IDLE, 20); // Clear the fifo
sportUpdateState = SPORT_VERSION_REQ;
for (int i=0; i<10; i++) {
// max 10 attempts
sportClearPacket(packet) ;
packet[0] = 0x50 ;
packet[1] = PRIM_REQ_VERSION ;
sportWritePacket(packet);
if (sportWaitState(SPORT_VERSION_ACK, 200))
return true;
}
return false;
}
bool sportUpdateUploadFile(const char *filename)
{
FIL file;
uint32_t buffer[1024/4];
UINT count;
uint8_t packet[8];
if (f_open(&file, filename, FA_READ) != FR_OK) {
return false;
}
sportWaitState(SPORT_IDLE, 200); // Clear the fifo
sportUpdateState = SPORT_DATA_TRANSFER;
sportClearPacket(packet) ;
packet[0] = 0x50 ;
packet[1] = PRIM_CMD_DOWNLOAD ;
// Stop here for testing
sportWritePacket(packet);
while(1) {
if (f_read(&file, buffer, 1024, &count) != FR_OK) {
f_close(&file);
return false;
}
count >>= 2;
for (UINT i=0; i<count; i++) {
if (!sportWaitState(SPORT_DATA_REQ, 2000)) {
return false;
}
packet[0] = 0x50 ;
packet[1] = PRIM_DATA_WORD ;
packet[6] = sportUpdateAddr & 0x000000FF;
uint32_t offset = ( sportUpdateAddr & 1023 ) >> 2; // 32 bit word offset into buffer
uint32_t *data = (uint32_t *)(&packet[2]);
*data = buffer[offset];
sportUpdateState = SPORT_DATA_TRANSFER,
sportWritePacket(packet);
if (i==0) {
updateProgressBar(file.fptr, file.fsize);
}
}
if (count < 256) {
f_close(&file);
return true;
}
}
}
bool sportUpdateEnd()
{
uint8_t packet[8];
if (!sportWaitState(SPORT_DATA_REQ, 2000))
return false;
sportClearPacket(packet);
packet[0] = 0x50 ;
packet[1] = PRIM_DATA_EOF;
sportWritePacket(packet);
return sportWaitState(SPORT_COMPLETE, 2000);
}
void sportFlashDevice(ModuleIndex module, const char * filename)
{
pausePulses();
lcdClear();
drawProgressBar(STR_WRITING);
#if defined(PCBTARANIS) || defined(PCBHORUS)
uint8_t intPwr = IS_INTERNAL_MODULE_ON();
uint8_t extPwr = IS_EXTERNAL_MODULE_ON();
INTERNAL_MODULE_OFF();
EXTERNAL_MODULE_OFF();
#endif
bool result = sportUpdatePowerOn(module);
if (result)
result = sportUpdateReqVersion();
if (result)
result = sportUpdateUploadFile(filename);
if (result)
result = sportUpdateEnd();
if (result == false) {
POPUP_WARNING("Firmware Update Error");
}
#if defined(PCBTARANIS) || defined(PCBHORUS)
INTERNAL_MODULE_OFF();
EXTERNAL_MODULE_OFF();
#endif
sportWaitState(SPORT_IDLE, 500); // Clear the fifo
#if defined(PCBTARANIS) || defined(PCBHORUS)
if (intPwr)
INTERNAL_MODULE_ON();
if (extPwr)
EXTERNAL_MODULE_ON();
#endif
sportUpdateState = SPORT_IDLE;
resumePulses();
}
#endif
void sportProcessPacket(uint8_t * packet)
{
#if defined(STM32)
if (sportUpdateState != SPORT_IDLE) {
sportProcessUpdatePacket(packet); // Uses different chksum
return;
}
#endif
sportProcessTelemetryPacket(packet);
}
+49
View File
@@ -0,0 +1,49 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*/
#ifndef _IO_ARM_H_
#define _IO_ARM_H_
#include "dataconstants.h"
#if defined(TELEMETRY_FRSKY_SPORT)
PACK(union SportTelemetryPacket
{
struct {
uint8_t physicalId;
uint8_t primId;
uint16_t dataId;
uint32_t value;
};
uint8_t raw[8];
});
void sportProcessPacket(uint8_t * packet);
bool isSportOutputBufferAvailable();
void sportOutputPushPacket(SportTelemetryPacket * packet);
void sportFlashDevice(ModuleIndex module, const char * filename);
#endif
#if defined(STM32)
bool isBootloader(const char * filename);
void bootloaderFlash(const char * filename);
#endif
#endif // _IO_ARM_H_