26#include <interfaces/arch_registers.h>
33#include "arch/common/drivers/serial.h"
35#ifndef USE_MOCK_PERIPHERALS
43#define N_USART_PORTS 8
45#define N_USART_PORTS 7
47#define N_USART_PORTS 6
49#define N_USART_PORTS 5
51#define N_USART_PORTS 4
53#define N_USART_PORTS 3
55#define N_USART_PORTS 2
57#define N_USART_PORTS 1
59#error "Your architecture doesn't support UART"
92 void* buffer,
size_t nBytes,
93 std::chrono::nanoseconds timeout = std::chrono::nanoseconds::zero())
96 return readImpl(buffer, nBytes, temp,
true, timeout);
110 void* buffer,
size_t nBytes,
size_t& nBytesRead,
111 std::chrono::nanoseconds timeout = std::chrono::nanoseconds::zero())
113 return readImpl(buffer, nBytes, nBytesRead,
true, timeout);
121 virtual void write(
const void* buf,
size_t nBytes) = 0;
149 virtual bool readImpl(
void* buffer,
size_t nBytes,
size_t& nBytesRead,
151 std::chrono::nanoseconds timeout) = 0;
212 unsigned int queueLen = INTERNAL_QUEUE_LENGTH);
236 [[nodiscard]]
bool read(
void* buffer,
size_t nBytes)
239 auto timeout = std::chrono::nanoseconds::zero();
240 return readImpl(buffer, nBytes, temp,
false, timeout);
253 [[nodiscard]]
bool read(
void* buffer,
size_t nBytes,
size_t& nBytesRead)
255 auto timeout = std::chrono::nanoseconds::zero();
256 return readImpl(buffer, nBytes, nBytesRead,
false, timeout);
264 void write(
const void* buf,
size_t nBytes);
278 bool writeFile(
const std::string& fileName);
336 [[nodiscard]]
bool readImpl(
void* buffer,
size_t nBytes,
size_t& nBytesRead,
338 std::chrono::nanoseconds timeout)
override;
340 miosix::FastMutex rxMutex;
341 miosix::FastMutex txMutex;
343 miosix::Thread* rxWaiter =
346 miosix::DynUnsyncQueue<char> rxQueue;
355 static constexpr unsigned int INTERNAL_QUEUE_LENGTH = 256;
407 void write(
const void* buf,
size_t nBytes);
428 [[nodiscard]]
bool readImpl(
void* buffer,
size_t nBytes,
size_t& nBytesRead,
430 std::chrono::nanoseconds timeout)
override;
436 bool serialCommSetup();
438 miosix::STM32Serial* serial;
static PrintLogger getLogger(const string &name)
Wrapper for the STM32Serial driver in miosix.
void write(const void *buf, size_t nBytes)
Blocking write operation.
STM32SerialWrapper & operator=(STM32SerialWrapper &&)=delete
STM32SerialWrapper(STM32SerialWrapper &&)=delete
~STM32SerialWrapper()
Removes the device from the list of the devices and closes the file of the device.
STM32SerialWrapper & operator=(const STM32SerialWrapper &)=delete
STM32SerialWrapper(const STM32SerialWrapper &)=delete
void writeString(const char *buffer)
Write a string to the serial, comprising the '\0' character.
Driver for STM32F4 low level USART/UART peripheral.
void setParity(ParityBit pb)
Set the presence of the parity in the data sent.
void IRQhandleInterrupt()
Interrupt handler that deals with receive and idle interrupts.
void setWordLength(WordLength wl)
Set the length of the word to 8 or to 9.
void writeString(const char *buffer)
Write a string to the serial, comprising the '\0' character.
void clearQueue()
Clears the rxQueue.
bool read(void *buffer, size_t nBytes)
Non-blocking read operation to read nBytes or till the data transfer is complete.
USART(const USART &)=delete
bool writeFile(const std::string &fileName)
Given a filename, uses the USART interface to stream the file in 1KB chunks.
USART & operator=(const USART &)=delete
void write(const void *buf, size_t nBytes)
Blocking write operation.
void setStopBits(int stopBits)
Set the number of stop bits.
void setOversampling(bool oversampling)
Sets the Over8 bit.
void setBaudrate(int baudrate)
Set the baudrate in the BRR register.
USART & operator=(USART &&)=delete
~USART() override
Disables the flags for the generation of the interrupts, the IRQ from the NVIC, the peripheral and re...
bool read(void *buffer, size_t nBytes, size_t &nBytesRead)
Non-blocking read operation to read nBytes or till the data transfer is complete.
Abstract class that implements the interface for the USART/UART serial communication.
int id
Can be from 1 to 8, -1 is invalid.
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.
virtual void writeString(const char *buffer)=0
Write a string to the serial, comprising the '\0' character.
USARTType * getPeripheral()
int getId()
Returns the id of the serial.
IRQn_Type irqn
IRQ number.
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
std::string serialPortName
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.
Driver for the VN100S IMU.