working on l452

This commit is contained in:
ggw
2026-04-28 17:40:30 -05:00
parent d853c57c16
commit 4e4ea8f15e
13 changed files with 266 additions and 4949 deletions
+8 -10
View File
@@ -1,4 +1,5 @@
#include "ads131.hpp"
#include "tim.h"
#include "gpio.h"
#include <cstring>
#include "packet.hpp"
@@ -51,7 +52,7 @@ struct packet_insideT p_insideT;
extern volatile uint16_t tim6_reloads;
void read_ads131(SPI_HandleTypeDef *hspi1) {
if (HAL_GPIO_ReadPin(GPIOA, ADC_DRDY_Pin) != GPIO_PIN_RESET) {
return;
}
@@ -66,11 +67,11 @@ void read_ads131(SPI_HandleTypeDef *hspi1) {
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);
// WARNING AVERAGING LATER PROBABLY WON'T WORK - 2s COMPLEMENT!!!
//--------------
if (p_ekg.index == 0) {
p_ekg.t.tim6_reloads = tim6_reloads;
p_ekg.t.tim6_value = TIM6->CNT;
p_ekg.t = ((uint32_t)tim6_reloads) * (htim6.Init.Period + 1) + TIM6->CNT;
}
p_ekg.readings_cnts[p_ekg.index] = (((uint32_t)rx_buff[3]<<16) +
((uint32_t)rx_buff[4]<<8) +
@@ -83,8 +84,7 @@ void read_ads131(SPI_HandleTypeDef *hspi1) {
}
//-------------
if (p_strain.index == 0) {
p_strain.t.tim6_reloads = tim6_reloads;
p_strain.t.tim6_value = TIM6->CNT;
p_strain.t = ((uint32_t)tim6_reloads) * (htim6.Init.Period + 1) + TIM6->CNT;
}
p_strain.readings_cnts[p_strain.index / 10] += (((uint32_t)rx_buff[6]<<16) +
((uint32_t)rx_buff[7]<<8) +
@@ -99,8 +99,7 @@ void read_ads131(SPI_HandleTypeDef *hspi1) {
}
//-------------
if (p_outsideT.index == 0) {
p_outsideT.t.tim6_reloads = tim6_reloads;
p_outsideT.t.tim6_value = TIM6->CNT;
p_outsideT.t = ((uint32_t)tim6_reloads) * (htim6.Init.Period + 1) + TIM6->CNT;
}
p_outsideT.readings_cnts[p_outsideT.index / 10] += (((uint32_t)rx_buff[9]<<16) +
((uint32_t)rx_buff[10]<<8) +
@@ -115,8 +114,7 @@ void read_ads131(SPI_HandleTypeDef *hspi1) {
}
//-------------
if (p_insideT.index == 0) {
p_insideT.t.tim6_reloads = tim6_reloads;
p_insideT.t.tim6_value = TIM6->CNT;
p_insideT.t = ((uint32_t)tim6_reloads) * (htim6.Init.Period + 1) + TIM6->CNT;
}
p_insideT.readings_cnts[p_insideT.index / 10] += (((uint32_t)rx_buff[12]<<16) +
((uint32_t)rx_buff[13]<<8) +
@@ -0,0 +1,47 @@
#include "datadescriptor.hpp"
#include <cstddef>
#include <cstdio>
#include "usbd_cdc_if.h"
#include "packet.hpp"
void data_description() {
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);
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);
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);
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), "uint32_t readings_cnts[50]", offsetof(packet_ekg, readings_cnts), sizeof(uint32_t));
CDC_Transmit_FS((uint8_t*)buff, cx);
HAL_Delay(10);
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), "uint32_t readings_cnts[5]", offsetof(packet_strain, readings_cnts), sizeof(uint32_t));
CDC_Transmit_FS((uint8_t*)buff, cx);
HAL_Delay(10);
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), "uint32_t readings_cnts[5]", offsetof(packet_outsideT, readings_cnts), sizeof(uint32_t));
CDC_Transmit_FS((uint8_t*)buff, cx);
HAL_Delay(10);
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), "uint32_t readings_cnts[5]", offsetof(packet_insideT, readings_cnts), sizeof(uint32_t));
CDC_Transmit_FS((uint8_t*)buff, cx);
HAL_Delay(10);
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);
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);
}
+3 -3
View File
@@ -1,6 +1,7 @@
#include "lsm6dsv.hpp"
#include "packet.hpp"
#include "gpio.h"
#include "tim.h"
#include "buff.hpp"
extern volatile uint16_t tim6_reloads;
@@ -65,7 +66,7 @@ void initialize_lsm6dsv(SPI_HandleTypeDef *hspi1) {
struct packet_imu p_imu;
void read_lsm6dsv(SPI_HandleTypeDef *hspi1) {
if (HAL_GPIO_ReadPin(GPIOC, IMU_DRDY_Pin) != GPIO_PIN_RESET) {
return;
}
@@ -93,8 +94,7 @@ void read_lsm6dsv(SPI_HandleTypeDef *hspi1) {
HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_SET);
}
p_imu.t.tim6_reloads = tim6_reloads;
p_imu.t.tim6_value = TIM6->CNT;
p_imu.t = ((uint32_t)tim6_reloads) * (htim6.Init.Period + 1) + TIM6->CNT;
p_imu.readings_cnts[0] = rx_buff[1];
p_imu.readings_cnts[1] = ((uint16_t)rx_buff[2] << 8) + rx_buff[3];
p_imu.readings_cnts[2] = ((uint16_t)rx_buff[4] << 8) + rx_buff[5];
+17 -67
View File
@@ -38,8 +38,7 @@
#include "buff.hpp"
#include "ads131.hpp"
#include "lsm6dsv.hpp"
#include "refl.hpp"
#include <tuple>
#include "datadescriptor.hpp"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@@ -80,6 +79,7 @@ volatile uint16_t adc_value = 0;
volatile uint8_t adc_ready = 0;
volatile uint16_t tim6_reloads = 0;
volatile bool usb_ready = false;
volatile bool print_desc = false;
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) {
if (hadc->Instance == ADC1) {
@@ -100,6 +100,9 @@ void USB_CDC_RxHandler(uint8_t* Buf, uint32_t Len) {
if (memcmp(Buf, "R", Len) == 0) {
usb_ready = true;
}
if (memcmp(Buf, "?", Len) == 0) {
print_desc = true;
}
//if (Len == 1) {
// return;
//}
@@ -108,56 +111,6 @@ void USB_CDC_RxHandler(uint8_t* Buf, uint32_t Len) {
//int i = snprintf(buffer, sizeof(buffer), "Length: %d\n\r", Len);
//
}
REFL_TYPE(packet_imu)
REFL_END
// #include <cstdint>
// template<std::intmax_t N>
// class to_string_t {
// constexpr static auto buflen() noexcept {
// unsigned int len = N > 0 ? 1 : 2;
// for (auto n = N; n; len++, n /= 10);
// return len;
// }
// char buf[buflen()] = {};
// public:
// constexpr to_string_t() noexcept {
// auto ptr = buf + buflen();
// *--ptr = '\0';
// if (N != 0) {
// for (auto n = N; n; n /= 10)
// *--ptr = "0123456789"[(N < 0 ? -1 : 1) * (n % 10)];
// if (N < 0)
// *--ptr = '-';
// } else {
// buf[0] = '0';
// }
// }
// constexpr operator const char *() const { return buf; }
// };
// template<std::intmax_t N>
// constexpr to_string_t<N> to_string;
// // shamelessly stolen from https://stackoverflow.com/questions/6713420/c-convert-integer-to-string-at-compile-time
// auto c = to_string<555>;
//string_writer(c);
packet_imu p;
//auto name = refl::reflect(p).name
// for member in x, we wnat the offset_of
int x = offsetof(packet_imu, readings_cnts);
//const uint8_t[100] str;
//consteval snprintf(str, "%s", 100)
/* USER CODE END 0 */
/**
@@ -252,15 +205,15 @@ int main(void)
// CDC_Transmit_FS((uint8_t*) "Not Detected\n\r", 14);
// HAL_Delay(10);
// }
//CDC_Transmit_FS((uint8_t*) name, sizeof(name));
HAL_Delay(1000);
continue;
if (print_desc) {
data_description();
print_desc = false;
}
if (to_recordTime) {
struct timer_time t;
t.tim6_reloads = tim6_reloads;
t.tim6_value = TIM6->CNT;
struct packet_rtc p_rtc;
p_rtc.t = t;
p_rtc.t = ((uint32_t)tim6_reloads) * (htim6.Init.Period + 1) + TIM6->CNT;
HAL_RTC_GetTime(&hrtc, &p_rtc.sTime, RTC_FORMAT_BCD);
HAL_RTC_GetDate(&hrtc, &p_rtc.sDate, RTC_FORMAT_BCD);
write(p_rtc);
@@ -275,11 +228,8 @@ int main(void)
if (adc_ready) {
adc_ready = 0;
struct timer_time t;
t.tim6_reloads = tim6_reloads;
t.tim6_value = TIM6->CNT;
struct packet_vbatt p_vbatt;
p_vbatt.t = t;
p_vbatt.t = ((uint32_t)tim6_reloads) * (htim6.Init.Period + 1) + TIM6->CNT;
p_vbatt.vbatt_cnts = HAL_ADC_GetValue(&hadc1);
write(p_vbatt);
}
@@ -305,10 +255,10 @@ int main(void)
// }
// }
//HAL_GPIO_WritePin(GPIOB, LED1_Pin|LED2_Pin|LED3_Pin, GPIO_PIN_SET);
//HAL_Delay(100);
//HAL_GPIO_WritePin(GPIOB, LED1_Pin|LED2_Pin|LED3_Pin, GPIO_PIN_RESET);
//HAL_Delay(100);
// HAL_GPIO_WritePin(GPIOB, LED1_Pin|LED2_Pin|LED3_Pin, GPIO_PIN_SET);
// HAL_Delay(100);
// HAL_GPIO_WritePin(GPIOB, LED1_Pin|LED2_Pin|LED3_Pin, GPIO_PIN_RESET);
// HAL_Delay(100);
}
/* USER CODE END 3 */
}
+2 -2
View File
@@ -40,8 +40,8 @@ void MX_SPI1_Init(void)
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT; // low + 2 edge for adc
hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH; // high
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;