25
25
#define ADC_12BIT_RANGE 0xFFF
26
26
#define ADC_RANGE ADC_12BIT_RANGE
27
27
28
- void analog_in_event_handler (nrf_drv_saadc_evt_t const * p_event )// type of nrf_drv_saadc_event_handler_t
28
+ static void analog_in_event_handler (nrf_drv_saadc_evt_t const * p_event )// type of nrf_drv_saadc_event_handler_t
29
29
{
30
30
(void ) p_event ;
31
31
}
32
32
33
- const nrf_drv_saadc_config_t saadc_config =
33
+ static const nrf_drv_saadc_config_t saadc_config =
34
34
{
35
35
.resolution = NRF_SAADC_RESOLUTION_12BIT ,
36
36
.oversample = NRF_SAADC_OVERSAMPLE_DISABLED ,
@@ -39,7 +39,6 @@ const nrf_drv_saadc_config_t saadc_config =
39
39
40
40
void analogin_init (analogin_t * obj , PinName pin )
41
41
{
42
- /// @todo check if initialized
43
42
ret_code_t ret_code ;
44
43
45
44
ret_code = nrf_drv_saadc_init (& saadc_config , analog_in_event_handler );
@@ -52,13 +51,17 @@ void analogin_init(analogin_t *obj, PinName pin)
52
51
obj -> adc_pin = saadcIn - 1 ;
53
52
54
53
nrf_saadc_channel_config_t channel_config =
55
- NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE (saadcIn );
54
+ NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE (saadcIn ); //Single ended, negative input to ADC shorted to GND.
56
55
57
56
ret_code = nrf_drv_saadc_channel_init (obj -> adc_pin , & channel_config );
58
57
MBED_ASSERT (ret_code == NRF_SUCCESS );
59
58
}
60
59
61
- int16_t analogin_read_i16 (analogin_t * obj )
60
+ /**
61
+ * NRF52 SAR ADC module provides measurement with sign.
62
+ * Already mbed API dosn't support readout of signed integer value.
63
+ */
64
+ static int16_t analogin_read_i16 (analogin_t * obj )
62
65
{
63
66
nrf_saadc_value_t adc_value ;
64
67
ret_code_t ret_code ;
@@ -69,6 +72,23 @@ int16_t analogin_read_i16(analogin_t *obj)
69
72
return adc_value ;
70
73
}
71
74
75
+ uint16_t analogin_read_u16 (analogin_t * obj )
76
+ {
77
+ int16_t adc_value ;
78
+
79
+ adc_value = analogin_read_i16 (obj );
80
+
81
+ if (adc_value < 0 )
82
+ {
83
+ // Even in the single ended mode measured value can be {-0}. Saturation for avoid casting to a big integer.
84
+ return 0 ;
85
+ }
86
+ else
87
+ {
88
+ return (uint16_t ) adc_value ;
89
+ }
90
+ }
91
+
72
92
float analogin_read (analogin_t * obj )
73
93
{
74
94
int16_t value = analogin_read_i16 (obj );
0 commit comments