///////////////////////////////////////////////////////////////////// // Calibration data error handler ///////////////////////////////////////////////////////////////////// void caldata_error(char *ptr, char *msg) { memset(ptr, 0x20, 80); ptr[0] = '\r'; ptr[80] = '\0'; serwrite(ptr, strlen(ptr)); sprintf(ptr, msg); serwrite(ptr, strlen(ptr)); // Make sure all data gets transmitted before exiting the program while (serwrFree() != OUTBUFSIZE); while((RdPortI(SXSR)&0x08) || (RdPortI(SXSR)&0x04)); }
/* * Print a string on the console. Convert \n to \r\n for serial * line consoles. Locking of the queues is left up to the screen * or uart code. Multi-line messages to serial consoles may get * interspersed with other messages. */ static void putstrn0(char *str, int n, int usewrite) { int m; char *t; char buf[PRINTSIZE + 2]; ERRSTACK(1); /* * if kprint is open, put the message there, otherwise * if there's an attached bit mapped display, * put the message there. */ m = consoleprint; if (canrlock(&(&kprintq)->rwlock)) { if (kprintq.q != NULL) { if (waserror()) { runlock(&(&kprintq)->rwlock); nexterror(); } if (usewrite) qwrite(kprintq.q, str, n); else qiwrite(kprintq.q, str, n); poperror(); m = 0; } runlock(&(&kprintq)->rwlock); } if (m && screenputs != NULL) screenputs(str, n); /* * if there's a serial line being used as a console, * put the message there. */ if (serwrite != NULL) { serwrite(str, n); return; } if (printq == 0) return; while (n > 0) { t = memchr(str, '\n', n); if (t && !kbd.raw) { m = t - str; if (m > sizeof(buf) - 2) m = sizeof(buf) - 2; memmove(buf, str, m); buf[m] = '\r'; buf[m + 1] = '\n'; if (usewrite) qwrite(printq, buf, m + 2); else qiwrite(printq, buf, m + 2); str = t + 1; n -= m + 1; } else { if (usewrite) qwrite(printq, str, n); else qiwrite(printq, str, n); break; } } }
main() { auto int channel, gaincode, ch; char tempbuf[64]; char sendbuf[128]; char i; brdInit(); seropen(BAUDRATE); serrdFlush(); serwrFlush(); if (PRINTDEBUG) { printf("Block size = 0x%x\n", 4096*GetIDBlockSize()); printf("ADC single-ended address 0x%x\n", ADC_CALIB_ADDRS); //single-ended address start printf("DAC single-ended address 0x%x\n", DAC_CALIB_ADDRS); //single-ended address start printf("\nRead constants from user block\n"); } anaInEERd(ALLCHAN); //read all single-ended input anaOutEERd(ALLCHAN); //read all single-ended output memset(sendbuf, '\x0', sizeof(sendbuf)); while(serrdFree() != INBUFSIZE) sergetc(); // Send data out the serial port to the PC serputs("Uploading calibration table . . .\n\r\x0"); serputs("Enter the serial number of your controller = \x0"); ch = 0; i=0; while (ch != '\r') { // make sure you have local echo enabled in Tera Term, so that the // serial number will written to the calibration file. while ((ch = sergetc()) == -1); // Check for a BACKSPACE...allow editing of the serial number if (ch == '\b' && i > 0) { --i; } else tempbuf[i++] = ch; } tempbuf[i] = '\x0'; sprintf(sendbuf, "\n\r::\n\rSN"); strcat(sendbuf, tempbuf); strcat(sendbuf, "\n\r\x0"); serputs(sendbuf); //// //// if (PRINTDEBUG) printf("\n\nFormatting single-ended channels to transmit\n"); sprintf(sendbuf, "ADSE\n\r\x0"); serputs(sendbuf); for(channel = STARTAD; channel <= ENDAD; channel++) { memset(sendbuf, '\x0', sizeof(sendbuf)); sprintf(sendbuf, "%d\n\r\x0", channel); sprintf(tempbuf, "%9.6f,%9.6f,", _adcCalibS[channel].kconst, _adcCalibS[channel].offset); strcat(sendbuf, tempbuf); strcat(sendbuf, "\n\r\x0"); serwrite(sendbuf, sizeof(sendbuf)); } //// //// if (PRINTDEBUG) printf("\n\nFormatting single-ended channels to transmit\n"); sprintf(sendbuf, "DASE\n\r\x0"); serputs(sendbuf); for(channel = STARTDA; channel <= ENDDA; channel++) { memset(sendbuf, '\x0', sizeof(sendbuf)); sprintf(sendbuf, "%d\n\r\x0", channel); sprintf(tempbuf, "%9.6f,%9.6f,", _dacCalibS[channel].kconst, _dacCalibS[channel].offset); strcat(sendbuf, tempbuf); strcat(sendbuf, "\n\r\x0"); serwrite(sendbuf, sizeof(sendbuf)); } sprintf(sendbuf, "END\n\r::\n\r\x0"); serputs(sendbuf); serEputs("End of table upload\n\n\r\x0"); // Make sure all data gets transmitted before exiting the program while (serwrFree() != OUTBUFSIZE); while((RdPortI(SXSR)&0x08) || (RdPortI(SXSR)&0x04)); serclose(); }
///////////////////////////////////////////////////////////////////// // Retrieve analog calibration data and rewrite to the flash ///////////////////////////////////////////////////////////////////// void main() { auto unsigned long fileptr, tempPtr, xmemPtr, index; auto unsigned long len; auto int i; auto char serialNumber[64]; //------------------------------------------------------------------------ // Initialize the Controller //------------------------------------------------------------------------ brdInit(); BitWrPortI(PEDR, &PEDRShadow, 0, 5); //set low to enable rs232 device seropen(BAUDRATE); //set baud rates for the serial ports to be used serwrFlush(); //clear Rx and Tx data buffers serrdFlush(); //------------------------------------------------------------------------ // Allocate and Clear XMEM //------------------------------------------------------------------------ // Allocate XMEM memory for the file that will be read in from the PC xmemPtr = xalloc(FILEBUFSIZE); // Clear the buffer in XMEM for(index =0; index < FILEBUFSIZE; index++) { root2xmem(xmemPtr + index, "\x00", 1); } //------------------------------------------------------------------------ // Download the Data File from the PC //------------------------------------------------------------------------ sprintf(string, "\r\nWaiting...Please Send Data file\n\r"); serwrite(string, strlen(string)); // Get the calibration data file from the PC and put it into XMEM if(!(len = getfile(xmemPtr))) { caldata_error(string, "\r\n\nEncounter an error while reading calibration file"); exit(1); } fileptr = xmemPtr; sprintf(string, "\r\n\nDownload Complete\n\n\r"); serwrite(string, strlen(string)); //------------------------------------------------------------------------ // Parse data file and write to calibrations to flash //------------------------------------------------------------------------ sprintf(string, "\r\nParsing data file\n\r"); serwrite(string, strlen(string)); tempPtr = find_tag(fileptr, len); sprintf(string, "\r\n\nExiting....Calibration data successfully written\n\n\r"); serwrite(string, strlen(string)); while (serwrFree() != OUTBUFSIZE); while((RdPortI(SXSR)&0x08) || (RdPortI(SXSR)&0x04)); serclose(); }
///////////////////////////////////////////////////////////////////// // Locate the calibration data within the file using known // identifier TAGS. ///////////////////////////////////////////////////////////////////// unsigned long find_tag(unsigned long fileptr, long len) { auto char data[2047]; auto long index,i; auto char *begptr, *firstline, *secondline, *saveptr; auto int eofile, eoline, nextcase, dnstate, channel, gaincode; index = 0; xmem2root(data, fileptr, (int)len); begptr = strtok(data, "\n\r"); //begin data file while (strncmp(begptr, "::", 2)) //look for start { begptr = strtok(NULL, "\n\r"); } begptr = strtok(NULL, "\n\r"); serputs("\n\rData file serial number is \x0"); serwrite(begptr, strlen(begptr)); eofile = FALSE; saveptr = begptr+strlen(begptr)+1; while (!eofile) { eoline = FALSE; nextcase = 0; begptr = strtok(saveptr, "\n\r"); saveptr = begptr+strlen(begptr)+1; if (!strncmp(begptr, "ADSE", 4)) { dnstate = 2; } else if (!strncmp(begptr, "ADDF", 4)) { dnstate = 3; } else if (!strncmp(begptr, "ADMA", 4)) { dnstate = 4; } else if (!strncmp(begptr, "END", 3)) { eofile = TRUE; eoline = TRUE; } else nextcase = 1; while (!eoline) { switch (nextcase) { case 2: //single ended firstline = strtok(NULL, "\n\r"); secondline = strtok(NULL, "\n\r"); for (gaincode = 0; gaincode <= 3; gaincode++) { begptr = strtok(firstline, ","); _adcCalibS[channel][gaincode].kconst = atof(begptr); begptr = strtok(NULL, ","); _adcCalibS[channel][gaincode].offset = atof(begptr); firstline = begptr+strlen(begptr)+1; } for (gaincode = 4; gaincode <= 7; gaincode++) { begptr = strtok(secondline, ","); _adcCalibS[channel][gaincode].kconst = atof(begptr); begptr = strtok(NULL, ","); _adcCalibS[channel][gaincode].offset = atof(begptr); secondline = begptr+strlen(begptr)+1; } saveptr = secondline+1; eoline = TRUE; break; case 3: //differential firstline = strtok(NULL, "\n\r"); secondline = strtok(NULL, "\n\r"); for (gaincode = 0; gaincode <= 3; gaincode++) { begptr = strtok(firstline, ","); _adcCalibD[channel][gaincode].kconst = atof(begptr); begptr = strtok(NULL, ","); _adcCalibD[channel][gaincode].offset = atof(begptr); firstline = begptr+strlen(begptr)+1; } for (gaincode = 4; gaincode <= 7; gaincode++) { begptr = strtok(secondline, ","); _adcCalibD[channel][gaincode].kconst = atof(begptr); begptr = strtok(NULL, ","); _adcCalibD[channel][gaincode].offset = atof(begptr); secondline = begptr+strlen(begptr)+1; } saveptr = secondline+1; eoline = TRUE; break; case 4: //milli-amp firstline = strtok(NULL, "\n\r"); begptr = strtok(firstline, ","); _adcCalibM[channel].kconst = atof(begptr); begptr = strtok(NULL, ","); _adcCalibM[channel].offset = atof(begptr); saveptr = begptr+strlen(begptr)+2; eoline = TRUE; break; case 1: channel = atoi(begptr); nextcase = dnstate; eoline = FALSE; break; case 0: eoline = TRUE; break; } //switch } //while not eoline } //while not eofile anaInEEWr(ALLCHAN, SINGLE, gaincode); //read all single-ended anaInEEWr(ALLCHAN, DIFF, gaincode); //read all differential anaInEEWr(ALLCHAN, mAMP, gaincode); //read all milli-amp }