@@ -77,9 +77,15 @@ void analogin_init(analogin_t *obj, PinName pin)
7777
7878 pinmap_pinout (pin , PinMap_ADC );
7979
80+ /* Need to ensure the pin is in input mode */
81+ gpio_init (& gpio , pin );
82+ gpio_dir (& gpio , PIN_INPUT );
83+
8084 reg = IOCON -> PIO [port_number ][pin_number ];
81- /* Clear the DIGIMODE bit */
82- reg &= ~IOCON_PIO_DIGIMODE_MASK ;
85+
86+ /* Clear the MODE & DIGIMODE bit */
87+ reg &= ~(IOCON_PIO_DIGIMODE_MASK | IOCON_PIO_MODE_MASK );
88+
8389 /* For pins PIO0_9, PIO0_11, PIO0_12, PIO0_15, PIO0_18, PIO0_31, PIO1_0 and
8490 PIO1_9, leave ASW bit at '0' in the related IOCON register. */
8591 if (((port_number == 0 ) && ((pin_number == 9 ) || (pin_number == 11 ) || (pin_number == 12 ) ||
@@ -91,11 +97,8 @@ void analogin_init(analogin_t *obj, PinName pin)
9197 /* Enable Analog Switch Input control */
9298 reg |= IOCON_PIO_ASW_MASK ;
9399 }
94- IOCON -> PIO [port_number ][pin_number ] = reg ;
95100
96- /* Need to ensure the pin is in input mode */
97- gpio_init (& gpio , pin );
98- gpio_dir (& gpio , PIN_INPUT );
101+ IOCON -> PIO [port_number ][pin_number ] = reg ;
99102}
100103
101104uint16_t analogin_read_u16 (analogin_t * obj )
@@ -110,16 +113,30 @@ uint16_t analogin_read_u16(analogin_t *obj)
110113 memset (& mLpadcCommandConfigStruct , 0 , sizeof (mLpadcCommandConfigStruct ));
111114 memset (& mLpadcResultConfigStruct , 0 , sizeof (mLpadcResultConfigStruct ));
112115
116+ #if (defined(FSL_FEATURE_LPADC_FIFO_COUNT ) && (FSL_FEATURE_LPADC_FIFO_COUNT == 2 ))
117+ LPADC_DoResetFIFO0 (adc_addrs [instance ]);
118+ LPADC_DoResetFIFO1 (adc_addrs [instance ]);
119+ #else
120+ LPADC_DoResetFIFO (adc_addrs [instance ]);
121+ #endif /* FSL_FEATURE_LPADC_FIFO_COUNT */
122+
113123 /* Set conversion CMD configuration. */
114124 LPADC_GetDefaultConvCommandConfig (& mLpadcCommandConfigStruct );
125+
126+ /* Check if we should use the B channel */
127+ if (obj -> adc >> ADC_B_CHANNEL_SHIFT ) {
128+ mLpadcCommandConfigStruct .sampleChannelMode = kLPADC_SampleChannelSingleEndSideB ;
129+ }
130+
115131 mLpadcCommandConfigStruct .channelNumber = channel ;
132+ mLpadcCommandConfigStruct .conversionResoultuionMode = kLPADC_ConversionResolutionStandard ;
116133 LPADC_SetConvCommandConfig (adc_addrs [instance ], LPADC_USER_CMDID , & mLpadcCommandConfigStruct );
117134
118135 /* Set trigger configuration. */
119136 LPADC_GetDefaultConvTriggerConfig (& mLpadcTriggerConfigStruct );
120137 mLpadcTriggerConfigStruct .targetCommandId = LPADC_USER_CMDID ;
121138 mLpadcTriggerConfigStruct .enableHardwareTrigger = false;
122- LPADC_SetConvTriggerConfig (adc_addrs [instance ], 0U , & mLpadcTriggerConfigStruct ); /* Configurate the trigger0. */
139+ LPADC_SetConvTriggerConfig (adc_addrs [instance ], 0U , & mLpadcTriggerConfigStruct ); /* Configure the trigger0. */
123140
124141 LPADC_DoSoftwareTrigger (adc_addrs [instance ], 1U ); /* 1U is trigger0 mask. */
125142
@@ -131,13 +148,13 @@ uint16_t analogin_read_u16(analogin_t *obj)
131148 }
132149#endif /* FSL_FEATURE_LPADC_FIFO_COUNT */
133150
134- return (( mLpadcResultConfigStruct .convValue ) >> 3U );
151+ return (mLpadcResultConfigStruct .convValue << 1 );
135152}
136153
137154float analogin_read (analogin_t * obj )
138155{
139156 uint16_t value = analogin_read_u16 (obj );
140- return (float )value * (1.0f / (float )0xFFFF );
157+ return (float )value * (1.0f / (float )0xFFF0 );
141158}
142159
143160const PinMap * analogin_pinmap ()
0 commit comments