View difference between Paste ID: GbxWizK6 and UnjqR6w0
SHOW: | | - or go back to the newest paste.
1
void sendSerial() {
2
	
3
	uint32_t nBitsSent = 0;
4
	
5
	/* 	Padding: we need to pre-pad
6
		We will send 15 12-bit values (180 bits) which is 22.5 8-bit SPI 8-bit bytes
7
		For every TLC5490 we actually need to send 16 12-bit values (192 bits in total)
8
		because that's what the TLC5490 serial comms expect. 
9
		Since we are using only 15 of the output lines, 
10
		the 16th value is unnecessary (we are not using OUT15)
11
		We also need to pad for the extra nibble (22.5 SPI above) 
12
		So if we start by sending the empty 12-bit value, we are OK
13
		This padding needs to be done for every chip
14
	*/
15
	//~ padSPI(0);
16
	SPDR = 0;					// First 8 bits of the empty 12-bit data
17
	while(!(SPSR & (1<<SPIF))) {
18
		// Wait for the transfer to finish
19
	}
20
	SPDR = 0;					// Last 4 bits of the empty 12-bit data
21
								// plus the empty 4 high bits of the first intensity data
22
	while(!(SPSR & (1<<SPIF))) {
23
		// Wait for the transfer to finish
24
	}
25
	nBitsSent += 16;
26
	uint8_t idxNextSendType = 2;	// The next send type is type 2
27
										// so we indicate that
28
	
29
	// Different curColDatas are not implemented yet
30
	//~ uint8_t * curColData = nColumnData[idxUnit];
31
	
32
	uint8_t idxChannel = 0;
33
	uint8_t nPixelsSent = 0;
34
	uint8_t dataByte = (idxChannel==0)?0xFF:0x00;
35
	uint8_t toSPDR = dataByte;
36
	
37
	for (;;) { // Send bytes loop
38
		SPDR = toSPDR;			// Send the byte
39
		nBitsSent += 8;
40
41
		while(!(SPSR & (1<<SPIF))) {
42
			// Wait for the transfer to finish
43
		}
44
		
45
#define PADDING
46
#ifdef PADDING
47
		if (nBitsSent % 192 == 0) {
48
			if (nBitsSent >= 192 *3)
49
				break;
50
			
51
			
52
			SPDR = 0;					// First 8 bits of the empty 12-bit data
53
			while(!(SPSR & (1<<SPIF))) {
54
				// Wait for the transfer to finish
55
			}
56
			SPDR = 0;					// Last 4 bits of the empty 12-bit data
57
										// plus the empty 4 high bits of the first intensity data
58
			while(!(SPSR & (1<<SPIF))) {
59
				// Wait for the transfer to finish
60
			}
61
			idxNextSendType = 1;		// It will get incremented first
62
										// so the first byte will actually be
63
										// idxNextSendType = 2
64
65
			nBitsSent += 16;
66
		}
67
#endif								
68
		
69
		// Update the send type
70
		if (++idxNextSendType>2) { idxNextSendType = 0; }
71
		//~ ++idxNextSendType %= 4;
72
		
73
		// Prepare the next one for loading
74
		if (idxNextSendType==1) {
75
				// Case 1: Need to send the low nibble + padding
76
				toSPDR = dataByte<<4;
77
		} else {
78
			// Either way we need another new data in dataByte
79
			// 
80
			if (idxChannel==0) {
81
				// Load next palette index
82
				nPixelsSent++;
83
			}
84
			dataByte = (idxChannel==0)?0xFF:0x00;
85
			
86
			if (++idxChannel>2) idxChannel = 0;
87
			//~ This fails: ++idxChannel %= 3;
88
			
89
			if (idxNextSendType) {
90
				// idxSendType == 2
91
				// Case 2: Need to send pure data
92
				toSPDR = dataByte;
93
			} else {
94
				// idxSendType == 0
95
				// Case 0: Need to send padding + high nibble
96
				toSPDR = dataByte >> 4;
97
			}
98
		}
99
100
		while(!(SPSR & (1<<SPIF))) 	// Wait for the transfer to finish
101
			;				
102
		
103-
	} // End of send bytes loop
103+
	} // End of send bytes loop
104
}