]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/ThirdParty/CDK/T-HEAD_CK802/port.c
Introduce a port for T-HEAD CK802. A simple demo for T-HEAD CB2201 is also included.
[freertos] / FreeRTOS / Source / portable / ThirdParty / CDK / T-HEAD_CK802 / port.c
1 /*
2  * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy of
5  * this software and associated documentation files (the "Software"), to deal in
6  * the Software without restriction, including without limitation the rights to
7  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8  * the Software, and to permit persons to whom the Software is furnished to do so,
9  * subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20  *
21  * 1 tab == 4 spaces!
22  */
23
24 /* Kernel includes. */
25 #include "FreeRTOS.h"
26 #include "task.h"
27
28 extern void vPortStartTask(void);
29
30 /* Used to keep track of the number of nested calls to taskENTER_CRITICAL().  This
31 will be set to 0 prior to the first task being started. */
32 portLONG ulCriticalNesting = 0x9999UL;
33
34 /* Used to record one tack want to swtich task after enter critical area, we need know it
35  * and implement task switch after exit critical area */
36 portLONG pendsvflag = 0;
37
38 StackType_t *pxPortInitialiseStack( StackType_t * pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
39 {
40     StackType_t *stk  = NULL;
41
42     stk = pxTopOfStack;
43
44     *(--stk)  = (uint32_t)pxCode;            /* Entry Point                                         */
45     *(--stk)  = (uint32_t)0xE0000140L;       /* PSR                                                 */
46     *(--stk)  = (uint32_t)0xFFFFFFFEL;       /* R15 (LR) (init value will cause fault if ever used) */
47     *(--stk)  = (uint32_t)0x13131313L;       /* R13                                                 */
48     *(--stk)  = (uint32_t)0x12121212L;       /* R12                                                 */
49     *(--stk)  = (uint32_t)0x11111111L;       /* R11                                                 */
50     *(--stk)  = (uint32_t)0x10101010L;       /* R10                                                 */
51     *(--stk)  = (uint32_t)0x09090909L;       /* R9                                                  */
52     *(--stk)  = (uint32_t)0x08080808L;       /* R8                                                  */
53     *(--stk)  = (uint32_t)0x07070707L;       /* R7                                                  */
54     *(--stk)  = (uint32_t)0x06060606L;       /* R6                                                  */
55     *(--stk)  = (uint32_t)0x05050505L;       /* R5                                                  */
56     *(--stk)  = (uint32_t)0x04040404L;       /* R4                                                  */
57     *(--stk)  = (uint32_t)0x03030303L;       /* R3                                                  */
58     *(--stk)  = (uint32_t)0x02020202L;       /* R2                                                  */
59     *(--stk)  = (uint32_t)0x01010101L;       /* R1                                                  */
60     *(--stk)  = (uint32_t)pvParameters;      /* R0 : argument                                       */
61
62     return stk;
63 }
64
65 BaseType_t xPortStartScheduler( void )
66 {
67     ulCriticalNesting = 0UL;
68
69     vPortStartTask();
70
71     return pdFALSE;
72 }
73
74
75 void vPortEndScheduler( void )
76 {
77     /* Not implemented as there is nothing to return to. */
78 }
79
80 void vPortEnterCritical( void )
81 {
82     portDISABLE_INTERRUPTS();
83     ulCriticalNesting ++;
84 }
85
86 void vPortExitCritical( void )
87 {
88     if (ulCriticalNesting == 0) {
89         while(1);
90     }
91
92     ulCriticalNesting --;
93     if (ulCriticalNesting == 0)
94     {
95         portENABLE_INTERRUPTS();
96
97         if (pendsvflag)
98         {
99             pendsvflag = 0;
100             portYIELD();
101         }
102     }
103 }
104
105 #if configUSE_PREEMPTION == 0
106 void xPortSysTickHandler( void )
107 {
108     portLONG ulDummy;
109
110     ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();
111     xTaskIncrementTick();
112     portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );
113 }
114
115 #else
116 void xPortSysTickHandler( void )
117 {
118     portLONG ulDummy;
119
120     ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();
121     {
122         if (xTaskIncrementTick() != pdFALSE)
123         {
124             portYIELD_FROM_ISR(pdTRUE);
125         }
126     }
127     portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );
128 }
129 #endif
130
131 void vPortYieldHandler( void )
132 {
133     uint32_t ulSavedInterruptMask;
134
135     ulSavedInterruptMask = portSET_INTERRUPT_MASK_FROM_ISR();
136
137     vTaskSwitchContext();
138
139     portCLEAR_INTERRUPT_MASK_FROM_ISR( ulSavedInterruptMask );
140 }
141
142 __attribute__((weak)) void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed portCHAR *pcTaskName )
143 {
144     for(;;);
145 }
146
147 __attribute__((weak)) void vApplicationMallocFailedHook( void )
148 {
149     for(;;);
150 }