rework #4
@ -133,6 +133,20 @@ void dma_disable(enum DmaPeriph dma, enum DmaChannel channel)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint16_t dma_get_remaining(enum DmaPeriph dma, enum DmaChannel channel)
|
||||||
|
{
|
||||||
|
switch (dma) {
|
||||||
|
case DMA_PERIPH_1:
|
||||||
|
return dma1->CHANNELS[channel].CNDTR.NDT;
|
||||||
|
break;
|
||||||
|
case DMA_PERIPH_2:
|
||||||
|
return dma2->CHANNELS[channel].CNDTR.NDT;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//--local functions-------------------------------------------------------------
|
//--local functions-------------------------------------------------------------
|
||||||
|
|
||||||
|
|||||||
@ -94,6 +94,8 @@ void dma_enable(enum DmaPeriph dma, enum DmaChannel channel);
|
|||||||
|
|
||||||
void dma_disable(enum DmaPeriph dma, enum DmaChannel channel);
|
void dma_disable(enum DmaPeriph dma, enum DmaChannel channel);
|
||||||
|
|
||||||
|
uint16_t dma_get_remaining(enum DmaPeriph dma, enum DmaChannel channe);
|
||||||
|
|
||||||
|
|
||||||
#endif //_DMA_H_
|
#endif //_DMA_H_
|
||||||
|
|
||||||
|
|||||||
@ -19,9 +19,12 @@
|
|||||||
|
|
||||||
//--local definitions-----------------------------------------------------------
|
//--local definitions-----------------------------------------------------------
|
||||||
|
|
||||||
static volatile struct USART* const usart1 = (struct USART*)USART1_BASE_ADDRESS;
|
struct CircularBuffer {
|
||||||
static volatile struct USART* const usart2 = (struct USART*)USART2_BASE_ADDRESS;
|
volatile uint8_t* buffer;
|
||||||
static volatile struct USART* const usart3 = (struct USART*)USART3_BASE_ADDRESS;
|
uint16_t size;
|
||||||
|
uint16_t begin;
|
||||||
|
bool dmaLooped;
|
||||||
|
};
|
||||||
|
|
||||||
static void configure_usart(volatile struct USART* regs,
|
static void configure_usart(volatile struct USART* regs,
|
||||||
enum UsartConfig config);
|
enum UsartConfig config);
|
||||||
@ -29,7 +32,7 @@ static void configure_baudrate(volatile struct USART* regs, uint32_t clock,
|
|||||||
uint32_t baudrate);
|
uint32_t baudrate);
|
||||||
static uint32_t periph_regs(enum UsartPeriph periph,
|
static uint32_t periph_regs(enum UsartPeriph periph,
|
||||||
volatile struct USART** regs);
|
volatile struct USART** regs);
|
||||||
static uint32_t read_from_buffer(volatile uint8_t* buffer,
|
static uint32_t read_from_buffer(struct CircularBuffer* buffer,
|
||||||
enum DmaChannel channel, uint8_t* byte);
|
enum DmaChannel channel, uint8_t* byte);
|
||||||
|
|
||||||
static void usart1_rx_callback(enum DmaIRQSource src);
|
static void usart1_rx_callback(enum DmaIRQSource src);
|
||||||
@ -37,7 +40,11 @@ static void usart1_rx_callback(enum DmaIRQSource src);
|
|||||||
|
|
||||||
//--local variables-------------------------------------------------------------
|
//--local variables-------------------------------------------------------------
|
||||||
|
|
||||||
static volatile uint8_t* usart1_rx_buffer;
|
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 struct CircularBuffer usart1_rx_buffer;
|
||||||
|
|
||||||
|
|
||||||
//--public functions------------------------------------------------------------
|
//--public functions------------------------------------------------------------
|
||||||
@ -87,13 +94,13 @@ uint32_t usart_write_byte(enum UsartPeriph periph, uint8_t byte)
|
|||||||
uint32_t usart_read_byte(enum UsartPeriph periph, uint8_t* byte)
|
uint32_t usart_read_byte(enum UsartPeriph periph, uint8_t* byte)
|
||||||
{
|
{
|
||||||
volatile struct USART* regs;
|
volatile struct USART* regs;
|
||||||
volatile uint8_t* buffer;
|
struct CircularBuffer* buffer;
|
||||||
enum DmaChannel dma_channel;
|
enum DmaChannel dma_channel;
|
||||||
|
|
||||||
switch (periph) {
|
switch (periph) {
|
||||||
case USART_PERIPH_1:
|
case USART_PERIPH_1:
|
||||||
regs = usart1;
|
regs = usart1;
|
||||||
buffer = usart1_rx_buffer;
|
buffer = &usart1_rx_buffer;
|
||||||
dma_channel = DMA_CHANNEL_5;
|
dma_channel = DMA_CHANNEL_5;
|
||||||
break;
|
break;
|
||||||
case USART_PERIPH_2:
|
case USART_PERIPH_2:
|
||||||
@ -103,7 +110,7 @@ uint32_t usart_read_byte(enum UsartPeriph periph, uint8_t* byte)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buffer) {
|
if (buffer->buffer) {
|
||||||
return read_from_buffer(buffer, dma_channel, byte);
|
return read_from_buffer(buffer, dma_channel, byte);
|
||||||
} else {
|
} else {
|
||||||
if (regs->SR.RXNE) {
|
if (regs->SR.RXNE) {
|
||||||
@ -126,7 +133,10 @@ void usart_set_rx_buffer(enum UsartPeriph periph, uint8_t* buffer,
|
|||||||
| DMA_CONFIG_PSIZE_8BITS | DMA_CONFIG_MSIZE_8BITS
|
| DMA_CONFIG_PSIZE_8BITS | DMA_CONFIG_MSIZE_8BITS
|
||||||
| DMA_CONFIG_PRIO_LOW, (void*)&usart1->DR, buffer,
|
| DMA_CONFIG_PRIO_LOW, (void*)&usart1->DR, buffer,
|
||||||
size, usart1_rx_callback);
|
size, usart1_rx_callback);
|
||||||
usart1_rx_buffer = buffer;
|
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);
|
reg_set(usart1->CR3, USART_CR3_DMAR);
|
||||||
break;
|
break;
|
||||||
case USART_PERIPH_2:
|
case USART_PERIPH_2:
|
||||||
@ -140,6 +150,7 @@ void usart_set_rx_buffer(enum UsartPeriph periph, uint8_t* buffer,
|
|||||||
|
|
||||||
static void configure_usart(volatile struct USART* regs, enum UsartConfig config)
|
static void configure_usart(volatile struct USART* regs, enum UsartConfig config)
|
||||||
{
|
{
|
||||||
|
usart1_rx_buffer.buffer = NULL;
|
||||||
//configure parity
|
//configure parity
|
||||||
switch (config)
|
switch (config)
|
||||||
{
|
{
|
||||||
@ -247,16 +258,34 @@ static uint32_t periph_regs(enum UsartPeriph periph, volatile struct USART** reg
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32_t read_from_buffer(volatile uint8_t* buffer,
|
static uint32_t read_from_buffer(struct CircularBuffer* buffer,
|
||||||
enum DmaChannel channel, uint8_t* byte)
|
enum DmaChannel channel, uint8_t* byte)
|
||||||
{
|
{
|
||||||
|
uint16_t end = buffer->size - dma_get_remaining(DMA_PERIPH_1, channel);
|
||||||
|
|
||||||
|
if ((end > buffer->begin) && buffer->dmaLooped) {
|
||||||
|
//TODO overflow
|
||||||
|
buffer->begin = end;
|
||||||
|
} else if ((buffer->begin == end) && !buffer->dmaLooped) {
|
||||||
|
//TODO no data
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
*byte = buffer->buffer[buffer->begin];
|
||||||
|
++buffer->begin;
|
||||||
|
if (buffer->begin >= buffer->size) {
|
||||||
|
buffer->begin = 0;
|
||||||
|
buffer->dmaLooped = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//--callbacks-------------------------------------------------------------------
|
//--callbacks-------------------------------------------------------------------
|
||||||
|
|
||||||
static void usart1_rx_callback(enum DmaIRQSource src)
|
static void usart1_rx_callback(enum DmaIRQSource src)
|
||||||
{
|
{
|
||||||
|
(void)src; //only transfer complete expected
|
||||||
|
usart1_rx_buffer.dmaLooped = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user