-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
316 lines (272 loc) · 10.8 KB
/
main.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
/**
This code runs on the TI MSP430 5529 launchpad
http://www.ti.com/tool/msp-exp430f5529lp?DCMP=msp-f5529&HQS=msp-f5529-b
it supports the led strips with the 800kHz WS2812 controller chips, e.g.
http://www.adafruit.com/products/1138
it is partly based on the idea of UART patterns from robg:
http://forum.43oh.com/topic/3971-wearable-ws2812-strip-controllers/
but uses the DMA rather than a for() loop:
+ work is delegated to DMA, frees foreground application (easier to code color changes)
- needs app. 8x more RAM, as the LED bits must be deflated in advance
REF0 serves as the reference for the FLL. DCO is set to app. 24MHz. SPI UART runs
from SMCLK (app.8MHz). XT2 is not used so porting the code to any 5xx derivate
with the REFO should not be difficult
LED strip must be connected to P3.3 on the launchpad header
An LED strip of 60 LEDs takes about 2msec to update
The code serves demonstration purposes only, may need cleanup. Have fun!
marfis ([email protected])
*/
#include <msp430.h>
#include <stdint.h>
#include <stdbool.h>
#define NR_OF_LEDS 60 // 60 LEDs strip, configure this here
#define WS_BIT_0 0xe0 // UART pattern for Bit 0
#define WS_BIT_1 0xf8 // UART pattern for Bit 1
#define SIZE_OF_LED_ARRAY (NR_OF_LEDS * 3 * 8) // DMA buffer, 24 bytes per LED
#define max(a,b) (((a)>(b)) ? (a) : (b))
#define min(a,b) (((a)<(b)) ? (a) : (b))
#define st(x) do { x } while (__LINE__ == -1)
#define SELECT_ACLK(source) st(UCSCTL4 = (UCSCTL4 & ~(SELA_7)) | (source);)
#define SELECT_MCLK(source) st(UCSCTL4 = (UCSCTL4 & ~(SELM_7)) | (source);)
#define SELECT_SMCLK(source) st(UCSCTL4 = (UCSCTL4 & ~(SELS_7)) | (source);)
uint8_t led_raw[SIZE_OF_LED_ARRAY]; // DMA space, incl. 50usec break time
typedef struct {
uint8_t r;
uint8_t g;
uint8_t b;
} led_color_t; // led color struct
void hardware_clock_init(void) {
uint8_t i;
__disable_interrupt(); // Disable global interrupts
// Enable XT2 pins
P5SEL |= 0x0C;
// Use REFO as the FLL reference
UCSCTL3 = (UCSCTL3 & ~(SELREF_7)) | (SELREF_2);
UCSCTL4 |= SELA__REFOCLK; // ACLK source
uint16_t srRegisterState = __read_status_register() & SCG0;
__bis_SR_register(SCG0);
// Set lowest possible DCOx, MODx
UCSCTL0 = 0x0000;
// Select DCO range 50MHz operation
UCSCTL1 = DCORSEL_7 | 1;
UCSCTL2 = FLLD_0 + 550;
__bic_SR_register(SCG0);
for (i=0; i<100; i++) {
__delay_cycles(0xffff);
}
// check for oscillator fault
while (SFRIFG1 & OFIFG) {
UCSCTL7 &= ~(DCOFFG+XT1LFOFFG+XT2OFFG);
SFRIFG1 &= ~OFIFG;
}
SELECT_MCLK(SELM__DCOCLK);
SELECT_ACLK(SELA__REFOCLK);
SELECT_SMCLK(SELS__DCOCLK);
UCSCTL7 = 0; // Errata UCS11
__bis_SR_register(srRegisterState); // Restore previous SCG0
}
// TimerA0 Interrupt Vector (TAIV) handler, re-enables the DMA
static void __attribute__((__interrupt__(TIMER0_A0_VECTOR))) timer_a_irq(void) {
TA0CCR0 += 300;
DMA2CTL |= DMAEN | DMAIE ;
DMA2SA = (uint16_t)(&led_raw[1]); // source
UCA0TXBUF = led_raw[0];
}
// the DMA interrupt of channel2 just exits from low power mode
// so that a foreground application can do its work
static void __attribute__((__interrupt__(DMA_VECTOR))) dma_irq(void) {
switch(DMAIV) {
case 0: break;
case 2: break;
case 4: break; // DMA1IFG = DMA Channel 1
case 6:
// DMA transfer to LEDs finished, exit low power
LPM0_EXIT;
break;
case 8: break; // DMA3IFG = DMA Channel 3
case 10: break; // DMA4IFG = DMA Channel 4
case 12: break; // DMA5IFG = DMA Channel 5
case 14: break; // DMA6IFG = DMA Channel 6
case 16: break; // DMA7IFG = DMA Channel 7
default: break;
}
}
/* timer_init
*
* initalizes timerA0 to 10msec intervals, CCRO, running from REFO (32kHz)
*
*/
void timer_init(void) {
TA0CCTL0 = CCIE;
TA0CCR0 = 300;
// ACLK, continous up, clear TAR
TA0CTL = TASSEL_1 + MC__CONTINOUS + TACLR;
}
/*
* init_spi_master
* configures USCIA0 to SPI master ,8MHz clock rate, 8Bit, running from SMCLK
*
*/
void init_spi_master(void) {
P3SEL |= BIT3+BIT4; // P3.3,4 option select
P2SEL |= BIT7; // P2.7 option select
UCA0CTL1 |= UCSWRST; // **Put state machine in reset**
UCA0CTL0 |= UCMST+UCSYNC+UCCKPL+UCMSB; // 3-pin, 8-bit SPI master
// Clock polarity high, MSB
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 3; // /3 = ca.8MHz
UCA0BR1 = 0; //
UCA0MCTL = 0; // No modulation
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
}
/* SetVCoreUp
* TI PMM driverlib Code to set Vcore to a given level
*
* this is complicated code, but apparently is needed to come around
* erratum FLASH37
* removed some original comments to compress code
*
*/
uint16_t SetVCoreUp(uint8_t level)
{
uint16_t PMMRIE_backup, SVSMHCTL_backup, SVSMLCTL_backup;
// Open PMM registers for write access
PMMCTL0_H = 0xA5;
// Disable dedicated Interrupts
// Backup all registers
PMMRIE_backup = PMMRIE;
PMMRIE &= ~(SVMHVLRPE | SVSHPE | SVMLVLRPE | SVSLPE | SVMHVLRIE | SVMHIE | SVSMHDLYIE | SVMLVLRIE | SVMLIE | SVSMLDLYIE );
SVSMHCTL_backup = SVSMHCTL;
SVSMLCTL_backup = SVSMLCTL;
// Clear flags
PMMIFG = 0;
// Set SVM highside to new level and check if a VCore increase is possible
SVSMHCTL = SVMHE | SVSHE | (SVSMHRRL0 * level);
// Wait until SVM highside is settled
while ((PMMIFG & SVSMHDLYIFG) == 0);
// Clear flag
PMMIFG &= ~SVSMHDLYIFG;
// Check if a VCore increase is possible
if ((PMMIFG & SVMHIFG) == SVMHIFG) { // -> Vcc is too low for a Vcore increase
// recover the previous settings
PMMIFG &= ~SVSMHDLYIFG;
SVSMHCTL = SVSMHCTL_backup;
// Wait until SVM highside is settled
while ((PMMIFG & SVSMHDLYIFG) == 0);
// Clear all Flags
PMMIFG &= ~(SVMHVLRIFG | SVMHIFG | SVSMHDLYIFG | SVMLVLRIFG | SVMLIFG | SVSMLDLYIFG);
PMMRIE = PMMRIE_backup; // Restore PMM interrupt enable register
PMMCTL0_H = 0x00; // Lock PMM registers for write access
return 0; // return: voltage not set
}
// Set also SVS highside to new level
SVSMHCTL |= (SVSHRVL0 * level);
PMMIFG &= ~SVSMHDLYIFG;
// Set VCore to new level
PMMCTL0_L = PMMCOREV0 * level;
// Set SVM, SVS low side to new level
SVSMLCTL = SVMLE | (SVSMLRRL0 * level) | SVSLE | (SVSLRVL0 * level);
PMMIFG &= ~SVSMLDLYIFG;
SVSMLCTL &= (SVSLRVL0+SVSLRVL1+SVSMLRRL0+SVSMLRRL1+SVSMLRRL2);
SVSMLCTL_backup &= ~(SVSLRVL0+SVSLRVL1+SVSMLRRL0+SVSMLRRL1+SVSMLRRL2);
SVSMLCTL |= SVSMLCTL_backup;
SVSMHCTL &= (SVSHRVL0+SVSHRVL1+SVSMHRRL0+SVSMHRRL1+SVSMHRRL2);
SVSMHCTL_backup &= ~(SVSHRVL0+SVSHRVL1+SVSMHRRL0+SVSMHRRL1+SVSMHRRL2);
SVSMHCTL |= SVSMHCTL_backup;
PMMIFG &= ~(SVMHVLRIFG | SVMHIFG | SVSMHDLYIFG | SVMLVLRIFG | SVMLIFG | SVSMLDLYIFG);
PMMRIE = PMMRIE_backup; // Restore PMM interrupt enable register
PMMCTL0_H = 0x00; // Lock PMM registers for write access
return 1;
}
/* _set_single_color
*
* writes one brightness byte at the given location in the led array
*
* as the DMA uses the led array directly to output the bits over SPI
* it must be deflated in advance in memory. One bit of WS2812 information
* is one byte to transfer over SPI
* -> bit1 = WS_BIT_1 and bit0 = WS_BIT_0
*
* disadvantage is that it uses 8x more RAM...
*
* this function is normally called by ::set_LED
*
* @param led_color_ptr [in] pointer to location within the ::led_raw array
* @param led_color [in] single byte, represents the brightness
*/
void _set_single_color(uint8_t* led_color_ptr, uint8_t brightness) {
uint8_t i;
uint8_t mask = 0x80;
for (i=0; i<8; i++) {
if (brightness & mask) {
*led_color_ptr = WS_BIT_1;
} else {
*led_color_ptr = WS_BIT_0;
}
mask = mask >> 1;
led_color_ptr++;
}
}
/* set_LED
*
* writes the color information (RGB) of the given LED number
* within the ::led_raw array
*
* @param led_nr [in] position of LED within the strip
* @param led_color [in] RGB information
*/
void set_LED(uint8_t led_nr, led_color_t* led_color) {
uint8_t* led_ptr = led_raw + led_nr * 24;
_set_single_color(led_ptr, led_color->g);
_set_single_color(led_ptr+8, led_color->r);
_set_single_color(led_ptr+16, led_color->b);
}
int main(void)
{
uint16_t time = 0;
led_color_t col;
// dog
WDTCTL = WDTPW + WDTHOLD;
// set vcore to highest value, mclk 24 MHz
SetVCoreUp(0x01);
SetVCoreUp(0x02);
SetVCoreUp(0x03);
// hw init
hardware_clock_init();
init_spi_master();
timer_init();
// internal LED and debug pins
P1DIR |= BIT0;
P2DIR |= BIT2;
// channel 17 = UCATXIFG as trigger for DMA
DMACTL0 = 0;
DMACTL1 = 17;
DMACTL2 = 0;
DMACTL3 = 0;
// DMA2 configuration
// bytewise access for source and destination, increment source, single transfer
DMA2CTL = DMADT_0 | DMASRCINCR_3 | DMASRCBYTE | DMADSTBYTE | DMAIE;
DMA2SA = (uint16_t)(&led_raw[1]); // source
DMA2DA = (uint16_t)&UCA0TXBUF; // destination
DMA2SZ = SIZE_OF_LED_ARRAY-1; // size in bytes
__eint();
// all set, now let the DMA run
DMA2CTL |= DMAEN ;
UCA0TXBUF = led_raw[0];
while (1)
{
time++;
P1OUT ^= BIT0;
LPM0;
// DMA transfer has finished, you may calculate LED updates now
// timer A is triggered app. every 10msec, so you have around 6msec time
// to update the strip
// this is a color fading thing
for (uint8_t i=0; i<NR_OF_LEDS; i++){
col.g = (time % 512) + i*4;
col.r = (time % 256) + i;
col.b = (time / 1024) + (NR_OF_LEDS - i);
set_LED(i,&col);
}
}
}