I migrated my Atmega328p projet to an Atmega32A device, becuause it has a JTAG interface, and easier to debug problems. With the Mega328p I was handling an SD card with FatFS successful. I copied the source code and made the necessary adjusments (SPI pinout definition) in the FatFs library, but regarding the datasheet the SPI registers and settings are the same, I thought it is enough. I'm using the sample code from ffsample.zip:
int main(void) { UINT bw; FRESULT fr; FATFS fs; FIL fil; UART_Init(9600); LCDInit(LS_NONE); LCDClear(); LCDWriteStringXY(0, 0, "Welcome"); _delay_ms(1000); // SDCard write test f_mount(&fs, "", 0); /* Give a work area to the default drive */ fr = f_open(&fil, "newfile.txt", FA_WRITE | FA_CREATE_ALWAYS); /* Create a file */ LCDWriteIntXY(0, 1, fr, 1); if (fr == FR_OK) { f_write(&fil, "It works!\r\n", 11, &bw); /* Write data to the file */ LCDWriteIntXY(2, 1, bw, 2); fr = f_close(&fil); LCDWriteIntXY(5, 1, fr, 1); if (fr == FR_OK && bw == 11) { LCDClear(); LCDWriteStringXY(0, 0, "Written"); } } while(1){} }
adjustments in mmc_avr.c:
/* Port controls (Platform dependent) */ #define SS (1<<PORTB4) #define MOSI (1<<PORTB5) #define MISO (1<<PORTB6) #define SCK (1<<PORTB7) #define CS_LOW() PORTB &= ~SS /* CS=low */ #define CS_HIGH() PORTB |= SS /* CS=high */ #define FCLK_SLOW() SPCR = 0x52 /* Set slow clock (F_CPU / 64) */ #define FCLK_FAST() SPCR = 0x50 /* Set fast clock (F_CPU / 4) */ static void power_on (void) { DDRB |= SS | SCK | MOSI; SPCR = 0x52; /* Enable SPI function in mode 0 */ SPSR = 0x01; /* SPI 2x mode */ } static void power_off (void) { /* Disable SPI function */ SPCR = 0; /* Set SCK/MOSI/CS as hi-z, INS#/WP as pull-up */ DDRB &= ~(SS | SCK | MOSI); }
With the Mega328p I was using Atmel Studio 7, but with the JTAG-ICE debugger from ebay, I'm using AVR Studio 4.17. I tried to compile the Atmega32 code in Atmel Studio 7 too, but the result was annoying.
The newfile.txt sometimes created, sometimes not, and sometimes a bunch of unreadable files also created with a strange-character name.
Do you know any major differnce between the 2 CPU which makes difference?