Various fixes around Companion trainer mode (#7116)
[opentx.git] / radio / src / io / frsky_firmware_update.h
bloba229edb2773ed73e60895cc94e43747b78169cfe
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #ifndef _FRSKY_FIRMWARE_UPDATE_H_
22 #define _FRSKY_FIRMWARE_UPDATE_H_
24 #include "dataconstants.h"
25 #include "definitions.h"
26 #include "frsky_pxx2.h"
27 #include "ff.h"
29 enum FrskyFirmwareProductFamily {
30 FIRMWARE_FAMILY_INTERNAL_MODULE,
31 FIRMWARE_FAMILY_EXTERNAL_MODULE,
32 FIRMWARE_FAMILY_RECEIVER,
33 FIRMWARE_FAMILY_SENSOR,
34 FIRMWARE_FAMILY_BLUETOOTH_CHIP,
35 FIRMWARE_FAMILY_POWER_MANAGEMENT_UNIT,
38 enum FrskyFirmwareProductId {
39 FIRMWARE_ID_NONE,
40 FIRMWARE_ID_XJT = 0x01,
41 FIRMWARE_ID_ISRM = 0x02,
44 PACK(struct FrSkyFirmwareInformation {
45 uint32_t fourcc;
46 uint8_t headerVersion;
47 uint8_t firmwareVersionMajor;
48 uint8_t firmwareVersionMinor;
49 uint8_t firmwareVersionRevision;
50 uint32_t size;
51 uint8_t productFamily;
52 uint8_t productId;
53 uint16_t crc;
54 });
56 const char * readFrSkyFirmwareInformation(const char * filename, FrSkyFirmwareInformation & data);
58 class FrskyDeviceFirmwareUpdate {
59 enum State {
60 SPORT_IDLE,
61 SPORT_POWERUP_REQ,
62 SPORT_POWERUP_ACK,
63 SPORT_VERSION_REQ,
64 SPORT_VERSION_ACK,
65 SPORT_DATA_TRANSFER,
66 SPORT_DATA_REQ,
67 SPORT_COMPLETE,
68 SPORT_FAIL
71 public:
72 FrskyDeviceFirmwareUpdate(ModuleIndex module):
73 module(module) {
76 const char * flashFirmware(const char * filename);
78 protected:
79 uint8_t state = SPORT_IDLE;
80 uint32_t address = 0;
81 ModuleIndex module;
82 uint8_t frame[12];
84 void startFrame(uint8_t command);
85 void sendFrame();
87 bool readBuffer(uint8_t * buffer, uint8_t count, uint32_t timeout);
88 const uint8_t * readFullDuplexFrame(ModuleFifo & fifo, uint32_t timeout);
89 const uint8_t * readHalfDuplexFrame(uint32_t timeout);
90 const uint8_t * readFrame(uint32_t timeout);
91 bool waitState(State state, uint32_t timeout);
92 void processFrame(const uint8_t * frame);
94 const char * doFlashFirmware(const char * filename);
95 const char * sendPowerOn();
96 const char * sendReqVersion();
97 const char * uploadFileNormal(const char * filename, FIL * file);
98 const char * uploadFileToHorusXJT(const char * filename, FIL * file);
99 const char * endTransfer();
102 class FrskyChipFirmwareUpdate {
103 public:
104 FrskyChipFirmwareUpdate()
108 const char * flashFirmware(const char * filename, bool wait = true);
110 protected:
111 uint8_t crc;
113 void sendByte(uint8_t byte, bool crc = true);
114 const char * waitAnswer(uint8_t & status);
115 const char * startBootloader();
116 const char * sendUpgradeCommand(char command, uint32_t packetsCount);
117 const char * sendUpgradeData(uint32_t index, uint8_t * data);
119 const char * doFlashFirmware(const char * filename);
122 #endif // _FRSKY_FIRMWARE_UPDATE_H_