forked from ReikoR/instaspin-bldc-driver-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhal_obj.h
223 lines (165 loc) · 6.9 KB
/
hal_obj.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#ifndef _HAL_OBJ_H_
#define _HAL_OBJ_H_
/* --COPYRIGHT--,BSD
* Copyright (c) 2012, Texas Instruments Incorporated
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* --/COPYRIGHT--*/
//! \file solutions/instaspin_foc/src/hal_obj.h
//! \brief Defines the structures for the HAL object
//!
//! (C) Copyright 2012, Texas Instruments, Inc.
// drivers
#include "drivers/adc/adc.h"
#include "drivers/clk/clk.h"
#include "drivers/cpu/cpu.h"
#include "drivers/flash/flash.h"
#include "drivers/gpio/gpio.h"
#include "drivers/osc/osc.h"
#include "drivers/pie/pie.h"
#include "drivers/pll/pll.h"
#include "drivers/pwm/pwm.h"
#include "drivers/pwmdac/pwmdac.h"
#include "drivers/pwr/pwr.h"
#include "drivers/spi/spi.h"
#include "drivers/timer/timer.h"
#include "drivers/wdog/wdog.h"
#include "drivers/drv8301/drv8301.h"
// modules
#include "modules/offset/offset.h"
#include "modules/types/types.h"
#include "modules/usDelay/usDelay.h"
// platforms
#include "user.h"
//!
//!
//! \defgroup HAL_OBJ HAL_OBJ
//!
//@{
#ifdef __cplusplus
extern "C" {
#endif
//! \brief Defines the ADC data
//! \details This data structure contains the voltage and current values that are used when
//! performing a HAL_AdcRead and then this structure is passed to the CTRL controller
//! and the FAST estimator.
//!
typedef struct _HAL_AdcData_t_
{
MATH_vec3 I; //!< the current values
MATH_vec3 V; //!< the voltage values
_iq dcBus; //!< the dcBus value
} HAL_AdcData_t;
//! \brief Defines the DAC data
//! \details This data structure contains the pwm values that are used for the DAC output
//! on a lot of the hardware kits for debugging.
//!
typedef struct _HAL_DacData_t_
{
_iq value[4]; //!< the DAC data
} HAL_DacData_t;
//! \brief Defines the PWM data
//! \details This structure contains the pwm voltage values for the three phases. A
//! HAL_PwmData_t variable is filled with values from, for example, a space
//! vector modulator and then sent to functions like HAL_writePwmData() to
//! write to the PWM peripheral.
//!
typedef struct _HAL_PwmData_t_
{
MATH_vec3 Tabc; //!< the PWM time-durations for each motor phase
} HAL_PwmData_t;
//! \brief Defines the hardware abstraction layer (HAL) data
//! \details The HAL object contains all handles to peripherals. When accessing a
//! peripheral on a processor, use a HAL function along with the HAL handle
//! for that processor to access its peripherals.
//!
typedef struct _HAL_Obj_
{
ADC_Handle adcHandle; //!< the ADC handle
CLK_Handle clkHandle; //!< the clock handle
CPU_Handle cpuHandle; //!< the CPU handle
FLASH_Handle flashHandle; //!< the flash handle
GPIO_Handle gpioHandle; //!< the GPIO handle
OFFSET_Handle offsetHandle_I[3]; //!< the handles for the current offset estimators
OFFSET_Obj offset_I[3]; //!< the current offset objects
OFFSET_Handle offsetHandle_V[3]; //!< the handles for the voltage offset estimators
OFFSET_Obj offset_V[3]; //!< the voltage offset objects
OSC_Handle oscHandle; //!< the oscillator handle
PIE_Handle pieHandle; //<! the PIE handle
PLL_Handle pllHandle; //!< the PLL handle
PWM_Handle pwmHandle[3]; //<! the PWM handles
PWMDAC_Handle pwmDacHandle[3]; //<! the PWMDAC handles
PWR_Handle pwrHandle; //<! the power handle
TIMER_Handle timerHandle[3]; //<! the timer handles
WDOG_Handle wdogHandle; //!< the watchdog handle
HAL_AdcData_t adcBias; //!< the ADC bias
_iq current_sf; //!< the current scale factor, amps_pu/cnt
_iq voltage_sf; //!< the voltage scale factor, volts_pu/cnt
uint_least8_t numCurrentSensors; //!< the number of current sensors
uint_least8_t numVoltageSensors; //!< the number of voltage sensors
SPI_Handle spiAHandle; //!< the SPIA handle
SPI_Obj spiA; //!< the SPIA object
DRV8301_Handle drv8301Handle; //!< the drv8301 interface handle
DRV8301_Obj drv8301; //!< the drv8301 interface object
} HAL_Obj;
//! \brief Defines the HAL handle
//! \details The HAL handle is a pointer to a HAL object. In all HAL functions
//! the HAL handle is passed so that the function knows what peripherals
//! are to be accessed.
//!
typedef struct _HAL_Obj_ *HAL_Handle;
//! \brief Defines the HAL object
//!
extern HAL_Obj hal;
//! \brief Runs offset estimation
//! \details Offsets of the voltage and current feedbacks are required for good low
//! speed performance of the motor drive.
//! \param[in] handle The hardware abstraction layer (HAL) handle
//! \param[in] pAdcData The pointer to the ADC data
inline void HAL_runOffsetEst(HAL_Handle handle,const HAL_AdcData_t *pAdcData)
{
uint_least8_t cnt;
HAL_Obj *obj = (HAL_Obj *)handle;
// estimate the current offsets
for(cnt=0;cnt<obj->numCurrentSensors;cnt++)
{
OFFSET_run(obj->offsetHandle_I[cnt],pAdcData->I.value[cnt]);
}
// estimate the voltage offsets
for(cnt=0;cnt<obj->numVoltageSensors;cnt++)
{
OFFSET_run(obj->offsetHandle_V[cnt],pAdcData->V.value[cnt]);
}
return;
} // end of HAL_runOffsetEst() function
#ifdef __cplusplus
}
#endif // extern "C"
//@} // ingroup
#endif // end of _HAL_OBJ_H_ definition