00001 /****************************************************************************** 00002 * $Id$ / $Rev$ / $Date$ 00003 * $URL$ 00004 *****************************************************************************/ 00005 /* (C) Fujitsu Microelectronics Europe GmbH */ 00006 /* */ 00007 /* The following software deliverable is intended for and must only be */ 00008 /* used in an evaluation laboratory environment. */ 00009 /* It is provided without charge and therefore provided on an as-is */ 00010 /* basis, subject to alterations. */ 00011 /* */ 00012 /* The software deliverable is to be used exclusively in connection */ 00013 /* with FME products. */ 00014 /* In the event the software deliverable includes the use of open */ 00015 /* source components, the provisions of the governing open source */ 00016 /* license agreement shall apply with respect to such software */ 00017 /* deliverable. */ 00018 /* FME does not warrant that the deliverables do not infringe any */ 00019 /* third party intellectual property right (IPR). In the event that */ 00020 /* the deliverables infringe a third party IPR it is the sole */ 00021 /* responsibility of the customer to obtain necessary licenses to */ 00022 /* continue the usage of the deliverable. */ 00023 /* */ 00024 /* To the maximum extent permitted by applicable law FME disclaims all */ 00025 /* warranties, whether express or implied, in particular, but not */ 00026 /* limited to, warranties of merchantability and fitness for a */ 00027 /* particular purpose for which the deliverable is not designated. */ 00028 /* */ 00029 /* To the maximum extent permitted by applicable law, FME’s liability */ 00030 /* is restricted to intention and gross negligence. */ 00031 /* FME is not liable for consequential damages. */ 00032 /* */ 00033 /* (Disclaimer V1.2) */ 00034 /*****************************************************************************/ 00043 #include "base_type.h" 00044 #include "mb96338us.h" /* include all IO-Register definitions */ 00045 #include "uart.h" /* include all UART-Functions */ 00046 #include "integer.h" 00047 /*===========================================================================*/ 00048 00049 /* declarations */ 00050 00051 /* global variables */ 00052 char_t line_buffer[255]; 00053 char_t *line_buffer_ptr; 00054 uint8_t use_uart = 0; 00055 /* constants */ 00056 const char_t ASCII[] = "0123456789ABCDEF"; 00057 00058 /* prototypes */ 00059 00060 /*===========================================================================*/ 00061 00062 00063 /*****************************************************************************/ 00064 /* UART0 Communication routines */ 00065 /*****************************************************************************/ 00066 00067 /***************************************************************************** 00068 * DESCRIPTION: Initializes UART1 00069 * 00070 * PARAMETERS: None 00071 * 00072 * RETURNS: None 00073 *****************************************************************************/ 00074 00075 void InitUART0( void ) 00076 { 00077 PIER08 |= 0x04; // enable SIN0 00078 DDR08_D3 = 1; // SOT0 = output 00079 00080 BGR0 = 417; // 115.2kbaud @ 48 MHz 00081 SCR0 = 0x17; // 8N1 00082 SMR0 = 0x0d; // enable SOT0, Reset, normal mode 00083 SSR0 = 0x00; // LSB first 00084 use_uart = 0; 00085 } 00086 void InitUART1( void ) 00087 { 00088 PIER08_IE5 = 1; // enable SIN1 00089 DDR08_D6 = 1; // SOT1 = output 00090 00091 BGR1 = 417; // 115.2kbaud @ 48 MHz 00092 SCR1 = 0x17; // 8N1 00093 SMR1 = 0x0d; // enable SOT1, Reset, normal mode 00094 SSR1 = 0x00; // LSB first 00095 use_uart = 1; 00096 } 00097 00098 /***************************************************************************** 00099 * DESCRIPTION: sends a single character (no timeout !) 00100 * 00101 * PARAMETERS: Character 00102 * 00103 * RETURNS: None 00104 *****************************************************************************/ 00105 void putch (char_t ch) /* sends a char_t */ 00106 { 00107 switch (use_uart) { 00108 case 0: 00109 while (SSR0_TDRE == 0); // wait for transmit buffer empty 00110 TDR0 = ch; // put ch into buffer 00111 break; 00112 case 1: 00113 while (SSR1_TDRE == 0); // wait for transmit buffer empty 00114 TDR1 = ch; // put ch into buffer 00115 break; 00116 } 00117 } 00118 00119 /***************************************************************************** 00120 * DESCRIPTION: sends a complete string (0-terminated) 00121 * 00122 * PARAMETERS: Pointer to (constant) file of bytes in mem 00123 * 00124 * RETURNS: None 00125 *****************************************************************************/ 00126 void puts(char_t *buf) 00127 { 00128 while (*buf != '\0') 00129 { 00130 if(*buf == '\n') putch('\r'); 00131 putch(*buf++); /* send every char_t of string */ 00132 } 00133 } 00134 00135 /***************************************************************************** 00136 * DESCRIPTION: sends a x-digit Hex-number (as ASCII charcaters) 00137 * to terminal via ext. UART 00138 * 00139 * PARAMETERS: Value and number of Hex-digits (e.g. FF = 2 Hex digits) 00140 * 00141 * RETURNS: NONE 00142 *****************************************************************************/ 00143 void puthex(uint32_t n, uint8_t digits) 00144 { 00145 uint8_t i,ch,div=0; 00146 00147 puts("0x"); /* hex string */ 00148 div=(digits-1) << 2; /* init shift divisor */ 00149 00150 for (i=0; i<digits; i++) { 00151 ch = (n >> div) & 0xF;/* get hex-digit value */ 00152 putch(ASCII[ch]); /* prompt to terminal as ASCII */ 00153 div-=4; /* next digit shift */ 00154 } 00155 } 00156 00157 void putbytehex(BYTE n) 00158 { 00159 uint8_t digits = 2; 00160 uint8_t i,ch,div=0; 00161 00162 puts("0x"); /* hex string */ 00163 div=(digits-1) << 2; /* init shift divisor */ 00164 00165 for (i=0; i<digits; i++) { 00166 ch = (n >> div) & 0xF;/* get hex-digit value */ 00167 putch(ASCII[ch]); /* prompt to terminal as ASCII */ 00168 div-=4; /* next digit shift */ 00169 } 00170 } 00171 /***************************************************************************** 00172 * DESCRIPTION: send a x-digit Dec-number (as ASCII charcaters) 00173 * to terminal via ext. UART 00174 * 00175 * PARAMETERS: integer value 00176 * 00177 * RETURNS: None 00178 *****************************************************************************/ 00179 void putdec(uint32_t x) 00180 { 00181 int16_t i; 00182 char_t buf[9]; 00183 if (x == 0) 00184 { 00185 puts(" 0"); 00186 return; 00187 } 00188 buf[8]='\0'; /* end sign of string */ 00189 00190 for (i=8; i>0; i--) 00191 { 00192 buf[i-1] = ASCII[x % 10]; 00193 x = x/10; 00194 00195 } 00196 00197 for (i=0; buf[i]=='0'; i++) // no print16_t of zero 00198 { 00199 buf[i] = ' '; 00200 } 00201 puts(buf); /* send string */ 00202 } 00203 00204 /***************************************************************************** 00205 * DESCRIPTION: waits for and returns incoming character (no timeout !) 00206 * 00207 * PARAMETERS: None 00208 * 00209 * RETURNS: Character or "-1" (Error) 00210 *****************************************************************************/ 00211 char_t getch (void) 00212 { 00213 char_t data; 00214 while((!SSR0_RDRF) && (!SSR1_RDRF)); 00215 if (SSR0_RDRF) use_uart = 0; 00216 if (SSR1_RDRF) use_uart = 1; 00217 switch (use_uart) { 00218 case 0: 00219 while(!SSR0_RDRF); /* wait for data in buffer */ 00220 if(SSR0_FRE || SSR0_ORE || SSR0_PE) // Framing-, overrun- or parity error 00221 { 00222 data = RDR0; 00223 SCR0_CRE = 1; // reset FRE, ORE, PE error flags 00224 return (-1); 00225 } else 00226 return (RDR0); /* return char_t */ 00227 break; 00228 00229 case 1: 00230 while(!SSR1_RDRF); /* wait for data in buffer */ 00231 if(SSR1_FRE || SSR1_ORE || SSR1_PE) // Framing-, overrun- or parity error 00232 { 00233 data = RDR1; 00234 SCR1_CRE = 1; // reset FRE, ORE, PE error flags 00235 return (-1); 00236 } else 00237 return (RDR1); /* return char_t */ 00238 break; 00239 } 00240 return (-1); 00241 } 00242 00243 /***************************************************************************** 00244 * DESCRIPTION: Converts ASCII inputs (0..8,a..f,A..F) to num. value 00245 * 00246 * PARAMETERS: ASCII input 00247 * 00248 * RETURNS: value or -1 (error) 00249 *****************************************************************************/ 00250 uint32_t ASCIItobin(uint8_t k) 00251 { 00252 char_t d=(char_t) -1; 00253 if ((k > 47) & (k < 58)) d = k - 48; /* 0..9 */ 00254 if ((k > 64) & (k < 71)) d = k - 55; /* A..F */ 00255 if ((k > 96) & (k < 103)) d = k - 87; /* a..f */ 00256 return d; 00257 } 00258 00259 /***************************************************************************** 00260 * DESCRIPTION: Receives a string until CR from ext UART 00261 * 00262 * PARAMETERS: (stored in global variable "line_buffer") 00263 * 00264 * RETURNS: None 00265 *****************************************************************************/ 00266 void receive_line(void) 00267 { 00268 uint8_t ch; 00269 uint16_t i=0; 00270 00271 do { 00272 ch = getch(); 00273 if((ch == '\r') | (ch=='\n')) break; 00274 line_buffer[i++] = ch; 00275 } while(1); 00276 00277 line_buffer[i]='\0'; 00278 line_buffer_ptr = line_buffer + i; 00279 } 00280 00281 /***************************************************************************** 00282 * DESCRIPTION: Receives a string until CR from ext UART + Echo 00283 * 00284 * PARAMETERS: (stored in global variable "line_buffer") 00285 * 00286 * RETURNS: None 00287 *****************************************************************************/ 00288 int16_t receive_line_echo(uint16_t *cnt) 00289 { 00290 00291 uint8_t ch; 00292 uint16_t i=0; 00293 memset(line_buffer,0,sizeof(line_buffer)); 00294 00295 do { 00296 ch = 0xFF; 00297 while(ch == 0xFF) 00298 { 00299 ch = getch(); 00300 } 00301 if ((i == 0) & (ch != 8) | (i > 0)) putch(ch); 00302 if((ch == 13) | (ch==27) | (i > 252)) break; 00303 if(ch == 8) 00304 { 00305 if (i > 0) line_buffer[i--] == '\0'; 00306 } else 00307 { 00308 line_buffer[i++] = ch; 00309 } 00310 } while(1); 00311 00312 line_buffer[i++]='\0'; 00313 line_buffer_ptr = line_buffer + i; 00314 if (cnt) *cnt = i; 00315 //BufferPtr = (char_t) &line_buffer; 00316 return (int16_t) &line_buffer; 00317 } 00318 00319 /*********************************************************************** 00320 * DESCRIPTION: Scans the string 'line_buffer' for the string 'string'. 00321 * Both strings must be null-terminated. 'line_buffer_ptr' 00322 * is set appropriately. 00323 * RETURN VALUE: 1, if string 'string' is contained in string 'line_buffer' 00324 * at its beginning. 'line_buffer_ptr' points to the 00325 * next character of 'line_buffer' after the equal field. 00326 * 2, if string 'string' was not found in string 'line_buffer'. 00327 * 0, if strings are identical to the final null character. 00328 ************************************************************************/ 00329 int16_t scan_line(char_t *str) 00330 { 00331 line_buffer_ptr = line_buffer; 00332 00333 while((int16_t)*line_buffer_ptr==(int16_t)*str) 00334 { 00335 if((int16_t)*str=='\0') return(0); 00336 line_buffer_ptr++; 00337 str++; 00338 } 00339 00340 if((int16_t)*str=='\0') return(1); 00341 return(2); 00342 } 00343 00344 /***************************************************************************** 00345 * DESCRIPTION: Inputs a x-digit Hex-number (ASCII characters expected) 00346 * ESC aborts input 00347 * 00348 * PARAMETERS: Number of Hex-digits (e.g. FF = 2 Hex digits) 00349 * 00350 * RETURNS: Input value or -1 (abort) 00351 *****************************************************************************/ 00352 uint32_t Inputhex(uint8_t digits) 00353 { 00354 uint32_t number=0,digit=0; 00355 uint8_t abort=0,mlt=0,i,key; 00356 00357 mlt=(4*(digits-1)); /* 20 for 6 hex-digit numers, 4 for 2 hex-digits */ 00358 for (i=0;i<digits;i++) 00359 { 00360 digit = -1; 00361 while ((digit==-1) & (!abort)) /* input next valid digit */ 00362 { 00363 key = getch(); /* wait for next key input (ASCII) */ 00364 putch(key); 00365 if (key == 27) abort=1; /* ESC aborts */ 00366 digit = ASCIItobin(key); /* convert to number */ 00367 if (digit == -1) putch(8); /* backspace if not valid */ 00368 } 00369 number+= (digit << mlt); /* add digit value to number */ 00370 mlt-=4; /* next digit shift */ 00371 } 00372 00373 if (!abort) 00374 return number; /* return input value */ 00375 else 00376 { 00377 puts("\n\n input cancled \n"); 00378 return -1; /* return abort indicator */ 00379 } 00380 } 00381 00382 char_t getkey(char_t LKey, char_t HKey) 00383 { 00384 char_t key; 00385 00386 do /* input next valid digit */ 00387 { 00388 key = upcase(getch()); /* wait for next key input (0-9,A-Z,a-z) */ 00389 if (key == 27) 00390 { 00391 puts("\r>>> cancel input \n"); 00392 return -1; /* return with ESC aborts */ 00393 } 00394 00395 if ( (key < LKey) || (key > HKey) ) 00396 { 00397 /* undefinded key pressed */ 00398 puts("\r>>> key not defined \r"); 00399 } 00400 else 00401 { 00402 puts("\r> \r"); 00403 putch(key); 00404 return key; /* return input value */ 00405 } 00406 00407 } while(1); 00408 } 00409 00410 char_t upcase(char_t k) 00411 { 00412 char_t d=(char_t) -1; 00413 if ((k > 47) & (k < 58)) d = k; /* 0..9 */ 00414 if ((k > 64) & (k < 71)) d = k; /* A..F */ 00415 if ((k > 96) & (k < 123)) d = k - 32; /* a..f */ 00416 if (k == 27) d = k; /* ESC */ 00417 return d; 00418 } 00419 //============================================================================ 00420 // ----------------------- End of Procedure --------------------------------- 00421 //============================================================================
1.6.1