123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- #include "stm32_util.h"
- #include <stdint.h>
- #include <string.h>
- #include <stdio.h>
- #include "bouncebuffer.h"
- #if defined(STM32H7)
- #define IS_DMA_SAFE(addr) false
- #elif defined(STM32F7)
- #define IS_DMA_SAFE(addr) ((((uint32_t)(addr)) & ((0xFFFFFFFF & ~(64*1024U-1)) | 1U)) == 0x20000000)
- #elif defined(STM32F1)
- #define IS_DMA_SAFE(addr) true
- #else
- #define IS_DMA_SAFE(addr) ((((uint32_t)(addr)) & 0xF0000001) == 0x20000000)
- #endif
- void bouncebuffer_init(struct bouncebuffer_t **bouncebuffer, uint32_t prealloc_bytes, bool sdcard)
- {
- (*bouncebuffer) = calloc(1, sizeof(struct bouncebuffer_t));
- osalDbgAssert(((*bouncebuffer) != NULL), "bouncebuffer init");
- (*bouncebuffer)->is_sdcard = sdcard;
- if (prealloc_bytes) {
- (*bouncebuffer)->dma_buf = sdcard?malloc_sdcard_dma(prealloc_bytes):malloc_dma(prealloc_bytes);
- osalDbgAssert(((*bouncebuffer)->dma_buf != NULL), "bouncebuffer preallocate");
- (*bouncebuffer)->size = prealloc_bytes;
- }
- }
- void bouncebuffer_setup_read(struct bouncebuffer_t *bouncebuffer, uint8_t **buf, uint32_t size)
- {
- if (!bouncebuffer || IS_DMA_SAFE(*buf)) {
-
- return;
- }
- osalDbgAssert((bouncebuffer->busy == false), "bouncebuffer read");
- bouncebuffer->orig_buf = *buf;
- if (bouncebuffer->size < size) {
- if (bouncebuffer->size > 0) {
- free(bouncebuffer->dma_buf);
- }
- bouncebuffer->dma_buf = bouncebuffer->is_sdcard?malloc_sdcard_dma(size):malloc_dma(size);
- osalDbgAssert((bouncebuffer->dma_buf != NULL), "bouncebuffer read allocate");
- bouncebuffer->size = size;
- }
- *buf = bouncebuffer->dma_buf;
- #if defined(STM32H7)
- osalDbgAssert((((uint32_t)*buf)&31) == 0, "bouncebuffer read align");
- stm32_cacheBufferInvalidate(*buf, (size+31)&~31);
- #endif
- bouncebuffer->busy = true;
- }
- void bouncebuffer_finish_read(struct bouncebuffer_t *bouncebuffer, const uint8_t *buf, uint32_t size)
- {
- if (bouncebuffer && buf == bouncebuffer->dma_buf) {
- osalDbgAssert((bouncebuffer->busy == true), "bouncebuffer finish_read");
- if (bouncebuffer->orig_buf) {
- memcpy(bouncebuffer->orig_buf, buf, size);
- }
- bouncebuffer->busy = false;
- }
- }
- void bouncebuffer_setup_write(struct bouncebuffer_t *bouncebuffer, const uint8_t **buf, uint32_t size)
- {
- if (!bouncebuffer || IS_DMA_SAFE(*buf)) {
-
- return;
- }
- osalDbgAssert((bouncebuffer->busy == false), "bouncebuffer write");
- if (bouncebuffer->size < size) {
- if (bouncebuffer->size > 0) {
- free(bouncebuffer->dma_buf);
- }
- bouncebuffer->dma_buf = bouncebuffer->is_sdcard?malloc_sdcard_dma(size):malloc_dma(size);
- osalDbgAssert((bouncebuffer->dma_buf != NULL), "bouncebuffer write allocate");
- bouncebuffer->size = size;
- }
- if (*buf) {
- memcpy(bouncebuffer->dma_buf, *buf, size);
- }
- *buf = bouncebuffer->dma_buf;
- #if defined(STM32H7)
- osalDbgAssert((((uint32_t)*buf)&31) == 0, "bouncebuffer write align");
- stm32_cacheBufferFlush(*buf, (size+31)&~31);
- #endif
- bouncebuffer->busy = true;
- }
- void bouncebuffer_finish_write(struct bouncebuffer_t *bouncebuffer, const uint8_t *buf)
- {
- if (bouncebuffer && buf == bouncebuffer->dma_buf) {
- osalDbgAssert((bouncebuffer->busy == true), "bouncebuffer finish_wite");
- bouncebuffer->busy = false;
- }
- }
|