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