1 /*
2  * Copyright 2021 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef __FSL_ADAPTER_RFCOMMON_H__
10 #define __FSL_ADAPTER_RFCOMMON_H__
11 
12 #include "fsl_os_abstraction.h"
13 #include "fsl_imu.h"
14 #include "assert.h"
15 #if defined(CPU2)
16 #include "core_cm3.h"
17 #endif
18 #include "fsl_adapter_rpmsg.h"
19 
20 /*! @brief TRUE/FALSE definition. */
21 #ifndef FALSE
22 #define FALSE 0
23 #endif
24 
25 #ifndef TRUE
26 #define TRUE 1
27 #endif
28 
29 /*******************************************************************************
30  * List Definitions and APIs
31  ******************************************************************************/
32 /**
33  * @brief Macro to iterate over a list using a user specified loop cursor.
34  *
35  * @param cur   the variable used for cursor, representing the current list node.
36  * @param list  the head for your list.
37  */
38 #define list_for_each(cur, list) for ((cur) = (list)->next; (cur) != (list); (cur) = (cur)->next)
39 
40 /**
41  * @brief An element in a doubly-linked circular list.
42  *  A list is a doubly-linked circular list.
43  */
44 typedef struct LIST_ELEM_st
45 {
46     struct LIST_ELEM_st *next;
47     struct LIST_ELEM_st *prev;
48 } LIST_ELEM_st;
49 
50 /**
51  *  @brief Initializes a list, as an empty list.
52  *  @param list The list descriptor, i.e. the fake list element that identifies a given list.
53  */
LIST_init(LIST_ELEM_st * list)54 static inline void LIST_init(LIST_ELEM_st *list)
55 {
56     list->next = list;
57     list->prev = list;
58 }
59 
60 /**
61  *  @brief Initializes an element, as an element not yet bound within a list.
62  *  @param elem The element to Initialize.
63  */
LIST_elemInit(LIST_ELEM_st * elem)64 static inline void LIST_elemInit(LIST_ELEM_st *elem)
65 {
66     elem->next = NULL;
67     elem->prev = NULL;
68 }
69 
70 /**
71  *  @brief  Inserts a new element in a list before a given existing element.
72  *  @param  list    The list descriptor, i.e. the fake list element that identifies a given list.
73  *  @param  elem    The new element is placed before this element.
74  *  @param  newElem New element to insert in the list.
75  */
LIST_insertBefore(LIST_ELEM_st * list,LIST_ELEM_st * elem,LIST_ELEM_st * newElem)76 static inline void LIST_insertBefore(LIST_ELEM_st *list, LIST_ELEM_st *elem, LIST_ELEM_st *newElem)
77 {
78     assert(newElem != list);
79 
80     (void)list;
81 
82     (elem->prev)->next = newElem;
83     newElem->next      = elem;
84     newElem->prev      = elem->prev;
85     elem->prev         = newElem;
86 }
87 
88 /**
89  *  @brief  Adds an element in a list at the tail of that list.
90  *  @param  list The list descriptor, i.e. the fake list element that identifies a given list.
91  *  @param  elem New element to insert in the list.
92  */
LIST_addTail(LIST_ELEM_st * list,LIST_ELEM_st * elem)93 static inline void LIST_addTail(LIST_ELEM_st *list, LIST_ELEM_st *elem)
94 {
95     LIST_insertBefore(list, list, elem);
96 }
97 
98 /**
99  *  @brief  Removes an element from a list.
100  *  @param  list The list descriptor, i.e. the fake list element that identifies a given list.
101  *  @param  elem The element to remove.
102  */
LIST_remove(LIST_ELEM_st * list,LIST_ELEM_st * elem)103 static inline void LIST_remove(LIST_ELEM_st *list, LIST_ELEM_st *elem)
104 {
105     assert(elem != list);
106 
107     (void)list;
108 
109     if ((elem->next == NULL) || (elem->prev == NULL))
110     {
111         return;
112     }
113     (elem->prev)->next = elem->next;
114     (elem->next)->prev = elem->prev;
115 
116     elem->next = NULL; // to indicate this is not linked yet
117     elem->prev = NULL;
118 }
119 
120 /*!
121  * @addtogroup IMU_Adapter
122  * @{
123  */
124 
125 /*******************************************************************************
126  * Definitions
127  ******************************************************************************/
128 
129 /*!
130    IRQ index should come from board level definition.
131    Remove following definition after RPMSG code integration.
132 */
133 #if defined(CPU2)
134 #define IRQ_IMU_CPU12 CPU1_TO_CPU2_MSG_RDY_IMU_INT_IRQn
135 #define IRQ_IMU_CPU32 CPU3_TO_CPU2_MSG_RDY_IMU_INT_IRQn
136 #else
137 #define IRQ_IMU_CPU13 WL_MCI_WAKEUP0_IRQn
138 #define IRQ_IMU_CPU23 BLE_MCI_WAKEUP0_IRQn
139 #endif
140 
141 #if defined(CPU2)
142 #define IMU_MSG_SND_Q(imuLinkId) (((imuLinkId) == kIMU_LinkCpu1Cpu2) ? imuMsgQ12->msgQ[1] : imuMsgQ23->msgQ[0])
143 #define IMU_MSG_CUR_MAGIC_P(imuLinkId) \
144     (((imuLinkId) == kIMU_LinkCpu1Cpu2) ? (&imuMsgQ12->magic[1]) : (&imuMsgQ23->magic[0]))
145 #define IMU_MSG_PEER_MAGIC_P(imuLinkId) \
146     (((imuLinkId) == kIMU_LinkCpu1Cpu2) ? (&imuMsgQ12->magic[0]) : (&imuMsgQ23->magic[1]))
147 #define IRQID_TO_IMULINKID(irqId)     (((irqId) == IRQ_IMU_CPU12) ? kIMU_LinkCpu1Cpu2 : kIMU_LinkCpu2Cpu3)
148 #define IMULINKID_TO_IRQID(imuLinkId) (((imuLinkId) == kIMU_LinkCpu1Cpu2) ? IRQ_IMU_CPU12 : IRQ_IMU_CPU32)
149 #else
150 #define IMU_MSG_SND_Q(imuLinkId) (((imuLinkId) == kIMU_LinkCpu1Cpu3) ? imuMsgQ13->msgQ[1] : imuMsgQ23->msgQ[1])
151 #define IMU_MSG_CUR_MAGIC_P(imuLinkId) \
152     (((imuLinkId) == kIMU_LinkCpu1Cpu3) ? (&imuMsgQ13->magic[1]) : (&imuMsgQ23->magic[1]))
153 #define IMU_MSG_PEER_MAGIC_P(imuLinkId) \
154     (((imuLinkId) == kIMU_LinkCpu1Cpu3) ? (&imuMsgQ13->magic[0]) : (&imuMsgQ23->magic[0]))
155 #define IRQID_TO_IMULINKID(irqId)     (((irqId) == IRQ_IMU_CPU13) ? kIMU_LinkCpu1Cpu3 : kIMU_LinkCpu2Cpu3)
156 #define IMULINKID_TO_IRQID(imuLinkId) (((imuLinkId) == kIMU_LinkCpu1Cpu3) ? IRQ_IMU_CPU13 : IRQ_IMU_CPU23)
157 #endif
158 #endif /* __FSL_ADAPTER_RFCOMMON_H__ */
159