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