Skip to content

Commit

Permalink
Merge branch 'main' into #23-state-machine
Browse files Browse the repository at this point in the history
  • Loading branch information
nwdepatie authored Mar 19, 2024
2 parents 4e480c3 + 7a58b33 commit a80f6e4
Show file tree
Hide file tree
Showing 11 changed files with 368 additions and 203 deletions.
6 changes: 3 additions & 3 deletions .mxproject

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions CM7/Core/Inc/fault.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef PROTEUS_FAULT_H
#define PROTEUS_FAULT_H

#include "cmsis_os.h"

typedef enum
{
DEFCON1 = 1,
DEFCON2,
DEFCON3,
DEFCON4,
DEFCON5
} fault_sev_t;

// TODO: add actual fault codes
typedef enum
{
FAULTS_CLEAR = 0x0,
MAX_FAULTS
} fault_code_t;

typedef struct
{
fault_code_t id;
fault_sev_t severity;
char *diag;
} fault_data_t;

/* Function to queue a fault */
int queue_fault(fault_data_t *fault_data);

/* Defining Fault Hanlder Task */
void vFaultHandler(void *pv_params);
extern osThreadId_t fault_handle;
extern const osThreadAttr_t fault_handle_attributes;

#endif // FAULT_H
2 changes: 1 addition & 1 deletion CM7/Core/Inc/gatedriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ enum {
typedef struct {
TIM_HandleTypeDef* tim;
osMutexId_t* tim_mutex;
TIM_OC_InitTypeDef* pwm_cfg;
TIM_OC_InitTypeDef pwm_cfg;
uint32_t pulses[GATEDRV_NUM_PHASES];

ADC_HandleTypeDef *hdma_adc;
Expand Down
31 changes: 31 additions & 0 deletions CM7/Core/Inc/ssi_encoder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef SSI_ENCODER_H
#define SSI_ENCODER_H

#include "stm32h7xx.h"
#include "stm32h7xx_hal_spi.h"
#include <stdint.h>

typedef struct ssi_encoder {
SPI_HandleTypeDef *hspi;
} ssi_encoder_t;

/**
* @brief Creates new ssi encoder operating on given spi peripheral
*
* @param spi_handle
* @return ssi_encoder_t* pointer to new ssi encoder object
*/
ssi_encoder_t *ssi_encoder_init(SPI_HandleTypeDef *spi_handle);

/**
* @brief Reads absolute angle from encoder and converts it to radians
* NOTE: The datasheet for the encoder states that the minimum delay
* between reads must be between 12.5 us and 20.5 us
*
* @param encoder
* @param angle pointer to angle in radians
* @return int16_t nonzero if the read fails, 0 on success
*/
int16_t ssi_encoder_get_angle(ssi_encoder_t *encoder, float *angle);

#endif /* SSI_ENCODER_H */
57 changes: 57 additions & 0 deletions CM7/Core/Src/fault.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "fault.h"
#include "task.h"

#define FAULT_HANDLE_QUEUE_SIZE 16

osMessageQueueId_t fault_handle_queue;

osThreadId_t fault_handle;
const osThreadAttr_t fault_handle_attributes = {
.name = "FaultHandler",
.stack_size = 128 * 8,
.priority = (osPriority_t)osPriorityHigh7,
};

int queue_fault(fault_data_t *fault_data)
{
if (!fault_handle_queue)
return -1;

osMessageQueuePut(fault_handle_queue, fault_data, 0U, 0U);
return 0;
}

void vFaultHandler(void *pv_params)
{
fault_data_t fault_data;
osStatus_t status;
fault_handle_queue = osMessageQueueNew(FAULT_HANDLE_QUEUE_SIZE, sizeof(fault_data_t), NULL);

for (;;)
{
/* Wait until a message is in the queue, send messages when they are in the queue */
status = osMessageQueueGet(fault_handle_queue, &fault_data, NULL, osWaitForever);
if (status == osOK)
{
// TODO: add logging functionality?
switch (fault_data.severity)
{
case DEFCON1: /* Highest(1st) Priority */
break;
case DEFCON2:
break;
case DEFCON3:
break;
case DEFCON4:
break;
case DEFCON5: /* Lowest Priority */
break;
default:
break;
}
}

/* Yield to other tasks */
osThreadYield();
}
}
40 changes: 19 additions & 21 deletions CM7/Core/Src/gatedriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,12 @@ gatedriver_t* gatedrv_init(TIM_HandleTypeDef* tim, ADC_HandleTypeDef *hdma_adc,
assert(HAL_TIM_PWM_Init(tim) != HAL_OK);

/* Common configuration for all PWM channels */
TIM_OC_InitTypeDef pwm_cfg;
pwm_cfg.OCMode = TIM_OCMODE_PWM1;
pwm_cfg.OCPolarity = TIM_OCPOLARITY_HIGH;
pwm_cfg.OCNPolarity = TIM_OCNPOLARITY_HIGH;
pwm_cfg.OCIdleState = TIM_OCIDLESTATE_SET;
pwm_cfg.OCNIdleState = TIM_OCNIDLESTATE_RESET;
pwm_cfg.OCFastMode = TIM_OCFAST_DISABLE;
gatedriver->pwm_cfg = &pwm_cfg;
gatedriver->pwm_cfg.OCMode = TIM_OCMODE_PWM1;
gatedriver->pwm_cfg.OCPolarity = TIM_OCPOLARITY_HIGH;
gatedriver->pwm_cfg.OCNPolarity = TIM_OCNPOLARITY_HIGH;
gatedriver->pwm_cfg.OCIdleState = TIM_OCIDLESTATE_SET;
gatedriver->pwm_cfg.OCNIdleState = TIM_OCNIDLESTATE_RESET;
gatedriver->pwm_cfg.OCFastMode = TIM_OCFAST_DISABLE;

/* Configure DMA */
assert(HAL_ADC_Start_DMA(gatedriver->hdma_adc, gatedriver->intern_adc_buffer, GATEDRV_SIZE_OF_ADC_DMA));
Expand Down Expand Up @@ -101,37 +99,37 @@ int16_t gatedrv_write_pwm(gatedriver_t* drv, float duty_cycles[GATEDRV_NUM_PHASE
pulses[2] = (uint32_t) (duty_cycles[2] * PERIOD_VALUE / 100);

/* Getting PWM channel config */
TIM_OC_InitTypeDef* config = drv->pwm_cfg;
TIM_OC_InitTypeDef config = drv->pwm_cfg;

/* Attempting to set channel 1 */
config->Pulse = pulses[0];
if(HAL_TIM_PWM_ConfigChannel(drv->tim, config, TIM_CHANNEL_1) != HAL_OK)
config.Pulse = pulses[0];
if(HAL_TIM_PWM_ConfigChannel(drv->tim, &config, TIM_CHANNEL_1) != HAL_OK)
{
/* Do nothing and return */
return 1;
}

/* Attempting to set channel 2 */
config->Pulse = pulses[1];
if(HAL_TIM_PWM_ConfigChannel(drv->tim, config, TIM_CHANNEL_2) != HAL_OK)
config.Pulse = pulses[1];
if(HAL_TIM_PWM_ConfigChannel(drv->tim, &config, TIM_CHANNEL_2) != HAL_OK)
{
/* Attempt to revert last channel change and return */
config->Pulse = drv->pulses[0];
HAL_TIM_PWM_ConfigChannel(drv->tim, config, TIM_CHANNEL_1);
config.Pulse = drv->pulses[0];
HAL_TIM_PWM_ConfigChannel(drv->tim, &config, TIM_CHANNEL_1);

return 1;
}

/* Attempting to set channel 3 */
config->Pulse = pulses[2];
if(HAL_TIM_PWM_ConfigChannel(drv->tim, config, TIM_CHANNEL_3) != HAL_OK)
config.Pulse = pulses[2];
if(HAL_TIM_PWM_ConfigChannel(drv->tim, &config, TIM_CHANNEL_3) != HAL_OK)
{
/* Attempt to revert previous channel changes and return */
config->Pulse = drv->pulses[0];
HAL_TIM_PWM_ConfigChannel(drv->tim, config, TIM_CHANNEL_1);
config.Pulse = drv->pulses[0];
HAL_TIM_PWM_ConfigChannel(drv->tim, &config, TIM_CHANNEL_1);

config->Pulse = drv->pulses[1];
HAL_TIM_PWM_ConfigChannel(drv->tim, config, TIM_CHANNEL_2);
config.Pulse = drv->pulses[1];
HAL_TIM_PWM_ConfigChannel(drv->tim, &config, TIM_CHANNEL_2);

return 1;
}
Expand Down
Loading

0 comments on commit a80f6e4

Please sign in to comment.