- fix F411 F334 systeminit when HSI used - portinout always read IDR regardless of port direction

Fork of mbed-src by mbed official

Committer:
Geremia
Date:
Sat Sep 27 11:16:28 2014 +0000
Revision:
332:e299ae530e63
Parent:
20:4263a77256ae
- fix F411 F334 systeminit when HSI used; - STMs PortInOut port.read() always read input data register (real external pin state) even if direction is output (same as other platforms)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 20:4263a77256ae 1 /* mbed Microcontroller Library
bogdanm 20:4263a77256ae 2 * Copyright (c) 2006-2013 ARM Limited
bogdanm 20:4263a77256ae 3 *
bogdanm 20:4263a77256ae 4 * Licensed under the Apache License, Version 2.0 (the "License");
bogdanm 20:4263a77256ae 5 * you may not use this file except in compliance with the License.
bogdanm 20:4263a77256ae 6 * You may obtain a copy of the License at
bogdanm 20:4263a77256ae 7 *
bogdanm 20:4263a77256ae 8 * http://www.apache.org/licenses/LICENSE-2.0
bogdanm 20:4263a77256ae 9 *
bogdanm 20:4263a77256ae 10 * Unless required by applicable law or agreed to in writing, software
bogdanm 20:4263a77256ae 11 * distributed under the License is distributed on an "AS IS" BASIS,
bogdanm 20:4263a77256ae 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bogdanm 20:4263a77256ae 13 * See the License for the specific language governing permissions and
bogdanm 20:4263a77256ae 14 * limitations under the License.
bogdanm 20:4263a77256ae 15 */
bogdanm 20:4263a77256ae 16 #include "port_api.h"
bogdanm 20:4263a77256ae 17 #include "pinmap.h"
bogdanm 20:4263a77256ae 18 #include "gpio_api.h"
bogdanm 20:4263a77256ae 19
bogdanm 20:4263a77256ae 20 #if DEVICE_PORTIN || DEVICE_PORTOUT
bogdanm 20:4263a77256ae 21
bogdanm 20:4263a77256ae 22 PinName port_pin(PortName port, int pin_n) {
bogdanm 20:4263a77256ae 23 return pin_n + (port << 4);
bogdanm 20:4263a77256ae 24 }
bogdanm 20:4263a77256ae 25
bogdanm 20:4263a77256ae 26 void port_init(port_t *obj, PortName port, int mask, PinDirection dir) {
bogdanm 20:4263a77256ae 27 obj->port = port;
bogdanm 20:4263a77256ae 28 obj->mask = mask;
bogdanm 20:4263a77256ae 29
bogdanm 20:4263a77256ae 30 uint32_t port_index = (uint32_t) port;
bogdanm 20:4263a77256ae 31
bogdanm 20:4263a77256ae 32 GPIO_TypeDef *port_reg = (GPIO_TypeDef *)(GPIOA_BASE + (port_index << 10));
bogdanm 20:4263a77256ae 33 // Enable GPIO peripheral clock
bogdanm 20:4263a77256ae 34 RCC->AHB1ENR |= 1 << port_index;
bogdanm 20:4263a77256ae 35
bogdanm 20:4263a77256ae 36 obj->reg_mode = &port_reg->MODER;
bogdanm 20:4263a77256ae 37 obj->reg_set = &port_reg->BSRRH;
bogdanm 20:4263a77256ae 38 obj->reg_clr = &port_reg->BSRRL;
bogdanm 20:4263a77256ae 39 obj->reg_in = &port_reg->IDR;
bogdanm 20:4263a77256ae 40 obj->reg_out = &port_reg->ODR;
bogdanm 20:4263a77256ae 41
bogdanm 20:4263a77256ae 42 port_dir(obj, dir);
bogdanm 20:4263a77256ae 43 }
bogdanm 20:4263a77256ae 44
bogdanm 20:4263a77256ae 45 void port_mode(port_t *obj, PinMode mode) {
bogdanm 20:4263a77256ae 46 uint32_t i;
bogdanm 20:4263a77256ae 47 // The mode is set per pin: reuse pinmap logic
bogdanm 20:4263a77256ae 48 for (i=0; i<16; i++) {
bogdanm 20:4263a77256ae 49 if (obj->mask & (1<<i)) {
bogdanm 20:4263a77256ae 50 pin_mode(port_pin(obj->port, i), mode);
bogdanm 20:4263a77256ae 51 }
bogdanm 20:4263a77256ae 52 }
bogdanm 20:4263a77256ae 53 }
bogdanm 20:4263a77256ae 54
bogdanm 20:4263a77256ae 55 void port_dir(port_t *obj, PinDirection dir) {
bogdanm 20:4263a77256ae 56 obj->direction = dir;
bogdanm 20:4263a77256ae 57 uint32_t tmp = *obj->reg_mode;
bogdanm 20:4263a77256ae 58 for (int i=0; i<16; i++) {
bogdanm 20:4263a77256ae 59 if (obj->mask & (1 << i)) {
bogdanm 20:4263a77256ae 60 // Clear the mode bits (i.e. set to input)
bogdanm 20:4263a77256ae 61 tmp &= ~(0x3 << (i << 1));
bogdanm 20:4263a77256ae 62 if (dir == PIN_OUTPUT) {
bogdanm 20:4263a77256ae 63 // Set to output
bogdanm 20:4263a77256ae 64 tmp |= 0x1 << (i << 1);
bogdanm 20:4263a77256ae 65 }
bogdanm 20:4263a77256ae 66 }
bogdanm 20:4263a77256ae 67 }
bogdanm 20:4263a77256ae 68 *obj->reg_mode = tmp;
bogdanm 20:4263a77256ae 69 }
bogdanm 20:4263a77256ae 70
bogdanm 20:4263a77256ae 71 void port_write(port_t *obj, int value) {
bogdanm 20:4263a77256ae 72 *obj->reg_out = (*obj->reg_out & ~obj->mask) | (value & obj->mask);
bogdanm 20:4263a77256ae 73 }
bogdanm 20:4263a77256ae 74
bogdanm 20:4263a77256ae 75 int port_read(port_t *obj) {
Geremia 332:e299ae530e63 76 return *obj->reg_in & obj->mask;
bogdanm 20:4263a77256ae 77 }
bogdanm 20:4263a77256ae 78
bogdanm 20:4263a77256ae 79 #endif