ws2812.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. WS2812B CPU and memory efficient library
  3. Date: 28.9.2016
  4. Author: Martin Hubacek
  5. http://www.martinhubacek.cz
  6. @hubmartin
  7. Licence: MIT License
  8. */
  9. #include <string.h>
  10. #include "stm32f3xx_hal.h"
  11. #include "ws2812.h"
  12. extern WS2812_Struct ws2812b;
  13. // Define source arrays for my DMAs
  14. uint32_t WS2812_IO_High[] = { WS2812B_PINS };
  15. uint32_t WS2812_IO_Low[] = {WS2812B_PINS << 16};
  16. // WS2812 framebuffer - buffer for 2 LEDs - two times 24 bits
  17. uint16_t ws2812bDmaBitBuffer[24 * 2];
  18. // Gamma correction table
  19. const uint8_t gammaTable[] = {
  20. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  21. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
  22. 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
  23. 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
  24. 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
  25. 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
  26. 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
  27. 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
  28. 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
  29. 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
  30. 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
  31. 90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
  32. 115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
  33. 144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
  34. 177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
  35. 215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 };
  36. static void ws2812b_gpio_init(void)
  37. {
  38. // WS2812B outputs
  39. WS2812B_GPIO_CLK_ENABLE();
  40. GPIO_InitTypeDef GPIO_InitStruct;
  41. GPIO_InitStruct.Pin = WS2812B_PINS;
  42. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  43. GPIO_InitStruct.Pull = GPIO_NOPULL;
  44. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  45. HAL_GPIO_Init(WS2812B_PORT, &GPIO_InitStruct);
  46. // Enable output pins for debuging to see DMA Full and Half transfer interrupts
  47. #if defined(LED4_PORT) && defined(LED5_PORT)
  48. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  49. GPIO_InitStruct.Pull = GPIO_NOPULL;
  50. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  51. GPIO_InitStruct.Pin = LED4_PIN;
  52. HAL_GPIO_Init(LED4_PORT, &GPIO_InitStruct);
  53. GPIO_InitStruct.Pin = LED5_PIN;
  54. HAL_GPIO_Init(LED5_PORT, &GPIO_InitStruct);
  55. #endif
  56. }
  57. TIM_HandleTypeDef Tim2Handle;
  58. TIM_OC_InitTypeDef tim2OC1;
  59. TIM_OC_InitTypeDef tim2OC2;
  60. uint32_t tim_period;
  61. static void TIM2_init(void)
  62. {
  63. // TIM2 Periph clock enable
  64. __HAL_RCC_TIM2_CLK_ENABLE();
  65. // This computation of pulse length should work ok,
  66. // at some slower core speeds it needs some tuning.
  67. tim_period = SystemCoreClock / 800000; // 0,125us period (10 times lower the 1,25us period to have fixed math below)
  68. uint32_t cc1 = (10 * tim_period) / 36;
  69. uint32_t cc2 = (10 * tim_period) / 15;
  70. Tim2Handle.Instance = TIM2;
  71. Tim2Handle.Init.Period = tim_period;
  72. Tim2Handle.Init.RepetitionCounter = 0;
  73. Tim2Handle.Init.Prescaler = 0;
  74. Tim2Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  75. Tim2Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
  76. HAL_TIM_PWM_Init(&Tim2Handle);
  77. HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);
  78. HAL_NVIC_EnableIRQ(TIM2_IRQn);
  79. tim2OC1.OCMode = TIM_OCMODE_PWM1;
  80. tim2OC1.OCPolarity = TIM_OCPOLARITY_HIGH;
  81. tim2OC1.Pulse = cc1;
  82. tim2OC1.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  83. tim2OC1.OCFastMode = TIM_OCFAST_DISABLE;
  84. HAL_TIM_PWM_ConfigChannel(&Tim2Handle, &tim2OC1, TIM_CHANNEL_1);
  85. tim2OC2.OCMode = TIM_OCMODE_PWM1;
  86. tim2OC2.OCPolarity = TIM_OCPOLARITY_HIGH;
  87. tim2OC2.Pulse = cc2;
  88. tim2OC2.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  89. tim2OC2.OCFastMode = TIM_OCFAST_DISABLE;
  90. tim2OC2.OCIdleState = TIM_OCIDLESTATE_RESET;
  91. tim2OC2.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  92. HAL_TIM_PWM_ConfigChannel(&Tim2Handle, &tim2OC2, TIM_CHANNEL_2);
  93. HAL_TIM_Base_Start(&Tim2Handle);
  94. HAL_TIM_PWM_Start(&Tim2Handle, TIM_CHANNEL_1);
  95. }
  96. DMA_HandleTypeDef dmaUpdate;
  97. DMA_HandleTypeDef dmaCC1;
  98. DMA_HandleTypeDef dmaCC2;
  99. #define BUFFER_SIZE (sizeof(ws2812bDmaBitBuffer)/sizeof(uint16_t))
  100. static void DMA_init(void)
  101. {
  102. // TIM2 Update event
  103. __HAL_RCC_DMA1_CLK_ENABLE();
  104. dmaUpdate.Init.Direction = DMA_MEMORY_TO_PERIPH;
  105. dmaUpdate.Init.PeriphInc = DMA_PINC_DISABLE;
  106. dmaUpdate.Init.MemInc = DMA_MINC_DISABLE;
  107. dmaUpdate.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
  108. dmaUpdate.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
  109. dmaUpdate.Init.Mode = DMA_CIRCULAR;
  110. dmaUpdate.Init.Priority = DMA_PRIORITY_VERY_HIGH;
  111. dmaUpdate.Instance = DMA1_Channel2;
  112. //dmaUpdate.XferCpltCallback = TransferComplete;
  113. //dmaUpdate.XferErrorCallback = TransferError;
  114. HAL_DMA_Init(&dmaUpdate);
  115. //HAL_NVIC_SetPriority(DMA1_Channel2_IRQn, 0, 0);
  116. //HAL_NVIC_EnableIRQ(DMA1_Channel2_IRQn);
  117. HAL_DMA_Start(&dmaUpdate, (uint32_t)WS2812_IO_High, (uint32_t)&WS2812B_PORT->BSRR, BUFFER_SIZE);
  118. // TIM2 CC1 event
  119. dmaCC1.Init.Direction = DMA_MEMORY_TO_PERIPH;
  120. dmaCC1.Init.PeriphInc = DMA_PINC_DISABLE;
  121. dmaCC1.Init.MemInc = DMA_MINC_ENABLE;
  122. dmaCC1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
  123. dmaCC1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
  124. dmaCC1.Init.Mode = DMA_CIRCULAR;
  125. dmaCC1.Init.Priority = DMA_PRIORITY_VERY_HIGH;
  126. dmaCC1.Instance = DMA1_Channel5;
  127. //dmaUpdate.XferCpltCallback = TransferComplete;
  128. //dmaUpdate.XferErrorCallback = TransferError;
  129. //dmaUpdate.XferHalfCpltCallback = TransferHalf;
  130. //HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0);
  131. //HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn);
  132. HAL_DMA_Init(&dmaCC1);
  133. HAL_DMA_Start(&dmaCC1, (uint32_t)ws2812bDmaBitBuffer, (uint32_t)&WS2812B_PORT->BRR, BUFFER_SIZE);
  134. // TIM2 CC2 event
  135. dmaCC2.Init.Direction = DMA_MEMORY_TO_PERIPH;
  136. dmaCC2.Init.PeriphInc = DMA_PINC_DISABLE;
  137. dmaCC2.Init.MemInc = DMA_MINC_DISABLE;
  138. dmaCC2.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
  139. dmaCC2.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
  140. dmaCC2.Init.Mode = DMA_CIRCULAR;
  141. dmaCC2.Init.Priority = DMA_PRIORITY_VERY_HIGH;
  142. dmaCC2.Instance = DMA1_Channel7;
  143. dmaCC2.XferCpltCallback = DMA_TransferCompleteHandler;
  144. dmaCC2.XferHalfCpltCallback = DMA_TransferHalfHandler;
  145. //dmaUpdate.XferErrorCallback = TransferError;
  146. HAL_DMA_Init(&dmaCC2);
  147. HAL_NVIC_SetPriority(DMA1_Channel7_IRQn, 0, 0);
  148. HAL_NVIC_EnableIRQ(DMA1_Channel7_IRQn);
  149. HAL_DMA_Start_IT(&dmaCC2, (uint32_t)WS2812_IO_Low, (uint32_t)&WS2812B_PORT->BSRR, BUFFER_SIZE);
  150. }
  151. /*
  152. void DMA1_Channel2_IRQHandler(void)
  153. {
  154. // Check the interrupt and clear flag
  155. HAL_DMA_IRQHandler(&dmaUpdate);
  156. }
  157. void DMA1_Channel5_IRQHandler(void)
  158. {
  159. // Check the interrupt and clear flag
  160. HAL_DMA_IRQHandler(&dmaCC1);
  161. }*/
  162. void DMA1_Channel7_IRQHandler(void)
  163. {
  164. // Check the interrupt and clear flag
  165. HAL_DMA_IRQHandler(&dmaCC2);
  166. }
  167. static void loadNextFramebufferData(WS2812_BufferItem *bItem, uint32_t row)
  168. {
  169. uint32_t r = bItem->frameBufferPointer[bItem->frameBufferCounter++];
  170. uint32_t g = bItem->frameBufferPointer[bItem->frameBufferCounter++];
  171. uint32_t b = bItem->frameBufferPointer[bItem->frameBufferCounter++];
  172. if(bItem->frameBufferCounter == bItem->frameBufferSize)
  173. bItem->frameBufferCounter = 0;
  174. ws2812b_set_pixel(bItem->channel, row, r, g, b);
  175. }
  176. // Transmit the framebuffer
  177. static void WS2812_sendbuf()
  178. {
  179. // transmission complete flag
  180. ws2812b.transferComplete = 0;
  181. uint32_t i;
  182. for( i = 0; i < WS2812_BUFFER_COUNT; i++ )
  183. {
  184. ws2812b.item[i].frameBufferCounter = 0;
  185. loadNextFramebufferData(&ws2812b.item[i], 0); // ROW 0
  186. loadNextFramebufferData(&ws2812b.item[i], 1); // ROW 0
  187. }
  188. // clear all DMA flags
  189. __HAL_DMA_CLEAR_FLAG(&dmaUpdate, DMA_FLAG_TC2 | DMA_FLAG_HT2 | DMA_FLAG_TE2);
  190. __HAL_DMA_CLEAR_FLAG(&dmaCC1, DMA_FLAG_TC5 | DMA_FLAG_HT5 | DMA_FLAG_TE5);
  191. __HAL_DMA_CLEAR_FLAG(&dmaCC2, DMA_FLAG_TC7 | DMA_FLAG_HT7 | DMA_FLAG_TE7);
  192. // configure the number of bytes to be transferred by the DMA controller
  193. dmaUpdate.Instance->CNDTR = BUFFER_SIZE;
  194. dmaCC1.Instance->CNDTR = BUFFER_SIZE;
  195. dmaCC2.Instance->CNDTR = BUFFER_SIZE;
  196. // clear all TIM2 flags
  197. __HAL_TIM_CLEAR_FLAG(&Tim2Handle, TIM_FLAG_UPDATE | TIM_FLAG_CC1 | TIM_FLAG_CC2 | TIM_FLAG_CC3 | TIM_FLAG_CC4);
  198. // enable DMA channels
  199. __HAL_DMA_ENABLE(&dmaUpdate);
  200. __HAL_DMA_ENABLE(&dmaCC1);
  201. __HAL_DMA_ENABLE(&dmaCC2);
  202. // IMPORTANT: enable the TIM2 DMA requests AFTER enabling the DMA channels!
  203. __HAL_TIM_ENABLE_DMA(&Tim2Handle, TIM_DMA_UPDATE);
  204. __HAL_TIM_ENABLE_DMA(&Tim2Handle, TIM_DMA_CC1);
  205. __HAL_TIM_ENABLE_DMA(&Tim2Handle, TIM_DMA_CC2);
  206. TIM2->CNT = tim_period-1;
  207. // start TIM2
  208. __HAL_TIM_ENABLE(&Tim2Handle);
  209. }
  210. void DMA_TransferHalfHandler(DMA_HandleTypeDef *DmaHandle)
  211. {
  212. #if defined(LED4_PORT)
  213. LED4_PORT->BSRR = LED4_PIN;
  214. #endif
  215. // Is this the last LED?
  216. if(ws2812b.repeatCounter != (WS2812B_NUMBER_OF_LEDS / 2 - 1))
  217. {
  218. uint32_t i;
  219. for( i = 0; i < WS2812_BUFFER_COUNT; i++ )
  220. {
  221. loadNextFramebufferData(&ws2812b.item[i], 0);
  222. }
  223. } else {
  224. // If this is the last pixel, set the next pixel value to zeros, because
  225. // the DMA would not stop exactly at the last bit.
  226. ws2812b_set_pixel(0, 0, 0, 0, 0);
  227. }
  228. #if defined(LED4_PORT)
  229. LED4_PORT->BRR = LED4_PIN;
  230. #endif
  231. }
  232. void DMA_TransferCompleteHandler(DMA_HandleTypeDef *DmaHandle)
  233. {
  234. #if defined(LED5_PORT)
  235. LED5_PORT->BSRR = LED5_PIN;
  236. #endif
  237. ws2812b.repeatCounter++;
  238. if(ws2812b.repeatCounter == WS2812B_NUMBER_OF_LEDS / 2)
  239. {
  240. // Transfer of all LEDs is done, disable DMA but enable tiemr update IRQ to stop the 50us pulse
  241. ws2812b.repeatCounter = 0;
  242. // Enable TIM2 Update interrupt for 50us Treset signal
  243. __HAL_TIM_ENABLE_IT(&Tim2Handle, TIM_IT_UPDATE);
  244. // Disable DMA
  245. __HAL_DMA_DISABLE(&dmaUpdate);
  246. __HAL_DMA_DISABLE(&dmaCC1);
  247. __HAL_DMA_DISABLE(&dmaCC2);
  248. // Disable the DMA requests
  249. __HAL_TIM_DISABLE_DMA(&Tim2Handle, TIM_DMA_UPDATE);
  250. __HAL_TIM_DISABLE_DMA(&Tim2Handle, TIM_DMA_CC1);
  251. __HAL_TIM_DISABLE_DMA(&Tim2Handle, TIM_DMA_CC2);
  252. // Manually set outputs to low to generate 50us reset impulse
  253. WS2812B_PORT->BSRR = WS2812_IO_Low[0];
  254. } else {
  255. // Load bitbuffer with next RGB LED values
  256. uint32_t i;
  257. for( i = 0; i < WS2812_BUFFER_COUNT; i++ )
  258. {
  259. loadNextFramebufferData(&ws2812b.item[i], 1);
  260. }
  261. }
  262. #if defined(LED5_PORT)
  263. LED5_PORT->BRR = LED5_PIN;
  264. #endif
  265. }
  266. void TIM2_IRQHandler(void)
  267. {
  268. HAL_TIM_IRQHandler(&Tim2Handle);
  269. }
  270. // TIM2 Interrupt Handler gets executed on every TIM2 Update if enabled
  271. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  272. {
  273. // I have to wait 50us to generate Treset signal
  274. if (ws2812b.timerPeriodCounter < (uint8_t)WS2812_RESET_PERIOD)
  275. {
  276. // count the number of timer periods
  277. ws2812b.timerPeriodCounter++;
  278. }
  279. else
  280. {
  281. ws2812b.timerPeriodCounter = 0;
  282. __HAL_TIM_DISABLE(&Tim2Handle);
  283. TIM2->CR1 = 0; // disable timer
  284. // disable the TIM2 Update
  285. __HAL_TIM_DISABLE_IT(&Tim2Handle, TIM_IT_UPDATE);
  286. // set TransferComplete flag
  287. ws2812b.transferComplete = 1;
  288. }
  289. }
  290. static void ws2812b_set_pixel(uint8_t row, uint16_t column, uint8_t red, uint8_t green, uint8_t blue)
  291. {
  292. // Apply gamma
  293. red = gammaTable[red];
  294. green = gammaTable[green];
  295. blue = gammaTable[blue];
  296. uint32_t calcCol = (column*24);
  297. uint32_t invRed = ~red;
  298. uint32_t invGreen = ~green;
  299. uint32_t invBlue = ~blue;
  300. #if defined(SETPIX_1)
  301. uint8_t i;
  302. uint32_t calcClearRow = ~(0x01<<row);
  303. for (i = 0; i < 8; i++)
  304. {
  305. // clear the data for pixel
  306. ws2812bDmaBitBuffer[(calcCol+i)] &= calcClearRow;
  307. ws2812bDmaBitBuffer[(calcCol+8+i)] &= calcClearRow;
  308. ws2812bDmaBitBuffer[(calcCol+16+i)] &= calcClearRow;
  309. // write new data for pixel
  310. ws2812bDmaBitBuffer[(calcCol+i)] |= (((((invGreen)<<i) & 0x80)>>7)<<row);
  311. ws2812bDmaBitBuffer[(calcCol+8+i)] |= (((((invRed)<<i) & 0x80)>>7)<<row);
  312. ws2812bDmaBitBuffer[(calcCol+16+i)] |= (((((invBlue)<<i) & 0x80)>>7)<<row);
  313. }
  314. #elif defined(SETPIX_2)
  315. uint8_t i;
  316. for (i = 0; i < 8; i++)
  317. {
  318. // Set or clear the data for the pixel
  319. if(((invGreen)<<i) & 0x80)
  320. varSetBit(ws2812bDmaBitBuffer[(calcCol+i)], row);
  321. else
  322. varResetBit(ws2812bDmaBitBuffer[(calcCol+i)], row);
  323. if(((invRed)<<i) & 0x80)
  324. varSetBit(ws2812bDmaBitBuffer[(calcCol+8+i)], row);
  325. else
  326. varResetBit(ws2812bDmaBitBuffer[(calcCol+8+i)], row);
  327. if(((invBlue)<<i) & 0x80)
  328. varSetBit(ws2812bDmaBitBuffer[(calcCol+16+i)], row);
  329. else
  330. varResetBit(ws2812bDmaBitBuffer[(calcCol+16+i)], row);
  331. }
  332. #elif defined(SETPIX_3)
  333. ws2812bDmaBitBuffer[(calcCol+0)] |= (((((invGreen)<<0) & 0x80)>>7)<<row);
  334. ws2812bDmaBitBuffer[(calcCol+8+0)] |= (((((invRed)<<0) & 0x80)>>7)<<row);
  335. ws2812bDmaBitBuffer[(calcCol+16+0)] |= (((((invBlue)<<0) & 0x80)>>7)<<row);
  336. ws2812bDmaBitBuffer[(calcCol+1)] |= (((((invGreen)<<1) & 0x80)>>7)<<row);
  337. ws2812bDmaBitBuffer[(calcCol+8+1)] |= (((((invRed)<<1) & 0x80)>>7)<<row);
  338. ws2812bDmaBitBuffer[(calcCol+16+1)] |= (((((invBlue)<<1) & 0x80)>>7)<<row);
  339. ws2812bDmaBitBuffer[(calcCol+2)] |= (((((invGreen)<<2) & 0x80)>>7)<<row);
  340. ws2812bDmaBitBuffer[(calcCol+8+2)] |= (((((invRed)<<2) & 0x80)>>7)<<row);
  341. ws2812bDmaBitBuffer[(calcCol+16+2)] |= (((((invBlue)<<2) & 0x80)>>7)<<row);
  342. ws2812bDmaBitBuffer[(calcCol+3)] |= (((((invGreen)<<3) & 0x80)>>7)<<row);
  343. ws2812bDmaBitBuffer[(calcCol+8+3)] |= (((((invRed)<<3) & 0x80)>>7)<<row);
  344. ws2812bDmaBitBuffer[(calcCol+16+3)] |= (((((invBlue)<<3) & 0x80)>>7)<<row);
  345. ws2812bDmaBitBuffer[(calcCol+4)] |= (((((invGreen)<<4) & 0x80)>>7)<<row);
  346. ws2812bDmaBitBuffer[(calcCol+8+4)] |= (((((invRed)<<4) & 0x80)>>7)<<row);
  347. ws2812bDmaBitBuffer[(calcCol+16+4)] |= (((((invBlue)<<4) & 0x80)>>7)<<row);
  348. ws2812bDmaBitBuffer[(calcCol+5)] |= (((((invGreen)<<5) & 0x80)>>7)<<row);
  349. ws2812bDmaBitBuffer[(calcCol+8+5)] |= (((((invRed)<<5) & 0x80)>>7)<<row);
  350. ws2812bDmaBitBuffer[(calcCol+16+5)] |= (((((invBlue)<<5) & 0x80)>>7)<<row);
  351. ws2812bDmaBitBuffer[(calcCol+6)] |= (((((invGreen)<<6) & 0x80)>>7)<<row);
  352. ws2812bDmaBitBuffer[(calcCol+8+6)] |= (((((invRed)<<6) & 0x80)>>7)<<row);
  353. ws2812bDmaBitBuffer[(calcCol+16+6)] |= (((((invBlue)<<6) & 0x80)>>7)<<row);
  354. ws2812bDmaBitBuffer[(calcCol+7)] |= (((((invGreen)<<7) & 0x80)>>7)<<row);
  355. ws2812bDmaBitBuffer[(calcCol+8+7)] |= (((((invRed)<<7) & 0x80)>>7)<<row);
  356. ws2812bDmaBitBuffer[(calcCol+16+7)] |= (((((invBlue)<<7) & 0x80)>>7)<<row);
  357. #elif defined(SETPIX_4)
  358. // Bitband optimizations with pure increments, 5us interrupts
  359. uint32_t *bitBand = BITBAND_SRAM(&ws2812bDmaBitBuffer[(calcCol)], row);
  360. *bitBand = (invGreen >> 7);
  361. bitBand+=16;
  362. *bitBand = (invGreen >> 6);
  363. bitBand+=16;
  364. *bitBand = (invGreen >> 5);
  365. bitBand+=16;
  366. *bitBand = (invGreen >> 4);
  367. bitBand+=16;
  368. *bitBand = (invGreen >> 3);
  369. bitBand+=16;
  370. *bitBand = (invGreen >> 2);
  371. bitBand+=16;
  372. *bitBand = (invGreen >> 1);
  373. bitBand+=16;
  374. *bitBand = (invGreen >> 0);
  375. bitBand+=16;
  376. // RED
  377. *bitBand = (invRed >> 7);
  378. bitBand+=16;
  379. *bitBand = (invRed >> 6);
  380. bitBand+=16;
  381. *bitBand = (invRed >> 5);
  382. bitBand+=16;
  383. *bitBand = (invRed >> 4);
  384. bitBand+=16;
  385. *bitBand = (invRed >> 3);
  386. bitBand+=16;
  387. *bitBand = (invRed >> 2);
  388. bitBand+=16;
  389. *bitBand = (invRed >> 1);
  390. bitBand+=16;
  391. *bitBand = (invRed >> 0);
  392. bitBand+=16;
  393. // BLUE
  394. *bitBand = (invBlue >> 7);
  395. bitBand+=16;
  396. *bitBand = (invBlue >> 6);
  397. bitBand+=16;
  398. *bitBand = (invBlue >> 5);
  399. bitBand+=16;
  400. *bitBand = (invBlue >> 4);
  401. bitBand+=16;
  402. *bitBand = (invBlue >> 3);
  403. bitBand+=16;
  404. *bitBand = (invBlue >> 2);
  405. bitBand+=16;
  406. *bitBand = (invBlue >> 1);
  407. bitBand+=16;
  408. *bitBand = (invBlue >> 0);
  409. bitBand+=16;
  410. #endif
  411. }
  412. void ws2812b_init()
  413. {
  414. ws2812b_gpio_init();
  415. DMA_init();
  416. TIM2_init();
  417. // Need to start the first transfer
  418. ws2812b.transferComplete = 1;
  419. }
  420. void ws2812b_handle()
  421. {
  422. if(ws2812b.startTransfer) {
  423. ws2812b.startTransfer = 0;
  424. WS2812_sendbuf();
  425. }
  426. }