Files
wideband/firmware/console/binary/tunerstudio_io.h
rusefillc ccb790f532 Steal tunerstudio files from RusEFI (#71)
* Steal tunerstudio files from RusEFI

(cherry picked from commit dcba04fbd31700f5ca4ef4f12a6e220c5713c7d0)

* Steal crc32 files from RusEFI

(cherry picked from commit bfe88b5297c6fc90c8e647e4144c91edafef25f9)

* fix TS steal

(cherry picked from commit a77b9761da4c185359bb2d7e10abd6a8e69c740d)

* fix crc32

(cherry picked from commit cdb4df09f69c2494316c2b12f97dbe19f6bdb598)

* TS stealed from RE

(cherry picked from commit ff470e134036372425da44e9e0fb98737f31c9e9)

* Steal TS

(cherry picked from commit d11406edc2d6b3c1567ce800e207038745a162e7)

* Steal Thread Controller

(cherry picked from commit 2aa5bbd57150e0d5b51e2e88752bdc8697698861)

* Update TunerStudio protocol files

(cherry picked from commit 58f14216ec6e7fc4408e52ccc28a4f912e340b05)

* byteswap.h

(cherry picked from commit 581d647fd49890af4628f0ff5b51e9f193efcac4)

* bugfix

* one more file to compile, another file not ready - removing from this PR

* removing more from this PR

Co-authored-by: Andrey Gusakov <dron0gus@gmail.com>
Co-authored-by: rusefillc <sdfsdfqsf2334234234>
2022-06-29 01:29:40 -07:00

210 lines
6.6 KiB
C++

/**
* @file tunerstudio_io.h
* @file TS protocol commands and methods are here
*
* @date Mar 8, 2015
* @author Andrey Belomutskiy, (c) 2012-2020
*/
#pragma once
/* to get ChibiOS hal config options */
#include "hal.h"
#include "tunerstudio_impl.h"
/* TODO: find better place */
#define BLOCKING_FACTOR 256
/* TODO: From autogenerated, rework! */
#define TS_BURN_COMMAND 'B'
#define TS_BURN_COMMAND_char B
#define TS_CHUNK_WRITE_COMMAND 'C'
#define TS_CHUNK_WRITE_COMMAND_char C
#define ts_command_e_TS_BENCH_CATEGORY 22
#define ts_command_e_TS_CLEAR_WARNINGS 17
#define ts_command_e_TS_COMMAND_1 1
#define ts_command_e_TS_COMMAND_11 11
#define ts_command_e_TS_COMMAND_12 12
#define ts_command_e_TS_COMMAND_13 13
#define ts_command_e_TS_COMMAND_14 14
#define ts_command_e_TS_COMMAND_15 15
#define ts_command_e_TS_COMMAND_16 16
#define ts_command_e_TS_COMMAND_4 4
#define ts_command_e_TS_COMMAND_5 5
#define ts_command_e_TS_COMMAND_9 9
#define ts_command_e_TS_CRAZY 32
#define ts_command_e_TS_DEBUG_MODE 0
#define ts_command_e_TS_GRAB_PEDAL_UP 6
#define ts_command_e_TS_GRAB_PEDAL_WOT 7
#define ts_command_e_TS_GRAB_TPS_CLOSED 2
#define ts_command_e_TS_GRAB_TPS_WOT 3
#define ts_command_e_TS_IGNITION_CATEGORY 18
#define ts_command_e_TS_INJECTOR_CATEGORY 19
#define ts_command_e_TS_RESET_TLE8888 8
#define ts_command_e_TS_UNUSED_23 23
#define ts_command_e_TS_UNUSED_25 25
#define ts_command_e_TS_UNUSED_26 26
#define ts_command_e_TS_UNUSED_27 27
#define ts_command_e_TS_UNUSED_28 28
#define ts_command_e_TS_UNUSED_29 29
#define ts_command_e_TS_UNUSED_30 30
#define ts_command_e_TS_UNUSED_31 31
#define ts_command_e_TS_UNUSED_CJ125_CALIB 24
#define ts_command_e_TS_WIDEBAND 21
#define ts_command_e_TS_WRITE_FLASH 10
#define ts_command_e_TS_X14 20
#define TS_COMMAND_F 'F'
#define TS_COMMAND_F_char F
#define TS_COMPOSITE_DISABLE 2
#define TS_COMPOSITE_ENABLE 1
#define TS_CRC_CHECK_COMMAND 'k'
#define TS_CRC_CHECK_COMMAND_char k
#define TS_EXECUTE 'E'
#define TS_EXECUTE_char E
#define TS_FILE_VERSION 20210312
#define TS_GET_COMPOSITE_BUFFER_DONE_DIFFERENTLY '8'
#define TS_GET_COMPOSITE_BUFFER_DONE_DIFFERENTLY_char 8
#define TS_GET_CONFIG_ERROR 'e'
#define TS_GET_CONFIG_ERROR_char e
#define TS_GET_FIRMWARE_VERSION 'V'
#define TS_GET_FIRMWARE_VERSION_char V
#define TS_GET_LOGGER_GET_BUFFER 'L'
#define TS_GET_LOGGER_GET_BUFFER_char L
#define TS_GET_OUTPUTS_SIZE '4'
#define TS_GET_OUTPUTS_SIZE_char 4
#define TS_GET_STRUCT '9'
#define TS_GET_STRUCT_char 9
#define TS_GET_TEXT 'G'
#define TS_GET_TEXT_char G
#define TS_HELLO_COMMAND 'S'
#define TS_HELLO_COMMAND_char S
#define TS_IO_TEST_COMMAND 'Z'
#define TS_IO_TEST_COMMAND_char Z
#define TS_ONLINE_PROTOCOL 'z'
#define TS_ONLINE_PROTOCOL_char z
#define TS_OUTPUT_COMMAND 'O'
#define TS_OUTPUT_COMMAND_char O
#define TS_PAGE_COMMAND 'P'
#define TS_PAGE_COMMAND_char P
#define TS_PERF_TRACE_BEGIN '_'
#define TS_PERF_TRACE_BEGIN_char _
#define TS_PERF_TRACE_GET_BUFFER 'b'
#define TS_PERF_TRACE_GET_BUFFER_char b
#define TS_PROTOCOL "001"
#define TS_QUERY_COMMAND 'Q'
#define TS_QUERY_COMMAND_char Q
#define TS_READ_COMMAND 'R'
#define TS_READ_COMMAND_char R
#define TS_RESPONSE_BURN_OK 4
#define TS_RESPONSE_COMMAND_OK 7
#define TS_RESPONSE_CRC_FAILURE 0x82
#define TS_RESPONSE_FRAMING_ERROR 0x8D
#define TS_RESPONSE_OK 0
#define TS_RESPONSE_OUT_OF_RANGE 0x84
#define TS_RESPONSE_UNDERRUN 0x80
#define TS_RESPONSE_UNRECOGNIZED_COMMAND 0x83
#define TS_SET_LOGGER_SWITCH 'l'
#define TS_SET_LOGGER_SWITCH_char l
#define TS_SINGLE_WRITE_COMMAND 'W'
#define TS_SINGLE_WRITE_COMMAND_char W
#define TS_TEST_COMMAND 't'
#define TS_TEST_COMMAND_char t
#define TS_TOTAL_OUTPUT_SIZE 1016
class TsChannelBase {
public:
TsChannelBase(const char *name);
// Virtual functions - implement these for your underlying transport
virtual void write(const uint8_t* buffer, size_t size, bool isEndOfPacket = false) = 0;
virtual size_t readTimeout(uint8_t* buffer, size_t size, int timeout) = 0;
// These functions are optional to implement, not all channels need them
virtual void flush() { }
virtual bool isConfigured() const { return true; }
virtual bool isReady() const { return true; }
virtual void stop() { }
// Base functions that use the above virtual implementation
size_t read(uint8_t* buffer, size_t size);
#ifdef EFI_CAN_SERIAL
virtual // CAN device needs this function to be virtual for small-packet optimization
#endif
void writeCrcPacket(uint8_t responseCode, const uint8_t* buf, size_t size, bool allowLongPackets = false);
void sendResponse(ts_response_format_e mode, const uint8_t * buffer, int size, bool allowLongPackets = false);
/**
* See 'blockingFactor' in rusefi.ini
*/
char scratchBuffer[BLOCKING_FACTOR + 30];
const char *name;
void assertPacketSize(size_t size, bool allowLongPackets);
void crcAndWriteBuffer(uint8_t responseCode, size_t size);
void copyAndWriteSmallCrcPacket(uint8_t responseCode, const uint8_t* buf, size_t size);
private:
void writeCrcPacketLarge(uint8_t responseCode, const uint8_t* buf, size_t size);
};
// This class represents a channel for a physical async serial poart
class SerialTsChannelBase : public TsChannelBase {
public:
SerialTsChannelBase(const char *name) : TsChannelBase(name) {};
// Open the serial port with the specified baud rate
virtual void start(uint32_t baud) = 0;
};
#if HAL_USE_SERIAL
// This class implements a ChibiOS Serial Driver
class SerialTsChannel : public SerialTsChannelBase {
public:
SerialTsChannel(SerialDriver& driver) : SerialTsChannelBase("Serial"), m_driver(&driver) { }
void start(uint32_t baud) override;
void stop() override;
void write(const uint8_t* buffer, size_t size, bool isEndOfPacket) override;
size_t readTimeout(uint8_t* buffer, size_t size, int timeout) override;
private:
SerialDriver* const m_driver;
};
#endif // HAL_USE_SERIAL
#if HAL_USE_UART
// This class implements a ChibiOS UART Driver
class UartTsChannel : public SerialTsChannelBase {
public:
UartTsChannel(UARTDriver& driver) : SerialTsChannelBase("UART"), m_driver(&driver) { }
void start(uint32_t baud) override;
void stop() override;
void write(const uint8_t* buffer, size_t size, bool isEndOfPacket) override;
size_t readTimeout(uint8_t* buffer, size_t size, int timeout) override;
protected:
UARTDriver* const m_driver;
UARTConfig m_config;
};
#endif // HAL_USE_UART
#define CRC_VALUE_SIZE 4
// todo: double-check this
#define CRC_WRAPPING_SIZE (CRC_VALUE_SIZE + 3)
// that's 1 second
#define BINARY_IO_TIMEOUT TIME_MS2I(1000)
// that's 1 second
#define SR5_READ_TIMEOUT TIME_MS2I(1000)
void startSerialChannels();
SerialTsChannelBase* getBluetoothChannel();
void startCanConsole();
void sendOkResponse(TsChannelBase *tsChannel, ts_response_format_e mode);