My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

Tone.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /* Tone.cpp
  2. A Tone Generator Library
  3. Written by Brett Hagman
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. Version Modified By Date Comments
  16. ------- ----------- -------- --------
  17. 0001 B Hagman 09/08/02 Initial coding
  18. 0002 B Hagman 09/08/18 Multiple pins
  19. 0003 B Hagman 09/08/18 Moved initialization from constructor to begin()
  20. 0004 B Hagman 09/09/26 Fixed problems with ATmega8
  21. 0005 B Hagman 09/11/23 Scanned prescalars for best fit on 8 bit timers
  22. 09/11/25 Changed pin toggle method to XOR
  23. 09/11/25 Fixed timer0 from being excluded
  24. 0006 D Mellis 09/12/29 Replaced objects with functions
  25. 0007 M Sproul 10/08/29 Changed #ifdefs from cpu to register
  26. *************************************************/
  27. #include <avr/interrupt.h>
  28. #include <avr/pgmspace.h>
  29. #include "wiring.h"
  30. #include "pins_arduino.h"
  31. #if defined(__AVR_ATmega8__) || defined(__AVR_ATmega128__)
  32. #define TCCR2A TCCR2
  33. #define TCCR2B TCCR2
  34. #define COM2A1 COM21
  35. #define COM2A0 COM20
  36. #define OCR2A OCR2
  37. #define TIMSK2 TIMSK
  38. #define OCIE2A OCIE2
  39. #define TIMER2_COMPA_vect TIMER2_COMP_vect
  40. #define TIMSK1 TIMSK
  41. #endif
  42. // timerx_toggle_count:
  43. // > 0 - duration specified
  44. // = 0 - stopped
  45. // < 0 - infinitely (until stop() method called, or new play() called)
  46. #if !defined(__AVR_ATmega8__)
  47. volatile long timer0_toggle_count;
  48. volatile uint8_t *timer0_pin_port;
  49. volatile uint8_t timer0_pin_mask;
  50. #endif
  51. volatile long timer1_toggle_count;
  52. volatile uint8_t *timer1_pin_port;
  53. volatile uint8_t timer1_pin_mask;
  54. volatile long timer2_toggle_count;
  55. volatile uint8_t *timer2_pin_port;
  56. volatile uint8_t timer2_pin_mask;
  57. #if defined(TIMSK3)
  58. volatile long timer3_toggle_count;
  59. volatile uint8_t *timer3_pin_port;
  60. volatile uint8_t timer3_pin_mask;
  61. #endif
  62. #if defined(TIMSK4)
  63. volatile long timer4_toggle_count;
  64. volatile uint8_t *timer4_pin_port;
  65. volatile uint8_t timer4_pin_mask;
  66. #endif
  67. #if defined(TIMSK5)
  68. volatile long timer5_toggle_count;
  69. volatile uint8_t *timer5_pin_port;
  70. volatile uint8_t timer5_pin_mask;
  71. #endif
  72. // MLS: This does not make sense, the 3 options are the same
  73. #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  74. #define AVAILABLE_TONE_PINS 1
  75. const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 3, 4, 5, 1, 0 */ };
  76. static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255, 255, 255, 255 */ };
  77. #elif defined(__AVR_ATmega8__)
  78. #define AVAILABLE_TONE_PINS 1
  79. const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1 */ };
  80. static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };
  81. #else
  82. #define AVAILABLE_TONE_PINS 1
  83. // Leave timer 0 to last.
  84. const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1, 0 */ };
  85. static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255 */ };
  86. #endif
  87. static int8_t toneBegin(uint8_t _pin)
  88. {
  89. int8_t _timer = -1;
  90. // if we're already using the pin, the timer should be configured.
  91. for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
  92. if (tone_pins[i] == _pin) {
  93. return pgm_read_byte(tone_pin_to_timer_PGM + i);
  94. }
  95. }
  96. // search for an unused timer.
  97. for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
  98. if (tone_pins[i] == 255) {
  99. tone_pins[i] = _pin;
  100. _timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
  101. break;
  102. }
  103. }
  104. if (_timer != -1)
  105. {
  106. // Set timer specific stuff
  107. // All timers in CTC mode
  108. // 8 bit timers will require changing prescalar values,
  109. // whereas 16 bit timers are set to either ck/1 or ck/64 prescalar
  110. switch (_timer)
  111. {
  112. #if defined(TCCR0A) && defined(TCCR0B)
  113. case 0:
  114. // 8 bit timer
  115. TCCR0A = 0;
  116. TCCR0B = 0;
  117. bitWrite(TCCR0A, WGM01, 1);
  118. bitWrite(TCCR0B, CS00, 1);
  119. timer0_pin_port = portOutputRegister(digitalPinToPort(_pin));
  120. timer0_pin_mask = digitalPinToBitMask(_pin);
  121. break;
  122. #endif
  123. #if defined(TCCR1A) && defined(TCCR1B) && defined(WGM12)
  124. case 1:
  125. // 16 bit timer
  126. TCCR1A = 0;
  127. TCCR1B = 0;
  128. bitWrite(TCCR1B, WGM12, 1);
  129. bitWrite(TCCR1B, CS10, 1);
  130. timer1_pin_port = portOutputRegister(digitalPinToPort(_pin));
  131. timer1_pin_mask = digitalPinToBitMask(_pin);
  132. break;
  133. #endif
  134. #if defined(TCCR2A) && defined(TCCR2B)
  135. case 2:
  136. // 8 bit timer
  137. TCCR2A = 0;
  138. TCCR2B = 0;
  139. bitWrite(TCCR2A, WGM21, 1);
  140. bitWrite(TCCR2B, CS20, 1);
  141. timer2_pin_port = portOutputRegister(digitalPinToPort(_pin));
  142. timer2_pin_mask = digitalPinToBitMask(_pin);
  143. break;
  144. #endif
  145. #if defined(TCCR3A) && defined(TCCR3B) && defined(TIMSK3)
  146. case 3:
  147. // 16 bit timer
  148. TCCR3A = 0;
  149. TCCR3B = 0;
  150. bitWrite(TCCR3B, WGM32, 1);
  151. bitWrite(TCCR3B, CS30, 1);
  152. timer3_pin_port = portOutputRegister(digitalPinToPort(_pin));
  153. timer3_pin_mask = digitalPinToBitMask(_pin);
  154. break;
  155. #endif
  156. #if defined(TCCR4A) && defined(TCCR4B) && defined(TIMSK4)
  157. case 4:
  158. // 16 bit timer
  159. TCCR4A = 0;
  160. TCCR4B = 0;
  161. #if defined(WGM42)
  162. bitWrite(TCCR4B, WGM42, 1);
  163. #elif defined(CS43)
  164. #warning this may not be correct
  165. // atmega32u4
  166. bitWrite(TCCR4B, CS43, 1);
  167. #endif
  168. bitWrite(TCCR4B, CS40, 1);
  169. timer4_pin_port = portOutputRegister(digitalPinToPort(_pin));
  170. timer4_pin_mask = digitalPinToBitMask(_pin);
  171. break;
  172. #endif
  173. #if defined(TCCR5A) && defined(TCCR5B) && defined(TIMSK5)
  174. case 5:
  175. // 16 bit timer
  176. TCCR5A = 0;
  177. TCCR5B = 0;
  178. bitWrite(TCCR5B, WGM52, 1);
  179. bitWrite(TCCR5B, CS50, 1);
  180. timer5_pin_port = portOutputRegister(digitalPinToPort(_pin));
  181. timer5_pin_mask = digitalPinToBitMask(_pin);
  182. break;
  183. #endif
  184. }
  185. }
  186. return _timer;
  187. }
  188. // frequency (in hertz) and duration (in milliseconds).
  189. void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
  190. {
  191. uint8_t prescalarbits = 0b001;
  192. long toggle_count = 0;
  193. uint32_t ocr = 0;
  194. int8_t _timer;
  195. _timer = toneBegin(_pin);
  196. if (_timer >= 0)
  197. {
  198. // Set the pinMode as OUTPUT
  199. pinMode(_pin, OUTPUT);
  200. // if we are using an 8 bit timer, scan through prescalars to find the best fit
  201. if (_timer == 0 || _timer == 2)
  202. {
  203. ocr = F_CPU / frequency / 2 - 1;
  204. prescalarbits = 0b001; // ck/1: same for both timers
  205. if (ocr > 255)
  206. {
  207. ocr = F_CPU / frequency / 2 / 8 - 1;
  208. prescalarbits = 0b010; // ck/8: same for both timers
  209. if (_timer == 2 && ocr > 255)
  210. {
  211. ocr = F_CPU / frequency / 2 / 32 - 1;
  212. prescalarbits = 0b011;
  213. }
  214. if (ocr > 255)
  215. {
  216. ocr = F_CPU / frequency / 2 / 64 - 1;
  217. prescalarbits = _timer == 0 ? 0b011 : 0b100;
  218. if (_timer == 2 && ocr > 255)
  219. {
  220. ocr = F_CPU / frequency / 2 / 128 - 1;
  221. prescalarbits = 0b101;
  222. }
  223. if (ocr > 255)
  224. {
  225. ocr = F_CPU / frequency / 2 / 256 - 1;
  226. prescalarbits = _timer == 0 ? 0b100 : 0b110;
  227. if (ocr > 255)
  228. {
  229. // can't do any better than /1024
  230. ocr = F_CPU / frequency / 2 / 1024 - 1;
  231. prescalarbits = _timer == 0 ? 0b101 : 0b111;
  232. }
  233. }
  234. }
  235. }
  236. #if defined(TCCR0B)
  237. if (_timer == 0)
  238. {
  239. TCCR0B = prescalarbits;
  240. }
  241. else
  242. #endif
  243. #if defined(TCCR2B)
  244. {
  245. TCCR2B = prescalarbits;
  246. }
  247. #else
  248. {
  249. // dummy place holder to make the above ifdefs work
  250. }
  251. #endif
  252. }
  253. else
  254. {
  255. // two choices for the 16 bit timers: ck/1 or ck/64
  256. ocr = F_CPU / frequency / 2 - 1;
  257. prescalarbits = 0b001;
  258. if (ocr > 0xffff)
  259. {
  260. ocr = F_CPU / frequency / 2 / 64 - 1;
  261. prescalarbits = 0b011;
  262. }
  263. if (_timer == 1)
  264. {
  265. #if defined(TCCR1B)
  266. TCCR1B = (TCCR1B & 0b11111000) | prescalarbits;
  267. #endif
  268. }
  269. #if defined(TCCR3B)
  270. else if (_timer == 3)
  271. TCCR3B = (TCCR3B & 0b11111000) | prescalarbits;
  272. #endif
  273. #if defined(TCCR4B)
  274. else if (_timer == 4)
  275. TCCR4B = (TCCR4B & 0b11111000) | prescalarbits;
  276. #endif
  277. #if defined(TCCR5B)
  278. else if (_timer == 5)
  279. TCCR5B = (TCCR5B & 0b11111000) | prescalarbits;
  280. #endif
  281. }
  282. // Calculate the toggle count
  283. if (duration > 0)
  284. {
  285. toggle_count = 2 * frequency * duration / 1000;
  286. }
  287. else
  288. {
  289. toggle_count = -1;
  290. }
  291. // Set the OCR for the given timer,
  292. // set the toggle count,
  293. // then turn on the interrupts
  294. switch (_timer)
  295. {
  296. #if defined(OCR0A) && defined(TIMSK0) && defined(OCIE0A)
  297. case 0:
  298. OCR0A = ocr;
  299. timer0_toggle_count = toggle_count;
  300. bitWrite(TIMSK0, OCIE0A, 1);
  301. break;
  302. #endif
  303. case 1:
  304. #if defined(OCR1A) && defined(TIMSK1) && defined(OCIE1A)
  305. OCR1A = ocr;
  306. timer1_toggle_count = toggle_count;
  307. bitWrite(TIMSK1, OCIE1A, 1);
  308. #elif defined(OCR1A) && defined(TIMSK) && defined(OCIE1A)
  309. // this combination is for at least the ATmega32
  310. OCR1A = ocr;
  311. timer1_toggle_count = toggle_count;
  312. bitWrite(TIMSK, OCIE1A, 1);
  313. #endif
  314. break;
  315. #if defined(OCR2A) && defined(TIMSK2) && defined(OCIE2A)
  316. case 2:
  317. OCR2A = ocr;
  318. timer2_toggle_count = toggle_count;
  319. bitWrite(TIMSK2, OCIE2A, 1);
  320. break;
  321. #endif
  322. #if defined(TIMSK3)
  323. case 3:
  324. OCR3A = ocr;
  325. timer3_toggle_count = toggle_count;
  326. bitWrite(TIMSK3, OCIE3A, 1);
  327. break;
  328. #endif
  329. #if defined(TIMSK4)
  330. case 4:
  331. OCR4A = ocr;
  332. timer4_toggle_count = toggle_count;
  333. bitWrite(TIMSK4, OCIE4A, 1);
  334. break;
  335. #endif
  336. #if defined(OCR5A) && defined(TIMSK5) && defined(OCIE5A)
  337. case 5:
  338. OCR5A = ocr;
  339. timer5_toggle_count = toggle_count;
  340. bitWrite(TIMSK5, OCIE5A, 1);
  341. break;
  342. #endif
  343. }
  344. }
  345. }
  346. // XXX: this function only works properly for timer 2 (the only one we use
  347. // currently). for the others, it should end the tone, but won't restore
  348. // proper PWM functionality for the timer.
  349. void disableTimer(uint8_t _timer)
  350. {
  351. switch (_timer)
  352. {
  353. case 0:
  354. #if defined(TIMSK0)
  355. TIMSK0 = 0;
  356. #elif defined(TIMSK)
  357. TIMSK = 0; // atmega32
  358. #endif
  359. break;
  360. #if defined(TIMSK1) && defined(OCIE1A)
  361. case 1:
  362. bitWrite(TIMSK1, OCIE1A, 0);
  363. break;
  364. #endif
  365. case 2:
  366. #if defined(TIMSK2) && defined(OCIE2A)
  367. bitWrite(TIMSK2, OCIE2A, 0); // disable interrupt
  368. #endif
  369. #if defined(TCCR2A) && defined(WGM20)
  370. TCCR2A = (1 << WGM20);
  371. #endif
  372. #if defined(TCCR2B) && defined(CS22)
  373. TCCR2B = (TCCR2B & 0b11111000) | (1 << CS22);
  374. #endif
  375. #if defined(OCR2A)
  376. OCR2A = 0;
  377. #endif
  378. break;
  379. #if defined(TIMSK3)
  380. case 3:
  381. TIMSK3 = 0;
  382. break;
  383. #endif
  384. #if defined(TIMSK4)
  385. case 4:
  386. TIMSK4 = 0;
  387. break;
  388. #endif
  389. #if defined(TIMSK5)
  390. case 5:
  391. TIMSK5 = 0;
  392. break;
  393. #endif
  394. }
  395. }
  396. void noTone(uint8_t _pin)
  397. {
  398. int8_t _timer = -1;
  399. for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
  400. if (tone_pins[i] == _pin) {
  401. _timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
  402. tone_pins[i] = 255;
  403. }
  404. }
  405. disableTimer(_timer);
  406. digitalWrite(_pin, 0);
  407. }
  408. #if 0
  409. #if !defined(__AVR_ATmega8__)
  410. ISR(TIMER0_COMPA_vect)
  411. {
  412. if (timer0_toggle_count != 0)
  413. {
  414. // toggle the pin
  415. *timer0_pin_port ^= timer0_pin_mask;
  416. if (timer0_toggle_count > 0)
  417. timer0_toggle_count--;
  418. }
  419. else
  420. {
  421. disableTimer(0);
  422. *timer0_pin_port &= ~(timer0_pin_mask); // keep pin low after stop
  423. }
  424. }
  425. #endif
  426. ISR(TIMER1_COMPA_vect)
  427. {
  428. if (timer1_toggle_count != 0)
  429. {
  430. // toggle the pin
  431. *timer1_pin_port ^= timer1_pin_mask;
  432. if (timer1_toggle_count > 0)
  433. timer1_toggle_count--;
  434. }
  435. else
  436. {
  437. disableTimer(1);
  438. *timer1_pin_port &= ~(timer1_pin_mask); // keep pin low after stop
  439. }
  440. }
  441. #endif
  442. ISR(TIMER2_COMPA_vect)
  443. {
  444. if (timer2_toggle_count != 0)
  445. {
  446. // toggle the pin
  447. *timer2_pin_port ^= timer2_pin_mask;
  448. if (timer2_toggle_count > 0)
  449. timer2_toggle_count--;
  450. }
  451. else
  452. {
  453. // need to call noTone() so that the tone_pins[] entry is reset, so the
  454. // timer gets initialized next time we call tone().
  455. // XXX: this assumes timer 2 is always the first one used.
  456. noTone(tone_pins[0]);
  457. // disableTimer(2);
  458. // *timer2_pin_port &= ~(timer2_pin_mask); // keep pin low after stop
  459. }
  460. }
  461. //#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  462. #if 0
  463. ISR(TIMER3_COMPA_vect)
  464. {
  465. if (timer3_toggle_count != 0)
  466. {
  467. // toggle the pin
  468. *timer3_pin_port ^= timer3_pin_mask;
  469. if (timer3_toggle_count > 0)
  470. timer3_toggle_count--;
  471. }
  472. else
  473. {
  474. disableTimer(3);
  475. *timer3_pin_port &= ~(timer3_pin_mask); // keep pin low after stop
  476. }
  477. }
  478. ISR(TIMER4_COMPA_vect)
  479. {
  480. if (timer4_toggle_count != 0)
  481. {
  482. // toggle the pin
  483. *timer4_pin_port ^= timer4_pin_mask;
  484. if (timer4_toggle_count > 0)
  485. timer4_toggle_count--;
  486. }
  487. else
  488. {
  489. disableTimer(4);
  490. *timer4_pin_port &= ~(timer4_pin_mask); // keep pin low after stop
  491. }
  492. }
  493. ISR(TIMER5_COMPA_vect)
  494. {
  495. if (timer5_toggle_count != 0)
  496. {
  497. // toggle the pin
  498. *timer5_pin_port ^= timer5_pin_mask;
  499. if (timer5_toggle_count > 0)
  500. timer5_toggle_count--;
  501. }
  502. else
  503. {
  504. disableTimer(5);
  505. *timer5_pin_port &= ~(timer5_pin_mask); // keep pin low after stop
  506. }
  507. }
  508. #endif