Skip to content
This repository was archived by the owner on Feb 21, 2020. It is now read-only.

Commit 9d6e244

Browse files
committed
Fixed Tone Library
1 parent cc1970f commit 9d6e244

File tree

6 files changed

+46
-16
lines changed

6 files changed

+46
-16
lines changed

cores/arduino/HardwareTimer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ void HardwareTimer::attachInterrupt(int channel, voidFuncPtr handler) {
128128
timer_attach_interrupt(this->dev, (uint8)channel, handler);
129129
}
130130

131+
void HardwareTimer::toneAttachInterrupt(int channel, voidFuncPtr handler) {
132+
tone_attach_interrupt(this->dev, (uint8)channel, handler);
133+
}
134+
131135
void HardwareTimer::detachInterrupt(int channel) {
132136
timer_detach_interrupt(this->dev, (uint8)channel);
133137
}

cores/arduino/HardwareTimer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ class HardwareTimer {
192192
* @see voidFuncPtr
193193
*/
194194
void attachInterrupt(int channel, voidFuncPtr handler);
195+
void toneAttachInterrupt(int channel, voidFuncPtr handler); // alfran : used for tone
195196

196197
/**
197198
* @brief Remove the interrupt handler attached to the given

cores/arduino/Tone.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Version Modified By Date Comments
4040

4141
volatile static int toggle_count=0;
4242
static int tone_pin;
43-
int timer_CH = 2;
43+
int timer_CH = 5;
4444

4545
HardwareTimer timer(timer_CH);
4646

@@ -55,7 +55,8 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration)
5555
timer.setOverflow(1000000/frequency/4);
5656
timer.setMode(TIMER_CH1, TIMER_OUTPUT_COMPARE);
5757
timer.setCompare(TIMER_CH1, 1); // Interrupt 1 count after each update
58-
timer.attachInterrupt(TIMER_CH1, handler_tone);
58+
//timer.attachInterrupt(TIMER_CH1, handler_tone);
59+
timer.toneAttachInterrupt(TIMER_CH1, handler_tone);
5960
timer.refresh(); // start it up
6061
timer.resume();
6162

@@ -66,17 +67,22 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration)
6667
void noTone(uint8_t pin)
6768
{
6869
timer.pause();
69-
gpio_write_bit(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit, 0);
70-
71-
return;
70+
timer.setPrescaleFactor(1);
71+
timer.setOverflow(0xFFFF);
72+
timer.setMode(TIMER_CH1, TIMER_PWM);
73+
timer.refresh();
74+
timer.resume();
75+
gpio_write_bit(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit, 0);
76+
77+
return;
7278
}
73-
79+
/*
7480
void set_timer(int ch)
7581
{
7682
HardwareTimer timer(ch);
7783
timer_CH = ch;
7884
}
79-
85+
*/
8086
void handler_tone(void)
8187
{
8288
if (toggle_count != 0){

cores/arduino/pwm.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void analogWrite(uint8 pin, uint16 passed_val)
6767
timer_dev *dev = PIN_MAP[pin].timer_device;
6868
if (pin >= GPIO_PINS || dev == NULL || dev->type == TIMER_BASIC)
6969
{
70-
return;
70+
return;
7171
}
7272
if (PIN_MAP[pin].alternate_function != AFx)
7373
{
@@ -76,6 +76,7 @@ void analogWrite(uint8 pin, uint16 passed_val)
7676
timer_set_compare(dev, PIN_MAP[pin].timer_channel, duty_cycle);
7777
timer_cc_enable(dev, PIN_MAP[pin].timer_channel);
7878
gpio_set_mode(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit, GPIO_AF_OUTPUT_PP);
79+
7980
}
8081
}
8182

cores/arduino/timer.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@
4343
* [5] = COM;
4444
* [6] = TRG;
4545
* [7] = BRK. */
46-
#define NR_ADV_HANDLERS 2
46+
#define NR_ADV_HANDLERS 8//2
4747
/* Update, capture/compare 1,2,3,4; <junk>; trigger. */
48-
#define NR_GEN_HANDLERS 10
48+
#define NR_GEN_HANDLERS 4//10
4949
/* Update only. */
5050
#define NR_BAS_HANDLERS 2
5151

@@ -306,8 +306,8 @@ void timer_foreach(void (*fn)(timer_dev*)) {
306306
//*bb_perip(&(TIMER6->regs).gen->EGR, TIMER_EGR_UG_BIT) = 1;
307307

308308
// --- Timer 7 Preset
309-
//(TIMER7->regs).gen->PSC = 0;
310-
//*bb_perip(&(TIMER7->regs).gen->EGR, TIMER_EGR_UG_BIT) = 1;
309+
(TIMER7->regs).gen->PSC = 0;
310+
*bb_perip(&(TIMER7->regs).gen->EGR, TIMER_EGR_UG_BIT) = 1;
311311

312312
// --- Timer 8 Preset
313313
(TIMER8->regs).gen->PSC = 2;
@@ -330,12 +330,12 @@ void timer_foreach(void (*fn)(timer_dev*)) {
330330
*bb_perip(&(TIMER12->regs).gen->EGR, TIMER_EGR_UG_BIT) = 1;
331331

332332
// --- Timer 13 Preset
333-
//(TIMER13->regs).gen->PSC = 0;
334-
//*bb_perip(&(TIMER13->regs).gen->EGR, TIMER_EGR_UG_BIT) = 1;
333+
(TIMER13->regs).gen->PSC = 0;
334+
*bb_perip(&(TIMER13->regs).gen->EGR, TIMER_EGR_UG_BIT) = 1;
335335

336336
// --- Timer 14 Preset
337-
//(TIMER14->regs).gen->PSC = 0;
338-
//*bb_perip(&(TIMER14->regs).gen->EGR, TIMER_EGR_UG_BIT) = 1;
337+
(TIMER14->regs).gen->PSC = 0;
338+
*bb_perip(&(TIMER14->regs).gen->EGR, TIMER_EGR_UG_BIT) = 1;
339339

340340
}
341341

@@ -357,6 +357,14 @@ void timer_attach_interrupt(timer_dev *dev,
357357
enable_irq(dev, interrupt);
358358
}
359359

360+
void tone_attach_interrupt(timer_dev *dev,
361+
uint8 interrupt,
362+
voidFuncPtr handler) {
363+
dev->handlers[interrupt] = handler;
364+
timer_enable_irq(dev, interrupt);
365+
enable_irq(dev, interrupt);
366+
}
367+
360368
/**
361369
* @brief Detach a timer interrupt.
362370
* @param dev Timer device
@@ -371,6 +379,11 @@ void timer_detach_interrupt(timer_dev *dev, uint8 interrupt) {
371379
dev->handlers[interrupt-1] = NULL;
372380
}
373381

382+
void tone_detach_interrupt(timer_dev *dev, uint8 interrupt) {
383+
timer_disable_irq(dev, interrupt);
384+
dev->handlers[interrupt-1] = NULL;
385+
}
386+
374387
/*
375388
* IRQ handlers
376389
*/

cores/arduino/timer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,11 @@ typedef enum timer_interrupt_id {
605605
void timer_attach_interrupt(timer_dev *dev,
606606
uint8 interrupt,
607607
voidFuncPtr handler);
608+
609+
void tone_attach_interrupt(timer_dev *dev,
610+
uint8 interrupt,
611+
voidFuncPtr handler);
612+
608613
void timer_detach_interrupt(timer_dev *dev, uint8 interrupt);
609614

610615
/**

0 commit comments

Comments
 (0)