mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
186:707f6e361f3e
mbed library release version 165

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 144:ef7eb2e8f9f7 1 /*
<> 144:ef7eb2e8f9f7 2 * Copyright (c) 2013 Nordic Semiconductor ASA
<> 144:ef7eb2e8f9f7 3 * All rights reserved.
<> 144:ef7eb2e8f9f7 4 *
<> 144:ef7eb2e8f9f7 5 * Redistribution and use in source and binary forms, with or without modification,
<> 144:ef7eb2e8f9f7 6 * are permitted provided that the following conditions are met:
<> 144:ef7eb2e8f9f7 7 *
<> 144:ef7eb2e8f9f7 8 * 1. Redistributions of source code must retain the above copyright notice, this list
<> 144:ef7eb2e8f9f7 9 * of conditions and the following disclaimer.
<> 144:ef7eb2e8f9f7 10 *
<> 144:ef7eb2e8f9f7 11 * 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
<> 144:ef7eb2e8f9f7 12 * integrated circuit in a product or a software update for such product, must reproduce
<> 144:ef7eb2e8f9f7 13 * the above copyright notice, this list of conditions and the following disclaimer in
<> 144:ef7eb2e8f9f7 14 * the documentation and/or other materials provided with the distribution.
<> 144:ef7eb2e8f9f7 15 *
<> 144:ef7eb2e8f9f7 16 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
<> 144:ef7eb2e8f9f7 17 * used to endorse or promote products derived from this software without specific prior
<> 144:ef7eb2e8f9f7 18 * written permission.
<> 144:ef7eb2e8f9f7 19 *
<> 144:ef7eb2e8f9f7 20 * 4. This software, with or without modification, must only be used with a
<> 144:ef7eb2e8f9f7 21 * Nordic Semiconductor ASA integrated circuit.
<> 144:ef7eb2e8f9f7 22 *
<> 144:ef7eb2e8f9f7 23 * 5. Any software provided in binary or object form under this license must not be reverse
<> 144:ef7eb2e8f9f7 24 * engineered, decompiled, modified and/or disassembled.
<> 144:ef7eb2e8f9f7 25 *
<> 144:ef7eb2e8f9f7 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
<> 144:ef7eb2e8f9f7 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
<> 144:ef7eb2e8f9f7 28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
<> 144:ef7eb2e8f9f7 29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
<> 144:ef7eb2e8f9f7 30 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
<> 144:ef7eb2e8f9f7 31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
<> 144:ef7eb2e8f9f7 32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
<> 144:ef7eb2e8f9f7 33 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
<> 144:ef7eb2e8f9f7 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
<> 144:ef7eb2e8f9f7 35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<> 144:ef7eb2e8f9f7 36 *
<> 144:ef7eb2e8f9f7 37 */
<> 144:ef7eb2e8f9f7 38
<> 144:ef7eb2e8f9f7 39 #include "port_api.h"
<> 144:ef7eb2e8f9f7 40 #include "pinmap.h"
AnnaBridge 165:e614a9f1c9e2 41
AnnaBridge 165:e614a9f1c9e2 42 #if defined(TARGET_MCU_NRF51822) || defined(TARGET_MCU_NRF52832)
AnnaBridge 165:e614a9f1c9e2 43 #define GPIO_REG_LIST {NRF_GPIO}
AnnaBridge 165:e614a9f1c9e2 44 #endif
AnnaBridge 165:e614a9f1c9e2 45
AnnaBridge 165:e614a9f1c9e2 46 static NRF_GPIO_Type * const m_ports[] = GPIO_REG_LIST;
AnnaBridge 165:e614a9f1c9e2 47
AnnaBridge 165:e614a9f1c9e2 48 #if defined(TARGET_MCU_NRF51822)
AnnaBridge 165:e614a9f1c9e2 49 static const uint32_t m_gpio_pin_count[] = {31};
AnnaBridge 165:e614a9f1c9e2 50 #elif defined(TARGET_MCU_NRF52832)
AnnaBridge 165:e614a9f1c9e2 51 static const uint32_t m_gpio_pin_count[] = {32};
AnnaBridge 165:e614a9f1c9e2 52 #elif defined(TARGET_MCU_NRF52840)
AnnaBridge 165:e614a9f1c9e2 53 static const uint32_t m_gpio_pin_count[] = {32, 16};
AnnaBridge 165:e614a9f1c9e2 54 #else
AnnaBridge 165:e614a9f1c9e2 55 #error not recognized gpio count for mcu
AnnaBridge 165:e614a9f1c9e2 56 #endif
AnnaBridge 165:e614a9f1c9e2 57
AnnaBridge 165:e614a9f1c9e2 58 #define GPIO_PORT_COUNT (sizeof(m_gpio_pin_count)/sizeof(m_gpio_pin_count[0]))
AnnaBridge 165:e614a9f1c9e2 59
<> 144:ef7eb2e8f9f7 60
<> 144:ef7eb2e8f9f7 61 PinName port_pin(PortName port, int pin_n)
<> 144:ef7eb2e8f9f7 62 {
AnnaBridge 165:e614a9f1c9e2 63 #if defined(TARGET_MCU_NRF51822) || defined(TARGET_MCU_NRF52832)
AnnaBridge 165:e614a9f1c9e2 64 return (PinName)pin_n;
AnnaBridge 165:e614a9f1c9e2 65 #else
AnnaBridge 165:e614a9f1c9e2 66 return (PinName)NRF_GPIO_PIN_MAP(port, pin_n);
AnnaBridge 165:e614a9f1c9e2 67 #endif
<> 144:ef7eb2e8f9f7 68 }
<> 144:ef7eb2e8f9f7 69
<> 144:ef7eb2e8f9f7 70 void port_init(port_t *obj, PortName port, int mask, PinDirection dir)
<> 144:ef7eb2e8f9f7 71 {
AnnaBridge 165:e614a9f1c9e2 72 MBED_ASSERT((uint32_t)port < GPIO_PORT_COUNT);
AnnaBridge 165:e614a9f1c9e2 73
<> 144:ef7eb2e8f9f7 74 obj->port = port;
<> 144:ef7eb2e8f9f7 75 obj->mask = mask;
<> 144:ef7eb2e8f9f7 76
<> 144:ef7eb2e8f9f7 77 port_dir(obj, dir);
<> 144:ef7eb2e8f9f7 78 }
<> 144:ef7eb2e8f9f7 79
<> 144:ef7eb2e8f9f7 80 void port_mode(port_t *obj, PinMode mode)
<> 144:ef7eb2e8f9f7 81 {
<> 144:ef7eb2e8f9f7 82 uint32_t i;
<> 144:ef7eb2e8f9f7 83 // The mode is set per pin: reuse pinmap logic
<> 144:ef7eb2e8f9f7 84 for (i = 0; i<31; i++) {
<> 144:ef7eb2e8f9f7 85 if (obj->mask & (1 << i)) {
<> 144:ef7eb2e8f9f7 86 pin_mode(port_pin(obj->port, i), mode);
<> 144:ef7eb2e8f9f7 87 }
<> 144:ef7eb2e8f9f7 88 }
<> 144:ef7eb2e8f9f7 89 }
<> 144:ef7eb2e8f9f7 90
<> 144:ef7eb2e8f9f7 91 void port_dir(port_t *obj, PinDirection dir)
<> 144:ef7eb2e8f9f7 92 {
AnnaBridge 165:e614a9f1c9e2 93 uint32_t i;
AnnaBridge 165:e614a9f1c9e2 94
AnnaBridge 165:e614a9f1c9e2 95 volatile uint32_t *reg_cnf = (volatile uint32_t*) m_ports[obj->port]->PIN_CNF;
AnnaBridge 165:e614a9f1c9e2 96
<> 144:ef7eb2e8f9f7 97 switch (dir) {
AnnaBridge 165:e614a9f1c9e2 98 case PIN_INPUT:
AnnaBridge 165:e614a9f1c9e2 99
AnnaBridge 165:e614a9f1c9e2 100 for (i = 0; i < m_gpio_pin_count[obj->port]; i++) {
AnnaBridge 165:e614a9f1c9e2 101 if (obj->mask & (1 << i)) {
AnnaBridge 165:e614a9f1c9e2 102 reg_cnf[i] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
AnnaBridge 165:e614a9f1c9e2 103 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
AnnaBridge 165:e614a9f1c9e2 104 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
AnnaBridge 165:e614a9f1c9e2 105 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
AnnaBridge 165:e614a9f1c9e2 106 }
<> 144:ef7eb2e8f9f7 107 }
AnnaBridge 165:e614a9f1c9e2 108 break;
AnnaBridge 165:e614a9f1c9e2 109
AnnaBridge 165:e614a9f1c9e2 110 case PIN_OUTPUT:
AnnaBridge 165:e614a9f1c9e2 111
AnnaBridge 165:e614a9f1c9e2 112 for (i = 0; i < m_gpio_pin_count[obj->port]; i++) {
AnnaBridge 165:e614a9f1c9e2 113 if (obj->mask & (1 << i)) {
AnnaBridge 165:e614a9f1c9e2 114 reg_cnf[i] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
AnnaBridge 165:e614a9f1c9e2 115 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
AnnaBridge 165:e614a9f1c9e2 116 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
AnnaBridge 165:e614a9f1c9e2 117 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
AnnaBridge 165:e614a9f1c9e2 118 | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
AnnaBridge 165:e614a9f1c9e2 119 }
<> 144:ef7eb2e8f9f7 120 }
AnnaBridge 165:e614a9f1c9e2 121 break;
<> 144:ef7eb2e8f9f7 122 }
<> 144:ef7eb2e8f9f7 123 }
<> 144:ef7eb2e8f9f7 124
<> 144:ef7eb2e8f9f7 125 void port_write(port_t *obj, int value)
<> 144:ef7eb2e8f9f7 126 {
AnnaBridge 165:e614a9f1c9e2 127 m_ports[obj->port]->OUTSET = value & obj->mask;
AnnaBridge 165:e614a9f1c9e2 128 m_ports[obj->port]->OUTCLR = (~value) & obj->mask;
<> 144:ef7eb2e8f9f7 129 }
<> 144:ef7eb2e8f9f7 130
<> 144:ef7eb2e8f9f7 131 int port_read(port_t *obj)
<> 144:ef7eb2e8f9f7 132 {
AnnaBridge 165:e614a9f1c9e2 133 return ((m_ports[obj->port]->IN) & obj->mask);
<> 144:ef7eb2e8f9f7 134 }