1 /************************************
  2 *       Wireless to CAN Conv.       *
  3 *                                   *
  4 *       Made by, YuriHan            *
  5 *       Extronics Co,. LTD          *
  6 *                                   *
  7 *       Since 2007.10,31            *
  8 ************************************/
  9
 10
 11 #include<avr/io.h>
 12 #include<avr/interrupt.h>
 13 #include<stdio.h>
 14 #include<string.h>
 15 #include"global.h"
 16 #include"spi.h"
 17 #include"uart.h"
 18 #include"initialize.h"
 19
 20 #define F_CPU   16000000UL                // 16MHz Xtal.
 21
 22 void dbginputkey(char* data);
 23 void dbgmenu(void);
 24
 25 volatile u16 count = 0;
 26
 27 int main(void)
 28 {
 29
 30     // Initialize Routine              /*{{{*/
 31     
 32     cli();                              // Disable All Interrupt
 33     MCU_Init();                         // PORT I/O Initialize
 34     
 35     DDRG=0xff;
 36
 37     Timer0_Init();                      // 10ms
 38     spiInit();                          // Master Mode
 39 //  RFModule_Init();                    // RF Module Initialize
 40     Uart0_init(38400);
 41     Uart1_init(500000);
 42     sei();                              // Set Enable All Interrupt      /*}}}*/
 43     fdevopen(uart0SendByte, NULL);     // UART를 strem 으로.. printf 사용 가능.
 44     dbgmenu();
 45
 46     while(1)                           // 테스트용 루틴
 47     {
 48         if(!count) count=50;
 49         while(count);
 50         PORTG=0x01;
 51         if(!count) count=50;
 52         while(count);
 53         PORTG=0x02;
 54     }
 55
 56     return 0;
 57 }
 58
 59
 60 ISR(USART0_RX_vect)
 61 {
 62     char data = UDR0 ;
 63     static char rx_buf[16]={0,};
 64     static u08 cnt=0;
 65     uart0SendByte(data);                // echo
 66
 67     if(data==\'\\r\')                     // 키 입력 완료
 68     {
 69         rx_buf[cnt]=\'\\0\';             // Null
 70         dbginputkey(rx_buf);
 71         cnt=0;
 72     }
 73     else rx_buf[cnt++]=data;
 74     if(cnt==16){printf("\\nERROR!! Command is Limit Length by 16\\n\\nCmd> "); cnt=0;}    // Limiter
 75     
 76 }
 77
 78 ISR(USART1_RX_vect)                     // CC2500 UART Interface
 79 {
 80     char data = UDR1 ;
 81 }
 82
 83 ISR(TIMER0_OVF_vect)
 84 {
 85     TCNT0=0x64;            // 10ms @16MHz
 86     if(count)count--;  
 87 }
 88
 89 void dbgmenu(void)
 90 {
 91     printf( "\\n\\n"
 92             "-----------------------------------------------\\n"
 93             "2.4 GHz CC2500 Chipset CAN Comm. Debug program.\\n"
 94             "Since 2007. 11. ~           Program Version 1.0\\n"
 95             "Made by, YuriHan. Extronics CO., LTD\\n"
 96             "-----------------------------------------------\\n"
 97             "    \\nCommand List : \\n\\n"
 98             "    help    -   Command List View\\n"
 99             "    reset   -   Reset The CC2500 Module\\n"
100             "    send    -   Instruction send to CC2500\\n"
101             "    recv    -   Receive data to CC2500\\n"
102             "\\n\\nCmd> ");
103     return;
104 }
105
106 /*
107 // Enum으로 바꿀것.
108 #define HELP        1
109 #define SEND        2
110 #define RECV        3
111 #define RESET       4
112 #define NONE        -1
113 #define INPUTMODE   9
114 */
115 void dbginputkey(char* data)
116 {
117     static enum mode{idle,undef,help,send,recv,reset,input}mode;
118     u16 buf=0;
119
120     if(mode==input)                                        // send 입력.
121     {
122         sscanf(data,"%x",&buf);
123         if(buf>0 || buf<255)                          // 입력 검증
124         {
125             spiTransferByte(buf);
126             printf("\\nSend ok.\\n\\nCmd> ");
127         }
128         else
129             printf("ERROR!!\\n\\nCmd> ");
130         mode=idle;
131         return;
132     }
133         
134         
135     if(*data==0){ printf("\\nCmd> "); return; }         // 키 입력없었을 경우.
136
137     if(mode==idle)
138     {
139              if(!strcmp(data,"help")) mode=help;
140         else if(!strcmp(data,"send")) mode=send;
141         else if(!strcmp(data,"recv")) mode=recv;
142         else if(!strcmp(data,"reset"))mode=reset;
143         else mode=undef;
144         switch(mode)
145         {
146             case help :
147                 dbgmenu();
148                 break;
149             case send :
150                 printf("\\nmessage > 0x");
151                 mode=input;
152                 return;
153                 break;
154             case recv :
155                 buf=spiTransferByte(buf);
156                 printf("\\nReceive data is 0x%2x\\n\\nCmd> ",buf);
157                 break;
158             default :
159                 printf("\\nCommand error\\nCmd> ");
160         }
161     }
162     mode=idle;
163 }
164