00001
00083 #include <avr32/io.h>
00084 #include "board.h"
00085 #include "compiler.h"
00086 #include "gpio.h"
00087 #include "wdt.h"
00088 #include "pm.h"
00089 #include "cycle_counter.h"
00090
00091
00092 #define WDT_MIN_VALUE_US 1000000
00093
00094 #define WDT_MAX_VALUE_US 4000000
00095
00096 #define WDT_CTRL_STEP_US 1000000
00097
00098
00099 volatile avr32_pm_t* pm = &AVR32_PM;
00100
00101
00102
00103 volatile U32 current_wdt_value = WDT_MIN_VALUE_US;
00104
00105 volatile U8 step_led_task = 0;
00106
00109 void led_task()
00110 {
00111 switch(step_led_task)
00112 {
00113 case 0:
00114 gpio_clr_gpio_pin(LED1_GPIO);
00115 gpio_set_gpio_pin(LED2_GPIO);
00116 gpio_set_gpio_pin(LED3_GPIO);
00117 step_led_task=1;
00118 cpu_delay_ms(300,FOSC0);
00119 break;
00120 case 1:
00121 gpio_set_gpio_pin(LED1_GPIO);
00122 gpio_clr_gpio_pin(LED2_GPIO);
00123 gpio_set_gpio_pin(LED3_GPIO);
00124 step_led_task=2;
00125 cpu_delay_ms(300,FOSC0);
00126 break;
00127 case 2:
00128 gpio_set_gpio_pin(LED1_GPIO);
00129 gpio_set_gpio_pin(LED2_GPIO);
00130 gpio_clr_gpio_pin(LED3_GPIO);
00131 step_led_task=0;
00132 cpu_delay_ms(300,FOSC0);
00133 break;
00134 default :
00135 gpio_clr_gpio_pin(LED1_GPIO);
00136 gpio_set_gpio_pin(LED2_GPIO);
00137 gpio_set_gpio_pin(LED3_GPIO);
00138 step_led_task=1;
00139 cpu_delay_ms(300,FOSC0);
00140 break;
00141 }
00142 }
00143
00146 void wdt_scheduler(void)
00147 {
00148
00149
00150 if(pm->RCAUSE.wdt) {
00151 wdt_reenable();
00152 gpio_clr_gpio_pin(LED0_GPIO);
00153 gpio_clr_gpio_pin(LED1_GPIO);
00154 gpio_clr_gpio_pin(LED2_GPIO);
00155 gpio_clr_gpio_pin(LED3_GPIO);
00156 cpu_delay_ms(300,FOSC0);
00157
00158 }else if (pm->RCAUSE.por) {
00159 current_wdt_value = WDT_MIN_VALUE_US ;
00160
00161 pm_write_gplp(pm,0,current_wdt_value);
00162 wdt_enable(current_wdt_value);
00163
00164 }else if (pm->RCAUSE.ext) {
00165
00166 current_wdt_value = pm_read_gplp(pm,0);
00167 current_wdt_value += WDT_CTRL_STEP_US;
00168 if (current_wdt_value >= WDT_MAX_VALUE_US) current_wdt_value = WDT_MIN_VALUE_US;
00169 wdt_enable(current_wdt_value);
00170
00171 pm_write_gplp(pm,0,current_wdt_value);
00172
00173
00174 }else{
00175 current_wdt_value = WDT_MIN_VALUE_US;
00176
00177 pm_write_gplp(pm,0,current_wdt_value);
00178 wdt_enable(current_wdt_value);
00179 }
00180 }
00181
00182
00183
00184
00185 int main(void)
00186 {
00187
00188 pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);
00189
00190 wdt_scheduler();
00191 while(1)
00192 {
00193
00194 led_task();
00195 }
00196 }