OpenCores
URL https://opencores.org/ocsvn/igor/igor/trunk

Subversion Repositories igor

[/] [igor/] [trunk/] [avr/] [eth-test/] [bus.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 atypic
#include <avr/interrupt.h>
2
#include <avr/io.h>
3
#include <stdint.h>
4
 
5
#include "bus.h"
6
#include "global.h"
7
#include "dispatch.h"
8
#include "dev/7seg.h"
9
 
10
 
11
uint32_t read_bus_data(void);
12
void fpga_finish_write(void);
13
void tristate_data_bus(void);
14
 
15
void init_fpgabus(void) {
16
        // Setup the boot port
17
        BOOTEDDDR |= (1<<BOOTED);
18
        BOOTEDPORT &= ~(1<<BOOTED);
19
 
20
        // Tristate databus
21
        DATA0DDR  = 0x00;
22
        DATA0PORT = 0x00;
23
        DATA1DDR  = 0x00;
24
        DATA1PORT = 0x00;
25
        DATA2DDR  = 0x00;
26
        DATA2PORT = 0x00;
27
        DATA3DDR  = 0x00;
28
        DATA3PORT = 0x00;
29
 
30
        // Set addr as input
31
        ADDRLDDR  &= ~0xF0;
32
        ADDRLPORT &= ~0xF0;
33
 
34
        ADDRHDDR  &= ~0xF0;
35
        ADDRHPORT &= ~0xF0;
36
 
37
        // Set RDWR as input
38
        RDWRDDR   &= ~(RD|WR);
39
        RDWRPORT  &= ~(RD|WR);
40
 
41
        // Set RDY as output
42
        RDYDDR    |= (1<<RDY);
43
        // Init high
44
        RDYPORT   |= (1<<RDY);
45
 
46
        /* Setup interrupt pin as input
47
         * ISC11 = 1, ISC10 => Interrupt on falling edge on INT1
48
         */
49
        INTDDR  &= ~(1<<INT1);
50
        INTPORT &= ~(1<<INT1);
51
        EICRA |= (1<<ISC11);
52
        EICRA &= ~(1<<ISC10);
53
        EIMSK |= (1<<INT1);
54
}
55
 
56
 
57
 
58
/* AVR-FGPA COM interrupt routine
59
 * TODO: ADD BURSTMODE operation
60
 */
61
ISR(SIG_INTERRUPT1) {
62
 
63
        uint8_t addr;
64
        uint8_t rdwr;
65
        uint32_t data;
66
 
67
        _delay_ms(0.1);
68
        addr = (ADDRHPIN&0xF0)|((ADDRLPIN>>4)&0x0F);
69
 
70
        rdwr = RDWRPIN&(WR|RD);
71
 
72
        if ( rdwr == (WR|RD)) {
73
                // Both flags a set.. invalid operation. Should probably panic here
74
                // BAEBU BAEBU! 
75
                display_char(16);
76
                while(1);
77
        } else if ( (rdwr&RD) == 0 ) {
78
                // We are reading data
79
                if (dispatch_request_read(addr, &data)) {
80
                        // Failed
81
                        // main should call finish_read when data is available
82
                        return;
83
                } else {
84
                        // The data was avaiable. Finish transfer
85
                        fpga_finish_read(data);
86
                }
87
        } else if ( (rdwr&WR) == 0 ) {
88
                // We are writing data
89
                // XXX: Add writefunction here
90
                data = read_bus_data();
91
                if (dispatch_request_write(addr,data)) {
92
                        // Failed
93
                        //display_char(12);
94
                        // main should call fpga_delayed_write when bufferspace is avaiable
95
                        return;
96
                } else {
97
                        fpga_finish_write();
98
                        return;
99
                }
100
        } else {
101
                // No flags set.. thats odd
102
                display_char(15);
103
 
104
                while(1) ;
105
        }
106
 
107
}
108
/* Output the requested data on the bus
109
 * And complete the transfer with a 4-way handshake
110
 */
111
void fpga_finish_read(uint32_t data) {
112
        // Intermediate stage as per datasheet
113
        DATA0PORT = 0xFF;
114
        DATA1PORT = 0xFF;
115
        DATA2PORT = 0xFF;
116
        DATA3PORT = 0xFF;
117
 
118
        // Set all dataports to output
119
        DATA0DDR  = 0xFF;
120
        DATA1DDR  = 0xFF;
121
        DATA2DDR  = 0xFF;
122
        DATA3DDR  = 0xFF;
123
 
124
        DATA0PORT = (char)(data);
125
        DATA1PORT = (char)(data>>8);
126
        DATA2PORT = (char)(data>>16);
127
        DATA3PORT = (char)(data>>24);
128
 
129
 
130
        // Assert RDY
131
        RDYPORT &= ~(1<<RDY);
132
 
133
        // Spinlock until WR/RD,CE has been deasserted
134
        while (!(RDWRPIN & WR) || !(RDWRPIN & RD) || !(INTPIN & (1<<INT1)));
135
 
136
        _delay_ms(0.1);
137
        // Release the databus
138
        tristate_data_bus();
139
        // Deassert RDY
140
        RDYPORT |= (1<<RDY);
141
        return;
142
 
143
}
144
 
145
uint32_t fpga_delayed_write() {
146
        int32_t data = read_bus_data();
147
        fpga_finish_write();
148
        return data;
149
}
150
 
151
/* Complete with a 4-way handshake */
152
void fpga_finish_write() {
153
        // Assert RDY
154
        RDYPORT &= ~(1<<RDY);
155
 
156
        // Spinlock until WR/RD,CE has been deasserted
157
        while (!(RDWRPIN & WR) || !(RDWRPIN & RD) || !(INTPIN & (1<<INT1)));
158
 
159
        _delay_ms(0.1);
160
        // Deassert RDY
161
        RDYPORT |= (1<<RDY);
162
        return;
163
}
164
 
165
void tristate_data_bus() {
166
        // Tristate databus
167
        DATA0DDR  = 0x00;
168
        DATA0PORT = 0x00;
169
        DATA1DDR  = 0x00;
170
        DATA1PORT = 0x00;
171
        DATA2DDR  = 0x00;
172
        DATA2PORT = 0x00;
173
        DATA3DDR  = 0x00;
174
        DATA3PORT = 0x00;
175
        return;
176
}
177
 
178
uint32_t read_bus_data() {
179
        return ((uint32_t)DATA3PIN<<24)|((uint32_t)DATA2PIN<<16)|((uint32_t)DATA1PIN<<8)|(DATA0PIN);
180
}
181
 
182
/* We use this to syncronize the avr and the FPGA on boot */
183
void avr_online() {
184
        BOOTEDPORT |= (1<<BOOTED);
185
        return;
186
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.