instaspin_foc
proj_lab01.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2012, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
36 
38 
39 
44 
45 //
46 // **************************************************************************
47 // the includes
48 
49 // system includes
50 #include <math.h>
51 
52 
53 // modules
54 #include "sw/modules/math/src/32b/math.h"
56 
57 
58 // drivers
59 
60 
61 // platforms
62 #include "main.h"
63 
64 
65 // **************************************************************************
66 // the defines
67 
68 #define LED_BLINK_FREQ_Hz 5
69 
70 // **************************************************************************
71 // the globals
72 
73 uint_least32_t gLEDcnt = 0; // Counter used to divide down the ISR rate for visually blinking an LED
74 
75 #ifdef F2802xF
76 #pragma DATA_SECTION(halHandle,"rom_accessed_data");
77 #endif
78 HAL_Handle halHandle; // Handle to the Inverter hardware abstraction layer
79 
80 #ifdef F2802xF
81 #pragma DATA_SECTION(gUserParams,"rom_accessed_data");
82 #endif
83 USER_Params gUserParams; // Contains the user.h settings
84 
85 HAL_PwmData_t gPwmData = {0,0,0}; // Contains PWM duty cycles in global Q format
86 
87 HAL_AdcData_t gAdcData = {0,0,0,0,0,0,0}; // Contains Current and Voltage ADC readings in global Q format
88 
90 
91 #ifdef FLASH
92 // Used for running BackGround in flash, and ISR in RAM
93 extern uint16_t *RamfuncsLoadStart, *RamfuncsLoadEnd, *RamfuncsRunStart;
94 
95 #ifdef F2802xF
96 extern uint16_t *econst_start, *econst_end, *econst_ram_load;
97 extern uint16_t *switch_start, *switch_end, *switch_ram_load;
98 #endif
99 
100 #endif
101 
102 
103 #ifdef DRV8301_SPI
104 // Watch window interface to the 8301 SPI
105 DRV_SPI_8301_Vars_t gDrvSpi8301Vars;
106 #endif
107 
108 #ifdef DRV8305_SPI
109 // Watch window interface to the 8305 SPI
110 DRV_SPI_8305_Vars_t gDrvSpi8305Vars;
111 #endif
112 
113 // **************************************************************************
114 // the functions
115 
116 void main(void)
117 {
118  // Only used if running from FLASH
119  // Note that the variable FLASH is defined by the project
120  #ifdef FLASH
121  // Copy time critical code and Flash setup code to RAM
122  // The RamfuncsLoadStart, RamfuncsLoadEnd, and RamfuncsRunStart
123  // symbols are created by the linker. Refer to the linker files.
124  memCopy((uint16_t *)&RamfuncsLoadStart,(uint16_t *)&RamfuncsLoadEnd,(uint16_t *)&RamfuncsRunStart);
125 
126  #ifdef F2802xF
127  //copy .econst to unsecure RAM
128  if(*econst_end - *econst_start)
129  {
130  memCopy((uint16_t *)&econst_start,(uint16_t *)&econst_end,(uint16_t *)&econst_ram_load);
131  }
132 
133  //copy .switch ot unsecure RAM
134  if(*switch_end - *switch_start)
135  {
136  memCopy((uint16_t *)&switch_start,(uint16_t *)&switch_end,(uint16_t *)&switch_ram_load);
137  }
138  #endif
139 
140  #endif
141 
142  // initialize the hardware abstraction layer
143  halHandle = HAL_init(&hal,sizeof(hal));
144 
145 
146  // check for errors in user parameters
147  USER_checkForErrors(&gUserParams);
148 
149 
150  // store user parameter error in global variable
151  gMotorVars.UserErrorCode = USER_getErrorCode(&gUserParams);
152 
153 
154  // do not allow code execution if there is a user parameter error
155  if(gMotorVars.UserErrorCode != USER_ErrorCode_NoError)
156  {
157  for(;;)
158  {
159  gMotorVars.Flag_enableSys = false;
160  }
161  }
162 
163 
164  // initialize the user parameters
165  USER_setParams(&gUserParams);
166 
167 
168  // set the hardware abstraction layer parameters
169  HAL_setParams(halHandle,&gUserParams);
170 
171 #ifdef LAUNCHPAD
172  // Setup GPIOs 0 and 1 as outputs for use in project lab1 only.
173  // This is specific to the launchpad because its LEDs are also used by the PWM channels.
174  HAL_setupLaunchPadGpio0and1(halHandle);
175 #endif
176 
177  // setup faults
178  HAL_setupFaults(halHandle);
179 
180 
181  // initialize the interrupt vector table
182  HAL_initIntVectorTable(halHandle);
183 
184 
185  // enable the ADC interrupts
186  HAL_enableAdcInts(halHandle);
187 
188 
189  // enable global interrupts
190  HAL_enableGlobalInts(halHandle);
191 
192 
193  // enable debug interrupts
194  HAL_enableDebugInt(halHandle);
195 
196 
197  // disable the PWM
198  HAL_disablePwm(halHandle);
199 
200 
201 #ifdef DRV8301_SPI
202  // turn on the DRV8301 if present
203  HAL_enableDrv(halHandle);
204  // initialize the DRV8301 interface
205  HAL_setupDrvSpi(halHandle,&gDrvSpi8301Vars);
206 #endif
207 
208 #ifdef DRV8305_SPI
209  // turn on the DRV8305 if present
210  HAL_enableDrv(halHandle);
211  // initialize the DRV8305 interface
212  HAL_setupDrvSpi(halHandle,&gDrvSpi8305Vars);
213 #endif
214 
215 
216  // For ever loop
217  while(true);
218 
219 } // end of main() function
220 
221 
222 interrupt void mainISR(void)
223 {
224 
225  // toggle status LED
226  if(gLEDcnt++ > (uint_least32_t)(USER_ISR_FREQ_Hz / LED_BLINK_FREQ_Hz))
227  {
228  HAL_toggleLed(halHandle,(GPIO_Number_e)HAL_Gpio_LED2);
229  gLEDcnt = 0;
230  }
231 
232 
233  // acknowledge the ADC interrupt
234  HAL_acqAdcInt(halHandle,ADC_IntNumber_1);
235 
236 
237  // convert the ADC data
238  HAL_readAdcData(halHandle,&gAdcData);
239 
240 
241  // ADC processing and pwm result code goes here
242 
243 
244  // write the PWM compare values
245  HAL_writePwmData(halHandle,&gPwmData);
246 
247 
248  return;
249 } // end of mainISR() function
250 
251 
253 // end of file
254 
255 
256 
257 
void HAL_enableGlobalInts(HAL_Handle handle)
void HAL_enableAdcInts(HAL_Handle handle)
USER_ErrorCode_e UserErrorCode
Definition: main.h:150
void HAL_setupLaunchPadGpio0and1(HAL_Handle handle)
static void HAL_writePwmData(HAL_Handle handle, HAL_PwmData_t *pPwmData)
static void HAL_acqAdcInt(HAL_Handle handle, const ADC_IntNumber_e intNumber)
HAL_Handle halHandle
The hal handle.
Definition: proj_lab01.c:78
void memCopy(uint16_t *srcStartAddr, uint16_t *srcEndAddr, uint16_t *dstAddr)
volatile MOTOR_Vars_t gMotorVars
Definition: proj_lab01.c:89
interrupt void mainISR(void)
The main interrupt service (ISR) routine.
Definition: proj_lab01.c:222
void USER_setParams(USER_Params *pUserParams)
Sets the user parameter values.
#define HAL_toggleLed
Defines the structures, global initialization, and functions used in MAIN.
HAL_AdcData_t gAdcData
Defines the ADC data.
Definition: proj_lab01.c:87
void HAL_setupFaults(HAL_Handle handle)
uint_least32_t gLEDcnt
Definition: proj_lab01.c:73
USER_ErrorCode_e USER_getErrorCode(USER_Params *pUserParams)
Gets the error code in the user parameters.
void HAL_enableDrv(HAL_Handle handle)
void main(void)
Definition: proj_lab01.c:116
void HAL_setupDrvSpi(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
void HAL_enableDebugInt(HAL_Handle handle)
HAL_PwmData_t gPwmData
Defines the PWM data.
Definition: proj_lab01.c:85
#define MOTOR_Vars_INIT
Initialization values of global variables.
Definition: main.h:76
#define USER_ISR_FREQ_Hz
Defines the Interrupt Service Routine (ISR) frequency, Hz.
Definition: user.h:173
static void HAL_disablePwm(HAL_Handle handle)
HAL_Handle HAL_init(void *pMemory, const size_t numBytes)
static void HAL_readAdcData(HAL_Handle handle, HAL_AdcData_t *pAdcData)
HAL_Obj hal
bool Flag_enableSys
Definition: main.h:136
static void HAL_initIntVectorTable(HAL_Handle handle)
void HAL_setParams(HAL_Handle handle, const USER_Params *pUserParams)
USER_Params gUserParams
The user parameters.
Definition: proj_lab01.c:83
#define LED_BLINK_FREQ_Hz
Definition: proj_lab01.c:68
void USER_checkForErrors(USER_Params *pUserParams)
Checks for errors in the user parameter values.
GPIO_Number_e