Advertisement
makkarpov

Untitled

Jul 3rd, 2015
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #else // Arduino Due
  2.  
  3.   #define SCALE      VARIANT_MCK / 2UL / 1000000UL
  4.   #define INST       (2UL * F_CPU / VARIANT_MCK)
  5.   #define TIME_800_0 ((int)(0.40 * SCALE + 0.5) - (5 * INST))
  6.   #define TIME_800_1 ((int)(0.80 * SCALE + 0.5) - (5 * INST))
  7.   #define PERIOD_800 ((int)(1.25 * SCALE + 0.5) - (5 * INST))
  8.   #define TIME_400_0 ((int)(0.50 * SCALE + 0.5) - (5 * INST))
  9.   #define TIME_400_1 ((int)(1.20 * SCALE + 0.5) - (5 * INST))
  10.   #define PERIOD_400 ((int)(2.50 * SCALE + 0.5) - (5 * INST))
  11.  
  12.   int             pinMask, time0, time1, period, t;
  13.   Pio            *port;
  14.   volatile WoReg *portSet, *portClear, *timeValue, *timeReset;
  15.   uint8_t        *p, *end, pix, mask;
  16.  
  17.   pmc_set_writeprotect(false);
  18.   pmc_enable_periph_clk((uint32_t)TC3_IRQn);
  19.   TC_Configure(TC1, 0,
  20.     TC_CMR_WAVE | TC_CMR_WAVSEL_UP | TC_CMR_TCCLKS_TIMER_CLOCK1);
  21.   TC_Start(TC1, 0);
  22.  
  23.   pinMask   = g_APinDescription[pin].ulPin; // Don't 'optimize' these into
  24.   port      = g_APinDescription[pin].pPort; // declarations above.  Want to
  25.   portSet   = &(port->PIO_SODR);            // burn a few cycles after
  26.   portClear = &(port->PIO_CODR);            // starting timer to minimize
  27.   timeValue = &(TC1->TC_CHANNEL[0].TC_CV);  // the initial 'while'.
  28.   timeReset = &(TC1->TC_CHANNEL[0].TC_CCR);
  29.   p         =  pixels;
  30.   end       =  p + numBytes;
  31.   pix       = *p++;
  32.   mask      = 0x80;
  33.  
  34. #ifdef NEO_KHZ400
  35.   if((type & NEO_SPDMASK) == NEO_KHZ800) { // 800 KHz bitstream
  36. #endif
  37.     time0 = TIME_800_0;
  38.     time1 = TIME_800_1;
  39.     period = PERIOD_800;
  40. #ifdef NEO_KHZ400
  41.   } else { // 400 KHz bitstream
  42.     time0 = TIME_400_0;
  43.     time1 = TIME_400_1;
  44.     period = PERIOD_400;
  45.   }
  46. #endif
  47.  
  48.   for(t = time0;; t = time0) {
  49.     if(pix & mask) t = time1;
  50.     while(*timeValue < period);
  51.     *portSet   = pinMask;
  52.     *timeReset = TC_CCR_CLKEN | TC_CCR_SWTRG;
  53.     while(*timeValue < t);
  54.     *portClear = pinMask;
  55.     if(!(mask >>= 1)) {   // This 'inside-out' loop logic utilizes
  56.       if(p >= end) break; // idle time to minimize inter-byte delays.
  57.       pix = *p++;
  58.       mask = 0x80;
  59.     }
  60.   }
  61.   while(*timeValue < period); // Wait for last bit
  62.   TC_Stop(TC1, 0);
  63.  
  64. #endif // end Arduino Due
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement