From a4c071bbd07e86e72e137f7a2e67f75ca6625ccf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cle=CC=81ment=20Grennerat?= Date: Tue, 16 Sep 2025 12:44:17 +0200 Subject: [PATCH] Setup UART framework --- Core/Inc/crc.h | 2 +- Core/Inc/usart.h | 18 +++++++++ Core/Src/{crc.cpp => crc.c} | 2 +- Core/Src/dashboard.c | 44 ++++++++++++++++++--- Core/Src/usart.c | 78 +++++++++++++++++++++++++++++++++++++ 5 files changed, 137 insertions(+), 7 deletions(-) create mode 100644 Core/Inc/usart.h rename Core/Src/{crc.cpp => crc.c} (97%) create mode 100644 Core/Src/usart.c diff --git a/Core/Inc/crc.h b/Core/Inc/crc.h index 1724b6a..b753918 100644 --- a/Core/Inc/crc.h +++ b/Core/Inc/crc.h @@ -25,6 +25,6 @@ /* * Functions */ -unsigned short crc16(unsigned char *buf, unsigned int len); +unsigned short crc16(const unsigned char *buf, unsigned int len); #endif /* CRC_H_ */ diff --git a/Core/Inc/usart.h b/Core/Inc/usart.h new file mode 100644 index 0000000..27bf69e --- /dev/null +++ b/Core/Inc/usart.h @@ -0,0 +1,18 @@ +#ifndef USART_H_ +#define USART_H_ + + +#include + +void USART1_SendPacket(const uint8_t *data, uint8_t length); +uint8_t USART1_ReceiveByte(void); +uint8_t USART1_ReceiveByteTimeout(uint32_t timeout); +void USART1_Flush(void); + + +uint16_t USART1_ReceiveUInt16(void); +int16_t USART1_ReceiveInt16(void); +uint32_t USART1_ReceiveUInt32(void); +int32_t USART1_ReceiveInt32(void); + +#endif diff --git a/Core/Src/crc.cpp b/Core/Src/crc.c similarity index 97% rename from Core/Src/crc.cpp rename to Core/Src/crc.c index 2691acf..bbfee50 100644 --- a/Core/Src/crc.cpp +++ b/Core/Src/crc.c @@ -50,7 +50,7 @@ const unsigned short crc16_tab[] = { 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x0cc1, 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 }; -unsigned short crc16(unsigned char *buf, unsigned int len) { +unsigned short crc16(const unsigned char *buf, unsigned int len) { unsigned int i; unsigned short cksum = 0; for (i = 0; i < len; i++) { diff --git a/Core/Src/dashboard.c b/Core/Src/dashboard.c index d39f192..4acffb2 100644 --- a/Core/Src/dashboard.c +++ b/Core/Src/dashboard.c @@ -5,6 +5,8 @@ #include "monomaniacone14pt.h" #include "monomaniacone20pt.h" #include "monomaniacone72pt.h" +#include "usart.h" +#include "crc.h" #include void run_dashboard_loop() { @@ -13,6 +15,10 @@ void run_dashboard_loop() { uint32_t i = 0; while (1) { + if (i % 10 == 0) { + update_values(); + } + draw_battery(654, i * 100 / 4, 1321, 343); draw_speed(i * 1000 / 4); int16_t duty = 1000 - (int16_t) i * 1000 / 200; @@ -37,8 +43,35 @@ void init() { LCD_Init(); draw_init(); } + void update_values() { // UART send 0201 04 4084 03 + + uint8_t packet[1]; + packet[0] = 0x04;// PAYLOAD (COMM_GET_VALUES) + USART1_SendPacket(packet, 1); + +// Wait for start byte (0x02) + while (USART1_ReceiveByte() != 0x02) { + } + ; +// Read length (big-endian) + uint16_t len = USART1_ReceiveUInt16() & 0xFF; +// Read packet + uint8_t data[len]; + for (uint16_t i = 0; i < len - 2; i++) { + data[i] = USART1_ReceiveByte(); + } + uint16_t crc = USART1_ReceiveUInt16(); +// End byte + if (USART1_ReceiveByte() != 0x03) { + return;// Invalid packet + } +// CRC check + if (crc != crc16(data, len)) { + return; + } + __NOP(); } void update_values_setup() { // UART send 0201 2F D58D 03 @@ -423,21 +456,21 @@ uint16_t last_temp_mot = 1000; void draw_temps(int16_t temp_fet_scaled, int16_t temp_motor_scaled) { uint16_t temp_fet = temp_fet_scaled < 0 ? 0 : temp_fet_scaled / 10; uint16_t temp_mot = temp_motor_scaled < 0 ? 0 : temp_motor_scaled / 10; - if(temp_fet > 99) { + if (temp_fet > 99) { temp_fet = 99; } - if(temp_mot > 99) { + if (temp_mot > 99) { temp_mot = 99; } - if (temp_mot != last_temp_mot) { last_temp_mot = temp_mot; char text[8]; sprintf(text, "%u>C", temp_mot); LCD_Draw_Rectangle(LEFT_CENTER_COL1 - 38, 138, 2 * 39, 25, COLOR_BG); - GFX_DrawText(LEFT_CENTER_COL1, 159, text, &monomaniacone14pt, COLOR_SECONDARY, COLOR_BG, 1, -1); + GFX_DrawText(LEFT_CENTER_COL1, 159, text, &monomaniacone14pt, + COLOR_SECONDARY, COLOR_BG, 1, -1); } if (temp_fet != last_temp_fet) { last_temp_fet = temp_fet; @@ -445,6 +478,7 @@ void draw_temps(int16_t temp_fet_scaled, int16_t temp_motor_scaled) { char text[8]; sprintf(text, "%u>C", temp_fet); LCD_Draw_Rectangle(LEFT_CENTER_COL2 - 39, 138, 2 * 39, 25, COLOR_BG); - GFX_DrawText(LEFT_CENTER_COL2, 159, text, &monomaniacone14pt, COLOR_PRIMARY, COLOR_BG, 1, -1); + GFX_DrawText(LEFT_CENTER_COL2, 159, text, &monomaniacone14pt, COLOR_PRIMARY, + COLOR_BG, 1, -1); } } diff --git a/Core/Src/usart.c b/Core/Src/usart.c new file mode 100644 index 0000000..6ce0806 --- /dev/null +++ b/Core/Src/usart.c @@ -0,0 +1,78 @@ +#include "usart.h" +#include "crc.h" +#include "stm32f1xx.h" +#include "main.h" + + + +// USART1 Send Byte (blocking) +void USART1_SendByte(uint8_t byte) { + while (!(USART1->SR & USART_SR_TXE)); // Wait until transmit buffer empty + USART1->DR = byte; // Send byte + while (!(USART1->SR & USART_SR_TC)); // Wait until transmission complete +} + +// USART1 Receive Byte (blocking) +uint8_t USART1_ReceiveByte(void) { + while (!(USART1->SR & USART_SR_RXNE)); // Wait until data received + return USART1->DR; // Return received byte +} +// Receive byte with timeout (returns 0xFF if timeout) +uint8_t USART1_ReceiveByteTimeout(uint32_t timeout) { + uint32_t start = HAL_GetTick(); + while (!(USART1->SR & USART_SR_RXNE)) { + if (HAL_GetTick() - start > timeout) { + return 0xFF; // Timeout + } + } + return USART1->DR; +} +// Flush receive buffer +void USART1_Flush(void) { + while (USART1->SR & USART_SR_RXNE) { + USART1->DR; // Read and discard + } +} + +void USART1_SendPacket(const uint8_t *data, uint8_t length) { + + uint16_t crc = crc16(data, length); + uint8_t crc_h = (crc >> 8) & 0xFF; // CRC high byte (big-endian) + uint8_t crc_l = crc & 0xFF; // CRC low byte + + USART1_SendByte(0x02); // Start byte + USART1_SendByte(0x02); // length type < 256 + USART1_SendByte(length); + for (uint16_t i = 0; i < length; i++) { + USART1_SendByte(data[i]); + } + USART1_SendByte(crc_h); + USART1_SendByte(crc_l); + USART1_SendByte(0x03); +} +void USART1_ReceivePacket(void) { + +} + +// Receive data types (big-endian) +uint16_t USART1_ReceiveUInt16(void) { + uint16_t value = (uint16_t)USART1_ReceiveByte() << 8; // MSB first + value |= USART1_ReceiveByte(); // LSB second + return value; +} + +int16_t USART1_ReceiveInt16(void) { + return (int16_t) USART1_ReceiveUInt16(); // Same byte order, just cast +} + +uint32_t USART1_ReceiveUInt32(void) { + uint32_t value = (uint32_t)USART1_ReceiveByte() << 24; // MSB first + value |= (uint32_t)USART1_ReceiveByte() << 16; + value |= (uint32_t)USART1_ReceiveByte() << 8; + value |= USART1_ReceiveByte(); // LSB last + return value; +} + +int32_t USART1_ReceiveInt32(void) { + return (int32_t)USART1_ReceiveUInt32(); // Same byte order, just cast +}