Author Topic: WSD2811 led time code in C, optimize further  (Read 1086 times)

0 Members and 1 Guest are viewing this topic.

Offline Vindhyachal.taknikiTopic starter

  • Frequent Contributor
  • **
  • Posts: 490
WSD2811 led time code in C, optimize further
« on: May 27, 2019, 02:04:17 pm »
1. I am using WSD2811 to run led in series. I am using C-code for programming. My below code is working by carefully adjusting some of NOP instructions.

2. 100 leds are working fine on WSD2811, my code is in C, so no issues. Dont want to write in assembly.

3. Below is my code, in which I check each bit of red/green/blue by if/else, then make pin high/low

4. Want to know if there any other way I can optimize code in c language or better way to do that in C-language.

5. Please dont suggest to write in assembly or if code is already working why to change.
 
I want to write in C only, & check if there any way I can further optimize code in C language.

Again by current code, my leds are working fine


Code: [Select]
static void ws2811_rgb_write(uint8_t red, uint8_t green , uint8_t blue)  
{
//R1
    if(red & 0x80U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    } 
///   
   
//R2
    if(red & 0x40U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
/// 

//R3
    if(red & 0x20U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///     
   
//R4
    if(red & 0x10U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
/// 

//R5
    if(red & 0x08U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///   

//R6
    if(red & 0x04U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }   
///     

//R7
    if(red & 0x02U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///   

//R8
    if(red & 0x01U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///   


   
   
//R1
    if(green & 0x80U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    } 
///   
   
//R2
    if(green & 0x40U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
/// 

//R3
    if(green & 0x20U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }   
///     
   
//R4
    if(green & 0x10U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }       
/// 

//R5
    if(green & 0x08U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///   

//R6
    if(green & 0x04U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///     

//R7
    if(green & 0x02U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///   

//R8
    if(green & 0x01U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///       

   
   
   
//R1
    if(blue & 0x80U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    } 
///   
   
//R2
    if(blue & 0x40U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
/// 

//R3
    if(blue & 0x20U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///     
   
//R4
    if(blue & 0x10U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
/// 

//R5
    if(blue & 0x08U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///   

//R6
    if(blue & 0x04U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///     

//R7
    if(blue & 0x02U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///   

//R8
    if(blue & 0x01U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///       
   
     
 
}
 

Offline garethw

  • Regular Contributor
  • *
  • Posts: 88
  • Country: gb
Re: WSD2811 led time code in C, optimize further
« Reply #1 on: May 27, 2019, 02:10:56 pm »
I believe that apart from purchasing a more efficient compiler the only alternative is what you’ve told us not to tell you. ;)


Sent from my iPhone using Tapatalk
Father
Husband
MENG Electronic Engineering student
 

Offline cgroen

  • Supporter
  • ****
  • Posts: 642
  • Country: dk
    • Carstens personal web
Re: WSD2811 led time code in C, optimize further
« Reply #2 on: May 27, 2019, 02:14:34 pm »
Get a CPU that will allow you to handle some of that timing direct in hardware....
I use a LPC1549 that uses one of its SCT modules to handle this. I do all in C and have no problem with timing ;)
 

Offline garethw

  • Regular Contributor
  • *
  • Posts: 88
  • Country: gb
Re: WSD2811 led time code in C, optimize further
« Reply #3 on: May 27, 2019, 02:18:55 pm »
What is it that isn’t working? You said it works with 100 leds. Why try to optimise if it already works.
I’m not the best person to comment as I really like assembly and only use C for quick jobs if I’m using an unfamiliar mcu.


Sent from my iPhone using Tapatalk
Father
Husband
MENG Electronic Engineering student
 

Online Kjelt

  • Super Contributor
  • ***
  • Posts: 6586
  • Country: nl
Re: WSD2811 led time code in C, optimize further
« Reply #4 on: May 27, 2019, 03:49:41 pm »
Easy optimization is a for loop per led  color databyte where you just shift left and use the same bitmask 0x80.
Code looks cleaner that way and others can easier understand the code IMO.

BTW working with NOPs for bitbanging is a last resort if you really need the extreme speed.
It will only work on that uC , others it might fail due to timing issues. Cleaner to use a timer peripheral for the exact timing, but as said this might be impossible in some extreme cases.
 

Offline Nerull

  • Frequent Contributor
  • **
  • Posts: 694
Re: WSD2811 led time code in C, optimize further
« Reply #5 on: May 27, 2019, 04:06:44 pm »
WS2811s require really tight timing, depending on the MCU and clock speed used there may not be time to use timers. For example, to send a '0' you must toggle the IO with less than 500ns between the rising and falling edges, which is a single instruction on a 8MHz PIC.
 

Offline zzattack

  • Regular Contributor
  • *
  • Posts: 136
  • Country: nl
Re: WSD2811 led time code in C, optimize further
« Reply #6 on: May 27, 2019, 05:52:40 pm »
Easiest way to drive these LEDs without having to worry about timing too much is with an SPI peripheral, even better if you have DMA too. I've driven a chain of over 2000 leds using a PIC18 but getting timing right with C code seems a dumb effort.
 

Offline Vindhyachal.taknikiTopic starter

  • Frequent Contributor
  • **
  • Posts: 490
Re: WSD2811 led time code in C, optimize further
« Reply #7 on: June 06, 2019, 12:00:04 pm »
thanks for inputs. current code is working fine for me at the moment
 

Online ejeffrey

  • Super Contributor
  • ***
  • Posts: 4034
  • Country: us
Re: WSD2811 led time code in C, optimize further
« Reply #8 on: June 06, 2019, 04:23:42 pm »
4. Want to know if there any other way I can optimize code in c language or better way to do that in C-language.

Basically at this level optimizations are extremely specific to the platform, compiler, compiler settings, and so on.  People might be able to give you some ideas, but it isn't usually practical to say X will be faster since we can't really test it on your system.  Typically you would also look at the generated assembler to see what the compiler is doing.  You haven't even specified your architecture, compiler, or what you have already tried, so we don't really know what is going on.

That said, some things I would try:

Rewrite LED_PIN_HIGH() and LED_PIN_LOW() into a macro LED_SET_PIN(int val) that can set the pin by writing the value directly to an IO port rather than requiring a conditional comparison and jump followed by the write.  That can save you 1-2 instructions.

Pack rgb into 24 bits of a 32 bit integer and use a loop with bit shifting and then always look at the high or low order bit.  On some architectures this will shove the bit that shifts off the end of the register into an overflow/carry flag which can directly be used by a conditional without extra logical operations.  Maybe your C compiler is smart enough to figure out what you are doing and maybe not.

Other options: use a faster CPU, or one that supports DMA.  DMA will get you two advantages: first, the bits can be shifted out as fast as the hardware allows, and second you can be doing your computations to figure out what bits to toggle in parallel with the DMA transfer.  With a faster CPU you have to be careful: if you change architectures, you may get a faster core with a slower GPIO peripheral.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf