prevent multiple initialization memory allocation and inform user if allocation was fail

This commit is contained in:
Zaltora 2018-03-27 10:56:46 +02:00
parent a89417e26e
commit 6dde352842
2 changed files with 20 additions and 6 deletions

View file

@ -117,7 +117,7 @@ static inline void init_descriptors_list(uint8_t *buf, uint32_t total_dma_data_s
} }
} }
void ws2812_i2s_init(uint32_t pixels_number, pixeltype_t type) int ws2812_i2s_init(uint32_t pixels_number, pixeltype_t type)
{ {
dma_buffer_size = pixels_number * type; dma_buffer_size = pixels_number * type;
dma_block_list_size = dma_buffer_size / MAX_DMA_BLOCK_SIZE; dma_block_list_size = dma_buffer_size / MAX_DMA_BLOCK_SIZE;
@ -130,11 +130,24 @@ void ws2812_i2s_init(uint32_t pixels_number, pixeltype_t type)
debug("allocating %d dma blocks\n", dma_block_list_size); debug("allocating %d dma blocks\n", dma_block_list_size);
if(!dma_block_list)
{
dma_block_list = (dma_descriptor_t*)malloc( dma_block_list = (dma_descriptor_t*)malloc(
dma_block_list_size * sizeof(dma_descriptor_t)); dma_block_list_size * sizeof(dma_descriptor_t));
if(!dma_block_list)
{
return -1;
}
}
debug("allocating %d bytes for DMA buffer\n", dma_buffer_size); debug("allocating %d bytes for DMA buffer\n", dma_buffer_size);
if(!dma_buffer)
{
dma_buffer = malloc(dma_buffer_size); dma_buffer = malloc(dma_buffer_size);
if(!dma_buffer)
{
return -1;
}
}
memset(dma_buffer, 0xFA, dma_buffer_size); memset(dma_buffer, 0xFA, dma_buffer_size);
init_descriptors_list(dma_buffer, dma_buffer_size); init_descriptors_list(dma_buffer, dma_buffer_size);
@ -146,6 +159,7 @@ void ws2812_i2s_init(uint32_t pixels_number, pixeltype_t type)
clock_div.bclk_div, clock_div.clkm_div); clock_div.bclk_div, clock_div.clkm_div);
i2s_dma_init(dma_isr_handler, NULL, clock_div, i2s_pins); i2s_dma_init(dma_isr_handler, NULL, clock_div, i2s_pins);
return 0;
} }
const IRAM_DATA int16_t bitpatterns[16] = const IRAM_DATA int16_t bitpatterns[16] =

View file

@ -53,7 +53,7 @@ typedef enum {
* *
* @param pixels_number Number of pixels in the strip. * @param pixels_number Number of pixels in the strip.
*/ */
void ws2812_i2s_init(uint32_t pixels_number, pixeltype_t type); int ws2812_i2s_init(uint32_t pixels_number, pixeltype_t type);
/** /**
* Update ws2812 pixels. * Update ws2812 pixels.