/*********************************************************************************
 * Copyright(C),Ningbo Joyson Preh Car Connect Co.,Ltd.
 * FileName:    Gpio_drv.c
 * Author:      Zheng shijie
 * Version :    Ver0.1
 * Date:        2020-10-09
 * Description:
 *
 *********************************************************************************/
#include "Gpio_drv.h"

/*
 * Gpio_drv.c
 *
 *  Created on: 2020?9?27?
 *      Author: zheng_s2
 */

#include "Gpio_drv.h"

void GPIO_Port_Clk_Enable(uint8_t group_index, uint8_t state)
{
	//parameter1:PCC_PORTA_INDEX~PCC_PORTD_INDEX
	//parameter2:ENABLE or Disable
	if(state == CLK_ENABLE)
	{
		PCC->PCCn[group_index] = PCC_PCCn_CGC(1);
	}
	else
	{
		PCC->PCCn[group_index] = PCC_PCCn_CGC(0);
	}
}
void GPIO_Port_Function_Set(PORT_Type *port_index, uint8_t pin_index, uint8_t pin_function)
{
	//parameter1:PORTA
	//parameter2:PIN_INDEX_11:PTA_11
	//parameter3:PIN_FUNCTION_GPIO
	port_index->PCR[pin_index] |= PORT_PCR_MUX(pin_function);
}

void GPIO_Port_Dir_Set(GPIO_Type *port_index, uint8_t pin_index, uint8_t dir)
{
	//parameter1:PTA
	//parameter2:PIN_INDEX_11:PTA_11
	//parameter3:DIR_OUTPUT
	port_index->PDDR |= (dir << pin_index);
}

void GPIO_Port_Lev_Set(GPIO_Type *port_index, uint8_t pin_index, uint8_t lev)
{
	//parameter1:PTA
	//parameter2:PIN_INDEX_11:PTA_11
	//parameter3:PORT_HIGH or PORT_LOW
	if(lev == PORT_HIGH)
	{
		port_index->PSOR |= (1 << pin_index);
	}
	else if(lev == PORT_LOW)
	{
		port_index->PCOR |= (1 << pin_index);
	}
}
uint8_t GPIO_INPUT_Port_Lev_Read(GPIO_Type *port_index, uint8_t pin_index)
{
	//parameter1:PTA
	//parameter2:PIN_INDEX_11:PTA_11
	uint8_t retval;

	if( port_index->PDIR & (1 << pin_index) )
	{
		retval = 1;
	}
	else
	{
		retval = 0;
	}
	return retval;
}
