DMA based FillRect works

This commit is contained in:
Attila Body 2019-09-11 15:21:25 +02:00 committed by Attila BODY
parent d8ee389442
commit 98aacc5c47
22 changed files with 821 additions and 200 deletions

View file

@ -1,74 +0,0 @@
#include "stm32f4xx_hal.h"
#include "main.h"
#include "ILI9341.h"
#include <inttypes.h>
#include <string.h>
#include <stdarg.h>
#define LCD_W 320
#define LCD_H 240
void ili9341_cmd_write(uint16_t cmd, uint16_t cnt, ...)
{
va_list argp;
va_start(argp, cnt);
LCD_REG = cmd;
for(uint16_t n = 0; n < cnt; ++n) {
LCD_RAM = (uint16_t) va_arg(argp, uint32_t);
}
va_end(argp);
}
void ili9341_cmd_read(uint16_t cmd, uint16_t cnt, uint16_t *results)
{
LCD_REG = cmd;
for(uint16_t n = 0; n < cnt; ++n) {
*results++ = LCD_RAM;
}
}
void ili9341_init(void)
{
// volatile uint16_t id[5];
LCD_REG = ILI9341_RESET;
HAL_Delay(10);
LCD_REG = ILI9341_DISPLAY_OFF;
ili9341_cmd_write(ILI9341_POWERA, 5, 0x39, 0x2C, 0x00, 0x34, 0x02);
ili9341_cmd_write(ILI9341_POWERB, 3, 0x00, 0xC1, 0x30);
ili9341_cmd_write(ILI9341_DTCA, 3, 0x85, 0x00, 0x78);
ili9341_cmd_write(ILI9341_DTCB, 2, 0x00, 0x00);
ili9341_cmd_write(ILI9341_POWER_SEQ, 4, 0x64, 0x03, 0x12, 0x81);
ili9341_cmd_write(ILI9341_PRC, 1, 0x20);
ili9341_cmd_write(ILI9341_POWER1, 1, 0x23);
ili9341_cmd_write(ILI9341_POWER2, 1, 0x10);
ili9341_cmd_write(ILI9341_VCOM1, 2, 0x3E, 0x28);
ili9341_cmd_write(ILI9341_VCOM2, 1, 0x86);
ili9341_cmd_write(ILI9341_MAC, 1, 0xe8);
ili9341_cmd_write(ILI9341_PIXEL_FORMAT, 1, 0x55);
ili9341_cmd_write(ILI9341_FRC, 2, 0x00, 0x18);
ili9341_cmd_write(ILI9341_DFC, 3, 0x08, 0x82, 0x27);
ili9341_cmd_write(ILI9341_3GAMMA_EN, 1, 0x00);
ili9341_cmd_write(ILI9341_COLUMN_ADDR, 4, 0x00, 0x00, 0x01, 0x3F);
ili9341_cmd_write(ILI9341_PAGE_ADDR, 4, 0x00, 0x00, 0x00, 0xEF);
ili9341_cmd_write(ILI9341_GAMMA, 1, 0x01);
ili9341_cmd_write(ILI9341_PGAMMA, 15, 0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, 0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00);
ili9341_cmd_write(ILI9341_NGAMMA, 15, 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, 0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F);
LCD_REG = ILI9341_SLEEP_OUT;
HAL_Delay(100);
LCD_REG = ILI9341_DISPLAY_ON;
LCD_REG = ILI9341_GRAM;
uint16_t fillers[4] = { 0xf800, 0x7e0, 0x1f, 0xffff };
for(uint32_t y = 0; y < LCD_H; ++y) {
uint32_t x;
for(x = 0; x < LCD_W / 2; ++x) {
LCD_RAM = fillers[ (x&0x18) >> 3 ];
}
for( ; x < LCD_W; ++x) {
LCD_RAM = 0;
}
}
}

9
App/globals.c Normal file
View file

@ -0,0 +1,9 @@
/*
* globals.c
*
* Created on: Aug 29, 2019
* Author: abody
*/
#include "globals.h"

11
App/globals.h Normal file
View file

@ -0,0 +1,11 @@
/*
* globals.h
*
* Created on: Aug 29, 2019
* Author: abody
*/
#ifndef GLOBALS_H_
#define GLOBALS_H_
#endif /* GLOBALS_H_ */

View file

@ -1,14 +1,19 @@
#ifndef __ili9341_H
#define __ili9341_H
#ifdef __cplusplus
extern "C" {
#endif
#include <ili9341.h>
#include "stm32f4xx_hal.h"
#include "main.h"
#include <inttypes.h>
#include <string.h>
#include <stdarg.h>
#define LCD_L 320
#define LCD_S 240
#define LCD_W LCD_L
#define LCD_H LCD_S
#define LCD_REG (*((volatile unsigned short *) 0x60000000)) /* DC = 0 */
#define LCD_RAM (*((volatile unsigned short *) 0x60080000)) /* DC = 1 */
/* Includes ------------------------------------------------------------------*/
#define ILI9341_NOP 0x00
#define ILI9341_RESET 0x01
#define ILI9341_READ_DISPLAY_IDENTIFICATION_INFORMATION 0x04
@ -107,14 +112,151 @@
#define ILI9341_WHITE 0xFFFF /* 255, 255, 255 */
#define ILI9341_ORANGE 0xFD20 /* 255, 165, 0 */
#define ILI9341_GREENYELLOW 0xAFE5 /* 173, 255, 47 */
#define ILI9341_PINK 0xF81F
void ili9341_Reset(void);
void ili9341_writeData(char Data);
void ili9341_writeCommand(char Command);
void ili9341_init(void);
#define ILI9341_PINK 0xF81F
Ili9341Fsmc::Ili9341Fsmc(volatile uint16_t *reg, volatile uint16_t *ram,
DMA_TypeDef *dma, uint32_t dmaStream,
bool horizontal)
: m_reg(reg)
, m_ram(ram)
, m_dma(dma)
, m_dmaStream(dmaStream)
, m_width(horizontal ? LCD_L : LCD_S)
, m_height(horizontal ? LCD_S : LCD_L)
, m_rectX(0)
, m_rectY(0)
, m_rectWidth(m_width)
, m_rectHeight(m_height)
{
LL_DMA_EnableIT_TC(m_dma, m_dmaStream);
LL_DMA_EnableIT_TE(m_dma, m_dmaStream);
WriteCmd(ILI9341_RESET);
HAL_Delay(10);
WriteCmd(ILI9341_DISPLAY_OFF);
WriteCmd(ILI9341_POWERA, {0x39, 0x2C, 0x00, 0x34, 0x02});
WriteCmd(ILI9341_POWERB, {0x00, 0xC1, 0x30});
WriteCmd(ILI9341_DTCA, {0x85, 0x00, 0x78});
WriteCmd(ILI9341_DTCB, {0x00, 0x00});
WriteCmd(ILI9341_POWER_SEQ, {0x64, 0x03, 0x12, 0x81});
WriteCmd(ILI9341_PRC, 0x20);
WriteCmd(ILI9341_POWER1, 0x23);
WriteCmd(ILI9341_POWER2, 0x10);
WriteCmd(ILI9341_VCOM1, {0x3E, 0x28});
WriteCmd(ILI9341_VCOM2, 0x86);
WriteCmd(ILI9341_MAC, horizontal ? 0xe8 : 0x48);
WriteCmd(ILI9341_PIXEL_FORMAT, 0x55);
WriteCmd(ILI9341_FRC, {0x00, 0x18});
WriteCmd(ILI9341_DFC, {0x08, 0x82, 0x27});
WriteCmd(ILI9341_3GAMMA_EN, 0x00);
WriteCmd(ILI9341_COLUMN_ADDR, {0x00, 0x00, (uint16_t)((m_width-1) >> 8), (uint16_t)((m_width-1) & 0x00ff)});
WriteCmd(ILI9341_PAGE_ADDR, {0x00, 0x00, (uint16_t)((m_height-1) >> 8), (uint16_t)((m_height-1) & 0x00ff)});
WriteCmd(ILI9341_GAMMA, 0x01);
WriteCmd(ILI9341_PGAMMA, {0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, 0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00});
WriteCmd(ILI9341_NGAMMA, {0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, 0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F});
WriteCmd(ILI9341_SLEEP_OUT);
HAL_Delay(100);
WriteCmd(ILI9341_DISPLAY_ON);
}
void Ili9341Fsmc::WriteCmd(uint16_t cmd, std::initializer_list<uint16_t> params)
{
*m_reg = cmd;
for(uint16_t param : params)
*m_ram = param;
}
void Ili9341Fsmc::WriteCmd_(uint16_t cmd, uint16_t cnt, ...)
{
va_list argp;
va_start(argp, cnt);
*m_reg = cmd;
for(uint16_t n = 0; n < cnt; ++n) {
*m_ram = (uint16_t) va_arg(argp, uint32_t);
}
va_end(argp);
}
void Ili9341Fsmc::SetRect(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
{
if(x > m_width || y > m_height || !width || !height )
return;
WaitDmaIddle();
m_rectX = x;
m_rectY = y;
m_rectWidth = x + width > m_width ? m_width - x : width;
m_rectHeight = y + height > m_height ? m_height - x : height;
uint16_t right = x + m_rectWidth - 1;
uint16_t bottom = y + m_rectHeight - 1;
WriteCmd(ILI9341_COLUMN_ADDR, {(uint16_t)(x >> 8), (uint16_t)(x & 0xff), (uint16_t)(right >> 8), (uint16_t)(right & 0xff)});
WriteCmd(ILI9341_PAGE_ADDR, {(uint16_t)(y >> 8), (uint16_t)(y & 0xff), (uint16_t)(bottom >> 8), (uint16_t)(bottom & 0xff)});
}
void Ili9341Fsmc::FillRect(uint16_t color)
{
uint32_t count = m_rectWidth * m_rectHeight;
WaitDmaIddle();
m_dmaColor = color;
WriteCmd(ILI9341_GRAM);
LL_DMA_SetM2MDstAddress(m_dma, m_dmaStream, (uint32_t)m_ram);
LL_DMA_SetM2MSrcAddress(m_dma, m_dmaStream, (uint32_t) &m_dmaColor);
LL_DMA_SetMemoryIncMode(m_dma, m_dmaStream, LL_DMA_MEMORY_NOINCREMENT);
LL_DMA_SetPeriphIncMode(m_dma, m_dmaStream, LL_DMA_PERIPH_NOINCREMENT);
SetupDmaSize(count);
m_dmaEngineBusy = true;
LL_DMA_EnableStream(m_dma, m_dmaStream);
// for(uint32_t pix = 0; pix < count; ++pix)
// *m_ram = color;
}
void Ili9341Fsmc::FillRect(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t color)
{
SetRect(x, y, width, height);
FillRect(color);
}
void Ili9341Fsmc::SetupDmaSize(uint32_t size)
{
if(size > 0xffff) {
LL_DMA_SetDataLength(m_dma, m_dmaStream, 0xffff);
m_dmaRemainingPixels = size - 0xffff;
} else {
LL_DMA_SetDataLength(m_dma, m_dmaStream, size);
m_dmaRemainingPixels = 0;
}
}
void Ili9341Fsmc::DmaTransferComplete()
{
if(!m_dmaRemainingPixels)
m_dmaEngineBusy = false;
else {
SetupDmaSize(m_dmaRemainingPixels);
LL_DMA_EnableStream(m_dma, m_dmaStream);
}
}
void Ili9341Fsmc::Test()
{
FillRect(ILI9341_PINK);
WaitDmaIddle();
WriteCmd(ILI9341_GRAM);
uint16_t fillers[4] = { 0xf800, 0x7e0, 0x1f, 0xffff };
for(uint32_t y = 0; y < m_height; ++y) {
uint32_t x;
for(x = 0; x < m_width / 2; ++x) {
*m_ram = fillers[ (x&0x18) >> 3 ];
}
for( ; x < m_width; ++x) {
*m_ram = 0;
}
}
#ifdef __cplusplus
}
#endif
#endif

83
App/ili9341.h Normal file
View file

@ -0,0 +1,83 @@
#ifndef __ili9341_H
#define __ili9341_H
#include "singleton.h"
#include <inttypes.h>
#include <initializer_list>
#include "main.h"
// Color definitions
#define ILI9341_BLACK 0x0000 /* 0, 0, 0 */
#define ILI9341_NAVY 0x000F /* 0, 0, 128 */
#define ILI9341_DARKGREEN 0x03E0 /* 0, 128, 0 */
#define ILI9341_DARKCYAN 0x03EF /* 0, 128, 128 */
#define ILI9341_MAROON 0x7800 /* 128, 0, 0 */
#define ILI9341_PURPLE 0x780F /* 128, 0, 128 */
#define ILI9341_OLIVE 0x7BE0 /* 128, 128, 0 */
#define ILI9341_LIGHTGREY 0xC618 /* 192, 192, 192 */
#define ILI9341_DARKGREY 0x7BEF /* 128, 128, 128 */
#define ILI9341_BLUE 0x001F /* 0, 0, 255 */
#define ILI9341_GREEN 0x07E0 /* 0, 255, 0 */
#define ILI9341_CYAN 0x07FF /* 0, 255, 255 */
#define ILI9341_RED 0xF800 /* 255, 0, 0 */
#define ILI9341_MAGENTA 0xF81F /* 255, 0, 255 */
#define ILI9341_YELLOW 0xFFE0 /* 255, 255, 0 */
#define ILI9341_WHITE 0xFFFF /* 255, 255, 255 */
#define ILI9341_ORANGE 0xFD20 /* 255, 165, 0 */
#define ILI9341_GREENYELLOW 0xAFE5 /* 173, 255, 47 */
#define ILI9341_PINK 0xF81F
class Ili9341Fsmc : public Singleton<Ili9341Fsmc>
{
public:
Ili9341Fsmc(volatile uint16_t *reg, volatile uint16_t *ram,
DMA_TypeDef *dma, uint32_t dmaStream,
bool horizontal = true);
void FillRect(uint16_t color);
void FillRect(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t color);
uint16_t Width() { return m_width; }
uint16_t Height() { return m_height; }
inline void WaitDmaIddle() { while(m_dmaEngineBusy); }
void Test();
private:
inline void WriteCmd(uint16_t cmd) { *m_reg = cmd; }
inline void WriteCmd(uint16_t cmd, uint16_t param) { *m_reg = cmd; *m_ram = param; }
void WriteCmd(uint16_t cmd, std::initializer_list<uint16_t> params);
void WriteCmd_(uint16_t cmd, uint16_t cnt, ...);
void SetRect(uint16_t x, uint16_t y, uint16_t width, uint16_t height);
volatile uint16_t * const m_reg;
volatile uint16_t * const m_ram;
DMA_TypeDef *m_dma;
uint32_t m_dmaStream;
friend void HandleLcdDmaIrq();
void SetupDmaSize(uint32_t size);
void DmaTransferComplete();
bool m_dmaEngineBusy = false;
uint32_t m_dmaRemainingPixels = 0;
uint16_t m_dmaColor = 0;
uint16_t const m_width;
uint16_t const m_height;
uint16_t m_rectX;
uint16_t m_rectY;
uint16_t m_rectWidth;
uint16_t m_rectHeight;
};
/* Includes ------------------------------------------------------------------*/
void ili9341_Reset(void);
void ili9341_writeData(char Data);
void ili9341_writeCommand(char Command);
void ili9341_init(void);
#endif

View file

@ -0,0 +1,21 @@
/*
* interrupt.c
*
* Created on: Aug 29, 2019
* Author: abody
*/
#include <interrupt_handlers.h>
#include "main.h"
#include "globals.h"
#include "ili9341.h"
void HandleLcdDmaIrq()
{
// DMA2 Stream4
if(LL_DMA_IsActiveFlag_TC4(DMA2)) {
LL_DMA_DisableStream(DMA2, LL_DMA_STREAM_4);
LL_DMA_ClearFlag_TC4(DMA2);
Ili9341Fsmc::Instance().DmaTransferComplete();
} else if(LL_DMA_IsActiveFlag_TE4(DMA2))
LL_DMA_ClearFlag_TE4(DMA2);
}

21
App/interrupt_handlers.h Normal file
View file

@ -0,0 +1,21 @@
/*
* interrupt.h
*
* Created on: Aug 29, 2019
* Author: abody
*/
#ifndef INTERRUPT_HANDLERS_H_
#define INTERRUPT_HANDLERS_H_
#ifdef __cplusplus
extern "C" {
#endif
void HandleLcdDmaIrq();
#ifdef __cplusplus
}
#endif
#endif /* INTERRUPT_HANDLERS_H_ */

57
App/mainloop.cpp Normal file
View file

@ -0,0 +1,57 @@
/*
* mainloop.cpp
*
* Created on: Sep 11, 2019
* Author: abody
*/
#include <ili9341.h>
#include "main.h"
#include <mainloop.h>
#include <initializer_list>
#define LCD_REG 0x60000000 /* DC = 0 */
#define LCD_RAM 0x60080000 /* DC = 1 */
#define BORDER 60
#define BARWIDTH 2
void MainLoop()
{
uint16_t colors[] = {
ILI9341_RED, ILI9341_ORANGE, ILI9341_YELLOW, ILI9341_GREENYELLOW,
ILI9341_GREEN, ILI9341_CYAN, ILI9341_BLUE, ILI9341_MAGENTA
};
static uint16_t const colorCount = sizeof(colors)/sizeof(colors[0]);
uint32_t lastTick = 0;
uint32_t tmpTick;
uint32_t lastDiff;
//LL_SYSTICK_EnableIT();
Ili9341Fsmc &lcd(Ili9341Fsmc::Instance((volatile unsigned short *) LCD_REG, (volatile unsigned short *) LCD_RAM, DMA2, LL_DMA_STREAM_4, true));
lcd.FillRect(ILI9341_WHITE);
lcd.FillRect(BORDER, BORDER, lcd.Width()- BORDER * 2, lcd.Height()-BORDER * 2, ILI9341_BLACK);
uint16_t offset = 0;
uint16_t maxidx = (lcd.Width() - BARWIDTH) / BARWIDTH;
for(;;) {
uint16_t idx = 0;
while(idx <= maxidx ) {
lcd.FillRect(idx * BARWIDTH, 0, BARWIDTH, lcd.Height(), colors[(idx+offset) % colorCount]);
++idx;
}
offset = (offset+1)%colorCount;
tmpTick = HAL_GetTick();
lastDiff = tmpTick - lastTick;
do tmpTick = HAL_GetTick(); while(tmpTick - lastTick < 20);
lastTick = tmpTick;
// LL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
// LL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
// LL_mDelay(25);
}
}
MainLoop::MainLoop()
{
}

27
App/mainloop.h Normal file
View file

@ -0,0 +1,27 @@
/*
* mainloop.h
*
* Created on: Sep 11, 2019
* Author: abody
*/
#ifndef MAINLOOP_H_
#define MAINLOOP_H_
#if defined(__cplusplus)
extern "C" {
#endif
void MainLoop();
#if defined(__cplusplus)
class MainLoop {
MainLoop();
};
#endif // __cplusplus
#if defined(__cplusplus)
}
#endif
#endif /* MAINLOOP_H_ */

35
App/singleton.h Normal file
View file

@ -0,0 +1,35 @@
/*
* singleton.h
*
* Created on: Sep 11, 2019
* Author: compi
*/
#ifndef SINGLETON_H_
#define SINGLETON_H_
#include <utility>
template<typename T> class Singleton {
public:
static T &Instance() { return *m_instance; }
template<typename ... Args>
static T &Instance(Args &&... args)
{
static T instance{ std::forward<Args>(args)... };
if(!m_instance)
m_instance = &instance;
return instance;
}
protected:
Singleton() = default;
Singleton(const Singleton &) = delete;
Singleton &operator=(const Singleton &) = delete;
virtual ~Singleton() = default;
static T *m_instance;
};
template<typename T> T* Singleton<T>::m_instance = nullptr;
#endif /* SINGLETON_H_ */