Rev 6 | 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" |
||
8 | mjames | 10 | #include "dmx.h" |
2 | mjames | 11 | |
12 | // Target clock rate 800 kHz |
||
13 | #define SPICLOCK 9000000UL |
||
14 | // one clock of SPI is 166nS |
||
15 | #define TSPI (1000000000UL/SPICLOCK) |
||
16 | // reset period is 50000nS |
||
17 | #define TRESET 50000 |
||
5 | mjames | 18 | // stop period is 20000nS |
19 | #define TSTOP 50000 |
||
2 | mjames | 20 | // total bit period is 1250nS or 1/800kHz |
21 | #define TBIT 1250 |
||
22 | // to produce '0' value go high for 400nS, low for 850nS |
||
23 | #define T0H 400 |
||
24 | #define T0L 850 |
||
25 | // to produce '1' value go high for 850nS, low for 400nS |
||
26 | #define T1H 850 |
||
27 | #define T1L 400 |
||
28 | |||
29 | |||
30 | |||
5 | mjames | 31 | |
2 | mjames | 32 | #define BUFFER_SIZE 1000 |
33 | |||
34 | |||
35 | |||
36 | |||
37 | uint8_t buffer [BUFFER_SIZE]; |
||
38 | // byte pointer |
||
39 | unsigned bufPtr; |
||
40 | // bit index pointer |
||
41 | uint8_t bitPtr; |
||
42 | // bit transmission time offset from start of transmission |
||
43 | unsigned SPItime = 0; |
||
44 | |||
45 | |||
46 | unsigned RealTime = 0; |
||
47 | |||
48 | static volatile uint8_t busy = 0; |
||
49 | |||
50 | void initCode(void) |
||
51 | { |
||
52 | bufPtr = 0; |
||
53 | bitPtr = 0; |
||
54 | SPItime = 0; |
||
55 | RealTime = 0; |
||
56 | } |
||
57 | |||
58 | // send a '1' or '0' on SPI bus |
||
59 | void codeBit(uint8_t val, unsigned until) |
||
60 | { |
||
61 | // calculate true limit time |
||
62 | RealTime += until; |
||
5 | mjames | 63 | while(SPItime<= RealTime) |
2 | mjames | 64 | { |
65 | if(val) |
||
66 | buffer[bufPtr] |= (1<<bitPtr); |
||
67 | else |
||
68 | buffer[bufPtr] &= ~(1<<bitPtr); |
||
69 | bitPtr++; |
||
70 | if(bitPtr == 8) |
||
71 | { |
||
72 | bitPtr = 0; |
||
73 | bufPtr ++; |
||
74 | } |
||
75 | // accumulate transmission time on SPI bus |
||
76 | SPItime += TSPI; |
||
77 | |||
78 | if(bufPtr >= BUFFER_SIZE) |
||
79 | bufPtr = BUFFER_SIZE-1; // limit |
||
80 | } |
||
81 | } |
||
82 | |||
83 | |||
8 | mjames | 84 | |
2 | mjames | 85 | void codeReset (void) |
86 | { |
||
87 | codeBit(0,TRESET); |
||
88 | } |
||
89 | |||
5 | mjames | 90 | void codeStop (void) |
91 | { |
||
92 | codeBit(0,TSTOP); |
||
93 | } |
||
94 | |||
2 | mjames | 95 | // send a single Run Length code bit |
96 | void codeRL(uint8_t bit) |
||
97 | { |
||
98 | unsigned lim = bit ? T1H : T0H; |
||
99 | codeBit(1,lim); |
||
100 | codeBit(0,TBIT-lim); |
||
101 | } |
||
102 | |||
103 | // code a byte using the RL code |
||
104 | void codeRLByte(uint8_t val) |
||
105 | { |
||
106 | uint8_t i; |
||
107 | for (i=0; i<8; i++) |
||
108 | { |
||
109 | codeRL(val & 0x80); |
||
110 | val<<=1; |
||
111 | } |
||
112 | } |
||
113 | |||
5 | mjames | 114 | |
115 | void codeFRGBW(frgbw_t led ) |
||
2 | mjames | 116 | { |
117 | |||
118 | codeRLByte(((uint16_t)(led.green) * led.fader)/256); |
||
119 | codeRLByte(((uint16_t)(led.red) * led.fader)/256); |
||
120 | codeRLByte(((uint16_t)(led.blue) * led.fader)/256); |
||
8 | mjames | 121 | codeRLByte(((uint16_t)(led.white) * led.fader)/256); // if only RGB LEDS skip this step. |
2 | mjames | 122 | } |
123 | |||
124 | |||
8 | mjames | 125 | // hook the weak symbol |
126 | void HAL_SPI_TxCpltCallback( SPI_HandleTypeDef * hspi) |
||
127 | { |
||
128 | (void ) hspi; |
||
129 | busy = 0; |
||
130 | } |
||
2 | mjames | 131 | |
132 | |||
8 | mjames | 133 | void sendCode(void) |
2 | mjames | 134 | { |
8 | mjames | 135 | busy= 1; |
5 | mjames | 136 | HAL_SPI_Transmit_DMA(&hspi1,buffer,bufPtr); |
2 | mjames | 137 | // |
138 | while(busy) {}; |
||
8 | mjames | 139 | }; |
2 | mjames | 140 | |
141 |