[FL-1274] Interrupt manager: cleaned version (#490)
* HAL interrupl manager: cleaned version * HAL interrupt manager: mem barriers * HAL interrupt manager: volatile interrupt list Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
		
							parent
							
								
									84f46e670a
								
							
						
					
					
						commit
						f49f30f8a7
					
				| @ -1,123 +1,65 @@ | |||||||
| #include "api-interrupt-mgr.h" | #include "api-interrupt-mgr.h" | ||||||
| #include "mlib/m-dict.h" |  | ||||||
| 
 |  | ||||||
| #include <m-list.h> |  | ||||||
| #include <cmsis_os2.h> | #include <cmsis_os2.h> | ||||||
|  | #include <furi.h> | ||||||
| 
 | 
 | ||||||
| LIST_DEF(list_interrupt, InterruptCallbackItem, M_POD_OPLIST); | static volatile InterruptCallbackItem callback_list[InterruptTypeLast]; | ||||||
| DICT_DEF2(dict_interrupt, uint32_t, M_DEFAULT_OPLIST, list_interrupt_t, M_A1_OPLIST); |  | ||||||
| 
 |  | ||||||
| dict_interrupt_t interrupts_dict; |  | ||||||
| osMutexId_t interrupt_mutex; |  | ||||||
| 
 | 
 | ||||||
| bool api_interrupt_init() { | bool api_interrupt_init() { | ||||||
|     dict_interrupt_init(interrupts_dict); |     for(uint8_t i = 0; i < InterruptTypeLast; i++) { | ||||||
|     interrupt_mutex = osMutexNew(NULL); |         callback_list[i].callback = NULL; | ||||||
|     return (interrupt_mutex != NULL); |         callback_list[i].context = NULL; | ||||||
|  |         callback_list[i].ready = false; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return true; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void api_interrupt_add(InterruptCallback callback, InterruptType type, void* context) { | void api_interrupt_add(InterruptCallback callback, InterruptType type, void* context) { | ||||||
|     if(osMutexAcquire(interrupt_mutex, osWaitForever) == osOK) { |     furi_assert(type < InterruptTypeLast); | ||||||
|         list_interrupt_t* list = dict_interrupt_get(interrupts_dict, (uint32_t)type); |     furi_check(callback_list[type].callback == NULL); | ||||||
| 
 | 
 | ||||||
|         if(list == NULL) { |     callback_list[type].callback = callback; | ||||||
|             list_interrupt_t new_list; |     callback_list[type].context = context; | ||||||
|             list_interrupt_init(new_list); |     __DMB(); | ||||||
|             dict_interrupt_set_at(interrupts_dict, (uint32_t)type, new_list); |     callback_list[type].ready = true; | ||||||
|             list = dict_interrupt_get(interrupts_dict, (uint32_t)type); |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         // put uninitialized item to the list
 |  | ||||||
|         // M_POD_OPLIST provide memset(&(a), 0, sizeof (a)) constructor
 |  | ||||||
|         // so item will not be ready until we set ready flag
 |  | ||||||
|         InterruptCallbackItem* item = list_interrupt_push_new(*list); |  | ||||||
| 
 |  | ||||||
|         // initialize item
 |  | ||||||
|         item->callback = callback; |  | ||||||
|         item->type = type; |  | ||||||
|         item->context = context; |  | ||||||
|         asm volatile("dmb" : : : "memory"); |  | ||||||
|         item->ready = true; |  | ||||||
| 
 |  | ||||||
|         osMutexRelease(interrupt_mutex); |  | ||||||
|     } |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void api_interrupt_remove(InterruptCallback callback, InterruptType type) { | void api_interrupt_remove(InterruptCallback callback, InterruptType type) { | ||||||
|     if(osMutexAcquire(interrupt_mutex, osWaitForever) == osOK) { |     furi_assert(type < InterruptTypeLast); | ||||||
|         list_interrupt_t* list = dict_interrupt_get(interrupts_dict, (uint32_t)type); |     if(callback_list[type].callback != NULL) { | ||||||
| 
 |         furi_check(callback_list[type].callback == callback); | ||||||
|         if(list != NULL) { |  | ||||||
|             // iterate over items
 |  | ||||||
|             list_interrupt_it_t it; |  | ||||||
|             list_interrupt_it(it, *list); |  | ||||||
|             while(!list_interrupt_end_p(it)) { |  | ||||||
|                 if(it->current->data.callback == callback) { |  | ||||||
|                     list_interrupt_remove(*list, it); |  | ||||||
|                 } else { |  | ||||||
|                     list_interrupt_next(it); |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|         osMutexRelease(interrupt_mutex); |     callback_list[type].ready = false; | ||||||
|     } |     __DMB(); | ||||||
|  |     callback_list[type].callback = NULL; | ||||||
|  |     callback_list[type].context = NULL; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void api_interrupt_enable(InterruptCallback callback, InterruptType type) { | void api_interrupt_enable(InterruptCallback callback, InterruptType type) { | ||||||
|     if(osMutexAcquire(interrupt_mutex, osWaitForever) == osOK) { |     furi_assert(type < InterruptTypeLast); | ||||||
|         list_interrupt_t* list = dict_interrupt_get(interrupts_dict, (uint32_t)type); |     furi_check(callback_list[type].callback == callback); | ||||||
| 
 | 
 | ||||||
|         if(list != NULL) { |     callback_list[type].ready = true; | ||||||
|             // iterate over items
 |     __DMB(); | ||||||
|             list_interrupt_it_t it; |  | ||||||
|             for(list_interrupt_it(it, *list); !list_interrupt_end_p(it); list_interrupt_next(it)) { |  | ||||||
|                 // if the iterator is equal to our element
 |  | ||||||
|                 if(it->current->data.callback == callback) { |  | ||||||
|                     it->current->data.ready = true; |  | ||||||
|                     asm volatile("dmb" : : : "memory"); |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         osMutexRelease(interrupt_mutex); |  | ||||||
|     } |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void api_interrupt_disable(InterruptCallback callback, InterruptType type) { | void api_interrupt_disable(InterruptCallback callback, InterruptType type) { | ||||||
|     if(osMutexAcquire(interrupt_mutex, osWaitForever) == osOK) { |     furi_assert(type < InterruptTypeLast); | ||||||
|         list_interrupt_t* list = dict_interrupt_get(interrupts_dict, (uint32_t)type); |     furi_check(callback_list[type].callback == callback); | ||||||
| 
 | 
 | ||||||
|         if(list != NULL) { |     callback_list[type].ready = false; | ||||||
|             // iterate over items
 |     __DMB(); | ||||||
|             list_interrupt_it_t it; |  | ||||||
|             for(list_interrupt_it(it, *list); !list_interrupt_end_p(it); list_interrupt_next(it)) { |  | ||||||
|                 // if the iterator is equal to our element
 |  | ||||||
|                 if(it->current->data.callback == callback) { |  | ||||||
|                     it->current->data.ready = false; |  | ||||||
|                     asm volatile("dmb" : : : "memory"); |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         osMutexRelease(interrupt_mutex); |  | ||||||
|     } |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void api_interrupt_call(InterruptType type, void* hw) { | void api_interrupt_call(InterruptType type, void* hw) { | ||||||
|     // that executed in interrupt ctx so mutex don't needed
 |     // that executed in interrupt ctx so mutex don't needed
 | ||||||
|     // but we need to check ready flag
 |     // but we need to check ready flag
 | ||||||
|  |     furi_assert(type < InterruptTypeLast); | ||||||
| 
 | 
 | ||||||
|     list_interrupt_t* list = dict_interrupt_get(interrupts_dict, (uint32_t)type); |     if(callback_list[type].callback != NULL) { | ||||||
| 
 |         if(callback_list[type].ready) { | ||||||
|     if(list != NULL) { |             callback_list[type].callback(hw, callback_list[type].context); | ||||||
|         // iterate over items
 |  | ||||||
|         list_interrupt_it_t it; |  | ||||||
|         for(list_interrupt_it(it, *list); !list_interrupt_end_p(it); list_interrupt_next(it)) { |  | ||||||
|             // if the iterator is equal to our element
 |  | ||||||
|             if(it->current->data.ready) { |  | ||||||
|                 it->current->data.callback(hw, it->current->data.context); |  | ||||||
|             } |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -12,14 +12,13 @@ typedef void (*InterruptCallback)(void*, void*); | |||||||
| /** Interupt type */ | /** Interupt type */ | ||||||
| typedef enum { | typedef enum { | ||||||
|     InterruptTypeComparatorTrigger, |     InterruptTypeComparatorTrigger, | ||||||
|     InterruptTypeTimerOutputCompare, |  | ||||||
|     InterruptTypeTimerUpdate, |     InterruptTypeTimerUpdate, | ||||||
|  |     InterruptTypeLast, | ||||||
| } InterruptType; | } InterruptType; | ||||||
| 
 | 
 | ||||||
| /** Interrupt callback type */ | /** Interrupt callback type */ | ||||||
| typedef struct { | typedef struct { | ||||||
|     InterruptCallback callback; |     InterruptCallback callback; | ||||||
|     InterruptType type; |  | ||||||
|     void* context; |     void* context; | ||||||
|     bool ready; |     bool ready; | ||||||
| } InterruptCallbackItem; | } InterruptCallbackItem; | ||||||
|  | |||||||
| @ -10,11 +10,6 @@ void HAL_COMP_TriggerCallback(COMP_HandleTypeDef* hcomp) { | |||||||
|     api_interrupt_call(InterruptTypeComparatorTrigger, hcomp); |     api_interrupt_call(InterruptTypeComparatorTrigger, hcomp); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /* Output compare event */ |  | ||||||
| void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef* htim) { |  | ||||||
|     api_interrupt_call(InterruptTypeTimerOutputCompare, htim); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| /* Timer update event */ | /* Timer update event */ | ||||||
| void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) { | void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) { | ||||||
|     api_interrupt_call(InterruptTypeTimerUpdate, htim); |     api_interrupt_call(InterruptTypeTimerUpdate, htim); | ||||||
|  | |||||||
| @ -10,11 +10,6 @@ void HAL_COMP_TriggerCallback(COMP_HandleTypeDef* hcomp) { | |||||||
|     api_interrupt_call(InterruptTypeComparatorTrigger, hcomp); |     api_interrupt_call(InterruptTypeComparatorTrigger, hcomp); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /* Output compare event */ |  | ||||||
| void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef* htim) { |  | ||||||
|     api_interrupt_call(InterruptTypeTimerOutputCompare, htim); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| /* Timer update event */ | /* Timer update event */ | ||||||
| void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) { | void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) { | ||||||
|     api_interrupt_call(InterruptTypeTimerUpdate, htim); |     api_interrupt_call(InterruptTypeTimerUpdate, htim); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 SG
						SG