wiring digital speed optimization #15
@ -106,7 +106,7 @@ void digitalWrite(uint32_t PinNumber, uint32_t Val)
|
||||
GPIO_TypeDef* port = digitalPinToPort(PinNumber);
|
||||
HAL_PinsTypeDef pin = digitalPinToBitMask(PinNumber);
|
||||
|
||||
if (digitalPinHasPWM(PinNumber))
|
||||
if (digitalPinPwmIsOn(PinNumber))
|
||||
// if the pin can use PWM, disable PWM
|
||||
analogWriteStop(PinNumber);
|
||||
|
||||
@ -127,7 +127,7 @@ int digitalRead(uint32_t PinNumber)
|
||||
GPIO_TypeDef* port = digitalPinToPort(PinNumber);
|
||||
HAL_PinsTypeDef pin = digitalPinToBitMask(PinNumber);
|
||||
|
||||
if (digitalPinHasPWM(PinNumber))
|
||||
if (digitalPinPwmIsOn(PinNumber))
|
||||
// if the pin can use PWM, disable PWM
|
||||
analogWriteStop(PinNumber);
|
||||
|
||||
|
||||
@ -31,6 +31,7 @@ extern "C" {
|
||||
|
||||
#include "mik32_hal_gpio.h"
|
||||
#include "mik32_hal_timer32.h"
|
||||
#include "wiring_LL.h"
|
||||
|
||||
void ErrorMsgHandler(const char * msg);
|
||||
|
||||
@ -125,8 +126,35 @@ 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
|
||||
// use only if digitalPinHasPWM() == true
|
||||
#define PWM_PIN_TO_PORT_NUMBER(pin) (((pin==10)||(pin==11)||(pin==12)||(pin==13)) ? 1:0)
|
||||
|
||||
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
|
||||
static inline __attribute__((always_inline)) 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
|
||||
TIMER32_TypeDef* pwmPinToTimer(uint32_t digPinNumber);
|
||||
// determines which timer channel the pin belongs to
|
||||
|
||||
@ -14,7 +14,6 @@
|
||||
#include "pins_arduino.h"
|
||||
#include "mik32_hal_adc.h"
|
||||
#include "wiring_analog.h"
|
||||
#include "wiring_LL.h"
|
||||
|
||||
#define SPI0_SWITCH_PORT GPIO_1
|
||||
#define SPI0_SWITCH_PIN GPIO_PIN_10
|
||||
@ -126,34 +125,6 @@ uint32_t analogInputToChannelNumber(uint32_t PinNumber)
|
||||
}
|
||||
|
||||
// ---------------------- PWM ---------------------- //
|
||||
// use only if digitalPinHasPWM() == true
|
||||
#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
|
||||
bool digitalPinPwmIsOn(uint8_t 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;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// function is used only if digitalPinHasPWM() is true
|
||||
TIMER32_TypeDef* pwmPinToTimer(uint32_t digPinNumber)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user