ws2812.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * LEDDriver.c
  3. *
  4. * Created on: Aug 26, 2013
  5. * Author: Omri Iluz
  6. */
  7. #include "ws2812.h"
  8. #include "stdlib.h"
  9. static uint8_t *fb;
  10. static int sLeds;
  11. static stm32_gpio_t *sPort;
  12. static uint32_t sMask;
  13. uint8_t* dma_source;
  14. static LED_TYPE led_array[RGBLED_NUM];
  15. void setColor(uint8_t color, uint8_t *buf,uint32_t mask){
  16. int i;
  17. for (i=0;i<8;i++){
  18. buf[i]=((color<<i)&0b10000000?0x0:mask);
  19. }
  20. }
  21. void setColorRGB(Color c, uint8_t *buf, uint32_t mask) {
  22. setColor(c.G,buf, mask);
  23. setColor(c.R,buf+8, mask);
  24. setColor(c.B,buf+16, mask);
  25. }
  26. /**
  27. * @brief Initialize Led Driver
  28. * @details Initialize the Led Driver based on parameters.
  29. * Following initialization, the frame buffer would automatically be
  30. * exported to the supplied port and pins in the right timing to drive
  31. * a chain of WS2812B controllers
  32. * @note The function assumes the controller is running at 72Mhz
  33. * @note Timing is critical for WS2812. While all timing is done in hardware
  34. * need to verify memory bandwidth is not exhausted to avoid DMA delays
  35. *
  36. * @param[in] leds length of the LED chain controlled by each pin
  37. * @param[in] port which port would be used for output
  38. * @param[in] mask Which pins would be used for output, each pin is a full chain
  39. * @param[out] o_fb initialized frame buffer
  40. *
  41. */
  42. void WS2812_init(void) {
  43. static uint8_t * p;
  44. //uint32_t port = RGBLED_PORT;
  45. //ledDriverInit(RGBLED_NUM, (stm32_gpio_t *)(port & 0xFFF0), 1 << (port & 0xF), &p);
  46. ledDriverInit(RGBLED_NUM, GPIOA, 0b00000010, &p);
  47. }
  48. void ledDriverInit(int leds, stm32_gpio_t *port, uint32_t mask, uint8_t **o_fb) {
  49. sLeds=leds;
  50. sPort=port;
  51. sMask=mask;
  52. palSetGroupMode(port, sMask, 0, PAL_MODE_OUTPUT_PUSHPULL|PAL_STM32_OSPEED_HIGHEST|PAL_STM32_PUPDR_FLOATING);
  53. // maybe don't do whole port?
  54. // palSetPadMode(port, 8, PAL_MODE_OUTPUT_PUSHPULL|PAL_STM32_OSPEED_HIGHEST|PAL_STM32_PUPDR_FLOATING);
  55. // configure pwm timers -
  56. // timer 2 as master, active for data transmission and inactive to disable transmission during reset period (50uS)
  57. // timer 3 as slave, during active time creates a 1.25 uS signal, with duty cycle controlled by frame buffer values
  58. static PWMConfig pwmc2 = {72000000 / 90, /* 800Khz PWM clock frequency. 1/90 of PWMC3 */
  59. (72000000 / 90) * 0.05, /*Total period is 50ms (20FPS), including sLeds cycles + reset length for ws2812b and FB writes */
  60. NULL,
  61. { {PWM_OUTPUT_ACTIVE_HIGH, NULL},
  62. {PWM_OUTPUT_DISABLED, NULL},
  63. {PWM_OUTPUT_DISABLED, NULL},
  64. {PWM_OUTPUT_DISABLED, NULL}},
  65. TIM_CR2_MMS_2, /* master mode selection */
  66. 0, };
  67. /* master mode selection */
  68. static PWMConfig pwmc3 = {72000000,/* 72Mhz PWM clock frequency. */
  69. 90, /* 90 cycles period (1.25 uS per period @72Mhz */
  70. NULL,
  71. { {PWM_OUTPUT_ACTIVE_HIGH, NULL},
  72. {PWM_OUTPUT_ACTIVE_HIGH, NULL},
  73. {PWM_OUTPUT_ACTIVE_HIGH, NULL},
  74. {PWM_OUTPUT_ACTIVE_HIGH, NULL}},
  75. 0,
  76. 0,
  77. };
  78. dma_source = chHeapAlloc(NULL, 1);
  79. fb = chHeapAlloc(NULL, ((sLeds) * 24)+10);
  80. *o_fb=fb;
  81. int j;
  82. for (j = 0; j < (sLeds) * 24; j++) fb[j] = 0;
  83. dma_source[0] = sMask;
  84. // DMA stream 2, triggered by channel3 pwm signal. if FB indicates, reset output value early to indicate "0" bit to ws2812
  85. dmaStreamAllocate(STM32_DMA1_STREAM2, 10, NULL, NULL);
  86. dmaStreamSetPeripheral(STM32_DMA1_STREAM2, &(sPort->BSRR.H.clear));
  87. dmaStreamSetMemory0(STM32_DMA1_STREAM2, fb);
  88. dmaStreamSetTransactionSize(STM32_DMA1_STREAM2, (sLeds) * 24);
  89. dmaStreamSetMode(
  90. STM32_DMA1_STREAM2,
  91. STM32_DMA_CR_DIR_M2P | STM32_DMA_CR_MINC | STM32_DMA_CR_PSIZE_BYTE
  92. | STM32_DMA_CR_MSIZE_BYTE | STM32_DMA_CR_CIRC | STM32_DMA_CR_PL(2));
  93. // DMA stream 3, triggered by pwm update event. output high at beginning of signal
  94. dmaStreamAllocate(STM32_DMA1_STREAM3, 10, NULL, NULL);
  95. dmaStreamSetPeripheral(STM32_DMA1_STREAM3, &(sPort->BSRR.H.set));
  96. dmaStreamSetMemory0(STM32_DMA1_STREAM3, dma_source);
  97. dmaStreamSetTransactionSize(STM32_DMA1_STREAM3, 1);
  98. dmaStreamSetMode(
  99. STM32_DMA1_STREAM3, STM32_DMA_CR_TEIE |
  100. STM32_DMA_CR_DIR_M2P | STM32_DMA_CR_PSIZE_BYTE | STM32_DMA_CR_MSIZE_BYTE
  101. | STM32_DMA_CR_CIRC | STM32_DMA_CR_PL(3));
  102. // DMA stream 6, triggered by channel1 update event. reset output value late to indicate "1" bit to ws2812.
  103. // always triggers but no affect if dma stream 2 already change output value to 0
  104. dmaStreamAllocate(STM32_DMA1_STREAM6, 10, NULL, NULL);
  105. dmaStreamSetPeripheral(STM32_DMA1_STREAM6, &(sPort->BSRR.H.clear));
  106. dmaStreamSetMemory0(STM32_DMA1_STREAM6, dma_source);
  107. dmaStreamSetTransactionSize(STM32_DMA1_STREAM6, 1);
  108. dmaStreamSetMode(
  109. STM32_DMA1_STREAM6,
  110. STM32_DMA_CR_DIR_M2P | STM32_DMA_CR_PSIZE_BYTE | STM32_DMA_CR_MSIZE_BYTE
  111. | STM32_DMA_CR_CIRC | STM32_DMA_CR_PL(3));
  112. pwmStart(&PWMD2, &pwmc2);
  113. pwmStart(&PWMD3, &pwmc3);
  114. // set pwm3 as slave, triggerd by pwm2 oc1 event. disables pwmd2 for synchronization.
  115. PWMD3.tim->SMCR |= TIM_SMCR_SMS_0 | TIM_SMCR_SMS_2 | TIM_SMCR_TS_0;
  116. PWMD2.tim->CR1 &= ~TIM_CR1_CEN;
  117. // set pwm values.
  118. // 28 (duty in ticks) / 90 (period in ticks) * 1.25uS (period in S) = 0.39 uS
  119. pwmEnableChannel(&PWMD3, 2, 28);
  120. // 58 (duty in ticks) / 90 (period in ticks) * 1.25uS (period in S) = 0.806 uS
  121. pwmEnableChannel(&PWMD3, 0, 58);
  122. // active during transfer of 90 cycles * sLeds * 24 bytes * 1/90 multiplier
  123. pwmEnableChannel(&PWMD2, 0, 90 * sLeds * 24 / 90);
  124. // stop and reset counters for synchronization
  125. PWMD2.tim->CNT = 0;
  126. // Slave (TIM3) needs to "update" immediately after master (TIM2) start in order to start in sync.
  127. // this initial sync is crucial for the stability of the run
  128. PWMD3.tim->CNT = 89;
  129. PWMD3.tim->DIER |= TIM_DIER_CC3DE | TIM_DIER_CC1DE | TIM_DIER_UDE;
  130. dmaStreamEnable(STM32_DMA1_STREAM3);
  131. dmaStreamEnable(STM32_DMA1_STREAM6);
  132. dmaStreamEnable(STM32_DMA1_STREAM2);
  133. // all systems go! both timers and all channels are configured to resonate
  134. // in complete sync without any need for CPU cycles (only DMA and timers)
  135. // start pwm2 for system to start resonating
  136. PWMD2.tim->CR1 |= TIM_CR1_CEN;
  137. }
  138. void ledDriverWaitCycle(void){
  139. while (PWMD2.tim->CNT < 90 * sLeds * 24 / 90){chThdSleepMicroseconds(1);};
  140. }
  141. void testPatternFB(uint8_t *fb){
  142. int i;
  143. Color tmpC = {rand()%256, rand()%256, rand()%256};
  144. for (i=0;i<sLeds;i++){
  145. setColorRGB(tmpC,fb+24*i, sMask);
  146. }
  147. }
  148. void ws2812_setleds(LED_TYPE *ledarray, uint16_t number_of_leds) {
  149. // uint8_t i = 0;
  150. // while (i < number_of_leds) {
  151. // ws2812_write_led(i, ledarray[i].r, ledarray[i].g, ledarray[i].b);
  152. // i++;
  153. // }
  154. uint8_t i = 0;
  155. while (i < number_of_leds) {
  156. setColor(ledarray[i].g, (fb+24*i), sMask);
  157. setColor(ledarray[i].r, (fb+24*i)+8, sMask);
  158. setColor(ledarray[i].b, (fb+24*i)+16, sMask);
  159. i++;
  160. }
  161. }
  162. void ws2812_setleds_rgbw(LED_TYPE *ledarray, uint16_t number_of_leds) {
  163. }
  164. void WS2812_send_color( uint8_t index ) {
  165. setColor(led_array[index].g, (fb+24*index), sMask);
  166. setColor(led_array[index].r, (fb+24*index)+8, sMask);
  167. setColor(led_array[index].b, (fb+24*index)+16, sMask);
  168. }
  169. void WS2812_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) {
  170. led_array[index].r = red;
  171. led_array[index].g = green;
  172. led_array[index].b = blue;
  173. }
  174. void WS2812_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) {
  175. for (int i = 0; i < RGBLED_NUM; i++) {
  176. WS2812_set_color( i, red, green, blue );
  177. }
  178. }
  179. void WS2812_send_colors(void) {
  180. for (int i = 0; i < RGBLED_NUM; i++) {
  181. WS2812_send_color( i );
  182. }
  183. }