| 
									
										
										
										
											2022-01-29 16:49:56 +08:00
										 |  |  | /* | 
					
						
							|  |  |  |  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: Apache-2.0 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2023-05-04 17:31:31 +02:00
										 |  |  | #include "sdkconfig.h" | 
					
						
							|  |  |  | #include "portmacro.h" | 
					
						
							|  |  |  | #if CONFIG_ESP_SYSTEM_HW_STACK_GUARD | 
					
						
							|  |  |  | #include "esp_private/hw_stack_guard.h" | 
					
						
							|  |  |  | #endif | 
					
						
							| 
									
										
										
										
											2020-11-06 15:03:21 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |     .global uxInterruptNesting
 | 
					
						
							|  |  |  |     .global uxSchedulerRunning
 | 
					
						
							|  |  |  |     .global xIsrStackTop
 | 
					
						
							|  |  |  |     .global pxCurrentTCB
 | 
					
						
							|  |  |  |     .global vTaskSwitchContext
 | 
					
						
							|  |  |  |     .global xPortSwitchFlag
 | 
					
						
							| 
									
										
										
										
											2023-05-04 17:31:31 +02:00
										 |  |  | #if CONFIG_ESP_SYSTEM_HW_STACK_GUARD | 
					
						
							|  |  |  |     .global xIsrStack
 | 
					
						
							|  |  |  |     .global port_offset_pxStack
 | 
					
						
							|  |  |  |     .global port_offset_pxEndOfStack
 | 
					
						
							|  |  |  |     .global esp_hw_stack_guard_monitor_stop
 | 
					
						
							|  |  |  |     .global esp_hw_stack_guard_monitor_start
 | 
					
						
							|  |  |  |     .global esp_hw_stack_guard_set_bounds
 | 
					
						
							|  |  |  | #endif /* CONFIG_ESP_SYSTEM_HW_STACK_GUARD */ | 
					
						
							| 
									
										
										
										
											2020-11-06 15:03:21 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |     .section .text | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * This function makes the RTOS aware about a ISR entering, it takes the | 
					
						
							| 
									
										
										
										
											2023-05-04 17:31:31 +02:00
										 |  |  |  * current task stack saved, places into the TCB, loads the ISR stack. | 
					
						
							|  |  |  |  * TODO: ISR nesting code improvements ? | 
					
						
							| 
									
										
										
										
											2020-11-06 15:03:21 +11:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     .global rtos_int_enter
 | 
					
						
							|  |  |  |     .type rtos_int_enter, @function
 | 
					
						
							|  |  |  | rtos_int_enter: | 
					
						
							|  |  |  |     /* scheduler not enabled, jump directly to ISR handler */ | 
					
						
							| 
									
										
										
										
											2020-12-29 12:31:54 +08:00
										 |  |  |     lw t0, uxSchedulerRunning | 
					
						
							| 
									
										
										
										
											2020-11-04 18:34:47 -03:00
										 |  |  |     beq t0,zero, rtos_enter_end | 
					
						
							| 
									
										
										
										
											2020-11-06 15:03:21 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-04 18:34:47 -03:00
										 |  |  |     /* increments the ISR nesting count */ | 
					
						
							|  |  |  | 	la t3, uxInterruptNesting | 
					
						
							|  |  |  | 	lw t4, 0x0(t3) | 
					
						
							|  |  |  | 	addi t5,t4,1 | 
					
						
							|  |  |  | 	sw  t5, 0x0(t3) | 
					
						
							| 
									
										
										
										
											2020-12-29 12:31:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-04 18:34:47 -03:00
										 |  |  |     /* If reached here from another low-prio ISR, skip stack pushing to TCB */ | 
					
						
							|  |  |  | 	bne t4,zero, rtos_enter_end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-04 17:31:31 +02:00
										 |  |  | #if CONFIG_ESP_SYSTEM_HW_STACK_GUARD | 
					
						
							|  |  |  |     /* esp_hw_stack_guard_monitor_stop(); */ | 
					
						
							|  |  |  |     ESP_HW_STACK_GUARD_MONITOR_STOP_CPU0 | 
					
						
							|  |  |  | #endif /* CONFIG_ESP_SYSTEM_HW_STACK_GUARD */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-04 18:34:47 -03:00
										 |  |  |     /* Save current TCB and load the ISR stack */ | 
					
						
							| 
									
										
										
										
											2020-11-06 15:03:21 +11:00
										 |  |  |     lw  t0, pxCurrentTCB | 
					
						
							| 
									
										
										
										
											2023-05-04 17:31:31 +02:00
										 |  |  |     sw  sp, 0x0(t0) | 
					
						
							| 
									
										
										
										
											2020-11-06 15:03:21 +11:00
										 |  |  |     lw  sp, xIsrStackTop | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-04 17:31:31 +02:00
										 |  |  | #if CONFIG_ESP_SYSTEM_HW_STACK_GUARD | 
					
						
							|  |  |  |     /* esp_hw_stack_guard_set_bounds(xIsrStack, xIsrStackTop); */ | 
					
						
							|  |  |  |     la      a0, xIsrStack | 
					
						
							|  |  |  |     mv      a1, sp | 
					
						
							|  |  |  |     ESP_HW_STACK_GUARD_SET_BOUNDS_CPU0 | 
					
						
							|  |  |  |     ESP_HW_STACK_GUARD_MONITOR_START_CPU0 | 
					
						
							|  |  |  | #endif /* CONFIG_ESP_SYSTEM_HW_STACK_GUARD */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-04 18:34:47 -03:00
										 |  |  | rtos_enter_end: | 
					
						
							| 
									
										
										
										
											2020-11-06 15:03:21 +11:00
										 |  |  |     ret | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2023-05-04 17:31:31 +02:00
										 |  |  |  * Recovers the next task to run stack pointer. | 
					
						
							| 
									
										
										
										
											2020-11-06 15:03:21 +11:00
										 |  |  |  */ | 
					
						
							|  |  |  |     .global rtos_int_exit
 | 
					
						
							|  |  |  |     .type rtos_int_exit, @function
 | 
					
						
							|  |  |  | rtos_int_exit: | 
					
						
							|  |  |  |     /* may skip RTOS aware interrupt since scheduler was not started */ | 
					
						
							| 
									
										
										
										
											2020-12-29 12:31:54 +08:00
										 |  |  |     lw t0, uxSchedulerRunning | 
					
						
							|  |  |  |     beq t0,zero, rtos_exit_end | 
					
						
							| 
									
										
										
										
											2020-11-04 18:34:47 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* update nesting interrupts counter */ | 
					
						
							| 
									
										
										
										
											2020-11-13 16:03:50 -03:00
										 |  |  |     la t2, uxInterruptNesting | 
					
						
							|  |  |  |     lw t3, 0x0(t2) | 
					
						
							| 
									
										
										
										
											2020-11-04 18:34:47 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-19 15:14:54 -03:00
										 |  |  |     /* Already zero, protect against underflow */ | 
					
						
							| 
									
										
										
										
											2020-11-13 16:03:50 -03:00
										 |  |  |     beq t3, zero, isr_skip_decrement | 
					
						
							|  |  |  |     addi t3,t3, -1 | 
					
						
							|  |  |  |     sw  t3, 0x0(t2) | 
					
						
							| 
									
										
										
										
											2020-11-04 18:34:47 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | isr_skip_decrement: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 16:03:50 -03:00
										 |  |  |     /* may still have interrupts pending, skip section below and exit */ | 
					
						
							| 
									
										
										
										
											2020-12-29 12:31:54 +08:00
										 |  |  |     bne t3,zero,rtos_exit_end | 
					
						
							| 
									
										
										
										
											2020-11-06 15:03:21 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Schedule the next task if a yield is pending */ | 
					
						
							|  |  |  |     la t0, xPortSwitchFlag | 
					
						
							|  |  |  |     lw t2, 0x0(t0) | 
					
						
							|  |  |  |     beq t2, zero, no_switch | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 11:26:21 +08:00
										 |  |  |     /* preserve return address and schedule next task | 
					
						
							|  |  |  |        stack pointer for riscv should always be 16 byte aligned */ | 
					
						
							|  |  |  |     addi sp,sp,-16 | 
					
						
							| 
									
										
										
										
											2021-01-26 19:05:41 +08:00
										 |  |  |     sw  ra, 0(sp) | 
					
						
							| 
									
										
										
										
											2020-11-06 15:03:21 +11:00
										 |  |  |     call vTaskSwitchContext | 
					
						
							| 
									
										
										
										
											2021-01-26 19:05:41 +08:00
										 |  |  |     lw  ra, 0(sp) | 
					
						
							| 
									
										
										
										
											2021-02-19 11:26:21 +08:00
										 |  |  |     addi sp, sp, 16 | 
					
						
							| 
									
										
										
										
											2020-11-06 15:03:21 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Clears the switch pending flag */ | 
					
						
							|  |  |  |     la t0, xPortSwitchFlag | 
					
						
							|  |  |  |     mv t2, zero | 
					
						
							|  |  |  |     sw  t2, 0x0(t0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | no_switch: | 
					
						
							| 
									
										
										
										
											2023-05-04 17:31:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if CONFIG_ESP_SYSTEM_HW_STACK_GUARD | 
					
						
							|  |  |  |     /* esp_hw_stack_guard_monitor_stop(); */ | 
					
						
							|  |  |  |     ESP_HW_STACK_GUARD_MONITOR_STOP_CPU0 | 
					
						
							|  |  |  | #endif /* CONFIG_ESP_SYSTEM_HW_STACK_GUARD */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Recover the stack of next task */ | 
					
						
							|  |  |  |     lw t0, pxCurrentTCB | 
					
						
							|  |  |  |     lw sp, 0x0(t0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if CONFIG_ESP_SYSTEM_HW_STACK_GUARD | 
					
						
							|  |  |  |     /* esp_hw_stack_guard_set_bounds(pxCurrentTCB[0]->pxStack, | 
					
						
							|  |  |  |      *                               pxCurrentTCB[0]->pxEndOfStack);
 | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     lw      a0, PORT_OFFSET_PX_STACK(t0) | 
					
						
							|  |  |  |     lw      a1, PORT_OFFSET_PX_END_OF_STACK(t0) | 
					
						
							|  |  |  |     ESP_HW_STACK_GUARD_SET_BOUNDS_CPU0 | 
					
						
							|  |  |  |     /* esp_hw_stack_guard_monitor_start(); */ | 
					
						
							|  |  |  |     ESP_HW_STACK_GUARD_MONITOR_START_CPU0 | 
					
						
							|  |  |  | #endif /* CONFIG_ESP_SYSTEM_HW_STACK_GUARD */ | 
					
						
							| 
									
										
										
										
											2020-11-06 15:03:21 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-04 18:34:47 -03:00
										 |  |  | rtos_exit_end: | 
					
						
							| 
									
										
										
										
											2020-11-06 15:03:21 +11:00
										 |  |  |     ret |