добавлена проверка - не превышает ли номер вывода из аргумента функции общее кол-во выводов

This commit is contained in:
KLASSENTS 2025-02-21 15:43:13 +07:00
parent 5b2e2eb1d5
commit fbaa4b931a
11 changed files with 171 additions and 96 deletions

View File

@ -79,6 +79,12 @@ static void calcFrequencyParams(FrequencyParams_t* params, unsigned int newFrequ
// start tone with frequency (in hertz) and duration (in milliseconds) // start tone with frequency (in hertz) and duration (in milliseconds)
void tone(uint8_t pin, unsigned int frequency, unsigned long duration) void tone(uint8_t pin, unsigned int frequency, unsigned long duration)
{ {
if ((pin>=pinCommonQty()))
{
ErrorMsgHandler("tone(): pin number exceeds the total number of pins");
return;
}
if (!timerIsOn) // if tone is not generated at the moment if (!timerIsOn) // if tone is not generated at the moment
{ {
// calculate the parameters necessary to ensure a given frequency if the frequency has changed // calculate the parameters necessary to ensure a given frequency if the frequency has changed
@ -123,6 +129,12 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration)
// stop tone // stop tone
void noTone(uint8_t pin) void noTone(uint8_t pin)
{ {
if ((pin>=pinCommonQty()))
{
ErrorMsgHandler("noTone(): pin number exceeds the total number of pins");
return;
}
if (timerIsOn) if (timerIsOn)
{ {
// pin to 0 // pin to 0

View File

@ -170,7 +170,7 @@ It is recommended to turn off the timer in the following order:
*/ */
void analogWriteStop(uint32_t PinNumber) void analogWriteStop(uint32_t PinNumber)
{ {
if ((pwmIsInited > 0) && (digitalPinPwmIsOn(PinNumber))) if (digitalPinHasPWM(PinNumber) && (pwmIsInited > 0) && digitalPinPwmIsOn(PinNumber))
{ {
// load the timer address and channel number corresponding to the specified pin // load the timer address and channel number corresponding to the specified pin
htimer32.Instance = pwmPinToTimer(PinNumber); htimer32.Instance = pwmPinToTimer(PinNumber);

View File

@ -69,8 +69,15 @@ void pinMode(uint32_t PinNumber, uint32_t PinMode)
void fastPinMode(uint32_t PinNumber, uint32_t PinMode) void fastPinMode(uint32_t PinNumber, uint32_t PinMode)
{ {
if ((PinNumber>=pinCommonQty()))
{
ErrorMsgHandler("fastPinMode(): pin number exceeds the total number of pins");
return;
}
GPIO_TypeDef* port = digitalPinToPort(PinNumber); GPIO_TypeDef* port = digitalPinToPort(PinNumber);
HAL_PinsTypeDef pinMask = digitalPinToBitMask(PinNumber); HAL_PinsTypeDef pinMask = digitalPinToBitMask(PinNumber);
// set direction // set direction
if (PinMode == OUTPUT) if (PinMode == OUTPUT)
GPIO_OUTPUT_MODE_PIN(port, pinMask); GPIO_OUTPUT_MODE_PIN(port, pinMask);

View File

@ -19,7 +19,13 @@ NeoPixel::~NeoPixel() {
} }
void NeoPixel::begin(void) { void NeoPixel::begin(void) {
if (pin >= 0) { if (pin >= 0)
{
if (pin >= pinCommonQty())
{
ErrorMsgHandler("NeoPixel.begin(): pin number exceeds the total number of pins");
return;
}
pinMode(pin, OUTPUT); pinMode(pin, OUTPUT);
digitalWrite(pin, LOW); digitalWrite(pin, LOW);
} }
@ -59,58 +65,61 @@ static void __attribute__((noinline, section(".ram_text"))) mik32Show(GPIO_TypeD
// not support 400khz // not support 400khz
if (!is800KHz) return; if (!is800KHz) return;
volatile uint32_t* set = &m_port->SET; if ((m_port != NULL) && (m_pin != NC))
volatile uint32_t* clr = &m_port->CLEAR; {
volatile uint32_t* set = &m_port->SET;
volatile uint32_t* clr = &m_port->CLEAR;
uint8_t* ptr = pixels; uint8_t* ptr = pixels;
uint8_t* end = ptr + numBytes; uint8_t* end = ptr + numBytes;
uint8_t p = *ptr++; uint8_t p = *ptr++;
uint8_t bitMask = 0x80; uint8_t bitMask = 0x80;
noInterrupts(); noInterrupts();
while (1) { while (1) {
if (p & bitMask) { // ONE if (p & bitMask) { // ONE
// High 800ns - 25,6 tick // High 800ns - 25,6 tick
*set = m_pin; *set = m_pin;
__asm volatile ( __asm volatile (
"nop; nop; nop; nop; nop; nop; nop; nop;" "nop; nop; nop; nop; nop; nop; nop; nop;"
"nop; nop; nop; nop; nop; nop; nop; nop;" "nop; nop; nop; nop; nop; nop; nop; nop;"
"nop; nop; nop; nop;" "nop; nop; nop; nop;"
); );
// Low 450ns - 14,4 tick // Low 450ns - 14,4 tick
*clr = m_pin; *clr = m_pin;
__asm volatile ( __asm volatile (
"nop; nop; nop; nop; nop;" "nop; nop; nop; nop; nop;"
); );
} else { // ZERO } else { // ZERO
// High 400ns - 12,8 tick // High 400ns - 12,8 tick
*set = m_pin; *set = m_pin;
__asm volatile ( __asm volatile (
"nop; nop; nop; nop; nop; nop;" "nop; nop; nop; nop; nop; nop;"
); );
// Low 850ns - 27,2 tick // Low 850ns - 27,2 tick
*clr = m_pin; *clr = m_pin;
__asm volatile ( __asm volatile (
"nop; nop; nop; nop; nop; nop; nop; nop;" "nop; nop; nop; nop; nop; nop; nop; nop;"
"nop; nop; nop;" "nop; nop; nop;"
); );
} }
if (bitMask >>= 1) { if (bitMask >>= 1) {
// Move on to the next pixel // Move on to the next pixel
} }
else { else {
if (ptr >= end) { if (ptr >= end) {
break; break;
}
p = *ptr++;
bitMask = 0x80;
} }
p = *ptr++;
bitMask = 0x80;
} }
interrupts();
} }
interrupts();
} }
void NeoPixel::show(void) { void NeoPixel::show(void) {

View File

@ -228,6 +228,18 @@ void SoftwareSerial::begin(long speed)
// delays are empirical values here // delays are empirical values here
_rx_delay_centering = _rx_delay_intrabit = _rx_delay_stopbit = _tx_delay = 0; _rx_delay_centering = _rx_delay_intrabit = _rx_delay_stopbit = _tx_delay = 0;
// if pins exceeds the total number of pins, return with zero delays - it blocks further library work
if ((_transmitPin >= pinCommonQty()))
{
ErrorMsgHandler("SoftwareSerial.begin(): Tx pin number exceeds the total number of pins");
return;
}
if ((_receivePin >= pinCommonQty()))
{
ErrorMsgHandler("SoftwareSerial.begin(): Rx pin number exceeds the total number of pins");
return;
}
// limit speed // limit speed
if (speed > MAX_SOFT_SERIAL_SPEED) speed = MAX_SOFT_SERIAL_SPEED; if (speed > MAX_SOFT_SERIAL_SPEED) speed = MAX_SOFT_SERIAL_SPEED;
if (speed < MIN_SOFT_SERIAL_SPEED) speed = MIN_SOFT_SERIAL_SPEED; if (speed < MIN_SOFT_SERIAL_SPEED) speed = MIN_SOFT_SERIAL_SPEED;

View File

@ -67,7 +67,13 @@ 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) GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber)
{ {
GPIO_TypeDef* gpioNum = 0; GPIO_TypeDef* gpioNum = NULL;
if (digPinNumber >= pinCommonQty())
{
ErrorMsgHandler("digitalPinToPort(): pin number exceeds the total number of pins");
return NULL;
}
// port 2 - led (22) // port 2 - led (22)
if (digPinNumber == LED_BUILTIN) if (digPinNumber == LED_BUILTIN)
gpioNum = GPIO_2; gpioNum = GPIO_2;
@ -85,20 +91,21 @@ 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)
{ {
if (digPinNumber < pinCommonQty()) if (digPinNumber >= pinCommonQty())
{ {
HAL_PinsTypeDef mask; ErrorMsgHandler("digitalPinToBitMask(): pin number exceeds the total number of pins");
// 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;
}
else
return NC; 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) uint16_t pinCommonQty(void)

View File

@ -65,7 +65,13 @@ const HAL_PinsTypeDef digitalPinToGpioPinArray[] =
// etermines the address of the port by the board pin number to which this pin belongs on the MCU // 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* digitalPinToPort(uint32_t digPinNumber)
{ {
GPIO_TypeDef* gpioNum = 0; GPIO_TypeDef* gpioNum = NULL;
if (digPinNumber >= pinCommonQty())
{
ErrorMsgHandler("digitalPinToPort(): pin number exceeds the total number of pins");
return NULL;
}
// 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; gpioNum = GPIO_2;
@ -83,20 +89,21 @@ 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)
{ {
if (digPinNumber < pinCommonQty()) if (digPinNumber >= pinCommonQty())
{ {
HAL_PinsTypeDef mask; ErrorMsgHandler("digitalPinToBitMask(): pin number exceeds the total number of pins");
// 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;
}
else
return NC; 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) uint16_t pinCommonQty(void)

View File

@ -95,13 +95,7 @@ static const uint8_t A7 = PIN_A7;
// 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); 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
static inline HAL_PinsTypeDef digitalPinToBitMask(uint32_t digitalPinNumber) HAL_PinsTypeDef digitalPinToBitMask(uint32_t digitalPinNumber);
{
if (digitalPinNumber >= P2_6)
return (HAL_PinsTypeDef)(1 << ((digitalPinNumber+6) & 0xF));
else
return (HAL_PinsTypeDef)(1 << (digitalPinNumber & 0xF));
}
// total number of pins available for initialization // total number of pins available for initialization
static inline uint16_t pinCommonQty(void) static inline uint16_t pinCommonQty(void)
{ {

View File

@ -23,6 +23,12 @@
GPIO_TypeDef *digitalPinToPort(uint32_t digitalPinNumber) GPIO_TypeDef *digitalPinToPort(uint32_t digitalPinNumber)
{ {
if (digitalPinNumber >= pinCommonQty())
{
ErrorMsgHandler("digitalPinToPort(): pin number exceeds the total number of pins");
return NULL;
}
if (digitalPinNumber <= P0_15) if (digitalPinNumber <= P0_15)
return GPIO_0; return GPIO_0;
else if ((digitalPinNumber >= P1_0) && (digitalPinNumber <= P1_15)) else if ((digitalPinNumber >= P1_0) && (digitalPinNumber <= P1_15))
@ -33,6 +39,21 @@ GPIO_TypeDef *digitalPinToPort(uint32_t digitalPinNumber)
return NULL; return NULL;
} }
// determines the pin address inside the port by the board pin number
HAL_PinsTypeDef digitalPinToBitMask(uint32_t digitalPinNumber)
{
if (digitalPinNumber >= PINS_COMMON_QTY)
{
ErrorMsgHandler("digitalPinToBitMask(): pin number exceeds the total number of pins");
return NC;
}
if (digitalPinNumber >= P2_6)
return (HAL_PinsTypeDef)(1 << ((digitalPinNumber+6) & 0xF));
else
return (HAL_PinsTypeDef)(1 << (digitalPinNumber & 0xF));
}
// 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)
{ {

View File

@ -107,10 +107,7 @@ static const uint8_t A7 = PIN_A7;
// 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); 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
static inline HAL_PinsTypeDef digitalPinToBitMask(uint32_t digitalPinNumber) HAL_PinsTypeDef digitalPinToBitMask(uint32_t digitalPinNumber);
{
return (HAL_PinsTypeDef)(1 << (digitalPinNumber & 0xF));
}
// total number of pins available for initialization // total number of pins available for initialization
static inline uint16_t pinCommonQty(void) static inline uint16_t pinCommonQty(void)
{ {

View File

@ -23,22 +23,31 @@
GPIO_TypeDef *digitalPinToPort(uint32_t digitalPinNumber) GPIO_TypeDef *digitalPinToPort(uint32_t digitalPinNumber)
{ {
if (digitalPinNumber < 16u) GPIO_TypeDef* port = NULL;
if (digitalPinNumber < pinCommonQty())
{ {
return GPIO_0; if (digitalPinNumber < 16u)
} port = GPIO_0;
else if ((digitalPinNumber >= 16u) && (digitalPinNumber < 32u)) else if ((digitalPinNumber >= 16u) && (digitalPinNumber < 32u))
{ port = GPIO_1;
return GPIO_1; else if ((digitalPinNumber >= 32u) && (digitalPinNumber < 40u))
} port = GPIO_2;
else if ((digitalPinNumber >= 32u) && (digitalPinNumber < 40u))
{
return GPIO_2;
} }
else else
ErrorMsgHandler("digitalPinToPort(): pin number exceeds the total number of pins");
return port;
}
// determines the pin address inside the port by the board pin number
HAL_PinsTypeDef digitalPinToBitMask(uint32_t digitalPinNumber)
{
if (digitalPinNumber >= pinCommonQty())
{ {
return NULL; ErrorMsgHandler("digitalPinToBitMask(): pin number exceeds the total number of pins");
return NC;
} }
return (HAL_PinsTypeDef)(1 << (digitalPinNumber & 0xF));
} }
// 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