wiring digital speed optimization

Уменьшено время выполнения функций digitalRead/Write/Toggle для всех плат
This commit is contained in:
klassents 2025-03-17 07:53:28 +03:00
parent 0adc8293d7
commit 1aaf8c59ae
11 changed files with 146 additions and 160 deletions

View File

@ -36,6 +36,7 @@ extern "C" {
*/ */
typedef enum __HAL_PinsTypeDef typedef enum __HAL_PinsTypeDef
{ {
NOT_A_PIN = (0 << 0), /**< Не выбран пин. */
GPIO_PIN_0 = (1 << 0), /**< Выбран пин 0. */ GPIO_PIN_0 = (1 << 0), /**< Выбран пин 0. */
GPIO_PIN_1 = (1 << 1), /**< Выбран пин 1. */ GPIO_PIN_1 = (1 << 1), /**< Выбран пин 1. */
GPIO_PIN_2 = (1 << 2), /**< Выбран пин 2. */ GPIO_PIN_2 = (1 << 2), /**< Выбран пин 2. */

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,42 +103,46 @@ void digitalWrite(uint32_t PinNumber, uint32_t Val)
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_WritePin(digitalPinToPort(PinNumber), digitalPinToBitMask(PinNumber), (Val == HIGH) ? GPIO_PIN_HIGH : GPIO_PIN_LOW); if (Val == HIGH)
GPIO_SET_PIN(digitalPinToPort(PinNumber), digitalPinToBitMask(PinNumber));
else
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;
} }
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);
GPIO_PinState pinState = HAL_GPIO_ReadPin(digitalPinToPort(PinNumber), digitalPinToBitMask(PinNumber)); return GPIO_READ_PIN(digitalPinToPort(PinNumber), digitalPinToBitMask(PinNumber));
return (pinState == GPIO_PIN_LOW) ? LOW : HIGH;
} }
// 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

@ -65,7 +65,7 @@ static void __attribute__((noinline, section(".ram_text"))) mik32Show(GPIO_TypeD
// not support 400khz // not support 400khz
if (!is800KHz) return; if (!is800KHz) return;
if ((m_port != NULL) && (m_pin != NC)) if ((m_port != NULL) && (m_pin != NOT_A_PIN))
{ {
volatile uint32_t* set = &m_port->SET; volatile uint32_t* set = &m_port->SET;
volatile uint32_t* clr = &m_port->CLEAR; volatile uint32_t* clr = &m_port->CLEAR;

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
@ -219,31 +209,22 @@ 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) __attribute__((noinline, section(".ram_text"))) bool digitalPinPwmIsOn(uint8_t digitalPin)
{ {
uint8_t config = 0; if (digitalPinHasPWM(digitalPin))
uint8_t pinShift = PIN_MASK_TO_PIN_NUMBER(digitalPinToBitMask(digitalPin)); {
uint8_t config = 0;
uint8_t pinShift = PIN_MASK_TO_PIN_NUMBER(digitalPinToBitMask(digitalPin));
if (PWM_PIN_TO_PORT_NUMBER(digitalPin) == 0) if (PWM_PIN_TO_PORT_NUMBER(digitalPin) == 0)
config = PIN_GET_PAD_CONFIG(PORT_0_CFG, pinShift); config = PIN_GET_PAD_CONFIG(PORT_0_CFG, pinShift);
else else
config = PIN_GET_PAD_CONFIG(PORT_1_CFG, pinShift); config = PIN_GET_PAD_CONFIG(PORT_1_CFG, pinShift);
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

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 24
#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)
@ -47,7 +54,6 @@ static const uint8_t A3 = PIN_A3;
static const uint8_t A4 = PIN_A4; static const uint8_t A4 = PIN_A4;
static const uint8_t A5 = PIN_A5; static const uint8_t A5 = PIN_A5;
// digital pins // digital pins
// D0...D13, D18, D19 // D0...D13, D18, D19
@ -55,12 +61,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
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
@ -80,8 +84,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

@ -34,7 +34,7 @@ bool spi0NssPinIsBlocked = false;
bool spi1NssPinIsBlocked = false; bool spi1NssPinIsBlocked = false;
// list of pin numbers from both ports with pins inside the port // list of pin numbers from both ports with pins inside the port
const HAL_PinsTypeDef digitalPinToGpioPinArray[] = const HAL_PinsTypeDef digitalPinToGpioPinArray[PINS_COMMON_QTY] =
{ {
GPIO_PIN_5, // D0 GPIO_PIN_5, // D0
GPIO_PIN_6, // D1 GPIO_PIN_6, // D1
@ -62,10 +62,9 @@ const HAL_PinsTypeDef digitalPinToGpioPinArray[] =
GPIO_PIN_6 // BTN(pin 23) GPIO_PIN_6 // BTN(pin 23)
}; };
// etermines 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");
@ -74,41 +73,32 @@ GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber)
// port 2 - led and button // port 2 - led and button
if (digPinNumber == LED_BUILTIN || digPinNumber == BTN_BUILTIN) if (digPinNumber == LED_BUILTIN || digPinNumber == BTN_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)) // 10 pieces || digPinNumber == 18 || digPinNumber == 19 || (digPinNumber == 9 && spi0NssPinIsBlocked)) // 10 pieces
gpioNum = GPIO_1; return GPIO_1;
// port 0 - board pins 0...6, 9, 16(A2), 17(A3), 20(A4), 21(A5) // port 0 - board pins 0...6, 9, 16(A2), 17(A3), 20(A4), 21(A5)
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
@ -180,33 +170,23 @@ uint32_t analogInputToChannelNumber(uint32_t PinNumber)
// use only if digitalPinHasPWM() == true // use only if digitalPinHasPWM() == true
#define PWM_PIN_TO_PORT_NUMBER(pin) (((pin==10)||(pin==11)||(pin==12)||(pin==13)) ? 1:0) #define PWM_PIN_TO_PORT_NUMBER(pin) (((pin==10)||(pin==11)||(pin==12)||(pin==13)) ? 1:0)
// use only if digitalPinHasPWM() == true
// return true if digitalPin configured as pwm // return true if digitalPin configured as pwm
bool digitalPinPwmIsOn(uint8_t digitalPin) __attribute__((noinline, section(".ram_text"))) bool digitalPinPwmIsOn(uint8_t digitalPin)
{ {
uint8_t config = 0; if (digitalPinHasPWM(digitalPin))
uint8_t pinShift = PIN_MASK_TO_PIN_NUMBER(digitalPinToBitMask(digitalPin)); {
uint8_t config = 0;
uint8_t pinShift = PIN_MASK_TO_PIN_NUMBER(digitalPinToBitMask(digitalPin));
if (PWM_PIN_TO_PORT_NUMBER(digitalPin) == 0) if (PWM_PIN_TO_PORT_NUMBER(digitalPin) == 0)
config = PIN_GET_PAD_CONFIG(PORT_0_CFG, pinShift); config = PIN_GET_PAD_CONFIG(PORT_0_CFG, pinShift);
else else
config = PIN_GET_PAD_CONFIG(PORT_1_CFG, pinShift); config = PIN_GET_PAD_CONFIG(PORT_1_CFG, pinShift);
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 D9 or 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

View File

@ -71,6 +71,8 @@ typedef enum
P2_7, // 33 P2_7, // 33
PINS_COMMON_QTY, // 34 PINS_COMMON_QTY, // 34
} DigitalPinsTypeDef; } DigitalPinsTypeDef;
// total number of pins available for initialization
#define pinCommonQty() (PINS_COMMON_QTY)
// analog pins // analog pins
#define PIN_A0 (P1_5) #define PIN_A0 (P1_5)
@ -96,11 +98,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 digitalPinNumber); HAL_PinsTypeDef digitalPinToBitMask(uint32_t digitalPinNumber);
// total number of pins available for initialization
static inline uint16_t pinCommonQty(void)
{
return (uint16_t)PINS_COMMON_QTY;
}
// 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
@ -120,8 +117,12 @@ 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 {
return (p <= P1_15) && ((p & 0xF) < 4);
}
// 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

@ -21,7 +21,7 @@
* @return The address of the port corresponding to the board pin number. Can return 0 if the pin not exists * @return The address of the port corresponding to the board pin number. Can return 0 if the pin not exists
*/ */
GPIO_TypeDef *digitalPinToPort(uint32_t digitalPinNumber) __attribute__((noinline, section(".ram_text"))) GPIO_TypeDef *digitalPinToPort(uint32_t digitalPinNumber)
{ {
if (digitalPinNumber >= pinCommonQty()) if (digitalPinNumber >= pinCommonQty())
{ {
@ -40,12 +40,12 @@ GPIO_TypeDef *digitalPinToPort(uint32_t digitalPinNumber)
} }
// 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 digitalPinNumber) __attribute__((noinline, section(".ram_text"))) HAL_PinsTypeDef digitalPinToBitMask(uint32_t digitalPinNumber)
{ {
if (digitalPinNumber >= PINS_COMMON_QTY) if (digitalPinNumber >= PINS_COMMON_QTY)
{ {
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;
} }
if (digitalPinNumber >= P2_6) if (digitalPinNumber >= P2_6)
@ -110,27 +110,24 @@ 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) __attribute__((noinline, section(".ram_text"))) bool digitalPinPwmIsOn(uint8_t digitalPin)
{ {
uint8_t config = 0; if (digitalPinHasPWM(digitalPin))
uint8_t pinShift = digitalPin & 0x3; {
uint8_t config = 0;
uint8_t pinShift = digitalPin & 0x3;
if (PWM_PIN_TO_PORT_NUMBER(digitalPin) == 0) if (PWM_PIN_TO_PORT_NUMBER(digitalPin) == 0)
config = PIN_GET_PAD_CONFIG(PORT_0_CFG, pinShift); config = PIN_GET_PAD_CONFIG(PORT_0_CFG, pinShift);
else else
config = PIN_GET_PAD_CONFIG(PORT_1_CFG, pinShift); config = PIN_GET_PAD_CONFIG(PORT_1_CFG, pinShift);
if (config == 2) if (config == 2)
return true; return true;
else }
return false; return false;
} }
bool digitalPinHasPWM(uint8_t digitalPin)
{
return (digitalPin <= P1_15) && ((digitalPin & 0xF) < 4);
}
// 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)
{ {

View File

@ -78,6 +78,9 @@ typedef enum
PINS_COMMON_QTY, // 40 PINS_COMMON_QTY, // 40
} DigitalPinsTypeDef; } DigitalPinsTypeDef;
// total number of pins available for initialization
#define pinCommonQty() (PINS_COMMON_QTY)
// analog pins // analog pins
#define PIN_A0 (P1_5) #define PIN_A0 (P1_5)
#define PIN_A1 (P1_7) #define PIN_A1 (P1_7)
@ -108,11 +111,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 digitalPinNumber); HAL_PinsTypeDef digitalPinToBitMask(uint32_t digitalPinNumber);
// total number of pins available for initialization
static inline uint16_t pinCommonQty(void)
{
return (uint16_t)PINS_COMMON_QTY;
}
// 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
@ -132,8 +130,12 @@ 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 {
return (p < 32) && ((p & 0xF) < 4);
}
// 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

@ -21,7 +21,7 @@
* @return The address of the port corresponding to the board pin number. Can return 0 if the pin not exists * @return The address of the port corresponding to the board pin number. Can return 0 if the pin not exists
*/ */
GPIO_TypeDef *digitalPinToPort(uint32_t digitalPinNumber) __attribute__((noinline, section(".ram_text"))) GPIO_TypeDef *digitalPinToPort(uint32_t digitalPinNumber)
{ {
GPIO_TypeDef* port = NULL; GPIO_TypeDef* port = NULL;
if (digitalPinNumber < pinCommonQty()) if (digitalPinNumber < pinCommonQty())
@ -39,12 +39,12 @@ GPIO_TypeDef *digitalPinToPort(uint32_t digitalPinNumber)
} }
// 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 digitalPinNumber) __attribute__((noinline, section(".ram_text"))) HAL_PinsTypeDef digitalPinToBitMask(uint32_t digitalPinNumber)
{ {
if (digitalPinNumber >= pinCommonQty()) if (digitalPinNumber >= 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;
} }
return (HAL_PinsTypeDef)(1 << (digitalPinNumber & 0xF)); return (HAL_PinsTypeDef)(1 << (digitalPinNumber & 0xF));
@ -106,27 +106,24 @@ 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) __attribute__((noinline, section(".ram_text"))) bool digitalPinPwmIsOn(uint8_t digitalPin)
{ {
uint8_t config = 0; if (digitalPinHasPWM(digitalPin))
uint8_t pinShift = digitalPin & 3; {
uint8_t config = 0;
uint8_t pinShift = digitalPin & 3;
if (PWM_PIN_TO_PORT_NUMBER(digitalPin) == 0) if (PWM_PIN_TO_PORT_NUMBER(digitalPin) == 0)
config = PIN_GET_PAD_CONFIG(PORT_0_CFG, pinShift); config = PIN_GET_PAD_CONFIG(PORT_0_CFG, pinShift);
else else
config = PIN_GET_PAD_CONFIG(PORT_1_CFG, pinShift); config = PIN_GET_PAD_CONFIG(PORT_1_CFG, pinShift);
if (config == 2) if (config == 2)
return true; return true;
else }
return false; return false;
} }
bool digitalPinHasPWM(uint8_t p)
{
return (p < 32) && ((p & 0xF) < 4);
}
// 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)
{ {