LED Strip with K64F

Fork of PololuLedStrip by David Grayson

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PololuLedStrip.cpp Source File

PololuLedStrip.cpp

00001 #include "PololuLedStrip.h"
00002 
00003 bool PololuLedStrip::interruptFriendly = false;
00004 
00005 // The two timed delays, in units of half-cycles.
00006 uint8_t led_strip_write_delays[2];
00007 
00008 void PololuLedStrip::calculateDelays()
00009 {
00010     int f_mhz = SystemCoreClock / 1000000;   // Clock frequency in MHz.
00011 
00012     if (f_mhz <= 48)
00013     {
00014         // The delays below result in 360/1120 ns pulses and a 1880 ns period on the mbed NXP LPC11U24.        
00015         led_strip_write_delays[0] = 0;
00016         led_strip_write_delays[1] = 0;
00017     }
00018     else
00019     {
00020         // Try to generally compute what the delays should be for a ide range of clock frequencies.
00021         
00022         // The fudge factors below were experimentally chosen so that we would have
00023         // ~100/840 ns pulses and a ~1430 ns period on the mbed NXP LPC1768 (96 MHz Cortex-M3).
00024         // There seem to be some ~100 ns inconsistencies in the timing depending on which example program is
00025         // running; the most likely explanation is some kind of flash caching that affects the timing.
00026         // If you ever change these numbers, it is important to check the the subtractions below
00027         // will not overflow in the worst case (smallest possible f_mhz).
00028         led_strip_write_delays[0] = 750*f_mhz/1000 - 33;
00029         led_strip_write_delays[1] = 550*f_mhz/1000 - 20;    
00030     }
00031  
00032     // Convert from units of cycles to units of half-cycles; it makes the assembly faster.   
00033     for(int i = 0; i < 2; i++)
00034     {
00035         led_strip_write_delays[i] <<= 1;
00036     }
00037 }
00038 
00039 PololuLedStrip::PololuLedStrip(PinName pinName)
00040 {
00041     gpio_init_out(&gpio, pinName);
00042 }
00043 
00044 void PololuLedStrip::write(rgb_color * colors, unsigned int count)
00045 {
00046     calculateDelays();
00047 
00048     __disable_irq();   // Disable interrupts temporarily because we don't want our pulse timing to be messed up.
00049 
00050     while(count--)
00051     {
00052         led_strip_write_color(colors, gpio.reg_set, gpio.reg_clr, gpio.mask);
00053         colors++;
00054          
00055         if (interruptFriendly)
00056         {
00057             __enable_irq();
00058             __nop();
00059             __nop();
00060             __nop();
00061             __disable_irq();
00062         }
00063     }
00064         
00065     __enable_irq();   // Re-enable interrupts now that we are done.
00066     wait_us(24);      // Hold the line low for 24 microseconds to send the reset signal.
00067     
00068     //*(gpio.reg_set) = gpio.mask;
00069     //*(gpio.reg_clr) = gpio.mask;
00070 
00071 }