1 /*
2  * Copyright 2018-2022 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include "fsl_powerquad.h"
9 
10 /*******************************************************************************
11  * Definitions
12  ******************************************************************************/
13 
14 /* Component ID definition, used by tools. */
15 #ifndef FSL_COMPONENT_ID
16 #define FSL_COMPONENT_ID "platform.drivers.powerquad_basic"
17 #endif
18 
19 /*******************************************************************************
20  * Code
21  ******************************************************************************/
PQ_GetDefaultConfig(pq_config_t * config)22 void PQ_GetDefaultConfig(pq_config_t *config)
23 {
24     config->inputAFormat   = kPQ_Float;
25     config->inputAPrescale = 0;
26     config->inputBFormat   = kPQ_Float;
27     config->inputBPrescale = 0;
28     config->outputFormat   = kPQ_Float;
29     config->outputPrescale = 0;
30     config->tmpFormat      = kPQ_Float;
31     config->tmpPrescale    = 0;
32     config->machineFormat  = kPQ_Float;
33     config->tmpBase        = (uint32_t *)0xE0000000U;
34 }
35 
PQ_SetConfig(POWERQUAD_Type * base,const pq_config_t * config)36 void PQ_SetConfig(POWERQUAD_Type *base, const pq_config_t *config)
37 {
38     assert(NULL != config);
39 
40     base->TMPBASE   = (uint32_t)config->tmpBase;
41     base->INAFORMAT = ((uint32_t)config->inputAPrescale << 8U) | ((uint32_t)config->inputAFormat << 4U) |
42                       (uint32_t)config->machineFormat;
43     base->INBFORMAT = ((uint32_t)config->inputBPrescale << 8U) | ((uint32_t)config->inputBFormat << 4U) |
44                       (uint32_t)config->machineFormat;
45     base->TMPFORMAT =
46         ((uint32_t)config->tmpPrescale << 8U) | ((uint32_t)config->tmpFormat << 4U) | (uint32_t)config->machineFormat;
47     base->OUTFORMAT = ((uint32_t)config->outputPrescale << 8U) | ((uint32_t)config->outputFormat << 4U) |
48                       (uint32_t)config->machineFormat;
49 }
50 
PQ_Init(POWERQUAD_Type * base)51 void PQ_Init(POWERQUAD_Type *base)
52 {
53 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
54 #if defined(POWERQUAD_CLOCKS)
55     CLOCK_EnableClock(kCLOCK_PowerQuad);
56 #endif
57 #endif
58 #if !(defined(FSL_SDK_DISABLE_DRIVER_RESET_CONTROL) && FSL_SDK_DISABLE_DRIVER_RESET_CONTROL)
59 #if defined(POWERQUAD_RSTS)
60     RESET_PeripheralReset(kPOWERQUAD_RST_SHIFT_RSTn);
61 #endif
62 #endif
63 
64     /* Enable event used for WFE. */
65     base->EVENTEN = POWERQUAD_EVENTEN_EVENT_OFLOW_MASK | POWERQUAD_EVENTEN_EVENT_NAN_MASK |
66                     POWERQUAD_EVENTEN_EVENT_FIXED_MASK | POWERQUAD_EVENTEN_EVENT_UFLOW_MASK |
67                     POWERQUAD_EVENTEN_EVENT_BERR_MASK | POWERQUAD_EVENTEN_EVENT_COMP_MASK;
68 }
69 
PQ_Deinit(POWERQUAD_Type * base)70 void PQ_Deinit(POWERQUAD_Type *base)
71 {
72 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
73 #if defined(POWERQUAD_CLOCKS)
74     CLOCK_DisableClock(kCLOCK_PowerQuad);
75 #endif
76 #endif
77 }
78 
PQ_SetFormat(POWERQUAD_Type * base,pq_computationengine_t engine,pq_format_t format)79 void PQ_SetFormat(POWERQUAD_Type *base, pq_computationengine_t engine, pq_format_t format)
80 {
81     pq_config_t config;
82 
83     PQ_GetDefaultConfig(&config);
84 
85     /* 32-bit Float point */
86     if (kPQ_Float == format)
87     {
88         config.inputAFormat   = kPQ_Float;
89         config.inputAPrescale = 0;
90         config.inputBFormat   = kPQ_Float;
91         config.inputBPrescale = 0;
92         config.outputFormat   = kPQ_Float;
93         config.outputPrescale = 0;
94         config.tmpFormat      = kPQ_Float;
95         config.tmpPrescale    = 0;
96     }
97     /* 32-bit Fixed point */
98     if (kPQ_32Bit == format)
99     {
100         config.inputAFormat   = kPQ_32Bit;
101         config.inputAPrescale = 0;
102         config.inputBFormat   = kPQ_32Bit;
103         config.inputBPrescale = 0;
104         config.outputFormat   = kPQ_32Bit;
105         config.outputPrescale = 0;
106         config.tmpFormat      = kPQ_Float;
107         config.tmpPrescale    = 0;
108     }
109     /* 16-bit Fixed point */
110     if (kPQ_16Bit == format)
111     {
112         config.inputAFormat   = kPQ_16Bit;
113         config.inputAPrescale = 0;
114         config.inputBFormat   = kPQ_16Bit;
115         config.inputBPrescale = 0;
116         config.outputFormat   = kPQ_16Bit;
117         config.outputPrescale = 0;
118         config.tmpFormat      = kPQ_Float;
119         config.tmpPrescale    = 0;
120     }
121 
122     if (CP_FFT == (uint8_t)engine)
123     {
124         config.machineFormat = kPQ_32Bit;
125     }
126     else
127     {
128         config.machineFormat = kPQ_Float;
129     }
130 
131     PQ_SetConfig(base, &config);
132 }
133