Skip to content

Commit

Permalink
Adding Basic MPU Interface with LED and ADC functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
nwdepatie committed Dec 30, 2023
1 parent 9702ab0 commit 5f69db4
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 65 deletions.
4 changes: 0 additions & 4 deletions Core/Inc/cerberus_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@
#define GPIO_4_GPIO_Port GPIOB
#define WATCHDOG_Pin GPIO_PIN_15
#define WATCHDOG_GPIO_Port GPIOB
#define LED_1_Pin GPIO_PIN_8
#define LED_1_GPIO_Port GPIOC
#define LED_2_Pin GPIO_PIN_9
#define LED_2_GPIO_Port GPIOC

#define CANID_TEMP_SENSOR 0xBEEF
#define CANID_PEDAL_SENSOR 0xDEAD
Expand Down
7 changes: 0 additions & 7 deletions Core/Inc/monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ void vWatchdogMonitor(void *pv_params);
extern osThreadId_t watchdog_monitor_handle;
extern const osThreadAttr_t watchdog_monitor_attributes;

typedef struct
{
ADC_HandleTypeDef *accel_adc1;
ADC_HandleTypeDef *accel_adc2;
ADC_HandleTypeDef *brake_adc;
} pedal_params_t;

/* Parameters for the pedal monitoring task */
#define MAX_ADC_VAL_12B 4096
#define PEDAL_DIFF_THRESH 10
Expand Down
40 changes: 40 additions & 0 deletions Core/Inc/mpu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef MPU_H
#define MPU_H

#include <stdint.h>
#include <stdbool.h>
#include "stm32f405xx.h"
#include "sht30.h"
#include "lsm6dso.h"
#include "cmsis_os.h"

typedef struct {
I2C_HandleTypeDef *hi2c;
ADC_HandleTypeDef *accel_adc1;
ADC_HandleTypeDef *accel_adc2;
ADC_HandleTypeDef *brake_adc;
GPIO_TypeDef* led_gpio;
GPIO_TypeDef* watchdog_gpio;
sht30_t *temp_sensor;
lsm6dso_t *imu;
osMutexId_t *adc_mutex;
osMutexId_t *i2c_mutex;
/* Not including LED Mutexes because not necessary */
} mpu_t;

mpu_t *init_mpu(I2C_HandleTypeDef *hi2c, ADC_HandleTypeDef *accel_adc1,
ADC_HandleTypeDef *accel_adc2, ADC_HandleTypeDef *brake_adc,
GPIO_TypeDef* led_gpio, GPIO_TypeDef* watchdog_gpio);

int8_t write_rled(mpu_t *mpu, bool status);

int8_t toggle_rled(mpu_t *mpu);

int8_t write_yled(mpu_t *mpu, bool status);

int8_t toggle_yled(mpu_t *mpu);

/* Note: this should be called from within a thread since it yields to scheduler */
int8_t read_adc(mpu_t *mpu, uint16_t raw[3]);

#endif /* MPU */
32 changes: 14 additions & 18 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "state_machine.h"
#include "torque.h"
#include "pdu.h"
#include "mpu.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
Expand Down Expand Up @@ -161,13 +162,17 @@ int main(void)
MX_ADC3_Init();
/* USER CODE BEGIN 2 */

HAL_GPIO_TogglePin(GPIOC, LED_2_Pin); // Toggle on LED2
/* Create Interfaces to Represent Relevant Hardware */
mpu_t *mpu = init_mpu(&hi2c1, &hadc1, &hadc2, &hadc3, GPIOC, GPIOB);
pdu_t *pdu = init_pdu(&hi2c2);

toggle_yled(mpu); // Toggle on LED2
HAL_Delay(500);
HAL_GPIO_TogglePin(GPIOC, LED_2_Pin); // Toggle on LED2
toggle_yled(mpu); // Toggle on LED2
HAL_Delay(500);
HAL_GPIO_TogglePin(GPIOC, LED_2_Pin); // Toggle on LED2
toggle_yled(mpu); // Toggle on LED2
HAL_Delay(500);
HAL_GPIO_TogglePin(GPIOC, LED_2_Pin); // Toggle on LED2
toggle_yled(mpu); // Toggle on LED2

/* USER CODE END 2 */

Expand All @@ -189,15 +194,6 @@ int main(void)
/* USER CODE BEGIN RTOS_QUEUES */
onboard_temp_queue = osMessageQueueNew(ONBOARD_TEMP_QUEUE_SIZE, sizeof(onboard_temp_t), NULL);
imu_queue = osMessageQueueNew(IMU_QUEUE_SIZE, sizeof(imu_data_t), NULL);

// A bit sloppy since I don't free this, but it will be allocated for lifetime
pedal_params_t *pedal_params = malloc(sizeof(pedal_params_t));
pedal_params->accel_adc1 = &hadc1;
pedal_params->accel_adc2 = &hadc2;
pedal_params->brake_adc = &hadc3;

pdu_t *pdu = init_pdu(&hi2c1);

pedal_data_queue = osMessageQueueNew(PEDAL_DATA_QUEUE_SIZE, sizeof(pedals_t), NULL);
/* USER CODE END RTOS_QUEUES */

Expand All @@ -207,10 +203,10 @@ int main(void)

/* USER CODE BEGIN RTOS_THREADS */
/* Monitors */
temp_monitor_handle = osThreadNew(vTempMonitor, &hi2c1, &temp_monitor_attributes);
temp_monitor_handle = osThreadNew(vTempMonitor, mpu, &temp_monitor_attributes);
watchdog_monitor_handle = osThreadNew(vWatchdogMonitor, GPIOB, &watchdog_monitor_attributes);
imu_monitor_handle = osThreadNew(vIMUMonitor, &hi2c1, &imu_monitor_attributes);
pedals_monitor_handle = osThreadNew(vPedalsMonitor, pedal_params, &pedals_monitor_attributes);
imu_monitor_handle = osThreadNew(vIMUMonitor, mpu, &imu_monitor_attributes);
pedals_monitor_handle = osThreadNew(vPedalsMonitor, mpu, &pedals_monitor_attributes);
fusing_monitor_handle = osThreadNew(vFusingMonitor, pdu, &fusing_monitor_attributes);
shutdown_monitor_handle = osThreadNew(vShutdownMonitor, pdu, &shutdown_monitor_attributes);

Expand Down Expand Up @@ -766,9 +762,9 @@ void StartDefaultTask(void *argument)
//HAL_StatusTypeDef err = HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
//if (!err)
// serial_print("%d\r\n", HAL_ADC_GetValue(&hadc1));

/* Toggle LED at certain frequency */
HAL_GPIO_TogglePin(GPIOC, LED_1_Pin); // Toggle on LED2
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_8); // I am not using MPU interface because I'm lazy
i++;

if (i % 2 == 1)
Expand Down
45 changes: 9 additions & 36 deletions Core/Src/monitor.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <string.h>
#include <stdbool.h>
#include "monitor.h"
#include "can.h"
#include "can_handler.h"
#include "cerberus_conf.h"
#include "fault.h"
#include "queues.h"
Expand All @@ -12,6 +12,7 @@
#include "serial_monitor.h"
#include "timer.h"
#include "pdu.h"
#include "mpu.h"

osThreadId_t temp_monitor_handle;
const osThreadAttr_t temp_monitor_attributes = {
Expand All @@ -27,18 +28,10 @@ void vTempMonitor(void* pv_params)
const uint8_t can_msg_len = 4; /* bytes */
static onboard_temp_t sensor_data;
fault_data_t fault_data = { .id = ONBOARD_TEMP_FAULT, .severity = DEFCON4 };
sht30_t temp_sensor;
I2C_HandleTypeDef* hi2c1;
can_msg_t temp_msg
= { .id = CANID_TEMP_SENSOR, .len = can_msg_len, .data = { 0 } };

hi2c1 = (I2C_HandleTypeDef*)pv_params;
temp_sensor.i2c_handle = hi2c1;

//if (sht30_init(&temp_sensor)) {
// fault_data.diag = "Temp Monitor Init Failed";
// queue_fault(&fault_data);
//}
mpu_t *mpu = (mpu_t *)pv_params;

for (;;) {
/* Take measurement */
Expand Down Expand Up @@ -115,36 +108,16 @@ void vPedalsMonitor(void* pv_params)
= { .id = CANID_PEDAL_SENSOR, .len = can_msg_len, .data = { 0 } };

/* Handle ADC Data for two input accelerator value and two input brake value*/
pedal_params_t* params = (pedal_params_t*)pv_params;
mpu_t *mpu = (mpu_t *)pv_params;

uint16_t adc_data[4];

uint32_t curr_tick = HAL_GetTick();
uint16_t adc_data[3];

for (;;) {
//serial_print("Pedals Task: %d\r\n", HAL_GetTick() - curr_tick);
/*
* Get the value from the adc at the brake and accelerator
* pin addresses and average them to the sensor data value
*/
HAL_ADC_Start(params->accel_adc1);
HAL_ADC_Start(params->accel_adc2);
HAL_ADC_Start(params->brake_adc);

/* Yield to other tasks */
osDelay(delay_time);

HAL_StatusTypeDef err = HAL_ADC_PollForConversion(params->accel_adc1, HAL_MAX_DELAY);
if (!err)
HAL_ADC_GetValue(params->accel_adc1);

err = HAL_ADC_PollForConversion(params->accel_adc2, HAL_MAX_DELAY);
if (!err)
HAL_ADC_GetValue(params->accel_adc2);

err = HAL_ADC_PollForConversion(params->brake_adc, HAL_MAX_DELAY);
if (!err)
HAL_ADC_GetValue(params->brake_adc);
if (read_adc(mpu, adc_data)) {
fault_data.diag = "Failed to collect ADC Data!";
queue_fault(&fault_data);
}

//err = HAL_ADC_PollForConversion(params->brake_adc, HAL_MAX_DELAY);
//if (!err)
Expand Down
156 changes: 156 additions & 0 deletions Core/Src/mpu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#include "mpu.h"
#include <stdlib.h>
#include <assert.h>
#include "stm32f405xx.h"

#define YLED_PIN GPIO_PIN_8
#define RLED_PIN GPIO_PIN_9
#define ADC_TIMEOUT 2 /* ms */

static osMutexAttr_t mpu_i2c_mutex_attr;
static osMutexAttr_t mpu_adc_mutex_attr;

mpu_t *init_mpu(I2C_HandleTypeDef *hi2c, ADC_HandleTypeDef *accel_adc1,
ADC_HandleTypeDef *accel_adc2, ADC_HandleTypeDef *brake_adc,
GPIO_TypeDef* led_gpio, GPIO_TypeDef* watchdog_gpio)
{
assert(hi2c);
assert(accel_adc1);
assert(accel_adc2);
assert(brake_adc);
assert(led_gpio);
assert(watchdog_gpio);

/* Create MPU struct */
mpu_t *mpu = malloc(sizeof(mpu_t));
assert(mpu);

mpu->hi2c = hi2c;
mpu->accel_adc1 = accel_adc1;
mpu->accel_adc2 = accel_adc2;
mpu->brake_adc = brake_adc;
mpu->led_gpio = led_gpio;
mpu->watchdog_gpio = watchdog_gpio;

/* Initialize the Onboard Temperature Sensor */
mpu->temp_sensor = malloc(sizeof(sht30_t));
assert(mpu->temp_sensor);
mpu->temp_sensor->i2c_handle = hi2c;
assert(!sht30_init(mpu->temp_sensor)); /* This is always connected */

/* Initialize the IMU */
mpu->imu = malloc(sizeof(lsm6dso_t));
assert(mpu->imu);
assert(!lsm6dso_init(mpu->imu, mpu->hi2c)); /* This is always connected */

/* Create Mutexes */
mpu->i2c_mutex = osMutexNew(&mpu_i2c_mutex_attr);
assert(mpu->i2c_mutex);

mpu->adc_mutex = osMutexNew(&mpu_adc_mutex_attr);
assert(mpu->adc_mutex);

return 0;
}

int8_t write_rled(mpu_t *mpu, bool status)
{
if (!mpu)
return -1;

HAL_GPIO_WritePin(mpu->led_gpio, RLED_PIN, status);
return 0;
}

int8_t toggle_rled(mpu_t *mpu)
{
if (!mpu)
return -1;

HAL_GPIO_TogglePin(mpu->led_gpio, RLED_PIN);
return 0;
}

int8_t write_yled(mpu_t *mpu, bool status)
{
if (!mpu)
return -1;

HAL_GPIO_WritePin(mpu->led_gpio, YLED_PIN, status);
return 0;
}

int8_t toggle_yled(mpu_t *mpu)
{
if (!mpu)
return -1;

HAL_GPIO_TogglePin(mpu->led_gpio, YLED_PIN);
return 0;
}

static int8_t start_adcs(mpu_t *mpu)
{
HAL_StatusTypeDef hal_stat;

hal_stat = HAL_ADC_Start(mpu->accel_adc1);
if (hal_stat)
return hal_stat;

hal_stat = HAL_ADC_Start(mpu->accel_adc2);
if (hal_stat)
return hal_stat;

hal_stat = HAL_ADC_Start(mpu->brake_adc);
if (hal_stat)
return hal_stat;

return 0;
}

static int8_t poll_adc_threaded(ADC_HandleTypeDef *adc)
{
HAL_StatusTypeDef hal_stat = HAL_TIMEOUT;
while (hal_stat == HAL_TIMEOUT) {
hal_stat = HAL_ADC_PollForConversion(adc, ADC_TIMEOUT);

if (hal_stat == HAL_TIMEOUT)
osThreadYield();
}
return hal_stat;
}

/* Note: this should be called from within a thread since it yields to scheduler */
int8_t read_adc(mpu_t *mpu, uint16_t raw[3])
{
if (!mpu)
return -1;

osStatus_t mut_stat = osMutexAcquire(mpu->adc_mutex, osWaitForever);
if (mut_stat)
return mut_stat;

HAL_StatusTypeDef hal_stat = start_adcs(mpu);
if (hal_stat)
return hal_stat;

hal_stat = poll_adc_threaded(mpu->accel_adc1);
if (hal_stat)
return hal_stat;

hal_stat = poll_adc_threaded(mpu->accel_adc2);
if (hal_stat)
return hal_stat;

hal_stat = poll_adc_threaded(mpu->brake_adc);
if (hal_stat)
return hal_stat;

raw[0] = HAL_ADC_GetValue(mpu->accel_adc1);
raw[1] = HAL_ADC_GetValue(mpu->accel_adc2);
raw[2] = HAL_ADC_GetValue(mpu->brake_adc);

return 0;
}


1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Core/Src/dti.c \
Core/Src/state_machine.c \
Core/Src/torque.c \
Core/Src/pdu.c \
Core/Src/mpu.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_can.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c \
Expand Down

0 comments on commit 5f69db4

Please sign in to comment.