db947be359b803c749d12499f555ded2fe975237
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 give some time to CF to set...

Stefan Schuermans authored 12 years ago

64) // time to give CF to set outputs after reset (in 20ms steps)
65) #define CF_POST_RESET_TIME (2) // 40ms
66) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

94) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

98) 
99) // number of sectors on compact flash card
100) // - zero if no CF card is present, CF card has not yet been identified or CF 
101) // card error occured
102) unsigned long CfSectorCnt = 0;
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 give some time to CF to set...

Stefan Schuermans authored 12 years ago

413)   // wait a little bit
414)   if (!CF_IS_DETECT()) {
415)     CfGone();
416)     return -1;
417)   }
418)   CfTickCnt = CF_POST_RESET_TIME;
419)   while (CfTickCnt > 0)
420)     Tasks();
421) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

425) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

426)   // report working CF present
427)   CfErrorState = 0;
428)   StatusInfoCfPresent = 1;
429)   debug_cf_printf("CF ready");
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

430) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

432) }
433) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

434) /**
435)  * @brief identify CF card
436)  * @param[out] sectors number of sectors on CF card
437)  * @return 0 if CF card could be identified, -1 if not
438)  */
439) char CfIdentify(unsigned long *sectors) // (extern)
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

443) 
444)   debug_cf_printf("CF identify");
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

445)   if (CfErrorState) {
446)     debug_cf_printf("CF in error state");
447)     return -1;
448)   }
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

449) 
450)   // issue identify drive command
451)   if (CfWriteReg(CF_REG_CMD, CF_CMD_IDENTIFY) != 0) {
452)     CfError();
453)     return -1;
454)   }
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

503)   *sectors = CfSectorCnt;
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

504) 
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

505)   return 0;
506) }
507) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

508) /**
509)  * @brief read from CF card
510)  * @param[in] sector number of sector to read
511)  * @param[out] data data read from sector
512)  * @return 0 if sector could be read, -1 if not
513)  */
514) char CfRead(unsigned long sector, unsigned char data[CF_SECTOR_SIZE]) // (extern)
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

516)   unsigned char status;
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)   debug_cf_printf("CF read (sector %lu)", sector);
519)   if (CfErrorState) {
520)     debug_cf_printf("CF in error state");
521)     return -1;
522)   }
523)   if (sector >= CfSectorCnt) {
524)     debug_cf_printf("sector does not exist");
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)   // set sector number (LBA mode) and sector count (1)
529)   if (CfWriteReg(CF_REG_HEAD, 0xE0 | (sector >> 24 & 0x0F)) != 0 ||
530)       CfWriteReg(CF_REG_CYL_H, sector >> 16) != 0 ||
531)       CfWriteReg(CF_REG_CYL_L, sector >> 8) != 0 ||
532)       CfWriteReg(CF_REG_SEC_NO, sector) != 0 ||
533)       CfWriteReg(CF_REG_SEC_CNT, 1) != 0) {
534)     CfError();
535)     return -1;
536)   }
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

537) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

538)   // issue read sector command
539)   if (CfWriteReg(CF_REG_CMD, CF_CMD_READ_SEC) != 0) {
540)     CfError();
541)     return -1;
542)   }
543)   if (CfWaitReady(CF_READY_TIMEOUT) != 0)
544)     return -1;
545) 
546)   // read status register
547)   CfReadReg(CF_REG_STATUS, &status);
548)   // check that BUSY=0, RDY=1, DWF=0, DSC=1, IDX=0, ERR=0
549)   if ((status &
550)        (1 << CF_SB_BUSY | 1 << CF_SB_RDY | 1 << CF_SB_DWF | 1 << CF_SB_DSC |
551)         1 << CF_SB_IDX | 1 << CF_SB_ERR)) !=
552)       (1 << CF_SB_RDY | 1 << CF_SB_DSC)) {
553)     debug_cf_printf("unexpected status 0x%02X (identify)", status);
554)     CfError();
555)     return -1;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

556)   }
557) 
Stefan Schuermans converted CF processing to...

Stefan Schuermans authored 12 years ago

558)   // read sector bytes
559)   if (CfReadBytes(data, CF_SECTOR_SIZE) != 0)
560)     return -1;
561) 
562)   return 0;
Stefan Schuermans initial commit after making...

Stefan Schuermans authored 12 years ago

563) }