AM263x Motor Control SDK  09.02.00
hall.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2024 Texas Instruments Incorporated
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * 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
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef _HALL_H_
34 #define _HALL_H_
35 
36 #ifdef __cplusplus
37 extern "C"
38 {
39 #endif
40 
53 // the includes
54 #include <stdlib.h>
55 #include <stdint.h>
56 #include <math.h>
57 
58 //modules
59 #include "math_types.h"
60 #include "userParams.h"
61 #include "ti_drivers_config.h"
62 
63 // **************************************************************************
64 // the defines
65 
66 
67 // **************************************************************************
68 // the typedefs
69 
70 // State machine typedef for HALL status
71 typedef enum
72 {
73  HALL_IDLE = 0,
76  HALL_RUN = 3
78 
79 #ifdef HALL_CAL
80 #define CAL_BUF_NUM 13
81 #endif //HALL_CAL
82 
83 
86 typedef struct _HALL_Obj_
87 {
88  float32_t capScaler; // Scaler converting 1/N CPU cycles
89  float32_t pwmScaler; // Scaler converting 1/N CPU cycles
94  uint32_t timeStampCAP; //
95  uint32_t timeCountPWM; //
96  uint32_t timeCount[2]; //
97  uint32_t timeCountMax; //
98 
99  float32_t thetaBuff[7]; // 1~6 are valid value
103  uint32_t gpioHallU; // the GPIO for Hall U
104  uint32_t gpioHallV; // the GPIO for Hall V
105  uint32_t gpioHallW; // the GPIO for Hall W
106  uint32_t gpioHallUBase; // the GPIO base address for Hall U
107  uint32_t gpioHallVBase; // the GPIO base address for Hall V
108  uint32_t gpioHallWBase; // the GPIO base address for Hall W
109  uint16_t hallIndex; //
110  uint16_t hallIndexPrev; //
111  uint16_t hallPrev[7]; //
112  uint16_t hallDirection; //
114 
115 #ifdef HALL_CAL
116  float32_t thetaCalBuff[7]; //
117  float32_t thetaIndexBuff[CAL_BUF_NUM]; //
118  uint32_t hallIndexNow; //
119  uint16_t hallIndexPre; //
120  uint16_t hallIndexBuf[CAL_BUF_NUM]; //
121  uint16_t hallIndexFlag; //
122 #endif //HALL_CAL
123 } HALL_Obj;
124 
125 
128 typedef struct _HALL_Obj_ *HALL_Handle;
129 
130 // **************************************************************************
131 // the function prototypes
132 
133 //*****************************************************************************
134 //
140 //*****************************************************************************
141 extern HALL_Handle HALL_init(void *pMemory, const size_t numBytes);
142 
143 //*****************************************************************************
144 //
149 //*****************************************************************************
150 extern void HALL_setParams(HALL_Handle handle, const USER_Params *pUserParams);
151 
152 //*****************************************************************************
153 //
158 //*****************************************************************************
159 extern void HALL_setAngleBuf(HALL_Handle handle, const float32_t *ptrAngleBuf);
160 
161 //*****************************************************************************
162 //
166 //*****************************************************************************
167 static inline void HALL_resetParams(HALL_Handle handle)
168 {
169  HALL_Obj *obj = (HALL_Obj *)handle;
170 
171  obj->timeCountPWM = obj->timeCountMax;
172  obj->timeCount[0] = obj->timeCountMax;
173  obj->timeCount[1] = obj->timeCountMax;
174 
175  obj->hallIndexPrev = 0;
176  obj->hallDirection = 0;
177 
178  return;
179 }
180 
181 //*****************************************************************************
182 //
192 //*****************************************************************************
193 static inline void HALL_setGPIOs(HALL_Handle handle, const uint32_t gpioHallU,
194  const uint32_t gpioHallV, const uint32_t gpioHallW,
195  const uint32_t gpioHallUBase, const uint32_t gpioHallVBase,
196  const uint32_t gpioHallWBase)
197 {
198  HALL_Obj *obj = (HALL_Obj *)handle;
199 
200  obj->gpioHallU = gpioHallU;
201  obj->gpioHallV = gpioHallV;
202  obj->gpioHallW = gpioHallW;
203  obj->gpioHallUBase = gpioHallUBase;
204  obj->gpioHallVBase = gpioHallVBase;
205  obj->gpioHallWBase = gpioHallWBase;
206 
207  return;
208 }
209 
210 //*****************************************************************************
211 //
215 //*****************************************************************************
217 {
218  HALL_Obj *obj = (HALL_Obj *)handle;
219 
220  return(obj->thetaHall_rad);
221 }
222 
223 //*****************************************************************************
224 //
228 //*****************************************************************************
229 static inline uint16_t HALL_getInputState(HALL_Handle handle)
230 {
231  HALL_Obj *obj = (HALL_Obj *)handle;
232 
233  uint32_t hallState = 0;
234 
235  hallState = GPIO_pinRead(obj->gpioHallUBase, obj->gpioHallU);
236  hallState += GPIO_pinRead(obj->gpioHallVBase, obj->gpioHallV)<<1;
237  hallState += GPIO_pinRead(obj->gpioHallWBase, obj->gpioHallW)<<2;
238 
239  return(hallState & 0x0007);
240 }
241 
242 //*****************************************************************************
243 //
247 //*****************************************************************************
249 {
250  HALL_Obj *obj = (HALL_Obj *)handle;
251 
252  return(obj->speedHall_Hz);
253 }
254 
255 //*****************************************************************************
256 //
261 //*****************************************************************************
262 static inline void HALL_setSpeed_Hz(HALL_Handle handle, const float32_t speedHall_Hz)
263 {
264  HALL_Obj *obj = (HALL_Obj *)handle;
265 
266  obj->speedHall_Hz = speedHall_Hz;
267 
268  return;
269 }
270 
271 //*****************************************************************************
272 //
277 //*****************************************************************************
278 static inline
279 void HALL_setAngleDelta_rad(HALL_Handle handle, const float32_t thetaDelta_rad)
280 {
281  HALL_Obj *obj = (HALL_Obj *)handle;
282 
283  obj->thetaDelta_rad = thetaDelta_rad;
284 
285  return;
286 }
287 
288 //*****************************************************************************
289 //
294 //*****************************************************************************
295 static inline void HALL_setTimeStamp(HALL_Handle handle, const uint32_t timeStamp)
296 {
297  HALL_Obj *obj = (HALL_Obj *)handle;
298 
299  obj->timeStampCAP = timeStamp;
300 
301  return;
302 }
303 
304 #ifdef HALL_CAL
305 //*****************************************************************************
306 //
311 //*****************************************************************************
312 static inline void HALL_calibrateIndexAngle(HALL_Handle handle, float32_t angle)
313 {
314  HALL_Obj *obj = (HALL_Obj *)handle;
315  obj->hallIndexNow = HALL_getInputState(handle);
316 
317  if(obj->hallIndexNow != obj->hallIndexPre)
318  {
319  obj->hallIndexPre = obj->hallIndexNow;
320 
321  if(obj->hallIndexFlag == 0)
322  {
323  obj->thetaIndexBuff[obj->hallIndexNow] = angle;
324  obj->hallIndexBuf[obj->hallIndexNow] = obj->hallIndexNow;
325 
326  if(obj->hallIndexNow == 0x01)
327  {
328  obj->hallIndexFlag = 1;
329  }
330  }
331  else
332  {
333  obj->thetaIndexBuff[6 + obj->hallIndexNow] = angle;
334  obj->hallIndexBuf[6 + obj->hallIndexNow] = obj->hallIndexNow;
335 
336  if(obj->hallIndexNow == 0x01)
337  {
338  obj->hallIndexFlag = 0;
339  }
340  }
341 
342  obj->thetaCalBuff[obj->hallIndexNow] = 0.5f *
343  (obj->thetaIndexBuff[obj->hallIndexNow] +
344  obj->thetaIndexBuff[6 + obj->hallIndexNow]);
345  }
346 
347  return;
348 }
349 #endif //HALL_CAL
350 
351 //*****************************************************************************
352 //
357 //*****************************************************************************
358 static inline void HALL_setForceAngleAndIndex(HALL_Handle handle, float32_t speedRef)
359 {
360  HALL_Obj *obj = (HALL_Obj *)handle;
361  obj->hallIndex = HALL_getInputState(handle);
362 
363  if(speedRef > 0.0f)
364  {
365  obj->hallIndexPrev = obj->hallPrev[obj->hallIndex];
366  obj->hallDirection = 0;
367 
368  obj->thetaHall_rad = obj->thetaBuff[obj->hallIndex] - obj->thetaDelta_rad;
369  }
370  else
371  {
372  obj->hallIndexPrev = obj->hallIndex;
373  obj->hallDirection = 1;
374 
375  obj->thetaHall_rad = obj->thetaBuff[obj->hallIndex] + obj->thetaDelta_rad;
376  }
377 
378  if(obj->thetaHall_rad >= MATH_PI)
379  {
380  obj->thetaHall_rad = obj->thetaHall_rad - MATH_TWO_PI;
381  }
382  else if(obj->thetaHall_rad <= -MATH_PI)
383  {
384  obj->thetaHall_rad = obj->thetaHall_rad + MATH_TWO_PI;
385  }
386 
387  return;
388 }
389 
390 //*****************************************************************************
391 //
396 //*****************************************************************************
397 #define HALL_calcAngle HALL_run
398 
399 static __attribute__((always_inline))
400 void HALL_run(HALL_Handle handle, float32_t speedRef)
401 {
402  HALL_Obj *obj = (HALL_Obj *)handle;
403 
404  obj->hallIndex = HALL_getInputState(handle);
405  obj->timeCountPWM++;
406 
407  if(obj->hallIndex != obj->hallIndexPrev)
408  {
409  if(speedRef > 0.0f)
410  {
411  obj->thetaHall_rad = obj->thetaBuff[obj->hallIndex] + obj->thetaDelta_rad;
412  obj->hallDirection = 0;
413  }
414  else
415  {
416  obj->thetaHall_rad = obj->thetaBuff[obj->hallIndex] - obj->thetaDelta_rad;
417  obj->hallDirection = 1;
418  }
419 
420  obj->hallIndexPrev = obj->hallIndex;
421 
422  float32_t speedCAP_Hz = 0.0f;
423  if(obj->timeStampCAP != 0)
424  {
425  speedCAP_Hz = obj->capScaler / ((float32_t)obj->timeStampCAP);
426  }
427  else
428  {
429  speedCAP_Hz = 0.0f;
430  }
431 
432  float32_t speedPWM_Hz = obj->pwmScaler /
433  ((float32_t)(obj->timeCountPWM + obj->timeCount[1] + obj->timeCount[0]));
434 
435  if(obj->hallDirection == 0)
436  {
437  obj->speedCAP_Hz = speedCAP_Hz;
438  obj->speedPWM_Hz = speedPWM_Hz;
439  }
440  else
441  {
442  obj->speedCAP_Hz = -speedCAP_Hz;
443  obj->speedPWM_Hz = -speedPWM_Hz;
444  }
445 
446  if(speedPWM_Hz > obj->speedSwitch_Hz)
447  {
448  obj->speedHall_Hz = obj->speedCAP_Hz;
449  }
450  else
451  {
452  obj->speedHall_Hz = obj->speedPWM_Hz;
453  }
454 
455  obj->timeCount[1] = obj->timeCount[0];
456  obj->timeCount[0] = obj->timeCountPWM;
457  obj->timeCountPWM = 0;
458  }
459 
460  if(obj->timeCountPWM > obj->timeCountMax)
461  {
462  obj->speedCAP_Hz = 0.0f;
463  obj->speedPWM_Hz = 0.0f;
464  obj->speedHall_Hz = 0.0f;
465  obj->timeCountPWM = 0;
466  }
467 
468  if(obj->thetaHall_rad >= MATH_PI)
469  {
470  obj->thetaHall_rad = obj->thetaHall_rad - MATH_TWO_PI;
471  }
472  else if(obj->thetaHall_rad <= -MATH_PI)
473  {
474  obj->thetaHall_rad = obj->thetaHall_rad + MATH_TWO_PI;
475  }
476 
477  return;
478 }
479 
480 //*****************************************************************************
481 //
482 // Close the Doxygen group.
484 //
485 //*****************************************************************************
486 
487 //*****************************************************************************
488 //
489 // Mark the end of the C bindings section for C++ compilers.
490 //
491 //*****************************************************************************
492 #ifdef __cplusplus
493 }
494 #endif
495 
496 #endif //end of _HALL_H_ definition
497 
HALL_setAngleBuf
void HALL_setAngleBuf(HALL_Handle handle, const float32_t *ptrAngleBuf)
Sets the angle buffer value for hall estimator.
HALL_Obj::timeCountPWM
uint32_t timeCountPWM
Definition: hall.h:95
HALL_Obj::speedPWM_Hz
float32_t speedPWM_Hz
Definition: hall.h:91
HALL_CALIBRATION
@ HALL_CALIBRATION
Definition: hall.h:75
HALL_Obj
Defines the HALL controller object.
Definition: hall.h:87
HALL_Obj::speedSwitch_Hz
float32_t speedSwitch_Hz
Definition: hall.h:93
HALL_getAngle_rad
static float32_t HALL_getAngle_rad(HALL_Handle handle)
Gets the angle from the hall estimator, rad.
Definition: hall.h:216
HALL_Obj::hallIndexPrev
uint16_t hallIndexPrev
Definition: hall.h:110
HALL_Obj::gpioHallWBase
uint32_t gpioHallWBase
Definition: hall.h:108
HALL_Handle
struct _HALL_Obj_ * HALL_Handle
Defines the HALL handle.
Definition: hall.h:128
HALL_Obj::speedCAP_Hz
float32_t speedCAP_Hz
Definition: hall.h:90
HALL_getInputState
static uint16_t HALL_getInputState(HALL_Handle handle)
Gets the hall sensors input GPIO state.
Definition: hall.h:229
HALL_Obj::gpioHallW
uint32_t gpioHallW
Definition: hall.h:105
HALL_Obj::gpioHallVBase
uint32_t gpioHallVBase
Definition: hall.h:107
HALL_Obj::capScaler
float32_t capScaler
Definition: hall.h:88
HALL_setAngleDelta_rad
static void HALL_setAngleDelta_rad(HALL_Handle handle, const float32_t thetaDelta_rad)
Sets a value to the theta offset for the hall estimator.
Definition: hall.h:279
HALL_Status_e
HALL_Status_e
Definition: hall.h:72
USER_Params
Defines a structure for the user parameters.
Definition: userParams.h:75
HALL_Obj::gpioHallU
uint32_t gpioHallU
Definition: hall.h:103
HALL_Obj::hallPrev
uint16_t hallPrev[7]
Definition: hall.h:111
HALL_setParams
void HALL_setParams(HALL_Handle handle, const USER_Params *pUserParams)
Sets the hall estimator parameters.
HALL_setSpeed_Hz
static void HALL_setSpeed_Hz(HALL_Handle handle, const float32_t speedHall_Hz)
Sets a value to the speed for the hall estimator.
Definition: hall.h:262
HALL_IDLE
@ HALL_IDLE
Definition: hall.h:73
HALL_init
HALL_Handle HALL_init(void *pMemory, const size_t numBytes)
Initializes the HALL controller.
HALL_getSpeed_Hz
static float32_t HALL_getSpeed_Hz(HALL_Handle handle)
Gets the feedback speed from hall estimator.
Definition: hall.h:248
HALL_run
static void HALL_run(HALL_Handle handle, float32_t speedRef)
Definition: hall.h:400
HALL_Obj::hallStatus
HALL_Status_e hallStatus
Definition: hall.h:113
HALL_ALIGNMENT
@ HALL_ALIGNMENT
Definition: hall.h:74
HALL_setForceAngleAndIndex
static void HALL_setForceAngleAndIndex(HALL_Handle handle, float32_t speedRef)
Sets the force angle and index for next step of the hall estimator.
Definition: hall.h:358
HALL_Obj::thetaHall_pu
float32_t thetaHall_pu
Definition: hall.h:102
HALL_resetParams
static void HALL_resetParams(HALL_Handle handle)
Resets the hall estimator parameters.
Definition: hall.h:167
HALL_RUN
@ HALL_RUN
Definition: hall.h:76
HALL_Obj::thetaBuff
float32_t thetaBuff[7]
Definition: hall.h:99
HALL_Obj::thetaDelta_rad
float32_t thetaDelta_rad
Definition: hall.h:101
userParams.h
Contains the public interface for the HAL and EST modules.
HALL_Obj::timeCount
uint32_t timeCount[2]
Definition: hall.h:96
HALL_Obj::timeCountMax
uint32_t timeCountMax
Definition: hall.h:97
HALL_Obj::hallIndex
uint16_t hallIndex
Definition: hall.h:109
HALL_Obj::speedHall_Hz
float32_t speedHall_Hz
Definition: hall.h:92
HALL_Obj::gpioHallUBase
uint32_t gpioHallUBase
Definition: hall.h:106
HALL_Obj::hallDirection
uint16_t hallDirection
Definition: hall.h:112
HALL_Obj::gpioHallV
uint32_t gpioHallV
Definition: hall.h:104
float32_t
float float32_t
Definition: dcl_common.h:58
HALL_Obj::thetaHall_rad
float32_t thetaHall_rad
Definition: hall.h:100
HALL_Obj::pwmScaler
float32_t pwmScaler
Definition: hall.h:89
HALL_setGPIOs
static void HALL_setGPIOs(HALL_Handle handle, const uint32_t gpioHallU, const uint32_t gpioHallV, const uint32_t gpioHallW, const uint32_t gpioHallUBase, const uint32_t gpioHallVBase, const uint32_t gpioHallWBase)
Sets GPIO for hall sensors input.
Definition: hall.h:193
HALL_setTimeStamp
static void HALL_setTimeStamp(HALL_Handle handle, const uint32_t timeStamp)
Sets a value to the time stamp for the hall estimator.
Definition: hall.h:295
HALL_Obj::timeStampCAP
uint32_t timeStampCAP
Definition: hall.h:94