/* ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio. This file is part of ChibiOS. ChibiOS is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. ChibiOS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ /** * @file chheap.c * @brief Heaps code. * * @addtogroup heaps * @details Heap Allocator related APIs. *

Operation mode

* The heap allocator implements a first-fit strategy and its APIs * are functionally equivalent to the usual @p malloc() and @p free() * library functions. The main difference is that the OS heap APIs * are guaranteed to be thread safe and there is the ability to * return memory blocks aligned to arbitrary powers of two.
* @pre In order to use the heap APIs the @p CH_CFG_USE_HEAP option must * be enabled in @p chconf.h. * @note Compatible with RT and NIL. * @{ */ #include "ch.h" #if (CH_CFG_USE_HEAP == TRUE) || defined(__DOXYGEN__) /*===========================================================================*/ /* Module local definitions. */ /*===========================================================================*/ /* * Defaults on the best synchronization mechanism available. */ #if (CH_CFG_USE_MUTEXES == TRUE) || defined(__DOXYGEN__) #define H_LOCK(h) chMtxLock(&(h)->mtx) #define H_UNLOCK(h) chMtxUnlock(&(h)->mtx) #else #define H_LOCK(h) (void) chSemWait(&(h)->sem) #define H_UNLOCK(h) chSemSignal(&(h)->sem) #endif #define H_BLOCK(hp) ((hp) + 1U) #define H_LIMIT(hp) (H_BLOCK(hp) + H_PAGES(hp)) #define H_NEXT(hp) ((hp)->free.next) #define H_PAGES(hp) ((hp)->free.pages) #define H_HEAP(hp) ((hp)->used.heap) #define H_SIZE(hp) ((hp)->used.size) /* * Number of pages between two pointers in a MISRA-compatible way. */ #define NPAGES(p1, p2) \ /*lint -save -e9033 [10.8] The cast is safe.*/ \ ((size_t)((p1) - (p2))) \ /*lint -restore*/ /*===========================================================================*/ /* Module exported variables. */ /*===========================================================================*/ /*===========================================================================*/ /* Module local types. */ /*===========================================================================*/ /*===========================================================================*/ /* Module local variables. */ /*===========================================================================*/ /** * @brief Default heap descriptor. */ static memory_heap_t default_heap; /*===========================================================================*/ /* Module local functions. */ /*===========================================================================*/ /*===========================================================================*/ /* Module exported functions. */ /*===========================================================================*/ /** * @brief Initializes the default heap. * * @notapi */ void _heap_init(void) { default_heap.provider = chCoreAllocAlignedWithOffset; H_NEXT(&default_heap.header) = NULL; H_PAGES(&default_heap.header) = 0; #if (CH_CFG_USE_MUTEXES == TRUE) || defined(__DOXYGEN__) chMtxObjectInit(&default_heap.mtx); #else chSemObjectInit(&default_heap.sem, (cnt_t)1); #endif } /** * @brief Initializes a memory heap from a static memory area. * @note The heap buffer base and size are adjusted if the passed buffer * is not aligned to @p CH_HEAP_ALIGNMENT. This mean that the * effective heap size can be less than @p size. * * @param[out] heapp pointer to the memory heap descriptor to be initialized * @param[in] buf heap buffer base * @param[in] size heap size * * @init */ void chHeapObjectInit(memory_heap_t *heapp, void *buf, size_t size) { heap_header_t *hp = (heap_header_t *)MEM_ALIGN_NEXT(buf, CH_HEAP_ALIGNMENT); chDbgCheck((heapp != NULL) && (size > 0U)); /* Adjusting the size in case the initial block was not correctly aligned.*/ /*lint -save -e9033 [10.8] Required cast operations.*/ size -= (size_t)((uint8_t *)hp - (uint8_t *)buf); /*lint restore*/ /* Initializing the heap header.*/ heapp->provider = NULL; H_NEXT(&heapp->header) = hp; H_PAGES(&heapp->header) = 0; H_NEXT(hp) = NULL; H_PAGES(hp) = (size - sizeof (heap_header_t)) / CH_HEAP_ALIGNMENT; #if (CH_CFG_USE_MUTEXES == TRUE) || defined(__DOXYGEN__) chMtxObjectInit(&heapp->mtx); #else chSemObjectInit(&heapp->sem, (cnt_t)1); #endif } /** * @brief Allocates a block of memory from the heap by using the first-fit * algorithm. * @details The allocated block is guaranteed to be properly aligned to the * specified alignment. * * @param[in] heapp pointer to a heap descriptor or @p NULL in order to * access the default heap. * @param[in] size the size of the block to be allocated. Note that the * allocated block may be a bit bigger than the requested * size for alignment and fragmentation reasons. * @param[in] align desired memory alignment * @return A pointer to the aligned allocated block. * @retval NULL if the block cannot be allocated. * * @api */ void *chHeapAllocAligned(memory_heap_t *heapp, size_t size, unsigned align) { heap_header_t *qp, *hp, *ahp; size_t pages; chDbgCheck((size > 0U) && MEM_IS_VALID_ALIGNMENT(align)); /* If an heap is not specified then the default system header is used.*/ if (heapp == NULL) { heapp = &default_heap; } /* Minimum alignment is constrained by the heap header structure size.*/ if (align < CH_HEAP_ALIGNMENT) { align = CH_HEAP_ALIGNMENT; } /* Size is converted in number of elementary allocation units.*/ pages = MEM_ALIGN_NEXT(size, CH_HEAP_ALIGNMENT) / CH_HEAP_ALIGNMENT; /* Taking heap mutex/semaphore.*/ H_LOCK(heapp); /* Start of the free blocks list.*/ qp = &heapp->header; while (H_NEXT(qp) != NULL) { /* Next free block.*/ hp = H_NEXT(qp); /* Pointer aligned to the requested alignment.*/ ahp = (heap_header_t *)MEM_ALIGN_NEXT(H_BLOCK(hp), align) - 1U; if ((ahp < H_LIMIT(hp)) && (pages <= NPAGES(H_LIMIT(hp), ahp + 1U))) { /* The block is large enough to cont
/**
  ******************************************************************************
  * @file    system_stm32l4xx.h
  * @author  MCD Application Team
  * @brief   CMSIS Cortex-M4 Device System Source File for STM32L4xx devices.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  *
  * 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 of STMicroelectronics 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 HOLDER 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.
  *
  ******************************************************************************
  */

/** @addtogroup CMSIS
  * @{
  */

/** @addtogroup stm32l4xx_system
  * @{
  */

/**
  * @brief Define to prevent recursive inclusion
  */
#ifndef __SYSTEM_STM32L4XX_H
#define __SYSTEM_STM32L4XX_H

#ifdef __cplusplus
 extern "C" {
#endif

/** @addtogroup STM32L4xx_System_Includes
  * @{
  */

/**
  * @}
  */


/** @addtogroup STM32L4xx_System_Exported_Variables
  * @{
  */
  /* The SystemCoreClock variable is updated in three ways:
      1) by calling CMSIS function SystemCoreClockUpdate()
      2) by calling HAL API function HAL_RCC_GetSysClockFreq()
      3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
         Note: If you use this function to configure the system clock; then there
               is no need to call the 2 first functions listed above, since SystemCoreClock
               variable is updated automatically.
  */
extern uint32_t SystemCoreClock;            /*!< System Clock Frequency (Core Clock) */

extern const uint8_t  AHBPrescTable[16];    /*!< AHB prescalers table values */
extern const uint8_t  APBPrescTable[8];     /*!< APB prescalers table values */
extern const uint32_t MSIRangeTable[12];    /*!< MSI ranges table values     */

/**
  * @}
  */

/** @addtogroup STM32L4xx_System_Exported_Constants
  * @{
  */

/**
  * @}
  */

/** @addtogroup STM32L4xx_System_Exported_Macros
  * @{
  */

/**
  * @}
  */

/** @addtogroup STM32L4xx_System_Exported_Functions
  * @{
  */

extern void SystemInit(void);
extern void SystemCoreClockUpdate(void);
/**
  * @}
  */

#ifdef __cplusplus
}
#endif

#endif /*__SYSTEM_STM32L4XX_H */

/**
  * @}
  */

/**
  * @}
  */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/