Setup UART framework
This commit is contained in:
parent
0fa53919e0
commit
a4c071bbd0
@ -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_ */
|
||||
|
||||
18
Core/Inc/usart.h
Normal file
18
Core/Inc/usart.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef USART_H_
|
||||
#define USART_H_
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
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
|
||||
@ -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++) {
|
||||
@ -5,6 +5,8 @@
|
||||
#include "monomaniacone14pt.h"
|
||||
#include "monomaniacone20pt.h"
|
||||
#include "monomaniacone72pt.h"
|
||||
#include "usart.h"
|
||||
#include "crc.h"
|
||||
#include <stdio.h>
|
||||
|
||||
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
|
||||
@ -430,14 +463,14 @@ void draw_temps(int16_t temp_fet_scaled, int16_t temp_motor_scaled) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
78
Core/Src/usart.c
Normal file
78
Core/Src/usart.c
Normal file
@ -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
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user