diff --git a/src/drivers/boards/aerocore/board_config.h b/src/drivers/boards/aerocore/board_config.h index 922ddb6ee4..9eb97d559a 100644 --- a/src/drivers/boards/aerocore/board_config.h +++ b/src/drivers/boards/aerocore/board_config.h @@ -235,6 +235,8 @@ extern void stm32_spiinitialize(void); int nsh_archinitialize(void); #endif +#include "../common/board_common.h" + #endif /* __ASSEMBLY__ */ __END_DECLS diff --git a/src/drivers/boards/common/board_common.h b/src/drivers/boards/common/board_common.h new file mode 100644 index 0000000000..2b802639a5 --- /dev/null +++ b/src/drivers/boards/common/board_common.h @@ -0,0 +1,101 @@ +/**************************************************************************** + * + * Copyright (C) 2016 PX4 Development Team. All rights reserved. + * Author: David Sidrane + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/** + * @file board_common.h + * + * Provide the the common board interfaces + */ + +#pragma once + +/************************************************************************************ + * Included Files + ************************************************************************************/ +#include +/************************************************************************************ + * Definitions + ************************************************************************************/ + +/************************************************************************************ + * Private Functions + ************************************************************************************/ + +/************************************************************************************ + * Public Functions + ************************************************************************************/ + +/************************************************************************************ + * Name: board_name + * + * Description: + * All boards must provide this API to return the board name. + * + ************************************************************************************/ + +__EXPORT const char *board_name(void); + +/************************************************************************************ + * Name: board_dma_alloc_init + * + * Description: + * All boards may optionally provide this API to instantiate a pool of + * memory for uses with FAST FS DMA operations. + * + * Provision is controlled by declaring BOARD_DMA_ALLOC_POOL_SIZE in board_config.h + * + * + ************************************************************************************/ +#if defined(BOARD_DMA_ALLOC_POOL_SIZE) +__EXPORT int board_dma_alloc_init(void); +#else +#define board_dma_alloc_init() (-EPERM) +#endif + +/************************************************************************************ + * Name: board_get_dma_usage + * + * Description: + * All boards may optionally provide this API to supply instrumentation for a pool of + * memory used for DMA operations. + * + * Provision is controlled by declaring BOARD_DMA_ALLOC_POOL_SIZE in board_config.h + * + * + ************************************************************************************/ +#if defined(BOARD_DMA_ALLOC_POOL_SIZE) +__EXPORT int board_get_dma_usage(uint16_t *dma_total, uint16_t *dma_used, uint16_t *dma_peek_used); +#else +#define board_get_dma_usage(dma_total,dma_used, dma_peek_used) (-ENOMEM) +#endif diff --git a/src/drivers/boards/common/board_dma_alloc.c b/src/drivers/boards/common/board_dma_alloc.c new file mode 100644 index 0000000000..2c7126d1a0 --- /dev/null +++ b/src/drivers/boards/common/board_dma_alloc.c @@ -0,0 +1,156 @@ +/**************************************************************************** + * + * Copyright (C) 2016 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/** + * @file board_dma_alloc.c + * + * Provide the board dma allocator interface. + */ + +/************************************************************************************ + * Included Files + ************************************************************************************/ + +#include +#include "board_config.h" + +#include +#include +#include + +#include + +/************************************************************************************ + * Definitions + ************************************************************************************/ + +/************************************************************************************ + * Private Functions + ************************************************************************************/ + +/************************************************************************************ + * Public Functions + ************************************************************************************/ + +/************************************************************************************ + * Name: board_dma_alloc_init + * + * Description: + * All boards may optionally provide this API to instantiate a pool of + * memory for uses with FAST FS DMA operations. + * + ************************************************************************************/ +#if defined(BOARD_DMA_ALLOC_POOL_SIZE) + +# if !defined(CONFIG_GRAN) || !defined(CONFIG_FAT_DMAMEMORY) +# error microSD DMA support requires CONFIG_GRAN +# endif + +static GRAN_HANDLE dma_allocator; + +/* + * The DMA heap size constrains the total number of things that can be + * ready to do DMA at a time. + * + * For example, FAT DMA depends on one sector-sized buffer per filesystem plus + * one sector-sized buffer per file. + * + * We use a fundamental alignment / granule size of 64B; this is sufficient + * to guarantee alignment for the largest STM32 DMA burst (16 beats x 32bits). + */ +static uint8_t g_dma_heap[BOARD_DMA_ALLOC_POOL_SIZE] __attribute__((aligned(64))); +static perf_counter_t g_dma_perf; +static uint16_t dma_heap_inuse; +static uint16_t dma_heap_peek_use; +/**************************************************************************** + * Public Functions + ****************************************************************************/ +__EXPORT int board_dma_alloc_init(void) +{ + dma_allocator = gran_initialize(g_dma_heap, + sizeof(g_dma_heap), + 7, /* 128B granule - must be > alignment (XXX bug?) */ + 6); /* 64B alignment */ + + if (dma_allocator == NULL) { + return -ENOMEM; + + } else { + dma_heap_inuse = 0; + dma_heap_peek_use = 0; + g_dma_perf = perf_alloc(PC_COUNT, "dma_alloc"); + } + + return OK; +} + +__EXPORT int board_get_dma_usage(uint16_t *dma_total, uint16_t *dma_used, uint16_t *dma_peek_used) +{ + *dma_total = sizeof(g_dma_heap); + *dma_used = dma_heap_inuse; + *dma_peek_used = dma_heap_peek_use; + + return OK; +} +/* + * DMA-aware allocator stubs for the FAT filesystem. + */ + +__EXPORT void *fat_dma_alloc(size_t size); +__EXPORT void fat_dma_free(FAR void *memory, size_t size); + +void * +fat_dma_alloc(size_t size) +{ + void *rv = NULL; + perf_count(g_dma_perf); + rv = gran_alloc(dma_allocator, size); + + if (rv != NULL) { + dma_heap_inuse += size; + + if (dma_heap_inuse > dma_heap_peek_use) { + dma_heap_peek_use = dma_heap_inuse; + } + } + + return rv; +} + +void +fat_dma_free(FAR void *memory, size_t size) +{ + gran_free(dma_allocator, memory, size); + dma_heap_inuse -= size; +} +#endif /* defined(BOARD_DMA_ALLOC_POOL_SIZE) */ diff --git a/src/drivers/boards/mindpx-v2/CMakeLists.txt b/src/drivers/boards/mindpx-v2/CMakeLists.txt index e64b05469e..06598cda39 100644 --- a/src/drivers/boards/mindpx-v2/CMakeLists.txt +++ b/src/drivers/boards/mindpx-v2/CMakeLists.txt @@ -35,6 +35,7 @@ px4_add_module( COMPILE_FLAGS -Os SRCS + ../common/board_dma_alloc.c ../common/board_name.c mindpx_can.c mindpx2_init.c diff --git a/src/drivers/boards/mindpx-v2/board_config.h b/src/drivers/boards/mindpx-v2/board_config.h index 0c478057f3..5bd7e39c77 100644 --- a/src/drivers/boards/mindpx-v2/board_config.h +++ b/src/drivers/boards/mindpx-v2/board_config.h @@ -319,6 +319,9 @@ __BEGIN_DECLS {GPIO_GPIO11_INPUT, GPIO_GPIO11_OUTPUT, 0}, \ {GPIO_GPIO12_INPUT, GPIO_GPIO12_OUTPUT, 0}, } +/* This board provides a DMA pool and APIs */ + +#define BOARD_DMA_ALLOC_POOL_SIZE 8192 /**************************************************************************************************** * Public Types @@ -369,6 +372,8 @@ extern void stm32_usbinitialize(void); int nsh_archinitialize(void); #endif +#include "../common/board_common.h" + #endif /* __ASSEMBLY__ */ __END_DECLS diff --git a/src/drivers/boards/mindpx-v2/mindpx2_init.c b/src/drivers/boards/mindpx-v2/mindpx2_init.c index 94ea735f8d..5639a3a0f8 100644 --- a/src/drivers/boards/mindpx-v2/mindpx2_init.c +++ b/src/drivers/boards/mindpx-v2/mindpx2_init.c @@ -110,73 +110,9 @@ __END_DECLS /**************************************************************************** * Protected Functions ****************************************************************************/ - -#if defined(CONFIG_FAT_DMAMEMORY) -# if !defined(CONFIG_GRAN) || !defined(CONFIG_FAT_DMAMEMORY) -# error microSD DMA support requires CONFIG_GRAN -# endif - -static GRAN_HANDLE dma_allocator; - -/* - * The DMA heap size constrains the total number of things that can be - * ready to do DMA at a time. - * - * For example, FAT DMA depends on one sector-sized buffer per filesystem plus - * one sector-sized buffer per file. - * - * We use a fundamental alignment / granule size of 64B; this is sufficient - * to guarantee alignment for the largest STM32 DMA burst (16 beats x 32bits). - */ -static uint8_t g_dma_heap[8192] __attribute__((aligned(64))); -static perf_counter_t g_dma_perf; - -static void -dma_alloc_init(void) -{ - dma_allocator = gran_initialize(g_dma_heap, - sizeof(g_dma_heap), - 7, /* 128B granule - must be > alignment (XXX bug?) */ - 6); /* 64B alignment */ - - if (dma_allocator == NULL) { - message("DMA alloc FAILED"); - - } else { - g_dma_perf = perf_alloc(PC_COUNT, "dma_alloc"); - } -} - /**************************************************************************** * Public Functions ****************************************************************************/ - -/* - * DMA-aware allocator stubs for the FAT filesystem. - */ - -__EXPORT void *fat_dma_alloc(size_t size); -__EXPORT void fat_dma_free(FAR void *memory, size_t size); - -void * -fat_dma_alloc(size_t size) -{ - perf_count(g_dma_perf); - return gran_alloc(dma_allocator, size); -} - -void -fat_dma_free(FAR void *memory, size_t size) -{ - gran_free(dma_allocator, memory, size); -} - -#else - -# define dma_alloc_init() - -#endif - /************************************************************************************ * Name: stm32_boardinitialize * @@ -252,7 +188,10 @@ __EXPORT int nsh_archinitialize(void) hrt_init(); /* configure the DMA allocator */ - dma_alloc_init(); + + if (board_dma_alloc_init() < 0) { + message("DMA alloc FAILED"); + } /* configure CPU load estimation */ #ifdef CONFIG_SCHED_INSTRUMENTATION diff --git a/src/drivers/boards/px4-stm32f4discovery/board_config.h b/src/drivers/boards/px4-stm32f4discovery/board_config.h index 1d6aba13a1..2b476bf644 100644 --- a/src/drivers/boards/px4-stm32f4discovery/board_config.h +++ b/src/drivers/boards/px4-stm32f4discovery/board_config.h @@ -134,6 +134,8 @@ extern void stm32_usbinitialize(void); int nsh_archinitialize(void); #endif +#include "../common/board_common.h" + #endif /* __ASSEMBLY__ */ __END_DECLS diff --git a/src/drivers/boards/px4fmu-v1/board_config.h b/src/drivers/boards/px4fmu-v1/board_config.h index 549e0ec892..ec01c1bd5f 100644 --- a/src/drivers/boards/px4fmu-v1/board_config.h +++ b/src/drivers/boards/px4fmu-v1/board_config.h @@ -272,6 +272,8 @@ extern void stm32_usbinitialize(void); int nsh_archinitialize(void); #endif +#include "../common/board_common.h" + #endif /* __ASSEMBLY__ */ __END_DECLS diff --git a/src/drivers/boards/px4fmu-v2/CMakeLists.txt b/src/drivers/boards/px4fmu-v2/CMakeLists.txt index fe43efae75..e53cc92ac0 100644 --- a/src/drivers/boards/px4fmu-v2/CMakeLists.txt +++ b/src/drivers/boards/px4fmu-v2/CMakeLists.txt @@ -36,6 +36,7 @@ px4_add_module( -Os SRCS ../common/board_name.c + ../common/board_dma_alloc.c px4fmu_can.c px4fmu2_init.c px4fmu_timer_config.c diff --git a/src/drivers/boards/px4fmu-v2/board_config.h b/src/drivers/boards/px4fmu-v2/board_config.h index 4ff10f0c99..e0b916c5cc 100644 --- a/src/drivers/boards/px4fmu-v2/board_config.h +++ b/src/drivers/boards/px4fmu-v2/board_config.h @@ -270,6 +270,9 @@ __BEGIN_DECLS {GPIO_VDD_5V_HIPOWER_OC, 0, 0}, \ {GPIO_VDD_5V_PERIPH_OC, 0, 0}, } +/* This board provides a DMA pool and APIs */ + +#define BOARD_DMA_ALLOC_POOL_SIZE 8192 /**************************************************************************************************** * Public Types ****************************************************************************************************/ @@ -318,6 +321,8 @@ extern void board_peripheral_reset(int ms); int nsh_archinitialize(void); #endif +#include "../common/board_common.h" + #endif /* __ASSEMBLY__ */ __END_DECLS diff --git a/src/drivers/boards/px4fmu-v2/px4fmu2_init.c b/src/drivers/boards/px4fmu-v2/px4fmu2_init.c index c95c5ffd44..494341f46f 100644 --- a/src/drivers/boards/px4fmu-v2/px4fmu2_init.c +++ b/src/drivers/boards/px4fmu-v2/px4fmu2_init.c @@ -58,7 +58,6 @@ #include #include #include -#include #include #include "board_config.h" @@ -111,73 +110,9 @@ __END_DECLS /**************************************************************************** * Protected Functions ****************************************************************************/ - -#if defined(CONFIG_FAT_DMAMEMORY) -# if !defined(CONFIG_GRAN) || !defined(CONFIG_FAT_DMAMEMORY) -# error microSD DMA support requires CONFIG_GRAN -# endif - -static GRAN_HANDLE dma_allocator; - -/* - * The DMA heap size constrains the total number of things that can be - * ready to do DMA at a time. - * - * For example, FAT DMA depends on one sector-sized buffer per filesystem plus - * one sector-sized buffer per file. - * - * We use a fundamental alignment / granule size of 64B; this is sufficient - * to guarantee alignment for the largest STM32 DMA burst (16 beats x 32bits). - */ -static uint8_t g_dma_heap[8192] __attribute__((aligned(64))); -static perf_counter_t g_dma_perf; - -static void -dma_alloc_init(void) -{ - dma_allocator = gran_initialize(g_dma_heap, - sizeof(g_dma_heap), - 7, /* 128B granule - must be > alignment (XXX bug?) */ - 6); /* 64B alignment */ - - if (dma_allocator == NULL) { - message("DMA alloc FAILED"); - - } else { - g_dma_perf = perf_alloc(PC_COUNT, "dma_alloc"); - } -} - /**************************************************************************** * Public Functions ****************************************************************************/ - -/* - * DMA-aware allocator stubs for the FAT filesystem. - */ - -__EXPORT void *fat_dma_alloc(size_t size); -__EXPORT void fat_dma_free(FAR void *memory, size_t size); - -void * -fat_dma_alloc(size_t size) -{ - perf_count(g_dma_perf); - return gran_alloc(dma_allocator, size); -} - -void -fat_dma_free(FAR void *memory, size_t size) -{ - gran_free(dma_allocator, memory, size); -} - -#else - -# define dma_alloc_init() - -#endif - /************************************************************************************ * Name: board_peripheral_reset * @@ -269,7 +204,10 @@ __EXPORT int nsh_archinitialize(void) hrt_init(); /* configure the DMA allocator */ - dma_alloc_init(); + + if (board_dma_alloc_init() < 0) { + message("DMA alloc FAILED"); + } /* configure CPU load estimation */ #ifdef CONFIG_SCHED_INSTRUMENTATION diff --git a/src/drivers/boards/px4fmu-v4/CMakeLists.txt b/src/drivers/boards/px4fmu-v4/CMakeLists.txt index 2afe449392..8370a7c857 100644 --- a/src/drivers/boards/px4fmu-v4/CMakeLists.txt +++ b/src/drivers/boards/px4fmu-v4/CMakeLists.txt @@ -36,6 +36,7 @@ px4_add_module( -Os SRCS ../common/board_name.c + ../common/board_dma_alloc.c px4fmu_can.c px4fmu_init.c px4fmu_timer_config.c diff --git a/src/drivers/boards/px4fmu-v4/board_config.h b/src/drivers/boards/px4fmu-v4/board_config.h index dfc691b5fa..bf18bd9099 100644 --- a/src/drivers/boards/px4fmu-v4/board_config.h +++ b/src/drivers/boards/px4fmu-v4/board_config.h @@ -297,6 +297,9 @@ __BEGIN_DECLS {0, GPIO_VDD_3V3_SENSORS_EN, 0}, \ {GPIO_VDD_BRICK_VALID, 0, 0}, } +/* This board provides a DMA pool and APIs */ + +#define BOARD_DMA_ALLOC_POOL_SIZE 8192 /**************************************************************************************************** * Public Types @@ -347,6 +350,8 @@ extern void board_peripheral_reset(int ms); int nsh_archinitialize(void); #endif +#include "../common/board_common.h" + #endif /* __ASSEMBLY__ */ __END_DECLS diff --git a/src/drivers/boards/px4fmu-v4/px4fmu_init.c b/src/drivers/boards/px4fmu-v4/px4fmu_init.c index f3af5e3f6d..2e6adaf000 100644 --- a/src/drivers/boards/px4fmu-v4/px4fmu_init.c +++ b/src/drivers/boards/px4fmu-v4/px4fmu_init.c @@ -111,73 +111,9 @@ __END_DECLS /**************************************************************************** * Protected Functions ****************************************************************************/ - -#if defined(CONFIG_FAT_DMAMEMORY) -# if !defined(CONFIG_GRAN) || !defined(CONFIG_FAT_DMAMEMORY) -# error microSD DMA support requires CONFIG_GRAN -# endif - -static GRAN_HANDLE dma_allocator; - -/* - * The DMA heap size constrains the total number of things that can be - * ready to do DMA at a time. - * - * For example, FAT DMA depends on one sector-sized buffer per filesystem plus - * one sector-sized buffer per file. - * - * We use a fundamental alignment / granule size of 64B; this is sufficient - * to guarantee alignment for the largest STM32 DMA burst (16 beats x 32bits). - */ -static uint8_t g_dma_heap[8192] __attribute__((aligned(64))); -static perf_counter_t g_dma_perf; - -static void -dma_alloc_init(void) -{ - dma_allocator = gran_initialize(g_dma_heap, - sizeof(g_dma_heap), - 7, /* 128B granule - must be > alignment (XXX bug?) */ - 6); /* 64B alignment */ - - if (dma_allocator == NULL) { - message("DMA alloc FAILED"); - - } else { - g_dma_perf = perf_alloc(PC_COUNT, "dma_alloc"); - } -} - /**************************************************************************** * Public Functions ****************************************************************************/ - -/* - * DMA-aware allocator stubs for the FAT filesystem. - */ - -__EXPORT void *fat_dma_alloc(size_t size); -__EXPORT void fat_dma_free(FAR void *memory, size_t size); - -void * -fat_dma_alloc(size_t size) -{ - perf_count(g_dma_perf); - return gran_alloc(dma_allocator, size); -} - -void -fat_dma_free(FAR void *memory, size_t size) -{ - gran_free(dma_allocator, memory, size); -} - -#else - -# define dma_alloc_init() - -#endif - /************************************************************************************ * Name: board_peripheral_reset * @@ -278,7 +214,10 @@ __EXPORT int nsh_archinitialize(void) hrt_init(); /* configure the DMA allocator */ - dma_alloc_init(); + + if (board_dma_alloc_init() < 0) { + message("DMA alloc FAILED"); + } /* configure CPU load estimation */ #ifdef CONFIG_SCHED_INSTRUMENTATION