FabGL
ESP32 Display Controller and Graphics Library
PIT8253.h
1 /*
2  Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3  Copyright (c) 2019-2021 Fabrizio Di Vittorio.
4  All rights reserved.
5 
6 
7 * Please contact fdivitto2013@gmail.com if you need a commercial license.
8 
9 
10 * This library and related software is available under GPL v3. Feel free to use FabGL in free software and hardware:
11 
12  FabGL 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, either version 3 of the License, or
15  (at your option) any later version.
16 
17  FabGL is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with FabGL. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 
27 #pragma once
28 
29 #include "fabgl.h"
30 
31 
32 namespace fabgl {
33 
34 
35 
36 // PIT 8253 (Programmable Interval Timers)
37 
38 class PIT8253 {
39 
40 public:
41 
42  typedef void (*ChangeOut)(void * context, int timerIndex);
43  typedef void (*Tick)(void * context, int ticks);
44 
45  struct TimerInfo {
46  bool BCD; // BCD mode
47  int32_t mode; // Timer mode
48  int32_t RLMode; // Read/Load mode
49  int32_t resetHolding; // Holding area for timer reset count
50  int32_t resetCount; // Reload value when count is zero
51  int32_t count; // Current timer counter
52  int32_t latch; // Latched timer count (-1 = not latched)
53  bool LSBToggle; // true: Read load LSB, false: Read load MSB
54  bool out; // out state
55  bool gate; // date (1 = timer running)
56  bool running; // counting down in course
57  };
58 
59  PIT8253();
60  ~PIT8253();
61 
62  void setCallbacks(void * context, ChangeOut changeOut, Tick tick) {
63  m_context = context;
64  m_changeOut = changeOut;
65  m_tick = tick;
66  }
67 
68  void runAutoTick(int freq, int updatesPerSec);
69 
70  void reset();
71 
72  void tick(int ticks);
73 
74  void write(int reg, uint8_t value);
75  uint8_t read(int reg);
76 
77  bool getOut(int timerIndex) { return m_timer[timerIndex].out; }
78  bool getGate(int timerIndex) { return m_timer[timerIndex].gate; }
79 
80  void setGate(int timerIndex, bool value);
81 
82  TimerInfo const & timerInfo(int timerIndex) { return m_timer[timerIndex]; }
83 
84 
85 private:
86 
87 
88  static void autoTickTask(void * pvParameters);
89 
90  void changeOut(int timer, bool value);
91 
92  void unsafeTick(int ticks);
93 
94  TimerInfo m_timer[3];
95 
96  SemaphoreHandle_t m_mutex;
97 
98  // callbacks
99  void * m_context;
100  ChangeOut m_changeOut;
101  Tick m_tick;
102 
103  // autotick support
104  TaskHandle_t m_taskHandle;
105  int32_t m_autoTickFreq;
106  int32_t m_updatesPerSec;
107 
108 };
109 
110 
111 } // namespace fabgl
Definition: canvas.cpp:36
This file is the all in one include file. Application can just include this file to use FabGL library...