diff --git a/cores/arduino/wiring_digital.c b/cores/arduino/wiring_digital.c index bb270e5..3095e79 100644 --- a/cores/arduino/wiring_digital.c +++ b/cores/arduino/wiring_digital.c @@ -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 diff --git a/cores/arduino/wiring_digital.h b/cores/arduino/wiring_digital.h index b5a515b..036fab6 100644 --- a/cores/arduino/wiring_digital.h +++ b/cores/arduino/wiring_digital.h @@ -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" diff --git a/variants/standard/sipeedm1.h b/variants/standard/sipeedm1.h index 407b664..9d4d2ae 100644 --- a/variants/standard/sipeedm1.h +++ b/variants/standard/sipeedm1.h @@ -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 */