|
| 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 | +} |
0 commit comments