From 62712f1b99e330078cf6fc434dc6ddcf0723bc6e Mon Sep 17 00:00:00 2001 From: Khoi Hoang <57012152+khoih-prog@users.noreply.github.com> Date: Sun, 6 Feb 2022 22:11:13 -0500 Subject: [PATCH] Delete megaAVR_Slow_PWM_ISR.hpp --- src_h/megaAVR_Slow_PWM_ISR.hpp | 357 --------------------------------- 1 file changed, 357 deletions(-) delete mode 100644 src_h/megaAVR_Slow_PWM_ISR.hpp diff --git a/src_h/megaAVR_Slow_PWM_ISR.hpp b/src_h/megaAVR_Slow_PWM_ISR.hpp deleted file mode 100644 index 438be03..0000000 --- a/src_h/megaAVR_Slow_PWM_ISR.hpp +++ /dev/null @@ -1,357 +0,0 @@ -/**************************************************************************************************************************** - megaAVR_Slow_PWM.hpp - For Arduino megaAVR ATMEGA4809-based boards (UNO WiFi Rev2, NANO_EVERY, etc. ) - Written by Khoi Hoang - - Built by Khoi Hoang https://github.com/khoih-prog/megaAVR_Slow_PWM - Licensed under MIT license - - Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by - unsigned long miliseconds), you just consume only one megaAVR-based timer and avoid conflicting with other cores' tasks. - The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers - Therefore, their executions are not blocked by bad-behaving functions / tasks. - This important feature is absolutely necessary for mission-critical tasks. - - Version: 1.1.0 - - Version Modified By Date Comments - ------- ----------- ---------- ----------- - 1.0.0 K.Hoang 27/09/2021 Initial coding for megaAVR-based boards (UNO WiFi Rev2, NANO_EVERY, etc.) - 1.1.0 K Hoang 10/11/2021 Add functions to modify PWM settings on-the-fly -*****************************************************************************************************************************/ - -#pragma once - -#ifndef MEGA_AVR_SLOW_PWM_ISR_HPP -#define MEGA_AVR_SLOW_PWM_ISR_HPP - -#include - -/////////////////////////////////////////////////// - - -uint32_t timeNow() -{ -#if USING_MICROS_RESOLUTION - return ( (uint32_t) micros() ); -#else - return ( (uint32_t) millis() ); -#endif -} - -/////////////////////////////////////////////////// - -MEGA_AVR_SLOW_PWM_ISR::MEGA_AVR_SLOW_PWM_ISR() - : numChannels (-1) -{ -} - -/////////////////////////////////////////////////// - -void MEGA_AVR_SLOW_PWM_ISR::init() -{ - uint32_t currentTime = timeNow(); - - for (uint8_t channelNum = 0; channelNum < MAX_NUMBER_CHANNELS; channelNum++) - { - memset((void*) &PWM[channelNum], 0, sizeof (PWM_t)); - PWM[channelNum].prevTime = currentTime; - PWM[channelNum].pin = INVALID_MEGA_AVR_PIN; - } - - numChannels = 0; -} - -/////////////////////////////////////////////////// - -void MEGA_AVR_SLOW_PWM_ISR::run() -{ - uint32_t currentTime = timeNow(); - - noInterrupts(); - - for (uint8_t channelNum = 0; channelNum < MAX_NUMBER_CHANNELS; channelNum++) - { - // If enabled => check - // start period / dutyCycle => digitalWrite HIGH - // end dutyCycle => digitalWrite LOW - if (PWM[channelNum].enabled) - { - if ( (uint32_t) (currentTime - PWM[channelNum].prevTime) <= PWM[channelNum].onTime ) - { - if (!PWM[channelNum].pinHigh) - { - digitalWrite(PWM[channelNum].pin, HIGH); - PWM[channelNum].pinHigh = true; - - // callbackStart - if (PWM[channelNum].callbackStart != nullptr) - { - (*(timer_callback) PWM[channelNum].callbackStart)(); - } - } - } - else if ( (uint32_t) (currentTime - PWM[channelNum].prevTime) < PWM[channelNum].period ) - { - if (PWM[channelNum].pinHigh) - { - digitalWrite(PWM[channelNum].pin, LOW); - PWM[channelNum].pinHigh = false; - - // callback when PWM pulse stops (LOW) - if (PWM[channelNum].callbackStop != nullptr) - { - (*(timer_callback) PWM[channelNum].callbackStop)(); - } - } - } - else - //else if ( (uint32_t) (currentTime - PWM[channelNum].prevTime) >= PWM[channelNum].period ) - { - PWM[channelNum].prevTime = currentTime; - } - } - } - - interrupts(); -} - - -/////////////////////////////////////////////////// - -// find the first available slot -// return -1 if none found -int MEGA_AVR_SLOW_PWM_ISR::findFirstFreeSlot() -{ - // all slots are used - if (numChannels >= MAX_NUMBER_CHANNELS) - { - return -1; - } - - // return the first slot with no callbackStart (i.e. free) - for (uint8_t channelNum = 0; channelNum < MAX_NUMBER_CHANNELS; channelNum++) - { - if ( (PWM[channelNum].period == 0) && !PWM[channelNum].enabled ) - { - return channelNum; - } - } - - // no free slots found - return -1; -} - -/////////////////////////////////////////////////// - -int MEGA_AVR_SLOW_PWM_ISR::setupPWMChannel(uint32_t pin, uint32_t period, uint32_t dutycycle, void* cbStartFunc, void* cbStopFunc) -{ - int channelNum; - - // Invalid input, such as period = 0, etc - if ( (period == 0) || (dutycycle > 100) ) - { - PWM_LOGERROR(F("Error: Invalid period or dutycycle")); - return -1; - } - - if (numChannels < 0) - { - init(); - } - - channelNum = findFirstFreeSlot(); - - if (channelNum < 0) - { - return -1; - } - - PWM[channelNum].pin = pin; - PWM[channelNum].period = period; - PWM[channelNum].onTime = ( period * dutycycle ) / 100; - - pinMode(pin, OUTPUT); - digitalWrite(pin, HIGH); - PWM[channelNum].pinHigh = true; - - PWM[channelNum].prevTime = timeNow(); - - PWM[channelNum].callbackStart = cbStartFunc; - PWM[channelNum].callbackStop = cbStopFunc; - - PWM_LOGINFO0(F("Channel : ")); PWM_LOGINFO0(channelNum); - PWM_LOGINFO0(F("\tPeriod : ")); PWM_LOGINFO0(PWM[channelNum].period); - PWM_LOGINFO0(F("\t\tOnTime : ")); PWM_LOGINFO0(PWM[channelNum].onTime); - PWM_LOGINFO0(F("\tStart_Time : ")); PWM_LOGINFOLN0(PWM[channelNum].prevTime); - - numChannels++; - - PWM[channelNum].enabled = true; - - return channelNum; -} - -/////////////////////////////////////////////////// - -bool MEGA_AVR_SLOW_PWM_ISR::modifyPWMChannel_Period(unsigned channelNum, uint32_t pin, uint32_t period, uint32_t dutycycle) -{ - // Invalid input, such as period = 0, etc - if ( (period == 0) || (dutycycle > 100) ) - { - PWM_LOGERROR("Error: Invalid period or dutycycle"); - return false; - } - - if (channelNum > MAX_NUMBER_CHANNELS) - { - PWM_LOGERROR("Error: channelNum > MAX_NUMBER_CHANNELS"); - return false; - } - - if (PWM[channelNum].pin != pin) - { - PWM_LOGERROR("Error: channelNum and pin mismatched"); - return false; - } - - PWM[channelNum].period = period; - PWM[channelNum].onTime = ( period * dutycycle ) / 100; - - digitalWrite(pin, HIGH); - PWM[channelNum].pinHigh = true; - - PWM[channelNum].prevTime = timeNow(); - - PWM_LOGINFO0("Channel : "); PWM_LOGINFO0(channelNum); PWM_LOGINFO0("\tPeriod : "); PWM_LOGINFO0(PWM[channelNum].period); - PWM_LOGINFO0("\t\tOnTime : "); PWM_LOGINFO0(PWM[channelNum].onTime); PWM_LOGINFO0("\tStart_Time : "); PWM_LOGINFOLN0(PWM[channelNum].prevTime); - - return true; -} - - -/////////////////////////////////////////////////// - -void MEGA_AVR_SLOW_PWM_ISR::deleteChannel(unsigned channelNum) -{ - if (channelNum >= MAX_NUMBER_CHANNELS) - { - return; - } - - // nothing to delete if no timers are in use - if (numChannels == 0) - { - return; - } - - // don't decrease the number of timers if the specified slot is already empty (zero period, invalid) - if ( (PWM[channelNum].pin != INVALID_MEGA_AVR_PIN) && (PWM[channelNum].period != 0) ) - { - memset((void*) &PWM[channelNum], 0, sizeof (PWM_t)); - - PWM[channelNum].pin = INVALID_MEGA_AVR_PIN; - - // update number of timers - numChannels--; - } -} - -/////////////////////////////////////////////////// - -void MEGA_AVR_SLOW_PWM_ISR::restartChannel(unsigned channelNum) -{ - if (channelNum >= MAX_NUMBER_CHANNELS) - { - return; - } - - PWM[channelNum].prevTime = timeNow(); -} - -/////////////////////////////////////////////////// - -bool MEGA_AVR_SLOW_PWM_ISR::isEnabled(unsigned channelNum) -{ - if (channelNum >= MAX_NUMBER_CHANNELS) - { - return false; - } - - return PWM[channelNum].enabled; -} - -/////////////////////////////////////////////////// - -void MEGA_AVR_SLOW_PWM_ISR::enable(unsigned channelNum) -{ - if (channelNum >= MAX_NUMBER_CHANNELS) - { - return; - } - - PWM[channelNum].enabled = true; -} - -/////////////////////////////////////////////////// - -void MEGA_AVR_SLOW_PWM_ISR::disable(unsigned channelNum) -{ - if (channelNum >= MAX_NUMBER_CHANNELS) - { - return; - } - - PWM[channelNum].enabled = false; -} - -/////////////////////////////////////////////////// - -void MEGA_AVR_SLOW_PWM_ISR::enableAll() -{ - // Enable all timers with a callbackStart assigned (used) - - for (uint8_t channelNum = 0; channelNum < MAX_NUMBER_CHANNELS; channelNum++) - { - if (PWM[channelNum].period != 0) - { - PWM[channelNum].enabled = true; - } - } -} - -/////////////////////////////////////////////////// - -void MEGA_AVR_SLOW_PWM_ISR::disableAll() -{ - // Disable all timers with a callbackStart assigned (used) - for (uint8_t channelNum = 0; channelNum < MAX_NUMBER_CHANNELS; channelNum++) - { - if (PWM[channelNum].period != 0) - { - PWM[channelNum].enabled = false; - } - } -} - -/////////////////////////////////////////////////// - -void MEGA_AVR_SLOW_PWM_ISR::toggle(unsigned channelNum) -{ - if (channelNum >= MAX_NUMBER_CHANNELS) - { - return; - } - - PWM[channelNum].enabled = !PWM[channelNum].enabled; -} - -/////////////////////////////////////////////////// - -unsigned MEGA_AVR_SLOW_PWM_ISR::getnumChannels() -{ - return numChannels; -} - -#endif // MEGA_AVR_SLOW_PWM_ISR_HPP -