1 /*
2  * Copyright 2018-2019 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     CLOCK_EnableClock(kCLOCK_PowerQuad);
55 #endif
56 #if !(defined(FSL_SDK_DISABLE_DRIVER_RESET_CONTROL) && FSL_SDK_DISABLE_DRIVER_RESET_CONTROL)
57     RESET_PeripheralReset(kPOWERQUAD_RST_SHIFT_RSTn);
58 #endif
59 
60     /* Enable event used for WFE. */
61     base->EVENTEN = POWERQUAD_EVENTEN_EVENT_OFLOW_MASK | POWERQUAD_EVENTEN_EVENT_NAN_MASK |
62                     POWERQUAD_EVENTEN_EVENT_FIXED_MASK | POWERQUAD_EVENTEN_EVENT_UFLOW_MASK |
63                     POWERQUAD_EVENTEN_EVENT_BERR_MASK | POWERQUAD_EVENTEN_EVENT_COMP_MASK;
64 }
65 
PQ_Deinit(POWERQUAD_Type * base)66 void PQ_Deinit(POWERQUAD_Type *base)
67 {
68 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
69     CLOCK_DisableClock(kCLOCK_PowerQuad);
70 #endif
71 }
72 
PQ_SetFormat(POWERQUAD_Type * base,pq_computationengine_t engine,pq_format_t format)73 void PQ_SetFormat(POWERQUAD_Type *base, pq_computationengine_t engine, pq_format_t format)
74 {
75     pq_config_t config;
76 
77     PQ_GetDefaultConfig(&config);
78 
79     /* 32-bit Float point */
80     if (kPQ_Float == format)
81     {
82         config.inputAFormat   = kPQ_Float;
83         config.inputAPrescale = 0;
84         config.inputBFormat   = kPQ_Float;
85         config.inputBPrescale = 0;
86         config.outputFormat   = kPQ_Float;
87         config.outputPrescale = 0;
88         config.tmpFormat      = kPQ_Float;
89         config.tmpPrescale    = 0;
90     }
91     /* 32-bit Fixed point */
92     if (kPQ_32Bit == format)
93     {
94         config.inputAFormat   = kPQ_32Bit;
95         config.inputAPrescale = 0;
96         config.inputBFormat   = kPQ_32Bit;
97         config.inputBPrescale = 0;
98         config.outputFormat   = kPQ_32Bit;
99         config.outputPrescale = 0;
100         config.tmpFormat      = kPQ_Float;
101         config.tmpPrescale    = 0;
102     }
103     /* 16-bit Fixed point */
104     if (kPQ_16Bit == format)
105     {
106         config.inputAFormat   = kPQ_16Bit;
107         config.inputAPrescale = 0;
108         config.inputBFormat   = kPQ_16Bit;
109         config.inputBPrescale = 0;
110         config.outputFormat   = kPQ_16Bit;
111         config.outputPrescale = 0;
112         config.tmpFormat      = kPQ_Float;
113         config.tmpPrescale    = 0;
114     }
115 
116     if (CP_FFT == (uint8_t)engine)
117     {
118         config.machineFormat = kPQ_32Bit;
119     }
120     else
121     {
122         config.machineFormat = kPQ_Float;
123     }
124 
125     PQ_SetConfig(base, &config);
126 }
127