ebf9e5c12af1baf045dc1fdd114d2f4edcc27cd5
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) // sector buffer
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

102) unsigned char CfSectorBuffer[CF_SECTOR_SIZE];   // FIXME
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

103) 
104) // write a compact flash register (returns 1 if successful or 0 on error)
105) // - returns 0 on success and -1 on error
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

166) 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

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

219) 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

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

274)   debug_cf_printf("CF error");
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)   CfErrorState = 1;
277)   CfTickCnt = 0;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

278) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

280) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

283) }
284) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

288)   debug_cf_printf("CF gone");
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)   CfError();
291) }
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

292) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

293) // compact flash timeout
294) static void CfTimeout(void)
295) {
296)   debug_cf_printf("CF timeout");
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)   CfError();
299) }
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

300) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

301) // initialize
302) void CfInit(void)       // (extern)
303) {
304)   // setup ports
305)   bit_clear(CF_PORT_nCD, CF_BIT_nCD);   // pull-up of card detect pin off
306)                                         // (external pull-up is present)
307)   bit_clear(CF_DDR_nCD, CF_BIT_nCD);    // card detect pin to input
308)   bit_set(CF_PORT_nRST, CF_BIT_nRST);   // reset not active
309)   bit_set(CF_DDR_nRST, CF_BIT_nRST);    // reset pin to output
310)   bit_clear(CF_PORT_RDY, CF_BIT_RDY);   // pull-up of ready pin pin off
311)                                         // (external pull-up is present)
312)   bit_clear(CF_DDR_RDY, CF_BIT_RDY);    // ready pin to input
313)   bit_set(CF_PORT_nCE, CF_BIT_nCE);     // card not enabled
314)   bit_set(CF_DDR_nCE, CF_BIT_nCE);      // card enable pin to output
315) }
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

316) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

323) }
324) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

325) /**
326)  * @brief wait for CF to become ready
327)  * @param[in] timeout timeout in 20ms steps
328)  * @return 0 if CF ready, -1 if CF gone or timeout
329)  */
330) static int CfWaitReady(unsigned char timeout)
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

333) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

335) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

338)       CfGone();
339)       return -1;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

346)     }
347) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

350) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

352) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

354) }
355) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

356) /**
357)  * @brief read bytes from CF card
358)  * @param[in] ptr pointer to data buffer
359)  * @param[in] sz size of data to read
360)  * @return 0 if reading was successful, -1 if CF gon, timeout or error
361)  */
362) static int CfReadBytes(unsigned char *ptr, unsigned int sz)
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

364)   // read bytes
365)   if (CfReadRegBuf(CF_REG_DATA, ptr, sz) != 0) {
366)     debug_cf_printf("reading from CF failed (offset length 0x%03X)", sz);
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

370) 
371)   return 0;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

372) }
373) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

374) /**
375)  * @brief check if CF card is present
376)  * @return 1 if CF card is present, 0 if not
377)  */
378) char CfIsPresent(void) // (extern)
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

380)   if (CF_IS_DETECT())
381)     return 1;
382)   else
383)     return 0;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

384) }
385) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

386) /**
387)  * @brief reset CF card
388)  * @return 0 if CF card is present and could be reset, -1 if not
389)  */
390) char CfReset(void) // (extern)
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

394)     CfGone();
395)     return -1;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

398)   while (CfTickCnt > 0)
399)     Tasks();
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

400) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

412) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

416) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

421) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

423) }
424) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

425) /**
426)  * @brief identify CF card
427)  * @param[out] sectors number of sectors on CF card
428)  * @return 0 if CF card could be identified, -1 if not
429)  */
430) char CfIdentify(unsigned long *sectors) // (extern)
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

434) 
435)   debug_cf_printf("CF identify");
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

495)   return 0;
496) }
497) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

498) /**
499)  * @brief read from CF card
500)  * @param[in] sector number of sector to read
501)  * @param[out] data data read from sector
502)  * @return 0 if sector could be read, -1 if not
503)  */
504) char CfRead(unsigned long sector, unsigned char data[CF_SECTOR_SIZE]) // (extern)
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

507) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

517) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

518)   // set sector number (LBA mode) and sector count (1)
519)   if (CfWriteReg(CF_REG_HEAD, 0xE0 | (sector >> 24 & 0x0F)) != 0 ||
520)       CfWriteReg(CF_REG_CYL_H, sector >> 16) != 0 ||
521)       CfWriteReg(CF_REG_CYL_L, sector >> 8) != 0 ||
522)       CfWriteReg(CF_REG_SEC_NO, sector) != 0 ||
523)       CfWriteReg(CF_REG_SEC_CNT, 1) != 0) {
524)     CfError();
525)     return -1;
526)   }
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

527) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

528)   // issue read sector command
529)   if (CfWriteReg(CF_REG_CMD, CF_CMD_READ_SEC) != 0) {
530)     CfError();
531)     return -1;
532)   }
533)   if (CfWaitReady(CF_READY_TIMEOUT) != 0)
534)     return -1;
535) 
536)   // read status register
537)   CfReadReg(CF_REG_STATUS, &status);
538)   // check that BUSY=0, RDY=1, DWF=0, DSC=1, IDX=0, ERR=0
539)   if ((status &
540)        (1 << CF_SB_BUSY | 1 << CF_SB_RDY | 1 << CF_SB_DWF | 1 << CF_SB_DSC |
541)         1 << CF_SB_IDX | 1 << CF_SB_ERR)) !=
542)       (1 << CF_SB_RDY | 1 << CF_SB_DSC)) {
543)     debug_cf_printf("unexpected status 0x%02X (identify)", status);
544)     CfError();
545)     return -1;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

546)   }
547) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

553) }