Rev 18 | Rev 21 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 18 | Rev 19 | ||
---|---|---|---|
Line 52... | Line 52... | ||
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
57 | - | ||
- | 57 | #include <avr/io.h> |
|
- | 58 | #include <avr/interrupt.h> |
|
- | 59 | #include <avr/wdt.h> |
|
58 | #include "main.h" |
60 | #include "main.h" |
59 | #include "uart.h" |
61 | #include "uart.h" |
- | 62 | #include "timer0.h" |
|
- | 63 | #include "twislave.h" |
|
- | 64 | ||
- | 65 | ||
- | 66 | #define FALSE 0 |
|
- | 67 | #define TRUE 1 |
|
- | 68 | ||
- | 69 | #define TXD_BUFFER_LEN 100 |
|
- | 70 | #define RXD_BUFFER_LEN 100 |
|
Line -... | Line 71... | ||
- | 71 | ||
- | 72 | volatile uint8_t txd_buffer[TXD_BUFFER_LEN]; |
|
- | 73 | volatile uint8_t rxd_buffer_locked = FALSE; |
|
60 | 74 | volatile uint8_t rxd_buffer[RXD_BUFFER_LEN]; |
|
61 | #define MAX_SENDE_BUFF 100 |
75 | volatile uint8_t txd_complete = TRUE; |
Line 62... | Line -... | ||
62 | #define MAX_EMPFANGS_BUFF 100 |
- | |
63 | 76 | volatile uint8_t ReceivedBytes = 0; |
|
64 | unsigned volatile char SIO_Sollwert = 0; |
- | |
65 | unsigned volatile char SioTmp = 0; |
- | |
66 | unsigned volatile char SendeBuffer[MAX_SENDE_BUFF]; |
- | |
67 | unsigned volatile char RxdBuffer[MAX_EMPFANGS_BUFF]; |
- | |
68 | unsigned volatile char NeuerDatensatzEmpfangen = 0; |
- | |
69 | unsigned volatile char UebertragungAbgeschlossen = 1; |
- | |
70 | unsigned char GetVersionAnforderung = 0,DebugTextAnforderung = 0,DebugGetAnforderung = 0, KompassAntwort = 0; |
- | |
71 | unsigned char MeineSlaveAdresse; |
- | |
72 | unsigned char MotorTest[4] = {0,0,0,0}; |
- | |
73 | unsigned volatile char AnzahlEmpfangsBytes = 0; |
- | |
74 | unsigned char PcZugriff; |
77 | |
75 | 78 | uint8_t RequestVerInfo = FALSE; |
|
76 | struct str_DebugOut DebugOut; |
79 | uint8_t RequestDebugData = FALSE; |
77 | struct str_ExternData ExternData; |
- | |
Line -... | Line 80... | ||
- | 80 | uint8_t RequestDebugLabel = FALSE; |
|
Line -... | Line 81... | ||
- | 81 | uint8_t RequestCompassHeading = FALSE; |
|
Line 78... | Line 82... | ||
78 | struct str_ExternControl ExternControl; |
82 | |
- | 83 | uint8_t MySlaveAddr = 0; |
|
- | 84 | ||
- | 85 | //unsigned char,DebugGetAnforderung = 0, |
|
Line -... | Line 86... | ||
- | 86 | ||
- | 87 | DebugOut_t DebugOut; |
|
79 | struct str_VersionInfo VersionInfo; |
88 | ExternData_t ExternData; |
80 | 89 | ExternControl_t ExternControl; |
|
81 | 90 | VersionInfo_t VersionInfo; |
|
82 | 91 | ||
83 | int Debug_Timer; |
92 | uint16_t Debug_Timer; |
84 | 93 | ||
85 | const unsigned char ANALOG_TEXT[32][16] = |
94 | const uint8_t ANALOG_LABEL[32][16] = |
86 | { |
95 | { |
87 | //1234567890123456 |
96 | //1234567890123456 |
88 | "Magnet N ", //0 |
97 | "Magnet X ", //0 |
89 | "Magnet R ", |
98 | "Magnet Y ", |
90 | "Magnet Z ", |
99 | "Magnet Z ", |
91 | "Raw N ", |
100 | "Raw X ", |
92 | "Raw R ", |
101 | "Raw Y ", |
93 | "Raw Z ", //5 |
102 | "Raw Z ", //5 |
94 | "Lage N ", |
103 | "Attitude Nick ", |
95 | "Lage R ", |
104 | "Attitude Roll ", |
96 | "Xmin ", |
105 | "XOffset ", |
97 | "Xmax ", |
106 | "XRange ", |
98 | "Ymin ", //10 |
107 | "YOffset ", //10 |
99 | "Ymax ", |
108 | "YRange ", |
100 | "Zmin ", |
109 | "ZOffset ", |
101 | "ZMax ", |
110 | "ZRange ", |
102 | "Calstate ", |
111 | "Calstate ", |
Line 118... | Line 127... | ||
118 | "Analog30 ", //30 |
127 | "Analog30 ", //30 |
119 | "Analog31 " |
128 | "Analog31 " |
120 | }; |
129 | }; |
Line -... | Line 130... | ||
- | 130 | ||
- | 131 | ||
- | 132 | /****************************************************************/ |
|
- | 133 | /* Initialization of the USART0 */ |
|
- | 134 | /****************************************************************/ |
|
- | 135 | void USART0_Init (void) |
|
- | 136 | { |
|
- | 137 | uint8_t sreg = SREG; |
|
- | 138 | ||
- | 139 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * BAUD_RATE) - 1); |
|
- | 140 | ||
- | 141 | // disable all interrupts before configuration |
|
- | 142 | cli(); |
|
- | 143 | ||
- | 144 | // disable RX-Interrupt |
|
- | 145 | UCSR0B &= ~(1 << RXCIE0); |
|
- | 146 | // disable TX-Interrupt |
|
- | 147 | UCSR0B &= ~(1 << TXCIE0); |
|
- | 148 | ||
- | 149 | // set direction of RXD0 and TXD0 pins |
|
- | 150 | ||
- | 151 | // set RXD0 (PD0) as an input pin |
|
- | 152 | PORTD |= (1 << PORTD0); |
|
- | 153 | DDRD &= ~(1 << DDD0); |
|
- | 154 | // set TXD0 (PD1) as an output pin |
|
- | 155 | PORTD |= (1 << PORTD1); |
|
- | 156 | DDRD |= (1 << DDD1); |
|
- | 157 | ||
- | 158 | // USART0 Baud Rate Register |
|
- | 159 | // set clock divider |
|
- | 160 | UBRR0H = (uint8_t)(ubrr >> 8); |
|
- | 161 | UBRR0L = (uint8_t)ubrr; |
|
- | 162 | ||
- | 163 | // USART0 Control and Status Register A, B, C |
|
- | 164 | ||
- | 165 | // enable double speed operation in |
|
- | 166 | UCSR0A |= (1 << U2X0); |
|
- | 167 | // enable receiver and transmitter in |
|
- | 168 | UCSR0B |= (1 << TXEN0) | (1 << RXEN0); |
|
- | 169 | // set asynchronous mode |
|
- | 170 | UCSR0C &= ~(1 << UMSEL01); |
|
- | 171 | UCSR0C &= ~(1 << UMSEL00); |
|
- | 172 | // no parity |
|
- | 173 | UCSR0C &= ~(1 << UPM01); |
|
- | 174 | UCSR0C &= ~(1 << UPM00); |
|
- | 175 | // 1 stop bit |
|
- | 176 | UCSR0C &= ~(1 << USBS0); |
|
- | 177 | // 8-bit |
|
- | 178 | UCSR0B &= ~(1 << UCSZ02); |
|
- | 179 | UCSR0C |= (1 << UCSZ01); |
|
- | 180 | UCSR0C |= (1 << UCSZ00); |
|
- | 181 | ||
- | 182 | // flush receive buffer |
|
- | 183 | while ( UCSR0A & (1<<RXC0) ) UDR0; |
|
- | 184 | ||
- | 185 | // enable interrupts at the end |
|
- | 186 | // enable RX-Interrupt |
|
- | 187 | UCSR0B |= (1 << RXCIE0); |
|
Line 121... | Line -... | ||
121 | - | ||
122 | 188 | // enable TX-Interrupt |
|
123 | - | ||
124 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
189 | UCSR0B |= (1 << TXCIE0); |
125 | //++ Sende-Part der Datenübertragung |
190 | |
126 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
191 | rxd_buffer_locked = FALSE; |
- | 192 | txd_complete = TRUE; |
|
- | 193 | ||
127 | SIGNAL(INT_VEC_TX) |
194 | Debug_Timer = SetDelay(200); |
128 | { |
195 | |
129 | static unsigned int ptr = 0; |
196 | |
130 | unsigned char tmp_tx; |
197 | // restore global interrupt flags |
131 | if(!UebertragungAbgeschlossen) |
198 | SREG = sreg; |
132 | { |
199 | |
133 | ptr++; // die [0] wurde schon gesendet |
200 | VersionInfo.Major = VERSION_MAJOR; |
134 | tmp_tx = SendeBuffer[ptr]; |
201 | VersionInfo.Minor = VERSION_MINOR; |
- | 202 | VersionInfo.PCCompatible = VERSION_COMPATIBLE; |
|
- | 203 | ||
135 | if((tmp_tx == '\r') || (ptr == MAX_SENDE_BUFF)) |
204 | // Version beim Start ausgeben (nicht schön, aber geht... ) |
136 | { |
205 | uart_putchar ('\n');uart_putchar ('C');uart_putchar ('P');uart_putchar (':'); |
- | 206 | uart_putchar ('V');uart_putchar (0x30 + VERSION_MAJOR);uart_putchar ('.');uart_putchar (0x30 + VERSION_MINOR/10); uart_putchar (0x30 + VERSION_MINOR%10); |
|
- | 207 | uart_putchar ('\n'); |
|
137 | ptr = 0; |
208 | } |
- | 209 | ||
- | 210 | // --------------------------------------------------------------------------------- |
|
- | 211 | void USART0_EnableTXD(void) |
|
138 | UebertragungAbgeschlossen = 1; |
212 | { |
- | 213 | DDRD |= (1<<DDD1); // set TXD pin as output |
|
- | 214 | UCSR0B |= (1 << TXEN0); // enable TX in USART |
|
139 | } |
215 | } |
- | 216 | ||
- | 217 | // --------------------------------------------------------------------------------- |
|
- | 218 | void USART0_DisableTXD(void) |
|
140 | UDR = tmp_tx; |
219 | { |
Line -... | Line 220... | ||
- | 220 | UCSR0B &= ~(1 << TXEN0); // disable TXD in USART |
|
- | 221 | DDRD &= ~(1<<DDD1); // set TXD pin as input |
|
- | 222 | } |
|
141 | } |
223 | |
142 | else ptr = 0; |
224 | /****************************************************************/ |
143 | } |
225 | /* USART0 transmitter ISR */ |
144 | 226 | /****************************************************************/ |
|
145 | void SendUart(void) |
- | |
146 | { |
227 | ISR(USART_TX_vect) |
147 | static unsigned int ptr = 0; |
228 | { |
148 | unsigned char tmp_tx; |
229 | static uint16_t ptr_txd_buffer = 0; |
149 | if(!(USR & 0x40)) return; |
230 | uint8_t tmp_tx; |
- | 231 | if(!txd_complete) // transmission not completed |
|
150 | if(!UebertragungAbgeschlossen) |
232 | { |
151 | { |
233 | ptr_txd_buffer++; // die [0] wurde schon gesendet |
152 | ptr++; // die [0] wurde schon gesendet |
234 | tmp_tx = txd_buffer[ptr_txd_buffer]; |
153 | tmp_tx = SendeBuffer[ptr]; |
235 | // if terminating character or end of txd buffer was reached |
154 | if((tmp_tx == '\r') || (ptr == MAX_SENDE_BUFF)) |
236 | if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN)) |
155 | { |
- | |
156 | ptr = 0; |
237 | { |
157 | UebertragungAbgeschlossen = 1; |
238 | ptr_txd_buffer = 0; // reset txd pointer |
- | 239 | txd_complete = 1; // stop transmission |
|
158 | } |
240 | } |
159 | USR |= (1<TXC0); |
241 | UDR0 = tmp_tx; // send current byte will trigger this ISR again |
Line 160... | Line 242... | ||
160 | UDR = tmp_tx; |
242 | } |
161 | } |
243 | // transmission completed |
162 | else ptr = 0; |
- | |
163 | } |
- | |
164 | 244 | else ptr_txd_buffer = 0; |
|
165 | // -------------------------------------------------------------------------- |
- | |
166 | void Decode64(unsigned char *ptrOut, unsigned char len, unsigned char ptrIn,unsigned char max) // Wohin mit den Daten; Wie lang; Wo im RxdBuffer |
245 | } |
167 | { |
246 | |
168 | unsigned char a,b,c,d; |
247 | /****************************************************************/ |
169 | unsigned char ptr = 0; |
248 | /* USART0 receiver ISR */ |
170 | unsigned char x,y,z; |
249 | /****************************************************************/ |
171 | while(len) |
250 | ISR(USART_RX_vect) |
172 | { |
- | |
Line 173... | Line -... | ||
173 | a = RxdBuffer[ptrIn++] - '='; |
- | |
174 | b = RxdBuffer[ptrIn++] - '='; |
251 | { |
175 | c = RxdBuffer[ptrIn++] - '='; |
- | |
Line 176... | Line -... | ||
176 | d = RxdBuffer[ptrIn++] - '='; |
- | |
177 | if(ptrIn > max - 2) break; // nicht mehr Daten verarbeiten, als empfangen wurden |
252 | static uint16_t crc; |
178 | - | ||
179 | x = (a << 2) | (b >> 4); |
- | |
Line -... | Line 253... | ||
- | 253 | static uint8_t ptr_rxd_buffer = 0; |
|
- | 254 | uint8_t crc1, crc2; |
|
- | 255 | uint8_t c; |
|
- | 256 | ||
- | 257 | c = UDR0; // catch the received byte |
|
180 | y = ((b & 0x0f) << 4) | (c >> 2); |
258 | |
181 | z = ((c & 0x03) << 6) | d; |
259 | if(rxd_buffer_locked) return; // if rxd buffer is locked immediately return |
- | 260 | ||
182 | 261 | // the rxd buffer is unlocked |
|
183 | if(len--) ptrOut[ptr++] = x; else break; |
262 | if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received |
184 | if(len--) ptrOut[ptr++] = y; else break; |
263 | { |
- | 264 | rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer |
|
- | 265 | crc = c; // init crc |
|
185 | if(len--) ptrOut[ptr++] = z; else break; |
266 | } |
186 | } |
- | |
187 | 267 | #if 0 |
|
188 | } |
268 | else if (ptr_rxd_buffer == 1) // handle address |
- | 269 | { |
|
189 | 270 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
|
190 | 271 | crc += c; // update crc |
|
191 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
192 | //++ Empfangs-Part der Datenübertragung |
272 | } |
193 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
194 | SIGNAL(INT_VEC_RX) |
- | |
195 | { |
273 | #endif |
196 | static unsigned int crc; |
274 | else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes |
- | 275 | { |
|
197 | static unsigned char crc1,crc2,buf_ptr; |
276 | if(c != '\r') // no termination character |
198 | static unsigned char UartState = 0; |
277 | { |
199 | unsigned char CrcOkay = 0; |
278 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
- | 279 | crc += c; // update crc |
|
200 | 280 | } |
|
201 | SioTmp = UDR; |
281 | else // termination character was received |
202 | if(buf_ptr >= MAX_EMPFANGS_BUFF) UartState = 0; |
282 | { |
203 | if(SioTmp == '\r' && UartState == 2) |
283 | // the last 2 bytes are no subject for checksum calculation |
204 | { |
284 | // they are the checksum itself |
205 | UartState = 0; |
285 | crc -= rxd_buffer[ptr_rxd_buffer-2]; |
206 | crc -= RxdBuffer[buf_ptr-2]; |
- | |
207 | crc -= RxdBuffer[buf_ptr-1]; |
286 | crc -= rxd_buffer[ptr_rxd_buffer-1]; |
208 | crc %= 4096; |
287 | // calculate checksum from transmitted data |
209 | crc1 = '=' + crc / 64; |
288 | crc %= 4096; |
- | 289 | crc1 = '=' + crc / 64; |
|
210 | crc2 = '=' + crc % 64; |
290 | crc2 = '=' + crc % 64; |
211 | CrcOkay = 0; |
- | |
212 | if((crc1 == RxdBuffer[buf_ptr-2]) && (crc2 == RxdBuffer[buf_ptr-1])) CrcOkay = 1; else { CrcOkay = 0; }; |
- | |
213 | if(CrcOkay) // Datensatz schon verarbeitet |
291 | // compare checksum to transmitted checksum bytes |
214 | { |
292 | if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1])) |
215 | NeuerDatensatzEmpfangen = 1; |
293 | { // checksum valid |
- | 294 | rxd_buffer_locked = TRUE; // lock the rxd buffer |
|
- | 295 | ReceivedBytes = ptr_rxd_buffer; // store number of received bytes |
|
- | 296 | rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character |
|
- | 297 | // if 2nd byte is an 'R' enable watchdog that will result in an reset |
|
- | 298 | if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando |
|
- | 299 | } |
|
216 | AnzahlEmpfangsBytes = buf_ptr; |
300 | else |
217 | RxdBuffer[buf_ptr] = '\r'; |
- | |
218 | if((RxdBuffer[2] == 'R')) wdt_enable(WDTO_250MS); // Reset-Commando |
- | |
219 | // uart_putchar(RxdBuffer[2]); |
- | |
220 | } |
301 | { // checksum invalid |
221 | } |
- | |
222 | else |
- | |
223 | switch(UartState) |
- | |
224 | { |
- | |
225 | case 0: |
- | |
226 | if(SioTmp == '#' && !NeuerDatensatzEmpfangen) UartState = 1; // Startzeichen und Daten schon verarbeitet |
- | |
227 | buf_ptr = 0; |
- | |
228 | RxdBuffer[buf_ptr++] = SioTmp; |
- | |
229 | crc = SioTmp; |
- | |
230 | break; |
302 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
231 | case 1: // Adresse auswerten |
- | |
232 | UartState++; |
- | |
233 | RxdBuffer[buf_ptr++] = SioTmp; |
303 | } |
234 | crc += SioTmp; |
- | |
235 | break; |
- | |
236 | case 2: // Eingangsdaten sammeln |
- | |
237 | RxdBuffer[buf_ptr] = SioTmp; |
304 | ptr_rxd_buffer = 0; // reset rxd buffer pointer |
238 | if(buf_ptr < MAX_EMPFANGS_BUFF) buf_ptr++; |
- | |
239 | else UartState = 0; |
- | |
Line 240... | Line 305... | ||
240 | crc += SioTmp; |
305 | } |
241 | break; |
306 | } |
242 | default: |
307 | else // rxd buffer overrun |
243 | UartState = 0; |
308 | { |
244 | break; |
309 | ptr_rxd_buffer = 0; // reset rxd buffer |
245 | } |
310 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
246 | 311 | } |
|
247 | }; |
312 | } |
248 | 313 | ||
249 | 314 | ||
250 | // -------------------------------------------------------------------------- |
315 | // -------------------------------------------------------------------------- |
251 | void AddCRC(unsigned int wieviele) |
316 | void AddCRC(uint16_t datalen) |
252 | { |
317 | { |
253 | unsigned int tmpCRC = 0,i; |
318 | uint16_t tmpCRC = 0, i; |
254 | for(i = 0; i < wieviele;i++) |
319 | for(i = 0; i < datalen; i++) |
Line -... | Line 320... | ||
- | 320 | { |
|
255 | { |
321 | tmpCRC += txd_buffer[i]; |
256 | tmpCRC += SendeBuffer[i]; |
322 | } |
257 | } |
323 | tmpCRC %= 4096; |
258 | tmpCRC %= 4096; |
324 | txd_buffer[i++] = '=' + tmpCRC / 64; |
259 | SendeBuffer[i++] = '=' + tmpCRC / 64; |
325 | txd_buffer[i++] = '=' + tmpCRC % 64; |
260 | SendeBuffer[i++] = '=' + tmpCRC % 64; |
326 | txd_buffer[i++] = '\r'; |
261 | SendeBuffer[i++] = '\r'; |
327 | txd_complete = FALSE; |
262 | UebertragungAbgeschlossen = 0; |
328 | UDR0 = txd_buffer[0]; // initiates the transmission (continued in the TXD ISR) |
263 | UDR = SendeBuffer[0]; |
329 | } |
264 | } |
330 | |
Line 265... | Line 331... | ||
265 | 331 | ||
266 | 332 | ||
267 | // -------------------------------------------------------------------------- |
333 | // -------------------------------------------------------------------------- |
268 | void SendOutData(unsigned char cmd,unsigned char modul, unsigned char *snd, unsigned char len) |
334 | void SendOutData(uint8_t cmd,uint8_t module, uint8_t *snd, uint8_t len) |
269 | { |
335 | { |
270 | unsigned int pt = 0; |
336 | uint16_t pt = 0; |
271 | unsigned char a,b,c; |
337 | uint8_t a,b,c; |
272 | unsigned char ptr = 0; |
338 | uint8_t ptr = 0; |
273 | 339 | ||
274 | SendeBuffer[pt++] = '#'; // Startzeichen |
340 | txd_buffer[pt++] = '#'; // Start character |
275 | SendeBuffer[pt++] = modul; // Adresse (a=0; b=1,...) |
341 | txd_buffer[pt++] = module; // Address (a=0; b=1,...) |
276 | SendeBuffer[pt++] = cmd; // Commando |
342 | txd_buffer[pt++] = cmd; // Command |
Line 277... | Line -... | ||
277 | - | ||
278 | while(len) |
- | |
279 | { |
- | |
280 | if(len) { a = snd[ptr++]; len--;} else a = 0; |
- | |
281 | if(len) { b = snd[ptr++]; len--;} else b = 0; |
- | |
282 | if(len) { c = snd[ptr++]; len--;} else c = 0; |
- | |
283 | SendeBuffer[pt++] = '=' + (a >> 2); |
- | |
284 | SendeBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
- | |
285 | SendeBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
- | |
286 | SendeBuffer[pt++] = '=' + ( c & 0x3f); |
- | |
287 | } |
- | |
288 | AddCRC(pt); |
- | |
289 | } |
- | |
290 | - | ||
291 | - | ||
292 | - | ||
293 | //############################################################################ |
- | |
294 | //Routine für die Serielle Ausgabe |
343 | |
295 | int uart_putchar (char c) |
344 | while(len) |
296 | //############################################################################ |
345 | { |
297 | { |
346 | if(len) { a = snd[ptr++]; len--;} else a = 0; |
298 | if(!(UCR & (1 << TXEN))) return (0); |
- | |
299 | if (c == '\n') |
- | |
300 | uart_putchar('\r'); |
347 | if(len) { b = snd[ptr++]; len--;} else b = 0; |
301 | //Warten solange bis Zeichen gesendet wurde |
348 | if(len) { c = snd[ptr++]; len--;} else c = 0; |
302 | loop_until_bit_is_set(USR, UDRE); |
- | |
- | 349 | txd_buffer[pt++] = '=' + (a >> 2); |
|
303 | //Ausgabe des Zeichens |
350 | txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
- | 351 | txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
|
304 | UDR = c; |
352 | txd_buffer[pt++] = '=' + ( c & 0x3f); |
- | 353 | } |
|
- | 354 | AddCRC(pt); // add checksum after data block and initates the transmission |
|
- | 355 | } |
|
Line 305... | Line 356... | ||
305 | 356 | ||
306 | return (0); |
357 | |
307 | } |
358 | // -------------------------------------------------------------------------- |
308 | - | ||
Line -... | Line 359... | ||
- | 359 | void Decode64(uint8_t *ptrOut, uint8_t len, uint8_t ptrIn, uint8_t max) |
|
- | 360 | { |
|
309 | // -------------------------------------------------------------------------- |
361 | uint8_t a,b,c,d; |
- | 362 | uint8_t ptr = 0; |
|
- | 363 | uint8_t x,y,z; |
|
Line 310... | Line -... | ||
310 | void WriteProgramData(unsigned int pos, unsigned char wert) |
- | |
311 | { |
- | |
Line -... | Line 364... | ||
- | 364 | while(len) |
|
312 | } |
365 | { |
- | 366 | a = rxd_buffer[ptrIn++] - '='; |
|
313 | 367 | b = rxd_buffer[ptrIn++] - '='; |
|
314 | //############################################################################ |
368 | c = rxd_buffer[ptrIn++] - '='; |
315 | //INstallation der Seriellen Schnittstelle |
369 | d = rxd_buffer[ptrIn++] - '='; |
316 | void UART_Init (void) |
370 | if(ptrIn > max - 2) break; |
317 | //############################################################################ |
371 | |
318 | { |
372 | x = (a << 2) | (b >> 4); |
319 | //Enable TXEN im Register UCR TX-Data Enable & RX Enable |
- | |
- | 373 | y = ((b & 0x0f) << 4) | (c >> 2); |
|
320 | 374 | z = ((c & 0x03) << 6) | d; |
|
321 | UCR=(1 << TXEN) | (1 << RXEN); |
375 | |
Line -... | Line 376... | ||
- | 376 | if(len--) ptrOut[ptr++] = x; else break; |
|
322 | // UART Double Speed (U2X) |
377 | if(len--) ptrOut[ptr++] = y; else break; |
323 | USR |= (1<<U2X); |
378 | if(len--) ptrOut[ptr++] = z; else break; |
324 | // RX-Interrupt Freigabe |
379 | } |
325 | 380 | } |
|
326 | UCSRB |= (1<<RXCIE); // serieller Empfangsinterrupt |
- | |
327 | - | ||
- | 381 | ||
328 | // TX-Interrupt Freigabe |
382 | |
329 | UCSRB |= (1<<TXCIE); |
- | |
330 | - | ||
331 | //Teiler wird gesetzt |
- | |
332 | UBRR= (SYSCLK / (BAUD_RATE * 8L) -1 ); |
- | |
Line 333... | Line 383... | ||
333 | //öffnet einen Kanal für printf (STDOUT) |
383 | // -------------------------------------------------------------------------- |
334 | // fdevopen (uart_putchar, NULL); |
384 | int16_t uart_putchar (int8_t c) |
335 | Debug_Timer = SetDelay(200); |
385 | { |
336 | // Version beim Start ausgeben (nicht schön, aber geht... ) |
386 | // if tx is not enabled return immediatly |
337 | uart_putchar ('\n');uart_putchar ('C');uart_putchar ('P');uart_putchar (':'); |
387 | if(!(UCSR0B & (1 << TXEN0))) return (0); |
338 | uart_putchar ('V');uart_putchar (0x30 + VERSION_HAUPTVERSION);uart_putchar ('.');uart_putchar (0x30 + VERSION_NEBENVERSION/10); uart_putchar (0x30 + VERSION_NEBENVERSION%10); |
388 | if (c == '\n') uart_putchar('\r'); |
339 | uart_putchar ('\n'); |
- | |
340 | } |
- | |
341 | - | ||
342 | - | ||
343 | void BearbeiteRxDaten(void) |
- | |
344 | { |
- | |
345 | if(!NeuerDatensatzEmpfangen) return; |
- | |
346 | // unsigned int tmp_int_arr1[1]; |
- | |
347 | // unsigned int tmp_int_arr2[2]; |
- | |
348 | // unsigned int tmp_int_arr3[3]; |
- | |
349 | unsigned char tmp_char_arr2[2]; |
389 | // wait until previous character was send |
Line 350... | Line 390... | ||
350 | // unsigned char tmp_char_arr3[3]; |
390 | loop_until_bit_is_set(UCSR0A, UDRE0); |
- | 391 | // send character |
|
351 | // unsigned char tmp_char_arr4[4]; |
392 | UDR0 = c; |
352 | //if(!MotorenEin) |
393 | return (0); |
353 | PcZugriff = 255; |
394 | } |
354 | 395 | ||
- | 396 | ||
355 | switch(RxdBuffer[2]) |
397 | // -------------------------------------------------------------------------- |
356 | { |
- | |
357 | case 'w':// Lagewinkel |
398 | void USART0_ProcessRxData(void) |
358 | Decode64((unsigned char *) &ExternData,sizeof(ExternData),3,AnzahlEmpfangsBytes); |
- | |
359 | DebugOut.Analog[15]++; |
399 | { |
- | 400 | // if data in the rxd buffer are not locked immediately return |
|
360 | KompassAntwort = 1; |
401 | if(!rxd_buffer_locked) return; |
- | 402 | ||
361 | break; |
403 | uint8_t tmp_char_arr2[2]; // local char buffer |
362 | case 'c': |
404 | |
363 | case 'b': |
- | |
364 | Decode64((unsigned char *) &ExternControl,sizeof(ExternControl),3,AnzahlEmpfangsBytes); |
- | |
365 | ExternData.Winkel[0] = ExternControl.Par1; |
- | |
366 | ExternData.Winkel[1] = ExternControl.Par2; |
- | |
367 | break; |
- | |
368 | case 'v': // Version-Anforderung und Ausbaustufe |
- | |
369 | GetVersionAnforderung = 1; |
- | |
370 | PC_Connected = 255; |
- | |
371 | break; |
- | |
372 | - | ||
373 | case 'a':// Texte der Analogwerte |
- | |
374 | Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes); |
- | |
375 | DebugTextAnforderung = tmp_char_arr2[0]; |
- | |
376 | PC_Connected = 255; |
- | |
377 | break; |
- | |
378 | case 'g':// "Get"-Anforderung für Debug-Daten |
- | |
379 | // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
- | |
380 | PC_Connected = 255; |
- | |
381 | DebugGetAnforderung = 1; |
- | |
382 | break; |
- | |
383 | case 'h':// x-1 Displayzeilen |
- | |
384 | PC_Connected = 255; |
- | |
385 | break; |
- | |
386 | /* |
- | |
387 | case 'b': |
- | |
388 | Decode64((unsigned char *) &ExternControl,sizeof(ExternControl),3,AnzahlEmpfangsBytes); |
- | |
389 | RemoteTasten |= ExternControl.RemoteTasten; |
- | |
390 | ConfirmFrame = ExternControl.Frame; |
- | |
391 | break; |
- | |
392 | case 'c': |
- | |
393 | Decode64((unsigned char *) &ExternControl,sizeof(ExternControl),3,AnzahlEmpfangsBytes); |
- | |
394 | RemoteTasten |= ExternControl.RemoteTasten; |
- | |
395 | ConfirmFrame = ExternControl.Frame; |
- | |
396 | DebugDataAnforderung = 1; |
- | |
397 | break; |
- | |
398 | case 'h':// x-1 Displayzeilen |
- | |
Line -... | Line 405... | ||
- | 405 | switch(rxd_buffer[2]) |
|
- | 406 | { |
|
- | 407 | case 'w':// Attitude |
|
- | 408 | Decode64((uint8_t *) &ExternData, sizeof(ExternData), 3, ReceivedBytes); |
|
399 | Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes); |
409 | DebugOut.Analog[15]++; |
Line 400... | Line -... | ||
400 | RemoteTasten |= tmp_char_arr2[0]; |
- | |
401 | if(tmp_char_arr2[1] == 255) NurKanalAnforderung = 1; else NurKanalAnforderung = 0; // keine Displaydaten |
- | |
402 | DebugDisplayAnforderung = 1; |
- | |
403 | break; |
410 | RequestCompassHeading = TRUE; |
404 | case 't':// Motortest |
411 | break; |
405 | Decode64((unsigned char *) &MotorTest[0],sizeof(MotorTest),3,AnzahlEmpfangsBytes); |
- | |
406 | break; |
- | |
407 | case 'k':// Keys von DubWise |
- | |
408 | Decode64((unsigned char *) &DubWiseKeys[0],sizeof(DubWiseKeys),3,AnzahlEmpfangsBytes); |
- | |
409 | ConfirmFrame = DubWiseKeys[3]; |
- | |
410 | break; |
412 | |
411 | case 'q':// "Get"-Anforderung für Settings |
413 | case 'b': // extern control |
412 | // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
- | |
413 | Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes); |
- | |
414 | if(tmp_char_arr2[0] != 0xff) |
414 | case 'c': // extern control with debug request |
415 | { |
415 | Decode64((uint8_t *) &ExternControl,sizeof(ExternControl), 3,ReceivedBytes); |
416 | if(tmp_char_arr2[0] > 5) tmp_char_arr2[0] = 5; |
416 | ExternData.Attitude[0] = ExternControl.Par1; |
417 | ReadParameterSet(tmp_char_arr2[0], (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
417 | ExternData.Attitude[1] = ExternControl.Par2; |
Line -... | Line 418... | ||
- | 418 | break; |
|
418 | SendOutData('L' + tmp_char_arr2[0] -1,MeineSlaveAdresse,(unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE); |
419 | |
419 | } |
420 | case 'h':// x-1 display columns |
420 | else |
421 | PC_Connected = 255; |
- | 422 | break; |
|
- | 423 | ||
- | 424 | case 'v': // get version and board release |
|
- | 425 | RequestVerInfo = TRUE; |
|
421 | SendOutData('L' + GetActiveParamSetNumber()-1,MeineSlaveAdresse,(unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE); |
426 | PC_Connected = 255; |
422 | |
427 | break; |
423 | break; |
428 | |
424 | |
429 | case 'a':// Labels of the Analog Debug outputs |
425 | case 'l': |
430 | Decode64((uint8_t *) &tmp_char_arr2[0], sizeof(tmp_char_arr2), 3, ReceivedBytes); |
426 | case 'm': |
431 | RequestDebugLabel = tmp_char_arr2[0]; |
- | 432 | PC_Connected = 255; |
|
427 | case 'n': |
433 | break; |
428 | case 'o': |
434 | |
429 | case 'p': // Parametersatz speichern |
435 | case 'g':// get debug data |
430 | Decode64((unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE,3,AnzahlEmpfangsBytes); |
436 | RequestDebugData = TRUE; |
431 | WriteParameterSet(RxdBuffer[2] - 'l' + 1, (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
437 | PC_Connected = 255; |
- | 438 | break; |
|
432 | eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], RxdBuffer[2] - 'l' + 1); // aktiven Datensatz merken |
439 | } |
433 | Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L; |
440 | // unlock the rxd buffer after processing |
434 | Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L; |
441 | rxd_buffer_locked = FALSE; |
435 | Piep(GetActiveParamSetNumber()); |
442 | } |
436 | break; |
443 | |
- | 444 | ||
437 | */ |
445 | |
438 | 446 | //--------------------------------------------------------------------------------------------- |
|
- | 447 | void USART0_TransmitTxData(void) |
|
439 | } |
448 | { |
440 | // DebugOut.AnzahlZyklen = Debug_Timer_Intervall; |
449 | if(!(UCSR0B & (1 << TXEN0))) return; |
441 | NeuerDatensatzEmpfangen = 0; |
450 | |
- | 451 | if(!txd_complete) return; |
|
442 | } |
452 | |
443 | 453 | if(CheckDelay(Debug_Timer) && txd_complete) |
|
444 | 454 | { |
|
445 | //--------------------------------------------------------------------------------------------- |
455 | SetDebugValues(); |
446 | void DatenUebertragung(void) |
456 | SendOutData('D',MySlaveAddr,(uint8_t *) &DebugOut,sizeof(DebugOut)); |
447 | { |
457 | Debug_Timer = SetDelay(250); |
448 | if((CheckDelay(Debug_Timer) && UebertragungAbgeschlossen)) // im Singlestep-Betrieb in jedem Schtitt senden |
- |