t
Fork of mbed-dev by
targets/TARGET_Atmel/TARGET_SAM_CortexM0P/port_api.c@149:156823d33999, 2016-10-28 (annotated)
- Committer:
- <>
- Date:
- Fri Oct 28 11:17:30 2016 +0100
- Revision:
- 149:156823d33999
- Parent:
- targets/hal/TARGET_Atmel/TARGET_SAM_CortexM0P/port_api.c@64:41a834223ea3
This updates the lib to the mbed lib v128
NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 15:a81a8d6c1dfe | 1 | /* mbed Microcontroller Library |
mbed_official | 15:a81a8d6c1dfe | 2 | * Copyright (c) 2006-2015 ARM Limited |
mbed_official | 15:a81a8d6c1dfe | 3 | * |
mbed_official | 15:a81a8d6c1dfe | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 15:a81a8d6c1dfe | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 15:a81a8d6c1dfe | 6 | * You may obtain a copy of the License at |
mbed_official | 15:a81a8d6c1dfe | 7 | * |
mbed_official | 15:a81a8d6c1dfe | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 15:a81a8d6c1dfe | 9 | * |
mbed_official | 15:a81a8d6c1dfe | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 15:a81a8d6c1dfe | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 15:a81a8d6c1dfe | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 15:a81a8d6c1dfe | 13 | * See the License for the specific language governing permissions and |
mbed_official | 15:a81a8d6c1dfe | 14 | * limitations under the License. |
mbed_official | 15:a81a8d6c1dfe | 15 | */ |
mbed_official | 15:a81a8d6c1dfe | 16 | #include "port_api.h" |
mbed_official | 15:a81a8d6c1dfe | 17 | #include "pinmap.h" |
mbed_official | 15:a81a8d6c1dfe | 18 | #include "gpio_api.h" |
mbed_official | 15:a81a8d6c1dfe | 19 | #include "port.h" |
mbed_official | 15:a81a8d6c1dfe | 20 | |
mbed_official | 64:41a834223ea3 | 21 | |
mbed_official | 15:a81a8d6c1dfe | 22 | #if defined(TARGET_SAMR21G18A) |
mbed_official | 15:a81a8d6c1dfe | 23 | #define PORTA_MASK 0xDBDFFFF3 // mask for available pins in Port A |
mbed_official | 15:a81a8d6c1dfe | 24 | #define PORTB_MASK 0xC0C3C30D // mask for available pins in Port B |
mbed_official | 15:a81a8d6c1dfe | 25 | #define PORTC_MASK 0x000D0000 // mask for available pins in Port C |
mbed_official | 15:a81a8d6c1dfe | 26 | #elif defined(TARGET_SAMD21J18A) |
mbed_official | 15:a81a8d6c1dfe | 27 | #define PORTA_MASK 0xDBFFFFFF // mask for available pins in Port A |
mbed_official | 15:a81a8d6c1dfe | 28 | #define PORTB_MASK 0xC0C3FFFF // mask for available pins in Port B |
mbed_official | 15:a81a8d6c1dfe | 29 | #elif defined(TARGET_SAMD21G18A) |
mbed_official | 15:a81a8d6c1dfe | 30 | #define PORTA_MASK 0xDBFFFFFF // mask for available pins in Port A |
mbed_official | 15:a81a8d6c1dfe | 31 | #define PORTB_MASK 0x00C00F0C // mask for available pins in Port B |
mbed_official | 18:da299f395b9e | 32 | #elif defined(TARGET_SAML21J18A) |
mbed_official | 18:da299f395b9e | 33 | #define PORTA_MASK 0xCBFFFFFF // mask for available pins in Port A |
mbed_official | 18:da299f395b9e | 34 | #define PORTB_MASK 0xC0C3FFFF // mask for available pins in Port B |
mbed_official | 15:a81a8d6c1dfe | 35 | #else |
mbed_official | 15:a81a8d6c1dfe | 36 | #endif |
mbed_official | 15:a81a8d6c1dfe | 37 | |
mbed_official | 15:a81a8d6c1dfe | 38 | uint32_t start_pin(PortName port) |
mbed_official | 15:a81a8d6c1dfe | 39 | { |
mbed_official | 15:a81a8d6c1dfe | 40 | if(port < PortMax) { /* PortC value is 2*/ |
mbed_official | 15:a81a8d6c1dfe | 41 | return port * 32; |
mbed_official | 15:a81a8d6c1dfe | 42 | } else { |
mbed_official | 64:41a834223ea3 | 43 | return (uint32_t)NC; |
mbed_official | 15:a81a8d6c1dfe | 44 | } |
mbed_official | 15:a81a8d6c1dfe | 45 | } |
mbed_official | 15:a81a8d6c1dfe | 46 | void port_init(port_t *obj, PortName port, int mask, PinDirection dir) |
mbed_official | 15:a81a8d6c1dfe | 47 | { |
mbed_official | 15:a81a8d6c1dfe | 48 | MBED_ASSERT(obj); |
mbed_official | 15:a81a8d6c1dfe | 49 | struct port_config pin_conf; |
mbed_official | 15:a81a8d6c1dfe | 50 | int i, j; |
mbed_official | 15:a81a8d6c1dfe | 51 | int start; |
mbed_official | 15:a81a8d6c1dfe | 52 | |
mbed_official | 15:a81a8d6c1dfe | 53 | port_get_config_defaults(&pin_conf); |
mbed_official | 15:a81a8d6c1dfe | 54 | switch (dir) { |
mbed_official | 15:a81a8d6c1dfe | 55 | case PIN_INPUT : |
mbed_official | 15:a81a8d6c1dfe | 56 | pin_conf.direction = PORT_PIN_DIR_INPUT; |
mbed_official | 15:a81a8d6c1dfe | 57 | break; |
mbed_official | 15:a81a8d6c1dfe | 58 | case PIN_OUTPUT: |
mbed_official | 15:a81a8d6c1dfe | 59 | pin_conf.direction = PORT_PIN_DIR_OUTPUT; |
mbed_official | 15:a81a8d6c1dfe | 60 | break; |
mbed_official | 15:a81a8d6c1dfe | 61 | case PIN_INPUT_OUTPUT: |
mbed_official | 15:a81a8d6c1dfe | 62 | pin_conf.direction = PORT_PIN_DIR_OUTPUT_WTH_READBACK; |
mbed_official | 15:a81a8d6c1dfe | 63 | break; |
mbed_official | 15:a81a8d6c1dfe | 64 | default: |
mbed_official | 15:a81a8d6c1dfe | 65 | return; |
mbed_official | 15:a81a8d6c1dfe | 66 | } |
mbed_official | 15:a81a8d6c1dfe | 67 | |
mbed_official | 15:a81a8d6c1dfe | 68 | PortGroup *const port_base = (PortGroup*)port_get_group_from_gpio_pin(port * 32); // 32 pins in port // function reused to get the port base |
mbed_official | 15:a81a8d6c1dfe | 69 | if(port_base == NULL) return; /* returns NULL if invalid*/ |
mbed_official | 15:a81a8d6c1dfe | 70 | switch (port) { |
mbed_official | 15:a81a8d6c1dfe | 71 | case PortA: |
mbed_official | 15:a81a8d6c1dfe | 72 | obj->mask = (uint32_t)mask & PORTA_MASK; |
mbed_official | 15:a81a8d6c1dfe | 73 | break; |
mbed_official | 15:a81a8d6c1dfe | 74 | case PortB: |
mbed_official | 15:a81a8d6c1dfe | 75 | obj->mask = (uint32_t)mask & PORTB_MASK; |
mbed_official | 15:a81a8d6c1dfe | 76 | break; |
mbed_official | 15:a81a8d6c1dfe | 77 | #if defined(TARGET_SAMR21G18A) |
mbed_official | 15:a81a8d6c1dfe | 78 | case PortC: |
mbed_official | 15:a81a8d6c1dfe | 79 | obj->mask = (uint32_t)mask & PORTC_MASK; |
mbed_official | 15:a81a8d6c1dfe | 80 | break; |
mbed_official | 15:a81a8d6c1dfe | 81 | #endif |
mbed_official | 15:a81a8d6c1dfe | 82 | default: |
mbed_official | 15:a81a8d6c1dfe | 83 | return; |
mbed_official | 15:a81a8d6c1dfe | 84 | } |
mbed_official | 15:a81a8d6c1dfe | 85 | start = start_pin(port); |
mbed_official | 15:a81a8d6c1dfe | 86 | if(start == NC) |
mbed_official | 15:a81a8d6c1dfe | 87 | return; |
mbed_official | 15:a81a8d6c1dfe | 88 | obj->port = port; |
mbed_official | 15:a81a8d6c1dfe | 89 | obj->direction = dir; |
mbed_official | 15:a81a8d6c1dfe | 90 | obj->powersave = pin_conf.powersave; |
mbed_official | 15:a81a8d6c1dfe | 91 | obj->mode = PORT_PIN_PULL_UP; |
mbed_official | 15:a81a8d6c1dfe | 92 | |
mbed_official | 15:a81a8d6c1dfe | 93 | for (i = start, j = 0; j < 32; i++, j++) { |
mbed_official | 15:a81a8d6c1dfe | 94 | if (obj->mask & (1<<j)) { |
mbed_official | 15:a81a8d6c1dfe | 95 | port_pin_set_config((PinName)i, &pin_conf); |
mbed_official | 15:a81a8d6c1dfe | 96 | } |
mbed_official | 15:a81a8d6c1dfe | 97 | } |
mbed_official | 15:a81a8d6c1dfe | 98 | obj->OUTCLR = &port_base->OUTCLR.reg; |
mbed_official | 15:a81a8d6c1dfe | 99 | obj->OUTSET = &port_base->OUTSET.reg; |
mbed_official | 15:a81a8d6c1dfe | 100 | obj->IN = &port_base->IN.reg; |
mbed_official | 15:a81a8d6c1dfe | 101 | obj->OUT = &port_base->OUT.reg; |
mbed_official | 15:a81a8d6c1dfe | 102 | } |
mbed_official | 15:a81a8d6c1dfe | 103 | |
mbed_official | 15:a81a8d6c1dfe | 104 | void port_mode(port_t *obj, PinMode mode) |
mbed_official | 15:a81a8d6c1dfe | 105 | { |
mbed_official | 15:a81a8d6c1dfe | 106 | MBED_ASSERT(obj); |
mbed_official | 15:a81a8d6c1dfe | 107 | int i, j; |
mbed_official | 15:a81a8d6c1dfe | 108 | int start; |
mbed_official | 15:a81a8d6c1dfe | 109 | start = start_pin(obj->port); |
mbed_official | 15:a81a8d6c1dfe | 110 | if(start == NC) |
mbed_official | 15:a81a8d6c1dfe | 111 | return; |
mbed_official | 15:a81a8d6c1dfe | 112 | for (i = start, j = 0; j < 32; i++, j++) { |
mbed_official | 15:a81a8d6c1dfe | 113 | if (obj->mask & (1<<j)) { |
mbed_official | 15:a81a8d6c1dfe | 114 | pin_mode((PinName)i , mode); |
mbed_official | 15:a81a8d6c1dfe | 115 | } |
mbed_official | 15:a81a8d6c1dfe | 116 | } |
mbed_official | 15:a81a8d6c1dfe | 117 | } |
mbed_official | 15:a81a8d6c1dfe | 118 | |
mbed_official | 15:a81a8d6c1dfe | 119 | void port_dir(port_t *obj, PinDirection dir) |
mbed_official | 15:a81a8d6c1dfe | 120 | { |
mbed_official | 15:a81a8d6c1dfe | 121 | MBED_ASSERT(obj); |
mbed_official | 15:a81a8d6c1dfe | 122 | struct port_config pin_conf; |
mbed_official | 15:a81a8d6c1dfe | 123 | int i, j; |
mbed_official | 15:a81a8d6c1dfe | 124 | int start; |
mbed_official | 15:a81a8d6c1dfe | 125 | |
mbed_official | 15:a81a8d6c1dfe | 126 | switch (dir) { |
mbed_official | 15:a81a8d6c1dfe | 127 | case PIN_INPUT : |
mbed_official | 15:a81a8d6c1dfe | 128 | pin_conf.direction = PORT_PIN_DIR_INPUT; |
mbed_official | 15:a81a8d6c1dfe | 129 | break; |
mbed_official | 15:a81a8d6c1dfe | 130 | case PIN_OUTPUT: |
mbed_official | 15:a81a8d6c1dfe | 131 | pin_conf.direction = PORT_PIN_DIR_OUTPUT; |
mbed_official | 15:a81a8d6c1dfe | 132 | break; |
mbed_official | 15:a81a8d6c1dfe | 133 | case PIN_INPUT_OUTPUT: |
mbed_official | 15:a81a8d6c1dfe | 134 | pin_conf.direction = PORT_PIN_DIR_OUTPUT_WTH_READBACK; |
mbed_official | 15:a81a8d6c1dfe | 135 | break; |
mbed_official | 15:a81a8d6c1dfe | 136 | default: |
mbed_official | 15:a81a8d6c1dfe | 137 | return; |
mbed_official | 15:a81a8d6c1dfe | 138 | } |
mbed_official | 15:a81a8d6c1dfe | 139 | start = start_pin(obj->port); |
mbed_official | 15:a81a8d6c1dfe | 140 | if(start == NC) |
mbed_official | 15:a81a8d6c1dfe | 141 | return; |
mbed_official | 15:a81a8d6c1dfe | 142 | obj->direction = dir; |
mbed_official | 64:41a834223ea3 | 143 | pin_conf.input_pull = (enum port_pin_pull)obj->mode; |
mbed_official | 15:a81a8d6c1dfe | 144 | pin_conf.powersave = obj->powersave; |
mbed_official | 15:a81a8d6c1dfe | 145 | |
mbed_official | 15:a81a8d6c1dfe | 146 | for (i = start, j = 0; j < 32; i++, j++) { |
mbed_official | 15:a81a8d6c1dfe | 147 | if (obj->mask & (1<<j)) { |
mbed_official | 15:a81a8d6c1dfe | 148 | port_pin_set_config((PinName)i, &pin_conf); |
mbed_official | 15:a81a8d6c1dfe | 149 | } |
mbed_official | 15:a81a8d6c1dfe | 150 | } |
mbed_official | 15:a81a8d6c1dfe | 151 | } |
mbed_official | 15:a81a8d6c1dfe | 152 | |
mbed_official | 15:a81a8d6c1dfe | 153 | void port_write(port_t *obj, int value) |
mbed_official | 15:a81a8d6c1dfe | 154 | { |
mbed_official | 15:a81a8d6c1dfe | 155 | MBED_ASSERT(obj); |
mbed_official | 15:a81a8d6c1dfe | 156 | int i; |
mbed_official | 15:a81a8d6c1dfe | 157 | int start; |
mbed_official | 15:a81a8d6c1dfe | 158 | start = start_pin(obj->port); |
mbed_official | 15:a81a8d6c1dfe | 159 | if(start == NC) |
mbed_official | 15:a81a8d6c1dfe | 160 | return; |
mbed_official | 15:a81a8d6c1dfe | 161 | for (i = 0; i < 32 ; i++) { |
mbed_official | 15:a81a8d6c1dfe | 162 | if (obj->mask & (1<<i)) { |
mbed_official | 15:a81a8d6c1dfe | 163 | if (value & (1<<i)) { |
mbed_official | 15:a81a8d6c1dfe | 164 | *obj->OUTSET = 1 << i; |
mbed_official | 15:a81a8d6c1dfe | 165 | } else { |
mbed_official | 15:a81a8d6c1dfe | 166 | *obj->OUTCLR = 1 << i; |
mbed_official | 15:a81a8d6c1dfe | 167 | } |
mbed_official | 15:a81a8d6c1dfe | 168 | } |
mbed_official | 15:a81a8d6c1dfe | 169 | } |
mbed_official | 15:a81a8d6c1dfe | 170 | } |
mbed_official | 15:a81a8d6c1dfe | 171 | |
mbed_official | 15:a81a8d6c1dfe | 172 | int port_read(port_t *obj) |
mbed_official | 15:a81a8d6c1dfe | 173 | { |
mbed_official | 15:a81a8d6c1dfe | 174 | MBED_ASSERT(obj); |
mbed_official | 15:a81a8d6c1dfe | 175 | if (obj->direction == PIN_OUTPUT) { |
mbed_official | 15:a81a8d6c1dfe | 176 | return (*obj->OUT & obj->mask); |
mbed_official | 15:a81a8d6c1dfe | 177 | } else { |
mbed_official | 15:a81a8d6c1dfe | 178 | return (*obj->IN & obj->mask); |
mbed_official | 15:a81a8d6c1dfe | 179 | } |
mbed_official | 15:a81a8d6c1dfe | 180 | } |