|
| 1 | +/* Tone.cpp |
| 2 | +
|
| 3 | + A Tone Generator Library |
| 4 | +
|
| 5 | + Written by Brett Hagman |
| 6 | +
|
| 7 | + This library is free software; you can redistribute it and/or |
| 8 | + modify it under the terms of the GNU Lesser General Public |
| 9 | + License as published by the Free Software Foundation; either |
| 10 | + version 2.1 of the License, or (at your option) any later version. |
| 11 | +
|
| 12 | + This library is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + Lesser General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU Lesser General Public |
| 18 | + License along with this library; if not, write to the Free Software |
| 19 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | +*/ |
| 21 | +//Modified 3 june 2024 by TempersLee for CH32duino |
| 22 | + |
| 23 | +#include "Arduino.h" |
| 24 | +#include "HardwareTimer.h" |
| 25 | + |
| 26 | +#if defined(TIM_MODULE_ENABLED) && defined(TIMER_TONE) && !defined(TIM_MODULE_ONLY) |
| 27 | + |
| 28 | +#define MAX_FREQ 65535 |
| 29 | + |
| 30 | +typedef struct { |
| 31 | + PinName pin; |
| 32 | + int32_t count; |
| 33 | +} timerPinInfo_t; |
| 34 | + |
| 35 | +static void timerTonePinInit(PinName p, uint32_t frequency, uint32_t duration); |
| 36 | +static void tonePeriodElapsedCallback(); |
| 37 | +static timerPinInfo_t TimerTone_pinInfo = {NC, 0}; |
| 38 | +static HardwareTimer *TimerTone = NULL; |
| 39 | + |
| 40 | +/** |
| 41 | + * @brief Tone Period elapsed callback in non-blocking mode |
| 42 | + * @param htim : timer handle |
| 43 | + * @retval None |
| 44 | + */ |
| 45 | +static void tonePeriodElapsedCallback() |
| 46 | +{ |
| 47 | + GPIO_TypeDef *port = get_GPIO_Port(CH_PORT(TimerTone_pinInfo.pin)); |
| 48 | + |
| 49 | + if (port != NULL) { |
| 50 | + if (TimerTone_pinInfo.count != 0) { |
| 51 | + if (TimerTone_pinInfo.count > 0) { |
| 52 | + TimerTone_pinInfo.count--; |
| 53 | + } |
| 54 | + digital_io_toggle(port, CH_MAP_GPIO_PIN(TimerTone_pinInfo.pin)); |
| 55 | + } else { |
| 56 | + digital_io_write(port, CH_MAP_GPIO_PIN(TimerTone_pinInfo.pin), 0); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * @brief This function will reset the tone timer |
| 63 | + * @param port : pointer to port |
| 64 | + * @param pin : pin number to toggle |
| 65 | + * @retval None |
| 66 | + */ |
| 67 | +static void timerTonePinDeinit() |
| 68 | +{ |
| 69 | + if (TimerTone != NULL) { |
| 70 | + TimerTone->timerHandleDeinit(); |
| 71 | + } |
| 72 | + if (TimerTone_pinInfo.pin != NC) { |
| 73 | + pin_function(TimerTone_pinInfo.pin, CH_PIN_DATA(CH_MODE_INPUT, CH_CNF_INPUT_FLOAT, NOPULL, AFIO_NONE)); |
| 74 | + TimerTone_pinInfo.pin = NC; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +static void timerTonePinInit(PinName p, uint32_t frequency, uint32_t duration) |
| 79 | +{ |
| 80 | + uint32_t timFreq = 2 * frequency; |
| 81 | + |
| 82 | + if (frequency <= MAX_FREQ) { |
| 83 | + if (frequency == 0) { |
| 84 | + if (TimerTone != NULL) { |
| 85 | + TimerTone->pause(); |
| 86 | + } |
| 87 | + } else { |
| 88 | + TimerTone_pinInfo.pin = p; |
| 89 | + |
| 90 | + //Calculate the toggle count |
| 91 | + if (duration > 0) { |
| 92 | + TimerTone_pinInfo.count = ((timFreq * duration) / 1000); |
| 93 | + } else { |
| 94 | + TimerTone_pinInfo.count = -1; |
| 95 | + } |
| 96 | + |
| 97 | + pin_function(TimerTone_pinInfo.pin, CH_PIN_DATA(CH_MODE_OUTPUT_50MHz, CH_CNF_OUTPUT_PP, NOPULL, AFIO_NONE)); |
| 98 | + |
| 99 | + TimerTone->setOverflow(timFreq, HERTZ_FORMAT); |
| 100 | + TimerTone->attachInterrupt(tonePeriodElapsedCallback); |
| 101 | + TimerTone->resume(); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// frequency (in hertz) and duration (in milliseconds). |
| 107 | +void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) |
| 108 | +{ |
| 109 | + PinName p = digitalPinToPinName(_pin); |
| 110 | + |
| 111 | + if (TimerTone == NULL) { |
| 112 | + TimerTone = new HardwareTimer(TIMER_TONE); |
| 113 | + } |
| 114 | + |
| 115 | + if (p != NC) { |
| 116 | + if ((TimerTone_pinInfo.pin == NC) || (TimerTone_pinInfo.pin == p)) { |
| 117 | + timerTonePinInit(p, frequency, duration); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +void noTone(uint8_t _pin, bool destruct) |
| 123 | +{ |
| 124 | + PinName p = digitalPinToPinName(_pin); |
| 125 | + if ((p != NC) && (TimerTone_pinInfo.pin == p) && (TimerTone != NULL)) { |
| 126 | + if (destruct) { |
| 127 | + timerTonePinDeinit(); |
| 128 | + delete (TimerTone); |
| 129 | + TimerTone = NULL; |
| 130 | + } else { |
| 131 | + TimerTone->pause(); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | +#else |
| 136 | +#warning "TIMER_TONE or TIM_MODULE_ENABLED not defined" |
| 137 | +void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) |
| 138 | +{ |
| 139 | + (void)(_pin); |
| 140 | + (void)(frequency); |
| 141 | + (void)(duration); |
| 142 | +} |
| 143 | + |
| 144 | +void noTone(uint8_t _pin, bool destruct) |
| 145 | +{ |
| 146 | + (void)(_pin); |
| 147 | + (void)(destruct); |
| 148 | +} |
| 149 | +#endif /* TIM_MODULE_ENABLED && TIMER_TONE && !TIM_MODULE_ONLY*/ |
0 commit comments