From 2435eb94c7f1246e2b2fc12ccc05ec04f6ae8ace Mon Sep 17 00:00:00 2001 From: Jack Rubacha Date: Tue, 18 Jun 2024 20:24:40 -0400 Subject: [PATCH] Auto formatting (#6) * attempt a format check * use correct filepath * use absolute filepath * use different syntax * do check * Revert "use different syntax" This reverts commit a8d8b3ad271fa4f0fb0c4dd4a0297761b38f5238. * fix check path * introducing massive regex * try regex escaped * try to ignore freertos * clang format manually * fix build check * use main * use linux kernel format * add align consecutive macros * attempt ignore * fix * try new ignore * final ignore fix --- .github/workflows/build-check.yml | 4 +- .github/workflows/format-check.yml | 20 ++ Core/.clang-format-ignore | 5 + Core/Inc/msb.h | 47 ++-- Core/Inc/msb_conf.h | 24 +- Core/Src/can_handler.c | 26 +- Core/Src/controller.c | 56 ++-- Core/Src/monitor.c | 409 +++++++++++++++-------------- Core/Src/msb.c | 28 +- Core/Src/serial_monitor.c | 24 +- Drivers/Embedded-Base | 2 +- 11 files changed, 337 insertions(+), 308 deletions(-) create mode 100644 .github/workflows/format-check.yml create mode 100644 Core/.clang-format-ignore diff --git a/.github/workflows/build-check.yml b/.github/workflows/build-check.yml index 4dec4f0..5ee76bb 100644 --- a/.github/workflows/build-check.yml +++ b/.github/workflows/build-check.yml @@ -4,11 +4,11 @@ jobs: run-build: runs-on: ubuntu-latest container: - image: nwdepatie/ner-gcc-arm:latest + image: ghcr.io/northeastern-electric-racing/embedded-base:main timeout-minutes: 10 steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: submodules: recursive diff --git a/.github/workflows/format-check.yml b/.github/workflows/format-check.yml new file mode 100644 index 0000000..acba58d --- /dev/null +++ b/.github/workflows/format-check.yml @@ -0,0 +1,20 @@ +name: Code Style Check + +on: [push] + +jobs: + formatting-check: + name: Check format of C + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + - name: Run clang-format style check for C/C++ sources + uses: Northeastern-Electric-Racing/clang-format-action@main + with: + clang-format-version: '18' + # only check core, embedded base covered internally + check-path: "Core/" + # use the clang-format from embedded base + format-filepath: "./Drivers/Embedded-Base/clang-format" diff --git a/Core/.clang-format-ignore b/Core/.clang-format-ignore new file mode 100644 index 0000000..c00f84f --- /dev/null +++ b/Core/.clang-format-ignore @@ -0,0 +1,5 @@ +./*/stm32* +./*/sys* +./*/FreeRTOS* +./*/freertos* +./*/main* \ No newline at end of file diff --git a/Core/Inc/msb.h b/Core/Inc/msb.h index 76ce537..b72eab5 100644 --- a/Core/Inc/msb.h +++ b/Core/Inc/msb.h @@ -2,39 +2,38 @@ #ifndef MSB_CENTRAL_H #define MSB_CENTRAL_H +#include "cmsis_os.h" #include "lsm6dso.h" #include "sht30.h" #include "stm32f405xx.h" -#include "cmsis_os.h" #include "vl6180x_api.h" -typedef enum -{ - DEVICE_FRONT_LEFT, - DEVICE_FRONT_RIGHT, - DEVICE_BACK_RIGHT, - DEVICE_BACK_LEFT, +typedef enum { + DEVICE_FRONT_LEFT, + DEVICE_FRONT_RIGHT, + DEVICE_BACK_RIGHT, + DEVICE_BACK_LEFT, } device_loc_t; -typedef struct -{ - I2C_HandleTypeDef *hi2c; - sht30_t *temp_sensor; - lsm6dso_t *imu; - VL6180xDev_t tof; - ADC_HandleTypeDef *adc1; - uint32_t adc1_buf[3]; - GPIO_TypeDef *debug_led1_gpio; - uint16_t *debug_led1_pin; - GPIO_TypeDef *debug_led2_gpio; - uint16_t *debug_led2_pin; - device_loc_t *device_loc; - osMutexId_t *i2c_mutex; +typedef struct { + I2C_HandleTypeDef *hi2c; + sht30_t *temp_sensor; + lsm6dso_t *imu; + VL6180xDev_t tof; + ADC_HandleTypeDef *adc1; + uint32_t adc1_buf[3]; + GPIO_TypeDef *debug_led1_gpio; + uint16_t *debug_led1_pin; + GPIO_TypeDef *debug_led2_gpio; + uint16_t *debug_led2_pin; + device_loc_t *device_loc; + osMutexId_t *i2c_mutex; } msb_t; -msb_t *init_msb(I2C_HandleTypeDef *hi2c, ADC_HandleTypeDef *adc1, GPIO_TypeDef *debug_led1_gpio, - uint16_t *debug_led1_pin, GPIO_TypeDef *debug_led2_gpio, uint16_t *debug_led2_pin, - device_loc_t *device_loc); +msb_t *init_msb(I2C_HandleTypeDef *hi2c, ADC_HandleTypeDef *adc1, + GPIO_TypeDef *debug_led1_gpio, uint16_t *debug_led1_pin, + GPIO_TypeDef *debug_led2_gpio, uint16_t *debug_led2_pin, + device_loc_t *device_loc); int8_t measure_central_temp(msb_t *msb, uint16_t *temp, uint16_t *humidity); diff --git a/Core/Inc/msb_conf.h b/Core/Inc/msb_conf.h index d707261..b82a2e3 100644 --- a/Core/Inc/msb_conf.h +++ b/Core/Inc/msb_conf.h @@ -1,21 +1,21 @@ /// All commonly changed settings and constants that could effect multiple factors, esp. in telemetry // DELAYS -#define DELAY_CAN_DISPATCH 5 +#define DELAY_CAN_DISPATCH 5 #define DELAY_TEMP_SENSOR_REFRESH 500 -#define DELAY_IMU_REFRESH 500 -#define DELAY_SHOCKPOT_REFRESH 500 -#define DELAY_STRAIN_REFRESH 500 -#define DELAY_TOF_REFRESH 500 -#define DELAY_WHEEL_TEMP_REFRESH 500 +#define DELAY_IMU_REFRESH 500 +#define DELAY_SHOCKPOT_REFRESH 500 +#define DELAY_STRAIN_REFRESH 500 +#define DELAY_TOF_REFRESH 500 +#define DELAY_WHEEL_TEMP_REFRESH 500 #define DELAY_DEBUG_LED_REFRESH 250 // CAN IDS -#define CANID_TEMP_SENSOR 0x602 -#define CANID_IMU_ACCEL 0x603 -#define CANID_IMU_GYRO 0x604 +#define CANID_TEMP_SENSOR 0x602 +#define CANID_IMU_ACCEL 0x603 +#define CANID_IMU_GYRO 0x604 #define CANID_STRAIN_SENSE 0x605 -#define CANID_SHOCK_SENSE 0x606 -#define CANID_TOF 0x607 -#define CANID_WHEEL_TEMP 0x608 +#define CANID_SHOCK_SENSE 0x606 +#define CANID_TOF 0x607 +#define CANID_WHEEL_TEMP 0x608 diff --git a/Core/Src/can_handler.c b/Core/Src/can_handler.c index 3482a61..45da1ca 100644 --- a/Core/Src/can_handler.c +++ b/Core/Src/can_handler.c @@ -14,9 +14,9 @@ #include "msb_conf.h" #include "serial_monitor.h" +#include "stdio.h" #include #include -#include "stdio.h" #include #define CAN_MSG_QUEUE_SIZE 25 /* messages */ @@ -35,7 +35,8 @@ void init_can1(CAN_HandleTypeDef *hcan) assert(!can_init(can1)); - can_outbound_queue = osMessageQueueNew(CAN_MSG_QUEUE_SIZE, sizeof(can_msg_t), NULL); + can_outbound_queue = + osMessageQueueNew(CAN_MSG_QUEUE_SIZE, sizeof(can_msg_t), NULL); } osThreadId_t can_dispatch_handle; @@ -50,24 +51,21 @@ void vCanDispatch(void *pv_params) can_msg_t msg_from_queue; HAL_StatusTypeDef msg_status; - for (;;) - { + for (;;) { /* Send CAN message */ - if (osOK == osMessageQueueGet(can_outbound_queue, &msg_from_queue, NULL, osWaitForever)) - { + if (osOK == osMessageQueueGet(can_outbound_queue, + &msg_from_queue, NULL, + osWaitForever)) { msg_status = can_send_msg(can1, &msg_from_queue); - if (msg_status == HAL_ERROR) - { + if (msg_status == HAL_ERROR) { serial_print("Failed to send CAN message"); - } - else if (msg_status == HAL_BUSY) - { + } else if (msg_status == HAL_BUSY) { serial_print("Outbound mailbox full!"); } #ifdef LOG_VERBOSE - else - { - printf("Message sent: %lX\r\n", msg_from_queue.id); + else { + printf("Message sent: %lX\r\n", + msg_from_queue.id); } #endif } diff --git a/Core/Src/controller.c b/Core/Src/controller.c index 21c94cf..d9520e9 100644 --- a/Core/Src/controller.c +++ b/Core/Src/controller.c @@ -1,42 +1,38 @@ #include "controller.h" -#include "msb_conf.h" #include "msb.h" +#include "msb_conf.h" osThreadId_t led_controller_handle; const osThreadAttr_t led_controller_attributes = { - .name = "LedController", - .stack_size = 32 * 8, - .priority = (osPriority_t)osPriorityBelowNormal1, + .name = "LedController", + .stack_size = 32 * 8, + .priority = (osPriority_t)osPriorityBelowNormal1, }; void vLedController(void *pv_params) { + msb_t *msb = (msb_t *)pv_params; - msb_t *msb = (msb_t *)pv_params; - - switch (*msb->device_loc) - { - case DEVICE_FRONT_LEFT: - write_debug1(msb, true); - write_debug2(msb, true); - break; - case DEVICE_FRONT_RIGHT: - write_debug1(msb, true); - write_debug2(msb, false); - break; - case DEVICE_BACK_LEFT: - write_debug1(msb, false); - write_debug2(msb, true); - break; - case DEVICE_BACK_RIGHT: - write_debug1(msb, false); - write_debug2(msb, false); - break; - } - - for (;;) - { + switch (*msb->device_loc) { + case DEVICE_FRONT_LEFT: + write_debug1(msb, true); + write_debug2(msb, true); + break; + case DEVICE_FRONT_RIGHT: + write_debug1(msb, true); + write_debug2(msb, false); + break; + case DEVICE_BACK_LEFT: + write_debug1(msb, false); + write_debug2(msb, true); + break; + case DEVICE_BACK_RIGHT: + write_debug1(msb, false); + write_debug2(msb, false); + break; + } - osDelay(DELAY_DEBUG_LED_REFRESH); - } + for (;;) { + osDelay(DELAY_DEBUG_LED_REFRESH); + } } \ No newline at end of file diff --git a/Core/Src/monitor.c b/Core/Src/monitor.c index 9cff2f5..23f47c9 100644 --- a/Core/Src/monitor.c +++ b/Core/Src/monitor.c @@ -1,9 +1,9 @@ -#include "msb_conf.h" -#include "msb.h" #include "c_utils.h" #include "can.h" #include "can_handler.h" #include "cmsis_os.h" +#include "msb.h" +#include "msb_conf.h" #include "serial_monitor.h" #include "stm32f405xx.h" @@ -15,283 +15,292 @@ uint16_t convert_can(uint16_t original_value, device_loc_t *mode) { - switch (*mode) - { - case DEVICE_FRONT_LEFT: - return original_value; - case DEVICE_FRONT_RIGHT: - return original_value + 0x20; - case DEVICE_BACK_LEFT: - return original_value + 0x40; - case DEVICE_BACK_RIGHT: - return original_value + 0x60; - default: - return original_value; - } + switch (*mode) { + case DEVICE_FRONT_LEFT: + return original_value; + case DEVICE_FRONT_RIGHT: + return original_value + 0x20; + case DEVICE_BACK_LEFT: + return original_value + 0x40; + case DEVICE_BACK_RIGHT: + return original_value + 0x60; + default: + return original_value; + } } osThreadId_t temp_monitor_handle; const osThreadAttr_t temp_monitor_attributes = { - .name = "TempMonitor", - .stack_size = 32 * 8, - .priority = (osPriority_t)osPriorityHigh1, + .name = "TempMonitor", + .stack_size = 32 * 8, + .priority = (osPriority_t)osPriorityHigh1, }; void vTempMonitor(void *pv_params) { + msb_t *msb = (msb_t *)pv_params; - msb_t *msb = (msb_t *)pv_params; - - can_msg_t temp_sensor_msg = {.id = convert_can(CANID_TEMP_SENSOR, msb->device_loc), .len = 4, .data = {0}}; + can_msg_t temp_sensor_msg = { .id = convert_can(CANID_TEMP_SENSOR, + msb->device_loc), + .len = 4, + .data = { 0 } }; - struct __attribute__((__packed__)) - { - uint16_t temp; - uint16_t humidity; - } temp_sensor_data; + struct __attribute__((__packed__)) { + uint16_t temp; + uint16_t humidity; + } temp_sensor_data; - uint16_t temp_dat = 0; - uint16_t humidity_dat = 0; + uint16_t temp_dat = 0; + uint16_t humidity_dat = 0; - for (;;) - { + for (;;) { + if (measure_central_temp(msb, &temp_dat, &humidity_dat)) { + printf("Failed to get temp"); + } - if (measure_central_temp(msb, &temp_dat, &humidity_dat)) - { - printf("Failed to get temp"); - } - - temp_sensor_data.temp = temp_dat; - temp_sensor_data.humidity = humidity_dat; + 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); + serial_print("Board Temperature:\t%d\r\n", + temp_sensor_data.temp); + serial_print("Board Humidity:\t%d\r\n", + temp_sensor_data.humidity); #endif - endian_swap(&temp_sensor_data.temp, sizeof(temp_sensor_data.temp)); - endian_swap(&temp_sensor_data.humidity, sizeof(temp_sensor_data.humidity)); - - memcpy(temp_sensor_msg.data, &temp_sensor_data, temp_sensor_msg.len); - /* Send CAN message */ - if (queue_can_msg(temp_sensor_msg)) - { - serial_print("Failed to send CAN message"); - } - - /* Yield to other tasks */ - osDelay(DELAY_TEMP_SENSOR_REFRESH); - } + endian_swap(&temp_sensor_data.temp, + sizeof(temp_sensor_data.temp)); + endian_swap(&temp_sensor_data.humidity, + sizeof(temp_sensor_data.humidity)); + + memcpy(temp_sensor_msg.data, &temp_sensor_data, + temp_sensor_msg.len); + /* Send CAN message */ + if (queue_can_msg(temp_sensor_msg)) { + serial_print("Failed to send CAN message"); + } + + /* Yield to other tasks */ + osDelay(DELAY_TEMP_SENSOR_REFRESH); + } } osThreadId_t imu_monitor_handle; const osThreadAttr_t imu_monitor_attributes = { - .name = "IMUMonitor", - .stack_size = 32 * 8, - .priority = (osPriority_t)osPriorityHigh, + .name = "IMUMonitor", + .stack_size = 32 * 8, + .priority = (osPriority_t)osPriorityHigh, }; void vIMUMonitor(void *pv_params) { - msb_t *msb = (msb_t *)pv_params; - - const uint8_t num_samples = 10; - can_msg_t imu_accel_msg = {.id = convert_can(CANID_IMU_ACCEL, msb->device_loc), .len = 6, .data = {0}}; - can_msg_t imu_gyro_msg = {.id = convert_can(CANID_IMU_GYRO, msb->device_loc), .len = 6, .data = {0}}; - - struct __attribute__((__packed__)) - { - uint16_t accel_x; - uint16_t accel_y; - uint16_t accel_z; - } accel_data; - - struct __attribute__((__packed__)) - { - uint16_t gyro_x; - uint16_t gyro_y; - uint16_t gyro_z; - } gyro_data; - - uint16_t accel_data_temp[3] = {0}; - uint16_t gyro_data_temp[3] = {0}; - - for (;;) - { - /* Take measurement */ - - if (read_accel(msb, accel_data_temp)) - { - serial_print("Failed to get IMU acceleration"); - } - - if (read_gyro(msb, gyro_data_temp)) - { - serial_print("Failed to get IMU gyroscope"); - } - - /* 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; + msb_t *msb = (msb_t *)pv_params; + + const uint8_t num_samples = 10; + can_msg_t imu_accel_msg = { .id = convert_can(CANID_IMU_ACCEL, + msb->device_loc), + .len = 6, + .data = { 0 } }; + can_msg_t imu_gyro_msg = { .id = convert_can(CANID_IMU_GYRO, + msb->device_loc), + .len = 6, + .data = { 0 } }; + + struct __attribute__((__packed__)) { + uint16_t accel_x; + uint16_t accel_y; + uint16_t accel_z; + } accel_data; + + struct __attribute__((__packed__)) { + uint16_t gyro_x; + uint16_t gyro_y; + uint16_t gyro_z; + } gyro_data; + + uint16_t accel_data_temp[3] = { 0 }; + uint16_t gyro_data_temp[3] = { 0 }; + + for (;;) { + /* Take measurement */ + + if (read_accel(msb, accel_data_temp)) { + serial_print("Failed to get IMU acceleration"); + } + + if (read_gyro(msb, gyro_data_temp)) { + serial_print("Failed to get IMU gyroscope"); + } + + /* 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; #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); + 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); #endif - /* convert to big endian */ - endian_swap(&accel_data.accel_x, sizeof(accel_data.accel_x)); - endian_swap(&accel_data.accel_y, sizeof(accel_data.accel_y)); - endian_swap(&accel_data.accel_z, sizeof(accel_data.accel_z)); - endian_swap(&gyro_data.gyro_x, sizeof(gyro_data.gyro_x)); - endian_swap(&gyro_data.gyro_y, sizeof(gyro_data.gyro_y)); - endian_swap(&gyro_data.gyro_z, sizeof(gyro_data.gyro_z)); - - /* 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"); - } - - 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"); - } - - /* Yield to other tasks */ - osDelay(DELAY_IMU_REFRESH); - } + /* convert to big endian */ + endian_swap(&accel_data.accel_x, sizeof(accel_data.accel_x)); + endian_swap(&accel_data.accel_y, sizeof(accel_data.accel_y)); + endian_swap(&accel_data.accel_z, sizeof(accel_data.accel_z)); + endian_swap(&gyro_data.gyro_x, sizeof(gyro_data.gyro_x)); + endian_swap(&gyro_data.gyro_y, sizeof(gyro_data.gyro_y)); + endian_swap(&gyro_data.gyro_z, sizeof(gyro_data.gyro_z)); + + /* 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"); + } + + 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"); + } + + /* Yield to other tasks */ + osDelay(DELAY_IMU_REFRESH); + } } osThreadId_t tof_monitor_handle; const osThreadAttr_t tof_monitor_attributes = { - .name = "TOFMonitor", - .stack_size = 32 * 8, - .priority = (osPriority_t)osPriorityHigh, + .name = "TOFMonitor", + .stack_size = 32 * 8, + .priority = (osPriority_t)osPriorityHigh, }; void vTOFMonitor(void *pv_params) { - msb_t *msb = (msb_t *)pv_params; + msb_t *msb = (msb_t *)pv_params; - can_msg_t range_msg = {.id = convert_can(CANID_TOF, msb->device_loc), .len = 4, .data = {0}}; + can_msg_t range_msg = { .id = convert_can(CANID_TOF, msb->device_loc), + .len = 4, + .data = { 0 } }; - int32_t range; + int32_t range; - for (;;) - { - if (read_distance(msb, &range)) - { - serial_print("failed to read distance!"); - continue; - } + for (;;) { + if (read_distance(msb, &range)) { + serial_print("failed to read distance!"); + continue; + } #ifdef LOG_VERBOSE - serial_print("Range is: %d", range); + serial_print("Range is: %d", range); #endif - endian_swap(&range, sizeof(range)); + endian_swap(&range, sizeof(range)); - memcpy(range_msg.data, &range, range_msg.len); - /* Send CAN message */ - if (queue_can_msg(range_msg)) - { - serial_print("Failed to send CAN message"); - } + memcpy(range_msg.data, &range, range_msg.len); + /* Send CAN message */ + if (queue_can_msg(range_msg)) { + serial_print("Failed to send CAN message"); + } - osDelay(DELAY_TOF_REFRESH); - } + osDelay(DELAY_TOF_REFRESH); + } } osThreadId_t shockpot_monitor_handle; const osThreadAttr_t shockpot_monitor_attributes = { - .name = "ShockpotMonitor", - .stack_size = 32 * 8, - .priority = (osPriority_t)osPriorityHigh1, + .name = "ShockpotMonitor", + .stack_size = 32 * 8, + .priority = (osPriority_t)osPriorityHigh1, }; void vShockpotMonitor(void *pv_params) { + msb_t *msb = (msb_t *)pv_params; - msb_t *msb = (msb_t *)pv_params; + can_msg_t shockpot_msg = { .id = convert_can(CANID_SHOCK_SENSE, + msb->device_loc), + .len = 4, + .data = { 0 } }; - can_msg_t shockpot_msg = {.id = convert_can(CANID_SHOCK_SENSE, msb->device_loc), .len = 4, .data = {0}}; + uint32_t shock_value = 0; - uint32_t shock_value = 0; - - for (;;) - { - read_shockpot(msb, shock_value); + for (;;) { + read_shockpot(msb, shock_value); #ifdef LOG_VERBOSE - serial_print("Shock value:\t%d\r\n", shock_value); + serial_print("Shock value:\t%d\r\n", shock_value); #endif - endian_swap(&shock_value, sizeof(shock_value)); + endian_swap(&shock_value, sizeof(shock_value)); - 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"); - } + 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"); + } - /* Yield to other tasks */ - osDelay(DELAY_SHOCKPOT_REFRESH); - } + /* Yield to other tasks */ + osDelay(DELAY_SHOCKPOT_REFRESH); + } } osThreadId_t strain_monitor_handle; const osThreadAttr_t strain_monitor_attributes = { - .name = "StrainMonitor", - .stack_size = 32 * 8, - .priority = (osPriority_t)osPriorityHigh1, + .name = "StrainMonitor", + .stack_size = 32 * 8, + .priority = (osPriority_t)osPriorityHigh1, }; void vStrainMonitor(void *pv_params) { + msb_t *msb = (msb_t *)pv_params; - msb_t *msb = (msb_t *)pv_params; - - can_msg_t strain_msg = {.id = convert_can(CANID_STRAIN_SENSE, msb->device_loc), .len = 8, .data = {0}}; + can_msg_t strain_msg = { .id = convert_can(CANID_STRAIN_SENSE, + msb->device_loc), + .len = 8, + .data = { 0 } }; - struct __attribute__((__packed__)) - { - uint32_t strain1; - uint32_t strain2; - } strain_data; + struct __attribute__((__packed__)) { + uint32_t strain1; + uint32_t strain2; + } strain_data; - uint32_t strain1_dat = 0; - uint32_t strain2_dat = 0; - for (;;) - { - read_strain1(msb, strain1_dat); - read_strain2(msb, strain2_dat); + uint32_t strain1_dat = 0; + uint32_t strain2_dat = 0; + for (;;) { + read_strain1(msb, strain1_dat); + read_strain2(msb, strain2_dat); #ifdef LOG_VERBOSE - serial_print("Strain 1: %d 2: %d \r\n", strain1_dat, strain2_dat); + serial_print("Strain 1: %d 2: %d \r\n", strain1_dat, + strain2_dat); #endif - strain_data.strain1 = strain1_dat; - strain_data.strain2 = strain2_dat; + strain_data.strain1 = strain1_dat; + strain_data.strain2 = strain2_dat; - endian_swap(&strain_data.strain1, sizeof(strain_data.strain1)); - endian_swap(&strain_data.strain2, sizeof(strain_data.strain2)); + endian_swap(&strain_data.strain1, sizeof(strain_data.strain1)); + endian_swap(&strain_data.strain2, sizeof(strain_data.strain2)); - 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"); - } + 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"); + } - /* Yield to other tasks */ - osDelay(DELAY_SHOCKPOT_REFRESH); - } + /* Yield to other tasks */ + osDelay(DELAY_SHOCKPOT_REFRESH); + } } \ No newline at end of file diff --git a/Core/Src/msb.c b/Core/Src/msb.c index c09efe7..0e24f52 100644 --- a/Core/Src/msb.c +++ b/Core/Src/msb.c @@ -1,15 +1,16 @@ #include "msb.h" +#include "lsm6dso.h" #include #include -#include #include -#include "lsm6dso.h" +#include static osMutexAttr_t msb_i2c_mutex_attr; -msb_t *init_msb(I2C_HandleTypeDef *hi2c, ADC_HandleTypeDef *adc1, GPIO_TypeDef *debug_led1_gpio, - uint16_t *debug_led1_pin, GPIO_TypeDef *debug_led2_gpio, uint16_t *debug_led2_pin, - device_loc_t *device_loc) +msb_t *init_msb(I2C_HandleTypeDef *hi2c, ADC_HandleTypeDef *adc1, + GPIO_TypeDef *debug_led1_gpio, uint16_t *debug_led1_pin, + GPIO_TypeDef *debug_led2_gpio, uint16_t *debug_led2_pin, + device_loc_t *device_loc) { assert(hi2c); assert(adc1); @@ -37,7 +38,8 @@ msb_t *init_msb(I2C_HandleTypeDef *hi2c, ADC_HandleTypeDef *adc1, GPIO_TypeDef * /* Initialize the IMU */ msb->imu = malloc(sizeof(lsm6dso_t)); assert(msb->imu); - assert(!lsm6dso_init(msb->imu, msb->hi2c)); /* This is always connected */ + assert(!lsm6dso_init(msb->imu, + msb->hi2c)); /* This is always connected */ /* Initialize the ToF sensor */ msb->tof = malloc(sizeof(VL6180xDev_t)); @@ -46,7 +48,8 @@ msb_t *init_msb(I2C_HandleTypeDef *hi2c, ADC_HandleTypeDef *adc1, GPIO_TypeDef * assert(!VL6180x_InitData(msb->tof)); assert(!VL6180x_Prepare(msb->tof)); - assert(!HAL_ADC_Start_DMA(msb->adc1, msb->adc1_buf, sizeof(msb->adc1_buf) / sizeof(uint32_t))); + assert(!HAL_ADC_Start_DMA(msb->adc1, msb->adc1_buf, + sizeof(msb->adc1_buf) / sizeof(uint32_t))); /* Create Mutexes */ msb->i2c_mutex = osMutexNew(&msb_i2c_mutex_attr); @@ -86,7 +89,8 @@ void read_adc1(msb_t *msb, uint32_t adc1_buf[3]) void read_shockpot(msb_t *msb, uint32_t shockpot_sense) { - memcpy((uint32_t *)shockpot_sense, msb->adc1_buf, sizeof(shockpot_sense)); + memcpy((uint32_t *)shockpot_sense, msb->adc1_buf, + sizeof(shockpot_sense)); } void read_strain1(msb_t *msb, uint32_t strain1) @@ -138,7 +142,8 @@ int8_t read_gyro(msb_t *msb, uint16_t gyro[3]) } VL6180x_RangeData_t *range; -int8_t read_distance(msb_t *msb, int32_t *range_mm) { +int8_t read_distance(msb_t *msb, int32_t *range_mm) +{ if (!msb) return -1; @@ -148,12 +153,13 @@ int8_t read_distance(msb_t *msb, int32_t *range_mm) { VL6180x_RangePollMeasurement(msb->tof, range); if (range->errorStatus) { - serial_print("Error in range %f", VL6180x_RangeGetStatusErrString(range->errorStatus)); + serial_print( + "Error in range %f", + VL6180x_RangeGetStatusErrString(range->errorStatus)); return range->errorStatus; } memcpy(range_mm, &range->range_mm, sizeof(range->range_mm)); - osMutexRelease(msb->i2c_mutex); return 0; diff --git a/Core/Src/serial_monitor.c b/Core/Src/serial_monitor.c index a0e2f15..554a414 100644 --- a/Core/Src/serial_monitor.c +++ b/Core/Src/serial_monitor.c @@ -4,7 +4,7 @@ #include #include -#define PRINTF_QUEUE_SIZE 25 /* Strings */ +#define PRINTF_QUEUE_SIZE 25 /* Strings */ #define PRINTF_BUFFER_LEN 128 /* Characters */ osMessageQueueId_t printf_queue; @@ -28,16 +28,14 @@ int serial_print(const char *format, ...) va_end(arg); /* Check to make sure we don't overflow buffer */ - if (len > PRINTF_BUFFER_LEN - 1) - { + 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) - { + if (stat) { free(buffer); return -3; } @@ -50,18 +48,16 @@ void vSerialMonitor(void *pv_params) char *message; osStatus_t status; - printf_queue = osMessageQueueNew(PRINTF_QUEUE_SIZE, sizeof(char *), NULL); + printf_queue = + osMessageQueueNew(PRINTF_QUEUE_SIZE, sizeof(char *), NULL); - for (;;) - { + for (;;) { /* Wait until new printf message comes into queue */ - status = osMessageQueueGet(printf_queue, &message, NULL, osWaitForever); - if (status != osOK) - { + status = osMessageQueueGet(printf_queue, &message, NULL, + osWaitForever); + if (status != osOK) { // TODO: Trigger fault ? - } - else - { + } else { printf(message); free(message); } diff --git a/Drivers/Embedded-Base b/Drivers/Embedded-Base index db7634a..e3359df 160000 --- a/Drivers/Embedded-Base +++ b/Drivers/Embedded-Base @@ -1 +1 @@ -Subproject commit db7634a8f0995d75a76441782beac906d617e366 +Subproject commit e3359dff1d54a7abdb0890911828ce2fc4f86513