usb_dfu.c 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 HISTORY/V1.0/usb_dfu.c.

#include "conf_usb.h"
#include <avr32/io.h>
#include <stddef.h>
#include <string.h>
#include "compiler.h"
#include "pm.h"
#include "flashc.h"
#include "usb_drv.h"
#include "usb_task.h"
#include "usb_descriptors.h"
#include "usb_dfu.h"
#include "boot.h"
#include "conf_isp.h"
#include "isp.h"

Go to the source code of this file.

Data Structures

union  address_t

Defines

#define AVR32_FLASH_SIZE   0x00080000
#define CMD_BLANK_CHECK   0x01
#define CMD_ERASE   0x00
#define CMD_ERASE_ARG_CHIP   0xFF
#define CMD_GRP_DNLOAD   0x01
#define CMD_GRP_EXEC   0x04
#define CMD_GRP_SELECT   0x06
#define CMD_GRP_UPLOAD   0x03
#define CMD_PROGRAM_START   0x00
#define CMD_READ_MEMORY   0x00
#define CMD_SELECT_MEMORY   0x03
#define CMD_SELECT_MEMORY_ARG_PAGE   0x01
#define CMD_SELECT_MEMORY_ARG_UNIT   0x00
#define CMD_START_APPLI   0x03
#define CMD_START_APPLI_ARG_NO_RESET   0x01
#define CMD_START_APPLI_ARG_RESET   0x00
#define ISP_ID0_ADDRESS   0x01
#define ISP_ID1_ADDRESS   0x02
#define ISP_INF_ITEM_COUNT   (ISP_ID1_ADDRESS + 1)
#define ISP_VERSION_ADDRESS   0x00
#define MEM_BOOTLOADER   0x04
#define MEM_CONFIGURATION   0x03
#define MEM_COUNT   (MEM_USER + 1)
#define MEM_EEPROM   0x01
#define MEM_FLASH   0x00
#define MEM_SECURITY   0x02
#define MEM_SIGNATURE   0x05
#define MEM_USER   0x06
#define PRODUCT_FAMILY_ID_ADDRESS   0x01
#define PRODUCT_ID_ADDRESS   0x02
#define PRODUCT_INF_ITEM_COUNT   (PRODUCT_REVISION_ADDRESS + 1)
#define PRODUCT_MANUFACTURER_ID_ADDRESS   0x00
#define PRODUCT_REVISION_ADDRESS   0x03
#define dfu_format_dfu_to_mcu_data(width, data)   ((TPASTE2(U, width))(data))
#define Dfu_format_dfu_to_mcu_data(width, data)   ((TPASTE2(U, width))(data))
#define dfu_format_mcu_to_dfu_data(width, data)   ((TPASTE2(U, width))(data))
#define Dfu_format_mcu_to_dfu_data(width, data)   ((TPASTE2(U, width))(data))
 Convert 16-, 32- or 64-bit data between MCU and Atmel-DFU endianisms.

Functions

static void erase_check_mem (void)
static void force_isp (Bool force)
static Bool get_and_check_mem_range (void)
static Bool is_isp_forced (void)
static void mem_bootloader_read (void *dst, U32 src, size_t nbytes)
static void mem_configuration_read (void *dst, U32 src, size_t nbytes)
static void mem_configuration_write (U32 dst, const void *src, size_t nbytes)
static void mem_flash_read (void *dst, U32 src, size_t nbytes)
static void mem_flash_write (U32 dst, const void *src, size_t nbytes)
static void mem_security_read (void *dst, U32 src, size_t nbytes)
static void mem_security_write (U32 dst, const void *src, size_t nbytes)
static void mem_signature_read (void *dst, U32 src, size_t nbytes)
static void mem_user_read (void *dst, U32 src, size_t nbytes)
static void mem_user_write (U32 dst, const void *src, size_t nbytes)
static void read_mem (void)
Bool usb_dfu_dnload (void)
 Management of the DFU_DNLOAD requests.
void usb_dfu_init (void)
static void usb_dfu_stop (void)
Bool usb_dfu_upload (void)
 Management of the DFU_UPLOAD requests.
static void write_mem (void)

Variables

static U8 cmd = CMD_START_APPLI
static U8 cmd_group = CMD_GRP_EXEC
static U32 data_bytes
static U8 dfu_frame [DFU_FRAME_LENGTH]
static U8 dfu_status = STATUS_OK
static address_t end_address
static const U8 ISP_INF [ISP_INF_ITEM_COUNT]
static S32 length
static U8 memory = MEM_FLASH
struct {
   void(*   read )(void *dst, U32 src, size_t nbytes)
   void(*   write )(U32 dst, const void *src, size_t nbytes)
MEMORY_ACCESS [MEM_COUNT]
static address_t MEMORY_END_ADDRESS [MEM_COUNT]
static const U8 MEMORY_ERASE_VALUE [MEM_COUNT]
static U8 PRODUCT_INF [PRODUCT_INF_ITEM_COUNT]
static Bool security_active = TRUE
static address_t start_address
U8 usb_dfu_state = STATE_dfuIDLE
U8 usb_dfu_status = STATUS_OK


Define Documentation

#define AVR32_FLASH_SIZE   0x00080000

#define CMD_BLANK_CHECK   0x01

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

Referenced by usb_dfu_dnload(), and usb_dfu_upload().

#define CMD_ERASE   0x00

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

Referenced by usb_dfu_dnload().

#define CMD_ERASE_ARG_CHIP   0xFF

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

Referenced by usb_dfu_dnload().

#define CMD_GRP_DNLOAD   0x01

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

Referenced by usb_dfu_dnload().

#define CMD_GRP_EXEC   0x04

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

Referenced by usb_dfu_dnload().

#define CMD_GRP_SELECT   0x06

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

Referenced by usb_dfu_dnload().

#define CMD_GRP_UPLOAD   0x03

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

Referenced by usb_dfu_dnload(), and usb_dfu_upload().

#define CMD_PROGRAM_START   0x00

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

Referenced by usb_dfu_dnload().

#define CMD_READ_MEMORY   0x00

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

Referenced by usb_dfu_dnload(), and usb_dfu_upload().

#define CMD_SELECT_MEMORY   0x03

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

Referenced by usb_dfu_dnload().

#define CMD_SELECT_MEMORY_ARG_PAGE   0x01

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

Referenced by usb_dfu_dnload().

#define CMD_SELECT_MEMORY_ARG_UNIT   0x00

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

Referenced by usb_dfu_dnload().

#define CMD_START_APPLI   0x03

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

Referenced by usb_dfu_dnload().

#define CMD_START_APPLI_ARG_NO_RESET   0x01

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

Referenced by usb_dfu_dnload().

#define CMD_START_APPLI_ARG_RESET   0x00

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

Referenced by usb_dfu_dnload().

#define dfu_format_dfu_to_mcu_data ( width,
data   )     ((TPASTE2(U, width))(data))

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

Referenced by get_and_check_mem_range().

#define Dfu_format_dfu_to_mcu_data ( width,
data   )     ((TPASTE2(U, width))(data))

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

#define dfu_format_mcu_to_dfu_data ( width,
data   )     ((TPASTE2(U, width))(data))

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

Referenced by usb_dfu_upload().

#define Dfu_format_mcu_to_dfu_data ( width,
data   )     ((TPASTE2(U, width))(data))

Convert 16-, 32- or 64-bit data between MCU and Atmel-DFU endianisms.

Depending on MCU endianism, swap or not data bytes.

Parameters:
width Data width in bits: 16, 32 or 64
data 16-, 32- or 64-bit data to format
Returns:
Formatted 16-, 32- or 64-bit data

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

#define ISP_ID0_ADDRESS   0x01

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

#define ISP_ID1_ADDRESS   0x02

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

#define ISP_INF_ITEM_COUNT   (ISP_ID1_ADDRESS + 1)

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

#define ISP_VERSION_ADDRESS   0x00

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

#define MEM_BOOTLOADER   0x04

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

Referenced by usb_dfu_dnload().

#define MEM_CONFIGURATION   0x03

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

Referenced by usb_dfu_dnload().

#define MEM_COUNT   (MEM_USER + 1)

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

#define MEM_EEPROM   0x01

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

Referenced by usb_dfu_dnload().

#define MEM_FLASH   0x00

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

Referenced by usb_dfu_dnload(), and usb_dfu_init().

#define MEM_SECURITY   0x02

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

Referenced by usb_dfu_dnload().

#define MEM_SIGNATURE   0x05

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

Referenced by usb_dfu_dnload().

#define MEM_USER   0x06

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

Referenced by usb_dfu_dnload().

#define PRODUCT_FAMILY_ID_ADDRESS   0x01

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

#define PRODUCT_ID_ADDRESS   0x02

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

Referenced by usb_dfu_init().

#define PRODUCT_INF_ITEM_COUNT   (PRODUCT_REVISION_ADDRESS + 1)

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

#define PRODUCT_MANUFACTURER_ID_ADDRESS   0x00

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

#define PRODUCT_REVISION_ADDRESS   0x03

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

Referenced by usb_dfu_init().


Function Documentation

static void erase_check_mem ( void   )  [static]

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

References DFU_FRAME_LENGTH, address_t::long_address, MEMORY_ACCESS, and STATUS_errCHECK_ERASED.

Referenced by usb_dfu_dnload().

00416 {
00417   U8 *frame;
00418   U32 frame_bytes;
00419 
00420   while (data_bytes)
00421   {
00422     frame = dfu_frame;
00423     frame_bytes = min(data_bytes, DFU_FRAME_LENGTH);
00424     data_bytes -= frame_bytes;
00425 
00426     MEMORY_ACCESS[memory].read(frame, start_address.long_address, frame_bytes);
00427 
00428     while (frame_bytes--)
00429     {
00430       if (*frame++ != MEMORY_ERASE_VALUE[memory])
00431       {
00432         dfu_status = STATUS_errCHECK_ERASED;
00433         return;
00434       }
00435       start_address.long_address++;
00436     }
00437   }
00438 }

static void force_isp ( Bool  force  )  [static]

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

References ISP_GPFB_FORCE_OFFSET.

Referenced by usb_dfu_dnload(), and usb_dfu_stop().

00322 {
00323   flashc_set_gp_fuse_bit(ISP_GPFB_FORCE_OFFSET, force);
00324 }

static Bool get_and_check_mem_range ( void   )  [static]

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

References dfu_format_dfu_to_mcu_data, address_t::long_address, address_t::page_offset, and STATUS_errADDRESS.

Referenced by usb_dfu_dnload().

00328 {
00329   start_address.page_offset = Usb_read_endpoint_data(EP_CONTROL, 16);
00330   start_address.page_offset = dfu_format_dfu_to_mcu_data(16, start_address.page_offset);
00331   end_address.page_offset = Usb_read_endpoint_data(EP_CONTROL, 16);
00332   end_address.page_offset = dfu_format_dfu_to_mcu_data(16, end_address.page_offset);
00333 
00334   if (end_address.long_address < start_address.long_address ||
00335       end_address.long_address > MEMORY_END_ADDRESS[memory].long_address)
00336   {
00337     dfu_status = STATUS_errADDRESS;
00338     return FALSE;
00339   }
00340 
00341   data_bytes = end_address.long_address - start_address.long_address + 1;
00342   return TRUE;
00343 }

static Bool is_isp_forced ( void   )  [static]

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

References ISP_GPFB_FORCE_OFFSET.

Referenced by usb_dfu_dnload().

00316 {
00317   return flashc_read_gp_fuse_bit(ISP_GPFB_FORCE_OFFSET);
00318 }

static void mem_bootloader_read ( void *  dst,
U32  src,
size_t  nbytes 
) [static]

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

References ISP_INF.

00392 {
00393   memcpy(dst, ISP_INF + src, nbytes);
00394 }

static void mem_configuration_read ( void *  dst,
U32  src,
size_t  nbytes 
) [static]

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

00376 {
00377   U8 *dest = dst;
00378   while (nbytes--)
00379     *dest++ = flashc_read_gp_fuse_bit(src++);
00380 }

static void mem_configuration_write ( U32  dst,
const void *  src,
size_t  nbytes 
) [static]

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

00384 {
00385   const U8 *source = src;
00386   while (nbytes--)
00387     flashc_set_gp_fuse_bit(dst++, *source++);
00388 }

static void mem_flash_read ( void *  dst,
U32  src,
size_t  nbytes 
) [static]

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

00347 {
00348   memcpy(dst, AVR32_FLASH + src, nbytes);
00349 }

static void mem_flash_write ( U32  dst,
const void *  src,
size_t  nbytes 
) [static]

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

00353 {
00354   flashc_memcpy(AVR32_FLASH + dst, src, nbytes, FALSE);
00355 }

static void mem_security_read ( void *  dst,
U32  src,
size_t  nbytes 
) [static]

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

00359 {
00360   if (nbytes)
00361     *(U8 *)dst = flashc_is_security_bit_active();
00362 }

static void mem_security_write ( U32  dst,
const void *  src,
size_t  nbytes 
) [static]

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

00366 {
00367   if (nbytes && *(U8 *)src)
00368   {
00369     security_active = TRUE;
00370     flashc_activate_security_bit();
00371   }
00372 }

static void mem_signature_read ( void *  dst,
U32  src,
size_t  nbytes 
) [static]

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

References PRODUCT_INF.

00398 {
00399   memcpy(dst, PRODUCT_INF + src, nbytes);
00400 }

static void mem_user_read ( void *  dst,
U32  src,
size_t  nbytes 
) [static]

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

00404 {
00405   memcpy(dst, (U8 *)AVR32_FLASHC_USER_PAGE + src, nbytes);
00406 }

static void mem_user_write ( U32  dst,
const void *  src,
size_t  nbytes 
) [static]

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

00410 {
00411   flashc_memcpy(AVR32_FLASHC_USER_PAGE + dst, src, nbytes, TRUE);
00412 }

static void read_mem ( void   )  [static]

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

References DFU_FRAME_LENGTH, address_t::long_address, and MEMORY_ACCESS.

Referenced by usb_dfu_upload().

00442 {
00443   void *frame;
00444   U32 frame_bytes;
00445 
00446   while (data_bytes)
00447   {
00448     frame = dfu_frame;
00449     frame_bytes = min(data_bytes, DFU_FRAME_LENGTH);
00450     data_bytes -= frame_bytes;
00451 
00452     MEMORY_ACCESS[memory].read(frame, start_address.long_address, frame_bytes);
00453     start_address.long_address += frame_bytes;
00454 
00455     while (frame_bytes)
00456     {
00457       while (!Is_usb_control_in_ready());
00458 
00459       Usb_reset_endpoint_fifo_access(EP_CONTROL);
00460       frame_bytes = usb_write_ep_txpacket(EP_CONTROL, frame, frame_bytes, (const void **)&frame);
00461       Usb_ack_control_in_ready_send();
00462     }
00463   }
00464 }

Bool usb_dfu_dnload ( void   ) 

Management of the DFU_DNLOAD requests.

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

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.

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 }

static void usb_dfu_stop ( void   )  [static]

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

References force_isp().

Referenced by usb_dfu_dnload().

00520 {
00521   force_isp(FALSE);
00522 
00523   Usb_ack_control_out_received_free();
00524   Usb_ack_control_in_ready_send();
00525 
00526   while (!Is_usb_setup_received());
00527   Usb_ack_setup_received_free();
00528   Usb_ack_control_in_ready_send();
00529 
00530   while (!Is_usb_control_in_ready());
00531 
00532   Disable_global_interrupt();
00533   Usb_disable();
00534   (void)Is_usb_enabled();
00535   Enable_global_interrupt();
00536   Usb_disable_otg_pad();
00537 }

Bool usb_dfu_upload ( void   ) 

Management of the DFU_UPLOAD requests.

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

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 }

static void write_mem ( void   )  [static]

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

References DFU_FRAME_LENGTH, EP_CONTROL_LENGTH, address_t::long_address, and MEMORY_ACCESS.

Referenced by usb_dfu_dnload().

00468 {
00469   void *frame;
00470   U32 frame_bytes, unaligned_frame_bytes;
00471 
00472   data_bytes += Get_align(start_address.long_address, EP_CONTROL_LENGTH);
00473   length -= EP_CONTROL_LENGTH + Align_up(data_bytes, EP_CONTROL_LENGTH);
00474 
00475   while (data_bytes)
00476   {
00477     frame = dfu_frame;
00478     frame_bytes = min(data_bytes, DFU_FRAME_LENGTH);
00479     unaligned_frame_bytes = frame_bytes - Get_align(start_address.long_address, EP_CONTROL_LENGTH);
00480     data_bytes -= frame_bytes;
00481 
00482     while (frame_bytes)
00483     {
00484       while (!Is_usb_control_out_received());
00485 
00486       Usb_reset_endpoint_fifo_access(EP_CONTROL);
00487       frame_bytes = usb_read_ep_rxpacket(EP_CONTROL, frame, frame_bytes, &frame);
00488       Usb_ack_control_out_received_free();
00489     }
00490 
00491     MEMORY_ACCESS[memory].write(start_address.long_address,
00492                                 dfu_frame + Get_align(start_address.long_address, EP_CONTROL_LENGTH),
00493                                 unaligned_frame_bytes);
00494     start_address.long_address += unaligned_frame_bytes;
00495   }
00496 
00497   while (length > 0)
00498   {
00499     while (!Is_usb_control_out_received());
00500     Usb_ack_control_out_received_free();
00501     length -= EP_CONTROL_LENGTH;
00502   }
00503 }


Variable Documentation

U8 cmd = CMD_START_APPLI [static]

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

U8 cmd_group = CMD_GRP_EXEC [static]

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

U32 data_bytes [static]

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

U8 dfu_frame[DFU_FRAME_LENGTH] [static]

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

U8 dfu_status = STATUS_OK [static]

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

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

const U8 ISP_INF[ISP_INF_ITEM_COUNT] [static]

Initial value:

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

Referenced by mem_bootloader_read().

S32 length [static]

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

U8 memory = MEM_FLASH [static]

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

struct { ... } MEMORY_ACCESS[MEM_COUNT] [static]

address_t MEMORY_END_ADDRESS[MEM_COUNT] [static]

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

const U8 MEMORY_ERASE_VALUE[MEM_COUNT] [static]

Initial value:

{
  
  0xFF,

  
  0xFF,

  
  0x00,

  
  0x01,

  
  0x00,

  
  0x00,

  
  0xFF
}

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

U8 PRODUCT_INF[PRODUCT_INF_ITEM_COUNT] [static]

Initial value:

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

Referenced by mem_signature_read(), and usb_dfu_init().

void(* read)(void *dst, U32 src, size_t nbytes)

Referenced by usb_dfu_dnload().

Bool security_active = TRUE [static]

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

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

U8 usb_dfu_state = STATE_dfuIDLE

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

Referenced by usb_user_read_request().

U8 usb_dfu_status = STATUS_OK

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

Referenced by usb_user_read_request().

void(* write)(U32 dst, const void *src, size_t nbytes)

Referenced by usb_dfu_dnload().


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