diff --git a/Core/Inc/LSM9DS1.h b/Core/Inc/LSM9DS1.h new file mode 100644 index 0000000..611659d --- /dev/null +++ b/Core/Inc/LSM9DS1.h @@ -0,0 +1,8 @@ +#ifndef __LSM9DS1 +#define __LSM9DS1 + +void LSM9DS1_test_accel(void); +void LSM9DS1_cleanup_accel(void); +void LSM9DS1_test_magnet(void); + +#endif diff --git a/Core/Src/LSM9DS1.c b/Core/Src/LSM9DS1.c new file mode 100644 index 0000000..edff896 --- /dev/null +++ b/Core/Src/LSM9DS1.c @@ -0,0 +1,129 @@ +#include "main.h" +#include "LSM9DS1.h" +#include "lcd.h" + +#define FAILSAFE_PRE_OP for (size_t t = 0; t < 5; t++) { +#define FAILSAFE_POST_OP_ACCEL(prefix) if (op_result) print_error_accel(op_result, err_count, t, prefix); else break; } +#define FAILSAFE_POST_OP_MAGNET(prefix) if (op_result) print_error_magnet(op_result, err_count, t, prefix); else break; } + +extern I2C_HandleTypeDef hi2c1; + +static void print_error_message(HAL_StatusTypeDef result, size_t *err_count, size_t t, char *prefix) +{ + 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(1000); +} + +static void print_error_accel(HAL_StatusTypeDef result, size_t *err_count, size_t t, char *prefix) +{ + DISPLAY_CLEAR; + display_write_data_seq("LSM9DS1 Accel"); + + print_error_message(result, err_count, t, prefix); +} + +static void print_error_magnet(HAL_StatusTypeDef result, size_t *err_count, size_t t, char *prefix) +{ + DISPLAY_CLEAR; + display_write_data_seq("LSM9DS1 Magnet"); + + print_error_message(result, err_count, t, prefix); +} + +void LSM9DS1_test_accel(void) +{ + DISPLAY_CLEAR; + display_write_data_seq("LSM9DS1 Accel"); + + HAL_StatusTypeDef op_result; + uint8_t buffer[6]; + size_t err_count = 0; + + // enable sampling at 10 Hz + buffer[0] = 0x20; + buffer[1] = 0x20; + FAILSAFE_PRE_OP; + op_result = HAL_I2C_Master_Transmit(&hi2c1, 0xD6, buffer, 2, 1000); + FAILSAFE_POST_OP_ACCEL("E"); + + HAL_Delay(100); + + // set future read address + buffer[0] = 0x28; + FAILSAFE_PRE_OP; + op_result = HAL_I2C_Master_Transmit(&hi2c1, 0xD6, buffer, 1, 1000); + FAILSAFE_POST_OP_ACCEL("A"); + + // read from registers sequentially + FAILSAFE_PRE_OP; + op_result = HAL_I2C_Master_Receive(&hi2c1, 0xD7, buffer, 6, 1000); + FAILSAFE_POST_OP_ACCEL("R"); + + if (!err_count) { + DISPLAY_SET_CURSOR(1, 1); + display_write_data_seq("OK"); + } else { + 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(" errors"); + } +} + +void LSM9DS1_cleanup_accel(void) +{ + // power down the accelerometer + uint8_t buffer[2] = {0x20, 0x00}; + HAL_I2C_Master_Transmit(&hi2c1, 0xD6, buffer, 2, 1000); +} + +void LSM9DS1_test_magnet(void) +{ + DISPLAY_CLEAR; + display_write_data_seq("LSM9DS1 Magnet"); + + HAL_StatusTypeDef op_result; + uint8_t buffer[6]; + size_t err_count = 0; + + // set future read address + buffer[0] = 0x28; + FAILSAFE_PRE_OP; + op_result = HAL_I2C_Master_Transmit(&hi2c1, 0x3C, buffer, 1, 1000); + FAILSAFE_POST_OP_ACCEL("A"); + + // read from registers sequentially + FAILSAFE_PRE_OP; + op_result = HAL_I2C_Master_Receive(&hi2c1, 0x3D, buffer, 6, 1000); + FAILSAFE_POST_OP_ACCEL("R"); + + if (!err_count) { + DISPLAY_SET_CURSOR(1, 1); + display_write_data_seq("OK"); + } else { + 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(" errors"); + } +} diff --git a/Core/Src/main.c b/Core/Src/main.c index 3d17360..93bdbfa 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -32,6 +32,7 @@ #include "LIS302DL.h" #include "DHT11.h" #include "MP45DT02.h" +#include "LSM9DS1.h" /* USER CODE END Includes */ @@ -75,7 +76,9 @@ void ((*executors[])(void)) = { SST25VF016B_run_test, LIS302DL_run_test, LIS302DL_run_test_dynamic, - MP45DT02_run_test + MP45DT02_run_test, + LSM9DS1_test_accel, + LSM9DS1_test_magnet }; void ((*cleanup_functions[])(void)) = { @@ -87,6 +90,8 @@ void ((*cleanup_functions[])(void)) = { NULL, NULL, NULL, + NULL, + LSM9DS1_cleanup_accel, NULL }; @@ -99,6 +104,8 @@ int delay_between_runs[] = { -1, -1, 200, + -1, + -1, -1 }; @@ -126,7 +133,7 @@ static void MX_I2S2_Init(void); void switch_to_next_test(void) { current_executor_id += 1; - current_executor_id %= 9; + current_executor_id %= 11; } void buttons_switch_to_interrupt(void) diff --git a/Debug/Core/Src/subdir.mk b/Debug/Core/Src/subdir.mk index b4c756c..85c5623 100644 --- a/Debug/Core/Src/subdir.mk +++ b/Debug/Core/Src/subdir.mk @@ -10,6 +10,7 @@ C_SRCS += \ ../Core/Src/DHT11.c \ ../Core/Src/DNI.c \ ../Core/Src/LIS302DL.c \ +../Core/Src/LSM9DS1.c \ ../Core/Src/MP45DT02.c \ ../Core/Src/PCA9685.c \ ../Core/Src/SST25VF016B.c \ @@ -27,6 +28,7 @@ OBJS += \ ./Core/Src/DHT11.o \ ./Core/Src/DNI.o \ ./Core/Src/LIS302DL.o \ +./Core/Src/LSM9DS1.o \ ./Core/Src/MP45DT02.o \ ./Core/Src/PCA9685.o \ ./Core/Src/SST25VF016B.o \ @@ -44,6 +46,7 @@ C_DEPS += \ ./Core/Src/DHT11.d \ ./Core/Src/DNI.d \ ./Core/Src/LIS302DL.d \ +./Core/Src/LSM9DS1.d \ ./Core/Src/MP45DT02.d \ ./Core/Src/PCA9685.d \ ./Core/Src/SST25VF016B.d \ @@ -63,7 +66,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/DHT11.cyclo ./Core/Src/DHT11.d ./Core/Src/DHT11.o ./Core/Src/DHT11.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/MP45DT02.cyclo ./Core/Src/MP45DT02.d ./Core/Src/MP45DT02.o ./Core/Src/MP45DT02.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/DHT11.cyclo ./Core/Src/DHT11.d ./Core/Src/DHT11.o ./Core/Src/DHT11.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/LSM9DS1.cyclo ./Core/Src/LSM9DS1.d ./Core/Src/LSM9DS1.o ./Core/Src/LSM9DS1.su ./Core/Src/MP45DT02.cyclo ./Core/Src/MP45DT02.d ./Core/Src/MP45DT02.o ./Core/Src/MP45DT02.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 dc44988..b546871 100644 --- a/Debug/objects.list +++ b/Debug/objects.list @@ -3,6 +3,7 @@ "./Core/Src/DHT11.o" "./Core/Src/DNI.o" "./Core/Src/LIS302DL.o" +"./Core/Src/LSM9DS1.o" "./Core/Src/MP45DT02.o" "./Core/Src/PCA9685.o" "./Core/Src/SST25VF016B.o"