Mouse code for the MacroRat
mbed-dev/targets/TARGET_NORDIC/TARGET_NRF5/TARGET_MCU_NRF52832/analogin_api.c@18:6a4db94011d3, 2017-05-14 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sun May 14 23:18:57 2017 +0000
- Revision:
- 18:6a4db94011d3
Publishing again
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /* mbed Microcontroller Library |
sahilmgandhi | 18:6a4db94011d3 | 2 | * Copyright (c) 2006-2013 ARM Limited |
sahilmgandhi | 18:6a4db94011d3 | 3 | * |
sahilmgandhi | 18:6a4db94011d3 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sahilmgandhi | 18:6a4db94011d3 | 5 | * you may not use this file except in compliance with the License. |
sahilmgandhi | 18:6a4db94011d3 | 6 | * You may obtain a copy of the License at |
sahilmgandhi | 18:6a4db94011d3 | 7 | * |
sahilmgandhi | 18:6a4db94011d3 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sahilmgandhi | 18:6a4db94011d3 | 9 | * |
sahilmgandhi | 18:6a4db94011d3 | 10 | * Unless required by applicable law or agreed to in writing, software |
sahilmgandhi | 18:6a4db94011d3 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sahilmgandhi | 18:6a4db94011d3 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sahilmgandhi | 18:6a4db94011d3 | 13 | * See the License for the specific language governing permissions and |
sahilmgandhi | 18:6a4db94011d3 | 14 | * limitations under the License. |
sahilmgandhi | 18:6a4db94011d3 | 15 | */ |
sahilmgandhi | 18:6a4db94011d3 | 16 | |
sahilmgandhi | 18:6a4db94011d3 | 17 | #include "mbed_assert.h" |
sahilmgandhi | 18:6a4db94011d3 | 18 | #include "analogin_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 19 | #include "cmsis.h" |
sahilmgandhi | 18:6a4db94011d3 | 20 | #include "pinmap.h" |
sahilmgandhi | 18:6a4db94011d3 | 21 | #include "app_util_platform.h" |
sahilmgandhi | 18:6a4db94011d3 | 22 | #include "nrf_drv_saadc.h" |
sahilmgandhi | 18:6a4db94011d3 | 23 | |
sahilmgandhi | 18:6a4db94011d3 | 24 | #ifdef DEVICE_ANALOGIN |
sahilmgandhi | 18:6a4db94011d3 | 25 | |
sahilmgandhi | 18:6a4db94011d3 | 26 | #define ADC_12BIT_RANGE 0xFFF |
sahilmgandhi | 18:6a4db94011d3 | 27 | #define ADC_RANGE ADC_12BIT_RANGE |
sahilmgandhi | 18:6a4db94011d3 | 28 | |
sahilmgandhi | 18:6a4db94011d3 | 29 | static void analog_in_event_handler(nrf_drv_saadc_evt_t const *p_event)// type of nrf_drv_saadc_event_handler_t |
sahilmgandhi | 18:6a4db94011d3 | 30 | { |
sahilmgandhi | 18:6a4db94011d3 | 31 | (void) p_event; |
sahilmgandhi | 18:6a4db94011d3 | 32 | } |
sahilmgandhi | 18:6a4db94011d3 | 33 | |
sahilmgandhi | 18:6a4db94011d3 | 34 | static const nrf_drv_saadc_config_t saadc_config = |
sahilmgandhi | 18:6a4db94011d3 | 35 | { |
sahilmgandhi | 18:6a4db94011d3 | 36 | .resolution = NRF_SAADC_RESOLUTION_12BIT, |
sahilmgandhi | 18:6a4db94011d3 | 37 | .oversample = NRF_SAADC_OVERSAMPLE_DISABLED, |
sahilmgandhi | 18:6a4db94011d3 | 38 | .interrupt_priority = SAADC_CONFIG_IRQ_PRIORITY |
sahilmgandhi | 18:6a4db94011d3 | 39 | }; |
sahilmgandhi | 18:6a4db94011d3 | 40 | |
sahilmgandhi | 18:6a4db94011d3 | 41 | void SAADC_IRQHandler(void); |
sahilmgandhi | 18:6a4db94011d3 | 42 | |
sahilmgandhi | 18:6a4db94011d3 | 43 | void analogin_init(analogin_t *obj, PinName pin) |
sahilmgandhi | 18:6a4db94011d3 | 44 | { |
sahilmgandhi | 18:6a4db94011d3 | 45 | ret_code_t ret_code; |
sahilmgandhi | 18:6a4db94011d3 | 46 | |
sahilmgandhi | 18:6a4db94011d3 | 47 | NVIC_SetVector(SAADC_IRQn, (uint32_t)SAADC_IRQHandler); |
sahilmgandhi | 18:6a4db94011d3 | 48 | |
sahilmgandhi | 18:6a4db94011d3 | 49 | ret_code = nrf_drv_saadc_init(&saadc_config, analog_in_event_handler); |
sahilmgandhi | 18:6a4db94011d3 | 50 | MBED_ASSERT(((ret_code == NRF_SUCCESS) || (ret_code == NRF_ERROR_INVALID_STATE))); //NRF_ERROR_INVALID_STATE expected for multiple channels used. |
sahilmgandhi | 18:6a4db94011d3 | 51 | |
sahilmgandhi | 18:6a4db94011d3 | 52 | uint8_t saadcIn = nrf_drv_saadc_gpio_to_ain(pin); |
sahilmgandhi | 18:6a4db94011d3 | 53 | MBED_ASSERT(saadcIn != NRF_SAADC_INPUT_DISABLED); |
sahilmgandhi | 18:6a4db94011d3 | 54 | |
sahilmgandhi | 18:6a4db94011d3 | 55 | obj->adc = ADC0_0; // only one instance of ADC in nRF52 SoC |
sahilmgandhi | 18:6a4db94011d3 | 56 | obj->adc_pin = saadcIn - 1; |
sahilmgandhi | 18:6a4db94011d3 | 57 | |
sahilmgandhi | 18:6a4db94011d3 | 58 | nrf_saadc_channel_config_t channel_config = |
sahilmgandhi | 18:6a4db94011d3 | 59 | NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(saadcIn); //Single ended, negative input to ADC shorted to GND. |
sahilmgandhi | 18:6a4db94011d3 | 60 | |
sahilmgandhi | 18:6a4db94011d3 | 61 | ret_code = nrf_drv_saadc_channel_init(obj->adc_pin, &channel_config); |
sahilmgandhi | 18:6a4db94011d3 | 62 | MBED_ASSERT(ret_code == NRF_SUCCESS); |
sahilmgandhi | 18:6a4db94011d3 | 63 | } |
sahilmgandhi | 18:6a4db94011d3 | 64 | |
sahilmgandhi | 18:6a4db94011d3 | 65 | |
sahilmgandhi | 18:6a4db94011d3 | 66 | uint16_t analogin_read_u16(analogin_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 67 | { |
sahilmgandhi | 18:6a4db94011d3 | 68 | int16_t adc_value; |
sahilmgandhi | 18:6a4db94011d3 | 69 | ret_code_t ret_code; |
sahilmgandhi | 18:6a4db94011d3 | 70 | |
sahilmgandhi | 18:6a4db94011d3 | 71 | ret_code = nrf_drv_saadc_sample_convert(obj->adc_pin, &adc_value); |
sahilmgandhi | 18:6a4db94011d3 | 72 | MBED_ASSERT(ret_code == NRF_SUCCESS); |
sahilmgandhi | 18:6a4db94011d3 | 73 | |
sahilmgandhi | 18:6a4db94011d3 | 74 | if (adc_value < 0) |
sahilmgandhi | 18:6a4db94011d3 | 75 | { |
sahilmgandhi | 18:6a4db94011d3 | 76 | // Even in the single ended mode measured value can be {-0}. Saturation for avoid casting to a big integer. |
sahilmgandhi | 18:6a4db94011d3 | 77 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 78 | } |
sahilmgandhi | 18:6a4db94011d3 | 79 | else |
sahilmgandhi | 18:6a4db94011d3 | 80 | { |
sahilmgandhi | 18:6a4db94011d3 | 81 | return (uint16_t) adc_value; |
sahilmgandhi | 18:6a4db94011d3 | 82 | } |
sahilmgandhi | 18:6a4db94011d3 | 83 | } |
sahilmgandhi | 18:6a4db94011d3 | 84 | |
sahilmgandhi | 18:6a4db94011d3 | 85 | float analogin_read(analogin_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 86 | { |
sahilmgandhi | 18:6a4db94011d3 | 87 | uint16_t value = analogin_read_u16(obj); |
sahilmgandhi | 18:6a4db94011d3 | 88 | return (float)value * (1.0f / (float)ADC_RANGE); |
sahilmgandhi | 18:6a4db94011d3 | 89 | } |
sahilmgandhi | 18:6a4db94011d3 | 90 | |
sahilmgandhi | 18:6a4db94011d3 | 91 | #endif // DEVICE_ANALOGIN |