Clan up the structure
This commit is contained in:
parent
340f0329fa
commit
45141798d8
42 changed files with 134 additions and 452 deletions
65
components/f1ll/src/console_handler.cpp
Normal file
65
components/f1ll/src/console_handler.cpp
Normal file
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
* ll_consolehandler.cpp
|
||||
*
|
||||
* Created on: Nov 7, 2019
|
||||
* Author: abody
|
||||
*/
|
||||
|
||||
#include <f1ll/console_handler.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
namespace f1ll {
|
||||
|
||||
console_handler::console_handler(USART_TypeDef *usart, DMA_TypeDef *dma, uint32_t channelRx, uint32_t channelTx)
|
||||
: usart_core(usart, dma, channelRx, channelTx)
|
||||
{
|
||||
}
|
||||
|
||||
void console_handler::receiver_idle(void) {}
|
||||
void console_handler::transmission_complete(void)
|
||||
{
|
||||
m_busy = false;
|
||||
m_used = 0;
|
||||
}
|
||||
void console_handler::framing_error(void) {}
|
||||
void console_handler::overrun(void) {}
|
||||
void console_handler::rx_dma_transfer_complete(void) {}
|
||||
void console_handler::rx_dma_half_transfer(void) {}
|
||||
void console_handler::rx_dma_error(void) {}
|
||||
void console_handler::tx_dma_transfer_complete(void)
|
||||
{
|
||||
LL_USART_EnableIT_TC(m_usart);
|
||||
LL_DMA_DisableChannel(m_txDma.get_dma(), m_txDma.get_channel());
|
||||
}
|
||||
void console_handler::tx_dma_half_transfer(void) {}
|
||||
void console_handler::tx_dma_error(void) {}
|
||||
|
||||
void console_handler::append(char const *s)
|
||||
{
|
||||
while (m_busy)
|
||||
;
|
||||
size_t len = strlen(s);
|
||||
size_t max_len = sizeof(m_buffer) - m_used;
|
||||
strncpy(m_buffer + m_used, s, max_len);
|
||||
m_used += len < max_len ? len : max_len;
|
||||
}
|
||||
|
||||
void console_handler::flush()
|
||||
{
|
||||
while (m_busy)
|
||||
;
|
||||
|
||||
if (m_used) {
|
||||
m_busy = true;
|
||||
setup_transmit(m_buffer, m_used);
|
||||
}
|
||||
}
|
||||
|
||||
void console_handler::print(char const *s)
|
||||
{
|
||||
append(s);
|
||||
flush();
|
||||
}
|
||||
|
||||
} // namespace f1ll
|
38
components/f1ll/src/dma_helper.cpp
Normal file
38
components/f1ll/src/dma_helper.cpp
Normal file
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
q * ll_dmahelper.cpp
|
||||
*
|
||||
* Created on: Oct 25, 2019
|
||||
* Author: abody
|
||||
*/
|
||||
|
||||
#include <f1ll/dma_helper.h>
|
||||
|
||||
namespace f1ll {
|
||||
|
||||
const uint32_t dma_helper::m_te_masks[7] = {
|
||||
DMA_ISR_TEIF1, DMA_ISR_TEIF2, DMA_ISR_TEIF3, DMA_ISR_TEIF4,
|
||||
DMA_ISR_TEIF5, DMA_ISR_TEIF6, DMA_ISR_TEIF7};
|
||||
const uint32_t dma_helper::m_ht_masks[7] = {
|
||||
DMA_ISR_HTIF1, DMA_ISR_HTIF2, DMA_ISR_HTIF3, DMA_ISR_HTIF4,
|
||||
DMA_ISR_HTIF5, DMA_ISR_HTIF6, DMA_ISR_HTIF7};
|
||||
const uint32_t dma_helper::m_tc_masks[7] = {
|
||||
DMA_ISR_TCIF1, DMA_ISR_TCIF2, DMA_ISR_TCIF3, DMA_ISR_TCIF4,
|
||||
DMA_ISR_TCIF5, DMA_ISR_TCIF6, DMA_ISR_TCIF7};
|
||||
const uint32_t dma_helper::m_gi_masks[7] = {
|
||||
DMA_ISR_GIF1, DMA_ISR_GIF2, DMA_ISR_GIF3, DMA_ISR_GIF4,
|
||||
DMA_ISR_GIF5, DMA_ISR_GIF6, DMA_ISR_GIF7};
|
||||
|
||||
dma_helper::dma_helper(DMA_TypeDef *dma, uint32_t channel)
|
||||
: m_dma(dma), m_channel(channel)
|
||||
#ifdef DMA2
|
||||
,
|
||||
m_is_reg(dma == DMA1 ? &DMA1->ISR : &DMA2->ISR),
|
||||
m_ifc_reg(dma == DMA1 ? &DMA1->IFCR : &DMA2->IFCR)
|
||||
#else
|
||||
,
|
||||
m_is_reg(&DMA1->ISR), m_ifc_reg(&DMA1->IFCR)
|
||||
#endif
|
||||
{
|
||||
}
|
||||
|
||||
} // namespace f1ll
|
98
components/f1ll/src/str_util.cpp
Normal file
98
components/f1ll/src/str_util.cpp
Normal file
|
@ -0,0 +1,98 @@
|
|||
#include <f1ll/str_util.h>
|
||||
#include <stdint.h>
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
size_t strcpy_ex(char *dst, char const *src) {
|
||||
size_t ret = 0;
|
||||
do {
|
||||
*dst++ = *src;
|
||||
++ret;
|
||||
} while (*src++);
|
||||
return ret - 1;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
void strrev(char *first, char *last) {
|
||||
char tmp;
|
||||
while (last > first) {
|
||||
tmp = *first;
|
||||
*first++ = *last;
|
||||
*last-- = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
char tochr(const uint8_t in, const uint8_t upper) {
|
||||
return in + ((in < 10) ? '0' : (upper ? 'A' : 'a') - 10);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
size_t uitodec(char *buffer, uint32_t data) {
|
||||
char *b2 = buffer;
|
||||
if (!data) {
|
||||
*b2++ = '0';
|
||||
*b2 = '\0';
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (data) {
|
||||
*b2++ = (data % 10) + '0';
|
||||
data /= 10;
|
||||
}
|
||||
size_t ret = b2 - buffer;
|
||||
|
||||
*b2-- = 0;
|
||||
|
||||
strrev(buffer, b2);
|
||||
return ret;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
size_t uitohex(char *buffer, uint32_t data, uint8_t chars) {
|
||||
char *b2 = buffer;
|
||||
size_t ret = 0;
|
||||
|
||||
if (chars == 0xff || !chars) {
|
||||
if (!data) {
|
||||
*b2++ = '0';
|
||||
*b2 = '\0';
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (data) {
|
||||
uint8_t curval = data & 0x0f;
|
||||
*b2++ = tochr(curval, 1);
|
||||
data >>= 4;
|
||||
}
|
||||
ret = b2 - buffer;
|
||||
|
||||
} else {
|
||||
ret = chars;
|
||||
for (uint8_t pos = 0; pos < (uint8_t)ret; ++pos) {
|
||||
*b2++ = tochr(data & 0x0f, 1);
|
||||
data >>= 4;
|
||||
}
|
||||
}
|
||||
*b2-- = 0;
|
||||
strrev(buffer, b2);
|
||||
return ret;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
size_t itodec(char *buffer, int data) {
|
||||
if (data < 0) {
|
||||
*buffer++ = '-';
|
||||
return uitodec(buffer, -data) + 1;
|
||||
}
|
||||
|
||||
return uitodec(buffer, data);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
size_t itohex(char *buffer, int data) {
|
||||
if (data < 0) {
|
||||
*buffer++ = '-';
|
||||
return uitohex(buffer, -data, 0) + 1;
|
||||
}
|
||||
return uitohex(buffer, data, 0);
|
||||
}
|
119
components/f1ll/src/usart_core.cpp
Normal file
119
components/f1ll/src/usart_core.cpp
Normal file
|
@ -0,0 +1,119 @@
|
|||
/*
|
||||
* ll_dmadrivenusartcore.cpp
|
||||
*
|
||||
* Created on: Nov 4, 2019
|
||||
* Author: abody
|
||||
*/
|
||||
|
||||
#include <f1ll/usart_core.h>
|
||||
|
||||
namespace f1ll {
|
||||
|
||||
usart_core::usart_core(USART_TypeDef *usart, DMA_TypeDef *dma,
|
||||
uint32_t channelRx, uint32_t channelTx)
|
||||
: m_usart(usart), m_rxDma(dma, channelRx), m_txDma(dma, channelTx) {
|
||||
uint32_t status = usart->SR;
|
||||
volatile uint32_t tmpreg =
|
||||
usart->DR; // clearing some of the error/status bits in the USART
|
||||
(void)tmpreg;
|
||||
(void)status;
|
||||
|
||||
*m_txDma.get_ifc_reg() = m_txDma.get_gi_mask();
|
||||
*m_rxDma.get_ifc_reg() = m_rxDma.get_gi_mask();
|
||||
|
||||
LL_DMA_EnableIT_HT(dma, channelRx);
|
||||
LL_DMA_EnableIT_TC(dma, channelRx);
|
||||
LL_DMA_EnableIT_TE(dma, channelRx);
|
||||
LL_DMA_EnableIT_HT(dma, channelTx);
|
||||
LL_DMA_EnableIT_TC(dma, channelTx);
|
||||
LL_DMA_EnableIT_TE(dma, channelTx);
|
||||
}
|
||||
|
||||
void usart_core::usart_isr() {
|
||||
uint32_t status = m_usart->SR;
|
||||
volatile uint32_t tmpreg =
|
||||
m_usart->DR; // clearing some of the error/status bits in the HW
|
||||
(void)tmpreg;
|
||||
|
||||
if (LL_USART_IsEnabledIT_TC(m_usart) &&
|
||||
LL_USART_IsActiveFlag_TC(m_usart)) { // transmission complete
|
||||
LL_USART_DisableIT_TC(m_usart);
|
||||
transmission_complete();
|
||||
}
|
||||
if (LL_USART_IsEnabledIT_IDLE(m_usart) && (status & USART_SR_IDLE)) {
|
||||
receiver_idle();
|
||||
}
|
||||
if (LL_USART_IsEnabledIT_ERROR(m_usart)) {
|
||||
if (status & USART_SR_FE) {
|
||||
framing_error();
|
||||
}
|
||||
if (status & USART_SR_ORE) {
|
||||
overrun();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void usart_core::rx_dma_isr() {
|
||||
if (*m_rxDma.get_is_reg() & m_rxDma.get_tc_mask()) {
|
||||
*m_rxDma.get_ifc_reg() = m_rxDma.get_tc_mask();
|
||||
if (m_rxDma.is_enabled_it_tc()) {
|
||||
rx_dma_transfer_complete();
|
||||
}
|
||||
}
|
||||
if (*m_rxDma.get_is_reg() & m_rxDma.get_ht_mask()) {
|
||||
*m_rxDma.get_ifc_reg() = m_rxDma.get_ht_mask();
|
||||
if (m_rxDma.is_enabled_it_ht()) {
|
||||
rx_dma_half_transfer();
|
||||
}
|
||||
}
|
||||
if (*m_rxDma.get_is_reg() & m_rxDma.get_te_mask()) {
|
||||
*m_rxDma.get_ifc_reg() = m_rxDma.get_te_mask();
|
||||
if (m_rxDma.is_enabled_it_te()) {
|
||||
rx_dma_error();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void usart_core::tx_dma_isr() {
|
||||
if (*m_txDma.get_is_reg() & m_txDma.get_tc_mask()) { // DMA transfer complete
|
||||
*m_txDma.get_ifc_reg() = m_txDma.get_tc_mask();
|
||||
if (m_txDma.is_enabled_it_tc()) {
|
||||
tx_dma_transfer_complete();
|
||||
}
|
||||
}
|
||||
if (*m_txDma.get_is_reg() & m_txDma.get_ht_mask()) {
|
||||
*m_txDma.get_ifc_reg() = m_txDma.get_ht_mask();
|
||||
if (m_txDma.is_enabled_it_ht()) {
|
||||
tx_dma_half_transfer();
|
||||
}
|
||||
}
|
||||
if (*m_txDma.get_is_reg() & m_txDma.get_te_mask()) {
|
||||
*m_txDma.get_ifc_reg() = m_txDma.get_te_mask();
|
||||
if (m_txDma.is_enabled_it_te()) {
|
||||
tx_dma_error();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void usart_core::setup_transmit(void const *buffer, uint16_t length) {
|
||||
LL_DMA_ConfigAddresses(m_txDma.get_dma(), m_txDma.get_channel(),
|
||||
reinterpret_cast<uint32_t>(buffer),
|
||||
LL_USART_DMA_GetRegAddr(m_usart),
|
||||
LL_DMA_DIRECTION_MEMORY_TO_PERIPH);
|
||||
LL_DMA_SetDataLength(m_txDma.get_dma(), m_txDma.get_channel(), length);
|
||||
LL_USART_EnableDMAReq_TX(m_usart);
|
||||
LL_DMA_EnableChannel(m_txDma.get_dma(), m_txDma.get_channel());
|
||||
}
|
||||
|
||||
void usart_core::setup_receive(void *buffer, uint16_t length) {
|
||||
LL_DMA_ConfigAddresses(m_rxDma.get_dma(), m_rxDma.get_channel(),
|
||||
LL_USART_DMA_GetRegAddr(m_usart),
|
||||
reinterpret_cast<uint32_t>(buffer),
|
||||
LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
|
||||
LL_DMA_SetDataLength(m_rxDma.get_dma(), m_rxDma.get_channel(), length);
|
||||
LL_USART_EnableDMAReq_RX(m_usart);
|
||||
LL_USART_ClearFlag_ORE(m_usart);
|
||||
LL_DMA_EnableChannel(m_rxDma.get_dma(), m_rxDma.get_channel());
|
||||
}
|
||||
|
||||
} // namespace f1ll
|
Loading…
Add table
Add a link
Reference in a new issue