1 /*
2  * Copyright 2020 GreenWaves Technologies
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
18 
19 /*
20  * hal_fc_event.c
21  *
22  *  Created on: Feb 19, 2021
23  *      Author: qlblue
24  */
25 
26 #include "hal_fc_event.h"
27 #include "hal_soc_eu_periph.h"
28 #include "rtthread.h"
29 #ifdef PKG_USING_FREERTOS_WRAPPER
30 #include "FreeRTOS.h"
31 #include "semphr.h"
32 #endif
33 /*******************************************************************************
34  * Variables, macros, structures,... definition
35  ******************************************************************************/
36 
37 /*******************************************************************************
38  * Function definition
39  ******************************************************************************/
40 
41 static void fc_event_null_event(void *arg);
42 static volatile pi_fc_event_handler_t fc_event_handlers[SOC_EU_NB_FC_EVENTS];
43 #ifdef PKG_USING_FREERTOS_WRAPPER
44 static SemaphoreHandle_t  fc_event_semaphores[SOC_EU_NB_FC_EVENTS];
45 #endif
fc_event_null_event(void * arg)46 static void fc_event_null_event(void *arg)
47 {
48 	return;
49 }
50 
pi_fc_event_handler_init(uint32_t fc_event_irq)51 void pi_fc_event_handler_init(uint32_t fc_event_irq)
52 {
53 	/* TODO: fix this mess, that should be 8 32-bit writes */
54 	/* open the mask for fc_soc_event irq */
55 	for (int i = 0; i < SOC_EU_NB_FC_EVENTS; i++) {
56 		pi_fc_event_handler_clear(i);
57 	}
58 	/* NVIC_SetVector(fc_event_irq, (uint32_t)__handler_wrapper_light_fc_event_handler);*/
59 	//irqn_enable(fc_event_irq);
60 }
61 #ifdef PKG_USING_FREERTOS_WRAPPER
pi_fc_event_handler_set(uint32_t event_id,pi_fc_event_handler_t event_handler,SemaphoreHandle_t semaphoreHandle)62 void pi_fc_event_handler_set(uint32_t event_id,
63 			     pi_fc_event_handler_t event_handler,
64 					SemaphoreHandle_t semaphoreHandle)
65 {
66 	if (event_handler != NULL) {
67 		fc_event_handlers[event_id] = event_handler;
68 	}
69 	if (semaphoreHandle != NULL) {
70 		fc_event_semaphores[event_id] = semaphoreHandle;
71 	}
72 }
73 #endif
user_pi_fc_event_handler_set(uint32_t event_id,pi_fc_event_handler_t event_handler)74 void user_pi_fc_event_handler_set(uint32_t event_id,
75 			     pi_fc_event_handler_t event_handler)
76 {
77 	if (event_handler != NULL) {
78 		fc_event_handlers[event_id] = event_handler;
79 	}
80 }
81 
pi_fc_event_handler_clear(uint32_t event_id)82 void pi_fc_event_handler_clear(uint32_t event_id)
83 {
84 	fc_event_handlers[event_id] = (pi_fc_event_handler_t)fc_event_null_event;
85 	#ifdef PKG_USING_FREERTOS_WRAPPER
86 	fc_event_semaphores[event_id] = NULL;
87 	#endif
88 }
89 
90 /* TODO: Use Eric's FIRQ ABI */
fc_soc_event_handler1(uint32_t mcause)91 void fc_soc_event_handler1 (uint32_t mcause)
92 {
93 	uint32_t val = 0;
94 	#ifdef PKG_USING_FREERTOS_WRAPPER
95 	static BaseType_t xHigherPriorityTaskWoken;
96 	#endif
97 	uint32_t event_id = *(uint32_t*)(0x1a106090); // new event fifo address
98 
99 	event_id &= 0xFF;
100 
101 	/* redirect to handler with jump table */
102 	if (fc_event_handlers[event_id] != NULL) {
103 		fc_event_handlers[event_id]((void *)event_id);
104 	}
105 	#ifdef PKG_USING_FREERTOS_WRAPPER
106 	if (fc_event_semaphores[event_id] != NULL) {
107 		/* Unblock the task by releasing the semaphore. */
108 		SemaphoreHandle_t xSemaphoreHandle = fc_event_semaphores[event_id];
109 		xSemaphoreGiveFromISR( xSemaphoreHandle, &xHigherPriorityTaskWoken );
110 		portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
111 	}
112 	#endif
113 }
114