Skip to content

Commit

Permalink
Modifying enums to follow ADC DMA indices
Browse files Browse the repository at this point in the history
  • Loading branch information
nwdepatie committed Mar 13, 2024
1 parent 1b543ff commit 51263a7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
30 changes: 24 additions & 6 deletions CM7/Core/Inc/gatedriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,56 @@

#define MAX_ADC_BUF 7

/*
* Note that these phases readings should ALWAYS be mapped to the corresponding indices
* Ensure the ADC DMA is mapped the same across boards
*/
enum {
PHASE_U,
PHASE_V,
PHASE_W,
NUM_PHASES
GATEDRV_PHASE_U,
GATEDRV_PHASE_V,
GATEDRV_PHASE_W,
GATEDRV_NUM_PHASES
};

enum {
GATEDRV_DC_CURRENT = GATEDRV_NUM_PHASES, /* Keep index rolling from phase enum */
GATEDRV_IGBT_TEMP,
SIZE_OF_ADC_DMA
};

/* Definition of gatedriver struct */
typedef struct {
TIM_HandleTypeDef* tim;
osMutexId_t* tim_mutex;
TIM_OC_InitTypeDef* pwm_cfg;
uint32_t pulses[NUM_PHASES];
uint32_t pulses[GATEDRV_NUM_PHASES];

ADC_HandleTypeDef *hdma_adc;
SPI_HandleTypeDef *adc_spi;
uint32_t intern_adc_buffer[MAX_ADC_BUF];
uint32_t intern_adc_buffer[SIZE_OF_ADC_DMA];

osMutexId_t* tim_mutex_mutex;
osMutexAttr_t tim_mutex_attr;
osMutexId_t* ext_adc_mutex;
osMutexAttr_t ext_adc_mutex_attr;
} gatedriver_t;

/* initialize a new gatedriver */
gatedriver_t* gatedrv_init(TIM_HandleTypeDef* tim, ADC_HandleTypeDef *hdma_adc, SPI_HandleTypeDef *adc_spi);

/* read the dc voltage (V) */
int16_t gatedrv_read_dc_voltage(gatedriver_t* drv);

/* read the dc current (A) */
int16_t gatedrv_read_dc_current(gatedriver_t* drv);

/* Note: This has to atomically write to ALL PWM registers */
int16_t gatedrv_write_pwm(gatedriver_t* drv, float duty_cycles[]);

/* read the internal IGBT temp */
int16_t gatedrv_read_igbt_temp(gatedriver_t* drv);

/* read the currents of each phase */
void gatedrv_get_phase_currents(gatedriver_t* drv, int16_t current_buf[GATEDRV_NUM_PHASES]);

#endif /* GATEDRIVER_H */
11 changes: 5 additions & 6 deletions CM7/Core/Src/gatedriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,11 @@ gatedriver_t* gatedrv_init(TIM_HandleTypeDef* tim, ADC_HandleTypeDef *hdma_adc,
return gatedriver;
}

void gatedrv_get_phase_currents(gatedriver_t* drv, int16_t current_buf[])
void gatedrv_get_phase_currents(gatedriver_t* drv, int16_t current_buf[GATEDRV_NUM_PHASES])
{
//TODO: Ensure the ADC DMA is mapped the same across boards
current_buf[PHASE_U] = drv->intern_adc_buffer[0];
current_buf[PHASE_V] = drv->intern_adc_buffer[1];
current_buf[PHASE_W] = drv->intern_adc_buffer[2];
current_buf[GATEDRV_PHASE_U] = drv->intern_adc_buffer[GATEDRV_PHASE_U];
current_buf[GATEDRV_PHASE_V] = drv->intern_adc_buffer[GATEDRV_PHASE_V];
current_buf[GATEDRV_PHASE_W] = drv->intern_adc_buffer[GATEDRV_PHASE_W];
}

int16_t gatedrv_read_dc_voltage(gatedriver_t* drv)
Expand All @@ -87,7 +86,7 @@ int16_t gatedrv_read_dc_current(gatedriver_t* drv)
}

/* Note: This has to atomically write to ALL PWM registers */
int16_t gatedrv_write_pwm(gatedriver_t* drv, float duty_cycles[])
int16_t gatedrv_write_pwm(gatedriver_t* drv, float duty_cycles[GATEDRV_NUM_PHASES])
{
/* Acquiring mutex lock */
osStatus_t mut_stat = osMutexAcquire(drv->tim_mutex, osWaitForever);
Expand Down

0 comments on commit 51263a7

Please sign in to comment.