Memory allocation changes

Move malloc from static class constructor to begin(). Possible fix for #3
This commit is contained in:
mrfaptastic 2019-01-03 23:09:00 +00:00
parent 6445a566fe
commit 47a79db50f
2 changed files with 17 additions and 10 deletions

View file

@ -15,7 +15,7 @@ void RGB64x32MatrixPanel_I2S_DMA::configureDMA(int r1_pin, int g1_pin, int b1_
} }
int ramrequired = numDescriptorsPerRow * ROWS_PER_FRAME * ESP32_NUM_FRAME_BUFFERS * sizeof(lldesc_t); int ramrequired = numDescriptorsPerRow * ROWS_PER_FRAME * ESP32_NUM_FRAME_BUFFERS * sizeof(lldesc_t);
ramrequired += 100000; // HACK Hard Coded: Keep at least 100k free! ramrequired += 64000; // HACK Hard Coded: Keep at least 64k free!
int largestblockfree = heap_caps_get_largest_free_block(MALLOC_CAP_DMA); int largestblockfree = heap_caps_get_largest_free_block(MALLOC_CAP_DMA);
Serial.printf("lsbMsbTransitionBit of %d requires %d RAM, %d available, leaving %d free: \r\n", lsbMsbTransitionBit, ramrequired, largestblockfree, largestblockfree - ramrequired); Serial.printf("lsbMsbTransitionBit of %d requires %d RAM, %d available, leaving %d free: \r\n", lsbMsbTransitionBit, ramrequired, largestblockfree, largestblockfree - ramrequired);
@ -61,7 +61,7 @@ void RGB64x32MatrixPanel_I2S_DMA::configureDMA(int r1_pin, int g1_pin, int b1_
Serial.printf("lsbMsbTransitionBit of %d gives %d Hz refresh: \r\n", lsbMsbTransitionBit, actualRefreshRate); Serial.printf("lsbMsbTransitionBit of %d gives %d Hz refresh: \r\n", lsbMsbTransitionBit, actualRefreshRate);
if (actualRefreshRate > 250) // HACK Hard Coded: Minimum frame rate of 160 if (actualRefreshRate > 150) // HACK Hard Coded: Minimum frame rate of 150
break; break;
@ -252,13 +252,13 @@ void RGB64x32MatrixPanel_I2S_DMA::updateMatrixDMABuffer(int16_t x_coord, int16_t
/* When using the Adafruit drawPixel, we only have one pixel co-ordinate and color to draw (duh) /* When using the Adafruit drawPixel, we only have one pixel co-ordinate and colour to draw (duh)
* so we can't paint a top and bottom half (or whatever row split the panel is) at the same time. * so we can't paint a top and bottom half (or whatever row split the panel is) at the same time.
* Need to be smart and check the DMA buffer to see what the other half thinks (pun intended) * Need to be smart and check the DMA buffer to see what the other half thinks (pun intended)
* and persis this when we refresh. * and persist this when we refresh.
* *
* The DMA buffer order has also been reversed (fer to the last code in this function) * The DMA buffer order has also been reversed (fer to the last code in this function)
* so we have to check for this and check ther correct possition of the MATRIX_DATA_STORAGE_TYPE * so we have to check for this and check the correct position of the MATRIX_DATA_STORAGE_TYPE
* data. * data.
*/ */
int16_t tmp_x_coord = x_coord; int16_t tmp_x_coord = x_coord;

View file

@ -119,7 +119,7 @@
#define COLOR_CHANNELS_PER_PIXEL 3 #define COLOR_CHANNELS_PER_PIXEL 3
#define PIXELS_PER_LATCH ((MATRIX_WIDTH * MATRIX_HEIGHT) / MATRIX_HEIGHT) // = 64 #define PIXELS_PER_LATCH ((MATRIX_WIDTH * MATRIX_HEIGHT) / MATRIX_HEIGHT) // = 64
#define COLOR_DEPTH_BITS (COLOR_DEPTH/COLOR_CHANNELS_PER_PIXEL) // = 8 #define COLOR_DEPTH_BITS (COLOR_DEPTH/COLOR_CHANNELS_PER_PIXEL) // = 8
#define ROWS_PER_FRAME (MATRIX_HEIGHT/MATRIX_ROWS_IN_PARALLEL) // = 2 #define ROWS_PER_FRAME (MATRIX_HEIGHT/MATRIX_ROWS_IN_PARALLEL) // = 16
/***************************************************************************************/ /***************************************************************************************/
/* You really don't want to change this stuff */ /* You really don't want to change this stuff */
@ -137,7 +137,7 @@
// note: sizeof(data) must be multiple of 32 bits, as ESP32 DMA linked list buffer address pointer must be word-aligned. // note: sizeof(data) must be multiple of 32 bits, as ESP32 DMA linked list buffer address pointer must be word-aligned.
struct rowBitStruct { struct rowBitStruct {
MATRIX_DATA_STORAGE_TYPE data[((MATRIX_WIDTH * MATRIX_HEIGHT) / 32) + CLKS_DURING_LATCH]; MATRIX_DATA_STORAGE_TYPE data[((MATRIX_WIDTH * MATRIX_HEIGHT) / MATRIX_HEIGHT) + CLKS_DURING_LATCH];
// this evaluates to just MATRIX_DATA_STORAGE_TYPE data[64] really; // this evaluates to just MATRIX_DATA_STORAGE_TYPE data[64] really;
// and array of 64 uint16_t's // and array of 64 uint16_t's
}; };
@ -168,8 +168,7 @@ class RGB64x32MatrixPanel_I2S_DMA : public Adafruit_GFX {
public: public:
RGB64x32MatrixPanel_I2S_DMA(bool _doubleBuffer = false) // doublebuffer always enabled, option makes no difference RGB64x32MatrixPanel_I2S_DMA(bool _doubleBuffer = false) // doublebuffer always enabled, option makes no difference
: Adafruit_GFX(MATRIX_WIDTH, MATRIX_HEIGHT), doubleBuffer(_doubleBuffer) { : Adafruit_GFX(MATRIX_WIDTH, MATRIX_HEIGHT), doubleBuffer(_doubleBuffer) {
allocateDMAbuffers();
backbuf_id = 0; backbuf_id = 0;
brightness = 32; // default to max brightness, wear sunglasses when looking directly at panel. brightness = 32; // default to max brightness, wear sunglasses when looking directly at panel.
@ -179,6 +178,11 @@ class RGB64x32MatrixPanel_I2S_DMA : public Adafruit_GFX {
void begin(int dma_r1_pin = R1_PIN_DEFAULT , int dma_g1_pin = G1_PIN_DEFAULT, int dma_b1_pin = B1_PIN_DEFAULT , int dma_r2_pin = R2_PIN_DEFAULT , int dma_g2_pin = G2_PIN_DEFAULT , int dma_b2_pin = B2_PIN_DEFAULT , int dma_a_pin = A_PIN_DEFAULT , int dma_b_pin = B_PIN_DEFAULT , int dma_c_pin = C_PIN_DEFAULT , int dma_d_pin = D_PIN_DEFAULT , int dma_e_pin = E_PIN_DEFAULT , int dma_lat_pin = LAT_PIN_DEFAULT, int dma_oe_pin = OE_PIN_DEFAULT , int dma_clk_pin = CLK_PIN_DEFAULT) void begin(int dma_r1_pin = R1_PIN_DEFAULT , int dma_g1_pin = G1_PIN_DEFAULT, int dma_b1_pin = B1_PIN_DEFAULT , int dma_r2_pin = R2_PIN_DEFAULT , int dma_g2_pin = G2_PIN_DEFAULT , int dma_b2_pin = B2_PIN_DEFAULT , int dma_a_pin = A_PIN_DEFAULT , int dma_b_pin = B_PIN_DEFAULT , int dma_c_pin = C_PIN_DEFAULT , int dma_d_pin = D_PIN_DEFAULT , int dma_e_pin = E_PIN_DEFAULT , int dma_lat_pin = LAT_PIN_DEFAULT, int dma_oe_pin = OE_PIN_DEFAULT , int dma_clk_pin = CLK_PIN_DEFAULT)
{ {
/* As DMA buffers are dynamically allocated, we must allocated in begin()
* Ref: https://github.com/espressif/arduino-esp32/issues/831
*/
allocateDMAbuffers();
// Change 'if' to '1' to enable, 0 to not include this Serial output in compiled program // Change 'if' to '1' to enable, 0 to not include this Serial output in compiled program
#if 1 #if 1
Serial.printf("Using pin %d for the R1_PIN\n", dma_r1_pin); Serial.printf("Using pin %d for the R1_PIN\n", dma_r1_pin);
@ -245,7 +249,7 @@ class RGB64x32MatrixPanel_I2S_DMA : public Adafruit_GFX {
void allocateDMAbuffers() void allocateDMAbuffers()
{ {
matrixUpdateFrames = (frameStruct *)heap_caps_malloc(sizeof(frameStruct) * ESP32_NUM_FRAME_BUFFERS, MALLOC_CAP_DMA); matrixUpdateFrames = (frameStruct *)heap_caps_malloc(sizeof(frameStruct) * ESP32_NUM_FRAME_BUFFERS, MALLOC_CAP_DMA);
Serial.printf("Allocating Refresh Buffer:\r\nDMA Memory Available: %d bytes total, %d bytes largest free block: \r\n", heap_caps_get_free_size(MALLOC_CAP_DMA), heap_caps_get_largest_free_block(MALLOC_CAP_DMA)); Serial.printf("Allocating DMA Refresh Buffer...\r\nTotal DMA Memory available: %d bytes total. Largest free block: %d bytes\r\n", heap_caps_get_free_size(MALLOC_CAP_DMA), heap_caps_get_largest_free_block(MALLOC_CAP_DMA));
} // end initMatrixDMABuffer() } // end initMatrixDMABuffer()
@ -255,7 +259,10 @@ class RGB64x32MatrixPanel_I2S_DMA : public Adafruit_GFX {
// Need to wipe the contents of the matrix buffers or weird things happen. // Need to wipe the contents of the matrix buffers or weird things happen.
for (int y=0;y<MATRIX_HEIGHT; y++) for (int y=0;y<MATRIX_HEIGHT; y++)
for (int x=0;x<MATRIX_WIDTH; x++) for (int x=0;x<MATRIX_WIDTH; x++)
{
//Serial.printf("\r\nFlushing x, y coord %d, %d", x, y);
updateMatrixDMABuffer( x, y, 0, 0, 0); updateMatrixDMABuffer( x, y, 0, 0, 0);
}
} }
void configureDMA(int r1_pin, int g1_pin, int b1_pin, int r2_pin, int g2_pin, int b2_pin, int a_pin, int b_pin, int c_pin, int d_pin, int e_pin, int lat_pin, int oe_pin, int clk_pin); // Get everything setup. Refer to the .c file void configureDMA(int r1_pin, int g1_pin, int b1_pin, int r2_pin, int g2_pin, int b2_pin, int a_pin, int b_pin, int c_pin, int d_pin, int e_pin, int lat_pin, int oe_pin, int clk_pin); // Get everything setup. Refer to the .c file