Alternating buffers for DMA (works only with optimization)
This commit is contained in:
parent
fe332dea5d
commit
731bba0f0e
8 changed files with 106 additions and 40 deletions
|
@ -169,7 +169,7 @@
|
||||||
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.target_cpuid.427092296" name="CpuId" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.target_cpuid" value="0" valueType="string"/>
|
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.target_cpuid.427092296" name="CpuId" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.target_cpuid" value="0" valueType="string"/>
|
||||||
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.target_coreid.803288303" name="CpuCoreId" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.target_coreid" value="0" valueType="string"/>
|
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.target_coreid.803288303" name="CpuCoreId" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.target_coreid" value="0" valueType="string"/>
|
||||||
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.target_board.142185949" name="Board" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.target_board" value="genericBoard" valueType="string"/>
|
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.target_board.142185949" name="Board" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.target_board" value="genericBoard" valueType="string"/>
|
||||||
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.defaults.1897240971" name="Defaults" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.defaults" value="com.st.stm32cube.ide.common.services.build.inputs.revA.1.0.3 || Release || false || Executable || com.st.stm32cube.ide.mcu.gnu.managedbuild.toolchain.base.gnu-tools-for-stm32 || STM32F103C8Tx || 0 || 0 || arm-none-eabi- || ${gnu_tools_for_stm32_compiler_path} || ../Inc | ../Drivers/STM32F1xx_HAL_Driver/Inc | ../Drivers/STM32F1xx_HAL_Driver/Inc/Legacy | ../Drivers/CMSIS/Device/ST/STM32F1xx/Include | ../Drivers/CMSIS/Include || || || USE_HAL_DRIVER | STM32F103xB || || Drivers | Src | Startup || || || ${workspace_loc:/${ProjName}/STM32F103C8TX_FLASH.ld} || true || NonSecure || || secure_nsclib.o || " valueType="string"/>
|
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.defaults.1897240971" name="Defaults" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.option.defaults" value="com.st.stm32cube.ide.common.services.build.inputs.revA.1.0.3 || Release || true || Executable || com.st.stm32cube.ide.mcu.gnu.managedbuild.toolchain.base.gnu-tools-for-stm32 || STM32F103C8Tx || 0 || 0 || arm-none-eabi- || ${gnu_tools_for_stm32_compiler_path} || ../Inc | ../Drivers/CMSIS/Device/ST/STM32F1xx/Include | ../Drivers/CMSIS/Include | ../Drivers/STM32F1xx_HAL_Driver/Inc/Legacy | ../Drivers/STM32F1xx_HAL_Driver/Inc || || || VDD_VALUE=3300 | HSE_VALUE=8000000 | STM32F103xB | LSI_VALUE=40000 | HSI_VALUE=8000000 | HSE_STARTUP_TIMEOUT=100 | USE_FULL_LL_DRIVER | LSE_STARTUP_TIMEOUT=5000 | LSE_VALUE=32768 | PREFETCH_ENABLE=1 || || Drivers | Src | Startup || || || ${workspace_loc:/${ProjName}/STM32F103C8TX_FLASH.ld} || true || NonSecure || || secure_nsclib.o || " valueType="string"/>
|
||||||
<targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.ELF" id="com.st.stm32cube.ide.mcu.gnu.managedbuild.targetplatform.1774904877" isAbstract="false" osList="all" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.targetplatform"/>
|
<targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.ELF" id="com.st.stm32cube.ide.mcu.gnu.managedbuild.targetplatform.1774904877" isAbstract="false" osList="all" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.targetplatform"/>
|
||||||
<builder buildPath="${workspace_loc:/NeoPixelF103MVP}/Release" id="com.st.stm32cube.ide.mcu.gnu.managedbuild.builder.1500348018" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="Gnu Make Builder" parallelBuildOn="true" parallelizationNumber="optimal" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.builder"/>
|
<builder buildPath="${workspace_loc:/NeoPixelF103MVP}/Release" id="com.st.stm32cube.ide.mcu.gnu.managedbuild.builder.1500348018" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="Gnu Make Builder" parallelBuildOn="true" parallelizationNumber="optimal" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.builder"/>
|
||||||
<tool id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.assembler.2006730064" name="MCU GCC Assembler" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.assembler">
|
<tool id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.assembler.2006730064" name="MCU GCC Assembler" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.assembler">
|
||||||
|
|
92
App/App.cpp
92
App/App.cpp
|
@ -21,9 +21,16 @@
|
||||||
|
|
||||||
Sparkle g_sparkles[NUMSPARKLES];
|
Sparkle g_sparkles[NUMSPARKLES];
|
||||||
volatile uint8_t g_spi_idle = 0;
|
volatile uint8_t g_spi_idle = 0;
|
||||||
|
uint16_t g_pixels_transmitted;
|
||||||
|
volatile uint8_t g_buffer_in_transmit;
|
||||||
|
volatile bool g_need_refill;
|
||||||
|
uint32_t g_refills = 0;
|
||||||
|
uint16_t g_pixels_converted = 0;
|
||||||
|
|
||||||
volatile uint32_t g_tick = 0;
|
volatile uint32_t g_tick = 0;
|
||||||
|
|
||||||
|
#define MIN(a,b) ((a) < (b) ? (a) : (b))
|
||||||
|
|
||||||
void convert(uint8_t *src, uint8_t *dst, uint16_t size)
|
void convert(uint8_t *src, uint8_t *dst, uint16_t size)
|
||||||
{
|
{
|
||||||
static uint8_t const bits[4] = { 0b10001000, 0b10001110, 0b11101000, 0b11101110 };
|
static uint8_t const bits[4] = { 0b10001000, 0b10001110, 0b11101000, 0b11101110 };
|
||||||
|
@ -51,7 +58,7 @@ uint16_t ChoosePixel()
|
||||||
do {
|
do {
|
||||||
chosen = rr(NUMPIXELS);
|
chosen = rr(NUMPIXELS);
|
||||||
for(spi=0; spi<NUMSPARKLES; ++spi) {
|
for(spi=0; spi<NUMSPARKLES; ++spi) {
|
||||||
if(static_cast<Pixel_t*>(g_sparkles[spi]) && static_cast<Pixel_t*>(g_sparkles[spi]) == &g_pixels[chosen])
|
if(static_cast<pixel_t*>(g_sparkles[spi]) && static_cast<pixel_t*>(g_sparkles[spi]) == &g_pixels[chosen])
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} while(spi < NUMSPARKLES);
|
} while(spi < NUMSPARKLES);
|
||||||
|
@ -63,7 +70,7 @@ uint16_t ChoosePixel()
|
||||||
|
|
||||||
void StartSparkle( Sparkle &s )
|
void StartSparkle( Sparkle &s )
|
||||||
{
|
{
|
||||||
s.Start(g_pixels+ChoosePixel(), Pixel(255,255,255), Pixel(rr(8)+3,rr(8)+3,rr(8)+3));
|
s.Start(g_pixels+ChoosePixel(), rr(32), Pixel(255,255,255), Pixel(rr(8)+3,rr(8)+3,rr(8)+3));
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" void HandleSystick()
|
extern "C" void HandleSystick()
|
||||||
|
@ -78,54 +85,97 @@ extern "C" uint32_t GetTick()
|
||||||
|
|
||||||
extern "C" void HandleSpiDmaIrq()
|
extern "C" void HandleSpiDmaIrq()
|
||||||
{
|
{
|
||||||
if(LL_DMA_IsActiveFlag_TC3(DMA1)) {
|
static bool endframe;
|
||||||
|
|
||||||
|
if(LL_DMA_IsActiveFlag_TC3(DMA1))
|
||||||
|
{
|
||||||
LL_DMA_ClearFlag_TC3(DMA1);
|
LL_DMA_ClearFlag_TC3(DMA1);
|
||||||
g_spi_idle = true;
|
|
||||||
LL_SPI_DisableDMAReq_TX(SPI1);
|
|
||||||
LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_3);
|
LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_3);
|
||||||
} else if(LL_DMA_IsActiveFlag_TE3(DMA1))
|
|
||||||
|
g_buffer_in_transmit ^= 1;
|
||||||
|
g_pixels_transmitted += SPIBUFFER_PIXELS;
|
||||||
|
|
||||||
|
if(g_pixels_transmitted < NUMPIXELS)
|
||||||
|
{
|
||||||
|
LL_DMA_SetMemoryAddress(DMA1, LL_DMA_CHANNEL_3, (uint32_t)g_spibuffer[g_buffer_in_transmit]);
|
||||||
|
LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_3, SPIBUFFER_SIZE);
|
||||||
|
LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_3);
|
||||||
|
|
||||||
|
if(g_pixels_converted < NUMPIXELS)
|
||||||
|
{
|
||||||
|
LL_GPIO_TogglePin(GPIOC, LL_GPIO_PIN_13);
|
||||||
|
convert((uint8_t*)&g_pixels[g_pixels_converted],
|
||||||
|
g_spibuffer[g_buffer_in_transmit ^ 1],
|
||||||
|
MIN(SPIBUFFER_PIXELS, NUMPIXELS - g_pixels_converted) * sizeof(pixel_t));
|
||||||
|
g_pixels_converted += MIN(SPIBUFFER_PIXELS, NUMPIXELS - g_pixels_converted);
|
||||||
|
LL_GPIO_TogglePin(GPIOC, LL_GPIO_PIN_13);
|
||||||
|
endframe = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
memset(g_spibuffer[g_buffer_in_transmit ^1], 0, sizeof(g_spibuffer[g_buffer_in_transmit ^1]));
|
||||||
|
endframe = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else if(endframe)
|
||||||
|
{
|
||||||
|
LL_DMA_SetMemoryAddress(DMA1, LL_DMA_CHANNEL_3, (uint32_t)g_spibuffer[g_buffer_in_transmit]);
|
||||||
|
LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_3, SPIBUFFER_SIZE);
|
||||||
|
LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_3);
|
||||||
|
endframe = false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
g_spi_idle = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(LL_DMA_IsActiveFlag_TE3(DMA1))
|
||||||
LL_DMA_ClearFlag_TE3(DMA1);
|
LL_DMA_ClearFlag_TE3(DMA1);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" void App()
|
extern "C" void App()
|
||||||
{
|
{
|
||||||
g_spibuffer[sizeof(g_spibuffer)-1] = 0;
|
uint32_t lastTick = GetTick();
|
||||||
|
|
||||||
for(uint16_t px = 0; px < NUMPIXELS; ++px)
|
for(uint16_t px = 0; px < NUMPIXELS; ++px)
|
||||||
g_pixels[px] = Pixel( DEFAULT_COLOR );
|
g_pixels[px] = Pixel(DEFAULT_COLOR);
|
||||||
|
|
||||||
uint32_t lastTick = GetTick();
|
|
||||||
|
|
||||||
LL_SYSTICK_EnableIT();
|
LL_SYSTICK_EnableIT();
|
||||||
LL_SPI_Enable(SPI1);
|
LL_SPI_Enable(SPI1);
|
||||||
|
LL_SPI_EnableDMAReq_TX(SPI1);
|
||||||
|
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
while(GetTick() - lastTick < FRAMETIME );
|
while(GetTick() - lastTick < FRAMETIME );
|
||||||
lastTick += FRAMETIME;
|
lastTick += FRAMETIME;
|
||||||
LL_GPIO_TogglePin(GPIOC, LL_GPIO_PIN_13);
|
|
||||||
|
|
||||||
for(int16_t spi = 0; spi < NUMSPARKLES; ++spi) {
|
for(int16_t spi = 0; spi < NUMSPARKLES; ++spi) {
|
||||||
if(static_cast<Pixel_t*>(g_sparkles[spi]))
|
if(static_cast<pixel_t*>(g_sparkles[spi]))
|
||||||
g_sparkles[spi].Step();
|
g_sparkles[spi].Step();
|
||||||
else
|
else
|
||||||
StartSparkle(g_sparkles[spi]);
|
StartSparkle(g_sparkles[spi]);
|
||||||
}
|
}
|
||||||
LL_GPIO_TogglePin(GPIOC, LL_GPIO_PIN_13);
|
|
||||||
|
|
||||||
convert((uint8_t*)g_pixels, g_spibuffer, sizeof(g_pixels));
|
g_pixels_converted = 0;
|
||||||
LL_GPIO_TogglePin(GPIOC, LL_GPIO_PIN_13);
|
g_pixels_transmitted = 0;
|
||||||
|
g_need_refill = true;
|
||||||
|
|
||||||
g_spi_idle = false;
|
convert((uint8_t*)g_pixels, g_spibuffer[0], SPIBUFFER_PIXELS * sizeof(pixel_t));
|
||||||
// HAL_SPI_Transmit_DMA(&hspi1, g_ledBits, sizeof(g_ledBits));
|
g_pixels_converted += SPIBUFFER_PIXELS;
|
||||||
LL_DMA_ConfigAddresses(DMA1, LL_DMA_CHANNEL_3, (uint32_t)g_spibuffer, LL_SPI_DMA_GetRegAddr(SPI1), LL_DMA_DIRECTION_MEMORY_TO_PERIPH);
|
convert((uint8_t*)&g_pixels[g_pixels_converted], g_spibuffer[1], SPIBUFFER_PIXELS * sizeof(pixel_t));
|
||||||
LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_3, sizeof(g_spibuffer));
|
g_pixels_converted += SPIBUFFER_PIXELS;
|
||||||
|
|
||||||
|
g_buffer_in_transmit = 0;
|
||||||
|
|
||||||
|
LL_DMA_ConfigAddresses(DMA1, LL_DMA_CHANNEL_3, (uint32_t)g_spibuffer[0], LL_SPI_DMA_GetRegAddr(SPI1), LL_DMA_DIRECTION_MEMORY_TO_PERIPH);
|
||||||
|
LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_3, SPIBUFFER_SIZE);
|
||||||
LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_3);
|
LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_3);
|
||||||
LL_SPI_EnableDMAReq_TX(SPI1);
|
|
||||||
LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_3);
|
LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_3);
|
||||||
LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_3);
|
LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_3);
|
||||||
|
g_spi_idle = false;
|
||||||
|
|
||||||
while(!g_spi_idle);
|
while(!g_spi_idle);
|
||||||
LL_GPIO_TogglePin(GPIOC, LL_GPIO_PIN_13);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,11 +13,13 @@
|
||||||
#ifndef DBG_CHOSEN_PIXEL
|
#ifndef DBG_CHOSEN_PIXEL
|
||||||
# define NUMPIXELS 144
|
# define NUMPIXELS 144
|
||||||
# define NUMSPARKLES (NUMPIXELS/12+1)
|
# define NUMSPARKLES (NUMPIXELS/12+1)
|
||||||
|
# define FRAMETIME 20
|
||||||
#else
|
#else
|
||||||
# define NUMPIXELS 6
|
# define NUMPIXELS 6
|
||||||
# define NUMSPARKLES 1
|
# define NUMSPARKLES 1
|
||||||
|
# define FRAMETIME 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define FRAMETIME 20
|
#define DEFAULT_COLOR 63,63,63
|
||||||
#define DEFAULT_COLOR 0,0,0
|
#define SPIBUFFER_PIXELS 2
|
||||||
#endif /* CONFIG_H_ */
|
#endif /* CONFIG_H_ */
|
||||||
|
|
|
@ -6,8 +6,13 @@
|
||||||
*/
|
*/
|
||||||
#include "LedBuffers.h"
|
#include "LedBuffers.h"
|
||||||
|
|
||||||
Pixel_t g_pixels[NUMPIXELS];
|
pixel_t g_pixels[NUMPIXELS] = {
|
||||||
uint8_t g_spibuffer[sizeof(g_pixels) * 8 / 2 + 1];
|
{0xaa,0x55,0},
|
||||||
|
{0x55,0xaa,0xff},
|
||||||
|
{0xaa,0x55,0},
|
||||||
|
{0x55,0xaa,0xff},
|
||||||
|
{0xaa,0x55,0},
|
||||||
|
{0x55,0xaa,0xff},
|
||||||
|
};
|
||||||
|
|
||||||
|
uint8_t g_spibuffer[2][SPIBUFFER_SIZE];
|
||||||
|
|
|
@ -11,7 +11,10 @@
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
#include "Pixel.h"
|
#include "Pixel.h"
|
||||||
|
|
||||||
extern Pixel_t g_pixels[NUMPIXELS];
|
extern pixel_t g_pixels[NUMPIXELS];
|
||||||
extern uint8_t g_spibuffer[sizeof(g_pixels) * 8 / 2 + 1];
|
|
||||||
|
#define SPIBUFFER_SIZE (SPIBUFFER_PIXELS * sizeof(pixel_t) * 8 / 2)
|
||||||
|
|
||||||
|
extern uint8_t g_spibuffer[2][SPIBUFFER_SIZE];
|
||||||
|
|
||||||
#endif /* LEDBUFFERS_H_ */
|
#endif /* LEDBUFFERS_H_ */
|
||||||
|
|
|
@ -18,14 +18,14 @@ typedef struct {
|
||||||
uint8_t g;
|
uint8_t g;
|
||||||
uint8_t r;
|
uint8_t r;
|
||||||
uint8_t b;
|
uint8_t b;
|
||||||
} Pixel_t;
|
} pixel_t;
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
struct Pixel : public Pixel_t {
|
struct Pixel : public pixel_t {
|
||||||
Pixel(uint8_t _r, uint8_t _g, uint8_t _b) {
|
Pixel(uint8_t _r, uint8_t _g, uint8_t _b) {
|
||||||
g =_g;
|
g =_g;
|
||||||
r = _r;
|
r = _r;
|
||||||
|
|
|
@ -14,16 +14,22 @@ Sparkle::Sparkle()
|
||||||
Sparkle::~Sparkle() {
|
Sparkle::~Sparkle() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sparkle::Start(Pixel_t *target, Pixel_t color, Pixel_t fadeSpeed)
|
void Sparkle::Start(pixel_t *target, uint8_t delay, pixel_t color, pixel_t fadeSpeed)
|
||||||
{
|
{
|
||||||
m_target = target;
|
m_target = target;
|
||||||
*target = color;
|
*target = color;
|
||||||
|
m_delay = delay;
|
||||||
m_fadeSpeed = fadeSpeed;
|
m_fadeSpeed = fadeSpeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Sparkle::Step()
|
bool Sparkle::Step()
|
||||||
{
|
{
|
||||||
Pixel_t current = *m_target;
|
if(m_delay) {
|
||||||
|
--m_delay;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
pixel_t current = *m_target;
|
||||||
uint8_t remaining = 3;
|
uint8_t remaining = 3;
|
||||||
|
|
||||||
if(current.r > m_fadeSpeed.r )current.r -= m_fadeSpeed.r;
|
if(current.r > m_fadeSpeed.r )current.r -= m_fadeSpeed.r;
|
||||||
|
@ -48,5 +54,4 @@ bool Sparkle::Step()
|
||||||
if(remaining) return true;
|
if(remaining) return true;
|
||||||
m_target = nullptr;
|
m_target = nullptr;
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,13 +14,14 @@ class Sparkle {
|
||||||
public:
|
public:
|
||||||
Sparkle();
|
Sparkle();
|
||||||
~Sparkle();
|
~Sparkle();
|
||||||
void Start(Pixel_t *target, Pixel_t color, Pixel_t fadeSpeed);
|
void Start(pixel_t *target, uint8_t delay, pixel_t color, pixel_t fadeSpeed);
|
||||||
bool Step();
|
bool Step();
|
||||||
operator Pixel_t*() { return m_target; }
|
operator pixel_t*() { return m_target; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Pixel_t *m_target = nullptr;
|
pixel_t *m_target = nullptr;
|
||||||
Pixel_t m_fadeSpeed = {1, 1, 1};
|
uint8_t m_delay;
|
||||||
|
pixel_t m_fadeSpeed = {1, 1, 1};
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* SPARKLE_H_ */
|
#endif /* SPARKLE_H_ */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue