1 /*
2  * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy of
7  * this software and associated documentation files (the "Software"), to deal in
8  * the Software without restriction, including without limitation the rights to
9  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10  * the Software, and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  */
24 
25 #ifndef PORTMACRO_H
26 #define PORTMACRO_H
27 
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <csi_core.h>
31 
32 extern void vPortYield( void );
33 
34 /* *INDENT-OFF* */
35 #ifdef __cplusplus
36     class vPortYield;
37     extern "C" {
38 #endif
39 /* *INDENT-ON* */
40 
41 
42 /*-----------------------------------------------------------
43  * Port specific definitions.
44  *
45  * The settings in this file configure FreeRTOS correctly for the
46  * given hardware and compiler.
47  *
48  * These settings should not be altered.
49  *-----------------------------------------------------------
50  */
51 
52 /* Type definitions. */
53 #define portCHAR          char
54 #define portFLOAT         float
55 #define portDOUBLE        double
56 #define portLONG          long
57 #define portSHORT         short
58 #define portSTACK_TYPE    uint32_t
59 #define portBASE_TYPE     long
60 
61 typedef portSTACK_TYPE   StackType_t;
62 typedef long             BaseType_t;
63 typedef unsigned long    UBaseType_t;
64 typedef void (* portvectorfunc)( void );
65 
66 #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
67     typedef uint16_t     TickType_t;
68     #define portMAX_DELAY    ( TickType_t ) 0xffff
69 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
70     typedef uint32_t     TickType_t;
71     #define portMAX_DELAY    ( TickType_t ) 0xffffffffUL
72 #else
73     #error configTICK_TYPE_WIDTH_IN_BITS set to unsupported tick type width.
74 #endif
75 
76 
77 /* Hardware specifics. */
78 #define portBYTE_ALIGNMENT    8
79 #define portSTACK_GROWTH      -1
80 #define portMS_PERIOD_TICK    10
81 #define portTICK_PERIOD_MS    ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
82 
83 
vPortEnableInterrupt(void)84 static inline void vPortEnableInterrupt( void )
85 {
86     __enable_irq();
87 }
88 
vPortDisableInterrupt(void)89 static inline void vPortDisableInterrupt( void )
90 {
91     __disable_irq();
92 }
93 
GetCurrentPSR(void)94 static inline portLONG GetCurrentPSR( void )
95 {
96     return __get_PSR();
97 }
98 
SaveLocalPSR(void)99 static inline portLONG SaveLocalPSR( void )
100 {
101     portLONG flags = __get_PSR();
102 
103     __disable_irq();
104     return flags;
105 }
106 
RestoreLocalPSR(portLONG newMask)107 static inline void RestoreLocalPSR( portLONG newMask )
108 {
109     __asm__ __volatile__ (
110         "mtcr   %0, psr \n"
111         :
112         : "r" ( newMask )
113         : "memory"
114         );
115 }
116 
117 extern void vPortEnterCritical( void );
118 extern void vPortExitCritical( void );
119 extern __attribute__( ( naked ) ) void cpu_yeild( void );
120 
121 #define portDISABLE_INTERRUPTS()                  vPortDisableInterrupt()
122 #define portENABLE_INTERRUPTS()                   vPortEnableInterrupt()
123 #define portENTER_CRITICAL()                      vPortEnterCritical()
124 #define portEXIT_CRITICAL()                       vPortExitCritical()
125 #define portSET_INTERRUPT_MASK_FROM_ISR()         SaveLocalPSR()
126 #define portCLEAR_INTERRUPT_MASK_FROM_ISR( a )    RestoreLocalPSR( a )
127 
128 #define portNOP()                                 asm ( "nop" )
129 
130 extern portLONG ulCriticalNesting;
131 extern portLONG pendsvflag;
132 
133 #define portYIELD()              \
134     if( ulCriticalNesting == 0 ) \
135     {                            \
136         vPortYield();            \
137     }                            \
138     else                         \
139     {                            \
140         pendsvflag = 1;          \
141     }                            \
142     portNOP(); portNOP()
143 
144 /*-----------------------------------------------------------*/
145 
146 /* Task function macros as described on the FreeRTOS.org WEB site. */
147 #define portTASK_FUNCTION_PROTO( vFunction, pvParameters )    void vFunction( void * pvParameters ) __attribute__( ( noreturn ) )
148 #define portTASK_FUNCTION( vFunction, pvParameters )          void vFunction( void * pvParameters )
149 /*-----------------------------------------------------------*/
150 
151 #define portEND_SWITCHING_ISR( xSwitchRequired ) \
152     do {                                         \
153         if( xSwitchRequired != pdFALSE )         \
154         {                                        \
155             traceISR_EXIT_TO_SCHEDULER();        \
156             portYIELD();                         \
157         }                                        \
158         else                                     \
159         {                                        \
160             traceISR_EXIT();                     \
161         }                                        \
162     } while( 0 )
163 
164 #define portYIELD_FROM_ISR( a )    vTaskSwitchContext()
165 
166 
167 
168 /* *INDENT-OFF* */
169 #ifdef __cplusplus
170     }
171 #endif
172 /* *INDENT-ON* */
173 
174 #endif /* PORTMACRO_H */
175