Skip to content

Commit

Permalink
[uart] Add more interface methods
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Jul 31, 2020
1 parent cbce428 commit 026f82d
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 7 deletions.
16 changes: 16 additions & 0 deletions src/modm/architecture/interface/uart.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ class Uart : public ::modm::PeripheralDriver
static std::size_t
read(uint8_t *data, std::size_t length);

/// @return the size of the receive FIFO queue.
static std::size_t
receiveBufferSize();

/**
* Empty the receive FIFO queue and hardware buffer.
*
Expand All @@ -135,13 +139,25 @@ class Uart : public ::modm::PeripheralDriver
static std::size_t
discardReceiveBuffer();

/// @return the size of the transmit FIFO queue.
static std::size_t
transmitBufferSize();

/**
* Empty the transmit FIFO queue and hardware buffer.
*
* @return the size of the deleted FIFO queue.
*/
static std::size_t
discardTransmitBuffer();

/// @return `true` if an error occured during any write or read
static bool
hasError();

/// Reset the sticky error indication
static void
clearError();
#endif
};

Expand Down
42 changes: 42 additions & 0 deletions src/modm/platform/uart/stm32/uart.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ modm::platform::{{ name }}::isWriteFinished()
%% endif
}

std::size_t
modm::platform::{{ name }}::transmitBufferSize()
{
%% if options["buffered"]
return txBuffer.getSize();
%% else
return {{ hal }}::isTransmitRegisterEmpty() ? 0 : 1;
%% endif
}

std::size_t
modm::platform::{{ name }}::discardTransmitBuffer()
{
Expand Down Expand Up @@ -178,6 +188,16 @@ modm::platform::{{ name }}::read(uint8_t *data, std::size_t length)
%% endif
}

std::size_t
modm::platform::{{ name }}::receiveBufferSize()
{
%% if options["buffered"]
return rxBuffer.getSize();
%% else
return {{ hal }}::isReceiveRegisterNotEmpty() ? 1 : 0;
%% endif
}

std::size_t
modm::platform::{{ name }}::discardReceiveBuffer()
{
Expand All @@ -193,6 +213,27 @@ modm::platform::{{ name }}::discardReceiveBuffer()
%% endif
}

bool
modm::platform::{{ name }}::hasError()
{
return {{ hal }}::getInterruptFlags().any(
{{ hal }}::InterruptFlag::ParityError |
#ifdef USART_ISR_NE
{{ hal }}::InterruptFlag::NoiseError |
#endif
{{ hal }}::InterruptFlag::OverrunError | {{ hal }}::InterruptFlag::FramingError);
}
void
modm::platform::{{ name }}::clearError()
{
return {{ hal }}::acknowledgeInterruptFlags(
{{ hal }}::InterruptFlag::ParityError |
#ifdef USART_ISR_NE
{{ hal }}::InterruptFlag::NoiseError |
#endif
{{ hal }}::InterruptFlag::OverrunError | {{ hal }}::InterruptFlag::FramingError);
}


%% if options["buffered"]
%% set hal = "modm::platform::" ~ hal
Expand All @@ -219,5 +260,6 @@ MODM_ISR({{ name | upper }})
txBuffer.pop();
}
}
{{ hal }}::acknowledgeInterruptFlags({{ hal }}::InterruptFlag::OverrunError);
}
%% endif
18 changes: 16 additions & 2 deletions src/modm/platform/uart/stm32/uart.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@ private:
%% endif

public:
using Hal = {{ hal }};
// Expose jinja template parameters to be checked by e.g. drivers or application
static constexpr size_t RxBufferSize = {{ options["buffer.rx"] }};
static constexpr size_t TxBufferSize = {{ options["buffer.tx"] }};

public:
template< template<Peripheral _> class... Signals >
static void
connect(Gpio::InputType InputTypeRx = Gpio::InputType::PullUp)
connect(Gpio::InputType InputTypeRx = Gpio::InputType::PullUp,
Gpio::OutputType OutputTypeTx = Gpio::OutputType::PushPull)
{
using Connector = GpioConnector<Peripheral::{{ name }}, Signals...>;
using Tx = typename Connector::template GetSignal< Gpio::Signal::Tx >;
Expand All @@ -67,7 +69,7 @@ public:
"{{ name }}::connect() requires one Tx and/or one Rx signal!");

// Connector::disconnect();
Tx::setOutput(Gpio::OutputType::PushPull);
Tx::setOutput(OutputTypeTx);
Rx::setInput(InputTypeRx);
Connector::connect();
}
Expand Down Expand Up @@ -113,6 +115,9 @@ public:
static bool
isWriteFinished();

static std::size_t
transmitBufferSize();

static std::size_t
discardTransmitBuffer();

Expand All @@ -122,9 +127,18 @@ public:
static std::size_t
read(uint8_t *buffer, std::size_t length);

static std::size_t
receiveBufferSize();

static std::size_t
discardReceiveBuffer();

static bool
hasError();

static void
clearError();

%% if id in shared_irq_ids
static void
irq();
Expand Down
17 changes: 17 additions & 0 deletions src/modm/platform/uart/stm32/uart_base.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public:
%% set reg = "ISR"
/// Set if match character is received.
CharacterMatch = USART_{{reg}}_CMF,
#ifdef USART_{{reg}}_NE
/// Set if noise was detected on the input.
NoiseError = USART_{{reg}}_NE,
#endif
%% else
%% set reg = "SR"
%% endif
Expand Down Expand Up @@ -128,6 +132,19 @@ public:
Output = USART_CR2_LBCL,
};

enum class
WordLength : uint32_t
{
#ifdef USART_CR1_M1
Bit7 = USART_CR1_M1,
Bit8 = 0,
Bit9 = USART_CR1_M0,
#else
Bit8 = 0,
Bit9 = USART_CR1_M,
#endif
};

enum class
SpiClock : uint32_t
{
Expand Down
12 changes: 7 additions & 5 deletions src/modm/platform/uart/stm32/uart_hal.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@ public:
initialize( Parity parity = Parity::Disabled);
%% endif

static inline void
setWordLength(WordLength length);

/**
* Initialize Uart HAL Peripheral
*
* Enables clocks, the UART peripheral (but neither TX nor RX)
* Sets raw brr, parity and oversampling mode.
*/
* Initialize Uart HAL Peripheral
*
* Enables clocks, the UART peripheral (but neither TX nor RX)
* Sets raw brr, parity and oversampling mode.
*/
static void
initializeWithBrr(uint16_t brr,
%% if target["family"] == "f1"
Expand Down
25 changes: 25 additions & 0 deletions src/modm/platform/uart/stm32/uart_hal_impl.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,31 @@ modm::platform::{{ name }}::setSpiDataMode(SpiDataMode mode)
%% endif
}

void
modm::platform::{{ name }}::setWordLength(WordLength length)
{
%% if "extended" in driver["type"]
const bool usartEnabled = ({{ peripheral }}->CR1 & USART_CR1_UE);
if(usartEnabled) {
disableOperation();
}
%% endif

{{ peripheral }}->CR1 =
#ifdef USART_CR1_M1
({{ peripheral }}->CR1 & ~(USART_CR1_M0 | USART_CR1_M1))
#else
({{ peripheral }}->CR1 & ~USART_CR1_M)
#endif
| static_cast<uint32_t>(length);

%% if "extended" in driver["type"]
if(usartEnabled) {
enableOperation();
}
%% endif
}

void
modm::platform::{{ name }}::setLastBitClockPulse(LastBitClockPulse pulse)
{
Expand Down

0 comments on commit 026f82d

Please sign in to comment.