Skyward boardcore
Loading...
Searching...
No Matches
USART.h
Go to the documentation of this file.
1/* Copyright (c) 2022 Skyward Experimental Rocketry
2 * Author: Emilio Corigliano
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
26#include <interfaces/arch_registers.h>
27#include <miosix.h>
28#include <utils/ClockUtils.h>
29
30#include <chrono>
31#include <string>
32
33#include "arch/common/drivers/serial.h"
34
35#ifndef USE_MOCK_PERIPHERALS
36using USARTType = USART_TypeDef;
37#else
38// TODO: Create test utils
39using USARTType = USART_TypeDef;
40#endif
41
42#if defined(UART8)
43#define N_USART_PORTS 8
44#elif defined(UART7)
45#define N_USART_PORTS 7
46#elif defined(USART6)
47#define N_USART_PORTS 6
48#elif defined(UART5)
49#define N_USART_PORTS 5
50#elif defined(UART4)
51#define N_USART_PORTS 4
52#elif defined(USART3)
53#define N_USART_PORTS 3
54#elif defined(USART2)
55#define N_USART_PORTS 2
56#elif defined(USART1)
57#define N_USART_PORTS 1
58#else
59#error "Your architecture doesn't support UART"
60#endif
61
62namespace Boardcore
63{
64
70{
71public:
78 explicit USARTInterface(USARTType* usart, int baudrate);
79
80 virtual ~USARTInterface() = 0;
81
91 [[nodiscard]] virtual bool readBlocking(
92 void* buffer, size_t nBytes,
93 std::chrono::nanoseconds timeout = std::chrono::nanoseconds::zero())
94 {
95 size_t temp;
96 return readImpl(buffer, nBytes, temp, true, timeout);
97 };
98
109 [[nodiscard]] virtual bool readBlocking(
110 void* buffer, size_t nBytes, size_t& nBytesRead,
111 std::chrono::nanoseconds timeout = std::chrono::nanoseconds::zero())
112 {
113 return readImpl(buffer, nBytes, nBytesRead, true, timeout);
114 };
115
121 virtual void write(const void* buf, size_t nBytes) = 0;
122
127 virtual void writeString(const char* buffer) = 0;
128
132 int getId() { return id; };
133
134protected:
147 virtual bool readImpl(void* buffer, size_t nBytes, size_t& nBytesRead,
148 const bool blocking,
149 std::chrono::nanoseconds timeout) = 0;
150
152 int id = -1;
153 IRQn_Type irqn;
154 std::string serialPortName;
159
161};
162
169class USART : public USARTInterface
170{
171public:
172 enum class WordLength : bool
173 {
174 BIT8 = 0,
175 BIT9 = 1
176 };
177
178 enum class ParityBit : bool
179 {
180 NO_PARITY = 0,
181 PARITY = 1
182 };
183
190 void IRQhandleInterrupt();
191
210 unsigned int queueLen = INTERNAL_QUEUE_LENGTH);
211
213 USART(const USART&) = delete;
214 USART& operator=(const USART&) = delete;
215 USART(USART&&) = delete;
216 USART& operator=(USART&&) = delete;
217
223 ~USART() override;
224
234 [[nodiscard]] bool read(void* buffer, size_t nBytes)
235 {
236 size_t temp;
237 auto timeout = std::chrono::nanoseconds::zero();
238 return readImpl(buffer, nBytes, temp, false, timeout);
239 }
240
251 [[nodiscard]] bool read(void* buffer, size_t nBytes, size_t& nBytesRead)
252 {
253 auto timeout = std::chrono::nanoseconds::zero();
254 return readImpl(buffer, nBytes, nBytesRead, false, timeout);
255 };
256
262 void write(const void* buf, size_t nBytes);
263
268 void writeString(const char* buffer);
269
276 bool writeFile(const std::string& fileName);
277
283 void setWordLength(WordLength wl);
284
291 void setParity(ParityBit pb);
292
298 void setStopBits(int stopBits);
299
306 void setBaudrate(int baudrate);
307
314 void setOversampling(bool oversampling);
315
319 void clearQueue();
320
321private:
334 [[nodiscard]] bool readImpl(void* buffer, size_t nBytes, size_t& nBytesRead,
335 const bool blocking,
336 std::chrono::nanoseconds timeout) override;
337
338 miosix::FastMutex rxMutex;
339 miosix::FastMutex txMutex;
340
341 miosix::Thread* rxWaiter =
342 nullptr;
343
344 miosix::DynUnsyncQueue<char> rxQueue;
345 bool idle = true;
347 WordLength wordLength = WordLength::BIT8;
348 int stopBits = 1;
349 bool over8 = false;
350 bool error = false;
351
353 static constexpr unsigned int INTERNAL_QUEUE_LENGTH = 256;
354};
355
360{
361public:
374
385 STM32SerialWrapper(USARTType* usart, int baudrate, miosix::GpioPin tx,
386 miosix::GpioPin rx);
387
393
399
405 void write(const void* buf, size_t nBytes);
406
411 void writeString(const char* buffer);
412
413private:
426 [[nodiscard]] bool readImpl(void* buffer, size_t nBytes, size_t& nBytesRead,
427 const bool blocking,
428 std::chrono::nanoseconds timeout) override;
429
434 bool serialCommSetup();
435
436 miosix::STM32Serial* serial;
437
439 int fd;
440};
441
442} // namespace Boardcore
USART_TypeDef USARTType
Definition USART.h:36
static PrintLogger getLogger(const string &name)
Wrapper for the STM32Serial driver in miosix.
Definition USART.h:360
void write(const void *buf, size_t nBytes)
Blocking write operation.
Definition USART.cpp:734
STM32SerialWrapper & operator=(STM32SerialWrapper &&)=delete
STM32SerialWrapper(STM32SerialWrapper &&)=delete
STM32SerialWrapper(USARTType *usart, int baudrate)
Initializes the serialPortName and initializes the default pins, which are:
Definition USART.cpp:625
~STM32SerialWrapper()
Removes the device from the list of the devices and closes the file of the device.
Definition USART.cpp:672
STM32SerialWrapper & operator=(const STM32SerialWrapper &)=delete
STM32SerialWrapper(const STM32SerialWrapper &)=delete
void writeString(const char *buffer)
Write a string to the serial, comprising the '\0' character.
Definition USART.cpp:739
Driver for STM32F4 low level USART/UART peripheral.
Definition USART.h:170
void setParity(ParityBit pb)
Set the presence of the parity in the data sent.
Definition USART.cpp:422
void IRQhandleInterrupt()
Interrupt handler that deals with receive and idle interrupts.
Definition USART.cpp:311
void setWordLength(WordLength wl)
Set the length of the word to 8 or to 9.
Definition USART.cpp:414
void writeString(const char *buffer)
Write a string to the serial, comprising the '\0' character.
Definition USART.cpp:564
void clearQueue()
Clears the rxQueue.
Definition USART.cpp:616
bool read(void *buffer, size_t nBytes)
Non-blocking read operation to read nBytes or till the data transfer is complete.
Definition USART.h:234
USART(const USART &)=delete
bool writeFile(const std::string &fileName)
Given a filename, uses the USART interface to stream the file in 1KB chunks.
Definition USART.cpp:596
USART(USARTType *usart, int baudrate, unsigned int queueLen=INTERNAL_QUEUE_LENGTH)
Automatically enables the peripheral and timer peripheral clock.
Definition USART.cpp:360
USART & operator=(const USART &)=delete
void write(const void *buf, size_t nBytes)
Blocking write operation.
Definition USART.cpp:542
void setStopBits(int stopBits)
Set the number of stop bits.
Definition USART.cpp:430
void setOversampling(bool oversampling)
Sets the Over8 bit.
Definition USART.cpp:439
void setBaudrate(int baudrate)
Set the baudrate in the BRR register.
Definition USART.cpp:447
USART & operator=(USART &&)=delete
~USART() override
Disables the flags for the generation of the interrupts, the IRQ from the NVIC, the peripheral and re...
Definition USART.cpp:400
bool read(void *buffer, size_t nBytes, size_t &nBytesRead)
Non-blocking read operation to read nBytes or till the data transfer is complete.
Definition USART.h:251
USART(USART &&)=delete
Abstract class that implements the interface for the USART/UART serial communication.
Definition USART.h:70
int id
Can be from 1 to 8, -1 is invalid.
Definition USART.h:152
virtual bool readBlocking(void *buffer, size_t nBytes, std::chrono::nanoseconds timeout=std::chrono::nanoseconds::zero())
Blocking read operation to read nBytes until the data transfer is complete or the timeout is reached.
Definition USART.h:91
virtual void writeString(const char *buffer)=0
Write a string to the serial, comprising the '\0' character.
USARTInterface(USARTType *usart, int baudrate)
Constructor of the USART in order to assign usart and baudrate.
Definition USART.cpp:241
int getId()
Returns the id of the serial.
Definition USART.h:132
IRQn_Type irqn
IRQ number.
Definition USART.h:153
virtual bool readImpl(void *buffer, size_t nBytes, size_t &nBytesRead, const bool blocking, std::chrono::nanoseconds timeout)=0
Read method implementation that supports both blocking and non-blocking mode and the return of the nu...
virtual ~USARTInterface()=0
Definition USART.cpp:309
std::string serialPortName
Definition USART.h:154
virtual void write(const void *buf, size_t nBytes)=0
Blocking write operation.
virtual bool readBlocking(void *buffer, size_t nBytes, size_t &nBytesRead, std::chrono::nanoseconds timeout=std::chrono::nanoseconds::zero())
Blocking read operation to read nBytes until the data transfer is complete or the timeout is reached.
Definition USART.h:109
This file includes all the types the logdecoder script will decode.