00001
00085 #include <avr32/io.h>
00086 #include "board.h"
00087 #include "compiler.h"
00088 #include "gpio.h"
00089 #include "wdt4.h"
00090 #include "power_clocks_lib.h"
00091 #include "cycle_counter.h"
00092
00093
00094 #define WDT_MIN_VALUE_US 1000000
00095
00096 #define WDT_MAX_VALUE_US 4000000
00097
00098 #define WDT_CTRL_STEP_US 1000000
00099
00100
00101 volatile U32 current_wdt_value = WDT_MIN_VALUE_US;
00102
00103 volatile U8 step_led_task = 0;
00104
00105 wdt_opt_t opt = {
00106 .dar = FALSE,
00107 .mode = WDT_BASIC_MODE,
00108 .sfv = FALSE,
00109 .fcd = FALSE,
00110 .cssel = WDT_CLOCK_SOURCE_SELECT_RCSYS,
00111 .us_timeout_period = WDT_MIN_VALUE_US
00112 };
00113
00116 void led_task()
00117 {
00118 switch(step_led_task)
00119 {
00120 case 0:
00121 gpio_clr_gpio_pin(LED1_GPIO);
00122 gpio_set_gpio_pin(LED2_GPIO);
00123 gpio_set_gpio_pin(LED3_GPIO);
00124 step_led_task=1;
00125 cpu_delay_ms(300,FOSC0);
00126 break;
00127 case 1:
00128 gpio_set_gpio_pin(LED1_GPIO);
00129 gpio_clr_gpio_pin(LED2_GPIO);
00130 gpio_set_gpio_pin(LED3_GPIO);
00131 step_led_task=2;
00132 cpu_delay_ms(300,FOSC0);
00133 break;
00134 case 2:
00135 gpio_set_gpio_pin(LED1_GPIO);
00136 gpio_set_gpio_pin(LED2_GPIO);
00137 gpio_clr_gpio_pin(LED3_GPIO);
00138 step_led_task=0;
00139 cpu_delay_ms(300,FOSC0);
00140 break;
00141 default :
00142 gpio_clr_gpio_pin(LED1_GPIO);
00143 gpio_set_gpio_pin(LED2_GPIO);
00144 gpio_set_gpio_pin(LED3_GPIO);
00145 step_led_task=1;
00146 cpu_delay_ms(300,FOSC0);
00147 break;
00148 }
00149 }
00150
00153 void wdt_scheduler(void)
00154 {
00155
00156
00157 if(AVR32_PM.RCAUSE.wdt) {
00158 wdt_reenable();
00159 gpio_clr_gpio_pin(LED0_GPIO);
00160 gpio_clr_gpio_pin(LED1_GPIO);
00161 gpio_clr_gpio_pin(LED2_GPIO);
00162 gpio_clr_gpio_pin(LED3_GPIO);
00163 cpu_delay_ms(300,FOSC0);
00164
00165 }else if (AVR32_PM.RCAUSE.por) {
00166 current_wdt_value = WDT_MIN_VALUE_US ;
00167
00168 pcl_write_gplp(0,current_wdt_value);
00169 opt.us_timeout_period = current_wdt_value;
00170 wdt_enable(&opt);
00171
00172 }else if (AVR32_PM.RCAUSE.ext) {
00173
00174 current_wdt_value = pcl_read_gplp(0);
00175 current_wdt_value += WDT_CTRL_STEP_US;
00176 if (current_wdt_value >= WDT_MAX_VALUE_US) current_wdt_value = WDT_MIN_VALUE_US;
00177 opt.us_timeout_period = current_wdt_value;
00178 wdt_enable(&opt);
00179
00180 pcl_write_gplp(0,current_wdt_value);
00181
00182 }else{
00183 current_wdt_value = WDT_MIN_VALUE_US;
00184
00185 pcl_write_gplp(0,current_wdt_value);
00186 opt.us_timeout_period = current_wdt_value;
00187 wdt_enable(&opt);
00188 }
00189 }
00190
00191
00192
00193
00194 int main(void)
00195 {
00196
00197 pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP);
00198
00199
00200 wdt_scheduler();
00201
00202 while(1)
00203 {
00204
00205 led_task();
00206 }
00207
00208 }