|
17 | 17 | #include "analogin_api.h"
|
18 | 18 | #include "cmsis.h"
|
19 | 19 | #include "pinmap.h"
|
| 20 | +#include "app_util_platform.h" |
| 21 | +#include "nrf_drv_saadc.h" |
20 | 22 |
|
21 | 23 | #ifdef DEVICE_ANALOGIN
|
22 | 24 |
|
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 |
26 | 27 |
|
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 |
35 | 38 | };
|
36 | 39 |
|
37 | 40 | void analogin_init(analogin_t *obj, PinName pin)
|
38 | 41 | {
|
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); |
63 | 59 | }
|
64 | 60 |
|
65 |
| -uint16_t analogin_read_u16(analogin_t *obj) |
| 61 | +int16_t analogin_read_i16(analogin_t *obj) |
66 | 62 | {
|
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; |
78 | 70 | }
|
79 | 71 |
|
80 | 72 | float analogin_read(analogin_t *obj)
|
81 | 73 | {
|
82 |
| - uint16_t value = analogin_read_u16(obj); |
| 74 | + int16_t value = analogin_read_i16(obj); |
83 | 75 | return (float)value * (1.0f / (float)ADC_RANGE);
|
84 | 76 | }
|
85 | 77 |
|
|
0 commit comments