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

Subversion Repositories ftdi_wb_bridge

[/] [ftdi_wb_bridge/] [trunk/] [sw/] [load.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ultra_embe
#include <stdio.h>
2
#include <string.h>
3
#include <stdlib.h>
4
#include <unistd.h>
5
#include "ftdi_hw.h"
6
 
7
//-----------------------------------------------------------------
8
// Defines:
9
//-----------------------------------------------------------------
10
#define CHUNK_SIZE          256
11
#define DEFAULT_FTDI_IFACE  1
12
 
13
//-----------------------------------------------------------------
14
// load_file_to_mem
15
//-----------------------------------------------------------------
16
static uint8_t* load_file_to_mem(const char *filename, long size_override, int *pSize)
17
{
18
    uint8_t *buf = NULL;
19
    FILE *f = fopen(filename, "rb");
20
 
21
    *pSize = 0;
22
 
23
    if (f)
24
    {
25
        long size;
26
 
27
        // Get size of file
28
        fseek(f, 0, SEEK_END);
29
        size = ftell(f);
30
        rewind(f);
31
 
32
        // User overriden file size
33
        if (size_override >= 0)
34
        {
35
            if (size > size_override)
36
                size = size_override;
37
        }
38
 
39
        buf = (uint8_t*)malloc(size);
40
        if (buf)
41
        {
42
            // Read file data into allocated memory
43
            int len = fread(buf, 1, size, f);
44
            if (len != size)
45
            {
46
                free(buf);
47
                buf = NULL;
48
            }
49
            else
50
                *pSize = size;
51
        }
52
        fclose(f);
53
    }
54
 
55
    return buf;
56
}
57
//-----------------------------------------------------------------
58
// upload
59
//-----------------------------------------------------------------
60
static int upload(uint32_t addr, uint8_t *buf, int length)
61
{
62
    int err = 0;
63
    int i;
64
    int size;
65
 
66
    for (i=0;i<length;i+=CHUNK_SIZE)
67
    {
68
        size = (length - i);
69
        if (size > CHUNK_SIZE)
70
            size = CHUNK_SIZE;
71
 
72
        if (ftdi_hw_mem_write(addr, buf, size) != size)
73
        {
74
            fprintf(stderr, "Upload: Error uploading file\n");
75
            err = 1;
76
            break;
77
        }
78
 
79
        addr += CHUNK_SIZE;
80
        buf  += CHUNK_SIZE;
81
 
82
        printf("\r%d%%", (i * 100) / length);
83
        fflush(stdout);
84
    }
85
 
86
    return err;
87
}
88
//-----------------------------------------------------------------
89
// main
90
//-----------------------------------------------------------------
91
int main(int argc, char *argv[])
92
{
93
    int c;
94
    long size_override = -1;
95
    char *filename = NULL;
96
    int help = 0;
97
    int err = 1;
98
    int size;
99
    uint32_t address = 0x0;
100
    uint8_t *buf;
101
    int ftdi_iface = DEFAULT_FTDI_IFACE;
102
 
103
    while ((c = getopt (argc, argv, "f:s:a:i:")) != -1)
104
    {
105
        switch(c)
106
        {
107
            case 'f':
108
                 filename = optarg;
109
                 break;
110
            case 's':
111
                 size_override = strtol(optarg, NULL, 0);
112
                 break;
113
            case 'a':
114
                address = strtoul(optarg, NULL, 0);
115
                break;
116
            case 'i':
117
                 ftdi_iface = (int)strtol(optarg, NULL, 0);
118
                 break;
119
            default:
120
                help = 1;
121
                break;
122
        }
123
    }
124
 
125
    if (help || filename == NULL)
126
    {
127
        fprintf (stderr,"Usage:\n");
128
        fprintf (stderr,"-f filename.bin   = Executable to load (binary)\n");
129
        fprintf (stderr,"-a 0xnnnn         = Address to load to (default to 0x0)\n");
130
        fprintf (stderr,"-i id             = FTDI interface ID (0 = A, 1 = B)\n");
131
        fprintf (stderr,"-s 0xnnnn         = Size override\n");
132
 
133
        exit(-1);
134
    }
135
 
136
    // Try and communicate with FTDI interface
137
    if (ftdi_hw_init(ftdi_iface) != 0)
138
    {
139
        fprintf(stderr, "ERROR: Could not open FTDI interface, try SUDOing / check connection\n");
140
        exit(-2);
141
    }
142
 
143
    // Read file into memory
144
    buf = load_file_to_mem(filename, size_override, &size);
145
    if (buf)
146
    {
147
        printf("Loading %s (%dKB) to 0x%x:\n", filename, (size + 1023) / 1024, address);
148
 
149
        // Upload file to target
150
        err = upload(address, buf, size);
151
 
152
        // Free file memory
153
        free(buf);
154
        buf = NULL;
155
 
156
        if (!err)
157
            printf("\rDone!\n");
158
        else
159
            printf("\rFailed!\n");
160
    }
161
    else
162
    {
163
        fprintf (stderr,"Error: Could not open image\n");
164
        err = 1;
165
    }
166
 
167
    ftdi_hw_close();
168
 
169
    return err;
170
}

powered by: WebSVN 2.1.0

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