|
-
-
- #include "core_pins.h"
- #include "pins_arduino.h"
- #include "HardwareSerial.h"
- #include "IntervalTimer.h"
-
- #if 1
-
-
-
-
-
- static uint32_t tone_toggle_count;
- static volatile uint8_t *tone_reg;
- static uint8_t tone_pin=255;
- static uint16_t tone_frequency=0;
- IntervalTimer tone_timer;
-
- void tone_interrupt(void);
-
- void tone(uint8_t pin, uint16_t frequency, uint32_t duration)
- {
- uint32_t count;
- volatile uint32_t *config;
- float usec;
-
- if (pin >= CORE_NUM_DIGITAL) return;
- if (duration) {
- count = (frequency * duration / 1000) * 2;
- } else {
- count = 0xFFFFFFFF;
- }
- usec = (float)500000.0 / (float)frequency;
- config = portConfigRegister(pin);
-
-
-
- __disable_irq();
- if (pin == tone_pin) {
- if (frequency == tone_frequency) {
-
-
-
-
-
- tone_toggle_count = count;
- } else {
-
- tone_reg[0] = 1;
- tone_timer.begin(tone_interrupt, usec);
- }
- } else {
- if (tone_pin < CORE_NUM_DIGITAL) {
- tone_reg[0] = 1;
- }
- tone_pin = pin;
- tone_reg = portClearRegister(pin);
- tone_reg[0] = 1;
- tone_reg[384] = 1;
- *config = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
- tone_toggle_count = count;
- tone_timer.begin(tone_interrupt, usec);
- }
- __enable_irq();
- }
-
-
- void tone_interrupt(void)
- {
- if (tone_toggle_count) {
- tone_reg[128] = 1;
- if (tone_toggle_count < 0xFFFFFFFF) tone_toggle_count--;
- } else {
- tone_timer.end();
- tone_reg[0] = 0;
- tone_pin = 255;
- tone_frequency = 0;
- }
- }
-
- void noTone(uint8_t pin)
- {
- if (pin >= CORE_NUM_DIGITAL) return;
- __disable_irq();
- if (pin == tone_pin) {
- tone_timer.end();
- tone_reg[0] = 0;
- tone_pin = 255;
- tone_frequency = 0;
- }
- __enable_irq();
- }
- #endif
-
-
-
- #if 0
-
-
-
- static uint32_t tone_toggle_count;
- static volatile uint8_t *tone_reg;
- static uint8_t tone_pin;
-
- void init_tone(void)
- {
- if (SIM_SCGC6 & SIM_SCGC6_PIT) return;
- SIM_SCGC6 |= SIM_SCGC6_PIT;
- PIT_MCR = 0;
- PIT_TCTRL3 = 0;
- tone_pin = 255;
- NVIC_ENABLE_IRQ(IRQ_PIT_CH3);
- }
-
- void tone(uint8_t pin, uint16_t frequency, uint32_t duration)
- {
- uint32_t count, load;
- volatile uint32_t *config;
-
- init_tone();
- if (pin >= CORE_NUM_DIGITAL) return;
- if (duration) {
- count = (frequency * duration / 1000) * 2;
- } else {
- count = 0xFFFFFFFF;
- }
- load = (F_BUS / 2) / frequency;
- config = portConfigRegister(pin);
- __disable_irq();
- if (pin != tone_pin) {
- if (tone_pin < CORE_NUM_DIGITAL) {
- tone_reg[0] = 1;
- }
- tone_pin = pin;
- tone_reg = portClearRegister(pin);
- tone_reg[0] = 1;
- tone_reg[384] = 1;
- *config = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
- }
- tone_toggle_count = count;
- if (PIT_LDVAL3 != load) {
- PIT_TCTRL3 = 0;
- PIT_LDVAL3 = load;
- PIT_TCTRL3 = 3;
- }
- __enable_irq();
- }
-
- void pit3_isr(void)
- {
- PIT_TFLG3 = 1;
-
- if (tone_toggle_count) {
- tone_reg[128] = 1;
- if (tone_toggle_count < 0xFFFFFFFF) tone_toggle_count--;
- } else {
- PIT_TCTRL3 = 0;
- PIT_LDVAL3 = 0;
- tone_reg[0] = 0;
- tone_pin = 255;
- }
- }
-
- void noTone(uint8_t pin)
- {
- if (pin >= CORE_NUM_DIGITAL) return;
- __disable_irq();
- if (pin == tone_pin) {
- PIT_TCTRL3 = 0;
- PIT_LDVAL3 = 0;
- tone_reg[0] = 0;
- tone_pin = 255;
- }
- __enable_irq();
- }
- #endif
-
-
-
-
-
-
-
-
|