Line |
Branch |
Decision |
Exec |
Source |
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 |
|
|
|
|
30 |
|
|
|
class TsChannelBase { |
31 |
|
|
|
public: |
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 |
|
|
4 |
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 |
66 |
|
|
|
page1_s page1; |
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 |
|
|
|
void crcAndWriteBuffer(const uint8_t responseCode, const size_t size); |
73 |
|
|
|
void copyAndWriteSmallCrcPacket(uint8_t responseCode, const uint8_t* buf, size_t size); |
74 |
|
|
|
|
75 |
|
|
|
// Write a response code with no data |
76 |
|
|
✗ |
void writeCrcResponse(uint8_t responseCode) { |
77 |
|
|
✗ |
writeCrcPacketLarge(responseCode, nullptr, 0); |
78 |
|
|
✗ |
} |
79 |
|
|
|
|
80 |
|
|
|
/* When TsChannel is in "not in sync" state tsProcessOne will silently try to find |
81 |
|
|
|
* begining of packet. |
82 |
|
|
|
* As soon as tsProcessOne was able to receive valid packet with valid size and crc |
83 |
|
|
|
* TsChannel becomes "in sync". That means it will react on any futher errors: it will |
84 |
|
|
|
* emit packet with error code and switch back to "not in sync" mode. |
85 |
|
|
|
* This insures that RE will send only one error message after lost of synchronization |
86 |
|
|
|
* with TS. |
87 |
|
|
|
* Also while in "not in sync" state - tsProcessOne will not try to receive whole packet |
88 |
|
|
|
* by one read. Instead after getting packet size it will try to receive one byte of |
89 |
|
|
|
* command and check if it is supported. */ |
90 |
|
|
|
bool in_sync = false; |
91 |
|
|
|
|
92 |
|
|
|
private: |
93 |
|
|
|
bool isBigPacket(size_t size); |
94 |
|
|
|
void writeCrcPacketLarge(uint8_t responseCode, const uint8_t* buf, size_t size); |
95 |
|
|
|
}; |
96 |
|
|
|
|
97 |
|
|
|
// This class represents a channel for a physical async serial poart |
98 |
|
|
|
class SerialTsChannelBase : public TsChannelBase { |
99 |
|
|
|
public: |
100 |
|
|
|
SerialTsChannelBase(const char *p_name) : TsChannelBase(p_name) {}; |
101 |
|
|
|
// Open the serial port with the specified baud rate |
102 |
|
|
|
virtual void start(uint32_t baud) = 0; |
103 |
|
|
|
}; |
104 |
|
|
|
|
105 |
|
|
|
#if HAL_USE_SERIAL |
106 |
|
|
|
// This class implements a ChibiOS Serial Driver |
107 |
|
|
|
class SerialTsChannel final : public SerialTsChannelBase { |
108 |
|
|
|
public: |
109 |
|
|
|
SerialTsChannel(SerialDriver& driver) : SerialTsChannelBase("Serial"), m_driver(&driver) { } |
110 |
|
|
|
|
111 |
|
|
|
void start(uint32_t baud) override; |
112 |
|
|
|
void stop() override; |
113 |
|
|
|
|
114 |
|
|
|
void write(const uint8_t* buffer, size_t size, bool isEndOfPacket) override; |
115 |
|
|
|
size_t readTimeout(uint8_t* buffer, size_t size, int timeout) override; |
116 |
|
|
|
|
117 |
|
|
|
private: |
118 |
|
|
|
SerialDriver* const m_driver; |
119 |
|
|
|
}; |
120 |
|
|
|
#endif // HAL_USE_SERIAL |
121 |
|
|
|
|
122 |
|
|
|
#if HAL_USE_UART |
123 |
|
|
|
// This class implements a ChibiOS UART Driver |
124 |
|
|
|
class UartTsChannel : public SerialTsChannelBase { |
125 |
|
|
|
public: |
126 |
|
|
|
UartTsChannel(UARTDriver& driver) : SerialTsChannelBase("UART"), m_driver(&driver) { } |
127 |
|
|
|
|
128 |
|
|
|
void start(uint32_t baud) override; |
129 |
|
|
|
void stop() override; |
130 |
|
|
|
|
131 |
|
|
|
void write(const uint8_t* buffer, size_t size, bool isEndOfPacket) override; |
132 |
|
|
|
size_t readTimeout(uint8_t* buffer, size_t size, int timeout) override; |
133 |
|
|
|
|
134 |
|
|
|
protected: |
135 |
|
|
|
UARTDriver* const m_driver; |
136 |
|
|
|
UARTConfig m_config; |
137 |
|
|
|
}; |
138 |
|
|
|
#endif // HAL_USE_UART |
139 |
|
|
|
|
140 |
|
|
|
// that's 1 second |
141 |
|
|
|
#define BINARY_IO_TIMEOUT TIME_MS2I(1000) |
142 |
|
|
|
|
143 |
|
|
|
// that's 1 second |
144 |
|
|
|
#define SR5_READ_TIMEOUT TIME_MS2I(1000) |
145 |
|
|
|
|
146 |
|
|
|
void startSerialChannels(); |
147 |
|
|
|
SerialTsChannelBase* getBluetoothChannel(); |
148 |
|
|
|
|
149 |
|
|
|
void startCanConsole(); |
150 |
|
|
|
|