⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test-1__.c

📁 This fat 16 can be used for logging function. The user can use it for logger device.
💻 C
📖 第 1 页 / 共 5 页
字号:
                }

            _FF_spi(0xFF);
            _FF_spi(0xFF);
            _FF_spi(0xFF);
            SD_CS_OFF();
            _FF_spi(0xFF);
            _FF_error = NO_ERR;
            if(sd_read_buff == _FF_Buff)
                _FF_BuffAddr = sd_addr;

            return(0);
        }
        SD_CS_OFF();
        _FF_spi(0xFF);
        if (c == 4)
            set_SD_mode(SD_SPI);             /* Try to reset and initialize the card to SPI mode */
    }
    _FF_error = READ_ERR;

    return((int8) EOF);
}


#ifndef _READ_ONLY_

/****************************************************************************
**
** Writes 1 sector of bytes (usually 512) designated by *sd_write_buff to
** the address sd_addr.
**
** Parameters:  sd_addr, the sector address of the card where the function
**                  will write to.
**              *sd_write_buff, pointer to the buffer that the function will
**                  write data from.
**
** Returns:  0 - Write Successful
**          -1 - Write Failed
**
****************************************************************************/
int8 _FF_write(uint32 sd_addr, uint8 *sd_write_buff)
{
    #if defined(_USE_MULTIBLOCK_SINGLE_)
        uint8 resp, calc, c;
        int16 n;

        for(c = 0; c < 4; c++)
        {
            clear_sd_buff();
            resp = 0xFF;

            _SD_send_cmd(CMD25, (sd_addr << 9));                /* Send Write Multi-Block Command */
            for(n = 0; ((n < 30000) && (resp == 0xFF)); n++)
                resp = _FF_spi(0xFF);
            if(resp == 0)
            {
                #ifdef _DEBUG_MULTI_BLOCK_
                    _FF_printf(_FF_WriteBlockStartStr);
                #endif

                /* Start Sending the Packet in the correct format */
                _FF_spi(0xFF);
                _FF_spi(SD_MULTI_START);        /* Start Block Token */

                /* Send Data */
                #ifdef _BYTES_PER_SEC_512_
                    for(n = 0; n < 512; n++)
                    {
                #else
                    for(n = 0; n < BPB_BytsPerSec.uval16; n++)
                    {
                #endif
                        _FF_spi(sd_write_buff[n]);        /* Write Data in buffer to card */
                    }

                _FF_spi(0xFF);                    /* Send 2 blank CRC bytes */
                _FF_spi(0xFF);
                n = 0;
                do
                {
                    resp = _FF_spi(0xFF);            /* Response should be 0bXXX00101 */
                }   while((resp == 0xFF) && (n++ < 30000));
                #ifdef _DEBUG_MULTI_BLOCK_
                   _FF_printf(_FF_WriteBlock02XStr, resp, n);
                #endif

                calc = resp | 0xE0;
                if(calc == 0xE5)
                {
                    while(_FF_spi(0xFF) == 0)
                    {
                        /* Wait for not busy */
                        if(n++ > 30000)
                        {
                            #ifdef _FF_WRITE_DEBUG_
                                _FF_printf(_FF_WriteAddrErrStr, 1);
                            #endif
                            break;    /* Clear Buffer before returning 'OK' */
                        }
                    }

                    _FF_spi(0xFF);
                    _FF_spi(SD_STOP_TRANS);            /* Stop Block Token */

                    while(_FF_spi(0xFF) == 0)
                    {
                        /* Wait for not busy */
                        if(n++ > 30000)
                        {
                            #ifdef _FF_WRITE_DEBUG_
                                _FF_printf(_FF_WriteAddrErrStr, 1);
                            #endif
                            break;    /* Clear Buffer before returning 'OK' */
                        }
                    }
                    SD_CS_OFF();
                    _FF_spi(0xFF);
                    return(0);     /* All Good, return */
                }
                else
                {
                    _FF_spi(0xFF);
                    _FF_spi(SD_STOP_TRANS);
                }
            }
            #ifdef _DEBUG_MULTI_BLOCK_
                else
                {
                    _FF_printf(_FF_WriteMultiStartStr, 4, resp);
                }
            #endif
        }

        SD_CS_OFF();
        _FF_spi(0xFF);
        return((uint8) EOF);

    #else   /*defined(_USE_MULTIBLOCK_SINGLE_)*/
        uint8 resp, calc, c;
        uint16 n;

        #ifdef _FF_WRITE_DEBUG_
            _FF_printf(_FF_WriteAddr_ldXStr, sd_addr<<9);
        #endif

        if(sd_write_buff == _FF_Buff)
            _FF_BuffAddr = sd_addr;

        if(sd_addr <= _FF_PartitionAddr)
        {
            /* if the Address is l */
            _FF_error = WRITE_ERR;
            return((int8) EOF);
        }

        for(c = 0; c < 10; c++)
        {
            clear_sd_buff();
            #ifdef _BYTES_PER_SEC_512_
                _SD_send_cmd(CMD24, (sd_addr<<9));
            #else
                _SD_send_cmd(CMD24, (sd_addr*(uint32)BPB_BytsPerSec.uval16));
            #endif
            resp = _FF_spi(0xFF);
            for(n = 0; ((n < 30000) && (resp != 0x00)); n++)
                resp = _FF_spi(0xFF);

            if(resp == 0)
            {
                _FF_spi(0xFF);
                _FF_spi(SD_START_TOKEN);            /* Start Block Token */
                #ifdef _BYTES_PER_SEC_512_
                    for(n = 0; n < 512; n++)
                    {
                #else
                    for(n = 0; n < BPB_BytsPerSec.uval16; n++)
                    {
                #endif
                        _FF_spi(sd_write_buff[n]);      /* Write Data in buffer to card */
                    }
                _FF_spi(0xFF);                          /* Send 2 blank CRC bytes */
                _FF_spi(0xFF);
                resp = _FF_spi(0xFF);                   /* Response should be 0bXXX00101 */
                calc = resp | 0xE0;
                if(calc == 0xE5)
                {
                    n = 0;
                    while(_FF_spi(0xFF) == 0)
                    {
                        if(n++ > 30000)
                        {
                            #ifdef _FF_WRITE_DEBUG_
                                _FF_printf(_FF_WriteAddrErrStr, 1);
                            #endif
                            break;    /* Clear Buffer before returning 'OK' */
                        }
                    }
                    SD_CS_OFF();
                    _FF_spi(0xFF);
                    _FF_error = NO_ERR;

                    return(0);
                }
            }
            SD_CS_OFF();
            _FF_spi(0xFF);
            if (c == 5)
                set_SD_mode(SD_SPI);             /* Try to reset and initialize the card to SPI mode */
        }
        _FF_error = WRITE_ERR;
        #ifdef _FF_WRITE_DEBUG_
            _FF_printf(_FF_WriteAddrErrStr, 2);
        #endif
        return((int8) EOF);
    #endif  /*defined(_USE_MULTIBLOCK_SINGLE_)*/
}
#endif  /*_READ_ONLY_*/


#if defined(_SD_BLOCK_WRITE_) && !defined(_READ_ONLY_)
/****************************************************************************
**
** Function used to start a block write
**
** Parameters: sd_data, byte to write in block mode
**
** Returns:  0 - Byte written successfully
**          -1 - An error occurred
**
****************************************************************************/
int8 SD_write_block_start(uint32 sd_addr, uint32 blk_size)
{
    uint8 resp;
    int16 n;

    sd_addr <<= 9;

    if(blk_size == 0)
        return((int8) EOF);

    clear_sd_buff();
    resp = 0xFF;

    clear_sd_buff();
    resp = 0xFF;
    _SD_send_cmd(CMD25, sd_addr);        /* Send Write Multi-Block Command */
    for(n = 0; ((n < 30000) && (resp == 0xFF)); n++)
        resp = _FF_spi(0xFF);
    if(resp == 0)
    {
        #ifdef _DEBUG_MULTI_BLOCK_
            _FF_printf(_FF_StartBlockWriteStr);
            _BLOCK_ADDRESS_ = sd_addr;
        #endif
        SDBlockWriteBlockCnt = 0;
        return(0);
    }
    SD_CS_OFF();
    _FF_spi(0xFF);
    #ifdef _DEBUG_MULTI_BLOCK_
        _FF_printf(_FF_StartMultiErrStr, 4, resp);
    #endif
    return((uint8) EOF);
}

/****************************************************************************
**
** Function used to write 1 byte when block writing to the SD/MMC media.
**
** Parameters: sd_data, byte to write in block mode
**
** Returns:  0 - Byte written successfully
**          -1 - An error occurred
**
****************************************************************************/
int8 SD_write_block_byte(uint8 sd_data)
{
    uint8 resp, calc;
    int16 n, i;

    #ifdef _DEBUG_MULTIBLOCK_BYTE_
        _FF_putchar(sd_data);
    #endif
    i = SDBlockWriteBlockCnt % 512;
    if(i == 0)
    {
        _FF_spi(0xFF);
        _FF_spi(SD_MULTI_START);    /* Start Block Token */
        #ifdef _DEBUG_MULTI_BLOCK_
            _FF_printf(_FF_MultiWriteStr, _BLOCK_ADDRESS_);
            _BLOCK_ADDRESS_ += 0x200;
        #endif
    }
    _FF_spi(sd_data);               /* Send the data */
    SDBlockWriteBlockCnt++;
    i++;
    if(i == 512)
    {
        _FF_spi(0xFF);              /* Send 2 blank CRC bytes */
        _FF_spi(0xFF);
        resp = _FF_spi(0xFF);       /* Response should be 0bXXX00101 */
        calc = resp | 0xE0;
        if(calc == 0xE5)
        {
            n = 0;
            do
            {
                /* Wait for not busy */
                resp = _FF_spi(0xFF);
                if(n++ > 30000)
                    resp = 0xFF;    /* Response not received for too long, ignore*/
            } while(resp == 0);
        }
        else
        {
            #ifdef _DEBUG_MULTI_BLOCK_
                _FF_printf(_FF_Multi02Str, resp);
            #endif
            SD_CS_OFF();
            _FF_spi(0xFF);
            return((int8) EOF);
        }
    }
    return(0);
}


/****************************************************************************
**
** Function called when block writing to the SD/MMC media is done
**
** Parameters: None
**
** Returns:  0 - Block ended successfully
**          -1 - An error occurred
**
****************************************************************************/
int8 SD_write_block_end(void)
{
    uint8 resp, calc;
    int16 n, i;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -