Compare commits
No commits in common. "e1097048a69b77b5ce12c6d73e25df9aa2496d90" and "256e3f30ab581a0ee1684244b0dbc4af77270dcf" have entirely different histories.
e1097048a6
...
256e3f30ab
@ -19,7 +19,7 @@
|
||||
//--local definitions-----------------------------------------------------------
|
||||
|
||||
static void configure_dma(volatile struct DMA* dma, enum DmaChannel channel,
|
||||
enum DmaConfig config_mask, volatile void* periph, volatile void* mem,
|
||||
enum DmaConfig config_mask, volatile void* periph, 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, volatile void* mem,
|
||||
enum DmaConfig config_mask, volatile void* periph, 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, volatile void* mem,
|
||||
enum DmaConfig config_mask, volatile void* periph, 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, volatile void* mem,
|
||||
enum DmaConfig config_mask, volatile void* periph, void* mem,
|
||||
uint16_t size, DmaCallback callback, volatile void* cb_param);
|
||||
|
||||
void dma_configure_mem2mem(enum DmaPeriph dma, enum DmaChannel channel,
|
||||
@ -1,8 +1,8 @@
|
||||
/** @file dma_mbuf.h
|
||||
* Module handling Direct Memory Access controller's multibuffer system
|
||||
/** @file dma_mbuf.c
|
||||
* Module handling Direct Memory Access controller's TX functions
|
||||
*
|
||||
* The module provides a convenient tool to send data to a peripheral in a
|
||||
* buffered, low-altency, low-cpu usage, non-blocking way
|
||||
* The module provides convenient tools to send data to a peripheral or memory
|
||||
* area in a buffered, non-blocking way
|
||||
*/
|
||||
|
||||
//--includes--------------------------------------------------------------------
|
||||
@ -12,18 +12,8 @@
|
||||
|
||||
//--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)
|
||||
|
||||
static void mbuf_callback(enum DmaIRQSource src, volatile void* param);
|
||||
|
||||
//take last 2 bytes of a buffer and assemble them store the current byte index
|
||||
#define byte_index (((uint16_t**)buffer->buffers) \
|
||||
[buffer->buffer_index][buffer->buffer_size/2])
|
||||
#define dma_byte_index (((uint16_t**)buffer->buffers) \
|
||||
[buffer->dma_buffer_index][buffer->buffer_size/2])
|
||||
|
||||
|
||||
//--local variables-------------------------------------------------------------
|
||||
|
||||
@ -31,14 +21,15 @@ static void mbuf_callback(enum DmaIRQSource src, volatile void* param);
|
||||
|
||||
void dma_mbuf_configure(volatile struct DmaMultiBuffer* buffer, void** buffers,
|
||||
volatile void* dest, uint16_t buffer_size, uint8_t buffer_nb,
|
||||
enum DmaPeriph dma, enum DmaChannel channel, enum DmaConfig priority)
|
||||
enum DmaPeriph dma, enum DmaChannel channel, enum DmaConfig config)
|
||||
{
|
||||
#warning "check for null ptr"
|
||||
|
||||
buffer->buffers = buffers;
|
||||
buffer->dest = dest;
|
||||
|
||||
buffer->buffer_size = buffer_size - 2;
|
||||
buffer->buffer_size = buffer_size;
|
||||
buffer->byte_index = 0;
|
||||
|
||||
buffer->buffer_nb = buffer_nb;
|
||||
buffer->free_buffer_nb = buffer_nb - 1;
|
||||
@ -47,13 +38,7 @@ void dma_mbuf_configure(volatile struct DmaMultiBuffer* buffer, void** buffers,
|
||||
|
||||
buffer->dma = dma;
|
||||
buffer->channel = channel;
|
||||
buffer->config = DMA_CONFIG | priority;
|
||||
|
||||
for (uint8_t i=0; i<buffer->buffer_nb; ++i) {
|
||||
for (uint16_t j=0; j<buffer->buffer_size + 2; ++j) {
|
||||
((uint8_t**)buffer->buffers)[i][j] = 0;
|
||||
}
|
||||
}
|
||||
buffer->config = config;
|
||||
}
|
||||
|
||||
uint32_t dma_mbuf_write_byte(volatile struct DmaMultiBuffer* buffer,
|
||||
@ -62,8 +47,7 @@ uint32_t dma_mbuf_write_byte(volatile struct DmaMultiBuffer* buffer,
|
||||
dma_enter_critical(buffer->dma, buffer->channel);
|
||||
|
||||
//if the current buffer is full, we need to switch it with an empty one
|
||||
volatile uint16_t index = byte_index;
|
||||
if (byte_index >= buffer->buffer_size) {
|
||||
if (buffer->byte_index >= buffer->buffer_size) {
|
||||
|
||||
//if all buffer full, give up
|
||||
if (buffer->free_buffer_nb == 0) {
|
||||
@ -77,14 +61,13 @@ uint32_t dma_mbuf_write_byte(volatile struct DmaMultiBuffer* buffer,
|
||||
}
|
||||
--buffer->free_buffer_nb;
|
||||
|
||||
byte_index = 0;
|
||||
buffer->byte_index = 0;
|
||||
}
|
||||
|
||||
//write the byte
|
||||
uint8_t** buffers = (uint8_t**)buffer->buffers;
|
||||
buffers[buffer->buffer_index][byte_index] = byte;
|
||||
++byte_index;
|
||||
++index;
|
||||
buffers[buffer->buffer_index][buffer->byte_index] = byte;
|
||||
++buffer->byte_index;
|
||||
|
||||
dma_exit_critical(buffer->dma, buffer->channel);
|
||||
return 0;
|
||||
@ -94,14 +77,14 @@ void dma_mbuf_refresh(volatile struct DmaMultiBuffer* buffer)
|
||||
{
|
||||
//no more data to send, stop here
|
||||
if (buffer->dma_buffer_index == buffer->buffer_index
|
||||
&& byte_index == 0) {
|
||||
&& buffer->byte_index == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
//else start a new transfer
|
||||
dma_configure(buffer->dma, buffer->channel, buffer->config, buffer->dest,
|
||||
buffer->buffers[buffer->dma_buffer_index],
|
||||
dma_byte_index, mbuf_callback, buffer);
|
||||
buffer->byte_index, mbuf_callback, buffer);
|
||||
|
||||
//if the newly transfering buffer was being written to, switch the current
|
||||
//buffer. Since we just ended a transfer, the next buffer should be empty
|
||||
@ -111,9 +94,7 @@ void dma_mbuf_refresh(volatile struct DmaMultiBuffer* buffer)
|
||||
if (buffer->buffer_index >= buffer->buffer_nb) {
|
||||
buffer->buffer_index = 0;
|
||||
}
|
||||
--buffer->free_buffer_nb;
|
||||
|
||||
byte_index = 0;
|
||||
buffer->byte_index = 0;
|
||||
}
|
||||
|
||||
return;
|
||||
49
drivers/dma_mbuf.h
Normal file
49
drivers/dma_mbuf.h
Normal file
@ -0,0 +1,49 @@
|
||||
/** @file dma_mbuf.h
|
||||
* Module handling Direct Memory Access controller's TX functions
|
||||
*
|
||||
* The module provides convenient tools to send data to a peripheral in a
|
||||
* buffered, non-blocking way
|
||||
*/
|
||||
|
||||
#ifndef _DMA_MBUF_H_
|
||||
#define _DMA_MBUF_H_
|
||||
|
||||
//--includes--------------------------------------------------------------------
|
||||
|
||||
#include "dma.h"
|
||||
|
||||
|
||||
//--type definitions------------------------------------------------------------
|
||||
|
||||
struct DmaMultiBuffer {
|
||||
void** buffers; //list of buffers to write to
|
||||
volatile void* dest; //destination peripheral register
|
||||
|
||||
uint16_t buffer_size; //size of a single buffer
|
||||
uint16_t byte_index; //index of the current byte in the current buffer
|
||||
|
||||
uint8_t buffer_nb; //total number of buffers
|
||||
uint8_t free_buffer_nb; //number of buffers not currently used
|
||||
uint8_t buffer_index; //index of the current buffer
|
||||
uint8_t dma_buffer_index; //index of the DMA's current 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
|
||||
};
|
||||
|
||||
|
||||
//--functions-------------------------------------------------------------------
|
||||
|
||||
void dma_mbuf_configure(volatile struct DmaMultiBuffer* buffer, void** buffers,
|
||||
volatile void* dest, uint16_t buffer_size, uint8_t buffer_nb,
|
||||
enum DmaPeriph dma, enum DmaChannel channel, enum DmaConfig config);
|
||||
|
||||
uint32_t dma_mbuf_write_byte(volatile struct DmaMultiBuffer* buffer,
|
||||
uint8_t byte);
|
||||
|
||||
void dma_mbuf_refresh(volatile struct DmaMultiBuffer* buffer);
|
||||
|
||||
|
||||
#endif //_DMA_MBUF_H_
|
||||
|
||||
@ -15,17 +15,34 @@
|
||||
#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)
|
||||
|
||||
|
||||
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-------------------------------------------------------------
|
||||
@ -34,11 +51,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 DmaCircBuffer usart1_rx_buffer;
|
||||
static volatile struct CircularBuffer usart1_rx_buffer;
|
||||
static volatile struct DmaMultiBuffer usart1_tx_buffer;
|
||||
static volatile struct DmaCircBuffer usart2_rx_buffer;
|
||||
static volatile struct CircularBuffer usart2_rx_buffer;
|
||||
static volatile struct DmaMultiBuffer usart2_tx_buffer;
|
||||
static volatile struct DmaCircBuffer usart3_rx_buffer;
|
||||
static volatile struct CircularBuffer usart3_rx_buffer;
|
||||
static volatile struct DmaMultiBuffer usart3_tx_buffer;
|
||||
|
||||
|
||||
@ -106,7 +123,7 @@ uint32_t usart_write_byte(enum UsartPeriph periph, uint8_t byte)
|
||||
|
||||
if (buffer->buffers) {
|
||||
//if the tx register is empty, there is no need to go through the dma
|
||||
if (regs->SR.TC) {
|
||||
if (regs->SR.TXE) {
|
||||
reg_write(regs->DR, USART_DR_DR, byte);
|
||||
//enable IRQ, disable DMA
|
||||
reg_reset(regs->CR3, USART_CR3_DMAT);
|
||||
@ -130,20 +147,24 @@ 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 DmaCircBuffer* buffer;
|
||||
volatile struct CircularBuffer* buffer;
|
||||
enum DmaChannel dma_channel;
|
||||
|
||||
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;
|
||||
@ -151,7 +172,7 @@ uint32_t usart_read_byte(enum UsartPeriph periph, uint8_t* byte)
|
||||
}
|
||||
|
||||
if (buffer->buffer) {
|
||||
return dma_cbuf_read_byte(buffer, byte);
|
||||
return read_from_buffer(buffer, dma_channel, byte);
|
||||
} else {
|
||||
if (regs->SR.RXNE) {
|
||||
*byte = regs->DR.DR;
|
||||
@ -163,45 +184,69 @@ uint32_t usart_read_byte(enum UsartPeriph periph, uint8_t* byte)
|
||||
}
|
||||
|
||||
void usart_set_tx_buffer(enum UsartPeriph periph, uint8_t** buffers,
|
||||
uint16_t buffer_size, uint8_t buffer_nb, enum DmaConfig priority)
|
||||
uint16_t buffer_size, uint8_t buffer_nb)
|
||||
{
|
||||
switch (periph) {
|
||||
case USART_PERIPH_1:
|
||||
dma_mbuf_configure(&usart1_tx_buffer, (void**)buffers, &usart1->DR,
|
||||
buffer_size, buffer_nb, DMA_PERIPH_1, DMA_CHANNEL_4,
|
||||
priority);
|
||||
DMA_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,
|
||||
priority);
|
||||
DMA_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,
|
||||
priority);
|
||||
DMA_CONFIG);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void usart_set_rx_buffer(enum UsartPeriph periph, uint8_t* buffer,
|
||||
uint16_t size, enum DmaConfig priority)
|
||||
uint16_t size)
|
||||
{
|
||||
switch (periph) {
|
||||
case USART_PERIPH_1:
|
||||
dma_cbuf_configure(&usart1_rx_buffer, buffer, (void*)&usart1->DR,
|
||||
size, DMA_PERIPH_1, DMA_CHANNEL_5, priority);
|
||||
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;
|
||||
reg_set(usart1->CR3, USART_CR3_DMAR);
|
||||
break;
|
||||
case USART_PERIPH_2:
|
||||
dma_cbuf_configure(&usart2_rx_buffer, buffer, (void*)&usart2->DR,
|
||||
size, DMA_PERIPH_1, DMA_CHANNEL_6, priority);
|
||||
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;
|
||||
reg_set(usart2->CR3, USART_CR3_DMAR);
|
||||
break;
|
||||
|
||||
case USART_PERIPH_3:
|
||||
dma_cbuf_configure(&usart3_rx_buffer, buffer, (void*)&usart3->DR,
|
||||
size, DMA_PERIPH_1, DMA_CHANNEL_3, priority);
|
||||
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;
|
||||
reg_set(usart3->CR3, USART_CR3_DMAR);
|
||||
|
||||
break;
|
||||
@ -309,6 +354,68 @@ 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------------------------------------------------------------------------
|
||||
|
||||
@ -10,8 +10,6 @@
|
||||
|
||||
//--includes--------------------------------------------------------------------
|
||||
|
||||
#include "dma.h"
|
||||
|
||||
#include "stdint.h"
|
||||
|
||||
|
||||
@ -49,10 +47,10 @@ uint32_t usart_write_byte(enum UsartPeriph periph, uint8_t byte);
|
||||
uint32_t usart_read_byte(enum UsartPeriph periph, uint8_t* byte);
|
||||
|
||||
void usart_set_tx_buffer(enum UsartPeriph periph, uint8_t** buffers,
|
||||
uint16_t buffer_size, uint8_t buffer_nb, enum DmaConfig priority);
|
||||
uint16_t buffer_size, uint8_t buffer_nb);
|
||||
|
||||
void usart_set_rx_buffer(enum UsartPeriph periph, uint8_t* buffer,
|
||||
uint16_t size, enum DmaConfig priority);
|
||||
uint16_t size);
|
||||
|
||||
|
||||
#endif //_USART_H_
|
||||
@ -1,89 +0,0 @@
|
||||
/** @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-----------------------------------------------------------
|
||||
|
||||
#define DMA_CONFIG (DMA_CONFIG_IRQ_COMPLETE | DMA_CONFIG_FROM_PERIPH \
|
||||
| DMA_CONFIG_CIRCULAR | DMA_CONFIG_INC_MEM \
|
||||
| DMA_CONFIG_PSIZE_8BITS | DMA_CONFIG_MSIZE_8BITS)
|
||||
|
||||
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 priority)
|
||||
{
|
||||
#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 = DMA_CONFIG | priority;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@ -1,58 +0,0 @@
|
||||
/** @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 used by the circular buffer system. This system allows bufferized
|
||||
* reads to a peripheral with no latency, minimal cpu usage and customisable
|
||||
* buffer sizes
|
||||
*/
|
||||
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-------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Configure a DMA circular buffer for a single DMA channel. A standard buffer
|
||||
* is used a base to construct a circular buffer, supplied in data by DMA
|
||||
* tranfers from a peripheral. The DMA's priority is given as parameters.
|
||||
* The peripheral's specific configuration must be handled separately.
|
||||
*
|
||||
* Once this function is called, the system will start running right away and
|
||||
* will stay running until manually stopped
|
||||
*/
|
||||
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 priority);
|
||||
|
||||
uint32_t dma_cbuf_read_byte(volatile struct DmaCircBuffer* buffer,
|
||||
uint8_t* byte);
|
||||
|
||||
|
||||
#endif //_DMA_CBUF_H_
|
||||
|
||||
@ -1,78 +0,0 @@
|
||||
/** @file dma_mbuf.h
|
||||
* Module handling Direct Memory Access controller's multibuffer system
|
||||
*
|
||||
* The module provides a convenient tool to send data to a peripheral in a
|
||||
* buffered, low-altency, low-cpu usage, non-blocking way
|
||||
*/
|
||||
|
||||
#ifndef _DMA_MBUF_H_
|
||||
#define _DMA_MBUF_H_
|
||||
|
||||
//--includes--------------------------------------------------------------------
|
||||
|
||||
#include "dma.h"
|
||||
|
||||
|
||||
//--type definitions------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Struct used by the multibuffer system. This system allows bufferized writes
|
||||
* to a peripheral with no latency, minimal cpu usage and customisable buffer
|
||||
* sizes
|
||||
*/
|
||||
struct DmaMultiBuffer {
|
||||
void** buffers; //list of buffers to write to
|
||||
volatile void* dest; //destination peripheral register
|
||||
|
||||
uint16_t buffer_size; //size of a single buffer
|
||||
|
||||
uint8_t buffer_nb; //total number of buffers
|
||||
uint8_t free_buffer_nb; //number of buffers not currently used
|
||||
uint8_t buffer_index; //index of the current buffer
|
||||
uint8_t dma_buffer_index; //index of the DMA's current 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
|
||||
};
|
||||
|
||||
|
||||
//--functions-------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Configure a DMA multibuffer for a single DMA channel. A list of buffers is
|
||||
* used to allow concurent write and DMA tranfers to the specified destination
|
||||
* wich must be a peripheral. The DMA's priority is also given as parameters.
|
||||
* The peripheral's specific configuration must be handled separately. 2 bytes
|
||||
* are reserved in each buffer for index storage.
|
||||
*
|
||||
* This system needs to be started manually: dma_mbuf_refresh() should be called
|
||||
* whenever a DMA transfer can be started. This can be done manually after
|
||||
* filling up the buffer. Transfers will then automatically be started as long
|
||||
* as there are bytes in the buffer. See the usart module for an usage exemple
|
||||
*/
|
||||
void dma_mbuf_configure(volatile struct DmaMultiBuffer* buffer, void** buffers,
|
||||
volatile void* dest, uint16_t buffer_size, uint8_t buffer_nb,
|
||||
enum DmaPeriph dma, enum DmaChannel channel, enum DmaConfig priority);
|
||||
|
||||
/**
|
||||
* Write the given byte to the given buffer. Returns 0 if the write operation
|
||||
* was successfull, 1 otherwise. The function is non-blocking.
|
||||
*
|
||||
* Note: calling this function will not trigger a DMA transfer, see
|
||||
* dma_mbuf_refresh() to do that
|
||||
*/
|
||||
uint32_t dma_mbuf_write_byte(volatile struct DmaMultiBuffer* buffer,
|
||||
uint8_t byte);
|
||||
|
||||
/**
|
||||
* Refresh the buffer state, checking for bytes to send and triggering a DMA
|
||||
* transfer if necessary. Should be called for the first transfer, any remaining
|
||||
* transfer will be handled automatically until there are no bytes left in the
|
||||
* buffer
|
||||
*/
|
||||
void dma_mbuf_refresh(volatile struct DmaMultiBuffer* buffer);
|
||||
|
||||
|
||||
#endif //_DMA_MBUF_H_
|
||||
|
||||
@ -64,10 +64,7 @@ SECTIONS
|
||||
.bss :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
_sbss = .;
|
||||
*(.bss)
|
||||
. = ALIGN(4);
|
||||
_ebss = .;
|
||||
} > RAM
|
||||
|
||||
/* memory space allocated to stack. The stack is situated at the very end of
|
||||
|
||||
60
srv/debug.c
60
srv/debug.c
@ -1,60 +0,0 @@
|
||||
/** @file debug.c
|
||||
* Module handling various debug functions
|
||||
*
|
||||
* The module provides a range of functions and tools to make debug more
|
||||
* convenient
|
||||
*/
|
||||
|
||||
//--includes--------------------------------------------------------------------
|
||||
|
||||
#include "debug.h"
|
||||
|
||||
|
||||
//--local definitions-----------------------------------------------------------
|
||||
|
||||
#define BUFFER_SIZE 83 //80 char line + \n + 2 bytes for index
|
||||
#define BUFFER_NB 3 //1 buffer in write, one in transfer and one waiting
|
||||
|
||||
//--local variables-------------------------------------------------------------
|
||||
|
||||
static enum UsartPeriph usart_periph;
|
||||
static uint8_t tx_buffer1[BUFFER_SIZE];
|
||||
static uint8_t tx_buffer2[BUFFER_SIZE];
|
||||
static uint8_t tx_buffer3[BUFFER_SIZE];
|
||||
static uint8_t* tx_buffers[BUFFER_NB] = {tx_buffer1, tx_buffer2, tx_buffer3};
|
||||
|
||||
|
||||
//--public functions------------------------------------------------------------
|
||||
|
||||
#if DEBUG_TRACE || DEBUG_WARN || DEBUG_ERROR
|
||||
|
||||
void _debug_init(enum UsartPeriph usart, enum GpioPort tx_port,
|
||||
enum GpioPin tx_pin)
|
||||
{
|
||||
usart_periph = usart;
|
||||
gpio_configure(tx_port, tx_pin, GPIO_MODE_OUTPUT_FAST,
|
||||
GPIO_CONFIG_OUT_ALT_PUSH_PULL);
|
||||
usart_configure(usart_periph, USART_CONFIG_8N1, 1152000);
|
||||
|
||||
usart_set_tx_buffer(USART_PERIPH_3, tx_buffers, BUFFER_SIZE, BUFFER_NB,
|
||||
DMA_CONFIG_PRIO_LOW);
|
||||
|
||||
debug_trace("\n");
|
||||
debug_trace("--------------------------------------------------------------------------------\n");
|
||||
debug_trace("starting debug software\n");
|
||||
debug_trace("compiled on " __DATE__ " at " __TIME__ "\n");
|
||||
debug_trace("--------------------------------------------------------------------------------\n");
|
||||
}
|
||||
|
||||
void _debug_trace(char* format, ...)
|
||||
{
|
||||
while(*format != '\0') {
|
||||
while(usart_write_byte(usart_periph, *format)) {}
|
||||
++format;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
//--local functions-------------------------------------------------------------
|
||||
|
||||
59
srv/debug.h
59
srv/debug.h
@ -1,59 +0,0 @@
|
||||
/** @file debug.h
|
||||
* Module handling various debug functions
|
||||
*
|
||||
* The module provides a range of functions and tools to make debug more
|
||||
* convenient
|
||||
*/
|
||||
|
||||
#ifndef _DEBUG_H_
|
||||
#define _DEBUG_H_
|
||||
|
||||
//--includes--------------------------------------------------------------------
|
||||
|
||||
#include "../drv/usart.h"
|
||||
#include "../drv/gpio.h"
|
||||
|
||||
#include "stdint.h"
|
||||
|
||||
#define DEBUG_TRACE 1
|
||||
#define DEBUG_WARN 0
|
||||
#define DEBUG_ERROR 0
|
||||
|
||||
//--type definitions------------------------------------------------------------
|
||||
|
||||
#if DEBUG_TRACE || DEBUG_WARN || DEBUG_ERROR
|
||||
#define debug_init(usart, tx_port, tx_pin) _debug_init(usart, tx_port, tx_pin)
|
||||
#else
|
||||
#define debug_init(usart) do {} while (0)
|
||||
#endif
|
||||
|
||||
#if DEBUG_TRACE
|
||||
#define debug_trace(format, ...) _debug_trace(format __VA_OPT__(,) __VA_ARGS__)
|
||||
#else
|
||||
#define debug_trace(format, ...) do {} while (0)
|
||||
#endif
|
||||
|
||||
#if DEBUG_WARN
|
||||
#define debug_warn(format, ...) _debug_warn(format __VA_OPT__(,) __VA_ARGS__)
|
||||
#else
|
||||
#define debug_warn(format, ...) do {} while (0)
|
||||
#endif
|
||||
|
||||
#if DEBUG_ERROR
|
||||
#define debug_error(format, ...) _debug_error(format __VA_OPT__(,) __VA_ARGS__)
|
||||
#else
|
||||
#define debug_error(format, ...) do {} while (0)
|
||||
#endif
|
||||
|
||||
|
||||
//--functions-------------------------------------------------------------------
|
||||
|
||||
void _debug_init(enum UsartPeriph usart, enum GpioPort tx_port,
|
||||
enum GpioPin tx_pin);
|
||||
void _debug_trace(char* format, ...);
|
||||
void _debug_warn(char* format, ...);
|
||||
void _debug_error(char* format, ...);
|
||||
|
||||
|
||||
#endif //_DEBUG_H_
|
||||
|
||||
24
startup.s
24
startup.s
@ -34,35 +34,19 @@ hdr_reset:
|
||||
ldr r1, = _sdata
|
||||
ldr r2, = _edata
|
||||
subs r2, r2, r1
|
||||
bcc data_init_end
|
||||
bcc startup_init_end
|
||||
ldr r3, = _sromdata
|
||||
|
||||
movs r0, #0
|
||||
|
||||
data_init_loop:
|
||||
startup_init_loop:
|
||||
ldr r4, [r3, r0]
|
||||
str r4, [r1, r0]
|
||||
adds r0, r0, #4
|
||||
cmp r0, r2
|
||||
bcc data_init_loop
|
||||
bcc startup_init_loop
|
||||
|
||||
data_init_end:
|
||||
//initialize RAM to zero
|
||||
ldr r1, = _sbss
|
||||
ldr r2, = _ebss
|
||||
subs r2, r2, r1
|
||||
bcc bss_init_end
|
||||
movs r3, #0
|
||||
|
||||
movs r0, #0
|
||||
|
||||
bss_init_loop:
|
||||
str r3, [r1, r0]
|
||||
adds r0, r0, #4
|
||||
cmp r0, r2
|
||||
bcc bss_init_loop
|
||||
|
||||
bss_init_end:
|
||||
startup_init_end:
|
||||
b main
|
||||
b hdr_reset
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user