From 3522da08b0441faea107343e5f74a9bf67e1a804 Mon Sep 17 00:00:00 2001 From: chain40 Date: Sat, 30 Mar 2024 20:52:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AF=A6=E4=BD=9C=20=20builtin=5Fadc?= =?UTF-8?q?=20driver?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- adc_drv.c | 3 +- bmd380_peripheral-Debug.vgdbsettings | 13 ++++- bmd380_peripheral.vcxproj | 2 + builtin_saadc.c.c | 82 ++++++++++++++++++++++++++++ builtin_saadc.h | 9 +++ 5 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 builtin_saadc.c.c create mode 100644 builtin_saadc.h diff --git a/adc_drv.c b/adc_drv.c index 6d9d50b..c77c51e 100644 --- a/adc_drv.c +++ b/adc_drv.c @@ -4,7 +4,8 @@ extern adc_drv_if_t ads8691; static adc_drv_if_t *p_inst = &ads8691; #else -static adc_drv_if_t *p_inst = NULL; +#include "builtin_saadc.h" +static const adc_drv_if_t *p_inst = &builtin_saadc; #endif int adc_init(void) diff --git a/bmd380_peripheral-Debug.vgdbsettings b/bmd380_peripheral-Debug.vgdbsettings index be17c3c..206b6d0 100644 --- a/bmd380_peripheral-Debug.vgdbsettings +++ b/bmd380_peripheral-Debug.vgdbsettings @@ -111,11 +111,12 @@ 0 true false + true jlink-jtag com.sysprogs.debug.jlink.jlinksw - 000682409936 + 000682238881 -select USB -device $$SYS:MCU_ID$$ -speed auto -if SWD Enabled @@ -140,9 +141,17 @@ false _estack - 0 + com.sysprogs.arm.dwt + 64000000 false + + + false + false + false + + true \ No newline at end of file diff --git a/bmd380_peripheral.vcxproj b/bmd380_peripheral.vcxproj index 2242d15..a1445e5 100644 --- a/bmd380_peripheral.vcxproj +++ b/bmd380_peripheral.vcxproj @@ -201,6 +201,7 @@ + @@ -363,6 +364,7 @@ + diff --git a/builtin_saadc.c.c b/builtin_saadc.c.c new file mode 100644 index 0000000..25c69da --- /dev/null +++ b/builtin_saadc.c.c @@ -0,0 +1,82 @@ +#include "builtin_saadc.h" +#include "nrf_saadc.h" + +static uint32_t m_gain = NRF_SAADC_GAIN1_6; + +static int init(void) +{ + NRF_SAADC->ENABLE = 0; + NRF_SAADC->TASKS_STOP = 1; + NRF_SAADC->RESOLUTION = SAADC_RESOLUTION_VAL_14bit; + NRF_SAADC->OVERSAMPLE = NRF_SAADC_OVERSAMPLE_4X; + return 0; +} + +static int reset(void) +{ + // Do nothing... + return 0; +} + +static int read(uint32_t channel, int32_t *adc_val) +{ + NRF_SAADC->TASKS_STOP = 1; + NRF_SAADC->ENABLE = 0; + NRF_SAADC->CH[0].PSELP = NRF_SAADC_INPUT_AIN0 + channel; + NRF_SAADC->CH[0].PSELN = NRF_SAADC_INPUT_DISABLED; + NRF_SAADC->CH[0].CONFIG = + (NRF_SAADC_RESISTOR_DISABLED << SAADC_CH_CONFIG_RESP_Pos) | + (NRF_SAADC_RESISTOR_DISABLED << SAADC_CH_CONFIG_RESN_Pos) | + (m_gain << SAADC_CH_CONFIG_GAIN_Pos) | + (NRF_SAADC_REFERENCE_INTERNAL << SAADC_CH_CONFIG_REFSEL_Pos) | + (NRF_SAADC_ACQTIME_40US << SAADC_CH_CONFIG_TACQ_Pos) | + (NRF_SAADC_MODE_SINGLE_ENDED << SAADC_CH_CONFIG_MODE_Pos) | + (NRF_SAADC_BURST_ENABLED << SAADC_CH_CONFIG_BURST_Pos); + NRF_SAADC->ENABLE = 1; + + uint16_t val = 0; + NRF_SAADC->RESULT.PTR = (uint32_t)&val; + NRF_SAADC->RESULT.MAXCNT = 1; + NRF_SAADC->EVENTS_RESULTDONE = 0; + NRF_SAADC->TASKS_START = 1; + NRF_SAADC->TASKS_SAMPLE = 1; + do { + } while (NRF_SAADC->EVENTS_RESULTDONE == 0); + *adc_val = val; + return 0; +} + +static int gain(adc_gain_t gain) +{ + int ret; + switch (gain) + { + case P_GAIN_3P000: + m_gain = SAADC_CH_CONFIG_GAIN_Gain1_2; + ret = 0; + break; + case P_GAIN_1P500: + m_gain = SAADC_CH_CONFIG_GAIN_Gain1_4; + ret = 0; + break; + case P_GAIN_2P500: + case P_GAIN_1P250: + case NP_GAIN_3P000: + case NP_GAIN_2P500: + case NP_GAIN_1P500: + case NP_GAIN_1P250: + case NP_GAIN_0P625: + default: + m_gain = SAADC_CH_CONFIG_GAIN_Gain1_6; + ret = -1; + break; + } + return ret; +} + +const adc_drv_if_t builtin_saadc = { + .init = init, + .reset = reset, + .read = read, + .gain = gain +}; diff --git a/builtin_saadc.h b/builtin_saadc.h new file mode 100644 index 0000000..7c8d354 --- /dev/null +++ b/builtin_saadc.h @@ -0,0 +1,9 @@ +#pragma once +#ifndef __BUILTIN_SAADC_H__ +#define __BUILTIN_SAADC_H__ + +#include "adc_drv_if.h" + +extern const adc_drv_if_t builtin_saadc; + +#endif // !__BUILTIN_SAADC_H__