@@ -30,9 +30,13 @@ static ADC_Type *const adc_addrs[] = ADC_BASE_PTRS;
30
30
extern void ADC_ClockPower_Configuration (void );
31
31
32
32
#define MAX_FADC 6000000
33
+ #define MAX_ADC_CLOCK 80000000
33
34
34
35
void analogin_init (analogin_t * obj , PinName pin )
35
36
{
37
+ uint32_t clkval ;
38
+ uint32_t clkdiv = 1 ;
39
+
36
40
obj -> adc = (ADCName )pinmap_peripheral (pin , PinMap_ADC );
37
41
MBED_ASSERT (obj -> adc != (ADCName )NC );
38
42
@@ -42,23 +46,29 @@ void analogin_init(analogin_t *obj, PinName pin)
42
46
uint32_t pin_number = pin & 0x1F ;
43
47
uint8_t port_number = pin / 32 ;
44
48
45
- /* Clear the DIGIMODE bit */
46
- reg = IOCON -> PIO [port_number ][pin_number ] & ~IOCON_PIO_DIGIMODE_MASK ;
47
- IOCON -> PIO [port_number ][pin_number ] = reg ;
48
-
49
49
ADC_ClockPower_Configuration ();
50
50
51
+ /* Ensure the ADC clock derived from the system clock is less than 80MHz */
52
+ clkval = CLOCK_GetFreq (kCLOCK_CoreSysClk );
53
+ while ((clkval / clkdiv ) > MAX_ADC_CLOCK ) {
54
+ clkdiv ++ ;
55
+ }
56
+
51
57
/* Calibration after power up. */
52
58
if (!(ADC_DoSelfCalibration (adc_addrs [instance ]))) {
53
59
/* Calibration failed */
54
60
return ;
55
61
}
56
62
57
63
ADC_GetDefaultConfig (& adc_config );
58
- adc_config .clockDividerNumber = 1 ;
64
+ adc_config .clockDividerNumber = clkdiv ;
59
65
60
66
ADC_Init (adc_addrs [instance ], & adc_config );
61
67
pinmap_pinout (pin , PinMap_ADC );
68
+
69
+ /* Clear the DIGIMODE bit */
70
+ reg = IOCON -> PIO [port_number ][pin_number ] & ~IOCON_PIO_DIGIMODE_MASK ;
71
+ IOCON -> PIO [port_number ][pin_number ] = reg ;
62
72
}
63
73
64
74
uint16_t analogin_read_u16 (analogin_t * obj )
@@ -70,7 +80,7 @@ uint16_t analogin_read_u16(analogin_t *obj)
70
80
71
81
adcConvSeqConfigStruct .channelMask = (1U << channel );
72
82
adcConvSeqConfigStruct .triggerMask = 0U ;
73
- adcConvSeqConfigStruct .triggerPolarity = kADC_TriggerPolarityNegativeEdge ;
83
+ adcConvSeqConfigStruct .triggerPolarity = kADC_TriggerPolarityPositiveEdge ;
74
84
adcConvSeqConfigStruct .enableSingleStep = false;
75
85
adcConvSeqConfigStruct .enableSyncBypass = false;
76
86
adcConvSeqConfigStruct .interruptMode = kADC_InterruptForEachSequence ;
0 commit comments