rt-thread/bsp/nrf5x/libraries/drivers/drv_adc.c

283 lines
8.0 KiB
C
Raw Normal View History

2020-08-18 22:11:50 +08:00
/*
2021-03-14 15:18:33 +08:00
* Copyright (c) 2006-2021, RT-Thread Development Team
2020-08-18 22:11:50 +08:00
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-08-18 guohp1128 the first version
*/
#include "drv_adc.h"
#ifdef RT_USING_ADC
struct rt_adc_device nrf5x_adc_device;
drv_nrfx_saadc_result_t results;
nrf_saadc_value_t result_buff_cache[8];
static void nrf5x_saadc_event_hdr(nrfx_saadc_evt_t const * p_event)
{
uint8_t i,j;
if(p_event->type == NRFX_SAADC_EVT_DONE)
{
j = 0;
for(i = 0; i < 8; i++)
{
if(results.channels[i].channel_index == i)
{
results.result_buffer[i] = result_buff_cache[j];
j ++;
}
}
2021-03-14 15:18:33 +08:00
results.done = 1;
2020-08-18 22:11:50 +08:00
}
}
static uint32_t get_channels_mask(void)
{
uint8_t i;
uint32_t mask = 0;
for(i = 0; i < 8; i++)
{
if(results.channels[i].channel_index != 0xff)
{
mask |= (1 << results.channels[i].channel_index);
}
}
return mask;
}
static void set_channels(drv_nrfx_saadc_channel_t * channel)
{
uint8_t i;
if(channel -> mode == NRF_SAADC_MODE_SINGLE_ENDED)
{
results.channels[channel->channel_num] = (nrfx_saadc_channel_t)NRFX_SAADC_DEFAULT_CHANNEL_SE(channel -> pin_p + 1, channel -> channel_num);
}
else if(channel -> mode == NRF_SAADC_MODE_DIFFERENTIAL)
{
results.channels[channel->channel_num] = (nrfx_saadc_channel_t)NRFX_SAADC_DEFAULT_CHANNEL_DIFFERENTIAL(channel -> pin_p + 1, channel -> pin_n + 1, channel -> channel_num);
}
results.channel_count = 0;
for(i = 0; i < 8; i++)
{
if(results.channels[i].channel_index != 0xff)
{
results.channel_count ++;
}
}
}
/* channel: 0-7 */
static rt_err_t nrf5x_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
2021-03-14 15:18:33 +08:00
{
nrfx_err_t err_code = NRFX_SUCCESS;
2020-08-18 22:11:50 +08:00
uint8_t i,j;
if (enabled)
2021-03-14 15:18:33 +08:00
{
2020-08-18 22:11:50 +08:00
RT_ASSERT(device != RT_NULL);
RT_ASSERT(device->parent.user_data != RT_NULL);
2021-03-14 15:18:33 +08:00
2020-08-18 22:11:50 +08:00
drv_nrfx_saadc_channel_t * drv_channel_config = NULL;
2021-03-14 15:18:33 +08:00
drv_channel_config = (drv_nrfx_saadc_channel_t *)device->parent.user_data;
2020-08-18 22:11:50 +08:00
set_channels(drv_channel_config);
2021-03-14 15:18:33 +08:00
2020-08-18 22:11:50 +08:00
nrfx_saadc_channel_t channels_cache[results.channel_count];
2021-03-14 15:18:33 +08:00
2020-08-18 22:11:50 +08:00
j = 0;
for(i = 0; i < 8; i++)
{
if(results.channels[i].channel_index != 0xff)
{
channels_cache[j] = results.channels[i];
j ++;
}
}
2021-03-14 15:18:33 +08:00
2020-08-18 22:11:50 +08:00
err_code = nrfx_saadc_channels_config(channels_cache,results.channel_count);
err_code = nrfx_saadc_simple_mode_set(get_channels_mask(),
NRF_SAADC_RESOLUTION_12BIT,
NRF_SAADC_OVERSAMPLE_DISABLED,
nrf5x_saadc_event_hdr);
2021-03-14 15:18:33 +08:00
err_code = nrfx_saadc_buffer_set(result_buff_cache, results.channel_count);
2020-08-18 22:11:50 +08:00
}
else
{
results.channels[channel].channel_index = 0xff;
2021-03-14 15:18:33 +08:00
2020-08-18 22:11:50 +08:00
results.channel_count = 0;
for(i = 0; i < 8; i++)
{
if(results.channels[i].channel_index != 0xff)
{
results.channel_count ++;
}
}
2021-03-14 15:18:33 +08:00
2020-08-18 22:11:50 +08:00
if(results.channel_count == 0)
{
nrfx_saadc_channel_t channels_cache[1];
err_code = nrfx_saadc_channels_config(channels_cache, 0);
return err_code;
}
else
{
nrfx_saadc_channel_t channels_cache[results.channel_count];
2021-03-14 15:18:33 +08:00
2020-08-18 22:11:50 +08:00
j = 0;
for(i = 0; i < 8; i++)
{
if(results.channels[i].channel_index != 0xff)
{
channels_cache[j] = results.channels[i];
j ++;
}
}
2021-03-14 15:18:33 +08:00
2020-08-18 22:11:50 +08:00
err_code = nrfx_saadc_channels_config(channels_cache,results.channel_count);
err_code = nrfx_saadc_simple_mode_set(get_channels_mask(),
NRF_SAADC_RESOLUTION_12BIT,
NRF_SAADC_OVERSAMPLE_DISABLED,
nrf5x_saadc_event_hdr);
2021-03-14 15:18:33 +08:00
2020-08-18 22:11:50 +08:00
err_code = nrfx_saadc_buffer_set(result_buff_cache, results.channel_count);
}
}
2021-03-14 15:18:33 +08:00
return err_code;
2020-08-18 22:11:50 +08:00
}
static rt_err_t nrf5x_get_adc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
{
2021-03-14 15:18:33 +08:00
nrfx_err_t err_code = NRFX_SUCCESS;
2020-08-18 22:11:50 +08:00
if (results.channels[channel].channel_index != 0xff)
{
results.done = 0;
err_code = nrfx_saadc_mode_trigger();
while(results.done == 0)
{
;
}
* value = results.result_buffer[channel];
results.done = 0;
}
2021-03-14 15:18:33 +08:00
2020-08-18 22:11:50 +08:00
return err_code;
}
static const struct rt_adc_ops nrf5x_adc_ops =
{
.enabled = nrf5x_adc_enabled,
.convert = nrf5x_get_adc_value,
};
int rt_hw_adc_init(void)
{
int result = RT_EOK;
uint8_t i;
2020-11-23 00:08:24 +08:00
char name_buf[6] = ADC_NAME;
2021-03-14 15:18:33 +08:00
2020-08-18 22:11:50 +08:00
for(i = 0; i < 8; i++)
{
results.channels[i].channel_index = 0xff;
results.result_buffer[i] = 0;
results.channel_count = 0;
results.done = 0;
}
2021-03-14 15:18:33 +08:00
2020-08-18 22:11:50 +08:00
/* initializing SAADC interrupt priority */
if (nrfx_saadc_init(NRFX_SAADC_CONFIG_IRQ_PRIORITY) != NRFX_SUCCESS)
{
rt_kprintf("%s init failed", name_buf);
rt_kprintf("The driver is already initialized.");
result = -RT_ERROR;
}
else
{
/* register ADC device */
if (rt_hw_adc_register(&nrf5x_adc_device, name_buf, &nrf5x_adc_ops, nrf5x_adc_device.parent.user_data) == RT_EOK)
{
rt_kprintf("%s init success", name_buf);
}
else
{
rt_kprintf("%s register failed", name_buf);
result = -RT_ERROR;
}
}
return result;
}
INIT_BOARD_EXPORT(rt_hw_adc_init);
/*test saadc*/
#include <drv_adc.h>
2020-11-23 00:08:24 +08:00
#define SAMPLE_ADC_MODE_SINGLE_ENDED 0 //single-ended mode
#define SAMPLE_ADC_MODE_DIFFERENTIAL 1 //differential mode
#define SAMPLE_ADC_AIN1 1
#define SAMPLE_ADC_AIN2 2
#define SAMPLE_ADC_AIN7 7
#define SAMPLE_ADC_AIN_NC 0 //disable input of AINx
#define SAMPLE_ADC_CHANNEL_0 0
#define SAMPLE_ADC_CHANNEL_1 1
#define SAMPLE_ADC_CHANNEL_5 5
2020-08-18 22:11:50 +08:00
void saadc_sample(void)
{
drv_nrfx_saadc_channel_t channel_config;
2021-03-14 15:18:33 +08:00
rt_uint32_t result;
2020-08-18 22:11:50 +08:00
rt_adc_device_t adc_dev;
2020-11-23 00:08:24 +08:00
adc_dev = (rt_adc_device_t)rt_device_find(ADC_NAME);
2020-08-18 22:11:50 +08:00
adc_dev->parent.user_data = &channel_config;
2021-03-14 15:18:33 +08:00
channel_config = (drv_nrfx_saadc_channel_t){.mode = SAMPLE_ADC_MODE_SINGLE_ENDED,
.pin_p = SAMPLE_ADC_AIN1,
.pin_n = SAMPLE_ADC_AIN_NC,
2020-11-23 00:08:24 +08:00
.channel_num = SAMPLE_ADC_CHANNEL_0};
2020-08-18 22:11:50 +08:00
rt_adc_enable(adc_dev, channel_config.channel_num);
2021-03-14 15:18:33 +08:00
channel_config = (drv_nrfx_saadc_channel_t){.mode = SAMPLE_ADC_MODE_SINGLE_ENDED,
.pin_p = SAMPLE_ADC_AIN2,
.pin_n = SAMPLE_ADC_AIN_NC,
2020-11-23 00:08:24 +08:00
.channel_num = SAMPLE_ADC_CHANNEL_1};
2020-08-18 22:11:50 +08:00
rt_adc_enable(adc_dev, channel_config.channel_num);
2021-03-14 15:18:33 +08:00
channel_config = (drv_nrfx_saadc_channel_t){.mode = SAMPLE_ADC_MODE_SINGLE_ENDED,
.pin_p = SAMPLE_ADC_AIN7,
.pin_n = SAMPLE_ADC_AIN_NC,
2020-11-23 00:08:24 +08:00
.channel_num = SAMPLE_ADC_CHANNEL_5};
2020-08-18 22:11:50 +08:00
rt_adc_enable(adc_dev, channel_config.channel_num);
2021-03-14 15:18:33 +08:00
int count = 1;
2020-08-18 22:11:50 +08:00
while(count++)
{
result = rt_adc_read(adc_dev, 0);
rt_kprintf("saadc channel 0 value = %d, ",result);
2021-03-14 15:18:33 +08:00
2020-08-18 22:11:50 +08:00
result = rt_adc_read(adc_dev, 1);
rt_kprintf("saadc channel 1 value = %d, ",result);
2021-03-14 15:18:33 +08:00
2020-08-18 22:11:50 +08:00
result = rt_adc_read(adc_dev, 5);
2021-03-14 15:18:33 +08:00
rt_kprintf("saadc channel 5 value = %d",result);
rt_kprintf("\r\n");
2020-08-18 22:11:50 +08:00
rt_thread_mdelay(1000);
}
}
MSH_CMD_EXPORT(saadc_sample, saadc sample);
#endif /* RT_USING_ADC */