arch_avr_timer.h Source File

yasimavr: arch_avr_timer.h Source File
yasimavr
Loading...
Searching...
No Matches
arch_avr_timer.h
Go to the documentation of this file.
1/*
2 * arch_avr_timer.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_AVR_TIMER_H__
25#define __YASIMAVR_AVR_TIMER_H__
26
27#include "arch_avr_globals.h"
28#include "core/sim_peripheral.h"
29#include "core/sim_interrupt.h"
31
33
34
35//=======================================================================================
36
47#define AVR_CTLREQ_TMR_GET_EXTCLK_HOOK (AVR_CTLREQ_BASE + 1)
48
52#define AVR_CTLREQ_TMR_GET_CAPT_HOOK (AVR_CTLREQ_BASE + 2)
53
55
56
62
79
93
105
117
125
131 unsigned char bit;
132 };
133
143
153
159 OVF ovf : 2;
161 Top top : 2;
163 unsigned int fixed_top_exp : 4;
165 OCR ocr : 2;
167 bool double_slope : 1;
169 bool disable_foc: 1;
171 unsigned int com_variant : 4;
172 };
173
175 typedef std::vector<COM_config_t> COM_variant_t;
176
179
181 std::vector<clock_config_t> clocks;
183 std::vector<mode_config_t> modes;
184
191 std::vector<COM_variant_t> com_modes;
192
194 std::vector<OC_config_t> oc_channels;
195
212
213};
214
215
232
233public:
234
252
253 ArchAVR_Timer(int num, const ArchAVR_TimerConfig& config);
255
256 virtual bool init(Device& device) override;
257 virtual void reset(int flags) override;
258 virtual bool ctlreq(ctlreq_id_t req, ctlreq_data_t* data) override;
259 virtual uint8_t ioreg_read_handler(reg_addr_t addr, uint8_t value) override;
260 virtual uint8_t ioreg_peek_handler(reg_addr_t addr, uint8_t value) override;
261 virtual void ioreg_write_handler(reg_addr_t addr, const ioreg_write_t& data) override;
262
263private:
264
265 struct OutputCompareChannel;
266 friend struct OutputCompareChannel;
267
268 const ArchAVR_TimerConfig& m_config;
269
270 //***** Clock management *****
271 unsigned long m_clk_ps_max; //Max value counted by the clock prescaler
272 //Input Capture Register value
273 uint16_t m_icr;
274 //Temporary register when the CPU is reading 16-bits registers
275 uint8_t m_temp;
276 //Current timer/counter mode
278 //List of output compare modules
279 std::vector<OutputCompareChannel*> m_oc_channels;
280 //Timer counter engine
281 TimerCounter m_counter;
283 //Interrupt and signal management
284 InterruptFlag m_intflag_ovf;
285 InterruptFlag m_intflag_icr;
286 DataSignal m_signal;
288
289 void cnt_raised(const signal_data_t& sigdata, int hooktag);
290 void update_top();
291 void capt_raised(const signal_data_t& sigdata, int hooktag);
292 ArchAVR_TimerConfig::COM_config_t get_COM_config(uint8_t regval);
293 void change_OC_state(size_t index, int event_flags);
294 bool output_active(ArchAVR_TimerConfig::COM_config_t& mode, size_t output_index);
295
296};
297
298
300
301#endif //__YASIMAVR_AVR_TIMER_H__
#define AVR_ARCHAVR_PUBLIC_API
Definition arch_avr_globals.h:46
Timer/Counter model for AVR series.
Definition arch_avr_timer.h:231
SignalId
Definition arch_avr_timer.h:235
@ Signal_OVF
Raised on a overflow event, no data is carried.
Definition arch_avr_timer.h:237
@ Signal_CompMatch
Definition arch_avr_timer.h:242
@ Signal_CompOutput
Definition arch_avr_timer.h:248
Definition sim_signal.h:227
Definition sim_signal.h:137
Basic AVR device model.
Definition sim_device.h:61
Generic helper to manage a typical Interrupt Flag/Enable in a I/O register.
Definition sim_interrupt.h:324
Abstract class defining a framework for MCU peripherals.
Definition sim_peripheral.h:286
virtual bool init(Device &device)
Definition sim_peripheral.cpp:60
virtual bool ctlreq(ctlreq_id_t req, ctlreq_data_t *data)
Definition sim_peripheral.cpp:79
virtual void ioreg_write_handler(reg_addr_t addr, const ioreg_write_t &data) override
Definition sim_peripheral.cpp:117
virtual uint8_t ioreg_read_handler(reg_addr_t addr, uint8_t value) override
Definition sim_peripheral.cpp:91
virtual uint8_t ioreg_peek_handler(reg_addr_t addr, uint8_t value) override
Definition sim_peripheral.cpp:106
virtual void reset(int flags)
Definition sim_peripheral.cpp:72
Generic model of a Counter.
Definition sim_timer.h:164
TickSource
Tick source mode.
Definition sim_timer.h:169
Representation of a I/O register address, with validity state.
Definition sim_types.h:60
Definition sim_types.h:431
Represents a field in a I/O register with address.
Definition sim_types.h:348
int ctlreq_id_t
Definition sim_peripheral.h:111
#define YASIMAVR_BEGIN_NAMESPACE
Definition sim_globals.h:58
#define YASIMAVR_END_NAMESPACE
Definition sim_globals.h:59
short int_vect_t
Definition sim_types.h:43
Definition arch_avr_timer.cpp:38
Configuration structure for one COM setting.
Definition arch_avr_timer.h:147
COM top
Definition arch_avr_timer.h:151
COM down
Definition arch_avr_timer.h:149
COM bottom
Definition arch_avr_timer.h:150
COM up
Definition arch_avr_timer.h:148
Configuration structure for one compare channel.
Definition arch_avr_timer.h:137
regbit_t rb_force
Definition arch_avr_timer.h:141
vector_config_t vector
Definition arch_avr_timer.h:139
regbit_t rb_mode
Definition arch_avr_timer.h:140
reg_addr_t reg_oc
Definition arch_avr_timer.h:138
Configuration structure for clock source/prescaler options.
Definition arch_avr_timer.h:121
unsigned long div
Definition arch_avr_timer.h:123
TimerCounter::TickSource source
Definition arch_avr_timer.h:122
Configuration structure for timer modes.
Definition arch_avr_timer.h:157
bool double_slope
Controls the slope mode , false=single, true=double.
Definition arch_avr_timer.h:167
unsigned int com_variant
Controls which COM config variant is used.
Definition arch_avr_timer.h:171
Top top
Controls the counter value used for TOP.
Definition arch_avr_timer.h:161
OVF ovf
Controls when the OVerFlow interrupt flag is set.
Definition arch_avr_timer.h:159
unsigned int fixed_top_exp
Controls the fixed top value when top is set to Top_OnFixed. The fixed value is (2^n - 1),...
Definition arch_avr_timer.h:163
bool disable_foc
If true, a Forced Output Compare strobe has no effect.
Definition arch_avr_timer.h:169
OCR ocr
Controls when the OC compare values are updated from the registers.
Definition arch_avr_timer.h:165
Configuration structure for one interrupt vector.
Definition arch_avr_timer.h:129
unsigned char bit
Definition arch_avr_timer.h:131
int_vect_t num
Definition arch_avr_timer.h:130
Configuration structure for ArchAVR_Timer.
Definition arch_avr_timer.h:61
reg_addr_t reg_icr
Input compare register address.
Definition arch_avr_timer.h:203
Top
Definition arch_avr_timer.h:83
@ Top_OnCompA
TOP == OCRA.
Definition arch_avr_timer.h:89
@ Top_OnMax
Maximum permitted value (0xFF for 8-bits counter, 0xFFFF for 16-bits counters)
Definition arch_avr_timer.h:85
@ Top_OnIC
TOP == ICR.
Definition arch_avr_timer.h:91
@ Top_OnFixed
Fixed value.
Definition arch_avr_timer.h:87
regbit_compound_t rbc_mode
Regbit for the timer mode control register.
Definition arch_avr_timer.h:199
vector_config_t vect_icr
Input Capture Interrupt configuration.
Definition arch_avr_timer.h:211
regbit_t rb_clock
Regbit for the clock/prescaler configuration register.
Definition arch_avr_timer.h:197
std::vector< mode_config_t > modes
List of the timer mode configurations.
Definition arch_avr_timer.h:183
std::vector< OC_config_t > oc_channels
List of Output Compare channel configurations.
Definition arch_avr_timer.h:194
std::vector< COM_config_t > COM_variant_t
Array of COM variants.
Definition arch_avr_timer.h:175
OCR
Definition arch_avr_timer.h:97
@ OCR_UpdateOnTop
Update on TOP event.
Definition arch_avr_timer.h:101
@ OCR_Unbuffered
Immediate update.
Definition arch_avr_timer.h:99
@ OCR_UpdateOnBottom
Update on BOTTOM event.
Definition arch_avr_timer.h:103
bool is_16bits
Boolean indicating if the timer is 8-bits (false) or 16-bits (true)
Definition arch_avr_timer.h:178
COM
Definition arch_avr_timer.h:67
@ COM_NoChange
No change to the output.
Definition arch_avr_timer.h:69
@ COM_Toggle
Toggle the output.
Definition arch_avr_timer.h:71
@ COM_Set
Set the output (set to one)
Definition arch_avr_timer.h:75
@ COM_ToggleA
Toggle the output for channel A, no change for other channels.
Definition arch_avr_timer.h:77
@ COM_Clear
Clear the output (set to zero)
Definition arch_avr_timer.h:73
reg_addr_t reg_int_flag
Interrupt flag register address.
Definition arch_avr_timer.h:207
std::vector< COM_variant_t > com_modes
Definition arch_avr_timer.h:191
OVF
Definition arch_avr_timer.h:109
@ OVF_SetOnTop
OVF == TOP.
Definition arch_avr_timer.h:113
@ OVF_SetOnBottom
OVF == BOTTOM.
Definition arch_avr_timer.h:115
@ OVF_SetOnMax
OVF == MAX.
Definition arch_avr_timer.h:111
vector_config_t vect_ovf
Overflow Interrupt configuration.
Definition arch_avr_timer.h:209
reg_addr_t reg_int_enable
Interrupt enable register address.
Definition arch_avr_timer.h:205
reg_addr_t reg_cnt
Counter register address.
Definition arch_avr_timer.h:201
std::vector< clock_config_t > clocks
List of clock source configurations.
Definition arch_avr_timer.h:181
Definition sim_peripheral.h:252
Definition sim_peripheral.h:237
Definition sim_ioreg.h:39
Definition sim_signal.h:39