add LIS302DL accelerometer testing function

This commit is contained in:
ІО-23 Шмуляр Олег 2025-03-09 21:53:24 +02:00
parent 8112ed27e8
commit 207f9c9ddf
5 changed files with 186 additions and 6 deletions

7
Core/Inc/LIS302DL.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef __LIS302DL
#define __LIS302DL
void LIS302DL_run_test(void);
void LIS302DL_run_test_dynamic(void);
#endif

164
Core/Src/LIS302DL.c Normal file
View File

@ -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");
}
}

View File

@ -29,6 +29,7 @@
#include "24AA02E48.h" #include "24AA02E48.h"
#include "CS43L22.h" #include "CS43L22.h"
#include "SST25VF016B.h" #include "SST25VF016B.h"
#include "LIS302DL.h"
/* USER CODE END Includes */ /* USER CODE END Includes */
@ -65,7 +66,9 @@ void ((*executors[])(void)) = {
PCA9685_run_test, PCA9685_run_test,
EEPROM_24AA02E48_run_test, EEPROM_24AA02E48_run_test,
CS43L22_run_test, CS43L22_run_test,
SST25VF016B_run_test SST25VF016B_run_test,
LIS302DL_run_test,
LIS302DL_run_test_dynamic
}; };
void ((*cleanup_functions[])(void)) = { void ((*cleanup_functions[])(void)) = {
@ -74,10 +77,12 @@ void ((*cleanup_functions[])(void)) = {
PCA9685_cleanup, PCA9685_cleanup,
NULL, NULL,
CS43L22_cleanup, CS43L22_cleanup,
NULL,
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; size_t current_executor_id = 0;
@ -101,7 +106,7 @@ static void MX_SPI1_Init(void);
void switch_to_next_test(void) void switch_to_next_test(void)
{ {
current_executor_id += 1; current_executor_id += 1;
current_executor_id %= 6; current_executor_id %= 8;
} }
void buttons_switch_to_interrupt(void) void buttons_switch_to_interrupt(void)
@ -283,11 +288,11 @@ int main(void)
if (delay_between_runs[current_executor_id] == -1) { if (delay_between_runs[current_executor_id] == -1) {
WAIT_UNTIL_CB_PRESSED; WAIT_UNTIL_CB_PRESSED;
HAL_Delay(150); HAL_Delay(150);
GPIOD->ODR = 0x1000; GPIOD->BSRR = 0x1000;
WAIT_UNTIL_CB_RELEASED; WAIT_UNTIL_CB_RELEASED;
HAL_Delay(150); HAL_Delay(150);
GPIOD->ODR = 0x0000; GPIOD->BSRR = 0x1000 << 16;
if (cleanup_functions[current_executor_id]) if (cleanup_functions[current_executor_id])
cleanup_functions[current_executor_id](); cleanup_functions[current_executor_id]();

View File

@ -8,6 +8,7 @@ C_SRCS += \
../Core/Src/24AA02E48.c \ ../Core/Src/24AA02E48.c \
../Core/Src/CS43L22.c \ ../Core/Src/CS43L22.c \
../Core/Src/DNI.c \ ../Core/Src/DNI.c \
../Core/Src/LIS302DL.c \
../Core/Src/PCA9685.c \ ../Core/Src/PCA9685.c \
../Core/Src/SST25VF016B.c \ ../Core/Src/SST25VF016B.c \
../Core/Src/lcd.c \ ../Core/Src/lcd.c \
@ -22,6 +23,7 @@ OBJS += \
./Core/Src/24AA02E48.o \ ./Core/Src/24AA02E48.o \
./Core/Src/CS43L22.o \ ./Core/Src/CS43L22.o \
./Core/Src/DNI.o \ ./Core/Src/DNI.o \
./Core/Src/LIS302DL.o \
./Core/Src/PCA9685.o \ ./Core/Src/PCA9685.o \
./Core/Src/SST25VF016B.o \ ./Core/Src/SST25VF016B.o \
./Core/Src/lcd.o \ ./Core/Src/lcd.o \
@ -36,6 +38,7 @@ C_DEPS += \
./Core/Src/24AA02E48.d \ ./Core/Src/24AA02E48.d \
./Core/Src/CS43L22.d \ ./Core/Src/CS43L22.d \
./Core/Src/DNI.d \ ./Core/Src/DNI.d \
./Core/Src/LIS302DL.d \
./Core/Src/PCA9685.d \ ./Core/Src/PCA9685.d \
./Core/Src/SST25VF016B.d \ ./Core/Src/SST25VF016B.d \
./Core/Src/lcd.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: clean-Core-2f-Src
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 .PHONY: clean-Core-2f-Src

View File

@ -1,6 +1,7 @@
"./Core/Src/24AA02E48.o" "./Core/Src/24AA02E48.o"
"./Core/Src/CS43L22.o" "./Core/Src/CS43L22.o"
"./Core/Src/DNI.o" "./Core/Src/DNI.o"
"./Core/Src/LIS302DL.o"
"./Core/Src/PCA9685.o" "./Core/Src/PCA9685.o"
"./Core/Src/SST25VF016B.o" "./Core/Src/SST25VF016B.o"
"./Core/Src/lcd.o" "./Core/Src/lcd.o"