Skip to content

Commit eef471b

Browse files
committed
AnalogIn api implenetation for nRF52
1 parent 5ba9392 commit eef471b

File tree

5 files changed

+1437
-48
lines changed

5 files changed

+1437
-48
lines changed

hal/targets/hal/TARGET_NORDIC/TARGET_NRF5/TARGET_MCU_NRF52832/analogin_api.c

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,69 +17,61 @@
1717
#include "analogin_api.h"
1818
#include "cmsis.h"
1919
#include "pinmap.h"
20+
#include "app_util_platform.h"
21+
#include "nrf_drv_saadc.h"
2022

2123
#ifdef DEVICE_ANALOGIN
2224

23-
#define ANALOGIN_MEDIAN_FILTER 1
24-
#define ADC_10BIT_RANGE 0x3FF
25-
#define ADC_RANGE ADC_10BIT_RANGE
25+
#define ADC_12BIT_RANGE 0xFFF
26+
#define ADC_RANGE ADC_12BIT_RANGE
2627

27-
static const PinMap PinMap_ADC[] = {
28-
{p1, ADC0_0, 4},
29-
{p2, ADC0_0, 8},
30-
{p3, ADC0_0, 16},
31-
{p4, ADC0_0, 32},
32-
{p5, ADC0_0, 64},
33-
{p6, ADC0_0, 128},
34-
{NC, NC, 0}
28+
void analog_in_event_handler(nrf_drv_saadc_evt_t const *p_event)// type of nrf_drv_saadc_event_handler_t
29+
{
30+
(void) p_event;
31+
}
32+
33+
const nrf_drv_saadc_config_t saadc_config =
34+
{
35+
.resolution = NRF_SAADC_RESOLUTION_12BIT,
36+
.oversample = NRF_SAADC_OVERSAMPLE_DISABLED,
37+
.interrupt_priority = APP_IRQ_PRIORITY_LOW
3538
};
3639

3740
void analogin_init(analogin_t *obj, PinName pin)
3841
{
39-
// TODO: usage on nrf52 ?
40-
#if 0
41-
int analogInputPin = 0;
42-
const PinMap *map = PinMap_ADC;
43-
44-
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); //(NRF_ADC_Type *)
45-
MBED_ASSERT(obj->adc != (ADCName)NC);
46-
47-
while (map->pin != NC) {
48-
if (map->pin == pin) {
49-
analogInputPin = map->function;
50-
break;
51-
}
52-
map++;
53-
}
54-
obj->adc_pin = (uint8_t)analogInputPin;
55-
56-
NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled;
57-
NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) |
58-
(ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) |
59-
(ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling << ADC_CONFIG_REFSEL_Pos) |
60-
(analogInputPin << ADC_CONFIG_PSEL_Pos) |
61-
(ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos);
62-
#endif
42+
/// @todo check if initialized
43+
ret_code_t ret_code;
44+
45+
ret_code = nrf_drv_saadc_init(&saadc_config, analog_in_event_handler);
46+
MBED_ASSERT(((ret_code == NRF_SUCCESS) || (ret_code == NRF_ERROR_INVALID_STATE))); //NRF_ERROR_INVALID_STATE expected for multiple channels used.
47+
48+
uint8_t saadcIn = nrf_drv_saadc_gpio_to_ain(pin);
49+
MBED_ASSERT(saadcIn != NRF_SAADC_INPUT_DISABLED);
50+
51+
obj->adc = ADC0_0; // only one instance of ADC in nRF52 SoC
52+
obj->adc_pin = saadcIn - 1;
53+
54+
nrf_saadc_channel_config_t channel_config =
55+
NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(saadcIn);
56+
57+
ret_code = nrf_drv_saadc_channel_init(obj->adc_pin, &channel_config);
58+
MBED_ASSERT(ret_code == NRF_SUCCESS);
6359
}
6460

65-
uint16_t analogin_read_u16(analogin_t *obj)
61+
int16_t analogin_read_i16(analogin_t *obj)
6662
{
67-
// TODO: usage on nrf52 ?
68-
#if 0
69-
NRF_ADC->CONFIG &= ~ADC_CONFIG_PSEL_Msk;
70-
NRF_ADC->CONFIG |= obj->adc_pin << ADC_CONFIG_PSEL_Pos;
71-
NRF_ADC->TASKS_START = 1;
72-
while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {
73-
}
74-
75-
return (uint16_t)NRF_ADC->RESULT; // 10 bit
76-
#endif
77-
return 0;
63+
nrf_saadc_value_t adc_value;
64+
ret_code_t ret_code;
65+
66+
ret_code = nrf_drv_saadc_sample_convert(obj->adc_pin, &adc_value);
67+
MBED_ASSERT(ret_code == NRF_SUCCESS);
68+
69+
return adc_value;
7870
}
7971

8072
float analogin_read(analogin_t *obj)
8173
{
82-
uint16_t value = analogin_read_u16(obj);
74+
int16_t value = analogin_read_i16(obj);
8375
return (float)value * (1.0f / (float)ADC_RANGE);
8476
}
8577

0 commit comments

Comments
 (0)