]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/port_api.c
Merge commit 'fdc38ef3f92af7adeeb4de49550d8838c8a39b5c'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NORDIC / TARGET_MCU_NRF51822 / port_api.c
1 /* mbed Microcontroller Library
2 * Copyright (c) 2013 Nordic Semiconductor
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "port_api.h"
17 #include "pinmap.h"
18 #include "gpio_api.h"
19
20 PinName port_pin(PortName port, int pin_n)
21 {
22 return (PinName)(pin_n);
23 }
24
25 void port_init(port_t *obj, PortName port, int mask, PinDirection dir)
26 {
27 obj->port = port;
28 obj->mask = mask;
29
30 obj->reg_out = &NRF_GPIO->OUT;
31 obj->reg_in = &NRF_GPIO->IN;
32 obj->reg_cnf = NRF_GPIO->PIN_CNF;
33
34 port_dir(obj, dir);
35 }
36
37 void port_mode(port_t *obj, PinMode mode)
38 {
39 uint32_t i;
40 // The mode is set per pin: reuse pinmap logic
41 for (i = 0; i<31; i++) {
42 if (obj->mask & (1 << i)) {
43 pin_mode(port_pin(obj->port, i), mode);
44 }
45 }
46 }
47
48 void port_dir(port_t *obj, PinDirection dir)
49 {
50 int i;
51 switch (dir) {
52 case PIN_INPUT:
53 for (i = 0; i<31; i++) {
54 if (obj->mask & (1 << i)) {
55 obj->reg_cnf[i] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
56 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
57 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
58 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
59 }
60 }
61 break;
62 case PIN_OUTPUT:
63 for (i = 0; i<31; i++) {
64 if (obj->mask & (1 << i)) {
65 obj->reg_cnf[i] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
66 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
67 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
68 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
69 | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
70 }
71 }
72 break;
73 }
74 }
75
76 void port_write(port_t *obj, int value)
77 {
78 *obj->reg_out = value;
79 }
80
81 int port_read(port_t *obj)
82 {
83 return (*obj->reg_in);
84 }
Imprint / Impressum