From: hadaq Date: Tue, 1 Sep 2009 15:09:07 +0000 (+0000) Subject: Now supports ADCM1, ADCM2, MDC X-Git-Tag: v6.0~385 X-Git-Url: https://jspc29.x-matter.uni-frankfurt.de/git/?a=commitdiff_plain;h=05beadd0bd9b399d7fd435ecf6d3098a5bcdafca;p=trbnettools.git Now supports ADCM1, ADCM2, MDC --- diff --git a/trbrich/trbflash.c b/trbrich/trbflash.c index 3a31014..dc30286 100644 --- a/trbrich/trbflash.c +++ b/trbrich/trbflash.c @@ -31,18 +31,15 @@ static uint8_t* imageBuffer = NULL; /* Bytes: */ #define PAGE_SIZE 256 -#define NUM_PAGES 16384 +static unsigned int NUM_PAGES = 0; + #define BLOCK_SIZE (64 * 1024) -#define NUM_BLOCKS 64 +static unsigned int NUM_BLOCKS = 0; #define PAGE_BUFFER_SIZE (PAGE_SIZE * NUM_ENDPOINTS) -static const uint32_t manId[1] = { - 0x0000471f -}; - static FILE *logFile = NULL; -static char logFileName[256] = "trbflash.log"; +static const char logFileName[256] = "trbflash.log"; static const char bussy[4][5] = { "|\b", @@ -51,9 +48,20 @@ static const char bussy[4][5] = { "\\\b" }; -#define NUM_MAN_IDS 1 +static const unsigned int timeout = 10000; -/* ------ MAIN ---------------------------------------------------------- */ +typedef enum { + FLASH_INVALID = 0, + FLASH_ADCM1 = 1, + FLASH_ADCM2 = 2, + FLASH_MDC = 3 +} FlashType; + +static const char FlashTypeStr[][16] = {"INVALID", "ADCM1", "ADCM2", "MDC"}; + +static FlashType flashType = FLASH_INVALID; + +/* ------ Local Functions ----------------------------------------------- */ static void atexit0() { @@ -74,8 +82,6 @@ static void atexit0() free(imageBuffer); } -static const unsigned int timeout = 10000; - static int readSetupRegister(uint16_t trb_address, uint8_t value[NUM_ENDPOINTS]) { @@ -150,7 +156,7 @@ static int writeSetupRegister(uint16_t trb_address, uint8_t value) return -1; } - return 1; + return 0; } static int readCtrlRegister(uint16_t trb_address, @@ -214,7 +220,7 @@ static int writeCtrlRegister(uint16_t trb_address, uint32_t value) ctr++; } while (trb_term.status_channel != 0); - return 1; + return 0; } static int sendCommand(uint16_t trb_address, uint32_t cmd, uint8_t max) @@ -271,6 +277,102 @@ static int checkStatus(uint16_t trb_address) return ret; } +static int initTransfer(uint16_t trb_address) +{ + /* Find Endpoint(s) ManId and allocate needed memory */ + uint32_t trbcmd; + int status; + unsigned int counter = 0; + int success = 0; + int i; + + /* Read ManId */ + trbcmd = 0x9f << 24; + if (sendCommand(trb_address, trbcmd, 2) == -1) { + return -1; + } + + if ((status = + trb_register_read(trb_address, BlockRam, trbBuffer, + TRB_BUFFER_SIZE)) == -1) { + fprintf(logFile, "Error > initTransfer: TRBNet %s\n", + trb_strerror(trb_errno)); + return -1; + } + + if (status <= 0) { + return -1; + } + + flashType = FLASH_INVALID; + success = 0; + for (i = 0; i < status; i += 2) { + switch ((trbBuffer[i + 1] & 0x00ffffff)) { + case 0x01461f: + /* ADCM */ + if ((flashType != FLASH_INVALID) && (flashType != FLASH_ADCM1)) { + success = -2; + } + NUM_PAGES = 8192; + flashType = FLASH_ADCM1; + break; + + case 0x00471f: + /* ADCM */ + if ((flashType != FLASH_INVALID) && (flashType != FLASH_ADCM2)) { + success = -2; + } + NUM_PAGES = 16384; + flashType = FLASH_ADCM2; + break; + + case 0x1520c2: + /* MDC */ + if ((flashType != FLASH_INVALID) && (flashType != FLASH_MDC)) { + success = -2; + } + NUM_PAGES = 8192; + flashType = FLASH_MDC; + break; + + default: + flashType = FLASH_INVALID; + success = -1; + break; + } + + if (success == -2) { + fprintf(logFile, "Error > initTransfer: " + "Incompatible ManId(s) 0x%04x on EndPoint 0x%04x\n", + trbBuffer[i + 1], trbBuffer[i] & 0xffff); + return -1; + } + + if (flashType == FLASH_INVALID) { + fprintf(logFile, "Error > initTransfer: " + "Unsupported ManId(s) 0x%04x on EndPoint 0x%04x\n", + trbBuffer[i + 1], trbBuffer[i] & 0xffff); + return -1; + } + + counter++; + } + + NUM_BLOCKS = (NUM_PAGES * PAGE_SIZE) / BLOCK_SIZE; + + /* Buffer holding the entire rom-image */ + imageBuffer = + (uint8_t*)malloc(sizeof(uint8_t) * (PAGE_SIZE * (NUM_PAGES + 2))); + if (imageBuffer == NULL) { + abort(); + } + + fprintf(stderr, "Found %d Endpoint(s) of type %s\n", + counter, FlashTypeStr[flashType]); + + return 0; +} + static int readPage(uint16_t trb_address, uint32_t pageNumber, uint32_t numBytes) { @@ -391,51 +493,6 @@ static int writePage(uint16_t trb_address, uint32_t pageNumber, return 0; } -static int checkManId(uint16_t trb_address) -{ - uint32_t trbcmd; - int status; - int i; - - - /* Read ManId */ - trbcmd = 0x9f << 24; - if (sendCommand(trb_address, trbcmd, 2) == -1) { - return -1; - } - - if ((status = - trb_register_read(trb_address, BlockRam, trbBuffer, - TRB_BUFFER_SIZE)) == -1) { - fprintf(logFile, "Error > checkManId: TRBNet %s\n", - trb_strerror(trb_errno)); - return -1; - } - - if (status <= 0) { - return -1; - } - - for (i = 0; i < status; i += 2) { - unsigned int id; - int accepted = -1; - for (id = 0; id < NUM_MAN_IDS; id++) { - if ((trbBuffer[i + 1] & 0x00ffffff) == manId[id]) { - accepted = 0; - break; - } - } - if (accepted == -1) { - fprintf(logFile, "Error > checkManId: " - "Invalid ManId 0x%04x on EndPoint 0x%04x\n", - trbBuffer[i + 1], trbBuffer[i] & 0xffff); - return -1; - } - } - - return 0; -} - static int programImageBuffer(uint16_t trb_address, unsigned int size, int verifyOnly) { @@ -469,6 +526,26 @@ static int programImageBuffer(uint16_t trb_address, unsigned int size, fprintf(stderr, "Block: 0 1 2 3 4 5 6 7 8 9 A B C D E F"); 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)); + 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; if (block % 16 == 0) { @@ -477,20 +554,22 @@ static int programImageBuffer(uint16_t trb_address, unsigned int size, fprintf(stderr, ".\b"); if (verifyOnly == 0) { - /* Enable writing */ - if (sendCommand(trb_address, 0x06 << 24, 0) == -1) { - fprintf(stderr, "Error > 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", - block); - return -1; + 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"); + return -1; + } + + /* Unprotect sector */ + if (sendCommand(trb_address, 0x39 << 24 | (BLOCK_SIZE * block), 3) + == -1) { + fprintf(stderr, "Error > program: unprotect sector #%d, aborting\n", + block); + return -1; + } } - + /* Enable writing */ if (sendCommand(trb_address, 0x06 << 24, 0) == -1) { fprintf(stderr, "Error > program: write enable, aborting\n"); @@ -533,18 +612,20 @@ 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"); - return -1; - } - - /* Protect sector */ - if (sendCommand(trb_address, 0x36 << 24 | (BLOCK_SIZE * block), 3) - == -1) { - fprintf(stderr, "Error > program: unprotect sector #%d, aborting\n", - block); - return -1; + 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"); + return -1; + } + + /* Protect sector */ + if (sendCommand(trb_address, 0x36 << 24 | (BLOCK_SIZE * block), 3) + == -1) { + fprintf(stderr, "Error > program: unprotect sector #%d, aborting\n", + block); + return -1; + } } } @@ -590,6 +671,26 @@ static int programImageBuffer(uint16_t trb_address, unsigned int size, fprintf(stderr, "X "); } } + + 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)); + 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 { @@ -606,6 +707,10 @@ static int readImageFile(const char *imageFileName) { FILE *imageFile = NULL; int imageSize; + unsigned int i; + + /* Cleanup Buffer */ + for (i = 0; i < (PAGE_SIZE * (NUM_PAGES + 2)); i++) imageBuffer[i] = 0; /* Read in image */ imageFile = fopen(imageFileName, "r"); @@ -635,16 +740,57 @@ static int readImageFile(const char *imageFileName) static int prepareImageBuffer() { + char strId[32] = ""; + int found; + unsigned int end; unsigned int i; + + /* Verify imageFile Id */ + switch (flashType) { + case FLASH_ADCM1: + strncpy(strId, "edif_adcmv1", 32); + break; + + case FLASH_ADCM2: + strncpy(strId, "edif_adcmv2", 32); + break; + + case FLASH_MDC: + strncpy(strId, "mdc_oepb", 32); + break; + + default: + abort(); + break; + } /* Verify imageFile Id */ - if (strncmp(imageBuffer + 105, "edif_adcmv2", 11) != 0) { + found = 0; + for (i = 0; i < 2 * PAGE_SIZE; i++) { + if (memcmp(imageBuffer + i, strId, strlen(strId)) == 0) { + found = 1; + break; + } + } + + if (found == 0) { fprintf(logFile, "Error > prepareImageBuffer: invalid Firmware-Id\n"); return -1; } - - /* Overwrite the first 301 byte with 0xff */ - for (i = 0; i < 0x12a; i++) { + + /* Overwrite Header with 0xff */ + end = 0; + for (i = 0; i < 2 * PAGE_SIZE; i++) { + if (strncmp(imageBuffer + i, "Bitstream CRC: 0x", 17) == 0) { + end = i + 22; + break; + } + } + if (end == 0) { + fprintf(logFile, "Error > prepareImageBuffer: invalid Firmware, " + "CRC not found\n"); + } + for (i = 0; i < end; i++) { imageBuffer[i] = 0xff; } @@ -672,18 +818,25 @@ static int openLog() { return 0; } +/* ------ MAIN ---------------------------------------------------------- */ + void usage(const char *progName) { - printf("Usage: %s [-h] [-d level] \n", + printf("Usage: %s [-h] \n", progName); printf("Options:\n"); printf(" -h give this help\n"); printf("\nCommands:\n"); - printf(" program -> \n"); - printf(" verify -> \n"); - printf(" backup -> \n"); - printf(" restore -> \n"); - printf(" dumppage pagenumber -> \n"); + printf(" program -> " + "program bit-file to flash-memory\n"); + printf(" verify -> " + "compare bit-file to flash-memory\n"); + printf(" backup -> " + "write entire flash-memory to raw-file\n"); + printf(" restore -> " + "write backuped raw-file to flash-memory\n"); + printf(" dumppage pagenumber -> " + "dump one flash-memory page to stdout\n"); } int main(int argc, char ** argv) @@ -706,6 +859,8 @@ int main(int argc, char ** argv) trb_debug = strtoul(optarg, NULL, 0); break; default: + usage(basename(argv[0])); + exit(EXIT_FAILURE); break; } } @@ -746,13 +901,6 @@ int main(int argc, char ** argv) abort(); } - /* Buffer holding the entire rom-image */ - imageBuffer = - (uint8_t*)malloc(sizeof(uint8_t) * (PAGE_SIZE * NUM_PAGES + 2)); - if (imageBuffer == NULL) { - abort(); - } - /* Execute command */ if (strcmp(argv[optind], "program") == 0) { @@ -773,7 +921,7 @@ int main(int argc, char ** argv) imageFileName = argv[optind + 2]; /* Check for correct ManId */ - if (checkManId(trb_address) == -1) { + if (initTransfer(trb_address) == -1) { fprintf(stderr, "Invalid ManId(s), aborting\n"); exit(EXIT_FAILURE); } @@ -819,7 +967,7 @@ int main(int argc, char ** argv) imageFileName = argv[optind + 2]; /* Check for correct ManId */ - if (checkManId(trb_address) == -1) { + if (initTransfer(trb_address) == -1) { fprintf(stderr, "Invalid ManId(s), aborting\n"); exit(EXIT_FAILURE); } @@ -869,9 +1017,8 @@ int main(int argc, char ** argv) "Broadcast addresses are not supported by this command\n"); exit(EXIT_FAILURE); } - - - if (checkManId(trb_address) == -1) { + + if (initTransfer(trb_address) == -1) { exit(EXIT_FAILURE); } @@ -925,7 +1072,7 @@ int main(int argc, char ** argv) imageFileName = argv[optind + 2]; /* Check for correct ManId */ - if (checkManId(trb_address) == -1) { + if (initTransfer(trb_address) == -1) { fprintf(stderr, "Invalid ManId(s), aborting\n"); exit(EXIT_FAILURE); } @@ -972,6 +1119,12 @@ int main(int argc, char ** argv) trb_address = strtoul(argv[optind + 1], NULL, 0); pageNumber = strtoul(argv[optind + 2], NULL, 0); + /* Check for correct ManId */ + if (initTransfer(trb_address) == -1) { + fprintf(stderr, "Invalid ManId(s), aborting\n"); + exit(EXIT_FAILURE); + } + if ((status = readPage(trb_address, pageNumber, 256)) == -1) { fprintf(stderr, "Error reading Page, aborting\n"); exit(EXIT_FAILURE); @@ -981,7 +1134,7 @@ int main(int argc, char ** argv) char text[32] = ""; char cc[2] = " \0"; int c; - fprintf(stdout, "EndPoint: 0x%04x Page #%d\n", + fprintf(stdout, "\nEndPoint: 0x%04x Page #%d\n", pageBufferAddress[i],pageNumber); for (c = 0; c < 256; c++) { if ((c % 16) == 0) {