void MainWrite(unsigned char *data, unsigned int page_addr, unsigned int byte_addr, unsigned int length, unsigned char buf) { unsigned int i; AT45DBXX_Enable;//CS LOW if (buf==BUFFER1) { SPI_Send_byte( MAIN_WRITE1); //opcode for read status } if (buf==BUFFER2) { SPI_Send_byte(MAIN_WRITE2); } //wait for transmission complete SPI_Send_byte((char)(page_addr>>7)); //wait for transmission complete SPI_Send_byte((char)(page_addr<<1)|(0x01 & (byte_addr>>8))); SPI_Send_byte((char)(byte_addr)); for (i=0;i<length;i++) { SPI_Send_byte(*data); //data data++; } AT45DBXX_Disable; //end }
void BUFRead(unsigned char *data, unsigned int addr, unsigned int length, unsigned char buf) { unsigned char i; AT45DBXX_Enable; //CS LOW if (buf==BUFFER1) { SPI_Send_byte( BUF1_READ); //opcode for read status } if (buf==BUFFER2) { SPI_Send_byte(BUF2_READ); } SPI_Send_byte(0x00); //don't care SPI_Send_byte((char)(addr>>8)); //don't care and 1st 2 addr bits SPI_Send_byte((char)(addr)); //address SPI_Send_byte(0x00); //don't care for(i=0;i<length;i++) { *data = SPI_Receive_byte(); data++; } AT45DBXX_Disable; //end }
void BUFWrite(unsigned char *data, unsigned int addr, unsigned int length, unsigned char buf) { unsigned int i; AT45DBXX_Enable;//CS LOW if (buf==BUFFER1) { SPI_Send_byte( BUF1_WRITE); //opcode } if (buf==BUFFER2) { SPI_Send_byte(BUF2_WRITE); } SPI_Send_byte(0xff); //don't care //wait for transmission complete SPI_Send_byte((char)(addr>>8)); //don't care and 1st 2 addr bits SPI_Send_byte((char)(addr)); //address 0 for (i=0;i<length;i++) { SPI_Send_byte( data[i]); //data //data++; } AT45DBXX_Disable; }
uint_8 SD_SendCommand(uint_8 u8SDCommand,uint_8 u8SDResponse) { /*Body*/ uint_8 u8Counter; volatile uint_8 u8Temp=0; /* Send Start byte */ SPI_Send_byte(u8SDCommand); /* Send Argument */ for(u8Counter=0;u8Counter<4;u8Counter++) SPI_Send_byte(gu8SD_Argument.bytes[u8Counter]); /* Send CRC */ SPI_Send_byte(0x95); /* Response RHandler */ u8Counter=SD_WAIT_CYCLES; do { u8Temp=SPI_Receive_byte(); u8Counter--; }while((u8Temp != u8SDResponse) && u8Counter > 0); if(u8Counter) return(0); else return(1); }/*EndBody*/
UINT8 SD_SendCommand(UINT8 u8SDCommand,UINT8 u8SDResponse) { UINT8 u8Counter; volatile UINT8 u8Temp=0; /* Send Start byte */ SPI_Send_byte(u8SDCommand); /* Send Argument */ for(u8Counter=0;u8Counter<4;u8Counter++) SPI_Send_byte(gu8SD_Argument.bytes[u8Counter]); /* Send CRC */ SPI_Send_byte(0x95); /* Response RHandler */ u8Counter=SD_WAIT_CYCLES; do { u8Temp=SPI_Receive_byte(); u8Counter--; }while((u8Temp != u8SDResponse) && u8Counter > 0); if(u8Counter) return(OK); else return(COMMAND_FAILS); }
void SD_Write_Block(PTR_LBA_APP_STRUCT lba_data_ptr) { /* Body */ uint_32 u32Counter; SPI_set_SS(); gu8SD_Argument.lword = lba_data_ptr->offset; if(SD_SendCommand(SD_CMD24|0x40,SD_OK)) { SPI_clr_SS() ; return; /* Command IDLE fail */ } /* EndIf */ SPI_Send_byte(0xFE); for(u32Counter=0;u32Counter<lba_data_ptr->size;u32Counter++) { SPI_Send_byte(*(lba_data_ptr->buff_ptr + u32Counter)); } /* EndFor */ SPI_Send_byte(0xFF); /* checksum Bytes not needed */ SPI_Send_byte(0xFF); if((SPI_Receive_byte() & 0x0F) != 0x05) { SPI_clr_SS() ; return; /* Command fail */ } /* EndIf */ while(SPI_Receive_byte()==0x00) { __RESET_WATCHDOG(); /* Dummy SPI cycle */ } /* EndWhile */ SPI_clr_SS() ; return; } /* EndBody */
UINT8 SD_Init(void) { _SD_PRESENT=_IN; /* Check for SD */ if(SD_PRESENT) return(NO_SD_CARD); /* Initialize SPI Module */ SPI_Init(); /* Start SD card Init */ SPI_SS=ENABLE; SD_CLKDelay(10); // Send 80 clocks SPI_SS=DISABLE; gu8SD_Argument.lword=0; SD_CLKDelay(8); /* IDLE Command */ SPI_SS=ENABLE; if(SD_SendCommand(SD_CMD0|0x40,SD_IDLE)) { SPI_SS=DISABLE; return(INIT_FAILS); } SPI_SS=DISABLE; (void)SPI_Receive_byte(); // Dummy SPI cycle /* Initialize SD Command */ SPI_SS=ENABLE; while(SD_SendCommand(SD_CMD1|0x40,SD_OK)); SPI_SS=DISABLE; (void)SPI_Receive_byte(); // Dummy SPI cycle /* Block Length */ SPI_SS=ENABLE; gu8SD_Argument.lword=SD_BLOCK_SIZE; if(SD_SendCommand(SD_CMD16|0x40,SD_OK)) { SPI_SS=DISABLE; return(INIT_FAILS); } SPI_SS=DISABLE; SPI_High_rate(); SPI_Send_byte(0x00); SPI_Send_byte(0x00); return(OK); }
void PageErase(unsigned int page_addr) { AT45DBXX_Enable; //CS LOW //while (SPI_BUSY); SPI_Send_byte((char)(page_addr>>7)); SPI_Send_byte((char)(page_addr<<1)); SPI_Send_byte(0x00); //don't care; AT45DBXX_Disable; }
void BlockErase(unsigned int block_addr) { AT45DBXX_Enable; //CS LOW //while (SPI_BUSY); SPI_Send_byte((char)(block_addr>>3)); SPI_Send_byte((char)(block_addr<<5)); SPI_Send_byte(0x00); //don't care; AT45DBXX_Disable; }
void AT45DBXX_Read_sec_lock(unsigned char *sec) { u8 i; AT45DBXX_Enable; SPI_Send_byte(0x35); SPI_Send_byte(0x00); SPI_Send_byte(0x00); SPI_Send_byte(0x00); for(i=0;i<8;i++) sec[i]=SPI_Receive_byte(); AT45DBXX_Disable; }
void SectorErase(unsigned int sector_addr) { AT45DBXX_Enable; //CS LOW SPI_Send_byte((char)(sector_addr<<2)); //while (SPI_BUSY); SPI_Send_byte(0x00); //don't care; //while (SPI_BUSY); SPI_Send_byte( 0x00); //don't care; AT45DBXX_Disable; }
//FLASH INTERACTION FUNCTIONS char FLASHStatus(void) { int i; //AT45DBXX_BUSY(); AT45DBXX_Enable; //CS LOW SPI_Send_byte(STATUS_READ); //opcode for read status i=SPI_Receive_byte(); AT45DBXX_Disable; //end return i; }
void Main2BufRead(unsigned int page_addr, unsigned char buf) { AT45DBXX_Enable; //CS LOW if (buf==BUFFER1) { SPI_Send_byte(MAIN_TO_BUF1_READ); //opcode for read status } if (buf==BUFFER2) { SPI_Send_byte( MAIN_TO_BUF2_READ); } //wait for transmission complete SPI_Send_byte((char)(page_addr>>7)); //2 reserve, then 6 page_addr bits, PA11-PA6 //wait for transmission complete SPI_Send_byte( (char)(page_addr<<1)); //6 addr bits and 2 don't care SPI_Send_byte(0x00); // 8 don't cares AT45DBXX_Disable; //end }
void SD_Write_Block(PTR_LBA_APP_STRUCT lba_data_ptr) { uint_32 u32Counter; gu8SD_Argument.lword = lba_data_ptr->offset; while(SPI_Receive_byte()==0x00) { Watchdog_Reset(); } if(SD_SendCommand(SD_CMD24|0x40,SD_OK)) { return; // Command IDLE fail } SPI_Send_byte(0xFE); for(u32Counter=0;u32Counter<lba_data_ptr->size;u32Counter++) { SPI_Send_byte(*(lba_data_ptr->buff_ptr + u32Counter)); } SPI_Send_byte(0xFF); // checksum Bytes not needed SPI_Send_byte(0xFF); if((SPI_Receive_byte() & 0x0F) != 0x05) { // return; // Command fail } while(SPI_Receive_byte()==0x00) { Watchdog_Reset(); } // Dummy SPI cycle return; }
unsigned char Status_read(void) { u8 i; AT45DBXX_Enable; SPI_Send_byte( STATUS_READ); do {i = SPI_Receive_byte();} while(!(i & 0x80)); AT45DBXX_Disable; return i; }
void Buf2MainWrite (unsigned int addr, unsigned char buf) { AT45DBXX_Enable; //CS LOW if (buf==BUFFER1) { SPI_Send_byte(BUF1_TO_MAIN_WRITE); } if (buf==BUFFER2) { SPI_Send_byte(BUF2_TO_MAIN_WRITE); } SPI_Send_byte((char)(addr>>7)); SPI_Send_byte((char)(addr<<1)); SPI_Send_byte(0x00); //don't care; AT45DBXX_Disable; //end }
uint_8 SD_Init(void) { /* Body */ SPI_Init(); /* SPI Initialization */ SPI_set_SS(); SD_CLKDelay(10); /* Send 80 clocks */ SPI_clr_SS() ; gu8SD_Argument.lword = 0; SD_CLKDelay(8); /* IDLE Command */ SPI_set_SS(); if(SD_SendCommand(SD_CMD0|0x40,SD_IDLE)) { SPI_clr_SS() ; return(1); /* Command IDLE fail */ } /* EndIf */ SPI_clr_SS() ; (void)SPI_Receive_byte(); /* Dummy SPI cycle */ /* Initialize SD Command */ SPI_set_SS(); while(SD_SendCommand(SD_CMD1|0x40,SD_OK)) { __RESET_WATCHDOG(); } /* EndWhile */ SPI_clr_SS() ; (void)SPI_Receive_byte(); /* Dummy SPI cycle */ /* Block Length */ SPI_set_SS(); gu8SD_Argument.lword = SD_BLOCK_SIZE; if(SD_SendCommand(SD_CMD16|0x40,SD_OK)) { SPI_clr_SS() ; return(1); /* Command IDLE fail */ } /* EndIf */ SPI_clr_SS() ; SPI_High_rate(); SPI_Send_byte(0x00); SPI_Send_byte(0x00); return(0); } /* EndBody */
uint_8 SD_Init(void) { SPI_Init(); // SPI Initialization SD_CLKDelay(10); // Send 80 clocks gu8SD_Argument.lword = 0; SD_CLKDelay(8); /* IDLE Command */ if(SD_SendCommand(SD_CMD0|0x40,SD_IDLE)) { return(1); // Command IDLE fail } (void)SPI_Receive_byte(); // Dummy SPI cycle /* Initialize SD Command */ while(SD_SendCommand(SD_CMD1|0x40,SD_OK)) { Watchdog_Reset(); } (void)SPI_Receive_byte(); // Dummy SPI cycle /* Block Length */ gu8SD_Argument.lword = SD_BLOCK_SIZE; if(SD_SendCommand(SD_CMD16|0x40,SD_OK)) { return(1); // Command IDLE fail } SPI_High_rate(); SPI_Send_byte(0x00); SPI_Send_byte(0x00); //(void)SPI_Receive_byte(); // Dummy SPI cycle return(0); }
UINT8 SD_Write_Block(UINT32 u16SD_Block,UINT8 *pu8DataPointer) { UINT16 u16Counter; SPI_SS=ENABLE; gu8SD_Argument.lword=u16SD_Block; gu8SD_Argument.lword=gu8SD_Argument.lword<< SD_BLOCK_SHIFT; if(SD_SendCommand(SD_CMD24|0x40,SD_OK)) { SPI_SS=DISABLE; return(WRITE_COMMAND_FAILS); } SPI_Send_byte(0xFE); for(u16Counter=0;u16Counter<BLOCK_SIZE;u16Counter++) SPI_Send_byte(*pu8DataPointer++); SPI_Send_byte(0xFF); // checksum Bytes not needed SPI_Send_byte(0xFF); for(u16Counter=0;u16Counter<BLOCK_SIZE;u16Counter++); if((SPI_Receive_byte() & 0x0F) != 0x05) { SPI_SS=DISABLE; return(WRITE_DATA_FAILS); } while(SPI_Receive_byte()==0x00); // Dummy SPI cycle SPI_SS=DISABLE; return(OK); }
void PageRead (unsigned char *data, unsigned int page_addr, unsigned int byte_addr, unsigned int length) { unsigned int i; AT45DBXX_Enable; //end SPI_Send_byte( PAGE_READ); SPI_Send_byte((char)(page_addr>>7)); SPI_Send_byte((char)(page_addr<<1)|(0x01 & (byte_addr>>8))); SPI_Send_byte((char)(byte_addr)); //32 don't care bits for (i=0;i<4;i++) {SPI_Send_byte( 0x00); } for (i=0;i<length;i++) { *data =SPI_Receive_byte(); data++; } AT45DBXX_Disable; }
void SD_CLKDelay(UINT8 u8Frames) { while(u8Frames--) SPI_Send_byte(0xFF); }
uint_8 SD_Init(void) { #ifdef MCU_MKL25Z4 SPI_Init(); /* SPI Initialization */ SD_CLKDelay(10); /* Send 80 clocks */ SPI_clr_SS() ; gu8SD_Argument.lword = 0; SPI_set_SS(); SD_CLKDelay(8); /* IDLE Command */ SPI_set_SS(); if(SD_SendCommand(SD_CMD0|0x40,SD_IDLE)) { SPI_clr_SS(); return(FALSE); /* Command IDLE fail */ } SPI_clr_SS(); (void)SPI_Receive_byte(); /* Dummy SPI cycle */ /* Initialize SD Command */ SPI_set_SS(); while(SD_SendCommand(SD_CMD1|0x40,SD_OK)) { Watchdog_Reset(); } SPI_clr_SS() ; (void)SPI_Receive_byte(); /* Dummy SPI cycle */ /* Block Length */ gu8SD_Argument.lword = BYTESWAP32(SD_BLOCK_SIZE); SPI_set_SS(); if(SD_SendCommand(SD_CMD16|0x40,SD_OK)) { SPI_clr_SS() ; return(FALSE); /* Command IDLE fail */ } SPI_clr_SS() ; SPI_High_rate(); SPI_Send_byte(0x00); SPI_Send_byte(0x00); return(TRUE); #else /*Body*/ SPI_Init(); /* SPI Initialization*/ SD_CLKDelay(10); /* Send 80 clocks*/ gu8SD_Argument.lword = 0; SD_CLKDelay(8); /* IDLE Command */ if(SD_SendCommand(SD_CMD0|0x40,SD_IDLE)) { return(FALSE); /* Command IDLE fail*/ }/*Endif*/ (void)SPI_Receive_byte(); /* Dummy SPI cycle*/ /* Initialize SD Command */ while(SD_SendCommand(SD_CMD1|0x40,SD_OK)) { Watchdog_Reset(); }/*EndWhile*/ (void)SPI_Receive_byte(); /*Dummy SPI cycle */ /* Block Length */ gu8SD_Argument.lword = BYTESWAP32(SD_BLOCK_SIZE); if(SD_SendCommand(SD_CMD16|0x40,SD_OK)) { return(FALSE); /* Command IDLE fail */ }/*Endif*/ SPI_High_rate(); SPI_Send_byte(0x00); SPI_Send_byte(0x00); return(TRUE); #endif }/*End Body*/
void SD_CLKDelay(uint_8 u8Frames) { /*Body*/ while(u8Frames--) SPI_Send_byte(0xFF); }/*EndBody*/
void SD_Write_Block(PTR_LBA_APP_STRUCT lba_data_ptr) { #ifdef MCU_MKL25Z4 uint_32 u32Counter; SPI_set_SS(); gu8SD_Argument.lword = BYTESWAP32(lba_data_ptr->offset); if(SD_SendCommand(SD_CMD24|0x40,SD_OK)) { SPI_clr_SS() ; return; /* Command IDLE fail */ } SPI_Send_byte(0xFE); for(u32Counter=0;u32Counter<lba_data_ptr->size;u32Counter++) { SPI_Send_byte(*(lba_data_ptr->buff_ptr + u32Counter)); } SPI_Send_byte(0xFF); /* checksum Bytes not needed */ SPI_Send_byte(0xFF); if((SPI_Receive_byte() & 0x0F) != 0x05) { SPI_clr_SS() ; return; /* Command fail */ } while(SPI_Receive_byte()==0x00) { Watchdog_Reset(); /* Dummy SPI cycle */ } SPI_clr_SS() ; return; #else /* Body */ uint_32 u32Counter, time_out; gu8SD_Argument.lword = BYTESWAP32(lba_data_ptr->offset); if(SD_SendCommand(SD_CMD24|0x40,SD_OK)) { return; /* Command IDLE fail */ } /* EndIf */ SPI_Send_byte(0xFE); for(u32Counter=0;u32Counter<lba_data_ptr->size;u32Counter++) { SPI_Send_byte(*(lba_data_ptr->buff_ptr + u32Counter)); } /* EndFor */ SPI_Send_byte(0xFF); /* checksum Bytes not needed */ SPI_Send_byte(0xFF); for (time_out=0;time_out<1000;time_out++) { if((SPI_Receive_byte() & 0x1F) < 16) { break; } /* EndIf */ }/* EndFor */ if (time_out>=1000) return; while(SPI_Receive_byte()==0x00) { Watchdog_Reset(); } /* EndWhile */ return; #endif }