Hi Team,
I referred the following tutorial and the ADC reference of Zephyr :
https://www.youtube.com/watch?v=Y8FaEcAp_kY
https://docs.zephyrproject.org/latest/samples/drivers/adc/README.html
I made changes to the LPCXpresso55s16 board file by referring the board file of LPCXpresso55s69:
1) Added pinmux_lpadc0 configuration to lpcxpresso55s16-pinctrl.dtsi file
pinmux_lpadc0: pinmux_lpadc0 {
group0 {
pinmux = <ADC0_CH0_PIO0_23>,
<ADC0_CH8_PIO0_16>,
<ADC0_CH4_PIO1_8>,
<ADC0_CH12_PIO1_9>;
slew-rate = "standard";
nxp,analog-mode;
};
};
2) Enabled ADC through adding the below code to lpcxpresso55s16.dts file
&adc0 {
status = "okay";
pinctrl-0 = <&pinmux_lpadc0>;
pinctrl-names = "default";
};
3) Added 'adc' under supported section of the lpcxpresso55s16.yaml file
identifier: lpcxpresso55s16
name: NXP LPCXpresso55S16
type: mcu
arch: arm
ram: 96
flash: 256
toolchain:
- zephyr
- gnuarmemb
- xtools
supported:
- adc
- arduino_gpio
- arduino_i2c
- arduino_spi
- can
- gpio
- i2c
- spi
- usb_device
4) Added adc0 under the aliases field of the lpcxpresso55s16_common.dtsi file
aliases{
led0 = &red_led;
led1 = &green_led;
led2 = &blue_led;
sw0 = &btn_wk;
sw1 = &btn_usr;
sw2 = &btn_isp;
adcctrl = &adc0;
usart-0 = &flexcomm0;
magn0 = &fxos8700;
accel0 = &fxos8700;
};
I used the below code to read the adc samples from LPCXpresso55s16 through PIO0_23/ADC0_0 (12th pin - LPC55S16JBD64)
#include <stdint.h>
#include <stdio.h>
#include <zephyr.h>
#include <device.h>
#include <drivers/adc.h>
#include "logging/osal_log.h"
#include <can_com.h>
#include "can/osal_can.h"
#define ADC_DEVICE_NAME "DT_LABEL(DT_ALIAS(adcctrl))"
OSAL_LOG_MODULE_REGISTER(com_data_handler);
void main(void)
{
enum adc_gain {
ADC_GAIN_1_6,
ADC_GAIN_1_5,
ADC_GAIN_1_4,
ADC_GAIN_1_3,
ADC_GAIN_1_2,
ADC_GAIN_2_3,
ADC_GAIN_1,
ADC_GAIN_2,
ADC_GAIN_3,
ADC_GAIN_4,
ADC_GAIN_8,
ADC_GAIN_16,
ADC_GAIN_32,
ADC_GAIN_64,
};
enum adc_reference {
ADC_REF_VDD_1,
ADC_REF_VDD_1_2,
ADC_REF_VDD_1_3,
ADC_REF_VDD_1_4,
ADC_REF_INTERNAL,
ADC_REF_EXTERNAL0,
ADC_REF_EXTERNAL1,
};
enum adc_input_positive {
ADC_POSITIVE_INPUT_AIN0,
ADC_POSITIVE_INPUT_AIN1,
ADC_POSITIVE_INPUT_AIN2,
ADC_POSITIVE_INPUT_AIN3,
ADC_POSITIVE_INPUT_AIN4,
ADC_POSITIVE_INPUT_AIN5,
ADC_POSITIVE_INPUT_AIN6,
ADC_POSITIVE_INPUT_AIN7,
ADC_POSITIVE_INPUT_AIN8,
ADC_POSITIVE_INPUT_AIN9,
ADC_POSITIVE_INPUT_AIN10,
ADC_POSITIVE_INPUT_AIN11,
ADC_POSITIVE_INPUT_AIN12,
ADC_POSITIVE_INPUT_AIN13,
ADC_POSITIVE_INPUT_AIN14,
ADC_POSITIVE_INPUT_AIN15,
};
struct adc_channel_cfg {
uint32_t acquisition_time;
uint8_t channel_id;
enum adc_input_positive input_positive;
enum adc_gain gain;
enum adc_reference reference;
};
struct adc_sequence {
uint16_t *buffer;
uint16_t buffer_size;
uint8_t resolution;
uint8_t channels;
struct adc_channel_cfg *channels_cfg;
const struct adc_sequence_options *options;
uint32_t timeout;
bool calibrate;
};
const struct device *adc_dev;
struct adc_channel_cfg channel_cfg;
struct adc_sequence sequence;
uint16_t buffer[4];
adc_dev = device_get_binding(ADC_DEVICE_NAME);
if (!adc_dev) {
OSAL_LOG_INF("Failed to get ADC device\n");
return;
}
channel_cfg.gain = ADC_GAIN_1_6;
channel_cfg.reference = ADC_REF_INTERNAL;
channel_cfg.acquisition_time = ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 40);
channel_cfg.channel_id = 0;
channel_cfg.input_positive = ADC_POSITIVE_INPUT_AIN0;
sequence.buffer = buffer;
sequence.buffer_size = sizeof(buffer);
sequence.resolution = 16;
sequence.channels = 1;
sequence.options = NULL;
sequence.timeout = SYS_FOREVER_MS;
sequence.calibrate = false;
sequence.channels_cfg = &channel_cfg;
while (1) {
if (adc_read(adc_dev, &sequence) != 0) {
OSAL_LOG_INF("ADC read failed\n");
return;
}
OSAL_LOG_INF("ADC value: %u\n", buffer[0]);
k_sleep(K_SECONDS(1));
}
}
I am able to build the code without any error. But, after connecting the real LPCXpresso55s16 board and flashed the hex file into the board.
The output is showing error:
*** Booting Zephyr OS build zephyr-v3.3.0-3441-g3154e03c454a ***
[00:00:00.005,000] <inf> com_data_handler: Failed to get ADC device
I am not sure where I made mistakes. If there is any possibilities, please guide me to resolve this issue.
Any suggestions and corrections are greatly appreciated.
Thanks in advance.