wiring digital speed optimization #15
@ -32,6 +32,13 @@ extern "C" {
|
||||
#include "mik32_hal_gpio.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
|
||||
#define PIN_A0 (14)
|
||||
#define PIN_A1 (15)
|
||||
@ -63,8 +70,6 @@ static const uint8_t A7 = PIN_A7;
|
||||
GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber);
|
||||
// determines the pin address inside the port by the board pin number
|
||||
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
|
||||
volatile uint32_t* portOutputRegister(GPIO_TypeDef* GPIO_x);
|
||||
// the function returns a reference to the STATE address of the GPIO register
|
||||
@ -84,8 +89,15 @@ uint32_t analogInputToChannelNumber(uint32_t PinNumber);
|
||||
|
||||
// PWM
|
||||
#define PWM_FREQUENCY_MAX 1000000 // Hz
|
||||
bool digitalPinHasPWM(uint8_t p);
|
||||
bool digitalPinPwmIsOn(uint8_t digitalPin); // use only if digitalPinHasPWM() == true
|
||||
static inline __attribute__((always_inline)) bool digitalPinHasPWM(uint8_t p)
|
||||
{
|
||||
// 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
|
||||
TIMER32_TypeDef* pwmPinToTimer(uint32_t digPinNumber);
|
||||
// determines which timer channel the pin belongs to
|
||||
|
||||
@ -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
|
||||
GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber)
|
||||
__attribute__((noinline, section(".ram_text"))) GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber)
|
||||
{
|
||||
GPIO_TypeDef* gpioNum = NULL;
|
||||
if (digPinNumber >= pinCommonQty())
|
||||
{
|
||||
ErrorMsgHandler("digitalPinToPort(): pin number exceeds the total number of pins");
|
||||
@ -76,41 +75,32 @@ GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber)
|
||||
|
||||
// port 2 - led (22)
|
||||
if (digPinNumber == LED_BUILTIN)
|
||||
gpioNum = GPIO_2;
|
||||
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))
|
||||
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)
|
||||
else
|
||||
gpioNum = GPIO_0;
|
||||
|
||||
return gpioNum;
|
||||
return GPIO_0;
|
||||
}
|
||||
|
||||
// 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())
|
||||
{
|
||||
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 ((digPinNumber == 10) && spi1NssPinIsBlocked)
|
||||
mask = SPI1_NSS_OUT_PIN;
|
||||
return SPI1_NSS_OUT_PIN;
|
||||
else if ((digPinNumber == 9) && spi0NssPinIsBlocked)
|
||||
mask = SPI0_NSS_OUT_PIN;
|
||||
return SPI0_NSS_OUT_PIN;
|
||||
else
|
||||
mask = digitalPinToGpioPinArray[digPinNumber];
|
||||
return mask;
|
||||
}
|
||||
|
||||
uint16_t pinCommonQty(void)
|
||||
{
|
||||
return (uint16_t)(sizeof(digitalPinToGpioPinArray)/sizeof(digitalPinToGpioPinArray[0]));
|
||||
return digitalPinToGpioPinArray[digPinNumber];
|
||||
}
|
||||
|
||||
// the function returns a reference to the OUTPUT address of the GPIO register
|
||||
@ -221,29 +211,20 @@ uint32_t analogInputToChannelNumber(uint32_t PinNumber)
|
||||
// return true if digitalPin configured as pwm
|
||||
bool digitalPinPwmIsOn(uint8_t digitalPin)
|
||||
{
|
||||
uint8_t config = 0;
|
||||
uint8_t pinShift = PIN_MASK_TO_PIN_NUMBER(digitalPinToBitMask(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 (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;
|
||||
else
|
||||
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;
|
||||
if (config == 2)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// function is used only if digitalPinHasPWM() is true
|
||||
|
||||
Loading…
Reference in New Issue
Block a user