PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

531 line
17KB

  1. /* Copyright (c) 2009 by Alex Leone <acleone ~AT~ gmail.com>
  2. This file is part of the Arduino TLC5940 Library.
  3. The Arduino TLC5940 Library is free software: you can redistribute it
  4. and/or modify it under the terms of the GNU General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. The Arduino TLC5940 Library is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with The Arduino TLC5940 Library. If not, see
  13. <http://www.gnu.org/licenses/>. */
  14. /** \file
  15. Tlc5940 class functions. */
  16. #include "Tlc5940.h"
  17. #include "pinouts/pin_functions.h"
  18. /** This will be true (!= 0) if update was just called and the data has not
  19. been latched in yet. */
  20. volatile uint8_t tlc_needXLAT;
  21. /** Some of the extened library will need to be called after a successful
  22. update. */
  23. volatile void (*tlc_onUpdateFinished)(void);
  24. /** Packed grayscale data, 24 bytes (16 * 12 bits) per TLC.
  25. Format: Lets assume we have 2 TLCs, A and B, daisy-chained with the SOUT of
  26. A going into the SIN of B.
  27. - byte 0: upper 8 bits of B.15
  28. - byte 1: lower 4 bits of B.15 and upper 4 bits of B.14
  29. - byte 2: lower 8 bits of B.0
  30. - ...
  31. - byte 24: upper 8 bits of A.15
  32. - byte 25: lower 4 bits of A.15 and upper 4 bits of A.14
  33. - ...
  34. - byte 47: lower 8 bits of A.0
  35. \note Normally packing data like this is bad practice. But in this
  36. situation, shifting the data out is really fast because the format of
  37. the array is the same as the format of the TLC's serial interface. */
  38. uint8_t tlc_GSData[NUM_TLCS * 24];
  39. /** Don't add an extra SCLK pulse after switching from dot-correction mode. */
  40. static uint8_t firstGSInput;
  41. /** Interrupt called after an XLAT pulse to prevent more XLAT pulses. */
  42. static inline void Tlc5940_interrupt(void)
  43. {
  44. disable_XLAT_pulses();
  45. clear_XLAT_interrupt();
  46. tlc_needXLAT = 0;
  47. if (tlc_onUpdateFinished) {
  48. sei();
  49. tlc_onUpdateFinished();
  50. }
  51. }
  52. #if defined(__AVR__)
  53. ISR(TIMER1_OVF_vect)
  54. {
  55. Tlc5940_interrupt();
  56. }
  57. #elif defined(__arm__) && defined(TEENSYDUINO)
  58. void ftm1_isr(void)
  59. {
  60. uint32_t sc = FTM1_SC;
  61. if (sc & 0x80) FTM1_SC = sc & 0x7F;
  62. Tlc5940_interrupt();
  63. }
  64. #endif
  65. /** \defgroup ReqVPRG_ENABLED Functions that Require VPRG_ENABLED
  66. Functions that require VPRG_ENABLED == 1.
  67. You can enable VPRG by changing
  68. \code #define VPRG_ENABLED 0 \endcode to
  69. \code #define VPRG_ENABLED 1 \endcode in tlc_config.h
  70. You will also have to connect Arduino digital pin 6 to TLC pin 27. (The
  71. pin to be used can be changed in tlc_config.h). If VPRG is not enabled,
  72. the TLC pin should grounded (remember to unconnect TLC pin 27 from GND
  73. if you do enable VPRG). */
  74. /* @{ */ /* @} */
  75. /** \defgroup CoreFunctions Core Libary Functions
  76. These function are all prefixed with "Tlc." */
  77. /* @{ */
  78. /** Pin i/o and Timer setup. The grayscale register will be reset to all
  79. zeros, or whatever initialValue is set to and the Timers will start.
  80. \param initialValue = 0, optional parameter specifing the inital startup
  81. value */
  82. void Tlc5940::init(uint16_t initialValue)
  83. {
  84. /* Pin Setup */
  85. output_pin(XLAT_DDR, XLAT_PIN);
  86. output_pin(BLANK_DDR, BLANK_PIN);
  87. output_pin(GSCLK_DDR, GSCLK_PIN);
  88. #if VPRG_ENABLED
  89. output_pin(VPRG_DDR, VPRG_PIN);
  90. clear_pin(VPRG_PORT, VPRG_PIN); // grayscale mode (VPRG low)
  91. #endif
  92. #if XERR_ENABLED
  93. pullup_pin(XERR_DDR, XERR_PORT, XERR_PIN); // XERR as input, enable pull-up resistor
  94. #endif
  95. set_pin(BLANK_PORT, BLANK_PIN); // leave blank high (until the timers start)
  96. tlc_shift8_init();
  97. setAll(initialValue);
  98. update();
  99. disable_XLAT_pulses();
  100. clear_XLAT_interrupt();
  101. tlc_needXLAT = 0;
  102. pulse_pin(XLAT_PORT, XLAT_PIN);
  103. /* Timer Setup */
  104. #if defined(__AVR__)
  105. /* Timer 1 - BLANK / XLAT */
  106. TCCR1A = _BV(COM1B1); // non inverting, output on OC1B, BLANK
  107. TCCR1B = _BV(WGM13); // Phase/freq correct PWM, ICR1 top
  108. OCR1A = 1; // duty factor on OC1A, XLAT is inside BLANK
  109. OCR1B = 2; // duty factor on BLANK (larger than OCR1A (XLAT))
  110. ICR1 = TLC_PWM_PERIOD; // see tlc_config.h
  111. /* Timer 2 - GSCLK */
  112. #if defined(TLC_ATMEGA_8_H)
  113. TCCR2 = _BV(COM20) // set on BOTTOM, clear on OCR2A (non-inverting),
  114. | _BV(WGM21); // output on OC2B, CTC mode with OCR2 top
  115. OCR2 = TLC_GSCLK_PERIOD / 2; // see tlc_config.h
  116. TCCR2 |= _BV(CS20); // no prescale, (start pwm output)
  117. #elif defined(TLC_TIMER3_GSCLK)
  118. TCCR3A = _BV(COM3A1) // set on BOTTOM, clear on OCR3A (non-inverting),
  119. // output on OC3A
  120. | _BV(WGM31); // Fast pwm with ICR3 top
  121. OCR3A = 0; // duty factor (as short a pulse as possible)
  122. ICR3 = TLC_GSCLK_PERIOD; // see tlc_config.h
  123. TCCR3B = _BV(CS30) // no prescale, (start pwm output)
  124. | _BV(WGM32) // Fast pwm with ICR3 top
  125. | _BV(WGM33); // Fast pwm with ICR3 top
  126. #else
  127. TCCR2A = _BV(COM2B1) // set on BOTTOM, clear on OCR2A (non-inverting),
  128. // output on OC2B
  129. | _BV(WGM21) // Fast pwm with OCR2A top
  130. | _BV(WGM20); // Fast pwm with OCR2A top
  131. TCCR2B = _BV(WGM22); // Fast pwm with OCR2A top
  132. OCR2B = 0; // duty factor (as short a pulse as possible)
  133. OCR2A = TLC_GSCLK_PERIOD; // see tlc_config.h
  134. TCCR2B |= _BV(CS20); // no prescale, (start pwm output)
  135. #endif
  136. TCCR1B |= _BV(CS10); // no prescale, (start pwm output)
  137. #elif defined(__arm__) && defined(TEENSYDUINO)
  138. clear_pin(XLAT_DDR, XLAT_PIN);
  139. SIM_SCGC4 |= SIM_SCGC4_CMT;
  140. CMT_MSC = 0;
  141. CMT_PPS = 0;
  142. CMT_CGH1 = TLC_TIMER_TEENSY3_NORMAL_CGH1;
  143. CMT_CGL1 = TLC_TIMER_TEENSY3_NORMAL_CGL1;
  144. CMT_CMD1 = 1;
  145. CMT_CMD2 = 0;
  146. CMT_CMD3 = 0;
  147. CMT_CMD4 = 0;
  148. CMT_OC = 0x60;
  149. CMT_MSC = 0x01;
  150. CORE_PIN5_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_DSE|PORT_PCR_SRE;
  151. FTM1_SC = 0;
  152. FTM1_MOD = TLC_TIMER_TEENSY3_NORMAL_MOD;
  153. FTM1_CNT = 0;
  154. FTM1_C0SC = 0x24;
  155. FTM1_C1SC = 0x24;
  156. FTM1_C0V = TLC_TIMER_TEENSY3_NORMAL_MOD - TLC_TIMER_TEENSY3_NORMAL_CV;
  157. FTM1_C1V = TLC_TIMER_TEENSY3_NORMAL_MOD - TLC_TIMER_TEENSY3_NORMAL_CV - 1;
  158. FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_CPWMS;
  159. NVIC_ENABLE_IRQ(IRQ_FTM1);
  160. CORE_PIN4_CONFIG = PORT_PCR_MUX(3)|PORT_PCR_DSE|PORT_PCR_SRE;
  161. #endif
  162. update();
  163. }
  164. /** Clears the grayscale data array, #tlc_GSData, but does not shift in any
  165. data. This call should be followed by update() if you are turning off
  166. all the outputs. */
  167. void Tlc5940::clear(void)
  168. {
  169. setAll(0);
  170. }
  171. /** Shifts in the data from the grayscale data array, #tlc_GSData.
  172. If data has already been shifted in this grayscale cycle, another call to
  173. update() will immediately return 1 without shifting in the new data. To
  174. ensure that a call to update() does shift in new data, use
  175. \code while(Tlc.update()); \endcode
  176. or
  177. \code while(tlc_needXLAT); \endcode
  178. \returns 1 if there is data waiting to be latched, 0 if data was
  179. successfully shifted in */
  180. uint8_t Tlc5940::update(void)
  181. {
  182. if (tlc_needXLAT) {
  183. return 1;
  184. }
  185. disable_XLAT_pulses();
  186. if (firstGSInput) {
  187. // adds an extra SCLK pulse unless we've just set dot-correction data
  188. firstGSInput = 0;
  189. } else {
  190. pulse_pin(SCLK_PORT, SCLK_PIN);
  191. }
  192. uint8_t *p = tlc_GSData;
  193. while (p < tlc_GSData + NUM_TLCS * 24) {
  194. tlc_shift8(*p++);
  195. tlc_shift8(*p++);
  196. tlc_shift8(*p++);
  197. }
  198. tlc_needXLAT = 1;
  199. enable_XLAT_pulses();
  200. set_XLAT_interrupt();
  201. return 0;
  202. }
  203. /** Sets channel to value in the grayscale data array, #tlc_GSData.
  204. \param channel (0 to #NUM_TLCS * 16 - 1). OUT0 of the first TLC is
  205. channel 0, OUT0 of the next TLC is channel 16, etc.
  206. \param value (0-4095). The grayscale value, 4095 is maximum.
  207. \see get */
  208. void Tlc5940::set(TLC_CHANNEL_TYPE channel, uint16_t value)
  209. {
  210. TLC_CHANNEL_TYPE index8 = (NUM_TLCS * 16 - 1) - channel;
  211. uint8_t *index12p = tlc_GSData + ((((uint16_t)index8) * 3) >> 1);
  212. if (index8 & 1) { // starts in the middle
  213. // first 4 bits intact | 4 top bits of value
  214. *index12p = (*index12p & 0xF0) | (value >> 8);
  215. // 8 lower bits of value
  216. *(++index12p) = value & 0xFF;
  217. } else { // starts clean
  218. // 8 upper bits of value
  219. *(index12p++) = value >> 4;
  220. // 4 lower bits of value | last 4 bits intact
  221. *index12p = ((uint8_t)(value << 4)) | (*index12p & 0xF);
  222. }
  223. }
  224. /** Gets the current grayscale value for a channel
  225. \param channel (0 to #NUM_TLCS * 16 - 1). OUT0 of the first TLC is
  226. channel 0, OUT0 of the next TLC is channel 16, etc.
  227. \returns current grayscale value (0 - 4095) for channel
  228. \see set */
  229. uint16_t Tlc5940::get(TLC_CHANNEL_TYPE channel)
  230. {
  231. TLC_CHANNEL_TYPE index8 = (NUM_TLCS * 16 - 1) - channel;
  232. uint8_t *index12p = tlc_GSData + ((((uint16_t)index8) * 3) >> 1);
  233. return (index8 & 1)? // starts in the middle
  234. (((uint16_t)(*index12p & 15)) << 8) | // upper 4 bits
  235. *(index12p + 1) // lower 8 bits
  236. : // starts clean
  237. (((uint16_t)(*index12p)) << 4) | // upper 8 bits
  238. ((*(index12p + 1) & 0xF0) >> 4); // lower 4 bits
  239. // that's probably the ugliest ternary operator I've ever created.
  240. }
  241. /** Sets all channels to value.
  242. \param value grayscale value (0 - 4095) */
  243. void Tlc5940::setAll(uint16_t value)
  244. {
  245. uint8_t firstByte = value >> 4;
  246. uint8_t secondByte = (value << 4) | (value >> 8);
  247. uint8_t *p = tlc_GSData;
  248. while (p < tlc_GSData + NUM_TLCS * 24) {
  249. *p++ = firstByte;
  250. *p++ = secondByte;
  251. *p++ = (uint8_t)value;
  252. }
  253. }
  254. #if VPRG_ENABLED
  255. /** \addtogroup ReqVPRG_ENABLED
  256. From the \ref CoreFunctions "Core Functions":
  257. - \link Tlc5940::setAllDC Tlc.setAllDC(uint8_t value(0-63)) \endlink - sets
  258. all the dot correction data to value */
  259. /* @{ */
  260. /** Sets the dot correction for all channels to value. The dot correction
  261. value correspondes to maximum output current by
  262. \f$\displaystyle I_{OUT_n} = I_{max} \times \frac{DCn}{63} \f$
  263. where
  264. - \f$\displaystyle I_{max} = \frac{1.24V}{R_{IREF}} \times 31.5 =
  265. \frac{39.06}{R_{IREF}} \f$
  266. - DCn is the dot correction value for channel n
  267. \param value (0-63) */
  268. void Tlc5940::setAllDC(uint8_t value)
  269. {
  270. tlc_dcModeStart();
  271. uint8_t firstByte = value << 2 | value >> 4;
  272. uint8_t secondByte = value << 4 | value >> 2;
  273. uint8_t thirdByte = value << 6 | value;
  274. for (TLC_CHANNEL_TYPE i = 0; i < NUM_TLCS * 12; i += 3) {
  275. tlc_shift8(firstByte);
  276. tlc_shift8(secondByte);
  277. tlc_shift8(thirdByte);
  278. }
  279. pulse_pin(XLAT_PORT, XLAT_PIN);
  280. tlc_dcModeStop();
  281. }
  282. /* @} */
  283. #endif
  284. #if XERR_ENABLED
  285. /** Checks for shorted/broken LEDs reported by any of the TLCs.
  286. \returns 1 if a TLC is reporting an error, 0 otherwise. */
  287. uint8_t Tlc5940::readXERR(void)
  288. {
  289. return ((XERR_PINS & _BV(XERR_PIN)) == 0);
  290. }
  291. #endif
  292. /* @} */
  293. #if DATA_TRANSFER_MODE == TLC_BITBANG
  294. /** Sets all the bit-bang pins to output */
  295. void tlc_shift8_init(void)
  296. {
  297. output_pin(SIN_DDR, SIN_PIN); // SIN as output
  298. output_pin(SCLK_DDR, SCLK_PIN); // SCLK as output
  299. clear_pin(SCLK_PORT, SCLK_PIN);
  300. }
  301. /** Shifts a byte out, MSB first */
  302. void tlc_shift8(uint8_t byte)
  303. {
  304. for (uint8_t bit = 0x80; bit; bit >>= 1) {
  305. if (bit & byte) {
  306. set_pin(SIN_PORT, SIN_PIN);
  307. } else {
  308. clear_pin(SIN_PORT, SIN_PIN);
  309. }
  310. pulse_pin(SCLK_PORT, SCLK_PIN);
  311. }
  312. }
  313. #elif DATA_TRANSFER_MODE == TLC_SPI
  314. /** Initializes the SPI module to double speed (f_osc / 2) */
  315. void tlc_shift8_init(void)
  316. {
  317. output_pin(SIN_DDR, SIN_PIN); // SPI MOSI as output
  318. output_pin(SCLK_DDR, SCLK_PIN); // SPI SCK as output
  319. output_pin(TLC_SS_DDR, TLC_SS_PIN); // SPI SS as output
  320. clear_pin(SCLK_PORT, SCLK_PIN);
  321. SPSR = _BV(SPI2X); // double speed (f_osc / 2)
  322. SPCR = _BV(SPE) // enable SPI
  323. | _BV(MSTR); // master mode
  324. }
  325. /** Shifts out a byte, MSB first */
  326. void tlc_shift8(uint8_t byte)
  327. {
  328. SPDR = byte; // starts transmission
  329. while (!(SPSR & _BV(SPIF)))
  330. ; // wait for transmission complete
  331. }
  332. #endif
  333. #if VPRG_ENABLED
  334. /** Switches to dot correction mode and clears any waiting grayscale latches.*/
  335. void tlc_dcModeStart(void)
  336. {
  337. disable_XLAT_pulses(); // ensure that no latches happen
  338. clear_XLAT_interrupt(); // (in case this was called right after update)
  339. tlc_needXLAT = 0;
  340. set_pin(VPRG_PORT, VPRG_PIN); // dot correction mode
  341. }
  342. /** Switches back to grayscale mode. */
  343. void tlc_dcModeStop(void)
  344. {
  345. clear_pin(VPRG_PORT, VPRG_PIN); // back to grayscale mode
  346. firstGSInput = 1;
  347. }
  348. #endif
  349. /** Preinstantiated Tlc variable. */
  350. Tlc5940 Tlc;
  351. /** \defgroup ExtendedFunctions Extended Library Functions
  352. These functions require an include statement at the top of the sketch. */
  353. /* @{ */ /* @} */
  354. /** \mainpage
  355. The <a href="http://www.ti.com/lit/gpn/TLC5940">Texas Instruments TLC5940
  356. </a> is a 16-channel, constant-current sink LED driver. Each channel has
  357. an individually adjustable 4096-step grayscale PWM brightness control and
  358. a 64-step, constant-current sink (no LED resistors needed!). This chip
  359. is a current sink, so be sure to use common anode RGB LEDs.
  360. Check the <a href="http://code.google.com/p/tlc5940arduino/">tlc5940arduino
  361. project</a> on Google Code for updates. To install, unzip the "Tlc5940"
  362. folder to &lt;Arduino Folder&gt;/hardware/libraries/
  363. &nbsp;
  364. \section hardwaresetup Hardware Setup
  365. The basic hardware setup is explained at the top of the Examples. A good
  366. place to start would be the BasicUse Example. (The examples are in
  367. File->Sketchbook->Examples->Library-Tlc5940).
  368. All the options for the library are located in tlc_config.h, including
  369. #NUM_TLCS, what pins to use, and the PWM period. After changing
  370. tlc_config.h, be sure to delete the Tlc5940.o file in the library folder
  371. to save the changes.
  372. &nbsp;
  373. \section libref Library Reference
  374. \ref CoreFunctions "Core Functions" (see the BasicUse Example and Tlc5940):
  375. - \link Tlc5940::init Tlc.init(int initialValue (0-4095))\endlink - Call this is
  376. to setup the timers before using any other Tlc functions.
  377. initialValue defaults to zero (all channels off).
  378. - \link Tlc5940::clear Tlc.clear()\endlink - Turns off all channels
  379. (Needs Tlc.update())
  380. - \link Tlc5940::set Tlc.set(uint8_t channel (0-(NUM_TLCS * 16 - 1)),
  381. int value (0-4095))\endlink - sets the grayscale data for channel.
  382. (Needs Tlc.update())
  383. - \link Tlc5940::setAll Tlc.setAll(int value(0-4095))\endlink - sets all
  384. channels to value. (Needs Tlc.update())
  385. - \link Tlc5940::get uint16_t Tlc.get(uint8_t channel)\endlink - returns
  386. the grayscale data for channel (see set).
  387. - \link Tlc5940::update Tlc.update()\endlink - Sends the changes from any
  388. Tlc.clear's, Tlc.set's, or Tlc.setAll's.
  389. \ref ExtendedFunctions "Extended Functions". These require an include
  390. statement at the top of the sketch to use.
  391. \ref ReqVPRG_ENABLED "Functions that require VPRG_ENABLED". These
  392. require VPRG_ENABLED == 1 in tlc_config.h
  393. &nbsp;
  394. \section expansion Expanding the Library
  395. Lets say we wanted to add a function like "tlc_goCrazy(...)". The first
  396. thing to do is to create a source file in the library folder,
  397. "tlc_my_crazy_functions.h".
  398. A template for this .h file is
  399. \code
  400. // Explination for my crazy function extension
  401. #ifndef TLC_MY_CRAZY_FUNCTIONS_H
  402. #define TLC_MY_CRAZY_FUNCTIONS_H
  403. #include "tlc_config.h"
  404. #include "Tlc5940.h"
  405. void tlc_goCrazy(void);
  406. void tlc_goCrazy(void)
  407. {
  408. uint16_t crazyFactor = 4000;
  409. Tlc.clear();
  410. for (uint8_t channel = 4; channel < 9; channel++) {
  411. Tlc.set(channel, crazyFactor);
  412. }
  413. Tlc.update();
  414. }
  415. #endif
  416. * \endcode
  417. * Now to use your library in a sketch, just add
  418. * \code
  419. #include "tlc_my_crazy_functions.h"
  420. // ...
  421. tlc_goCrazy();
  422. \endcode
  423. If you would like to share your extended functions for others to use,
  424. email me (acleone ~AT~ gmail.com) with the file and an example and I'll
  425. include them in the library.
  426. &nbsp;
  427. \section bugs Contact
  428. If you found a bug in the library, email me so I can fix it!
  429. My email is acleone ~AT~ gmail.com
  430. &nbsp;
  431. \section license License - GPLv3
  432. Copyright (c) 2009 by Alex Leone <acleone ~AT~ gmail.com>
  433. This file is part of the Arduino TLC5940 Library.
  434. The Arduino TLC5940 Library is free software: you can redistribute it
  435. and/or modify it under the terms of the GNU General Public License as
  436. published by the Free Software Foundation, either version 3 of the
  437. License, or (at your option) any later version.
  438. The Arduino TLC5940 Library is distributed in the hope that it will be
  439. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  440. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  441. GNU General Public License for more details.
  442. You should have received a copy of the GNU General Public License
  443. along with The Arduino TLC5940 Library. If not, see
  444. <http://www.gnu.org/licenses/>. */