SimpleFOClibrary 2.4.0
Loading...
Searching...
No Matches
stm32l4_mcu.cpp
Go to the documentation of this file.
1#include "../../../hardware_api.h"
2
3#if defined(STM32L4xx)
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 "stm32l4_hal.h"
12#include "Arduino.h"
13
14
15#define _ADC_VOLTAGE_L4 3.3f
16#define _ADC_RESOLUTION_L4 4096.0f
17
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
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_L4) / (_ADC_RESOLUTION_L4)
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
62 bool tim_interrupt = _initTimerInterruptDownsampling(cs_params, driver_params, adc_interrupt_config[adc_index]);
63 if(tim_interrupt) {
64 // error in the timer interrupt initialization
65 SIMPLEFOC_DEBUG("STM32-CS: timer has no repetition counter, ADC interrupt has to be used");
66 }
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 if(cs_params->adc_handle->Instance == ADC1) {
78 // enable interrupt
79 HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
80 HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
81 }
82 #ifdef ADC2
83 else if (cs_params->adc_handle->Instance == ADC2) {
84 // enable interrupt
85 HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
86 HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
87 }
88 #endif
89 #ifdef ADC3
90 else if (cs_params->adc_handle->Instance == ADC3) {
91 // enable interrupt
92 HAL_NVIC_SetPriority(ADC3_IRQn, 0, 0);
93 HAL_NVIC_EnableIRQ(ADC3_IRQn);
94 }
95 #endif
96 #ifdef ADC4
97 else if (cs_params->adc_handle->Instance == ADC4) {
98 // enable interrupt
99 HAL_NVIC_SetPriority(ADC4_IRQn, 0, 0);
100 HAL_NVIC_EnableIRQ(ADC4_IRQn);
101 }
102 #endif
103 #ifdef ADC5
104 else if (cs_params->adc_handle->Instance == ADC5) {
105 // enable interrupt
106 HAL_NVIC_SetPriority(ADC5_IRQn, 0, 0);
107 HAL_NVIC_EnableIRQ(ADC5_IRQn);
108 }
109 #endif
110 HAL_ADCEx_InjectedStart_IT(cs_params->adc_handle);
111 }else{
112 HAL_ADCEx_InjectedStart(cs_params->adc_handle);
113 }
114
115 // restart all the timers of the driver
116 stm32_resume(driver_params);
117 // return the cs parameters
118 // successfully initialized
119 // TODO verify if success in future
120 return _cs_params;
121}
122
123// function reading an ADC value and returning the read voltage
124float _readADCVoltageLowSide(const int pin, const void* cs_params){
125 uint8_t adc_index = (uint8_t)_adcToIndex(((Stm32CurrentSenseParams*)cs_params)->adc_handle);
126 return _readADCInjectedChannelVoltage(pin, (void*)cs_params, adc_interrupt_config[adc_index], adc_val[adc_index]);
127}
128
129extern "C" {
130 void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *AdcHandle){
131 uint8_t adc_index = (uint8_t)_adcToIndex(AdcHandle);
132 _handleInjectedConvCpltCallback(AdcHandle, adc_interrupt_config[adc_index], adc_val[adc_index]);
133 }
134}
135
136#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