adc_channel_setup error when measuring ADC for nrf5340dk

Hi

I am currently trying to create ADC and BLE Services using nrf5340 DK for evaluation before jump to custom board. So based on several threads in this forum, I found this is the simplest tutorial and I think adc almost work (not works yet). .

As you can see below is some of my code and configratuin that I create for firmwre.

---------------------- app.overlay

 &adc {
	status = "okay";
	label = "ADC_0";
};

 / {
	zephyr,user {
		io-channels = <&adc 1>, <&adc 1>, <&adc 2>, <&adc 3>;
	};
	chosen {
		nordic,nus-uart = &uart0;
	};
};

---------------------- prj.conf

# ADC Configuration
CONFIG_ADC=y
CONFIG_CBPRINTF_FP_SUPPORT=y
CONFIG_ADC_ASYNC=y
CONFIG_ADC_NRFX_SAADC=y

----------------------- m_1st_channel_cfg

static const struct adc_channel_cfg m_1st_channel_cfg = {
	.gain = ADC_GAIN,
	.reference = ADC_REFERENCE,
	.acquisition_time = ADC_ACQUISITION_TIME,
	.channel_id = ADC_1ST_CHANNEL_ID,
#if defined(CONFIG_ADC_CONFIGURABLE_INPUTS)
	.input_positive = ADC_1ST_CHANNEL_INPUT,
#endif
};

---------------------- ADC Initializarion Part in main.c

	LOG_INF("Initialize ADC");
	adc_dev = device_get_binding("ADC_0");
	if (!adc_dev) {
		LOG_ERR("device_get_binding ADC_0 failed\n");
	}	else {LOG_INF("success");}

	LOG_INF("Initialize channel 0 ");
	err = adc_channel_setup(adc_dev, &m_1st_channel_cfg);
	if (err) {
		LOG_ERR("Error in adc channel 0 setup: %d\n", err);
		k_sleep(K_MSEC(100));
	}	else {LOG_INF("success");}

---------------------- read_adc_channel functions

static int read_adc_channel(uint8_t adc_channel)
{
	int ret;
	LOG_INF("Reading channel %d", adc_channel);
	if (adc_channel == 0) {
		const struct adc_sequence sequence = {
			.options = &sequence_opts,
			.channels = BIT(ADC_1ST_CHANNEL_ID),
			.buffer = m_sample_buffer,
			.buffer_size = sizeof(m_sample_buffer),
			.resolution = ADC_RESOLUTION,
		};
		ret = adc_read(adc_dev, &sequence);
	}

	if (!adc_dev) {
		LOG_ERR("Error on reading ADC");
		return -1;
	}
	
	LOG_INF("\n Measured voltage: ");
	for (int i = 0; i < BUFFER_SIZE; i++) {
		float adc_voltage = 0;
		adc_voltage = (float)(((float)m_sample_buffer[i] / 1023.0f) *
				      3600.0f);
		LOG_INF("%f ",adc_voltage);
	}
	LOG_INF("\n");
	LOG_INF("Done reading ADC");

	return ret;
}

---------------------- main loop in main.c

	for (;;) {
		k_sleep(K_MSEC(100));
		
		if (responding == 0) {
			char idle_msg[4] = {'test'};
			bt_service_send(NULL, idle_msg, sizeof(idle_msg));
			responding = -1;
		} else if (responding == 1) {
			send_dummy_data();
			// for (int i = 0; i < 4; i++) {
			// 	read_adc_channel(i);
			// 	send_channel_data();
			// }
			responding = -1;
		} else if (responding == 2) {
			read_adc_channel(0); // read channel 0
			responding = -1;
		} else if (responding == 3) {
			// read_adc_channel(1);
			responding = -1;
		} else if (responding == 4) {
			// read_adc_channel(2);
			responding = -1;
		} else if (responding == 5) {
			// read_adc_channel(3);
			responding = -1;
		}
	}

And below is the result for that view using RTT Jlink

<inf> peripheral_uart: Init fw
[00:00:00.257,415] <inf> peripheral_uart: Initializing BT
[00:00:00.262,969] <inf> fs_nvs: 2 Sectors of 4096 bytes
[00:00:00.262,969] <inf> fs_nvs: alloc wra: 0, fc8
[00:00:00.263,000] <inf> fs_nvs: data wra: 0, 2c
[00:00:00.283,569] <inf> bt_hci_core: No ID address. App must call settings_load()
[00:00:00.283,569] <inf> peripheral_uart: success
[00:00:00.283,599] <inf> peripheral_uart: Initializing Settings
[00:00:00.284,637] <inf> peripheral_uart: Initializing BUS BLE
[00:00:00.284,637] <inf> peripheral_uart: success
[00:00:00.284,667] <inf> peripheral_uart: Advertizing BLE
[00:00:00.287,048] <inf> peripheral_uart: success
[00:00:00.287,048] <inf> peripheral_uart: Initialize ADC
[00:00:00.287,078] <inf> peripheral_uart: success
[00:00:00.287,078] <inf> peripheral_uart: Initialize channel 0 
[00:00:00.287,109] <err> [00:01:31.649,993] <inf> peripheral_uart: Connected 65:FD:AF:5C:6C:C3 (random)
[00:01:42.755,645] <inf> peripheral_uart: Received data from: 65:FD:AF:5C:6C:C3 (random)
[00:01:49.360,748] <inf> peripheral_uart: Reading channel 0
[00:01:49.360,778] <err> adc_nrfx_saadc: Channel 0 not configured
[00:01:49.360,778] <inf> peripheral_uart: 
 Measured voltage: 
[00:01:49.360,809] <inf> peripheral_uart: 0.000000 
[00:01:49.360,809] <inf> peripheral_uart: 

[00:01:49.360,839] <inf> peripheral_uart: Done reading ADC

So I think the issue was from err = adc_channel_setup(adc_dev, &m_1st_channel_cfg); but on RTT Viewer, I can't check the error number, it only prints err without let me know the error number. Anybody have idea about this? Thank you.

Related