// // General I/O support routines // // History: Created on May 1st 2002 by Stephane Gauthier (stephane.gauthier@alcatel.com) // - August 29th 2002 - S. Gauthier - Added PutHexLong() and adjusted _PutHex() accordingly. // - January 3rd 2003 - S. Gauthier - Added GetDecSignedWord(). // #include #include "generalio.h" // Include to prevent scope issues for routines using other routines defined lower (abs_byte in PutDecSignedByte) #include "serialio.h" // For Low level routines. #include // For atoi(). #include "hardware.h" // For TRUE & FALSE. #include // For isxdigit() & others. #include // For reading strings in FLASH (PRG_RDB()) // Function Name: PrintString() // // Description: Output a character string stored in FLASH. // // void PrintString(const char * s) { char ch; while ((ch = PRG_RDB(s++))){PutChar(ch);} } // Function Name: _GetChar() // // Description: Get a character from serial port and echo it back. // // unsigned char _GetChar(void) { unsigned char data; data = GetChar(); PutChar(data); return(data); } // Function Name: GetHexWord() // // Description: Get a Hex Word from serial port. // // unsigned int GetHexWord(void) { return(_GetHex(4)); // Get Four digits } // Function Name: GetHexByte() // // Description: Get a Hex Byte from serial port. // // unsigned char GetHexByte(void) { return(_GetHex(2)); // Get Two digits } // Function Name: GetHexChar() // // Description: Get a single Hex digit from serial port. // // unsigned char GetHexChar(void) { return(_GetHex(1)); // Get One digit } // Function Name: _GetHex() // // Description: Get the specified number of Hex digit from serial port. // // unsigned int _GetHex(unsigned char digits) { unsigned int value = 0; unsigned char i = 0; unsigned char digit; unsigned char temp = 0; for (i = digits ; i > 0 ; i--) { do { digit = GetChar(); }while (!isxdigit(digit)); // Validate input PutChar(digit); // Echo it if (isalpha(digit)) { digit = toupper(digit); temp = digit - 0x37; value += temp << (4 * (i-1)); } else value += (atoi((unsigned char *)&digit) << (4 * (i-1))); } return(value); } // Function Name: PutHexLong() // // Description: Format and output Long (32bits) Word to serial port. // // void PutHexLong(unsigned long data) { // To prevent confusion with decimal PutChar('0'); PutChar('x'); _PutHex(data>>16, 2); // Put out upper Four Hex digits. _PutHex(data, 2); } // Function Name: PutHexWord() // // Description: Format and output Hex Word (16bits)to serial port. // // void PutHexWord(unsigned int data) { // To prevent confusion with decimal PutChar('0'); PutChar('x'); _PutHex(data, 2); // Put out Four Hex digits. } // Function Name: PutHexByte() // // Description: Format and output Hex Byte (8bits) to serial port. // // void PutHexByte(unsigned char data) { // To prevent confusion with decimal PutChar('0'); PutChar('x'); _PutHex((unsigned char)data, 1); // Put out Two Hex digits. } // Function Name: PutHexChar() // // Description: Format and output a single Hex digit to serial port. // // void PutHexChar(unsigned char data) { // To prevent confusion with decimal PutChar('0'); PutChar('x'); PutChar(asciify(data)); // Put out One Hex digits. } // Function Name: _PutHex() // // Description: Format and output specified number of Hex byte to serial port. // // void _PutHex(unsigned int data, unsigned char size) { unsigned int temp; if (size > 1) { // Do Upper nibble temp = data >> 8; PutChar(asciify(temp>>4)); // Do lower nibble PutChar(asciify(temp)); } // Do Upper nibble temp = data >> 4; PutChar(asciify(temp)); // Do lower nibble PutChar(asciify(data)); } // Function Name: PutCRLF() // // Description: Bring terminal cursor to beginning of next line // // void PutCRLF(void) { PutChar(0x0D); PutChar(0x0A); } // Function Name: asciffy() // // Description: Convert low nibble to character. // // unsigned char asciify(unsigned char value) { // Check for invalid out-of-range value and return the ? symbol. value = value & 0x0F; //truncate unused upper nibble if (value > 0x09) return(value + 0x37); else return(value + 0x30); } // Function Name: GetDecWord() // // Description: Reads in a 3 digit decimal number from the serial port // // unsigned int GetDecWord(void) { unsigned int total = 0; unsigned char in; // Get valid digit for 100's in = GetChar(); while ( in < '0' || in > '9') in = GetChar(); PutChar(in); total = (in - '0') * 100; // Get valid digit for 10's in = GetChar(); while ( in < '0' || in > '9') in = GetChar(); PutChar(in); total += (in - '0') * 10; // Get valid digit for 1's in = GetChar(); while ( in < '0' || in > '9') in = GetChar(); PutChar(in); total += (in - '0'); return(total); } // Function Name: PutDecWord() // // Description: This function will format and send a Word to the serial port in a decimal format. (eg. 0x0041 = 65) // // void PutDecWord(unsigned int value) { unsigned char count = 0; // Special case if (value == 0) { PutChar('0'); return; } /* how many hundred of thousands? */ /* while(value > 99999) { value = value - 100000; count++; } if (count != 0 || value == 0) // Remove leading 0s PutChar('0' + count); count = 0; */ /* how many tens of thousands? */ while(value > 9999) { value = value - 10000; count++; } if (count != 0 || value == 0) // Remove leading 0s PutChar('0' + count); count = 0; /* how many thousands? */ while(value > 999) { value = value - 1000; count++; } if (count != 0 || value == 0) // Remove leading 0s PutChar('0' + count); count = 0; /* how many hundreds? */ while(value > 99) { value = value - 100; count++; } if (count != 0 || value == 0) // Remove leading 0s PutChar('0' + count); /* how many tens? */ count = 0; while(value > 9) { value = value - 10; count++; } if (count != 0 || value == 0) // Remove leading 0s PutChar('0' + count); /* and finally units */ PutChar('0' + value); } // Function Name: PutDecSignedByte() // // Description: This function will format and send a SIGNED BYTE to the serial port in a decimal format. (eg. 0x41 = +65) // // void PutDecSignedByte(signed char value) { if (value > 0) PutChar('+'); else if (value < 0) PutChar('-'); // Otherwise it's probably a zero so don't output any sign PutDecByte((unsigned char)(abs(value))); } // Function Name: PutDecSignedWord() // // Description: This function will format and send a SIGNED BYTE to the serial port in a decimal format. (eg. 0x41 = +65) // // void PutDecSignedWord(signed int value) { if (value > 0) PutChar('+'); else if (value < 0) PutChar('-'); // Otherwise it's probably a zero so don't output any sign PutDecWord((unsigned int)(abs(value))); } // Function Name: abs_byte() // // Description: This function will return the absolute of the passed signed byte // // unsigned char abs_byte(signed char x) { if (x > 0) return x; if (x < 0) return -x; // Zero case. return 0; } // Function Name: abs_word() // // Description: This function will return the absolute of the passed signed word // // unsigned int abs_word(signed int x) { if (x > 0) return x; if (x < 0) return -x; // Zero case. return 0; } // Function Name: PutBin() // // Description: This function will format and send a byte to the serial port in a binary format. (eg. 0x41 = 00100001b) // // void PutBin(unsigned char value) { unsigned char i; unsigned char temp = 0x80; for (i = 0; i < 8 ; i++) { if (value & temp) PutChar('1'); else PutChar('0'); temp = temp >> 1; } //PutChar('b'); } // Function Name: PutDecByte() // // Description: This function will format and send a BYTE to the serial port in a decimal format. (eg. 0x41 = 65) // // void PutDecByte(unsigned char value) { unsigned char count = 0; // Special case if (value == 0) { PutChar('0'); return; } /* how many hundreds? */ while (value > 99) { value = value - 100; count++; } if (count != 0 || value == 0) // Remove leading 0s PutChar('0' + count); /* how many tens? */ count = 0; while (value > 9) { value = value - 10; count++; } if (count != 0 || value == 0) // Remove leading 0s PutChar('0' + count); /* and finally units */ PutChar('0' + value); } // Function Name: GetDecSignedWord() // // Description: Reads in a signed 3 digit decimal number from the serial port // // int GetDecSignedWord(void) { #define PLUS 1 #define MINUS 0 int total = 0; unsigned char in; unsigned char sign; // Get sign. in = GetChar(); if (in == '-') { sign = MINUS; PutChar('-'); } else { sign = PLUS; PutChar('+'); } // Get valid digit for 100's //if (in == '-' || in =) //in = GetChar(); while ( in < '0' || in > '9') in = GetChar(); PutChar(in); total = (in - '0') * 100; // Get valid digit for 10's in = GetChar(); while ( in < '0' || in > '9') in = GetChar(); PutChar(in); total += (in - '0') * 10; // Get valid digit for 1's in = GetChar(); while ( in < '0' || in > '9') in = GetChar(); PutChar(in); total += (in - '0'); if (sign == MINUS) return(-total); else return(total); }