mirror of
https://github.com/DashyFox/StackSport.git
synced 2025-05-04 07:10:17 +00:00
Working UART
This commit is contained in:
parent
cad6b45c5d
commit
2ef50d8b07
@ -17,6 +17,39 @@ typedef enum ServoMap{
|
|||||||
SERVO_VERTICAL = 2
|
SERVO_VERTICAL = 2
|
||||||
}ServoMap;
|
}ServoMap;
|
||||||
|
|
||||||
|
typedef enum CurrentMode {
|
||||||
|
NoneMode, ShotMode, ProgramMode, MacroMode,
|
||||||
|
} Mode;
|
||||||
|
|
||||||
|
typedef struct CurrentShot {
|
||||||
|
uint8_t index;
|
||||||
|
Shot shot;
|
||||||
|
} CurrentShot;
|
||||||
|
|
||||||
|
typedef struct CurrentProgram {
|
||||||
|
uint8_t index;
|
||||||
|
Program program;
|
||||||
|
uint8_t shot_index;
|
||||||
|
} CurrentProgram;
|
||||||
|
|
||||||
|
typedef struct CurrentMacro {
|
||||||
|
uint8_t index;
|
||||||
|
Macro macro;
|
||||||
|
uint8_t program_index;
|
||||||
|
} CurrentMacro;
|
||||||
|
|
||||||
|
typedef struct CurrentState {
|
||||||
|
uint8_t isPause;
|
||||||
|
uint8_t isShooting;
|
||||||
|
} CurrentState;
|
||||||
|
|
||||||
|
typedef struct CurrentInfo {
|
||||||
|
Mode mode;
|
||||||
|
CurrentState state;
|
||||||
|
CurrentShot shot;
|
||||||
|
CurrentProgram program;
|
||||||
|
CurrentMacro macro;
|
||||||
|
} CurrentInfo;
|
||||||
|
|
||||||
|
|
||||||
void doShot(Shot*);
|
void doShot(Shot*);
|
||||||
|
@ -124,6 +124,7 @@ MemoryStatus EEPROM_EARSE() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
MemoryStatus EEPROM_INIT() {
|
MemoryStatus EEPROM_INIT() {
|
||||||
|
memset(&infoBlock, 0x00, sizeof(infoBlock));
|
||||||
MemoryStatus status = getInfoBlock(&infoBlock);
|
MemoryStatus status = getInfoBlock(&infoBlock);
|
||||||
if (status != EEPROM_OK) {
|
if (status != EEPROM_OK) {
|
||||||
char errorMsg[] = "Error reading InfoBlock from EEPROM\n";
|
char errorMsg[] = "Error reading InfoBlock from EEPROM\n";
|
||||||
|
@ -48,7 +48,7 @@ void printNumber(long int number) {
|
|||||||
|
|
||||||
// Преобразование числа в строку
|
// Преобразование числа в строку
|
||||||
int_to_str(number, buffer, 10); // 10 — это основание системы счисления (десятичная система)
|
int_to_str(number, buffer, 10); // 10 — это основание системы счисления (десятичная система)
|
||||||
strcat(buffer, "\r\n"); // Добавление новой строки
|
// strcat(buffer, "\r\n"); // Добавление новой строки
|
||||||
|
|
||||||
// Отправка строки через USB CDC
|
// Отправка строки через USB CDC
|
||||||
CDC_Transmit_FS((uint8_t*) buffer, strlen(buffer));
|
CDC_Transmit_FS((uint8_t*) buffer, strlen(buffer));
|
||||||
|
@ -9,31 +9,10 @@
|
|||||||
#include "EEPROM.h"
|
#include "EEPROM.h"
|
||||||
#include "Print.h"
|
#include "Print.h"
|
||||||
|
|
||||||
typedef enum Mode {
|
|
||||||
NoneMode, ShotMode, ProgramMode, MacroMode,
|
|
||||||
} Mode;
|
|
||||||
|
|
||||||
uint8_t isPause = 0;
|
uint8_t isPause = 0;
|
||||||
uint8_t isShooting = 0;
|
uint8_t isShooting = 0;
|
||||||
|
|
||||||
typedef struct CurrentProgram {
|
CurrentInfo currentInfo;
|
||||||
Program program;
|
|
||||||
uint8_t shot_index;
|
|
||||||
} CurrentProgram;
|
|
||||||
|
|
||||||
typedef struct CurrentMacro {
|
|
||||||
Macro macro;
|
|
||||||
uint8_t program_index;
|
|
||||||
} CurrentMacro;
|
|
||||||
|
|
||||||
typedef struct Current {
|
|
||||||
Mode mode;
|
|
||||||
Shot shot;
|
|
||||||
CurrentProgram program;
|
|
||||||
CurrentMacro macro;
|
|
||||||
} Current;
|
|
||||||
|
|
||||||
Current current;
|
|
||||||
|
|
||||||
HardwareInit_t hwSettings = {
|
HardwareInit_t hwSettings = {
|
||||||
/*DelayTimes*/{
|
/*DelayTimes*/{
|
||||||
@ -59,31 +38,31 @@ void doShot(Shot *shot) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void startShooting() {
|
void startShooting() {
|
||||||
switch (current.mode) {
|
switch (currentInfo.mode) {
|
||||||
case ShotMode:
|
case ShotMode:
|
||||||
print("StartShooting\n");
|
print("StartShooting\n");
|
||||||
if (current.shot.isExist) {
|
if (currentInfo.shot.shot.isExist) {
|
||||||
print("Fire!\n");
|
print("Fire!\n");
|
||||||
|
|
||||||
print("isExist ");
|
print("isExist ");
|
||||||
printNumber(current.shot.isExist);
|
printNumber(currentInfo.shot.shot.isExist);
|
||||||
print("countRepeatShot; ");
|
print("countRepeatShot; ");
|
||||||
printNumber(current.shot.countRepeatShot);
|
printNumber(currentInfo.shot.shot.countRepeatShot);
|
||||||
print("speedRollerTop; ");
|
print("speedRollerTop; ");
|
||||||
printNumber(current.shot.speedRollerTop);
|
printNumber(currentInfo.shot.shot.speedRollerTop);
|
||||||
print("speedRollerBottom; ");
|
print("speedRollerBottom; ");
|
||||||
printNumber(current.shot.speedRollerBottom);
|
printNumber(currentInfo.shot.shot.speedRollerBottom);
|
||||||
print("speedScrew; ");
|
print("speedScrew; ");
|
||||||
printNumber(current.shot.speedScrew);
|
printNumber(currentInfo.shot.shot.speedScrew);
|
||||||
print("rotationAxial; ");
|
print("rotationAxial; ");
|
||||||
printNumber(current.shot.rotationAxial);
|
printNumber(currentInfo.shot.shot.rotationAxial);
|
||||||
print("rotationHorizontal; ");
|
print("rotationHorizontal; ");
|
||||||
printNumber(current.shot.rotationHorizontal);
|
printNumber(currentInfo.shot.shot.rotationHorizontal);
|
||||||
print("rotationVertical; ");
|
print("rotationVertical; ");
|
||||||
printNumber(current.shot.rotationVertical);
|
printNumber(currentInfo.shot.shot.rotationVertical);
|
||||||
|
|
||||||
isShooting = 1;
|
currentInfo.state.isShooting = 1;
|
||||||
doShot(¤t.shot);
|
doShot(¤tInfo.shot.shot);
|
||||||
} else {
|
} else {
|
||||||
print("Current Shot is NULL\n");
|
print("Current Shot is NULL\n");
|
||||||
// TODO: sound_ERR(); ledFX_ERR();
|
// TODO: sound_ERR(); ledFX_ERR();
|
||||||
@ -101,8 +80,8 @@ void startShooting() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void stopShooting() {
|
void stopShooting() {
|
||||||
isShooting = 0;
|
currentInfo.state.isPause = 0;
|
||||||
isPause = 0;
|
currentInfo.state.isShooting = 0;
|
||||||
|
|
||||||
setScrewkSpeed(0);
|
setScrewkSpeed(0);
|
||||||
setRollersSpeed(100, 100);
|
setRollersSpeed(100, 100);
|
||||||
@ -117,8 +96,8 @@ uint8_t prepareShot(uint8_t number) {
|
|||||||
Shot shot;
|
Shot shot;
|
||||||
getShot(number, &shot);
|
getShot(number, &shot);
|
||||||
if (shot.isExist) {
|
if (shot.isExist) {
|
||||||
current.mode = ShotMode;
|
currentInfo.mode = ShotMode;
|
||||||
current.shot = shot;
|
currentInfo.shot.shot = shot;
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
// TODO: sound_ERR(); ledFX_ERR();
|
// TODO: sound_ERR(); ledFX_ERR();
|
||||||
|
@ -7,14 +7,25 @@
|
|||||||
|
|
||||||
#include "UART3_CMD_Handler.h"
|
#include "UART3_CMD_Handler.h"
|
||||||
#include "Print.h"
|
#include "Print.h"
|
||||||
|
#include "RobotFunctions.h"
|
||||||
|
#include "EEPROM.h"
|
||||||
|
|
||||||
|
#define HIGHBIT(b) (((b)>>8)&0xff)
|
||||||
|
#define LOWBIT(b) ((b)&0xff)
|
||||||
|
|
||||||
|
|
||||||
|
extern CurrentInfo currentInfo;
|
||||||
|
extern InfoBlock infoBlock;
|
||||||
|
|
||||||
extern void SendResponse(uint8_t command, uint8_t result, uint8_t *data,
|
extern void SendResponse(uint8_t command, uint8_t result, uint8_t *data,
|
||||||
uint8_t data_length);
|
uint8_t data_length);
|
||||||
|
|
||||||
uint8_t checkLen(uint8_t cmd, uint8_t current_length, uint8_t required_length) {
|
uint8_t checkLen(uint8_t cmd, uint8_t current_length, uint8_t required_length) {
|
||||||
if (current_length >= required_length) {
|
if (current_length < required_length) {
|
||||||
print("Invalid length for command");
|
print("Invalid length for command ");
|
||||||
printNumber(cmd);
|
printNumber(cmd);
|
||||||
|
print(" len = ");
|
||||||
|
printNumber(current_length);
|
||||||
print("\n");
|
print("\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -226,7 +237,36 @@ void UART3_ReadStatistics(uint8_t *dataPtr, uint8_t len) {
|
|||||||
if (!checkLen(dataPtr[0], len, MIN_PARAM_LENGTH))
|
if (!checkLen(dataPtr[0], len, MIN_PARAM_LENGTH))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
SendResponse(dataPtr[0], 0, NULL, 0);
|
typedef struct __attribute__((packed)){
|
||||||
|
uint8_t status;
|
||||||
|
uint8_t macro_number;
|
||||||
|
uint8_t program_number;
|
||||||
|
uint8_t shot_number;
|
||||||
|
uint8_t total_macro_done_HIGH;
|
||||||
|
uint8_t total_macro_done_LOW;
|
||||||
|
uint8_t total_program_done_HIGH;
|
||||||
|
uint8_t total_program_done_LOW;
|
||||||
|
uint8_t total_shot_done_HIGH;
|
||||||
|
uint8_t total_shot_done_LOW;
|
||||||
|
} StatusStruct;
|
||||||
|
|
||||||
|
StatusStruct res;
|
||||||
|
|
||||||
|
res.status = currentInfo.state.isShooting && !currentInfo.state.isPause;
|
||||||
|
res.shot_number = currentInfo.shot.index;
|
||||||
|
res.program_number = currentInfo.program.index;
|
||||||
|
res.macro_number = currentInfo.macro.index;
|
||||||
|
|
||||||
|
res.total_shot_done_HIGH = HIGHBIT(infoBlock.statInfo.totalShots);
|
||||||
|
res.total_shot_done_LOW = LOWBIT(infoBlock.statInfo.totalShots);
|
||||||
|
|
||||||
|
res.total_program_done_HIGH = HIGHBIT(infoBlock.statInfo.totalPrograms);
|
||||||
|
res.total_program_done_LOW = LOWBIT(infoBlock.statInfo.totalPrograms);
|
||||||
|
|
||||||
|
res.total_macro_done_HIGH = HIGHBIT(infoBlock.statInfo.totalMacros);
|
||||||
|
res.total_macro_done_LOW = LOWBIT(infoBlock.statInfo.totalMacros);
|
||||||
|
|
||||||
|
SendResponse(dataPtr[0], 0, (uint8_t*)&res, sizeof(res));
|
||||||
}
|
}
|
||||||
|
|
||||||
void UART3_ResetStatistics(uint8_t *dataPtr, uint8_t len) {
|
void UART3_ResetStatistics(uint8_t *dataPtr, uint8_t len) {
|
||||||
|
@ -24,19 +24,6 @@ void UART3_START() {
|
|||||||
HAL_UART_Receive_DMA(&huart3, uart_rx_buffer, UART_BUFFER_SIZE);
|
HAL_UART_Receive_DMA(&huart3, uart_rx_buffer, UART_BUFFER_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
//void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
|
|
||||||
// if (huart->Instance == USART3) {
|
|
||||||
// uartTimer = millis();
|
|
||||||
// packet_index++;
|
|
||||||
//
|
|
||||||
// CDC_Transmit_FS(uart_rx_buffer, UART_BUFFER_SIZE);
|
|
||||||
//
|
|
||||||
// if (packet_index < UART_BUFFER_SIZE) {
|
|
||||||
// HAL_UART_Receive_DMA(&huart3, &uart_rx_buffer[packet_index], 1);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
volatile uint32_t last_rx_time = 0;
|
volatile uint32_t last_rx_time = 0;
|
||||||
volatile uint32_t rx_complete_timeout = 10;
|
volatile uint32_t rx_complete_timeout = 10;
|
||||||
volatile uint32_t old_pos = 0;
|
volatile uint32_t old_pos = 0;
|
||||||
@ -53,16 +40,16 @@ void handle_rx_complete(uint8_t *data, uint16_t len)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Распечатать пришедший буфер с указанием номеров байт
|
// // Распечатать пришедший буфер с указанием номеров байт
|
||||||
char byte_msg[128];
|
// char byte_msg[128];
|
||||||
snprintf(byte_msg, sizeof(byte_msg), "Received buffer (%d bytes):\n", len);
|
// snprintf(byte_msg, sizeof(byte_msg), "Received buffer (%d bytes):\n", len);
|
||||||
CDC_Transmit_FS((uint8_t*) byte_msg, strlen(byte_msg));
|
// CDC_Transmit_FS((uint8_t*) byte_msg, strlen(byte_msg));
|
||||||
|
//
|
||||||
for (uint16_t i = 0; i < len; i++) {
|
// for (uint16_t i = 0; i < len; i++) {
|
||||||
char byte_info[32];
|
// char byte_info[32];
|
||||||
snprintf(byte_info, sizeof(byte_info), "Byte %d: 0x%02X\n", i, data[i]);
|
// snprintf(byte_info, sizeof(byte_info), "Byte %d: 0x%02X\n", i, data[i]);
|
||||||
CDC_Transmit_FS((uint8_t*) byte_info, strlen(byte_info));
|
// CDC_Transmit_FS((uint8_t*) byte_info, strlen(byte_info));
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Проверка правильности пакета (первый байт должен быть 0xF0)
|
// Проверка правильности пакета (первый байт должен быть 0xF0)
|
||||||
if (data[0] != 0xF0) {
|
if (data[0] != 0xF0) {
|
||||||
@ -100,7 +87,7 @@ void handle_rx_complete(uint8_t *data, uint16_t len)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
CDC_Transmit_FS((uint8_t*)"\n\n", 2);
|
// CDC_Transmit_FS((uint8_t*)"\n\n", 2);
|
||||||
|
|
||||||
// Если пакет корректен, передать управление обработчику команд
|
// Если пакет корректен, передать управление обработчику команд
|
||||||
uint8_t *data_ptr = &data[2];
|
uint8_t *data_ptr = &data[2];
|
||||||
@ -144,16 +131,14 @@ void check_uart3_timeout(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void UART3_Handler() {
|
void UART3_Handler() {
|
||||||
// Текущая позиция в буфере (DMA передаёт значение обратного счётчика)
|
|
||||||
uint32_t pos = UART_BUFFER_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart3_rx);
|
uint32_t pos = UART_BUFFER_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart3_rx);
|
||||||
|
|
||||||
if (pos != old_pos)
|
if (pos != old_pos)
|
||||||
{
|
{
|
||||||
// Если есть новые данные, обновляем таймер
|
|
||||||
last_rx_time = millis();
|
last_rx_time = millis();
|
||||||
rx_in_progress = 1;
|
rx_in_progress = 1;
|
||||||
process_uart_data(old_pos, pos); // Обрабатываем новые данные
|
process_uart_data(old_pos, pos);
|
||||||
old_pos = pos; // Обновляем позицию
|
old_pos = pos;
|
||||||
}
|
}
|
||||||
else if (rx_in_progress && (millis() - last_rx_time) > rx_complete_timeout)
|
else if (rx_in_progress && (millis() - last_rx_time) > rx_complete_timeout)
|
||||||
{
|
{
|
||||||
@ -164,34 +149,47 @@ void UART3_Handler() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SendResponse(uint8_t command, uint8_t result, uint8_t *data,
|
void SendResponse(uint8_t command, uint8_t result, uint8_t *data, uint8_t data_length) {
|
||||||
uint8_t data_length) {
|
uint8_t response_buffer[64];
|
||||||
uint8_t response_buffer[64];
|
uint8_t index = 0;
|
||||||
uint8_t index = 0;
|
|
||||||
|
|
||||||
response_buffer[index++] = 0xF0;
|
response_buffer[index++] = 0xF0; // Начальный байт
|
||||||
response_buffer[index++] = data_length + 3;
|
response_buffer[index++] = data_length + 2; // Длина данных + 2 байта (команда, результат)
|
||||||
response_buffer[index++] = command;
|
response_buffer[index++] = command; // Команда
|
||||||
response_buffer[index++] = result;
|
response_buffer[index++] = result; // Результат
|
||||||
|
|
||||||
if (data != NULL && data_length > 0) {
|
if (data != NULL && data_length > 0) {
|
||||||
for (uint8_t i = 0; i < data_length; i++) {
|
for (uint8_t i = 0; i < data_length; i++) {
|
||||||
response_buffer[index++] = data[i];
|
response_buffer[index++] = data[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t checksum = 0;
|
uint16_t checksum = 0;
|
||||||
for (uint8_t i = 0; i < index; i++) {
|
for (uint8_t i = 0; i < index; i++) {
|
||||||
checksum += response_buffer[i];
|
checksum += response_buffer[i];
|
||||||
}
|
}
|
||||||
checksum %= 0xFF;
|
checksum %= 0xFF;
|
||||||
|
|
||||||
response_buffer[index++] = checksum;
|
response_buffer[index++] = checksum;
|
||||||
HAL_UART_Transmit(&huart3, response_buffer, index, HAL_MAX_DELAY);
|
|
||||||
|
// // Печать пакета в CDC с нумерацией байт
|
||||||
|
// char byte_msg[128];
|
||||||
|
// snprintf(byte_msg, sizeof(byte_msg), "Sending response buffer (%d bytes):\n", index);
|
||||||
|
// CDC_Transmit_FS((uint8_t*) byte_msg, strlen(byte_msg));
|
||||||
|
//
|
||||||
|
// for (uint8_t i = 0; i < index; i++) {
|
||||||
|
// char byte_info[32];
|
||||||
|
// snprintf(byte_info, sizeof(byte_info), "Byte %d: 0x%02X\n", i, response_buffer[i]);
|
||||||
|
// CDC_Transmit_FS((uint8_t*) byte_info, strlen(byte_info));
|
||||||
|
// }CDC_Transmit_FS((uint8_t*) "\n\n", 2);
|
||||||
|
|
||||||
|
HAL_UART_Transmit(&huart3, response_buffer, index, HAL_MAX_DELAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void UART3_CMD_Handler(uint8_t *dataPtr, uint8_t len) {
|
void UART3_CMD_Handler(uint8_t *dataPtr, uint8_t len) {
|
||||||
uint8_t command = dataPtr[0];
|
uint8_t command = dataPtr[0];
|
||||||
|
if(command != 0xb5){
|
||||||
|
|
||||||
print("Received command: ");
|
print("Received command: ");
|
||||||
printNumber(command);
|
printNumber(command);
|
||||||
@ -204,6 +202,7 @@ void UART3_CMD_Handler(uint8_t *dataPtr, uint8_t len) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
print("]\n");
|
print("]\n");
|
||||||
|
}
|
||||||
|
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case 10:
|
case 10:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user