aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Drivers/FPGA/Motors_Driver/L6470.h
blob: 745a7f75395189a35131a25e0d6a00fec6f4df50 (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* cL6470.h
*
* Created: 6/22/2016 8:40:34 PM
*  Author: Atif
*/


#ifndef CL6470_H_
#define CL6470_H_

//#include <Arduino.h>
//#include <SPI.h>

//

//

// constant definitions for overcurrent thresholds. Write these values to
//  register x_OCD_TH to set the level at which an overcurrent even occurs.
#define x_OCD_TH_375mA  0x00
#define x_OCD_TH_750mA  0x01
#define x_OCD_TH_1125mA 0x02
#define x_OCD_TH_1500mA 0x03
#define x_OCD_TH_1875mA 0x04
#define x_OCD_TH_2250mA 0x05
#define x_OCD_TH_2625mA 0x06
#define x_OCD_TH_3000mA 0x07
#define x_OCD_TH_3375mA 0x08
#define x_OCD_TH_3750mA 0x09
#define x_OCD_TH_4125mA 0x0A
#define x_OCD_TH_4500mA 0x0B
#define x_OCD_TH_4875mA 0x0C
#define x_OCD_TH_5250mA 0x0D
#define x_OCD_TH_5625mA 0x0E
#define x_OCD_TH_6000mA 0x0F

// STEP_MODE option values.
// First comes the "microsteps per step" options...
#define x_STEP_MODE_STEP_SEL 0x07  // Mask for these bits only.
#define x_STEP_SEL_1     0x00
#define x_STEP_SEL_1_2   0x01
#define x_STEP_SEL_1_4   0x02
#define x_STEP_SEL_1_8   0x03
#define x_STEP_SEL_1_16  0x04
#define x_STEP_SEL_1_32  0x05
#define x_STEP_SEL_1_64  0x06
#define x_STEP_SEL_1_128 0x07

// ...next, define the SYNC_EN bit. When set, the BUSYN pin will instead
//  output a clock related to the full-step frequency as defined by the
//  SYNC_SEL bits below.
#define x_STEP_MODE_SYNC_EN  0x80  // Mask for this bit
#define x_SYNC_EN 0x80

// ...last, define the SYNC_SEL modes. The clock output is defined by
//  the full-step frequency and the value in these bits- see the datasheet
//  for a matrix describing that relationship (page 46).
#define x_STEP_MODE_SYNC_SEL 0x70
#define x_SYNC_SEL_1_2 0x00
#define x_SYNC_SEL_1   0x10
#define x_SYNC_SEL_2   0x20
#define x_SYNC_SEL_4   0x30
#define x_SYNC_SEL_8   0x40
#define x_SYNC_SEL_16  0x50
#define x_SYNC_SEL_32  0x60
#define x_SYNC_SEL_64  0x70

// Bit names for the ALARM_EN register.
//  Each of these bits defines one potential alarm condition.
//  When one of these conditions occurs and the respective bit in ALARM_EN is set,
//  the FLAG pin will go low. The register must be queried to determine which event
//  caused the alarm.
#define x_ALARM_EN_OVERCURRENT       0x01
#define x_ALARM_EN_THERMAL_SHUTDOWN  0x02
#define x_ALARM_EN_THERMAL_WARNING   0x04
#define x_ALARM_EN_UNDER_VOLTAGE     0x08
#define x_ALARM_EN_STALL_DET_A       0x10
#define x_ALARM_EN_STALL_DET_B       0x20
#define x_ALARM_EN_SW_TURN_ON        0x40
#define x_ALARM_EN_WRONG_NPERF_CMD   0x80

// CONFIG register renames.

// Oscillator options.
// The x needs to know what the clock frequency is because it uses that for some
//  calculations during operation.
#define x_CONFIG_OSC_SEL                 0x000F // Mask for this bit field.
#define x_CONFIG_INT_16MHZ               0x0000 // Internal 16MHz, no output
#define x_CONFIG_INT_16MHZ_OSCOUT_2MHZ   0x0008 // Default; internal 16MHz, 2MHz output
#define x_CONFIG_INT_16MHZ_OSCOUT_4MHZ   0x0009 // Internal 16MHz, 4MHz output
#define x_CONFIG_INT_16MHZ_OSCOUT_8MHZ   0x000A // Internal 16MHz, 8MHz output
#define x_CONFIG_INT_16MHZ_OSCOUT_16MHZ  0x000B // Internal 16MHz, 16MHz output
#define x_CONFIG_EXT_8MHZ_XTAL_DRIVE     0x0004 // External 8MHz crystal
#define x_CONFIG_EXT_16MHZ_XTAL_DRIVE    0x0005 // External 16MHz crystal
#define x_CONFIG_EXT_24MHZ_XTAL_DRIVE    0x0006 // External 24MHz crystal
#define x_CONFIG_EXT_32MHZ_XTAL_DRIVE    0x0007 // External 32MHz crystal
#define x_CONFIG_EXT_8MHZ_OSCOUT_INVERT  0x000C // External 8MHz crystal, output inverted
#define x_CONFIG_EXT_16MHZ_OSCOUT_INVERT 0x000D // External 16MHz crystal, output inverted
#define x_CONFIG_EXT_24MHZ_OSCOUT_INVERT 0x000E // External 24MHz crystal, output inverted
#define x_CONFIG_EXT_32MHZ_OSCOUT_INVERT 0x000F // External 32MHz crystal, output inverted

// Configure the functionality of the external switch input
#define x_CONFIG_SW_MODE                 0x0010 // Mask for this bit.
#define x_CONFIG_SW_HARD_STOP            0x0000 // Default; hard stop motor on switch.
#define x_CONFIG_SW_USER                 0x0010 // Tie to the GoUntil and ReleaseSW
//  commands to provide jog function.
//  See page 25 of datasheet.

// Configure the motor voltage compensation mode (see page 34 of datasheet)
#define x_CONFIG_EN_VSCOMP               0x0020  // Mask for this bit.
#define x_CONFIG_VS_COMP_DISABLE         0x0000  // Disable motor voltage compensation.
#define x_CONFIG_VS_COMP_ENABLE          0x0020  // Enable motor voltage compensation.

// Configure overcurrent detection event handling
#define x_CONFIG_OC_SD                   0x0080  // Mask for this bit.
#define x_CONFIG_OC_SD_DISABLE           0x0000  // Bridges do NOT shutdown on OC detect
#define x_CONFIG_OC_SD_ENABLE            0x0080  // Bridges shutdown on OC detect

// Configure the slew rate of the power bridge output
#define x_CONFIG_POW_SR                  0x0300  // Mask for this bit field.
#define x_CONFIG_SR_180V_us              0x0000  // 180V/us
#define x_CONFIG_SR_290V_us              0x0200  // 290V/us
#define x_CONFIG_SR_530V_us              0x0300  // 530V/us

// Integer divisors for PWM sinewave generation
//  See page 32 of the datasheet for more information on this.
#define x_CONFIG_F_PWM_DEC               0x1C00      // mask for this bit field
#define x_CONFIG_PWM_MUL_0_625           (0x00)<<10
#define x_CONFIG_PWM_MUL_0_75            (0x01)<<10
#define x_CONFIG_PWM_MUL_0_875           (0x02)<<10
#define x_CONFIG_PWM_MUL_1               (0x03)<<10
#define x_CONFIG_PWM_MUL_1_25            (0x04)<<10
#define x_CONFIG_PWM_MUL_1_5             (0x05)<<10
#define x_CONFIG_PWM_MUL_1_75            (0x06)<<10
#define x_CONFIG_PWM_MUL_2               (0x07)<<10

// Multiplier for the PWM sinewave frequency
#define x_CONFIG_F_PWM_INT               0xE000     // mask for this bit field.
#define x_CONFIG_PWM_DIV_1               (0x00)<<13
#define x_CONFIG_PWM_DIV_2               (0x01)<<13
#define x_CONFIG_PWM_DIV_3               (0x02)<<13
#define x_CONFIG_PWM_DIV_4               (0x03)<<13
#define x_CONFIG_PWM_DIV_5               (0x04)<<13
#define x_CONFIG_PWM_DIV_6               (0x05)<<13
#define x_CONFIG_PWM_DIV_7               (0x06)<<13

// Status register bit renames- read-only bits conferring information about the
//  device to the user.
#define x_STATUS_HIZ                     0x0001 // high when bridges are in HiZ mode
#define x_STATUS_BUSY                    0x0002 // mirrors BUSY pin
#define x_STATUS_SW_F                    0x0004 // low when switch open, high when closed
#define x_STATUS_SW_EVN                  0x0008 // active high, set on switch falling edge,
//  cleared by reading STATUS
#define x_STATUS_DIR                     0x0010 // Indicates current motor direction.
//  High is FWD, Low is REV.
#define x_STATUS_NOTPERF_CMD             0x0080 // Last command not performed.
#define x_STATUS_WRONG_CMD               0x0100 // Last command not valid.
#define x_STATUS_UVLO                    0x0200 // Undervoltage lockout is active
#define x_STATUS_TH_WRN                  0x0400 // Thermal warning
#define x_STATUS_TH_SD                   0x0800 // Thermal shutdown
#define x_STATUS_OCD                     0x1000 // Overcurrent detected
#define x_STATUS_STEP_LOSS_A             0x2000 // Stall detected on A bridge
#define x_STATUS_STEP_LOSS_B             0x4000 // Stall detected on B bridge
#define x_STATUS_SCK_MOD                 0x8000 // Step clock mode is active

// Status register motor status field
#define x_STATUS_MOT_STATUS                0x0060      // field mask
#define x_STATUS_MOT_STATUS_STOPPED       (0x0000)<<13 // Motor stopped
#define x_STATUS_MOT_STATUS_ACCELERATION  (0x0001)<<13 // Motor accelerating
#define x_STATUS_MOT_STATUS_DECELERATION  (0x0002)<<13 // Motor decelerating
#define x_STATUS_MOT_STATUS_CONST_SPD     (0x0003)<<13 // Motor at constant speed

// Register address redefines.
//  See the x_Param_Handler() function for more info about these.
#define x_ABS_POS              0x01
#define x_EL_POS               0x02
#define x_MARK                 0x03
#define x_SPEED                0x04
#define x_ACC                  0x05
#define x_DEC                  0x06
#define x_MAX_SPEED            0x07
#define x_MIN_SPEED            0x08
#define x_FS_SPD               0x15
#define x_KVAL_HOLD            0x09
#define x_KVAL_RUN             0x0A
#define x_KVAL_ACC             0x0B
#define x_KVAL_DEC             0x0C
#define x_INT_SPD              0x0D
#define x_ST_SLP               0x0E
#define x_FN_SLP_ACC           0x0F
#define x_FN_SLP_DEC           0x10
#define x_K_THERM              0x11
#define x_ADC_OUT              0x12
#define x_OCD_TH               0x13
#define x_STALL_TH             0x14
#define x_STEP_MODE            0x16
#define x_ALARM_EN             0x17
#define x_CONFIG               0x18
#define x_STATUS               0x19

//x commands
#define x_NOP                  0x00
#define x_SET_PARAM            0x00
#define x_GET_PARAM            0x20
#define x_RUN                  0x50
#define x_STEP_CLOCK           0x58
#define x_MOVE                 0x40
#define x_GOTO                 0x60
#define x_GOTO_DIR             0x68
#define x_GO_UNTIL             0x82
#define x_RELEASE_SW           0x92
#define x_GO_HOME              0x70
#define x_GO_MARK              0x78
#define x_RESET_POS            0xD8
#define x_RESET_DEVICE         0xC0
#define x_SOFT_STOP            0xB0
#define x_HARD_STOP            0xB8
#define x_SOFT_HIZ             0xA0
#define x_HARD_HIZ             0xA8
#define x_GET_STATUS           0xD0 // use Get_and_Clear_Status

/* x direction options */
#define FWD  0x01
#define REV  0x00

/* x action options */
#define ACTION_RESET  0x00
#define ACTION_COPY   0x01



//class cL6470 {
//public:

//Default constuctot
//cL6470();

//Overload Constructor
//@param int MOSI, int MISO, int CS, int CLK, int BUSY, int RESET
//cL6470(int, int, int, int, int, int);

#include "../../../DataDef.h"

void init();
unsigned long GetParam(byte);
byte Data_To_Transfer(byte);
unsigned long ParamHandler(byte, unsigned long);
void  SetParam(byte, unsigned long);
unsigned long Param(unsigned long, byte);
int GetStatus();
uint32_t SpdCalc(float);
unsigned long IntSpdCalc(float);
unsigned long FSCalc(float);
unsigned long MinSpdCalc(float);
unsigned long MaxSpdCalc(float);
unsigned long DecCalc(float);
unsigned long AccCalc(float);
void SetLSPDOpt(boolean);
void Run(byte, unsigned long);
void Step_Clock(byte);
void Move(byte, unsigned long);
void GoTo(unsigned long);
void GoTo_DIR(byte, unsigned long);
void GoUntil(byte, byte, unsigned long);
void ReleaseSW(byte, byte);
void GoHome();
void GoMark();
void ResetPos();
void ResetDev();
void SoftStop();
void HardStop();
void SoftHiZ();
void HardHiZ();
float CurrentSpdCalc(uint32_t stepspertick);// for Speed response
void delay(int number_of_seconds);
void Run_tx_test(byte dir, unsigned long spd);
////TODO update the addresses
//#define MOSI_BASE            0x00000001  //
//#define MISO_BASE              0x00000002  //
//#define CS_BASE                0x00000003  //
//#define CLK_BASE               0x00000004  //
//#define BUSY_BASE              0x00000005  //
//#define RESET_BASE             0x00000006  //
//#define TX_BASE                0x00000006  //
//#define RX_BASE                0x00000007  //


//extern int volatile * const MOSI_Reg;
//extern int volatile * const MISO_Reg;
//extern int volatile * const CS_Reg;
//extern int volatile * const CLK_Reg;
//extern int volatile * const BUSY_Reg;
//extern int volatile * const RESET_Reg;
//extern char volatile * const TX_Reg;
//extern char volatile * const RX_Reg;

//*MOSI = value; /* write to port */
//value = *MOSI; /* read from port */



///////////////


////@param int MOSI
//void Set_MOSI(int);
////@param int MISO
//void Set_MISO(int);
////@param int CS
//void Set_CS(int);
////@param int CLK
//void Set_CLK(int);
////@param int BUSY
//void Set_BUSY(int);
////@param int RESET
//void Set_RESET(int);



//int Get_MOSI();
//int Get_MISO();
//int Get_CS();
//int Get_CLK();
//int Get_BUSY();
//int Get_RESET();


//private:
// Member Variable

//int MOSI = 0;
//int MISO = 0;
//int CS = 0;
//int CLK = 0;
//int BUSY = 0;
//int RESET = 0;
//};


#endif /* CL6470_H_ */