SimpleFOClibrary 2.4.0
Loading...
Searching...
No Matches
stm32h7_mcu.cpp
Go to the documentation of this file.
1#include "../../../hardware_api.h"
2
3#if defined(STM32H7xx)
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 "stm32h7_hal.h"
11#include "Arduino.h"
12
13
14#define _ADC_VOLTAGE 3.3f
15#define _ADC_RESOLUTION 4096.0f
16
17
18// array of values of 4 injected channels per adc instance (5)
19uint32_t adc_val[5][4]={0};
20
21#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
22#define USE_ADC_INTERRUPT 1
23#else
24#define USE_ADC_INTERRUPT 0
25#endif
26
27// structure containing the configuration of the adc interrupt
28Stm32AdcInterruptConfig adc_interrupt_config[5] = {
29 {0, 0, USE_ADC_INTERRUPT}, // ADC1
30 {0, 0, USE_ADC_INTERRUPT}, // ADC2
31 {0, 0, USE_ADC_INTERRUPT}, // ADC3
32 {0, 0, USE_ADC_INTERRUPT}, // ADC4
33 {0, 0, USE_ADC_INTERRUPT} // ADC5
34};
35
36
37void* _configureADCLowSide(const void* driver_params, const int pinA, const int pinB, const int pinC){
38
39 Stm32CurrentSenseParams* cs_params= new Stm32CurrentSenseParams {
40 .pins={(int)NOT_SET,(int)NOT_SET,(int)NOT_SET},
41 .adc_voltage_conv = (_ADC_VOLTAGE) / (_ADC_RESOLUTION)
42 };
43 if(_adc_gpio_init(cs_params, pinA,pinB,pinC) != 0) return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED;
44 if(_adc_init(cs_params, (STM32DriverParams*)driver_params) != 0) return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED;
45 return cs_params;
46}
47
48
49void* _driverSyncLowSide(void* _driver_params, void* _cs_params){
50 STM32DriverParams* driver_params = (STM32DriverParams*)_driver_params;
51 Stm32CurrentSenseParams* cs_params = (Stm32CurrentSenseParams*)_cs_params;
52
53 // if compatible timer has not been found
54 if (cs_params->timer_handle == NULL) return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED;
55
56 // stop all the timers for the driver
57 stm32_pause(driver_params);
58
59 // get the index of the adc
60 int adc_index = _adcToIndex(cs_params->adc_handle);
61 bool tim_interrupt = _initTimerInterruptDownsampling(cs_params, driver_params, adc_interrupt_config[adc_index]);
62 if(tim_interrupt) {
63 // error in the timer interrupt initialization
64 SIMPLEFOC_DEBUG("STM32-CS: timer has no repetition counter, ADC interrupt has to be used");
65 }
66
67
68 // set the trigger output event
69 LL_TIM_SetTriggerOutput(cs_params->timer_handle->Instance, LL_TIM_TRGO_UPDATE);
70
71 // Start the adc calibration
72 if(HAL_ADCEx_Calibration_Start(cs_params->adc_handle, ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED) != HAL_OK){
73 #ifdef SIMPLEFOC_STM32_DEBUG
74 SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot calibrate ADC!");
75 #endif
77 }
78
79 // start the adc
80 if(adc_interrupt_config[adc_index].use_adc_interrupt){
81
82 if(cs_params->adc_handle->Instance == ADC1){
83 // enable interrupt
84 HAL_NVIC_SetPriority(ADC_IRQn, 0, 0);
85 HAL_NVIC_EnableIRQ(ADC_IRQn);
86 }
87 #ifdef ADC2 // if defined ADC2
88 else if(cs_params->adc_handle->Instance == ADC2) {
89 // enable interrupt
90 HAL_NVIC_SetPriority(ADC3_IRQn, 0, 0);
91 HAL_NVIC_EnableIRQ(ADC3_IRQn);
92 }
93 #endif
94 #ifdef ADC3 // if defined ADC3
95 else if(cs_params->adc_handle->Instance == ADC3) {
96 // enable interrupt
97 HAL_NVIC_SetPriority(ADC3_IRQn, 0, 0);
98 HAL_NVIC_EnableIRQ(ADC3_IRQn);
99 }
100 #endif
101 if(HAL_ADCEx_InjectedStart_IT(cs_params->adc_handle) != HAL_OK){
102 #ifdef SIMPLEFOC_STM32_DEBUG
103 SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot start injected channels in interrupt mode!");
104 #endif
106 }
107 }else{
108 if(HAL_ADCEx_InjectedStart(cs_params->adc_handle) != HAL_OK){
109 #ifdef SIMPLEFOC_STM32_DEBUG
110 SIMPLEFOC_DEBUG("STM32-CS: ERR: cannot start injected channels!");
111 #endif
113 }
114 }
115
116
117 // restart all the timers of the driver
118 stm32_resume(driver_params);
119
120 // return the cs parameters
121 // successfully initialized
122 // TODO verify if success in future
123 return _cs_params;
124}
125
126
127// function reading an ADC value and returning the read voltage
128float _readADCVoltageLowSide(const int pin, const void* cs_params){
129 uint8_t adc_index = (uint8_t)_adcToIndex(((Stm32CurrentSenseParams*)cs_params)->adc_handle);
130 return _readADCInjectedChannelVoltage(pin, (void*)cs_params, adc_interrupt_config[adc_index], adc_val[adc_index]);
131}
132
133extern "C" {
134 void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *AdcHandle){
135 uint8_t adc_index = (uint8_t)_adcToIndex(AdcHandle);
136 _handleInjectedConvCpltCallback(AdcHandle, adc_interrupt_config[adc_index], adc_val[adc_index]);
137 }
138}
139
140#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