Move DMA's rx buffer to a separate module
This commit is contained in:
parent
e2adf48f82
commit
d84d9cef83
@ -19,7 +19,7 @@
|
||||
//--local definitions-----------------------------------------------------------
|
||||
|
||||
static void configure_dma(volatile struct DMA* dma, enum DmaChannel channel,
|
||||
enum DmaConfig config_mask, volatile void* periph, void* mem,
|
||||
enum DmaConfig config_mask, volatile void* periph, volatile void* mem,
|
||||
uint16_t size);
|
||||
|
||||
|
||||
@ -36,7 +36,7 @@ static volatile void* dma2_cb_params[5];
|
||||
//--public functions------------------------------------------------------------
|
||||
|
||||
void dma_configure(enum DmaPeriph dma, enum DmaChannel channel,
|
||||
enum DmaConfig config_mask, volatile void* periph, void* mem,
|
||||
enum DmaConfig config_mask, volatile void* periph, volatile void* mem,
|
||||
uint16_t size, DmaCallback callback, volatile void* cb_param)
|
||||
{
|
||||
//reset peripheral first, to ensure proper configuration
|
||||
@ -185,7 +185,7 @@ uint16_t dma_get_remaining(enum DmaPeriph dma, enum DmaChannel channel)
|
||||
//--local functions-------------------------------------------------------------
|
||||
|
||||
static void configure_dma(volatile struct DMA* dma, enum DmaChannel channel,
|
||||
enum DmaConfig config_mask, volatile void* periph, void* mem,
|
||||
enum DmaConfig config_mask, volatile void* periph, volatile void* mem,
|
||||
uint16_t size)
|
||||
{
|
||||
volatile struct DMA_CHANNEL* regs = &dma->CHANNELS[channel];
|
||||
|
||||
@ -81,7 +81,7 @@ typedef void (*DmaCallback)(enum DmaIRQSource, volatile void* param);
|
||||
//--functions-------------------------------------------------------------------
|
||||
|
||||
void dma_configure(enum DmaPeriph dma, enum DmaChannel channel,
|
||||
enum DmaConfig config_mask, volatile void* periph, void* mem,
|
||||
enum DmaConfig config_mask, volatile void* periph, volatile void* mem,
|
||||
uint16_t size, DmaCallback callback, volatile void* cb_param);
|
||||
|
||||
void dma_configure_mem2mem(enum DmaPeriph dma, enum DmaChannel channel,
|
||||
|
||||
85
drivers/dma_cbuf.c
Normal file
85
drivers/dma_cbuf.c
Normal file
@ -0,0 +1,85 @@
|
||||
/** @file dma_cbuf.c
|
||||
* Module handling Direct Memory Access controller's circular system
|
||||
*
|
||||
* The module provides a convenient tool to receive data from a peripheral in a
|
||||
* buffered, low-latency, low-cpu usage, non-blocking way
|
||||
*/
|
||||
|
||||
//--includes--------------------------------------------------------------------
|
||||
|
||||
#include "dma_cbuf.h"
|
||||
|
||||
|
||||
//--local definitions-----------------------------------------------------------
|
||||
|
||||
static void cbuf_callback(enum DmaIRQSource src, volatile void* param);
|
||||
|
||||
|
||||
//--local variables-------------------------------------------------------------
|
||||
|
||||
//--public functions------------------------------------------------------------
|
||||
|
||||
void dma_cbuf_configure(volatile struct DmaCircBuffer* buffer,
|
||||
volatile void* raw_buffer, volatile void* src, uint16_t buffer_size,
|
||||
enum DmaPeriph dma, enum DmaChannel channel, enum DmaConfig config)
|
||||
{
|
||||
#warning "check for null ptr"
|
||||
|
||||
buffer->buffer = raw_buffer;
|
||||
buffer->src = src;
|
||||
|
||||
buffer->size = buffer_size;
|
||||
buffer->begin = 0;
|
||||
|
||||
buffer->dma = dma;
|
||||
buffer->channel = channel;
|
||||
buffer->config = config;
|
||||
|
||||
buffer->dma_looped = false;
|
||||
|
||||
dma_configure(buffer->dma, buffer->channel, buffer->config, buffer->src,
|
||||
buffer->buffer, buffer->size, cbuf_callback, buffer);
|
||||
}
|
||||
|
||||
uint32_t dma_cbuf_read_byte(volatile struct DmaCircBuffer* buffer,
|
||||
uint8_t* byte)
|
||||
{
|
||||
//retreive the current end of the buffer based on the DMA's progress
|
||||
uint16_t end = buffer->size
|
||||
- dma_get_remaining(buffer->dma, buffer->channel);
|
||||
|
||||
//check for bytes to read and overflow
|
||||
if ((end > buffer->begin) && buffer->dma_looped) {
|
||||
//TODO overflow
|
||||
buffer->begin = end;
|
||||
} else if ((buffer->begin == end) && !buffer->dma_looped) {
|
||||
//TODO no data
|
||||
return 1;
|
||||
}
|
||||
|
||||
//read the oldest byte and advance the buffer
|
||||
uint8_t* raw_buffer = (uint8_t*)buffer->buffer;
|
||||
*byte = raw_buffer[buffer->begin];
|
||||
++buffer->begin;
|
||||
if (buffer->begin >= buffer->size) {
|
||||
buffer->begin = 0;
|
||||
buffer->dma_looped = false;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//--local functions-------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Callback called on DMA RX tranfert's completion. Sets a flag needed to
|
||||
* properly handle the circular buffer
|
||||
*/
|
||||
static void cbuf_callback(enum DmaIRQSource src, volatile void* param)
|
||||
{
|
||||
(void)src; //only transfer complete expected
|
||||
volatile struct DmaCircBuffer* buffer = param;
|
||||
buffer->dma_looped = true;
|
||||
}
|
||||
|
||||
|
||||
44
drivers/dma_cbuf.h
Normal file
44
drivers/dma_cbuf.h
Normal file
@ -0,0 +1,44 @@
|
||||
/** @file dma_cbuf.h
|
||||
* Module handling Direct Memory Access controller's circular system
|
||||
*
|
||||
* The module provides a convenient tool to receive data from a peripheral in a
|
||||
* buffered, low-latency, low-cpu usage, non-blocking way
|
||||
*/
|
||||
|
||||
#ifndef _DMA_CBUF_H_
|
||||
#define _DMA_CBUF_H_
|
||||
|
||||
//--includes--------------------------------------------------------------------
|
||||
|
||||
#include "dma.h"
|
||||
|
||||
|
||||
//--type definitions------------------------------------------------------------
|
||||
|
||||
struct DmaCircBuffer {
|
||||
volatile void* buffer; //the buffer to use as a circular buffer
|
||||
volatile void* src; //source peripheral register
|
||||
|
||||
uint16_t size; //the size of the buffer
|
||||
uint16_t begin; //pointer to the current begin of the buffer
|
||||
|
||||
enum DmaPeriph dma; //DMA peripheral, must correspond to peripheral
|
||||
enum DmaChannel channel; //DMA channel, must correspond to peripheral
|
||||
enum DmaConfig config; //DMA config, must correspond to peripheral
|
||||
|
||||
bool dma_looped; //whether the DMA looped or not (buffer overflow)
|
||||
};
|
||||
|
||||
|
||||
//--functions-------------------------------------------------------------------
|
||||
|
||||
void dma_cbuf_configure(volatile struct DmaCircBuffer* buffer,
|
||||
volatile void* raw_buffer, volatile void* src, uint16_t buffer_size,
|
||||
enum DmaPeriph dma, enum DmaChannel channel, enum DmaConfig config);
|
||||
|
||||
uint32_t dma_cbuf_read_byte(volatile struct DmaCircBuffer* buffer,
|
||||
uint8_t* byte);
|
||||
|
||||
|
||||
#endif //_DMA_CBUF_H_
|
||||
|
||||
142
drivers/usart.c
142
drivers/usart.c
@ -15,34 +15,27 @@
|
||||
#include "rcc.h"
|
||||
#include "dma.h"
|
||||
#include "dma_mbuf.h"
|
||||
#include "dma_cbuf.h"
|
||||
|
||||
#include "stddef.h"
|
||||
|
||||
|
||||
//--local definitions-----------------------------------------------------------
|
||||
|
||||
#define DMA_CONFIG (DMA_CONFIG_IRQ_COMPLETE | DMA_CONFIG_FROM_MEM \
|
||||
| DMA_CONFIG_INC_MEM | DMA_CONFIG_PSIZE_8BITS \
|
||||
| DMA_CONFIG_MSIZE_8BITS | DMA_CONFIG_PRIO_LOW)
|
||||
#define DMA_TX_CONFIG (DMA_CONFIG_IRQ_COMPLETE | DMA_CONFIG_FROM_MEM \
|
||||
| DMA_CONFIG_INC_MEM | DMA_CONFIG_PSIZE_8BITS \
|
||||
| DMA_CONFIG_MSIZE_8BITS | DMA_CONFIG_PRIO_LOW)
|
||||
|
||||
#define DMA_RX_CONFIG (DMA_CONFIG_IRQ_COMPLETE | DMA_CONFIG_FROM_PERIPH \
|
||||
| DMA_CONFIG_CIRCULAR | DMA_CONFIG_INC_MEM \
|
||||
| DMA_CONFIG_PSIZE_8BITS | DMA_CONFIG_MSIZE_8BITS \
|
||||
| DMA_CONFIG_PRIO_LOW)
|
||||
|
||||
struct CircularBuffer {
|
||||
volatile uint8_t* buffer; //the buffer to use as a circular buffer
|
||||
uint16_t size; //the size of the buffer
|
||||
uint16_t begin; //pointer to the current begin of the buffer
|
||||
bool dmaLooped; //whether the DMA looped or not (buffer overflow)
|
||||
};
|
||||
|
||||
static void configure_usart(volatile struct USART* regs,
|
||||
enum UsartConfig config);
|
||||
static void configure_baudrate(volatile struct USART* regs, uint32_t clock,
|
||||
uint32_t baudrate);
|
||||
static uint32_t read_from_buffer(volatile struct CircularBuffer* buffer,
|
||||
enum DmaChannel channel, uint8_t* byte);
|
||||
|
||||
static void usart1_rx_callback(enum DmaIRQSource src, volatile void* param);
|
||||
static void usart2_rx_callback(enum DmaIRQSource src, volatile void* param);
|
||||
static void usart3_rx_callback(enum DmaIRQSource src, volatile void* param);
|
||||
|
||||
|
||||
//--local variables-------------------------------------------------------------
|
||||
@ -51,11 +44,11 @@ static volatile struct USART* const usart1 = (struct USART*)USART1_BASE_ADDRESS;
|
||||
static volatile struct USART* const usart2 = (struct USART*)USART2_BASE_ADDRESS;
|
||||
static volatile struct USART* const usart3 = (struct USART*)USART3_BASE_ADDRESS;
|
||||
|
||||
static volatile struct CircularBuffer usart1_rx_buffer;
|
||||
static volatile struct DmaCircBuffer usart1_rx_buffer;
|
||||
static volatile struct DmaMultiBuffer usart1_tx_buffer;
|
||||
static volatile struct CircularBuffer usart2_rx_buffer;
|
||||
static volatile struct DmaCircBuffer usart2_rx_buffer;
|
||||
static volatile struct DmaMultiBuffer usart2_tx_buffer;
|
||||
static volatile struct CircularBuffer usart3_rx_buffer;
|
||||
static volatile struct DmaCircBuffer usart3_rx_buffer;
|
||||
static volatile struct DmaMultiBuffer usart3_tx_buffer;
|
||||
|
||||
|
||||
@ -72,6 +65,7 @@ void usart_configure(enum UsartPeriph periph, enum UsartConfig config,
|
||||
rcc_enable(RCC_AHB_NONE, RCC_APB1_NONE, RCC_APB2_USART);
|
||||
configure_baudrate(usart1, clocks.apb2_freq, baudrate);
|
||||
configure_usart(usart1, config);
|
||||
#warning "fix zeroed variables init"
|
||||
usart1_tx_buffer.buffers = NULL;
|
||||
usart1_rx_buffer.buffer = NULL;
|
||||
break;
|
||||
@ -147,24 +141,20 @@ uint32_t usart_write_byte(enum UsartPeriph periph, uint8_t byte)
|
||||
uint32_t usart_read_byte(enum UsartPeriph periph, uint8_t* byte)
|
||||
{
|
||||
volatile struct USART* regs;
|
||||
volatile struct CircularBuffer* buffer;
|
||||
enum DmaChannel dma_channel;
|
||||
volatile struct DmaCircBuffer* buffer;
|
||||
|
||||
switch (periph) {
|
||||
case USART_PERIPH_1:
|
||||
regs = usart1;
|
||||
buffer = &usart1_rx_buffer;
|
||||
dma_channel = DMA_CHANNEL_5;
|
||||
break;
|
||||
case USART_PERIPH_2:
|
||||
regs = usart2;
|
||||
buffer = &usart2_rx_buffer;
|
||||
dma_channel = DMA_CHANNEL_6;
|
||||
break;
|
||||
case USART_PERIPH_3:
|
||||
regs = usart3;
|
||||
buffer = &usart3_rx_buffer;
|
||||
dma_channel = DMA_CHANNEL_3;
|
||||
break;
|
||||
default:
|
||||
return 1;
|
||||
@ -172,7 +162,7 @@ uint32_t usart_read_byte(enum UsartPeriph periph, uint8_t* byte)
|
||||
}
|
||||
|
||||
if (buffer->buffer) {
|
||||
return read_from_buffer(buffer, dma_channel, byte);
|
||||
return dma_cbuf_read_byte(buffer, byte);
|
||||
} else {
|
||||
if (regs->SR.RXNE) {
|
||||
*byte = regs->DR.DR;
|
||||
@ -190,17 +180,17 @@ void usart_set_tx_buffer(enum UsartPeriph periph, uint8_t** buffers,
|
||||
case USART_PERIPH_1:
|
||||
dma_mbuf_configure(&usart1_tx_buffer, (void**)buffers, &usart1->DR,
|
||||
buffer_size, buffer_nb, DMA_PERIPH_1, DMA_CHANNEL_4,
|
||||
DMA_CONFIG);
|
||||
DMA_TX_CONFIG);
|
||||
break;
|
||||
case USART_PERIPH_2:
|
||||
dma_mbuf_configure(&usart2_tx_buffer, (void**)buffers, &usart2->DR,
|
||||
buffer_size, buffer_nb, DMA_PERIPH_1, DMA_CHANNEL_7,
|
||||
DMA_CONFIG);
|
||||
DMA_TX_CONFIG);
|
||||
break;
|
||||
case USART_PERIPH_3:
|
||||
dma_mbuf_configure(&usart3_tx_buffer, (void**)buffers, &usart3->DR,
|
||||
buffer_size, buffer_nb, DMA_PERIPH_1, DMA_CHANNEL_2,
|
||||
DMA_CONFIG);
|
||||
DMA_TX_CONFIG);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -210,43 +200,19 @@ void usart_set_rx_buffer(enum UsartPeriph periph, uint8_t* buffer,
|
||||
{
|
||||
switch (periph) {
|
||||
case USART_PERIPH_1:
|
||||
dma_configure(DMA_PERIPH_1, DMA_CHANNEL_5,
|
||||
DMA_CONFIG_IRQ_COMPLETE | DMA_CONFIG_FROM_PERIPH
|
||||
| DMA_CONFIG_CIRCULAR | DMA_CONFIG_INC_MEM
|
||||
| DMA_CONFIG_PSIZE_8BITS | DMA_CONFIG_MSIZE_8BITS
|
||||
| DMA_CONFIG_PRIO_LOW, (void*)&usart1->DR, buffer,
|
||||
size, usart1_rx_callback, NULL);
|
||||
usart1_rx_buffer.buffer = buffer;
|
||||
usart1_rx_buffer.size = size;
|
||||
usart1_rx_buffer.begin = 0;
|
||||
usart1_rx_buffer.dmaLooped = false;
|
||||
dma_cbuf_configure(&usart1_rx_buffer, buffer, (void*)&usart1->DR,
|
||||
size, DMA_PERIPH_1, DMA_CHANNEL_5, DMA_RX_CONFIG);
|
||||
reg_set(usart1->CR3, USART_CR3_DMAR);
|
||||
break;
|
||||
case USART_PERIPH_2:
|
||||
dma_configure(DMA_PERIPH_1, DMA_CHANNEL_6,
|
||||
DMA_CONFIG_IRQ_COMPLETE | DMA_CONFIG_FROM_PERIPH
|
||||
| DMA_CONFIG_CIRCULAR | DMA_CONFIG_INC_MEM
|
||||
| DMA_CONFIG_PSIZE_8BITS | DMA_CONFIG_MSIZE_8BITS
|
||||
| DMA_CONFIG_PRIO_LOW, (void*)&usart2->DR, buffer,
|
||||
size, usart2_rx_callback, NULL);
|
||||
usart2_rx_buffer.buffer = buffer;
|
||||
usart2_rx_buffer.size = size;
|
||||
usart2_rx_buffer.begin = 0;
|
||||
usart2_rx_buffer.dmaLooped = false;
|
||||
dma_cbuf_configure(&usart2_rx_buffer, buffer, (void*)&usart2->DR,
|
||||
size, DMA_PERIPH_1, DMA_CHANNEL_6, DMA_RX_CONFIG);
|
||||
reg_set(usart2->CR3, USART_CR3_DMAR);
|
||||
break;
|
||||
|
||||
case USART_PERIPH_3:
|
||||
dma_configure(DMA_PERIPH_1, DMA_CHANNEL_3,
|
||||
DMA_CONFIG_IRQ_COMPLETE | DMA_CONFIG_FROM_PERIPH
|
||||
| DMA_CONFIG_CIRCULAR | DMA_CONFIG_INC_MEM
|
||||
| DMA_CONFIG_PSIZE_8BITS | DMA_CONFIG_MSIZE_8BITS
|
||||
| DMA_CONFIG_PRIO_LOW, (void*)&usart3->DR, buffer,
|
||||
size, usart3_rx_callback, NULL);
|
||||
usart3_rx_buffer.buffer = buffer;
|
||||
usart3_rx_buffer.size = size;
|
||||
usart3_rx_buffer.begin = 0;
|
||||
usart3_rx_buffer.dmaLooped = false;
|
||||
dma_cbuf_configure(&usart3_rx_buffer, buffer, (void*)&usart3->DR,
|
||||
size, DMA_PERIPH_1, DMA_CHANNEL_3, DMA_RX_CONFIG);
|
||||
reg_set(usart3->CR3, USART_CR3_DMAR);
|
||||
|
||||
break;
|
||||
@ -354,68 +320,6 @@ static void configure_baudrate(volatile struct USART* regs, uint32_t clock,
|
||||
reg_write(regs->BRR, USART_BRR_DIV_Fraction, divider & 0xF);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the oldest byte from the given CircularBuffer if any. Returns 0 if the
|
||||
* read was successfull, 1 otherwise
|
||||
*/
|
||||
static uint32_t read_from_buffer(volatile struct CircularBuffer* buffer,
|
||||
enum DmaChannel channel, uint8_t* byte)
|
||||
{
|
||||
//retreive the current end of the buffer based on the DMA's progress
|
||||
uint16_t end = buffer->size - dma_get_remaining(DMA_PERIPH_1, channel);
|
||||
|
||||
//check for bytes to read and overflow
|
||||
if ((end > buffer->begin) && buffer->dmaLooped) {
|
||||
//TODO overflow
|
||||
buffer->begin = end;
|
||||
} else if ((buffer->begin == end) && !buffer->dmaLooped) {
|
||||
//TODO no data
|
||||
return 1;
|
||||
}
|
||||
|
||||
//read the oldest byte and advance the buffer
|
||||
*byte = buffer->buffer[buffer->begin];
|
||||
++buffer->begin;
|
||||
if (buffer->begin >= buffer->size) {
|
||||
buffer->begin = 0;
|
||||
buffer->dmaLooped = false;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//--allbacks-------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Callback called on DMA RX tranfert's completion. Sets a flag needed to
|
||||
* properly handle the circular buffer
|
||||
*/
|
||||
static void usart1_rx_callback(enum DmaIRQSource src, volatile void* param)
|
||||
{
|
||||
(void)src; //only transfer complete expected
|
||||
usart1_rx_buffer.dmaLooped = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback called on DMA RX tranfert's completion. Sets a flag needed to
|
||||
* properly handle the circular buffer
|
||||
*/
|
||||
static void usart2_rx_callback(enum DmaIRQSource src, volatile void* param)
|
||||
{
|
||||
(void)src; //only transfer complete expected
|
||||
usart2_rx_buffer.dmaLooped = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback called on DMA RX tranfert's completion. Sets a flag needed to
|
||||
* properly handle the circular buffer
|
||||
*/
|
||||
static void usart3_rx_callback(enum DmaIRQSource src, volatile void* param)
|
||||
{
|
||||
(void)src; //only transfer complete expected
|
||||
usart3_rx_buffer.dmaLooped = true;
|
||||
}
|
||||
|
||||
|
||||
//--ISRs------------------------------------------------------------------------
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user