Rev 4 | Rev 6 | 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 | |||
5 | mjames | 29 | // DMX state variables |
30 | DMX_State_t DMX_State = DMX_IDLE; |
||
31 | uint8_t DMX_Buffer[512]; |
||
32 | uint32_t DMX_Pointer; |
||
2 | mjames | 33 | |
5 | mjames | 34 | |
35 | |||
2 | mjames | 36 | #define BUFFER_SIZE 1000 |
37 | |||
38 | |||
39 | |||
40 | |||
41 | uint8_t buffer [BUFFER_SIZE]; |
||
42 | // byte pointer |
||
43 | unsigned bufPtr; |
||
44 | // bit index pointer |
||
45 | uint8_t bitPtr; |
||
46 | // bit transmission time offset from start of transmission |
||
47 | unsigned SPItime = 0; |
||
48 | |||
49 | |||
50 | unsigned RealTime = 0; |
||
51 | |||
52 | static volatile uint8_t busy = 0; |
||
53 | |||
54 | void initCode(void) |
||
55 | { |
||
56 | bufPtr = 0; |
||
57 | bitPtr = 0; |
||
58 | SPItime = 0; |
||
59 | RealTime = 0; |
||
60 | } |
||
61 | |||
62 | // send a '1' or '0' on SPI bus |
||
63 | void codeBit(uint8_t val, unsigned until) |
||
64 | { |
||
65 | // calculate true limit time |
||
66 | RealTime += until; |
||
5 | mjames | 67 | while(SPItime<= RealTime) |
2 | mjames | 68 | { |
69 | if(val) |
||
70 | buffer[bufPtr] |= (1<<bitPtr); |
||
71 | else |
||
72 | buffer[bufPtr] &= ~(1<<bitPtr); |
||
73 | bitPtr++; |
||
74 | if(bitPtr == 8) |
||
75 | { |
||
76 | bitPtr = 0; |
||
77 | bufPtr ++; |
||
78 | } |
||
79 | // accumulate transmission time on SPI bus |
||
80 | SPItime += TSPI; |
||
81 | |||
82 | if(bufPtr >= BUFFER_SIZE) |
||
83 | bufPtr = BUFFER_SIZE-1; // limit |
||
84 | } |
||
85 | } |
||
86 | |||
87 | |||
88 | void codeReset (void) |
||
89 | { |
||
90 | codeBit(0,TRESET); |
||
91 | } |
||
92 | |||
5 | mjames | 93 | void codeStop (void) |
94 | { |
||
95 | codeBit(0,TSTOP); |
||
96 | } |
||
97 | |||
2 | mjames | 98 | // send a single Run Length code bit |
99 | void codeRL(uint8_t bit) |
||
100 | { |
||
101 | unsigned lim = bit ? T1H : T0H; |
||
102 | codeBit(1,lim); |
||
103 | codeBit(0,TBIT-lim); |
||
104 | } |
||
105 | |||
106 | // code a byte using the RL code |
||
107 | void codeRLByte(uint8_t val) |
||
108 | { |
||
109 | uint8_t i; |
||
110 | for (i=0; i<8; i++) |
||
111 | { |
||
112 | codeRL(val & 0x80); |
||
113 | val<<=1; |
||
114 | } |
||
115 | |||
116 | } |
||
117 | |||
5 | mjames | 118 | |
119 | |||
120 | |||
121 | |||
122 | void codeFRGBW(frgbw_t led ) |
||
2 | mjames | 123 | { |
124 | |||
125 | codeRLByte(((uint16_t)(led.green) * led.fader)/256); |
||
126 | codeRLByte(((uint16_t)(led.red) * led.fader)/256); |
||
127 | codeRLByte(((uint16_t)(led.blue) * led.fader)/256); |
||
5 | mjames | 128 | codeRLByte(((uint16_t)(led.white) * led.fader)/256); |
2 | mjames | 129 | } |
130 | |||
131 | |||
5 | mjames | 132 | frgbw_t led0 = { 128, 0, 0, 0, 0 }; |
133 | frgbw_t led1 = { 128, 0, 0, 0, 2 }; |
||
134 | frgbw_t ledZ = { 0,0,0,0 }; |
||
2 | mjames | 135 | |
136 | |||
137 | void sendLeds() |
||
138 | { |
||
139 | |||
140 | initCode(); |
||
141 | codeReset(); |
||
142 | |||
143 | led0.green++; |
||
5 | mjames | 144 | /// led0.fader++; |
4 | mjames | 145 | |
146 | led1.red++; |
||
5 | mjames | 147 | // led1.fader--; |
4 | mjames | 148 | |
5 | mjames | 149 | codeFRGBW(led0); |
150 | codeFRGBW(led1); |
||
151 | |||
2 | mjames | 152 | // send terminal |
5 | mjames | 153 | |
154 | codeStop(); |
||
155 | busy= 1; |
||
156 | HAL_SPI_Transmit_DMA(&hspi1,buffer,bufPtr); |
||
2 | mjames | 157 | // |
158 | while(busy) {}; |
||
159 | } |
||
160 | |||
161 | |||
162 | void HAL_SPI_TxCpltCallback( SPI_HandleTypeDef * hspi) |
||
163 | { |
||
164 | (void ) hspi; |
||
165 | busy = 0; |
||
166 | } |
||
167 | |||
168 | |||
169 |