From 207f9c9ddfb3e2025abce5d2c99e42e5dc0be889 Mon Sep 17 00:00:00 2001 From: hasslesstech Date: Sun, 9 Mar 2025 21:53:24 +0200 Subject: [PATCH] add LIS302DL accelerometer testing function --- Core/Inc/LIS302DL.h | 7 ++ Core/Src/LIS302DL.c | 164 +++++++++++++++++++++++++++++++++++++++ Core/Src/main.c | 15 ++-- Debug/Core/Src/subdir.mk | 5 +- Debug/objects.list | 1 + 5 files changed, 186 insertions(+), 6 deletions(-) create mode 100644 Core/Inc/LIS302DL.h create mode 100644 Core/Src/LIS302DL.c diff --git a/Core/Inc/LIS302DL.h b/Core/Inc/LIS302DL.h new file mode 100644 index 0000000..b4bfb0a --- /dev/null +++ b/Core/Inc/LIS302DL.h @@ -0,0 +1,7 @@ +#ifndef __LIS302DL +#define __LIS302DL + +void LIS302DL_run_test(void); +void LIS302DL_run_test_dynamic(void); + +#endif diff --git a/Core/Src/LIS302DL.c b/Core/Src/LIS302DL.c new file mode 100644 index 0000000..71cc240 --- /dev/null +++ b/Core/Src/LIS302DL.c @@ -0,0 +1,164 @@ +#include "main.h" +#include "lcd.h" +#include "LIS302DL.h" + +#define FAILSAFE_PRE_OP for (size_t t = 0; t < 5; t++) { +#define FAILSAFE_POST_OP(prefix) if (op_result) print_error_message(op_result, err_count, t, prefix, postfix); else break; } + +extern I2C_HandleTypeDef hi2c1; + +static char convert_char_at(uint8_t value, size_t position) +{ + char d = (value >> (4 * position)) % 16; + + if (d > 9) + d += 7; + + return '0' + d; +} + +static void print_at(uint8_t value, size_t offset) +{ + DISPLAY_SET_CURSOR(1, offset); + + // most significant digit + display_write_data_byte(convert_char_at(value, 1)); + + // least significant digit + display_write_data_byte(convert_char_at(value, 0)); +} + +static void print_error_message(HAL_StatusTypeDef result, size_t *err_count, size_t t, char *prefix, char *postfix) +{ + DISPLAY_CLEAR; + display_write_data_seq("LIS302DL Accel "); + display_write_data_seq(postfix); + DISPLAY_SET_CURSOR(1, 0); + display_write_data_seq(prefix); + display_write_data_byte(' '); + + (*err_count)++; + + display_write_data_byte('1' + t); + display_write_data_seq("/5 "); + + switch (result) { + case HAL_ERROR: + display_write_data_seq("ERROR"); + break; + case HAL_BUSY: + display_write_data_seq("BUSY"); + break; + case HAL_TIMEOUT: + display_write_data_seq("TIMEOUT"); + break; + } + + HAL_Delay(2000); +} + +static void retrieve_data(size_t *err_count, uint8_t *data_xyz, char *postfix) +{ + HAL_StatusTypeDef op_result; + uint8_t data_buffer[2]; + + // enable device + data_buffer[0] = 0x20; + data_buffer[1] = 0x47; + FAILSAFE_PRE_OP; + op_result = HAL_I2C_Master_Transmit(&hi2c1, 0xD6, data_buffer, 2, 2000); + FAILSAFE_POST_OP("E"); + + // select OutX register + data_buffer[0] = 0x29; + FAILSAFE_PRE_OP; + op_result = HAL_I2C_Master_Transmit(&hi2c1, 0xD6 | 0x1, data_buffer, 1, 2000); + FAILSAFE_POST_OP("SX"); + + // receive data from it + FAILSAFE_PRE_OP; + op_result = HAL_I2C_Master_Receive(&hi2c1, 0xD6 | 0x1, data_xyz, 1, 2000); + FAILSAFE_POST_OP("RX"); + + // select OutY register + data_buffer[0] = 0x2B; + FAILSAFE_PRE_OP; + op_result = HAL_I2C_Master_Transmit(&hi2c1, 0xD6 | 0x1, data_buffer, 1, 2000); + FAILSAFE_POST_OP("SY"); + + // receive data from it + FAILSAFE_PRE_OP; + op_result = HAL_I2C_Master_Receive(&hi2c1, 0xD6 | 0x1, &(data_xyz[1]), 1, 2000); + FAILSAFE_POST_OP("RY"); + + // select OutZ register + data_buffer[0] = 0x2D; + FAILSAFE_PRE_OP; + op_result = HAL_I2C_Master_Transmit(&hi2c1, 0xD6 | 0x1, data_buffer, 1, 2000); + FAILSAFE_POST_OP("SZ"); + + // receive data from it + FAILSAFE_PRE_OP; + op_result = HAL_I2C_Master_Receive(&hi2c1, 0xD6 | 0x1, &(data_xyz[2]), 1, 2000); + FAILSAFE_POST_OP("RZ"); +} + +void LIS302DL_run_test(void) +{ + DISPLAY_CLEAR; + display_write_data_seq("LIS302DL Accel"); + + size_t err_count = 0; + uint8_t data_xyz[3]; + + retrieve_data(&err_count, data_xyz, ""); + + DISPLAY_CLEAR; + display_write_data_seq("LIS302DL Accel"); + + // output retrieved values + print_at(data_xyz[2], 14); + print_at(data_xyz[1], 11); + print_at(data_xyz[0], 8); + + // print the execution stats + if (err_count) { + DISPLAY_SET_CURSOR(1, 0); + display_write_data_byte('0' + err_count / 10 % 10); + display_write_data_byte('0' + err_count % 10); + display_write_data_seq(" errs"); + } else { + DISPLAY_SET_CURSOR(1, 1); + display_write_data_seq("OK"); + } +} + +void LIS302DL_run_test_dynamic(void) +{ + DISPLAY_CLEAR; + display_write_data_seq("LIS302DL Accel D"); + + size_t err_count = 0; + uint8_t data_xyz[3]; + + retrieve_data(&err_count, data_xyz, "D"); + + DISPLAY_CLEAR; + display_write_data_seq("LIS302DL Accel D"); + + // output retrieved values + print_at(data_xyz[2], 14); + print_at(data_xyz[1], 11); + print_at(data_xyz[0], 8); + + // print the execution stats + if (err_count) { + DISPLAY_SET_CURSOR(1, 0); + display_write_data_byte('0' + err_count / 10 % 10); + display_write_data_byte('0' + err_count % 10); + display_write_data_seq(" errs"); + } else { + DISPLAY_SET_CURSOR(1, 1); + display_write_data_seq("OK"); + } +} diff --git a/Core/Src/main.c b/Core/Src/main.c index d13016d..673b8ec 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -29,6 +29,7 @@ #include "24AA02E48.h" #include "CS43L22.h" #include "SST25VF016B.h" +#include "LIS302DL.h" /* USER CODE END Includes */ @@ -65,7 +66,9 @@ void ((*executors[])(void)) = { PCA9685_run_test, EEPROM_24AA02E48_run_test, CS43L22_run_test, - SST25VF016B_run_test + SST25VF016B_run_test, + LIS302DL_run_test, + LIS302DL_run_test_dynamic }; void ((*cleanup_functions[])(void)) = { @@ -74,10 +77,12 @@ void ((*cleanup_functions[])(void)) = { PCA9685_cleanup, NULL, CS43L22_cleanup, + NULL, + NULL, NULL }; -int delay_between_runs[] = {250, 250, -1, -1, -1, -1}; +int delay_between_runs[] = {250, 250, -1, -1, -1, -1, -1, 200}; size_t current_executor_id = 0; @@ -101,7 +106,7 @@ static void MX_SPI1_Init(void); void switch_to_next_test(void) { current_executor_id += 1; - current_executor_id %= 6; + current_executor_id %= 8; } void buttons_switch_to_interrupt(void) @@ -283,11 +288,11 @@ int main(void) if (delay_between_runs[current_executor_id] == -1) { WAIT_UNTIL_CB_PRESSED; HAL_Delay(150); - GPIOD->ODR = 0x1000; + GPIOD->BSRR = 0x1000; WAIT_UNTIL_CB_RELEASED; HAL_Delay(150); - GPIOD->ODR = 0x0000; + GPIOD->BSRR = 0x1000 << 16; if (cleanup_functions[current_executor_id]) cleanup_functions[current_executor_id](); diff --git a/Debug/Core/Src/subdir.mk b/Debug/Core/Src/subdir.mk index 59dbd38..03c71dc 100644 --- a/Debug/Core/Src/subdir.mk +++ b/Debug/Core/Src/subdir.mk @@ -8,6 +8,7 @@ C_SRCS += \ ../Core/Src/24AA02E48.c \ ../Core/Src/CS43L22.c \ ../Core/Src/DNI.c \ +../Core/Src/LIS302DL.c \ ../Core/Src/PCA9685.c \ ../Core/Src/SST25VF016B.c \ ../Core/Src/lcd.c \ @@ -22,6 +23,7 @@ OBJS += \ ./Core/Src/24AA02E48.o \ ./Core/Src/CS43L22.o \ ./Core/Src/DNI.o \ +./Core/Src/LIS302DL.o \ ./Core/Src/PCA9685.o \ ./Core/Src/SST25VF016B.o \ ./Core/Src/lcd.o \ @@ -36,6 +38,7 @@ C_DEPS += \ ./Core/Src/24AA02E48.d \ ./Core/Src/CS43L22.d \ ./Core/Src/DNI.d \ +./Core/Src/LIS302DL.d \ ./Core/Src/PCA9685.d \ ./Core/Src/SST25VF016B.d \ ./Core/Src/lcd.d \ @@ -54,7 +57,7 @@ Core/Src/%.o Core/Src/%.su Core/Src/%.cyclo: ../Core/Src/%.c Core/Src/subdir.mk clean: clean-Core-2f-Src clean-Core-2f-Src: - -$(RM) ./Core/Src/24AA02E48.cyclo ./Core/Src/24AA02E48.d ./Core/Src/24AA02E48.o ./Core/Src/24AA02E48.su ./Core/Src/CS43L22.cyclo ./Core/Src/CS43L22.d ./Core/Src/CS43L22.o ./Core/Src/CS43L22.su ./Core/Src/DNI.cyclo ./Core/Src/DNI.d ./Core/Src/DNI.o ./Core/Src/DNI.su ./Core/Src/PCA9685.cyclo ./Core/Src/PCA9685.d ./Core/Src/PCA9685.o ./Core/Src/PCA9685.su ./Core/Src/SST25VF016B.cyclo ./Core/Src/SST25VF016B.d ./Core/Src/SST25VF016B.o ./Core/Src/SST25VF016B.su ./Core/Src/lcd.cyclo ./Core/Src/lcd.d ./Core/Src/lcd.o ./Core/Src/lcd.su ./Core/Src/main.cyclo ./Core/Src/main.d ./Core/Src/main.o ./Core/Src/main.su ./Core/Src/stm32f4xx_hal_msp.cyclo ./Core/Src/stm32f4xx_hal_msp.d ./Core/Src/stm32f4xx_hal_msp.o ./Core/Src/stm32f4xx_hal_msp.su ./Core/Src/stm32f4xx_it.cyclo ./Core/Src/stm32f4xx_it.d ./Core/Src/stm32f4xx_it.o ./Core/Src/stm32f4xx_it.su ./Core/Src/syscalls.cyclo ./Core/Src/syscalls.d ./Core/Src/syscalls.o ./Core/Src/syscalls.su ./Core/Src/sysmem.cyclo ./Core/Src/sysmem.d ./Core/Src/sysmem.o ./Core/Src/sysmem.su ./Core/Src/system_stm32f4xx.cyclo ./Core/Src/system_stm32f4xx.d ./Core/Src/system_stm32f4xx.o ./Core/Src/system_stm32f4xx.su + -$(RM) ./Core/Src/24AA02E48.cyclo ./Core/Src/24AA02E48.d ./Core/Src/24AA02E48.o ./Core/Src/24AA02E48.su ./Core/Src/CS43L22.cyclo ./Core/Src/CS43L22.d ./Core/Src/CS43L22.o ./Core/Src/CS43L22.su ./Core/Src/DNI.cyclo ./Core/Src/DNI.d ./Core/Src/DNI.o ./Core/Src/DNI.su ./Core/Src/LIS302DL.cyclo ./Core/Src/LIS302DL.d ./Core/Src/LIS302DL.o ./Core/Src/LIS302DL.su ./Core/Src/PCA9685.cyclo ./Core/Src/PCA9685.d ./Core/Src/PCA9685.o ./Core/Src/PCA9685.su ./Core/Src/SST25VF016B.cyclo ./Core/Src/SST25VF016B.d ./Core/Src/SST25VF016B.o ./Core/Src/SST25VF016B.su ./Core/Src/lcd.cyclo ./Core/Src/lcd.d ./Core/Src/lcd.o ./Core/Src/lcd.su ./Core/Src/main.cyclo ./Core/Src/main.d ./Core/Src/main.o ./Core/Src/main.su ./Core/Src/stm32f4xx_hal_msp.cyclo ./Core/Src/stm32f4xx_hal_msp.d ./Core/Src/stm32f4xx_hal_msp.o ./Core/Src/stm32f4xx_hal_msp.su ./Core/Src/stm32f4xx_it.cyclo ./Core/Src/stm32f4xx_it.d ./Core/Src/stm32f4xx_it.o ./Core/Src/stm32f4xx_it.su ./Core/Src/syscalls.cyclo ./Core/Src/syscalls.d ./Core/Src/syscalls.o ./Core/Src/syscalls.su ./Core/Src/sysmem.cyclo ./Core/Src/sysmem.d ./Core/Src/sysmem.o ./Core/Src/sysmem.su ./Core/Src/system_stm32f4xx.cyclo ./Core/Src/system_stm32f4xx.d ./Core/Src/system_stm32f4xx.o ./Core/Src/system_stm32f4xx.su .PHONY: clean-Core-2f-Src diff --git a/Debug/objects.list b/Debug/objects.list index 1ab18c6..fcf55b1 100644 --- a/Debug/objects.list +++ b/Debug/objects.list @@ -1,6 +1,7 @@ "./Core/Src/24AA02E48.o" "./Core/Src/CS43L22.o" "./Core/Src/DNI.o" +"./Core/Src/LIS302DL.o" "./Core/Src/PCA9685.o" "./Core/Src/SST25VF016B.o" "./Core/Src/lcd.o"