feat: 實作 builtin_adc driver

This commit is contained in:
chain40
2024-03-30 20:52:12 +08:00
parent 3741c79e97
commit 3522da08b0
5 changed files with 106 additions and 3 deletions
+2 -1
View File
@@ -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)
+11 -2
View File
@@ -111,11 +111,12 @@
<MaxBreakpointLimit>0</MaxBreakpointLimit>
<EnableVerboseMode>true</EnableVerboseMode>
<EnablePrettyPrinters>false</EnablePrettyPrinters>
<EnableAbsolutePathReporting>true</EnableAbsolutePathReporting>
</AdditionalGDBSettings>
<DebugMethod>
<ID>jlink-jtag</ID>
<InterfaceID>com.sysprogs.debug.jlink.jlinksw</InterfaceID>
<InterfaceSerialNumber>000682409936</InterfaceSerialNumber>
<InterfaceSerialNumber>000682238881</InterfaceSerialNumber>
<Configuration xsi:type="com.visualgdb.edp.segger.settings">
<CommandLine>-select USB -device $$SYS:MCU_ID$$ -speed auto -if SWD</CommandLine>
<ProgramMode>Enabled</ProgramMode>
@@ -140,9 +141,17 @@
<EnableVirtualHalts>false</EnableVirtualHalts>
<DynamicAnalysisSettings />
<EndOfStackSymbol>_estack</EndOfStackSymbol>
<TimestampProviderTicksPerSecond>0</TimestampProviderTicksPerSecond>
<TimestampProvider>com.sysprogs.arm.dwt</TimestampProvider>
<TimestampProviderTicksPerSecond>64000000</TimestampProviderTicksPerSecond>
<KeepConsoleAfterExit>false</KeepConsoleAfterExit>
<UnusedStackFillPattern xsi:nil="true" />
<RelatedExecutables>
<RelatedExecutable>
<Program>false</Program>
<LoadSymbols>false</LoadSymbols>
<ShowInLiveWatch>false</ShowInLiveWatch>
</RelatedExecutable>
</RelatedExecutables>
<CheckInterfaceDrivers>true</CheckInterfaceDrivers>
</Debug>
</VisualGDBProjectSettings2>
+2
View File
@@ -201,6 +201,7 @@
<ClCompile Include="le_gatt.c" />
<ClCompile Include="le_srv.c" />
<ClCompile Include="main.c" />
<ClCompile Include="builtin_saadc.c.c" />
<ClCompile Include="sw_drv.c" />
<ClCompile Include="syscalls.c" />
<None Include="nRF52811_XXAA_s140.lds" />
@@ -363,6 +364,7 @@
<ClInclude Include="elite_pin_ctrl.h" />
<ClInclude Include="FreeRTOSConfig.h" />
<ClInclude Include="elite_def.h" />
<ClInclude Include="builtin_saadc.h" />
<ClInclude Include="led_drv.h" />
<ClInclude Include="led_drv_if.h" />
<ClInclude Include="max5136.h" />
+82
View File
@@ -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
};
+9
View File
@@ -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__