From 2f76e62b2cfbd9d5001dd40c01c7a8907b338c72 Mon Sep 17 00:00:00 2001 From: EmilioCorigliano <emilio.corigliano@mail.polimi.it> Date: Mon, 3 Apr 2023 09:17:44 +0200 Subject: [PATCH] [UBXGPSSerial] Correctly using the USART bus instead of creating it --- src/shared/sensors/UBXGPS/UBXGPSSerial.cpp | 28 +++++++++---------- src/shared/sensors/UBXGPS/UBXGPSSerial.h | 21 +++++++------- .../algorithms/NAS/test-nas-parafoil.cpp | 14 ++++++++-- src/tests/sensors/test-ubxgps-serial.cpp | 15 ++++++++-- 4 files changed, 47 insertions(+), 31 deletions(-) diff --git a/src/shared/sensors/UBXGPS/UBXGPSSerial.cpp b/src/shared/sensors/UBXGPS/UBXGPSSerial.cpp index c7051b97e..380d959a9 100644 --- a/src/shared/sensors/UBXGPS/UBXGPSSerial.cpp +++ b/src/shared/sensors/UBXGPS/UBXGPSSerial.cpp @@ -31,14 +31,11 @@ using namespace miosix; namespace Boardcore { -UBXGPSSerial::UBXGPSSerial(int baudrate, uint8_t sampleRate, - USARTType* usartNumber, int defaultBaudrate) +UBXGPSSerial::UBXGPSSerial(USART& usart, uint32_t baudrate, uint8_t sampleRate, + uint32_t defaultBaudrate) + : usart(usart), baudrate(baudrate), sampleRate(sampleRate), + defaultBaudrate(defaultBaudrate) { - this->usart = nullptr; - this->baudrate = baudrate; - this->defaultBaudrate = defaultBaudrate; - this->sampleRate = sampleRate; - this->usartNumber = usartNumber; } bool UBXGPSSerial::init() @@ -170,7 +167,7 @@ bool UBXGPSSerial::setBaudrateAndUBX(bool safe) bool UBXGPSSerial::setSerialCommunication() { - usart = new USART(usartNumber, defaultBaudrate); + usart.setBaudrate(defaultBaudrate); // Change the baudrate only if it is different than the default if (baudrate != defaultBaudrate) { @@ -183,7 +180,7 @@ bool UBXGPSSerial::setSerialCommunication() } miosix::Thread::sleep(100); - usart->setBaudrate(baudrate); + usart.setBaudrate(baudrate); return true; } @@ -252,8 +249,9 @@ bool UBXGPSSerial::readUBXFrame(UBXFrame& frame) size_t i = 0; while (i < 2) { + // TODO: Check if this can deadlock uint8_t c; - if (usart->read(&c, 1) <= 0) // No more data available + if (usart.read(&c, 1) <= 0) // No more data available return false; if (c == UBXFrame::PREAMBLE[i]) @@ -273,10 +271,10 @@ bool UBXGPSSerial::readUBXFrame(UBXFrame& frame) } } - if (usart->read(&frame.message, 2) <= 0 || - usart->read(&frame.payloadLength, 2) <= 0 || - usart->read(frame.payload, frame.getRealPayloadLength()) <= 0 || - usart->read(frame.checksum, 2) <= 0) + if (usart.read(&frame.message, 2) <= 0 || + usart.read(&frame.payloadLength, 2) <= 0 || + usart.read(frame.payload, frame.getRealPayloadLength()) <= 0 || + usart.read(frame.checksum, 2) <= 0) return false; if (!frame.isValid()) @@ -299,7 +297,7 @@ bool UBXGPSSerial::writeUBXFrame(const UBXFrame& frame) uint8_t packedFrame[frame.getLength()]; frame.writePacked(packedFrame); - usart->write(packedFrame, frame.getLength()); + usart.write(packedFrame, frame.getLength()); return true; } diff --git a/src/shared/sensors/UBXGPS/UBXGPSSerial.h b/src/shared/sensors/UBXGPS/UBXGPSSerial.h index 9187466a6..dbcb156d5 100644 --- a/src/shared/sensors/UBXGPS/UBXGPSSerial.h +++ b/src/shared/sensors/UBXGPS/UBXGPSSerial.h @@ -56,15 +56,15 @@ public: /** * @brief Construct a new UBXGPSSerial object. * + * @param usart USART bus to be used for communication. * @param baudrate Baudrate to communicate with the device (max: 921600, * min: 4800 for NEO-M9N). * @param sampleRate GPS sample rate (max: 25 for NEO-M9N). - * @param serialPortNumber Number of the serial port connected to the GPS. - * @param serialPortName Name of the file for the gps device. * @param defaultBaudrate Startup baudrate (38400 for NEO-M9N). */ - UBXGPSSerial(int baudrate = 921600, uint8_t sampleRate = 10, - USARTType *usartNumber = USART2, int defaultBaudrate = 38400); + explicit UBXGPSSerial(USART& usart, uint32_t baudrate = 921600, + uint8_t sampleRate = 10, + uint32_t defaultBaudrate = 38400); /** * @brief Sets up the serial port baudrate, disables the NMEA messages, @@ -139,7 +139,7 @@ private: * @param frame The received frame. * @return True if a valid frame was read. */ - bool readUBXFrame(UBXFrame &frame); + bool readUBXFrame(UBXFrame& frame); /** * @brief Writes a UBX frame. @@ -147,7 +147,7 @@ private: * @param frame The frame to write. * @return True if the frame is valid. */ - bool writeUBXFrame(const UBXFrame &frame); + bool writeUBXFrame(const UBXFrame& frame); /** * @brief Writes a UBX frame and waits for its acknowledgement. @@ -155,15 +155,14 @@ private: * @param frame The frame to write. * @return True if the frame is valid and acknowledged. */ - bool safeWriteUBXFrame(const UBXFrame &frame); + bool safeWriteUBXFrame(const UBXFrame& frame); void run() override; - int baudrate; - int defaultBaudrate; + USART& usart; + uint32_t baudrate; uint8_t sampleRate; // [Hz] - USARTType *usartNumber; - USART *usart; // The usart interface + uint32_t defaultBaudrate; mutable miosix::FastMutex mutex; UBXGPSData threadSample{}; diff --git a/src/tests/algorithms/NAS/test-nas-parafoil.cpp b/src/tests/algorithms/NAS/test-nas-parafoil.cpp index e89dc6ce8..ed36658da 100644 --- a/src/tests/algorithms/NAS/test-nas-parafoil.cpp +++ b/src/tests/algorithms/NAS/test-nas-parafoil.cpp @@ -36,6 +36,10 @@ using namespace miosix; using namespace Boardcore; using namespace Eigen; +// USART2: AF7 +typedef miosix::Gpio<GPIOA_BASE, 2> u2tx1; +typedef miosix::Gpio<GPIOA_BASE, 3> u2rx1; + NASConfig getEKConfig(); void setInitialOrientation(); void init(); @@ -48,11 +52,17 @@ Vector2f startPos = Vector2f(45.501141, 9.156281); NAS* nas; SPIBus spi1(SPI1); -MPU9250* imu = nullptr; +MPU9250* imu = nullptr; +USART usart(USART2, 38400); UBXGPSSerial* gps = nullptr; int main() { + u2rx1::getPin().alternateFunction(7); + u2rx1::getPin().mode(Mode::ALTERNATE); + u2tx1::getPin().alternateFunction(7); + u2tx1::getPin().mode(Mode::ALTERNATE); + init(); nas = new NAS(getEKConfig()); @@ -112,7 +122,7 @@ void init() imu = new MPU9250(spi1, sensors::mpu9250::cs::getPin()); imu->init(); - gps = new UBXGPSSerial(38400, 10, USART2, 38400); + gps = new UBXGPSSerial(usart, 38400, 10, 38400); gps->init(); gps->start(); diff --git a/src/tests/sensors/test-ubxgps-serial.cpp b/src/tests/sensors/test-ubxgps-serial.cpp index 472f3dcde..0039317c1 100644 --- a/src/tests/sensors/test-ubxgps-serial.cpp +++ b/src/tests/sensors/test-ubxgps-serial.cpp @@ -32,12 +32,23 @@ using namespace miosix; #define RATE 4 +// USART2: AF7 +typedef miosix::Gpio<GPIOA_BASE, 2> u2tx1; +typedef miosix::Gpio<GPIOA_BASE, 3> u2rx1; + int main() { printf("Welcome to the ublox test\n"); + u2rx1::getPin().alternateFunction(7); + u2rx1::getPin().mode(Mode::ALTERNATE); + u2tx1::getPin().alternateFunction(7); + u2tx1::getPin().mode(Mode::ALTERNATE); + + USART usart(USART2, 38400); + // Keep GPS baud rate at default for easier testing - UBXGPSSerial gps(38400, RATE, USART2, 9600); + UBXGPSSerial gps(usart, 38400, RATE, 9600); UBXGPSData dataGPS; printf("Gps allocated\n"); @@ -67,13 +78,11 @@ int main() while (true) { - printf("a\n"); // Give time to the thread Thread::sleep(1000 / RATE); // Sample gps.sample(); - printf("b\n"); dataGPS = gps.getLastSample(); // Print out the latest sample -- GitLab