вспомогательные функции и функции digital вынесены в ram для увеличения быстродействия

This commit is contained in:
KLASSENTS 2025-03-14 11:17:47 +07:00
parent a417b70bde
commit ac51d0cae1
3 changed files with 69 additions and 68 deletions

View File

@ -95,7 +95,7 @@ void fastPinMode(uint32_t PinNumber, uint32_t PinMode)
} }
// write pin // write pin
void digitalWrite(uint32_t PinNumber, uint32_t Val) __attribute__((noinline, section(".ram_text"))) void digitalWrite(uint32_t PinNumber, uint32_t Val)
{ {
if ((PinNumber>=pinCommonQty())) if ((PinNumber>=pinCommonQty()))
{ {
@ -103,50 +103,46 @@ void digitalWrite(uint32_t PinNumber, uint32_t Val)
return; return;
} }
GPIO_TypeDef* port = digitalPinToPort(PinNumber);
HAL_PinsTypeDef pin = digitalPinToBitMask(PinNumber);
if (digitalPinPwmIsOn(PinNumber)) if (digitalPinPwmIsOn(PinNumber))
// if the pin can use PWM, disable PWM // if the pin can use PWM, disable PWM
analogWriteStop(PinNumber); analogWriteStop(PinNumber);
if (Val == HIGH) if (Val == HIGH)
GPIO_SET_PIN(port, pin); GPIO_SET_PIN(digitalPinToPort(PinNumber), digitalPinToBitMask(PinNumber));
else else
GPIO_CLEAR_PIN(port, pin); GPIO_CLEAR_PIN(digitalPinToPort(PinNumber), digitalPinToBitMask(PinNumber));
} }
// read pin // read pin
int digitalRead(uint32_t PinNumber) __attribute__((noinline, section(".ram_text"))) int digitalRead(uint32_t PinNumber)
{ {
if ((PinNumber>=pinCommonQty())) if ((PinNumber>=pinCommonQty()))
{ {
ErrorMsgHandler("digitalRead(): pin number exceeds the total number of pins"); ErrorMsgHandler("digitalRead(): pin number exceeds the total number of pins");
return -1; return -1;
} }
GPIO_TypeDef* port = digitalPinToPort(PinNumber);
HAL_PinsTypeDef pin = digitalPinToBitMask(PinNumber);
if (digitalPinPwmIsOn(PinNumber)) if (digitalPinPwmIsOn(PinNumber))
// if the pin can use PWM, disable PWM // if the pin can use PWM, disable PWM
analogWriteStop(PinNumber); analogWriteStop(PinNumber);
return GPIO_READ_PIN(port, pin); return GPIO_READ_PIN(digitalPinToPort(PinNumber), digitalPinToBitMask(PinNumber));
} }
// toggle pin // toggle pin
void digitalToggle(uint32_t PinNumber) __attribute__((noinline, section(".ram_text"))) void digitalToggle(uint32_t PinNumber)
{ {
if ((PinNumber>=pinCommonQty())) if ((PinNumber>=pinCommonQty()))
{ {
ErrorMsgHandler("digitalToggle(): pin number exceeds the total number of pins"); ErrorMsgHandler("digitalToggle(): pin number exceeds the total number of pins");
return; return;
} }
if (digitalPinHasPWM(PinNumber))
if (digitalPinPwmIsOn(PinNumber))
// if the pin can use PWM, disable PWM // if the pin can use PWM, disable PWM
analogWriteStop(PinNumber); analogWriteStop(PinNumber);
HAL_GPIO_TogglePin(digitalPinToPort(PinNumber), digitalPinToBitMask(PinNumber)); GPIO_TOGGLE_PIN(digitalPinToPort(PinNumber), digitalPinToBitMask(PinNumber));
} }
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -70,43 +70,10 @@ static const uint8_t A5 = PIN_A5;
#define LED_BUILTIN (22) #define LED_BUILTIN (22)
#define BTN_BUILTIN (23) #define BTN_BUILTIN (23)
// determines the address of the port by the board pin number to which this pin belongs on the MCU // determines the port address by the board pin number to which this pin belongs on the MCU
static inline __attribute__((always_inline)) GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber) GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber);
{
if (digPinNumber >= pinCommonQty())
{
ErrorMsgHandler("digitalPinToPort(): pin number exceeds the total number of pins");
return NULL;
}
// port 2 - led and button
if (digPinNumber == LED_BUILTIN || digPinNumber == BTN_BUILTIN)
return GPIO_2;
// port 1 - board pins 7, 8, 10...13, 14(А0), 15(А1), 18,19
else if (digPinNumber == 7 || digPinNumber == 8 || (digPinNumber >= 10 && digPinNumber <= 15)
|| digPinNumber == 18 || digPinNumber == 19 || (digPinNumber == 9 && spi0NssPinIsBlocked)) // 10 pieces
return GPIO_1;
// port 0 - board pins 0...6, 9, 16(A2), 17(A3), 20(A4), 21(A5)
else
return GPIO_0;
}
// determines the pin address inside the port by the board pin number // determines the pin address inside the port by the board pin number
static inline __attribute__((always_inline)) HAL_PinsTypeDef digitalPinToBitMask(uint32_t digPinNumber) HAL_PinsTypeDef digitalPinToBitMask(uint32_t digPinNumber);
{
if (digPinNumber >= pinCommonQty())
{
ErrorMsgHandler("digitalPinToBitMask(): pin number exceeds the total number of pins");
return NOT_A_PIN;
}
// if spi is on default pin NSS_IN is needed for spi, board pin is replaced to pin NSS_OUT
if ((digPinNumber == 10) && spi1NssPinIsBlocked)
return SPI1_NSS_OUT_PIN;
else if ((digPinNumber == 9) && spi0NssPinIsBlocked)
return SPI0_NSS_OUT_PIN;
else
return digitalPinToGpioPinArray[digPinNumber];
}
// 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
@ -136,25 +103,8 @@ static inline __attribute__((always_inline)) bool digitalPinHasPWM(uint8_t p)
return true; return true;
return false; return false;
} }
// return true if digitalPin configured as pwm // return true if digitalPin configured as pwm
static inline __attribute__((always_inline)) bool digitalPinPwmIsOn(uint8_t digitalPin) bool digitalPinPwmIsOn(uint8_t digitalPin);
{
if (digitalPinHasPWM(digitalPin))
{
uint8_t config = 0;
uint8_t pinShift = PIN_MASK_TO_PIN_NUMBER(digitalPinToBitMask(digitalPin));
if (PWM_PIN_TO_PORT_NUMBER(digitalPin) == 0)
config = PIN_GET_PAD_CONFIG(PORT_0_CFG, pinShift);
else
config = PIN_GET_PAD_CONFIG(PORT_1_CFG, pinShift);
if (config == 2)
return true;
}
return false;
}
// 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

@ -57,7 +57,44 @@ const HAL_PinsTypeDef digitalPinToGpioPinArray[PINS_COMMON_QTY] =
GPIO_PIN_6 // BTN(pin 23) GPIO_PIN_6 // BTN(pin 23)
}; };
// determines the address of the port by the board pin number to which this pin belongs on the MCU
__attribute__((noinline, section(".ram_text"))) GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber)
{
if (digPinNumber >= pinCommonQty())
{
ErrorMsgHandler("digitalPinToPort(): pin number exceeds the total number of pins");
return NULL;
}
// port 2 - led and button
if (digPinNumber == LED_BUILTIN || digPinNumber == BTN_BUILTIN)
return GPIO_2;
// port 1 - board pins 7, 8, 10...13, 14(А0), 15(А1), 18,19
else if (digPinNumber == 7 || digPinNumber == 8 || (digPinNumber >= 10 && digPinNumber <= 15)
|| digPinNumber == 18 || digPinNumber == 19 || (digPinNumber == 9 && spi0NssPinIsBlocked)) // 10 pieces
return GPIO_1;
// port 0 - board pins 0...6, 9, 16(A2), 17(A3), 20(A4), 21(A5)
else
return GPIO_0;
}
// determines the pin address inside the port by the board pin number
__attribute__((noinline, section(".ram_text"))) HAL_PinsTypeDef digitalPinToBitMask(uint32_t digPinNumber)
{
if (digPinNumber >= pinCommonQty())
{
ErrorMsgHandler("digitalPinToBitMask(): pin number exceeds the total number of pins");
return NOT_A_PIN;
}
// if spi is on default pin NSS_IN is needed for spi, board pin is replaced to pin NSS_OUT
if ((digPinNumber == 10) && spi1NssPinIsBlocked)
return SPI1_NSS_OUT_PIN;
else if ((digPinNumber == 9) && spi0NssPinIsBlocked)
return SPI0_NSS_OUT_PIN;
else
return digitalPinToGpioPinArray[digPinNumber];
}
// 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)
@ -125,6 +162,24 @@ uint32_t analogInputToChannelNumber(uint32_t PinNumber)
} }
// ---------------------- PWM ---------------------- // // ---------------------- PWM ---------------------- //
// return true if digitalPin configured as pwm
__attribute__((noinline, section(".ram_text"))) bool digitalPinPwmIsOn(uint8_t digitalPin)
{
if (digitalPinHasPWM(digitalPin))
{
uint8_t config = 0;
uint8_t pinShift = PIN_MASK_TO_PIN_NUMBER(digitalPinToBitMask(digitalPin));
if (PWM_PIN_TO_PORT_NUMBER(digitalPin) == 0)
config = PIN_GET_PAD_CONFIG(PORT_0_CFG, pinShift);
else
config = PIN_GET_PAD_CONFIG(PORT_1_CFG, pinShift);
if (config == 2)
return true;
}
return false;
}
// function is used only if digitalPinHasPWM() is true // function is used only if digitalPinHasPWM() is true
TIMER32_TypeDef* pwmPinToTimer(uint32_t digPinNumber) TIMER32_TypeDef* pwmPinToTimer(uint32_t digPinNumber)