format the code using uncrustify

migrate uart.[ch] to a BSD style license by reimplementing it
This commit is contained in:
Oliver Lehmann 2013-05-04 21:22:53 +00:00
parent 33fd0a9dc5
commit 34155a6dc6
1 changed files with 96 additions and 72 deletions

View File

@ -26,7 +26,7 @@
*/ */
/* /*
* $Id: wdc_drv_mmc.c,v 1.22 2013/04/20 23:22:47 olivleh1 Exp $ * $Id: wdc_drv_mmc.c,v 1.23 2013/05/04 21:22:53 olivleh1 Exp $
*/ */
#include <avr/io.h> #include <avr/io.h>
@ -35,23 +35,23 @@
#include "wdc_drv_mmc.h" #include "wdc_drv_mmc.h"
#include "uart.h" #include "uart.h"
#define wait_till_send_done() while ( ! ( SPSR & ( 1 << SPIF ) ) ) #define wait_till_send_done() while ( !( SPSR & ( 1 << SPIF ) ) )
#define wait_till_card_ready() do { send_dummy_byte(); } while ( !recv_byte() ) #define wait_till_card_ready() do { send_dummy_byte(); } while ( !recv_byte() )
#define send_dummy_byte() SPDR = 0xFF; wait_till_send_done() #define send_dummy_byte() SPDR = 0xFF; wait_till_send_done()
#define recv_byte() SPDR #define recv_byte() SPDR
#define xmit_byte(x) SPDR = x #define xmit_byte( x ) SPDR = x
#define CMD0 (0x40 + 0) #define CMD0 ( 0x40 + 0 )
#define CMD1 (0x40 + 1) #define CMD1 ( 0x40 + 1 )
#define CMD8 (0x40 + 8) #define CMD8 ( 0x40 + 8 )
#define CMD12 (0x40 + 12) #define CMD12 ( 0x40 + 12 )
#define CMD17 (0x40 + 17) #define CMD17 ( 0x40 + 17 )
#define CMD18 (0x40 + 18) #define CMD18 ( 0x40 + 18 )
#define CMD23 (0x40 + 23) #define CMD23 ( 0x40 + 23 )
#define CMD24 (0x40 + 24) #define CMD24 ( 0x40 + 24 )
#define CMD25 (0x40 + 25) #define CMD25 ( 0x40 + 25 )
#define CMD55 (0x40 + 55) #define CMD55 ( 0x40 + 55 )
#define CMD59 (0x40 + 59) #define CMD59 ( 0x40 + 59 )
#define SB_START 0xFE #define SB_START 0xFE
#define MB_START 0xFC #define MB_START 0xFC
@ -147,23 +147,26 @@ const uint8_t crc7_table[256] = {
uint8_t crc7 ( const uint8_t *buffer, uint8_t len ) uint8_t crc7 ( const uint8_t *buffer, uint8_t len )
{ {
uint8_t crc = 0x00; uint8_t crc = 0x00;
while ( len-- )
crc = crc7_table[ ( crc << 1 ) ^ *buffer++]; while ( len-- ) {
crc = crc7_table[( crc << 1 ) ^ *buffer++];
}
return crc; return crc;
} }
uint16_t crc16 ( const uint8_t *buffer, uint16_t len ) uint16_t crc16 ( const uint8_t *buffer, uint16_t len )
{ {
uint16_t crc = 0x0000; uint16_t crc = 0x0000;
while ( len-- ) { while ( len-- ) {
crc = ( crc << 8 ) ^ crc16_table[ ( crc >> 8 ) ^ *buffer++]; crc = ( crc << 8 ) ^ crc16_table[( crc >> 8 ) ^ *buffer++];
} }
return crc; return crc;
} }
uint8_t mmc_enable_crc ( uint8_t on_off ) uint8_t mmc_enable_crc ( uint8_t on_off )
{ {
unsigned char cmd[] = {CMD59, 0x00, 0x00, 0x00, 0x00, 0xFF}; uint8_t cmd[] = { CMD59, 0x00, 0x00, 0x00, 0x00, 0xFF };
/* last bit enables or disables CRC mode */ /* last bit enables or disables CRC mode */
cmd[4] = on_off & 0x01; cmd[4] = on_off & 0x01;
@ -173,6 +176,7 @@ uint8_t mmc_enable_crc ( uint8_t on_off )
} }
return 0; return 0;
} }
#endif #endif
uint8_t mmc_init () uint8_t mmc_init ()
@ -180,9 +184,12 @@ uint8_t mmc_init ()
uint16_t t16 = 0; uint16_t t16 = 0;
uint8_t a; uint8_t a;
uint8_t cmd[6]; uint8_t cmd[6];
uart_putstring ( PSTR ( "INFO: SDCard init start" ), true );
for ( a = 0; a < 200; a++ ) { for ( a = 0; a < 200; a++ ) {
nop(); nop();
}; }
SPCR = ( 1 << SPE ) | ( 1 << MSTR ) | ( 1 << SPR0 ) | ( 1 << SPR1 ); /* Enable SPI, SPI in Master Mode */ SPCR = ( 1 << SPE ) | ( 1 << MSTR ) | ( 1 << SPR0 ) | ( 1 << SPR1 ); /* Enable SPI, SPI in Master Mode */
SPSR = ( 0 << SPI2X ); SPSR = ( 0 << SPI2X );
@ -207,7 +214,7 @@ uint8_t mmc_init ()
while ( mmc_cmd ( cmd ) != 1 ) { while ( mmc_cmd ( cmd ) != 1 ) {
if ( t16++ > 1000 ) { if ( t16++ > 1000 ) {
disable_mmc(); disable_mmc();
return ( 1 ); return 1;
} }
} }
@ -223,8 +230,9 @@ uint8_t mmc_init ()
/* if SDHC Card */ /* if SDHC Card */
if ( a <= 1 ) { if ( a <= 1 ) {
/* skip 6 bytes */ /* skip 6 bytes */
for ( a = 0; a < 6; a++ ) for ( a = 0; a < 6; a++ ) {
send_dummy_byte(); send_dummy_byte();
}
is_sdhc = 1; is_sdhc = 1;
@ -252,14 +260,14 @@ uint8_t mmc_init ()
send_dummy_byte(); send_dummy_byte();
if ( t16++ > 1000 ) { if ( t16++ > 1000 ) {
disable_mmc(); disable_mmc();
return ( 2 ); return 2;
} }
} }
send_dummy_byte(); send_dummy_byte();
/* SPI Bus to max. speed */ /* SPI Bus to max. speed */
SPCR &= ~ ( ( 1 << SPR0 ) | ( 1 << SPR1 ) ); SPCR &= ~( ( 1 << SPR0 ) | ( 1 << SPR1 ) );
SPSR = SPSR | ( 1 << SPI2X ); SPSR = SPSR | ( 1 << SPI2X );
#ifdef SPI_CRC #ifdef SPI_CRC
@ -267,7 +275,7 @@ uint8_t mmc_init ()
#endif #endif
disable_mmc(); disable_mmc();
return ( 0 ); return 0;
} }
uint8_t mmc_cmd ( uint8_t *cmd ) uint8_t mmc_cmd ( uint8_t *cmd )
@ -276,6 +284,7 @@ uint8_t mmc_cmd ( uint8_t *cmd )
uint8_t i = 10; uint8_t i = 10;
uint8_t cmd0 = cmd[0]; uint8_t cmd0 = cmd[0];
uint8_t a; uint8_t a;
#ifdef SPI_CRC #ifdef SPI_CRC
/* Calculate CRC and framing bits */ /* Calculate CRC and framing bits */
cmd[0] = ( cmd[0] | ( 1 << 6 ) ) & 0x7F; cmd[0] = ( cmd[0] | ( 1 << 6 ) ) & 0x7F;
@ -288,21 +297,29 @@ uint8_t mmc_cmd ( uint8_t *cmd )
wait_till_send_done(); wait_till_send_done();
} }
if ( cmd0 == ( CMD12 ) ) recv_byte(); if ( cmd0 == ( CMD12 ) ) {
recv_byte();
}
do { do {
send_dummy_byte(); send_dummy_byte();
tmp = recv_byte(); tmp = recv_byte();
} while ( ( tmp & 0x80 ) && --i ); } while ( ( tmp & 0x80 ) && --i );
return ( tmp );
if ( !tmp ) {
uart_putstring ( PSTR ( "INFO: SDCard disk has been found" ), true );
}
return tmp;
} }
uint8_t mmc_write_sector ( uint32_t addr, uint8_t *buffer ) uint8_t mmc_write_sector ( uint32_t addr, uint8_t *buffer )
{ {
sint32 x; sint32 x;
uint8_t tmp; uint8_t tmp;
uint8_t cmd[] = {CMD24, 0x00, 0x00, 0x00, 0x00, 0xFF}; uint8_t cmd[] = { CMD24, 0x00, 0x00, 0x00, 0x00, 0xFF };
uint16_t i; uint16_t i;
#ifdef SPI_CRC #ifdef SPI_CRC
uint16_t crc; uint16_t crc;
uint8_t crcl, crch; uint8_t crcl, crch;
@ -312,8 +329,9 @@ uint8_t mmc_write_sector ( uint32_t addr, uint8_t *buffer )
wait_till_card_ready(); wait_till_card_ready();
/* convert blocks to bytes */ /* convert blocks to bytes */
if ( !is_sdhc ) if ( !is_sdhc ) {
addr = addr * MMC_BLOCKLEN; addr = addr * MMC_BLOCKLEN;
}
x.value32 = addr; x.value32 = addr;
cmd[1] = x.value8.hh; cmd[1] = x.value8.hh;
cmd[2] = x.value8.hl; cmd[2] = x.value8.hl;
@ -326,7 +344,7 @@ uint8_t mmc_write_sector ( uint32_t addr, uint8_t *buffer )
tmp = mmc_cmd ( cmd ); tmp = mmc_cmd ( cmd );
if ( tmp != 0 ) { if ( tmp != 0 ) {
disable_mmc(); disable_mmc();
return ( tmp ); return tmp;
} }
/* send Startbyte */ /* send Startbyte */
@ -339,7 +357,7 @@ uint8_t mmc_write_sector ( uint32_t addr, uint8_t *buffer )
#endif #endif
/* write a single block */ /* write a single block */
for ( i = MMC_BLOCKLEN ; i ; i-- ) { for ( i = MMC_BLOCKLEN; i; i-- ) {
uint8_t data = *buffer; uint8_t data = *buffer;
buffer++; buffer++;
wait_till_send_done(); wait_till_send_done();
@ -362,18 +380,19 @@ uint8_t mmc_write_sector ( uint32_t addr, uint8_t *buffer )
send_dummy_byte(); send_dummy_byte();
if ( ( recv_byte() & 0x1F ) != 0x05 ) { if ( ( recv_byte() & 0x1F ) != 0x05 ) {
disable_mmc(); disable_mmc();
return ( 2 ); return 2;
} }
disable_mmc(); disable_mmc();
return ( 0 ); return 0;
} }
uint8_t mmc_read_block ( uint8_t *cmd, uint8_t *buffer, uint16_t bytes ) uint8_t mmc_read_block ( uint8_t *cmd, uint8_t *buffer, uint16_t bytes )
{ {
uint16_t i = 1; uint16_t i = 1;
uint8_t by; uint8_t by;
#ifdef SPI_CRC #ifdef SPI_CRC
uint16_t crc; uint16_t crc;
#endif #endif
@ -384,15 +403,16 @@ uint8_t mmc_read_block ( uint8_t *cmd, uint8_t *buffer, uint16_t bytes )
/* send command */ /* send command */
if ( mmc_cmd ( cmd ) != 0 ) { if ( mmc_cmd ( cmd ) != 0 ) {
disable_mmc(); disable_mmc();
return ( 1 ); return 1;
} }
/* wait for startbyte */ /* wait for startbyte */
while ( 1 ) { while ( 1 ) {
send_dummy_byte(); send_dummy_byte();
if ( recv_byte() == SB_START ) if ( recv_byte() == SB_START ) {
break; break;
} }
}
/* read first byte */ /* read first byte */
send_dummy_byte(); send_dummy_byte();
@ -420,7 +440,7 @@ uint8_t mmc_read_block ( uint8_t *cmd, uint8_t *buffer, uint16_t bytes )
crc |= recv_byte(); crc |= recv_byte();
if ( crc != crc16 ( buffer, bytes ) ) { if ( crc != crc16 ( buffer, bytes ) ) {
disable_mmc(); disable_mmc();
return ( 3 ); return 3;
} }
#else #else
send_dummy_byte(); send_dummy_byte();
@ -428,7 +448,7 @@ uint8_t mmc_read_block ( uint8_t *cmd, uint8_t *buffer, uint16_t bytes )
disable_mmc(); disable_mmc();
return ( 0 ); return 0;
} }
uint8_t mmc_read_sector ( uint32_t addr, uint8_t *buffer ) uint8_t mmc_read_sector ( uint32_t addr, uint8_t *buffer )
@ -437,11 +457,12 @@ uint8_t mmc_read_sector ( uint32_t addr, uint8_t *buffer )
/* /*
* send CMD17 * send CMD17
*/ */
uint8_t cmd[] = {CMD17, 0x00, 0x00, 0x00, 0x00, 0xFF}; uint8_t cmd[] = { CMD17, 0x00, 0x00, 0x00, 0x00, 0xFF };
/* convert blocks to bytes */ /* convert blocks to bytes */
if ( !is_sdhc ) if ( !is_sdhc ) {
addr = addr * MMC_BLOCKLEN; addr = addr * MMC_BLOCKLEN;
}
x.value32 = addr; x.value32 = addr;
cmd[1] = x.value8.hh; cmd[1] = x.value8.hh;
cmd[2] = x.value8.hl; cmd[2] = x.value8.hl;
@ -449,7 +470,6 @@ uint8_t mmc_read_sector ( uint32_t addr, uint8_t *buffer )
cmd[4] = x.value8.ll; cmd[4] = x.value8.ll;
return mmc_read_block ( cmd, buffer, MMC_BLOCKLEN ); return mmc_read_block ( cmd, buffer, MMC_BLOCKLEN );
} }
uint8_t mmc_read_multiblock ( uint32_t addr, uint8_t *buffer, uint8_t numblocks ) uint8_t mmc_read_multiblock ( uint32_t addr, uint8_t *buffer, uint8_t numblocks )
@ -459,6 +479,7 @@ uint8_t mmc_read_multiblock ( uint32_t addr, uint8_t *buffer, uint8_t numblocks
uint8_t cmd[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF }; uint8_t cmd[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF };
uint8_t n = numblocks; uint8_t n = numblocks;
uint16_t i; uint16_t i;
#ifdef SPI_CRC #ifdef SPI_CRC
uint16_t crc; uint16_t crc;
#endif #endif
@ -487,8 +508,9 @@ uint8_t mmc_read_multiblock ( uint32_t addr, uint8_t *buffer, uint8_t numblocks
#endif #endif
/* convert blocks to bytes */ /* convert blocks to bytes */
if ( !is_sdhc ) if ( !is_sdhc ) {
addr = addr * MMC_BLOCKLEN; addr = addr * MMC_BLOCKLEN;
}
x.value32 = addr; x.value32 = addr;
#ifdef MMC_MULTIBLOCK #ifdef MMC_MULTIBLOCK
@ -506,7 +528,7 @@ uint8_t mmc_read_multiblock ( uint32_t addr, uint8_t *buffer, uint8_t numblocks
resp = mmc_cmd ( cmd ); resp = mmc_cmd ( cmd );
if ( resp > 1 ) { if ( resp > 1 ) {
disable_mmc(); disable_mmc();
return ( 1 ); return 1;
} }
#else #else
cmd[0] = CMD17; cmd[0] = CMD17;
@ -527,7 +549,7 @@ uint8_t mmc_read_multiblock ( uint32_t addr, uint8_t *buffer, uint8_t numblocks
resp = mmc_cmd ( cmd ); resp = mmc_cmd ( cmd );
if ( resp > 1 ) { if ( resp > 1 ) {
disable_mmc(); disable_mmc();
return ( 1 ); return 1;
} }
x.value32 += MMC_BLOCKLEN; x.value32 += MMC_BLOCKLEN;
#endif #endif
@ -537,9 +559,10 @@ uint8_t mmc_read_multiblock ( uint32_t addr, uint8_t *buffer, uint8_t numblocks
/* wait for startbyte */ /* wait for startbyte */
while ( 1 ) { while ( 1 ) {
send_dummy_byte(); send_dummy_byte();
if ( recv_byte() == SB_START ) if ( recv_byte() == SB_START ) {
break; break;
} }
}
send_dummy_byte(); send_dummy_byte();
*buffer = recv_byte(); *buffer = recv_byte();
@ -560,7 +583,7 @@ uint8_t mmc_read_multiblock ( uint32_t addr, uint8_t *buffer, uint8_t numblocks
crc |= recv_byte(); crc |= recv_byte();
if ( crc != crc16 ( buffer, MMC_BLOCKLEN ) ) { if ( crc != crc16 ( buffer, MMC_BLOCKLEN ) ) {
disable_mmc(); disable_mmc();
return ( 3 ); return 3;
} }
buffer = buffer + MMC_BLOCKLEN; buffer = buffer + MMC_BLOCKLEN;
#else #else
@ -593,7 +616,7 @@ uint8_t mmc_read_multiblock ( uint32_t addr, uint8_t *buffer, uint8_t numblocks
disable_mmc(); disable_mmc();
return ( 0 ); return 0;
} }
uint8_t mmc_write_multiblock ( uint32_t addr, uint8_t *buffer, uint8_t numblocks ) uint8_t mmc_write_multiblock ( uint32_t addr, uint8_t *buffer, uint8_t numblocks )
@ -603,6 +626,7 @@ uint8_t mmc_write_multiblock ( uint32_t addr, uint8_t *buffer, uint8_t numblocks
uint8_t cmd[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF }; uint8_t cmd[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF };
uint8_t n = numblocks; uint8_t n = numblocks;
uint16_t i; uint16_t i;
#ifdef SPI_CRC #ifdef SPI_CRC
uint16_t crc; uint16_t crc;
uint8_t crcl, crch; uint8_t crcl, crch;
@ -630,9 +654,9 @@ uint8_t mmc_write_multiblock ( uint32_t addr, uint8_t *buffer, uint8_t numblocks
#endif #endif
#endif #endif
/* convert blocks to bytes */ /* convert blocks to bytes */
/* convert blocks to bytes */ if ( !is_sdhc ) {
if ( !is_sdhc )
addr = addr * MMC_BLOCKLEN; addr = addr * MMC_BLOCKLEN;
}
x.value32 = addr; x.value32 = addr;
#ifdef MMC_MULTIBLOCK #ifdef MMC_MULTIBLOCK
cmd[0] = CMD25; cmd[0] = CMD25;
@ -647,7 +671,7 @@ uint8_t mmc_write_multiblock ( uint32_t addr, uint8_t *buffer, uint8_t numblocks
resp = mmc_cmd ( cmd ); resp = mmc_cmd ( cmd );
if ( resp > 1 ) { if ( resp > 1 ) {
disable_mmc(); disable_mmc();
return ( 1 ); return 1;
} }
#else #else
cmd[0] = CMD24; cmd[0] = CMD24;
@ -667,7 +691,7 @@ uint8_t mmc_write_multiblock ( uint32_t addr, uint8_t *buffer, uint8_t numblocks
resp = mmc_cmd ( cmd ); resp = mmc_cmd ( cmd );
if ( resp != 0 ) { if ( resp != 0 ) {
disable_mmc(); disable_mmc();
return ( resp ); return resp;
} }
x.value32 += MMC_BLOCKLEN; x.value32 += MMC_BLOCKLEN;
#endif #endif
@ -717,7 +741,7 @@ uint8_t mmc_write_multiblock ( uint32_t addr, uint8_t *buffer, uint8_t numblocks
send_dummy_byte(); send_dummy_byte();
if ( ( recv_byte() & 0x1F ) != 0x05 ) { if ( ( recv_byte() & 0x1F ) != 0x05 ) {
disable_mmc(); disable_mmc();
return ( 2 ); return 2;
} }
#ifndef MMC_MULTIBLOCK #ifndef MMC_MULTIBLOCK
@ -738,5 +762,5 @@ uint8_t mmc_write_multiblock ( uint32_t addr, uint8_t *buffer, uint8_t numblocks
#endif #endif
disable_mmc(); disable_mmc();
return ( 0 ); return 0;
} }