#include <90usb1287.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <interface.h>
#include <uart_drv.h>
#define CMD_SIZE 57
#define USB_CMD_SIZE 52
flash unsigned char * flash cmdList[CMD_SIZE] = {
"", // [0] no com
"INF", // [1] Displayes version date etc.
"RESET", // [2] Reset CPU
"boot", // [3] Goto boot
"DUMP", // [4] Display manual debug info
"AUT", // [5] Start automatic scanning
"STOP", // [6] Stop scanning
"STAF", // [7] Set start frequency
"STOF", // [8] Set stop frequency
"RES", // [9] Display manual debug info
"RATE", // [10] Display manual debug info
"GAIN", // [11] Set gain
"SCAN", // [12] Start custom scan
"SETUP", // [13] Display manual scan setup info
"TEMP", // [14] Set temperature (Celsius)
.....................
...................... (52 commands for USB board and 5 commands for ATmega32A board )
};
/***************************************************
C O M U N I C A T I O N RS-232
****************************************************/
unsigned char SerIn[SIBUFSIZE]; // Input buffer (raw data)
unsigned char RxCnt; // Location of next byte to be written
unsigned char RdCnt; // Location of next byte to be read
unsigned char BufCnt; // Size of unread contents in ring buffer
unsigned char CompIndex; // Index in Copmare array
unsigned char Compare[COMPBUFSIZE]; // Command string tokenizer
unsigned char Uart_CompIndex; // Index in Copmare array
unsigned char Command; // Current Command is executed
unsigned int Param; // Parameter used in command
float Param2; // Optional (second) parameter used in command
unsigned long Param3; // Optional (third) parameter in command
extern unsigned char Plot;
unsigned char Step;
// USART1 Receiver interrupt service routine
interrupt [USART1_RXC] void usart1_rx_isr(void){
char status,data;
status=UCSR1A;
data=UDR1;
printf("%c", data);
}
// USB Receive
void catchString(void){
while(UEBCLX){
if(++BufCnt >= SIBUFSIZE){ // Increment & check for buffer overflow
BufCnt = SIBUFSIZE-1; // Set to max value
// printf("!Overflow\r\n");
// UENUM = 2;
return; // Skip char
}else{ // Else: if buffer ok
if(++RxCnt >= SIBUFSIZE) RxCnt = 0; // Increment read counter, if 10 -> 0 (max 9)
SerIn[RxCnt] = UEDATX; // Write to SBUF (load the transmit register)
}
}
}
// Read from ringbuffer
char getcharb(void){
if(BufCnt){ // If anything
BufCnt--; // Decrement buffer counter
if(++RdCnt >= SIBUFSIZE) RdCnt = 0; // Increment read counter, if 10 -> 0 (max 9)
return SerIn[RdCnt]; // Read from SBUF (access receive register)
}
return 0;
}
void help(void){
unsigned char i;
printf("Commands: ");
for(i=0;i<CMD_SIZE;i++){
printf(cmdList[i]);
printf(", ");
}printf("\r\n");
}
/***************************************************
S T R I N G T O K E N I Z E R
Searches the input buffer for valid commands
returns the id of the command if a match is found or 0 if no cmd
****************************************************/
void getcom(void){
unsigned char c;
int comp_i=0;
// Read from ring-buffer and fill in Compare buffer
while(BufCnt){ // while unread contents in ring-buffer
c = getcharb(); // fetch next byte
// uart_putchar(c);
if(CompIndex >= COMPBUFSIZE) CompIndex = 0;// overflow protection
// Analyze char
if(c == '#'){ // Manual start
CompIndex = 0;
uart_putchar(c);
}else if(c == '\r'){ // CR continue (end of cmd without argument)
Compare[CompIndex]='\0'; // fill in end character of comp string
uart_putchar(c);
break; // possible valid cmd received -> check out
}else if(c == '\n'){ // New line (ignore)
// Do nothing (ignore)
}else if(c == 8){ // Backspace
if(CompIndex) CompIndex--; // decrement index
uart_putchar(c);
}else if(c == 9){ // Horizontal TAB
help(); // Write out cmds
uart_putchar(c);
}else if(c == 27){ // ESC button
Command = 0; // Stop current command
uart_putchar(c);
Param = 0; // Clear argument
Plot = 0; // Stop plotting
Step = 0;
}else{
Compare[CompIndex++]=c; // Default action: Store character
uart_putchar(c);
}if(!BufCnt) return; // if no more data to read -> exit
}CompIndex=0; // reset, ready for next command
c = 1;
while(c<CMD_SIZE){ // For each command
if(strncmpf(Compare,cmdList[c],strlenf(cmdList[c])) == 0) break;
c++;
}
if(c<USB_CMD_SIZE){ // If match on normal commands
Command = c;
if(isdigit(Compare[strlenf(cmdList[c])])){
Param = atoi(&Compare[strlenf(cmdList[c])]);
c = strpos(Compare,':');
if(c > 0){
Param2 = atof(&Compare[c+1]);
c = strrpos(Compare,':');
if(c > strpos(Compare,':')) Param3 = atol(&Compare[c+1]);
else Param3 = 0;
}else{
Param2 = 0;
Param3 = 0;
}
}else{
Param = 0;
Param2 = 0;
Param3 = 0;
}
printf("@%s\r\n",&Compare); //Ack command
}else{
if(c>CMD_SIZE-1){ // If match on normal commands
// printf("&E;1;\r\n"); // Command not found
// printf("->Unknown command: '%s'\r\n",&Compare); // If no match
Command = 0;
Param = 0;
Param2 = 0;
Param3 = 0;
}
}
}