Skip to content

Commit bf1ac68

Browse files
dcgong2917jerpelea
authored andcommitted
arch/arm/rtl8721f: add ADC driver support
Wire the shared Ameba ADC driver into the RTL8721F build: add the per-chip ameba_adc_chip.h (12 channels, CH0..CH7 external on PA20,PA19,PA18,PA17,PA15,PA14,PA13,PA12, PINMUX function 5), the board ADC table and registration, the adc board config, and build glue for both cmake and make (including the fwlib RAM-layer ameba_adc.c in ameba_board.mk). Document the ADC on the board index. Signed-off-by: dechao_gong <dechao_gong@realsil.com.cn> Assisted-by: Claude <noreply@anthropic.com>
1 parent e5267f0 commit bf1ac68

11 files changed

Lines changed: 301 additions & 2 deletions

File tree

Documentation/platforms/arm/rtl8721f/boards/rtl8721f_evb/index.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Supported in this NuttX port:
4242
on the SDK fwlib register layer
4343
* PWM output exposed as a ``/dev/pwm0`` character device, driven directly on
4444
the SDK fwlib timer register layer
45+
* ADC channels exposed as an ``/dev/adc0`` character device, driven directly
46+
on the SDK fwlib register layer
4547

4648
Buttons and LEDs
4749
================
@@ -147,6 +149,21 @@ example::
147149

148150
nsh> pwm -d 25 -f 1000 # 1 kHz, 25% duty on /dev/pwm0
149151

152+
adc
153+
---
154+
155+
Minimal NSH with the ADC driver and the ``adc`` example enabled (no Wi-Fi).
156+
The board registers its channels from a table (see
157+
``boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_adc.c``): ``/dev/adc0`` samples
158+
CH0 on PA20 and CH1 on PA19. Edit that table -- channel numbers and the analog
159+
pad each is wired to -- to match a board's wiring; the external channels
160+
CH0..CH7 map to pads PA20,PA19,PA18,PA17,PA15,PA14,PA13,PA12 and are muxed to
161+
the ADC function through the SDK ROM, while internal channels carry
162+
``AMEBA_ADC_PIN_NC``. Every listed channel is sampled, in order, on each
163+
trigger. Read the channels with the example::
164+
165+
nsh> adc -n 1 # one sweep of /dev/adc0
166+
150167
nsh
151168
---
152169

arch/arm/src/rtl8721f/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ if(CONFIG_AMEBA_PWM)
6666
list(APPEND SRCS ${AMEBA_COMMON}/ameba_pwm.c)
6767
endif()
6868

69+
if(CONFIG_AMEBA_ADC)
70+
list(APPEND SRCS ${AMEBA_COMMON}/ameba_adc.c)
71+
endif()
72+
6973
target_include_directories(arch PRIVATE ${AMEBA_COMMON})
7074
target_sources(arch PRIVATE ${SRCS})
7175

@@ -145,6 +149,14 @@ if(CONFIG_AMEBA_PWM)
145149
list(APPEND AMEBA_FWLIB_SRCS ${AMEBA_SOC}/fwlib/ram_common/ameba_tim.c)
146150
endif()
147151

152+
# ADC (SAR) register layer. The ADC driver (arch/.../common/ameba/ameba_adc.c)
153+
# calls the fwlib ADC API; the data tables and helpers it indexes live in this
154+
# RAM source and must be compiled in (--gc-sections drops the unused
155+
# interrupt/timer-trigger helpers).
156+
if(CONFIG_AMEBA_ADC)
157+
list(APPEND AMEBA_FWLIB_SRCS ${AMEBA_SOC}/fwlib/ram_common/ameba_adc.c)
158+
endif()
159+
148160
# Silence a couple of warnings the vendored SDK sources trip under NuttX's
149161
# warning set, scoped to this fwlib compile only (never relaxing NuttX's own):
150162
# -Wno-int-conversion: the SDK passes NULL to irq_register()'s u32 "Data"

arch/arm/src/rtl8721f/Make.defs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ ifeq ($(CONFIG_AMEBA_PWM),y)
7676
CHIP_CSRCS += ameba_pwm.c
7777
endif
7878

79+
ifeq ($(CONFIG_AMEBA_ADC),y)
80+
CHIP_CSRCS += ameba_adc.c
81+
endif
82+
7983
############################################################################
8084
# Realtek RTL8721F SDK integration
8185
#
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/****************************************************************************
2+
* arch/arm/src/rtl8721f/ameba_adc_chip.h
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
#ifndef __ARCH_ARM_SRC_RTL8721F_AMEBA_ADC_CHIP_H
24+
#define __ARCH_ARM_SRC_RTL8721F_AMEBA_ADC_CHIP_H
25+
26+
/****************************************************************************
27+
* Included Files
28+
****************************************************************************/
29+
30+
#include <nuttx/config.h>
31+
32+
#include <stdint.h>
33+
34+
/****************************************************************************
35+
* Pre-processor Definitions
36+
****************************************************************************/
37+
38+
/* Per-chip ADC wiring for RTL8721F (amebagreen2). The shared driver
39+
* (arch/arm/src/common/ameba/ameba_adc.c) includes this header to learn how
40+
* many channels the converter can list, the crossbar pad-mux code that turns
41+
* a pad into an analog input, and the peripheral-clock masks. A port to
42+
* another Ameba chip supplies a same-named header on the chip include path;
43+
* the shared driver is never edited -- it reads only the macros below, and
44+
* it drives the ADC through the SDK fwlib API, which internally selects the
45+
* secure/non-secure register alias (via TrustZone_IsSecure()), so no
46+
* register base appears here at all.
47+
*
48+
* Values below are taken from the amebagreen2 fwlib headers (verified, not
49+
* guessed): PINMUX_FUNCTION_ADC=5 (ameba_pinmux.h), ADC_IRQ=42
50+
* (ameba_vector_table.h), APBPeriph_ADC and APBPeriph_ADC_CLOCK both
51+
* bit30|bit23 (sysreg_lsys.h). RTL8721F does not route the ADC through the
52+
* cap-touch/CTC block, so no second clock domain is needed and
53+
* AMEBA_ADC_AUXCLK_* stay undefined.
54+
*
55+
* External channels CH0..CH7 map to pads PA20,PA19,PA18,PA17,PA15,PA14,
56+
* PA13,PA12 (from ameba_adc.h ADC_CHx_PIN); CH8..CH10 and CH14 are fixed
57+
* internal channels with no pad. The 16-bit conversion word (channel id in
58+
* [19:16], data in [15:0]) is identical on every current Ameba chip, so the
59+
* driver extracts it directly and no macro is needed here.
60+
*/
61+
62+
#define AMEBA_ADC_NCHAN 12 /* ADC_CH_NUM: CH0..CH7 ext, rest int */
63+
#define AMEBA_ADC_MAXLIST 16 /* Channel-switch list depth (Cvlist) */
64+
#define AMEBA_ADC_PINMUX_FID 5 /* PINMUX_FUNCTION_ADC */
65+
#define AMEBA_ADC_IRQ 42 /* ADC_IRQ (unused by this polling drv) */
66+
67+
/* sizeof(fwlib ADC_InitTypeDef): OpMode/CvlistLen/Cvlist[16] then
68+
* RxThresholdLevel/SpecialCh/SamplePeriodUs(u16) -> 22 bytes. The shared
69+
* driver sizes its stack mirror from this so ADC_StructInit() cannot
70+
* overflow it.
71+
*/
72+
73+
#define AMEBA_ADC_INIT_SIZE 22
74+
75+
/* APBPeriph_ADC (function) and APBPeriph_ADC_CLOCK masks. Equal on this
76+
* chip; both set the bit30 group selector and bit23 for the LP ADC block.
77+
* RTL8721F needs no second clock domain, so AMEBA_ADC_AUXCLK_* are left
78+
* undefined (amebalite/amebasmart define them to gate APBPeriph_CTC too).
79+
*/
80+
81+
#define AMEBA_ADC_APBPERIPH (((uint32_t)1 << 30) | ((uint32_t)1 << 23))
82+
#define AMEBA_ADC_APBPERIPH_CLK (((uint32_t)1 << 30) | ((uint32_t)1 << 23))
83+
84+
#endif /* __ARCH_ARM_SRC_RTL8721F_AMEBA_ADC_CHIP_H */

arch/arm/src/rtl8721f/ameba_board.mk

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ ifeq ($(CONFIG_AMEBA_PWM),y)
176176
AMEBA_FWLIB_SRCS += $(AMEBA_SOC)/fwlib/ram_common/ameba_tim.c
177177
endif
178178

179+
# ADC (SAR) register layer. The ADC driver
180+
# (arch/.../common/ameba/ameba_adc.c) calls the fwlib ADC API; the data tables
181+
# and helpers it indexes live in this RAM source and must be compiled in
182+
# (--gc-sections drops the unused interrupt/timer-trigger helpers).
183+
ifeq ($(CONFIG_AMEBA_ADC),y)
184+
AMEBA_FWLIB_SRCS += $(AMEBA_SOC)/fwlib/ram_common/ameba_adc.c
185+
endif
186+
179187
# -Wno-int-conversion: the vendored SDK passes NULL to irq_register()'s u32
180188
# "Data" (interrupt context) argument in many places -- an intentional
181189
# NULL-as-context idiom. Silence -Wint-conversion for the SDK fwlib sources
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#
2+
# This file is autogenerated: PLEASE DO NOT EDIT IT.
3+
#
4+
# You can use "make menuconfig" to make any modifications to the installed .config file.
5+
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
6+
# modifications.
7+
#
8+
# CONFIG_DEBUG_WARN is not set
9+
CONFIG_AMEBA_ADC=y
10+
CONFIG_ARCH="arm"
11+
CONFIG_ARCH_BOARD="rtl8721f_evb"
12+
CONFIG_ARCH_BOARD_RTL8721F_EVB=y
13+
CONFIG_ARCH_CHIP="rtl8721f"
14+
CONFIG_ARCH_CHIP_RTL8721F=y
15+
CONFIG_ARCH_INTERRUPTSTACK=2048
16+
CONFIG_ARCH_STACKDUMP=y
17+
CONFIG_ARMV8M_SYSTICK=y
18+
CONFIG_BUILTIN=y
19+
CONFIG_DEBUG_ASSERTIONS=y
20+
CONFIG_DEBUG_FEATURES=y
21+
CONFIG_DEBUG_FULLOPT=y
22+
CONFIG_DEBUG_SYMBOLS=y
23+
CONFIG_DEFAULT_TASK_STACKSIZE=4096
24+
CONFIG_EXAMPLES_ADC=y
25+
CONFIG_EXAMPLES_ADC_GROUPSIZE=2
26+
CONFIG_EXAMPLES_ADC_NSAMPLES=1
27+
CONFIG_EXAMPLES_ADC_SWTRIG=y
28+
CONFIG_EXAMPLES_HELLO=y
29+
CONFIG_FS_PROCFS=y
30+
CONFIG_FS_TMPFS=y
31+
CONFIG_IDLETHREAD_STACKSIZE=4096
32+
CONFIG_INIT_ENTRYPOINT="nsh_main"
33+
CONFIG_LIBC_MEMFD_ERROR=y
34+
CONFIG_MM_DEFAULT_ALIGNMENT=32
35+
CONFIG_NSH_BUILTIN_APPS=y
36+
CONFIG_NSH_FILEIOSIZE=512
37+
CONFIG_NSH_READLINE=y
38+
CONFIG_PREALLOC_TIMERS=4
39+
CONFIG_RAM_SIZE=401408
40+
CONFIG_RAM_START=0x20006000
41+
CONFIG_RR_INTERVAL=200
42+
CONFIG_SCHED_HPWORK=y
43+
CONFIG_SCHED_HPWORKPRIORITY=192
44+
CONFIG_SCHED_LPWORK=y
45+
CONFIG_STACK_COLORATION=y
46+
CONFIG_START_DAY=16
47+
CONFIG_START_MONTH=6
48+
CONFIG_START_YEAR=2026
49+
CONFIG_SYSTEM_NSH=y
50+
CONFIG_SYSTEM_NSH_STACKSIZE=2500
51+
CONFIG_TIMER=y
52+
CONFIG_TIMER_ARCH=y
53+
CONFIG_USEC_PER_TICK=1000

boards/arm/rtl8721f/rtl8721f_evb/src/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,18 @@ if(CONFIG_AMEBA_PWM)
4242
list(APPEND SRCS rtl8721f_pwm.c)
4343
endif()
4444

45+
if(CONFIG_AMEBA_ADC)
46+
list(APPEND SRCS rtl8721f_adc.c)
47+
endif()
48+
4549
target_sources(board PRIVATE ${SRCS})
4650

4751
if(CONFIG_AMEBA_GPIO
4852
OR CONFIG_AMEBA_UART
4953
OR CONFIG_AMEBA_I2C
5054
OR CONFIG_AMEBA_SPI
51-
OR CONFIG_AMEBA_PWM)
55+
OR CONFIG_AMEBA_PWM
56+
OR CONFIG_AMEBA_ADC)
5257
# The board pin tables pull in the shared drivers' public headers from
5358
# arch/arm/src/common/ameba/, not on the default board include path.
5459
target_include_directories(board

boards/arm/rtl8721f/rtl8721f_evb/src/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,14 @@ ifeq ($(CONFIG_AMEBA_PWM),y)
4444
CSRCS += rtl8721f_pwm.c
4545
endif
4646

47+
ifeq ($(CONFIG_AMEBA_ADC),y)
48+
CSRCS += rtl8721f_adc.c
49+
endif
50+
4751
# The board pin tables pull in the shared drivers' public headers from
4852
# arch/arm/src/common/ameba/, which is not on the default board include path.
4953

50-
ifneq ($(CONFIG_AMEBA_GPIO)$(CONFIG_AMEBA_UART)$(CONFIG_AMEBA_I2C)$(CONFIG_AMEBA_SPI)$(CONFIG_AMEBA_PWM),)
54+
ifneq ($(CONFIG_AMEBA_GPIO)$(CONFIG_AMEBA_UART)$(CONFIG_AMEBA_I2C)$(CONFIG_AMEBA_SPI)$(CONFIG_AMEBA_PWM)$(CONFIG_AMEBA_ADC),)
5155
CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)arch$(DELIM)arm$(DELIM)src$(DELIM)common$(DELIM)ameba
5256
endif
5357

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/****************************************************************************
2+
* boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_adc.c
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
/****************************************************************************
24+
* Included Files
25+
****************************************************************************/
26+
27+
#include <nuttx/config.h>
28+
29+
#include <sys/param.h>
30+
#include <syslog.h>
31+
#include <errno.h>
32+
33+
#include "ameba_gpio.h"
34+
#include "ameba_adc.h"
35+
#include "rtl8721f_rtl8721f_evb.h"
36+
37+
#ifdef CONFIG_AMEBA_ADC
38+
39+
/****************************************************************************
40+
* Private Data
41+
****************************************************************************/
42+
43+
/* Channels sampled on /dev/adc0 and the analog pad each is wired to. The
44+
* external channels CH0..CH7 map to pads PA20,PA19,PA18,PA17,PA15,PA14,
45+
* PA13,PA12; this board exposes CH5 on PA14 and CH6 on PA13. Any external
46+
* channel can be listed here; internal channels would carry
47+
* AMEBA_ADC_PIN_NC.
48+
*/
49+
50+
static const uint8_t g_adc_channels[] =
51+
{
52+
5, /* ADC_CH5 */
53+
6, /* ADC_CH6 */
54+
};
55+
56+
static const uint8_t g_adc_pins[] =
57+
{
58+
AMEBA_PA(14), /* CH5 -> PA14 */
59+
AMEBA_PA(13), /* CH6 -> PA13 */
60+
};
61+
62+
/****************************************************************************
63+
* Public Functions
64+
****************************************************************************/
65+
66+
/****************************************************************************
67+
* Name: rtl8721f_adc_initialize
68+
*
69+
* Description:
70+
* Register the board's ADC channels at /dev/adc0.
71+
*
72+
****************************************************************************/
73+
74+
int rtl8721f_adc_initialize(void)
75+
{
76+
int ret;
77+
78+
ret = ameba_adc_register("/dev/adc0", g_adc_channels, g_adc_pins,
79+
nitems(g_adc_channels));
80+
if (ret < 0)
81+
{
82+
syslog(LOG_ERR,
83+
"ERROR: ameba_adc_register(/dev/adc0) failed: %d\n", ret);
84+
return ret;
85+
}
86+
87+
return OK;
88+
}
89+
90+
#endif /* CONFIG_AMEBA_ADC */

boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_bringup.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,16 @@ int rtl8721f_bringup(void)
184184
}
185185
#endif
186186

187+
#ifdef CONFIG_AMEBA_ADC
188+
/* Register the board's ADC channels at /dev/adc0. */
189+
190+
ret = rtl8721f_adc_initialize();
191+
if (ret < 0)
192+
{
193+
syslog(LOG_ERR, "ERROR: rtl8721f_adc_initialize failed: %d\n", ret);
194+
}
195+
#endif
196+
187197
IPC_patch_function(rtos_critical_enter, rtos_critical_exit,
188198
AMEBA_RTOS_CRITICAL_SEMA);
189199
IPC_SEMDelayStub(rtos_time_delay_ms);

0 commit comments

Comments
 (0)