Skip to content

Commit ae1950e

Browse files
raiden00plxiaoxiang781216
authored andcommitted
boards/arm/nrf52/nrf52840-dk: add GPIO driver based on leds and buttons
With this change we can use on board leds and buttons with gpio driver Signed-off-by: raiden00pl <[email protected]>
1 parent b83493d commit ae1950e

File tree

7 files changed

+229
-2
lines changed

7 files changed

+229
-2
lines changed

boards/arm/nrf52/nrf52840-dk/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,11 @@ config NRF52840DK_HIGHPRI
99
bool "High priority interrupt test"
1010
default n
1111

12+
config NRF52840DK_BTNLEDS_GPIO
13+
bool "Export board LEDS and BUTTONS as GPIO"
14+
default n
15+
depends on !ARCH_LEDS && !ARCH_BUTTONS && GPIO_LOWER_HALF && DEV_GPIO
16+
---help---
17+
Export board LEDS and buttons as GPIOs.
18+
1219
endif

boards/arm/nrf52/nrf52840-dk/src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ if(CONFIG_ARCH_BUTTONS)
3636
list(APPEND SRCS nrf52_buttons.c)
3737
endif()
3838

39+
if(CONFIG_NRF52840DK_BTNLEDS_GPIO)
40+
list(APPEND SRCS nrf52_gpio.c)
41+
endif()
42+
3943
if(CONFIG_NRF52_SPI_MASTER)
4044
list(APPEND SRCS nrf52_spi.c)
4145
endif()

boards/arm/nrf52/nrf52840-dk/src/Make.defs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ ifeq ($(CONFIG_ARCH_BUTTONS),y)
3838
CSRCS += nrf52_buttons.c
3939
endif
4040

41+
ifeq ($(CONFIG_NRF52840DK_BTNLEDS_GPIO),y)
42+
CSRCS += nrf52_gpio.c
43+
endif
44+
4145
ifeq ($(CONFIG_NRF52_SPI_MASTER),y)
4246
CSRCS += nrf52_spi.c
4347
endif

boards/arm/nrf52/nrf52840-dk/src/nrf52840-dk.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,5 +203,17 @@ int nrf52_adc_setup(void);
203203
int nrf52_mx25_initialize(void);
204204
#endif
205205

206+
/****************************************************************************
207+
* Name: nrf52_gpioleds_initialize
208+
*
209+
* Description:
210+
* Initialize GPIO devices with board LEDS and buttons.
211+
*
212+
****************************************************************************/
213+
214+
#ifdef CONFIG_NRF52840DK_BTNLEDS_GPIO
215+
int nrf52_gpioleds_initialize(void);
216+
#endif
217+
206218
#endif /* __ASSEMBLY__ */
207219
#endif /* __BOARDS_ARM_NRF52_NRF52840_DK_SRC_NRF52840_DK_H */

boards/arm/nrf52/nrf52840-dk/src/nrf52_bringup.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@ int nrf52_bringup(void)
173173
}
174174
#endif
175175

176+
#ifdef CONFIG_NRF52840DK_BTNLEDS_GPIO
177+
ret = nrf52_gpioleds_initialize();
178+
if (ret < 0)
179+
{
180+
syslog(LOG_ERR,
181+
"ERROR: nrf52_gpioleds_initialize() failed: %d\n", ret);
182+
}
183+
#endif
184+
176185
#ifdef CONFIG_INPUT_BUTTONS
177186
/* Register the BUTTON driver */
178187

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/****************************************************************************
2+
* boards/arm/nrf52/nrf52840-dk/src/nrf52_gpio.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <nuttx/config.h>
26+
27+
#include <stdint.h>
28+
#include <stdbool.h>
29+
#include <debug.h>
30+
31+
#include <nuttx/ioexpander/gpio.h>
32+
33+
#include <arch/board/board.h>
34+
35+
#include "chip.h"
36+
#include "arm_internal.h"
37+
#include "nrf52840-dk.h"
38+
39+
/****************************************************************************
40+
* Private Types
41+
****************************************************************************/
42+
43+
struct nrf52gpio_dev_s
44+
{
45+
struct gpio_dev_s gpio;
46+
uint8_t id;
47+
};
48+
49+
/****************************************************************************
50+
* Private Function Prototypes
51+
****************************************************************************/
52+
53+
static int gpin_read(struct gpio_dev_s *dev, bool *value);
54+
static int gpout_read(struct gpio_dev_s *dev, bool *value);
55+
static int gpout_write(struct gpio_dev_s *dev, bool value);
56+
57+
/****************************************************************************
58+
* Private Data
59+
****************************************************************************/
60+
61+
static const struct gpio_operations_s gpin_ops =
62+
{
63+
.go_read = gpin_read,
64+
.go_write = NULL,
65+
.go_attach = NULL,
66+
.go_enable = NULL,
67+
};
68+
69+
static const struct gpio_operations_s gpout_ops =
70+
{
71+
.go_read = gpout_read,
72+
.go_write = gpout_write,
73+
.go_attach = NULL,
74+
.go_enable = NULL,
75+
};
76+
77+
/* Buttons as inputs */
78+
79+
static const uint32_t g_gpioinputs[NUM_BUTTONS] =
80+
{
81+
GPIO_BUTTON1,
82+
GPIO_BUTTON2,
83+
GPIO_BUTTON3,
84+
GPIO_BUTTON4
85+
};
86+
87+
static struct nrf52gpio_dev_s g_gpin[NUM_BUTTONS];
88+
89+
/* Leds as outputs */
90+
91+
static const uint32_t g_gpiooutputs[BOARD_NLEDS] =
92+
{
93+
#if 0 < BOARD_NLEDS
94+
GPIO_LED1,
95+
#endif
96+
#if 1 < BOARD_NLEDS
97+
GPIO_LED2,
98+
#endif
99+
#if 2 < BOARD_NLEDS
100+
GPIO_LED3,
101+
#endif
102+
#if 3 < BOARD_NLEDS
103+
GPIO_LED4,
104+
#endif
105+
};
106+
107+
static struct nrf52gpio_dev_s g_gpout[BOARD_NLEDS];
108+
109+
/****************************************************************************
110+
* Private Functions
111+
****************************************************************************/
112+
113+
static int gpin_read(struct gpio_dev_s *dev, bool *value)
114+
{
115+
struct nrf52gpio_dev_s *io = (struct nrf52gpio_dev_s *)dev;
116+
117+
*value = nrf52_gpio_read(g_gpioinputs[io->id]);
118+
return OK;
119+
}
120+
121+
static int gpout_read(struct gpio_dev_s *dev, bool *value)
122+
{
123+
struct nrf52gpio_dev_s *io = (struct nrf52gpio_dev_s *)dev;
124+
125+
*value = nrf52_gpio_read(g_gpiooutputs[io->id]);
126+
return OK;
127+
}
128+
129+
static int gpout_write(struct gpio_dev_s *dev, bool value)
130+
{
131+
struct nrf52gpio_dev_s *io = (struct nrf52gpio_dev_s *)dev;
132+
133+
nrf52_gpio_write(g_gpiooutputs[io->id], value);
134+
return OK;
135+
}
136+
137+
/****************************************************************************
138+
* Public Functions
139+
****************************************************************************/
140+
141+
/****************************************************************************
142+
* Name: nrf52_gpioleds_initialize
143+
*
144+
* Description:
145+
* Initialize GPIO devices with board LEDS and buttons.
146+
*
147+
****************************************************************************/
148+
149+
int nrf52_gpioleds_initialize(void)
150+
{
151+
int i;
152+
int pincount = 0;
153+
154+
/* Inputs */
155+
156+
for (i = 0; i < NUM_BUTTONS; i++)
157+
{
158+
/* Setup and register the GPIO pin */
159+
160+
g_gpin[i].gpio.gp_pintype = GPIO_INPUT_PIN;
161+
g_gpin[i].gpio.gp_ops = &gpin_ops;
162+
g_gpin[i].id = i;
163+
gpio_pin_register(&g_gpin[i].gpio, pincount);
164+
165+
/* Configure the pin that will be used as input */
166+
167+
nrf52_gpio_config(g_gpioinputs[i]);
168+
169+
pincount++;
170+
}
171+
172+
/* Outputs */
173+
174+
for (i = 0; i < BOARD_NLEDS; i++)
175+
{
176+
/* Setup and register the GPIO pin */
177+
178+
g_gpout[i].gpio.gp_pintype = GPIO_OUTPUT_PIN;
179+
g_gpout[i].gpio.gp_ops = &gpout_ops;
180+
g_gpout[i].id = i;
181+
gpio_pin_register(&g_gpout[i].gpio, pincount);
182+
183+
/* Configure the pin that will be used as input */
184+
185+
nrf52_gpio_config(g_gpiooutputs[i]);
186+
187+
pincount++;
188+
}
189+
190+
return OK;
191+
}

boards/arm/nrf52/nrf52840-dk/src/nrf52_userleds.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include "arm_internal.h"
3737
#include "nrf52840-dk.h"
3838

39-
#ifndef CONFIG_ARCH_LEDS
39+
#if !defined(CONFIG_ARCH_LEDS) && !defined(CONFIG_NRF52840DK_LEDS_GPIO)
4040

4141
/****************************************************************************
4242
* Pre-processor Definitions
@@ -147,4 +147,4 @@ void board_userled_all(uint32_t ledset)
147147
}
148148
}
149149

150-
#endif /* !CONFIG_ARCH_LEDS */
150+
#endif /* !defined(CONFIG_ARCH_LEDS) && !defined(CONFIG_NRF52840DK_LEDS_GPIO) */

0 commit comments

Comments
 (0)