From 00e6462895adbe28e4e6f08d26429bee1a2c60fe Mon Sep 17 00:00:00 2001 From: Jack Rubacha Date: Fri, 21 Jun 2024 16:49:32 -0400 Subject: [PATCH] Add IFDEFs and extern all structs (#7) * ifdef and extern * format * change naming syntax --- Core/Inc/can_handler.h | 4 +- Core/Inc/monitor.h | 11 ++ Core/Inc/msb.h | 50 +++++---- Core/Inc/msb_conf.h | 21 +++- Core/Src/can_handler.c | 8 +- Core/Src/controller.c | 22 ++-- Core/Src/main.c | 225 ++++++++++++++++++++++++----------------- Core/Src/monitor.c | 54 +++++----- Core/Src/msb.c | 190 +++++++++++++++++----------------- MSB-FW.ioc | 67 ++++++------ Makefile | 2 +- 11 files changed, 370 insertions(+), 284 deletions(-) diff --git a/Core/Inc/can_handler.h b/Core/Inc/can_handler.h index 6990b20..7d97fe4 100644 --- a/Core/Inc/can_handler.h +++ b/Core/Inc/can_handler.h @@ -17,13 +17,13 @@ #include "can.h" #include "cmsis_os.h" -void can1_callback(CAN_HandleTypeDef *hcan); +//void can1_callback(CAN_HandleTypeDef *hcan); void vCanDispatch(void *pv_params); extern osThreadId_t can_dispatch_handle; extern const osThreadAttr_t can_dispatch_attributes; int8_t queue_can_msg(can_msg_t msg); -void init_can1(CAN_HandleTypeDef *hcan); +void can1_init(); #endif diff --git a/Core/Inc/monitor.h b/Core/Inc/monitor.h index bff50aa..21283ef 100644 --- a/Core/Inc/monitor.h +++ b/Core/Inc/monitor.h @@ -3,27 +3,38 @@ #include "cmsis_os.h" #include "stm32f4xx_hal.h" +#include "msb_conf.h" +#ifdef SENSOR_TEMP /* Defining Temperature Monitor Task */ void vTempMonitor(void *pv_params); extern osThreadId_t temp_monitor_handle; extern const osThreadAttr_t temp_monitor_attributes; +#endif +#ifdef SENSOR_IMU /* Task for Monitoring the IMU */ void vIMUMonitor(void *pv_params); extern osThreadId_t imu_monitor_handle; extern const osThreadAttr_t imu_monitor_attributes; +#endif +#ifdef SENSOR_TOF void vTOFMonitor(void *pv_params); extern osThreadId_t tof_monitor_handle; extern const osThreadAttr_t tof_monitor_attributes; +#endif +#ifdef SENSOR_SHOCKPOT void vShockpotMonitor(void *pv_params); extern osThreadId_t shockpot_monitor_handle; extern const osThreadAttr_t shockpot_monitor_attributes; +#endif +#ifdef SENSOR_STRAIN void vStrainMonitor(void *pv_params); extern osThreadId_t strain_monitor_handle; extern const osThreadAttr_t strain_monitor_attributes; +#endif #endif \ No newline at end of file diff --git a/Core/Inc/msb.h b/Core/Inc/msb.h index b72eab5..8ed335e 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 "msb_conf.h" typedef enum { DEVICE_FRONT_LEFT, @@ -15,40 +16,35 @@ typedef enum { 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; -} msb_t; +int8_t msb_init(); -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); +#ifdef SENSOR_TEMP +int8_t central_temp_measure(uint16_t *temp, uint16_t *humidity); +#endif + +#ifdef SENSOR_IMU +int8_t accel_read(uint16_t accel[3]); -int8_t measure_central_temp(msb_t *msb, uint16_t *temp, uint16_t *humidity); +int8_t gyro_read(uint16_t gyro[3]); +#endif -int8_t read_accel(msb_t *msb, uint16_t accel[3]); +#ifdef SENSOR_TOF +int8_t distance_read(int32_t *range_mm); +#endif -int8_t read_gyro(msb_t *msb, uint16_t gyro[3]); +int8_t debug1_write(bool status); -int8_t read_distance(msb_t *msb, int32_t *range_mm); +int8_t debug2_write(bool status); -int8_t write_debug1(msb_t *msb, bool status); +int8_t vcc5_en_write(bool status); -int8_t write_debug2(msb_t *msb, bool status); +#ifdef SENSOR_SHOCKPOT +void shockpot_read(uint32_t shockpot_sense); +#endif -void read_shockpot(msb_t *msb, uint32_t shockpot_sense); -void read_strain1(msb_t *msb, uint32_t strain1); -void read_strain2(msb_t *msb, uint32_t strain2); +#ifdef SENSOR_STRAIN +void strain1_read(uint32_t strain1); +void strain2_read(uint32_t strain2); +#endif #endif diff --git a/Core/Inc/msb_conf.h b/Core/Inc/msb_conf.h index b82a2e3..8f5cb9b 100644 --- a/Core/Inc/msb_conf.h +++ b/Core/Inc/msb_conf.h @@ -1,7 +1,8 @@ /// All commonly changed settings and constants that could effect multiple factors, esp. in telemetry // DELAYS -#define DELAY_CAN_DISPATCH 5 + +// MONITOR #define DELAY_TEMP_SENSOR_REFRESH 500 #define DELAY_IMU_REFRESH 500 #define DELAY_SHOCKPOT_REFRESH 500 @@ -9,8 +10,11 @@ #define DELAY_TOF_REFRESH 500 #define DELAY_WHEEL_TEMP_REFRESH 500 +// CONTROLLER #define DELAY_DEBUG_LED_REFRESH 250 +#define DELAY_CAN_DISPATCH 5 + // CAN IDS #define CANID_TEMP_SENSOR 0x602 #define CANID_IMU_ACCEL 0x603 @@ -19,3 +23,18 @@ #define CANID_SHOCK_SENSE 0x606 #define CANID_TOF 0x607 #define CANID_WHEEL_TEMP 0x608 + +// Sensors to use, comment out to disable + +//on central +#define SENSOR_TEMP +#define SENSOR_SHOCKPOT +#define SENSOR_STRAIN +#define SENSOR_TOF + +// on knuckle or wheel +#define SENSOR_IMU +//#define SENSOR_WHEEL_TEMP + +// VERBOSE LOGGING +#define LOG_VERBOSE diff --git a/Core/Src/can_handler.c b/Core/Src/can_handler.c index 45da1ca..9c5680b 100644 --- a/Core/Src/can_handler.c +++ b/Core/Src/can_handler.c @@ -22,16 +22,16 @@ #define CAN_MSG_QUEUE_SIZE 25 /* messages */ static osMessageQueueId_t can_outbound_queue; +extern CAN_HandleTypeDef hcan1; + can_t *can1; -void init_can1(CAN_HandleTypeDef *hcan) +void can1_init() { - assert(hcan); - can1 = malloc(sizeof(can_t)); assert(can1); - can1->hcan = hcan; + can1->hcan = &hcan1; assert(!can_init(can1)); diff --git a/Core/Src/controller.c b/Core/Src/controller.c index d9520e9..6029ffa 100644 --- a/Core/Src/controller.c +++ b/Core/Src/controller.c @@ -2,6 +2,8 @@ #include "msb.h" #include "msb_conf.h" +extern device_loc_t device_loc; + osThreadId_t led_controller_handle; const osThreadAttr_t led_controller_attributes = { .name = "LedController", @@ -11,24 +13,22 @@ const osThreadAttr_t led_controller_attributes = { void vLedController(void *pv_params) { - msb_t *msb = (msb_t *)pv_params; - - switch (*msb->device_loc) { + switch (device_loc) { case DEVICE_FRONT_LEFT: - write_debug1(msb, true); - write_debug2(msb, true); + debug1_write(true); + debug2_write(true); break; case DEVICE_FRONT_RIGHT: - write_debug1(msb, true); - write_debug2(msb, false); + debug1_write(true); + debug2_write(false); break; case DEVICE_BACK_LEFT: - write_debug1(msb, false); - write_debug2(msb, true); + debug1_write(false); + debug2_write(true); break; case DEVICE_BACK_RIGHT: - write_debug1(msb, false); - write_debug2(msb, false); + debug1_write(false); + debug2_write(false); break; } diff --git a/Core/Src/main.c b/Core/Src/main.c index 0f8a8b5..2fbf4f3 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -27,6 +27,7 @@ #include "serial_monitor.h" #include "monitor.h" #include "msb.h" +#include "msb_conf.h" #include "assert.h" /* USER CODE END Includes */ @@ -62,11 +63,12 @@ UART_HandleTypeDef huart2; /* Definitions for defaultTask */ osThreadId_t defaultTaskHandle; const osThreadAttr_t defaultTask_attributes = { - .name = "defaultTask", - .stack_size = 128 * 4, - .priority = (osPriority_t)osPriorityNormal, + .name = "defaultTask", + .stack_size = 128 * 4, + .priority = (osPriority_t) osPriorityNormal, }; /* USER CODE BEGIN PV */ +device_loc_t device_loc; /* USER CODE END PV */ @@ -88,13 +90,33 @@ void StartDefaultTask(void *argument); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ +#ifdef __GNUC__ +#define PUTCHAR_PROTOTYPE int __io_putchar(int ch) +#else +#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) +#endif + +PUTCHAR_PROTOTYPE +{ + HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY); + return ch; +} + +int _write(int file, char* ptr, int len) { + int DataIdx; + + for (DataIdx = 0; DataIdx < len; DataIdx++) { + __io_putchar( *ptr++ ); + } + return len; +} /* USER CODE END 0 */ /** - * @brief The application entry point. - * @retval int - */ + * @brief The application entry point. + * @retval int + */ int main(void) { @@ -133,26 +155,25 @@ int main(void) bool loc2 = HAL_GPIO_ReadPin(Addr1_GPIO_Port, Addr1_Pin); bool loc3 = HAL_GPIO_ReadPin(Addr2_GPIO_Port, Addr2_Pin); - device_loc_t loc; if (loc1 && loc2) { - loc = DEVICE_FRONT_LEFT; + device_loc = DEVICE_FRONT_LEFT; } else if (loc1 && !loc2) { - loc = DEVICE_FRONT_RIGHT; + device_loc = DEVICE_FRONT_RIGHT; } else if (!loc1 && loc2) { - loc = DEVICE_BACK_LEFT; + device_loc = DEVICE_BACK_LEFT; } else if (!loc1 && !loc3) { - loc = DEVICE_BACK_RIGHT; + device_loc = DEVICE_BACK_RIGHT; } else { - loc = DEVICE_FRONT_LEFT; + device_loc = DEVICE_FRONT_LEFT; } /* USER CODE END 2 */ @@ -165,7 +186,8 @@ int main(void) // determine the configuration of the device - msb_t *msb = init_msb(&hi2c3, &hadc1, Debug_LED_1_GPIO_Port, (uint16_t *)Debug_LED_1_Pin, Debug_LED_2_GPIO_Port, (uint16_t *)Debug_LED_2_Pin, &loc); + msb_init(); + can1_init(); /* USER CODE END RTOS_MUTEX */ @@ -191,18 +213,32 @@ int main(void) /* USER CODE BEGIN RTOS_THREADS */ /* add threads, ... */ - temp_monitor_handle = osThreadNew(vTempMonitor, msb, &temp_monitor_attributes); + #ifdef SENSOR_TEMP + temp_monitor_handle = osThreadNew(vTempMonitor, NULL, &temp_monitor_attributes); assert(temp_monitor_handle); - imu_monitor_handle = osThreadNew(vIMUMonitor, msb, &imu_monitor_attributes); + #endif + + #ifdef SENSOR_IMU + imu_monitor_handle = osThreadNew(vIMUMonitor, NULL, &imu_monitor_attributes); assert(imu_monitor_handle); - tof_monitor_handle = osThreadNew(vIMUMonitor, msb, &tof_monitor_attributes); + #endif + + #ifdef SENSOR_TOF + tof_monitor_handle = osThreadNew(vIMUMonitor, NULL, &tof_monitor_attributes); assert(tof_monitor_handle); - shockpot_monitor_handle = osThreadNew(vIMUMonitor, msb, &shockpot_monitor_attributes); + #endif + + #ifdef SENSOR_SHOCKPOT + shockpot_monitor_handle = osThreadNew(vIMUMonitor, NULL, &shockpot_monitor_attributes); assert(shockpot_monitor_handle); - strain_monitor_handle = osThreadNew(vIMUMonitor, msb, &strain_monitor_attributes); + #endif + + #ifdef SENSOR_STRAIN + strain_monitor_handle = osThreadNew(vIMUMonitor, NULL, &strain_monitor_attributes); assert(strain_monitor_handle); + #endif - led_controller_handle = osThreadNew(vLedController, msb, &led_controller_attributes); + led_controller_handle = osThreadNew(vLedController, NULL, &led_controller_attributes); assert(led_controller_handle); /* USER CODE END RTOS_THREADS */ @@ -228,23 +264,23 @@ int main(void) } /** - * @brief System Clock Configuration - * @retval None - */ + * @brief System Clock Configuration + * @retval None + */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure the main internal regulator output voltage - */ + */ __HAL_RCC_PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /** Initializes the RCC Oscillators according to the specified parameters - * in the RCC_OscInitTypeDef structure. - */ - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSI; + * in the RCC_OscInitTypeDef structure. + */ + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.LSIState = RCC_LSI_ON; @@ -255,8 +291,9 @@ void SystemClock_Config(void) } /** Initializes the CPU, AHB and APB buses clocks - */ - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; + */ + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK + |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; @@ -269,10 +306,10 @@ void SystemClock_Config(void) } /** - * @brief ADC1 Initialization Function - * @param None - * @retval None - */ + * @brief ADC1 Initialization Function + * @param None + * @retval None + */ static void MX_ADC1_Init(void) { @@ -287,7 +324,7 @@ static void MX_ADC1_Init(void) /* USER CODE END ADC1_Init 1 */ /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) - */ + */ hadc1.Instance = ADC1; hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV6; hadc1.Init.Resolution = ADC_RESOLUTION_12B; @@ -306,7 +343,7 @@ static void MX_ADC1_Init(void) } /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. - */ + */ sConfig.Channel = ADC_CHANNEL_5; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; @@ -316,7 +353,7 @@ static void MX_ADC1_Init(void) } /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. - */ + */ sConfig.Rank = 2; if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { @@ -324,7 +361,7 @@ static void MX_ADC1_Init(void) } /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. - */ + */ sConfig.Rank = 3; if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { @@ -332,7 +369,7 @@ static void MX_ADC1_Init(void) } /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. - */ + */ sConfig.Channel = ADC_CHANNEL_0; sConfig.Rank = 4; if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) @@ -342,13 +379,14 @@ static void MX_ADC1_Init(void) /* USER CODE BEGIN ADC1_Init 2 */ /* USER CODE END ADC1_Init 2 */ + } /** - * @brief CAN1 Initialization Function - * @param None - * @retval None - */ + * @brief CAN1 Initialization Function + * @param None + * @retval None + */ static void MX_CAN1_Init(void) { @@ -376,16 +414,17 @@ static void MX_CAN1_Init(void) Error_Handler(); } /* USER CODE BEGIN CAN1_Init 2 */ - init_can1(&hcan1); + can1_init(&hcan1); /* USER CODE END CAN1_Init 2 */ + } /** - * @brief I2C3 Initialization Function - * @param None - * @retval None - */ + * @brief I2C3 Initialization Function + * @param None + * @retval None + */ static void MX_I2C3_Init(void) { @@ -412,13 +451,14 @@ static void MX_I2C3_Init(void) /* USER CODE BEGIN I2C3_Init 2 */ /* USER CODE END I2C3_Init 2 */ + } /** - * @brief IWDG Initialization Function - * @param None - * @retval None - */ + * @brief IWDG Initialization Function + * @param None + * @retval None + */ static void MX_IWDG_Init(void) { @@ -439,13 +479,14 @@ static void MX_IWDG_Init(void) /* USER CODE BEGIN IWDG_Init 2 */ /* USER CODE END IWDG_Init 2 */ + } /** - * @brief SPI2 Initialization Function - * @param None - * @retval None - */ + * @brief SPI2 Initialization Function + * @param None + * @retval None + */ static void MX_SPI2_Init(void) { @@ -476,13 +517,14 @@ static void MX_SPI2_Init(void) /* USER CODE BEGIN SPI2_Init 2 */ /* USER CODE END SPI2_Init 2 */ + } /** - * @brief USART2 Initialization Function - * @param None - * @retval None - */ + * @brief USART2 Initialization Function + * @param None + * @retval None + */ static void MX_USART2_UART_Init(void) { @@ -508,11 +550,12 @@ static void MX_USART2_UART_Init(void) /* USER CODE BEGIN USART2_Init 2 */ /* USER CODE END USART2_Init 2 */ + } /** - * Enable DMA controller clock - */ + * Enable DMA controller clock + */ static void MX_DMA_Init(void) { @@ -523,43 +566,44 @@ static void MX_DMA_Init(void) /* DMA2_Stream0_IRQn interrupt configuration */ HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 5, 0); HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn); + } /** - * @brief GPIO Initialization Function - * @param None - * @retval None - */ + * @brief GPIO Initialization Function + * @param None + * @retval None + */ static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; - /* USER CODE BEGIN MX_GPIO_Init_1 */ - /* USER CODE END MX_GPIO_Init_1 */ +/* USER CODE BEGIN MX_GPIO_Init_1 */ +/* USER CODE END MX_GPIO_Init_1 */ /* GPIO Ports Clock Enable */ - __HAL_RCC_GPIOH_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOH_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(GPIOC, VCC5_En_Pin | Debug_LED_1_Pin | Debug_LED_2_Pin, GPIO_PIN_RESET); + HAL_GPIO_WritePin(GPIOC, VCC5_En_Pin|Debug_LED_1_Pin|Debug_LED_2_Pin, GPIO_PIN_RESET); /*Configure GPIO pins : VCC5_En_Pin Debug_LED_1_Pin Debug_LED_2_Pin */ - GPIO_InitStruct.Pin = VCC5_En_Pin | Debug_LED_1_Pin | Debug_LED_2_Pin; + GPIO_InitStruct.Pin = VCC5_En_Pin|Debug_LED_1_Pin|Debug_LED_2_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); /*Configure GPIO pins : Addr0_Pin Addr1_Pin Addr2_Pin */ - GPIO_InitStruct.Pin = Addr0_Pin | Addr1_Pin | Addr2_Pin; + GPIO_InitStruct.Pin = Addr0_Pin|Addr1_Pin|Addr2_Pin; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); - /* USER CODE BEGIN MX_GPIO_Init_2 */ - /* USER CODE END MX_GPIO_Init_2 */ +/* USER CODE BEGIN MX_GPIO_Init_2 */ +/* USER CODE END MX_GPIO_Init_2 */ } /* USER CODE BEGIN 4 */ @@ -587,20 +631,19 @@ void StartDefaultTask(void *argument) } /** - * @brief Period elapsed callback in non blocking mode - * @note This function is called when TIM5 interrupt took place, inside - * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment - * a global variable "uwTick" used as application time base. - * @param htim : TIM handle - * @retval None - */ + * @brief Period elapsed callback in non blocking mode + * @note This function is called when TIM5 interrupt took place, inside + * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment + * a global variable "uwTick" used as application time base. + * @param htim : TIM handle + * @retval None + */ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { /* USER CODE BEGIN Callback 0 */ /* USER CODE END Callback 0 */ - if (htim->Instance == TIM5) - { + if (htim->Instance == TIM5) { HAL_IncTick(); } /* USER CODE BEGIN Callback 1 */ @@ -609,9 +652,9 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) } /** - * @brief This function is executed in case of error occurrence. - * @retval None - */ + * @brief This function is executed in case of error occurrence. + * @retval None + */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ @@ -623,14 +666,14 @@ void Error_Handler(void) /* USER CODE END Error_Handler_Debug */ } -#ifdef USE_FULL_ASSERT +#ifdef USE_FULL_ASSERT /** - * @brief Reports the name of the source file and the source line number - * where the assert_param error has occurred. - * @param file: pointer to the source file name - * @param line: assert_param error line source number - * @retval None - */ + * @brief Reports the name of the source file and the source line number + * where the assert_param error has occurred. + * @param file: pointer to the source file name + * @param line: assert_param error line source number + * @retval None + */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ diff --git a/Core/Src/monitor.c b/Core/Src/monitor.c index 23f47c9..bd16067 100644 --- a/Core/Src/monitor.c +++ b/Core/Src/monitor.c @@ -13,9 +13,11 @@ #include "monitor.h" -uint16_t convert_can(uint16_t original_value, device_loc_t *mode) +extern device_loc_t device_loc; + +uint16_t convert_can(uint16_t original_value, device_loc_t mode) { - switch (*mode) { + switch (mode) { case DEVICE_FRONT_LEFT: return original_value; case DEVICE_FRONT_RIGHT: @@ -29,6 +31,7 @@ uint16_t convert_can(uint16_t original_value, device_loc_t *mode) } } +#ifdef SENSOR_TEMP osThreadId_t temp_monitor_handle; const osThreadAttr_t temp_monitor_attributes = { .name = "TempMonitor", @@ -38,10 +41,8 @@ const osThreadAttr_t temp_monitor_attributes = { void vTempMonitor(void *pv_params) { - msb_t *msb = (msb_t *)pv_params; - can_msg_t temp_sensor_msg = { .id = convert_can(CANID_TEMP_SENSOR, - msb->device_loc), + device_loc), .len = 4, .data = { 0 } }; @@ -54,7 +55,7 @@ void vTempMonitor(void *pv_params) uint16_t humidity_dat = 0; for (;;) { - if (measure_central_temp(msb, &temp_dat, &humidity_dat)) { + if (central_temp_measure(&temp_dat, &humidity_dat)) { printf("Failed to get temp"); } @@ -84,7 +85,9 @@ void vTempMonitor(void *pv_params) osDelay(DELAY_TEMP_SENSOR_REFRESH); } } +#endif +#ifdef SENSOR_IMU osThreadId_t imu_monitor_handle; const osThreadAttr_t imu_monitor_attributes = { .name = "IMUMonitor", @@ -94,15 +97,13 @@ const osThreadAttr_t imu_monitor_attributes = { 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), + device_loc), .len = 6, .data = { 0 } }; can_msg_t imu_gyro_msg = { .id = convert_can(CANID_IMU_GYRO, - msb->device_loc), + device_loc), .len = 6, .data = { 0 } }; @@ -124,11 +125,11 @@ void vIMUMonitor(void *pv_params) for (;;) { /* Take measurement */ - if (read_accel(msb, accel_data_temp)) { + if (accel_read(accel_data_temp)) { serial_print("Failed to get IMU acceleration"); } - if (read_gyro(msb, gyro_data_temp)) { + if (gyro_read(gyro_data_temp)) { serial_print("Failed to get IMU gyroscope"); } @@ -178,7 +179,9 @@ void vIMUMonitor(void *pv_params) osDelay(DELAY_IMU_REFRESH); } } +#endif +#ifdef SENSOR_TOF osThreadId_t tof_monitor_handle; const osThreadAttr_t tof_monitor_attributes = { .name = "TOFMonitor", @@ -188,16 +191,14 @@ const osThreadAttr_t tof_monitor_attributes = { void vTOFMonitor(void *pv_params) { - msb_t *msb = (msb_t *)pv_params; - - can_msg_t range_msg = { .id = convert_can(CANID_TOF, msb->device_loc), + can_msg_t range_msg = { .id = convert_can(CANID_TOF, device_loc), .len = 4, .data = { 0 } }; int32_t range; for (;;) { - if (read_distance(msb, &range)) { + if (distance_read(&range)) { serial_print("failed to read distance!"); continue; } @@ -217,7 +218,9 @@ void vTOFMonitor(void *pv_params) osDelay(DELAY_TOF_REFRESH); } } +#endif +#ifdef SENSOR_SHOCKPOT osThreadId_t shockpot_monitor_handle; const osThreadAttr_t shockpot_monitor_attributes = { .name = "ShockpotMonitor", @@ -227,17 +230,15 @@ const osThreadAttr_t shockpot_monitor_attributes = { void vShockpotMonitor(void *pv_params) { - msb_t *msb = (msb_t *)pv_params; - can_msg_t shockpot_msg = { .id = convert_can(CANID_SHOCK_SENSE, - msb->device_loc), + device_loc), .len = 4, .data = { 0 } }; uint32_t shock_value = 0; for (;;) { - read_shockpot(msb, shock_value); + shockpot_read(shock_value); #ifdef LOG_VERBOSE serial_print("Shock value:\t%d\r\n", shock_value); @@ -255,7 +256,9 @@ void vShockpotMonitor(void *pv_params) osDelay(DELAY_SHOCKPOT_REFRESH); } } +#endif +#ifdef SENSOR_STRAIN osThreadId_t strain_monitor_handle; const osThreadAttr_t strain_monitor_attributes = { .name = "StrainMonitor", @@ -265,10 +268,8 @@ const osThreadAttr_t strain_monitor_attributes = { void vStrainMonitor(void *pv_params) { - msb_t *msb = (msb_t *)pv_params; - can_msg_t strain_msg = { .id = convert_can(CANID_STRAIN_SENSE, - msb->device_loc), + device_loc), .len = 8, .data = { 0 } }; @@ -280,8 +281,8 @@ void vStrainMonitor(void *pv_params) uint32_t strain1_dat = 0; uint32_t strain2_dat = 0; for (;;) { - read_strain1(msb, strain1_dat); - read_strain2(msb, strain2_dat); + strain1_read(strain1_dat); + strain2_read(strain2_dat); #ifdef LOG_VERBOSE serial_print("Strain 1: %d 2: %d \r\n", strain1_dat, @@ -303,4 +304,5 @@ void vStrainMonitor(void *pv_params) /* Yield to other tasks */ osDelay(DELAY_SHOCKPOT_REFRESH); } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/Core/Src/msb.c b/Core/Src/msb.c index 0e24f52..31ff2e6 100644 --- a/Core/Src/msb.c +++ b/Core/Src/msb.c @@ -1,4 +1,5 @@ #include "msb.h" +#include "main.h" #include "lsm6dso.h" #include #include @@ -7,151 +8,159 @@ 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) -{ - assert(hi2c); - assert(adc1); - assert(debug_led1_gpio); - assert(debug_led2_gpio); - assert(device_loc); - - msb_t *msb = malloc(sizeof(msb_t)); - assert(msb); - - msb->hi2c = hi2c; - msb->adc1 = adc1; - msb->debug_led1_gpio = debug_led1_gpio; - msb->debug_led1_pin = debug_led1_pin; - msb->debug_led2_gpio = debug_led2_gpio; - msb->debug_led2_pin = debug_led2_pin; - msb->device_loc = device_loc; +extern I2C_HandleTypeDef hi2c3; +extern ADC_HandleTypeDef hadc1; +extern device_loc_t device_loc; + +osMutexId_t i2c_mutex; + +#ifdef SENSOR_TEMP +sht30_t temp_sensor; +#endif + +#ifdef SENSOR_IMU +lsm6dso_t imu; +#endif + +#ifdef SENSOR_TOF +VL6180xDev_t tof; +#endif +#if defined SENSOR_STRAIN || defined SENSOR_SHOCKPOT +uint32_t adc1_buf[3]; +#endif + +int8_t msb_init() +{ +#ifdef SENSOR_TEMP /* Initialize the Onboard Temperature Sensor */ - msb->temp_sensor = malloc(sizeof(sht30_t)); - assert(msb->temp_sensor); - msb->temp_sensor->i2c_handle = hi2c; - assert(!sht30_init(msb->temp_sensor)); /* This is always connected */ + temp_sensor = (sht30_t){ + .i2c_handle = &hi2c3, + }; + assert(!sht30_init(&temp_sensor)); /* This is always connected */ +#endif +#ifdef SENSOR_IMU /* Initialize the IMU */ - msb->imu = malloc(sizeof(lsm6dso_t)); - assert(msb->imu); - assert(!lsm6dso_init(msb->imu, - msb->hi2c)); /* This is always connected */ + imu = (lsm6dso_t){ - /* Initialize the ToF sensor */ - msb->tof = malloc(sizeof(VL6180xDev_t)); - assert(msb->tof); - assert(!VL6180x_WaitDeviceBooted(msb->tof)); - assert(!VL6180x_InitData(msb->tof)); - assert(!VL6180x_Prepare(msb->tof)); + }; + assert(!lsm6dso_init(&imu, &hi2c3)); /* This is always connected */ +#endif - assert(!HAL_ADC_Start_DMA(msb->adc1, msb->adc1_buf, - sizeof(msb->adc1_buf) / sizeof(uint32_t))); +#ifdef SENSOR_TOF + /* Initialize the ToF sensor */ + tof = malloc(sizeof(VL6180xDev_t)); + assert(tof); + assert(!VL6180x_WaitDeviceBooted(tof)); + assert(!VL6180x_InitData(tof)); + assert(!VL6180x_Prepare(tof)); +#endif + +#if defined SENSOR_SHOCKPOT || defined SENSOR_STRAIN + assert(!HAL_ADC_Start_DMA(&hadc1, adc1_buf, + sizeof(adc1_buf) / sizeof(uint32_t))); +#endif /* Create Mutexes */ - msb->i2c_mutex = osMutexNew(&msb_i2c_mutex_attr); - assert(msb->i2c_mutex); + i2c_mutex = osMutexNew(&msb_i2c_mutex_attr); + assert(i2c_mutex); - return msb; + return 0; } +#ifdef SENSOR_TEMP /// @brief Measure the temperature and humidity of central MSB SHT30 /// @param out /// @return error code -int8_t measure_central_temp(msb_t *msb, uint16_t *temp, uint16_t *humidity) +int8_t central_temp_measure(uint16_t *temp, uint16_t *humidity) { - if (!msb) - return -1; - - osStatus_t mut_stat = osMutexAcquire(msb->i2c_mutex, osWaitForever); + osStatus_t mut_stat = osMutexAcquire(i2c_mutex, osWaitForever); if (mut_stat) return mut_stat; - HAL_StatusTypeDef hal_stat = sht30_get_temp_humid(msb->temp_sensor); + HAL_StatusTypeDef hal_stat = sht30_get_temp_humid(&temp_sensor); if (hal_stat) return hal_stat; - *temp = msb->temp_sensor->temp; - *humidity = msb->temp_sensor->humidity; + *temp = temp_sensor.temp; + *humidity = temp_sensor.humidity; - osMutexRelease(msb->i2c_mutex); + osMutexRelease(i2c_mutex); return 0; } +#endif -void read_adc1(msb_t *msb, uint32_t adc1_buf[3]) +#if defined SENSOR_SHOCKPOT || defined SENSOR_STRAIN +void adc1_read(uint32_t result_buf[3]) { - memcpy(adc1_buf, msb->adc1_buf, sizeof(msb->adc1_buf)); + memcpy(result_buf, adc1_buf, sizeof(adc1_buf)); } +#endif -void read_shockpot(msb_t *msb, uint32_t shockpot_sense) +#ifdef SENSOR_SHOCKPOT +void shockpot_read(uint32_t shockpot_sense) { - memcpy((uint32_t *)shockpot_sense, msb->adc1_buf, - sizeof(shockpot_sense)); + memcpy((uint32_t *)shockpot_sense, adc1_buf, sizeof(shockpot_sense)); } +#endif -void read_strain1(msb_t *msb, uint32_t strain1) +#ifdef SENSOR_STRAIN +void strain1_read(uint32_t strain1) { - memcpy((uint32_t *)strain1, msb->adc1_buf + 1, sizeof(strain1)); + memcpy((uint32_t *)strain1, adc1_buf + 1, sizeof(strain1)); } -void read_strain2(msb_t *msb, uint32_t strain2) +void strain2_read(uint32_t strain2) { - memcpy((uint32_t *)strain2, msb->adc1_buf + 2, sizeof(strain2)); + memcpy((uint32_t *)strain2, adc1_buf + 2, sizeof(strain2)); } +#endif -int8_t read_accel(msb_t *msb, uint16_t accel[3]) +#ifdef SENSOR_IMU +int8_t accel_read(uint16_t accel[3]) { - if (!msb) - return -1; - - osStatus_t mut_stat = osMutexAcquire(msb->i2c_mutex, osWaitForever); + osStatus_t mut_stat = osMutexAcquire(i2c_mutex, osWaitForever); if (mut_stat) return mut_stat; - HAL_StatusTypeDef hal_stat = lsm6dso_read_accel(msb->imu); + HAL_StatusTypeDef hal_stat = lsm6dso_read_accel(&imu); if (hal_stat) return hal_stat; - memcpy(accel, msb->imu->accel_data, 3); + memcpy(accel, imu.accel_data, 3); - osMutexRelease(msb->i2c_mutex); + osMutexRelease(i2c_mutex); return 0; } -int8_t read_gyro(msb_t *msb, uint16_t gyro[3]) +int8_t gyro_read(uint16_t gyro[3]) { - if (!msb) - return -1; - - osStatus_t mut_stat = osMutexAcquire(msb->i2c_mutex, osWaitForever); + osStatus_t mut_stat = osMutexAcquire(i2c_mutex, osWaitForever); if (mut_stat) return mut_stat; - HAL_StatusTypeDef hal_stat = lsm6dso_read_gyro(msb->imu); + HAL_StatusTypeDef hal_stat = lsm6dso_read_gyro(&imu); if (hal_stat) return hal_stat; - memcpy(gyro, msb->imu->gyro_data, 3); + memcpy(gyro, imu.gyro_data, 3); - osMutexRelease(msb->i2c_mutex); + osMutexRelease(i2c_mutex); return 0; } +#endif +#ifdef SENSOR_TOF VL6180x_RangeData_t *range; -int8_t read_distance(msb_t *msb, int32_t *range_mm) +int8_t distance_read(int32_t *range_mm) { - if (!msb) - return -1; - - osStatus_t mut_stat = osMutexAcquire(msb->i2c_mutex, osWaitForever); + osStatus_t mut_stat = osMutexAcquire(i2c_mutex, osWaitForever); if (mut_stat) return mut_stat; - VL6180x_RangePollMeasurement(msb->tof, range); + VL6180x_RangePollMeasurement(tof, range); if (range->errorStatus) { serial_print( "Error in range %f", @@ -161,24 +170,25 @@ int8_t read_distance(msb_t *msb, int32_t *range_mm) memcpy(range_mm, &range->range_mm, sizeof(range->range_mm)); - osMutexRelease(msb->i2c_mutex); + osMutexRelease(i2c_mutex); return 0; } +#endif -int8_t write_debug1(msb_t *msb, bool status) +int8_t debug1_write(bool status) { - if (!msb) - return -1; - - HAL_GPIO_WritePin(msb->debug_led1_gpio, *msb->debug_led1_pin, status); + HAL_GPIO_WritePin(Debug_LED_1_GPIO_Port, Debug_LED_1_Pin, status); return 0; } -int8_t write_debug2(msb_t *msb, bool status) +int8_t debug2_write(bool status) { - if (!msb) - return -1; + HAL_GPIO_WritePin(Debug_LED_2_GPIO_Port, Debug_LED_2_Pin, status); + return 0; +} - HAL_GPIO_WritePin(msb->debug_led2_gpio, *msb->debug_led2_pin, status); +int8_t vcc5_en_write(bool status) +{ + HAL_GPIO_WritePin(VCC5_En_GPIO_Port, VCC5_En_Pin, status); return 0; } \ No newline at end of file diff --git a/MSB-FW.ioc b/MSB-FW.ioc index 8e93673..d06b664 100644 --- a/MSB-FW.ioc +++ b/MSB-FW.ioc @@ -65,34 +65,36 @@ Mcu.IP9=SYS Mcu.IPNb=11 Mcu.Name=STM32F405RGTx Mcu.Package=LQFP64 -Mcu.Pin0=PH0-OSC_IN -Mcu.Pin1=PH1-OSC_OUT -Mcu.Pin10=PB12 -Mcu.Pin11=PB13 -Mcu.Pin12=PB14 -Mcu.Pin13=PB15 -Mcu.Pin14=PC9 -Mcu.Pin15=PA8 -Mcu.Pin16=PA11 -Mcu.Pin17=PA12 -Mcu.Pin18=PA13 -Mcu.Pin19=PA14 -Mcu.Pin2=PC2 -Mcu.Pin20=PC10 -Mcu.Pin21=PC11 -Mcu.Pin22=PC12 -Mcu.Pin23=PB3 -Mcu.Pin24=VP_FREERTOS_VS_CMSIS_V2 -Mcu.Pin25=VP_IWDG_VS_IWDG -Mcu.Pin26=VP_SYS_VS_tim5 -Mcu.Pin3=PA0-WKUP -Mcu.Pin4=PA2 -Mcu.Pin5=PA3 -Mcu.Pin6=PA5 -Mcu.Pin7=PA6 -Mcu.Pin8=PC4 -Mcu.Pin9=PC5 -Mcu.PinsNb=27 +Mcu.Pin0=PC14-OSC32_IN +Mcu.Pin1=PC15-OSC32_OUT +Mcu.Pin10=PC5 +Mcu.Pin11=PB12 +Mcu.Pin12=PB13 +Mcu.Pin13=PB14 +Mcu.Pin14=PB15 +Mcu.Pin15=PC9 +Mcu.Pin16=PA8 +Mcu.Pin17=PA11 +Mcu.Pin18=PA12 +Mcu.Pin19=PA13 +Mcu.Pin2=PH0-OSC_IN +Mcu.Pin20=PA14 +Mcu.Pin21=PA15 +Mcu.Pin22=PC10 +Mcu.Pin23=PC11 +Mcu.Pin24=PC12 +Mcu.Pin25=PB3 +Mcu.Pin26=VP_FREERTOS_VS_CMSIS_V2 +Mcu.Pin27=VP_IWDG_VS_IWDG +Mcu.Pin28=VP_SYS_VS_tim5 +Mcu.Pin3=PC2 +Mcu.Pin4=PA0-WKUP +Mcu.Pin5=PA2 +Mcu.Pin6=PA3 +Mcu.Pin7=PA5 +Mcu.Pin8=PA6 +Mcu.Pin9=PC4 +Mcu.PinsNb=29 Mcu.ThirdPartyNb=0 Mcu.UserConstants= Mcu.UserName=STM32F405RGTx @@ -132,6 +134,8 @@ PA13.Signal=SYS_JTMS-SWDIO PA14.Locked=true PA14.Mode=Trace_Asynchronous_SW PA14.Signal=SYS_JTCK-SWCLK +PA15.Locked=true +PA15.Signal=SYS_JTDI PA2.Locked=true PA2.Mode=Asynchronous PA2.Signal=USART2_TX @@ -175,6 +179,10 @@ PC12.GPIOParameters=GPIO_Label PC12.GPIO_Label=Addr2 PC12.Locked=true PC12.Signal=GPIO_Input +PC14-OSC32_IN.Locked=true +PC14-OSC32_IN.Signal=RCC_OSC32_IN +PC15-OSC32_OUT.Locked=true +PC15-OSC32_OUT.Signal=RCC_OSC32_OUT PC2.GPIOParameters=GPIO_Label PC2.GPIO_Label=VCC5_En PC2.Locked=true @@ -191,10 +199,7 @@ PC9.Locked=true PC9.Mode=I2C PC9.Signal=I2C3_SDA PH0-OSC_IN.Locked=true -PH0-OSC_IN.Mode=HSE-External-Oscillator PH0-OSC_IN.Signal=RCC_OSC_IN -PH1-OSC_OUT.Mode=HSE-External-Oscillator -PH1-OSC_OUT.Signal=RCC_OSC_OUT PinOutPanel.RotationAngle=0 ProjectManager.AskForMigrate=true ProjectManager.BackupPrevious=false diff --git a/Makefile b/Makefile index 13f3aa2..5093eb2 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ ########################################################################################################################## -# File automatically-generated by tool: [projectgenerator] version: [4.3.0-B58] date: [Fri May 24 08:00:53 GMT 2024] +# File automatically-generated by tool: [projectgenerator] version: [4.3.0-B58] date: [Fri Jun 21 02:22:40 GMT 2024] ########################################################################################################################## # ------------------------------------------------