This section covers more advanced debugging topics with Studio 7 both as video (linked below) and hands-on document. The main topics are using I/O View to work with Configuration Change Protected (CCP) registers, Memory View to validate EEPROM writes, as well as using the Watch window to cast pointers as an array.
int main(void) { /* * Set the Main clock division factor to 2X, * and keep the Main clock prescaler enabled. */ CLKCTRL.MCLKCTRLB = CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm;
_PROTECTED_WRITE(CLKCTRL.MCLKCTRLB, CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm);
uint8_t hello[] = "Hello World"; eeprom_write_block(hello, (void *)0, sizeof(hello)); uint8_t hi[] = "AVR says hi"; eeprom_write_block(hi, (void *)0, sizeof(hi));
This is covered in more detail in section Debugging 2: Conditional- and Action-Breakpoints, however, the note on how to cast pointers as an array in the Watch window is repeated here.
*(uint8_t (*)[<n>])<name_of_array_pointer>Where <n> is the number of elements in the array and <name_of_array_pointer> is the name of the array to be examined.
*(uint8_t (*)[5])&SW0_edge_countNote that the '&' symbol must be used in this case to obtain a pointer to the variable.
#include <avr/io.h> #include <avr/eeprom.h> void LED_on(void); void LED_off(void); void LED_set_state(uint8_t state); uint8_t SW_get_state(void); uint8_t SW_get_state_logic(void); int main(void) { PORTB.DIRSET = PIN4_bm; /* Configure LED Pin as output */ PORTB.PIN5CTRL = PORT_PULLUPEN_bm; /* Enable pull-up for SW0 pin */ _PROTECTED_WRITE(CLKCTRL.MCLKCTRLB, CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm); uint8_t Hello[] = "Hello World!"; save(Hello,sizeof(Hello)); uint8_t Hi[] = "AVR says hi!"; save(Hi,sizeof(Hi)); while(1) { uint8_t SW0_state = SW_get_state_logic(); /* Read switch state */ LED_set_state(SW0_state); /* Set LED state */ } } void save(const uint8_t* to_save, uint8_t size) { eeprom_write_block(to_save,(void*)0,size); } uint8_t SW_get_state() { return !(PORTB.IN & PIN5_bm); } uint8_t SW_get_state_logic(void) { static uint8_t SW0_prv_state = 0; static uint8_t SW0_edge_count = 0; uint8_t SW0_cur_state = !(PORTB.IN & PIN5_bm); /* Read the current SW0 state */ if (SW0_cur_state != SW0_prv_state) /* Check for edges */ { SW0_edge_count++; } SW0_prv_state = SW0_cur_state; /* Keep track of previous state */ /* * Report the switch as pushed when it is pushed or the edge counter is a * multiple of 3 */ return SW0_cur_state || !(SW0_edge_count % 3); } void LED_off(void) { PORTB.OUTSET = PIN4_bm; /* Turn LED off */ } void LED_on(void) { PORTB.OUTCLR = PIN4_bm; /* Turn LED on */ } void LED_set_state(uint8_t state) { if (state) { LED_on(); } else { LED_off(); } }