|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2016, Alex Taradov <[email protected]> |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | + * this list of conditions and the following disclaimer. |
| 10 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer in the |
| 12 | + * documentation and/or other materials provided with the distribution. |
| 13 | + * 3. The name of the author may not be used to endorse or promote products |
| 14 | + * derived from this software without specific prior written permission. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + */ |
| 28 | + |
| 29 | +#ifndef _HAL_GPIO_H_ |
| 30 | +#define _HAL_GPIO_H_ |
| 31 | + |
| 32 | +/*- Definitions -------------------------------------------------------------*/ |
| 33 | +#define HAL_GPIO_PORTA 0 |
| 34 | +#define HAL_GPIO_PORTB 1 |
| 35 | +#define HAL_GPIO_PORTC 2 |
| 36 | + |
| 37 | +#define HAL_GPIO_PMUX_A 0 |
| 38 | +#define HAL_GPIO_PMUX_B 1 |
| 39 | +#define HAL_GPIO_PMUX_C 2 |
| 40 | +#define HAL_GPIO_PMUX_D 3 |
| 41 | +#define HAL_GPIO_PMUX_E 4 |
| 42 | +#define HAL_GPIO_PMUX_F 5 |
| 43 | +#define HAL_GPIO_PMUX_G 6 |
| 44 | +#define HAL_GPIO_PMUX_H 7 |
| 45 | +#define HAL_GPIO_PMUX_I 8 |
| 46 | + |
| 47 | +#define HAL_GPIO_PIN(name, port, pin) \ |
| 48 | + static inline void HAL_GPIO_##name##_set(void) \ |
| 49 | + { \ |
| 50 | + PORT->Group[HAL_GPIO_PORT##port].OUTSET.reg = (1 << pin); \ |
| 51 | + (void)HAL_GPIO_##name##_set; \ |
| 52 | + } \ |
| 53 | + \ |
| 54 | + static inline void HAL_GPIO_##name##_clr(void) \ |
| 55 | + { \ |
| 56 | + PORT->Group[HAL_GPIO_PORT##port].OUTCLR.reg = (1 << pin); \ |
| 57 | + (void)HAL_GPIO_##name##_clr; \ |
| 58 | + } \ |
| 59 | + \ |
| 60 | + static inline void HAL_GPIO_##name##_toggle(void) \ |
| 61 | + { \ |
| 62 | + PORT->Group[HAL_GPIO_PORT##port].OUTTGL.reg = (1 << pin); \ |
| 63 | + (void)HAL_GPIO_##name##_toggle; \ |
| 64 | + } \ |
| 65 | + \ |
| 66 | + static inline void HAL_GPIO_##name##_write(int value) \ |
| 67 | + { \ |
| 68 | + if (value) \ |
| 69 | + PORT->Group[HAL_GPIO_PORT##port].OUTSET.reg = (1 << pin); \ |
| 70 | + else \ |
| 71 | + PORT->Group[HAL_GPIO_PORT##port].OUTCLR.reg = (1 << pin); \ |
| 72 | + (void)HAL_GPIO_##name##_write; \ |
| 73 | + } \ |
| 74 | + \ |
| 75 | + static inline void HAL_GPIO_##name##_in(void) \ |
| 76 | + { \ |
| 77 | + PORT->Group[HAL_GPIO_PORT##port].DIRCLR.reg = (1 << pin); \ |
| 78 | + PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_INEN; \ |
| 79 | + PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg &= ~PORT_PINCFG_PULLEN; \ |
| 80 | + (void)HAL_GPIO_##name##_in; \ |
| 81 | + } \ |
| 82 | + \ |
| 83 | + static inline void HAL_GPIO_##name##_out(void) \ |
| 84 | + { \ |
| 85 | + PORT->Group[HAL_GPIO_PORT##port].DIRSET.reg = (1 << pin); \ |
| 86 | + PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_INEN; \ |
| 87 | + (void)HAL_GPIO_##name##_out; \ |
| 88 | + } \ |
| 89 | + \ |
| 90 | + static inline void HAL_GPIO_##name##_pullup(void) \ |
| 91 | + { \ |
| 92 | + PORT->Group[HAL_GPIO_PORT##port].OUTSET.reg = (1 << pin); \ |
| 93 | + PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_PULLEN; \ |
| 94 | + (void)HAL_GPIO_##name##_pullup; \ |
| 95 | + } \ |
| 96 | + \ |
| 97 | + static inline int HAL_GPIO_##name##_read(void) \ |
| 98 | + { \ |
| 99 | + return (PORT->Group[HAL_GPIO_PORT##port].IN.reg & (1 << pin)) != 0; \ |
| 100 | + (void)HAL_GPIO_##name##_read; \ |
| 101 | + } \ |
| 102 | + \ |
| 103 | + static inline int HAL_GPIO_##name##_state(void) \ |
| 104 | + { \ |
| 105 | + return (PORT->Group[HAL_GPIO_PORT##port].DIR.reg & (1 << pin)) != 0; \ |
| 106 | + (void)HAL_GPIO_##name##_state; \ |
| 107 | + } \ |
| 108 | + \ |
| 109 | + static inline void HAL_GPIO_##name##_pmuxen(int mux) \ |
| 110 | + { \ |
| 111 | + PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_PMUXEN; \ |
| 112 | + if (pin & 1) \ |
| 113 | + PORT->Group[HAL_GPIO_PORT##port].PMUX[pin>>1].bit.PMUXO = mux; \ |
| 114 | + else \ |
| 115 | + PORT->Group[HAL_GPIO_PORT##port].PMUX[pin>>1].bit.PMUXE = mux; \ |
| 116 | + (void)HAL_GPIO_##name##_pmuxen; \ |
| 117 | + } \ |
| 118 | + \ |
| 119 | + static inline void HAL_GPIO_##name##_pmuxdis(void) \ |
| 120 | + { \ |
| 121 | + PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg &= ~PORT_PINCFG_PMUXEN; \ |
| 122 | + (void)HAL_GPIO_##name##_pmuxdis; \ |
| 123 | + } \ |
| 124 | + |
| 125 | +#endif // _HAL_GPIO_H_ |
| 126 | + |
0 commit comments