добавила поддержку второго spi, на железе еще не проверяла

This commit is contained in:
KLASSENTS 2025-01-29 17:08:06 +07:00
parent da5854d0fc
commit ced8debe30
4 changed files with 94 additions and 14 deletions

View File

@ -131,17 +131,29 @@ TIMER32_TypeDef* pwmPinToTimer(uint32_t digPinNumber);
HAL_TIMER32_CHANNEL_IndexTypeDef pwmPinToTimerChannel(uint32_t digPinNumber);
// SPI
#define PIN_SPI_SS (P1_3)
#define PIN_SPI_MOSI (P1_1)
#define PIN_SPI_MISO (P1_0)
#define PIN_SPI_SCK (P1_2)
#define SPI_COMMON_QTY 2
#define PIN_SPI_SS P1_3
#define PIN_SPI_MOSI P1_1
#define PIN_SPI_MISO P1_0
#define PIN_SPI_SCK P1_2
static const uint8_t SS = PIN_SPI_SS;
static const uint8_t MOSI = PIN_SPI_MOSI;
static const uint8_t MISO = PIN_SPI_MISO;
static const uint8_t SCK = PIN_SPI_SCK;
// functions is needed for compatibility with other boards
static inline void spi_onBegin(void) {}
static inline void spi_onEnd(void) {}
#define PIN_SPI1_SS P0_3
#define PIN_SPI1_MOSI P0_1
#define PIN_SPI1_MISO P0_0
#define PIN_SPI1_SCK P0_2
static const uint8_t SS1 = PIN_SPI1_SS;
static const uint8_t MOSI1 = PIN_SPI1_MOSI;
static const uint8_t MISO1 = PIN_SPI1_MISO;
static const uint8_t SCK1 = PIN_SPI1_SCK;
// check if pwm is used on spi pins
void spi_onBegin(uint8_t spiNum);
static inline void spi_onEnd(uint8_t spiNum){}
// I2C
#define PIN_WIRE_SDA (P1_12)

View File

@ -191,4 +191,32 @@ int8_t digitalPinToInterrupt(uint32_t digPinNumber)
return i;
}
return NOT_AN_INTERRUPT;
}
// ---------------------- SPI ---------------------- //
void spi_onBegin(uint8_t spiNum)
{
// On mik32 spi0 needs pin 0.3, spi1 needs pin 1.3 for correct work.
// This pins also can be used as pwm. If pwm is working when spi begins, we should stop the pwm channel
// get pin NSS_IN config
uint8_t config;
if (spiNum == 1)
config = PIN_GET_PAD_CONFIG(PORT_1_CFG, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_3));
else
config = PIN_GET_PAD_CONFIG(PORT_0_CFG, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_3));
if(config == 2) // timer for pwm
{
if (spiNum == 1)
{
analogWriteStop(P1_3);
ErrorMsgHandler("analogWrite(): P1_3 cannot be used as PWM pin while SPI is running");
}
else
{
analogWriteStop(P0_3);
ErrorMsgHandler("analogWrite(): P0_3 cannot be used as PWM pin while SPI1 is running");
}
}
}

View File

@ -143,17 +143,29 @@ TIMER32_TypeDef* pwmPinToTimer(uint32_t digPinNumber);
HAL_TIMER32_CHANNEL_IndexTypeDef pwmPinToTimerChannel(uint32_t digPinNumber);
// SPI
#define PIN_SPI_SS (P1_3)
#define PIN_SPI_MOSI (P1_1)
#define PIN_SPI_MISO (P1_0)
#define PIN_SPI_SCK (P1_2)
#define SPI_COMMON_QTY 2
#define PIN_SPI_SS P1_3
#define PIN_SPI_MOSI P1_1
#define PIN_SPI_MISO P1_0
#define PIN_SPI_SCK P1_2
static const uint8_t SS = PIN_SPI_SS;
static const uint8_t MOSI = PIN_SPI_MOSI;
static const uint8_t MISO = PIN_SPI_MISO;
static const uint8_t SCK = PIN_SPI_SCK;
// functions is needed for compatibility with other boards
static inline void spi_onBegin(void) {}
static inline void spi_onEnd(void) {}
#define PIN_SPI1_SS P0_3
#define PIN_SPI1_MOSI P0_1
#define PIN_SPI1_MISO P0_0
#define PIN_SPI1_SCK P0_2
static const uint8_t SS1 = PIN_SPI1_SS;
static const uint8_t MOSI1 = PIN_SPI1_MOSI;
static const uint8_t MISO1 = PIN_SPI1_MISO;
static const uint8_t SCK1 = PIN_SPI1_SCK;
// check if pwm is used on spi pins
void spi_onBegin(uint8_t spiNum);
static inline void spi_onEnd(uint8_t spiNum){}
// I2C
#define PIN_WIRE_SDA (P1_12)

View File

@ -205,4 +205,32 @@ int8_t digitalPinToInterrupt(uint32_t digPinNumber)
return i;
}
return NOT_AN_INTERRUPT;
}
// ---------------------- SPI ---------------------- //
void spi_onBegin(uint8_t spiNum)
{
// On mik32 spi0 needs pin 0.3, spi1 needs pin 1.3 for correct work.
// This pins also can be used as pwm. If pwm is working when spi begins, we should stop the pwm channel
// get pin NSS_IN config
uint8_t config;
if (spiNum == 1)
config = PIN_GET_PAD_CONFIG(PORT_1_CFG, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_3));
else
config = PIN_GET_PAD_CONFIG(PORT_0_CFG, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_3));
if(config == 2) // timer for pwm
{
if (spiNum == 1)
{
analogWriteStop(P1_3);
ErrorMsgHandler("analogWrite(): P1_3 cannot be used as PWM pin while SPI is running");
}
else
{
analogWriteStop(P0_3);
ErrorMsgHandler("analogWrite(): P0_3 cannot be used as PWM pin while SPI1 is running");
}
}
}