Some bits of Spy-Bi-Wire support, thanks to Mark Rages. (Not yet complete.)
[goodfet] / firmware / lib / command.c
1 /*! \file command.c
2   \author Travis Goodspeed
3   \brief These functions manage command interpretation.
4 */
5
6 #include "command.h"
7 #include "platform.h"
8 #include <string.h>
9
10 unsigned char cmddata[CMDDATALEN];
11 unsigned char silent=0;
12
13 //! Transmit a string.
14 void txstring(unsigned char app,
15               unsigned char verb,
16               const char *str){
17   unsigned long len=strlen(str);
18   txhead(app,verb,len);
19   while(len--)
20     serial_tx(*(str++));
21 }
22
23 /*! \brief Transmit a debug string.
24   
25   Transmits a debugging string that is to be printed
26   out of line by the client.  This is just for record-keeping;
27   it is not considered a proper reply to a query.
28  */
29 void debugstr(const char *str){
30   txstring(0xFF,0xFF,str);
31 }
32
33 //! brief Debug a hex word string.
34 void debughex(u16 v) {
35   unsigned char a[7];
36   a[0]='0'; a[1]='x';
37     
38   a[2]=0xf&(v>>12);
39   a[2]+=(a[2]>9)?('a'-10):'0';
40
41   a[3]=0xf&(v>>8);
42   a[3]+=(a[3]>9)?('a'-10):'0';
43
44   a[4]=0xf&(v>>4);
45   a[4]+=(a[4]>9)?('a'-10):'0';
46
47   a[5]=0xf&(v>>0);
48   a[5]+=(a[5]>9)?('a'-10):'0';
49
50   a[6]=0;
51
52   txstring(0xFF,0xFF,a);
53 }
54
55 /*! \brief Transmit debug bytes.
56   
57   Transmits bytes for debugging.
58 */
59 void debugbytes(const char *bytes, unsigned int len){
60   u16 i;
61   txhead(0xFF,0xFE,len);
62   for(i=0;i<len;i++)
63     serial_tx(bytes[i]);
64 }
65
66
67 //! Transmit a header.
68 void txhead(unsigned char app,
69             unsigned char verb,
70             unsigned long len){
71   serial_tx(app);
72   serial_tx(verb);
73   //serial_tx(len); //old protocol
74   txword(len);
75 }
76
77 //! Transmit data.
78 void txdata(unsigned char app,
79             unsigned char verb,
80             unsigned long len){
81   unsigned int i=0;
82   if(silent)
83     return;
84   txhead(app,verb,len);
85   for(i=0;i<len;i++){
86     serial_tx(cmddata[i]);
87   }
88 }
89
90 //! Receive a long.
91 unsigned long rxlong(){
92   unsigned long toret=0;
93   toret=serial_rx();
94   toret|=(((long)serial_rx())<<8);
95   toret|=(((long)serial_rx())<<16);
96   toret|=(((long)serial_rx())<<24);
97   return toret;
98 }
99 //! Receive a word.
100 unsigned int rxword(){
101   unsigned long toret=0;
102   toret=serial_rx();
103   toret|=(((long)serial_rx())<<8);
104   return toret;
105 }
106 //! Transmit a long.
107 void txlong(unsigned long l){
108   serial_tx(l&0xFF);
109   l>>=8;
110   serial_tx(l&0xFF);
111   l>>=8;
112   serial_tx(l&0xFF);
113   l>>=8;
114   serial_tx(l&0xFF);
115   l>>=8;
116 }
117 //! Transmit a word.
118 void txword(unsigned int l){
119   serial_tx(l&0xFF);
120   l>>=8;
121   serial_tx(l&0xFF);
122   l>>=8;
123 }
124
125 //Be very careful changing delay().
126 //It was chosen poorly by trial and error.
127
128 //! Delay for a count.
129 void delay(unsigned int count){
130   volatile unsigned int i=count;
131   while(i--) asm("nop");
132 }
133 //! MSDelay
134 void msdelay(unsigned int ms){
135   volatile unsigned int i,j;
136   i=100;
137   while(i--){
138     j=ms;
139     while(j--) asm("nop");
140   }
141   //Using TimerA might be cleaner.
142 }