1 /*
2  * ==========================================================
3  *
4  *    Copyright (C) 2020 QuickLogic Corporation
5  *    Licensed under the Apache License, Version 2.0 (the "License");
6  *    you may not use this file except in compliance with the License.
7  *    You may obtain a copy of the License at
8  * 		http://www.apache.org/licenses/LICENSE-2.0
9  *    Unless required by applicable law or agreed to in writing, software
10  *    distributed under the License is distributed on an "AS IS" BASIS,
11  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  *    See the License for the specific language governing permissions and
13  *    limitations under the License.
14  *
15  *    File      : eoss3_hal_ffe.h
16  *    Purpose 	: This file contains macros and APIs
17  *         for FFE
18  *
19  * ===========================================================
20  *
21  */
22 
23 #ifndef HAL_INC_EOSS3_HAL_FFE_H_
24 #define HAL_INC_EOSS3_HAL_FFE_H_
25 
26 #include <stdint.h>
27 #include <stddef.h>
28 
29 #include "test_types.h"
30 #include "eoss3_hal_def.h"
31 
32 /// @cond HAL_FFE_INTERNAL_MACROS
33 
34 /*! \def EXPORT_VAR_COUNT
35     \brief A macro to define export variable count.
36 */
37 #define EXPORT_VAR_COUNT			  64
38 
39 /*! \def FFE_CFG_BASE
40     \brief A macro to define M4 SRAM offset which stores the export section.
41 */
42 #define FFE_CFG_BASE				  0x2007F800
43 //#define FFE_CFG_BASE				  0x2007FE80  //Export section parsing base address for new celeris patch [Syama]
44 
45 /*! \def ALGS_COUNT
46     \brief A macro to define algorithm count.
47 */
48 #define ALGS_COUNT					  10
49 
50 /*! \def EXPO_FFE_MAILBOX1
51     \brief A macro to define export variable FFE_MAILBOX1.
52 */
53 #define EXPO_FFE_MAILBOX1			  1
54 
55 /*! \def EXPO_FFE_MAILBOX2
56     \brief A macro to define export variable FFE_MAILBOX2.
57 */
58 #define EXPO_FFE_MAILBOX2			  2
59 
60 /*! \def EXPO_SM_MAILBOX
61     \brief A macro to define export variable SM_MAILBOX.
62 */
63 #define EXPO_SM_MAILBOX				  3
64 
65 /*! \def EXPO_FFE_TICK_MS
66     \brief A macro to define export variable FFE_TICK_MS.
67 */
68 #define EXPO_FFE_TICK_MS			  4
69 
70 /*! \def EXPO_INTR_MASK
71     \brief A macro to define export variable INTR_MASK.
72 */
73 #define EXPO_INTR_MASK				  5
74 
75 /*! \def EXPO_M4_REQ_STATE
76     \brief A macro to define export variable M4_REQ_STATE.
77 */
78 #define EXPO_M4_REQ_STATE			  6
79 
80 /*! \def EXPO_FFE_RESP_STATE
81     \brief A macro to define export variable FFE_RESP_STATE.
82 */
83 #define EXPO_FFE_RESP_STATE			  7
84 
85 /*! \def EXPO_ACCEL_POLL_FREQ
86     \brief A macro to define export variable ACCEL_POLL_FREQ.
87 */
88 #define EXPO_ACCEL_POLL_FREQ		  8
89 
90 /*! \def EXPO_MAG_POLL_FREQ
91     \brief A macro to define export variable MAG_POLL_FREQ.
92 */
93 #define EXPO_MAG_POLL_FREQ		  	  9
94 
95 /*! \def EXPO_GYRO_POLL_FREQ
96     \brief A macro to define export variable GYRO_POLL_FREQ.
97 */
98 #define EXPO_GYRO_POLL_FREQ		  	  10
99 
100 /*! \def EXPO_DUMP
101     \brief A macro to define dump location for export structure.
102 */
103 #define EXPO_DUMP					  63
104 
105 /*! \def ALG_CONFIG1_TYPE
106     \brief A macro to define algorithm structure type with single dependent sensor.
107 */
108 #define ALG_CONFIG1_TYPE			 13
109 
110 /*! \def ALG_CONFIG2_TYPE
111     \brief A macro to define algorithm structure type with two dependent sensor.
112 */
113 #define ALG_CONFIG2_TYPE			  15
114 
115 /*! \def EXPORT_VAR_LIMIT
116     \brief A macro to define export variable count.
117 */
118 #define EXPORT_VAR_LIMIT			  0x64
119 
120 /*! \def MB1_SENSID_CUTOFF
121     \brief A macro to define Sensor Id cutoff for FFE Mailbox 1
122 */
123 #define MB1_SENSID_CUTOFF			  0x30
124 
125 /*! \def UPDATE_MB1
126     \brief A macro to inform FFE Mailbox 1 needed to be updated.
127 */
128 #define UPDATE_MB1					  0x81
129 
130 /*! \def UPDATE_MB2
131     \brief A macro to inform FFE Mailbox 2 needed to be updated.
132 */
133 #define UPDATE_MB2					  0x82
134 /*! \def SM0
135     \brief A macro to define SM type0.
136 */
137 #define SM0							  0
138 
139 /*! \def SM1
140     \brief A macro to define SM type1.
141 */
142 #define SM1							  1
143 
144 /// @endcond
145 
146 /// @cond HAL_FFE_INTERNAL_STRUCTURES
147 /*! \struct xSensor_alg_config_1
148  *	\brief	Sensor Configuration Parameters for single sensor dependency
149  */
150 typedef struct
151 {
152 	UINT32_t usSensor_Id;				/*!< Sensor Id */
153 	UINT32_t usDep1_Sensor_Id;			/*!< Dependent Sensor Id */
154 	UINT32_t usDep1_Sensor_Rate;		/*!< Dependent Sensor Rate*/
155 	UINT32_t usFifo_op_addr;			/*!< Fifo output address */
156 	UINT32_t usBatch_size;				/*!< Batching Size in bytes */
157 	UINT32_t usBatch_flush;
158 	UINT32_t usBatch_group_index;
159 	UINT32_t uiBatching_Mode;
160 	UINT32_t uiElapsed_Batch_Time;
161 	UINT32_t uiBatched_Packet_Count;
162 	UINT32_t usBG_Enable;				/*!< Always Enabled flag */
163 	UINT32_t output_rate;				/*!< Algorithm Output Rate */
164 	UINT32_t mb_value;					/*!< Mailbox Value */
165 
166 }xSensor_alg_config_1;
167 
168 /*! \struct xSensor_alg_config_2
169  *	\brief	Sensor Configuration Parameters for two sensor dependency
170  */
171 typedef struct
172 {
173 	UINT32_t usSensor_Id;				/*!< Sensor Id */
174 	UINT32_t usDep1_Sensor_Id;			/*!< Dependent1 Sensor Id */
175 	UINT32_t usDep1_Sensor_Rate;		/*!< Dependent1 Sensor Rate*/
176 	UINT32_t usDep2_Sensor_Id;			/*!< Dependent2 Sensor Id */
177 	UINT32_t usDep2_Sensor_Rate;		/*!< Dependent2 Sensor Rate*/
178 	UINT32_t usFifo_op_addr;			/*!< Fifo output address */
179 	UINT32_t usBatch_size;				/*!< Batching Size in bytes */
180 	UINT32_t usBatch_flush;
181 	UINT32_t usBatch_group_index;
182 	UINT32_t uiBatching_Mode;
183 	UINT32_t uiElapsed_Batch_Time;
184 	UINT32_t uiBatched_Packet_Count;
185 	UINT32_t usBG_Enable;				/*!< Always Enabled flag */
186 	UINT32_t output_rate;				/*!< Algorithm Output Rate */
187 	UINT32_t mb_value;					/*!< Mailbox Value */
188 
189 }xSensor_alg_config_2;
190 
191 /*! \struct xExport_var_t
192  *	\brief	Export variable structure
193  */
194 typedef struct
195 {
196 	UINT8_t 	ucId;		/*!< Export Section ID */
197 	UINT8_t 	ucMemType;      /*!< Export Section Memory Type */
198 	UINT16_t	usAddr;         /*!< Export Section Address offset */
199 	UINT32_t 	ulLen;		/*!< Export Section length */
200 
201 }xExport_var_t;
202 /// @endcond
203 
204 /*! \enum       eHAL_FFE_CmdType
205  *  \brief	HAL FFE Command Type
206  */
207 typedef enum
208 {
209 	eFFE_SENSOR_ENABLE = 1,
210 	eFFE_SENSOR_RATE_CHANGE,
211 	eFFE_SENSOR_BATCH_SIZE,
212 	eFFE_SENSOR_BATCH_FLUSH,
213 	eFFE_SENSOR_OUTPUT_DISABLE
214 }eHAL_FFE_CmdType;
215 
216 /*! \struct     FFE_FifoCfgTypeDef
217  *  \brief	HAL FIFO Configuration structure
218  */
219 typedef struct
220 {
221 	UINT8_t			ucFifoSrc;			/*!< FIFO source */
222 	UINT8_t			ucFifoDest;			/*!< FIFO destination */
223 	UINT8_t			ucFifoType;			/*!< FIFO type */
224 	UINT8_t 		ucIntSetting;		        /*!< FIFO Interrupt Settings */
225 	UINT16_t		usPopThreshIntCnt;	        /*!< FIFO threshold in terms of words (32-bit) */
226 	UINT16_t 		usPushThreshIntCnt;	        /*!< FIFO Push Threshold Interrupt Count */
227 
228 }FFE_FifoCfgTypeDef;
229 
230 /*! \struct     FFE_HandleTypeDef
231  *  \brief	HAL FFE Configuration structure
232  */
233 typedef struct
234 {
235 	FFE_FifoCfgTypeDef			FifoCfg;						/*!< config parameters for FIFO */
236 	UINT8_t 				ucRunOnce;						/*!< Flag to enable FFE to run in single step mode */
237 	UINT8_t  				ucRunContinous;					        /*!< Flag to enable FFE to run in continuous mode */
238 	UINT8_t  				ucStopFFE;						/*!< Flag to stop the FFE execution */
239 	UINT8_t					ucHaltFFE;						/*!< Flag to halt the FFE execution */
240 	UINT8_t					ucReleaseFFE;					        /*!< Flag to release the FFE halt */
241 	UINT32_t 				ulRunCnt;						/*!< FFE run count */
242 	UINT32_t 				ulFFEFreq;						/*!< FFE frequency */
243 
244 }FFE_HandleTypeDef;
245 
246 /*!
247  * \fn 		HAL_StatusTypeDef HAL_FFE_GetAlgParams(UINT8_t usSensId,xSensor_alg_config_1 *config1,xSensor_alg_config_2 *config2);
248  * \brief 	This function returns the Alg configuration parameters
249  * \param 	ucSensId   --- sensor ID
250  * \param   config1    --- pointer to Config 1 structure
251  * \param	config2    --- pointer to Config 2 structure
252  * \return	HAL Status
253  */
254 HAL_StatusTypeDef HAL_FFE_GetAlgParams(UINT8_t ucSensId,xSensor_alg_config_1 **config1,xSensor_alg_config_2 **config2);
255 /*!
256  * \fn 		HAL_StatusTypeDef HAL_FFE_SetAlgParams(UINT8_t *ucBuf, UINT8_t ucCmdType)
257  * \brief 	This function configures the Alg parameters inside FFE
258  * \param 	ucBuf	     --- User Command buffer
259  * \param       ucCmdType    --- Command Type
260  * \return	HAL Status
261  */
262 HAL_StatusTypeDef HAL_FFE_SetAlgParams(UINT8_t *ucBuf, UINT8_t ucCmdType);
263 /*!
264  * \fn 		HAL_StatusTypeDef HAL_FFE_WriteMem(volatile UINT32_t *ulMem_addr, UINT32_t ulLen, void *buf)
265  * \brief 	This function write into FFE Data memory section
266  * \param 	ulMem_addr -- DM Memory Address
267  * \param 	ulLen 	   -- Number of bytes to write
268  * \param 	buf        -- Data buffer to write
269  * \return      HAL Status
270  */
271 HAL_StatusTypeDef HAL_FFE_WriteMem32(volatile UINT32_t *ulMem_addr, UINT32_t value);
272 
273 HAL_StatusTypeDef HAL_FFE_WriteMem(volatile UINT32_t *ulMem_addr, UINT32_t ulLen, void *buf);
274 /*!
275  * \fn 		HAL_StatusTypeDef HAL_FFE_ReadMem(volatile UINT32_t *ulMem_addr, UINT32_t ulLen, void *buf)
276  * \brief 	This function read from FFE Data memory section
277  * \param 	ulMem_addr -- DM Memory Address
278  * \param 	ulLen 	   -- Number of bytes to read
279  * \param 	buf        -- Data buffer to read
280  * \return      HAL Status
281  */
282 HAL_StatusTypeDef HAL_FFE_ReadMem(volatile UINT32_t *ulMem_addr, UINT32_t ulLen, void *buf);
283 /*!
284  * \fn 		HAL_StatusTypeDef HAL_FFE_Config(FFE_HandleTypeDef *hFFE)
285  * \brief 	This function configure the FFE
286  * \param 	hFFE -- pointer to FFE Handle structure
287  * \return      HAL Status
288  */
289 HAL_StatusTypeDef HAL_FFE_Config(FFE_HandleTypeDef *hFFE);
290 /*!
291  * \fn 		HAL_StatusTypeDef HAL_FFE_Init(void)
292  * \brief 	This function initialize the FFE
293  * \return      HAL Status
294  */
295 HAL_StatusTypeDef HAL_FFE_Init(void);
296 /*!
297  * \fn 		HAL_StatusTypeDef HAL_FFE_DeInit(FFE_HandleTypeDef *hFFE)
298  * \brief 	This function deinitialize the FFE
299  * \return      HAL Status
300  */
301 HAL_StatusTypeDef HAL_FFE_DeInit(FFE_HandleTypeDef *hFFE);
302 
303 
304 void HAL_FFE_ENABLE_INT(void);
305 /// @cond HAL_FFE_INTERNAL_API
306 
307 /*!
308  * \fn          UINT32_t get_algsenabled_cnt(UINT32_t ulMailboxVal)
309  * \brief       Computes the total number of algorithms enabled based on mailbox value
310  * \param       ulMailboxVal -- Mailbox value
311  * \return      Alg count
312  */
313 static UINT32_t FFE_GetAlgsEnabledCnt(UINT32_t ulMailboxVal);
314 /*!
315  * \fn 	        void FFE_Compute_New_Samplerate(UINT16_t usMailbox)
316  * \brief       Based on mailbox value, it will check if any other algorithm is enabled,
317  *              if enabled then change the FFE sample time to the next max sample rate. *
318  * \param       usMailbox -- Mailbox bit.
319  */
320 static void FFE_Compute_New_Samplerate(UINT16_t usMailbox);
321 /*!
322  * \fn          void FFE_UpdateCfg(UINT8_t ucMbUpdate, xSensor_alg_config_1 *cfg1, xSensor_alg_config_2 *cfg2)
323  * \brief       This function updates the new FFE configuration.
324  * \param       ucMbUpdate -- Flag to set if Mailbox update is needed or not
325  * \param       *cfg1	   -- Pointer to Sensor_alg_Config1 structure
326  * \param       *cfg2 	   -- Pointer to Sensor_alg_config2 structure *
327  */
328 static void FFE_UpdateCfg(UINT8_t ucMbUpdate, xSensor_alg_config_1 *cfg1, xSensor_alg_config_2 *cfg2);
329 /*!
330  * \fn          static HAL_StatusTypeDef FFE_Get_AlgConfig(UINT8_t ucId, xSensor_alg_config_1 **config_1, xSensor_alg_config_2 **config_2)
331  * \brief       Traverse through sensor config structure list to identify corresponding sensor configuration
332  * \param       Id      -- sensor ID
333  * \param       config1 -- pointer to sensor configuration for single dependency sensor
334  * \param       config2 -- pointer to sensor configuration for two dependency sensor
335  * \return      HAL Status
336  */
337 static HAL_StatusTypeDef FFE_Get_AlgConfig(UINT8_t ucId, xSensor_alg_config_1 **config_1, xSensor_alg_config_2 **config_2);
338 /*!
339  * \fn 		static HAL_StatusTypeDef FFE_Parse_ExpoSection(void)
340  * \brief 	This function parses the export variables section in M4 SRAM
341  * \param 	None
342  * \return      HAL Status
343  */
344 static HAL_StatusTypeDef FFE_Parse_ExpoSection(void);
345 /*!
346  * \fn 		static UINT8_t FFE_CheckBitEnable(UINT32_t ulVar)
347  * \brief 	Compute the enabled bit position from Mailbox value
348  * \param 	ulVar -- Algorithm mailbox value
349  * \return	bit index which is set to 1.
350  */
351 static UINT8_t FFE_CheckBitEnable(UINT32_t ulVar);
352 /*!
353  * \fn 		static void FFE_Get_SensIdInfo(UINT8_t *ucSensId)
354  * \brief 	Traverse through the alg structure and store the sensor IDs
355  * \brief 	in an array based on mailbox bit position
356  * \param 	ucSensID -- Sensor ID
357  */
358 static void FFE_Get_SensIdInfo(UINT8_t *ucSensId);
359 /*!
360  * \fn		static void FFE_Enable(UINT8_t ucEnable)
361  * \brief 	Function to enable power & clock domains for FFE subsystem
362  * \param	ucEnable --- Enable Flag
363  */
364 static void FFE_Enable(UINT8_t ucEnable);
365 /*!
366  * \fn 		static void FFE_Set_Sampletime(UINT16_t usTimeinMS)
367  * \brief 	This function sets the new FFE sample time
368  * \param 	usTimeinMS -- Sample Time in milliseconds
369  */
370 static void FFE_Set_Sampletime(UINT16_t usTimeinMS);
371 /*!
372  * \fn 		static void FFE_AlgEnable(UINT8_t *ucBuf,xSensor_alg_config_1 *config1,xSensor_alg_config_2 *config2)
373  * \brief	Function to enable the corresponding Algorithm inside FFE
374  * \param	ucBuf	---	User Command buffer
375  * \param	config1	--- pointer to alg config1 structure
376  * \param	config2	--- pointer to alg config2 structure
377  */
378 static void FFE_AlgEnable(UINT8_t *ucBuf,xSensor_alg_config_1 *config1,xSensor_alg_config_2 *config2);
379 /*!
380  * \fn 		static void FFE_SampleRateCfg(UINT8_t *ucBuf, xSensor_alg_config_1 *config1,xSensor_alg_config_2 *config2)
381  * \brief	Function to configure the FFE sample rate inside FFE
382  * \param	ucBuf	--- User Command buffer
383  * \param	config1	--- pointer to alg config1 structure
384  * \param	config2	--- pointer to alg config2 structure
385  */
386 static void FFE_SampleRateCfg(UINT8_t *ucBuf, xSensor_alg_config_1 *config1,xSensor_alg_config_2 *config2);
387 /*!
388  * \fn 		static void FFE_CfgBatchSize(UINT8_t *ucBuf, xSensor_alg_config_1 *config1,xSensor_alg_config_2 *config2)
389  * \brief	Function to configure the batch size inside FFE
390  * \param	ucBuf	--- User Command buffer
391  * \param	config1	--- pointer to alg config1 structure
392  * \param	config2	--- pointer to alg config2 structure
393  */
394 static void FFE_CfgBatchSize(UINT8_t *ucBuf, xSensor_alg_config_1 *config1,xSensor_alg_config_2 *config2);
395 /*!
396  * \fn 		static void FFE_AlgOutputCfg(UINT8_t *ucBuf, xSensor_alg_config_1 *config1,xSensor_alg_config_2 *config2)
397  * \brief	Function to configure the sensor packet output mode configure inside FFE
398  * \param	ucBuf	--- User Command buffer
399  * \param	config1	--- pointer to alg config1 structure
400  * \param	config2	--- pointer to alg config2 structure
401  */
402 static void FFE_AlgOutputCfg(UINT8_t *ucBuf, xSensor_alg_config_1 *config1,xSensor_alg_config_2 *config2);
403 /*!
404  * \fn 		static HAL_StatusTypeDef FFE_ConfigureSM(UINT8_t ucSM_type)
405  * \brief 	This function configures SM 0/1 SDA and SCL pads
406  * \param 	SM_type -- Sensor Manager type (SM0/SM1)
407  * \return	Status
408  */
409 static HAL_StatusTypeDef FFE_ConfigureSM(UINT8_t ucSM_type);
410 
411 
412 /// @endcond
413 
414 #endif /* HAL_INC_EOSS3_HAL_FFE_H_ */
415