From 1ae59ca9c50a069fc9ed59635bec22ddf8d65872 Mon Sep 17 00:00:00 2001 From: Jack Rubacha Date: Tue, 5 Nov 2024 14:26:16 -0500 Subject: [PATCH] Feature/22a hw init (#8) * inital code changes Working: LEDs, temp sensor, IMU (need to verify numbers) Not initing: TOF Not tested but runs: ADCs * add ner build env support * fix prettier violations * CAN is operational! * can fixups * changing msb for imu driver * annoying blink on hard fault * validated * updated submodule * format --------- Co-authored-by: Caio Co-authored-by: Caio DaSilva <145620095+caiodasilva2005@users.noreply.github.com> --- .gitignore | 2 + Core/Inc/FreeRTOSConfig.h | 4 +- Core/Inc/msb.h | 1 + Core/Inc/msb_conf.h | 9 +++-- Core/Inc/serial_monitor.h | 15 -------- Core/Inc/stm32f4xx_it.h | 1 - Core/Src/can_handler.c | 20 +++++----- Core/Src/controller.c | 61 ++++++++++++++++++++--------- Core/Src/freertos.c | 35 ++++++++++++++++- Core/Src/main.c | 28 +++++++++----- Core/Src/monitor.c | 74 +++++++++++++++--------------------- Core/Src/msb.c | 36 +++++++++++++----- Core/Src/serial_monitor.c | 65 ------------------------------- Core/Src/stm32f4xx_hal_msp.c | 5 --- Core/Src/stm32f4xx_it.c | 30 +++++++-------- Drivers/Embedded-Base | 2 +- MSB-FW.ioc | 16 ++++---- Makefile | 3 +- compose.yml | 3 +- flash.cfg | 9 +++++ 20 files changed, 211 insertions(+), 208 deletions(-) delete mode 100644 Core/Inc/serial_monitor.h delete mode 100644 Core/Src/serial_monitor.c create mode 100644 flash.cfg diff --git a/.gitignore b/.gitignore index 9b5a1ba..f4cb8c4 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,8 @@ # Build files build/ +*.nix + # Prerequisites *.d diff --git a/Core/Inc/FreeRTOSConfig.h b/Core/Inc/FreeRTOSConfig.h index 5331016..251de20 100644 --- a/Core/Inc/FreeRTOSConfig.h +++ b/Core/Inc/FreeRTOSConfig.h @@ -59,7 +59,7 @@ #define configENABLE_FPU 0 #define configENABLE_MPU 0 -#define configUSE_PREEMPTION 1 +#define configUSE_PREEMPTION 0 #define configSUPPORT_STATIC_ALLOCATION 1 #define configSUPPORT_DYNAMIC_ALLOCATION 1 #define configUSE_IDLE_HOOK 0 @@ -74,7 +74,9 @@ #define configUSE_16_BIT_TICKS 0 #define configUSE_MUTEXES 1 #define configQUEUE_REGISTRY_SIZE 8 +#define configCHECK_FOR_STACK_OVERFLOW 2 #define configUSE_RECURSIVE_MUTEXES 1 +#define configUSE_MALLOC_FAILED_HOOK 1 #define configUSE_COUNTING_SEMAPHORES 1 #define configUSE_PORT_OPTIMISED_TASK_SELECTION 0 /* USER CODE BEGIN MESSAGE_BUFFER_LENGTH_TYPE */ diff --git a/Core/Inc/msb.h b/Core/Inc/msb.h index 8ed335e..d58773d 100644 --- a/Core/Inc/msb.h +++ b/Core/Inc/msb.h @@ -7,6 +7,7 @@ #include "sht30.h" #include "stm32f405xx.h" #include "vl6180x_api.h" +#include "vl6180x_platform.h" #include "msb_conf.h" typedef enum { diff --git a/Core/Inc/msb_conf.h b/Core/Inc/msb_conf.h index 8f5cb9b..2877ed8 100644 --- a/Core/Inc/msb_conf.h +++ b/Core/Inc/msb_conf.h @@ -13,7 +13,7 @@ // CONTROLLER #define DELAY_DEBUG_LED_REFRESH 250 -#define DELAY_CAN_DISPATCH 5 +#define DELAY_CAN_DISPATCH 2 // CAN IDS #define CANID_TEMP_SENSOR 0x602 @@ -26,14 +26,17 @@ // Sensors to use, comment out to disable +// internal +#define CAN_ENABLE + //on central #define SENSOR_TEMP #define SENSOR_SHOCKPOT #define SENSOR_STRAIN -#define SENSOR_TOF +//#define SENSOR_TOF -// on knuckle or wheel #define SENSOR_IMU +// on knuckle or /wheel //#define SENSOR_WHEEL_TEMP // VERBOSE LOGGING diff --git a/Core/Inc/serial_monitor.h b/Core/Inc/serial_monitor.h deleted file mode 100644 index 31cb392..0000000 --- a/Core/Inc/serial_monitor.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef SERIAL_MONITOR_H -#define SERIAL_MONITOR_H - -#include "cmsis_os.h" - -/* Function to queue a message to be sent on the UART stream */ -int serial_print(const char *format, ...); - -/* Defining Temperature Monitor Task */ -void vSerialMonitor(void *pv_params); - -extern osThreadId_t serial_monitor_handle; -extern const osThreadAttr_t serial_monitor_attributes; - -#endif // SERIAL_MONITOR_H diff --git a/Core/Inc/stm32f4xx_it.h b/Core/Inc/stm32f4xx_it.h index 53e0c55..232bacd 100644 --- a/Core/Inc/stm32f4xx_it.h +++ b/Core/Inc/stm32f4xx_it.h @@ -52,7 +52,6 @@ void MemManage_Handler(void); void BusFault_Handler(void); void UsageFault_Handler(void); void DebugMon_Handler(void); -void CAN1_RX0_IRQHandler(void); void TIM5_IRQHandler(void); void DMA2_Stream0_IRQHandler(void); /* USER CODE BEGIN EFP */ diff --git a/Core/Src/can_handler.c b/Core/Src/can_handler.c index 9c5680b..460ee90 100644 --- a/Core/Src/can_handler.c +++ b/Core/Src/can_handler.c @@ -12,7 +12,6 @@ #include "can_handler.h" #include "can.h" #include "msb_conf.h" -#include "serial_monitor.h" #include "stdio.h" #include @@ -28,12 +27,14 @@ can_t *can1; void can1_init() { +#ifdef CAN_ENABLE can1 = malloc(sizeof(can_t)); assert(can1); can1->hcan = &hcan1; assert(!can_init(can1)); +#endif can_outbound_queue = osMessageQueueNew(CAN_MSG_QUEUE_SIZE, sizeof(can_msg_t), NULL); @@ -49,28 +50,29 @@ const osThreadAttr_t can_dispatch_attributes = { void vCanDispatch(void *pv_params) { can_msg_t msg_from_queue; +#ifdef CAN_ENABLE HAL_StatusTypeDef msg_status; +#endif for (;;) { /* Send CAN message */ if (osOK == osMessageQueueGet(can_outbound_queue, &msg_from_queue, NULL, osWaitForever)) { +#ifdef CAN_ENABLE msg_status = can_send_msg(can1, &msg_from_queue); - if (msg_status == HAL_ERROR) { - serial_print("Failed to send CAN message"); + if (msg_status != HAL_OK) { + printf("Failed to send CAN message"); } else if (msg_status == HAL_BUSY) { - serial_print("Outbound mailbox full!"); + printf("Outbound mailbox full!"); } +#endif #ifdef LOG_VERBOSE - else { - printf("Message sent: %lX\r\n", - msg_from_queue.id); - } + printf("Message sent att: %lX\r\n", msg_from_queue.id); #endif } - osDelay(DELAY_CAN_DISPATCH); + //osDelay(DELAY_CAN_DISPATCH); } } diff --git a/Core/Src/controller.c b/Core/Src/controller.c index 6029ffa..b981d50 100644 --- a/Core/Src/controller.c +++ b/Core/Src/controller.c @@ -13,26 +13,49 @@ const osThreadAttr_t led_controller_attributes = { void vLedController(void *pv_params) { - switch (device_loc) { - case DEVICE_FRONT_LEFT: - debug1_write(true); - debug2_write(true); - break; - case DEVICE_FRONT_RIGHT: - debug1_write(true); - debug2_write(false); - break; - case DEVICE_BACK_LEFT: - debug1_write(false); - debug2_write(true); - break; - case DEVICE_BACK_RIGHT: - debug1_write(false); - debug2_write(false); - break; - } - + uint8_t i = 0; for (;;) { + if (i % 8 == 0) { + // occassionally oposing blink + switch (device_loc) { + case DEVICE_FRONT_LEFT: + debug1_write(false); + debug2_write(false); + break; + case DEVICE_FRONT_RIGHT: + debug1_write(false); + debug2_write(true); + break; + case DEVICE_BACK_LEFT: + debug1_write(true); + debug2_write(false); + break; + case DEVICE_BACK_RIGHT: + debug1_write(true); + debug2_write(true); + break; + } + } else { + switch (device_loc) { + case DEVICE_FRONT_LEFT: + debug1_write(true); + debug2_write(true); + break; + case DEVICE_FRONT_RIGHT: + debug1_write(true); + debug2_write(false); + break; + case DEVICE_BACK_LEFT: + debug1_write(false); + debug2_write(true); + break; + case DEVICE_BACK_RIGHT: + debug1_write(false); + debug2_write(false); + break; + } + } + i++; osDelay(DELAY_DEBUG_LED_REFRESH); } } \ No newline at end of file diff --git a/Core/Src/freertos.c b/Core/Src/freertos.c index 6a0d0f3..453e994 100644 --- a/Core/Src/freertos.c +++ b/Core/Src/freertos.c @@ -24,7 +24,7 @@ /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ - +#include /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -52,6 +52,39 @@ /* USER CODE END FunctionPrototypes */ +/* Hook prototypes */ +void vApplicationStackOverflowHook(xTaskHandle xTask, signed char *pcTaskName); +void vApplicationMallocFailedHook(void); + +/* USER CODE BEGIN 4 */ +void vApplicationStackOverflowHook(xTaskHandle xTask, signed char *pcTaskName) +{ + /* Run time stack overflow checking is performed if + configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook function is + called if a stack overflow is detected. */ + printf("ERROR: STACK OVERFLOW in "); + printf((char *)pcTaskName); + printf("\r\n"); +} +/* USER CODE END 4 */ + +/* USER CODE BEGIN 5 */ +void vApplicationMallocFailedHook(void) +{ + /* vApplicationMallocFailedHook() will only be called if + configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook + function that will get called if a call to pvPortMalloc() fails. + pvPortMalloc() is called internally by the kernel whenever a task, queue, + timer or semaphore is created. It is also called by various parts of the + demo application. If heap_1.c or heap_2.c are used, then the size of the + heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in + FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used + to query the size of free heap space that remains (although it does not + provide information on how the remaining heap might be fragmented). */ + printf("ERROR: MALLOC FAILED\r\n"); +} +/* USER CODE END 5 */ + /* Private application code --------------------------------------------------*/ /* USER CODE BEGIN Application */ diff --git a/Core/Src/main.c b/Core/Src/main.c index 2fbf4f3..b2d69b2 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -24,11 +24,11 @@ /* USER CODE BEGIN Includes */ #include "can_handler.h" #include "controller.h" -#include "serial_monitor.h" #include "monitor.h" #include "msb.h" #include "msb_conf.h" #include "assert.h" +#include /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -150,6 +150,7 @@ int main(void) MX_SPI2_Init(); MX_USART2_UART_Init(); /* USER CODE BEGIN 2 */ + printf("INIT MSB... FINDING ID"); // determine MSB location bool loc1 = HAL_GPIO_ReadPin(Addr0_GPIO_Port, Addr0_Pin); bool loc2 = HAL_GPIO_ReadPin(Addr1_GPIO_Port, Addr1_Pin); @@ -176,6 +177,7 @@ int main(void) device_loc = DEVICE_FRONT_LEFT; } + /* USER CODE END 2 */ /* Init scheduler */ @@ -203,8 +205,6 @@ int main(void) /* add queues, ... */ can_dispatch_handle = osThreadNew(vCanDispatch, NULL, &can_dispatch_attributes); assert(can_dispatch_handle); - serial_monitor_handle = osThreadNew(vSerialMonitor, NULL, &serial_monitor_attributes); - assert(serial_monitor_handle); /* USER CODE END RTOS_QUEUES */ /* Create the thread(s) */ @@ -224,17 +224,17 @@ int main(void) #endif #ifdef SENSOR_TOF - tof_monitor_handle = osThreadNew(vIMUMonitor, NULL, &tof_monitor_attributes); + tof_monitor_handle = osThreadNew(vTOFMonitor, NULL, &tof_monitor_attributes); assert(tof_monitor_handle); #endif #ifdef SENSOR_SHOCKPOT - shockpot_monitor_handle = osThreadNew(vIMUMonitor, NULL, &shockpot_monitor_attributes); + shockpot_monitor_handle = osThreadNew(vShockpotMonitor, NULL, &shockpot_monitor_attributes); assert(shockpot_monitor_handle); #endif #ifdef SENSOR_STRAIN - strain_monitor_handle = osThreadNew(vIMUMonitor, NULL, &strain_monitor_attributes); + strain_monitor_handle = osThreadNew(vStrainMonitor, NULL, &strain_monitor_attributes); assert(strain_monitor_handle); #endif @@ -398,7 +398,7 @@ static void MX_CAN1_Init(void) /* USER CODE END CAN1_Init 1 */ hcan1.Instance = CAN1; - hcan1.Init.Prescaler = 1; + hcan1.Init.Prescaler = 2; hcan1.Init.Mode = CAN_MODE_NORMAL; hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ; hcan1.Init.TimeSeg1 = CAN_BS1_13TQ; @@ -414,7 +414,6 @@ static void MX_CAN1_Init(void) Error_Handler(); } /* USER CODE BEGIN CAN1_Init 2 */ - can1_init(&hcan1); /* USER CODE END CAN1_Init 2 */ @@ -620,11 +619,19 @@ static void MX_GPIO_Init(void) void StartDefaultTask(void *argument) { /* USER CODE BEGIN 5 */ + uint8_t i = 0; + printf("Init MSB default task...\r\n"); /* Infinite loop */ for (;;) { /* Pet watchdog, will reset after 4ish seconds */ HAL_IWDG_Refresh(&hiwdg); + if (i % 2 == 0) { + printf("MSB TYPE: %d\r\n", device_loc); + } else { + printf("..\r\n"); + } + i++; osDelay(500); } /* USER CODE END 5 */ @@ -662,6 +669,7 @@ void Error_Handler(void) __disable_irq(); while (1) { + printf("Error Hanlder HIT!\r\n"); } /* USER CODE END Error_Handler_Debug */ } @@ -677,8 +685,8 @@ void Error_Handler(void) void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ - /* User can add his own implementation to report the file name and line number, - ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ + /* User can add his own implementation to report the file name and line number, */ + printf("Param Assert failed: file %s on line %ld\r\n", file, line); /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ diff --git a/Core/Src/monitor.c b/Core/Src/monitor.c index bd16067..1098260 100644 --- a/Core/Src/monitor.c +++ b/Core/Src/monitor.c @@ -4,7 +4,6 @@ #include "cmsis_os.h" #include "msb.h" #include "msb_conf.h" -#include "serial_monitor.h" #include "stm32f405xx.h" #include @@ -35,7 +34,7 @@ uint16_t convert_can(uint16_t original_value, device_loc_t mode) osThreadId_t temp_monitor_handle; const osThreadAttr_t temp_monitor_attributes = { .name = "TempMonitor", - .stack_size = 32 * 8, + .stack_size = 64 * 16, .priority = (osPriority_t)osPriorityHigh1, }; @@ -56,17 +55,15 @@ void vTempMonitor(void *pv_params) for (;;) { if (central_temp_measure(&temp_dat, &humidity_dat)) { - printf("Failed to get temp"); + printf("Failed to get temp\r\n"); } temp_sensor_data.temp = temp_dat; temp_sensor_data.humidity = humidity_dat; #ifdef LOG_VERBOSE - serial_print("Board Temperature:\t%d\r\n", - temp_sensor_data.temp); - serial_print("Board Humidity:\t%d\r\n", - temp_sensor_data.humidity); + printf("Board Temperature:\t%d\r\n", temp_sensor_data.temp); + printf("Board Humidity:\t%d\r\n", temp_sensor_data.humidity); #endif endian_swap(&temp_sensor_data.temp, @@ -78,7 +75,7 @@ void vTempMonitor(void *pv_params) temp_sensor_msg.len); /* Send CAN message */ if (queue_can_msg(temp_sensor_msg)) { - serial_print("Failed to send CAN message"); + printf("Failed to send CAN message\r\n"); } /* Yield to other tasks */ @@ -91,13 +88,13 @@ void vTempMonitor(void *pv_params) osThreadId_t imu_monitor_handle; const osThreadAttr_t imu_monitor_attributes = { .name = "IMUMonitor", - .stack_size = 32 * 8, + .stack_size = 64 * 8, .priority = (osPriority_t)osPriorityHigh, }; void vIMUMonitor(void *pv_params) { - const uint8_t num_samples = 10; + //const uint8_t num_samples = 10; can_msg_t imu_accel_msg = { .id = convert_can(CANID_IMU_ACCEL, device_loc), .len = 6, @@ -126,34 +123,26 @@ void vIMUMonitor(void *pv_params) /* Take measurement */ if (accel_read(accel_data_temp)) { - serial_print("Failed to get IMU acceleration"); + printf("Failed to get IMU acceleration\r\n"); } if (gyro_read(gyro_data_temp)) { - serial_print("Failed to get IMU gyroscope"); + printf("Failed to get IMU gyroscope\r\n"); } /* Run values through LPF of sample size */ - accel_data.accel_x = - (accel_data.accel_x + accel_data_temp[0]) / num_samples; - accel_data.accel_y = - (accel_data.accel_y + accel_data_temp[1]) / num_samples; - accel_data.accel_z = - (accel_data.accel_z + accel_data_temp[2]) / num_samples; - gyro_data.gyro_x = - (gyro_data.gyro_x + gyro_data_temp[0]) / num_samples; - gyro_data.gyro_y = - (gyro_data.gyro_y + gyro_data_temp[1]) / num_samples; - gyro_data.gyro_z = - (gyro_data.gyro_z + gyro_data_temp[2]) / num_samples; + accel_data.accel_x = (accel_data.accel_x + accel_data_temp[0]); + accel_data.accel_y = (accel_data.accel_y + accel_data_temp[1]); + accel_data.accel_z = (accel_data.accel_z + accel_data_temp[2]); + gyro_data.gyro_x = (gyro_data.gyro_x + gyro_data_temp[0]); + gyro_data.gyro_y = (gyro_data.gyro_y + gyro_data_temp[1]); + gyro_data.gyro_z = (gyro_data.gyro_z + gyro_data_temp[2]); #ifdef LOG_VERBOSE - serial_print("IMU Accel x: %d y: %d z: %d \r\n", - accel_data.accel_x, accel_data.accel_y, - accel_data.accel_z); - serial_print("IMU Gyro x: %d y: %d z: %d \r\n", - gyro_data.gyro_x, gyro_data.gyro_y, - gyro_data.gyro_z); + printf("IMU Accel x: %d y: %d z: %d \r\n", accel_data.accel_x, + accel_data.accel_y, accel_data.accel_z); + printf("IMU Gyro x: %d y: %d z: %d \r\n", gyro_data.gyro_x, + gyro_data.gyro_y, gyro_data.gyro_z); #endif /* convert to big endian */ @@ -167,12 +156,12 @@ void vIMUMonitor(void *pv_params) /* Send CAN message */ memcpy(imu_accel_msg.data, &accel_data, imu_accel_msg.len); if (queue_can_msg(imu_accel_msg)) { - serial_print("Failed to send CAN message"); + printf("Failed to send CAN message\r\n"); } memcpy(imu_gyro_msg.data, &gyro_data, imu_gyro_msg.len); if (queue_can_msg(imu_gyro_msg)) { - serial_print("Failed to send CAN message"); + printf("Failed to send CAN message\r\n"); } /* Yield to other tasks */ @@ -185,7 +174,7 @@ void vIMUMonitor(void *pv_params) osThreadId_t tof_monitor_handle; const osThreadAttr_t tof_monitor_attributes = { .name = "TOFMonitor", - .stack_size = 32 * 8, + .stack_size = 128 * 8, .priority = (osPriority_t)osPriorityHigh, }; @@ -199,12 +188,12 @@ void vTOFMonitor(void *pv_params) for (;;) { if (distance_read(&range)) { - serial_print("failed to read distance!"); + printf("failed to read distance!\r\n"); continue; } #ifdef LOG_VERBOSE - serial_print("Range is: %d", range); + printf("Range is: %ld\r\n", range); #endif endian_swap(&range, sizeof(range)); @@ -212,7 +201,7 @@ void vTOFMonitor(void *pv_params) memcpy(range_msg.data, &range, range_msg.len); /* Send CAN message */ if (queue_can_msg(range_msg)) { - serial_print("Failed to send CAN message"); + printf("Failed to send CAN message\r\n"); } osDelay(DELAY_TOF_REFRESH); @@ -224,7 +213,7 @@ void vTOFMonitor(void *pv_params) osThreadId_t shockpot_monitor_handle; const osThreadAttr_t shockpot_monitor_attributes = { .name = "ShockpotMonitor", - .stack_size = 32 * 8, + .stack_size = 64 * 8, .priority = (osPriority_t)osPriorityHigh1, }; @@ -241,7 +230,7 @@ void vShockpotMonitor(void *pv_params) shockpot_read(shock_value); #ifdef LOG_VERBOSE - serial_print("Shock value:\t%d\r\n", shock_value); + printf("Shock value:\t%ld\r\n", shock_value); #endif endian_swap(&shock_value, sizeof(shock_value)); @@ -249,7 +238,7 @@ void vShockpotMonitor(void *pv_params) memcpy(shockpot_msg.data, &shock_value, shockpot_msg.len); /* Send CAN message */ if (queue_can_msg(shockpot_msg)) { - serial_print("Failed to send CAN message"); + printf("Failed to send CAN message\r\n"); } /* Yield to other tasks */ @@ -262,7 +251,7 @@ void vShockpotMonitor(void *pv_params) osThreadId_t strain_monitor_handle; const osThreadAttr_t strain_monitor_attributes = { .name = "StrainMonitor", - .stack_size = 32 * 8, + .stack_size = 64 * 8, .priority = (osPriority_t)osPriorityHigh1, }; @@ -285,8 +274,7 @@ void vStrainMonitor(void *pv_params) strain2_read(strain2_dat); #ifdef LOG_VERBOSE - serial_print("Strain 1: %d 2: %d \r\n", strain1_dat, - strain2_dat); + printf("Strain 1: %ld 2: %ld \r\n", strain1_dat, strain2_dat); #endif strain_data.strain1 = strain1_dat; @@ -298,7 +286,7 @@ void vStrainMonitor(void *pv_params) memcpy(strain_msg.data, &strain_data, strain_msg.len); /* Send CAN message */ if (queue_can_msg(strain_msg)) { - serial_print("Failed to send CAN message"); + printf("Failed to send CAN message"); } /* Yield to other tasks */ diff --git a/Core/Src/msb.c b/Core/Src/msb.c index 31ff2e6..9c7be3f 100644 --- a/Core/Src/msb.c +++ b/Core/Src/msb.c @@ -2,9 +2,9 @@ #include "main.h" #include "lsm6dso.h" #include -#include #include #include +#include static osMutexAttr_t msb_i2c_mutex_attr; @@ -14,6 +14,22 @@ extern device_loc_t device_loc; osMutexId_t i2c_mutex; +// reads imu reg +static inline int imu_read_reg(uint8_t *data, uint8_t reg, uint8_t length) +{ + return HAL_I2C_Mem_Read(&hi2c3, LSM6DSO_I2C_ADDRESS, reg, + I2C_MEMADD_SIZE_8BIT, data, length, + HAL_MAX_DELAY); +} + +// read imu write +static inline int imu_write_reg(uint8_t *data, uint8_t reg, uint8_t length) +{ + return HAL_I2C_Mem_Write(&hi2c3, LSM6DSO_I2C_ADDRESS, reg, + I2C_MEMADD_SIZE_8BIT, data, length, + HAL_MAX_DELAY); +} + #ifdef SENSOR_TEMP sht30_t temp_sensor; #endif @@ -42,16 +58,19 @@ int8_t msb_init() #ifdef SENSOR_IMU /* Initialize the IMU */ - imu = (lsm6dso_t){ - - }; - assert(!lsm6dso_init(&imu, &hi2c3)); /* This is always connected */ + assert(!lsm6dso_init(&imu, imu_read_reg, + imu_write_reg)); /* This is always connected */ #endif #ifdef SENSOR_TOF /* Initialize the ToF sensor */ - tof = malloc(sizeof(VL6180xDev_t)); + struct MyDev_t tof_get = { + .i2c_bus_num = 0x29 << 1, + .i2c_handle = &hi2c3, + }; + tof = &tof_get; assert(tof); + osDelay(1); assert(!VL6180x_WaitDeviceBooted(tof)); assert(!VL6180x_InitData(tof)); assert(!VL6180x_Prepare(tof)); @@ -162,9 +181,8 @@ int8_t distance_read(int32_t *range_mm) VL6180x_RangePollMeasurement(tof, range); if (range->errorStatus) { - serial_print( - "Error in range %f", - VL6180x_RangeGetStatusErrString(range->errorStatus)); + printf("Error in range %s\r\n", + VL6180x_RangeGetStatusErrString(range->errorStatus)); return range->errorStatus; } diff --git a/Core/Src/serial_monitor.c b/Core/Src/serial_monitor.c deleted file mode 100644 index 554a414..0000000 --- a/Core/Src/serial_monitor.c +++ /dev/null @@ -1,65 +0,0 @@ -#include "serial_monitor.h" -#include -#include -#include -#include - -#define PRINTF_QUEUE_SIZE 25 /* Strings */ -#define PRINTF_BUFFER_LEN 128 /* Characters */ - -osMessageQueueId_t printf_queue; -osThreadId_t serial_monitor_handle; -const osThreadAttr_t serial_monitor_attributes; - -/* - * Referenced https://github.com/esp8266/Arduino/blob/master/cores/esp8266/Print.cpp - * Preformat string then put into a buffer - */ -int serial_print(const char *format, ...) -{ - va_list arg; - char *buffer = malloc(sizeof(char) * PRINTF_BUFFER_LEN); - if (buffer == NULL) - return -1; - - /* Format Variadic Args into string */ - va_start(arg, format); - size_t len = vsnprintf(buffer, PRINTF_BUFFER_LEN, format, arg); - va_end(arg); - - /* Check to make sure we don't overflow buffer */ - if (len > PRINTF_BUFFER_LEN - 1) { - free(buffer); - return -2; - } - - /* If string can't be queued */ - osStatus_t stat = osMessageQueuePut(printf_queue, &buffer, 0U, 0U); - if (stat) { - free(buffer); - return -3; - } - - return 0; -} - -void vSerialMonitor(void *pv_params) -{ - char *message; - osStatus_t status; - - printf_queue = - osMessageQueueNew(PRINTF_QUEUE_SIZE, sizeof(char *), NULL); - - for (;;) { - /* Wait until new printf message comes into queue */ - status = osMessageQueueGet(printf_queue, &message, NULL, - osWaitForever); - if (status != osOK) { - // TODO: Trigger fault ? - } else { - printf(message); - free(message); - } - } -} diff --git a/Core/Src/stm32f4xx_hal_msp.c b/Core/Src/stm32f4xx_hal_msp.c index 682805d..2fd16dd 100644 --- a/Core/Src/stm32f4xx_hal_msp.c +++ b/Core/Src/stm32f4xx_hal_msp.c @@ -195,9 +195,6 @@ void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan) GPIO_InitStruct.Alternate = GPIO_AF9_CAN1; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - /* CAN1 interrupt Init */ - HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 5, 0); - HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn); /* USER CODE BEGIN CAN1_MspInit 1 */ /* USER CODE END CAN1_MspInit 1 */ @@ -227,8 +224,6 @@ void HAL_CAN_MspDeInit(CAN_HandleTypeDef* hcan) */ HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12); - /* CAN1 interrupt DeInit */ - HAL_NVIC_DisableIRQ(CAN1_RX0_IRQn); /* USER CODE BEGIN CAN1_MspDeInit 1 */ /* USER CODE END CAN1_MspDeInit 1 */ diff --git a/Core/Src/stm32f4xx_it.c b/Core/Src/stm32f4xx_it.c index 55a2bd2..25a348d 100644 --- a/Core/Src/stm32f4xx_it.c +++ b/Core/Src/stm32f4xx_it.c @@ -56,7 +56,6 @@ /* External variables --------------------------------------------------------*/ extern DMA_HandleTypeDef hdma_adc1; -extern CAN_HandleTypeDef hcan1; extern TIM_HandleTypeDef htim5; /* USER CODE BEGIN EV */ @@ -87,11 +86,24 @@ void NMI_Handler(void) void HardFault_Handler(void) { /* USER CODE BEGIN HardFault_IRQn 0 */ - /* USER CODE END HardFault_IRQn 0 */ while (1) { /* USER CODE BEGIN W1_HardFault_IRQn 0 */ + // this is an ISR where the MCU doesnt have an intact stack frame or peripherals, so its ok to f around + // no sleep exists in an ISR, for good reason + volatile uint32_t i = 0; + while (i < 120000) { + i += 1; + } + HAL_GPIO_WritePin(Debug_LED_1_GPIO_Port, Debug_LED_1_Pin, 0); + HAL_GPIO_WritePin(Debug_LED_2_GPIO_Port, Debug_LED_2_Pin, 0); + i = 0; + while (i < 120000) { + i += 1; + } + HAL_GPIO_WritePin(Debug_LED_1_GPIO_Port, Debug_LED_1_Pin, 1); + HAL_GPIO_WritePin(Debug_LED_2_GPIO_Port, Debug_LED_2_Pin, 1); /* USER CODE END W1_HardFault_IRQn 0 */ } } @@ -161,20 +173,6 @@ void DebugMon_Handler(void) /* please refer to the startup file (startup_stm32f4xx.s). */ /******************************************************************************/ -/** - * @brief This function handles CAN1 RX0 interrupts. - */ -void CAN1_RX0_IRQHandler(void) -{ - /* USER CODE BEGIN CAN1_RX0_IRQn 0 */ - - /* USER CODE END CAN1_RX0_IRQn 0 */ - HAL_CAN_IRQHandler(&hcan1); - /* USER CODE BEGIN CAN1_RX0_IRQn 1 */ - - /* USER CODE END CAN1_RX0_IRQn 1 */ -} - /** * @brief This function handles TIM5 global interrupt. */ diff --git a/Drivers/Embedded-Base b/Drivers/Embedded-Base index e3359df..20415db 160000 --- a/Drivers/Embedded-Base +++ b/Drivers/Embedded-Base @@ -1 +1 @@ -Subproject commit e3359dff1d54a7abdb0890911828ce2fc4f86513 +Subproject commit 20415dbef0a34e5ffce356a2f68d9d8ad8108e86 diff --git a/MSB-FW.ioc b/MSB-FW.ioc index d06b664..ecc3d94 100644 --- a/MSB-FW.ioc +++ b/MSB-FW.ioc @@ -24,12 +24,12 @@ CAD.provider= CAN1.ABOM=ENABLE CAN1.BS1=CAN_BS1_13TQ CAN1.BS2=CAN_BS2_2TQ -CAN1.CalculateBaudRate=1000000 -CAN1.CalculateTimeBit=1000 -CAN1.CalculateTimeQuantum=62.5 +CAN1.CalculateBaudRate=500000 +CAN1.CalculateTimeBit=2000 +CAN1.CalculateTimeQuantum=125.0 CAN1.IPParameters=CalculateTimeQuantum,CalculateTimeBit,CalculateBaudRate,Prescaler,BS1,BS2,ABOM,NART CAN1.NART=DISABLE -CAN1.Prescaler=1 +CAN1.Prescaler=2 Dma.ADC1.0.Direction=DMA_PERIPH_TO_MEMORY Dma.ADC1.0.FIFOMode=DMA_FIFOMODE_DISABLE Dma.ADC1.0.Instance=DMA2_Stream0 @@ -42,8 +42,11 @@ Dma.ADC1.0.Priority=DMA_PRIORITY_MEDIUM Dma.ADC1.0.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode Dma.Request0=ADC1 Dma.RequestsNb=1 -FREERTOS.IPParameters=Tasks01 +FREERTOS.IPParameters=Tasks01,configUSE_PREEMPTION,configUSE_MALLOC_FAILED_HOOK,configCHECK_FOR_STACK_OVERFLOW FREERTOS.Tasks01=defaultTask,24,128,StartDefaultTask,Default,NULL,Dynamic,NULL,NULL +FREERTOS.configCHECK_FOR_STACK_OVERFLOW=2 +FREERTOS.configUSE_MALLOC_FAILED_HOOK=1 +FREERTOS.configUSE_PREEMPTION=0 File.Version=6 GPIO.groupedBy=Group By Peripherals IWDG.IPParameters=Prescaler @@ -101,7 +104,6 @@ Mcu.UserName=STM32F405RGTx MxCube.Version=6.11.1 MxDb.Version=DB.6.0.111 NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false -NVIC.CAN1_RX0_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true NVIC.DMA2_Stream0_IRQn=true\:5\:0\:false\:false\:true\:true\:false\:true\:true NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false NVIC.ForceEnableDMAVector=true @@ -215,7 +217,7 @@ ProjectManager.FreePins=false ProjectManager.HalAssertFull=false ProjectManager.HeapSize=0x200 ProjectManager.KeepUserCode=true -ProjectManager.LastFirmware=true +ProjectManager.LastFirmware=false ProjectManager.LibraryCopy=1 ProjectManager.MainLocation=Core/Src ProjectManager.NoMain=false diff --git a/Makefile b/Makefile index e8f25e0..7985941 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ ########################################################################################################################## -# File automatically-generated by tool: [projectgenerator] version: [4.3.0-B58] date: [Fri Jun 21 02:22:40 GMT 2024] +# File automatically-generated by tool: [projectgenerator] version: [4.3.0-B58] date: [Fri Oct 18 02:28:33 GMT 2024] ########################################################################################################################## # ------------------------------------------------ @@ -39,7 +39,6 @@ C_SOURCES = \ Core/Src/main.c \ Core/Src/can_handler.c \ Core/Src/controller.c \ -Core/Src/serial_monitor.c \ Core/Src/monitor.c \ Core/Src/msb.c \ Core/Src/stm32f4xx_it.c \ diff --git a/compose.yml b/compose.yml index 14ef1a2..a9bd242 100644 --- a/compose.yml +++ b/compose.yml @@ -4,8 +4,9 @@ services: ner-gcc-arm: image: ghcr.io/northeastern-electric-racing/embedded-base:main build: . - # network_mode: host + network_mode: host privileged: true + volumes: - type: bind source: . diff --git a/flash.cfg b/flash.cfg new file mode 100644 index 0000000..23aacea --- /dev/null +++ b/flash.cfg @@ -0,0 +1,9 @@ +# OpenOCD Configuration Script + +set STM_TARGET_NAME "stm32f4" + +source [find target/${STM_TARGET_NAME}x.cfg] + +# Set the adapter speed +adapter speed 5000 +