SimpleFOClibrary 2.4.0
Loading...
Searching...
No Matches
stm32f1_mcu.cpp
Go to the documentation of this file.
1#include "../../../hardware_api.h"
2
3#if defined(STM32F1xx)
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_adc_utils.h"
9#include "../stm32_mcu.h"
10#include "stm32f1_hal.h"
11#include "Arduino.h"
12
13#define _ADC_VOLTAGE_F1 3.3f
14#define _ADC_RESOLUTION_F1 4096.0f
15
16// array of values of 4 injected channels per adc instance (5)
17uint32_t adc_val[5][4]={0};
18
19#ifdef SIMPLEFOC_STM32_ADC_INTERRUPT
20#define USE_ADC_INTERRUPT 1
21#else
22#define USE_ADC_INTERRUPT 0
23#endif
24
25// structure containing the configuration of the adc interrupt
26Stm32AdcInterruptConfig adc_interrupt_config[5] = {
27 {0, 0, USE_ADC_INTERRUPT}, // ADC1
28 {0, 0, USE_ADC_INTERRUPT}, // ADC2
29 {0, 0, USE_ADC_INTERRUPT}, // ADC3
30 {0, 0, USE_ADC_INTERRUPT}, // ADC4
31 {0, 0, USE_ADC_INTERRUPT} // ADC5
32};
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_F1) / (_ADC_RESOLUTION_F1)
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 calibration
70 HAL_ADCEx_Calibration_Start(cs_params->adc_handle);
71
72 // start the adc
73 if(adc_interrupt_config[adc_index].use_adc_interrupt){
74 HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
75 HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
76
77 HAL_ADCEx_InjectedStart_IT(cs_params->adc_handle);
78 }else{
79 HAL_ADCEx_InjectedStart(cs_params->adc_handle);
80 }
81
82
83 // restart all the timers of the driver
84 stm32_resume(driver_params);
85
86 // return the cs parameters
87 // successfully initialized
88 // TODO verify if success in future
89 return _cs_params;
90}
91
92
93// function reading an ADC value and returning the read voltage
94float _readADCVoltageLowSide(const int pin, const void* cs_params){
95 uint8_t adc_index = (uint8_t)_adcToIndex(((Stm32CurrentSenseParams*)cs_params)->adc_handle);
96 return _readADCInjectedChannelVoltage(pin, (void*)cs_params, adc_interrupt_config[adc_index], adc_val[adc_index]);
97}
98
99extern "C" {
100 void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *AdcHandle){
101 uint8_t adc_index = (uint8_t)_adcToIndex(AdcHandle);
102 _handleInjectedConvCpltCallback(AdcHandle, adc_interrupt_config[adc_index], adc_val[adc_index]);
103 }
104}
105
106
107#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