From e18d973fd1b4c8268e26421866123d9f8898a5ff Mon Sep 17 00:00:00 2001
From: Alberto Nidasio <alberto.nidasio@skywarder.eu>
Date: Sun, 15 May 2022 21:44:55 +0200
Subject: [PATCH] [USART] Removed pin initialization from Skyward
implementation
---
src/shared/drivers/usart/USART.cpp | 149 +++++++------------------
src/shared/drivers/usart/USART.h | 69 ++++--------
src/tests/drivers/usart/test-usart.cpp | 14 ++-
3 files changed, 71 insertions(+), 161 deletions(-)
diff --git a/src/shared/drivers/usart/USART.cpp b/src/shared/drivers/usart/USART.cpp
index bc5612a08..65e675a44 100644
--- a/src/shared/drivers/usart/USART.cpp
+++ b/src/shared/drivers/usart/USART.cpp
@@ -251,29 +251,6 @@ namespace Boardcore
USARTInterface::~USARTInterface() {}
-bool USARTInterface::initPins(miosix::GpioPin tx, int nAFtx, miosix::GpioPin rx,
- int nAFrx)
-{
- if (pinInitialized)
- {
- return false;
- }
-
- miosix::FastInterruptDisableLock dLock;
-
- this->tx = tx;
- this->rx = rx;
-
- tx.mode(miosix::Mode::ALTERNATE);
- tx.alternateFunction(nAFtx);
-
- rx.mode(miosix::Mode::ALTERNATE);
- rx.alternateFunction(nAFrx);
-
- pinInitialized = true;
- return true;
-}
-
void USART::IRQhandleInterrupt()
{
char c;
@@ -315,110 +292,43 @@ USART::USART(USARTType *usart, Baudrate baudrate, unsigned int queueLen)
{
case USART1_BASE:
this->id = 1;
- initPins(u1tx1::getPin(), 7, u1rx1::getPin(), 7);
- irqn = USART1_IRQn;
+ irqn = USART1_IRQn;
break;
case USART2_BASE:
this->id = 2;
- initPins(u2tx1::getPin(), 7, u2rx1::getPin(), 7);
- irqn = USART2_IRQn;
+ irqn = USART2_IRQn;
break;
case USART3_BASE:
this->id = 3;
- initPins(u3tx1::getPin(), 7, u3rx1::getPin(), 7);
- irqn = USART3_IRQn;
+ irqn = USART3_IRQn;
break;
case UART4_BASE:
this->id = 4;
- initPins(u4tx1::getPin(), 8, u4rx1::getPin(), 8);
- irqn = UART4_IRQn;
+ irqn = UART4_IRQn;
break;
case UART5_BASE:
this->id = 5;
- initPins(u5tx::getPin(), 8, u5rx::getPin(), 8);
- irqn = UART5_IRQn;
+ irqn = UART5_IRQn;
break;
case USART6_BASE:
this->id = 6;
- initPins(u6tx1::getPin(), 8, u6rx1::getPin(), 8);
- irqn = USART6_IRQn;
+ irqn = USART6_IRQn;
break;
#ifdef STM32F429xx
case UART7_BASE:
this->id = 7;
- initPins(u7tx1::getPin(), 8, u7rx1::getPin(), 8);
- irqn = UART7_IRQn;
+ irqn = UART7_IRQn;
break;
case UART8_BASE:
this->id = 8;
- initPins(u8tx::getPin(), 8, u8rx::getPin(), 8);
- irqn = UART8_IRQn;
+ irqn = UART8_IRQn;
break;
#endif // STM32F429xx
}
- commonConstructor(usart, baudrate);
-}
-
-USART::USART(USARTType *usart, Baudrate baudrate, miosix::GpioPin tx,
- miosix::GpioPin rx, unsigned int queueLen)
- : rxQueue(queueLen)
-{
- // Setting the id of the serial port
- switch (reinterpret_cast<uint32_t>(usart))
- {
- case USART1_BASE:
- this->id = 1;
- initPins(tx, 7, rx, 7);
- irqn = USART1_IRQn;
- break;
- case USART2_BASE:
- this->id = 2;
- initPins(tx, 7, rx, 7);
- irqn = USART2_IRQn;
- break;
- case USART3_BASE:
- this->id = 3;
- initPins(tx, 7, rx, 7);
- irqn = USART3_IRQn;
- break;
- case UART4_BASE:
- this->id = 4;
- initPins(tx, 8, rx, 8);
- irqn = UART4_IRQn;
- break;
- case UART5_BASE:
- this->id = 5;
- initPins(tx, 8, rx, 8);
- irqn = UART5_IRQn;
- break;
- case USART6_BASE:
- this->id = 6;
- initPins(tx, 8, rx, 8);
- irqn = USART6_IRQn;
- break;
-#ifdef STM32F429xx
- case UART7_BASE:
- this->id = 7;
- initPins(tx, 8, rx, 8);
- irqn = UART7_IRQn;
- break;
- case UART8_BASE:
- this->id = 8;
- initPins(tx, 8, rx, 8);
- irqn = UART8_IRQn;
- break;
-#endif // STM32F429xx
- }
-
- commonConstructor(usart, baudrate);
-}
-
-void USART::commonConstructor(USARTType *usart, Baudrate baudrate)
-{
this->usart = usart;
- // Enabling the peripehral on the right APB
+ // Enabling the peripheral on the right APB
ClockUtils::enablePeripheralClock(usart);
RCC_SYNC();
@@ -440,25 +350,23 @@ void USART::commonConstructor(USARTType *usart, Baudrate baudrate)
USART::~USART()
{
- {
- miosix::FastInterruptDisableLock dLock;
+ miosix::FastInterruptDisableLock dLock;
- // Take out the usart object we are going to destruct
- USART::ports[this->id - 1] = nullptr;
+ // Take out the usart object we are going to destruct
+ USART::ports[this->id - 1] = nullptr;
- // Disabling the usart
- usart->CR1 &= ~(USART_CR1_UE | USART_CR1_TE | USART_CR1_RE);
+ // Disabling the usart
+ usart->CR1 &= ~(USART_CR1_UE | USART_CR1_TE | USART_CR1_RE);
- // Disabling the interrupt of the serial port
- NVIC_DisableIRQ(irqn);
- }
+ // Disabling the interrupt of the serial port
+ NVIC_DisableIRQ(irqn);
}
bool USART::init()
{
- if (id < 1 || id > MAX_SERIAL_PORTS || !pinInitialized)
+ if (id < 1 || id > MAX_SERIAL_PORTS)
{
- TRACE("Not supported USART id or pins not initialized\n");
+ TRACE("Not supported USART id\n");
return false;
}
@@ -761,6 +669,27 @@ bool STM32SerialWrapper::serialCommSetup()
return true;
}
+bool STM32SerialWrapper::initPins(miosix::GpioPin tx, int nAFtx,
+ miosix::GpioPin rx, int nAFrx)
+{
+ if (pinInitialized)
+ return false;
+
+ miosix::FastInterruptDisableLock dLock;
+
+ this->tx = tx;
+ this->rx = rx;
+
+ tx.mode(miosix::Mode::ALTERNATE);
+ tx.alternateFunction(nAFtx);
+
+ rx.mode(miosix::Mode::ALTERNATE);
+ rx.alternateFunction(nAFrx);
+
+ pinInitialized = true;
+ return true;
+}
+
int STM32SerialWrapper::writeString(const char *data)
{
// strlen + 1 in order to send the '/0' terminated string
diff --git a/src/shared/drivers/usart/USART.h b/src/shared/drivers/usart/USART.h
index 05cee3a16..14faf77c8 100644
--- a/src/shared/drivers/usart/USART.h
+++ b/src/shared/drivers/usart/USART.h
@@ -101,6 +101,7 @@ typedef miosix::Gpio<GPIOE_BASE, 0> u8rx;
namespace Boardcore
{
+
/**
* @brief Abstract class that implements the interface for the USART/UART serial
* communication.
@@ -155,24 +156,11 @@ public:
int getId() { return id; };
protected:
- /**
- * @brief Initializes the pins with the appropriate alternate functions.
- *
- * @param tx Tranmission pin.
- * @param nAFtx Tranmission pin alternate function.
- * @param rx Reception pin.
- * @param nAFrx Reception pin alternate function.
- */
- bool initPins(miosix::GpioPin tx, int nAFtx, miosix::GpioPin rx, int nAFrx);
-
- ///< True if initPins() already called successfully, false otherwise
- bool pinInitialized = false;
-
miosix::GpioPin tx{GPIOA_BASE, 0};
miosix::GpioPin rx{GPIOA_BASE, 0};
USARTType *usart;
- int id = 1; ///< Can be 1, 2, 3, 4, 5, 6, 7, 8
+ int id = -1; ///< Can be from 1 to 8, -1 is invalid
bool initialized = false;
Baudrate baudrate; ///< Baudrate of the serial communication
};
@@ -214,15 +202,7 @@ public:
*
* Sets the default values for all the parameters (1 stop bit, 8 bit data,
* no control flow and no oversampling).
- * Initializes the serial port using the default pins, which are:
- * - USART1: tx=PA9 rx=PA10
- * - USART2: tx=PA2 rx=PA3
- * - USART3: tx=PB10 rx=PB11
- * - UART4: tx=PA0 rx=PA1
- * - UART5: tx=PC12 rx=PD2
- * - USART6: tx=PC6 rx=PC7
- * - UART7: tx=PE8 rx=PE7
- * - UART8: tx=PE1 rx=PE0
+ * Requires that the gpios are configured for the choosen uart.
*
* @param usart structure that represents the usart peripheral [accepted
* are: USART1, USART2, USART3, UART4, UART5, USART6, UART7, UART8].
@@ -232,30 +212,12 @@ public:
USART(USARTType *usart, Baudrate baudrate,
unsigned int queueLen = usart_queue_default_capacity);
- /**
- * @brief Automatically enables the peripheral and timer peripheral clock.
- *
- * Sets the default values for all the parameters (1 stop bit, 8 bit data,
- * no control flow and no oversampling).
- * Initializes the serial port using custom pins.
- *
- * @param usart Structure that represents the usart peripheral [accepted
- * are: USART1, USART2, USART3, UART4, UART5, USART6, UART7, UART8].
- * @param baudrate Member of the enum Baudrate that represents the baudrate
- * with which the communication will take place.
- * @param tx Tranmission pin.
- * @param rx Reception pin.
- */
- USART(USARTType *usart, Baudrate baudrate, miosix::GpioPin tx,
- miosix::GpioPin rx,
- unsigned int queueLen = usart_queue_default_capacity);
-
/**
* @brief Disables the flags for the generation of the interrupts, the IRQ
* from the NVIC, the peripheral and removes his pointer from the ports
* list.
*/
- ~USART();
+ ~USART() override;
/**
* @brief Initializes the peripheral enabling his interrupts, the interrupts
@@ -265,23 +227,23 @@ public:
* All the setup phase must be done before the initialization of the
* peripheral. The pins must be initialized before calling this function.
*/
- bool init();
+ bool init() override;
/**
* @brief Blocking read operation to read nBytes or till the data transfer
* is complete.
*/
- int read(void *buffer, size_t nBytes);
+ int read(void *buffer, size_t nBytes) override;
/**
* @brief Blocking write operation.
*/
- int write(void *buf, size_t nChars);
+ int write(void *buf, size_t nChars) override;
/**
* @brief Write a string to the serial, comprising the '\0' character.
*/
- int writeString(const char *buffer);
+ int writeString(const char *buffer) override;
/**
* @brief Set the length of the word to 8 or to 9.
@@ -326,8 +288,6 @@ public:
void clearQueue();
private:
- void commonConstructor(USARTType *usart, Baudrate baudrate);
-
IRQn_Type irqn;
miosix::FastMutex rxMutex; ///< mutex for receiving on serial
miosix::FastMutex txMutex; ///< mutex for transmitting on serial
@@ -408,12 +368,25 @@ public:
int writeString(const char *buffer);
private:
+ /**
+ * @brief Initializes the pins with the appropriate alternate functions.
+ *
+ * @param tx Tranmission pin.
+ * @param nAFtx Tranmission pin alternate function.
+ * @param rx Reception pin.
+ * @param nAFrx Reception pin alternate function.
+ */
+ bool initPins(miosix::GpioPin tx, int nAFtx, miosix::GpioPin rx, int nAFrx);
+
/**
* @brief Creates a device that represents the serial port, adds it to the
* file system and opens the file that represents the device.
*/
bool serialCommSetup();
+ ///< True if initPins() already called successfully, false otherwise
+ bool pinInitialized = false;
+
miosix::STM32Serial *serial; ///< Pointer to the serial object
//< Port name of the port that has to be created for the communication
diff --git a/src/tests/drivers/usart/test-usart.cpp b/src/tests/drivers/usart/test-usart.cpp
index ce99d3d5a..48bff7359 100644
--- a/src/tests/drivers/usart/test-usart.cpp
+++ b/src/tests/drivers/usart/test-usart.cpp
@@ -136,6 +136,16 @@ bool testCommunicationSequential(USARTInterface *src, USARTInterface *dst)
*/
int main()
{
+ // Init serial port pins
+ u1rx2::getPin().mode(miosix::Mode::ALTERNATE);
+ u1rx2::getPin().alternateFunction(7);
+ u1tx1::getPin().mode(miosix::Mode::ALTERNATE);
+ u1tx1::getPin().alternateFunction(7);
+ u4rx1::getPin().mode(miosix::Mode::ALTERNATE);
+ u4rx1::getPin().alternateFunction(8);
+ u4tx1::getPin().mode(miosix::Mode::ALTERNATE);
+ u4tx1::getPin().alternateFunction(8);
+
bool testPassed = true;
printf("*** SERIAL 3 WORKING!\n");
for (unsigned int iBaud = 0;
@@ -145,12 +155,10 @@ int main()
printf("\n\n########################### %d\n", (int)baudrate);
// declaring the usart peripherals
- STM32SerialWrapper usartx(USART1, baudrate, u1rx2::getPin(),
- u1tx1::getPin());
+ STM32SerialWrapper usartx(USART1, baudrate);
usartx.init();
USART usarty(UART4, baudrate);
- // usarty.initPins(u5tx::getPin(), 8, u5rx::getPin(), 8);
// usarty.setOversampling(false);
// usarty.setStopBits(1);
// usarty.setWordLength(USART::WordLength::BIT8);
--
GitLab