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
135
136protected:
149 virtual bool readImpl(void* buffer, size_t nBytes, size_t& nBytesRead,
150 const bool blocking,
151 std::chrono::nanoseconds timeout) = 0;
152
154 int id = -1;
155 IRQn_Type irqn;
156 std::string serialPortName;
161
163};
164
171class USART : public USARTInterface
172{
173public:
174 enum class WordLength : bool
175 {
176 BIT8 = 0,
177 BIT9 = 1
178 };
179
180 enum class ParityBit : bool
181 {
182 NO_PARITY = 0,
183 PARITY = 1
184 };
185
192 void IRQhandleInterrupt();
193
212 unsigned int queueLen = INTERNAL_QUEUE_LENGTH);
213
215 USART(const USART&) = delete;
216 USART& operator=(const USART&) = delete;
217 USART(USART&&) = delete;
218 USART& operator=(USART&&) = delete;
219
225 ~USART() override;
226
236 [[nodiscard]] bool read(void* buffer, size_t nBytes)
237 {
238 size_t temp;
239 auto timeout = std::chrono::nanoseconds::zero();
240 return readImpl(buffer, nBytes, temp, false, timeout);
241 }
242
253 [[nodiscard]] bool read(void* buffer, size_t nBytes, size_t& nBytesRead)
254 {
255 auto timeout = std::chrono::nanoseconds::zero();
256 return readImpl(buffer, nBytes, nBytesRead, false, timeout);
257 };
258
264 void write(const void* buf, size_t nBytes);
265
270 void writeString(const char* buffer);
271
278 bool writeFile(const std::string& fileName);
279
285 void setWordLength(WordLength wl);
286
293 void setParity(ParityBit pb);
294
300 void setStopBits(int stopBits);
301
308 void setBaudrate(int baudrate);
309
316 void setOversampling(bool oversampling);
317
321 void clearQueue();
322
323private:
336 [[nodiscard]] bool readImpl(void* buffer, size_t nBytes, size_t& nBytesRead,
337 const bool blocking,
338 std::chrono::nanoseconds timeout) override;
339
340 miosix::FastMutex rxMutex;
341 miosix::FastMutex txMutex;
342
343 miosix::Thread* rxWaiter =
344 nullptr;
345
346 miosix::DynUnsyncQueue<char> rxQueue;
347 bool idle = true;
349 WordLength wordLength = WordLength::BIT8;
350 int stopBits = 1;
351 bool over8 = false;
352 bool error = false;
353
355 static constexpr unsigned int INTERNAL_QUEUE_LENGTH = 256;
356};
357
362{
363public:
376
387 STM32SerialWrapper(USARTType* usart, int baudrate, miosix::GpioPin tx,
388 miosix::GpioPin rx);
389
395
401
407 void write(const void* buf, size_t nBytes);
408
413 void writeString(const char* buffer);
414
415private:
428 [[nodiscard]] bool readImpl(void* buffer, size_t nBytes, size_t& nBytesRead,
429 const bool blocking,
430 std::chrono::nanoseconds timeout) override;
431
436 bool serialCommSetup();
437
438 miosix::STM32Serial* serial;
439
441 int fd;
442};
443
444} // 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:362
void write(const void *buf, size_t nBytes)
Blocking write operation.
Definition USART.cpp:735
STM32SerialWrapper & operator=(STM32SerialWrapper &&)=delete
STM32SerialWrapper(STM32SerialWrapper &&)=delete
~STM32SerialWrapper()
Removes the device from the list of the devices and closes the file of the device.
Definition USART.cpp:673
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:740
Driver for STM32F4 low level USART/UART peripheral.
Definition USART.h:172
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:565
void clearQueue()
Clears the rxQueue.
Definition USART.cpp:617
bool read(void *buffer, size_t nBytes)
Non-blocking read operation to read nBytes or till the data transfer is complete.
Definition USART.h:236
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:597
USART & operator=(const USART &)=delete
void write(const void *buf, size_t nBytes)
Blocking write operation.
Definition USART.cpp:543
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:253
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:154
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.
USARTType * getPeripheral()
Definition USART.h:134
int getId()
Returns the id of the serial.
Definition USART.h:132
IRQn_Type irqn
IRQ number.
Definition USART.h:155
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:156
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
Driver for the VN100S IMU.