From 6dde352842bcd6ec20c831353021dc2d175ad10a Mon Sep 17 00:00:00 2001 From: Zaltora Date: Tue, 27 Mar 2018 10:56:46 +0200 Subject: [PATCH] prevent multiple initialization memory allocation and inform user if allocation was fail --- extras/ws2812_i2s/ws2812_i2s.c | 24 +++++++++++++++++++----- extras/ws2812_i2s/ws2812_i2s.h | 2 +- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/extras/ws2812_i2s/ws2812_i2s.c b/extras/ws2812_i2s/ws2812_i2s.c index 62dc496..88caf27 100644 --- a/extras/ws2812_i2s/ws2812_i2s.c +++ b/extras/ws2812_i2s/ws2812_i2s.c @@ -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_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); - dma_block_list = (dma_descriptor_t*)malloc( - dma_block_list_size * sizeof(dma_descriptor_t)); - + if(!dma_block_list) + { + dma_block_list = (dma_descriptor_t*)malloc( + 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); - dma_buffer = malloc(dma_buffer_size); + if(!dma_buffer) + { + dma_buffer = malloc(dma_buffer_size); + if(!dma_buffer) + { + return -1; + } + } memset(dma_buffer, 0xFA, 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); i2s_dma_init(dma_isr_handler, NULL, clock_div, i2s_pins); + return 0; } const IRAM_DATA int16_t bitpatterns[16] = diff --git a/extras/ws2812_i2s/ws2812_i2s.h b/extras/ws2812_i2s/ws2812_i2s.h index ffb2701..c6c8df5 100644 --- a/extras/ws2812_i2s/ws2812_i2s.h +++ b/extras/ws2812_i2s/ws2812_i2s.h @@ -53,7 +53,7 @@ typedef enum { * * @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.