rt-thread-official/bsp/ls1cdev/libraries/ls1c_gpio.c

208 lines
4.3 KiB
C
Raw Normal View History

// <20><>װgpio<69>ӿ<EFBFBD>
#include "ls1c_public.h"
#include "ls1c_regs.h"
#include "ls1c_gpio.h"
#include "ls1c_pin.h"
/*
* <EFBFBD><EFBFBD>ȡָ<EFBFBD><EFBFBD>gpio<EFBFBD><EFBFBD>CFG<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>
* @gpio gpio<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @ret CFG<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>
*/
volatile unsigned int *gpio_get_cfg_reg(unsigned int gpio)
{
volatile unsigned int *gpio_cfgx = NULL; // GPIO_CFGx<47>Ĵ<EFBFBD><C4B4><EFBFBD>
unsigned int port = GPIO_GET_PORT(gpio);
switch (port)
{
case 0:
gpio_cfgx = (volatile unsigned int *)LS1C_GPIO_CFG0;
break;
case 1:
gpio_cfgx = (volatile unsigned int *)LS1C_GPIO_CFG1;
break;
case 2:
gpio_cfgx = (volatile unsigned int *)LS1C_GPIO_CFG2;
break;
case 3:
gpio_cfgx = (volatile unsigned int *)LS1C_GPIO_CFG3;
break;
default:
gpio_cfgx = NULL;
break;
}
return gpio_cfgx;
}
/*
* <EFBFBD><EFBFBD>ȡָ<EFBFBD><EFBFBD>gpio<EFBFBD><EFBFBD>EN<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>
* @gpio gpio<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @ret EN<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>
*/
volatile unsigned int *gpio_get_en_reg(unsigned int gpio)
{
volatile unsigned int *gpio_enx = NULL; // GPIO_ENx<4E>Ĵ<EFBFBD><C4B4><EFBFBD>
unsigned int port = GPIO_GET_PORT(gpio);
switch (port)
{
case 0:
gpio_enx = (volatile unsigned int *)LS1C_GPIO_EN0;
break;
case 1:
gpio_enx = (volatile unsigned int *)LS1C_GPIO_EN1;
break;
case 2:
gpio_enx = (volatile unsigned int *)LS1C_GPIO_EN2;
break;
case 3:
gpio_enx = (volatile unsigned int *)LS1C_GPIO_EN3;
break;
default:
gpio_enx = NULL;
return gpio_enx;
}
return gpio_enx;
}
/*
* gpio<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
* @gpio gpio<EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD>ȡֵ<EFBFBD><EFBFBD>Χ[0, 127]
* @mode gpio<EFBFBD>Ĺ<EFBFBD><EFBFBD><EFBFBD>ģʽ(<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
*
* <EFBFBD><EFBFBD>: <EFBFBD><EFBFBD>gpio50<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* gpio_init(50, gpio_mode_output);
*/
void gpio_init(unsigned int gpio, gpio_mode_t mode)
{
volatile unsigned int *gpio_enx = NULL; // GPIO_ENx<4E>Ĵ<EFBFBD><C4B4><EFBFBD>
unsigned int pin = GPIO_GET_PIN(gpio);
// <20><>pin<69><6E>Ϊ<EFBFBD><CEAA>ͨGPIO
pin_set_purpose(gpio, PIN_PURPOSE_GPIO);
// <20><><EFBFBD><EFBFBD>gpio<69><6F><EFBFBD><EFBFBD>ģʽ(<28><><EFBFBD><EFBFBD><EBA1A2><EFBFBD><EFBFBD>)
gpio_enx = gpio_get_en_reg(gpio);
if (gpio_mode_output == mode) // <20><><EFBFBD><EFBFBD>
{
reg_clr_one_bit(gpio_enx, pin);
}
else // <20><><EFBFBD><EFBFBD>
{
reg_set_one_bit(gpio_enx, pin);
}
return ;
}
/*
* <EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>gpio<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD>ƽ<EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>ƽ
* @gpio gpio<EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD>ȡֵ<EFBFBD><EFBFBD>Χ[0, 127]
* @level <EFBFBD><EFBFBD>ƽֵ
*
* <EFBFBD><EFBFBD>: <EFBFBD><EFBFBD>gpio50<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>ƽ
* gpio_set(50, gpio_level_low);
*/
void gpio_set(unsigned int gpio, gpio_level_t level)
{
volatile unsigned int *gpio_outx = NULL; // GPIO_OUTx<54>Ĵ<EFBFBD><C4B4><EFBFBD>
unsigned int port = GPIO_GET_PORT(gpio);
unsigned int pin = GPIO_GET_PIN(gpio);
// <20><>ȡ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ
switch (port)
{
case 0:
gpio_outx = (volatile unsigned int *)LS1C_GPIO_OUT0;
break;
case 1:
gpio_outx = (volatile unsigned int *)LS1C_GPIO_OUT1;
break;
case 2:
gpio_outx = (volatile unsigned int *)LS1C_GPIO_OUT2;
break;
case 3:
gpio_outx = (volatile unsigned int *)LS1C_GPIO_OUT3;
break;
default: // <20><>ȷ<EFBFBD>ij<EFBFBD><C4B3><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD><EFBFBD>ֱ<EFA3AC>ӷ<EFBFBD><D3B7><EFBFBD>
return ;
}
// <20><><EFBFBD><EFBFBD>
if (gpio_level_low == level)
{
reg_clr_one_bit(gpio_outx, pin);
}
else
{
reg_set_one_bit(gpio_outx, pin);
}
return ;
}
/*
* <EFBFBD><EFBFBD>ȡָ<EFBFBD><EFBFBD>gpio<EFBFBD><EFBFBD><EFBFBD>ŵ<EFBFBD>ֵ
* @gpio gpio<EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD>ȡֵ<EFBFBD><EFBFBD>Χ[0,127]
*
* <EFBFBD><EFBFBD>: <EFBFBD><EFBFBD>ȡgpio50<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD>ֵ
* gpio_level_t level;
* level = gpio_get(50);
*/
unsigned int gpio_get(unsigned int gpio)
{
volatile unsigned int *gpio_inx = NULL; // GPIO_INx<4E>Ĵ<EFBFBD><C4B4><EFBFBD>
unsigned int port = GPIO_GET_PORT(gpio);
unsigned int pin = GPIO_GET_PIN(gpio);
// <20><>ȡ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ
switch (port)
{
case 0:
gpio_inx = (volatile unsigned int *)LS1C_GPIO_IN0;
break;
case 1:
gpio_inx = (volatile unsigned int *)LS1C_GPIO_IN1;
break;
case 2:
gpio_inx = (volatile unsigned int *)LS1C_GPIO_IN2;
break;
case 3:
gpio_inx = (volatile unsigned int *)LS1C_GPIO_IN3;
break;
default: // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̲<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD><EFBFBD>ֱ<EFA3AC>ӷ<EFBFBD><D3B7><EFBFBD>
return 0;
}
// <20><>ȡ
return reg_get_bit(gpio_inx, pin);
}