DRESULT disk_write ( BYTE pdrv, /* Physical drive number (0..) */ const BYTE *buff, /* Data to be written */ DWORD sector, /* Sector address (LBA) */ UINT count /* Number of sectors to write (1..128) */ ) { DRESULT ret; outpw(REG_SDH_GCTL, SDH_GCTL_SDEN_Msk); //sysprintf("disk_write - drv:%d, sec:%d, cnt:%d, buff:0x%x\n", pdrv, sector, count, (UINT32)buff); if (!((UINT32)buff & 0x80000000)) { /* Disk write buffer is not non-cachable buffer. Use my non-cachable to do disk write. */ if (count * 512 > _MAX_SS) return RES_ERROR; fatfs_win_buff = (BYTE *)((unsigned int)fatfs_win_buff_pool | 0x80000000); memcpy(fatfs_win_buff, buff, count * 512); if (pdrv == DRV_SD0) ret = (DRESULT) SD_Write(SD_PORT0, fatfs_win_buff, sector, count); else if (pdrv == DRV_SD1) ret = (DRESULT) SD_Write(SD_PORT1, fatfs_win_buff, sector, count); else return RES_ERROR; } else { if (pdrv == DRV_SD0) ret = (DRESULT) SD_Write(SD_PORT0, (UINT8 *)buff, sector, count); else if (pdrv == DRV_SD1) ret = (DRESULT) SD_Write(SD_PORT1, (UINT8 *)buff, sector, count); else return RES_ERROR; } return ret; }
//******************************************** unsigned int Write_Single_Block(unsigned long int BlockAddress) { unsigned int temp,Response,MaximumTimes; MaximumTimes=10; if (BlockAddress>BlockNR) return 0xff20;//whether BlockAddress out of range? for(temp=0;temp<MaximumTimes;temp++) { Response=SD_CMD_Write(24,BlockAddress,1,1);//Send CMD24 if (Response==0xff00) temp=MaximumTimes; } for (temp=0;temp<8;temp++)//Provide 8 extra clock after CMD response { sd_clk=0;//CLK Low Delay5us(); sd_clk=1;//CLK High Delay5us(); } SD_Write(0x00fe);//Send Start Block Token //这里为了使只有512byte的单片机能够读写SD卡,特意节省了RAM的使用量,每次读写只有两个重复的128byte //如果您使用的单片机拥有1K以上的RAM请将"%128"去掉 for (temp=0;temp<256;temp++) SD_2Byte_Write(WriteBuffer[temp%128]);//Data Block SD_2Byte_Write(0xffff);//Send 2 Bytes CRC Response=SD_Read(); while (SD_Read()!=0xffff) {;} sd_cse=1;//CS_High() for (temp=0;temp<8;temp++)//Provide 8 extra clock after data response { sd_clk=0;//CLK Low Delay5us(); sd_clk=1;//CLK High Delay5us(); } return Response; }
//------------------------------------------------------------------------------ //! \brief Writes data on a SDRAM media //! \param media Pointer to a Media instance //! \param address Address at which to write //! \param data Pointer to the data to write //! \param length Size of the data buffer //! \param callback Optional pointer to a callback function to invoke when //! the write operation terminates //! \param argument Optional argument for the callback function //! \return Operation result code //! \see Media //! \see MediaCallback //------------------------------------------------------------------------------ static unsigned char MEDSdusb_Write(Media *media, unsigned int address, void *data, unsigned int length, MediaCallback callback, void *argument) { MEDTransfer * pXfr; unsigned char error; TRACE_INFO_WP("SDuWr(%d,%d) ", (int)address, (int)length); // Check that the media if ready if (media->state != MED_STATE_READY) { TRACE_INFO("MEDSdusb_Write: Busy\n\r"); return MED_STATUS_BUSY; } // Check that the data to write is not too big if ((length + address) > media->size) { TRACE_WARNING("MEDSdcard_Write: Data too big\n\r"); return MED_STATUS_ERROR; } // Put the media in Busy state media->state = MED_STATE_BUSY; // Start media transfer pXfr = &media->transfer; pXfr->data = data; pXfr->address = address; pXfr->length = length; pXfr->callback = callback; pXfr->argument = argument; error = SD_Write((SdCard*)media->interface, address, data, length, SdMmcCallback, media); return (error ? MED_STATUS_ERROR : MED_STATUS_SUCCESS); }
static uint32_t handle_cmd_write_pages(uint32_t cmd, uint32_t *mailbox) { union write_pages_mailbox *mbx = (union write_pages_mailbox*)mailbox; uint32_t offset = mbx->in.offset; uint32_t length = mbx->in.length; assert(cmd == APPLET_CMD_WRITE_PAGES); if (!initialized) { trace_error_wp("Applet not initialized\r\n"); return APPLET_FAIL; } /* check that requested size does not overflow buffer */ if ((length * BLOCK_SIZE) > buffer_size) { trace_error_wp("Buffer overflow\r\n"); return APPLET_FAIL; } /* check that requested offset/size does not overflow memory */ if (offset + length > mem_size) { trace_error_wp("Memory overflow\r\n"); return APPLET_FAIL; } if (SD_Write(&lib, offset, buffer, length, NULL, NULL) != SDMMC_OK) { trace_info_wp("Error while writing %u bytes at offset 0x%08x\r\n", (unsigned)(mbx->in.length * BLOCK_SIZE), (unsigned)(mbx->in.offset * BLOCK_SIZE)); mbx->out.pages_written = 0; return APPLET_READ_FAIL; } trace_info_wp("Wrote %u bytes at offset 0x%08x\r\n", (unsigned)(mbx->in.length * BLOCK_SIZE), (unsigned)(mbx->in.offset * BLOCK_SIZE)); mbx->out.pages_written = mbx->in.length; return APPLET_SUCCESS; }
//}}} //{{{ int8_t SD_WriteCached (uint8_t* buf, uint32_t blk_addr, uint16_t blocks) { if (SD_present()) { mWrites++; SD_Write (buf, blk_addr, blocks); mReadCacheBlock = 0xFFFFFFF0; if (blk_addr != mWriteBlock + mWriteMultipleLen) { if (mWriteMultipleLen) { // flush pending multiple //cLcd::debug ("wm:" + dec (mWriteBlock) + "::" + dec (mWriteMultipleLen)); mWriteMultipleLen = 0; } mWriteBlock = blk_addr; } mWriteMultipleLen += blocks; return 0; } return -1; }
/******************************************************************************* * Function Name : main * Description : Main program * Input : None * Output : None * Return : None *******************************************************************************/ int main(void) { /* NVIC configuration */ NVIC_Configuration(); initialisation(); /* Initialisation of SD Card and creation of File */ SD_Init(); /* Blinkmuster laden */ if( SD_Start("muster.txt", FA_OPEN_EXISTING | FA_READ) ) { SD_Read(&muster,1); } SD_Close(); /* LCD Initialisieren */ GLCD_Init(); GLCD_Clear(White); GLCD_SetTextColor(Red); GLCD_DisplayString(1, 1, 1, "Write to SD-Card"); GLCD_DisplayString(8, 1, 1, "Press User to stop"); GLCD_DisplayString(4, 1, 1, "Fit Card into Slot"); /* Wait until SD Card is in Slot */ while(!(SD_Start("time.txt", FA_CREATE_ALWAYS | FA_WRITE))) { } length=sprintf (buffer, "runtime: %d:%d:%d ", hour, min, sek); GLCD_DisplayString(4, 1, 1, buffer); /***************************** * ENDE INITIALISIERUNG * ******************************/ GPIO_ResetBits(GPIOE,GPIO_Pin_All); // LED off all pins while(1) { // BFH_GLCD_UDP(); /* Alle 10ms wird das tickFlag gesetzt */ if(tickFlag) { tickFlag=0; // reset tickFlag if(write > 1) { /* Jede Sekunde einen Stamp ins File schreiben */ if((write%1000) == 0) { /* Zeit hochzählen */ sek++; if(sek==60) { sek=0; min++; } if(min==60) { min=0; hour++; } if(hour==24) { hour=0; } /* write time to Diaplay */ length=sprintf (buffer, "runtime: %d:%d:%d ", hour, min, sek); GLCD_DisplayString(4, 1, 1, buffer); /* write time to card */ length=sprintf (buffer, "runtime: %d:%d:%d\ttext\r\n", hour, min, sek); SD_Write(buffer,length); /* switch LED */ GPIOE->ODR ^= muster << 8; } write += 10; } /* Close SD Card */ else if (write == 1) { SD_Close(); write--; } /* Schreiben beendet -> LED löschen */ else { GPIOE->ODR |= muster << 8; GLCD_DisplayString(8, 1, 1, "Application stopped"); } } /* Wenn Taste gedrückt wird, schreiben beenden */ if((GPIOB->IDR&0x0080) == 0) { write = 1; } } }