Perfecting fpio allocation mechanism

This commit is contained in:
Bigbits
2019-03-23 17:50:45 +08:00
parent 680684e1ea
commit 418dd7bcd7
3 changed files with 68 additions and 8 deletions

View File

@@ -13,19 +13,71 @@
extern "C"{
#endif // __cplusplus
void pinMode(uint8_t dwPin, uint8_t dwMode){ //48个fpio 对应32个gpiohs
fpioa_set_function(dwPin, FUNC_GPIOHS0 + dwPin);
gpiohs_set_drive_mode(dwPin, (gpio_drive_mode_t)dwMode);
void pinMode(uint8_t dwPin, uint8_t dwMode){
int gpionum = getGpio(dwPin);
if(gpionum >= 0){
fpioa_function_t function = FUNC_GPIOHS0 + gpionum;
fpioa_set_function(dwPin, function);
gpiohs_set_drive_mode((uint8_t)gpionum, (gpio_drive_mode_t)dwMode);
gpiohs_set_pin((uint8_t)gpionum, GPIO_PV_LOW);
}
return ;
}
void digitalWrite(uint8_t dwPin, uint8_t dwVal){
gpiohs_set_pin(dwPin, (gpio_pin_value_t)dwVal);
int gpionum = getGpio_s(dwPin);
if(gpionum >= 0){
gpiohs_set_pin((uint8_t)gpionum, (gpio_pin_value_t)dwVal);
}
return ;
}
int digitalRead(uint8_t ulPin){
return (int)gpiohs_get_pin(ulPin);
int digitalRead(uint8_t dwPin){
int gpionum = getGpio_s(dwPin);
if(gpionum >= 0){
return (int)gpiohs_get_pin((uint8_t)gpionum);
}
return -1;
}
int getGpio(uint8_t fpioPin) //分配一个gpio给fpio 输入 fpio 返回 gpiohs号
{
fpioa_function_t function = fpioa_get_function_buy_io(fpioPin);
if(function <= FUNC_GPIOHS31 && function >= FUNC_GPIOHS0 )
{
return (int)(function - FUNC_GPIOHS0);
}else{
return find_unused_gpiohs_io();
}
}
int getGpio_s(uint8_t fpioPin)
{
fpioa_function_t function = fpioa_get_function_buy_io(fpioPin);
if(function <= FUNC_GPIOHS31 && function >= FUNC_GPIOHS0 )
{
return (int)(function - FUNC_GPIOHS0);
}else{
return -1;
}
}
fpioa_function_t fpioa_get_function_buy_io(uint8_t fpioPin)
{
return (fpioa_function_t)fpioa->io[fpioPin].ch_sel ;
}
int find_unused_gpiohs_io(void) //返回一个未使用的gpio ,失败返回-1
{
static fpioa_function_t function = FUNC_GPIOHS0;
int _io ;
if (function <= FUNC_GPIOHS31)
{
_io = (int)(function - FUNC_GPIOHS0);
function++;
return _io;
}
return -1;
}
#ifdef __cplusplus

View File

@@ -47,11 +47,16 @@ extern void digitalWrite( uint8_t dwPin, uint8_t dwVal ) ;
/**
* \brief Reads the value from a specified digital pin, either HIGH or LOW.
*
* \param ulPin The number of the digital pin you want to read (int)
* \param dwPin The number of the digital pin you want to read (int)
*
* \return HIGH or LOW
*/
extern int digitalRead( uint8_t ulPin ) ;
extern int digitalRead( uint8_t dwPin ) ;
int getGpio(uint8_t fpioPin) ;
int getGpio_s(uint8_t fpioPin) ;
fpioa_function_t fpioa_get_function_buy_io(uint8_t fpioPin) ;
int find_unused_gpiohs_io(void) ;
#ifdef __cplusplus
} // extern "C"

View File

@@ -20,6 +20,9 @@
#define PIN_LED_RED 14
#define PIN_LED 13
#define LED_BUILTIN 13
#define LED_GREEN 13
#define LED_BLUE 12
#define LED_RED 14
/* KEY */
#define KEY0 16
/* UART */