Перенесла получение порта и маски пина в хэдер как всегда инлайн. Для ног не ШИМовских по 2 мкс длительность

This commit is contained in:
KLASSENTS 2025-03-12 15:59:04 +07:00
parent 348d9b2f69
commit c6f657d189
2 changed files with 53 additions and 62 deletions

View File

@ -32,6 +32,20 @@ extern "C" {
#include "mik32_hal_gpio.h" #include "mik32_hal_gpio.h"
#include "mik32_hal_timer32.h" #include "mik32_hal_timer32.h"
void ErrorMsgHandler(const char * msg);
// total number of pins available for initialization
#define PINS_COMMON_QTY 24
#define pinCommonQty() (PINS_COMMON_QTY)
#define SPI0_NSS_OUT_PORT GPIO_1
#define SPI0_NSS_OUT_PIN GPIO_PIN_14
#define SPI1_NSS_OUT_PORT GPIO_1
#define SPI1_NSS_OUT_PIN GPIO_PIN_4
extern bool spi0NssPinIsBlocked;
extern bool spi1NssPinIsBlocked;
extern const HAL_PinsTypeDef digitalPinToGpioPinArray[];
// analog pins // analog pins
#define PIN_A0 (14) #define PIN_A0 (14)
#define PIN_A1 (15) #define PIN_A1 (15)
@ -56,11 +70,42 @@ static const uint8_t A5 = PIN_A5;
#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 address of the port by the board pin number to which this pin belongs on the MCU
GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber); static inline __attribute__((always_inline)) 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
HAL_PinsTypeDef digitalPinToBitMask(uint32_t digPinNumber); static inline __attribute__((always_inline)) HAL_PinsTypeDef digitalPinToBitMask(uint32_t digPinNumber)
// total number of pins available for initialization {
uint16_t pinCommonQty(void); 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

View File

@ -20,21 +20,17 @@
#define SPI0_SWITCH_PIN GPIO_PIN_10 #define SPI0_SWITCH_PIN GPIO_PIN_10
#define SPI0_NSS_IN_PORT GPIO_0 #define SPI0_NSS_IN_PORT GPIO_0
#define SPI0_NSS_IN_PIN GPIO_PIN_3 #define SPI0_NSS_IN_PIN GPIO_PIN_3
#define SPI0_NSS_OUT_PORT GPIO_1
#define SPI0_NSS_OUT_PIN GPIO_PIN_14
#define SPI1_SWITCH_PORT GPIO_1 #define SPI1_SWITCH_PORT GPIO_1
#define SPI1_SWITCH_PIN GPIO_PIN_6 #define SPI1_SWITCH_PIN GPIO_PIN_6
#define SPI1_NSS_IN_PORT GPIO_1 #define SPI1_NSS_IN_PORT GPIO_1
#define SPI1_NSS_IN_PIN GPIO_PIN_3 #define SPI1_NSS_IN_PIN GPIO_PIN_3
#define SPI1_NSS_OUT_PORT GPIO_1
#define SPI1_NSS_OUT_PIN GPIO_PIN_4
bool spi0NssPinIsBlocked = false; 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,54 +58,7 @@ 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
GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber)
{
GPIO_TypeDef* gpioNum = NULL;
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)
gpioNum = 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
gpioNum = GPIO_1;
// port 0 - board pins 0...6, 9, 16(A2), 17(A3), 20(A4), 21(A5)
else
gpioNum = GPIO_0;
return gpioNum;
}
// determines the pin address inside the port by the board pin number
HAL_PinsTypeDef digitalPinToBitMask(uint32_t digPinNumber)
{
if (digPinNumber >= pinCommonQty())
{
ErrorMsgHandler("digitalPinToBitMask(): pin number exceeds the total number of pins");
return NC;
}
HAL_PinsTypeDef mask;
// if spi is on default pin NSS_IN is needed for spi, board pin is replaced to pin NSS_OUT
if ((digPinNumber == 10) && spi1NssPinIsBlocked)
mask = SPI1_NSS_OUT_PIN;
else if ((digPinNumber == 9) && spi0NssPinIsBlocked)
mask = SPI0_NSS_OUT_PIN;
else
mask = 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
volatile uint32_t* portOutputRegister(GPIO_TypeDef* GPIO_x) volatile uint32_t* portOutputRegister(GPIO_TypeDef* GPIO_x)
@ -200,13 +149,10 @@ bool digitalPinPwmIsOn(uint8_t digitalPin)
bool digitalPinHasPWM(uint8_t p) bool digitalPinHasPWM(uint8_t p)
{ {
bool ret = false;
// if spi is in use D9 or D10 cannot work as pwm // if spi is in use D9 or D10 cannot work as pwm
if (((p == 9) && spi0NssPinIsBlocked) || ((p == 10) && spi1NssPinIsBlocked)) if (p == 3 || p == 5 || p == 6 || (p >= 11 && p <= 13) || ((p == 9) && !spi0NssPinIsBlocked) || ((p == 10) && !spi1NssPinIsBlocked))
ret = false; return true;
else if (p == 3 || p == 5 || p == 6 || (p >= 9 && p <= 13)) return false;
ret = true;
return ret;
} }
// function is used only if digitalPinHasPWM() is true // function is used only if digitalPinHasPWM() is true