Skip to content

Commit 9b8a859

Browse files
committed
MCUXpresso: Update the Analogin driver for LPC devices
1. Update the clock divider setting 2. ADC resolution is 12-bits, update the API return value to return 16-bit result 3. Update IOMUX setup Signed-off-by: Mahesh Mahadevan <[email protected]>
1 parent f464867 commit 9b8a859

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_LPC/analogin_api.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void analogin_init(analogin_t *obj, PinName pin)
4949
ADC_ClockPower_Configuration();
5050

5151
/* Ensure the ADC clock derived from the system clock is less than 80MHz */
52-
clkval = CLOCK_GetFreq(kCLOCK_CoreSysClk);
52+
clkval = CLOCK_GetFreq(kCLOCK_BusClk);
5353
while ((clkval / clkdiv) > MAX_ADC_CLOCK) {
5454
clkdiv++;
5555
}
@@ -61,20 +61,20 @@ void analogin_init(analogin_t *obj, PinName pin)
6161
}
6262

6363
ADC_GetDefaultConfig(&adc_config);
64-
adc_config.clockDividerNumber = clkdiv;
64+
adc_config.clockDividerNumber = (clkdiv - 1);
6565

6666
ADC_Init(adc_addrs[instance], &adc_config);
6767
pinmap_pinout(pin, PinMap_ADC);
6868

69-
/* Clear the DIGIMODE bit */
70-
reg = IOCON->PIO[port_number][pin_number] & ~IOCON_PIO_DIGIMODE_MASK;
69+
/* Clear the DIGIMODE & MODE bits */
70+
reg = IOCON->PIO[port_number][pin_number] & ~(IOCON_PIO_DIGIMODE_MASK | IOCON_PIO_MODE_MASK);
7171
IOCON->PIO[port_number][pin_number] = reg;
7272
}
7373

7474
uint16_t analogin_read_u16(analogin_t *obj)
7575
{
7676
uint32_t instance = obj->adc >> ADC_INSTANCE_SHIFT;
77-
uint32_t channel = obj->adc & 0xF;
77+
uint32_t channel = obj->adc & 0xFF;
7878
adc_conv_seq_config_t adcConvSeqConfigStruct;
7979
adc_result_info_t adcResultInfoStruct;
8080

@@ -93,13 +93,15 @@ uint16_t analogin_read_u16(analogin_t *obj)
9393
while (!ADC_GetChannelConversionResult(adc_addrs[instance], channel, &adcResultInfoStruct)) {
9494
}
9595

96-
return adcResultInfoStruct.result;
96+
/* The ADC has 12 bit resolution. We shift in 4 0s */
97+
/* from the right to make it a 16 bit number as expected */
98+
return adcResultInfoStruct.result << 4;
9799
}
98100

99101
float analogin_read(analogin_t *obj)
100102
{
101103
uint16_t value = analogin_read_u16(obj);
102-
return (float)value * (1.0f / (float)0xFFFF);
104+
return (float)value * (1.0f / (float)0xFFF0);
103105
}
104106

105107
const PinMap *analogin_pinmap()

targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_MCU_LPC546XX/TARGET_LPCXpresso/mbed_overrides.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ void ADC_ClockPower_Configuration(void)
135135
* The divider would be set when configuring the converter.
136136
*/
137137
CLOCK_EnableClock(kCLOCK_Adc0); /* SYSCON->AHBCLKCTRL[0] |= SYSCON_AHBCLKCTRL_ADC0_MASK; */
138+
RESET_PeripheralReset(kADC0_RST_SHIFT_RSTn);
138139
}
139140

140141
/* Initialize the external memory. */

0 commit comments

Comments
 (0)