1 /*
2  * Copyright (c) 2015 Eric Holland
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 #include <lk/debug.h>
9 #include <assert.h>
10 #include <dev/gpio.h>
11 #include <platform/nrf51.h>
12 #include <platform/gpio.h>
13 
gpio_config(unsigned nr,unsigned flags)14 int gpio_config(unsigned nr, unsigned flags) {
15     DEBUG_ASSERT(nr <= NRF_MAX_PIN_NUMBER);
16 
17     unsigned init;
18 
19     if (flags & GPIO_OUTPUT) {
20 
21         NRF_GPIO->PIN_CNF[nr] = GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos     | \
22                                 GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos  | \
23                                 GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos   | \
24                                 GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos;
25     } else { // GPIO_INPUT
26         if (flags & GPIO_PULLUP) {
27             init    =   GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos;
28         } else if (flags & GPIO_PULLDOWN) {
29             init    =   GPIO_PIN_CNF_PULL_Pulldown << GPIO_PIN_CNF_PULL_Pos;
30         } else {
31             init    =   GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos;
32         }
33         NRF_GPIO->PIN_CNF[nr] = GPIO_PIN_CNF_DIR_Input      <<  GPIO_PIN_CNF_DIR_Pos    | \
34                                 GPIO_PIN_CNF_INPUT_Connect  <<  GPIO_PIN_CNF_INPUT_Pos  | \
35                                 init;
36     }
37     return 0;
38 }
39 
gpio_set(unsigned nr,unsigned on)40 void gpio_set(unsigned nr, unsigned on) {
41     DEBUG_ASSERT(nr <= NRF_MAX_PIN_NUMBER);
42 
43     if (on > 0) {
44         NRF_GPIO->OUTSET = 1 << nr;
45     } else {
46         NRF_GPIO->OUTCLR = 1 << nr;
47     }
48 }
49 
gpio_get(unsigned nr)50 int gpio_get(unsigned nr) {
51     DEBUG_ASSERT( nr <= NRF_MAX_PIN_NUMBER );
52 
53     if ( NRF_GPIO->IN & ( 1 << nr) ) {
54         return 1;
55     } else {
56         return 0;
57     }
58 }
59 
60