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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [bootloaders/] [orpmon/] [services/] [modem.c] - Blame information for rev 406

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 marcus.erl
#include "common.h"
2
#include "support.h"
3
#include "net.h"
4
#include "uart.h"
5 246 julius
#include "spr-defs.h"
6 2 marcus.erl
#include "flash.h"
7
 
8
#define SOH    0x01
9
#define STX    0x02
10
#define EOT    0x04
11
#define ACK    0x06
12
#define NAK    0x15
13
#define CAN    0x18
14
#define C      0x43
15
 
16
#define CPMEOF 0x1A
17
 
18
#define RETRY   10
19
#define TIMEOUT 3
20
 
21
/* update CRC */
22 406 julius
unsigned short updcrc(register int c, register unsigned int crc)
23 2 marcus.erl
{
24 406 julius
        register int count;
25
 
26
        for (count = 8; --count >= 0;) {
27
                if (crc & 0x8000) {
28
                        crc <<= 1;
29
                        crc += (((c <<= 1) & 0400) != 0);
30
                        crc ^= 0x1021;
31
                } else {
32
                        crc <<= 1;
33
                        crc += (((c <<= 1) & 0400) != 0);
34
                }
35
        }
36
        return crc;
37 2 marcus.erl
}
38
 
39
static thand_f *mTimeHandler;
40
static unsigned long mTimeValue;
41
static int mTimeoutCount;
42
 
43
static unsigned long src_addr;
44
 
45
unsigned int length;
46
unsigned int modemMode = 0, bLen = 128;
47
unsigned short mycrc;
48
unsigned long bno = 0;
49
 
50
static void mStartTimeout(void);
51
 
52 406 julius
void mSetTimeout(int iv, thand_f * f)
53 2 marcus.erl
{
54 406 julius
        if (iv == 0)
55
                mTimeHandler = (thand_f *) 0;
56
        else {
57
                mTimeHandler = f;
58
                mTimeValue = get_timer(0) + iv;
59
        }
60 2 marcus.erl
}
61
 
62 406 julius
static void mStartTimeout(void)
63 2 marcus.erl
{
64 406 julius
        if (++mTimeoutCount >= RETRY) {
65
                printf("...retry counter exceeded, quitting...\n");
66
                return;
67
        } else {
68
                printf(".");
69
                mSetTimeout(TIMEOUT * TICKS_PER_SEC, mStartTimeout);
70
                uart_putc(C);
71 2 marcus.erl
 
72 406 julius
        }
73 2 marcus.erl
}
74
 
75 406 julius
static void mReceiveTimeout(void)
76 2 marcus.erl
{
77 406 julius
        if (++mTimeoutCount >= RETRY) {
78
                uart_putc(NAK);
79
                printf("...");
80
                return;
81
        } else {
82
                mSetTimeout(TIMEOUT * TICKS_PER_SEC, mReceiveTimeout);
83
                uart_putc(NAK);
84
        }
85 2 marcus.erl
}
86
 
87 406 julius
int getBlock(unsigned char t)
88 2 marcus.erl
{
89 406 julius
        unsigned int i = 0, j = 0;
90
        unsigned char mybuf[133];
91
        unsigned char bNo, nBno;
92
        unsigned long dst_addr;
93 2 marcus.erl
 
94 406 julius
        unsigned char flags;
95
        flags = UART_LSR_FE | UART_LSR_PE | UART_LSR_OE | UART_LSR_BI;  /*frame,parity,overrun errors */
96 2 marcus.erl
 
97 406 julius
        mycrc = 0;
98 2 marcus.erl
 
99 406 julius
        switch (t) {
100
        case SOH:
101
                for (i = 0; i < 132; i++) {
102
                        if ((REG8(UART_BASE + UART_LSR) & flags) == flags) {
103
                                uart_putc(CAN);
104
                        }
105
                        mybuf[i] = uart_getc();
106
                }
107 2 marcus.erl
 
108 406 julius
                bNo = mybuf[0];  /* packet id */
109
                nBno = mybuf[1];        /* neg. packet id */
110 2 marcus.erl
 
111 406 julius
                if ((bNo == 0x00) && (nBno == 0xff) && (bno == 0)) {     /* start block */
112
                        modemMode = 2;  /* ymodem */
113
                        uart_putc(ACK);
114
                        uart_putc(C);
115
                        return 1;
116
                } else if ((0xff - bNo) == nBno) {      /* data block */
117
                        for (i = 2, j = 0; i < 130; i++, j++) {
118
                                length++;
119
                                mycrc = updcrc(mybuf[i], mycrc);
120
                                dst_addr =
121
                                    src_addr + (bno * 0x8000) +
122
                                    ((bNo - 1) * 0x80) + j;
123
                                REG8(dst_addr) = mybuf[i];
124
                        }
125 2 marcus.erl
 
126 406 julius
                        mycrc = updcrc(mybuf[130], mycrc);
127
                        mycrc = updcrc(mybuf[131], mycrc);
128 2 marcus.erl
 
129 406 julius
                        if (mycrc == 0) {        /* CRC match! */
130
                                uart_putc(ACK);
131
                                for (i = 0; i < 128; i += 4) {
132
                                        /*      for(j=0; j<4; j++) {
133
                                           tmp = tmp << 8;
134
                                           tmp |= mybuf[i+j+2];
135
                                           }
136
                                           dst_addr = src_addr+(bno*0x8000)+((bNo-1)*128)+i;
137
                                           fl_word_program(dst_addr, tmp); */
138
                                }
139
                                if (bNo == 0xff)
140
                                        bno++;
141
                                return 1;
142
                        } else {
143
                                uart_putc(NAK);
144
                                return -1;
145
                        }
146
                } else {        /* packet id didn't match neg packet id! */
147
                        uart_putc(NAK);
148
                        return -1;
149
                }
150
                return 1;
151
                break;
152
        case EOT:
153
                if (modemMode == 2) {   /* ymodem */
154
                        uart_putc(NAK);
155
                        if (uart_getc() == EOT) {
156
                                uart_putc(ACK);
157
                                uart_putc(C);
158
                        } else
159
                                uart_putc(ACK);
160
                } else          /* zmodem */
161
                        uart_putc(ACK);
162
 
163
                return 0;
164
                break;
165
        default:
166
                /* Unknown header */
167
                uart_putc(NAK);
168
                return -1;
169 2 marcus.erl
        }
170
}
171
 
172 406 julius
int mGetData(unsigned long saddr)
173 2 marcus.erl
{
174 406 julius
        int retval = 1;
175
        unsigned char c;
176 2 marcus.erl
 
177 406 julius
        length = 0;
178
        src_addr = saddr;
179
        modemMode = 1;
180
        bno = 0;
181 2 marcus.erl
 
182 406 julius
        printf("src_addr: 0x%lx\n", src_addr);
183
        if (fl_init() != 0) {
184
                printf("Flash init failed!\n");
185
                return (-1);
186
        }
187 2 marcus.erl
#if 0
188 406 julius
        printf("Unlocking flash...");
189
        for (i = 0, c = FLASH_BASE_ADDR; i < (FLASH_SIZE / FLASH_BLOCK_SIZE);
190
             i++, c += FLASH_BLOCK_SIZE)
191
                if (fl_unlock_one_block(c))
192
                        return 1;
193
        printf("done\n");
194 2 marcus.erl
 
195 406 julius
        printf("Erasing flash...");
196
        for (i = 0, c = FLASH_BASE_ADDR; i < (FLASH_SIZE / FLASH_BLOCK_SIZE);
197
             i++, c += FLASH_BLOCK_SIZE)
198
                if (fl_block_erase(c))
199
                        return 1;
200
        printf("done\n");
201 2 marcus.erl
#endif
202 406 julius
        printf("Waiting...");
203 2 marcus.erl
 
204 406 julius
        mTimeoutCount = 0;
205
        mSetTimeout(TIMEOUT * TICKS_PER_SEC, mStartTimeout);
206 2 marcus.erl
 
207 406 julius
        while (1) {
208
                if (mTimeHandler && (get_timer(0) > mTimeValue)) {
209
                        thand_f *x;
210
                        x = mTimeHandler;
211
                        mTimeHandler = (thand_f *) 0;
212
                        (*x) ();
213
                }
214
                c = uart_testc();
215
                if (c != 0)
216
                        break;
217
        }
218 2 marcus.erl
 
219 406 julius
        while (retval != 0) {
220
                retval = getBlock(c);
221
                if (retval != 0)
222
                        c = uart_getc();
223
        }
224 2 marcus.erl
 
225 406 julius
        if (modemMode == 2) {
226
                c = uart_getc();
227
                retval = getBlock(c);   /* last 'dummy' block for YModem */
228
                printf("... protocol: YModem, ");
229
        } else
230
                printf("... protocol: ZModem, ");
231
        return length;
232 2 marcus.erl
}

powered by: WebSVN 2.1.0

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