1 /*
2  * Copyright (c) 2023, Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /*
8  * MUTEX - Mutual exclusive peripheral
9  * https://infocenter.nordicsemi.com/topic/ps_nrf5340/mutex.html?cp=4_0_0_6_18
10  *
11  * This file provides the implementation of the nRF5340 MUTEX peripheral
12  */
13 
14 #include <string.h>
15 #include <stdbool.h>
16 #include "bs_types.h"
17 #include "nsi_tasks.h"
18 #include "NHW_config.h"
19 #include "NHW_peri_types.h"
20 
21 NRF_MUTEX_Type NRF_MUTEX_regs;
22 static bool MUTEX_state[sizeof(NRF_MUTEX_regs.MUTEX)];
23 
24 /**
25  * Initialize the MUTEX model
26  */
nhw_mutex_init(void)27 static void nhw_mutex_init(void) {
28   memset(NRF_MUTEX_regs.MUTEX, 0, sizeof(NRF_MUTEX_regs.MUTEX));
29   memset(MUTEX_state, 0, sizeof(MUTEX_state));
30 }
31 
32 NSI_TASK(nhw_mutex_init, HW_INIT, 100);
33 
34 /*
35  * Handle the sideeffects of writing a 0 to the MUTEX register
36  */
nhw_MUTEX_regw_sideeffects_MUTEX(uint n)37 void nhw_MUTEX_regw_sideeffects_MUTEX(uint n) {
38   MUTEX_state[n] = 0;
39   NRF_MUTEX_regs.MUTEX[n] = 0;
40 }
41 
42 /*
43  * Returns the value that would have been read from the MUTEX register
44  * and updates the MUTEX register itself
45  */
nhw_MUTEX_regr_sideeffects_MUTEX(uint n)46 int nhw_MUTEX_regr_sideeffects_MUTEX(uint n) {
47   if (MUTEX_state[n] == 0) {
48     MUTEX_state[n] = 1;
49     NRF_MUTEX_regs.MUTEX[n] = 1;
50     return 0;
51   }
52   return 1;
53 }
54