sim_pin.h Source File

yasimavr: sim_pin.h Source File
yasimavr
Loading...
Searching...
No Matches
sim_pin.h
Go to the documentation of this file.
1/*
2 * sim_pin.h
3 *
4 * Copyright 2021-2026 Clement Savergne <csavergne@yahoo.com>
5
6 This file is part of yasim-avr.
7
8 yasim-avr is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 yasim-avr is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with yasim-avr. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22//=======================================================================================
23
24#ifndef __YASIMAVR_PIN_H__
25#define __YASIMAVR_PIN_H__
26
27#include "sim_wire.h"
28
30
31
32//=======================================================================================
33
35
36
37class PinManager;
38
39
50
51public:
52
56 struct controls_t {
57
59 unsigned char dir = 0;
61 unsigned char drive = 0;
63 bool inverted = false;
65 bool pull_up = false;
66
67 };
68
69 explicit Pin(pin_id_t id);
70
71 pin_id_t id() const;
72
73 void set_external_state(const state_t& state);
74 void set_external_state(StateEnum state, double level = 0.0);
75 void set_external_state(char state, double level = 0.0);
76
77 void set_gpio_controls(const controls_t& controls);
78 controls_t gpio_controls() const;
79
80 Pin(const Pin&) = delete;
81 Pin& operator=(const Pin&) = delete;
82
83private:
84
85 friend class PinManager;
86 friend class PinDriver;
87
88 pin_id_t m_id;
89 controls_t m_gpio_controls;
90 state_t m_gpio_state;
91 PinManager* m_manager;
92
93 virtual void notify_digital_state(bool state) override;
94 virtual state_t state_for_resolution() const override;
95
96 void update_pin_state();
97
98};
99
100
101inline pin_id_t Pin::id() const
102{
103 return m_id;
104}
105
109inline void Pin::set_external_state(const state_t& state)
110{
112}
113
117inline void Pin::set_external_state(StateEnum state, double level)
118{
119 set_state(state, level);
120}
121
127inline void Pin::set_external_state(char state, double level)
128{
129 set_state(state, level);
130}
131
136{
137 return m_gpio_controls;
138}
139
140
141//=======================================================================================
142
160
161public:
162
163 typedef unsigned int pin_index_t;
164
165 PinDriver(ctl_id_t id, pin_index_t pin_count);
166 virtual ~PinDriver();
167
168 void set_enabled(bool enabled);
169 void set_enabled(pin_index_t index, bool enabled);
170 bool enabled(pin_index_t index) const;
171
172 void update_pin_state(pin_index_t pin_index);
173 void update_pin_states();
174
175 Wire::state_t pin_state(pin_index_t pin_index) const;
176 Pin::controls_t gpio_controls(pin_index_t pin_index) const;
177
178 PinDriver(const PinDriver&) = delete;
179 PinDriver& operator=(const PinDriver&) = delete;
180
189 virtual Pin::controls_t override_gpio(pin_index_t pin_index, const Pin::controls_t& controls) = 0;
190
191 virtual void digital_state_changed(pin_index_t pin_index, bool state);
192
193private:
194
195 friend class PinManager;
196
197 ctl_id_t m_id;
198 PinManager* m_manager;
199 pin_index_t m_pin_count;
200 Pin** m_pins;
201
202};
203
204
205//=======================================================================================
206
221
222public:
223
225 static constexpr mux_id_t default_mux_id = "DEFAULT";
226
227 explicit PinManager(const std::vector<pin_id_t>& pin_ids);
228 ~PinManager();
229
230 bool register_driver(PinDriver& drv);
231 bool add_mux_config(ctl_id_t drv, const std::vector<pin_id_t>& pins, mux_id_t mux_id = default_mux_id);
232
233 void set_current_mux(ctl_id_t drv, mux_id_t index);
234 void set_current_mux(ctl_id_t drv, PinDriver::pin_index_t, mux_id_t index);
235 mux_id_t current_mux(ctl_id_t drv, PinDriver::pin_index_t pin_index) const;
236 std::vector<pin_id_t> current_mux_pins(ctl_id_t drv) const;
237
238 Pin* pin(pin_id_t pin_id) const;
239
240 PinManager(const PinManager&) = delete;
241 PinManager& operator=(const PinManager&) = delete;
242
243private:
244
245 friend class Pin;
246 friend class PinDriver;
247
248 struct pin_entry_t;
249 struct drv_entry_t;
250
251 std::unordered_map<pin_id_t, pin_entry_t*> m_pins;
252 std::unordered_map<ctl_id_t, drv_entry_t*> m_drivers;
253
254 void add_driver_to_pin(pin_entry_t& pin_entry, PinDriver& drv, PinDriver::pin_index_t index);
255 void remove_driver_from_pin(pin_entry_t& pin_entry, PinDriver& drv, PinDriver::pin_index_t index);
256
257 Wire::state_t override_gpio(pin_id_t pin_id, const Pin::controls_t& gpio_controls);
258 void notify_digital_state(pin_id_t pin_id, bool state);
259 void set_current_pin_mux(drv_entry_t& drv, PinDriver::pin_index_t index, mux_id_t mux_id);
260 void set_driver_enabled(PinDriver& drv, PinDriver::pin_index_t index, bool enabled);
261 bool driver_enabled(const PinDriver& drv, PinDriver::pin_index_t index) const;
262 void unregister_driver(PinDriver& drv);
263
264};
265
266
268
269#endif //__YASIMAVR_PIN_H__
MCU pin driver.
Definition sim_pin.h:159
unsigned int pin_index_t
Definition sim_pin.h:163
PinDriver(const PinDriver &)=delete
virtual Pin::controls_t override_gpio(pin_index_t pin_index, const Pin::controls_t &controls)=0
PinDriver & operator=(const PinDriver &)=delete
MCU pin model.
Definition sim_pin.h:49
Pin(const Pin &)=delete
void set_external_state(const state_t &state)
Definition sim_pin.h:109
controls_t gpio_controls() const
Definition sim_pin.h:135
pin_id_t id() const
Definition sim_pin.h:101
Pin & operator=(const Pin &)=delete
MCU pin manager.
Definition sim_pin.h:220
PinManager(const PinManager &)=delete
sim_id_t mux_id_t
Definition sim_pin.h:224
PinManager & operator=(const PinManager &)=delete
Definition sim_wire.h:82
General Purpose wire model.
Definition sim_wire.h:55
virtual state_t state_for_resolution() const
Definition sim_wire.cpp:314
void set_state(const state_t &state)
Definition sim_wire.cpp:254
const state_t & state() const
Definition sim_wire.h:226
StateEnum
Definition sim_wire.h:63
virtual void notify_digital_state(bool state)
Definition sim_wire.cpp:354
Representation of a ID internally represented as a 64-bits integer but can be initialised with a stri...
Definition sim_types.h:627
#define YASIMAVR_BEGIN_NAMESPACE
Definition sim_globals.h:58
#define AVR_CORE_PUBLIC_API
Definition sim_globals.h:46
#define YASIMAVR_END_NAMESPACE
Definition sim_globals.h:59
YASIMAVR_BEGIN_NAMESPACE typedef sim_id_t pin_id_t
Definition sim_pin.h:34
Definition sim_pin.h:56
Definition sim_pin.cpp:225
Definition sim_pin.cpp:213