usb_dfu.h File Reference


Detailed Description

Management of the USB device firmware upgrade.

This file manages the USB device firmware upgrade.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file usb_dfu.h.

#include "conf_usb.h"
#include "compiler.h"

Go to the source code of this file.

Defines

#define STATE_appDETACH   0x01
#define STATE_appIDLE   0x00
#define STATE_dfuDNBUSY   0x04
#define STATE_dfuDNLOAD_IDLE   0x05
#define STATE_dfuDNLOAD_SYNC   0x03
#define STATE_dfuERROR   0x0A
#define STATE_dfuIDLE   0x02
#define STATE_dfuMANIFEST   0x07
#define STATE_dfuMANIFEST_SYNC   0x06
#define STATE_dfuMANIFEST_WAIT_RESET   0x08
#define STATE_dfuUPLOAD_IDLE   0x09
#define STATUS_errADDRESS   0x08
#define STATUS_errCHECK_ERASED   0x05
#define STATUS_errERASE   0x04
#define STATUS_errFILE   0x02
#define STATUS_errFIRMWARE   0x0A
#define STATUS_errNOTDONE   0x09
#define STATUS_errPOR   0x0D
#define STATUS_errPROG   0x06
#define STATUS_errSTALLEDPKT   0x0F
#define STATUS_errTARGET   0x01
#define STATUS_errUNKNOWN   0x0E
#define STATUS_errUSBR   0x0C
#define STATUS_errVENDOR   0x0B
#define STATUS_errVERIFY   0x07
#define STATUS_errWRITE   0x03
#define STATUS_OK   0x00

Functions

Bool usb_dfu_dnload (void)
 Management of the DFU_DNLOAD requests.
void usb_dfu_init (void)
Bool usb_dfu_upload (void)
 Management of the DFU_UPLOAD requests.

Variables

U8 usb_dfu_state
U8 usb_dfu_status


Define Documentation

#define STATE_appDETACH   0x01

Definition at line 86 of file usb_dfu.h.

#define STATE_appIDLE   0x00

Definition at line 85 of file usb_dfu.h.

#define STATE_dfuDNBUSY   0x04

Definition at line 89 of file usb_dfu.h.

#define STATE_dfuDNLOAD_IDLE   0x05

Definition at line 90 of file usb_dfu.h.

#define STATE_dfuDNLOAD_SYNC   0x03

Definition at line 88 of file usb_dfu.h.

#define STATE_dfuERROR   0x0A

Definition at line 95 of file usb_dfu.h.

Referenced by usb_user_read_request().

#define STATE_dfuIDLE   0x02

Definition at line 87 of file usb_dfu.h.

Referenced by usb_user_read_request().

#define STATE_dfuMANIFEST   0x07

Definition at line 92 of file usb_dfu.h.

#define STATE_dfuMANIFEST_SYNC   0x06

Definition at line 91 of file usb_dfu.h.

#define STATE_dfuMANIFEST_WAIT_RESET   0x08

Definition at line 93 of file usb_dfu.h.

#define STATE_dfuUPLOAD_IDLE   0x09

Definition at line 94 of file usb_dfu.h.

#define STATUS_errADDRESS   0x08

Definition at line 75 of file usb_dfu.h.

Referenced by get_and_check_mem_range(), and usb_dfu_dnload().

#define STATUS_errCHECK_ERASED   0x05

Definition at line 72 of file usb_dfu.h.

Referenced by erase_check_mem(), usb_dfu_dnload(), and usb_dfu_upload().

#define STATUS_errERASE   0x04

Definition at line 71 of file usb_dfu.h.

#define STATUS_errFILE   0x02

Definition at line 69 of file usb_dfu.h.

#define STATUS_errFIRMWARE   0x0A

Definition at line 77 of file usb_dfu.h.

#define STATUS_errNOTDONE   0x09

Definition at line 76 of file usb_dfu.h.

#define STATUS_errPOR   0x0D

Definition at line 80 of file usb_dfu.h.

#define STATUS_errPROG   0x06

Definition at line 73 of file usb_dfu.h.

#define STATUS_errSTALLEDPKT   0x0F

Definition at line 82 of file usb_dfu.h.

Referenced by usb_dfu_dnload(), usb_dfu_upload(), and usb_user_read_request().

#define STATUS_errTARGET   0x01

Definition at line 68 of file usb_dfu.h.

#define STATUS_errUNKNOWN   0x0E

Definition at line 81 of file usb_dfu.h.

#define STATUS_errUSBR   0x0C

Definition at line 79 of file usb_dfu.h.

#define STATUS_errVENDOR   0x0B

Definition at line 78 of file usb_dfu.h.

#define STATUS_errVERIFY   0x07

Definition at line 74 of file usb_dfu.h.

Referenced by usb_dfu_dnload().

#define STATUS_errWRITE   0x03

Definition at line 70 of file usb_dfu.h.

Referenced by usb_dfu_dnload().

#define STATUS_OK   0x00

Definition at line 67 of file usb_dfu.h.

Referenced by usb_dfu_dnload(), usb_dfu_upload(), and usb_user_read_request().


Function Documentation

Bool usb_dfu_dnload ( void   ) 

Management of the DFU_DNLOAD requests.

Definition at line 542 of file HISTORY/V1.0/usb_dfu.c.

References boot_program(), CMD_BLANK_CHECK, CMD_ERASE, CMD_ERASE_ARG_CHIP, CMD_GRP_DNLOAD, CMD_GRP_EXEC, CMD_GRP_SELECT, CMD_GRP_UPLOAD, CMD_PROGRAM_START, CMD_READ_MEMORY, CMD_SELECT_MEMORY, CMD_SELECT_MEMORY_ARG_PAGE, CMD_SELECT_MEMORY_ARG_UNIT, CMD_START_APPLI, CMD_START_APPLI_ARG_NO_RESET, CMD_START_APPLI_ARG_RESET, erase_check_mem(), force_isp(), get_and_check_mem_range(), is_isp_forced(), address_t::long_address, MEM_BOOTLOADER, MEM_CONFIGURATION, MEM_EEPROM, MEM_FLASH, MEM_SECURITY, MEM_SIGNATURE, MEM_USER, MEMORY_ACCESS, address_t::page, read, STATUS_errADDRESS, STATUS_errCHECK_ERASED, STATUS_errSTALLEDPKT, STATUS_errVERIFY, STATUS_errWRITE, STATUS_OK, sys_clk_gen_stop(), usb_dfu_stop(), wait_10_ms(), write, and write_mem().

Referenced by usb_user_read_request().

00543 {
00544   static U8 tmp_memory;
00545   static U16 tmp_page;
00546 
00547   if (!is_isp_forced()) force_isp(TRUE);
00548 
00549   dfu_status = STATUS_OK;
00550 
00551   Usb_read_endpoint_data(EP_CONTROL, 16); // Dummy read: wValue.
00552   Usb_read_endpoint_data(EP_CONTROL, 16); // Dummy read: wIndex.
00553   length = Usb_read_endpoint_data(EP_CONTROL, 16);
00554   length = usb_format_usb_to_mcu_data(16, length);
00555   Usb_ack_setup_received_free();
00556 
00557   if (length)
00558   {
00559     while (!Is_usb_control_out_received());
00560     Usb_reset_endpoint_fifo_access(EP_CONTROL);
00561 
00562     cmd_group = Usb_read_endpoint_data(EP_CONTROL, 8);
00563     cmd = Usb_read_endpoint_data(EP_CONTROL, 8);
00564     switch (cmd_group)
00565     {
00566     case CMD_GRP_DNLOAD:
00567       if (security_active)
00568       {
00569         goto unsupported_request;
00570       }
00571       if (!get_and_check_mem_range()) break;
00572       if (!MEMORY_ACCESS[memory].write)
00573       {
00574         dfu_status = STATUS_errWRITE;
00575         break;
00576       }
00577       switch (cmd)
00578       {
00579       case CMD_PROGRAM_START:
00580         Usb_ack_control_out_received_free();
00581         write_mem();
00582         break;
00583 
00584       default:
00585         goto unsupported_request;
00586       }
00587       break;
00588 
00589     case CMD_GRP_UPLOAD:
00590       if (security_active)
00591       {
00592         goto unsupported_request;
00593       }
00594       if (!get_and_check_mem_range()) break;
00595       if (!MEMORY_ACCESS[memory].read)
00596       {
00597         dfu_status = STATUS_errVERIFY;
00598         break;
00599       }
00600       switch (cmd)
00601       {
00602       case CMD_READ_MEMORY:
00603         break;
00604 
00605       case CMD_BLANK_CHECK:
00606         erase_check_mem();
00607         break;
00608 
00609       default:
00610         goto unsupported_request;
00611       }
00612       break;
00613 
00614     case CMD_GRP_EXEC:
00615       switch (cmd)
00616       {
00617       case CMD_ERASE:
00618         switch (Usb_read_endpoint_data(EP_CONTROL, 8))
00619         {
00620         case CMD_ERASE_ARG_CHIP:
00621           memory = MEM_FLASH;
00622           end_address.long_address = start_address.long_address = 0;
00623           flashc_lock_all_regions(FALSE);
00624           flashc_erase_all_pages(FALSE);
00625           security_active = FALSE;
00626           break;
00627 
00628         default:
00629           goto unsupported_request;
00630         }
00631         break;
00632 
00633       case CMD_START_APPLI:
00634         switch (Usb_read_endpoint_data(EP_CONTROL, 8))
00635         {
00636         case CMD_START_APPLI_ARG_RESET:
00637           usb_dfu_stop();
00638           Disable_global_interrupt();
00639           AVR32_WDT.ctrl = AVR32_WDT_CTRL_EN_MASK |
00640                            (10 << AVR32_WDT_CTRL_PSEL_OFFSET) |
00641                            (AVR32_WDT_KEY_VALUE << AVR32_WDT_CTRL_KEY_OFFSET);
00642           AVR32_WDT.ctrl = AVR32_WDT_CTRL_EN_MASK |
00643                            (10 << AVR32_WDT_CTRL_PSEL_OFFSET) |
00644                            ((~AVR32_WDT_KEY_VALUE << AVR32_WDT_CTRL_KEY_OFFSET) & AVR32_WDT_CTRL_KEY_MASK);
00645           while (1);
00646 
00647         case CMD_START_APPLI_ARG_NO_RESET:
00648           usb_dfu_stop();
00649           sys_clk_gen_stop();
00650           wait_10_ms();
00651           boot_program();
00652           break;
00653 
00654         default:
00655           goto unsupported_request;
00656         }
00657         break;
00658 
00659       default:
00660         goto unsupported_request;
00661       }
00662       break;
00663 
00664     case CMD_GRP_SELECT:
00665       if (security_active)
00666       {
00667         goto unsupported_request;
00668       }
00669       switch (cmd)
00670       {
00671       case CMD_SELECT_MEMORY:
00672         switch (Usb_read_endpoint_data(EP_CONTROL, 8))
00673         {
00674         case CMD_SELECT_MEMORY_ARG_UNIT:
00675           switch (tmp_memory = Usb_read_endpoint_data(EP_CONTROL, 8))
00676           {
00677           case MEM_FLASH:
00678           case MEM_EEPROM:
00679           case MEM_SECURITY:
00680           case MEM_CONFIGURATION:
00681           case MEM_BOOTLOADER:
00682           case MEM_SIGNATURE:
00683           case MEM_USER:
00684             memory = tmp_memory;
00685             end_address.long_address = start_address.long_address = 0;
00686             break;
00687 
00688           default:
00689             dfu_status = STATUS_errADDRESS;
00690           }
00691           break;
00692 
00693         case CMD_SELECT_MEMORY_ARG_PAGE:
00694           MSB(tmp_page) = Usb_read_endpoint_data(EP_CONTROL, 8);
00695           LSB(tmp_page) = Usb_read_endpoint_data(EP_CONTROL, 8);
00696           if (tmp_page <= MEMORY_END_ADDRESS[memory].page)
00697           {
00698             end_address.page = start_address.page = tmp_page;
00699           }
00700           else dfu_status = STATUS_errADDRESS;
00701           break;
00702 
00703         default:
00704           goto unsupported_request;
00705         }
00706         break;
00707 
00708       default:
00709         goto unsupported_request;
00710       }
00711       break;
00712 
00713     default:
00714 unsupported_request:
00715       dfu_status = STATUS_errSTALLEDPKT;
00716     }
00717 
00718     Usb_ack_control_out_received_free();
00719   }
00720 
00721   usb_dfu_status = dfu_status;
00722   if (usb_dfu_status != STATUS_OK &&
00723       usb_dfu_status != STATUS_errCHECK_ERASED) return FALSE;
00724 
00725   Usb_ack_control_in_ready_send();
00726   while (!Is_usb_control_in_ready());
00727   return TRUE;
00728 }

void usb_dfu_init ( void   ) 

Definition at line 506 of file HISTORY/V1.0/usb_dfu.c.

References address_t::long_address, MEM_FLASH, PRODUCT_ID_ADDRESS, PRODUCT_INF, and PRODUCT_REVISION_ADDRESS.

Referenced by main().

00507 {
00508   PRODUCT_INF[PRODUCT_ID_ADDRESS] =
00509     Rd_bitfield(Get_system_register(AVR32_CONFIG0), AVR32_CONFIG0_PROCESSORID_MASK);
00510   PRODUCT_INF[PRODUCT_REVISION_ADDRESS] =
00511     Rd_bitfield(Get_system_register(AVR32_CONFIG0), AVR32_CONFIG0_PROCESSORREVISION_MASK);
00512 
00513   MEMORY_END_ADDRESS[MEM_FLASH].long_address = flashc_get_flash_size() - 1;
00514 
00515   security_active = flashc_is_security_bit_active();
00516 }

Bool usb_dfu_upload ( void   ) 

Management of the DFU_UPLOAD requests.

Definition at line 733 of file HISTORY/V1.0/usb_dfu.c.

References CMD_BLANK_CHECK, CMD_GRP_UPLOAD, CMD_READ_MEMORY, dfu_format_mcu_to_dfu_data, address_t::page_offset, read_mem(), STATUS_errCHECK_ERASED, STATUS_errSTALLEDPKT, and STATUS_OK.

Referenced by usb_user_read_request().

00734 {
00735   Usb_ack_setup_received_free();
00736   Usb_reset_endpoint_fifo_access(EP_CONTROL);
00737 
00738   if (cmd_group == CMD_GRP_UPLOAD &&
00739       (dfu_status == STATUS_OK ||
00740        dfu_status == STATUS_errCHECK_ERASED))
00741   {
00742     switch (cmd)
00743     {
00744     case CMD_READ_MEMORY:
00745       read_mem();
00746       break;
00747 
00748     case CMD_BLANK_CHECK:
00749       Usb_write_endpoint_data(EP_CONTROL, 16, dfu_format_mcu_to_dfu_data(16, start_address.page_offset));
00750       break;
00751 
00752     default:
00753       goto unsupported_request;
00754     }
00755   }
00756   else
00757   {
00758 unsupported_request:
00759     dfu_status = STATUS_errSTALLEDPKT;
00760   }
00761 
00762   usb_dfu_status = dfu_status;
00763   if (usb_dfu_status != STATUS_OK &&
00764       usb_dfu_status != STATUS_errCHECK_ERASED) return FALSE;
00765 
00766   if (Usb_byte_count(EP_CONTROL)) Usb_ack_control_in_ready_send();
00767   while (!Is_usb_control_out_received());
00768   Usb_ack_control_out_received_free();
00769   return TRUE;
00770 }


Variable Documentation

Definition at line 312 of file HISTORY/V1.0/usb_dfu.c.

Referenced by usb_user_read_request().

Definition at line 311 of file HISTORY/V1.0/usb_dfu.c.

Referenced by usb_user_read_request().


Generated on Fri Feb 19 02:32:46 2010 for AVR32 - USB DFU Example: ISP by  doxygen 1.5.5