rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
tunerstudio_io.h
Go to the documentation of this file.
1/**
2 * @file tunerstudio_io.h
3 * @file TS protocol commands and methods are here
4 *
5 * @date Mar 8, 2015
6 * @author Andrey Belomutskiy, (c) 2012-2020
7 */
8
9#pragma once
10#include "global.h"
11#include "tunerstudio_impl.h"
12#include "page_1_generated.h"
13
14#if EFI_USB_SERIAL
15#include "usbconsole.h"
16#endif // EFI_USB_SERIAL
17
18#if EFI_PROD_CODE
19#include "pin_repository.h"
20#endif
21
22#ifndef USART_CR2_STOP1_BITS
23// todo: acticulate why exactly does prometheus_469 as for this hack
24#define USART_CR2_STOP1_BITS 0
25#endif
26
27#define TS_PACKET_HEADER_SIZE 3
28#define TS_PACKET_TAIL_SIZE 4
29
31public:
32 TsChannelBase(const char *name);
33 // Virtual functions - implement these for your underlying transport
34 virtual void write(const uint8_t* buffer, size_t size, bool isEndOfPacket = false) = 0;
35 virtual size_t readTimeout(uint8_t* buffer, size_t size, int timeout) = 0;
36
37 // These functions are optional to implement, not all channels need them
38 virtual void flush() { }
39 virtual bool isConfigured() const { return true; }
40 virtual bool isReady() const { return true; }
41 virtual void stop() { }
42
43 // Base functions that use the above virtual implementation
44 size_t read(uint8_t* buffer, size_t size);
45
46 int bytesIn = 0;
47 int bytesOut = 0;
48
49#ifdef EFI_CAN_SERIAL
50 virtual // CAN device needs this function to be virtual for small-packet optimization
51#endif
52 void writeCrcPacket(uint8_t responseCode, const uint8_t* buf, size_t size, bool allowLongPackets = false);
53 void sendResponse(ts_response_format_e mode, const uint8_t * buffer, int size, bool allowLongPackets = false);
54
55#ifdef CUSTOM_TS_BUFFER_SIZE
56 #define scratchBuffer_SIZE CUSTOM_TS_BUFFER_SIZE
57#else
58 #define scratchBuffer_SIZE BLOCKING_FACTOR
59#endif // CUSTOM_TS_BUFFER
60
61 /**
62 * See 'blockingFactor' in rusefi.ini
63 */
64 char scratchBuffer[scratchBuffer_SIZE + 30];
65#if EFI_TS_SCATTER
67#endif
68 const char *name;
69
70 void assertPacketSize(size_t size, bool allowLongPackets);
71 uint32_t writePacketHeader(const uint8_t responseCode, const size_t size);
72 uint32_t writePacketBody(const uint8_t* buf, const size_t size, uint32_t crc);
73 void writeCrcPacketTail(uint32_t crc);
74 void crcAndWriteBuffer(const uint8_t responseCode, const size_t size);
75 void copyAndWriteSmallCrcPacket(uint8_t responseCode, const uint8_t* buf, size_t size);
76
77 // Write a response code with no data
78 void writeCrcResponse(uint8_t responseCode) {
79 writeCrcPacketLarge(responseCode, nullptr, 0);
80 }
81
82 /* When TsChannel is in "not in sync" state tsProcessOne will silently try to find
83 * begining of packet.
84 * As soon as tsProcessOne was able to receive valid packet with valid size and crc
85 * TsChannel becomes "in sync". That means it will react on any futher errors: it will
86 * emit packet with error code and switch back to "not in sync" mode.
87 * This insures that RE will send only one error message after lost of synchronization
88 * with TS.
89 * Also while in "not in sync" state - tsProcessOne will not try to receive whole packet
90 * by one read. Instead after getting packet size it will try to receive one byte of
91 * command and check if it is supported. */
92 bool in_sync = false;
93
94private:
95 bool isBigPacket(size_t size);
96 void writeCrcPacketLarge(uint8_t responseCode, const uint8_t* buf, size_t size);
97};
98
99// This class represents a channel for a physical async serial poart
101public:
102 SerialTsChannelBase(const char *p_name) : TsChannelBase(p_name) {};
103 // Open the serial port with the specified baud rate
104 virtual void start(uint32_t baud) = 0;
105};
106
107#if HAL_USE_SERIAL
108// This class implements a ChibiOS Serial Driver
110public:
111 SerialTsChannel(SerialDriver& driver) : SerialTsChannelBase("Serial"), m_driver(&driver) { }
112
113 void start(uint32_t baud) override;
114 void stop() override;
115
116 void write(const uint8_t* buffer, size_t size, bool isEndOfPacket) override;
117 size_t readTimeout(uint8_t* buffer, size_t size, int timeout) override;
118
119private:
120 SerialDriver* const m_driver;
121};
122#endif // HAL_USE_SERIAL
123
124#if HAL_USE_UART
125// This class implements a ChibiOS UART Driver
127public:
128 UartTsChannel(UARTDriver& driver) : SerialTsChannelBase("UART"), m_driver(&driver) { }
129
130 void start(uint32_t baud) override;
131 void stop() override;
132
133 void write(const uint8_t* buffer, size_t size, bool isEndOfPacket) override;
134 size_t readTimeout(uint8_t* buffer, size_t size, int timeout) override;
135
136protected:
139};
140#endif // HAL_USE_UART
141
142// that's 1 second
143#define BINARY_IO_TIMEOUT TIME_MS2I(1000)
144
145// that's 1 second
146#define SR5_READ_TIMEOUT TIME_MS2I(1000)
147
150
151void startCanConsole();
virtual void start(uint32_t baud)=0
SerialTsChannelBase(const char *p_name)
SerialDriver *const m_driver
void write(const uint8_t *buffer, size_t size, bool isEndOfPacket) override
void start(uint32_t baud) override
size_t readTimeout(uint8_t *buffer, size_t size, int timeout) override
SerialTsChannel(SerialDriver &driver)
virtual void stop()
void crcAndWriteBuffer(const uint8_t responseCode, const size_t size)
virtual bool isReady() const
void copyAndWriteSmallCrcPacket(uint8_t responseCode, const uint8_t *buf, size_t size)
size_t read(uint8_t *buffer, size_t size)
virtual void flush()
const char * name
bool isBigPacket(size_t size)
char scratchBuffer[scratchBuffer_SIZE+30]
uint32_t writePacketHeader(const uint8_t responseCode, const size_t size)
uint32_t writePacketBody(const uint8_t *buf, const size_t size, uint32_t crc)
void writeCrcPacketLarge(uint8_t responseCode, const uint8_t *buf, size_t size)
virtual bool isConfigured() const
void writeCrcResponse(uint8_t responseCode)
void writeCrcPacketTail(uint32_t crc)
void assertPacketSize(size_t size, bool allowLongPackets)
virtual void writeCrcPacket(uint8_t responseCode, const uint8_t *buf, size_t size, bool allowLongPackets=false)
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
void sendResponse(ts_response_format_e mode, const uint8_t *buffer, int size, bool allowLongPackets=false)
UARTDriver *const m_driver
UARTConfig m_config
void start(uint32_t baud) override
size_t readTimeout(uint8_t *buffer, size_t size, int timeout) override
void write(const uint8_t *buffer, size_t size, bool isEndOfPacket) override
UartTsChannel(UARTDriver &driver)
I/O pin registry header.
Driver configuration structure.
Structure representing an UART driver.
composite packet size
static BigBufferHandle buffer
ts_response_format_e
void startCanConsole()
void startSerialChannels()
SerialTsChannelBase * getBluetoothChannel()