Difference between revisions of "BareMetal"
(→Example) |
|||
(10 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | µGFX can run on any system without any underlying OS or RTOS. The '''RAW32''' port provides all the required implementations to run on a bare metal system. The only thing required to implement by the user are two small functions which are used to calculate delays. See [[ | + | µGFX can run on any system without any underlying OS or RTOS. The '''RAW32''' port provides all the required implementations to run on a bare metal system. The only thing required to implement by the user are two small functions which are used to calculate delays. See [[#SysTick]]. |
− | The '''RAW32''' port also provides a multi-tasker that you can use in your own applications. The threads created are co-operative | + | The '''RAW32''' port also provides a multi-tasker that you can use in your own applications. The threads created are co-operative (non-preemptive) threads (you have to use <code>gfxSleepMilliseconds()</code> or <code>gfxYield()</code> to give another task a chance to run). See [[GOS#Threading|GOS]]. |
+ | |||
+ | == Using RAW32 == | ||
+ | To run µGFX on a bare-metal platform without any underlying operating system, the Raw32 port has to be enabled by setting <code>GFX_USE_OS_RAW32</code> to <code>TRUE</code> in the [[Configuration|configuration file]]. | ||
== Memory Management == | == Memory Management == | ||
Line 8: | Line 11: | ||
* Use the C runtime library memory manager | * Use the C runtime library memory manager | ||
− | Which one is used depends on the value of | + | Which one is used depends on the value of <code>GFX_OS_HEAP_SIZE</code> inside of the [[Configuration|configuration file]]. |
=== Built-in memory manager === | === Built-in memory manager === | ||
− | When | + | When <code>GFX_OS_HEAP_SIZE</code> is set to a value greater than 0 then the built-in memory manager of µGFX is used. In this case the value of <code>GFX_OS_HEAP_SIZE</code> specifies the size of the memory pool which the built-in memory manager will use. |
=== C runtime library memory manager === | === C runtime library memory manager === | ||
− | When | + | When <code>GFX_OS_HEAP_SIZE</code> is set to 0 then the memory manager of the C runtime library will be used. In that case <code>gfxAlloc()</code> and <code>gfxFree()</code> are just wrappers around <code>malloc()</code> and <code>free()</code>. |
− | Note that many C library implementations of | + | Note that many C library implementations of <code>malloc()</code> and <code>free()</code> have bugs that cause them to crash if the stack is changed (as it is for threading as required by µGFX). For this reason, if you are getting strange crashes it is advisable to use the built-in memory manager and to avoid using ''malloc()'' and <code>free()</code> in your own code (use <code>gfxAlloc()</code> and <code>gfxFree()</code> instead). |
== SysTick == | == SysTick == | ||
Some parts of the µGFX library require how much time passed by. For this access to the systems tick counter (SysTick) is required. To give µGFX the ability to calculate how much time passed the following to functions need to be implemented in the users application code: | Some parts of the µGFX library require how much time passed by. For this access to the systems tick counter (SysTick) is required. To give µGFX the ability to calculate how much time passed the following to functions need to be implemented in the users application code: | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c"> | ||
− | + | gTicks gfxSystemTicks(void); | |
− | + | gTicks gfxMillisecondsToTicks(delaytime_t ms); | |
</syntaxhighlight> | </syntaxhighlight> | ||
− | The first one, | + | The first one, <code>gfxSystemTicks()</code>, needs to return the current value of the systick counter. The second one, <code>gfxMillisecondsToTicks()</code> needs to convert the provided value in milliseconds to systicks. |
=== Example === | === Example === | ||
The following is an example showing how to implement the two functions when using an STM32F4 microcontroller using the STM32Cube HAL. Note that in this case the systick was set up to be incremented once every millisecond. If your systick updates in a different interval you need to do the corresponding calculations in ''gfxMillisecondsToTicks()''. | The following is an example showing how to implement the two functions when using an STM32F4 microcontroller using the STM32Cube HAL. Note that in this case the systick was set up to be incremented once every millisecond. If your systick updates in a different interval you need to do the corresponding calculations in ''gfxMillisecondsToTicks()''. | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c"> | ||
− | |||
#include "stm32f4xx_hal.h" | #include "stm32f4xx_hal.h" | ||
+ | #include "gfx.h" | ||
− | + | gTicks gfxSystemTicks(void) | |
{ | { | ||
return HAL_GetTick(); | return HAL_GetTick(); | ||
} | } | ||
− | + | gTicks gfxMillisecondsToTicks(gDelay ms) | |
{ | { | ||
return ms; | return ms; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | '''''Note:''' It is important that <code>gfx.h</code> is included after <code>stm32f4xx_hal.h</code> to avoid naming conflicts when using the CubeHAL with µGFX. |
Latest revision as of 09:27, 11 August 2021
µGFX can run on any system without any underlying OS or RTOS. The RAW32 port provides all the required implementations to run on a bare metal system. The only thing required to implement by the user are two small functions which are used to calculate delays. See #SysTick.
The RAW32 port also provides a multi-tasker that you can use in your own applications. The threads created are co-operative (non-preemptive) threads (you have to use gfxSleepMilliseconds()
or gfxYield()
to give another task a chance to run). See GOS.
Contents
Using RAW32
To run µGFX on a bare-metal platform without any underlying operating system, the Raw32 port has to be enabled by setting GFX_USE_OS_RAW32
to TRUE
in the configuration file.
Memory Management
µGFX can run either completely on static memory or on dynamic memory. While the first is very important for security and time critical applications it can be very tedious in generic applications. The RAW32 provides a memory manager to work with dynamic memory on any bare metal system. The following two configuration options are available:
- Use the µGFX built-in memory manager
- Use the C runtime library memory manager
Which one is used depends on the value of GFX_OS_HEAP_SIZE
inside of the configuration file.
Built-in memory manager
When GFX_OS_HEAP_SIZE
is set to a value greater than 0 then the built-in memory manager of µGFX is used. In this case the value of GFX_OS_HEAP_SIZE
specifies the size of the memory pool which the built-in memory manager will use.
C runtime library memory manager
When GFX_OS_HEAP_SIZE
is set to 0 then the memory manager of the C runtime library will be used. In that case gfxAlloc()
and gfxFree()
are just wrappers around malloc()
and free()
.
Note that many C library implementations of malloc()
and free()
have bugs that cause them to crash if the stack is changed (as it is for threading as required by µGFX). For this reason, if you are getting strange crashes it is advisable to use the built-in memory manager and to avoid using malloc() and free()
in your own code (use gfxAlloc()
and gfxFree()
instead).
SysTick
Some parts of the µGFX library require how much time passed by. For this access to the systems tick counter (SysTick) is required. To give µGFX the ability to calculate how much time passed the following to functions need to be implemented in the users application code:
gTicks gfxSystemTicks(void); gTicks gfxMillisecondsToTicks(delaytime_t ms);
The first one, gfxSystemTicks()
, needs to return the current value of the systick counter. The second one, gfxMillisecondsToTicks()
needs to convert the provided value in milliseconds to systicks.
Example
The following is an example showing how to implement the two functions when using an STM32F4 microcontroller using the STM32Cube HAL. Note that in this case the systick was set up to be incremented once every millisecond. If your systick updates in a different interval you need to do the corresponding calculations in gfxMillisecondsToTicks().
#include "stm32f4xx_hal.h" #include "gfx.h" gTicks gfxSystemTicks(void) { return HAL_GetTick(); } gTicks gfxMillisecondsToTicks(gDelay ms) { return ms; }
Note: It is important that gfx.h
is included after stm32f4xx_hal.h
to avoid naming conflicts when using the CubeHAL with µGFX.