Stefan Schuermans commited on 2012-05-02 20:53:14
Showing 6 changed files, with 343 additions and 351 deletions.
... | ... |
@@ -62,7 +62,7 @@ SRC = $(TARGET).c |
62 | 62 |
# uncomment the following: |
63 | 63 |
SRC += arp.c bus.c cf.c checksum.c config.c dhcp.c eeprom.c \ |
64 | 64 |
ethernet.c http.c icmp.c ip.c random.c rtl8019.c \ |
65 |
- ser62500.c status.c tcp.c timing.c uart.c udp.c \ |
|
65 |
+ ser62500.c status.c tasks.c tcp.c timing.c uart.c udp.c \ |
|
66 | 66 |
xtea.c |
67 | 67 |
|
68 | 68 |
# You can also wrap lines by appending a backslash to the end of the line: |
... | ... |
@@ -160,14 +160,14 @@ REBUILD_DEPS = Makefile.conf |
160 | 160 |
# Type: avrdude -c ? |
161 | 161 |
# to get a full listing. |
162 | 162 |
# |
163 |
-#AVRDUDE_PROGRAMMER = stk200 |
|
164 |
-AVRDUDE_PROGRAMMER = avrisp2 |
|
163 |
+AVRDUDE_PROGRAMMER = stk200 |
|
164 |
+#AVRDUDE_PROGRAMMER = avrisp2 |
|
165 | 165 |
|
166 | 166 |
|
167 | 167 |
#AVRDUDE_PORT = com1 # programmer connected to serial device |
168 | 168 |
#AVRDUDE_PORT = lpt1 # programmer connected to parallel port |
169 |
-#AVRDUDE_PORT = /dev/parport0 |
|
170 |
-AVRDUDE_PORT = usb |
|
169 |
+AVRDUDE_PORT = /dev/parport0 |
|
170 |
+#AVRDUDE_PORT = usb |
|
171 | 171 |
|
172 | 172 |
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex |
173 | 173 |
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep |
... | ... |
@@ -11,6 +11,7 @@ |
11 | 11 |
#include "debug.h" |
12 | 12 |
#include "macros.h" |
13 | 13 |
#include "status.h" |
14 |
+#include "tasks.h" |
|
14 | 15 |
#include "timing.h" |
15 | 16 |
|
16 | 17 |
// IO pins of compact flash |
... | ... |
@@ -57,13 +58,18 @@ |
57 | 58 |
#define CF_SB_IDX (1) |
58 | 59 |
#define CF_SB_ERR (0) |
59 | 60 |
|
61 |
+// timeout value for wait for ready after reset counter (in 20ms steps) |
|
62 |
+#define CF_RESET_READY_TIMEOUT (50) // 1s |
|
63 |
+ |
|
64 |
+// timeout value for wait for ready counter (in 20ms steps) |
|
65 |
+#define CF_READY_TIMEOUT (4) // 80ms |
|
66 |
+ |
|
60 | 67 |
// compact flash commands |
61 | 68 |
#define CF_CMD_IDENTIFY (0xEC) |
62 | 69 |
#define CF_CMD_READ_SEC (0x20) |
63 | 70 |
#define CF_CMD_WRITE_SEC (0x30) |
64 | 71 |
|
65 | 72 |
// some number of bytes |
66 |
-#define CF_BYTES_AT_ONCE (32) // number of bytes to read/write at once |
|
67 | 73 |
#define CF_BYTES_IDENTIFY (124) // number of bytes to read for identify |
68 | 74 |
|
69 | 75 |
// interesting locations in identify data |
... | ... |
@@ -77,61 +83,24 @@ |
77 | 83 |
#define CF_CAPA_BIT_LBA (9) // bit number of the LBA bit in the CF |
78 | 84 |
// capabilities |
79 | 85 |
|
80 |
-// timeout value for wait for ready after reset counter (in 20ms steps) |
|
81 |
-#define CF_WAIT_READY_RESET_20_TIMEOUT (50) // 1s |
|
86 |
+// compact flash error state (0 for no error, 1 for error) |
|
87 |
+char CfErrorState = 1; |
|
82 | 88 |
|
83 |
-// timeout value for wait for ready counter (in 20ms steps) |
|
84 |
-#define CF_WAIT_READY_20_TIMEOUT (4) // 80ms |
|
85 |
- |
|
86 |
-// compact flash insertion state |
|
87 |
-enum CfInsertionState { |
|
88 |
- CfInsNone, // no compact flash inserted |
|
89 |
- CfInsNew, // new compact flash has been inserted, wait for powerup |
|
90 |
- CfInsReset, // resetting new compact flash |
|
91 |
- CfInsWait, // waiting for new compact flash to get ready after reset |
|
92 |
- CfInsReady, // compact flash inserted and ready to work with |
|
93 |
- CfInsError, // compact flash error occured, compact flash deactivated |
|
94 |
-} CfInsState = CfInsNone; |
|
95 |
- |
|
96 |
-// compact flash working state |
|
97 |
-enum CfWorkingState { |
|
98 |
- CfWorkNone, // no compact flash work to do |
|
99 |
- CfWorkIdentify, // identification of compact flash active |
|
100 |
-} CfWorkState = CfWorkNone; |
|
101 |
- |
|
102 |
-// compact flash working step |
|
103 |
-enum CfWorkingStep { |
|
104 |
- CfStepNone, // no step in progress |
|
105 |
- CfStepCmdStatus, // checking command status |
|
106 |
- CfStepDataTransfer, // transferring (reading/writing) data |
|
107 |
- CfStepDataStatus, // checking data status |
|
108 |
-} CfWorkStep = CfStepNone; |
|
89 |
+// tick/timeout counter of compact flash |
|
90 |
+// - counts down in 20ms steps, stops at zero |
|
91 |
+unsigned char CfTickCnt = 0; |
|
109 | 92 |
|
110 | 93 |
// number of sectors on compact flash card |
111 | 94 |
// - zero if no CF card is present, CF card has not yet been identified or CF |
112 | 95 |
// card error occured |
113 | 96 |
unsigned long CfSectorCnt = 0; |
114 | 97 |
|
115 |
-// state of active command |
|
116 |
-unsigned long CfSectorNo = 0; // current sector number of compact flash |
|
117 |
-unsigned short CfNextOffset = 0; // next offset in sector that is |
|
118 |
- // processed (read, written, ...) |
|
119 |
- |
|
120 |
-// wait for ready counter of compact flash |
|
121 |
-// - counts down in 20ms steps |
|
122 |
-// - zero if inactive |
|
123 |
-unsigned char CfWaitReady20 = 0; |
|
124 |
- |
|
125 | 98 |
// sector buffer |
126 |
-unsigned char CfSectorBuffer[CF_SECTOR_SIZE]; // (extern) |
|
99 |
+unsigned char CfSectorBuffer[CF_SECTOR_SIZE]; // FIXME |
|
127 | 100 |
|
128 | 101 |
// write a compact flash register (returns 1 if successful or 0 on error) |
129 | 102 |
// - returns 0 on success and -1 on error |
130 |
-extern inline char CfWriteReg(unsigned char reg, unsigned char val) // force |
|
131 |
- // inlining |
|
132 |
- // by |
|
133 |
- // using |
|
134 |
- // "extern" |
|
103 |
+extern inline char CfWriteReg(unsigned char reg, unsigned char val) |
|
135 | 104 |
{ |
136 | 105 |
if (!CF_IS_DETECT() || !CF_IS_READY()) // check that card is present |
137 | 106 |
// and ready |
... | ... |
@@ -157,7 +126,6 @@ extern inline char CfWriteReg(unsigned char reg, unsigned char val) // force |
157 | 126 |
// write buffer to compact flash register |
158 | 127 |
// (returns 1 if successful or 0 on error) |
159 | 128 |
// - returns 0 on success and -1 on error |
160 |
-// - force inlining by using "extern" |
|
161 | 129 |
extern inline char CfWriteRegBuf(unsigned char reg, unsigned char *p_buf, |
162 | 130 |
unsigned short len) |
163 | 131 |
{ |
... | ... |
@@ -192,11 +160,7 @@ extern inline char CfWriteRegBuf(unsigned char reg, unsigned char *p_buf, |
192 | 160 |
// write constant value to compact flash register multiple times (returns 1 |
193 | 161 |
// if successful or 0 on error) |
194 | 162 |
// - returns 0 on success and -1 on error |
195 |
-extern inline char CfWriteRegConst(unsigned char reg, unsigned char val, unsigned short cnt) // force |
|
196 |
- // inlining |
|
197 |
- // by |
|
198 |
- // using |
|
199 |
- // "extern" |
|
163 |
+extern inline char CfWriteRegConst(unsigned char reg, unsigned char val, unsigned short cnt) |
|
200 | 164 |
{ |
201 | 165 |
if (!CF_IS_DETECT() || !CF_IS_READY()) // check that card is present |
202 | 166 |
// and ready |
... | ... |
@@ -226,11 +190,7 @@ extern inline char CfWriteRegConst(unsigned char reg, unsigned char val, unsigne |
226 | 190 |
|
227 | 191 |
// read a compact flash register (returns 1 if successful or 0 on error) |
228 | 192 |
// - returns 0 on success and -1 on error |
229 |
-extern inline char CfReadReg(unsigned char reg, unsigned char *p_var) // force |
|
230 |
- // inlining |
|
231 |
- // by |
|
232 |
- // using |
|
233 |
- // "extern" |
|
193 |
+extern inline char CfReadReg(unsigned char reg, unsigned char *p_var) |
|
234 | 194 |
{ |
235 | 195 |
if (!CF_IS_DETECT() || !CF_IS_READY()) // check that card is present |
236 | 196 |
// and ready |
... | ... |
@@ -253,11 +213,7 @@ extern inline char CfReadReg(unsigned char reg, unsigned char *p_var) // force |
253 | 213 |
// read buffer from a compact flash register (returns 1 if successful or 0 on |
254 | 214 |
// error) |
255 | 215 |
// - returns 0 on success and -1 on error |
256 |
-extern inline char CfReadRegBuf(unsigned char reg, unsigned char *p_buf, unsigned short len) // force |
|
257 |
- // inlining |
|
258 |
- // by |
|
259 |
- // using |
|
260 |
- // "extern" |
|
216 |
+extern inline char CfReadRegBuf(unsigned char reg, unsigned char *p_buf, unsigned short len) |
|
261 | 217 |
{ |
262 | 218 |
if (!CF_IS_DETECT() || !CF_IS_READY()) // check that card is present |
263 | 219 |
// and ready |
... | ... |
@@ -285,11 +241,7 @@ extern inline char CfReadRegBuf(unsigned char reg, unsigned char *p_buf, unsigne |
285 | 241 |
// read a compact flash register multiple times and throw away data (returns |
286 | 242 |
// 1 if successful or 0 on error) |
287 | 243 |
// - returns 0 on success and -1 on error |
288 |
-extern inline char CfReadRegMulti(unsigned char reg, unsigned short cnt) // force |
|
289 |
- // inlining |
|
290 |
- // by |
|
291 |
- // using |
|
292 |
- // "extern" |
|
244 |
+extern inline char CfReadRegMulti(unsigned char reg, unsigned short cnt) |
|
293 | 245 |
{ |
294 | 246 |
if (!CF_IS_DETECT() || !CF_IS_READY()) // check that card is present |
295 | 247 |
// and ready |
... | ... |
@@ -313,171 +265,184 @@ extern inline char CfReadRegMulti(unsigned char reg, unsigned short cnt) |
313 | 265 |
return cnt <= 0 ? 0 : -1; // success if everything has been read |
314 | 266 |
} |
315 | 267 |
|
316 |
-// compact flash work done |
|
317 |
-static void CfDone(void) |
|
268 |
+// compact flash error occured |
|
269 |
+static void CfError(void) |
|
318 | 270 |
{ |
319 |
- debug_cf_printf("CF done (%lu sectors)", CfSectorCnt); |
|
271 |
+ debug_cf_printf("CF error"); |
|
320 | 272 |
|
321 |
- // set working state and step to none |
|
322 |
- CfWorkState = CfWorkNone; |
|
323 |
- CfWorkStep = CfStepNone; |
|
273 |
+ CfErrorState = 1; |
|
274 |
+ CfTickCnt = 0; |
|
324 | 275 |
|
325 |
- // reset state of active command |
|
326 |
- CfSectorNo = 0; |
|
327 |
- CfNextOffset = 0; |
|
276 |
+ CfSectorCnt = 0; |
|
328 | 277 |
|
329 |
- // disable wait for ready counter |
|
330 |
- CfWaitReady20 = 0; |
|
278 |
+ // report no working CF present |
|
279 |
+ StatusInfoCfPresent = 0; |
|
331 | 280 |
} |
332 | 281 |
|
333 |
-// compact flash error occured |
|
334 |
-static void CfError(void) |
|
282 |
+// compact flash is gone |
|
283 |
+static void CfGone(void) |
|
335 | 284 |
{ |
336 |
- debug_cf_printf("CF error"); |
|
337 |
- |
|
338 |
- // set insertion state to error |
|
339 |
- CfInsState = CfInsError; |
|
285 |
+ debug_cf_printf("CF gone"); |
|
340 | 286 |
|
341 |
- // set working state and step to none |
|
342 |
- CfWorkState = CfWorkNone; |
|
343 |
- CfWorkStep = CfStepNone; |
|
287 |
+ CfError(); |
|
288 |
+} |
|
344 | 289 |
|
345 |
- // reset all information about CF card |
|
346 |
- CfSectorCnt = 0; |
|
290 |
+// compact flash timeout |
|
291 |
+static void CfTimeout(void) |
|
292 |
+{ |
|
293 |
+ debug_cf_printf("CF timeout"); |
|
347 | 294 |
|
348 |
- // reset state of active command |
|
349 |
- CfSectorNo = 0; |
|
350 |
- CfNextOffset = 0; |
|
295 |
+ CfError(); |
|
296 |
+} |
|
351 | 297 |
|
352 |
- // disable wait for ready counter |
|
353 |
- CfWaitReady20 = 0; |
|
298 |
+// initialize |
|
299 |
+void CfInit(void) // (extern) |
|
300 |
+{ |
|
301 |
+ // setup ports |
|
302 |
+ bit_clear(CF_PORT_nCD, CF_BIT_nCD); // pull-up of card detect pin off |
|
303 |
+ // (external pull-up is present) |
|
304 |
+ bit_clear(CF_DDR_nCD, CF_BIT_nCD); // card detect pin to input |
|
305 |
+ bit_set(CF_PORT_nRST, CF_BIT_nRST); // reset not active |
|
306 |
+ bit_set(CF_DDR_nRST, CF_BIT_nRST); // reset pin to output |
|
307 |
+ bit_clear(CF_PORT_RDY, CF_BIT_RDY); // pull-up of ready pin pin off |
|
308 |
+ // (external pull-up is present) |
|
309 |
+ bit_clear(CF_DDR_RDY, CF_BIT_RDY); // ready pin to input |
|
310 |
+ bit_set(CF_PORT_nCE, CF_BIT_nCE); // card not enabled |
|
311 |
+ bit_set(CF_DDR_nCE, CF_BIT_nCE); // card enable pin to output |
|
354 | 312 |
} |
355 | 313 |
|
356 |
-// process compact flash insertion |
|
357 |
-static void CfProcIns(void) |
|
314 |
+// tick procedure - call every 20ms |
|
315 |
+void CfTick20(void) // (extern) |
|
358 | 316 |
{ |
359 |
- switch (CfInsState) { |
|
360 |
- |
|
361 |
- // no compact flash inserted |
|
362 |
- case CfInsNone: |
|
363 |
- // if compact flash is detected, set state to new CF |
|
364 |
- if (CF_IS_DETECT()) { |
|
365 |
- CfInsState = CfInsNew; |
|
366 |
- debug_cf_printf("new CF"); |
|
367 |
- break; |
|
317 |
+ // count down ticks |
|
318 |
+ if (CfTickCnt > 0) |
|
319 |
+ --CfTickCnt; |
|
368 | 320 |
} |
369 |
- break; |
|
370 | 321 |
|
371 |
- // new compact flash has been inserted, wait for powerup |
|
372 |
- case CfInsNew: |
|
373 |
- // if compact flash has been removed, set state to none |
|
322 |
+/** |
|
323 |
+ * @brief wait for CF to become ready |
|
324 |
+ * @param[in] timeout timeout in 20ms steps |
|
325 |
+ * @return 0 if CF ready, -1 if CF gone or timeout |
|
326 |
+ */ |
|
327 |
+static int CfWaitReady(unsigned char timeout) |
|
328 |
+{ |
|
329 |
+ CfTickCnt = timeout; |
|
330 |
+ |
|
331 |
+ while (!CF_IS_READY()) { |
|
332 |
+ |
|
333 |
+ // card gone -> error |
|
374 | 334 |
if (!CF_IS_DETECT()) { |
375 |
- CfInsState = CfInsNone; |
|
376 |
- break; |
|
335 |
+ CfGone(); |
|
336 |
+ return -1; |
|
377 | 337 |
} |
378 |
- CfInsState = CfInsReset; |
|
379 |
- break; |
|
380 | 338 |
|
381 |
- // resetting new compact flash |
|
382 |
- case CfInsReset: |
|
383 |
- // set reset, wait, take back reset |
|
384 |
- CF_RESET_ACT(); |
|
385 |
- nop(); |
|
386 |
- nop(); |
|
387 |
- nop(); |
|
388 |
- CF_RESET_IDLE(); |
|
389 |
- debug_cf_printf("CF reset"); |
|
390 |
- // if compact flash has been removed, set state to none |
|
391 |
- if (!CF_IS_DETECT()) { |
|
392 |
- CfInsState = CfInsNone; |
|
393 |
- break; |
|
339 |
+ // timeout -> error |
|
340 |
+ if (CfTickCnt <= 0) { |
|
341 |
+ CfTimeout(); |
|
342 |
+ return -1; |
|
394 | 343 |
} |
395 |
- // wait for CF to become ready |
|
396 |
- CfInsState = CfInsWait; |
|
397 |
- CfWaitReady20 = CF_WAIT_READY_RESET_20_TIMEOUT; |
|
398 |
- break; |
|
399 | 344 |
|
400 |
- // waiting for new compact flash to get ready after reset |
|
401 |
- case CfInsWait: |
|
402 |
- // if compact flash has been removed, set state to none |
|
403 |
- if (!CF_IS_DETECT()) { |
|
404 |
- CfInsState = CfInsNone; |
|
405 |
- break; |
|
345 |
+ // execute other tasks while waiting |
|
346 |
+ Tasks(); |
|
347 |
+ |
|
348 |
+ } // while (!CF_IS_READY()) |
|
349 |
+ |
|
350 |
+ return 0; |
|
406 | 351 |
} |
407 |
- // if compact flash card is ready, set state to ready |
|
408 |
- if (CF_IS_READY()) { |
|
409 |
- CfInsState = CfInsReady; |
|
410 |
- CfWaitReady20 = 0; |
|
411 |
- debug_cf_printf("CF ready"); |
|
412 |
- break; |
|
352 |
+ |
|
353 |
+/** |
|
354 |
+ * @brief read bytes from CF card |
|
355 |
+ * @param[in] ptr pointer to data buffer |
|
356 |
+ * @param[in] sz size of data to read |
|
357 |
+ * @return 0 if reading was successful, -1 if CF gon, timeout or error |
|
358 |
+ */ |
|
359 |
+static int CfReadBytes(unsigned char *ptr, unsigned int sz) |
|
360 |
+{ |
|
361 |
+ // read bytes |
|
362 |
+ if (CfReadRegBuf(CF_REG_DATA, ptr, sz) != 0) { |
|
363 |
+ debug_cf_printf("reading from CF failed (offset length 0x%03X)", sz); |
|
364 |
+ CfError(); |
|
365 |
+ return -1; |
|
413 | 366 |
} |
414 |
- // count down timeout |
|
415 |
- CfWaitReady20--; |
|
416 |
- if (CfWaitReady20 <= 0) { |
|
417 |
- debug_cf_printf("timeout waiting for CF ready"); |
|
418 |
- // start over |
|
419 |
- CfInsState = CfInsNone; |
|
420 |
- break; |
|
367 |
+ |
|
368 |
+ return 0; |
|
421 | 369 |
} |
422 |
- break; |
|
423 | 370 |
|
424 |
- // compact flash inserted and ready to work with |
|
425 |
- case CfInsReady: |
|
426 |
- // do nothing here, card work is done by task procedure |
|
427 |
- // (task will detect CF removal or timeout and set state to error) |
|
428 |
- break; |
|
371 |
+/** |
|
372 |
+ * @brief check if CF card is present |
|
373 |
+ * @return 1 if CF card is present, 0 if not |
|
374 |
+ */ |
|
375 |
+char CfIsPresent(void) // (extern) |
|
376 |
+{ |
|
377 |
+ if (CF_IS_DETECT()) |
|
378 |
+ return 1; |
|
379 |
+ else |
|
380 |
+ return 0; |
|
381 |
+} |
|
429 | 382 |
|
430 |
- // compact flash error occured, compact flash deactivated |
|
431 |
- case CfInsError: |
|
432 |
- // if compact flash has been removed, set state to none |
|
383 |
+/** |
|
384 |
+ * @brief reset CF card |
|
385 |
+ * @return 0 if CF card is present and could be reset, -1 if not |
|
386 |
+ */ |
|
387 |
+char CfReset(void) // (extern) |
|
388 |
+{ |
|
389 |
+ // wait one tick |
|
433 | 390 |
if (!CF_IS_DETECT()) { |
434 |
- CfInsState = CfInsNone; |
|
435 |
- debug_cf_printf("CF removed"); |
|
391 |
+ CfGone(); |
|
392 |
+ return -1; |
|
436 | 393 |
} |
437 |
- break; |
|
394 |
+ CfTickCnt = 1; |
|
395 |
+ while (CfTickCnt > 0) |
|
396 |
+ Tasks(); |
|
438 | 397 |
|
439 |
- } // switch( CfInsState ) |
|
398 |
+ // set reset, wait, take back reset |
|
399 |
+ if (!CF_IS_DETECT()) { |
|
400 |
+ CfGone(); |
|
401 |
+ return -1; |
|
440 | 402 |
} |
403 |
+ CF_RESET_ACT(); |
|
404 |
+ nop(); |
|
405 |
+ nop(); |
|
406 |
+ nop(); |
|
407 |
+ CF_RESET_IDLE(); |
|
408 |
+ debug_cf_printf("CF reset"); |
|
441 | 409 |
|
442 |
-// detect compact flash timeout |
|
443 |
-// - called every 20ms |
|
444 |
-static void CfDetectTimeout20(void) |
|
445 |
-{ |
|
446 |
- // only detect CF timeout if insertion state is ready |
|
447 |
- // (otherwise, the insertion processing is still doing some work) |
|
448 |
- if (CfInsState != CfInsReady) |
|
449 |
- return; |
|
450 |
- |
|
451 |
- // wait for ready counter not active |
|
452 |
- if (CfWaitReady20 <= 0) |
|
453 |
- return; |
|
410 |
+ // wait for CF to become ready |
|
411 |
+ if (CfWaitReady(CF_RESET_READY_TIMEOUT) != 0) |
|
412 |
+ return -1; |
|
454 | 413 |
|
455 |
- // decrement wait for ready counter |
|
456 |
- CfWaitReady20--; |
|
414 |
+ // report working CF present |
|
415 |
+ CfErrorState = 0; |
|
416 |
+ StatusInfoCfPresent = 1; |
|
417 |
+ debug_cf_printf("CF ready"); |
|
457 | 418 |
|
458 |
- // timeout occured |
|
459 |
- if (CfWaitReady20 <= 0) { |
|
460 |
- debug_cf_printf("timeout while waiting for CF ready"); |
|
461 |
- CfError(); |
|
462 |
- } |
|
419 |
+ return 0; |
|
463 | 420 |
} |
464 | 421 |
|
465 |
-// continue with identifying compact flash |
|
466 |
-static void CfProcWorkIdentify(void) |
|
422 |
+/** |
|
423 |
+ * @brief identify CF card |
|
424 |
+ * @param[out] sectors number of sectors on CF card |
|
425 |
+ * @return 0 if CF card could be identified, -1 if not |
|
426 |
+ */ |
|
427 |
+char CfIdentify(unsigned long *sectors) // (extern) |
|
467 | 428 |
{ |
468 |
- unsigned char status = 0; |
|
469 |
- unsigned short len, val16; |
|
429 |
+ unsigned char status, identifyBuf[CF_BYTES_IDENTIFY]; |
|
430 |
+ unsigned short val16; |
|
470 | 431 |
|
471 |
- switch (CfWorkStep) { |
|
432 |
+ debug_cf_printf("CF identify"); |
|
433 |
+ if (CfErrorState) { |
|
434 |
+ debug_cf_printf("CF in error state"); |
|
435 |
+ return -1; |
|
436 |
+ } |
|
472 | 437 |
|
473 |
- // no step |
|
474 |
- case CfStepNone: |
|
475 |
- debug_cf_printf("internal error (identify, CfStepNone)"); |
|
438 |
+ // issue identify drive command |
|
439 |
+ if (CfWriteReg(CF_REG_CMD, CF_CMD_IDENTIFY) != 0) { |
|
476 | 440 |
CfError(); |
477 |
- break; |
|
441 |
+ return -1; |
|
442 |
+ } |
|
443 |
+ if (CfWaitReady(CF_READY_TIMEOUT) != 0) |
|
444 |
+ return -1; |
|
478 | 445 |
|
479 |
- // checking command status |
|
480 |
- case CfStepCmdStatus: |
|
481 | 446 |
// read status register |
482 | 447 |
CfReadReg(CF_REG_STATUS, &status); |
483 | 448 |
// check that BUSY=0, RDY=1, DWF=0, DSC=1, IDX=0, ERR=0 |
... | ... |
@@ -487,178 +452,100 @@ static void CfProcWorkIdentify(void) |
487 | 452 |
(1 << CF_SB_RDY | 1 << CF_SB_DSC)) { |
488 | 453 |
debug_cf_printf("unexpected status 0x%02X (identify)", status); |
489 | 454 |
CfError(); |
455 |
+ return -1; |
|
490 | 456 |
} |
491 |
- // continue with data transfer |
|
492 |
- CfWorkStep = CfStepDataTransfer; |
|
493 |
- CfNextOffset = 0; |
|
494 |
- break; |
|
495 | 457 |
|
496 |
- // transferring data |
|
497 |
- case CfStepDataTransfer: |
|
498 |
- // still bytes to read |
|
499 |
- if (CfNextOffset < CF_BYTES_IDENTIFY) { |
|
500 |
- // get number of bytes to read |
|
501 |
- len = CF_BYTES_IDENTIFY - CfNextOffset; |
|
502 |
- if (len > CF_BYTES_AT_ONCE) |
|
503 |
- len = CF_BYTES_AT_ONCE; |
|
504 |
- // read bytes |
|
505 |
- if (CfReadRegBuf(CF_REG_DATA, &CfSectorBuffer[CfNextOffset], len) != 0) { |
|
506 |
- debug_cf_printf |
|
507 |
- ("reading from CF failed (identify, offset 0x%03X, length 0x%02X)", |
|
508 |
- CfNextOffset, len); |
|
509 |
- CfError(); |
|
510 |
- break; |
|
511 |
- } |
|
512 |
- CfNextOffset += len; |
|
513 |
- } |
|
514 |
- // all bytes read |
|
515 |
- if (CfNextOffset >= CF_BYTES_IDENTIFY) { |
|
458 |
+ // read identify bytes |
|
459 |
+ if (CfReadBytes(identifyBuf, sizeof(identifyBuf)) != 0) |
|
460 |
+ return -1; |
|
461 |
+ |
|
516 | 462 |
// check identifier |
517 |
- val16 = (unsigned short)CfSectorBuffer[CF_ID_OFS_ID_16 + 0] |
|
518 |
- | (unsigned short)CfSectorBuffer[CF_ID_OFS_ID_16 + 1] << 8; |
|
463 |
+ val16 = (unsigned short)identifyBuf[CF_ID_OFS_ID_16 + 0] | |
|
464 |
+ (unsigned short)identifyBuf[CF_ID_OFS_ID_16 + 1] << 8; |
|
519 | 465 |
if (val16 != CF_ID) { |
520 | 466 |
debug_cf_printf("invalid CF identifier: 0x%04X", val16); |
521 | 467 |
CfError(); |
522 |
- break; |
|
468 |
+ return -1; |
|
523 | 469 |
} |
470 |
+ |
|
524 | 471 |
// check if LBA mode is supported |
525 |
- val16 = (unsigned short)CfSectorBuffer[CF_ID_OFS_CAPA_16 + 0] |
|
526 |
- | (unsigned short)CfSectorBuffer[CF_ID_OFS_CAPA_16 + 1] << 8; |
|
472 |
+ val16 = (unsigned short)identifyBuf[CF_ID_OFS_CAPA_16 + 0] |
|
473 |
+ | (unsigned short)identifyBuf[CF_ID_OFS_CAPA_16 + 1] << 8; |
|
527 | 474 |
if ((val16 & 1 << CF_CAPA_BIT_LBA) == 0) { |
528 | 475 |
debug_cf_printf("CF does not support LBA mode"); |
529 | 476 |
CfError(); |
530 |
- break; |
|
477 |
+ return -1; |
|
531 | 478 |
} |
532 | 479 |
// get number of sectors on CF |
533 |
- CfSectorCnt = (unsigned long)CfSectorBuffer[CF_ID_OFS_SEC_CNT_32 + 0] |
|
534 |
- | (unsigned long)CfSectorBuffer[CF_ID_OFS_SEC_CNT_32 + 1] << 8 |
|
535 |
- | (unsigned long)CfSectorBuffer[CF_ID_OFS_SEC_CNT_32 + 2] << 16 |
|
536 |
- | (unsigned long)CfSectorBuffer[CF_ID_OFS_SEC_CNT_32 + 3] << 24; |
|
480 |
+ CfSectorCnt = (unsigned long)identifyBuf[CF_ID_OFS_SEC_CNT_32 + 0] |
|
481 |
+ | (unsigned long)identifyBuf[CF_ID_OFS_SEC_CNT_32 + 1] << 8 |
|
482 |
+ | (unsigned long)identifyBuf[CF_ID_OFS_SEC_CNT_32 + 2] << 16 |
|
483 |
+ | (unsigned long)identifyBuf[CF_ID_OFS_SEC_CNT_32 + 3] << 24; |
|
537 | 484 |
if (CfSectorCnt <= 0) { |
538 | 485 |
debug_cf_printf("CF does not contain any sectors"); |
539 | 486 |
CfError(); |
540 |
- break; |
|
541 |
- } |
|
542 |
- // done |
|
543 |
- CfDone(); |
|
487 |
+ return -1; |
|
544 | 488 |
} |
545 |
- break; |
|
546 | 489 |
|
547 |
- // checking data status |
|
548 |
- case CfStepDataStatus: |
|
549 |
- debug_cf_printf("internal error (identify, CfStepDataStatus)"); |
|
550 |
- CfError(); |
|
551 |
- break; |
|
490 |
+ debug_cf_printf("CF done (%lu sectors)", CfSectorCnt); |
|
552 | 491 |
|
553 |
- } // switch( CfWorkStep ) |
|
492 |
+ return 0; |
|
554 | 493 |
} |
555 | 494 |
|
556 |
-// do normal compact flash work |
|
557 |
-static void CfProcWork(void) |
|
495 |
+/** |
|
496 |
+ * @brief read from CF card |
|
497 |
+ * @param[in] sector number of sector to read |
|
498 |
+ * @param[out] data data read from sector |
|
499 |
+ * @return 0 if sector could be read, -1 if not |
|
500 |
+ */ |
|
501 |
+char CfRead(unsigned long sector, unsigned char data[CF_SECTOR_SIZE]) // (extern) |
|
558 | 502 |
{ |
559 |
- // only do CF processing if insertion state is ready |
|
560 |
- // (otherwise, the insertion processing is still doing some work) |
|
561 |
- if (CfInsState != CfInsReady) |
|
562 |
- return; |
|
503 |
+ unsigned char status; |
|
563 | 504 |
|
564 |
- // compact flash has been removed -> error |
|
565 |
- if (!CF_IS_DETECT()) { |
|
566 |
- debug_cf_printf("CF no more present"); |
|
567 |
- CfError(); |
|
568 |
- return; |
|
569 |
- } |
|
570 |
- // compact flash is not ready |
|
571 |
- if (!CF_IS_READY()) { |
|
572 |
- // wait for ready not active --> error |
|
573 |
- if (CfWaitReady20 <= 0) { |
|
574 |
- debug_cf_printf("CF no more ready"); |
|
575 |
- CfError(); |
|
576 |
- } |
|
577 |
- // do nothing if CF is not ready |
|
578 |
- return; |
|
505 |
+ debug_cf_printf("CF read (sector %lu)", sector); |
|
506 |
+ if (CfErrorState) { |
|
507 |
+ debug_cf_printf("CF in error state"); |
|
508 |
+ return -1; |
|
579 | 509 |
} |
580 |
- |
|
581 |
- switch (CfWorkState) { |
|
582 |
- |
|
583 |
- // no compact flash work to do |
|
584 |
- case CfWorkNone: |
|
585 |
- // nothing to do here |
|
586 |
- break; |
|
587 |
- |
|
588 |
- // identification of compact flash active |
|
589 |
- case CfWorkIdentify: |
|
590 |
- CfProcWorkIdentify(); |
|
591 |
- break; |
|
592 |
- |
|
593 |
- } // switch( CfWorkState ) |
|
510 |
+ if (sector >= CfSectorCnt) { |
|
511 |
+ debug_cf_printf("sector does not exist"); |
|
512 |
+ return -1; |
|
594 | 513 |
} |
595 | 514 |
|
596 |
-// identify compact flash |
|
597 |
-// - returns 0 on success, 1 if not idle and -1 on error |
|
598 |
-static char CfIdentify(void) |
|
599 |
-{ |
|
600 |
- // check that no command is being worked on |
|
601 |
- if (CfWorkState != CfWorkNone) |
|
602 |
- return 1; |
|
603 |
- |
|
604 |
- debug_cf_printf("CF identify"); |
|
605 |
- |
|
606 |
- // issue identify drive command |
|
607 |
- if (CfWriteReg(CF_REG_CMD, CF_CMD_IDENTIFY) != 0) { |
|
515 |
+ // set sector number (LBA mode) and sector count (1) |
|
516 |
+ if (CfWriteReg(CF_REG_HEAD, 0xE0 | (sector >> 24 & 0x0F)) != 0 || |
|
517 |
+ CfWriteReg(CF_REG_CYL_H, sector >> 16) != 0 || |
|
518 |
+ CfWriteReg(CF_REG_CYL_L, sector >> 8) != 0 || |
|
519 |
+ CfWriteReg(CF_REG_SEC_NO, sector) != 0 || |
|
520 |
+ CfWriteReg(CF_REG_SEC_CNT, 1) != 0) { |
|
608 | 521 |
CfError(); |
609 | 522 |
return -1; |
610 | 523 |
} |
611 |
- // now identifying compact flash |
|
612 |
- CfWorkState = CfWorkIdentify; |
|
613 |
- CfWorkStep = CfStepCmdStatus; |
|
614 |
- CfNextOffset = 0; |
|
615 |
- CfWaitReady20 = CF_WAIT_READY_20_TIMEOUT; |
|
616 |
- return 0; |
|
617 |
-} |
|
618 | 524 |
|
619 |
-// initialize |
|
620 |
-void CfInit(void) // (extern) |
|
621 |
-{ |
|
622 |
- // setup ports |
|
623 |
- bit_clear(CF_PORT_nCD, CF_BIT_nCD); // pull-up of card detect pin off |
|
624 |
- // (external pull-up is present) |
|
625 |
- bit_clear(CF_DDR_nCD, CF_BIT_nCD); // card detect pin to input |
|
626 |
- bit_set(CF_PORT_nRST, CF_BIT_nRST); // reset not active |
|
627 |
- bit_set(CF_DDR_nRST, CF_BIT_nRST); // reset pin to output |
|
628 |
- bit_clear(CF_PORT_RDY, CF_BIT_RDY); // pull-up of ready pin pin off |
|
629 |
- // (external pull-up is present) |
|
630 |
- bit_clear(CF_DDR_RDY, CF_BIT_RDY); // ready pin to input |
|
631 |
- bit_set(CF_PORT_nCE, CF_BIT_nCE); // card not enabled |
|
632 |
- bit_set(CF_DDR_nCE, CF_BIT_nCE); // card enable pin to output |
|
525 |
+ // issue read sector command |
|
526 |
+ if (CfWriteReg(CF_REG_CMD, CF_CMD_READ_SEC) != 0) { |
|
527 |
+ CfError(); |
|
528 |
+ return -1; |
|
633 | 529 |
} |
530 |
+ if (CfWaitReady(CF_READY_TIMEOUT) != 0) |
|
531 |
+ return -1; |
|
634 | 532 |
|
635 |
-// tick procedure - call every 20ms |
|
636 |
-void CfTick20(void) // (extern) |
|
637 |
-{ |
|
638 |
- // process compact flash insertion |
|
639 |
- CfProcIns(); |
|
640 |
- |
|
641 |
- // detect compact flash timeout |
|
642 |
- CfDetectTimeout20(); |
|
533 |
+ // read status register |
|
534 |
+ CfReadReg(CF_REG_STATUS, &status); |
|
535 |
+ // check that BUSY=0, RDY=1, DWF=0, DSC=1, IDX=0, ERR=0 |
|
536 |
+ if ((status & |
|
537 |
+ (1 << CF_SB_BUSY | 1 << CF_SB_RDY | 1 << CF_SB_DWF | 1 << CF_SB_DSC | |
|
538 |
+ 1 << CF_SB_IDX | 1 << CF_SB_ERR)) != |
|
539 |
+ (1 << CF_SB_RDY | 1 << CF_SB_DSC)) { |
|
540 |
+ debug_cf_printf("unexpected status 0x%02X (identify)", status); |
|
541 |
+ CfError(); |
|
542 |
+ return -1; |
|
643 | 543 |
} |
644 | 544 |
|
645 |
-// task function to do the work - call from main loop |
|
646 |
-void CfTask(void) // (extern) |
|
647 |
-{ |
|
648 |
- // do nothing if no CF is inserted |
|
649 |
- if (CfInsState != CfInsReady) |
|
650 |
- return; |
|
545 |
+ // read sector bytes |
|
546 |
+ if (CfReadBytes(data, CF_SECTOR_SIZE) != 0) |
|
547 |
+ return -1; |
|
651 | 548 |
|
652 |
- // compact flash is ready but not yet identified |
|
653 |
- if (CfSectorCnt == 0) { |
|
654 |
- // identify compact flash |
|
655 |
- CfIdentify(); |
|
549 |
+ return 0; |
|
656 | 550 |
} |
657 |
- // do normal compact flash work |
|
658 |
- CfProcWork(); |
|
659 | 551 |
|
660 |
- // update status |
|
661 |
- StatusInfoCfPresent = CfInsState == CfInsReady; // report working CF |
|
662 |
- // if insertion state |
|
663 |
- // is ready |
|
664 |
-} |
... | ... |
@@ -9,16 +9,38 @@ |
9 | 9 |
// size of a sector in bytes |
10 | 10 |
#define CF_SECTOR_SIZE (512) |
11 | 11 |
|
12 |
-// sector buffer |
|
13 |
-extern unsigned char CfSectorBuffer[CF_SECTOR_SIZE]; |
|
14 |
- |
|
15 | 12 |
// initialize |
16 | 13 |
extern void CfInit(void); |
17 | 14 |
|
18 | 15 |
// tick procedure - call every 20ms |
19 | 16 |
extern void CfTick20(void); |
20 | 17 |
|
21 |
-// task function to do the work - call from main loop |
|
22 |
-extern void CfTask(void); |
|
18 |
+/** |
|
19 |
+ * @brief check if CF card is present |
|
20 |
+ * @return 1 if CF card is present, 0 if not |
|
21 |
+ */ |
|
22 |
+extern char CfIsPresent(void); |
|
23 |
+ |
|
24 |
+/** |
|
25 |
+ * @brief reset CF card |
|
26 |
+ * @return 0 if CF card could be reset, -1 if not |
|
27 |
+ */ |
|
28 |
+extern char CfReset(void); |
|
29 |
+ |
|
30 |
+/** |
|
31 |
+ * @brief identify CF card |
|
32 |
+ * @param[out] sectors number of sectors on CF card |
|
33 |
+ * @return 0 if CF card could be identified, -1 if not |
|
34 |
+ */ |
|
35 |
+extern char CfIdentify(unsigned long *sectors); |
|
36 |
+ |
|
37 |
+/** |
|
38 |
+ * @brief read from CF card |
|
39 |
+ * @param[in] sector number of sector to read |
|
40 |
+ * @param[out] data data read from sector |
|
41 |
+ * @return 0 if sector could be read, -1 if not |
|
42 |
+ */ |
|
43 |
+extern char CfRead(unsigned long sector, unsigned char data[CF_SECTOR_SIZE]); |
|
23 | 44 |
|
24 | 45 |
#endif // #ifndef INC_cf |
46 |
+ |
... | ... |
@@ -15,10 +15,56 @@ |
15 | 15 |
#include "random.h" |
16 | 16 |
#include "rtl8019.h" |
17 | 17 |
#include "status.h" |
18 |
+#include "tasks.h" |
|
18 | 19 |
#include "tcp.h" |
19 | 20 |
#include "timing.h" |
20 | 21 |
#include "uart.h" |
21 | 22 |
|
23 |
+// try to work with CF card |
|
24 |
+static void mainWorkCf(void) |
|
25 |
+{ |
|
26 |
+ unsigned long sectors, s; |
|
27 |
+ unsigned char buf[CF_SECTOR_SIZE]; |
|
28 |
+ |
|
29 |
+ // reset card |
|
30 |
+ if (CfReset() != 0) |
|
31 |
+ return; |
|
32 |
+ |
|
33 |
+ // identify card |
|
34 |
+ if (CfIdentify(§ors) != 0) |
|
35 |
+ return; |
|
36 |
+ |
|
37 |
+ // dump sectors |
|
38 |
+ for (s = 0; s < sectors; ++s) { |
|
39 |
+ if (CfRead(s, buf) != 0) |
|
40 |
+ return; |
|
41 |
+#if 0 |
|
42 |
+ unsigned int i; |
|
43 |
+ for (i = 0; i < CF_SECTOR_SIZE; ++i) { |
|
44 |
+ printf(" %02X", buf[i]); |
|
45 |
+ if ((i & 15) == 15) |
|
46 |
+ printf("\r\n"); |
|
47 |
+ } |
|
48 |
+#endif |
|
49 |
+ Tasks(); |
|
50 |
+ } // for s |
|
51 |
+} |
|
52 |
+ |
|
53 |
+// wait for CF card and work with it |
|
54 |
+static void mainWaitCf(void) |
|
55 |
+{ |
|
56 |
+ // wait for new card |
|
57 |
+ while (!CfIsPresent()) |
|
58 |
+ Tasks(); |
|
59 |
+ |
|
60 |
+ // work with card |
|
61 |
+ mainWorkCf(); |
|
62 |
+ |
|
63 |
+ // wait for failing card to be removed |
|
64 |
+ while (CfIsPresent()) |
|
65 |
+ Tasks(); |
|
66 |
+} |
|
67 |
+ |
|
22 | 68 |
// main code entry point |
23 | 69 |
int main(void) |
24 | 70 |
{ |
... | ... |
@@ -66,18 +112,8 @@ int main(void) |
66 | 112 |
debug_init_printf("start"); |
67 | 113 |
|
68 | 114 |
// main loop |
69 |
- while (1) { |
|
70 |
- wdt_reset(); |
|
71 |
- CfTask(); |
|
72 |
- wdt_reset(); |
|
73 |
- EepromTask(); |
|
74 |
- wdt_reset(); |
|
75 |
- RandomTask(); |
|
76 |
- wdt_reset(); |
|
77 |
- //RtlTask(); |
|
78 |
- wdt_reset(); |
|
79 |
- TimingTask(); |
|
80 |
- } |
|
115 |
+ while (1) |
|
116 |
+ mainWaitCf(); |
|
81 | 117 |
|
82 | 118 |
return 0; |
83 | 119 |
} |
... | ... |
@@ -0,0 +1,35 @@ |
1 |
+/* flaneth - flash and ethernet |
|
2 |
+ Copyright (C) 2007-2012 Stefan Schuermans <stefan@schuermans.info> |
|
3 |
+ Copyleft: GNU public license V2 - http://www.gnu.org/copyleft/gpl.html |
|
4 |
+ a BlinkenArea project - http://www.blinkenarea.org/ */ |
|
5 |
+ |
|
6 |
+#include <avr/wdt.h> |
|
7 |
+ |
|
8 |
+#include "arp.h" |
|
9 |
+#include "bus.h" |
|
10 |
+#include "cf.h" |
|
11 |
+#include "debug.h" |
|
12 |
+#include "eeprom.h" |
|
13 |
+#include "http.h" |
|
14 |
+#include "random.h" |
|
15 |
+#include "rtl8019.h" |
|
16 |
+#include "status.h" |
|
17 |
+#include "tasks.h" |
|
18 |
+#include "tcp.h" |
|
19 |
+#include "timing.h" |
|
20 |
+#include "uart.h" |
|
21 |
+ |
|
22 |
+// task function to call all tasks - call from main loop |
|
23 |
+void Tasks(void) // (extern) |
|
24 |
+{ |
|
25 |
+ wdt_reset(); |
|
26 |
+ EepromTask(); |
|
27 |
+ wdt_reset(); |
|
28 |
+ RandomTask(); |
|
29 |
+ wdt_reset(); |
|
30 |
+ RtlTask(); |
|
31 |
+ wdt_reset(); |
|
32 |
+ TimingTask(); |
|
33 |
+ wdt_reset(); |
|
34 |
+} |
|
35 |
+ |
... | ... |
@@ -0,0 +1,12 @@ |
1 |
+/* flaneth - flash and ethernet |
|
2 |
+ Copyright (C) 2007-2012 Stefan Schuermans <stefan@schuermans.info> |
|
3 |
+ Copyleft: GNU public license V2 - http://www.gnu.org/copyleft/gpl.html |
|
4 |
+ a BlinkenArea project - http://www.blinkenarea.org/ */ |
|
5 |
+ |
|
6 |
+#ifndef INC_tasks |
|
7 |
+#define INC_tasks |
|
8 |
+ |
|
9 |
+// task function to call all tasks - call from main loop |
|
10 |
+extern void Tasks(void); |
|
11 |
+ |
|
12 |
+#endif // #ifndef INC_tasks |
|
0 | 13 |