4
Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
6
This file is part of simavr.
8
simavr 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.
13
simavr 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.
18
You should have received a copy of the GNU General Public License
19
along with simavr. If not, see <http://www.gnu.org/licenses/>.
26
static avr_cycle_count_t avr_spi_raise( avr_spi_t* p)
28
if( avr_regbit_get(p->bit_bang.avr, p->spe) )
30
avr_raise_interrupt(p->bit_bang.avr, &p->spi);
31
if( avr_regbit_get(p->bit_bang.avr, p->mstr) )
33
avr_raise_irq(p->io.irq + SPI_IRQ_OUTPUT, p->output_data_register);
39
static uint32_t avr_spi_transfer_finished( uint32_t data, void* param )
41
avr_spi_t* p = (avr_spi_t *)param;
42
if( p->bit_bang.clk_generate ) avr_bitbang_stop( &(p->bit_bang) );
44
p->input_data_register = data & 0xFF;
47
// MOSI PIN High when idle
48
avr_raise_irq(avr_io_getirq(p->bit_bang.avr, AVR_IOCTL_IOPORT_GETIRQ( p->p_mosi.port ), p->p_mosi.pin), 1);
52
static void avr_spi_bitbang_switch_mode( avr_spi_t* p, uint8_t master )
56
p->bit_bang.p_in = p->p_miso;
57
p->bit_bang.p_out = p->p_mosi;
60
irq = avr_io_getirq( p->bit_bang.avr, AVR_IOCTL_IOPORT_GETIRQ(p->p_miso.port), IOPORT_IRQ_DIRECTION_ALL );
61
irq_val = irq->value & (1 << p->p_miso.pin);
62
avr_raise_irq( irq, irq_val );
63
// MOSI PIN High when idle
64
avr_raise_irq(avr_io_getirq(p->bit_bang.avr, AVR_IOCTL_IOPORT_GETIRQ( p->p_mosi.port ), p->p_mosi.pin), 1);
65
p->bit_bang.clk_generate = 1;
69
p->bit_bang.p_in = p->p_mosi;
70
p->bit_bang.p_out = p->p_miso;
74
irq = avr_io_getirq( p->bit_bang.avr, AVR_IOCTL_IOPORT_GETIRQ(p->bit_bang.p_clk.port), IOPORT_IRQ_DIRECTION_ALL );
75
irq_val = irq->value & (1 << p->bit_bang.p_clk.pin);
76
avr_raise_irq( irq, irq_val );
78
irq = avr_io_getirq( p->bit_bang.avr, AVR_IOCTL_IOPORT_GETIRQ(p->p_mosi.port), IOPORT_IRQ_DIRECTION_ALL );
79
irq_val = irq->value & (1 << p->p_mosi.pin);
80
avr_raise_irq( irq, irq_val );
82
irq = avr_io_getirq( p->bit_bang.avr, AVR_IOCTL_IOPORT_GETIRQ(p->p_ss.port), IOPORT_IRQ_DIRECTION_ALL );
83
irq_val = irq->value & (1 << p->p_ss.pin);
84
avr_raise_irq( irq, irq_val );
86
p->bit_bang.clk_generate = 0;
90
static uint8_t avr_spi_read( struct avr_t* avr, avr_io_addr_t addr, void* param)
92
avr_spi_t* p = (avr_spi_t*)param;
93
uint8_t v = p->input_data_register;
94
p->input_data_register = 0;
95
avr_regbit_clear(avr, p->spi.raised);
96
// printf("avr_spi_read = %02x\n", v);
100
static void avr_spi_write( struct avr_t* avr, avr_io_addr_t addr, uint8_t v, void* param)
102
avr_spi_t* p = (avr_spi_t *)param;
104
if (addr == p->r_spdr)
106
/* Clear the SPIF bit. See ATmega164/324/644 manual, Section 18.5.2. */
107
avr_regbit_clear( avr, p->spi.raised );
109
// The byte to be sent should NOT be written there,
110
// the value written could never be read back.
111
//avr_core_watch_write(avr, addr, v);
112
p->output_data_register = v;
113
if (avr->gdb) avr_gdb_handle_watchpoints(avr, addr, AVR_GDB_WATCH_WRITE);
115
if (avr_regbit_get(avr, p->spe))
117
avr_bitbang_stop(&(p->bit_bang));
118
avr_bitbang_reset(avr, &(p->bit_bang));
119
p->bit_bang.data = v;
121
if (avr_regbit_get(avr, p->mstr))
123
p->bit_bang.clk_generate = 1;
124
avr_bitbang_start(&(p->bit_bang));
128
p->bit_bang.clk_generate = 0;
132
else if ((addr == p->r_spcr) || (addr == p->r_spsr))
134
avr_core_watch_write(avr, addr, v);
135
avr_bitbang_stop(&(p->bit_bang));
136
avr_bitbang_reset(avr, &(p->bit_bang));
138
if (avr_regbit_get(avr, p->spe))
140
p->bit_bang.clk_phase = avr_regbit_get( avr, p->cpha );
141
p->bit_bang.clk_pol = avr_regbit_get( avr, p->cpol );
142
p->bit_bang.data_order = avr_regbit_get( avr, p->dord );
144
if( avr_regbit_get( avr, p->mstr ))
146
int clock_divider_ix = (avr_regbit_get(avr, p->spr[0]) | avr_regbit_get(avr, p->spr[1]));
147
int clock_divider = 1;
148
switch( clock_divider_ix )
163
if( avr_regbit_get(avr, p->spr[2]) ) clock_divider /= 2;
165
p->bit_bang.clk_cycles = clock_divider;
166
avr_spi_bitbang_switch_mode( p, 1 );
170
avr_spi_bitbang_switch_mode( p, 0 );
176
static void avr_spi_ss_hook( struct avr_irq_t * irq, uint32_t value, void * param)
178
avr_spi_t* p = (avr_spi_t*)param;
180
if( avr_regbit_get(p->bit_bang.avr, p->mstr) ) // master mode
182
avr_ioport_state_t iostate;
185
avr_ioctl(p->bit_bang.avr, AVR_IOCTL_IOPORT_GETSTATE( p->p_ss.port ), &iostate);
186
dir = ( iostate.ddr >> p->p_ss.pin ) & 1;
189
if( !value ) // other master is active, reset to slave mode
191
avr_bitbang_stop( &(p->bit_bang) );
192
avr_bitbang_reset( p->bit_bang.avr, &(p->bit_bang) );
193
avr_regbit_setto( p->bit_bang.avr, p->mstr, 0);
194
avr_spi_bitbang_switch_mode( p, 0);
203
avr_bitbang_stop(&(p->bit_bang));
204
avr_bitbang_reset(p->bit_bang.avr, &(p->bit_bang));
209
static void avr_spi_irq_input( struct avr_irq_t * irq, uint32_t value, void * param)
211
avr_spi_t * p = (avr_spi_t *)param;
212
avr_t * avr = p->io.avr;
214
// check to see if receiver is enabled
215
if (!avr_regbit_get(avr, p->spe)) return;
217
// double buffer the input.. ?
218
p->input_data_register = value;
219
avr_raise_interrupt(avr, &p->spi);
222
// 'output' the byte only when we received one...
223
if (!avr_regbit_get(avr, p->mstr)) avr_raise_irq(p->io.irq + SPI_IRQ_OUTPUT, avr->data[p->r_spdr]);
226
void avr_spi_reset( struct avr_io_t* io)
228
avr_spi_t* p = (avr_spi_t*)io;
229
avr_bitbang_reset(p->bit_bang.avr, &(p->bit_bang));
230
avr_irq_register_notify(p->io.irq + SPI_IRQ_INPUT, avr_spi_irq_input, p);
233
static const char * irq_names[SPI_IRQ_COUNT] = {
234
[SPI_IRQ_INPUT] = "8<in",
235
[SPI_IRQ_OUTPUT] = "8<out",
238
static avr_io_t _io = {
240
.reset = avr_spi_reset,
241
.irq_names = irq_names,
244
void avr_spi_init( avr_t* avr, avr_spi_t* p)
248
p->bit_bang.avr = avr;
249
p->bit_bang.callback_transfer_finished = avr_spi_transfer_finished;
250
p->bit_bang.callback_param = p;
251
p->bit_bang.buffer_size = 8;
253
avr_register_io(avr, &p->io);
254
avr_register_vector(avr, &p->spi);
255
// allocate this module's IRQ
256
avr_io_setirqs(&p->io, AVR_IOCTL_SPI_GETIRQ(p->name), SPI_IRQ_COUNT, NULL);
258
avr_register_io_write(avr, p->r_spdr, avr_spi_write, p);
259
avr_register_io_write(avr, p->r_spcr, avr_spi_write, p);
260
avr_register_io_write(avr, p->r_spsr, avr_spi_write, p);
261
avr_register_io_read(avr, p->r_spdr, avr_spi_read, p);
263
avr_irq_register_notify( avr_io_getirq(p->bit_bang.avr, AVR_IOCTL_IOPORT_GETIRQ( p->p_ss.port ), p->p_ss.pin),