SimpleFOClibrary 2.4.0
Loading...
Searching...
No Matches
stm32f4_mcu.cpp
Go to the documentation of this file.
1#include "../../../hardware_api.h"
2
3#if defined(STM32F4xx)
4#include "../../../../common/foc_utils.h"
5#include "../../../../drivers/hardware_api.h"
6#include "../../../../drivers/hardware_specific/stm32/stm32_mcu.h"
7#include "../../../hardware_api.h"
8#include "../stm32_mcu.h"
9#include "../stm32_adc_utils.h"
10#include "stm32f4_hal.h"
11#include "Arduino.h"
12
13
14#define _ADC_VOLTAGE_F4 3.3f
15#define _ADC_RESOLUTION_F4 4096.0f
16
17#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
18#define USE_ADC_INTERRUPT 1
19#else
20#define USE_ADC_INTERRUPT 0
21#endif
22
23// array of values of 4 injected channels per adc instance (5)
24uint32_t adc_val[5][4]={0};
25
26// structure containing the configuration of the adc interrupt
27Stm32AdcInterruptConfig adc_interrupt_config[5] = {
28 {0, 0, USE_ADC_INTERRUPT}, // ADC1
29 {0, 0, USE_ADC_INTERRUPT}, // ADC2
30 {0, 0, USE_ADC_INTERRUPT}, // ADC3
31 {0, 0, USE_ADC_INTERRUPT}, // ADC4
32 {0, 0, USE_ADC_INTERRUPT} // ADC5
33};
34
35void* _configureADCLowSide(const void* driver_params, const int pinA, const int pinB, const int pinC){
36
37 Stm32CurrentSenseParams* cs_params= new Stm32CurrentSenseParams {
38 .pins={(int)NOT_SET,(int)NOT_SET,(int)NOT_SET},
39 .adc_voltage_conv = (_ADC_VOLTAGE_F4) / (_ADC_RESOLUTION_F4)
40 };
41 if(_adc_gpio_init(cs_params, pinA,pinB,pinC) != 0) return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED;
42 if(_adc_init(cs_params, (STM32DriverParams*)driver_params) != 0) return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED;
43 return cs_params;
44}
45
46
47void* _driverSyncLowSide(void* _driver_params, void* _cs_params){
48 STM32DriverParams* driver_params = (STM32DriverParams*)_driver_params;
49 Stm32CurrentSenseParams* cs_params = (Stm32CurrentSenseParams*)_cs_params;
50
51 // if compatible timer has not been found
52 if (cs_params->timer_handle == NULL) return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED;
53
54 // stop all the timers for the driver
55 stm32_pause(driver_params);
56
57 // get the index of the adc
58 int adc_index = _adcToIndex(cs_params->adc_handle);
59
60 bool tim_interrupt = _initTimerInterruptDownsampling(cs_params, driver_params, adc_interrupt_config[adc_index]);
61 if(tim_interrupt) {
62 // error in the timer interrupt initialization
63 SIMPLEFOC_DEBUG("STM32-CS: timer has no repetition counter, ADC interrupt has to be used");
64 }
65
66 // set the trigger output event
67 LL_TIM_SetTriggerOutput(cs_params->timer_handle->Instance, LL_TIM_TRGO_UPDATE);
68
69 // start the adc
70 if (adc_interrupt_config[adc_index].use_adc_interrupt){
71 // enable interrupt
72 HAL_NVIC_SetPriority(ADC_IRQn, 0, 0);
73 HAL_NVIC_EnableIRQ(ADC_IRQn);
74
75 HAL_ADCEx_InjectedStart_IT(cs_params->adc_handle);
76 }else{
77 HAL_ADCEx_InjectedStart(cs_params->adc_handle);
78 }
79
80 // restart all the timers of the driver
81 stm32_resume(driver_params);
82
83 // return the cs parameters
84 // successfully initialized
85 // TODO verify if success in future
86 return _cs_params;
87}
88
89
90// function reading an ADC value and returning the read voltage
91float _readADCVoltageLowSide(const int pin, const void* cs_params){
92 uint8_t adc_index = (uint8_t)_adcToIndex(((Stm32CurrentSenseParams*)cs_params)->adc_handle);
93 return _readADCInjectedChannelVoltage(pin, (void*)cs_params, adc_interrupt_config[adc_index], adc_val[adc_index]);
94}
95
96extern "C" {
97 void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *AdcHandle){
98 uint8_t adc_index = (uint8_t)_adcToIndex(AdcHandle);
99 _handleInjectedConvCpltCallback(AdcHandle, adc_interrupt_config[adc_index], adc_val[adc_index]);
100 }
101}
102
103#endif
#define SIMPLEFOC_DEBUG(msg,...)
void * _driverSyncLowSide(void *driver_params, void *cs_params)
#define SIMPLEFOC_CURRENT_SENSE_INIT_FAILED
void * _configureADCLowSide(const void *driver_params, const int pinA, const int pinB, const int pinC=NOT_SET)
float _readADCVoltageLowSide(const int pinA, const void *cs_params)
const int const int const int pinC
#define NOT_SET
Definition foc_utils.h:34