-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtlc5948a.c
326 lines (287 loc) · 11.9 KB
/
tlc5948a.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
/*
* tlc5948a.c
* (c) 2016 George Louthan
* 3-clause BSD license; see license.md.
*/
#include "tlc5948a.h"
#include <stdint.h>
#include <string.h>
#include "qc13.h"
/*
* LED controller (TLC5948A)
* eUSCI_A0 - LEDs
* (write on rise, change on fall,
* clock inactive low, MSB first)
* somi, miso, clk (3-wire)
* GSCLK P1.2 (timer TA1.1)
* LAT P1.4
*
* This file's job is to keep the display going. Application logic will go
* elsewhere - this is strictly a driver for our 6 banks and 15 channels.
*
*/
#define TLC_THISISGS 0x00
#define TLC_THISISFUN 0x01
// Current TLC sending state:
uint8_t tlc_send_type = TLC_SEND_IDLE;
uint8_t tlc_tx_index = 0; // Index of currently sending buffer
uint8_t tlc_loopback_data_out = 0x00;
volatile uint8_t tlc_loopback_data_in = 0x00;
uint8_t tlc_active_bank = 0;
// Let's make these 12-bit. So the most significant hexadigit will be brightness-correct.
uint16_t tlc_bank_gs[6][16] = {
{0},
{0},
{0},
{0},
{0},
{0},
};
// This is the basic set of function data.
// A few of them can be edited.
uint8_t fun_base[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ...reserved...
// B135 / PSM(D1) 0
// B134 / PSM(D0) 0
// B133 / OLDENA 0
// B132 / IDMCUR(D1) 0
// B131 / IDMCUR(D0) 0
// B130 / IDMRPT(D0) 0
// B129 / IDMENA 0
// B128 / LATTMG(D1) 1:
0x01,
// B127 / LATTMG(D0) 1
// B126 / LSDVLT(D1) 0
// B125 / LSDVLT(D0) 0
// B124 / LODVLT(D1) 0
// B123 / LODVLT(D0) 0
// B122 / ESPWM 1
// B121 / TMGRST 1
// B120 / DSPRPT 1:
0x87,
// B119 / BLANK
// and 7 bits of global brightness correction:
0x7f,
// HERE WE SWITCH TO 7-BIT SPI.
// The following index is 18:
0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F,
0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F,
};
void tlc_set_gs() {
if (tlc_send_type != TLC_SEND_IDLE)
return;
tlc_send_type = TLC_SEND_TYPE_GS;
tlc_tx_index = 0;
EUSCI_A_SPI_transmitData(EUSCI_A0_BASE, TLC_THISISGS);
}
void tlc_set_fun() {
while (tlc_send_type != TLC_SEND_IDLE)
__no_operation(); // shouldn't ever actually have to block on this.
tlc_send_type = TLC_SEND_TYPE_FUN;
tlc_tx_index = 0;
EUSCI_A_SPI_transmitData(EUSCI_A0_BASE, TLC_THISISFUN);
}
// Stage the blank bit:
void tlc_stage_blank(uint8_t blank) {
if (blank) {
fun_base[17] |= BIT7;
fun_base[16] &= ~BIT1;
} else {
fun_base[17] &= ~BIT7;
fun_base[16] |= BIT1;
}
}
// Test the TLC chip with a shift-register loopback.
// Returns 0 for success and 1 for failure.
uint8_t tlc_test_loopback(uint8_t test_pattern) {
// Send the test pattern 34 times, and expect to receive it shifted
// a bit.
tlc_loopback_data_out = test_pattern;
while (tlc_send_type != TLC_SEND_IDLE); // I don't see this happening...
EUSCI_A_SPI_clearInterrupt(EUSCI_A0_BASE, EUSCI_A_SPI_RECEIVE_INTERRUPT);
EUSCI_A_SPI_enableInterrupt(EUSCI_A0_BASE, EUSCI_A_SPI_RECEIVE_INTERRUPT);
tlc_send_type = TLC_SEND_TYPE_LB;
tlc_tx_index = 0;
EUSCI_A_SPI_transmitData(EUSCI_A0_BASE, test_pattern);
// Spin while we send and receive:
while (tlc_send_type != TLC_SEND_IDLE);
EUSCI_A_SPI_disableInterrupt(EUSCI_A0_BASE, EUSCI_A_SPI_RECEIVE_INTERRUPT);
return tlc_loopback_data_in != (uint8_t) ((test_pattern << 7) | (test_pattern >> 1));
}
// Stage global brightness if different from default:
void tlc_stage_bc(uint8_t bc) {
bc = bc & 0b01111111; // Mask out BLANK just in case.
fun_base[17] &= 0b10000000;
fun_base[17] |= bc;
}
void tlc_init() {
// Initialize all the TLC GPIO:
// 6x banks
// GSCLK
// LAT
// SCLK, tx, rx
P1DIR |= BIT4; // TLC_LAT
P1OUT &= ~BIT4;
tlc_stage_blank(1);
// GS_CLK:
GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P1, GPIO_PIN2, GPIO_PRIMARY_MODULE_FUNCTION); // 1.2 TA1.1
// Set up USCI_A0 GPIO:
//1.5 SCLK
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1, GPIO_PIN5, GPIO_SECONDARY_MODULE_FUNCTION);
//2.0 tx
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P2, GPIO_PIN0, GPIO_SECONDARY_MODULE_FUNCTION);
//2.1 rx
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P2, GPIO_PIN1, GPIO_SECONDARY_MODULE_FUNCTION);
// First, we're going to configure the timer that outputs GSCLK.
// We want this to go as fast as possible. (Meaning as fast as we can, as
// its max, 33 MHz, is faster than our fastest possible source, 24MHz)
// Below this is configured to toggle every cycle of SMCLK,
// which should always be our fastest clock.
Timer_A_initUpModeParam gsclk_init = {};
gsclk_init.clockSource = TIMER_A_CLOCKSOURCE_SMCLK;
gsclk_init.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1;
gsclk_init.timerPeriod = 1;
gsclk_init.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE;
gsclk_init.captureCompareInterruptEnable_CCR0_CCIE = TIMER_A_CCIE_CCR0_INTERRUPT_DISABLE;
gsclk_init.timerClear = TIMER_A_SKIP_CLEAR;
gsclk_init.startTimer = false;
// Next we configure the clock that tells us when it's time to select the
// next LED channel bank.
// We'll run this off of ACLK, which is driven by our internal 39K clock.
// THIS IS OUR TIME LOOP!!!! :-D
Timer_A_initUpModeParam next_channel_timer_init = {};
next_channel_timer_init.clockSource = TIMER_A_CLOCKSOURCE_ACLK;
next_channel_timer_init.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_28;
next_channel_timer_init.timerPeriod = 2;
next_channel_timer_init.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE;
next_channel_timer_init.captureCompareInterruptEnable_CCR0_CCIE = TIMER_A_CCIE_CCR0_INTERRUPT_ENABLE;
next_channel_timer_init.timerClear = TIMER_A_SKIP_CLEAR;
next_channel_timer_init.startTimer = false;
// Start the clocks:
// A1 / GSCLK:
Timer_A_initUpMode(TIMER_A1_BASE, &gsclk_init);
Timer_A_setOutputMode(TIMER_A1_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1, TIMER_A_OUTPUTMODE_TOGGLE_RESET);
Timer_A_setCompareValue(TIMER_A1_BASE, TIMER_A_CAPTURECOMPARE_REGISTER_1, 1);
Timer_A_startCounter(TIMER_A1_BASE, TIMER_A_UP_MODE);
// A0 / LED channel timer:
Timer_A_initUpMode(TIMER_A0_BASE, &next_channel_timer_init);
Timer_A_startCounter(TIMER_A0_BASE, TIMER_A_UP_MODE);
UCA0CTLW0 |= UCSWRST; // Shut down USCI_A0,
// And USCI_A0 peripheral:
EUSCI_A_SPI_initMasterParam ini = {0};
ini.clockPhase = EUSCI_A_SPI_PHASE_DATA_CAPTURED_ONFIRST_CHANGED_ON_NEXT;
ini.clockPolarity = EUSCI_A_SPI_CLOCKPOLARITY_INACTIVITY_LOW;
ini.clockSourceFrequency = SMCLK_RATE_HZ;
ini.desiredSpiClock = 8000000;
ini.msbFirst = EUSCI_A_SPI_MSB_FIRST;
ini.selectClockSource = EUSCI_A_SPI_CLOCKSOURCE_SMCLK;
ini.spiMode = EUSCI_A_SPI_3PIN;
EUSCI_A_SPI_initMaster(EUSCI_A0_BASE, &ini);
UCA0CTLW0 &= ~UC7BIT; // put it in 8-bit mode out of caution.
UCA0CTLW0 &= ~UCSWRST; // and enable it.
EUSCI_A_SPI_clearInterrupt(EUSCI_A0_BASE, EUSCI_A_SPI_TRANSMIT_INTERRUPT);
EUSCI_A_SPI_enableInterrupt(EUSCI_A0_BASE, EUSCI_A_SPI_TRANSMIT_INTERRUPT);
tlc_set_fun();
tlc_set_gs();
}
#pragma vector=USCI_A0_VECTOR
__interrupt void EUSCI_A0_ISR(void)
{
switch (__even_in_range(UCA0IV, 4)) {
//Vector 2 - RXIFG
case 2:
// We received some garbage sent to us while we were sending.
if (tlc_send_type == TLC_SEND_TYPE_LB) {
// We're only interested in it if we're doing a loopback test.
tlc_loopback_data_in = EUSCI_B_SPI_receiveData(EUSCI_A0_BASE);
} else {
EUSCI_B_SPI_receiveData(EUSCI_A0_BASE); // Throw it away.
}
break; // End of RXIFG ///////////////////////////////////////////////////////
case 4: // Vector 4 - TXIFG : I just sent a byte.
if (tlc_send_type == TLC_SEND_TYPE_GS) {
if (tlc_tx_index == 32) { // done
LED_BANK1_OUT |= LED_BANK1_PIN | LED_BANK2_PIN | LED_BANK3_PIN
| LED_BANK4_PIN;
LED_BANK5_OUT |= LED_BANK5_PIN | LED_BANK6_PIN;
P1OUT |= BIT4; P1OUT &= ~BIT4; // Pulse LAT
tlc_send_type = TLC_SEND_IDLE;
switch (tlc_active_bank) {
case 0:
LED_BANK1_OUT &= ~LED_BANK1_PIN;
tlc_active_bank++;
break;
case 1:
LED_BANK2_OUT &= ~LED_BANK2_PIN;
tlc_active_bank++;
break;
case 2:
LED_BANK3_OUT &= ~LED_BANK3_PIN;
tlc_active_bank++;
break;
case 3:
LED_BANK4_OUT &= ~LED_BANK4_PIN;
tlc_active_bank++;
break;
case 4:
LED_BANK5_OUT &= ~LED_BANK5_PIN;
tlc_active_bank++;
break;
case 5:
LED_BANK6_OUT &= ~LED_BANK6_PIN;
tlc_active_bank = 0;
break;
}
break;
} else { // gs - MSB first; this starts with 0.
volatile static uint16_t channel_gs = 0;
channel_gs = (tlc_bank_gs[tlc_active_bank][tlc_tx_index>>1]);
if (tlc_tx_index & 0x01) { // odd; less significant byte
UCA0TXBUF = (channel_gs & 0xff);
} else { // even; more significant byte
UCA0TXBUF = (channel_gs >> 8) & 0xff;
}
}
tlc_tx_index++;
} else if (tlc_send_type == TLC_SEND_TYPE_FUN) {
if (tlc_tx_index == 18) { // after 18 we have to switch to 7-bit mode.
UCA0CTLW0 |= UCSWRST;
UCA0CTLW0 |= UC7BIT;
UCA0CTLW0 &= ~UCSWRST;
EUSCI_A_SPI_clearInterrupt(EUSCI_A0_BASE, EUSCI_A_SPI_TRANSMIT_INTERRUPT);
EUSCI_A_SPI_enableInterrupt(EUSCI_A0_BASE, EUSCI_A_SPI_TRANSMIT_INTERRUPT);
} else if (tlc_tx_index == 34) {
GPIO_pulse(TLC_LATPORT, TLC_LATPIN); // LATCH.
UCA0CTLW0 |= UCSWRST;
UCA0CTLW0 &= ~UC7BIT;
UCA0CTLW0 &= ~UCSWRST;
EUSCI_A_SPI_clearInterrupt(EUSCI_A0_BASE, EUSCI_A_SPI_TRANSMIT_INTERRUPT);
EUSCI_A_SPI_enableInterrupt(EUSCI_A0_BASE, EUSCI_A_SPI_TRANSMIT_INTERRUPT);
tlc_send_type = TLC_SEND_IDLE;
break;
}
EUSCI_A_SPI_transmitData(EUSCI_A0_BASE, fun_base[tlc_tx_index]);
tlc_tx_index++;
} else if (tlc_send_type == TLC_SEND_TYPE_LB) { // Loopback for POST
if (tlc_tx_index == 33) {
tlc_send_type = TLC_SEND_IDLE;
break;
}
EUSCI_A_SPI_transmitData(EUSCI_A0_BASE, tlc_loopback_data_out);
tlc_tx_index++;
} else {
tlc_send_type = TLC_SEND_IDLE; // probably shouldn't reach.
}
break; // End of TXIFG /////////////////////////////////////////////////////
default: break;
} // End of ISR flag switch ////////////////////////////////////////////////////
}
// Dedicated ISR for CCR0. Vector is cleared on service.
#pragma vector=TIMER0_A0_VECTOR
__interrupt void TIMER0_A0_ISR_HOOK(void)
{
f_time_loop = 1;
tlc_set_gs();
__bic_SR_register_on_exit(LPM0_bits);
}