1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
/*
****************************************************************************
*
* simulavr - A simulator for the Atmel AVR family of microcontrollers.
* Copyright (C) 2001, 2002, 2003 Klaus Rudolph
* Copyright (C) 2009 Onno Kortmann
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
****************************************************************************
*
* $Id$
*/
#include <assert.h>
#include <stdio.h>
#include "hwspi.h"
#include "flash.h"
#include "avrdevice.h"
#include "traceval.h"
#include "irqsystem.h"
#include "avrerror.h"
//configuration
#define SPIE 0x80
#define SPE 0x40
#define DORD 0x20 ///< "When the DORD bit is written to one, the LSB of the data word is transmitted first."
#define MSTR 0x10
#define CPOL 0x08 ///< "When this bit is written to one, SCK is high when idle."
#define CPHA 0x04 ///< When this bit is written to one, output is setup at leading edge and input is sampled trailing edge.
#define SPR1 0x02
#define SPR0 0x01
//status
#define SPIF 0x80
#define WCOL 0x40
#define SPI2X 0x01 //only on mega devices speed x 2
/* SPI verbosity level
FIXME: Make this configurable through the command line interface. */
#define SPI_VERBOSE 0
using namespace std;
void HWSpi::spdr_access() {
if (spsr_read) {
// if status is read with SPIF == 1
//we can remove the SPIF and WCOL flag after reading
//data register
spsr&=~(SPIF|WCOL);
spsr_read=false;
}
}
unsigned char HWSpi::GetSPDR() {
spdr_access();
return data_read;
}
unsigned char HWSpi::GetSPSR() {
spsr_read=true;
return spsr;
}
unsigned char HWSpi::GetSPCR() {
return spcr;
}
void HWSpi::SetSPDR(unsigned char val) {
spdr_access();
data_write=val;
if (spcr & MSTR) { // mster mode?
if (bitcnt<8) {
spsr|=WCOL; // not yet ready -> Write Collision
} else {
bitcnt=0;
finished=false;
clkcnt=0;
}
}
}
void HWSpi::updatePrescaler() {
int fac2x=(spsr&SPI2X) ? 1 : 2;
switch (spcr & (SPR1|SPR0)) {
case 0: clkdiv=1; break;
case SPR0: clkdiv=4; break;
case SPR1: clkdiv=16; break;
case SPR1|SPR0: clkdiv=32; break;
}
clkdiv*=fac2x;
}
void HWSpi::SetSPSR(unsigned char val) {
if (mega_mode) {
spsr&=~SPI2X;
spsr|=val&SPI2X;
updatePrescaler();
} else {
((core->trace_on) ?
(traceOut) : (cerr))
<< "spsr is read only! (0x" << hex << core->PC << " = " <<
core->Flash->GetSymbolAtAddress(core->PC) << ")" << endl;
}
}
void HWSpi::SetSPCR(unsigned char val) {
spcr=val;
if ( spcr & SPE) { //SPI is enabled
core->AddToCycleList(this);
if (spcr & MSTR) { //master
MISO.SetUseAlternateDdr(1);
MISO.SetAlternateDdr(0); //always input
MOSI.SetUseAlternatePortIfDdrSet(1);
/* according to the graphics in the atmega8 datasheet, p.132
(10/06), MOSI is high when idle. FIXME: check whether
this applies to real hardware. */
MOSI.SetAlternatePort(1);
SCK.SetAlternatePort(spcr & CPOL);
SCK.SetUseAlternatePortIfDdrSet(1);
assert(SCK.GetPin().outState == ((spcr & CPOL) ? Pin::HIGH : Pin::LOW));
assert(SCK.GetPin().outState == ((spcr & CPOL) ? Pin::HIGH : Pin::LOW));
} else { //slave
MISO.SetUseAlternatePortIfDdrSet(1);
MOSI.SetUseAlternateDdr(1);
MOSI.SetAlternateDdr(0);
SCK.SetUseAlternateDdr(1);
SCK.SetAlternateDdr(0);
SS.SetUseAlternateDdr(1);
SS.SetAlternateDdr(0);
}
} else { //Spi is off so unset alternate pin functions
/* FIXME: Check whether these will be really tied
to reset state as long as the SPI is off. Check
the switch on/off behaviour of the SPI interface! */
bitcnt=8;
finished=false;
core->RemoveFromCycleList(this);
MOSI.SetUseAlternatePortIfDdrSet(0);
MISO.SetUseAlternatePortIfDdrSet(0);
SCK.SetUseAlternatePortIfDdrSet(0);
MOSI.SetUseAlternateDdr(0);
MISO.SetUseAlternateDdr(0);
SCK.SetUseAlternateDdr(0);
SS.SetUseAlternateDdr(0);
}
updatePrescaler();
}
HWSpi::HWSpi(AvrDevice *_c,
HWIrqSystem *_irq,
PinAtPort mosi,
PinAtPort miso,
PinAtPort sck,
PinAtPort ss,
unsigned int ivec,
bool mm) :
Hardware(_c), TraceValueRegister(_c, "SPI"),
core(_c), irq(_irq),
MOSI(mosi), MISO(miso), SCK(sck), SS(ss),
irq_vector(ivec), mega_mode(mm),
spdr_reg(this, "SPDR", this, &HWSpi::GetSPDR, &HWSpi::SetSPDR),
spsr_reg(this, "SPSR", this, &HWSpi::GetSPSR, &HWSpi::SetSPSR),
spcr_reg(this, "SPCR", this, &HWSpi::GetSPCR, &HWSpi::SetSPCR)
{
irq->DebugVerifyInterruptVector(ivec, this);
bitcnt=8;
finished=false;
trace_direct(this, "shift_in", &shift_in);
trace_direct(this, "data_read", &data_read);
trace_direct(this, "data_write", &data_write);
trace_direct(this, "sSPSR", &spsr);
trace_direct(this, "sSPCR", &spcr);
Reset();
}
void HWSpi::Reset() {
SetSPCR(0);
spsr=0;
data_write=data_read=shift_in=0;
}
void HWSpi::ClearIrqFlag(unsigned int vector) {
if (vector==irq_vector) {
spsr&=~SPIF;
irq->ClearIrqFlag(irq_vector);
} else {
cerr << "WARNING: There is HWSPI called to get a irq vector which is not assigned for!?!?!?!?";
}
}
void HWSpi::txbit(const int bitpos) {
// set next output bit
PinAtPort *out=(spcr & MSTR) ? &MOSI : &MISO;
out->SetAlternatePort(data_write&(1<<bitpos));
}
void HWSpi::rxbit(const int bitpos) {
// sample input
bool bit=(spcr & MSTR) ? MISO : MOSI;
if (bit)
shift_in|=(1<<bitpos);
}
void HWSpi::trxend() {
if (finished) {
finished=false;
if (core->trace_on && SPI_VERBOSE)
traceOut << "SPI: READ " << int(shift_in) << endl;
/* set also data_write to allow continuous shifting
when slave. */
data_write=data_read=shift_in;
spsr|=SPIF;
if (spcr&SPIE) {
irq->SetIrqFlag(this, irq_vector);
}
spsr_read=false;
}
}
unsigned int HWSpi::CpuCycle() {
if ((spcr & SPE) == 0) // active at all?
return 0;
int bitpos=(spcr&DORD) ? bitcnt : 7-bitcnt;
int bitpos_prec=(spcr&DORD) ? bitcnt-1 : 8-bitcnt;
if (core->trace_on && SPI_VERBOSE) {
traceOut << "SPI: " << bitcnt << ", " << bitpos << ", " << clkcnt << endl;
}
if (spcr & MSTR) {
/* Check whether we're externally driven into slave mode.
FIXME: It is unclear at least from mega8 docs if this behaviour is
also right when the SPI is inactive!*/
if (! SS.GetDdr() && ! SS) {
SetSPCR(spcr & ~MSTR);
// request interrupt
spsr |= SPIF;
if (spcr&SPIE) {
irq->SetIrqFlag(this, irq_vector);
}
bitcnt = 8; // slave and idle
finished=false;
clkcnt=0;
}
if ((clkcnt%clkdiv) == 0){ // TRX bits
if (bitcnt < 8) {
if (bitcnt == 0)
shift_in = 0;
switch ((clkcnt/clkdiv)&1) {
case 0:
// set idle clock
SCK.SetAlternatePort(spcr&CPOL);
// late phase (for last bit)?
if (spcr&CPHA) {
if (bitcnt) {
rxbit(bitpos_prec);
}
} else {
txbit(bitpos);
}
break;
case 1:
// set valid clock
SCK.SetAlternatePort(!(spcr&CPOL));
if (spcr&CPHA) {
txbit(bitpos);
} else {
rxbit(bitpos);
}
bitcnt++;
break;
}
finished = (bitcnt==8);
} else if (finished) {
if (spcr&CPHA) {
rxbit(bitpos_prec);
}
trxend();
// set idle clock
SCK.SetAlternatePort(spcr&CPOL);
// set idle MOSI (high if CPHA==0)
if (!(spcr&CPHA))
MOSI.SetAlternatePort(1);
}
}
} else {
// possible slave mode
if (SS) {
// slave selected lifted-> force end of transmission
bitcnt=8;
} else {
// Slave mode
if (bitcnt == 8) {
bitcnt = 0;
finished = false;
shift_in = 0;
oldsck = SCK;
} else {
/* Set initial bit for CPHA==0 */
if (!(spcr&CPHA)) {
txbit(bitpos);
}
}
if (SCK != oldsck) { // edge detection
bool leading = false; // leading edge clock?
if (spcr&CPOL) {
// leading edge is falling edge
leading = ! SCK;
} else
leading = SCK;
// determine whether we should sample or setup
bool sample = leading ^ ((spcr&CPHA)!=0);
if (sample)
rxbit(bitpos);
else
txbit(bitpos);
if (!leading) {
bitcnt++;
finished = (bitcnt==8);
}
}
trxend();
oldsck = SCK;
}
}
clkcnt++;
return 0;
}
|