simulavr  1.1.0
hwspi.h
Go to the documentation of this file.
1  /*
2  ****************************************************************************
3  *
4  * simulavr - A simulator for the Atmel AVR family of microcontrollers.
5  * Copyright (C) 2001, 2002, 2003 Klaus Rudolph
6  * Copyright (C) 2009 Onno Kortmann
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  ****************************************************************************
22  *
23  * $Id$
24  */
25 
26 #ifndef HWSPI
27 #define HWSPI
28 
29 #include "hardware.h"
30 #include "pinatport.h"
31 #include "rwmem.h"
32 #include "traceval.h"
33 
34 class AvrDevice;
35 class HWIrqSystem;
36 
38 class HWSpi: public Hardware, public TraceValueRegister {
39 
40  private:
43  unsigned char shift_in;
45  unsigned char data_read;
47  unsigned char data_write;
48  unsigned char spsr;
49  unsigned char spcr;
50 
53 
58  unsigned int irq_vector;
59 
62  int clkdiv;
63 
65  void updatePrescaler();
66 
69  bool spsr_read;
70 
71  // For edge detection of SCK in slave mode
72  bool oldsck;
73 
75  int bitcnt;
76 
78  unsigned clkcnt;
79 
83  bool mega_mode;
84 
86  bool finished;
87 
89  void txbit(const int bitpos);
90  void rxbit(const int bitpos);
91 
93  void trxend();
94 
96  void spdr_access();
97 
98  public:
99  HWSpi(AvrDevice *core,
100  HWIrqSystem *,
101  PinAtPort mosi,
102  PinAtPort miso,
103  PinAtPort sck,
104  PinAtPort ss,
105  unsigned int irq_vec,
106  bool mega_mode=true);
107 
108  unsigned int CpuCycle();
109  void Reset();
110 
111  void SetSPDR(unsigned char val);
112  void SetSPSR(unsigned char val); // it is read only! but we need it for rwmem-> only tell that we have an error
113  void SetSPCR(unsigned char val);
114 
115  unsigned char GetSPDR();
116  unsigned char GetSPSR();
117  unsigned char GetSPCR();
118 
119  void ClearIrqFlag(unsigned int);
120 
122  spsr_reg,
123  spcr_reg;
124 };
125 
126 #endif
unsigned char data_write
Definition: hwspi.h:47
Basic AVR device, contains the core functionality.
Definition: avrdevice.h:66
unsigned int CpuCycle()
Definition: hwspi.cpp:239
void trxend()
Handle end of transmission if necessary.
Definition: hwspi.cpp:222
bool oldsck
Definition: hwspi.h:72
unsigned char spsr
Definition: hwspi.h:48
void spdr_access()
Called for all SPDR access to clear the WCOL and SPIF flags if needed.
Definition: hwspi.cpp:57
unsigned char GetSPDR()
Definition: hwspi.cpp:67
void ClearIrqFlag(unsigned int)
Definition: hwspi.cpp:200
PinAtPort MOSI
Definition: hwspi.h:54
int bitcnt
Definition: hwspi.h:75
void SetSPCR(unsigned char val)
Definition: hwspi.cpp:120
unsigned char spcr
Definition: hwspi.h:49
HWSpi(AvrDevice *core, HWIrqSystem *, PinAtPort mosi, PinAtPort miso, PinAtPort sck, PinAtPort ss, unsigned int irq_vec, bool mega_mode=true)
Definition: hwspi.cpp:166
void Reset()
Definition: hwspi.cpp:194
void txbit(const int bitpos)
Send/receive one bit.
Definition: hwspi.cpp:209
PinAtPort SCK
Definition: hwspi.h:56
void updatePrescaler()
Takes info from registers and updates clkdiv.
Definition: hwspi.cpp:95
unsigned char data_read
Definition: hwspi.h:45
IOReg< HWSpi > spdr_reg
Definition: hwspi.h:121
bool finished
finished transmission?
Definition: hwspi.h:86
IOReg< HWSpi > spcr_reg
Definition: hwspi.h:121
Build a register for TraceValue&#39;s.
Definition: traceval.h:442
PinAtPort SS
Definition: hwspi.h:57
unsigned char GetSPSR()
Definition: hwspi.cpp:72
unsigned char shift_in
Definition: hwspi.h:43
unsigned clkcnt
Definition: hwspi.h:78
void SetSPDR(unsigned char val)
Definition: hwspi.cpp:81
bool spsr_read
Definition: hwspi.h:69
PinAtPort MISO
Definition: hwspi.h:55
unsigned int irq_vector
Definition: hwspi.h:58
Definition: hwspi.h:38
void SetSPSR(unsigned char val)
Definition: hwspi.cpp:106
void rxbit(const int bitpos)
Definition: hwspi.cpp:215
unsigned char GetSPCR()
Definition: hwspi.cpp:77
HWIrqSystem * irq
Definition: hwspi.h:52
IOReg< HWSpi > spsr_reg
Definition: hwspi.h:121
bool mega_mode
Definition: hwspi.h:83
AvrDevice * core
Definition: hwspi.h:51
int clkdiv
Definition: hwspi.h:62