Make ring buffers volatile

This commit is contained in:
Mario Hüttel 2020-02-16 11:42:18 +01:00
parent 1d62bc22b8
commit 7ade829a70
3 changed files with 12 additions and 11 deletions

View File

@ -29,7 +29,7 @@
* @brief DMA ring buffer for data transfer from peripheral to memory
*/
struct dma_ring_buffer_to_mem {
void *data_ptr; /**< @brief Ring buffer */
volatile void *data_ptr; /**< @brief Ring buffer */
size_t buffer_count; /**< @brief Size of buffer in multiples of elements */
DMA_Stream_TypeDef *dma; /**< @brief DMA Stream used to transfer data */
size_t get_idx; /**< @brief Get index for SW. Indicates the next element to be read */
@ -41,7 +41,7 @@ struct dma_ring_buffer_to_mem {
* @brief DMA ring buffer for data transfer from memory to peripheral
*/
struct dma_ring_buffer_to_periph {
void *src_buffer; /**< @brief Ring buffer */
volatile void *src_buffer; /**< @brief Ring buffer */
size_t buffer_count; /**< @brief Size of buffer in multiples of elements */
DMA_Stream_TypeDef *dma; /**< @brief DMA Stream used to transfer data */
volatile size_t dma_get_idx_current; /**< @brief Current get index of the (not yet) running DMA Stream. */
@ -64,7 +64,7 @@ struct dma_ring_buffer_to_periph {
* @note The ring buffers do not have an overrun detection. You have to make sure to empty the buffer in time.
* @return Status (0 is ok)
*/
int dma_ring_buffer_periph_to_mem_initialize(struct dma_ring_buffer_to_mem *dma_buffer, uint8_t base_dma_id, DMA_Stream_TypeDef *dma_stream, size_t buffer_element_count, size_t element_size, void *data_buffer, void *src_reg, uint8_t dma_trigger_channel);
int dma_ring_buffer_periph_to_mem_initialize(struct dma_ring_buffer_to_mem *dma_buffer, uint8_t base_dma_id, DMA_Stream_TypeDef *dma_stream, size_t buffer_element_count, size_t element_size, volatile void *data_buffer, void *src_reg, uint8_t dma_trigger_channel);
/**
* @brief Get data from a peripheral to memory ring buffer
@ -73,7 +73,7 @@ int dma_ring_buffer_periph_to_mem_initialize(struct dma_ring_buffer_to_mem *dma_
* @param[out] len Length in elements
* @return 0 if successful, but no data), -1 if error, 1 if data, and 2 if data with wrap around. Call function again in this case to retrieve rest after wrap around.
*/
int dma_ring_buffer_periph_to_mem_get_data(struct dma_ring_buffer_to_mem *buff, const void **data_buff, size_t *len);
int dma_ring_buffer_periph_to_mem_get_data(struct dma_ring_buffer_to_mem *buff, const volatile void **data_buff, size_t *len);
/**
* @brief Stop the ring buffer operation.
@ -94,7 +94,7 @@ void dma_ring_buffer_periph_to_mem_stop(struct dma_ring_buffer_to_mem *buff);
* @param[in] dest_reg Destination register to stream data to
* @return 0 if successful
*/
int dma_ring_buffer_mem_to_periph_initialize(struct dma_ring_buffer_to_periph *dma_buffer, uint8_t base_dma_id, DMA_Stream_TypeDef *dma_stream, size_t buffer_element_count, size_t element_size, void *data_buffer, uint8_t dma_trigger_channel, void *dest_reg);
int dma_ring_buffer_mem_to_periph_initialize(struct dma_ring_buffer_to_periph *dma_buffer, uint8_t base_dma_id, DMA_Stream_TypeDef *dma_stream, size_t buffer_element_count, size_t element_size, volatile void *data_buffer, uint8_t dma_trigger_channel, void *dest_reg);
/**
* @brief Insert data into the ring buffer

View File

@ -50,7 +50,7 @@ static int dma_ring_buffer_switch_clock_enable(uint8_t base_dma, bool clk_en)
int dma_ring_buffer_periph_to_mem_initialize(struct dma_ring_buffer_to_mem *dma_buffer, uint8_t base_dma_id,
DMA_Stream_TypeDef *dma_stream, size_t buffer_element_count, size_t element_size,
void *data_buffer, void* src_reg, uint8_t dma_trigger_channel)
volatile void *data_buffer, void* src_reg, uint8_t dma_trigger_channel)
{
int ret_val = 0;
@ -83,7 +83,7 @@ int dma_ring_buffer_periph_to_mem_initialize(struct dma_ring_buffer_to_mem *dma_
return 0;
}
int dma_ring_buffer_periph_to_mem_get_data(struct dma_ring_buffer_to_mem *buff, const void **data_buff, size_t *len)
int dma_ring_buffer_periph_to_mem_get_data(struct dma_ring_buffer_to_mem *buff, const volatile void **data_buff, size_t *len)
{
int ret_code = 0;
uint32_t ndtr;
@ -92,17 +92,18 @@ int dma_ring_buffer_periph_to_mem_get_data(struct dma_ring_buffer_to_mem *buff,
if (!buff || !data_buff || !len)
return -1;
ndtr = buff->dma->NDTR;
put_idx = buff->buffer_count - ndtr;
/* Check if wrap around */
if (put_idx < buff->get_idx) {
/* Available data wraps around end of buffer: Return first part upt to the end of the ring buffer */
*data_buff = &(((char *)buff->data_ptr)[buff->get_idx * buff->element_size]);
*len = buff->buffer_count - buff->get_idx;
buff->get_idx = 0;
ret_code = 2;
} else if (put_idx > buff->get_idx) {
/* Data does not wrap around ring buffer. Return full data */
*data_buff = &(((char *)buff->data_ptr)[buff->get_idx * buff->element_size]);
*len = put_idx - buff->get_idx;
buff->get_idx += *len;
@ -130,7 +131,7 @@ void dma_ring_buffer_periph_to_mem_stop(struct dma_ring_buffer_to_mem *buff)
memset(buff, 0, sizeof(struct dma_ring_buffer_to_mem));
}
int dma_ring_buffer_mem_to_periph_initialize(struct dma_ring_buffer_to_periph *dma_buffer, uint8_t base_dma_id, DMA_Stream_TypeDef *dma_stream, size_t buffer_element_count, size_t element_size, void *data_buffer, uint8_t dma_trigger_channel, void *dest_reg)
int dma_ring_buffer_mem_to_periph_initialize(struct dma_ring_buffer_to_periph *dma_buffer, uint8_t base_dma_id, DMA_Stream_TypeDef *dma_stream, size_t buffer_element_count, size_t element_size, volatile void *data_buffer, uint8_t dma_trigger_channel, void *dest_reg)
{
if (!dma_buffer || !dma_stream || !data_buffer || !dest_reg)
return -1000;
@ -244,7 +245,7 @@ int dma_ring_buffer_mem_to_periph_insert_data(struct dma_ring_buffer_to_periph *
first_round_count = buff->element_size * (buff->buffer_count - buff->sw_put_idx);
memcpy(dest_ptr, insert_ptr, first_round_count);
insert_ptr += first_round_count;
memcpy(buff->src_buffer, insert_ptr, count - first_round_count);
memcpy((void *)buff->src_buffer, insert_ptr, count - first_round_count);
/* Move put index */
buff->sw_put_idx = count - first_round_count;

View File

@ -107,7 +107,7 @@ void uart_send_string_with_dma(const char *string)
int uart_receive_data_with_dma(const char **data, size_t *len)
{
return dma_ring_buffer_periph_to_mem_get_data(&ring_buff_rx, (const void **)data, len);
return dma_ring_buffer_periph_to_mem_get_data(&ring_buff_rx, (const volatile void **)data, len);
}
void DMA2_Stream7_IRQHandler()