1 | RESET(MCP2515_CS);
|
2 | spi_putc_async(0x90);
|
3 | /* then prepare the SDCard block in the meantime */
|
4 | mmc_prepareBlock();
|
5 | /* and finish the async read from mcp*/
|
6 | spi_getc_async();
|
7 | /* now start reading the bytes */
|
8 | spi_putc_async(0xff); /* sending the first byte request */
|
9 |
|
10 | /* byte 1 read from mcp + write to SD */
|
11 | b=spi_getc_async(); /* waiting for the MCP-byte to return*/ \
|
12 | spi_putc_async(0xff); /* sending the 2nd MCP-byte */ \
|
13 | uspi_putc_async(b); /* push the byte to the SD */
|
14 | *bp++=b; /* store the MCP-byte */ \
|
15 |
|
16 | #define HELPER() \
|
17 | b=spi_getc_async(); /* waiting for the MCP-byte to return*/ \
|
18 | spi_putc_async(0xff); /* sending the next MCP-byte request*/ \
|
19 | uspi_getc_async(); /* waiting for the SD-byte to return*/ \
|
20 | uspi_putc_async(b); /* push the byte to the SD */ \
|
21 | *bp++=b; /* store the current MCP-byte */ \
|
22 |
|
23 | HELPER();/* byte 2 read from mcp + write to SD */
|
24 | HELPER();/* byte 3 read from mcp + write to SD */
|
25 | HELPER();/* byte 4 read from mcp + write to SD */
|
26 | HELPER();/* byte 5 read from mcp + write to SD */
|
27 | HELPER();/* byte 6 read from mcp + write to SD */
|
28 | HELPER();/* byte 7 read from mcp + write to SD */
|
29 | HELPER();/* byte 8 read from mcp + write to SD */
|
30 | HELPER();/* byte 9 read from mcp + write to SD */
|
31 | HELPER();/* byte 10 read from mcp + write to SD */
|
32 | HELPER();/* byte 11 read from mcp + write to SD */
|
33 | HELPER();/* byte 12 read from mcp + write to SD */
|
34 |
|
35 | /* byte 13 is special again */
|
36 | b=spi_getc_async(); /* waiting for the MCP-byte to return*/
|
37 | SET(MCP2515_CS); /* pulling byte high as early as possible */
|
38 | uspi_getc_async(); /* waiting for the MCP-byte to return*/
|
39 | uspi_putc_async(b); /* push the last byte to the SD */
|
40 | *bp++=b; /* store the MCP-byte */
|
41 |
|
42 | /* read byte from SD */
|
43 | uspi_getc_async();
|
44 |
|
45 | /* and we finish the block (triggering writes and such...)*/
|
46 | mmc_finishBlock();
|
47 |
|
48 | ...
|
49 |
|
50 | void mmc_finishBlock() {
|
51 | /* if not at the end of the 512 byte block, return */
|
52 | if (condition_not_reached) {return;}
|
53 | /* finish the block with 2 bytes of bogus checksums */
|
54 | uspi_putc(0xde);
|
55 | uspi_putc(0xad);
|
56 | /* and wait for status */
|
57 | uint8_t response=uspi_putc(0xff);
|
58 | if (response!=0xff) { ; }
|
59 | /* and wait for the finished writing block - low until then...*/
|
60 | while(uspi_putc(0xaa)==0) ;
|
61 | /* wait some time - for some reason the next happens too quick before the data is out*/
|
62 | //_delay_us(1);
|
63 | /* and now pull line high */
|
64 | SET(MMC_CS);
|
65 | }
|
66 |
|
67 | inline void spi_putc_async(uint8_t data) {
|
68 | SPDR = data;
|
69 | }
|
70 |
|
71 | inline uint8_t spi_getc_async(void) {
|
72 | while( !( SPSR & (1<<SPIF) ) ) { ; }
|
73 | return SPDR;
|
74 | }
|
75 |
|
76 | inline void uspi_putc_async(uint8_t data) {
|
77 | UDR0 = data;
|
78 | }
|
79 | inline uint8_t uspi_getc_async(void) {
|
80 | while( !( UCSR0A & (1<<UDRE0) ) ) { ; }
|
81 | return UDR0;
|
82 | }
|