107
107
#define ADC_CAL2 ((uint16_t *)(ADC_CAL_ADDRESS + 4))
108
108
#define ADC_CAL_BITS (12)
109
109
110
- #elif defined(STM32G0 ) || defined(STM32G4 ) || defined(STM32H5 ) || defined(STM32L1 ) || defined(STM32L4 ) || defined(STM32N6 ) || defined(STM32WB )
110
+ #elif defined(STM32G0 ) || defined(STM32G4 ) || defined(STM32H5 ) || defined(STM32L1 ) || defined(STM32L4 ) || defined(STM32N6 ) || defined(STM32U5 ) || defined( STM32WB )
111
111
112
112
#define ADC_SCALE_V (((float)VREFINT_CAL_VREF) / 1000.0f)
113
113
#define ADC_CAL_ADDRESS (VREFINT_CAL_ADDR)
114
114
#define ADC_CAL1 (TEMPSENSOR_CAL1_ADDR)
115
115
#define ADC_CAL2 (TEMPSENSOR_CAL2_ADDR)
116
+ #if defined(STM32U5 )
117
+ #define ADC_CAL_BITS (14)
118
+ #else
116
119
#define ADC_CAL_BITS (12) // UM2319/UM2570, __HAL_ADC_CALC_TEMPERATURE: 'corresponds to a resolution of 12 bits'
120
+ #endif
117
121
118
122
#elif defined(STM32H7 )
119
123
169
173
#elif defined(STM32N6 )
170
174
// ADC2 VINP 16
171
175
#define VBAT_DIV (4)
176
+ #elif defined(STM32U5 )
177
+ #define VBAT_DIV (4)
172
178
#else
173
179
#error Unsupported processor
174
180
#endif
@@ -228,6 +234,14 @@ static inline uint32_t adc_get_internal_channel(uint32_t channel) {
228
234
} else if (channel == 18 ) {
229
235
channel = ADC_CHANNEL_VBAT ;
230
236
}
237
+ #elif defined(STM32U5 )
238
+ if (channel == 0 ) {
239
+ channel = ADC_CHANNEL_VREFINT ;
240
+ } else if (channel == 18 ) {
241
+ channel = ADC_CHANNEL_VBAT ;
242
+ } else if (channel == 19 ) {
243
+ channel = ADC_CHANNEL_TEMPSENSOR ;
244
+ }
231
245
#endif
232
246
return channel ;
233
247
}
@@ -242,7 +256,7 @@ static bool is_adcx_channel(int channel) {
242
256
// The HAL of STM32L1 defines some channels those may not be available on package
243
257
return __HAL_ADC_IS_CHANNEL_INTERNAL (channel )
244
258
|| (channel < MP_ARRAY_SIZE (pin_adcall_table ) && pin_adcall_table [channel ]);
245
- #elif defined(STM32G0 ) || defined(STM32H7 )
259
+ #elif defined(STM32G0 ) || defined(STM32H7 ) || defined( STM32U5 )
246
260
return __HAL_ADC_IS_CHANNEL_INTERNAL (channel )
247
261
|| IS_ADC_CHANNEL (__HAL_ADC_DECIMAL_NB_TO_CHANNEL (channel ));
248
262
#elif defined(STM32G4 ) || defined(STM32L4 ) || defined(STM32WB )
@@ -263,7 +277,7 @@ static void adc_wait_for_eoc_or_timeout(ADC_HandleTypeDef *adcHandle, int32_t ti
263
277
uint32_t tickstart = HAL_GetTick ();
264
278
#if defined(STM32F4 ) || defined(STM32F7 ) || defined(STM32L1 )
265
279
while ((adcHandle -> Instance -> SR & ADC_FLAG_EOC ) != ADC_FLAG_EOC ) {
266
- #elif defined(STM32F0) || defined(STM32G0) || defined(STM32G4) || defined(STM32H5) || defined(STM32H7) || defined(STM32L4) || defined(STM32N6) || defined(STM32WB)
280
+ #elif defined(STM32F0) || defined(STM32G0) || defined(STM32G4) || defined(STM32H5) || defined(STM32H7) || defined(STM32L4) || defined(STM32N6) || defined(STM32U5) || defined( STM32WB)
267
281
while (READ_BIT (adcHandle -> Instance -> ISR , ADC_FLAG_EOC ) != ADC_FLAG_EOC ) {
268
282
#else
269
283
#error Unsupported processor
@@ -297,7 +311,8 @@ static void adcx_clock_enable(ADC_HandleTypeDef *adch) {
297
311
if (__HAL_RCC_GET_ADC_SOURCE () == RCC_ADCCLKSOURCE_NONE ) {
298
312
__HAL_RCC_ADC_CONFIG (RCC_ADCCLKSOURCE_SYSCLK );
299
313
}
300
- __HAL_RCC_ADC_CLK_ENABLE ();
314
+ #elif defined(STM32U5 )
315
+ __HAL_RCC_ADC12_CLK_ENABLE ();
301
316
#else
302
317
#error Unsupported processor
303
318
#endif
@@ -343,8 +358,8 @@ static void adcx_init_periph(ADC_HandleTypeDef *adch, uint32_t resolution) {
343
358
adch -> Init .LowPowerAutoWait = DISABLE ;
344
359
adch -> Init .DataAlign = ADC_DATAALIGN_RIGHT ;
345
360
adch -> Init .DMAContinuousRequests = DISABLE ;
346
- #elif defined(STM32G0 ) || defined(STM32G4 ) || defined(STM32H5 ) || defined(STM32L4 ) || defined(STM32WB )
347
- #if defined(STM32G4 ) || defined(STM32H5 )
361
+ #elif defined(STM32G0 ) || defined(STM32G4 ) || defined(STM32H5 ) || defined(STM32L4 ) || defined(STM32U5 ) || defined( STM32WB )
362
+ #if defined(STM32G4 ) || defined(STM32H5 ) || defined( STM32U5 )
348
363
adch -> Init .ClockPrescaler = ADC_CLOCK_ASYNC_DIV16 ;
349
364
#else
350
365
adch -> Init .ClockPrescaler = ADC_CLOCK_ASYNC_DIV1 ;
@@ -355,6 +370,12 @@ static void adcx_init_periph(ADC_HandleTypeDef *adch, uint32_t resolution) {
355
370
adch -> Init .OversamplingMode = DISABLE ;
356
371
adch -> Init .DataAlign = ADC_DATAALIGN_RIGHT ;
357
372
adch -> Init .DMAContinuousRequests = DISABLE ;
373
+ #if defined(STM32U5 )
374
+ adch -> Init .GainCompensation = 0 ;
375
+ adch -> Init .TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH ;
376
+ adch -> Init .LeftBitShift = ADC_LEFTBITSHIFT_NONE ;
377
+ adch -> Init .ConversionDataManagement = ADC_CONVERSIONDATA_DR ;
378
+ #endif
358
379
#elif defined(STM32N6 )
359
380
adch -> Init .GainCompensation = 0 ;
360
381
adch -> Init .ScanConvMode = ADC_SCAN_DISABLE ;
@@ -370,7 +391,7 @@ static void adcx_init_periph(ADC_HandleTypeDef *adch, uint32_t resolution) {
370
391
371
392
HAL_ADC_Init (adch );
372
393
373
- #if defined(STM32H7 )
394
+ #if defined(STM32H7 ) || defined( STM32U5 )
374
395
HAL_ADCEx_Calibration_Start (adch , ADC_CALIB_OFFSET , ADC_SINGLE_ENDED );
375
396
#endif
376
397
#if defined(STM32G0 )
@@ -401,6 +422,8 @@ static void adc_config_channel(ADC_HandleTypeDef *adc_handle, uint32_t channel)
401
422
if (__HAL_ADC_IS_CHANNEL_INTERNAL (channel ) == 0 ) {
402
423
channel = __HAL_ADC_DECIMAL_NB_TO_CHANNEL (channel );
403
424
}
425
+ #elif defined(STM32U5 )
426
+ sConfig .Rank = ADC_REGULAR_RANK_1 ;
404
427
#else
405
428
sConfig .Rank = 1 ;
406
429
#endif
@@ -457,6 +480,15 @@ static void adc_config_channel(ADC_HandleTypeDef *adc_handle, uint32_t channel)
457
480
sConfig .OffsetSignedSaturation = DISABLE ;
458
481
sConfig .OffsetSaturation = DISABLE ;
459
482
sConfig .OffsetSign = ADC_OFFSET_SIGN_POSITIVE ;
483
+ #elif defined(STM32U5 )
484
+ if (__HAL_ADC_IS_CHANNEL_INTERNAL (channel )) {
485
+ sConfig .SamplingTime = ADC_SAMPLETIME_391CYCLES_5 ;
486
+ } else {
487
+ sConfig .SamplingTime = ADC4_SAMPLETIME_3CYCLES_5 ;
488
+ }
489
+ sConfig .SingleDiff = ADC_SINGLE_ENDED ;
490
+ sConfig .OffsetNumber = ADC_OFFSET_NONE ;
491
+ sConfig .Offset = 0 ;
460
492
#else
461
493
#error Unsupported processor
462
494
#endif
@@ -678,7 +710,7 @@ static mp_obj_t adc_read_timed(mp_obj_t self_in, mp_obj_t buf_in, mp_obj_t freq_
678
710
// for subsequent samples we can just set the "start sample" bit
679
711
#if defined(STM32F4 ) || defined(STM32F7 ) || defined(STM32L1 )
680
712
self -> handle .Instance -> CR2 |= (uint32_t )ADC_CR2_SWSTART ;
681
- #elif defined(STM32F0 ) || defined(STM32G0 ) || defined(STM32G4 ) || defined(STM32H5 ) || defined(STM32H7 ) || defined(STM32L4 ) || defined(STM32N6 ) || defined(STM32WB )
713
+ #elif defined(STM32F0 ) || defined(STM32G0 ) || defined(STM32G4 ) || defined(STM32H5 ) || defined(STM32H7 ) || defined(STM32L4 ) || defined(STM32N6 ) || defined(STM32U5 ) || defined( STM32WB )
682
714
SET_BIT (self -> handle .Instance -> CR , ADC_CR_ADSTART );
683
715
#else
684
716
#error Unsupported processor
@@ -788,7 +820,7 @@ static mp_obj_t adc_read_timed_multi(mp_obj_t adc_array_in, mp_obj_t buf_array_i
788
820
// ADC is started: set the "start sample" bit
789
821
#if defined(STM32F4 ) || defined(STM32F7 ) || defined(STM32L1 )
790
822
adc -> handle .Instance -> CR2 |= (uint32_t )ADC_CR2_SWSTART ;
791
- #elif defined(STM32F0 ) || defined(STM32G0 ) || defined(STM32G4 ) || defined(STM32H5 ) || defined(STM32H7 ) || defined(STM32L4 ) || defined(STM32N6 ) || defined(STM32WB )
823
+ #elif defined(STM32F0 ) || defined(STM32G0 ) || defined(STM32G4 ) || defined(STM32H5 ) || defined(STM32H7 ) || defined(STM32L4 ) || defined(STM32N6 ) || defined(STM32U5 ) || defined( STM32WB )
792
824
SET_BIT (adc -> handle .Instance -> CR , ADC_CR_ADSTART );
793
825
#else
794
826
#error Unsupported processor
@@ -939,7 +971,7 @@ float adc_read_core_temp_float(ADC_HandleTypeDef *adcHandle) {
939
971
return 0.0f ; // TODO
940
972
#else
941
973
942
- #if defined(STM32G4 ) || defined(STM32L1 ) || defined(STM32L4 )
974
+ #if defined(STM32G4 ) || defined(STM32L1 ) || defined(STM32L4 ) || defined( STM32U5 )
943
975
// Update the reference correction factor before reading tempsensor
944
976
// because TS_CAL1 and TS_CAL2 of STM32G4,L1/L4 are at VDDA=3.0V
945
977
adc_read_core_vref (adcHandle );
@@ -953,7 +985,7 @@ float adc_read_core_temp_float(ADC_HandleTypeDef *adcHandle) {
953
985
return 0 ;
954
986
}
955
987
float core_temp_avg_slope = (* ADC_CAL2 - * ADC_CAL1 ) / 100.0f ;
956
- #elif defined(STM32H5 ) || defined(STM32WB )
988
+ #elif defined(STM32H5 ) || defined(STM32U5 ) || defined( STM32WB )
957
989
int32_t raw_value = adc_config_and_read_ref (adcHandle , ADC_CHANNEL_TEMPSENSOR );
958
990
float core_temp_avg_slope = (* ADC_CAL2 - * ADC_CAL1 ) / 100.0f ;
959
991
#else
@@ -966,7 +998,7 @@ float adc_read_core_temp_float(ADC_HandleTypeDef *adcHandle) {
966
998
}
967
999
968
1000
float adc_read_core_vbat (ADC_HandleTypeDef * adcHandle ) {
969
- #if defined(STM32G4 ) || defined(STM32L4 )
1001
+ #if defined(STM32G4 ) || defined(STM32L4 ) || defined( STM32U5 )
970
1002
// Update the reference correction factor before reading tempsensor
971
1003
// because VREFINT of STM32G4,L4 is at VDDA=3.0V
972
1004
adc_read_core_vref (adcHandle );
0 commit comments