Rev 5 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | /* |
2 | * leds.c |
||
3 | * |
||
4 | * Created on: 7 Jul 2019 |
||
5 | * Author: Mike |
||
6 | */ |
||
7 | |||
8 | #include "main.h" |
||
9 | #include "leds.h" |
||
10 | |||
11 | // Target clock rate 800 kHz |
||
12 | #define SPICLOCK 9000000UL |
||
13 | // one clock of SPI is 166nS |
||
14 | #define TSPI (1000000000UL/SPICLOCK) |
||
15 | // reset period is 50000nS |
||
16 | #define TRESET 50000 |
||
5 | mjames | 17 | // stop period is 20000nS |
18 | #define TSTOP 50000 |
||
2 | mjames | 19 | // total bit period is 1250nS or 1/800kHz |
20 | #define TBIT 1250 |
||
21 | // to produce '0' value go high for 400nS, low for 850nS |
||
22 | #define T0H 400 |
||
23 | #define T0L 850 |
||
24 | // to produce '1' value go high for 850nS, low for 400nS |
||
25 | #define T1H 850 |
||
26 | #define T1L 400 |
||
27 | |||
28 | |||
29 | |||
5 | mjames | 30 | |
2 | mjames | 31 | #define BUFFER_SIZE 1000 |
32 | |||
33 | |||
34 | |||
35 | |||
36 | uint8_t buffer [BUFFER_SIZE]; |
||
37 | // byte pointer |
||
38 | unsigned bufPtr; |
||
39 | // bit index pointer |
||
40 | uint8_t bitPtr; |
||
41 | // bit transmission time offset from start of transmission |
||
42 | unsigned SPItime = 0; |
||
43 | |||
44 | |||
45 | unsigned RealTime = 0; |
||
46 | |||
47 | static volatile uint8_t busy = 0; |
||
48 | |||
49 | void initCode(void) |
||
50 | { |
||
51 | bufPtr = 0; |
||
52 | bitPtr = 0; |
||
53 | SPItime = 0; |
||
54 | RealTime = 0; |
||
55 | } |
||
56 | |||
57 | // send a '1' or '0' on SPI bus |
||
58 | void codeBit(uint8_t val, unsigned until) |
||
59 | { |
||
60 | // calculate true limit time |
||
61 | RealTime += until; |
||
5 | mjames | 62 | while(SPItime<= RealTime) |
2 | mjames | 63 | { |
64 | if(val) |
||
65 | buffer[bufPtr] |= (1<<bitPtr); |
||
66 | else |
||
67 | buffer[bufPtr] &= ~(1<<bitPtr); |
||
68 | bitPtr++; |
||
69 | if(bitPtr == 8) |
||
70 | { |
||
71 | bitPtr = 0; |
||
72 | bufPtr ++; |
||
73 | } |
||
74 | // accumulate transmission time on SPI bus |
||
75 | SPItime += TSPI; |
||
76 | |||
77 | if(bufPtr >= BUFFER_SIZE) |
||
78 | bufPtr = BUFFER_SIZE-1; // limit |
||
79 | } |
||
80 | } |
||
81 | |||
82 | |||
83 | void codeReset (void) |
||
84 | { |
||
85 | codeBit(0,TRESET); |
||
86 | } |
||
87 | |||
5 | mjames | 88 | void codeStop (void) |
89 | { |
||
90 | codeBit(0,TSTOP); |
||
91 | } |
||
92 | |||
2 | mjames | 93 | // send a single Run Length code bit |
94 | void codeRL(uint8_t bit) |
||
95 | { |
||
96 | unsigned lim = bit ? T1H : T0H; |
||
97 | codeBit(1,lim); |
||
98 | codeBit(0,TBIT-lim); |
||
99 | } |
||
100 | |||
101 | // code a byte using the RL code |
||
102 | void codeRLByte(uint8_t val) |
||
103 | { |
||
104 | uint8_t i; |
||
105 | for (i=0; i<8; i++) |
||
106 | { |
||
107 | codeRL(val & 0x80); |
||
108 | val<<=1; |
||
109 | } |
||
110 | |||
111 | } |
||
112 | |||
5 | mjames | 113 | |
114 | |||
115 | |||
116 | |||
117 | void codeFRGBW(frgbw_t led ) |
||
2 | mjames | 118 | { |
119 | |||
120 | codeRLByte(((uint16_t)(led.green) * led.fader)/256); |
||
121 | codeRLByte(((uint16_t)(led.red) * led.fader)/256); |
||
122 | codeRLByte(((uint16_t)(led.blue) * led.fader)/256); |
||
5 | mjames | 123 | codeRLByte(((uint16_t)(led.white) * led.fader)/256); |
2 | mjames | 124 | } |
125 | |||
126 | |||
5 | mjames | 127 | frgbw_t led0 = { 128, 0, 0, 0, 0 }; |
128 | frgbw_t led1 = { 128, 0, 0, 0, 2 }; |
||
129 | frgbw_t ledZ = { 0,0,0,0 }; |
||
2 | mjames | 130 | |
131 | |||
132 | void sendLeds() |
||
133 | { |
||
134 | |||
135 | initCode(); |
||
136 | codeReset(); |
||
137 | |||
138 | led0.green++; |
||
5 | mjames | 139 | /// led0.fader++; |
4 | mjames | 140 | |
141 | led1.red++; |
||
5 | mjames | 142 | // led1.fader--; |
4 | mjames | 143 | |
5 | mjames | 144 | codeFRGBW(led0); |
145 | codeFRGBW(led1); |
||
146 | |||
2 | mjames | 147 | // send terminal |
5 | mjames | 148 | |
149 | codeStop(); |
||
150 | busy= 1; |
||
151 | HAL_SPI_Transmit_DMA(&hspi1,buffer,bufPtr); |
||
2 | mjames | 152 | // |
153 | while(busy) {}; |
||
154 | } |
||
155 | |||
156 | |||
157 | void HAL_SPI_TxCpltCallback( SPI_HandleTypeDef * hspi) |
||
158 | { |
||
159 | (void ) hspi; |
||
160 | busy = 0; |
||
161 | } |
||
162 | |||
163 | |||
164 |