9f0724bec8a601ada62860454a804783b43e76a3
[ros_wild_thumper.git] / avr / nano / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <limits.h>
4 #include <avr/io.h>
5 #include <avr/interrupt.h>
6 #include <avr/sleep.h>
7 #include <util/delay.h>
8 #include <util/twi.h>
9 #include <avr/eeprom.h>
10 #include <avr/wdt.h>
11 #include <avr/pgmspace.h>
12 #include "uart.h"
13
14 /*
15  * I2C Register Map (8 Bit)
16  * 0x00 Register select
17  * 0x01 Distance left MSB
18  * 0x02 Distance left LSB
19  * 0x03 Distance right MSB
20  * 0x04 Distance right LSB
21  * 0x05 Distance forward1 MSB
22  * 0x06 Distance forward1 LSB
23  * 0x07 Distance backward MSB
24  * 0x08 Distance backward LSB
25  * 0x09 Voltage MSB
26  * 0x0A Voltage LSB
27  * 0x0B Distance forward2 MSB
28  * 0x0C Distance forward2 LSB
29  * 0x0D Current MSB
30  * 0x0E Current LSB
31  *
32  * 0x15 Distance forward1 MSB (read only)
33  * 0x16 Distance forward1 LSB (read only)
34  * 0x17 Distance backward MSB (read only)
35  * 0x18 Distance backward LSB (read only)
36  * 0x19 Distance forward2 MSB (read only)
37  * 0x1A Distance forward2 LSB (read only)
38  *
39  * 0xff Bootloader
40  */
41
42
43 /**
44  * Nano Pinout
45  * A0 PC0/ADC0
46  * A1 PC1/ADC1
47  * A2 PC2/ADC2
48  * A3 PC3/ADC3
49  * A4 PC4/ADC4
50  * A5 PC5/ADC5
51  * A6 ADC6
52  * A7 ADC7
53  * D0 PD0/RX
54  * D1 PD1/TX
55  * D2 PD2/IND0
56  * D3 PD3/IND1
57  * D4 PD4/T0
58  * D5 PD5/T1
59  * D6 PD6/AIN0
60  * D7 PD7/AIN1
61  * D8 PB0/ICP
62  * D9 PB1/OC1
63  * D10 PB2/SS
64  * D11 PB3/MOSI
65  * D12 PB4/MISO
66  * D13 PB5/SCK
67  */
68
69
70 #define TWI_ACK   TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN) | (1<<TWIE)
71 #define TWI_NAK   TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
72 #define TWI_RESET TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWSTO) | (1<<TWEN) | (1<<TWIE);
73
74 static volatile uint8_t ireg=0;
75 static volatile uint8_t bootloader=0;
76 static volatile uint16_t dist_left=0;
77 static volatile uint16_t dist_right=0;
78 static volatile uint16_t dist_forward1=0;
79 static volatile uint16_t dist_forward2=0;
80 static volatile uint16_t dist_backward=0;
81 static volatile uint8_t start_dist_fwd1=0;
82 static volatile uint8_t start_dist_fwd2=0;
83 static volatile uint8_t start_dist_bwd=0;
84 static volatile uint16_t voltage=0;
85 static volatile uint16_t current=0;
86 static volatile uint8_t pind_pre=0;
87
88 ISR(TWI_vect)
89 {
90         static int16_t tmp16=0;
91
92         switch(TW_STATUS)
93         {  
94                 case TW_SR_SLA_ACK: // start write
95                         TWI_ACK;
96                         ireg = 0;
97                         break;
98                 case TW_SR_DATA_ACK: // write
99                         switch(ireg) {
100                                 case 0x00: // register select
101                                         ireg = TWDR;
102
103                                         if (ireg == 0x05) start_dist_fwd1=1;
104                                         if (ireg == 0x07) start_dist_bwd=1;
105                                         if (ireg == 0x0b) start_dist_fwd2=1;
106
107                                         ireg--; // because we do ireg++ below
108                                         TWI_ACK;
109                                         break;
110                                 case 0xff: // bootloader
111                                         bootloader = TWDR;
112                                 default:
113                                         TWI_NAK;
114                         }
115                         if (ireg < 0xff) ireg++;
116                         break;
117                 case TW_ST_SLA_ACK: // start read
118                 case TW_ST_DATA_ACK: // read
119                         switch(ireg) {
120                                 case 0x01: // Distance left MSB
121                                         tmp16 = dist_left;
122                                         TWDR = tmp16>>8;
123                                         TWI_ACK;
124                                         break;
125                                 case 0x02: // Distance right LSB
126                                         TWDR = tmp16;
127                                         TWI_ACK;
128                                         break;
129                                 case 0x03: // Distance right MSB
130                                         tmp16 = dist_right;
131                                         TWDR = tmp16>>8;
132                                         TWI_ACK;
133                                         break;
134                                 case 0x04: // Distance right LSB
135                                         TWDR = tmp16;
136                                         TWI_ACK;
137                                         break;
138                                 case 0x05: // Distance forward1 MSB
139                                         tmp16 = dist_forward1;
140                                         TWDR = tmp16>>8;
141                                         TWI_ACK;
142                                         break;
143                                 case 0x06: // Distance forward1 LSB
144                                         TWDR = tmp16;
145                                         TWI_ACK;
146                                         break;
147                                 case 0x07: // Distance backward MSB
148                                         tmp16 = dist_backward;
149                                         TWDR = tmp16>>8;
150                                         TWI_ACK;
151                                         break;
152                                 case 0x08: // Distance backward LSB
153                                         TWDR = tmp16;
154                                         TWI_ACK;
155                                         break;
156                                 case 0x09: // Voltage MSB
157                                         tmp16 = voltage;
158                                         TWDR = tmp16>>8;
159                                         TWI_ACK;
160                                         break;
161                                 case 0x0A: // Voltage LSB
162                                         TWDR = tmp16;
163                                         TWI_ACK;
164                                         break;
165                                 case 0x0B: // Distance forward2 MSB
166                                         tmp16 = dist_forward2;
167                                         TWDR = tmp16>>8;
168                                         TWI_ACK;
169                                         break;
170                                 case 0x0C: // Distance forward2 LSB
171                                         TWDR = tmp16;
172                                         TWI_ACK;
173                                         break;
174                                 case 0x0D: // Current MSB
175                                         tmp16 = current;
176                                         TWDR = tmp16>>8;
177                                         TWI_ACK;
178                                         break;
179                                 case 0x0E: // Current LSB
180                                         TWDR = tmp16;
181                                         TWI_ACK;
182                                         break;
183                                 case 0x15: // Distance forward1 MSB
184                                         tmp16 = dist_forward1;
185                                         TWDR = tmp16>>8;
186                                         TWI_ACK;
187                                         break;
188                                 case 0x16: // Distance forward1 LSB
189                                         TWDR = tmp16;
190                                         TWI_ACK;
191                                         break;
192                                 case 0x17: // Distance backward MSB
193                                         tmp16 = dist_backward;
194                                         TWDR = tmp16>>8;
195                                         TWI_ACK;
196                                         break;
197                                 case 0x18: // Distance backward LSB
198                                         TWDR = tmp16;
199                                         TWI_ACK;
200                                         break;
201                                 case 0x19: // Distance forward2 MSB
202                                         tmp16 = dist_forward2;
203                                         TWDR = tmp16>>8;
204                                         TWI_ACK;
205                                         break;
206                                 case 0x1A: // Distance forward2 LSB
207                                         TWDR = tmp16;
208                                         TWI_ACK;
209                                         break;
210                                 default:
211                                         TWDR = 0;
212                                         TWI_NAK;
213                         }
214                         ireg++;
215                         break;
216                 case TW_SR_STOP:
217                         TWI_ACK;
218                         break;
219                 default:
220                         TWI_RESET;
221         }
222 }
223
224
225 uint16_t ReadChannel(uint8_t mux) {
226         uint8_t i;
227         uint16_t result;
228
229         ADCSRA = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0);    // Frequenzvorteiler 
230         // setzen auf 8 (1) und ADC aktivieren (1)
231
232         ADMUX = mux;                      // Kanal waehlen
233         ADMUX |= (1<<REFS0);
234
235         /* nach Aktivieren des ADC wird ein "Dummy-Readout" empfohlen, man liest
236            also einen Wert und verwirft diesen, um den ADC "warmlaufen zu lassen" */
237         ADCSRA |= (1<<ADSC);              // eine ADC-Wandlung 
238         while ( ADCSRA & (1<<ADSC)  ) {
239                      // auf Abschluss der Konvertierung warten 
240         }
241         result = ADCW;  // ADCW muss einmal gelesen werden,
242         // sonst wird Ergebnis der nächsten Wandlung
243         // nicht übernommen.
244
245         /* Eigentliche Messung - Mittelwert aus 5 aufeinanderfolgenden Wandlungen */
246         result = 0;
247         for( i=0; i<5; i++ )
248         {
249                 ADCSRA |= (1<<ADSC);            // eine Wandlung "single conversion"
250                 while ( ADCSRA & (1<<ADSC) ) {
251                            // auf Abschluss der Konvertierung warten
252                 }
253                 result += ADCW;             // Wandlungsergebnisse aufaddieren
254         }
255
256         ADCSRA &= ~(1<<ADEN);             // ADC deaktivieren (2)
257
258         result /= 5;                     // Summe durch 5 teilen = arithm. Mittelwert
259         
260         return result;
261 }
262
263
264 static unsigned short get_distance(uint8_t i) {
265         return ReadChannel(i);
266 }
267
268
269 static unsigned short get_voltage(void) {
270         return ReadChannel(2)*1.46;
271 }
272
273
274 /*
275  * zero offset: 0.5V
276  * sensitivity: 133mV/A
277  */
278 static unsigned short get_current(void) {
279         double volt = ReadChannel(3)*5.0171; // mV
280         return (volt-517.78)/0.12656; // mA
281 }
282
283
284 ISR(INT0_vect) {
285         static uint16_t t_start=0;
286         uint16_t t_now = TCNT1;
287         uint16_t t_diff;
288
289         if (bit_is_set(PIND, 2)) { // high level
290                 // start timer
291                 t_start = t_now;
292         } else {
293                 t_diff = t_now - t_start;
294                 dist_forward1 = t_diff*2.7586 + 0.5; // t [µs] / 580 = mm
295                 // disable this interrupt
296                 EIMSK &= ~(1 << INT0);
297         }
298 }
299
300
301 ISR(INT1_vect) {
302         static uint16_t t_start=0;
303         uint16_t t_now = TCNT1;
304         uint16_t t_diff;
305
306         if (bit_is_set(PIND, 3)) { // high level
307                 // start timer
308                 t_start = t_now;
309         } else {
310                 t_diff = t_now - t_start;
311                 dist_backward = t_diff*2.7586 + 0.5; // t [µs] / 580 = mm
312                 // disable this interrupt
313                 EIMSK &= ~(1 << INT1);
314         }
315 }
316
317
318 ISR(PCINT2_vect) {
319         uint8_t pind_cur = PIND;
320
321         if ((pind_cur ^ pind_pre) & (1<<4)) { // PCINT20
322                 static uint16_t t_start=0;
323                 uint16_t t_now = TCNT1;
324                 uint16_t t_diff;
325
326                 if (bit_is_set(pind_cur, 4)) { // high level
327                         // start timer
328                         t_start = t_now;
329                 } else {
330                         t_diff = t_now - t_start;
331                         dist_forward2 = t_diff*2.7586 + 0.5; // t [µs] / 580 = mm
332                         // disable this interrupt
333                         PCMSK2 &= ~(1 << PCINT20);
334                 }
335         }
336
337         pind_pre = pind_cur;
338 }
339
340
341 int main(void) {
342         bootloader = 0x00;
343         setup_uart(9600);
344         uart_setup_stdout();
345
346         // I2C
347         TWAR = 0x52;
348         TWI_ACK;
349
350         // Timer 1: Normal mode, Top: 0xffff, Prescaler: F_CPU/256=62500Hz
351         TCCR1A = 0x0;
352         TCCR1B = (1 << CS12);
353
354         // External Interrupts
355         EICRA = (1 << ISC10) | (1 << ISC00);
356         PCICR = (1 << PCIE2);
357
358         printf_P(PSTR("\r\nStart\r\n"));
359
360         set_sleep_mode(SLEEP_MODE_IDLE);
361         sei();
362         while(1) {
363                 switch(ireg) {
364                         case 0x01: // ir left
365                                 dist_left = get_distance(0);
366                                 break;
367                         case 0x03: // ir right
368                                 dist_right = get_distance(1);
369                                 break;
370                         case 0x09: // voltage
371                                 voltage = get_voltage();
372                                 break;
373                         case 0x0d: // current
374                                 current = get_current();
375                                 break;
376                         case 0xff: // Magic reg that starts the bootloader
377                                 if (bootloader == 0xa5) {
378                                         cli();
379                                         // write mark to first area in eeprom
380                                         eeprom_write_byte((uint8_t*)0, 123);
381                                         eeprom_busy_wait();
382                                         // Use watchdog to restart
383                                         wdt_enable(WDTO_15MS);
384                                 }
385                                 break;
386                 }
387
388                 if (start_dist_fwd1) {
389                         start_dist_fwd1 = 0;
390                         dist_forward1 = 0;
391
392                         DDRD |= (1 << 2);
393                         PORTD |= (1 << 2);
394                         _delay_us(10);
395                         PORTD &= ~(1 << 2);
396                         DDRD &= ~(1 << 2);
397                         // wait for interrupt
398                         EIFR &= (1 << INTF0); // clear old interrupt before enabling
399                         EIMSK |= (1 << INT0);
400                 }
401                 if (start_dist_bwd) {
402                         start_dist_bwd = 0;
403                         dist_backward = 0;
404
405                         DDRD |= (1 << 3);
406                         PORTD |= (1 << 3);
407                         _delay_us(10);
408                         PORTD &= ~(1 << 3);
409                         DDRD &= ~(1 << 3);
410                         // wait for interrupt
411                         EIFR &= (1 << INTF1); // clear old interrupt before enabling
412                         EIMSK |= (1 << INT1);
413                 }
414                 if (start_dist_fwd2) {
415                         start_dist_fwd2 = 0;
416                         dist_forward2 = 0;
417
418                         // PD4 = PCINT20
419                         DDRD |= (1 << 4);
420                         PORTD |= (1 << 4);
421                         _delay_us(10);
422                         PORTD &= ~(1 << 4);
423                         DDRD &= ~(1 << 4);
424                         // wait for interrupt
425                         pind_pre = PIND;
426                         PCMSK2 |= (1 << PCINT20);
427                 }
428
429                 sleep_mode();
430         }
431
432         return 0;
433 }