изменения для ускорения функций digital

This commit is contained in:
KLASSENTS 2025-03-14 12:19:20 +07:00
parent 8093d3a157
commit ca11ae5dbd
2 changed files with 37 additions and 44 deletions

View File

@ -32,6 +32,13 @@ extern "C" {
#include "mik32_hal_gpio.h" #include "mik32_hal_gpio.h"
#include "mik32_hal_timer32.h" #include "mik32_hal_timer32.h"
// total number of pins available for initialization
#define PINS_COMMON_QTY 26
#define pinCommonQty() (PINS_COMMON_QTY)
extern bool spi0NssPinIsBlocked;
extern bool spi1NssPinIsBlocked;
// analog pins // analog pins
#define PIN_A0 (14) #define PIN_A0 (14)
#define PIN_A1 (15) #define PIN_A1 (15)
@ -63,8 +70,6 @@ static const uint8_t A7 = PIN_A7;
GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber); GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber);
// determines the pin address inside the port by the board pin number // determines the pin address inside the port by the board pin number
HAL_PinsTypeDef digitalPinToBitMask(uint32_t digPinNumber); HAL_PinsTypeDef digitalPinToBitMask(uint32_t digPinNumber);
// total number of pins available for initialization
uint16_t pinCommonQty(void);
// the function returns a reference to the OUTPUT address of the GPIO register // the function returns a reference to the OUTPUT address of the GPIO register
volatile uint32_t* portOutputRegister(GPIO_TypeDef* GPIO_x); volatile uint32_t* portOutputRegister(GPIO_TypeDef* GPIO_x);
// the function returns a reference to the STATE address of the GPIO register // the function returns a reference to the STATE address of the GPIO register
@ -84,8 +89,15 @@ uint32_t analogInputToChannelNumber(uint32_t PinNumber);
// PWM // PWM
#define PWM_FREQUENCY_MAX 1000000 // Hz #define PWM_FREQUENCY_MAX 1000000 // Hz
bool digitalPinHasPWM(uint8_t p); static inline __attribute__((always_inline)) bool digitalPinHasPWM(uint8_t p)
bool digitalPinPwmIsOn(uint8_t digitalPin); // use only if digitalPinHasPWM() == true {
// if spi is in use D9 or D10 cannot work as pwm
if (p == 3 || p == 5 || p == 6 || (p >= 11 && p <= 13) || ((p == 9) && !spi0NssPinIsBlocked) || ((p == 10) && !spi1NssPinIsBlocked))
return true;
return false;
}
// return true if digitalPin configured as pwm
bool digitalPinPwmIsOn(uint8_t digitalPin);
// determines which timer the pin belongs to // determines which timer the pin belongs to
TIMER32_TypeDef* pwmPinToTimer(uint32_t digPinNumber); TIMER32_TypeDef* pwmPinToTimer(uint32_t digPinNumber);
// determines which timer channel the pin belongs to // determines which timer channel the pin belongs to

View File

@ -65,9 +65,8 @@ const HAL_PinsTypeDef digitalPinToGpioPinArray[] =
}; };
// determines the address of the port by the board pin number to which this pin belongs on the MCU // determines the address of the port by the board pin number to which this pin belongs on the MCU
GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber) __attribute__((noinline, section(".ram_text"))) GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber)
{ {
GPIO_TypeDef* gpioNum = NULL;
if (digPinNumber >= pinCommonQty()) if (digPinNumber >= pinCommonQty())
{ {
ErrorMsgHandler("digitalPinToPort(): pin number exceeds the total number of pins"); ErrorMsgHandler("digitalPinToPort(): pin number exceeds the total number of pins");
@ -76,41 +75,32 @@ GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber)
// port 2 - led (22) // port 2 - led (22)
if (digPinNumber == LED_BUILTIN) if (digPinNumber == LED_BUILTIN)
gpioNum = GPIO_2; return GPIO_2;
// port 1 - board pins 7, 8, 10...13, 14(А0), 15(А1), 18,19 // port 1 - board pins 7, 8, 10...13, 14(А0), 15(А1), 18,19
else if (digPinNumber == 7 || digPinNumber == 8 || (digPinNumber >= 10 && digPinNumber <= 15) else if (digPinNumber == 7 || digPinNumber == 8 || (digPinNumber >= 10 && digPinNumber <= 15)
|| digPinNumber == 18 || digPinNumber == 19 || (digPinNumber == 9 && spi0NssPinIsBlocked)) || digPinNumber == 18 || digPinNumber == 19 || (digPinNumber == 9 && spi0NssPinIsBlocked))
gpioNum = GPIO_1; return GPIO_1;
// port 0 - board pins 0...6, 9, 16(A2), 17(A3), 20(A4), 21(A5), 24(A6), 25(A7) // port 0 - board pins 0...6, 9, 16(A2), 17(A3), 20(A4), 21(A5), 24(A6), 25(A7)
else else
gpioNum = GPIO_0; return GPIO_0;
return gpioNum;
} }
// determines the pin address inside the port by the board pin number // determines the pin address inside the port by the board pin number
HAL_PinsTypeDef digitalPinToBitMask(uint32_t digPinNumber) __attribute__((noinline, section(".ram_text"))) HAL_PinsTypeDef digitalPinToBitMask(uint32_t digPinNumber)
{ {
if (digPinNumber >= pinCommonQty()) if (digPinNumber >= pinCommonQty())
{ {
ErrorMsgHandler("digitalPinToBitMask(): pin number exceeds the total number of pins"); ErrorMsgHandler("digitalPinToBitMask(): pin number exceeds the total number of pins");
return NC; return NOT_A_PIN;
} }
HAL_PinsTypeDef mask;
// if spi is on default pin NSS_IN is needed for spi, board pin is replaced to pin NSS_OUT // if spi is on default pin NSS_IN is needed for spi, board pin is replaced to pin NSS_OUT
if ((digPinNumber == 10) && spi1NssPinIsBlocked) if ((digPinNumber == 10) && spi1NssPinIsBlocked)
mask = SPI1_NSS_OUT_PIN; return SPI1_NSS_OUT_PIN;
else if ((digPinNumber == 9) && spi0NssPinIsBlocked) else if ((digPinNumber == 9) && spi0NssPinIsBlocked)
mask = SPI0_NSS_OUT_PIN; return SPI0_NSS_OUT_PIN;
else else
mask = digitalPinToGpioPinArray[digPinNumber]; return digitalPinToGpioPinArray[digPinNumber];
return mask;
}
uint16_t pinCommonQty(void)
{
return (uint16_t)(sizeof(digitalPinToGpioPinArray)/sizeof(digitalPinToGpioPinArray[0]));
} }
// the function returns a reference to the OUTPUT address of the GPIO register // the function returns a reference to the OUTPUT address of the GPIO register
@ -220,6 +210,8 @@ uint32_t analogInputToChannelNumber(uint32_t PinNumber)
// use only if digitalPinHasPWM() == true // use only if digitalPinHasPWM() == true
// return true if digitalPin configured as pwm // return true if digitalPin configured as pwm
bool digitalPinPwmIsOn(uint8_t digitalPin) bool digitalPinPwmIsOn(uint8_t digitalPin)
{
if (digitalPinHasPWM(digitalPin))
{ {
uint8_t config = 0; uint8_t config = 0;
uint8_t pinShift = PIN_MASK_TO_PIN_NUMBER(digitalPinToBitMask(digitalPin)); uint8_t pinShift = PIN_MASK_TO_PIN_NUMBER(digitalPinToBitMask(digitalPin));
@ -231,19 +223,8 @@ bool digitalPinPwmIsOn(uint8_t digitalPin)
if (config == 2) if (config == 2)
return true; return true;
else
return false;
} }
return false;
bool digitalPinHasPWM(uint8_t p)
{
bool ret = false;
// if spi is in use D10 cannot work as pwm
if (((p == 9) && spi0NssPinIsBlocked) || ((p == 10) && spi1NssPinIsBlocked))
ret = false;
else if (p == 3 || p == 5 || p == 6 || (p >= 9 && p <= 13))
ret = true;
return ret;
} }
// function is used only if digitalPinHasPWM() is true // function is used only if digitalPinHasPWM() is true