regular backup

This commit is contained in:
ggw
2026-05-05 14:09:15 -05:00
parent 42f91eb075
commit 90cd2273f1
16 changed files with 835 additions and 96 deletions
+16 -2
View File
@@ -1,8 +1,9 @@
#pragma once
#include "packet.hpp"
#include "usbd_cdc_if.h"
#include "fatfs.h"
#include "usart.h"
#include "usbd_cdc_if.h"
#include "packet.hpp"
extern uint8_t databank1[1024];
extern uint8_t databank2[1024];
@@ -10,6 +11,8 @@ extern uint16_t pos;
volatile extern bool usb_ready;
volatile extern bool wb1mmc_ready;
volatile extern bool sd_ready;
extern FIL file;
// 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
@@ -29,6 +32,12 @@ void write(packet_type packet) {
HAL_UART_Transmit_DMA(&huart1, databank1, sizeof(databank1));
wb1mmc_ready = false;
}
if (sd_ready) {
UINT bytes_written;
FRESULT res = f_write(&file, databank1, sizeof(databank1), &bytes_written);
if (res != FR_OK || bytes_written != sizeof(databank1)) {}
}
memset(databank2, 0, sizeof(databank2));
pos = sizeof(databank1);
return write(packet);
@@ -47,6 +56,11 @@ void write(packet_type packet) {
HAL_UART_Transmit_DMA(&huart1, databank2, sizeof(databank2));
wb1mmc_ready = false;
}
if (sd_ready) {
UINT bytes_written;
FRESULT res = f_write(&file, databank2, sizeof(databank2), &bytes_written);
if (res != FR_OK || bytes_written != sizeof(databank2)) {}
}
memset(databank1, 0, sizeof(databank1));
pos = 0;
return write(packet);
+1 -1
View File
@@ -1,2 +1,2 @@
void data_description();
void data_description(bool to_usb, bool to_file);
+2
View File
@@ -66,6 +66,8 @@ void SPI1_IRQHandler(void);
void USART1_IRQHandler(void);
void SDMMC1_IRQHandler(void);
void TIM6_DAC_IRQHandler(void);
void DMA2_Channel4_IRQHandler(void);
void DMA2_Channel5_IRQHandler(void);
void USB_IRQHandler(void);
/* USER CODE BEGIN EFP */
+18 -2
View File
@@ -20,13 +20,29 @@ void initialize_ads131(SPI_HandleTypeDef *hspi1) {
HAL_GPIO_WritePin(GPIOA, ADC_CS_Pin, GPIO_PIN_SET);
HAL_Delay(1);
// set OSR
// set OSR, reg 3h
cmd[0] = 0b01100001;
cmd[1] = 0b10000000;
cmd[2] = 0x00;
cmd[3] = 0b00001111;
cmd[4] = 0b00010111; // change last one to 0 to get 488Hz
cmd[4] = 0b00011111;
cmd[5] = 0x00;
HAL_GPIO_WritePin(GPIOA, ADC_CS_Pin, GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(hspi1, (uint8_t*) cmd, (uint8_t*) rx_buff, 18, HAL_MAX_DELAY);
HAL_GPIO_WritePin(GPIOA, ADC_CS_Pin, GPIO_PIN_SET);
HAL_Delay(1);
memset(cmd, 0, 6);
// set gain to 8, reg 4h
cmd[0] = 0b01100010;
cmd[1] = 0b00000000;
cmd[2] = 0x00;
cmd[3] = 0b00000000;
cmd[4] = 0b00000000;
cmd[5] = 0x00;
HAL_GPIO_WritePin(GPIOA, ADC_CS_Pin, GPIO_PIN_RESET);
+31 -19
View File
@@ -1,47 +1,59 @@
#include "datadescriptor.hpp"
#include <cstddef>
#include <cstdio>
#include "fatfs.h"
#include "usbd_cdc_if.h"
#include "packet.hpp"
void data_description() {
extern FIL file;
void data_description(bool to_usb, bool to_file) {
char buff[200];
int cx;
cx = snprintf(buff, sizeof(buff), "%s %d %d %s %d %d %s %d %d %s %d %d\n\r", "packet_rtc", typecode<packet_rtc>(), sizeof(packet_rtc), "uint32_t t", offsetof(packet_rtc, t), sizeof(uint32_t), "RTC_TimeTypeDef sTime", offsetof(packet_rtc, sTime), sizeof(RTC_TimeTypeDef), "RTC_DateTypeDef sDate", offsetof(packet_rtc, sDate), sizeof(RTC_DateTypeDef));
CDC_Transmit_FS((uint8_t*)buff, cx);
HAL_Delay(10);
if (to_file) {unsigned int bw; f_write(&file, buff, cx, &bw);}
if (to_usb) {CDC_Transmit_FS((uint8_t*)buff, cx);}
HAL_Delay(50);
cx = snprintf(buff, sizeof(buff), "%s %d %d %s %d %d %s %d %d\n\r", "packet_vbatt", typecode<packet_vbatt>(), sizeof(packet_vbatt), "uint32_t t", offsetof(packet_vbatt, t), sizeof(uint32_t), "uint16_t vbatt_cnts", offsetof(packet_vbatt, vbatt_cnts), sizeof(uint16_t));
CDC_Transmit_FS((uint8_t*)buff, cx);
HAL_Delay(10);
if (to_file) {unsigned int bw; f_write(&file, buff, cx, &bw);}
if (to_usb) {CDC_Transmit_FS((uint8_t*)buff, cx);}
HAL_Delay(50);
cx = snprintf(buff, sizeof(buff), "%s %d %d %s %d %d %s %d %d\n\r", "packet_imu", typecode<packet_imu>(), sizeof(packet_imu), "uint32_t t", offsetof(packet_imu, t), sizeof(uint32_t), "uint16_t readings_cnts[4]", offsetof(packet_imu, readings_cnts), sizeof(uint16_t));
CDC_Transmit_FS((uint8_t*)buff, cx);
HAL_Delay(10);
if (to_file) {unsigned int bw; f_write(&file, buff, cx, &bw);}
if (to_usb) {CDC_Transmit_FS((uint8_t*)buff, cx);}
HAL_Delay(50);
cx = snprintf(buff, sizeof(buff), "%s %d %d %s %d %d %s %d %d %s %d %d\n\r", "packet_ekg", typecode<packet_ekg>(), sizeof(packet_ekg), "uint32_t t", offsetof(packet_ekg, t), sizeof(uint32_t), "uint8_t index", offsetof(packet_ekg, index), sizeof(uint8_t), "int32_t readings_cnts[50]", offsetof(packet_ekg, readings_cnts), sizeof(int32_t));
CDC_Transmit_FS((uint8_t*)buff, cx);
HAL_Delay(10);
if (to_file) {unsigned int bw; f_write(&file, buff, cx, &bw);}
if (to_usb) {CDC_Transmit_FS((uint8_t*)buff, cx);}
HAL_Delay(50);
cx = snprintf(buff, sizeof(buff), "%s %d %d %s %d %d %s %d %d %s %d %d\n\r", "packet_strain", typecode<packet_strain>(), sizeof(packet_strain), "uint32_t t", offsetof(packet_strain, t), sizeof(uint32_t), "uint8_t index", offsetof(packet_strain, index), sizeof(uint8_t), "int32_t readings_cnts[5]", offsetof(packet_strain, readings_cnts), sizeof(int32_t));
CDC_Transmit_FS((uint8_t*)buff, cx);
HAL_Delay(10);
if (to_file) {unsigned int bw; f_write(&file, buff, cx, &bw);}
if (to_usb) {CDC_Transmit_FS((uint8_t*)buff, cx);}
HAL_Delay(50);
cx = snprintf(buff, sizeof(buff), "%s %d %d %s %d %d %s %d %d %s %d %d\n\r", "packet_outsideT", typecode<packet_outsideT>(), sizeof(packet_outsideT), "uint32_t t", offsetof(packet_outsideT, t), sizeof(uint32_t), "uint8_t index", offsetof(packet_outsideT, index), sizeof(uint8_t), "int32_t readings_cnts[5]", offsetof(packet_outsideT, readings_cnts), sizeof(int32_t));
CDC_Transmit_FS((uint8_t*)buff, cx);
HAL_Delay(10);
if (to_file) {unsigned int bw; f_write(&file, buff, cx, &bw);}
if (to_usb) {CDC_Transmit_FS((uint8_t*)buff, cx);}
HAL_Delay(50);
cx = snprintf(buff, sizeof(buff), "%s %d %d %s %d %d %s %d %d %s %d %d\n\r", "packet_insideT", typecode<packet_insideT>(), sizeof(packet_insideT), "uint32_t t", offsetof(packet_insideT, t), sizeof(uint32_t), "uint8_t index", offsetof(packet_insideT, index), sizeof(uint8_t), "int32_t readings_cnts[5]", offsetof(packet_insideT, readings_cnts), sizeof(int32_t));
CDC_Transmit_FS((uint8_t*)buff, cx);
HAL_Delay(10);
if (to_file) {unsigned int bw; f_write(&file, buff, cx, &bw);}
if (to_usb) {CDC_Transmit_FS((uint8_t*)buff, cx);}
HAL_Delay(50);
cx = snprintf(buff, sizeof(buff), "%s %d %d %s %d %d %s %d %d\n\r", "packet_button", typecode<packet_button>(), sizeof(packet_button), "uint32_t t", offsetof(packet_button, t), sizeof(uint32_t), "uint8_t button_vec", offsetof(packet_button, button_vec), sizeof(uint8_t));
CDC_Transmit_FS((uint8_t*)buff, cx);
HAL_Delay(10);
if (to_file) {unsigned int bw; f_write(&file, buff, cx, &bw);}
if (to_usb) {CDC_Transmit_FS((uint8_t*)buff, cx);}
HAL_Delay(50);
cx = snprintf(buff, sizeof(buff), "%s %d %d %s %d %d %s %d %d %s %d %d %s %d %d\n\r", "packet_spo2", typecode<packet_spo2>(), sizeof(packet_spo2), "uint32_t t", offsetof(packet_spo2, t), sizeof(uint32_t), "uint32_t green_cnts[25]", offsetof(packet_spo2, green_cnts), sizeof(uint32_t), "uint32_t red_cnts[25]", offsetof(packet_spo2, red_cnts), sizeof(uint32_t), "uint32_t ir_cnts[25]", offsetof(packet_spo2, ir_cnts), sizeof(uint32_t));
CDC_Transmit_FS((uint8_t*)buff, cx);
HAL_Delay(10);
if (to_file) {unsigned int bw; f_write(&file, buff, cx, &bw);}
if (to_usb) {CDC_Transmit_FS((uint8_t*)buff, cx);}
HAL_Delay(50);
}
+7
View File
@@ -41,6 +41,7 @@ void MX_DMA_Init(void)
/* DMA controller clock enable */
__HAL_RCC_DMA1_CLK_ENABLE();
__HAL_RCC_DMA2_CLK_ENABLE();
/* DMA interrupt init */
/* DMA1_Channel4_IRQn interrupt configuration */
@@ -49,6 +50,12 @@ void MX_DMA_Init(void)
/* DMA1_Channel5_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn);
/* DMA2_Channel4_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA2_Channel4_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA2_Channel4_IRQn);
/* DMA2_Channel5_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA2_Channel5_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA2_Channel5_IRQn);
}
+73 -39
View File
@@ -82,6 +82,11 @@ volatile uint16_t tim6_reloads = 0;
volatile bool usb_ready = false;
volatile bool wb1mmc_ready = false;
volatile bool print_desc = false;
volatile bool sd_ready = false;
FATFS fs;
FIL file;
FRESULT res;
uint8_t uart_rx_data[1];
@@ -101,10 +106,18 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
}
}
volatile bool to_turn_on_sd = false;
volatile bool to_turn_off_sd = false;
void USB_CDC_RxHandler(uint8_t* Buf, uint32_t Len) {
if (memcmp(Buf, "R", Len) == 0) {
usb_ready = true;
}
if (memcmp(Buf, "S", Len) == 0) {
to_turn_off_sd = true;
}
if (memcmp(Buf, "T", Len) == 0) {
to_turn_on_sd = true;
}
if (memcmp(Buf, "?", Len) == 0) {
print_desc = true;
}
@@ -112,13 +125,62 @@ void USB_CDC_RxHandler(uint8_t* Buf, uint32_t Len) {
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
if (uart_rx_data[0] == 'R') {
HAL_GPIO_TogglePin(GPIOB, LED1_Pin | LED2_Pin | LED3_Pin);
//HAL_GPIO_TogglePin(GPIOB, LED1_Pin | LED2_Pin | LED3_Pin);
wb1mmc_ready = true;
}
HAL_UART_Receive_DMA(&huart1, uart_rx_data, sizeof(uart_rx_data));
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {}
void turn_on_sd(void) {
to_turn_on_sd = false;
if (BSP_SD_IsDetected() != SD_PRESENT) {
CDC_Transmit_FS((uint8_t*)"SD NP\n\r", 7);
return;
}
if (sd_ready) {
CDC_Transmit_FS((uint8_t*)"SD RD\n\r", 7);
return;
}
if ((BSP_SD_IsDetected() == SD_PRESENT) && (!sd_ready)) {
CDC_Transmit_FS((uint8_t*)"HERE1\n\r", 7);
res = f_mount(&fs, "", 1);
if (res != FR_OK) {
CDC_Transmit_FS((uint8_t*)"SD NOK1\n\r", 9);
return;
}
CDC_Transmit_FS((uint8_t*)"HERE2\n\r", 7);
alignas(32) char buffer1[32];
alignas(32) char buffer2[32];
int cx = snprintf(buffer1, 32, "%06lu.log", TIM6->CNT);
cx = snprintf(buffer2, 32, "%s\n\r", buffer1);
CDC_Transmit_FS((uint8_t*)buffer2, cx);
res = f_open(&file, buffer1, FA_WRITE | FA_CREATE_ALWAYS);
if (res != FR_OK) {
CDC_Transmit_FS((uint8_t*)"SD NOK2\n\r", 9);
return;
}
CDC_Transmit_FS((uint8_t*)"SUCCESS\n\r", 9);
unsigned int bw;
res = f_write(&file, "HERE\n\r", 6, &bw);
cx = snprintf(buffer2, 32, "%d %d\n\r", res, bw);
CDC_Transmit_FS((uint8_t*)buffer2, cx);
//data_description(false, true);
sd_ready = true;
}
}
void turn_off_sd(void) {
to_turn_off_sd = false;
if (sd_ready) {
f_sync(&file);
f_close(&file);
res = f_mount(NULL, "", 1);
CDC_Transmit_FS((uint8_t*)"CLS\n\r", 7);
sd_ready = false;
}
}
/* USER CODE END 0 */
/**
@@ -174,50 +236,22 @@ int main(void)
// For some reason initializing the ads131 first breaks the lsm6dsv gyro readings
initialize_lsm6dsv(&hspi1);
initialize_ads131(&hspi1);
HAL_Delay(3000);
while (1)
{
if (to_turn_on_sd) {
turn_on_sd();
}
if (to_turn_off_sd) {
turn_off_sd();
}
continue;
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
// FATFS fs;
// FIL file;
// FRESULT res;
// bool fs_on = false;
// if (BSP_SD_IsDetected() == SD_PRESENT) {
// CDC_Transmit_FS((uint8_t*) "Detected\n\r", 10);
// res = f_mount(&fs, "", 1);
// if (res != FR_OK) {
// char buffer[32];
// int cx;
// cx = snprintf(buffer, 32, "Err: %d\n\r", res);
// CDC_Transmit_FS((uint8_t*)buffer, cx);
// } else if (res == FR_OK) {
// CDC_Transmit_FS((uint8_t*) "Ok\n\r", 8);
// bool fs_on = true;
// // HAL_Delay(10);
// // DIR dir;
// // FILINFO fno;
// // res = f_opendir(&dir, "");
// // if (res != FR_OK) {
// // } else {
// // res = f_readdir(&dir, &fno);
// // if (res != FR_OK || fno.name[0] == 0)
// // }
// // f_open(&file, "")
// f_mount(NULL, "", 0);
// }
// } else {
// CDC_Transmit_FS((uint8_t*) "Not Detected\n\r", 14);
// HAL_Delay(10);
// }
if (print_desc) {
data_description();
data_description(true, false);
print_desc = false;
}
+41
View File
@@ -25,6 +25,8 @@
/* USER CODE END 0 */
SD_HandleTypeDef hsd1;
DMA_HandleTypeDef hdma_sdmmc1_rx;
DMA_HandleTypeDef hdma_sdmmc1_tx;
/* SDMMC1 init function */
@@ -88,6 +90,41 @@ void HAL_SD_MspInit(SD_HandleTypeDef* sdHandle)
GPIO_InitStruct.Alternate = GPIO_AF12_SDMMC1;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/* SDMMC1 DMA Init */
/* SDMMC1_RX Init */
hdma_sdmmc1_rx.Instance = DMA2_Channel4;
hdma_sdmmc1_rx.Init.Request = DMA_REQUEST_7;
hdma_sdmmc1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_sdmmc1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_sdmmc1_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_sdmmc1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma_sdmmc1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma_sdmmc1_rx.Init.Mode = DMA_NORMAL;
hdma_sdmmc1_rx.Init.Priority = DMA_PRIORITY_LOW;
if (HAL_DMA_Init(&hdma_sdmmc1_rx) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(sdHandle,hdmarx,hdma_sdmmc1_rx);
/* SDMMC1_TX Init */
hdma_sdmmc1_tx.Instance = DMA2_Channel5;
hdma_sdmmc1_tx.Init.Request = DMA_REQUEST_7;
hdma_sdmmc1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_sdmmc1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_sdmmc1_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_sdmmc1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma_sdmmc1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma_sdmmc1_tx.Init.Mode = DMA_NORMAL;
hdma_sdmmc1_tx.Init.Priority = DMA_PRIORITY_LOW;
if (HAL_DMA_Init(&hdma_sdmmc1_tx) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(sdHandle,hdmatx,hdma_sdmmc1_tx);
/* SDMMC1 interrupt Init */
HAL_NVIC_SetPriority(SDMMC1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(SDMMC1_IRQn);
@@ -121,6 +158,10 @@ void HAL_SD_MspDeInit(SD_HandleTypeDef* sdHandle)
HAL_GPIO_DeInit(GPIOD, GPIO_PIN_2);
/* SDMMC1 DMA DeInit */
HAL_DMA_DeInit(sdHandle->hdmarx);
HAL_DMA_DeInit(sdHandle->hdmatx);
/* SDMMC1 interrupt Deinit */
HAL_NVIC_DisableIRQ(SDMMC1_IRQn);
/* USER CODE BEGIN SDMMC1_MspDeInit 1 */
+30
View File
@@ -58,6 +58,8 @@
extern PCD_HandleTypeDef hpcd_USB_FS;
extern ADC_HandleTypeDef hadc1;
extern I2C_HandleTypeDef hi2c1;
extern DMA_HandleTypeDef hdma_sdmmc1_rx;
extern DMA_HandleTypeDef hdma_sdmmc1_tx;
extern SD_HandleTypeDef hsd1;
extern SPI_HandleTypeDef hspi1;
extern TIM_HandleTypeDef htim6;
@@ -361,6 +363,34 @@ void TIM6_DAC_IRQHandler(void)
/* USER CODE END TIM6_DAC_IRQn 1 */
}
/**
* @brief This function handles DMA2 channel4 global interrupt.
*/
void DMA2_Channel4_IRQHandler(void)
{
/* USER CODE BEGIN DMA2_Channel4_IRQn 0 */
/* USER CODE END DMA2_Channel4_IRQn 0 */
HAL_DMA_IRQHandler(&hdma_sdmmc1_rx);
/* USER CODE BEGIN DMA2_Channel4_IRQn 1 */
/* USER CODE END DMA2_Channel4_IRQn 1 */
}
/**
* @brief This function handles DMA2 channel5 global interrupt.
*/
void DMA2_Channel5_IRQHandler(void)
{
/* USER CODE BEGIN DMA2_Channel5_IRQn 0 */
/* USER CODE END DMA2_Channel5_IRQn 0 */
HAL_DMA_IRQHandler(&hdma_sdmmc1_tx);
/* USER CODE BEGIN DMA2_Channel5_IRQn 1 */
/* USER CODE END DMA2_Channel5_IRQn 1 */
}
/**
* @brief This function handles USB event interrupt through EXTI line 17.
*/