rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
openblt_usb.cpp
Go to the documentation of this file.
1#include "pch.h"
2#include "usbcfg.h"
3#include "usbconsole.h"
4
5extern "C" {
6 #include "boot.h"
7 #include "rs232.h"
8}
9
11
12void Rs232Init() {
13#if (BOOT_BACKDOOR_ENTRY_TIMEOUT_MS == 0)
14 if (stayInBootloader || (NvmVerifyChecksum() == BLT_FALSE))
15#endif
16 {
17 // Set up USB serial
19 }
20}
21
22#define RS232_CTO_RX_PACKET_TIMEOUT_MS (100u)
23
25static void Rs232TransmitByte(blt_int8u data);
26
27/************************************************************************************//**
28** \brief Transmits a packet formatted for the communication interface.
29** \param data Pointer to byte array with data that it to be transmitted.
30** \param len Number of bytes that are to be transmitted.
31** \return none.
32**
33****************************************************************************************/
35{
36
37 /* verify validity of the len-paramenter */
38 // ASSERT_RT(len <= BOOT_COM_RS232_TX_MAX_DATA);
39
40 /* first transmit the length of the packet */
42
43 chnWriteTimeout(&SDU1, data, len, TIME_INFINITE);
44} /*** end of Rs232TransmitPacket ***/
45
46PUBLIC_API_WEAK void openBltUnexpectedByte(blt_int8u firstByte) {
47#if defined(OPEN_BLT_TEST_COMMAND)
48// 'z' is right at the end of 128 ascii range
49static_assert(BOOT_COM_RS232_RX_MAX_DATA < 'z');
50 if (firstByte == 'z') {
51 const char * bltTest = "openblt\n";
52 chnWriteTimeout(&SDU1, (const uint8_t*)bltTest, sizeof(bltTest), TIME_INFINITE);
53 }
54#endif // OPEN_BLT_TEST_COMMAND
55}
56
57/************************************************************************************//**
58** \brief Receives a communication interface packet if one is present.
59** \param data Pointer to byte array where the data is to be stored.
60** \param len Pointer where the length of the packet is to be stored.
61** \return BLT_TRUE if a packet was received, BLT_FALSE otherwise.
62**
63****************************************************************************************/
65{
66 static blt_int8u xcpCtoReqPacket[BOOT_COM_RS232_RX_MAX_DATA+1]; /* one extra for length */
67 static blt_int8u xcpCtoRxLength;
68 static blt_bool xcpCtoRxInProgress = BLT_FALSE;
69 static blt_int32u xcpCtoRxStartTime = 0;
70
71 /* start of cto packet received? */
72 if (xcpCtoRxInProgress == BLT_FALSE)
73 {
74 /* store the message length when received */
75 if (Rs232ReceiveByte(&xcpCtoReqPacket[0]) == BLT_TRUE)
76 {
77 if ( (xcpCtoReqPacket[0] > 0) &&
78 (xcpCtoReqPacket[0] <= BOOT_COM_RS232_RX_MAX_DATA) )
79 {
80 /* store the start time */
81 xcpCtoRxStartTime = TimerGet();
82 /* reset packet data count */
83 xcpCtoRxLength = 0;
84 /* indicate that a cto packet is being received */
85 xcpCtoRxInProgress = BLT_TRUE;
86 } else {
87 openBltUnexpectedByte(xcpCtoReqPacket[0]);
88 }
89 }
90 }
91 else
92 {
93 /* store the next packet byte */
94 if (Rs232ReceiveByte(&xcpCtoReqPacket[xcpCtoRxLength+1]) == BLT_TRUE)
95 {
96 /* increment the packet data count */
97 xcpCtoRxLength++;
98
99 /* check to see if the entire packet was received */
100 if (xcpCtoRxLength == xcpCtoReqPacket[0])
101 {
102 /* copy the packet data */
103 CpuMemCopy((blt_int32u)data, (blt_int32u)&xcpCtoReqPacket[1], xcpCtoRxLength);
104 /* done with cto packet reception */
105 xcpCtoRxInProgress = BLT_FALSE;
106 /* set the packet length */
107 *len = xcpCtoRxLength;
108 /* packet reception complete */
109 return BLT_TRUE;
110 }
111 }
112 else
113 {
114 /* check packet reception timeout */
115 if (TimerGet() > (xcpCtoRxStartTime + RS232_CTO_RX_PACKET_TIMEOUT_MS))
116 {
117 /* cancel cto packet reception due to timeout. note that that automaticaly
118 * discards the already received packet bytes, allowing the host to retry.
119 */
120 xcpCtoRxInProgress = BLT_FALSE;
121 }
122 }
123 }
124 /* packet reception not yet complete */
125 return BLT_FALSE;
126} /*** end of Rs232ReceivePacket ***/
127
129{
130 if (!is_usb_serial_ready()) {
131 return BLT_FALSE;
132 }
133
134 auto bytesRead = chnReadTimeout(&SDU1, data, 1, TIME_IMMEDIATE);
135
136 return bytesRead == 0 ? BLT_FALSE : BLT_TRUE;
137}
138
140{
141 chnWriteTimeout(&SDU1, &data, 1, TIME_INFINITE);
142}
BaseChannel SDU1
bool is_usb_serial_ready()
void usb_serial_start(void)
Main function of PDL.
blt_bool NvmVerifyChecksum(void)
Verifies the checksum, which indicates that a valid user program is present and can be started.
Definition nvm.c:108
void CpuMemCopy(blt_addr dest, blt_addr src, blt_int16u len)
blt_int32u TimerGet()
blt_bool stayInBootloader
blt_bool Rs232ReceivePacket(blt_int8u *data, blt_int8u *len)
Receives a communication interface packet if one is present.
void Rs232TransmitPacket(blt_int8u *data, blt_int8u len)
Transmits a packet formatted for the communication interface.
PUBLIC_API_WEAK void openBltUnexpectedByte(blt_int8u firstByte)
void Rs232Init()
static blt_bool Rs232ReceiveByte(blt_int8u *data)
static void Rs232TransmitByte(blt_int8u data)
unsigned char blt_int8u
Definition types.h:49
unsigned char blt_bool
Definition types.h:46
unsigned int blt_int32u
Definition types.h:53