comparison main.c @ 22:a55d7c2440fd colopi1

basically done
author Matt Johnston <matt@ucc.asn.au>
date Sun, 16 Jun 2013 22:05:55 +0800
parents 83a0663c082f
children 292cd17de2a4
comparison
equal deleted inserted replaced
21:83a0663c082f 22:a55d7c2440fd
102 static uint8_t watchdog_short_hit; 102 static uint8_t watchdog_short_hit;
103 static uint8_t newboot_hit; 103 static uint8_t newboot_hit;
104 static uint8_t oneshot_hit; 104 static uint8_t oneshot_hit;
105 static uint8_t reboot_hit; 105 static uint8_t reboot_hit;
106 106
107 // informational for status messages
108 static uint8_t boot_normal_status;
109
107 // flips between 0 and 1 each watchdog_long_hit, so eventually a 110 // flips between 0 and 1 each watchdog_long_hit, so eventually a
108 // working firmware should boot. set back to 0 for each 'alive' 111 // working firmware should boot. set back to 0 for each 'alive'
109 // command 112 // command
110 static uint8_t long_reboot_mode = 0; 113 static uint8_t long_reboot_mode = 0;
111 114
112 static uint8_t readpos; 115 static uint8_t readpos;
113 static char readbuf[150]; 116 static char readbuf[150];
114 static uint8_t have_cmd; 117 static uint8_t have_cmd;
115 118
116 int uart_putchar(char c, FILE *stream); 119 int uart_putchar(char c, FILE *stream);
117 static void long_delay(int ms); 120 static void long_delay(uint16_t ms);
118 static void blink(); 121 static void blink();
119 static uint16_t adc_vcc(); 122 static uint16_t adc_vcc();
120 static uint16_t adc_5v(uint16_t vcc); 123 static uint16_t adc_5v(uint16_t vcc);
121 static uint16_t adc_temp(); 124 static uint16_t adc_temp();
122 static void set_pi_boot_normal(uint8_t normal); 125 static void set_pi_boot_normal(uint8_t normal);
315 "watchdog_long %lu (%lu) watchdog newboot mode %d\n" 318 "watchdog_long %lu (%lu) watchdog newboot mode %d\n"
316 "watchdog_short %lu (%lu)\n" 319 "watchdog_short %lu (%lu)\n"
317 "newboot %lu (%lu)\n" 320 "newboot %lu (%lu)\n"
318 "oneshot (%lu)\n" 321 "oneshot (%lu)\n"
319 "uptime %lu rem %u\n" 322 "uptime %lu rem %u\n"
323 "boot normal %hhu\n"
320 ), 324 ),
321 watchdog_long_limit, cur_watchdog_long, long_reboot_mode, 325 watchdog_long_limit, cur_watchdog_long, long_reboot_mode,
322 watchdog_short_limit, cur_watchdog_short, 326 watchdog_short_limit, cur_watchdog_short,
323 newboot_limit, cur_newboot, 327 newboot_limit, cur_newboot,
324 cur_oneshot, 328 cur_oneshot,
325 t.ticks, t.rem); 329 t.ticks, t.rem,
330 boot_normal_status);
326 } 331 }
327 332
328 static void 333 static void
329 cmd_set_params(const char *params) 334 cmd_set_params(const char *params)
330 { 335 {
825 // /16 prescaler 830 // /16 prescaler
826 ADCSRA = _BV(ADEN) | _BV(ADPS2); 831 ADCSRA = _BV(ADEN) | _BV(ADPS2);
827 832
828 // set to measure 1.1 reference 833 // set to measure 1.1 reference
829 ADMUX = admux; 834 ADMUX = admux;
835
836 // delay after setting reference etc, allow settling
837 long_delay(300);
830 // average a number of samples 838 // average a number of samples
831 uint16_t sum = 0; 839 uint16_t sum = 0;
832 uint8_t num = 0; 840 uint8_t num = 0;
833 for (uint8_t n = 0; n < 20; n++) 841 for (uint8_t n = 0; n < 20; n++)
834 { 842 {
883 891
884 static uint16_t 892 static uint16_t
885 adc_temp() 893 adc_temp()
886 { 894 {
887 // set to measure temperature against 1.1v reference. 895 // set to measure temperature against 1.1v reference.
888 const uint8_t mux = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); 896 const uint8_t mux = _BV(REFS0) | _BV(REFS1) | _BV(MUX3);
889 uint16_t sum; 897 uint16_t sum;
890 uint8_t num; 898 uint8_t num;
891 899
892 adc_generic(mux, &num, &sum); 900 adc_generic(mux, &num, &sum);
893 901
894 // return the voltage 902 // return the voltage
895 return ((uint32_t)1100*1024*sum) / num; 903
904 return ((uint32_t)1100*sum) / (num*1024);
896 } 905 }
897 906
898 static void 907 static void
899 reboot_pi() 908 reboot_pi()
900 { 909 {
920 } 929 }
921 930
922 static void 931 static void
923 set_pi_boot_normal(uint8_t normal) 932 set_pi_boot_normal(uint8_t normal)
924 { 933 {
934 boot_normal_status = normal;
925 PORT_PI_BOOT &= ~_BV(PIN_PI_BOOT); 935 PORT_PI_BOOT &= ~_BV(PIN_PI_BOOT);
926 if (normal) 936 if (normal)
927 { 937 {
928 // tristate 938 // tristate
929 DDR_PI_BOOT &= ~_BV(PIN_PI_BOOT); 939 DDR_PI_BOOT &= ~_BV(PIN_PI_BOOT);
1015 PORT_LED |= _BV(PIN_LED); 1025 PORT_LED |= _BV(PIN_LED);
1016 #endif 1026 #endif
1017 } 1027 }
1018 1028
1019 static void 1029 static void
1020 long_delay(int ms) 1030 long_delay(uint16_t ms)
1021 { 1031 {
1022 int iter = ms / 100; 1032 uint16_t iter = ms / 10;
1023 1033
1024 for (int i = 0; i < iter; i++) 1034 for (uint16_t i = 0; i < iter; i++)
1025 { 1035 {
1026 _delay_ms(100); 1036 _delay_ms(10);
1027 } 1037 }
1028 } 1038 }
1029 1039
1030 ISR(BADISR_vect) 1040 ISR(BADISR_vect)
1031 { 1041 {