]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F4XX/port_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_STM / TARGET_STM32F4XX / port_api.c
1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
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 #if DEVICE_PORTIN || DEVICE_PORTOUT
21
22 PinName port_pin(PortName port, int pin_n) {
23 return pin_n + (port << 4);
24 }
25
26 void port_init(port_t *obj, PortName port, int mask, PinDirection dir) {
27 obj->port = port;
28 obj->mask = mask;
29
30 uint32_t port_index = (uint32_t) port;
31
32 GPIO_TypeDef *port_reg = (GPIO_TypeDef *)(GPIOA_BASE + (port_index << 10));
33 // Enable GPIO peripheral clock
34 RCC->AHB1ENR |= 1 << port_index;
35
36 obj->reg_mode = &port_reg->MODER;
37 obj->reg_set = &port_reg->BSRRH;
38 obj->reg_clr = &port_reg->BSRRL;
39 obj->reg_in = &port_reg->IDR;
40 obj->reg_out = &port_reg->ODR;
41
42 port_dir(obj, dir);
43 }
44
45 void port_mode(port_t *obj, PinMode mode) {
46 uint32_t i;
47 // The mode is set per pin: reuse pinmap logic
48 for (i=0; i<16; i++) {
49 if (obj->mask & (1<<i)) {
50 pin_mode(port_pin(obj->port, i), mode);
51 }
52 }
53 }
54
55 void port_dir(port_t *obj, PinDirection dir) {
56 obj->direction = dir;
57 uint32_t tmp = *obj->reg_mode;
58 for (int i=0; i<16; i++) {
59 if (obj->mask & (1 << i)) {
60 // Clear the mode bits (i.e. set to input)
61 tmp &= ~(0x3 << (i << 1));
62 if (dir == PIN_OUTPUT) {
63 // Set to output
64 tmp |= 0x1 << (i << 1);
65 }
66 }
67 }
68 *obj->reg_mode = tmp;
69 }
70
71 void port_write(port_t *obj, int value) {
72 *obj->reg_out = (*obj->reg_out & ~obj->mask) | (value & obj->mask);
73 }
74
75 int port_read(port_t *obj) {
76 switch (obj->direction) {
77 case PIN_OUTPUT: return *obj->reg_out & obj->mask;
78 case PIN_INPUT: return *obj->reg_in & obj->mask;
79 }
80 return 0;
81 }
82
83 #endif
Imprint / Impressum