22
33#define WAIT_FOR_TXE () while (!(UART1->SR & UART1_SR_TXE))
44
5- static inline uint16_t v2styxlib_uart_crc16_update (uint16_t crc , uint8_t data ) {
6- crc ^= (uint16_t )data << 8 ;
7- for (uint8_t i = 0 ; i < 8 ; i ++ ) {
8- if (crc & 0x8000 ) {
9- crc = (crc << 1 ) ^ V2STYXLIB_CRC16_POLY ;
10- } else {
11- crc <<= 1 ;
12- }
13- }
14- return crc ;
15- }
16-
17- static uint16_t v2styxlib_uart_crc16_calculate (const uint8_t * data , BufferSize_t length ) {
18- uint16_t crc = V2STYXLIB_CRC16_INITIAL_VALUE ;
19- for (BufferSize_t i = 0 ; i < length ; i ++ ) {
20- crc = v2styxlib_uart_crc16_update (crc , data [i ]);
21- }
22- return crc ;
23- }
24-
25- void v2styxlib_uart_configure_proto (
26- V2styxlibUartConfig * config ,
27- bool useStreamingMode
28- ) {
29- if (useStreamingMode ) {
30- config -> config |= V2STYXLIB_STREAMING_MODE ;
31- } else {
32- config -> config &= ~V2STYXLIB_STREAMING_MODE ;
33- }
34- }
35-
365void v2styxlib_uart_setup (uint16_t baudRateDivider )
376{
387 UART1 -> BRR2 = ((baudRateDivider >> 8 ) & 0xF0 ) | (baudRateDivider & 0x0F );
@@ -54,16 +23,20 @@ void v2styxlib_uart_send(
5423 UART1 -> DR = V2STYXLIB_SOF_MARKER_2 ;
5524 }
5625
57- // send packet size
58- WAIT_FOR_TXE ();
59- UART1 -> DR = length + 2 ; // +2 for CRC16
60-
61- // then CRC16
62- uint16_t crc = v2styxlib_uart_crc16_calculate (buffer , length );
63- WAIT_FOR_TXE ();
64- UART1 -> DR = (crc >> 8 ) & 0xFF ; // send high byte of CRC
6526 WAIT_FOR_TXE ();
66- UART1 -> DR = crc & 0xFF ; // send low byte of CRC
27+ if (config -> config & V2STYXLIB_SEND_CRC16 ) {
28+ // send packet size + 2 bytes for CRC16
29+ UART1 -> DR = length + 2 ;
30+ // then CRC16
31+ uint16_t crc = v2styxlib_crc16_calculate (buffer , length );
32+ WAIT_FOR_TXE ();
33+ UART1 -> DR = (crc >> 8 ) & 0xFF ; // send high byte of CRC
34+ WAIT_FOR_TXE ();
35+ UART1 -> DR = crc & 0xFF ; // send low byte of CRC
36+ } else {
37+ // send packet size
38+ UART1 -> DR = length ;
39+ }
6740
6841 // then send the actual data
6942 for (BufferSize_t i = 0 ; i < length ; i ++ ) {
0 commit comments