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

Subversion Repositories igor

[/] [igor/] [trunk/] [avr/] [bus-master/] [main.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 atypic
#include <stdio.h>
2
#include <avr/io.h>
3
 
4
#include "uart.h"
5
#include "busmaster.h"
6
 
7
int main(void);
8
void print_usage(void);
9
uint8_t read_hex_byte(void);
10
void read_single_address(uint8_t addr);
11
void write_single_address(uint8_t addr, uint32_t data);
12
void dump_memory(void);
13
static int uart_putchar(char c, FILE *stream);
14
 
15
static FILE stderrtest = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
16
 
17
char msg[] = "Hello world\n";
18
 
19
static int uart_putchar(char c, FILE *stream) {
20
 
21
        if ( c == '\n' ) uart_putchar('\r', stream);
22
        send_byte(c);
23
        return 0;
24
}
25
 
26
int main () {
27
//      char msg[] = "lol\n";
28
        uint8_t input;
29
        uint8_t param;
30
 
31
        XDIV = (0<<XDIVEN);
32
        init_usart();
33
        init_fpgabus();
34
 
35
        stdout = stdin = &stderrtest;
36
//      send_byte('B');
37
//      send_byte('C');
38
//      send_byte('D');
39
//      printString(msg);
40
        //vfprintf(stderr,"%s",msg);
41
        printf("\n");
42
        print_usage();
43
 
44
        while (1) {
45
                        input = read_byte();
46
                        printf("%c\n", input);
47
                        switch (input) {
48
                                case 'r':
49
                                        printf("Input address (hex) : 0x");
50
                                        param = read_hex_byte();
51
                                        send_byte('\r');
52
                                        send_byte('\n');
53
                                        read_single_address(param);
54
                                        break;
55
                                case 'd':
56
                                        dump_memory();
57
                                        break;
58
                                case 'w':
59
                                        printf("Input address (hex) : 0x");
60
                                        param = read_hex_byte();
61
                                        send_byte('\r');
62
                                        send_byte('\n');
63
                                        {
64
                                        volatile uint32_t data;
65
                                        printf("Input data (hex) : 0x");
66
 
67
                                        data = ((uint32_t)read_hex_byte())<<24;
68
                                        data |= ((uint32_t)read_hex_byte())<<16;
69
                                        data |= ((uint32_t)read_hex_byte())<<8;
70
                                        data |= read_hex_byte();
71
 
72
                                        send_byte('\r');
73
                                        send_byte('\n');
74
 
75
                                        write_single_address(param,data);
76
                                        }
77
                                default:
78
                                        print_usage();
79
                                        break;
80
                        }
81
 
82
        }
83
}
84
 
85
void print_usage() {
86
        printf("AVR-FPGA Bus master\n");
87
        printf("r) Read single address\n");
88
        printf("d) Dump entire memory\n");
89
        printf("w) Write single address\n");
90
        printf("\n");
91
}
92
 
93
void dump_memory() {
94
        for ( uint8_t i = 0 ; i < 0xFF; i++ ) {
95
                read_single_address(i);
96
        }
97
        // If we looped <= 0xFF the loop would run forever because of the limited range of i
98
        read_single_address(0xFF);
99
}
100
 
101
void read_single_address(uint8_t addr) {
102
        uint32_t data;
103
        printf("0x%02x\t",addr);
104
        data = bus_read(addr);
105
        printf("0x%04x%04x\n",(unsigned int)(data>>16), (unsigned int)data);
106
}
107
 
108
void write_single_address(uint8_t addr, uint32_t data) {
109
        printf("0x%02x\t",addr);
110
        bus_write(addr,data);
111
        printf("0x%04x%04x\n",(unsigned int)(data>>16), (unsigned int)data);
112
}
113
 
114
/* Lots of duplicate code.. */
115
uint8_t read_hex_byte() {
116
        uint8_t result = 0;
117
        uint8_t preconvert;
118
 
119
 
120
        preconvert = read_byte();
121
        send_byte(preconvert);
122
        if ( preconvert >= '0' && preconvert <= '9' ) {
123
                preconvert -= '0';
124
        } else if ( preconvert >= 'A' && preconvert <= 'F' ) {
125
                preconvert -= 'A'-10;
126
        } else if ( preconvert >= 'a' && preconvert <= 'f' ) {
127
                preconvert -= 'a'-10;
128
        } else {
129
                printf("invalid input\n");
130
                return read_hex_byte();
131
        }
132
 
133
        result = preconvert<<4;
134
 
135
        preconvert = read_byte();
136
        send_byte(preconvert);
137
        if ( preconvert >= '0' && preconvert <= '9' ) {
138
                preconvert -= '0';
139
        } else if ( preconvert >= 'A' && preconvert <= 'F' ) {
140
                preconvert -= 'A'-10;
141
        } else if ( preconvert >= 'a' && preconvert <= 'f' ) {
142
                preconvert -= 'a'-10;
143
        } else {
144
                printf("invalid input\n");
145
                return read_hex_byte();
146
        }
147
 
148
        result |= (preconvert&0x0F);
149
        return result;
150
}

powered by: WebSVN 2.1.0

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