This includes minor updates for the th different MCU variant, and bugfixes. Resolves #7
88 lines
2.5 KiB
C++
88 lines
2.5 KiB
C++
/*
|
|
* Copyright (C) 2019 Max Regan
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
* THE SOFTWARE.
|
|
*/
|
|
|
|
#include "Bsp/Drivers/UsartDriver.h"
|
|
#include "Bsp/macros.h"
|
|
|
|
namespace BSP {
|
|
|
|
using RC = BSP::ReturnCode;
|
|
using BSP::Schedule::TaskScheduler;
|
|
using BSP::Schedule::NextTime;
|
|
using BSP::Time;
|
|
|
|
UsartDriver::UsartDriver(USART_TypeDef *usart, TaskScheduler &scheduler)
|
|
: m_scheduler(scheduler)
|
|
, m_usart(usart)
|
|
{}
|
|
|
|
void UsartDriver::init()
|
|
{
|
|
#if defined(STM32L0XX)
|
|
if (m_usart == USART2) {
|
|
RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
|
|
}
|
|
#elif defined(STM32L4XX)
|
|
if (m_usart == USART1) {
|
|
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
|
|
} else if (m_usart == USART2) {
|
|
RCC->APB1ENR1 |= RCC_APB1ENR1_USART2EN;
|
|
} else if (m_usart == USART3) {
|
|
RCC->APB1ENR1 |= RCC_APB1ENR1_USART3EN;
|
|
}
|
|
#else
|
|
#error "Unknown device family"
|
|
#endif
|
|
|
|
// TODO: Don't hardcode the main clock value here
|
|
m_usart->BRR = (4100000) / 115200L; // set baudrate (APBCLK / baud)
|
|
m_usart->CR1 |= (USART_CR1_TE); // TX enable
|
|
m_usart->CR1 |= USART_CR1_UE; // USART enable
|
|
}
|
|
|
|
NextTime UsartDriver::execute()
|
|
{
|
|
return NextTime::never();
|
|
}
|
|
|
|
RC UsartDriver::tx_blocking(const uint8_t *data, size_t len)
|
|
{
|
|
for (size_t i = 0; i < len; i++) {
|
|
while (!(m_usart->ISR & USART_ISR_TXE)) {}
|
|
m_usart->TDR = data[i];
|
|
}
|
|
|
|
return RC::OK;
|
|
}
|
|
|
|
RC UsartDriver::tx_blocking(const char *data)
|
|
{
|
|
size_t len = 0;
|
|
while (data[len] != '\0') {
|
|
len++;
|
|
};
|
|
|
|
return tx_blocking(reinterpret_cast<const uint8_t *>(data), len);
|
|
}
|
|
|
|
|
|
}
|