/******************************************************************************
 * file    include/common.h 
 * author  YuLiang
 * version 1.0.0
 * date    24-Nov-2021
 * brief   This file provides all the headers of the gpio functions.
 
 ******************************************************************************
 * Attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2021 LandPower</center></h2>
 *
 * 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. Neither the name of LandPower nor the names of its contributors may 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 _HWGPIO_H_
#define _HWGPIO_H_

/* Includes ------------------------------------------------------------------*/

/* Define --------------------------------------------------------------------*/
#define GPIO_DIR_IN 1
#define GPIO_DIR_OUT 2

#define GPIO_RUN        79 // 3.15
#define GPIO_CAU1_POW   67 // 3.3
#define GPIO_485_EN_1       69
#define GPIO_485_EN_2       71
#define GPIO_4G_PWR     78//73  // 3.9-->3.14
#define GPIO_4G_RST     70  // 3.6
#define GPIO_4G_WAKUP   136 // 5.8
#define GPIO_WDT        123 // 4.27

/* Exported types ------------------------------------------------------------*/
/* 记录每个打开的gpio信息 */
typedef struct
{
    uint16_t index;         /* gpio在数组中的索引 */
    uint16_t gpio;          /* gpio号 gpio2-15 = (2 - 1) * 32 + 15 = 47 */
    uint8_t is_export;      /* 是否已导出 */
    uint8_t curr_dir;       /* 方向当前值 1-in 2-out */
    uint8_t curr_val;       /* 电平当前值 针对dir==out时  ,当dir为in时,每次都需要读文件 */
} gpio_node_t;

/* Exported macro ------------------------------------------------------------*/
#define GPIO_RUN_LED(_v_) gpio_val_set(gpio_run_idx, _v_)
#define GPIO_CAU1_POWER_ON gpio_val_set(gpio_cau1_pow_idx, 1)
#define GPIO_CAU1_POWER_OFF gpio_val_set(gpio_cau1_pow_idx, 0)

#define GPIO_485_1_CTRL(_v_)      gpio_val_set(gpio_485_en1_idx, _v_)
#define GPIO_485_2_CTRL(_v_)      gpio_val_set(gpio_485_en2_idx, _v_)
#define GPIO_USB_4G_PWR(_v_) gpio_val_set(gpio_usb_4g_pwr_idx, _v_)
#define GPIO_USB_4G_RST(_v_) gpio_val_set(gpio_usb_4g_rst_idx, _v_)
#define GPIO_USB_4G_WAKEUP(_v_) gpio_val_set(gpio_usb_4g_wakeup_idx, _v_)
#define GPIO_WDT_CTRL(_v_)      gpio_val_set(gpio_wdt_idx, _v_)


#define GPIO_ENABLE_SEND(_v_) { \
    GPIO_485_1_CTRL(_v_);    \
    GPIO_485_2_CTRL(_v_);    \
}

#define GPIO_SHUTDOWN_PWR() {   \
    GPIO_485_1_CTRL(0);     \
    GPIO_485_2_CTRL(1);      \
}

//#define GPIO_485_1_ENABLE_SEND  gpio_val_set(gpio_485_en1_idx, 1)
//#define GPIO_485_1_DISABLE_SEND  gpio_val_set(gpio_485_en1_idx, 0)
//#define GPIO_485_2_ENABLE_SEND  gpio_val_set(gpio_485_en2_idx, 1)
//#define GPIO_485_2_DISABLE_SEND  gpio_val_set(gpio_485_en2_idx, 0)



/* Extern global variables ---------------------------------------------------*/
extern int32_t gpio_run_idx;
extern int32_t gpio_cau1_pow_idx;
extern int32_t gpio_485_en1_idx;
extern int32_t gpio_485_en2_idx;
extern int32_t gpio_usb_4g_pwr_idx;
extern int32_t gpio_usb_4g_rst_idx;
extern int32_t gpio_usb_4g_wakeup_idx;
extern int32_t gpio_wdt_idx;

/* Extern functions ----------------------------------------------------------*/
extern int32_t gpio_val_set(uint16_t gpio, uint8_t value);
extern int32_t gpio_val_get(uint16_t gpio, uint8_t *value);
extern int32_t gpio_dir_set(uint16_t gpio, uint8_t dir);
extern int32_t gpio_export(uint16_t gpio);
extern int32_t gpio_init(void);

#endif
/************************ (C) COPYRIGHT LandPower ***** END OF FILE ****************/
