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