diff --git a/code/l452_code/Core/Inc/main.h b/code/l452_code/Core/Inc/main.h index b13c295..3920b07 100644 --- a/code/l452_code/Core/Inc/main.h +++ b/code/l452_code/Core/Inc/main.h @@ -59,7 +59,6 @@ void Error_Handler(void); /* Private defines -----------------------------------------------------------*/ #define IMU_DRDY_Pin GPIO_PIN_3 #define IMU_DRDY_GPIO_Port GPIOC -#define IMU_DRDY_EXTI_IRQn EXTI3_IRQn #define CLKIN_ADC_Pin GPIO_PIN_0 #define CLKIN_ADC_GPIO_Port GPIOA #define ADC_CS_Pin GPIO_PIN_3 @@ -84,7 +83,6 @@ void Error_Handler(void); #define LED3_GPIO_Port GPIOB #define PPG_DRDY_Pin GPIO_PIN_7 #define PPG_DRDY_GPIO_Port GPIOC -#define PPG_DRDY_EXTI_IRQn EXTI9_5_IRQn #define SD_DETECT_Pin GPIO_PIN_15 #define SD_DETECT_GPIO_Port GPIOA diff --git a/code/l452_code/Core/Inc/max30101.hpp b/code/l452_code/Core/Inc/max30101.hpp new file mode 100644 index 0000000..1566a57 --- /dev/null +++ b/code/l452_code/Core/Inc/max30101.hpp @@ -0,0 +1,5 @@ +#include "i2c.h" + +void initialize_max30101(I2C_HandleTypeDef *hi2c1); + +void read_max30101(I2C_HandleTypeDef *hi2c1); diff --git a/code/l452_code/Core/Inc/packet.hpp b/code/l452_code/Core/Inc/packet.hpp index d96acd6..b4f3a5f 100644 --- a/code/l452_code/Core/Inc/packet.hpp +++ b/code/l452_code/Core/Inc/packet.hpp @@ -25,43 +25,21 @@ struct packet_vbatt { struct packet_imu { uint32_t t; - uint16_t readings_cnts[4]; + uint8_t data[141]; }; -struct packet_ekg { +struct packet_adc { uint32_t t; uint8_t index; - int32_t readings_cnts[50]; -}; - -struct packet_strain { - uint32_t t; - uint8_t index; - int32_t readings_cnts[5]; -}; - -struct packet_outsideT { - uint32_t t; - uint8_t index; - int32_t readings_cnts[5]; -}; - -struct packet_insideT { - uint32_t t; - uint8_t index; - int32_t readings_cnts[5]; -}; - -struct packet_button { - uint32_t t; - uint8_t button_vec; + int32_t ekg_readings_cnts[50]; + int32_t str_readings_cnts[5]; + int32_t oT_readings_cnts[5]; + int32_t iT_readings_cnts[5]; }; struct packet_spo2 { uint32_t t; - uint32_t green_cnts[25]; - uint32_t red_cnts[25]; - uint32_t ir_cnts[25]; + uint8_t bytes[180]; }; struct packet_msg { @@ -85,41 +63,21 @@ __inline__ uint8_t typecode() { } template<> -__inline__ uint8_t typecode() { +__inline__ uint8_t typecode() { return 3; } template<> -__inline__ uint8_t typecode() { +__inline__ uint8_t typecode() { return 4; } template<> -__inline__ uint8_t typecode() { +__inline__ uint8_t typecode() { return 5; } template<> -__inline__ uint8_t typecode() { +__inline__ uint8_t typecode() { return 6; } - -template<> -__inline__ uint8_t typecode() { - return 7; -} - -template<> -__inline__ uint8_t typecode() { - return 8; -} - -template<> -__inline__ uint8_t typecode() { - return 9; -} - -template<> -__inline__ uint8_t typecode() { - return 10; -} diff --git a/code/l452_code/Core/Inc/stm32l4xx_it.h b/code/l452_code/Core/Inc/stm32l4xx_it.h index 9a73687..1286e5d 100644 --- a/code/l452_code/Core/Inc/stm32l4xx_it.h +++ b/code/l452_code/Core/Inc/stm32l4xx_it.h @@ -56,9 +56,10 @@ void DebugMon_Handler(void); void PendSV_Handler(void); void SysTick_Handler(void); void EXTI0_IRQHandler(void); -void EXTI3_IRQHandler(void); void DMA1_Channel4_IRQHandler(void); void DMA1_Channel5_IRQHandler(void); +void DMA1_Channel6_IRQHandler(void); +void DMA1_Channel7_IRQHandler(void); void ADC1_IRQHandler(void); void EXTI9_5_IRQHandler(void); void I2C1_EV_IRQHandler(void); diff --git a/code/l452_code/Core/Src/ads131.cpp b/code/l452_code/Core/Src/ads131.cpp index 2b8481c..38cf449 100644 --- a/code/l452_code/Core/Src/ads131.cpp +++ b/code/l452_code/Core/Src/ads131.cpp @@ -18,7 +18,7 @@ void initialize_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); - HAL_Delay(1); + HAL_Delay(2); // set OSR, reg 3h cmd[0] = 0b01100001; @@ -32,11 +32,11 @@ void initialize_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); - HAL_Delay(1); + HAL_Delay(2); memset(cmd, 0, 6); - // set gain to 8, reg 4h + // set gain to 1, reg 4h cmd[0] = 0b01100010; cmd[1] = 0b00000000; cmd[2] = 0x00; @@ -48,7 +48,7 @@ void initialize_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); - HAL_Delay(1); + HAL_Delay(2); memset(cmd, 0, 6); @@ -60,10 +60,7 @@ void initialize_ads131(SPI_HandleTypeDef *hspi1) { } } -struct packet_ekg p_ekg; -struct packet_strain p_strain; -struct packet_outsideT p_outsideT; -struct packet_insideT p_insideT; +struct packet_adc p_adc; extern volatile uint16_t tim6_reloads; @@ -83,58 +80,23 @@ 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 = ((uint32_t)tim6_reloads) * (htim6.Init.Period + 1) + TIM6->CNT; + + if (p_adc.index == 0) { + p_adc.t = ((uint32_t)tim6_reloads) * (htim6.Init.Period + 1) + TIM6->CNT; } - p_ekg.readings_cnts[p_ekg.index] = to_signed_24(&rx_buff[3]); - - if (p_ekg.index == 49) { - write(p_ekg); - p_ekg.index = 0; + p_adc.ekg_readings_cnts[p_adc.index] = to_signed_24(&rx_buff[3]); + p_adc.str_readings_cnts[p_adc.index / 10] += to_signed_24(&rx_buff[6]); + p_adc.oT_readings_cnts[p_adc.index / 10] += to_signed_24(&rx_buff[9]); + p_adc.iT_readings_cnts[p_adc.index / 10] += to_signed_24(&rx_buff[12]); + + if (p_adc.index == 49) { + write(p_adc); + p_adc.index = 0; + memset(p_adc.str_readings_cnts, 0, sizeof(p_adc.str_readings_cnts)); + memset(p_adc.oT_readings_cnts, 0, sizeof(p_adc.oT_readings_cnts)); + memset(p_adc.iT_readings_cnts, 0, sizeof(p_adc.iT_readings_cnts)); } else { - p_ekg.index++; + p_adc.index++; } - //------------- - if (p_strain.index == 0) { - p_strain.t = ((uint32_t)tim6_reloads) * (htim6.Init.Period + 1) + TIM6->CNT; - } - p_strain.readings_cnts[p_strain.index / 10] += to_signed_24(&rx_buff[6]); - - if (p_strain.index == 49) { - write(p_strain); - p_strain.index = 0; - memset(p_strain.readings_cnts, 0, sizeof(p_strain.readings_cnts)); - } else { - p_strain.index++; - } - //------------- - if (p_outsideT.index == 0) { - p_outsideT.t = ((uint32_t)tim6_reloads) * (htim6.Init.Period + 1) + TIM6->CNT; - } - p_outsideT.readings_cnts[p_outsideT.index / 10] += to_signed_24(&rx_buff[9]); - - if (p_outsideT.index == 49) { - write(p_outsideT); - p_outsideT.index = 0; - memset(p_outsideT.readings_cnts, 0, sizeof(p_outsideT.readings_cnts)); - } else { - p_outsideT.index++; - } - //------------- - if (p_insideT.index == 0) { - p_insideT.t = ((uint32_t)tim6_reloads) * (htim6.Init.Period + 1) + TIM6->CNT; - } - p_insideT.readings_cnts[p_insideT.index / 10] += to_signed_24(&rx_buff[12]); - - if (p_insideT.index == 49) { - write(p_insideT); - p_insideT.index = 0; - memset(p_insideT.readings_cnts, 0, sizeof(p_insideT.readings_cnts)); - } else { - p_insideT.index++; - } - //------------- } diff --git a/code/l452_code/Core/Src/datadescriptor.cpp b/code/l452_code/Core/Src/datadescriptor.cpp index 4ba1c6f..9125a82 100644 --- a/code/l452_code/Core/Src/datadescriptor.cpp +++ b/code/l452_code/Core/Src/datadescriptor.cpp @@ -21,37 +21,17 @@ void data_description(bool to_usb, bool to_file) { 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(), 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)); + cx = snprintf(buff, sizeof(buff), "%s %d %d %s %d %d %s %d %d\n\r", "packet_imu", typecode(), sizeof(packet_imu), "uint32_t t", offsetof(packet_imu, t), sizeof(uint32_t), "uint8_t data[141]", offsetof(packet_imu, data), sizeof(uint8_t)); 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(), 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)); + cx = snprintf(buff, sizeof(buff), "%s %d %d %s %d %d %s %d %d %s %d %d %s %d %d %s %d %d %s %d %d\n\r", "packet_adc", typecode(), sizeof(packet_adc), "uint32_t t", offsetof(packet_adc, t), sizeof(uint32_t), "uint8_t index", offsetof(packet_adc, index), sizeof(uint8_t), "int32_t ekg_readings_cnts[50]", offsetof(packet_adc, ekg_readings_cnts), sizeof(int32_t), "int32_t str_readings_cnts[5]", offsetof(packet_adc, str_readings_cnts), sizeof(int32_t), "int32_t oT_readings_cnts[5]", offsetof(packet_adc, oT_readings_cnts), sizeof(int32_t), "int32_t iT_readings_cnts[5]", offsetof(packet_adc, iT_readings_cnts), sizeof(int32_t)); 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(), 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)); - 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(), 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)); - 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(), 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)); - 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(), 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)); - 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(), 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)); + cx = snprintf(buff, sizeof(buff), "%s %d %d %s %d %d %s %d %d\n\r", "packet_spo2", typecode(), sizeof(packet_spo2), "uint32_t t", offsetof(packet_spo2, t), sizeof(uint32_t), "uint8_t bytes[180]", offsetof(packet_spo2, bytes), sizeof(uint8_t)); 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); diff --git a/code/l452_code/Core/Src/dma.c b/code/l452_code/Core/Src/dma.c index 869b1f6..bb0cf44 100644 --- a/code/l452_code/Core/Src/dma.c +++ b/code/l452_code/Core/Src/dma.c @@ -50,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); + /* DMA1_Channel6_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(DMA1_Channel6_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(DMA1_Channel6_IRQn); + /* DMA1_Channel7_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(DMA1_Channel7_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(DMA1_Channel7_IRQn); /* DMA2_Channel4_IRQn interrupt configuration */ HAL_NVIC_SetPriority(DMA2_Channel4_IRQn, 0, 0); HAL_NVIC_EnableIRQ(DMA2_Channel4_IRQn); diff --git a/code/l452_code/Core/Src/gpio.c b/code/l452_code/Core/Src/gpio.c index 35f956e..26089fd 100644 --- a/code/l452_code/Core/Src/gpio.c +++ b/code/l452_code/Core/Src/gpio.c @@ -59,9 +59,9 @@ void MX_GPIO_Init(void) /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOB, LED1_Pin|LED2_Pin|LED3_Pin, GPIO_PIN_SET); - /*Configure GPIO pins : IMU_DRDY_Pin PPG_DRDY_Pin */ - GPIO_InitStruct.Pin = IMU_DRDY_Pin|PPG_DRDY_Pin; - GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; + /*Configure GPIO pins : IMU_DRDY_Pin BUTTON2_Pin */ + GPIO_InitStruct.Pin = IMU_DRDY_Pin|BUTTON2_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); @@ -85,12 +85,6 @@ void MX_GPIO_Init(void) GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(IMU_CS_GPIO_Port, &GPIO_InitStruct); - /*Configure GPIO pin : BUTTON2_Pin */ - GPIO_InitStruct.Pin = BUTTON2_Pin; - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; - GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(BUTTON2_GPIO_Port, &GPIO_InitStruct); - /*Configure GPIO pin : BUTTON1_Pin */ GPIO_InitStruct.Pin = BUTTON1_Pin; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; @@ -104,6 +98,12 @@ void MX_GPIO_Init(void) GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + /*Configure GPIO pin : PPG_DRDY_Pin */ + GPIO_InitStruct.Pin = PPG_DRDY_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_PULLUP; + HAL_GPIO_Init(PPG_DRDY_GPIO_Port, &GPIO_InitStruct); + /*Configure GPIO pin : SD_DETECT_Pin */ GPIO_InitStruct.Pin = SD_DETECT_Pin; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; @@ -114,9 +114,6 @@ void MX_GPIO_Init(void) HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0); HAL_NVIC_EnableIRQ(EXTI0_IRQn); - HAL_NVIC_SetPriority(EXTI3_IRQn, 0, 0); - HAL_NVIC_EnableIRQ(EXTI3_IRQn); - HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 0); HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); diff --git a/code/l452_code/Core/Src/i2c.c b/code/l452_code/Core/Src/i2c.c index 44629db..bfbed9e 100644 --- a/code/l452_code/Core/Src/i2c.c +++ b/code/l452_code/Core/Src/i2c.c @@ -25,6 +25,8 @@ /* USER CODE END 0 */ I2C_HandleTypeDef hi2c1; +DMA_HandleTypeDef hdma_i2c1_rx; +DMA_HandleTypeDef hdma_i2c1_tx; /* I2C1 init function */ void MX_I2C1_Init(void) @@ -38,7 +40,7 @@ void MX_I2C1_Init(void) /* USER CODE END I2C1_Init 1 */ hi2c1.Instance = I2C1; - hi2c1.Init.Timing = 0x10D19CE4; + hi2c1.Init.Timing = 0x00F12981; hi2c1.Init.OwnAddress1 = 0; hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; @@ -105,6 +107,41 @@ void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle) /* I2C1 clock enable */ __HAL_RCC_I2C1_CLK_ENABLE(); + /* I2C1 DMA Init */ + /* I2C1_RX Init */ + hdma_i2c1_rx.Instance = DMA1_Channel7; + hdma_i2c1_rx.Init.Request = DMA_REQUEST_3; + hdma_i2c1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; + hdma_i2c1_rx.Init.PeriphInc = DMA_PINC_DISABLE; + hdma_i2c1_rx.Init.MemInc = DMA_MINC_ENABLE; + hdma_i2c1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; + hdma_i2c1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; + hdma_i2c1_rx.Init.Mode = DMA_NORMAL; + hdma_i2c1_rx.Init.Priority = DMA_PRIORITY_LOW; + if (HAL_DMA_Init(&hdma_i2c1_rx) != HAL_OK) + { + Error_Handler(); + } + + __HAL_LINKDMA(i2cHandle,hdmarx,hdma_i2c1_rx); + + /* I2C1_TX Init */ + hdma_i2c1_tx.Instance = DMA1_Channel6; + hdma_i2c1_tx.Init.Request = DMA_REQUEST_3; + hdma_i2c1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; + hdma_i2c1_tx.Init.PeriphInc = DMA_PINC_DISABLE; + hdma_i2c1_tx.Init.MemInc = DMA_MINC_ENABLE; + hdma_i2c1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; + hdma_i2c1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; + hdma_i2c1_tx.Init.Mode = DMA_NORMAL; + hdma_i2c1_tx.Init.Priority = DMA_PRIORITY_LOW; + if (HAL_DMA_Init(&hdma_i2c1_tx) != HAL_OK) + { + Error_Handler(); + } + + __HAL_LINKDMA(i2cHandle,hdmatx,hdma_i2c1_tx); + /* I2C1 interrupt Init */ HAL_NVIC_SetPriority(I2C1_EV_IRQn, 0, 0); HAL_NVIC_EnableIRQ(I2C1_EV_IRQn); @@ -133,6 +170,10 @@ void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle) HAL_GPIO_DeInit(GPIOA, GPIO_PIN_10); + /* I2C1 DMA DeInit */ + HAL_DMA_DeInit(i2cHandle->hdmarx); + HAL_DMA_DeInit(i2cHandle->hdmatx); + /* I2C1 interrupt Deinit */ HAL_NVIC_DisableIRQ(I2C1_EV_IRQn); /* USER CODE BEGIN I2C1_MspDeInit 1 */ diff --git a/code/l452_code/Core/Src/lsm6dsv.cpp b/code/l452_code/Core/Src/lsm6dsv.cpp index 649920c..ddefd6a 100644 --- a/code/l452_code/Core/Src/lsm6dsv.cpp +++ b/code/l452_code/Core/Src/lsm6dsv.cpp @@ -15,98 +15,90 @@ void initialize_lsm6dsv(SPI_HandleTypeDef *hspi1) { hspi1->Init.CLKPolarity = SPI_POLARITY_HIGH; HAL_SPI_Init(hspi1); - // Reset - tx_buff[0] = 0x01; + tx_buff[0] = 0x01; // Reset tx_buff[1] = 0x04; HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_RESET); HAL_SPI_TransmitReceive(hspi1, tx_buff, rx_buff, 2, HAL_MAX_DELAY); HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_SET); - HAL_Delay(1); + HAL_Delay(30); - tx_buff[0] = 0x09; // Set 7.5Hz data rate for fifo - tx_buff[1] = 0b00100010; + tx_buff[0] = 0x03; // active high pushpull interrupt + tx_buff[1] = 0x01; // I2C and I3C disabled HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_RESET); HAL_SPI_TransmitReceive(hspi1, tx_buff, rx_buff, 2, HAL_MAX_DELAY); HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_SET); - - tx_buff[0] = 0x0A; // Set continuous mode for fifo - tx_buff[1] = 0b00000110; + HAL_Delay(30); + + tx_buff[0] = 0x09; // Set 60Hz data rate for fifo + tx_buff[1] = 0b01010101; HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_RESET); HAL_SPI_TransmitReceive(hspi1, tx_buff, rx_buff, 2, HAL_MAX_DELAY); HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_SET); + HAL_Delay(30); tx_buff[0] = 0x0D; // Set drdy based on fifo tx_buff[1] = 0b00111000; HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_RESET); HAL_SPI_TransmitReceive(hspi1, tx_buff, rx_buff, 2, HAL_MAX_DELAY); HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_SET); + HAL_Delay(30); - tx_buff[0] = 0x10; // Set 7.5Hz accelerometer high performance - tx_buff[1] = 0b00000010; + tx_buff[0] = 0x10; // Set 60Hz accelerometer high performance + tx_buff[1] = 0b00000101; HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_RESET); HAL_SPI_TransmitReceive(hspi1, tx_buff, rx_buff, 2, HAL_MAX_DELAY); HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_SET); + HAL_Delay(30); - tx_buff[0] = 0x11; // Set 7.5Hz gyro high performance - tx_buff[1] = 0b00000010; + tx_buff[0] = 0x11; // Set 60Hz gyro high performance + tx_buff[1] = 0b00000101; HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_RESET); HAL_SPI_TransmitReceive(hspi1, tx_buff, rx_buff, 2, HAL_MAX_DELAY); HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_SET); + HAL_Delay(30); // todo: get watermark working tx_buff[0] = 0x07; - tx_buff[1] = 0x07; + tx_buff[1] = 21; // 21 samples HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_RESET); HAL_SPI_TransmitReceive(hspi1, tx_buff, rx_buff, 2, HAL_MAX_DELAY); HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_SET); + HAL_Delay(30); + + tx_buff[0] = 0x0A; // Set bypass mode for fifo + tx_buff[1] = 0b00000000; // to clear fifo + HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_RESET); + HAL_SPI_TransmitReceive(hspi1, tx_buff, rx_buff, 2, HAL_MAX_DELAY); + HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_SET); + HAL_Delay(30); + + tx_buff[0] = 0x0A; // Set continuous mode for fifo + tx_buff[1] = 0b00000110; + HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_RESET); + HAL_SPI_TransmitReceive(hspi1, tx_buff, rx_buff, 2, HAL_MAX_DELAY); + HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_SET); + HAL_Delay(30); } struct packet_imu p_imu; void read_lsm6dsv(SPI_HandleTypeDef *hspi1) { - if (HAL_GPIO_ReadPin(GPIOC, IMU_DRDY_Pin) != GPIO_PIN_RESET) { + if (HAL_GPIO_ReadPin(GPIOC, IMU_DRDY_Pin) == GPIO_PIN_RESET) { return; } - //CDC_Transmit_FS((uint8_t*) "Detected\n\r", 10); - - uint8_t tx_buff[8] = {0}; - uint8_t rx_buff[8] = {0}; - HAL_SPI_DeInit(hspi1); hspi1->Init.CLKPolarity = SPI_POLARITY_HIGH; HAL_SPI_Init(hspi1); - - tx_buff[0] = 0b10000000 + 0x1B; - tx_buff[1] = 0; - HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_RESET); - HAL_SPI_TransmitReceive(hspi1, tx_buff, rx_buff, 3, HAL_MAX_DELAY); - HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_SET); - // todo: read in batches - if (rx_buff[1] != 0) { - tx_buff[0] = 0b10000000 + 0x78; // FIFO addr - tx_buff[1] = 0; - HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_RESET); - HAL_SPI_TransmitReceive(hspi1, tx_buff, rx_buff, 8, HAL_MAX_DELAY); - HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_SET); + uint8_t tx_buff[141] = {0b10000000 + 0x78}; // FIFO addr - // cx = snprintf(buffer, 64, "%02x %02x %02x %02x %02x %02x %02x %02x\n\r", - // rx_buff[0], - // rx_buff[1], - // rx_buff[2], - // rx_buff[3], - // rx_buff[4], - // rx_buff[5], - // rx_buff[6], - // rx_buff[7]); - // CDC_Transmit_FS((uint8_t*)buffer, cx); - 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]; - p_imu.readings_cnts[3] = ((uint16_t)rx_buff[6] << 8) + rx_buff[7]; - write(p_imu); - } + // Watermark set for 21 samples, read 20 + tx_buff[0] = 0b10000000 + 0x78; // FIFO addr + p_imu.t = ((uint32_t)tim6_reloads) * (htim6.Init.Period + 1) + TIM6->CNT; + HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_RESET); + HAL_SPI_TransmitReceive(hspi1, tx_buff, p_imu.data, 141, HAL_MAX_DELAY); + HAL_GPIO_WritePin(GPIOC, IMU_CS_Pin, GPIO_PIN_SET); + write(p_imu); } diff --git a/code/l452_code/Core/Src/main.cpp b/code/l452_code/Core/Src/main.cpp index fc7f3f4..884ccc0 100644 --- a/code/l452_code/Core/Src/main.cpp +++ b/code/l452_code/Core/Src/main.cpp @@ -39,6 +39,7 @@ #include "buff.hpp" #include "ads131.hpp" #include "lsm6dsv.hpp" +#include "max30101.hpp" #include "datadescriptor.hpp" /* USER CODE END Includes */ @@ -146,10 +147,7 @@ void turn_on_sd(void) { return; } alignas(32) char buffer1[32]; - alignas(32) char buffer2[32]; int cx = snprintf(buffer1, 32, "%08lu.log", ((uint32_t)tim6_reloads) * (htim6.Init.Period + 1) + TIM6->CNT % 99999999); - 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) { res = f_mount(NULL, "", 1); @@ -167,7 +165,6 @@ void turn_off_sd(void) { f_sync(&file); f_close(&file); res = f_mount(NULL, "", 1); - CDC_Transmit_FS((uint8_t*)"CLS\n\r", 7); sd_ready = false; } } @@ -217,7 +214,7 @@ int main(void) MX_USB_DEVICE_Init(); MX_FATFS_Init(); /* USER CODE BEGIN 2 */ - + /* USER CODE END 2 */ /* Infinite loop */ @@ -226,20 +223,24 @@ int main(void) //HAL_UART_Receive_DMA(&huart1, uart_rx_data, sizeof(uart_rx_data)); // For some reason initializing the ads131 first breaks the lsm6dsv gyro readings initialize_lsm6dsv(&hspi1); - initialize_ads131(&hspi1); + //initialize_ads131(&hspi1); + //initialize_max30101(&hi2c1); - uint8_t buf[512]; - while (1) { - if (to_turn_on_sd) { - turn_on_sd(); - } - - if (to_turn_off_sd) { - turn_off_sd(); - } + read_lsm6dsv(&hspi1); + //read_max30101(&hi2c1); + //read_ads131(&hspi1); + HAL_Delay(1); continue; + // 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 */ @@ -273,7 +274,6 @@ int main(void) read_ads131(&hspi1); read_lsm6dsv(&hspi1); - // 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); diff --git a/code/l452_code/Core/Src/max30101.cpp b/code/l452_code/Core/Src/max30101.cpp new file mode 100644 index 0000000..71cf4e8 --- /dev/null +++ b/code/l452_code/Core/Src/max30101.cpp @@ -0,0 +1,75 @@ +#include "max30101.hpp" + +#include + +#include "gpio.h" +#include "tim.h" + +#include "packet.hpp" +#include "buff.hpp" + +extern uint16_t tim6_reloads; +struct packet_spo2 p_spo2; +uint8_t data[4]; + +bool followup = false; +bool print_ready = false; +void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c1) { + if (not followup) { + print_ready = true; + // Clear interrupt + HAL_I2C_Mem_Read_DMA(hi2c1, 0xAE, 0x00, 1, data, 1); + followup = true; + } else { + followup = false; + } +} + +void initialize_max30101(I2C_HandleTypeDef *hi2c1) { + data[0] = 0b01000111; // Reset + HAL_I2C_Mem_Write(hi2c1, 0xAE, 0x09, 1, data, 1, HAL_MAX_DELAY); + data[0] = 0b00000111; // Turn on all LEDs + HAL_I2C_Mem_Write(hi2c1, 0xAE, 0x09, 1, data, 1, HAL_MAX_DELAY); + data[0] = 0b00000011; //0.4mS pulse width + HAL_I2C_Mem_Write(hi2c1, 0xAE, 0x0A, 1, data, 1, HAL_MAX_DELAY); + + memset(data, 0x0F, 4); // Set power + HAL_I2C_Mem_Write(hi2c1, 0xAE, 0x0C, 1, data, 4, HAL_MAX_DELAY); + + data[0] = 0b00010010; // Time slots: RED, IR + HAL_I2C_Mem_Write(hi2c1, 0xAE, 0x11, 1, data, 1, HAL_MAX_DELAY); + data[0] = 0b00000011; // Time slots ctd: None, Green + HAL_I2C_Mem_Write(hi2c1, 0xAE, 0x12, 1, data, 1, HAL_MAX_DELAY); + + // No averaging, FIFO rollover, FIFO almost full @ 29 samples + data[0] = 0b00010011; + HAL_I2C_Mem_Write(hi2c1, 0xAE, 0x08, 1, data, 1, HAL_MAX_DELAY); + + // Interrupt only on FIFO almost full + data[0] = 0b10000000; + HAL_I2C_Mem_Write(hi2c1, 0xAE, 0x02, 1, data, 1, HAL_MAX_DELAY); + + // clear write and read ptrs + data[0] = 0; + HAL_I2C_Mem_Write(hi2c1, 0xAE, 0x04, 1, data, 1, HAL_MAX_DELAY); + HAL_I2C_Mem_Write(hi2c1, 0xAE, 0x05, 1, data, 1, HAL_MAX_DELAY); + HAL_I2C_Mem_Write(hi2c1, 0xAE, 0x06, 1, data, 1, HAL_MAX_DELAY); + + // read once to start the thing + HAL_I2C_Mem_Read(hi2c1, 0xAE, 0x07, 1, p_spo2.bytes, 180, HAL_MAX_DELAY); +} + +void read_max30101(I2C_HandleTypeDef *hi2c1) { + if (print_ready) { + p_spo2.t = ((uint32_t)tim6_reloads) * (htim6.Init.Period + 1) + TIM6->CNT; + write(p_spo2); + print_ready = false; + } + if (HAL_GPIO_ReadPin(GPIOC, PPG_DRDY_Pin) != GPIO_PIN_RESET) { + return; + } + if (HAL_I2C_GetState(hi2c1) == HAL_I2C_STATE_READY) { + HAL_I2C_Mem_Read_DMA(hi2c1, 0xAE, 0x07, 1, p_spo2.bytes, 180); + print_ready = false; + } +} diff --git a/code/l452_code/Core/Src/spi.c b/code/l452_code/Core/Src/spi.c index b14d342..fb65223 100644 --- a/code/l452_code/Core/Src/spi.c +++ b/code/l452_code/Core/Src/spi.c @@ -44,7 +44,7 @@ void MX_SPI1_Init(void) hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; hspi1.Init.CLKPhase = SPI_PHASE_2EDGE; hspi1.Init.NSS = SPI_NSS_SOFT; - hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128; + hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64; hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; hspi1.Init.TIMode = SPI_TIMODE_DISABLE; hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; diff --git a/code/l452_code/Core/Src/stm32l4xx_it.c b/code/l452_code/Core/Src/stm32l4xx_it.c index 2c78a54..4bf280f 100644 --- a/code/l452_code/Core/Src/stm32l4xx_it.c +++ b/code/l452_code/Core/Src/stm32l4xx_it.c @@ -57,6 +57,8 @@ /* External variables --------------------------------------------------------*/ extern PCD_HandleTypeDef hpcd_USB_FS; extern ADC_HandleTypeDef hadc1; +extern DMA_HandleTypeDef hdma_i2c1_rx; +extern DMA_HandleTypeDef hdma_i2c1_tx; extern I2C_HandleTypeDef hi2c1; extern DMA_HandleTypeDef hdma_sdmmc1; extern SD_HandleTypeDef hsd1; @@ -221,20 +223,6 @@ void EXTI0_IRQHandler(void) /* USER CODE END EXTI0_IRQn 1 */ } -/** - * @brief This function handles EXTI line3 interrupt. - */ -void EXTI3_IRQHandler(void) -{ - /* USER CODE BEGIN EXTI3_IRQn 0 */ - - /* USER CODE END EXTI3_IRQn 0 */ - HAL_GPIO_EXTI_IRQHandler(IMU_DRDY_Pin); - /* USER CODE BEGIN EXTI3_IRQn 1 */ - - /* USER CODE END EXTI3_IRQn 1 */ -} - /** * @brief This function handles DMA1 channel4 global interrupt. */ @@ -263,6 +251,34 @@ void DMA1_Channel5_IRQHandler(void) /* USER CODE END DMA1_Channel5_IRQn 1 */ } +/** + * @brief This function handles DMA1 channel6 global interrupt. + */ +void DMA1_Channel6_IRQHandler(void) +{ + /* USER CODE BEGIN DMA1_Channel6_IRQn 0 */ + + /* USER CODE END DMA1_Channel6_IRQn 0 */ + HAL_DMA_IRQHandler(&hdma_i2c1_tx); + /* USER CODE BEGIN DMA1_Channel6_IRQn 1 */ + + /* USER CODE END DMA1_Channel6_IRQn 1 */ +} + +/** + * @brief This function handles DMA1 channel7 global interrupt. + */ +void DMA1_Channel7_IRQHandler(void) +{ + /* USER CODE BEGIN DMA1_Channel7_IRQn 0 */ + + /* USER CODE END DMA1_Channel7_IRQn 0 */ + HAL_DMA_IRQHandler(&hdma_i2c1_rx); + /* USER CODE BEGIN DMA1_Channel7_IRQn 1 */ + + /* USER CODE END DMA1_Channel7_IRQn 1 */ +} + /** * @brief This function handles ADC1 global interrupt. */ @@ -286,7 +302,6 @@ void EXTI9_5_IRQHandler(void) /* USER CODE END EXTI9_5_IRQn 0 */ HAL_GPIO_EXTI_IRQHandler(ADC_DRDY_Pin); - HAL_GPIO_EXTI_IRQHandler(PPG_DRDY_Pin); /* USER CODE BEGIN EXTI9_5_IRQn 1 */ /* USER CODE END EXTI9_5_IRQn 1 */ diff --git a/code/l452_code/Makefile b/code/l452_code/Makefile index 2eccd16..06ae779 100644 --- a/code/l452_code/Makefile +++ b/code/l452_code/Makefile @@ -1,5 +1,5 @@ ########################################################################################################################## -# File automatically-generated by tool: [projectgenerator] version: [4.9.0-B19] date: [Tue May 05 18:08:47 CDT 2026] +# File automatically-generated by tool: [projectgenerator] version: [4.9.0-B19] date: [Thu May 07 13:29:52 CDT 2026] ########################################################################################################################## # ------------------------------------------------ diff --git a/code/l452_code/imu_grapher.py b/code/l452_code/imu_grapher.py new file mode 100644 index 0000000..6810ae1 --- /dev/null +++ b/code/l452_code/imu_grapher.py @@ -0,0 +1,44 @@ +import serial +import matplotlib.pyplot as plt +import numpy as np + +ser = serial.Serial(port='/dev/ttyACM1', timeout = 1) +ser.flush() +ser.reset_input_buffer() + +gyros = [] +accs = [] + +fig, axs = plt.subplots(2) +i = 0 + +while(1): + x = [int(e, 16) for e in ser.readline()[1:-1].split(b" ")] + imu_reading_type = x[0] >> 3 + imu_reading_tag_cnt = (x[0] >> 1) & 3 + #print(imu_reading_type, imu_reading_tag_cnt) + reading_xyz = [int.from_bytes(x[1:3], byteorder = 'little', signed = True), + int.from_bytes(x[3:5], byteorder = 'little', signed = True), + int.from_bytes(x[5:7], byteorder = 'little', signed = True)] + i += 1 + if imu_reading_type == 1: + gyros.append([250 * e / (1 << 16) for e in reading_xyz]) + elif imu_reading_type == 2: + accs.append([4 * e / (1<<16) for e in reading_xyz]) + + if len(gyros) > 50 and (i % 10) == 0: + gyros = gyros[-100:] + accs = accs[-100:] + a = np.array(accs) + g = np.array(gyros) + a -= np.mean(a, axis = 0).reshape(1, 3) + g -= np.mean(g, axis = 0).reshape(1, 3) + axs[0].cla() + axs[0].plot(g[:,0]) + axs[0].plot(g[:,1]) + axs[0].plot(g[:,2]) + axs[1].cla() + axs[1].plot(a[:,0]) + axs[1].plot(a[:,1]) + axs[1].plot(a[:,2]) + plt.pause(0.0001) diff --git a/code/l452_code/l452_code.ioc b/code/l452_code/l452_code.ioc index be7d9d7..2588b9d 100644 --- a/code/l452_code/l452_code.ioc +++ b/code/l452_code/l452_code.ioc @@ -10,10 +10,30 @@ ADC1.master=1 CAD.formats= CAD.pinconfig= CAD.provider= +Dma.I2C1_RX.3.Direction=DMA_PERIPH_TO_MEMORY +Dma.I2C1_RX.3.Instance=DMA1_Channel7 +Dma.I2C1_RX.3.MemDataAlignment=DMA_MDATAALIGN_BYTE +Dma.I2C1_RX.3.MemInc=DMA_MINC_ENABLE +Dma.I2C1_RX.3.Mode=DMA_NORMAL +Dma.I2C1_RX.3.PeriphDataAlignment=DMA_PDATAALIGN_BYTE +Dma.I2C1_RX.3.PeriphInc=DMA_PINC_DISABLE +Dma.I2C1_RX.3.Priority=DMA_PRIORITY_LOW +Dma.I2C1_RX.3.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority +Dma.I2C1_TX.4.Direction=DMA_MEMORY_TO_PERIPH +Dma.I2C1_TX.4.Instance=DMA1_Channel6 +Dma.I2C1_TX.4.MemDataAlignment=DMA_MDATAALIGN_BYTE +Dma.I2C1_TX.4.MemInc=DMA_MINC_ENABLE +Dma.I2C1_TX.4.Mode=DMA_NORMAL +Dma.I2C1_TX.4.PeriphDataAlignment=DMA_PDATAALIGN_BYTE +Dma.I2C1_TX.4.PeriphInc=DMA_PINC_DISABLE +Dma.I2C1_TX.4.Priority=DMA_PRIORITY_LOW +Dma.I2C1_TX.4.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority Dma.Request0=USART1_RX Dma.Request1=USART1_TX Dma.Request2=SDMMC1 -Dma.RequestsNb=3 +Dma.Request3=I2C1_RX +Dma.Request4=I2C1_TX +Dma.RequestsNb=5 Dma.SDMMC1.2.Direction=DMA_PERIPH_TO_MEMORY Dma.SDMMC1.2.Instance=DMA2_Channel4 Dma.SDMMC1.2.MemDataAlignment=DMA_MDATAALIGN_WORD @@ -59,8 +79,9 @@ FATFS0.BSP.semaphore= FATFS0.BSP.solution=PA15 (JTDI) File.Version=6 GPIO.groupedBy=Group By Peripherals -I2C1.IPParameters=Timing -I2C1.Timing=0x10D19CE4 +I2C1.I2C_Speed_Mode=I2C_Fast +I2C1.IPParameters=Timing,I2C_Speed_Mode +I2C1.Timing=0x00F12981 KeepUserPlacement=false Mcu.CPN=STM32L452RET3 Mcu.Family=STM32L4 @@ -129,10 +150,11 @@ NVIC.ADC1_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false NVIC.DMA1_Channel4_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true NVIC.DMA1_Channel5_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true +NVIC.DMA1_Channel6_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true +NVIC.DMA1_Channel7_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true NVIC.DMA2_Channel4_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false NVIC.EXTI0_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true -NVIC.EXTI3_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true NVIC.EXTI9_5_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true NVIC.ForceEnableDMAVector=true NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false @@ -221,10 +243,11 @@ PC11.Mode=SD_4_bits_Wide_bus PC11.Signal=SDMMC1_D3 PC12.Mode=SD_4_bits_Wide_bus PC12.Signal=SDMMC1_CK -PC3.GPIOParameters=GPIO_Label +PC3.GPIOParameters=GPIO_PuPd,GPIO_Label PC3.GPIO_Label=IMU_DRDY +PC3.GPIO_PuPd=GPIO_NOPULL PC3.Locked=true -PC3.Signal=GPXTI3 +PC3.Signal=GPIO_Input PC4.GPIOParameters=PinState,GPIO_Label PC4.GPIO_Label=IMU_CS PC4.Locked=true @@ -234,10 +257,11 @@ PC5.GPIOParameters=GPIO_Label PC5.GPIO_Label=BUTTON2 PC5.Locked=true PC5.Signal=GPIO_Input -PC7.GPIOParameters=GPIO_Label +PC7.GPIOParameters=GPIO_PuPd,GPIO_Label PC7.GPIO_Label=PPG_DRDY +PC7.GPIO_PuPd=GPIO_PULLUP PC7.Locked=true -PC7.Signal=GPXTI7 +PC7.Signal=GPIO_Input PC8.Mode=SD_4_bits_Wide_bus PC8.Signal=SDMMC1_D0 PC9.Mode=SD_4_bits_Wide_bus @@ -334,17 +358,13 @@ SH.ADCx_IN16.0=ADC1_IN16,IN16-Single-Ended SH.ADCx_IN16.ConfNb=1 SH.GPXTI0.0=GPIO_EXTI0 SH.GPXTI0.ConfNb=1 -SH.GPXTI3.0=GPIO_EXTI3 -SH.GPXTI3.ConfNb=1 SH.GPXTI5.0=GPIO_EXTI5 SH.GPXTI5.ConfNb=1 -SH.GPXTI7.0=GPIO_EXTI7 -SH.GPXTI7.ConfNb=1 SH.S_TIM2_ETR.0=TIM2_ETR,ClockSourceETR_Mode2 SH.S_TIM2_ETR.ConfNb=1 -SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_128 +SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_64 SPI1.CLKPhase=SPI_PHASE_2EDGE -SPI1.CalculateBaudRate=625.0 KBits/s +SPI1.CalculateBaudRate=1.25 MBits/s SPI1.DataSize=SPI_DATASIZE_8BIT SPI1.Direction=SPI_DIRECTION_2LINES SPI1.IPParameters=VirtualType,Mode,Direction,CalculateBaudRate,DataSize,BaudRatePrescaler,CLKPhase diff --git a/code/l452_code/packet_parser_serial.py b/code/l452_code/packet_parser_serial.py index 2904c4b..e1187bb 100644 --- a/code/l452_code/packet_parser_serial.py +++ b/code/l452_code/packet_parser_serial.py @@ -4,7 +4,6 @@ from scipy import signal import time import serial - ser = serial.Serial(port = '/dev/ttyACM1', timeout = 10.0) ser.reset_input_buffer() start = time.time() diff --git a/code/l452_code/ppg_plotter.py b/code/l452_code/ppg_plotter.py new file mode 100644 index 0000000..16e7451 --- /dev/null +++ b/code/l452_code/ppg_plotter.py @@ -0,0 +1,38 @@ +import serial +import numpy as np +import matplotlib.pyplot as plt +import time + +ser = serial.Serial(port = '/dev/ttyACM1', timeout = 10.0) +ser.reset_input_buffer() +ser.flush() + + +D = np.zeros((0,3)) + +fig, axs = plt.subplots(3) + +counter = 0 +start = time.time() +while(1): + ser.write(b"R") + read_data = ser.read(180) + counter += 1 + p = np.array([int.from_bytes(read_data[3 * i : 3 * i + 3], byteorder = 'big') for i in range(60)]).reshape(20,3) + D = np.concatenate((D, p)) + + if counter == 30: + print(15 * 30 / (time.time() - start)) + start = time.time() + counter = 0 + + if D.shape[0] > 400: + D = D[-400:,:] + + axs[0].cla() + axs[0].plot(D[:,0]) + axs[1].cla() + axs[1].plot(D[:,1]) + axs[2].cla() + axs[2].plot(D[:,2]) + plt.pause(0.00001)