64834351d4707e742f2d7ab333c7411682c13e70
Stefan Schuermans header fix

Stefan Schuermans authored 12 years ago

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/ */
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

5) 
6) #include <avr/io.h>
7) 
8) #include "bus.h"
9) #include "cf.h"
10) #include "config.h"
11) #include "debug.h"
12) #include "macros.h"
13) #include "status.h"
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

14) #include "tasks.h"
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

15) #include "timing.h"
16) 
17) // IO pins of compact flash
18) #define CF_DDR_nCD (DDRB)
19) #define CF_PORT_nCD (PORTB)
20) #define CF_PIN_nCD (PINB)
21) #define CF_BIT_nCD (4)
22) #define CF_DDR_nRST (DDRB)
23) #define CF_PORT_nRST (PORTB)
24) #define CF_BIT_nRST (5)
25) #define CF_DDR_RDY (DDRB)
26) #define CF_PORT_RDY (PORTB)
27) #define CF_PIN_RDY (PINB)
28) #define CF_BIT_RDY (7)
29) #define CF_DDR_nCE (DDRB)
30) #define CF_PORT_nCE (PORTB)
31) #define CF_BIT_nCE (6)
32) // special pin commands
33) #define CF_IS_DETECT() (is_bit_clear(CF_PIN_nCD, CF_BIT_nCD))
34) #define CF_RESET_ACT() (bit_clear(CF_PORT_nRST, CF_BIT_nRST))
35) #define CF_RESET_IDLE() (bit_set(CF_PORT_nRST, CF_BIT_nRST))
36) #define CF_IS_READY() (is_bit_set(CF_PIN_RDY, CF_BIT_RDY))
37) #define CF_CE_ACT() (bit_clear(CF_PORT_nCE, CF_BIT_nCE))
38) #define CF_CE_IDLE() (bit_set(CF_PORT_nCE, CF_BIT_nCE))
39) 
40) // compact flash registers
41) #define CF_REG_DATA (0x00)
42) #define CF_REG_ERR (0x01)
43) #define CF_REG_SEC_CNT (0x02)
44) #define CF_REG_SEC_NO (0x03)
45) #define CF_REG_CYL_L (0x04)
46) #define CF_REG_CYL_H (0x05)
47) #define CF_REG_HEAD (0x06)
48) #define CF_REG_STATUS (0x07)
49) #define CF_REG_CMD (0x07)
50) 
51) // compact flash status bits
52) #define CF_SB_BUSY (7)
53) #define CF_SB_RDY (6)
54) #define CF_SB_DWF (5)
55) #define CF_SB_DSC (4)
56) #define CF_SB_DRQ (3)
57) #define CF_SB_CORR (2)
58) #define CF_SB_IDX (1)
59) #define CF_SB_ERR (0)
60) 
Stefan Schuermans allow more time for CF to p...

Stefan Schuermans authored 12 years ago

61) // time to give CF to power up (in 20ms steps)
62) #define CF_POWERUP_TIME (5) // 100ms
63) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

64) // timeout value for wait for ready after reset counter (in 20ms steps)
65) #define CF_RESET_READY_TIMEOUT (50)    // 1s
66) 
67) // timeout value for wait for ready counter (in 20ms steps)
68) #define CF_READY_TIMEOUT (4)    // 80ms
69) 
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

70) // compact flash commands
71) #define CF_CMD_IDENTIFY (0xEC)
72) #define CF_CMD_READ_SEC (0x20)
73) #define CF_CMD_WRITE_SEC (0x30)
74) 
75) // some number of bytes
76) #define CF_BYTES_IDENTIFY (124) // number of bytes to read for identify
77) 
78) // interesting locations in identify data
79) #define CF_ID_OFS_ID_16 (0)     // CF identifier, 16 bit
80) #define CF_ID_OFS_CAPA_16 (98)  // CF capabilities, 16 bit
81) #define CF_ID_OFS_SEC_CNT_32 (120)      // number of sectors on CF (in LBA
82)                                         // mode), 32 bit
83) 
84) // various CF constants
85) #define CF_ID (0x848A)  // identifier of compact flash cards
86) #define CF_CAPA_BIT_LBA (9)     // bit number of the LBA bit in the CF
87)                                 // capabilities
88) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

89) // compact flash error state (0 for no error, 1 for error)
90) char CfErrorState = 1;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

91) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

92) // tick/timeout counter of compact flash
93) // - counts down in 20ms steps, stops at zero
94) unsigned char CfTickCnt = 0;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

95) 
96) // number of sectors on compact flash card
97) // - zero if no CF card is present, CF card has not yet been identified or CF 
98) // card error occured
99) unsigned long CfSectorCnt = 0;
100) 
101) // write a compact flash register (returns 1 if successful or 0 on error)
102) // - returns 0 on success and -1 on error
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

103) extern inline char CfWriteReg(unsigned char reg, unsigned char val)
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

104) {
105)   if (!CF_IS_DETECT() || !CF_IS_READY())        // check that card is present 
106)                                                 // and ready
107)     return -1;  // error
108)   BUS_DATA = val;       // output value
109)   BUS_DATA_DDR = 0xFF;  // data port to output
110)   BUS_ADDR = reg;       // output address
111)   CF_CE_ACT();  // set card enable
112)   BUS_WRITE_ACT();      // activate write signal
113)   nop();
114)   nop();
115)   nop();
116)   nop();
117)   nop();
118)   nop();
119)   BUS_WRITE_IDLE();     // take back write signal
120)   CF_CE_IDLE(); // take back card enable
121)   BUS_DATA_DDR = 0x00;  // data back port to input
122)   BUS_DATA = 0x00;      // turn off pullups
123)   return 0;     // success
124) }
125) 
126) // write buffer to compact flash register
127) // (returns 1 if successful or 0 on error)
128) // - returns 0 on success and -1 on error
129) extern inline char CfWriteRegBuf(unsigned char reg, unsigned char *p_buf,
130)                                  unsigned short len)
131) {
132)   if (!CF_IS_DETECT() || !CF_IS_READY())        // check that card is present 
133)                                                 // and ready
134)     return -1;  // error
135)   BUS_DATA = *p_buf;    // output first value to initialize port status
136)                         // before switching to output
137)   BUS_DATA_DDR = 0xFF;  // data port to output
138)   BUS_ADDR = reg;       // output address
139)   CF_CE_ACT();  // set card enable
140)   for (; len > 0; p_buf++, len--) {
141)     if (!CF_IS_DETECT() || !CF_IS_READY())      // check that card is present 
142)                                                 // and ready
143)       break;
144)     BUS_DATA = *p_buf;  // output value
145)     BUS_WRITE_ACT();    // activate write signal
146)     nop();
147)     nop();
148)     nop();
149)     nop();
150)     nop();
151)     nop();
152)     BUS_WRITE_IDLE();   // take back write signal
153)   }
154)   CF_CE_IDLE(); // take back card enable
155)   BUS_DATA_DDR = 0x00;  // data back port to input
156)   BUS_DATA = 0x00;      // turn off pullups
157)   return len <= 0 ? 0 : -1;     // success if everything has been written
158) }
159) 
160) // write constant value to compact flash register multiple times (returns 1
161) // if successful or 0 on error)
162) // - returns 0 on success and -1 on error
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

163) extern inline char CfWriteRegConst(unsigned char reg, unsigned char val, unsigned short cnt)
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

164) {
165)   if (!CF_IS_DETECT() || !CF_IS_READY())        // check that card is present 
166)                                                 // and ready
167)     return -1;  // error
168)   BUS_DATA = val;       // output value
169)   BUS_DATA_DDR = 0xFF;  // data port to output
170)   BUS_ADDR = reg;       // output address
171)   CF_CE_ACT();  // set card enable
172)   for (; cnt > 0; cnt--) {
173)     if (!CF_IS_DETECT() || !CF_IS_READY())      // check that card is present 
174)                                                 // and ready
175)       break;
176)     BUS_WRITE_ACT();    // activate write signal
177)     nop();
178)     nop();
179)     nop();
180)     nop();
181)     nop();
182)     nop();
183)     BUS_WRITE_IDLE();   // take back write signal
184)   }
185)   CF_CE_IDLE(); // take back card enable
186)   BUS_DATA_DDR = 0x00;  // data back port to input
187)   BUS_DATA = 0x00;      // turn off pullups
188)   return cnt <= 0 ? 0 : -1;     // success if everything has been written
189) }
190) 
191) // read a compact flash register (returns 1 if successful or 0 on error)
192) // - returns 0 on success and -1 on error
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

193) extern inline char CfReadReg(unsigned char reg, unsigned char *p_var)
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

194) {
195)   if (!CF_IS_DETECT() || !CF_IS_READY())        // check that card is present 
196)                                                 // and ready
197)     return -1;  // error
198)   BUS_ADDR = reg;       // output address
199)   CF_CE_ACT();  // set card enable
200)   BUS_READ_ACT();       // activate read signal
201)   nop();
202)   nop();
203)   nop();
204)   nop();
205)   nop();
206)   nop();
207)   *p_var = BUS_DATA_IN; // read data
208)   BUS_READ_IDLE();      // take back read signal
209)   CF_CE_IDLE(); // take back card enable
210)   return 0;     // success
211) }
212) 
213) // read buffer from a compact flash register (returns 1 if successful or 0 on 
214) // error)
215) // - returns 0 on success and -1 on error
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

216) extern inline char CfReadRegBuf(unsigned char reg, unsigned char *p_buf, unsigned short len)
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

217) {
218)   if (!CF_IS_DETECT() || !CF_IS_READY())        // check that card is present 
219)                                                 // and ready
220)     return -1;  // error
221)   BUS_ADDR = reg;       // output address
222)   CF_CE_ACT();  // set card enable
223)   for (; len > 0; p_buf++, len--) {
224)     if (!CF_IS_DETECT() || !CF_IS_READY())      // check that card is present 
225)                                                 // and ready
226)       break;
227)     BUS_READ_ACT();     // activate read signal
228)     nop();
229)     nop();
230)     nop();
231)     nop();
232)     nop();
233)     nop();
234)     *p_buf = BUS_DATA_IN;       // read data
235)     BUS_READ_IDLE();    // take back read signal
236)   }
237)   CF_CE_IDLE(); // take back card enable
238)   return len <= 0 ? 0 : -1;     // success if everything has been read
239) }
240) 
241) // read a compact flash register multiple times and throw away data (returns
242) // 1 if successful or 0 on error)
243) // - returns 0 on success and -1 on error
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

244) extern inline char CfReadRegMulti(unsigned char reg, unsigned short cnt)
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

245) {
246)   if (!CF_IS_DETECT() || !CF_IS_READY())        // check that card is present 
247)                                                 // and ready
248)     return -1;  // error
249)   BUS_ADDR = reg;       // output address
250)   CF_CE_ACT();  // set card enable
251)   for (; cnt > 0; cnt--) {
252)     if (!CF_IS_DETECT() || !CF_IS_READY())      // check that card is present 
253)                                                 // and ready
254)       break;
255)     BUS_READ_ACT();     // activate read signal
256)     nop();
257)     nop();
258)     nop();
259)     nop();
260)     nop();
261)     nop();
262)     BUS_READ_IDLE();    // take back read signal
263)   }
264)   CF_CE_IDLE(); // take back card enable
265)   return cnt <= 0 ? 0 : -1;     // success if everything has been read
266) }
267) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

268) // compact flash error occured
269) static void CfError(void)
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

270) {
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

271)   debug_cf_printf("CF error");
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

272) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

273)   CfErrorState = 1;
274)   CfTickCnt = 0;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

275) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

276)   CfSectorCnt = 0;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

277) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

278)   // report no working CF present
279)   StatusInfoCfPresent = 0;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

280) }
281) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

282) // compact flash is gone
283) static void CfGone(void)
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

284) {
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

285)   debug_cf_printf("CF gone");
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

286) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

287)   CfError();
288) }
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

289) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

290) // compact flash timeout
291) static void CfTimeout(void)
292) {
293)   debug_cf_printf("CF timeout");
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

294) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

295)   CfError();
296) }
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

297) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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
312) }
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

313) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

314) // tick procedure - call every 20ms
315) void CfTick20(void)     // (extern)
316) {
317)   // count down ticks
318)   if (CfTickCnt > 0)
319)     --CfTickCnt;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

320) }
321) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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)
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

328) {
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

329)   CfTickCnt = timeout;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

330) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

331)   while (!CF_IS_READY()) {
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

332) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

333)     // card gone -> error
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

334)     if (!CF_IS_DETECT()) {
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

335)       CfGone();
336)       return -1;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

337)     }
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

338) 
339)     // timeout -> error
340)     if (CfTickCnt <= 0) {
341)       CfTimeout();
342)       return -1;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

343)     }
344) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

345)     // execute other tasks while waiting
346)     Tasks();
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

347) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

348)   } // while (!CF_IS_READY())
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

349) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

350)   return 0;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

351) }
352) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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)
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

360) {
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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);
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

364)     CfError();
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

365)     return -1;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

366)   }
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

367) 
368)   return 0;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

369) }
370) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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)
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

376) {
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

377)   if (CF_IS_DETECT())
378)     return 1;
379)   else
380)     return 0;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

381) }
382) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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)
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

388) {
Stefan Schuermans allow more time for CF to p...

Stefan Schuermans authored 12 years ago

389)   // wait a little bit
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

390)   if (!CF_IS_DETECT()) {
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

391)     CfGone();
392)     return -1;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

393)   }
Stefan Schuermans allow more time for CF to p...

Stefan Schuermans authored 12 years ago

394)   CfTickCnt = CF_POWERUP_TIME;
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

395)   while (CfTickCnt > 0)
396)     Tasks();
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

397) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

398)   // set reset, wait, take back reset
399)   if (!CF_IS_DETECT()) {
400)     CfGone();
401)     return -1;
402)   }
403)   CF_RESET_ACT();
404)   nop();
405)   nop();
406)   nop();
407)   CF_RESET_IDLE();
408)   debug_cf_printf("CF reset");
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

409) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

410)   // wait for CF to become ready
411)   if (CfWaitReady(CF_RESET_READY_TIMEOUT) != 0)
412)     return -1;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

413) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

414)   // report working CF present
415)   CfErrorState = 0;
416)   StatusInfoCfPresent = 1;
417)   debug_cf_printf("CF ready");
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

418) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

419)   return 0;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

420) }
421) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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)
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

428) {
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

429)   unsigned char status, identifyBuf[CF_BYTES_IDENTIFY];
430)   unsigned short val16;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

431) 
432)   debug_cf_printf("CF identify");
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

433)   if (CfErrorState) {
434)     debug_cf_printf("CF in error state");
435)     return -1;
436)   }
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

437) 
438)   // issue identify drive command
439)   if (CfWriteReg(CF_REG_CMD, CF_CMD_IDENTIFY) != 0) {
440)     CfError();
441)     return -1;
442)   }
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

443)   if (CfWaitReady(CF_READY_TIMEOUT) != 0)
444)     return -1;
445) 
446)   // read status register
447)   CfReadReg(CF_REG_STATUS, &status);
448)   // check that BUSY=0, RDY=1, DWF=0, DSC=1, IDX=0, ERR=0
449)   if ((status &
450)        (1 << CF_SB_BUSY | 1 << CF_SB_RDY | 1 << CF_SB_DWF | 1 << CF_SB_DSC |
451)         1 << CF_SB_IDX | 1 << CF_SB_ERR)) !=
452)       (1 << CF_SB_RDY | 1 << CF_SB_DSC)) {
453)     debug_cf_printf("unexpected status 0x%02X (identify)", status);
454)     CfError();
455)     return -1;
456)   }
457) 
458)   // read identify bytes
459)   if (CfReadBytes(identifyBuf, sizeof(identifyBuf)) != 0)
460)     return -1;
461) 
462)   // check identifier
463)   val16 = (unsigned short)identifyBuf[CF_ID_OFS_ID_16 + 0] |
464)           (unsigned short)identifyBuf[CF_ID_OFS_ID_16 + 1] << 8;
465)   if (val16 != CF_ID) {
466)     debug_cf_printf("invalid CF identifier: 0x%04X", val16);
467)     CfError();
468)     return -1;
469)   }
470) 
471)   // check if LBA mode is supported
472)   val16 = (unsigned short)identifyBuf[CF_ID_OFS_CAPA_16 + 0]
473)       | (unsigned short)identifyBuf[CF_ID_OFS_CAPA_16 + 1] << 8;
474)   if ((val16 & 1 << CF_CAPA_BIT_LBA) == 0) {
475)     debug_cf_printf("CF does not support LBA mode");
476)     CfError();
477)     return -1;
478)   }
479)   // get number of sectors on CF
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;
484)   if (CfSectorCnt <= 0) {
485)     debug_cf_printf("CF does not contain any sectors");
486)     CfError();
487)     return -1;
488)   }
489) 
490)   debug_cf_printf("CF done (%lu sectors)", CfSectorCnt);
491) 
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

492)   return 0;
493) }
494) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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)
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

502) {
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

503)   unsigned char status;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

504) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

505)   debug_cf_printf("CF read (sector %lu)", sector);
506)   if (CfErrorState) {
507)     debug_cf_printf("CF in error state");
508)     return -1;
509)   }
510)   if (sector >= CfSectorCnt) {
511)     debug_cf_printf("sector does not exist");
512)     return -1;
513)   }
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

514) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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) {
521)     CfError();
522)     return -1;
523)   }
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

524) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

525)   // issue read sector command
526)   if (CfWriteReg(CF_REG_CMD, CF_CMD_READ_SEC) != 0) {
527)     CfError();
528)     return -1;
529)   }
530)   if (CfWaitReady(CF_READY_TIMEOUT) != 0)
531)     return -1;
532) 
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;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

543)   }
544) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

545)   // read sector bytes
546)   if (CfReadBytes(data, CF_SECTOR_SIZE) != 0)
547)     return -1;
548) 
549)   return 0;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

550) }