@@ -17,57 +17,60 @@ int VescUart::receiveUartMessage(uint8_t* payloadReceived) {
1717 bool messageRead = false ;
1818 uint8_t messageReceived[256 ];
1919 uint16_t lenPayload = 0 ;
20- size_t bytes_read = 1 ;
21- const auto start = chVTGetSystemTimeX ();
22- while (chVTTimeElapsedSinceX (start) < TIME_US2I (_TIMEOUT) &&
23- messageRead == false ) {
24- while (uartReceiveTimeout (uart_, &bytes_read, messageReceived + counter,
25- _TIMEOUT) == MSG_OK) {
26- counter += bytes_read;
27- // read one more byte
28- bytes_read = 1 ;
29- if (counter == 2 ) {
30- switch (messageReceived[0 ]) {
31- case 2 :
32- endMessage = messageReceived[1 ] +
33- 5 ; // Payload size + 2 for sice + 3 for SRC and End.
34- lenPayload = messageReceived[1 ];
35-
36- if (endMessage >= sizeof (messageReceived)) {
37- // invalid message length, try again
38- counter = 0 ;
39- bytes_read = 1 ;
40- } else {
41- // we know exactly how many more bytes to read
42- bytes_read = endMessage - counter;
43- }
44- break ;
45-
46- case 3 :
47- // ToDo: Add Message Handling > 255 (starting with 3)
48-
49- break ;
50-
51- default :
52-
53- break ;
54- }
20+ size_t bytes_read = 0 ;
21+ size_t bytes_to_read = 1 ;
22+ while (bytes_to_read > 0 &&
23+ (bytes_read = sdReadTimeout (uart_, messageReceived + counter,
24+ bytes_to_read, TIME_MS2I (_TIMEOUT))) > 0 ) {
25+ counter += bytes_read;
26+ // keep on reading, if a larger chunk was requested
27+ bytes_to_read -= bytes_read;
28+ // if only one byte was requested, read another one
29+ if (bytes_to_read == 0 ) {
30+ bytes_to_read = 1 ;
31+ }
32+ if (counter == 2 ) {
33+ switch (messageReceived[0 ]) {
34+ case 2 :
35+ endMessage = messageReceived[1 ] +
36+ 5 ; // Payload size + 2 for sice + 3 for SRC and End.
37+ lenPayload = messageReceived[1 ];
38+
39+ if (endMessage >= sizeof (messageReceived)) {
40+ // invalid message length, try again
41+ counter = 0 ;
42+ bytes_to_read = 1 ;
43+ } else {
44+ // we know exactly how many more bytes to read
45+ bytes_to_read = endMessage - counter;
46+ }
47+ break ;
48+
49+ case 3 :
50+ // ToDo: Add Message Handling > 255 (starting with 3)
51+
52+ break ;
53+
54+ default :
55+
56+ break ;
5557 }
58+ }
5659
57- if (counter >= sizeof (messageReceived)) {
58- counter = 0 ;
59- break ;
60- }
60+ if (counter >= sizeof (messageReceived)) {
61+ counter = 0 ;
62+ break ;
63+ }
6164
62- if (counter == endMessage && messageReceived[endMessage - 1 ] == 3 ) {
63- messageReceived[endMessage] = 0 ;
65+ if (counter == endMessage && messageReceived[endMessage - 1 ] == 3 ) {
66+ messageReceived[endMessage] = 0 ;
6467
65- messageRead = true ;
66- break ; // Exit if end of message is reached, even if there is still
67- // more data in the buffer.
68- }
68+ messageRead = true ;
69+ break ; // Exit if end of message is reached, even if there is still
70+ // more data in the buffer.
6971 }
7072 }
73+
7174 // if(messageRead == false && debugPort != nullptr ) {
7275 // debugPort->println("Timeout");
7376 // }
@@ -214,17 +217,17 @@ bool VescUart::processReadPacket(uint8_t* message) {
214217 }
215218}
216219
217- VescUart::VescUart (UARTDriver * uart_handle, uint32_t timeout_ms)
220+ VescUart::VescUart (SerialDriver * uart_handle, uint32_t timeout_ms)
218221 : _TIMEOUT(timeout_ms), uart_(uart_handle) {
219222 nunchuck.valueX = 127 ;
220223 nunchuck.valueY = 127 ;
221224 nunchuck.lowerButton = false ;
222225 nunchuck.upperButton = false ;
226+ }
227+ bool VescUart::startDriver () {
223228 // acquire the UART and never let go
224- uartAcquireBus (uart_);
225- uart_config_.speed = 115200 ;
226- uart_config_.cr2 = USART_CR2_LINEN | USART_CR2_STOP1_BITS;
227- uartStart (uart_, &uart_config_);
229+ serial_config_.speed = 115200 ;
230+ return sdStart (uart_, &serial_config_) == MSG_OK;
228231}
229232bool VescUart::getFWversion (void ) { return getFWversion (0 ); }
230233
@@ -401,5 +404,5 @@ void VescUart::sendKeepalive(uint8_t canId) {
401404 packSendPayload (payload, payloadSize);
402405}
403406void VescUart::sendRaw (uint8_t * data, size_t size) {
404- uartSendFullTimeout (uart_, &size, data, TIME_INFINITE );
407+ sdWrite (uart_, data, size );
405408}
0 commit comments