@@ -269,6 +269,20 @@ int LedDeviceWS2812b::write(const std::vector<ColorRgb> &ledValues)
269
269
unsigned int colorBits = 0 ; // Holds the GRB color before conversion to wire bit pattern
270
270
unsigned int wireBit = 0 ; // Holds the current bit we will set in PWMWaveform
271
271
272
+ // Copy PWM waveform to DMA's data buffer
273
+ // printf("Copying %d words to DMA data buffer\n", NUM_DATA_WORDS);
274
+ struct control_data_s *ctl = (struct control_data_s *)virtbase;
275
+ dma_cb_t *cbp = ctl->cb ;
276
+
277
+ // 72 bits per pixel / 32 bits per word = 2.25 words per pixel
278
+ // Add 1 to make sure the PWM FIFO gets the message: "we're sending zeroes"
279
+ // Times 4 because DMA works in bytes, not words
280
+ cbp->length = ((mLedCount * 2.25 ) + 1 ) * 4 ;
281
+ if (cbp->length > NUM_DATA_WORDS * 4 ) {
282
+ cbp->length = NUM_DATA_WORDS * 4 ;
283
+ mLedCount = (NUM_DATA_WORDS - 1 ) / 2.25 ;
284
+ }
285
+
272
286
for (size_t i=0 ; i<mLedCount ; i++) {
273
287
// Create bits necessary to represent one color triplet (in GRB, not RGB, order)
274
288
// printf("RGB: %d, %d, %d\n", ledValues[i].red, ledValues[i].green, ledValues[i].blue);
@@ -301,19 +315,6 @@ int LedDeviceWS2812b::write(const std::vector<ColorRgb> &ledValues)
301
315
}
302
316
}
303
317
304
- // Copy PWM waveform to DMA's data buffer
305
- // printf("Copying %d words to DMA data buffer\n", NUM_DATA_WORDS);
306
- struct control_data_s *ctl = (struct control_data_s *)virtbase;
307
- dma_cb_t *cbp = ctl->cb ;
308
-
309
- // 72 bits per pixel / 32 bits per word = 2.25 words per pixel
310
- // Add 1 to make sure the PWM FIFO gets the message: "we're sending zeroes"
311
- // Times 4 because DMA works in bytes, not words
312
- cbp->length = ((mLedCount * 2.25 ) + 1 ) * 4 ;
313
- if (cbp->length > NUM_DATA_WORDS * 4 ) {
314
- cbp->length = NUM_DATA_WORDS * 4 ;
315
- }
316
-
317
318
// This block is a major CPU hog when there are lots of pixels to be transmitted.
318
319
// It would go quicker with DMA.
319
320
// for(unsigned int i = 0; i < (cbp->length / 4); i++) {
0 commit comments