/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2022. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */

/******************************************************************************
 * @version: V1.0.0  demo
 ******************************************************************************/

/*!
 * @file gpio.c
 *
 * @brief This file contains the user gpio function..
 *
 */

/* ===========================================  Includes  =========================================== */
#include "gpio.h"
#include "stdbool.h"
#include "spi.h"

/* ============================================  Define  ============================================ */


/* ===========================================  Typedef  ============================================ */


/* ==========================================  Variables  =========================================== */


/* ====================================  Functions declaration  ===================================== */


/* ======================================  Functions define  ======================================== */
/*!
* @brief   user key callback function
*
* @param   device:
* @param   wpara: pin;
* @param   lpara: 0;
*
* @return  void
*/
static void EXTI_Key_Callback(void *device, uint32_t wpara, uint32_t lpara)
{
    g_spiTxBuff[0]++;
    LED1_TOGGLE;
    LED2_TOGGLE;
}
/*!
 * @brief   Led initialization
 *
 * @param   none
 * @return  none
 */
void GPIO_LedInit(void)
{
    /*! 初始化引脚功能,如果引脚上电后默认为GPIO,可省略掉初始化步骤.
      有部分引脚上电默认为非GPIO,则必须选择其功能为GPIO才能作为GPIO使用.*/
    GPIO_SetFunc(LED1_PORT, LED1_PIN, GPIO_FUN0);/*! 功能复用选择.*/
    GPIO_SetFunc(LED2_PORT, LED2_PIN, GPIO_FUN0);

    /*! 设置LED引脚为GPIO输出.*/
    GPIO_SetDir(LED1_PORT, LED1_PIN, GPIO_OUT);
    GPIO_SetDir(LED2_PORT, LED2_PIN, GPIO_OUT);

    /*! 上电默认LED熄灭.*/
    LED1_OFF;
    LED2_OFF;
}

/*!
 * @brief   key initialization
 *
 * @param   none
 * @return  none
 */
void GPIO_KeyInit(void)
{
    /*! 初始化引脚功能,如果引脚上电后默认为GPIO,可省略掉初始化步骤.
      有部分引脚上电默认为非GPIO,则必须选择其功能为GPIO才能作为GPIO使用.*/
    GPIO_SetFunc(KEY_PORT, KEY_PIN, GPIO_FUN0); /*! 功能复用选择.*/

    /*! 设置按键引脚为输入,检测按键动作.*/
    GPIO_SetDir(KEY_PORT, KEY_PIN, GPIO_IN);

    /*! 使能按键引脚的上拉.*/
    GPIO_SetPullup(KEY_PORT, KEY_PIN, ENABLE);

    /*! 使能按键外部中断,设置为下降沿产生中断,当按下按键时产生中断.*/
    GPIO_EnableExtInterrupt(KEY_PORT, KEY_PIN, EXTI_TRIGGER_FALLING);

    /*设置外部中断,中断回调函数.*/
    GPIO_SetCallback(KEY_PIN, EXTI_Key_Callback);

}


/* =============================================  EOF  ============================================== */
