добавлена поддержка второго spi. проект собирается, на железе не проверяла
This commit is contained in:
parent
be97466472
commit
c9ab7a50bf
@ -92,18 +92,19 @@ TIMER32_TypeDef* pwmPinToTimer(uint32_t digPinNumber);
|
|||||||
HAL_TIMER32_CHANNEL_IndexTypeDef pwmPinToTimerChannel(uint32_t digPinNumber);
|
HAL_TIMER32_CHANNEL_IndexTypeDef pwmPinToTimerChannel(uint32_t digPinNumber);
|
||||||
|
|
||||||
// SPI
|
// SPI
|
||||||
#define PIN_SPI_SS (10)
|
#define SPI_COMMON_QTY 2
|
||||||
#define PIN_SPI_MOSI (11)
|
#define PIN_SPI_SS 10
|
||||||
#define PIN_SPI_MISO (12)
|
#define PIN_SPI_MOSI 11
|
||||||
#define PIN_SPI_SCK (13)
|
#define PIN_SPI_MISO 12
|
||||||
|
#define PIN_SPI_SCK 13
|
||||||
static const uint8_t SS = PIN_SPI_SS;
|
static const uint8_t SS = PIN_SPI_SS;
|
||||||
static const uint8_t MOSI = PIN_SPI_MOSI;
|
static const uint8_t MOSI = PIN_SPI_MOSI;
|
||||||
static const uint8_t MISO = PIN_SPI_MISO;
|
static const uint8_t MISO = PIN_SPI_MISO;
|
||||||
static const uint8_t SCK = PIN_SPI_SCK;
|
static const uint8_t SCK = PIN_SPI_SCK;
|
||||||
// config SEL_NSS1 to replace D10 to different controller pin,
|
// config SEL_NSS1 to replace D10 to different controller pin,
|
||||||
// because pin 1.3 which is D10 by default is needed to spi
|
// because NSS which is D9/D10 by default is needed to spi
|
||||||
void spi_onBegin(void);
|
void spi_onBegin(uint8_t spiNum);
|
||||||
void spi_onEnd(void);
|
void spi_onEnd(uint8_t spiNum);
|
||||||
|
|
||||||
// I2C
|
// I2C
|
||||||
#define PIN_WIRE_SDA (18)
|
#define PIN_WIRE_SDA (18)
|
||||||
|
|||||||
@ -16,7 +16,22 @@
|
|||||||
#include "wiring_analog.h"
|
#include "wiring_analog.h"
|
||||||
#include "wiring_LL.h"
|
#include "wiring_LL.h"
|
||||||
|
|
||||||
bool spiNssPinIsBlocked = false;
|
#define SPI0_SWITCH_PORT GPIO_1
|
||||||
|
#define SPI0_SWITCH_PIN GPIO_PIN_10
|
||||||
|
#define SPI0_NSS_IN_PORT GPIO_0
|
||||||
|
#define SPI0_NSS_IN_PIN GPIO_PIN_3
|
||||||
|
#define SPI0_NSS_OUT_PORT GPIO_1
|
||||||
|
#define SPI0_NSS_OUT_PIN GPIO_PIN_14
|
||||||
|
|
||||||
|
#define SPI1_SWITCH_PORT GPIO_1
|
||||||
|
#define SPI1_SWITCH_PIN GPIO_PIN_6
|
||||||
|
#define SPI1_NSS_IN_PORT GPIO_1
|
||||||
|
#define SPI1_NSS_IN_PIN GPIO_PIN_3
|
||||||
|
#define SPI1_NSS_OUT_PORT GPIO_1
|
||||||
|
#define SPI1_NSS_OUT_PIN GPIO_PIN_4
|
||||||
|
|
||||||
|
bool spi0NssPinIsBlocked = false;
|
||||||
|
bool spi1NssPinIsBlocked = false;
|
||||||
|
|
||||||
// list of pin numbers from both ports with pins inside the port
|
// list of pin numbers from both ports with pins inside the port
|
||||||
const HAL_PinsTypeDef digitalPinToGpioPinArray[] =
|
const HAL_PinsTypeDef digitalPinToGpioPinArray[] =
|
||||||
@ -58,7 +73,7 @@ GPIO_TypeDef* digitalPinToPort(uint32_t digPinNumber)
|
|||||||
gpioNum = GPIO_2;
|
gpioNum = GPIO_2;
|
||||||
// port 1 - board pins 7, 8, 10...13, 14(А0), 15(А1), 18,19
|
// port 1 - board pins 7, 8, 10...13, 14(А0), 15(А1), 18,19
|
||||||
else if (digPinNumber == 7 || digPinNumber == 8 || (digPinNumber >= 10 && digPinNumber <= 15)
|
else if (digPinNumber == 7 || digPinNumber == 8 || (digPinNumber >= 10 && digPinNumber <= 15)
|
||||||
|| digPinNumber == 18 || digPinNumber == 19)
|
|| digPinNumber == 18 || digPinNumber == 19 || (digPinNumber == 9 && spi0NssPinIsBlocked))
|
||||||
gpioNum = GPIO_1;
|
gpioNum = GPIO_1;
|
||||||
// port 0 - board pins 0...6, 9, 16(A2), 17(A3), 20(A4), 21(A5), 24(A6), 25(A7)
|
// port 0 - board pins 0...6, 9, 16(A2), 17(A3), 20(A4), 21(A5), 24(A6), 25(A7)
|
||||||
else
|
else
|
||||||
@ -73,9 +88,11 @@ HAL_PinsTypeDef digitalPinToBitMask(uint32_t digPinNumber)
|
|||||||
if (digPinNumber < pinCommonQty())
|
if (digPinNumber < pinCommonQty())
|
||||||
{
|
{
|
||||||
HAL_PinsTypeDef mask;
|
HAL_PinsTypeDef mask;
|
||||||
// if spi is on default pin 1.3 is needed for spi, D10 is replaced to pin 1.4
|
// if spi is on default pin NSS_IN is needed for spi, board pin is replaced to pin NSS_OUT
|
||||||
if (spiNssPinIsBlocked && (digPinNumber == 10))
|
if ((digPinNumber == 10) && spi1NssPinIsBlocked)
|
||||||
mask = GPIO_PIN_4;
|
mask = SPI1_NSS_OUT_PIN;
|
||||||
|
else if ((digPinNumber == 9) && spi0NssPinIsBlocked)
|
||||||
|
mask = SPI0_NSS_OUT_PIN;
|
||||||
else
|
else
|
||||||
mask = digitalPinToGpioPinArray[digPinNumber];
|
mask = digitalPinToGpioPinArray[digPinNumber];
|
||||||
return mask;
|
return mask;
|
||||||
@ -215,7 +232,7 @@ bool digitalPinHasPWM(uint8_t p)
|
|||||||
{
|
{
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
// if spi is in use D10 cannot work as pwm
|
// if spi is in use D10 cannot work as pwm
|
||||||
if (spiNssPinIsBlocked && (p == 10))
|
if (((p == 9) && spi0NssPinIsBlocked) || ((p == 10) && spi1NssPinIsBlocked))
|
||||||
ret = false;
|
ret = false;
|
||||||
else if (p == 3 || p == 5 || p == 6 || (p >= 9 && p <= 13))
|
else if (p == 3 || p == 5 || p == 6 || (p >= 9 && p <= 13))
|
||||||
ret = true;
|
ret = true;
|
||||||
@ -303,44 +320,94 @@ int8_t digitalPinToInterrupt(uint32_t digPinNumber)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------- SPI ---------------------- //
|
// ---------------------- SPI ---------------------- //
|
||||||
void spi_onBegin(void)
|
void spi_onBegin(uint8_t spiNum)
|
||||||
{
|
{
|
||||||
// On Elbear Nano there is a seller on pin 1.6 which replace D10 from spi NSS pin 1.3 to pin 1.4,
|
// On Elbear Ace-Uno rev1.1.0 spi0 needs pin 0.3, spi1 needs pin 1.3 for correct work (NSS_IN).
|
||||||
// because spi needs pin 1.3 for correct work
|
// This pins are connected to board digital pins D9 and D10. There is a seller on each pin which
|
||||||
|
// replace board digital pin from NSS_IN pin to different free pin (NSS_OUT)
|
||||||
|
|
||||||
|
// replace config from NSS_IN to NSS_OUT
|
||||||
|
uint8_t config;
|
||||||
|
if (spiNum == 1)
|
||||||
|
config = PIN_GET_PAD_CONFIG(PORT_1_CFG, PIN_MASK_TO_PIN_NUMBER(SPI1_NSS_IN_PIN));
|
||||||
|
else
|
||||||
|
config = PIN_GET_PAD_CONFIG(PORT_0_CFG, PIN_MASK_TO_PIN_NUMBER(SPI0_NSS_IN_PIN));
|
||||||
|
|
||||||
// replace config from 1.3 to 1.4
|
|
||||||
uint8_t config = PIN_GET_PAD_CONFIG(PORT_1_CFG, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_3));
|
|
||||||
if (config == 0) // common gpio
|
if (config == 0) // common gpio
|
||||||
{
|
{
|
||||||
// get info from pin gpio1.3 and set config to gpio1.4
|
if (spiNum == 1)
|
||||||
HAL_GPIO_PinConfig(GPIO_1, GPIO_PIN_4, HAL_GPIO_GetPinDirection(GPIO_1, GPIO_PIN_3),
|
{
|
||||||
(HAL_GPIO_PullTypeDef)PIN_GET_PAD_CONFIG(PORT_1_PUPD, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_3)),
|
// get info from pin NSS_IN and set config to NSS_OUT
|
||||||
HAL_GPIO_DS_2MA);
|
HAL_GPIO_PinConfig(SPI1_NSS_OUT_PORT, SPI1_NSS_OUT_PIN, HAL_GPIO_GetPinDirection(SPI1_NSS_IN_PORT, SPI1_NSS_IN_PIN),
|
||||||
HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_4, (GPIO_PinState)GPIO_GET_PIN_STATE(GPIO_1, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_3)));
|
(HAL_GPIO_PullTypeDef)PIN_GET_PAD_CONFIG(PORT_1_PUPD, PIN_MASK_TO_PIN_NUMBER(SPI1_NSS_IN_PIN)),
|
||||||
|
HAL_GPIO_DS_2MA);
|
||||||
|
HAL_GPIO_WritePin(SPI1_NSS_OUT_PORT, SPI1_NSS_OUT_PIN, (GPIO_PinState)GPIO_GET_PIN_STATE(SPI1_NSS_IN_PORT, PIN_MASK_TO_PIN_NUMBER(SPI1_NSS_IN_PIN)));
|
||||||
|
|
||||||
// pin D10 was switched to different gpio and can be used further
|
// pin D10 was switched to different gpio and can be used further
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// get info from pin NSS_IN and set config to NSS_OUT
|
||||||
|
HAL_GPIO_PinConfig(SPI0_NSS_OUT_PORT, SPI0_NSS_OUT_PIN, HAL_GPIO_GetPinDirection(SPI0_NSS_IN_PORT, SPI0_NSS_IN_PIN),
|
||||||
|
(HAL_GPIO_PullTypeDef)PIN_GET_PAD_CONFIG(PORT_0_PUPD, PIN_MASK_TO_PIN_NUMBER(SPI0_NSS_IN_PIN)),
|
||||||
|
HAL_GPIO_DS_2MA);
|
||||||
|
HAL_GPIO_WritePin(SPI0_NSS_OUT_PORT, SPI0_NSS_OUT_PIN, (GPIO_PinState)GPIO_GET_PIN_STATE(SPI0_NSS_IN_PORT, PIN_MASK_TO_PIN_NUMBER(SPI0_NSS_IN_PIN)));
|
||||||
|
// pin D9 was switched to different gpio and can be used further
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if(config == 2) // timer for pwm
|
else if(config == 2) // timer for pwm
|
||||||
{
|
{
|
||||||
// if D10 (spi NSS pin) was used as pwm, we need to stop timer, because 1.4 doesn't support it
|
// if spi NSS_IN pin was used as pwm, we need to stop timer, because another pins don't support it
|
||||||
analogWriteStop(10);
|
if (spiNum == 1)
|
||||||
ErrorMsgHandler("analogWrite(): D10 cannot be used as PWM pin while SPI is running");
|
{
|
||||||
|
analogWriteStop(10);
|
||||||
|
ErrorMsgHandler("analogWrite(): D10 cannot be used as PWM pin while SPI is running");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
analogWriteStop(9);
|
||||||
|
ErrorMsgHandler("analogWrite(): D9 cannot be used as PWM pin while SPI1 is running");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// switch seller to pin 1.4
|
// switch seller to pin NSS_OUT
|
||||||
HAL_GPIO_PinConfig(GPIO_1, GPIO_PIN_6, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA);
|
if (spiNum == 1)
|
||||||
HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_6, GPIO_PIN_HIGH);
|
{
|
||||||
spiNssPinIsBlocked = true; // block spi pin
|
HAL_GPIO_PinConfig(SPI1_SWITCH_PORT, SPI1_SWITCH_PIN, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA);
|
||||||
|
HAL_GPIO_WritePin (SPI1_SWITCH_PORT, SPI1_SWITCH_PIN, GPIO_PIN_HIGH);
|
||||||
|
spi1NssPinIsBlocked = true; // block spi pin
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
HAL_GPIO_PinConfig(SPI0_SWITCH_PORT, SPI0_SWITCH_PIN, HAL_GPIO_MODE_GPIO_OUTPUT, HAL_GPIO_PULL_NONE, HAL_GPIO_DS_2MA);
|
||||||
|
HAL_GPIO_WritePin (SPI0_SWITCH_PORT, SPI0_SWITCH_PIN, GPIO_PIN_HIGH);
|
||||||
|
spi0NssPinIsBlocked = true; // block spi pin
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void spi_onEnd(void)
|
void spi_onEnd(uint8_t spiNum)
|
||||||
{
|
{
|
||||||
// get info from pin gpio1.4 and set config to gpio1.3
|
// get info from pin NSS_OUT and set config to NSS_IN
|
||||||
HAL_GPIO_PinConfig(GPIO_1, GPIO_PIN_3, HAL_GPIO_GetPinDirection(GPIO_1, GPIO_PIN_4),
|
if (spiNum == 1)
|
||||||
(HAL_GPIO_PullTypeDef)PIN_GET_PAD_CONFIG(PORT_1_PUPD, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_4)), HAL_GPIO_DS_2MA);
|
{
|
||||||
HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_3, (GPIO_PinState)GPIO_GET_PIN_STATE(GPIO_1, PIN_MASK_TO_PIN_NUMBER(GPIO_PIN_4)));
|
HAL_GPIO_PinConfig(SPI1_NSS_IN_PORT, SPI1_NSS_IN_PIN, HAL_GPIO_GetPinDirection(SPI1_NSS_OUT_PORT, SPI1_NSS_OUT_PIN),
|
||||||
|
(HAL_GPIO_PullTypeDef)PIN_GET_PAD_CONFIG(PORT_1_PUPD, PIN_MASK_TO_PIN_NUMBER(SPI1_NSS_OUT_PIN)), HAL_GPIO_DS_2MA);
|
||||||
|
HAL_GPIO_WritePin (SPI1_NSS_IN_PORT, SPI1_NSS_IN_PIN,
|
||||||
|
(GPIO_PinState)GPIO_GET_PIN_STATE(SPI1_NSS_OUT_PORT, PIN_MASK_TO_PIN_NUMBER(SPI1_NSS_OUT_PIN)));
|
||||||
|
|
||||||
// switch seller back to pin 1.3
|
// switch seller back to NSS_IN
|
||||||
HAL_GPIO_WritePin(GPIO_1, GPIO_PIN_6, GPIO_PIN_LOW);
|
HAL_GPIO_WritePin(SPI1_SWITCH_PORT, SPI1_SWITCH_PIN, GPIO_PIN_LOW);
|
||||||
spiNssPinIsBlocked = false; // unblock spi pin
|
spi1NssPinIsBlocked = false; // unblock spi pin
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
HAL_GPIO_PinConfig(SPI0_NSS_IN_PORT, SPI0_NSS_IN_PIN, HAL_GPIO_GetPinDirection(SPI0_NSS_OUT_PORT, SPI0_NSS_OUT_PIN),
|
||||||
|
(HAL_GPIO_PullTypeDef)PIN_GET_PAD_CONFIG(PORT_1_PUPD, PIN_MASK_TO_PIN_NUMBER(SPI0_NSS_OUT_PIN)), HAL_GPIO_DS_2MA);
|
||||||
|
HAL_GPIO_WritePin (SPI0_NSS_IN_PORT, SPI0_NSS_IN_PIN,
|
||||||
|
(GPIO_PinState)GPIO_GET_PIN_STATE(SPI0_NSS_OUT_PORT, PIN_MASK_TO_PIN_NUMBER(SPI0_NSS_OUT_PIN)));
|
||||||
|
|
||||||
|
// switch seller back to NSS_IN
|
||||||
|
HAL_GPIO_WritePin(SPI0_SWITCH_PORT, SPI0_SWITCH_PIN, GPIO_PIN_LOW);
|
||||||
|
spi0NssPinIsBlocked = false; // unblock spi pin
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user