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

📄 sflash.c

📁 IBM source for pallas/vulcan/vesta
💻 C
📖 第 1 页 / 共 2 页
字号:
    }    else    {        printf("scp_flash_stat failed with RC = %x \n", iRetVal);    }    free(pOutParam);    return (iRetVal);}/*-----------------------------------------------------------------------------|   Function        :   sflash_buffer_to_page||   Parameters      :   IN : unsigned short: Page Number < 0 to 1023>|                       IN :unsigned char : Buffer Number to use < 1 or 2>||   Return Value    :   0 - on Success, Non-zero on failure||   Notes           :   Assumes that data is already written to buffer.+----------------------------------------------------------------------------*/int sflash_buffer_to_page(unsigned short wPageNum,unsigned char btBufNum){    int iRetVal = SFLASH_SUCCESS;    unsigned long dwTemp;    unsigned long dwNumOfBytes;    unsigned char * pOutParam;    /* Setup the Input parameters */    memset(flash_cmd, 0, 296);    /* Use Buffer 1 for page writes */    if (btBufNum == SFLASH_BUFFER_1)        flash_cmd[0] = SFLASH_BUFF1_TO_PAGE_CMD;    else if (btBufNum == SFLASH_BUFFER_2)        flash_cmd[0] = SFLASH_BUFF2_TO_PAGE_CMD;    else        return SFLASH_ERR_INVALID_PARAM;    /* place address */    dwTemp = 0;                 /* Starting Address in Buffer */    dwTemp |= (wPageNum << 9);   /* Page Number                */    dwTemp <<= 8;               /* Remove the first bytes     */    memcpy(&flash_cmd[1], &dwTemp, 3);    /* Setup the output parameters */    /* Total bytes = dwNumOfBytes + Flash Command to Write */    dwNumOfBytes = 4;    pOutParam = (unsigned char *)malloc(dwNumOfBytes);    if (pOutParam == NULL)    {        printf("SFLASH: sflash_page_write: malloc failed()\n");        return SFLASH_ERR_MEM_ALLOC;    }    iRetVal = sflash_send_cmd(flash_cmd, pOutParam, dwNumOfBytes);    if(iRetVal != SCP_SUCCESS)    {        printf("scp_flash_stat failed with RC = %x \n", iRetVal);    }    free(pOutParam);    return (iRetVal);}/*-----------------------------------------------------------------------------|   Function        :   sflash_page_to_buffer||   Parameters      :   IN : unsigned short: Page Number < 0 to 1023>|                       IN :unsigned char : Buffer Number to use||   Return Value    :   0 - on Success, Non-zero on failure||   Notes           :+----------------------------------------------------------------------------*/int sflash_page_to_buffer(unsigned short wPageNum,unsigned char btBufNum){    int iRetVal = SFLASH_SUCCESS;    unsigned long dwTemp;    unsigned long dwNumOfBytes;    unsigned char * pOutParam;    int i;    /* Setup the Input parameters */    memset(flash_cmd, 0, 296);    /* Use Buffer 1 for page writes */    if (btBufNum == SFLASH_BUFFER_1)        flash_cmd[0] = SFLASH_PAGE_TO_BUFF1_CMD;    else if (btBufNum == SFLASH_BUFFER_2)        flash_cmd[0] = SFLASH_PAGE_TO_BUFF2_CMD;    else        return SFLASH_ERR_INVALID_PARAM;    /* place address */    dwTemp = 0;                 /* Starting Address in Buffer */    dwTemp |= (wPageNum << 9);   /* Page Number                */    dwTemp <<= 8;               /* Remove the first bytes     */    memcpy(&flash_cmd[1], &dwTemp, 3);    /* Setup the output parameters */    /* Total bytes = dwNumOfBytes + Flash Command to Write */    dwNumOfBytes = 4;    pOutParam = (unsigned char *)malloc(dwNumOfBytes);    if (pOutParam == NULL)    {        printf("SFLASH: sflash_page_write: malloc failed()\n");        return SFLASH_ERR_MEM_ALLOC;    }    iRetVal = sflash_send_cmd(flash_cmd, pOutParam, dwNumOfBytes);    if(iRetVal != SCP_SUCCESS)    {        printf("scp_flash_stat failed with RC = %x \n", iRetVal);    }    free(pOutParam);    return (iRetVal);}/*-----------------------------------------------------------------------------|   Function        :   sflash_partial_page_write||   Parameters      :   IN : unsigned short : Page Number|                       IN : unsigned short : Start Address|                       IN : unsigned char * : Pointer to Data|                       IN : unsigned long  : Length||   Return Value    :   0 - on Success, Non-zero on failure||   Notes           :+----------------------------------------------------------------------------*/int sflash_partial_page_write(unsigned short wPageNum, unsigned short wStartAddr, unsigned char * pData, unsigned long dwLen){    /* Read the Page into Buffer 1*/    if (sflash_ready() == 0)        return SFLASH_OP_ABORTED;    sflash_page_to_buffer(wPageNum, 1);    if (sflash_ready() == 0)        return SFLASH_OP_ABORTED;            /* Copy the user data into buffer */    sflash_write_buffer(SFLASH_BUFFER_1, wStartAddr, dwLen, pData);    if (sflash_ready() == 0)        return SFLASH_OP_ABORTED;    /* Commit the page */    sflash_buffer_to_page(wPageNum, SFLASH_BUFFER_1);    return SFLASH_SUCCESS;}/*-----------------------------------------------------------------------------|   Function        :   sflash_read||   Parameters      :   IN : unsigned long : Start Address <0 - 264K>|                       IN : unsigned long : Number of Bytes to READ <0 - 264>|                      OUT : unsigned char *: Pointer to Data.||   Return Value    :   0 - on Success, Non-zero on failure||   Notes           :   Bypasses Buffer 1 & 2.+----------------------------------------------------------------------------*/int sflash_read(unsigned long dwStartAddr, unsigned long dwLen, unsigned char * pData){    int iRetVal = 0;    unsigned long dwNumOfBytes;    unsigned char * pInParam;    unsigned char * pOutParam;    unsigned long dwTemp;    unsigned short wStartAddrInPage;    unsigned short wPageNum;        PDEBUG("sflash_read: dwStartAddr = 0x%x  dwLen = 0x%x\n", dwStartAddr, dwLen);    dwNumOfBytes = dwLen + 4 + 4 ;        if(dwNumOfBytes > SCP_BUFFER_SIZE)    {       printf("sflash_read: number of btyes to read 0x%x is greater than SCP_BUFFER_SIZE\n");       return -1;    }    pInParam = (unsigned char *)malloc(dwNumOfBytes);    if (pInParam == NULL)    {        return SFLASH_ERR_MEM_ALLOC;    }    pOutParam = (unsigned char *)malloc(dwNumOfBytes);    if (pOutParam == NULL)    {        free(pInParam);        return SFLASH_ERR_MEM_ALLOC;    }    /* place address */    wPageNum = dwStartAddr / SFLASH_PAGE_SIZE;    wStartAddrInPage = dwStartAddr % SFLASH_PAGE_SIZE;    dwTemp = wStartAddrInPage;                 /* Starting Address in Buffer */    dwTemp |= (wPageNum << 9);            /* Page Number                */    dwTemp <<= 8;                        /* Remove the first bytes     */    pInParam[0] = SFLASH_CONT_READ_CMD;    memcpy(&pInParam[1], &dwTemp, 3);        iRetVal = sflash_send_cmd(pInParam, pOutParam, dwNumOfBytes);    if(iRetVal == SCP_SUCCESS)    {        memcpy(pData, pOutParam + 8, dwLen);    }    else    {        printf("scp_flash_stat failed with RC = %x \n", iRetVal);    }    free(pInParam);    free(pOutParam);    return (iRetVal);}/*-----------------------------------------------------------------------------|   Function        :   sflash_write||   Parameters      :   IN : unsigned long : Start Address < 0 to 264K>|                       IN : unsigned long : Number of Bytes to WRITE|                      OUT : unsigned char *: Pointer to Data.||   Return Value    :   0 - on Success, Non-zero on failure||   Notes           :+----------------------------------------------------------------------------*/int sflash_write(unsigned long dwStartAddr, unsigned long dwLen, unsigned char * pData){    int iRetVal = SFLASH_SUCCESS;    unsigned long dwBytesLeft = dwLen;    unsigned long dwBytesToWrite = 0;    unsigned short wStartAddrInPage;    unsigned short wPageNum;    unsigned char btBufNum = 0;    unsigned char * pCurPos;    PDEBUG("sflash_write: dwStartAddr = 0x%x  dwLen = 0x%x\n",dwStartAddr, dwLen);        /* place address */    wPageNum = dwStartAddr / SFLASH_PAGE_SIZE;    wStartAddrInPage = dwStartAddr % SFLASH_PAGE_SIZE;    pCurPos = pData;    btBufNum = 2;    if (wStartAddrInPage)    {        /* Partial Page Write */        dwBytesToWrite = SFLASH_BUFFER_SIZE - wStartAddrInPage;        iRetVal = sflash_partial_page_write(wPageNum, wStartAddrInPage, pCurPos, dwBytesToWrite);        if (iRetVal != SFLASH_SUCCESS)            return iRetVal;        dwBytesLeft -= dwBytesToWrite;        pCurPos += dwBytesToWrite;        wPageNum++;        btBufNum = 1;    }    while (dwBytesLeft >= SFLASH_BUFFER_SIZE)    {        /* Write the data into Buffer - Use 1 and 2 alternatively */        btBufNum = btBufNum == 1 ? 2: 1;        if (sflash_ready() == 0)        {            return SFLASH_OP_ABORTED;        }        sflash_write_buffer(btBufNum, 0, SFLASH_BUFFER_SIZE, pCurPos);        /* Check to see if Device is not busy */        if (sflash_ready() == 0)        {            return SFLASH_OP_ABORTED;        }        /* Execute command to write the buffer to flash page */        sflash_buffer_to_page(wPageNum, btBufNum);        pCurPos += SFLASH_BUFFER_SIZE;        wPageNum++;        dwBytesLeft -= SFLASH_BUFFER_SIZE;    }    /* Partial Page write */    if (dwBytesLeft)    {         dwBytesToWrite = dwBytesLeft;        sflash_partial_page_write(wPageNum, 0, pCurPos, dwBytesToWrite);    }    return (iRetVal);}/*-----------------------------------------------------------------------------|   Function        :   sflash_erase_page||   Parameters      :   IN : unsigned short: Page Number to erase||   Return Value    :   0 - on Success, Non-zero on failure||   Notes           :+----------------------------------------------------------------------------*/int sflash_erase_page(unsigned short wPageNum){    int iRetVal = SFLASH_SUCCESS;    unsigned char *pOutParam;    unsigned long dwNumOfBytes;    unsigned long dwTemp;    /* Setup the Input parameters to scp_io() */    memset(flash_cmd, 0, 296);    if (wPageNum > MAX_PAGES)        return SFLASH_ERR_INVALID_PARAM;    if (sflash_ready() == 0)        return SFLASH_ERR_DEVICE_BUSY;    /* place address */    flash_cmd[0] = SFLASH_ERASE_PAGE_CMD;    dwTemp = 0;    dwTemp = wPageNum << 9;    dwTemp = dwTemp << 8;    memcpy(&flash_cmd[1], &dwTemp, 3);    /* Setup the output parameters to scp_io() */    /* Total bytes = dwNumOfBytes + Flash Command to Read */    dwNumOfBytes = 4 ;    pOutParam = (unsigned char *)malloc(dwNumOfBytes);    if (pOutParam == NULL)    {        printf("SFLASH: sflash_read_buf: malloc failed()\n");        return SFLASH_ERR_MEM_ALLOC;    }    iRetVal = sflash_send_cmd(flash_cmd, pOutParam, dwNumOfBytes);    if (iRetVal != SFLASH_SUCCESS)    {        printf("scp_flash_stat failed with RC = %x \n", iRetVal);    }    free(pOutParam);    return (iRetVal);}/*-----------------------------------------------------------------------------|   Function        :   sflash_erase_block||   Parameters      :   IN : unsigned short: Page Number to erase||   Return Value    :   0 - on Success, Non-zero on failure||   Notes           :+----------------------------------------------------------------------------*/int sflash_erase_block(unsigned short wBlockNum){    int iRetVal = SFLASH_SUCCESS;    unsigned char *pOutParam;    unsigned long dwNumOfBytes;    unsigned long dwTemp;    /* Setup the Input parameters to scp_io() */    memset(flash_cmd, 0, 296);    if (wBlockNum > MAX_BLOCKS)        return SFLASH_ERR_INVALID_PARAM;    if (sflash_ready() == 0)        return SFLASH_ERR_DEVICE_BUSY;    /* place address */    flash_cmd[0] = SFLASH_ERASE_BLOCK_CMD;    dwTemp = 0;    dwTemp = wBlockNum << 12;    dwTemp = dwTemp << 8;    memcpy(&flash_cmd[1], &dwTemp, 3);    /* Setup the output parameters to scp_io() */    /* Total bytes = dwNumOfBytes + Flash Command to Read */    dwNumOfBytes = 4 ;    pOutParam = (unsigned char *)malloc(dwNumOfBytes);    if (pOutParam == NULL)    {        printf("SFLASH: sflash_read_buf: malloc failed()\n");        return SFLASH_ERR_MEM_ALLOC;    }    iRetVal = sflash_send_cmd(flash_cmd, pOutParam, dwNumOfBytes);        // allow some time for the erase to complete    usleep(100);        if (iRetVal != SFLASH_SUCCESS)    {        printf("scp_flash_stat failed with RC = %x \n", iRetVal);    }    free(pOutParam);    return (iRetVal);}

⌨️ 快捷键说明

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