OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

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

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

powered by: WebSVN 2.1.0

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