sim_twi.h Source File

yasimavr: sim_twi.h Source File
yasimavr
Loading...
Searching...
No Matches
sim_twi.h
Go to the documentation of this file.
1/*
2 * sim_twi.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_TWI_H__
25#define __YASIMAVR_TWI_H__
26
27#include "../core/sim_signal.h"
28#include "../core/sim_cycle_timer.h"
29
31
32
33//=======================================================================================
66#define AVR_CTLREQ_TWI_BUS_ERROR (AVR_CTLREQ_BASE + 1)
67
70
71
72//=======================================================================================
73
79namespace TWI {
80
90
102
191
192
201
202public:
203
204 EndPoint();
205 virtual ~EndPoint() = default;
206
207 void line_state_changed(TWI::Line line, bool dig_state);
208
209 bool get_clock_drive() const;
210 bool get_data_drive() const;
211
212protected:
213
214 virtual void clock_level_changed(bool level) = 0;
215 virtual void data_level_changed(bool level) = 0;
216 virtual void set_line_state(TWI::Line line, bool dig_state) = 0;
217
218 void set_clock_drive(bool level);
219 bool get_clock_level() const;
220 void set_data_drive(bool level);
221 bool get_data_level() const;
222
223private:
224
225 bool m_clock_drive;
226 bool m_clock_level;
227 bool m_data_drive;
228 bool m_data_level;
229
230};
231
232
233inline bool EndPoint::get_clock_drive() const
234{
235 return m_clock_drive;
236}
237
238inline bool EndPoint::get_clock_level() const
239{
240 return m_clock_level;
241}
242
243inline bool EndPoint::get_data_drive() const
244{
245 return m_data_drive;
246}
247
248inline bool EndPoint::get_data_level() const
249{
250 return m_data_level;
251}
252
253
254//=======================================================================================
255
266
267public:
268
294
295 Client();
296
297 void init(CycleManager& cycle_manager);
298
299 State state() const;
300
301 void set_enabled(bool enabled);
302 bool enabled() const;
303
304 void reset();
305
306 bool active() const;
307 bool clock_hold() const;
308 unsigned char rw() const;
309 bool ack() const;
310
311 bool set_ack(bool ack);
312 bool start_data_tx(uint8_t data);
313 bool start_data_rx();
314
315 Signal& signal();
316
317 virtual cycle_count_t next(cycle_count_t when) override;
318
319protected:
320
321 virtual void clock_level_changed(bool level) override;
322 virtual void data_level_changed(bool level) override;
323
324private:
325
326 State m_state;
327 uint8_t m_shifter;
328 bool m_ack;
329 int m_bitcount;
330 bool m_hold;
331 CycleManager* m_cycle_manager;
332 int m_deferred_drive;
333 Signal m_signal;
334
335 void set_state(State state);
336 void defer_clock_release();
337 void defer_data_drive(bool level);
338
339};
340
345{
346 return m_state;
347}
348
352inline bool Client::enabled() const
353{
354 return m_state != State_Disabled;
355}
356
362inline bool Client::ack() const
363{
364 return m_ack;
365}
366
371{
372 return m_signal;
373}
374
375
376//=======================================================================================
377
388
389public:
390
420
421 Host();
422
423 void init(CycleManager& manager);
424
425 State state() const;
426
427 void set_enabled(bool enabled);
428 bool enabled() const;
429
430 void reset();
431
432 void set_bit_delay(cycle_count_t delay);
433
434 bool bus_busy() const;
435 bool active() const;
436 bool clock_hold() const;
437 bool clock_stretched() const;
438 unsigned char rw() const;
439 bool ack() const;
440
441 bool start_transfer();
442 bool set_address(uint8_t addr_rw);
443 bool start_data_tx(uint8_t data);
444 bool start_data_rx();
445 bool set_ack(bool ack);
446 bool stop_transfer();
447
448 Signal& signal();
449
450 virtual cycle_count_t next(cycle_count_t when) override;
451
452protected:
453
454 virtual void clock_level_changed(bool level) override;
455 virtual void data_level_changed(bool level) override;
456
457private:
458
459 State m_state;
460 uint16_t m_flags;
461 uint8_t m_shifter;
462 int m_step;
463 int m_bitcount;
464 uint8_t m_addr_rw;
465 bool m_ack;
466 cycle_count_t m_step_delay;
467 uint8_t m_pattern;
468 CycleManager* m_cycle_manager;
469 bool m_hold;
470 Signal m_signal;
471
472 void set_state(State state);
473 void apply_pattern();
474 void process_state_and_reschedule();
475 bool process_state(bool inc_step);
476 void transition_state();
477
478};
479
483inline bool Host::enabled() const
484{
485 return m_state != State_Disabled;
486}
487
492{
493 return m_state;
494}
495
501inline bool Host::ack() const
502{
503 return m_ack;
504}
505
510inline unsigned char Host::rw() const
511{
512 return m_addr_rw & 0x01;
513}
514
518inline bool Host::clock_hold() const
519{
520 return m_hold;
521}
522
527{
528 return m_signal;
529}
530
531}; //namespace TWI
532
533
535
536#endif //__YASIMAVR_TWI_H__
Definition sim_cycle_timer.h:134
Definition sim_cycle_timer.h:41
Signalling framework class.
Definition sim_signal.h:97
Base abstract definition for a TWI client. This class implements the basic state machine to interface...
Definition sim_twi.h:265
Signal & signal()
Definition sim_twi.h:370
bool enabled() const
Definition sim_twi.h:352
bool ack() const
Definition sim_twi.h:362
State state() const
Definition sim_twi.h:344
State
Definition sim_twi.h:269
@ State_Disabled
Client disabled
Definition sim_twi.h:271
@ State_DataRxAck
Data received, pending/sending ACK bit.
Definition sim_twi.h:289
@ State_DataTx
Read request ACKed, in TX mode, pending/transmitting data.
Definition sim_twi.h:283
@ State_AddressWAck
Pending/transmitting a ACK/NACK for a Write request.
Definition sim_twi.h:281
@ State_DataTxAck
Data sent, receiving ACK bit from the host.
Definition sim_twi.h:285
@ State_DataRx
Write request ACKed, in RX mode, pending/receiving data.
Definition sim_twi.h:287
@ State_AddressRx
Receving a Address/RW byte.
Definition sim_twi.h:277
@ State_Idle
Client idle
Definition sim_twi.h:273
@ State_AddressRAck
Pending/transmitting a ACK/NACK for a read request.
Definition sim_twi.h:279
@ State_Start
Receiving a Start condition.
Definition sim_twi.h:275
@ State_Count
Total number of states.
Definition sim_twi.h:292
An endpoint connected to a TWI bus. Represents a device connected to a TWI bus model and implements t...
Definition sim_twi.h:200
bool get_data_level() const
Definition sim_twi.h:248
virtual ~EndPoint()=default
bool get_data_drive() const
Definition sim_twi.h:243
bool get_clock_drive() const
Definition sim_twi.h:233
virtual void data_level_changed(bool level)=0
bool get_clock_level() const
Definition sim_twi.h:238
virtual void clock_level_changed(bool level)=0
virtual void set_line_state(TWI::Line line, bool dig_state)=0
Base abstract definition for a TWI host. This class implements the basic state machine to interface a...
Definition sim_twi.h:387
State state() const
Definition sim_twi.h:491
bool clock_hold() const
Definition sim_twi.h:518
State
Definition sim_twi.h:391
@ State_Disabled
Host disabled
Definition sim_twi.h:393
@ State_Idle
Host idle
Definition sim_twi.h:395
@ State_DataRxAck
Sending a ACK/NACK bit after receiving a data byte.
Definition sim_twi.h:409
@ State_Start
Sending a START condition.
Definition sim_twi.h:397
@ State_AddressAck
Waiting for a ACK/NACK bit after an address/RW byte.
Definition sim_twi.h:401
@ State_DataRx
Receiving a data byte.
Definition sim_twi.h:407
@ State_Stop
Sending a STOP condition.
Definition sim_twi.h:411
@ State_DataTxAck
Waiting for a ACK/NACK bit after sending a data byte.
Definition sim_twi.h:405
@ State_BusBusy
START condition by another host detected on the bus.
Definition sim_twi.h:413
@ State_DataTx
Sending a data byte.
Definition sim_twi.h:403
@ State_ArbLost
Arbitration lost.
Definition sim_twi.h:415
@ State_AddressTx
Sending an address/RW byte.
Definition sim_twi.h:399
Signal & signal()
Definition sim_twi.h:526
unsigned char rw() const
Definition sim_twi.h:510
bool enabled() const
Definition sim_twi.h:483
bool ack() const
Definition sim_twi.h:501
Common enums and signal definitions for TWI classes.
Definition sim_twi.h:79
BusState
Definition sim_twi.h:94
@ Bus_Idle
The bus is not in use.
Definition sim_twi.h:96
@ Bus_Owned
The current interface (host only) owns the bus.
Definition sim_twi.h:100
@ Bus_Busy
The bus is busy and owned by a host different from the current interface.
Definition sim_twi.h:98
SignalId
Definition sim_twi.h:106
@ Signal_ArbitrationLost
Definition sim_twi.h:176
@ Signal_BusStateChanged
Definition sim_twi.h:118
@ Signal_DataAckReceived
Definition sim_twi.h:159
@ Signal_AddressReceived
Definition sim_twi.h:143
@ Signal_Stop
Definition sim_twi.h:189
@ Signal_DataSent
Definition sim_twi.h:153
@ Signal_Start
Definition sim_twi.h:125
@ Signal_StateChanged
Definition sim_twi.h:111
@ Signal_AddressSent
Definition sim_twi.h:137
@ Signal_AddressStandby
Definition sim_twi.h:131
@ Signal_DataStandby
Definition sim_twi.h:148
@ Signal_DataAckSent
Definition sim_twi.h:170
@ Signal_BusCollision
Definition sim_twi.h:181
@ Signal_DataReceived
Definition sim_twi.h:165
Line
Definition sim_twi.h:84
@ Line_Clock
Index for the SCL line.
Definition sim_twi.h:86
@ Line_Data
Index for the SDA line.
Definition sim_twi.h:88
#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 long long cycle_count_t
Definition sim_types.h:40