-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDRV2605.c
417 lines (302 loc) · 13.8 KB
/
DRV2605.c
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* DRV2605.c
*
* Created on: May 11, 2018
* Author: Kevin Kuwata
*
*/
#include "DRV2605.h"
#include "msp.h"
/* Global */
volatile uint8_t bytesExpectedToReceive = 0;
volatile uint8_t indexRX = 0;
volatile uint8_t I2CReceived[10]; //just picked 10 but it should be as small as possible
void initDriver(void){
UCB3CTLW0 = UCSWRST; //unlock
// Master, i2c, smclk (3mhz), WR
UCB3CTLW0 |= UCMST | UCMODE_3 | UCSSEL__SMCLK | UCTR | UCSYNC; //uscyn always 1 bc spi or i2c only no uart.
//the system clock is not running at 3mhz! or if it is the slave is clock stretching
//remember i2c is 400khz max and 100khz standard
//need to divide 3Mhz down to 100 khz...
UCB3BR0 |= 30;
//EN pin and Trigger Pin both GPIO
P7SEL0 &= ~(BIT5 | BIT6);
P7SEL1 &= ~(BIT5 | BIT6);
//secondary!! mode for i2c table 6-77 msp datasheet
P6SEL0 &= ~(BIT6 | BIT7);
P6SEL1 |= BIT6 | BIT7;
P7DIR |= BIT5 | BIT6; //outputs
P7OUT |= BIT5; // enable! NOTE: remember to do this
P7OUT &= ~BIT6; // connect to ground for INT
UCB3CTL0 &= ~UCSWRST; //lock
//enable RX TX interrupts to process receieved bytes. ///change the transmit/reciever bits... for read/write correct?
UCB3IFG = 0; //clear existing interrupts
// UCB3IE |= UCNACKIE | UCRXIE0 | UCTXIE; //INTERRUPTS FOR TIMOUT, NACK, RX (BYTE RECIEVED), BYTE COMPLETE TRANSMIT
/* byte received collect the response, clear flag and collect next response in a global array
* byte transmitted, depending on mode write single or write multiple continue to write the next thing?
* or change modes, change to RX mode?
* */
NVIC_EnableIRQ(EUSCIB3_IRQn);
}
void autoCalibrationLRA(void){
/* put into auto calibration mode: register 0x01 -> 0x07 */
writeRegister(DRV_DEFAULT_ADDRESS, MODE_R, AUTO_CALIBRATION_MODE);
/* Enter standby mode */
writeRegister(DRV_DEFAULT_ADDRESS, MODE_R, STANDBY);
/* SET RATED voltage 3v for motor refer to the setup guide. */
writeRegister(DRV_DEFAULT_ADDRESS, RATED_VOLTAGE_R, 0x60);
/* Set Over drive voltage */
writeRegister(DRV_DEFAULT_ADDRESS, OVERDRIVE_CLAMP_V_R, 0x96);
/* Fill in auto calibration registers */
writeRegister(DRV_DEFAULT_ADDRESS, FB_CTRL_R, 0xB6);
writeRegister(DRV_DEFAULT_ADDRESS, CTRL_1_R, 0x13);
writeRegister(DRV_DEFAULT_ADDRESS, CTRL_2_R, 0xF5);
writeRegister(DRV_DEFAULT_ADDRESS, CTRL_3_R, 0x80);
/* library selection for ROM effects, library 6 for LRA */
writeRegister(DRV_DEFAULT_ADDRESS, LIBRARY_R, 0x06);
/* come out of standby mode */
writeRegister(DRV_DEFAULT_ADDRESS, MODE_R, 0x00);
/* send go bit */
writeRegister(DRV_DEFAULT_ADDRESS, GO_R, 1);
writeRegister(DRV_DEFAULT_ADDRESS, MODE_R, STANDBY);
}
void autoCalibrationERM(void){
/* put into auto calibration mode: register 0x01 pre config then auto calibrate */
writeRegister(DRV_DEFAULT_ADDRESS, MODE_R, 0x00);
/* SET RATED voltage 3v for motor refer to the setup guide. */
writeRegister(DRV_DEFAULT_ADDRESS, RATED_VOLTAGE_R, 0x90);
/* Set Over drive voltage */
writeRegister(DRV_DEFAULT_ADDRESS, OVERDRIVE_CLAMP_V_R, 0x96);
/* Fill in auto calibration registers */
writeRegister(DRV_DEFAULT_ADDRESS, FB_CTRL_R, 0x36);
writeRegister(DRV_DEFAULT_ADDRESS, CTRL_1_R, 0x93);
writeRegister(DRV_DEFAULT_ADDRESS, CTRL_2_R, 0xF5);
writeRegister(DRV_DEFAULT_ADDRESS, CTRL_3_R, 0x80);
/* library selection for ROM effects, library 6 for LRA */
writeRegister(DRV_DEFAULT_ADDRESS, LIBRARY_R, 0x01);
/* come out of standby mode */
writeRegister(DRV_DEFAULT_ADDRESS, MODE_R, 0x07);
/* Fill in auto calibration registers */
writeRegister(DRV_DEFAULT_ADDRESS, FB_CTRL_R, 0x36);
writeRegister(DRV_DEFAULT_ADDRESS, CTRL_1_R, 0x13);
writeRegister(DRV_DEFAULT_ADDRESS, CTRL_2_R, 0xF5);
writeRegister(DRV_DEFAULT_ADDRESS, CTRL_3_R, 0xA0);
writeRegister(DRV_DEFAULT_ADDRESS, CTRL_4_R, 0x20);
/* send go bit */
writeRegister(DRV_DEFAULT_ADDRESS, GO_R, 1);
writeRegister(DRV_DEFAULT_ADDRESS, MODE_R, STANDBY);
}
void preAutoCalibrationLRA(void){
/* set up the feedback control register (3a) - (3c) */
writeRegister(DRV_DEFAULT_ADDRESS, MODE_R, 0x07);
uint8_t valueToWrite = 0xB0 | 0x06; // equal to 0b1 011 01 01
writeRegister(DRV_DEFAULT_ADDRESS, FB_CTRL_R , valueToWrite);
/* set up rated voltage control register NOTE: CLOSED_LOOP Only (3d) */
valueToWrite = 0x3E; // default value, but what does this even correspond to in volts?
/* I don't think it actually is a voltage, but more of a scaling factor used in Equation 5.
* I don't know the frequency of the LRA and I have not set the sample time. how do I find
* the resonance frequency?
* TODO: f_LRA needs to be determined, recalculate this equation 5*/
writeRegister(DRV_DEFAULT_ADDRESS, RATED_VOLTAGE_R , valueToWrite);
/* set up OVER DRIVE CLAMP control register (3e) */
valueToWrite = 0x89; // TODO: use the default value until I find what f_LRA is
writeRegister(DRV_DEFAULT_ADDRESS, OVERDRIVE_CLAMP_V_R , valueToWrite); /* Responsible for max voltage in open loop, but allows for overshoot in closed loop */
/* default values for voltage and OD, with 200 hz resonant is 2.97 v max so it should be fine. unless resonant goes down. */
/* set up auto calibration time (500ms) , ZC_detect Time (100uS) Control Register 4 (3f) & (3k) */
valueToWrite = 0x20; // default values, 00 10 0000
writeRegister(DRV_DEFAULT_ADDRESS, CTRL_4_R , valueToWrite);
/* set up Drive Time Control Register 1 (3g) */
valueToWrite = 0x93; //default values TODO: find F_LRA then get period then recalculate this.
writeRegister(DRV_DEFAULT_ADDRESS, CTRL_1_R, valueToWrite);
/* set up Sample_Time (200uS), Blanking Time (50uS), IDISS_time (50uS), Control Register 2 (3h) - (3j) */
valueToWrite = 0xDA; //0b11 01 10 10 default values;
writeRegister(DRV_DEFAULT_ADDRESS, CTRL_2_R , valueToWrite);
/* Set the Go bit in 0x0C and then check the 4 registers:
* - bemf_gain (register 0x1A) bits 1 and 0
* - A_cal_comp (register 0x18)
* - a_cal_bemf (0x19)
* - diag_result == sucessful calibration? */
writeRegister(DRV_DEFAULT_ADDRESS, GO_R, 1);
}
void setGoBit(void){
writeRegister(DRV_DEFAULT_ADDRESS, GO_R, 1);
while(UCB3STATW & UCBBUSY);
}
void analogMode(void){
writeRegister(DRV_DEFAULT_ADDRESS, MODE_R, MODE_ANALOG_INPUT);
writeRegister(DRV_DEFAULT_ADDRESS, CTRL_3_R, 0x81);
}
void setAndPlay(uint8_t waveformID){
/* come out of standby */
writeRegister(DRV_DEFAULT_ADDRESS, MODE_R, 0x00); //internal trigger
writeRegister(DRV_DEFAULT_ADDRESS, 0x04, waveformID);
writeRegister(DRV_DEFAULT_ADDRESS, GO_R, 1);
}
uint8_t DRVSingleWrite(uint8_t registerToWrite, uint8_t valueToWrite){
beginTransmission(DRV_DEFAULT_ADDRESS);
UCB3CTL0 |= UCTR;
UCB3TXBUF = valueToWrite;
stopTransmission();
//interrupt will trigger and then we will do a stop condition.
return 1;
}
void beginTransmission(uint8_t address){
UCB3I2CSA = address;
UCB3CTL0 |= UCTXSTT;
}
void stopTransmission(void){
EUSCI_B3->CTLW0 |= UCTXSTP; //send stop command;
}
/* need to do a read modify write. */
void writeRegister(uint8_t address, uint8_t reg, uint8_t value){
//while(UCB3STATW & UCBBUSY); // wait while busy
while(UCB3CTLW0 & UCTXSTT); //soon as this is no longer true send the next thing
UCB3I2CSA = address;
UCB3CTL0 |= UCTR | UCTXSTT;
while(UCB3CTLW0 & UCTXSTT); //soon as this is no longer true send the next thing
UCB3TXBUF = reg;
while(!(UCB3IFG & UCTXIFG0)); //when empty send next thing
UCB3TXBUF = value;
while(!(UCB3IFG & UCTXIFG0)); //when empty send next thing
UCB3CTL0 |= UCTXSTP;
}
uint8_t readRegister(uint8_t address, uint8_t reg){
while(UCB3STAT & UCBBUSY); //wait if busy
UCB3I2CSA = address;
UCB3CTL0 |= UCTR | UCTXSTT;
while(UCB3CTLW0 & UCTXSTT);
UCB3TXBUF = reg;
while(!(UCB3IFG & UCTXIFG0)); //wait until buf is transmitted
/* read and repeat start */
UCB3CTL0 &= ~UCTR;
UCB3CTL0 |= UCTXSTT;
while(UCB3CTLW0 & UCTXSTT);
// while(!(UCB3IFG & UCRXIFG0));
UCB3CTL0 |= UCTXSTP;
/* collect the bytes requested, stuff into array, then send stop*/
uint8_t rxValue = UCB3RXBUF;
while(UCB3CTLW0 & UCTXSTP); //clear after stop been set and come out.
return rxValue;
}
/* Auto Calibration for LRA page 26 */
/* 1) set to calibration mode : write to register 0x01 with 0x07
* 2) set the following registers:
* 0x1A:
* bit7 = n_LRA
* bit6 - bit4 = brake factor
* bit3 - bit2 = loop gain
* bit1 - bit0 = back emf gain
* 0x1B :
* rated voltage
* od_clamp
* auto_calibration_time
* drive time
* sample time
* blanking time
* idiss_time
* zc_Detect_time
*
* then set go bit in 0x0c
*
* check status register to see if any faults??! what short?
*
*
*
*
*
* */
/* =====================================================================================================================================================*/
/* ======================================================================================================================================================*/
/* ======================================================================================================================================================*/
/* ISR */
/* ======================================================================================================================================================*/
/* ======================================================================================================================================================*/
/* ======================================================================================================================================================*/
void EUSCIB3_IRQHandler(void)
{
if (UCB3IFG & UCRXIFG0)
{
//clear flag
UCB3IFG &= ~UCRXIFG0;
//complete byte received, collect into global
if(indexRX < bytesExpectedToReceive){
I2CReceived[indexRX] = UCB3RXBUF; /* Global array to collect i2c data that the slave gave back. */
indexRX++; /* global and will be set to 0 when we set bytesExpectedToRecieve */
}
stopTransmission();
//slave may send more bytes than expected, let them fall off.
}
else if(UCB3IFG & UCTXIFG0){
UCB3IFG &= ~UCTXIFG0;
// UCB3CTL0 |= UCTXSTP;
//TX buffer empty
}
else if(UCB3IFG & UCNACKIFG){
UCB3IFG &= ~UCNACKIFG;
// UCB3CTL0 |= UCTXSTP;
// nack received
}
}
/*
*
* https://e2e.ti.com/support/microcontrollers/msp430/f/166/t/589712?MSP430FR5969-Read-multiple-bytes-of-data-i2c-with-repeated-start-and-without-interrupts
* oid i2c_init(){
P1SEL1 |= BIT6 | BIT7; // configure I2C pins
P1SEL0 &= ~(BIT6 | BIT6); // configure I2C pins
// I2C default uses SMCLK
UCB0CTL1 |= UCSWRST; // put eUSCI_B in reset state
UCB0CTLW0 |= UCMODE_3 | UCMST | UCSYNC; // I2C, master, sync
UCB0BRW = 0x000A; // baud rate = SMCLK / 10 = 100khz
UCB0CTL1 &= ~UCSWRST; // eUSCI_B in operational state
}
void i2c_write(uint8_t slv_addr, uint8_t reg_addr, uint8_t data){
while(UCB0STAT & UCBBUSY);
UCB0I2CSA = slv_addr; // set slave address
UCB0CTLW0 |= UCTR | UCTXSTT; // transmitter mode and START condition.
while (UCB0CTLW0 & UCTXSTT);
UCB0TXBUF = reg_addr;
while(!(UCB0IFG & UCTXIFG0));
UCB0TXBUF = data;
while(!(UCB0IFG & UCTXIFG0));
UCB0CTLW0 |= UCTXSTP;
while(UCB0CTLW0 & UCTXSTP); // wait for stop
}
uint8_t i2c_read(uint8_t slv_addr, uint8_t reg_addr){
uint8_t data = 0;
while(UCB0STAT & UCBBUSY);
UCB0I2CSA = slv_addr; // set slave address
UCB0CTLW0 |= UCTR | UCTXSTT; // transmitter mode and START condition.
while(UCB0CTLW0 & UCTXSTT);
UCB0TXBUF = reg_addr;
while(!(UCB0IFG & UCTXIFG0));
UCB0CTLW0 &= ~UCTR; // receiver mode
UCB0CTLW0 |= UCTXSTT; // START condition
while(UCB0CTLW0 & UCTXSTT); // make sure start has been cleared
UCB0CTLW0 |= UCTXSTP; // STOP condition
while(!(UCB0IFG & UCRXIFG0));
data = UCB0RXBUF;
while(UCB0CTLW0 & UCTXSTP);
return data;
}
multiple
void i2c_read_multi(uint8_t slv_addr, uint8_t reg_addr, uint8_t l, uint8_t *arr){
uint8_t i;
while(UCB0STAT & UCBBUSY);
UCB0I2CSA = slv_addr; // set slave address
UCB0CTLW0 |= UCTR | UCTXSTT; // transmitter mode and START condition.
while(UCB0CTLW0 & UCTXSTT);
UCB0TXBUF = reg_addr;
while(!(UCB0IFG & UCTXIFG0));
UCB0CTLW0 &= ~UCTR; // receiver mode
UCB0CTLW0 |= UCTXSTT; // START condition
while(UCB0CTLW0 & UCTXSTT); // make sure start has been cleared
for (i = 0; i < l; i++) {
while(!(UCB0IFG & UCRXIFG0));
if(i == l - 1){
UCB0CTLW0 |= UCTXSTP; // STOP condition
}
arr[i] = UCB0RXBUF;
}
while(UCB0CTLW0 & UCTXSTP);
}
* */