xref: /Kernel-v10.6.2/portable/MSVC-MingW/portmacro.h (revision ef7b253b56c9788077f5ecd6c9deb4021923d646)
1 /*
2  * FreeRTOS Kernel V10.6.2
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  * the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * https://www.FreeRTOS.org
25  * https://github.com/FreeRTOS
26  *
27  */
28 
29 #ifndef PORTMACRO_H
30 #define PORTMACRO_H
31 
32 #include <windows.h>
33 #include <winbase.h>
34 
35 /******************************************************************************
36     Defines
37 ******************************************************************************/
38 /* Type definitions. */
39 #define portCHAR        char
40 #define portFLOAT       float
41 #define portDOUBLE      double
42 #define portLONG        long
43 #define portSHORT       short
44 #define portSTACK_TYPE  size_t
45 #define portBASE_TYPE   long
46 #define portPOINTER_SIZE_TYPE size_t
47 
48 typedef portSTACK_TYPE StackType_t;
49 typedef long BaseType_t;
50 typedef unsigned long UBaseType_t;
51 
52 
53 #if( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
54     typedef uint16_t TickType_t;
55     #define portMAX_DELAY ( TickType_t ) 0xffff
56 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
57     typedef uint32_t TickType_t;
58     #define portMAX_DELAY ( TickType_t ) 0xffffffffUL
59 
60     /* 32/64-bit tick type on a 32/64-bit architecture, so reads of the tick
61     count do not need to be guarded with a critical section. */
62     #define portTICK_TYPE_IS_ATOMIC 1
63 #else
64     #error configTICK_TYPE_WIDTH_IN_BITS set to unsupported tick type width.
65 #endif
66 
67 /* Hardware specifics. */
68 #define portSTACK_GROWTH            ( -1 )
69 #define portTICK_PERIOD_MS          ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
70 #define portINLINE __inline
71 
72 #if defined( __x86_64__) || defined( _M_X64 )
73     #define portBYTE_ALIGNMENT      8
74 #else
75     #define portBYTE_ALIGNMENT      4
76 #endif
77 
78 #define portYIELD()                 vPortGenerateSimulatedInterrupt( portINTERRUPT_YIELD )
79 
80 
81 extern volatile BaseType_t xInsideInterrupt;
82 #define portSOFTWARE_BARRIER() while( xInsideInterrupt != pdFALSE )
83 
84 
85 /* Simulated interrupts return pdFALSE if no context switch should be performed,
86 or a non-zero number if a context switch should be performed. */
87 #define portYIELD_FROM_ISR( x ) ( void ) x
88 #define portEND_SWITCHING_ISR( x ) portYIELD_FROM_ISR( ( x ) )
89 
90 void vPortCloseRunningThread( void *pvTaskToDelete, volatile BaseType_t *pxPendYield );
91 void vPortDeleteThread( void *pvThreadToDelete );
92 #define portCLEAN_UP_TCB( pxTCB )   vPortDeleteThread( pxTCB )
93 #define portPRE_TASK_DELETE_HOOK( pvTaskToDelete, pxPendYield ) vPortCloseRunningThread( ( pvTaskToDelete ), ( pxPendYield ) )
94 #define portDISABLE_INTERRUPTS() vPortEnterCritical()
95 #define portENABLE_INTERRUPTS() vPortExitCritical()
96 
97 /* Critical section handling. */
98 void vPortEnterCritical( void );
99 void vPortExitCritical( void );
100 
101 #define portENTER_CRITICAL()        vPortEnterCritical()
102 #define portEXIT_CRITICAL()         vPortExitCritical()
103 
104 #ifndef configUSE_PORT_OPTIMISED_TASK_SELECTION
105     #define configUSE_PORT_OPTIMISED_TASK_SELECTION 1
106 #endif
107 
108 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1
109 
110     /* Check the configuration. */
111     #if( configMAX_PRIORITIES > 32 )
112         #error configUSE_PORT_OPTIMISED_TASK_SELECTION can only be set to 1 when configMAX_PRIORITIES is less than or equal to 32.  It is very rare that a system requires more than 10 to 15 difference priorities as tasks that share a priority will time slice.
113     #endif
114 
115     /* Store/clear the ready priorities in a bit map. */
116     #define portRECORD_READY_PRIORITY( uxPriority, uxReadyPriorities ) ( uxReadyPriorities ) |= ( 1UL << ( uxPriority ) )
117     #define portRESET_READY_PRIORITY( uxPriority, uxReadyPriorities ) ( uxReadyPriorities ) &= ~( 1UL << ( uxPriority ) )
118 
119 
120     /*-----------------------------------------------------------*/
121 
122     #ifdef __GNUC__
123         #define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities )    \
124             __asm volatile( "bsr %1, %0\n\t"                                    \
125                             :"=r"(uxTopPriority) : "rm"(uxReadyPriorities) : "cc" )
126     #else
127         /* BitScanReverse returns the bit position of the most significant '1'
128         in the word. */
129         #define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities ) _BitScanReverse( ( DWORD * ) &( uxTopPriority ), ( uxReadyPriorities ) )
130     #endif /* __GNUC__ */
131 
132 #endif /* taskRECORD_READY_PRIORITY */
133 
134 #ifndef __GNUC__
135     __pragma( warning( disable:4211 ) ) /* Nonstandard extension used, as extern is only nonstandard to MSVC. */
136 #endif
137 
138 
139 /* Task function macros as described on the FreeRTOS.org WEB site. */
140 #define portTASK_FUNCTION_PROTO( vFunction, pvParameters ) void vFunction( void * pvParameters )
141 #define portTASK_FUNCTION( vFunction, pvParameters ) void vFunction( void * pvParameters )
142 
143 #define portINTERRUPT_YIELD             ( 0UL )
144 #define portINTERRUPT_TICK              ( 1UL )
145 
146 /*
147  * Raise a simulated interrupt represented by the bit mask in ulInterruptMask.
148  * Each bit can be used to represent an individual interrupt - with the first
149  * two bits being used for the Yield and Tick interrupts respectively.
150 */
151 void vPortGenerateSimulatedInterrupt( uint32_t ulInterruptNumber );
152 
153 /*
154  * Install an interrupt handler to be called by the simulated interrupt handler
155  * thread.  The interrupt number must be above any used by the kernel itself
156  * (at the time of writing the kernel was using interrupt numbers 0, 1, and 2
157  * as defined above).  The number must also be lower than 32.
158  *
159  * Interrupt handler functions must return a non-zero value if executing the
160  * handler resulted in a task switch being required.
161  */
162 void vPortSetInterruptHandler( uint32_t ulInterruptNumber, uint32_t (*pvHandler)( void ) );
163 
164 #endif
165