Rev 4 | Go to most recent revision | Details | 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 |
||
| 17 | // total bit period is 1250nS or 1/800kHz |
||
| 18 | #define TBIT 1250 |
||
| 19 | // to produce '0' value go high for 400nS, low for 850nS |
||
| 20 | #define T0H 400 |
||
| 21 | #define T0L 850 |
||
| 22 | // to produce '1' value go high for 850nS, low for 400nS |
||
| 23 | #define T1H 850 |
||
| 24 | #define T1L 400 |
||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | #define BUFFER_SIZE 1000 |
||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | |||
| 33 | uint8_t buffer [BUFFER_SIZE]; |
||
| 34 | // byte pointer |
||
| 35 | unsigned bufPtr; |
||
| 36 | // bit index pointer |
||
| 37 | uint8_t bitPtr; |
||
| 38 | // bit transmission time offset from start of transmission |
||
| 39 | unsigned SPItime = 0; |
||
| 40 | |||
| 41 | DMX_State_t DMX_State = DMX_IDLE; |
||
| 42 | |||
| 43 | unsigned RealTime = 0; |
||
| 44 | |||
| 45 | static volatile uint8_t busy = 0; |
||
| 46 | |||
| 47 | void initCode(void) |
||
| 48 | { |
||
| 49 | bufPtr = 0; |
||
| 50 | bitPtr = 0; |
||
| 51 | SPItime = 0; |
||
| 52 | RealTime = 0; |
||
| 53 | } |
||
| 54 | |||
| 55 | // send a '1' or '0' on SPI bus |
||
| 56 | void codeBit(uint8_t val, unsigned until) |
||
| 57 | { |
||
| 58 | // calculate true limit time |
||
| 59 | RealTime += until; |
||
| 60 | while(SPItime< RealTime) |
||
| 61 | { |
||
| 62 | if(val) |
||
| 63 | buffer[bufPtr] |= (1<<bitPtr); |
||
| 64 | else |
||
| 65 | buffer[bufPtr] &= ~(1<<bitPtr); |
||
| 66 | bitPtr++; |
||
| 67 | if(bitPtr == 8) |
||
| 68 | { |
||
| 69 | bitPtr = 0; |
||
| 70 | bufPtr ++; |
||
| 71 | } |
||
| 72 | // accumulate transmission time on SPI bus |
||
| 73 | SPItime += TSPI; |
||
| 74 | |||
| 75 | if(bufPtr >= BUFFER_SIZE) |
||
| 76 | bufPtr = BUFFER_SIZE-1; // limit |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | |||
| 81 | void codeReset (void) |
||
| 82 | { |
||
| 83 | codeBit(0,TRESET); |
||
| 84 | } |
||
| 85 | |||
| 86 | // send a single Run Length code bit |
||
| 87 | void codeRL(uint8_t bit) |
||
| 88 | { |
||
| 89 | unsigned lim = bit ? T1H : T0H; |
||
| 90 | codeBit(1,lim); |
||
| 91 | codeBit(0,TBIT-lim); |
||
| 92 | } |
||
| 93 | |||
| 94 | // code a byte using the RL code |
||
| 95 | void codeRLByte(uint8_t val) |
||
| 96 | { |
||
| 97 | uint8_t i; |
||
| 98 | for (i=0; i<8; i++) |
||
| 99 | { |
||
| 100 | codeRL(val & 0x80); |
||
| 101 | val<<=1; |
||
| 102 | } |
||
| 103 | |||
| 104 | } |
||
| 105 | |||
| 106 | void codeFRGB(frgb_t led ) |
||
| 107 | { |
||
| 108 | |||
| 109 | codeRLByte(((uint16_t)(led.green) * led.fader)/256); |
||
| 110 | codeRLByte(((uint16_t)(led.red) * led.fader)/256); |
||
| 111 | codeRLByte(((uint16_t)(led.blue) * led.fader)/256); |
||
| 112 | } |
||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | frgb_t led0 = { 128, 0, 0, 0 }; |
||
| 117 | frgb_t ledZ = { 0,0,0,0 }; |
||
| 118 | |||
| 119 | void sendLeds() |
||
| 120 | { |
||
| 121 | |||
| 122 | initCode(); |
||
| 123 | codeReset(); |
||
| 124 | |||
| 125 | led0.green++; |
||
| 126 | led0.fader++; |
||
| 127 | if(led0.green %4 == 0) |
||
| 128 | led0.red --; |
||
| 129 | if(led0.green %55 == 0) |
||
| 130 | led0.blue +=123; |
||
| 131 | codeFRGB(led0); |
||
| 132 | // send terminal |
||
| 133 | codeFRGB(ledZ); |
||
| 134 | busy= 1; |
||
| 135 | HAL_SPI_Transmit_DMA(&hspi1,buffer,bufPtr+1); |
||
| 136 | // |
||
| 137 | while(busy) {}; |
||
| 138 | } |
||
| 139 | |||
| 140 | |||
| 141 | void HAL_SPI_TxCpltCallback( SPI_HandleTypeDef * hspi) |
||
| 142 | { |
||
| 143 | (void ) hspi; |
||
| 144 | busy = 0; |
||
| 145 | } |
||
| 146 | |||
| 147 | |||
| 148 |