variable size data sending, spi mode change fix, wb1mmc write start

This commit is contained in:
ggw
2026-05-08 15:21:26 -05:00
parent 02517fe9ae
commit 41ec35233d
14 changed files with 446 additions and 325 deletions
+84 -51
View File
@@ -3,66 +3,99 @@
#include "fatfs.h"
#include "usart.h"
#include "usbd_cdc_if.h"
#include "tim.h"
#include "packet.hpp"
extern uint8_t databank1[1024];
extern uint8_t databank2[1024];
extern uint16_t pos;
void sd_send(uint8_t data[], uint32_t len);
void usb_send(uint8_t data[], uint32_t len);
void uart_send(uint8_t data[], uint32_t len);
volatile extern bool usb_ready;
volatile extern bool wb1mmc_ready;
volatile extern bool sd_ready;
extern FIL file;
template<int N, void (*send_fn)(uint8_t data[], uint32_t len)>
struct dbl_buff {
uint32_t last_write;
uint32_t pos;
alignas(4) uint8_t send_buffer1[N + 4];
alignas(4) uint8_t send_buffer2[N + 4];
uint8_t* const databank1;
uint8_t* const databank2;
dbl_buff() : databank1(send_buffer1 + 4), databank2(send_buffer2 + 4) {};
// Every time we get a reading add to the active databank if it fits,
// if not we switch banks and write out the now unactive databank
template<typename packet_type> void write(const packet_type& packet) {
if (pos + sizeof(packet_type) + 1 < N) {
databank1[pos] = typecode<packet_type>();
memcpy(databank1 + pos + 1, &packet, sizeof(packet_type));
pos += sizeof(packet_type) + 1;
return;
} else if (pos < N) {
send();
return this->write(packet);
} else if (pos + sizeof(packet_type) + 1 < 2 * N) {
databank2[pos - N] = typecode<packet_type>();
memcpy(databank2 + pos + 1 - N, &packet, sizeof(packet_type));
pos += sizeof(packet_type) + 1;
return;
} else {
send();
return this->write(packet);
}
};
// Every time we get a reading add to the active databank if it fits,
// if not we switch banks and write out the now unactive databank
void send() {
if (pos < N) {
((uint32_t*)send_buffer1)[0] = pos;
send_fn(send_buffer1, pos + 4);
pos = N;
} else {
((uint32_t*)send_buffer2)[0] = pos - N;
send_fn(send_buffer2, pos - N + 4);
pos = 0;
}
last_write = total_tim6();
}
};
extern struct dbl_buff<1024, usb_send> usb_buff;
extern struct dbl_buff<1024, uart_send> ble_buff;
extern struct dbl_buff<1024, sd_send> sd_buff;
volatile extern bool send_imu_usb;
volatile extern bool send_imu_ble;
volatile extern bool send_adc_usb;
volatile extern bool send_adc_ble;
volatile extern bool send_ppg_usb;
volatile extern bool send_ppg_ble;
// Log all packets to SD card, only log big packets to
// other things if required
template<typename packet_type>
void write(packet_type packet) {
if (pos + sizeof(packet) + 2 < sizeof(databank1)) {
*(uint16_t*)(&databank1[pos]) = typecode<packet_type>();
memcpy(databank1 + pos + 2, &packet, sizeof(packet));
pos += sizeof(packet) + 2;
return;
} else if (pos < sizeof(databank1)) {
if (usb_ready) {
CDC_Transmit_FS(databank1, sizeof(databank1));
usb_ready = false;
sd_buff.write(packet);
if (typecode<packet_type>() == typecode<packet_imu>()) {
if (send_imu_usb) {
usb_buff.write(packet);
}
if (wb1mmc_ready) {
HAL_UART_Transmit_DMA(&huart1, databank1, sizeof(databank1));
wb1mmc_ready = false;
if (send_imu_ble) {
ble_buff.write(packet);
}
if (sd_ready) {
UINT bytes_written;
FRESULT res = f_write(&file, databank1, sizeof(databank1), &bytes_written);
if (res != FR_OK || bytes_written != sizeof(databank1)) {}
} else if (typecode<packet_type>() == typecode<packet_adc>()) {
if (send_adc_usb) {
usb_buff.write(packet);
}
memset(databank2, 0, sizeof(databank2));
pos = sizeof(databank1);
return write(packet);
} else if (pos + sizeof(packet) + 2 <
sizeof(databank1) + sizeof(databank2)) {
*(uint16_t*)(&databank2[pos - sizeof(databank1)]) = typecode<packet_type>();
memcpy(databank2 + pos + 2 - sizeof(databank1), &packet, sizeof(packet));
pos += sizeof(packet) + 2;
return;
} else {
if (usb_ready) {
CDC_Transmit_FS(databank2, sizeof(databank2));
usb_ready = false;
if (send_adc_ble) {
ble_buff.write(packet);
}
if (wb1mmc_ready) {
HAL_UART_Transmit_DMA(&huart1, databank2, sizeof(databank2));
wb1mmc_ready = false;
} else if (typecode<packet_type>() == typecode<packet_spo2>()) {
if (send_ppg_usb) {
usb_buff.write(packet);
}
if (sd_ready) {
UINT bytes_written;
FRESULT res = f_write(&file, databank2, sizeof(databank2), &bytes_written);
if (res != FR_OK || bytes_written != sizeof(databank2)) {}
if (send_ppg_ble) {
ble_buff.write(packet);
}
memset(databank1, 0, sizeof(databank1));
pos = 0;
return write(packet);
}
};
} else { // Send other types unconditionally
usb_buff.write(packet);
ble_buff.write(packet);
}
}
+4 -1
View File
@@ -37,7 +37,10 @@ extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim6;
/* USER CODE BEGIN Private defines */
extern volatile uint16_t tim6_reloads;
__inline__ uint32_t total_tim6() {
return ((uint32_t)tim6_reloads) * (htim6.Init.Period + 1) + TIM6->CNT;
}
/* USER CODE END Private defines */
void MX_TIM2_Init(void);