blob: 6aeadaffb14f14c9fdfdcc26523aafd424e0fc01 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2015 Eric Holland
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <debug.h>
24#include <assert.h>
25#include <dev/gpio.h>
26#include <platform/nrf51.h>
27#include <platform/gpio.h>
28
29int gpio_config(unsigned nr, unsigned flags)
30{
31 DEBUG_ASSERT(nr <= NRF_MAX_PIN_NUMBER);
32
33 unsigned init;
34
35 if (flags & GPIO_OUTPUT) {
36
37 NRF_GPIO->PIN_CNF[nr] = GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos | \
38 GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos | \
39 GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos | \
40 GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos;
41 } else { // GPIO_INPUT
42 if (flags & GPIO_PULLUP) {
43 init = GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos;
44 } else if (flags & GPIO_PULLDOWN) {
45 init = GPIO_PIN_CNF_PULL_Pulldown << GPIO_PIN_CNF_PULL_Pos;
46 } else {
47 init = GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos;
48 }
49 NRF_GPIO->PIN_CNF[nr] = GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos | \
50 GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos | \
51 init;
52 }
53 return 0;
54}
55
56void gpio_set(unsigned nr, unsigned on)
57{
58 DEBUG_ASSERT(nr <= NRF_MAX_PIN_NUMBER);
59
60 if (on > 0) {
61 NRF_GPIO->OUTSET = 1 << nr;
62 } else {
63 NRF_GPIO->OUTCLR = 1 << nr;
64 }
65}
66
67int gpio_get(unsigned nr)
68{
69 DEBUG_ASSERT( nr <= NRF_MAX_PIN_NUMBER );
70
71 if ( NRF_GPIO->IN & ( 1 << nr) ) {
72 return 1;
73 } else {
74 return 0;
75 }
76}
77