-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTLE75008.c
383 lines (328 loc) · 9.17 KB
/
TLE75008.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
/***********************************************************************************************************************
* File-Name: TLE75008.c
* AUTHOR: Matthias Ernst
* Infineon Technologies AG
* Date: 08.10.2020
*
* This file is for Control of TLE75008 Power Driver
*
*****************************************************************************
* Copyright (C) 2024 Infineon Technologies AG. All rights reserved.
*
* Infineon Technologies AG (INFINEON) is supplying this file for use
* exclusively with Infineon's products. This file can be freely
* distributed within development tools and software supporting such microcontroller
* products.
*
* THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
* INFINEON SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR DIRECT, INDIRECT, INCIDENTAL,
* ASPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
*
*/
/*******************************************************************************
** Includes **
*******************************************************************************/
#include "TLE75008.h"
#include "stdlib.h"
#include <FreeRTOS.h>
#include <task.h>
#include <queue.h>
#include "timers.h"
#include "gameController_boardControl.h"
/*******************************************************************************
** Private Variables **
*******************************************************************************/
/*******************************************************************************
** Global Variables **
*******************************************************************************/
/* PWM object */
cyhal_pwm_t pwm_rumble;
cyhal_spi_t mSPI;
uint16_t SPI_sendData;
uint32_t SPI_readData;
uint8_t TLE75008_mapin0_reg;
uint8_t TLE75008_out_reg;
TimerHandle_t LEDtimers[10];
uint8_t ledTimerID = 0;
uint8_t rumble_active = 1;
uint8_t led_toggle_flag = 0;
uint8_t led_activate_flag = 0;
/*******************************************************************************
** Global Function Definitions **
*******************************************************************************/
void spi_init(void)
{
printf("SPI master initialization: ");
cy_rslt_t result=CY_RSLT_SUCCESS;
uint32_t spi_master_frequency = 1000000;
result = cyhal_spi_init(&mSPI, CYBSP_SPI_MOSI, CYBSP_SPI_MISO, CYBSP_SPI_CLK, CYBSP_SPI_CS, NULL, 16, CYHAL_SPI_MODE_01_MSB, false);
cyhal_spi_slave_select_config(&mSPI, CYBSP_SPI_CS, CYHAL_SPI_SSEL_ACTIVE_LOW);
if (result != CY_RSLT_SUCCESS)
{
printf("init failed!\r\n");
CY_ASSERT(0);
}else{
printf("Success! \r\n");
}
result = cyhal_spi_set_frequency(&mSPI, spi_master_frequency);
}
void TLE75008_init()
{
cy_rslt_t result;
printf("SPIDER+ initialization: ");
/*Set IDLE pin to high */
cyhal_gpio_init(PIN_SPIDER_IDLE, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, 0);
cyhal_gpio_write(PIN_SPIDER_IDLE, true);
// cyhal_gpio_init(CYBSP_SPI_CS, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, 0);
// cyhal_gpio_write(CYBSP_SPI_CS, false);
SPI_sendData = 0x8C80; //activate active mode HWCR.act 0x0C:(7)
result = cyhal_spi_send(&mSPI, SPI_sendData);
if (result != CY_RSLT_SUCCESS)
{
printf("SPI com failed!");
CY_ASSERT(0);
}
SPI_sendData = 0x8400; //MAPIN0 all channels off
result = cyhal_spi_send(&mSPI, SPI_sendData);
SPI_sendData = 0x8500; //MAPIN1 all channels off
cyhal_spi_send(&mSPI, SPI_sendData);
SPI_sendData = 0x0001; //standard diagnosis
result = cyhal_spi_send(&mSPI, SPI_sendData);
if (result == CY_RSLT_SUCCESS)
{
printf("Success!\r\n");
}
//TLE75008_writeRegister(TLE75008_MAPIN0_ADDR, RUMBLE_LEFT|RUMBLE_RIGHT2);
TLE75008_initPWM();
TLE75008_startPWM(PWM_FREQUENCY, 25);
TLE75008_mapin0_reg = 0;
TLE75008_out_reg = 0;
}
void TLE75008_writeRegister(uint8 reg, uint8 data)
{
uint16_t transfer = (uint16)((reg|(uint16)0x80)<<8);
transfer = transfer|(uint16)(data);
cyhal_spi_send(&mSPI, transfer);
}
void TLE75008_initPWM()
{
cy_rslt_t result;
result = cyhal_pwm_init(&pwm_rumble, PIN_RUMBLE_PWM, NULL);
result = cyhal_pwm_set_duty_cycle(&pwm_rumble, 0, PWM_FREQUENCY);
}
void TLE75008_startPWM(uint32_t frequency, float dutyCycle)
{
cyhal_pwm_set_duty_cycle(&pwm_rumble, dutyCycle, frequency);
cyhal_pwm_start(&pwm_rumble);
}
void TLE75008_deactivatePWM()
{
cyhal_pwm_stop(&pwm_rumble);
}
void rumble_activate(uint8_t rumble, float dutyCycle)
{
if(rumble_active)
{
if(rumble==1||rumble==2||rumble==3){
TLE75008_writeRegister(TLE75008_OUT_ADDR, rumble);
}else{
printf("Unknown Rumble number. \r\n");
return;
}
}
}
void rumble_deactivate()
{
//cyhal_pwm_stop(&pwm_rumble);
TLE75008_writeRegister(TLE75008_OUT_ADDR, TLE75008_OUT_OUTn_OFF);
//TLE75008_deactivatePWM();
}
void buttonPress_haptic_feedback()
{
rumble_activate(RUMBLE_RIGHT2|RUMBLE_LEFT, 70);
cyhal_system_delay_ms(25);
rumble_deactivate();
}
void TLE75008_LED_activate(uint8_t LED)
{
uint8_t led_activate = LED;
led_activate&=LED_OUT_MASK; //check that only leds (OUT2 - OUT6) are in argument
TLE75008_mapin0_reg |= led_activate; //write OUT register
cyhal_pwm_start(&pwm_rumble);
TLE75008_writeRegister(TLE75008_MAPIN0_ADDR, TLE75008_mapin0_reg);
}
void TLE75008_LED_deactivate(uint8_t LED)
{
TLE75008_LED_toggle_ms_stop(LED);
int8_t led_activate = LED;
led_activate ^= 0xFF;
led_activate &= LED_OUT_MASK; //check that only leds (OUT2 - OUT6) are in argument
TLE75008_mapin0_reg &= led_activate; //write OUT register
TLE75008_writeRegister(TLE75008_MAPIN0_ADDR, TLE75008_mapin0_reg);
}
void TLE75008_LED_toggle(uint8_t LED)
{
int8_t led_toggle = LED;
led_toggle &= LED_OUT_MASK; //check that only leds (OUT2 - OUT6) are in argument
TLE75008_mapin0_reg ^= led_toggle;
TLE75008_writeRegister(TLE75008_MAPIN0_ADDR, TLE75008_mapin0_reg);
}
void ledToggleTimerCallback(TimerHandle_t timerHandle)
{
uint8_t led = 0;
ledTimerID = pvTimerGetTimerID(timerHandle);
switch(ledTimerID)
{
case 0:
led =LED_BUTTONS_RIGHT;
break;
case 1:
led = LED_BUTTONS_LEFT;
break;
case 2:
led = LED_HOME_BLUE;
break;
case 3:
led = LED_HOME_GREEN;
break;
case 4:
led = LED_HOME_RED;
break;
default:
break;
}
led_toggle_flag = led;
//TLE75008_LED_toggle(led);
}
void TLE75008_LED_toggle_ms(uint8_t LED, uint32_t ms)
{
int8_t led_toggle = LED;
led_toggle &= LED_OUT_MASK;
switch(led_toggle)
{
case LED_BUTTONS_RIGHT:
ledTimerID = 0;
break;
case LED_BUTTONS_LEFT:
ledTimerID = 1;
break;
case LED_HOME_BLUE:
ledTimerID = 2;
break;
case LED_HOME_GREEN:
ledTimerID = 3;
break;
case LED_HOME_RED:
ledTimerID = 4;
break;
default:
printf("LED toggle timer error: only choose one led at a time!\r\n");
break;
}
LEDtimers[ledTimerID] = xTimerCreate("LED toggle timer", ms*portTICK_PERIOD_MS, pdTRUE, (void *) ledTimerID, ledToggleTimerCallback);
if( LEDtimers[ledTimerID] == NULL )
{
printf("Led toggle timer not created\r\n");
}
else
{
printf("Led toggle timer created\r\n");
}
xTimerStart(LEDtimers[ledTimerID], 0);
}
void TLE75008_LED_toggle_ms_stop(uint8_t LED)
{
int8_t led_toggle = LED;
led_toggle &= LED_OUT_MASK;
switch(led_toggle)
{
case LED_BUTTONS_RIGHT:
ledTimerID = 0;
break;
case LED_BUTTONS_LEFT:
ledTimerID = 1;
break;
case LED_HOME_BLUE:
ledTimerID = 2;
break;
case LED_HOME_GREEN:
ledTimerID = 3;
break;
case LED_HOME_RED:
ledTimerID = 4;
break;
default:
printf("LED toggle timer error: only choose one led at a time!\r\n");
break;
}
if( LEDtimers[ledTimerID] != NULL )
{
xTimerStop(LEDtimers[ledTimerID], 0);
}
}
void ledActivateTimerCallback(TimerHandle_t timerHandle)
{
uint8_t led = 0;
ledTimerID = pvTimerGetTimerID(timerHandle);
switch(ledTimerID)
{
case 5:
led = LED_BUTTONS_RIGHT;
break;
case 6:
led = LED_BUTTONS_LEFT;
break;
case 7:
led = LED_HOME_BLUE;
break;
case 8:
led = LED_HOME_GREEN;
break;
case 9:
led = LED_HOME_RED;
break;
default:
break;
}
led_activate_flag = led;
//TLE75008_LED_deactivate(led);
}
void TLE75008_LED_activate_ms(uint8_t LED, uint32_t ms)
{
int8_t led_activate = LED;
led_activate &= LED_OUT_MASK;
switch(led_activate)
{
case LED_BUTTONS_RIGHT:
ledTimerID = 5;
break;
case LED_BUTTONS_LEFT:
ledTimerID = 6;
break;
case LED_HOME_BLUE:
ledTimerID = 7;
break;
case LED_HOME_GREEN:
ledTimerID = 8;
break;
case LED_HOME_RED:
ledTimerID = 9;
break;
default:
printf("LED activate timer error: only choose one led at a time!\r\n");
break;
}
LEDtimers[ledTimerID] = xTimerCreate("LED activate timer", ms*portTICK_PERIOD_MS, pdFALSE, (void *) ledTimerID, ledActivateTimerCallback);
if( LEDtimers[ledTimerID] == NULL )
{
printf("Led toggle timer not created\r\n");
}
else
{
printf("Led toggle timer created\r\n");
}
TLE75008_LED_activate(LED);
xTimerStart(LEDtimers[ledTimerID], 0);
}