From d419758ff906473a4b1e05476e2b204b96f62590 Mon Sep 17 00:00:00 2001 From: hadaq Date: Wed, 2 Sep 2009 14:04:48 +0000 Subject: [PATCH] bugfixes --- trbrich/trbflash.c | 108 +++++++++++++++++++++++++-------------------- 1 file changed, 60 insertions(+), 48 deletions(-) diff --git a/trbrich/trbflash.c b/trbrich/trbflash.c index dc30286..7be5f2c 100644 --- a/trbrich/trbflash.c +++ b/trbrich/trbflash.c @@ -242,7 +242,7 @@ static int checkStatus(uint16_t trb_address) { uint32_t trbcmd; int status; - int ret = 0; + int ret; int i; /* Read Status Register */ @@ -263,11 +263,15 @@ static int checkStatus(uint16_t trb_address) return -1; } + ret = 0; for (i = 0; i < status; i += 2) { - if (((trbBuffer[i + 1] >> 5) & 0x01) == 1) { - fprintf(logFile, "Error > checkStatus: Erase or program error on " - "EndPoint 0x%04x\n", trbBuffer[i] & 0xffff); - return -1; /* Fatal Error */ + if ((flashType == FLASH_ADCM1) || (flashType == FLASH_ADCM2)) { + /* Check EPE Bit (ADCM only) */ + if (((trbBuffer[i + 1] >> 5) & 0x01) == 1) { + fprintf(logFile, "Error > checkStatus: Erase or program error on " + "EndPoint 0x%04x\n", trbBuffer[i] & 0xffff); + return -1; /* Fatal Error */ + } } if ((trbBuffer[i + 1] & 0x01) == 1) { ret = -2; /* One is busy */ @@ -277,6 +281,37 @@ static int checkStatus(uint16_t trb_address) return ret; } +static int writeStatusRegister(uint16_t trb_address, uint8_t value) +{ + int status; + + /* Enable writing */ + if (sendCommand(trb_address, 0x06 << 24, 0) == -1) { + fprintf(logFile, "Error > writeStatusRegister: write enable\n"); + return -1; + } + + if (trb_register_write(trb_address, BlockRam, (uint32_t)value) == -1) { + fprintf(logFile, "Error > writeStatusRegister: TRBNet %s\n", + trb_strerror(trb_errno)); + return -1; + } + if (sendCommand(trb_address, 0x01 << 24, 0) == -1) { + fprintf(logFile, "Error > writeStatusRegister: sendCommand\n"); + return -1; + } + + /* Wait for not busy and check status */ + while ((status = checkStatus(trb_address) == -2)) {} + + if (status != 0) { + fprintf(logFile, "Error > writeStatusRegister: invalid status\n"); + return -1; + } + + return 0; +} + static int initTransfer(uint16_t trb_address) { /* Find Endpoint(s) ManId and allocate needed memory */ @@ -528,23 +563,12 @@ static int programImageBuffer(uint16_t trb_address, unsigned int size, errorCtr = 0; if ((verifyOnly == 0) && (flashType == FLASH_MDC)) { - /* Enable writing */ - if (sendCommand(trb_address, 0x06 << 24, 0) == -1) { - fprintf(stderr, "Error > program: write enable, aborting\n"); - return -1; - } - /* Unprotect all Sectors */ - if (trb_register_write(trb_address, BlockRam, 0) == -1) { - fprintf(logFile, "Error > program: TRBNet %s\n", - trb_strerror(trb_errno)); + if (writeStatusRegister(trb_address, 0) == -1) { + fprintf(stderr, "\nError > program: unprotect all sectors, aborting\n"); return -1; } - if (sendCommand(trb_address, 0x01 << 24, 0) == -1) { - fprintf(stderr, "Error > program: unprotect all sectors, aborting\n"); - return -1; - } - } + } for (block = 0; (block < NUM_BLOCKS) && (bytesWritten > 0); block++) { int error = 0; @@ -557,14 +581,14 @@ static int programImageBuffer(uint16_t trb_address, unsigned int size, if ((flashType == FLASH_ADCM1) || (flashType == FLASH_ADCM2)) { /* Enable writing */ if (sendCommand(trb_address, 0x06 << 24, 0) == -1) { - fprintf(stderr, "Error > program: write enable, aborting\n"); + fprintf(stderr, "\nError > program: write enable, aborting\n"); return -1; } /* Unprotect sector */ if (sendCommand(trb_address, 0x39 << 24 | (BLOCK_SIZE * block), 3) == -1) { - fprintf(stderr, "Error > program: unprotect sector #%d, aborting\n", + fprintf(stderr, "\nError > program: unprotect sector #%d, aborting\n", block); return -1; } @@ -572,7 +596,7 @@ static int programImageBuffer(uint16_t trb_address, unsigned int size, /* Enable writing */ if (sendCommand(trb_address, 0x06 << 24, 0) == -1) { - fprintf(stderr, "Error > program: write enable, aborting\n"); + fprintf(stderr, "\nError > program: write enable, aborting\n"); return -1; } @@ -580,18 +604,16 @@ static int programImageBuffer(uint16_t trb_address, unsigned int size, fprintf(stderr, "E\b"); if (sendCommand(trb_address, 0xd8 << 24 | (BLOCK_SIZE * block), 3) == -1) { - fprintf(stderr, "Error > program: erase block #%d, aborting\n", + fprintf(stderr, "\nError > program: erase block #%d, aborting\n", block); return -1; } /* Wait for not busy and check status */ - while ((status = checkStatus(trb_address) == -2)) { - /* fprintf(stderr, "Wait erase..\n");*/ - } + while ((status = checkStatus(trb_address) == -2)) {} if (status != 0) { - fprintf(stderr, "Error > program: invalid status, aborting\n"); + fprintf(stderr, "\nError > program: invalid status, aborting\n"); return -1; } @@ -606,7 +628,7 @@ static int programImageBuffer(uint16_t trb_address, unsigned int size, status = writePage(trb_address, page, imageBuffer + page * PAGE_SIZE, bytes); if (status == -1) { - fprintf(stderr, "Error > program: pageProgram page #%d, aborting\n", + fprintf(stderr, "\nError > program: pageProgram page #%d, aborting\n", page); return -1; } @@ -615,14 +637,14 @@ static int programImageBuffer(uint16_t trb_address, unsigned int size, if ((flashType == FLASH_ADCM1) || (flashType == FLASH_ADCM2)) { /* Enable writing */ if (sendCommand(trb_address, 0x06 << 24, 0) == -1) { - fprintf(stderr, "Error > program: write enable, aborting\n"); + fprintf(stderr, "\nError > program: write enable, aborting\n"); return -1; } /* Protect sector */ if (sendCommand(trb_address, 0x36 << 24 | (BLOCK_SIZE * block), 3) == -1) { - fprintf(stderr, "Error > program: unprotect sector #%d, aborting\n", + fprintf(stderr, "\nError > program: unprotect sector #%d, aborting\n", block); return -1; } @@ -641,7 +663,7 @@ static int programImageBuffer(uint16_t trb_address, unsigned int size, tmp -= bytes; if ((status = readPage(trb_address, page, bytes)) == -1) { - fprintf(stderr, "Error > program: reading Page #%d, aborting\n", + fprintf(stderr, "\nError > program: reading Page #%d, aborting\n", page); return -1; } @@ -673,24 +695,13 @@ static int programImageBuffer(uint16_t trb_address, unsigned int size, } if ((verifyOnly == 0) && (flashType == FLASH_MDC)) { - /* Enable writing */ - if (sendCommand(trb_address, 0x06 << 24, 0) == -1) { - fprintf(stderr, "Error > program: write enable, aborting\n"); - return -1; - } - /* Protect all Sectors */ - if (trb_register_write(trb_address, BlockRam, 0x3c) == -1) { - fprintf(logFile, "Error > program: TRBNet %s\n", - trb_strerror(trb_errno)); + if (writeStatusRegister(trb_address, 0x3c) == -1) { + fprintf(stderr, "\nError > program: protect all sectors, aborting\n"); return -1; } - if (sendCommand(trb_address, 0x01 << 24, 0) == -1) { - fprintf(stderr, "Error > program: unprotect all sectors, aborting\n"); - return -1; - } - } - + } + if (errorCtr == 0) { fprintf(stderr, "\n\nSuccess\n\n"); } else { @@ -1038,12 +1049,13 @@ int main(int argc, char ** argv) for (page = 0; page < NUM_PAGES; page++) { if (readPage(trb_address, page, PAGE_SIZE) == -1) { - fprintf(stderr, "Error reading Page #%d, aborting..\n", page); + fprintf(stderr, "\nError reading Page #%d, aborting..\n", page); fclose(imageFile); exit(EXIT_FAILURE); } if (fwrite((void*)pageBuffer[0], PAGE_SIZE, 1, imageFile) != 1) { - fprintf(stderr, "Error writing Page #%d to file, aborting..\n", page); + fprintf(stderr, + "\nError writing Page #%d to file, aborting..\n", page); fclose(imageFile); exit(EXIT_FAILURE); } -- 2.43.0