text: /*
   * Copyright (c) 2014-2016, Alex Taradov <alex@taradov.com>
   * All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions are met:
   *
   * 1. Redistributions of source code must retain the above copyright notice,
   *    this list of conditions and the following disclaimer.
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in the
   *    documentation and/or other materials provided with the distribution.
   * 3. The name of the author may not be used to endorse or promote products
   *    derived from this software without specific prior written permission.
   *
   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   * POSSIBILITY OF SUCH DAMAGE.
   */
  
  #ifndef _HAL_GPIO_H_
  #define _HAL_GPIO_H_
  
  /*- Definitions -------------------------------------------------------------*/
  #define HAL_GPIO_PORTA       0
  #define HAL_GPIO_PORTB       1
  #define HAL_GPIO_PORTC       2
  
  #define HAL_GPIO_PMUX_A      0
  #define HAL_GPIO_PMUX_B      1
  #define HAL_GPIO_PMUX_C      2
  #define HAL_GPIO_PMUX_D      3
  #define HAL_GPIO_PMUX_E      4
  #define HAL_GPIO_PMUX_F      5
  #define HAL_GPIO_PMUX_G      6
  #define HAL_GPIO_PMUX_H      7
  #define HAL_GPIO_PMUX_I      8
  
  #define HAL_GPIO_PIN(name, port, pin)						\
    static inline void HAL_GPIO_##name##_set(void)				\
    {										\
      PORT->Group[HAL_GPIO_PORT##port].OUTSET.reg = (1 << pin);			\
      (void)HAL_GPIO_##name##_set;						\
    }										\
  										\
    static inline void HAL_GPIO_##name##_clr(void)				\
    {										\
      PORT->Group[HAL_GPIO_PORT##port].OUTCLR.reg = (1 << pin);			\
      (void)HAL_GPIO_##name##_clr;						\
    }										\
  										\
    static inline void HAL_GPIO_##name##_toggle(void)				\
    {										\
      PORT->Group[HAL_GPIO_PORT##port].OUTTGL.reg = (1 << pin);			\
      (void)HAL_GPIO_##name##_toggle;						\
    }										\
  										\
    static inline void HAL_GPIO_##name##_write(int value)				\
    {										\
      if (value)									\
        PORT->Group[HAL_GPIO_PORT##port].OUTSET.reg = (1 << pin);			\
      else									\
        PORT->Group[HAL_GPIO_PORT##port].OUTCLR.reg = (1 << pin);			\
      (void)HAL_GPIO_##name##_write;						\
    }										\
  										\
    static inline void HAL_GPIO_##name##_in(void)					\
    {										\
      PORT->Group[HAL_GPIO_PORT##port].DIRCLR.reg = (1 << pin);			\
      PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_INEN;	\
      PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg &= ~PORT_PINCFG_PULLEN;	\
      (void)HAL_GPIO_##name##_in;							\
    }										\
  										\
    static inline void HAL_GPIO_##name##_out(void)				\
    {										\
      PORT->Group[HAL_GPIO_PORT##port].DIRSET.reg = (1 << pin);			\
      PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_INEN;	\
      (void)HAL_GPIO_##name##_out;						\
    }										\
  										\
    static inline void HAL_GPIO_##name##_pullup(void)				\
    {										\
      PORT->Group[HAL_GPIO_PORT##port].OUTSET.reg = (1 << pin);			\
      PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_PULLEN;	\
      (void)HAL_GPIO_##name##_pullup;						\
    }										\
  										\
    static inline void HAL_GPIO_##name##_pulldown(void)				\
    {										\
        PORT->Group[HAL_GPIO_PORT##port].OUTCLR.reg = (1 << pin);			\
        PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_PULLEN;	\
        (void)HAL_GPIO_##name##_pullup;						\
    }										\
                                          \
    static inline int HAL_GPIO_##name##_read(void)				\
    {										\
      return (PORT->Group[HAL_GPIO_PORT##port].IN.reg & (1 << pin)) != 0;		\
      (void)HAL_GPIO_##name##_read;						\
    }										\
  										\
    static inline int HAL_GPIO_##name##_state(void)				\
    {										\
      return (PORT->Group[HAL_GPIO_PORT##port].DIR.reg & (1 << pin)) != 0;	\
      (void)HAL_GPIO_##name##_state;						\
    }										\
  										\
    static inline void HAL_GPIO_##name##_pmuxen(int mux)				\
    {										\
      PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg |= PORT_PINCFG_PMUXEN;	\
      if (pin & 1)								\
        PORT->Group[HAL_GPIO_PORT##port].PMUX[pin>>1].bit.PMUXO = mux;		\
      else									\
        PORT->Group[HAL_GPIO_PORT##port].PMUX[pin>>1].bit.PMUXE = mux;		\
      (void)HAL_GPIO_##name##_pmuxen;						\
    }										\
  										\
    static inline void HAL_GPIO_##name##_pmuxdis(void)				\
    {										\
      PORT->Group[HAL_GPIO_PORT##port].PINCFG[pin].reg &= ~PORT_PINCFG_PMUXEN;	\
      (void)HAL_GPIO_##name##_pmuxdis;						\
    }										\
  
  
  HAL_GPIO_PIN(PIEZO_PIN, A, 17)
  HAL_GPIO_PIN(UART_TX,   A, 30)
  HAL_GPIO_PIN(UART_RX,   A, 31)
  
  HAL_GPIO_PIN(SCC_SDA,   A, 8)
  HAL_GPIO_PIN(SCC_SCL,   A, 9)
  
  HAL_GPIO_PIN(OLED_SCL,  A, 23)
  HAL_GPIO_PIN(OLED_SDA,  A, 22)
  
  HAL_GPIO_PIN(RC_PIN,    A, 16)
  
  HAL_GPIO_PIN(SIM_POWERKEY_PIN, A, 6)
  HAL_GPIO_PIN(SIM_ON_PIN, A, 7)
  
  HAL_GPIO_PIN(SIM_UART_TX,   A, 10)
  HAL_GPIO_PIN(SIM_UART_RX,   A, 11)
  
  HAL_GPIO_PIN(SIM_CTS_PIN,   A, 3)
  HAL_GPIO_PIN(SIM_STATUS_PIN,A, 5)
  
  HAL_GPIO_PIN(DOORSW_PIN,A, 4)
  HAL_GPIO_PIN(EXT_UART_TX,A, 14)
  HAL_GPIO_PIN(EXT_UART_RX,A, 15)
  HAL_GPIO_PIN(KEYPAD1,A, 18)
  HAL_GPIO_PIN(KEYPAD2,A, 19)
  HAL_GPIO_PIN(KEYPAD3,A, 24)
  HAL_GPIO_PIN(KEYPAD4,A, 25)
  HAL_GPIO_PIN(KEYPAD5,A, 27)
  HAL_GPIO_PIN(KEYPAD6,A, 28)
  
  
  #if (MASTER_PCB_v32==1)
  
  HAL_GPIO_PIN(SIM_RI_PIN,A, 2)
  
  #elif (MASTER_PCB_v32==0)
  
  HAL_GPIO_PIN(LED_RED,   A, 18)
  HAL_GPIO_PIN(SIM_AVAIL_PIN, A, 28)
  HAL_GPIO_PIN(SIM_RI_PIN,A, 0)
  
  #endif
  
  #endif // _HAL_GPIO_H_
  
  