Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
90 gunterl 1
/*****************************************************************************
2
Project : Roboboard
3
Date    : 1/14/2007
4
Author  : Gunter Logemann (C) ALL RIGHTS RESERVED                  
5
 
6
Comments: This project is optimized to work with the Mikrocopter (www.mikrokopter.de)
7
 
8
Redistributions of this source code (with or without modifications) or parts
9
of this sourcode must retain the above copyright notice, this list of
10
conditions and the following disclaimer.
11
* Neither the name of the copyright holders nor the names of contributors may
12
  be used to endorse or promote products derived from this software without
13
  specific prior written permission.
14
* The use of this source code permittet for non-commercial use (directly
15
  or indirectly) only.
16
* Commercial use Is only permitted with our written permission by
17
  Gunter Logemann (gunter@pccon.de)
18
 
19
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
POSSIBILITY OF SUCH DAMAGE.
30
 
31
Chip type           : ATmega8
32
Program type        : Application
33
Clock frequency     : CPUSPEED
34
*****************************************************************************/
35
 
36
#include <avr/interrupt.h>
37
#include <avr/sleep.h>
38
//#include <avr/signal.h>
39
#include <avr/io.h>
40
//#include <avr/stdio.h>
41
#include "StdDefines.h"
42
#include "Main.h"
43
 
44
//#define CPUSPEED_20
45
//#define CPUSPEED_16
46
#define CPUSPEED_11059
47
 
48
// Macros
49
#define WatchdogReset() asm("wdr")      
50
#define Wait() while(!(SPSR & (1<<SPIF)))
51
 
52
 
53
// Define CPU speed dependant constants:
54
 
55
 
56
 
57
#define MAX_RX_BUF 100 
58
#define MAX_TX_BUF 100
59
unsigned volatile char SioTmp = 0;
60
unsigned volatile char RxdBuffer[MAX_RX_BUF];
61
unsigned volatile char TxdBuffer[MAX_TX_BUF];
62
 
63
unsigned volatile char NeuerDatensatzEmpfangen = 0;
64
unsigned volatile char UebertragungAbgeschlossen = 1;
65
unsigned volatile char CntCrcError = 0;
66
unsigned volatile char AnzahlEmpfangsBytes = 0;
67
 
68
struct str_VersionInfo VersionInfo;
69
struct str_AnalogData AnalogData;
70
struct str_Exception Exception;
71
 
72
// --------------------------------------------------------------------------
73
int uart_putchar (char c)
74
{
75
        UDR = c;
76
        return (0);
77
}
78
 
79
// --------------------------------------------------------------------------
80
SIGNAL(SIG_UART_TRANS)
81
{
82
 static unsigned int ptr = 0;
83
 unsigned char tmp_tx;
84
 if(!UebertragungAbgeschlossen)  
85
  {
86
   ptr++;                    // die [0] wurde schon gesendet
87
   tmp_tx = TxdBuffer[ptr];  
88
   if((tmp_tx == '\r') || (ptr == MAX_TX_BUF))
89
    {
90
     ptr = 0;
91
     UebertragungAbgeschlossen = 1;
92
    }
93
   UDR = tmp_tx;
94
  }
95
  else ptr = 0;
96
}
97
 
98
// --------------------------------------------------------------------------
99
SIGNAL(SIG_UART_RECV)
100
{
101
 static unsigned int crc;
102
 static unsigned char crc1,crc2,buf_ptr;
103
 static unsigned char UartState = 0;
104
 unsigned char CrcOkay = 0;
105
 
106
 SioTmp = UDR;
107
 
108
        if(buf_ptr >= MAX_RX_BUF)    
109
                UartState = 0;
110
        if(SioTmp == '\r' && UartState == 2)
111
        {
112
                UartState = 0;
113
                crc -= RxdBuffer[buf_ptr-2];
114
                crc -= RxdBuffer[buf_ptr-1];
115
                crc %= 4096;
116
                crc1 = '=' + crc / 64;
117
                crc2 = '=' + crc % 64;
118
                CrcOkay = 0;
119
                if((crc1 == RxdBuffer[buf_ptr-2]) && (crc2 == RxdBuffer[buf_ptr-1]))
120
                {
121
                        CrcOkay = 1;
122
                }
123
                else
124
                {
125
                        CrcOkay = 0;
126
                        CntCrcError++;
127
                }
128
                if(!NeuerDatensatzEmpfangen && CrcOkay) // Datensatz schon verarbeitet
129
                {
130
                        NeuerDatensatzEmpfangen = 1;
131
                        AnzahlEmpfangsBytes = buf_ptr;
132
                        RxdBuffer[buf_ptr] = '\r';
133
                }                                
134
        }
135
        else
136
        switch(UartState)
137
        {
138
                case 0:
139
          if(SioTmp == '#' && !NeuerDatensatzEmpfangen) UartState = 1;  // Startzeichen und Daten schon verarbeitet
140
                  buf_ptr = 0;
141
                  RxdBuffer[buf_ptr++] = SioTmp;
142
                  crc = SioTmp;
143
 
144
          break;
145
                case 1: // Adresse auswerten
146
                  UartState++;
147
                  RxdBuffer[buf_ptr++] = SioTmp;
148
                  crc += SioTmp;
149
 
150
          break;
151
                case 2: //  Eingangsdaten sammeln
152
                  RxdBuffer[buf_ptr] = SioTmp;
153
                  if(buf_ptr < MAX_RX_BUF) buf_ptr++;
154
                  else UartState = 0;
155
                  crc += SioTmp;
156
 
157
                  break;
158
                default:
159
          UartState = 0;
160
          break;
161
        }      
162
}
163
 
164
// --------------------------------------------------------------------------
165
void AddCRC(unsigned int wieviele)
166
{
167
 unsigned int tmpCRC = 0,i;
168
 for(i = 0; i < wieviele;i++)
169
  {
170
   tmpCRC += TxdBuffer[i];
171
  }
172
   tmpCRC %= 4096;
173
   TxdBuffer[i++] = '=' + tmpCRC / 64;
174
   TxdBuffer[i++] = '=' + tmpCRC % 64;
175
   TxdBuffer[i++] = '\r';
176
  UebertragungAbgeschlossen = 0;
177
  UDR = TxdBuffer[0];
178
}
179
 
180
 
181
// --------------------------------------------------------------------------
182
void SendOutData(unsigned char cmd,unsigned char modul, unsigned char *snd, unsigned char len)
183
{
184
 unsigned int pt = 0;
185
 unsigned char a,b,c;
186
 unsigned char ptr = 0;
187
 
188
 TxdBuffer[pt++] = '#';               // Startzeichen
189
 TxdBuffer[pt++] = modul;             // Adresse (a=0; b=1,...)
190
 TxdBuffer[pt++] = cmd;                // Commando
191
 
192
 while(len)
193
  {
194
   if(len) { a = snd[ptr++]; len--;} else a = 0;
195
   if(len) { b = snd[ptr++]; len--;} else b = 0;
196
   if(len) { c = snd[ptr++]; len--;} else c = 0;
197
   TxdBuffer[pt++] = '=' + (a >> 2);
198
   TxdBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
199
   TxdBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
200
   TxdBuffer[pt++] = '=' + ( c & 0x3f);
201
  }
202
 AddCRC(pt);
203
}
204
 
205
 
206
//-----------------------------------------------------------------------------
207
//main 
208
//main execution loop
209
//-----------------------------------------------------------------------------
210
int main(void)
211
{
212
 
213
        // int message structures;
214
        VersionInfo.identifier          = XIDENTIFIER_VERSION;
215
        VersionInfo.majorversion        = MAJORVERSION;
216
        VersionInfo.minorversion        = MINORVERSION;
217
 
218
        AnalogData.identifier           = XIDENTIFIER_ANALOG;
219
 
220
        Exception.identifier            = XIDENTIFIER_EXCEPTION;
221
 
222
        // PORT D - unused right now    
223
        PORTD = 0x10;                                                  
224
        DDRD  = 0x00;  
225
 
226
        //Enable TXEN im Register UCR TX-Data Enable & RX Enable
227
 
228
        // USART initialization
229
        // Communication Parameters: 8 Data, 1 Stop, No Parity
230
        // USART Receiver: On
231
        // USART Transmitter: On
232
        // USART RX/TX interrupt enable
233
        // USART Mode: Asynchronous
234
        // USART Baud rate: 57600       
235
        UCSRA=0x00;
236
        UCSRB=0xD8;
237
        UCSRC=0x86;
238
#ifdef CPUSPEED_20      //20.000MHz
239
        UBRRH=0x00;
240
        UBRRL=0x15;    
241
#endif
242
 
243
#ifdef CPUSPEED_16      //16.000MHz
244
        UBRRH=0x00;
245
        UBRRL=0x10;
246
#endif
247
 
248
#ifdef CPUSPEED_11059 //11.059MHz
249
        UBRRH=0x00;
250
        UBRRL=0x0B;
251
#endif
252
 
253
 
254
        // Enable interrupts
255
        sei();
256
 
257
        NeuerDatensatzEmpfangen = 0;
258
 
259
        // main loop    
260
        while (1)
261
        {
262
                if(NeuerDatensatzEmpfangen==1) {
263
                        switch(RxdBuffer[3])
264
                        {
265
                                // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
266
                                // version request
267
                                case XIDENTIFIER_VERSION:
268
                                        SendOutData('X',0x00,(unsigned char *) &VersionInfo,sizeof(VersionInfo));
269
                                        break;
270
                                // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
271
                                case XIDENTIFIER_ANALOG:
272
                                        break;
273
                                // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
274
                                default:
275
                                        Exception.errorcode = ERRORCODE_NOTIMPLEMENTED;
276
                                        SendOutData('X',0x00,(unsigned char *) &Exception,sizeof(VersionInfo));
277
                        }
278
                        NeuerDatensatzEmpfangen=0;
279
                }
280
        }
281
 
282
        return(1);
283
}       /* main */
284
 
285