6 #include "soc/i2s_struct.h" 7 #include "soc/i2s_reg.h" 8 #include "driver/periph_ctrl.h" 17 #pragma GCC optimize ("O2") 25 volatile int VGATextController::s_scanLine;
26 uint32_t VGATextController::s_blankPatternDWord;
27 uint32_t * VGATextController::s_fgbgPattern =
nullptr;
28 int VGATextController::s_textRow;
29 bool VGATextController::s_upperRow;
30 lldesc_t
volatile * VGATextController::s_frameResetDesc;
32 #if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK 33 volatile uint64_t s_vgatxtcycles = 0;
40 VGATextController::VGATextController()
41 : m_charData(nullptr),
43 m_cursorEnabled(false),
48 m_cursorForeground(0),
49 m_cursorBackground(15)
54 VGATextController::~VGATextController()
56 free((
void*)m_charData);
63 while (m_map !=
nullptr && s_scanLine < m_timings.
VVisibleArea)
73 *columns = VGATextController_COLUMNS;
74 if (*rows > VGATextController_ROWS)
75 *rows = VGATextController_ROWS;
80 void VGATextController::init(gpio_num_t VSyncGPIO)
82 m_DMABuffers =
nullptr;
87 auto font = getFont();
88 int charDataSize = 256 * font->height * ((font->width + 7) / 8);
89 m_charData = (uint8_t*) heap_caps_malloc(charDataSize, MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
90 memcpy(m_charData, font->data, charDataSize);
95 void VGATextController::begin(gpio_num_t redGPIO, gpio_num_t greenGPIO, gpio_num_t blueGPIO, gpio_num_t HSyncGPIO, gpio_num_t VSyncGPIO)
100 setupGPIO(redGPIO, VGA_RED_BIT, GPIO_MODE_OUTPUT);
101 setupGPIO(greenGPIO, VGA_GREEN_BIT, GPIO_MODE_OUTPUT);
102 setupGPIO(blueGPIO, VGA_BLUE_BIT, GPIO_MODE_OUTPUT);
105 setupGPIO(HSyncGPIO, VGA_HSYNC_BIT, GPIO_MODE_OUTPUT);
106 setupGPIO(VSyncGPIO, VGA_VSYNC_BIT, GPIO_MODE_INPUT_OUTPUT);
108 RGB222::lowBitOnly =
true;
109 m_bitsPerChannel = 1;
114 void VGATextController::begin(gpio_num_t red1GPIO, gpio_num_t red0GPIO, gpio_num_t green1GPIO, gpio_num_t green0GPIO, gpio_num_t blue1GPIO, gpio_num_t blue0GPIO, gpio_num_t HSyncGPIO, gpio_num_t VSyncGPIO)
116 begin(red0GPIO, green0GPIO, blue0GPIO, HSyncGPIO, VSyncGPIO);
119 setupGPIO(red1GPIO, VGA_RED_BIT + 1, GPIO_MODE_OUTPUT);
120 setupGPIO(green1GPIO, VGA_GREEN_BIT + 1, GPIO_MODE_OUTPUT);
121 setupGPIO(blue1GPIO, VGA_BLUE_BIT + 1, GPIO_MODE_OUTPUT);
123 RGB222::lowBitOnly =
false;
124 m_bitsPerChannel = 2;
131 begin(GPIO_NUM_22, GPIO_NUM_21, GPIO_NUM_19, GPIO_NUM_18, GPIO_NUM_5, GPIO_NUM_4, GPIO_NUM_23, GPIO_NUM_15);
135 void VGATextController::setCursorForeground(
Color value)
137 m_cursorForeground = (int) value;
141 void VGATextController::setCursorBackground(
Color value)
143 m_cursorBackground = (int) value;
147 void VGATextController::setupGPIO(gpio_num_t gpio,
int bit, gpio_mode_t mode)
149 configureGPIO(gpio, mode);
150 gpio_matrix_out(gpio, I2S1O_DATA_OUT0_IDX + bit,
false,
false);
157 if (VGABaseController::convertModelineToTimings(VGATextController_MODELINE, &timings))
175 m_HVSync = packHVSync(
false,
false);
179 m_DMABuffers = (lldesc_t*) heap_caps_malloc(m_DMABuffersCount *
sizeof(lldesc_t), MALLOC_CAP_DMA);
181 m_lines = (uint32_t*) heap_caps_malloc(VGATextController_CHARHEIGHT * VGATextController_WIDTH, MALLOC_CAP_DMA);
184 m_blankLine = (uint8_t*) heap_caps_malloc(rawLineWidth, MALLOC_CAP_DMA);
185 m_syncLine = (uint8_t*) heap_caps_malloc(rawLineWidth, MALLOC_CAP_DMA);
194 for (
int i = 0, visLine = 0, invLine = 0; i < m_DMABuffersCount; ++i) {
199 m_DMABuffers[i].eof = (visLine == 0 || visLine == VGATextController_CHARHEIGHT / 2 ? 1 : 0);
200 m_DMABuffers[i].sosf = 0;
201 m_DMABuffers[i].offset = 0;
202 m_DMABuffers[i].owner = 1;
203 m_DMABuffers[i].qe.stqe_next = (lldesc_t*) &m_DMABuffers[i + 1];
205 m_DMABuffers[i].size = (m_DMABuffers[i].length + 3) & (~3);
206 m_DMABuffers[i].buf = (uint8_t*) m_blankLine;
211 m_DMABuffers[i].eof = 0;
212 m_DMABuffers[i].sosf = 0;
213 m_DMABuffers[i].offset = 0;
214 m_DMABuffers[i].owner = 1;
215 m_DMABuffers[i].qe.stqe_next = (lldesc_t*) &m_DMABuffers[i + 1];
217 m_DMABuffers[i].size = (m_DMABuffers[i].length + 3) & (~3);
218 m_DMABuffers[i].buf = (uint8_t*)(m_lines) + visLine * VGATextController_WIDTH;
221 if (visLine == VGATextController_CHARHEIGHT)
228 bool frameResetDesc = (invLine == 0);
231 s_frameResetDesc = &m_DMABuffers[i];
233 m_DMABuffers[i].eof = (frameResetDesc ? 1 : 0);
234 m_DMABuffers[i].sosf = 0;
235 m_DMABuffers[i].offset = 0;
236 m_DMABuffers[i].owner = 1;
237 m_DMABuffers[i].qe.stqe_next = (lldesc_t*) (i == m_DMABuffersCount - 1 ? &m_DMABuffers[0] : &m_DMABuffers[i + 1]);
238 m_DMABuffers[i].length = rawLineWidth;
239 m_DMABuffers[i].size = (m_DMABuffers[i].length + 3) & (~3);
242 m_DMABuffers[i].buf = (uint8_t*) m_blankLine;
244 m_DMABuffers[i].buf = (uint8_t*) m_syncLine;
255 s_blankPatternDWord = m_HVSync | (m_HVSync << 8) | (m_HVSync << 16) | (m_HVSync << 24);
257 if (s_fgbgPattern ==
nullptr) {
258 s_fgbgPattern = (uint32_t*) heap_caps_malloc(16384, MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
259 for (
int i = 0; i < 16; ++i)
260 for (
int fg = 0; fg < 16; ++fg)
261 for (
int bg = 0; bg < 16; ++bg) {
262 uint8_t fg_pat = preparePixel(RGB222((
Color)fg));
263 uint8_t bg_pat = preparePixel(RGB222((
Color)bg));
264 s_fgbgPattern[i | (bg << 4) | (fg << 8)] = (i & 0b1000 ? (fg_pat << 16) : (bg_pat << 16)) |
265 (i & 0b0100 ? (fg_pat << 24) : (bg_pat << 24)) |
266 (i & 0b0010 ? (fg_pat << 0) : (bg_pat << 0)) |
267 (i & 0b0001 ? (fg_pat << 8) : (bg_pat << 8));
276 m_GPIOStream.play(m_timings.
frequency, m_DMABuffers);
278 I2S1.int_clr.val = 0xFFFFFFFF;
279 I2S1.int_ena.out_eof = 1;
283 void VGATextController::freeBuffers()
285 heap_caps_free( (
void*) m_DMABuffers );
286 heap_caps_free((
void*) m_lines);
287 m_DMABuffers =
nullptr;
288 heap_caps_free((
void*) m_blankLine);
289 heap_caps_free((
void*) m_syncLine);
293 uint8_t IRAM_ATTR VGATextController::packHVSync(
bool HSync,
bool VSync)
295 uint8_t hsync_value = (m_timings.
HSyncLogic ==
'+' ? (HSync ? 1 : 0) : (HSync ? 0 : 1));
296 uint8_t vsync_value = (m_timings.
VSyncLogic ==
'+' ? (VSync ? 1 : 0) : (VSync ? 0 : 1));
297 return (vsync_value << VGA_VSYNC_BIT) | (hsync_value << VGA_HSYNC_BIT);
301 uint8_t IRAM_ATTR VGATextController::preparePixelWithSync(RGB222 rgb,
bool HSync,
bool VSync)
303 return packHVSync(HSync, VSync) | (rgb.B << VGA_BLUE_BIT) | (rgb.G << VGA_GREEN_BIT) | (rgb.R << VGA_RED_BIT);
307 void VGATextController::fillDMABuffers()
311 VGA_PIXELINROW(m_blankLine, x) = preparePixelWithSync((RGB222){0, 0, 0},
false,
false);
312 VGA_PIXELINROW(m_syncLine, x) = preparePixelWithSync((RGB222){0, 0, 0},
false,
true);
315 VGA_PIXELINROW(m_blankLine, x) = preparePixelWithSync((RGB222){0, 0, 0},
true,
false);
316 VGA_PIXELINROW(m_syncLine, x) = preparePixelWithSync((RGB222){0, 0, 0},
true,
true);
319 VGA_PIXELINROW(m_blankLine, x) = preparePixelWithSync((RGB222){0, 0, 0},
false,
false);
320 VGA_PIXELINROW(m_syncLine, x) = preparePixelWithSync((RGB222){0, 0, 0},
false,
true);
323 for (
int rx = 0; x < rawLineWidth; ++x, ++rx) {
324 VGA_PIXELINROW(m_blankLine, x) = preparePixelWithSync((RGB222){0, 0, 0},
false,
false);
325 VGA_PIXELINROW(m_syncLine, x) = preparePixelWithSync((RGB222){0, 0, 0},
false,
true);
326 for (
int i = 0; i < VGATextController_CHARHEIGHT; ++i)
327 VGA_PIXELINROW( ((uint8_t*)(m_lines) + i * VGATextController_WIDTH), rx) = preparePixelWithSync((RGB222){0, 0, 0},
false,
false);
332 void IRAM_ATTR VGATextController::ISRHandler(
void * arg)
334 #if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK 335 auto s1 = getCycleCount();
338 VGATextController * ctrl = (VGATextController *) arg;
340 if (I2S1.int_st.out_eof && ctrl->m_charData !=
nullptr) {
342 auto desc = (
volatile lldesc_t*) I2S1.out_eof_des_addr;
344 if (desc == s_frameResetDesc) {
350 if (ctrl->m_cursorEnabled) {
351 ++ctrl->m_cursorCounter;
352 if (ctrl->m_cursorCounter >= ctrl->m_cursorSpeed)
353 ctrl->m_cursorCounter = -ctrl->m_cursorSpeed;
356 if (ctrl->m_map ==
nullptr) {
357 I2S1.int_clr.val = I2S1.int_st.val;
358 #if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK 359 s_vgatxtcycles += getCycleCount() - s1;
364 }
else if (s_scanLine == 0) {
366 I2S1.int_clr.val = I2S1.int_st.val;
367 #if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK 368 s_vgatxtcycles += getCycleCount() - s1;
373 int scanLine = s_scanLine;
375 const int lineIndex = scanLine % VGATextController_CHARHEIGHT;
377 auto lines = ctrl->m_lines;
379 if (s_textRow < ctrl->m_rows) {
383 const auto cursorVisible = (ctrl->m_cursorEnabled && ctrl->m_cursorCounter >= 0 && s_textRow == ctrl->m_cursorRow);
385 cursorCol = ctrl->m_cursorCol;
386 cursorFGBG = (ctrl->m_cursorForeground << 4) | (ctrl->m_cursorBackground << 8);
389 const auto charData = ctrl->m_charData + (s_upperRow ? 0 : VGATextController_CHARHEIGHT / 2);
390 auto mapItemPtr = ctrl->m_map + s_textRow * VGATextController_COLUMNS;
392 for (
int col = 0; col < VGATextController_COLUMNS; ++col, ++mapItemPtr) {
394 const auto mapItem = *mapItemPtr;
396 int fgbg = (mapItem >> 4) & 0b111111110000;
398 const auto options = glyphMapItem_getOptions(mapItem);
402 fgbg = ((fgbg >> 4) & 0b11110000) | ((fgbg << 4) & 0b111100000000);
405 if (cursorVisible && col == cursorCol)
408 uint32_t * dest = lines + lineIndex * VGATextController_WIDTH /
sizeof(uint32_t) + col * VGATextController_CHARWIDTHBYTES * 2;
413 for (
int rowInChar = 0; rowInChar < VGATextController_CHARHEIGHT / 2; ++rowInChar) {
414 int v = s_fgbgPattern[fgbg];
417 dest += VGATextController_WIDTH /
sizeof(uint32_t);
422 const bool underline = (s_upperRow ==
false && options.underline);
423 const bool bold = options.bold;
425 auto charRowPtr = charData + glyphMapItem_getIndex(mapItem) * VGATextController_CHARHEIGHT * VGATextController_CHARWIDTHBYTES;
427 for (
int rowInChar = 0; rowInChar < VGATextController_CHARHEIGHT / 2; ++rowInChar) {
428 auto charRowData = *charRowPtr;
432 charRowData |= charRowData >> 1;
434 *dest = s_fgbgPattern[(charRowData >> 4) | fgbg];
435 *(dest + 1) = s_fgbgPattern[(charRowData & 0xF) | fgbg];
437 dest += VGATextController_WIDTH /
sizeof(uint32_t);
438 charRowPtr += VGATextController_CHARWIDTHBYTES;
443 dest -= VGATextController_WIDTH /
sizeof(uint32_t);
444 uint32_t v = s_fgbgPattern[0xF | fgbg];
461 for (
int i = 0; i < VGATextController_CHARHEIGHT / 2; ++i) {
462 auto dest = lines + ((scanLine + i) % VGATextController_CHARHEIGHT) * VGATextController_WIDTH /
sizeof(uint32_t);
463 for (
int i = 0; i < VGATextController_COLUMNS; ++i) {
464 *dest++ = s_blankPatternDWord;
465 *dest++ = s_blankPatternDWord;
470 scanLine += VGATextController_CHARHEIGHT / 2;
472 s_scanLine = scanLine;
476 #if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK 477 s_vgatxtcycles += getCycleCount() - s1;
480 I2S1.int_clr.val = I2S1.int_st.val;
void begin()
This is the 64 colors (8 GPIOs) initializer using default pinout.
This file contains fabgl::GPIOStream definition.
Color
This enum defines named colors.
void adjustMapSize(int *columns, int *rows)
Adjust columns and rows to the controller limits.
This file contains fabgl::VGATextController definition.
#define FABGLIB_VIDEO_CPUINTENSIVE_TASKS_CORE
void setResolution(char const *modeline=nullptr, int viewPortWidth=-1, int viewPortHeight=-1, bool doubleBuffered=false)
Sets fixed resolution.
Specifies the VGA timings. This is a modeline decoded.
This file contains some utility classes and functions.
void setTextMap(uint32_t const *map, int rows)
Sets text map to display.