Forked PololuLedStrip and modified it to work with the KL25Z. Renamed it to Adafruit_NeoPixel.

Fork of PololuLedStrip by David Grayson

Committer:
DavidEGrayson
Date:
Fri Mar 01 04:15:35 2013 +0000
Revision:
10:f1bb84b97788
Parent:
9:6ffb85d69eaf
Child:
12:b6df8ac053c8
Saved one cycle per bit by shifting the delays to the left in C.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DavidEGrayson 1:102307d9b701 1 #include "PololuLedStrip.h"
DavidEGrayson 1:102307d9b701 2
DavidEGrayson 1:102307d9b701 3 bool PololuLedStrip::interruptFriendly = false;
DavidEGrayson 1:102307d9b701 4
DavidEGrayson 7:9a088f042ee0 5 uint8_t led_strip_write_delays[3];
DavidEGrayson 4:d3b60bd43811 6
DavidEGrayson 7:9a088f042ee0 7 void PololuLedStrip::calculateDelays()
DavidEGrayson 7:9a088f042ee0 8 {
DavidEGrayson 8:1578776ceac5 9 // Get the clock frequency in MHz.
DavidEGrayson 8:1578776ceac5 10 int f_mhz = SystemCoreClock / 1000000;
DavidEGrayson 8:1578776ceac5 11
DavidEGrayson 8:1578776ceac5 12 // Arrange for a 700 nanosecond delay between the rise time and the fall time for a 0 bit.
DavidEGrayson 10:f1bb84b97788 13 led_strip_write_delays[0] = 700*f_mhz/1000 - 32;
DavidEGrayson 8:1578776ceac5 14
DavidEGrayson 8:1578776ceac5 15 // Arrange for a 600 nanosecond delay between the fall time for a 0 bit and the fall time for a 1 bit.
DavidEGrayson 8:1578776ceac5 16 // This means the pulses representing a 1 will be 700+600 = 1300 nanoseconds.
DavidEGrayson 8:1578776ceac5 17 led_strip_write_delays[1] = 600*f_mhz/1000 - 19;
DavidEGrayson 8:1578776ceac5 18
DavidEGrayson 8:1578776ceac5 19 // Arrange for a 1200 nanosecond delay between the fall time for a 1 bit and rise time of the next bit.
DavidEGrayson 8:1578776ceac5 20 // This means the period of the signal will be 2500 nanoseconds.
DavidEGrayson 8:1578776ceac5 21 led_strip_write_delays[2] = 1200*f_mhz/1000 - 24;
DavidEGrayson 10:f1bb84b97788 22
DavidEGrayson 10:f1bb84b97788 23 led_strip_write_delays[0] <<= 1;
DavidEGrayson 10:f1bb84b97788 24 led_strip_write_delays[1] <<= 1;
DavidEGrayson 10:f1bb84b97788 25 led_strip_write_delays[2] <<= 1;
DavidEGrayson 7:9a088f042ee0 26 }
DavidEGrayson 6:9d0530b7dae2 27
DavidEGrayson 1:102307d9b701 28 PololuLedStrip::PololuLedStrip(PinName pinName)
DavidEGrayson 1:102307d9b701 29 {
DavidEGrayson 1:102307d9b701 30 gpio_init(&gpio, pinName, PIN_OUTPUT);
DavidEGrayson 1:102307d9b701 31 }
DavidEGrayson 1:102307d9b701 32
DavidEGrayson 1:102307d9b701 33 void PololuLedStrip::write(rgb_color * colors, unsigned int count)
DavidEGrayson 1:102307d9b701 34 {
DavidEGrayson 8:1578776ceac5 35 calculateDelays();
DavidEGrayson 8:1578776ceac5 36
DavidEGrayson 1:102307d9b701 37 __disable_irq(); // Disable interrupts temporarily because we don't want our pulse timing to be messed up.
DavidEGrayson 7:9a088f042ee0 38
DavidEGrayson 1:102307d9b701 39 while(count--)
DavidEGrayson 1:102307d9b701 40 {
DavidEGrayson 9:6ffb85d69eaf 41 led_strip_write_color(colors, gpio.reg_set, gpio.reg_clr, gpio.mask);
DavidEGrayson 9:6ffb85d69eaf 42 colors++;
DavidEGrayson 9:6ffb85d69eaf 43
DavidEGrayson 1:102307d9b701 44 if (interruptFriendly)
DavidEGrayson 1:102307d9b701 45 {
DavidEGrayson 1:102307d9b701 46 __enable_irq();
DavidEGrayson 1:102307d9b701 47 __nop();
DavidEGrayson 1:102307d9b701 48 __nop();
DavidEGrayson 1:102307d9b701 49 __nop();
DavidEGrayson 1:102307d9b701 50 __disable_irq();
DavidEGrayson 1:102307d9b701 51 }
DavidEGrayson 1:102307d9b701 52 }
DavidEGrayson 1:102307d9b701 53
DavidEGrayson 1:102307d9b701 54 __enable_irq(); // Re-enable interrupts now that we are done.
DavidEGrayson 1:102307d9b701 55 wait_us(24); // Hold the line low for 24 microseconds to send the reset signal.
DavidEGrayson 9:6ffb85d69eaf 56
DavidEGrayson 9:6ffb85d69eaf 57 //*(gpio.reg_set) = gpio.mask;
DavidEGrayson 9:6ffb85d69eaf 58 //*(gpio.reg_clr) = gpio.mask;
DavidEGrayson 9:6ffb85d69eaf 59
DavidEGrayson 1:102307d9b701 60 }