diff --git a/variants/elsomik/pins_arduino.h b/variants/elsomik/pins_arduino.h index a311bcb..4382db1 100644 --- a/variants/elsomik/pins_arduino.h +++ b/variants/elsomik/pins_arduino.h @@ -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) diff --git a/variants/elsomik/variant.c b/variants/elsomik/variant.c index 0e8baff..3480f52 100644 --- a/variants/elsomik/variant.c +++ b/variants/elsomik/variant.c @@ -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"); + } + } } \ No newline at end of file diff --git a/variants/start/pins_arduino.h b/variants/start/pins_arduino.h index 4231d8a..8a95936 100644 --- a/variants/start/pins_arduino.h +++ b/variants/start/pins_arduino.h @@ -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) diff --git a/variants/start/variant.c b/variants/start/variant.c index 82e8e90..c19e64f 100644 --- a/variants/start/variant.c +++ b/variants/start/variant.c @@ -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"); + } + } } \ No newline at end of file