ESP_IOT v2.5
IOT ESP Coding
PTTimer.cpp
Go to the documentation of this file.
1/*
2 Name: esp32TIMER.ino
3 Created: 9/4/2021 6:55:06 PM
4 Author: wes
5*/
6#ifdef USE_PTTIMER
7
8
9/* Copyright (c) 2017 pcbreflux. All Rights Reserved.
10 * https://www.youtube.com/watch?v=LONGI_JcwEQ&ab_channel=pcbreflux
11 * https://github.com/pcbreflux/espressif/tree/master/esp32/arduino/sketchbook/ESP32_multitimer
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, version 3.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
23 *
24 */
25 //#include "PTTimer.h"
26#if 0 //build errors ex Severity Code Description Project File Line Suppression State
27//Error 33:1 : error : 'hw_timer_t' does not name a type C : \Projects\NimBLE_repo\NimBLE_PetTutor_Server\PTTimer.cpp 33
28
29#define PRESCALE0 80
30#define PRESCALE1 80
31#define COUNT_UP true
32#define COUNT_DOWN false
33#define EDGE_TRIGGERED true
34#define LEVEL_TRIGGERED false
35
36hw_timer_t* timer0 = NULL;
37//hw_timer_t* timer1 = NULL;
38portMUX_TYPE timerMux0 = portMUX_INITIALIZER_UNLOCKED;
39//portMUX_TYPE timerMux1 = portMUX_INITIALIZER_UNLOCKED;
40
41//volatile uint8_t led1stat = 0;
42//volatile uint8_t led2stat = 0;
43
44/* Global Countdown Timers */
45unsigned int u16CountDown0 = 0; // use for the x second timer on startup to update the device#
46unsigned int u16CountDown1 = 0; // use for timing the 5 seconds after a manual feed button press on the feeder to lower and then raise the power
47unsigned int u16CountDown2 = 0; // this is used to alert the program when flash is being updated
48unsigned int u16CountDown3 = 0;
49unsigned int u16CountDown4 = 0;
50
51void TickService0(void)
52{
53 if (u16CountDown0) u16CountDown0--;
54 if (u16CountDown1) u16CountDown1--;
55 if (u16CountDown2) u16CountDown2--;
56 if (u16CountDown3) u16CountDown3--;
57 if (u16CountDown4) u16CountDown4--;
58}
59
60void IRAM_ATTR onTimer0() {
61 portENTER_CRITICAL_ISR(&timerMux0);
62 TickService0();
63 portEXIT_CRITICAL_ISR(&timerMux0);
64}
65
66
67void PTTimerSetup() {
68 timer0 = timerBegin(0, PRESCALE0, COUNT_UP); // timer 0, MWDT clock period = 12.5 ns * TIMGn_Tx_WDT_CLK_PRESCALE -> 12.5 ns * 80 -> 1000 ns = 1 us, countUp
69 timerAttachInterrupt(timer0, &onTimer0, EDGE_TRIGGERED); // edge (not level) triggered
70 timerAlarmWrite(timer0, 1000, true); // 1000 * 1 us = 1 ms, autoreload true
71
72 // at least enable the timer alarms
73 timerAlarmEnable(timer0); // enable
74 u16CountDown0 = 0;
75 u16CountDown1 = 0;
76 u16CountDown2 = 0;
77 u16CountDown3 = 0;
78 u16CountDown4 = 0;
79}
80
81void test() {
82 // nope nothing here
83 if (!u16CountDown2) {
84// SerialDebug.print(u16CountDown2);
85// SerialDebug.println(String(" ...onTimer0() ") + String(millis()));
86 if (millis() > 10000) {
87 u16CountDown2 = 1000;
88 }
89 else {
90 u16CountDown2 = 200;
91 }
92
93 }
94}
95#endif //0
96
97#endif //USE_PT_TIMER
unsigned long millis()
Definition: TinyGPS.cpp:35