-
Notifications
You must be signed in to change notification settings - Fork 1
Issue #18: ECU STM32 Implement Communication Hub #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a675619
a7736d3
8ae8753
3a0a825
2539608
ef13eef
3bb0f36
8765b6f
13dbec8
45e9975
4d1efe0
5769d45
e9ec544
998435f
b191a7e
1b4cfc0
a7e974d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| #include "UARTTransport.h" | ||
|
|
||
| #include <string.h> | ||
| #include <stdint.h> | ||
| #include "cmsis_os2.h" | ||
|
|
||
| #define SEND_BUFFER_SIZE 64 | ||
| #define RECV_BUFFER_SIZE 32 | ||
|
|
||
| static uint8_t sendBuffer[SEND_BUFFER_SIZE]; | ||
| static uint8_t recvBuffer[RECV_BUFFER_SIZE]; | ||
| static uint8_t recvSize; | ||
|
|
||
| static void (*dispatch_table[256])(void); | ||
|
|
||
| static osThreadAttr_t ta = { | ||
| .stack_size = 64 * 4 | ||
| }; | ||
|
|
||
| static void dispatch_unsupported(void) | ||
| { | ||
| sendBuffer[0] = 0xFF; | ||
| UARTTransport_send(sendBuffer, 1); | ||
| } | ||
|
|
||
| static void dispatch_01(void) | ||
| { | ||
| sendBuffer[0] = 0x01; | ||
| sendBuffer[1] = 0x01; | ||
| UARTTransport_send(sendBuffer, 2); | ||
| } | ||
|
|
||
| static void dispatch_02(void) | ||
| { | ||
| sendBuffer[0] = 0x02; | ||
|
|
||
| switch (recvBuffer[1]) { | ||
| case 0: | ||
| sendBuffer[1] = 0x0; | ||
| break; | ||
| case 1: | ||
| sendBuffer[1] = 0x0; | ||
| break; | ||
| case 2: | ||
| sendBuffer[1] = 0x0; | ||
| break; | ||
| case 3: | ||
| sendBuffer[1] = 0x0; | ||
| break; | ||
| default: | ||
| sendBuffer[1] = 0x01; | ||
| break; | ||
| } | ||
|
|
||
| UARTTransport_send(sendBuffer, 2); | ||
| } | ||
|
|
||
| static void dispatch_03(void) | ||
| { | ||
| sendBuffer[0] = 0x03; | ||
| sendBuffer[1] = 0x00; | ||
| UARTTransport_send(sendBuffer, 2); | ||
| } | ||
|
|
||
| static void dispatch_05(void) | ||
| { | ||
| sendBuffer[0] = 0x05; | ||
| memset(sendBuffer + 1, 0xBB, 16); | ||
| UARTTransport_send(sendBuffer, 17); | ||
| } | ||
|
|
||
| static void dispatch_06(void) | ||
| { | ||
| sendBuffer[0] = 0x06; | ||
| memset(sendBuffer + 1, 0xCC, 52); | ||
| UARTTransport_send(sendBuffer, 53); | ||
| } | ||
|
|
||
| void ProtocolHandler_processTask(void *d) | ||
| { | ||
| (void) d; | ||
|
|
||
| UARTTransport_init(); | ||
|
|
||
| for ( ; ; ) { | ||
| UARTTransport_receive(recvBuffer, &recvSize); | ||
| dispatch_table[recvBuffer[0]](); | ||
| } | ||
| } | ||
|
|
||
| void ProtocolHandler_init(void) | ||
| { | ||
| for (uint32_t i = 0; i < 256; i++) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this |
||
| dispatch_table[i] = dispatch_unsupported; | ||
| } | ||
|
|
||
| dispatch_table[1] = dispatch_01; | ||
| dispatch_table[2] = dispatch_02; | ||
| dispatch_table[3] = dispatch_03; | ||
| dispatch_table[5] = dispatch_05; | ||
| dispatch_table[6] = dispatch_06; | ||
|
|
||
| (void) osThreadNew(ProtocolHandler_processTask, NULL, &ta); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #ifndef __PROTOCOL_HANDLER_H | ||
| #define __PROTOCOL_HANDLER_H | ||
|
|
||
|
|
||
| void ProtocolHandler_init(void); | ||
|
|
||
|
|
||
| #endif /* __PROTOCOL_HANDLER_H */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| #include <string.h> | ||
|
|
||
| #include "UARTTransport.h" | ||
| #include "cmsis_os2.h" | ||
|
|
||
| #include "drvUart.h" | ||
|
|
||
| #define FLAG_TRANSMIT_FINISH 0x1 | ||
|
|
||
| static osMessageQueueId_t sendQ; | ||
| static osMessageQueueId_t recvQ; | ||
|
|
||
| static osEventFlagsId_t ef; | ||
|
|
||
| static osThreadAttr_t ta = { | ||
| .stack_size = 64 * 4 | ||
| }; | ||
|
|
||
| void UARTTransport_sendTask(void *d) | ||
| { | ||
| (void) d; | ||
|
|
||
| static uint8_t sendMessageBuffer[UART_TRANSPORT_SEND_BUFFER_SIZE]; | ||
|
|
||
| for ( ; ; ) { | ||
| if (osMessageQueueGet(sendQ, sendMessageBuffer, NULL, osWaitForever)) | ||
| continue; | ||
|
|
||
| drvUart_send(sendMessageBuffer); | ||
|
|
||
| osEventFlagsWait(ef, FLAG_TRANSMIT_FINISH, 0, osWaitForever); | ||
| } | ||
| } | ||
|
|
||
| void UARTTransport_onRxCplt(const uint8_t * const buffer) | ||
| { | ||
| (void) osMessageQueuePut(recvQ, buffer, 0, 0); | ||
| } | ||
|
|
||
| void UARTTransport_onTxCplt(void) | ||
| { | ||
| (void) osEventFlagsSet(ef, FLAG_TRANSMIT_FINISH); | ||
| } | ||
|
|
||
| void UARTTransport_init(void) | ||
| { | ||
| (void) drvUart_on_rx_cplt(UARTTransport_onRxCplt); | ||
| (void) drvUart_on_tx_cplt(UARTTransport_onTxCplt); | ||
|
|
||
| (void) drvUart_start(); | ||
|
|
||
| ef = osEventFlagsNew(NULL); | ||
|
|
||
| sendQ = osMessageQueueNew(16, UART_TRANSPORT_SEND_BUFFER_SIZE, NULL); | ||
| recvQ = osMessageQueueNew(16, UART_TRANSPORT_RECV_BUFFER_SIZE, NULL); | ||
|
|
||
| (void) osThreadNew(UARTTransport_sendTask, NULL, &ta); | ||
| } | ||
|
|
||
| void UARTTransport_receive(uint8_t * const buf, uint8_t * const size) | ||
| { | ||
| static uint8_t recvBuffer[UART_TRANSPORT_RECV_BUFFER_SIZE]; | ||
|
|
||
| (void) osMessageQueueGet(recvQ, recvBuffer, NULL, osWaitForever); | ||
|
|
||
| *size = recvBuffer[0] - 1; | ||
| memcpy(buf, recvBuffer + 1, *size); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here also I prefer control size. What if recvBuffer[0] == 0? |
||
| } | ||
|
|
||
| void UARTTransport_send(const uint8_t * const buf, const uint8_t size) | ||
| { | ||
| static uint8_t sendEncodingBuffer[UART_TRANSPORT_SEND_BUFFER_SIZE]; | ||
|
|
||
| sendEncodingBuffer[0] = size + 1; | ||
| memcpy(sendEncodingBuffer + 1, buf, size); | ||
|
|
||
| (void) osMessageQueuePut(sendQ, sendEncodingBuffer, 0, osWaitForever); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| #ifndef __UART_TRANSPORT_H | ||
| #define __UART_TRANSPORT_H | ||
|
|
||
| #include <stdint.h> | ||
|
|
||
| #define UART_TRANSPORT_RECV_BUFFER_SIZE 32 | ||
| #define UART_TRANSPORT_SEND_BUFFER_SIZE 64 | ||
|
|
||
| void UARTTransport_init(void); | ||
| void UARTTransport_receive(uint8_t * const buf, uint8_t * const size); | ||
| void UARTTransport_send(const uint8_t * const buf, const uint8_t size); | ||
|
|
||
| #endif /* __UART_TRANSPORT_H */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| #include "crc16.h" | ||
|
|
||
| static uint16_t crc_table[UINT8_MAX + 1]; | ||
|
|
||
| uint16_t crc16( const unsigned char *buf, unsigned int len ) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Signature mismatch |
||
| { | ||
| uint16_t crc = 0xFFFF; | ||
| char i = 0; | ||
|
|
||
| while(len--) | ||
| { | ||
| crc ^= (*buf++); | ||
|
|
||
| for(i = 0; i < 8; i++) | ||
| { | ||
| if( crc & 1 ) | ||
| { | ||
| crc >>= 1; | ||
| crc ^= 0xA001; | ||
| } | ||
| else | ||
| { | ||
| crc >>= 1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return crc; | ||
| } | ||
|
|
||
| uint16_t crc16_tmp1(uint8_t const * data, size_t size) | ||
| { | ||
| uint16_t crc = 0xFFFF; | ||
| while (size--) { | ||
| /* XOR-in next input byte into MSB of crc, that's our new intermediate dividend */ | ||
| uint8_t pos = (uint8_t)( (crc >> 8) ^ *data++); /* equal: ((crc ^ (b << 8)) >> 8) */ | ||
| /* Shift out the MSB used for division per lookuptable and XOR with the remainder */ | ||
| crc = (uint16_t)((crc << 8) ^ (uint16_t)(crc_table[pos])); | ||
| } | ||
| return crc; | ||
| } | ||
|
|
||
| void crc16_fillTable() | ||
| { | ||
| const uint16_t generator = 0x8005; | ||
| for (int dividend = 0; dividend < UINT8_MAX + 1; ++dividend) { | ||
| uint16_t current_byte = dividend << 8; | ||
|
|
||
| for (uint8_t bit = 0; bit < 8; ++bit) { | ||
| if ((current_byte & 0x8000) != 0) { | ||
| current_byte <<= 1; | ||
| current_byte ^= generator; | ||
| } | ||
| else { | ||
| current_byte <<= 1; | ||
| } | ||
| } | ||
| crc_table[dividend] = current_byte; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| #ifndef COMMUNICATION_CRC16_H_ | ||
| #define COMMUNICATION_CRC16_H_ | ||
|
|
||
| #include <stdint.h> | ||
| #include <stddef.h> | ||
|
|
||
| uint16_t crc16(uint8_t const *data, size_t size); | ||
| void crc16_fillTable(void); | ||
|
|
||
| #endif /* COMMUNICATION_CRC16_H_ */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add if to check range.