1
+ #include " stm32f1_hal.h"
2
+
3
+ #if defined(STM32F1xx)
4
+
5
+ #include " ../../../../communication/SimpleFOCDebug.h"
6
+ #define _TRGO_NOT_AVAILABLE 12345
7
+
8
+ // timer to injected TRGO
9
+ // https://github.com/stm32duino/Arduino_Core_STM32/blob/e156c32db24d69cb4818208ccc28894e2f427cfa/system/Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_adc_ex.h#L215
10
+ uint32_t _timerToInjectedTRGO (HardwareTimer* timer){
11
+ if (timer->getHandle ()->Instance == TIM1)
12
+ return ADC_EXTERNALTRIGINJECCONV_T1_TRGO;
13
+ #ifdef TIM2 // if defined timer 2
14
+ else if (timer->getHandle ()->Instance == TIM2)
15
+ return ADC_EXTERNALTRIGINJECCONV_T2_TRGO;
16
+ #endif
17
+ #ifdef TIM4 // if defined timer 4
18
+ else if (timer->getHandle ()->Instance == TIM4)
19
+ return ADC_EXTERNALTRIGINJECCONV_T4_TRGO;
20
+ #endif
21
+ #ifdef TIM5 // if defined timer 5
22
+ else if (timer->getHandle ()->Instance == TIM5)
23
+ return ADC_EXTERNALTRIGINJECCONV_T5_TRGO;
24
+ #endif
25
+ else
26
+ return _TRGO_NOT_AVAILABLE;
27
+ }
28
+
29
+ // timer to regular TRGO
30
+ // https://github.com/stm32duino/Arduino_Core_STM32/blob/e156c32db24d69cb4818208ccc28894e2f427cfa/system/Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_adc_ex.h#L215
31
+ uint32_t _timerToRegularTRGO (HardwareTimer* timer){
32
+ if (timer->getHandle ()->Instance == TIM3)
33
+ return ADC_EXTERNALTRIGCONV_T3_TRGO;
34
+ #ifdef TIM8 // if defined timer 8
35
+ else if (timer->getHandle ()->Instance == TIM8)
36
+ return ADC_EXTERNALTRIGINJECCONV_T8_TRGO;
37
+ #endif
38
+ else
39
+ return _TRGO_NOT_AVAILABLE;
40
+ }
41
+
42
+ ADC_HandleTypeDef hadc;
43
+
44
+ int _adc_init (Stm32CurrentSenseParams* cs_params, const STM32DriverParams* driver_params)
45
+ {
46
+ ADC_InjectionConfTypeDef sConfigInjected ;
47
+
48
+ /* *Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
49
+ */
50
+ hadc.Instance = (ADC_TypeDef *)pinmap_peripheral (analogInputToPinName (cs_params->pins [0 ]), PinMap_ADC);
51
+ if (hadc.Instance == ADC1) __HAL_RCC_ADC1_CLK_ENABLE ();
52
+ #ifdef ADC2 // if defined ADC2
53
+ else if (hadc.Instance == ADC2) __HAL_RCC_ADC2_CLK_ENABLE ();
54
+ #endif
55
+ #ifdef ADC3 // if defined ADC3
56
+ else if (hadc.Instance == ADC3) __HAL_RCC_ADC3_CLK_ENABLE ();
57
+ #endif
58
+ else {
59
+ #ifdef SIMPLEFOC_STM32_DEBUG
60
+ SIMPLEFOC_DEBUG (" STM32-CS: ERR: Pin does not belong to any ADC!" );
61
+ #endif
62
+ return -1 ; // error not a valid ADC instance
63
+ }
64
+
65
+ hadc.Init .ScanConvMode = ADC_SCAN_ENABLE;
66
+ hadc.Init .ContinuousConvMode = ENABLE;
67
+ hadc.Init .DiscontinuousConvMode = DISABLE;
68
+ hadc.Init .ExternalTrigConv = ADC_SOFTWARE_START;
69
+ hadc.Init .DataAlign = ADC_DATAALIGN_RIGHT;
70
+ hadc.Init .NbrOfConversion = 0 ;
71
+ HAL_ADC_Init (&hadc);
72
+ /* *Configure for the selected ADC regular channel to be converted.
73
+ */
74
+
75
+ /* *Configures for the selected ADC injected channel its corresponding rank in the sequencer and its sample time
76
+ */
77
+ sConfigInjected .InjectedNbrOfConversion = _isset (cs_params->pins [2 ]) ? 3 : 2 ;
78
+ sConfigInjected .InjectedSamplingTime = ADC_SAMPLETIME_1CYCLE_5;
79
+ sConfigInjected .AutoInjectedConv = DISABLE;
80
+ sConfigInjected .InjectedDiscontinuousConvMode = DISABLE;
81
+ sConfigInjected .InjectedOffset = 0 ;
82
+
83
+ // automating TRGO flag finding - hardware specific
84
+ uint8_t tim_num = 0 ;
85
+ while (driver_params->timers [tim_num] != NP && tim_num < 6 ){
86
+ uint32_t trigger_flag = _timerToInjectedTRGO (driver_params->timers [tim_num++]);
87
+ if (trigger_flag == _TRGO_NOT_AVAILABLE) continue ; // timer does not have valid trgo for injected channels
88
+
89
+ // if the code comes here, it has found the timer available
90
+ // timer does have trgo flag for injected channels
91
+ sConfigInjected .ExternalTrigInjecConv = trigger_flag;
92
+
93
+ // this will be the timer with which the ADC will sync
94
+ cs_params->timer_handle = driver_params->timers [tim_num-1 ];
95
+ // done
96
+ break ;
97
+ }
98
+ if ( cs_params->timer_handle == NP ){
99
+ // not possible to use these timers for low-side current sense
100
+ #ifdef SIMPLEFOC_STM32_DEBUG
101
+ SIMPLEFOC_DEBUG (" STM32-CS: ERR: cannot sync any timer to injected channels!" );
102
+ #endif
103
+ return -1 ;
104
+ }
105
+ // display which timer is being used
106
+ #ifdef SIMPLEFOC_STM32_DEBUG
107
+ // it would be better to use the getTimerNumber from driver
108
+ SIMPLEFOC_DEBUG (" STM32-CS: injected trigger for timer index: " , get_timer_index (cs_params->timer_handle ->getHandle ()->Instance ) + 1 );
109
+ #endif
110
+
111
+ // first channel
112
+ sConfigInjected .InjectedRank = ADC_REGULAR_RANK_1;
113
+ sConfigInjected .InjectedChannel = STM_PIN_CHANNEL (pinmap_function (analogInputToPinName (cs_params->pins [0 ]), PinMap_ADC));
114
+ HAL_ADCEx_InjectedConfigChannel (&hadc, &sConfigInjected );
115
+ // second channel
116
+ sConfigInjected .InjectedRank = ADC_REGULAR_RANK_2;
117
+ sConfigInjected .InjectedChannel = STM_PIN_CHANNEL (pinmap_function (analogInputToPinName (cs_params->pins [1 ]), PinMap_ADC));
118
+ HAL_ADCEx_InjectedConfigChannel (&hadc, &sConfigInjected );
119
+
120
+ // third channel - if exists
121
+ if (_isset (cs_params->pins [2 ])){
122
+ sConfigInjected .InjectedRank = ADC_REGULAR_RANK_3;
123
+ sConfigInjected .InjectedChannel = STM_PIN_CHANNEL (pinmap_function (analogInputToPinName (cs_params->pins [2 ]), PinMap_ADC));
124
+ HAL_ADCEx_InjectedConfigChannel (&hadc, &sConfigInjected );
125
+ }
126
+
127
+ // enable interrupt
128
+ HAL_NVIC_SetPriority (ADC1_2_IRQn, 0 , 0 );
129
+ HAL_NVIC_EnableIRQ (ADC1_2_IRQn);
130
+
131
+ cs_params->adc_handle = &hadc;
132
+
133
+ return 0 ;
134
+ }
135
+
136
+ void _adc_gpio_init (Stm32CurrentSenseParams* cs_params, const int pinA, const int pinB, const int pinC)
137
+ {
138
+ uint8_t cnt = 0 ;
139
+ if (_isset (pinA)){
140
+ pinmap_pinout (analogInputToPinName (pinA), PinMap_ADC);
141
+ cs_params->pins [cnt++] = pinA;
142
+ }
143
+ if (_isset (pinB)){
144
+ pinmap_pinout (analogInputToPinName (pinB), PinMap_ADC);
145
+ cs_params->pins [cnt++] = pinB;
146
+ }
147
+ if (_isset (pinC)){
148
+ pinmap_pinout (analogInputToPinName (pinC), PinMap_ADC);
149
+ cs_params->pins [cnt] = pinC;
150
+ }
151
+
152
+ }
153
+
154
+ extern " C" {
155
+ void ADC1_2_IRQHandler (void )
156
+ {
157
+ HAL_ADC_IRQHandler (&hadc);
158
+ }
159
+
160
+ }
161
+
162
+ #endif
0 commit comments