Files
Gimbal/modules/master_machine/master_machine.c
T
SkyLiCoder 88702e2832 fix: move DMA buffers from DTCM to RAM_D1, improve bsp_usart error recovery
- Add .dma_buf section in RAM_D1 for DMA-accessible USART instance pool.
  DTCM (0x20000000) is not reachable by DMA on STM32H7, causing bus errors
  (TE) when DMA writes to malloc'd buffers placed in DTCM by the XG linker
  script.
- Replace malloc with static pool (usart_pool[]) placed in .dma_buf section.
- In RxEventCallback, set recv_buff_size from Size so module callbacks know
  how many bytes were actually received.
- Use USART_RXBUFF_LIMIT (256) as the DMA transfer size instead of per-module
  recv_buff_size. The actual buffer is always 256 bytes.
- Remove recv_buff_size from USART_Init_Config_s; it is now set at runtime.
- Flush RDR, clear error flags, and clear stale DMAR in error callback to
  prevent residual conditions from re-triggering errors on DMA restart.
- Switch Vision module from UART10 to UART1.
2026-06-02 16:03:21 +08:00

94 lines
2.9 KiB
C

#include "master_machine.h"
VisionData_t vision_data;
static USARTInstance *cv_usart_instance;
//static DaemonInstance *vision_daemon_instance;
static uint8_t calc_checksum(uint8_t *buf, uint8_t len)
{
uint8_t sum = 0;
for (uint8_t i = 0; i < len - 1; i++)
sum += buf[i];
return sum;
}
static void DecodeVision()
{
//DaemonReload(vision_daemon_instance);
static uint32_t counter;
float dt = DWT_GetDeltaT(&counter);
vision_data.frequency = 1.0f / dt;
uint8_t *buf = cv_usart_instance->recv_buff;
uint16_t len = cv_usart_instance->recv_buff_size;
uint8_t sof = buf[0];
if (sof == VISION_SOF_AUTO && len == VISION_AUTO_LEN)
{
if (calc_checksum(buf, VISION_AUTO_LEN) != buf[VISION_AUTO_LEN - 1])
return;
memcpy(&vision_data.error_x, buf + 1, 4);
memcpy(&vision_data.error_y, buf + 5, 4);
vision_data.auto_updated = 1;
}
else if (sof == VISION_SOF_MANUAL && len == VISION_MANUAL_LEN)
{
if (calc_checksum(buf, VISION_MANUAL_LEN) != buf[VISION_MANUAL_LEN - 1])
return;
vision_data.zero_force = buf[1];
memcpy(&vision_data.yaw_ref, buf + 2, 4);
memcpy(&vision_data.pitch_ref, buf + 6, 4);
vision_data.manual_updated = 1;
}
else if (sof == VISION_SOF_DEBUG && len == VISION_DEBUG_LEN)
{
if (calc_checksum(buf, VISION_DEBUG_LEN) != buf[VISION_DEBUG_LEN - 1])
return;
uint8_t *p = buf + 1;
memcpy(&vision_data.vision_yaw_Kp, p, 4); p += 4;
memcpy(&vision_data.vision_yaw_Ki, p, 4); p += 4;
memcpy(&vision_data.vision_yaw_Kd, p, 4); p += 4;
memcpy(&vision_data.vision_pitch_Kp, p, 4); p += 4;
memcpy(&vision_data.vision_pitch_Ki, p, 4); p += 4;
memcpy(&vision_data.vision_pitch_Kd, p, 4); p += 4;
memcpy(&vision_data.motor_yaw_Kp, p, 4); p += 4;
memcpy(&vision_data.motor_yaw_Ki, p, 4); p += 4;
memcpy(&vision_data.motor_yaw_Kd, p, 4); p += 4;
memcpy(&vision_data.motor_pitch_Kp, p, 4); p += 4;
memcpy(&vision_data.motor_pitch_Ki, p, 4); p += 4;
memcpy(&vision_data.motor_pitch_Kd, p, 4);
vision_data.debug_updated = 1;
}
else if (sof == VISION_SOF_CMD && len == VISION_CMD_LEN)
{
if (calc_checksum(buf, VISION_CMD_LEN) != buf[VISION_CMD_LEN - 1])
return;
vision_data.cmd = buf[1];
vision_data.cmd_pending = 1;
}
}
static void VisionOfflineCallback(void *id)
{
(void)id;
USARTServiceInit(cv_usart_instance);
}
void Vision_Uart_Init(UART_HandleTypeDef *_handle)
{
USART_Init_Config_s conf = {
.module_callback = DecodeVision,
.usart_handle = _handle,
};
cv_usart_instance = USARTRegister(&conf);
Daemon_Init_Config_s daemon_conf = {
.callback = NULL,
.owner_id = NULL,
.reload_count = 10,
};
//vision_daemon_instance = DaemonRegister(&daemon_conf);
}