# flashcdw.h File Reference

FLASHCDW driver for AVR32 UC3. More...

`#include <avr32/io.h>`
`#include <stddef.h>`
`#include "compiler.h"`

Go to the source code of this file.

## Defines
#define | AVR32_FLASHCDW_FGPFRLO_SECURE_SSEN_SSDDIS 2
| Secure bits value when Secure State is enabled and Secure State Debug is disabled.
#define | AVR32_FLASHCDW_FGPFRLO_SECURE_SSEN_SSDEN 1
| Secure bits value when Secure State is enabled and Secure State Debug is enabled.
#define | AVR32_FLASHCDW_FWS_0_MAX_FREQ 15000000
| Device-specific data.
#define | AVR32_FLASHCDW_FWS_1_MAX_FREQ 30000000
#define | AVR32_FLASHCDW_HSEN_FWS_0_MAX_FREQ 25000000
#define | AVR32_FLASHCDW_HSEN_FWS_1_MAX_FREQ 50000000
#define | AVR32_FLASHCDW_REGIONS
| Number of flash regions defined by the FLASHCDW.
## Functions
FLASHCDW Protection Mechanisms
void | flashcdw_activate_security_bit (void)
| Activates the Security bit.
void | flashcdw_disable_jtag_user_protection (void)
| Disable the jtag user protection feature (i.e. erase (set to "1") the UPROT bit).
void | flashcdw_enable_jtag_user_protection (void)
| Enable the jtag user protection feature (i.e. program (set to "0") the UPROT bit).
void | flashcdw_enable_secure_state_no_debug (void)
| Enable the Secure State with the Secure State debug disabled.
void | flashcdw_enable_secure_state_with_debug (void)
| Enable the Secure State with the Secure State debug enabled.
unsigned int | flashcdw_get_bootloader_protected_size (void)
| Gets the bootloader protected size.
Bool | flashcdw_is_external_privileged_fetch_locked (void)
| Tells whether external privileged fetch is locked.
Bool | flashcdw_is_jtag_user_protection_enabled (void)
| Tells whether the jtag user protection is enabled.
Bool | flashcdw_is_page_region_locked (int page_number)
| Tells whether the region of a page is locked.
Bool | flashcdw_is_region_locked (unsigned int region)
| Tells whether a region is locked.
Bool | flashcdw_is_secure_state_debug_enabled (void)
| Tells whether the Secure State debug is enabled.
Bool | flashcdw_is_secure_state_enabled (void)
| Tells whether the Secure State is enabled.
Bool | flashcdw_is_security_bit_active (void)
| Tells whether the Security bit is active.
void | flashcdw_lock_all_regions (Bool lock)
| Locks or unlocks all regions.
void | flashcdw_lock_external_privileged_fetch (Bool lock)
| Locks or unlocks external privileged fetch.
void | flashcdw_lock_page_region (int page_number, Bool lock)
| Locks or unlocks the region of a page.
void | flashcdw_lock_region (unsigned int region, Bool lock)
| Locks or unlocks a region.
unsigned int | flashcdw_set_bootloader_protected_size (unsigned int bootprot_size)
| Sets the bootloader protected size.
FLASHCDW Control
void | flashcdw_enable_lock_error_int (Bool enable)
| Enables or disables the Lock Error interrupt.
void | flashcdw_enable_prog_error_int (Bool enable)
| Enables or disables the Programming Error interrupt.
void | flashcdw_enable_ready_int (Bool enable)
| Enables or disables the Flash Ready interrupt.
unsigned int | flashcdw_get_wait_state (void)
| Gets the number of wait states of flash read accesses.
Bool | flashcdw_is_lock_error_int_enabled (void)
| Tells whether the Lock Error interrupt is enabled.
Bool | flashcdw_is_prog_error_int_enabled (void)
| Tells whether the Programming Error interrupt is enabled.
Bool | flashcdw_is_ready_int_enabled (void)
| Tells whether the Flash Ready interrupt is enabled.
void | flashcdw_set_flash_waitstate_and_readmode (unsigned long cpu_f_hz)
| Depednding to the CPU frequency, set the wait states of flash read accesses and enable or disable the High speed read mode.
void | flashcdw_set_wait_state (unsigned int wait_state)
| Sets the number of wait states of flash read accesses.
FLASHCDW Global Commands
void | flashcdw_erase_all (void)
| Issues an Erase All command to the FLASHCDW.
void | flashcdw_no_operation (void)
| Issues a No Operation command to the FLASHCDW.
Access to General-Purpose Fuses
Bool | flashcdw_erase_all_gp_fuses (Bool check)
| Erases all general-purpose fuses.
Bool | flashcdw_erase_gp_fuse_bit (unsigned int gp_fuse_bit, Bool check)
| Erases a general-purpose fuse bit.
Bool | flashcdw_erase_gp_fuse_bitfield (unsigned int pos, unsigned int width, Bool check)
| Erases a general-purpose fuse bit-field.
Bool | flashcdw_erase_gp_fuse_byte (unsigned int gp_fuse_byte, Bool check)
| Erases a general-purpose fuse byte.
U64 | flashcdw_read_all_gp_fuses (void)
| Reads all general-purpose fuses.
Bool | flashcdw_read_gp_fuse_bit (unsigned int gp_fuse_bit)
| Reads a general-purpose fuse bit.
U64 | flashcdw_read_gp_fuse_bitfield (unsigned int pos, unsigned int width)
| Reads a general-purpose fuse bit-field.
U8 | flashcdw_read_gp_fuse_byte (unsigned int gp_fuse_byte)
| Reads a general-purpose fuse byte.
void | flashcdw_set_all_gp_fuses (U64 value)
| Sets all general-purpose fuses with the appropriate erase and write operations.
void | flashcdw_set_gp_fuse_bit (unsigned int gp_fuse_bit, Bool value)
| Sets a general-purpose fuse bit with the appropriate erase and write operations.
void | flashcdw_set_gp_fuse_bitfield (unsigned int pos, unsigned int width, U64 value)
| Sets a general-purpose fuse bit-field with the appropriate erase and write operations.
void | flashcdw_set_gp_fuse_byte (unsigned int gp_fuse_byte, U8 value)
| Sets a general-purpose fuse byte with the appropriate erase and write operations.
void | flashcdw_write_all_gp_fuses (U64 value)
| Writes all general-purpose fuses.
void | flashcdw_write_gp_fuse_bit (unsigned int gp_fuse_bit, Bool value)
| Writes a general-purpose fuse bit.
void | flashcdw_write_gp_fuse_bitfield (unsigned int pos, unsigned int width, U64 value)
| Writes a general-purpose fuse bit-field.
void | flashcdw_write_gp_fuse_byte (unsigned int gp_fuse_byte, U8 value)
| Writes a general-purpose fuse byte.
FLASHCDW Command Control
unsigned int | flashcdw_get_command (void)
| Gets the last issued FLASHCDW command.
unsigned int | flashcdw_get_page_number (void)
| Gets the current FLASHCDW page number.
void | flashcdw_issue_command (unsigned int command, int page_number)
| Issues a FLASHCDW command.
Flash Properties
unsigned int | flashcdw_get_flash_size (void)
| Gets the size of the whole flash array.
unsigned int | flashcdw_get_page_count (void)
| Gets the total number of pages in the flash array.
unsigned int | flashcdw_get_page_count_per_region (void)
| Gets the number of pages in each flash region.
unsigned int | flashcdw_get_page_region (int page_number)
| Gets the region number of a page.
unsigned int | flashcdw_get_region_first_page_number (unsigned int region)
| Gets the number of the first page of a region.
## Access to Flash Pages
#define | flashcdw_memset(dst, src, src_width, nbytes, erase) TPASTE2(flashcdw_memset, src_width)((dst), (src), (nbytes), (erase))
| Copies nbytes bytes to the flash destination pointed to by dst from the repeated src big-endian source pattern.
void | flashcdw_clear_page_buffer (void)
| Clears the page buffer.
Bool | flashcdw_erase_all_pages (Bool check)
| Erases all pages within the flash array.
Bool | flashcdw_erase_page (int page_number, Bool check)
| Erases a page.
Bool | flashcdw_erase_user_page (Bool check)
| Erases the User page.
Bool | flashcdw_is_page_erased (void)
| Tells whether the page to which the last Quick Page Read or Quick Page Read User Page command was applied was erased.
volatile void * | flashcdw_memcpy (volatile void *dst, const void *src, size_t nbytes, Bool erase)
| Copies nbytes bytes to the flash destination pointed to by dst from the source pointed to by src.
volatile void * | flashcdw_memset16 (volatile void *dst, U16 src, size_t nbytes, Bool erase)
| Copies nbytes bytes to the flash destination pointed to by dst from the repeated src big-endian source half-word.
volatile void * | flashcdw_memset32 (volatile void *dst, U32 src, size_t nbytes, Bool erase)
| Copies nbytes bytes to the flash destination pointed to by dst from the repeated src big-endian source word.
volatile void * | flashcdw_memset64 (volatile void *dst, U64 src, size_t nbytes, Bool erase)
| Copies nbytes bytes to the flash destination pointed to by dst from the repeated src big-endian source double-word.
volatile void * | flashcdw_memset8 (volatile void *dst, U8 src, size_t nbytes, Bool erase)
| Copies nbytes bytes to the flash destination pointed to by dst from the repeated src source byte.
Bool | flashcdw_quick_page_read (int page_number)
| Applies the Quick Page Read command to a page.
Bool | flashcdw_quick_user_page_read (void)
| Issues a Quick Page Read User Page command to the FLASHCDW.
void | flashcdw_write_page (int page_number)
| Writes a page from the page buffer.
void | flashcdw_write_user_page (void)
| Writes the User page from the page buffer.
## FLASHCDW Status
void(*volatile | flashcdw_wait_until_ready )(void)
| Pointer to the function used by the driver when it needs to wait until the FLASHCDW is ready to run a new command.
void | flashcdw_default_wait_until_ready (void)
| Waits actively until the FLASHCDW is ready to run a new command.
Bool | flashcdw_is_high_speed_enabled (void)
| Tells whether the High-speed read mode is enabled.
Bool | flashcdw_is_lock_error (void)
| Tells whether a Lock Error has occurred during the last function called that issued one or more FLASHCDW commands.
Bool | flashcdw_is_programming_error (void)
| Tells whether a Programming Error has occurred during the last function called that issued one or more FLASHCDW commands.
Bool | flashcdw_is_ready (void)
| Tells whether the FLASHCDW is ready to run a new command.

## Detailed Description

FLASHCDW driver for AVR32 UC3.

AVR32 Flash Controller driver module.

- Compiler: IAR EWAVR32 and GNU GCC for AVR32
- Supported devices: All AVR32 devices with a FLASHCDW module can be used.
- AppNote:

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

Definition in file flashcdw.h.

## Define Documentation

#define AVR32_FLASHCDW_FGPFRLO_SECURE_SSEN_SSDDIS 2

Secure bits value when Secure State is enabled and Secure State Debug is disabled.

Definition at line 93 of file flashcdw.h.

Referenced by flashcdw_enable_secure_state_no_debug(), and flashcdw_is_secure_state_enabled().

#define AVR32_FLASHCDW_FGPFRLO_SECURE_SSEN_SSDEN 1

Secure bits value when Secure State is enabled and Secure State Debug is enabled.

Definition at line 96 of file flashcdw.h.

Referenced by flashcdw_enable_secure_state_with_debug(), flashcdw_is_secure_state_debug_enabled(), and flashcdw_is_secure_state_enabled().

#define AVR32_FLASHCDW_FWS_0_MAX_FREQ 15000000

Device-specific data.

Definition at line 79 of file flashcdw.h.

Referenced by flashcdw_set_flash_waitstate_and_readmode().

#define AVR32_FLASHCDW_FWS_1_MAX_FREQ 30000000

Definition at line 80 of file flashcdw.h.

Referenced by flashcdw_set_flash_waitstate_and_readmode().

#define AVR32_FLASHCDW_HSEN_FWS_0_MAX_FREQ 25000000

Definition at line 81 of file flashcdw.h.

#define AVR32_FLASHCDW_HSEN_FWS_1_MAX_FREQ 50000000

Definition at line 82 of file flashcdw.h.

#define AVR32_FLASHCDW_REGIONS

Value:

    (AVR32_FLASHCDW_FLASH_SIZE /\
                                   (AVR32_FLASHCDW_PAGES_PR_REGION * AVR32_FLASHCDW_PAGE_SIZE))

Number of flash regions defined by the FLASHCDW.

Definition at line 89 of file flashcdw.h.

Referenced by flashcdw_get_page_count_per_region(), flashcdw_is_region_locked(), and flashcdw_lock_all_regions().

#define flashcdw_memset | ( | dst,
|  | src,
|  | src_width,
|  | nbytes,
|  | erase |  | ) | TPASTE2(flashcdw_memset, src_width)((dst), (src), (nbytes), (erase))

Copies nbytes bytes to the flash destination pointed to by dst from the repeated src big-endian source pattern.

The destination areas that are not within the flash array or the User page are ignored.

All pointer and size alignments are supported.

Parameters:

  | dst | Pointer to flash destination.
  | src | Source double-word.
  | src_width | src width in bits: 8, 16, 32 or 64.
  | nbytes | Number of bytes to set.
  | erase | Whether to erase before writing: `TRUE` or `FALSE`.

Returns:
  The value of dst.

Warning:
  This function may be called with erase set to `FALSE` only if the destination consists only of erased words, i.e. this function can not be used to write only one bit of a previously written word. E.g., if `0x00000001` then `0xFFFFFFFE` are written to a word, the resulting value in flash may be different from `0x00000000`.
  A Lock Error is issued if the command is applied to pages belonging to a locked region or to the bootloader protected area or to a secure area requiring secure privileges.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.

Definition at line 1077 of file flashcdw.h.

## Function Documentation

void flashcdw_activate_security_bit | ( | void |  | ) |

Activates the Security bit.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.

Definition at line 349 of file flashcdw.c.

References flashcdw_issue_command().

    00350 {
    00351   flashcdw_issue_command(AVR32_FLASHCDW_FCMD_CMD_SSB, -1);
    00352 }

void flashcdw_clear_page_buffer | ( | void |  | ) |

Clears the page buffer.

This command resets all bits in the page buffer to one. Write accesses to the page buffer can only change page buffer bits from one to zero.

Warning:
  The page buffer is not automatically reset after a page write.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.

Definition at line 669 of file flashcdw.c.

References flashcdw_issue_command().

Referenced by flashcdw_memcpy(), and flashcdw_memset64().

    00670 {
    00671   flashcdw_issue_command(AVR32_FLASHCDW_FCMD_CMD_CPB, -1);
    00672 }

void flashcdw_default_wait_until_ready | ( | void |  | ) |

Waits actively until the FLASHCDW is ready to run a new command.

This is the default function assigned to flashcdw_wait_until_ready.

Definition at line 230 of file flashcdw.c.

References flashcdw_is_ready().

    00231 {
    00232   while (!flashcdw_is_ready());
    00233 }

void flashcdw_disable_jtag_user_protection | ( | void |  | ) |

Disable the jtag user protection feature (i.e. erase (set to "1") the UPROT bit).

Warning:
  A Lock Error is issued if the Security bit is active.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.

Definition at line 401 of file flashcdw.c.

References flashcdw_set_gp_fuse_bit(), and TRUE.

    00402 {
    00403   flashcdw_set_gp_fuse_bit(AVR32_FLASHCDW_FGPFRLO_UPROT_OFFSET, TRUE);
    00404 }

void flashcdw_enable_jtag_user_protection | ( | void |  | ) |

Enable the jtag user protection feature (i.e. program (set to "0") the UPROT bit).

Warning:
  A Lock Error is issued if the Security bit is active.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.

Definition at line 396 of file flashcdw.c.

References FALSE, and flashcdw_set_gp_fuse_bit().

    00397 {
    00398   flashcdw_set_gp_fuse_bit(AVR32_FLASHCDW_FGPFRLO_UPROT_OFFSET, FALSE);
    00399 }

void flashcdw_enable_lock_error_int | ( | Bool | enable | ) |

Enables or disables the Lock Error interrupt.

Parameters:

  | enable | Whether to enable the Lock Error interrupt: `TRUE` or `FALSE`.

Definition at line 194 of file flashcdw.c.

References FALSE, u_avr32_flashcdw_fcr_t::fcr, and u_avr32_flashcdw_fcr_t::FCR.

    00195 {
    00196   u_avr32_flashcdw_fcr_t u_avr32_flashcdw_fcr = {AVR32_FLASHCDW.fcr};
    00197   u_avr32_flashcdw_fcr.FCR.locke = (enable != FALSE);
    00198   AVR32_FLASHCDW.fcr = u_avr32_flashcdw_fcr.fcr;
    00199 }

void flashcdw_enable_prog_error_int | ( | Bool | enable | ) |

Enables or disables the Programming Error interrupt.

Parameters:

  | enable | Whether to enable the Programming Error interrupt: `TRUE` or `FALSE`.

Definition at line 208 of file flashcdw.c.

References FALSE, u_avr32_flashcdw_fcr_t::fcr, and u_avr32_flashcdw_fcr_t::FCR.

    00209 {
    00210   u_avr32_flashcdw_fcr_t u_avr32_flashcdw_fcr = {AVR32_FLASHCDW.fcr};
    00211   u_avr32_flashcdw_fcr.FCR.proge = (enable != FALSE);
    00212   AVR32_FLASHCDW.fcr = u_avr32_flashcdw_fcr.fcr;
    00213 }

void flashcdw_enable_ready_int | ( | Bool | enable | ) |

Enables or disables the Flash Ready interrupt.

Parameters:

  | enable | Whether to enable the Flash Ready interrupt: `TRUE` or `FALSE`.

Definition at line 180 of file flashcdw.c.

References FALSE, u_avr32_flashcdw_fcr_t::fcr, and u_avr32_flashcdw_fcr_t::FCR.

    00181 {
    00182   u_avr32_flashcdw_fcr_t u_avr32_flashcdw_fcr = {AVR32_FLASHCDW.fcr};
    00183   u_avr32_flashcdw_fcr.FCR.frdy = (enable != FALSE);
    00184   AVR32_FLASHCDW.fcr = u_avr32_flashcdw_fcr.fcr;
    00185 }

void flashcdw_enable_secure_state_no_debug | ( | void |  | ) |

Enable the Secure State with the Secure State debug disabled.

Warning:
  A Lock Error is issued if the SECURE mode is active.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.

Definition at line 423 of file flashcdw.c.

References AVR32_FLASHCDW_FGPFRLO_SECURE_SSEN_SSDDIS, and flashcdw_write_gp_fuse_bitfield().

    00424 {
    00425   flashcdw_write_gp_fuse_bitfield(AVR32_FLASHCDW_FGPFRLO_SECURE_OFFSET,
    00426                                   AVR32_FLASHCDW_FGPFRLO_SECURE_SIZE,
    00427                                   AVR32_FLASHCDW_FGPFRLO_SECURE_SSEN_SSDDIS);
    00428 }

void flashcdw_enable_secure_state_with_debug | ( | void |  | ) |

Enable the Secure State with the Secure State debug enabled.

Warning:
  A Lock Error is issued if the SECURE mode is active.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.

Definition at line 430 of file flashcdw.c.

References AVR32_FLASHCDW_FGPFRLO_SECURE_SSEN_SSDEN, and flashcdw_write_gp_fuse_bitfield().

    00431 {
    00432   flashcdw_write_gp_fuse_bitfield(AVR32_FLASHCDW_FGPFRLO_SECURE_OFFSET,
    00433                                   AVR32_FLASHCDW_FGPFRLO_SECURE_SIZE,
    00434                                   AVR32_FLASHCDW_FGPFRLO_SECURE_SSEN_SSDEN);
    00435 }

void flashcdw_erase_all | ( | void |  | ) |

Issues an Erase All command to the FLASHCDW.

This command erases all bits in the flash array, the general-purpose fuse bits and the Security bit. The User page is not erased.

This command also ensures that all volatile memories, such as register file and RAMs, are erased before the Security bit is erased, i.e. deactivated.

Warning:
  A Lock Error is issued if at least one region is locked or the bootloader protection is active.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.
  An erase operation can only set bits.

Definition at line 329 of file flashcdw.c.

References flashcdw_issue_command().

    00330 {
    00331   flashcdw_issue_command(AVR32_FLASHCDW_FCMD_CMD_EA, -1);
    00332 }

Bool flashcdw_erase_all_gp_fuses | ( | Bool | check | ) |

Erases all general-purpose fuses.

Parameters:

  | check | Whether to check erase: `TRUE` or `FALSE`.

Returns:
  Whether the erase succeeded or always `TRUE` if erase check was not requested.

Warning:
  A Lock Error is issued if the Security bit is active or if the SECURE mode is active.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.
  An erase operation can only set bits.
  The actual number of general-purpose fuse bits implemented by hardware is given by `AVR32_FLASHCDW_GPF_NUM`. The other bits among the 64 are fixed at 1 by hardware.

Definition at line 549 of file flashcdw.c.

References flashcdw_issue_command(), flashcdw_read_all_gp_fuses(), and TRUE.

Referenced by flashcdw_erase_gp_fuse_byte(), and flashcdw_set_all_gp_fuses().

    00550 {
    00551   flashcdw_issue_command(AVR32_FLASHCDW_FCMD_CMD_EAGPF, -1);
    00552   return (check) ? (flashcdw_read_all_gp_fuses() == 0xFFFFFFFFFFFFFFFFULL) : TRUE;
    00553 }

Bool flashcdw_erase_all_pages | ( | Bool | check | ) |

Erases all pages within the flash array.

Parameters:

  | check | Whether to check erase: `TRUE` or `FALSE`.

Returns:
  Whether the erase succeeded or always `TRUE` if erase check was not requested.

Warning:
  A Lock Error is issued if at least one region is locked or the bootloader protection is active or the SECURE mode is active.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.
  An erase operation can only set bits.

Definition at line 702 of file flashcdw.c.

References flashcdw_erase_page(), flashcdw_error_status, flashcdw_get_page_count(), and TRUE.

    00703 {
    00704   Bool all_pages_erased = TRUE;
    00705   unsigned int error_status = 0;
    00706   unsigned int page_number = flashcdw_get_page_count();
    00707   while (page_number)
    00708   {
    00709     all_pages_erased &= flashcdw_erase_page(--page_number, check);
    00710     error_status |= flashcdw_error_status;
    00711   }
    00712   flashcdw_error_status = error_status;
    00713   return all_pages_erased;
    00714 }

Bool flashcdw_erase_gp_fuse_bit | ( | unsigned int | gp_fuse_bit,
|  | Bool | check |
| ) |  |  |

Erases a general-purpose fuse bit.

Parameters:

  | gp_fuse_bit | The general-purpose fuse bit: `0` to `63`.
  | check | Whether to check erase: `TRUE` or `FALSE`.

Returns:
  Whether the erase succeeded or always `TRUE` if erase check was not requested.

Warning:
  A Lock Error is issued:
  - if the Security bit is active and the command is applied to BOOTPROT or EPFL or UPROT fuses,
  - if the SECURE mode is active and the command is applied to the SECURE bits.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.
  An erase operation can only set bits.
  The actual number of general-purpose fuse bits implemented by hardware is given by `AVR32_FLASHCDW_GPF_NUM`. The other bits among the 64 are fixed at 1 by hardware.

Definition at line 506 of file flashcdw.c.

References flashcdw_issue_command(), flashcdw_read_gp_fuse_bit(), and TRUE.

Referenced by flashcdw_erase_gp_fuse_bitfield(), and flashcdw_set_gp_fuse_bit().

    00507 {
    00508   flashcdw_issue_command(AVR32_FLASHCDW_FCMD_CMD_EGPB, gp_fuse_bit & 0x3F);
    00509   return (check) ? flashcdw_read_gp_fuse_bit(gp_fuse_bit) : TRUE;
    00510 }

Bool flashcdw_erase_gp_fuse_bitfield | ( | unsigned int | pos,
|  | unsigned int | width,
|  | Bool | check |
| ) |  |  |

Erases a general-purpose fuse bit-field.

Parameters:

  | pos | The bit-position of the general-purpose fuse bit-field: `0` to `63`.
  | width | The bit-width of the general-purpose fuse bit-field: `0` to `64`.
  | check | Whether to check erase: `TRUE` or `FALSE`.

Returns:
  Whether the erase succeeded or always `TRUE` if erase check was not requested.

Warning:
  A Lock Error is issued:
  - if the Security bit is active and the command is applied to BOOTPROT or EPFL or UPROT fuses,
  - if the SECURE mode is active and the command is applied to the SECURE bits.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.
  An erase operation can only set bits.
  The actual number of general-purpose fuse bits implemented by hardware is given by `AVR32_FLASHCDW_GPF_NUM`. The other bits among the 64 are fixed at 1 by hardware.

Definition at line 513 of file flashcdw.c.

References FALSE, flashcdw_erase_gp_fuse_bit(), flashcdw_error_status, flashcdw_read_gp_fuse_bitfield(), min, and TRUE.

    00514 {
    00515   unsigned int error_status = 0;
    00516   unsigned int gp_fuse_bit;
    00517   pos &= 0x3F;
    00518   width = min(width, 64);
    00519   for (gp_fuse_bit = pos; gp_fuse_bit < pos + width; gp_fuse_bit++)
    00520   {
    00521     flashcdw_erase_gp_fuse_bit(gp_fuse_bit, FALSE);
    00522     error_status |= flashcdw_error_status;
    00523   }
    00524   flashcdw_error_status = error_status;
    00525   return (check) ? (flashcdw_read_gp_fuse_bitfield(pos, width) == (1ULL << width) - 1) : TRUE;
    00526 }

Bool flashcdw_erase_gp_fuse_byte | ( | unsigned int | gp_fuse_byte,
|  | Bool | check |
| ) |  |  |

Erases a general-purpose fuse byte.

Parameters:

  | gp_fuse_byte | The general-purpose fuse byte: `0` to `7`.
  | check | Whether to check erase: `TRUE` or `FALSE`.

Returns:
  Whether the erase succeeded or always `TRUE` if erase check was not requested.

Warning:
  A Lock Error is issued if the Security bit is active or if the SECURE mode is active and the SECURE bits are in the target fuse byte.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.
  An erase operation can only set bits.
  The actual number of general-purpose fuse bits implemented by hardware is given by `AVR32_FLASHCDW_GPF_NUM`. The other bits among the 64 are fixed at 1 by hardware.

Definition at line 529 of file flashcdw.c.

References FALSE, flashcdw_erase_all_gp_fuses(), flashcdw_error_status, flashcdw_read_all_gp_fuses(), flashcdw_read_gp_fuse_byte(), flashcdw_write_gp_fuse_byte(), and TRUE.

Referenced by flashcdw_set_gp_fuse_byte().

    00530 {
    00531   unsigned int error_status;
    00532   unsigned int current_gp_fuse_byte;
    00533   U64 value = flashcdw_read_all_gp_fuses();
    00534   flashcdw_erase_all_gp_fuses(FALSE);
    00535   error_status = flashcdw_error_status;
    00536   for (current_gp_fuse_byte = 0; current_gp_fuse_byte < 8; current_gp_fuse_byte++, value >>= 8)
    00537   {
    00538     if (current_gp_fuse_byte != gp_fuse_byte)
    00539     {
    00540       flashcdw_write_gp_fuse_byte(current_gp_fuse_byte, value);
    00541       error_status |= flashcdw_error_status;
    00542     }
    00543   }
    00544   flashcdw_error_status = error_status;
    00545   return (check) ? (flashcdw_read_gp_fuse_byte(gp_fuse_byte) == 0xFF) : TRUE;
    00546 }

Bool flashcdw_erase_page | ( | int | page_number,
|  | Bool | check |
| ) |  |  |

Erases a page.

Parameters:

  | page_number | The page number: `0` to `(flashcdw_get_page_count() - 1)`: a page number within the flash array; `< 0`: the current page number.
  | check | Whether to check erase: `TRUE` or `FALSE`.

Returns:
  Whether the erase succeeded or always `TRUE` if erase check was not requested.

Warning:
  A Lock Error is issued if the command is applied to a page belonging to a locked region or to the bootloader protected area or to a secure area requiring secure privileges.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.
  An erase operation can only set bits.

Definition at line 688 of file flashcdw.c.

References flashcdw_error_status, flashcdw_issue_command(), flashcdw_quick_page_read(), and TRUE.

Referenced by flashcdw_erase_all_pages(), flashcdw_memcpy(), and flashcdw_memset64().

    00689 {
    00690   Bool page_erased = TRUE;
    00691   flashcdw_issue_command(AVR32_FLASHCDW_FCMD_CMD_EP, page_number);
    00692   if (check)
    00693   {
    00694     unsigned int error_status = flashcdw_error_status;
    00695     page_erased = flashcdw_quick_page_read(-1);
    00696     flashcdw_error_status |= error_status;
    00697   }
    00698   return page_erased;
    00699 }

Bool flashcdw_erase_user_page | ( | Bool | check | ) |

Erases the User page.

Parameters:

  | check | Whether to check erase: `TRUE` or `FALSE`.

Returns:
  Whether the erase succeeded or always `TRUE` if erase check was not requested.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.
  An erase operation can only set bits.

Definition at line 730 of file flashcdw.c.

References flashcdw_issue_command(), flashcdw_quick_user_page_read(), and TRUE.

Referenced by flashcdw_memcpy(), and flashcdw_memset64().

    00731 {
    00732   flashcdw_issue_command(AVR32_FLASHCDW_FCMD_CMD_EUP, -1);
    00733   return (check) ? flashcdw_quick_user_page_read() : TRUE;
    00734 }

unsigned int flashcdw_get_bootloader_protected_size | ( | void |  | ) |

Gets the bootloader protected size.

Returns:
  The bootloader protected size in bytes.

Definition at line 355 of file flashcdw.c.

References flashcdw_read_gp_fuse_bitfield().

Referenced by flashcdw_set_bootloader_protected_size().

    00356 {
    00357   unsigned int bootprot = (1 << AVR32_FLASHCDW_FGPFRLO_BOOTPROT_SIZE) - 1 -
    00358                           flashcdw_read_gp_fuse_bitfield(AVR32_FLASHCDW_FGPFRLO_BOOTPROT_OFFSET,
    00359                                                        AVR32_FLASHCDW_FGPFRLO_BOOTPROT_SIZE);
    00360   return (bootprot) ? AVR32_FLASHCDW_PAGE_SIZE << bootprot : 0;
    00361 }

unsigned int flashcdw_get_command | ( | void |  | ) |

Gets the last issued FLASHCDW command.

Returns:
  The last issued FLASHCDW command.

Definition at line 289 of file flashcdw.c.

    00290 {
    00291   return (AVR32_FLASHCDW.fcmd & AVR32_FLASHCDW_FCMD_CMD_MASK) >> AVR32_FLASHCDW_FCMD_CMD_OFFSET;
    00292 }

unsigned int flashcdw_get_flash_size | ( | void |  | ) |

Gets the size of the whole flash array.

Returns:
  The size of the whole flash array in bytes.

Definition at line 78 of file flashcdw.c.

Referenced by flashcdw_get_page_count(), flashcdw_memcpy(), and flashcdw_memset64().

    00079 {
    00080   static const unsigned int FLASH_SIZE[1 << AVR32_FLASHCDW_FPR_FSZ_SIZE] =
    00081   {
    00082       4 << 10,
    00083       8 << 10,
    00084       16 << 10,
    00085       32 << 10,
    00086       48 << 10,
    00087       64 << 10,
    00088       96 << 10,
    00089      128 << 10,
    00090      192 << 10,
    00091      256 << 10,
    00092      384 << 10,
    00093      512 << 10,
    00094      768 << 10,
    00095     1024 << 10,
    00096     2048 << 10
    00097   };
    00098   return FLASH_SIZE[(AVR32_FLASHCDW.fpr & AVR32_FLASHCDW_FPR_FSZ_MASK) >> AVR32_FLASHCDW_FPR_FSZ_OFFSET];
    00099 }

unsigned int flashcdw_get_page_count | ( | void |  | ) |

Gets the total number of pages in the flash array.

Returns:
  The total number of pages in the flash array.

Definition at line 102 of file flashcdw.c.

References flashcdw_get_flash_size().

Referenced by flashcdw_erase_all_pages(), and flashcdw_get_page_count_per_region().

    00103 {
    00104   return flashcdw_get_flash_size() / AVR32_FLASHCDW_PAGE_SIZE;
    00105 }

unsigned int flashcdw_get_page_count_per_region | ( | void |  | ) |

Gets the number of pages in each flash region.

Returns:
  The number of pages in each flash region.

Definition at line 108 of file flashcdw.c.

References AVR32_FLASHCDW_REGIONS, and flashcdw_get_page_count().

Referenced by flashcdw_get_page_region(), and flashcdw_get_region_first_page_number().

    00109 {
    00110   return flashcdw_get_page_count() / AVR32_FLASHCDW_REGIONS;
    00111 }

unsigned int flashcdw_get_page_number | ( | void |  | ) |

Gets the current FLASHCDW page number.

Returns:
  The current FLASHCDW page number.

Definition at line 295 of file flashcdw.c.

Referenced by flashcdw_get_page_region().

    00296 {
    00297   return (AVR32_FLASHCDW.fcmd & AVR32_FLASHCDW_FCMD_PAGEN_MASK) >> AVR32_FLASHCDW_FCMD_PAGEN_OFFSET;
    00298 }

unsigned int flashcdw_get_page_region | ( | int | page_number | ) |

Gets the region number of a page.

Parameters:

  | page_number | The page number: `0` to `(flashcdw_get_page_count() - 1)`: a page number within the flash array; `< 0`: the current page number.

Returns:
  The region number of the specified page.

Definition at line 114 of file flashcdw.c.

References flashcdw_get_page_count_per_region(), and flashcdw_get_page_number().

Referenced by flashcdw_is_page_region_locked().

    00115 {
    00116   return ((page_number >= 0) ? page_number : flashcdw_get_page_number()) / flashcdw_get_page_count_per_region();
    00117 }

unsigned int flashcdw_get_region_first_page_number | ( | unsigned int | region | ) |

Gets the number of the first page of a region.

Parameters:

  | region | The region number: `0` to `(AVR32_FLASHCDW_REGIONS - 1)`.

Returns:
  The number of the first page of the specified region.

Definition at line 120 of file flashcdw.c.

References flashcdw_get_page_count_per_region().

Referenced by flashcdw_lock_region().

    00121 {
    00122   return region * flashcdw_get_page_count_per_region();
    00123 }

unsigned int flashcdw_get_wait_state | ( | void |  | ) |

Gets the number of wait states of flash read accesses.

Returns:
  The number of wait states of flash read accesses.

Definition at line 134 of file flashcdw.c.

    00135 {
    00136   return (AVR32_FLASHCDW.fcr & AVR32_FLASHCDW_FCR_FWS_MASK) >> AVR32_FLASHCDW_FCR_FWS_OFFSET;
    00137 }

Bool flashcdw_is_external_privileged_fetch_locked | ( | void |  | ) |

Tells whether external privileged fetch is locked.

Returns:
  Whether external privileged fetch is locked.

Definition at line 380 of file flashcdw.c.

References flashcdw_read_gp_fuse_bit().

    00381 {
    00382   return (!flashcdw_read_gp_fuse_bit(AVR32_FLASHCDW_FGPFRLO_EPFL_OFFSET));
    00383 }

Bool flashcdw_is_high_speed_enabled | ( | void |  | ) |

Tells whether the High-speed read mode is enabled.

Returns:
  1 if the high-speed read mode is enabled, else 0.

Definition at line 275 of file flashcdw.c.

    00276 {
    00277   return ((AVR32_FLASHCDW.fsr & AVR32_FLASHCDW_FSR_HSMODE_MASK) != 0);
    00278 }

Bool flashcdw_is_jtag_user_protection_enabled | ( | void |  | ) |

Tells whether the jtag user protection is enabled.

Returns:
  Whether the jtag user protection is enabled.

Definition at line 391 of file flashcdw.c.

References flashcdw_read_gp_fuse_bit().

    00392 {
    00393   return (!flashcdw_read_gp_fuse_bit(AVR32_FLASHCDW_FGPFRLO_UPROT_OFFSET));
    00394 }

Bool flashcdw_is_lock_error | ( | void |  | ) |

Tells whether a Lock Error has occurred during the last function called that issued one or more FLASHCDW commands.

Returns:
  Whether a Lock Error has occurred during the last function called that issued one or more FLASHCDW commands.

Definition at line 263 of file flashcdw.c.

References flashcdw_error_status.

    00264 {
    00265   return ((flashcdw_error_status & AVR32_FLASHCDW_FSR_LOCKE_MASK) != 0);
    00266 }

Bool flashcdw_is_lock_error_int_enabled | ( | void |  | ) |

Tells whether the Lock Error interrupt is enabled.

Returns:
  Whether the Lock Error interrupt is enabled.

Definition at line 188 of file flashcdw.c.

    00189 {
    00190   return ((AVR32_FLASHCDW.fcr & AVR32_FLASHCDW_FCR_LOCKE_MASK) != 0);
    00191 }

Bool flashcdw_is_page_erased | ( | void |  | ) |

Tells whether the page to which the last Quick Page Read or Quick Page Read User Page command was applied was erased.

Returns:
  Whether the page to which the last Quick Page Read or Quick Page Read User Page command was applied was erased.

Definition at line 675 of file flashcdw.c.

Referenced by flashcdw_quick_page_read(), and flashcdw_quick_user_page_read().

    00676 {
    00677   return ((AVR32_FLASHCDW.fsr & AVR32_FLASHCDW_FSR_QPRR_MASK) != 0);
    00678 }

Bool flashcdw_is_page_region_locked | ( | int | page_number | ) |

Tells whether the region of a page is locked.

Parameters:

  | page_number | The page number: `0` to `(flashcdw_get_page_count() - 1)`: a page number within the flash array; `< 0`: the current page number.

Returns:
  Whether the region of the specified page is locked.

Definition at line 437 of file flashcdw.c.

References flashcdw_get_page_region(), and flashcdw_is_region_locked().

    00438 {
    00439   return flashcdw_is_region_locked(flashcdw_get_page_region(page_number));
    00440 }

Bool flashcdw_is_prog_error_int_enabled | ( | void |  | ) |

Tells whether the Programming Error interrupt is enabled.

Returns:
  Whether the Programming Error interrupt is enabled.

Definition at line 202 of file flashcdw.c.

    00203 {
    00204   return ((AVR32_FLASHCDW.fcr & AVR32_FLASHCDW_FCR_PROGE_MASK) != 0);
    00205 }

Bool flashcdw_is_programming_error | ( | void |  | ) |

Tells whether a Programming Error has occurred during the last function called that issued one or more FLASHCDW commands.

Returns:
  Whether a Programming Error has occurred during the last function called that issued one or more FLASHCDW commands.

Definition at line 269 of file flashcdw.c.

References flashcdw_error_status.

    00270 {
    00271   return ((flashcdw_error_status & AVR32_FLASHCDW_FSR_PROGE_MASK) != 0);
    00272 }

Bool flashcdw_is_ready | ( | void |  | ) |

Tells whether the FLASHCDW is ready to run a new command.

Returns:
  Whether the FLASHCDW is ready to run a new command.

Definition at line 224 of file flashcdw.c.

Referenced by flashcdw_default_wait_until_ready().

    00225 {
    00226   return ((AVR32_FLASHCDW.fsr & AVR32_FLASHCDW_FSR_FRDY_MASK) != 0);
    00227 }

Bool flashcdw_is_ready_int_enabled | ( | void |  | ) |

Tells whether the Flash Ready interrupt is enabled.

Returns:
  Whether the Flash Ready interrupt is enabled.

Definition at line 174 of file flashcdw.c.

    00175 {
    00176   return ((AVR32_FLASHCDW.fcr & AVR32_FLASHCDW_FCR_FRDY_MASK) != 0);
    00177 }

Bool flashcdw_is_region_locked | ( | unsigned int | region | ) |

Tells whether a region is locked.

Parameters:

  | region | The region number: `0` to `(AVR32_FLASHCDW_REGIONS - 1)`.

Returns:
  Whether the specified region is locked.

Definition at line 443 of file flashcdw.c.

References AVR32_FLASHCDW_REGIONS.

Referenced by flashcdw_is_page_region_locked().

    00444 {
    00445   return ((AVR32_FLASHCDW.fsr & AVR32_FLASHCDW_FSR_LOCK0_MASK << (region & (AVR32_FLASHCDW_REGIONS - 1))) != 0);
    00446 }

Bool flashcdw_is_secure_state_debug_enabled | ( | void |  | ) |

Tells whether the Secure State debug is enabled.

Returns:
  Whether the Secure State debug is enabled.

Definition at line 416 of file flashcdw.c.

References AVR32_FLASHCDW_FGPFRLO_SECURE_SSEN_SSDEN, and flashcdw_read_gp_fuse_bitfield().

    00417 {
    00418   return( AVR32_FLASHCDW_FGPFRLO_SECURE_SSEN_SSDEN
    00419           == flashcdw_read_gp_fuse_bitfield(AVR32_FLASHCDW_FGPFRLO_SECURE_OFFSET,
    00420                                             AVR32_FLASHCDW_FGPFRLO_SECURE_SIZE) );
    00421 }

Bool flashcdw_is_secure_state_enabled | ( | void |  | ) |

Tells whether the Secure State is enabled.

Returns:
  Whether the Secure State is enabled.

Definition at line 406 of file flashcdw.c.

References AVR32_FLASHCDW_FGPFRLO_SECURE_SSEN_SSDDIS, AVR32_FLASHCDW_FGPFRLO_SECURE_SSEN_SSDEN, FALSE, flashcdw_read_gp_fuse_bitfield(), and TRUE.

    00407 {
    00408   U64 temp = flashcdw_read_gp_fuse_bitfield(AVR32_FLASHCDW_FGPFRLO_SECURE_OFFSET,
    00409                                             AVR32_FLASHCDW_FGPFRLO_SECURE_SIZE);
    00410   if((AVR32_FLASHCDW_FGPFRLO_SECURE_SSEN_SSDDIS == temp) || (AVR32_FLASHCDW_FGPFRLO_SECURE_SSEN_SSDEN == temp))
    00411     return TRUE;
    00412   else
    00413     return FALSE;
    00414 }

Bool flashcdw_is_security_bit_active | ( | void |  | ) |

Tells whether the Security bit is active.

Returns:
  Whether the Security bit is active.

Definition at line 343 of file flashcdw.c.

    00344 {
    00345   return ((AVR32_FLASHCDW.fsr & AVR32_FLASHCDW_FSR_SECURITY_MASK) != 0);
    00346 }

void flashcdw_issue_command | ( | unsigned int | command,
|  | int | page_number |
| ) |  |  |

Issues a FLASHCDW command.

Parameters:

  | command | The command: `AVR32_FLASHCDW_FCMD_CMD_x`.
  | page_number | The page number to apply the command to: `0` to `(flashcdw_get_page_count() - 1)`: a page number within the flash array; `< 0`: use this to apply the command to the current page number or if the command does not apply to any page number; this argument may have other meanings according to the command. See the FLASHCDW chapter of the MCU datasheet.

Warning:
  A Lock Error is issued if the command violates the protection mechanism.
  A Programming Error is issued if the command is invalid.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.

Definition at line 301 of file flashcdw.c.

References u_avr32_flashcdw_fcmd_t::FCMD, u_avr32_flashcdw_fcmd_t::fcmd, flashcdw_error_status, flashcdw_get_error_status(), and flashcdw_wait_until_ready.

Referenced by flashcdw_activate_security_bit(), flashcdw_clear_page_buffer(), flashcdw_erase_all(), flashcdw_erase_all_gp_fuses(), flashcdw_erase_gp_fuse_bit(), flashcdw_erase_page(), flashcdw_erase_user_page(), flashcdw_lock_page_region(), flashcdw_no_operation(), flashcdw_quick_page_read(), flashcdw_quick_user_page_read(), flashcdw_set_flash_waitstate_and_readmode(), flashcdw_write_gp_fuse_bit(), flashcdw_write_gp_fuse_byte(), flashcdw_write_page(), and flashcdw_write_user_page().

    00302 {
    00303   u_avr32_flashcdw_fcmd_t u_avr32_flashcdw_fcmd;
    00304   flashcdw_wait_until_ready();
    00305   u_avr32_flashcdw_fcmd.fcmd = AVR32_FLASHCDW.fcmd;
    00306   u_avr32_flashcdw_fcmd.FCMD.cmd = command;
    00307   if (page_number >= 0) u_avr32_flashcdw_fcmd.FCMD.pagen = page_number;
    00308   u_avr32_flashcdw_fcmd.FCMD.key = AVR32_FLASHCDW_FCMD_KEY_KEY;
    00309   AVR32_FLASHCDW.fcmd = u_avr32_flashcdw_fcmd.fcmd;
    00310   flashcdw_error_status = flashcdw_get_error_status();
    00311   flashcdw_wait_until_ready();
    00312 }

void flashcdw_lock_all_regions | ( | Bool | lock | ) |

Locks or unlocks all regions.

Parameters:

  | lock | Whether to lock the regions: `TRUE` or `FALSE`.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.

Definition at line 461 of file flashcdw.c.

References AVR32_FLASHCDW_REGIONS, flashcdw_error_status, and flashcdw_lock_region().

    00462 {
    00463   unsigned int error_status = 0;
    00464   unsigned int region = AVR32_FLASHCDW_REGIONS;
    00465   while (region)
    00466   {
    00467     flashcdw_lock_region(--region, lock);
    00468     error_status |= flashcdw_error_status;
    00469   }
    00470   flashcdw_error_status = error_status;
    00471 }

void flashcdw_lock_external_privileged_fetch | ( | Bool | lock | ) |

Locks or unlocks external privileged fetch.

Parameters:

  | lock | Whether to lock external privileged fetch: `TRUE` or `FALSE`.

Warning:
  A Lock Error is issued if the Security bit is active.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.

Definition at line 386 of file flashcdw.c.

References flashcdw_set_gp_fuse_bit().

    00387 {
    00388   flashcdw_set_gp_fuse_bit(AVR32_FLASHCDW_FGPFRLO_EPFL_OFFSET, !lock);
    00389 }

void flashcdw_lock_page_region | ( | int | page_number,
|  | Bool | lock |
| ) |  |  |

Locks or unlocks the region of a page.

Parameters:

  | page_number | The page number: `0` to `(flashcdw_get_page_count() - 1)`: a page number within the flash array; `< 0`: the current page number.
  | lock | Whether to lock the region of the specified page: `TRUE` or `FALSE`.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.

Definition at line 449 of file flashcdw.c.

References flashcdw_issue_command().

Referenced by flashcdw_lock_region().

    00450 {
    00451   flashcdw_issue_command((lock) ? AVR32_FLASHCDW_FCMD_CMD_LP : AVR32_FLASHCDW_FCMD_CMD_UP, page_number);
    00452 }

void flashcdw_lock_region | ( | unsigned int | region,
|  | Bool | lock |
| ) |  |  |

Locks or unlocks a region.

Parameters:

  | region | The region number: `0` to `(AVR32_FLASHCDW_REGIONS - 1)`.
  | lock | Whether to lock the specified region: `TRUE` or `FALSE`.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.

Definition at line 455 of file flashcdw.c.

References flashcdw_get_region_first_page_number(), and flashcdw_lock_page_region().

Referenced by flashcdw_lock_all_regions().

    00456 {
    00457   flashcdw_lock_page_region(flashcdw_get_region_first_page_number(region), lock);
    00458 }

volatile void* flashcdw_memcpy | ( | volatile void * | dst,
|  | const void * | src,
|  | size_t | nbytes,
|  | Bool | erase |
| ) |  |  |

Copies nbytes bytes to the flash destination pointed to by dst from the source pointed to by src.

The destination areas that are not within the flash array or the User page are ignored.

All pointer and size alignments are supported.

Parameters:

  | dst | Pointer to flash destination.
  | src | Pointer to source data.
  | nbytes | Number of bytes to copy.
  | erase | Whether to erase before writing: `TRUE` or `FALSE`.

Returns:
  The value of dst.

Warning:
  If copying takes place between areas that overlap, the behavior is undefined.
  This function may be called with erase set to `FALSE` only if the destination consists only of erased words, i.e. this function can not be used to write only one bit of a previously written word. E.g., if `0x00000001` then `0xFFFFFFFE` are written to a word, the resulting value in flash may be different from `0x00000000`.
  A Lock Error is issued if the command is applied to pages belonging to a locked region or to the bootloader protected area or to a secure area requiring secure privileges.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.

Definition at line 934 of file flashcdw.c.

References Align_down, FALSE, flashcdw_clear_page_buffer(), flashcdw_erase_page(), flashcdw_erase_user_page(), flashcdw_error_status, flashcdw_get_flash_size(), flashcdw_write_page(), flashcdw_write_user_page(), Get_align, i, min, Test_align, TRUE, Union64::u16, UnionCPtr::u16ptr, StructCVPtr::u16ptr, StructCVPtr::u32ptr, Union64::u64, UnionCPtr::u64ptr, UnionVPtr::u64ptr, UnionCVPtr::u64ptr, StructCVPtr::u64ptr, Union64::u8, StructCVPtr::u8ptr, UnionCPtr::u8ptr, UnionVPtr::u8ptr, and UnionCVPtr::u8ptr.

    00935 {
    00936   // Use aggregated pointers to have several alignments available for a same address.
    00937   UnionCVPtr flash_array_end;
    00938   UnionVPtr dest;
    00939   UnionCPtr source;
    00940   StructCVPtr dest_end;
    00941   UnionCVPtr flash_page_source_end;
    00942   Bool incomplete_flash_page_end;
    00943   Union64 flash_dword;
    00944   Bool flash_dword_pending = FALSE;
    00945   UnionVPtr tmp;
    00946   unsigned int error_status = 0;
    00947   unsigned int i, j;
    00948
    00949   // Reformat arguments.
    00950   flash_array_end.u8ptr = AVR32_FLASH + flashcdw_get_flash_size();
    00951   dest.u8ptr = dst;
    00952   source.u8ptr = src;
    00953   dest_end.u8ptr = dest.u8ptr + nbytes;
    00954
    00955   // If destination is outside flash, go to next flash page if any.
    00956   if (dest.u8ptr < AVR32_FLASH)
    00957   {
    00958     source.u8ptr += AVR32_FLASH - dest.u8ptr;
    00959     dest.u8ptr = AVR32_FLASH;
    00960   }
    00961   else if (flash_array_end.u8ptr <= dest.u8ptr && dest.u8ptr < AVR32_FLASHCDW_USER_PAGE)
    00962   {
    00963     source.u8ptr += AVR32_FLASHCDW_USER_PAGE - dest.u8ptr;
    00964     dest.u8ptr = AVR32_FLASHCDW_USER_PAGE;
    00965   }
    00966
    00967   // If end of destination is outside flash, move it to the end of the previous flash page if any.
    00968   if (dest_end.u8ptr > AVR32_FLASHCDW_USER_PAGE + AVR32_FLASHCDW_USER_PAGE_SIZE)
    00969   {
    00970     dest_end.u8ptr = AVR32_FLASHCDW_USER_PAGE + AVR32_FLASHCDW_USER_PAGE_SIZE;
    00971   }
    00972   else if (AVR32_FLASHCDW_USER_PAGE >= dest_end.u8ptr && dest_end.u8ptr > flash_array_end.u8ptr)
    00973   {
    00974     dest_end.u8ptr = flash_array_end.u8ptr;
    00975   }
    00976
    00977   // Align each end of destination pointer with its natural boundary.
    00978   dest_end.u16ptr = (U16 *)Align_down((U32)dest_end.u8ptr, sizeof(U16));
    00979   dest_end.u32ptr = (U32 *)Align_down((U32)dest_end.u16ptr, sizeof(U32));
    00980   dest_end.u64ptr = (U64 *)Align_down((U32)dest_end.u32ptr, sizeof(U64));
    00981
    00982   // While end of destination is not reached...
    00983   while (dest.u8ptr < dest_end.u8ptr)
    00984   {
    00985     // Clear the page buffer in order to prepare data for a flash page write.
    00986     flashcdw_clear_page_buffer();
    00987     error_status |= flashcdw_error_status;
    00988
    00989     // Determine where the source data will end in the current flash page.
    00990     flash_page_source_end.u64ptr =
    00991       (U64 *)min((U32)dest_end.u64ptr,
    00992                  Align_down((U32)dest.u8ptr, AVR32_FLASHCDW_PAGE_SIZE) + AVR32_FLASHCDW_PAGE_SIZE);
    00993
    00994     // Determine if the current destination page has an incomplete end.
    00995     incomplete_flash_page_end = (Align_down((U32)dest.u8ptr, AVR32_FLASHCDW_PAGE_SIZE) >=
    00996                                  Align_down((U32)dest_end.u8ptr, AVR32_FLASHCDW_PAGE_SIZE));
    00997
    00998     // If destination does not point to the beginning of the current flash page...
    00999     if (!Test_align((U32)dest.u8ptr, AVR32_FLASHCDW_PAGE_SIZE))
    01000     {
    01001       // Fill the beginning of the page buffer with the current flash page data.
    01002       // This is required by the hardware, even if page erase is not requested,
    01003       // in order to be able to write successfully to erased parts of flash
    01004       // pages that have already been written to.
    01005       for (tmp.u8ptr = (U8 *)Align_down((U32)dest.u8ptr, AVR32_FLASHCDW_PAGE_SIZE);
    01006            tmp.u64ptr < (U64 *)Align_down((U32)dest.u8ptr, sizeof(U64));
    01007            tmp.u64ptr++)
    01008         *tmp.u64ptr = *tmp.u64ptr;
    01009
    01010       // If destination is not 64-bit aligned...
    01011       if (!Test_align((U32)dest.u8ptr, sizeof(U64)))
    01012       {
    01013         // Fill the beginning of the flash double-word buffer with the current
    01014         // flash page data.
    01015         // This is required by the hardware, even if page erase is not
    01016         // requested, in order to be able to write successfully to erased parts
    01017         // of flash pages that have already been written to.
    01018         for (i = 0; i < Get_align((U32)dest.u8ptr, sizeof(U64)); i++)
    01019           flash_dword.u8[i] = *tmp.u8ptr++;
    01020
    01021         // Fill the end of the flash double-word buffer with the source data.
    01022         for (; i < sizeof(U64); i++)
    01023           flash_dword.u8[i] = *source.u8ptr++;
    01024
    01025         // Align the destination pointer with its 64-bit boundary.
    01026         dest.u64ptr = (U64 *)Align_down((U32)dest.u8ptr, sizeof(U64));
    01027
    01028         // If the current destination double-word is not the last one...
    01029         if (dest.u64ptr < dest_end.u64ptr)
    01030         {
    01031           // Write the flash double-word buffer to the page buffer.
    01032           *dest.u64ptr++ = flash_dword.u64;
    01033         }
    01034         // If the current destination double-word is the last one, the flash
    01035         // double-word buffer must be kept for later.
    01036         else flash_dword_pending = TRUE;
    01037       }
    01038     }
    01039
    01040     // Read the source data with the maximal possible alignment and write it to
    01041     // the page buffer with 64-bit alignment.
    01042     switch (Get_align((U32)source.u8ptr, sizeof(U32)))
    01043     {
    01044     case 0:
    01045       for (i = flash_page_source_end.u64ptr - dest.u64ptr; i; i--)
    01046         *dest.u64ptr++ = *source.u64ptr++;
    01047       break;
    01048
    01049     case sizeof(U16):
    01050       for (i = flash_page_source_end.u64ptr - dest.u64ptr; i; i--)
    01051       {
    01052         for (j = 0; j < sizeof(U64) / sizeof(U16); j++) flash_dword.u16[j] = *source.u16ptr++;
    01053         *dest.u64ptr++ = flash_dword.u64;
    01054       }
    01055       break;
    01056
    01057     default:
    01058       for (i = flash_page_source_end.u64ptr - dest.u64ptr; i; i--)
    01059       {
    01060         for (j = 0; j < sizeof(U64); j++) flash_dword.u8[j] = *source.u8ptr++;
    01061         *dest.u64ptr++ = flash_dword.u64;
    01062       }
    01063     }
    01064
    01065     // If the current destination page has an incomplete end...
    01066     if (incomplete_flash_page_end)
    01067     {
    01068       // If the flash double-word buffer is in use, do not initialize it.
    01069       if (flash_dword_pending) i = Get_align((U32)dest_end.u8ptr, sizeof(U64));
    01070       // If the flash double-word buffer is free...
    01071       else
    01072       {
    01073         // Fill the beginning of the flash double-word buffer with the source data.
    01074         for (i = 0; i < Get_align((U32)dest_end.u8ptr, sizeof(U64)); i++)
    01075           flash_dword.u8[i] = *source.u8ptr++;
    01076       }
    01077
    01078       // This is required by the hardware, even if page erase is not requested,
    01079       // in order to be able to write successfully to erased parts of flash
    01080       // pages that have already been written to.
    01081       {
    01082         tmp.u8ptr = (volatile U8 *)dest_end.u8ptr;
    01083
    01084         // If end of destination is not 64-bit aligned...
    01085         if (!Test_align((U32)dest_end.u8ptr, sizeof(U64)))
    01086         {
    01087           // Fill the end of the flash double-word buffer with the current flash page data.
    01088           for (; i < sizeof(U64); i++)
    01089             flash_dword.u8[i] = *tmp.u8ptr++;
    01090
    01091           // Write the flash double-word buffer to the page buffer.
    01092           *dest.u64ptr++ = flash_dword.u64;
    01093         }
    01094
    01095         // Fill the end of the page buffer with the current flash page data.
    01096         for (; !Test_align((U32)tmp.u64ptr, AVR32_FLASHCDW_PAGE_SIZE); tmp.u64ptr++)
    01097           *tmp.u64ptr = *tmp.u64ptr;
    01098       }
    01099     }
    01100
    01101     // If the current flash page is in the flash array...
    01102     if (dest.u8ptr <= AVR32_FLASHCDW_USER_PAGE)
    01103     {
    01104       // Erase the current page if requested and write it from the page buffer.
    01105       if (erase)
    01106       {
    01107         flashcdw_erase_page(-1, FALSE);
    01108         error_status |= flashcdw_error_status;
    01109       }
    01110       flashcdw_write_page(-1);
    01111       error_status |= flashcdw_error_status;
    01112
    01113       // If the end of the flash array is reached, go to the User page.
    01114       if (dest.u8ptr >= flash_array_end.u8ptr)
    01115       {
    01116         source.u8ptr += AVR32_FLASHCDW_USER_PAGE - dest.u8ptr;
    01117         dest.u8ptr = AVR32_FLASHCDW_USER_PAGE;
    01118       }
    01119     }
    01120     // If the current flash page is the User page...
    01121     else
    01122     {
    01123       // Erase the User page if requested and write it from the page buffer.
    01124       if (erase)
    01125       {
    01126         flashcdw_erase_user_page(FALSE);
    01127         error_status |= flashcdw_error_status;
    01128       }
    01129       flashcdw_write_user_page();
    01130       error_status |= flashcdw_error_status;
    01131     }
    01132   }
    01133
    01134   // Update the FLASHC error status.
    01135   flashcdw_error_status = error_status;
    01136
    01137   // Return the initial destination pointer as the standard memcpy function does.
    01138   return dst;
    01139 }

volatile void* flashcdw_memset16 | ( | volatile void * | dst,
|  | U16 | src,
|  | size_t | nbytes,
|  | Bool | erase |
| ) |  |  |

Copies nbytes bytes to the flash destination pointed to by dst from the repeated src big-endian source half-word.

The destination areas that are not within the flash array or the User page are ignored.

All pointer and size alignments are supported.

Parameters:

  | dst | Pointer to flash destination.
  | src | Source half-word.
  | nbytes | Number of bytes to set.
  | erase | Whether to erase before writing: `TRUE` or `FALSE`.

Returns:
  The value of dst.

Warning:
  This function may be called with erase set to `FALSE` only if the destination consists only of erased words, i.e. this function can not be used to write only one bit of a previously written word. E.g., if `0x00000001` then `0xFFFFFFFE` are written to a word, the resulting value in flash may be different from `0x00000000`.
  A Lock Error is issued if the command is applied to pages belonging to a locked region or to the bootloader protected area or to a secure area requiring secure privileges.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.

Definition at line 749 of file flashcdw.c.

References flashcdw_memset32().

Referenced by flashcdw_memset8().

    00750 {
    00751   return flashcdw_memset32(dst, src | (U32)src << 16, nbytes, erase);
    00752 }

volatile void* flashcdw_memset32 | ( | volatile void * | dst,
|  | U32 | src,
|  | size_t | nbytes,
|  | Bool | erase |
| ) |  |  |

Copies nbytes bytes to the flash destination pointed to by dst from the repeated src big-endian source word.

The destination areas that are not within the flash array or the User page are ignored.

All pointer and size alignments are supported.

Parameters:

  | dst | Pointer to flash destination.
  | src | Source word.
  | nbytes | Number of bytes to set.
  | erase | Whether to erase before writing: `TRUE` or `FALSE`.

Returns:
  The value of dst.

Warning:
  This function may be called with erase set to `FALSE` only if the destination consists only of erased words, i.e. this function can not be used to write only one bit of a previously written word. E.g., if `0x00000001` then `0xFFFFFFFE` are written to a word, the resulting value in flash may be different from `0x00000000`.
  A Lock Error is issued if the command is applied to pages belonging to a locked region or to the bootloader protected area or to a secure area requiring secure privileges.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.

Definition at line 755 of file flashcdw.c.

References flashcdw_memset64().

Referenced by flashcdw_memset16().

    00756 {
    00757   return flashcdw_memset64(dst, src | (U64)src << 32, nbytes, erase);
    00758 }

volatile void* flashcdw_memset64 | ( | volatile void * | dst,
|  | U64 | src,
|  | size_t | nbytes,
|  | Bool | erase |
| ) |  |  |

Copies nbytes bytes to the flash destination pointed to by dst from the repeated src big-endian source double-word.

The destination areas that are not within the flash array or the User page are ignored.

All pointer and size alignments are supported.

Parameters:

  | dst | Pointer to flash destination.
  | src | Source double-word.
  | nbytes | Number of bytes to set.
  | erase | Whether to erase before writing: `TRUE` or `FALSE`.

Returns:
  The value of dst.

Warning:
  This function may be called with erase set to `FALSE` only if the destination consists only of erased words, i.e. this function can not be used to write only one bit of a previously written word. E.g., if `0x00000001` then `0xFFFFFFFE` are written to a word, the resulting value in flash may be different from `0x00000000`.
  A Lock Error is issued if the command is applied to pages belonging to a locked region or to the bootloader protected area or to a secure area requiring secure privileges.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.

Definition at line 761 of file flashcdw.c.

References Align_down, FALSE, flashcdw_clear_page_buffer(), flashcdw_erase_page(), flashcdw_erase_user_page(), flashcdw_error_status, flashcdw_get_flash_size(), flashcdw_write_page(), flashcdw_write_user_page(), Get_align, i, min, Test_align, StructCVPtr::u16ptr, StructCVPtr::u32ptr, Union64::u64, UnionVPtr::u64ptr, UnionCVPtr::u64ptr, StructCVPtr::u64ptr, Union64::u8, StructCVPtr::u8ptr, UnionVPtr::u8ptr, and UnionCVPtr::u8ptr.

Referenced by flashcdw_memset32().

    00762 {
    00763   // Use aggregated pointers to have several alignments available for a same address.
    00764   UnionCVPtr flash_array_end;
    00765   UnionVPtr dest;
    00766   Union64 source = {0};
    00767   StructCVPtr dest_end;
    00768   UnionCVPtr flash_page_source_end;
    00769   Bool incomplete_flash_page_end;
    00770   Union64 flash_dword;
    00771   UnionVPtr tmp;
    00772   unsigned int error_status = 0;
    00773   unsigned int i;
    00774
    00775   // Reformat arguments.
    00776   flash_array_end.u8ptr = AVR32_FLASH + flashcdw_get_flash_size();
    00777   dest.u8ptr = dst;
    00778   for (i = (Get_align((U32)dest.u8ptr, sizeof(U64)) - 1) & (sizeof(U64) - 1);
    00779        src; i = (i - 1) & (sizeof(U64) - 1))
    00780   {
    00781     source.u8[i] = src;
    00782     src >>= 8;
    00783   }
    00784   dest_end.u8ptr = dest.u8ptr + nbytes;
    00785
    00786   // If destination is outside flash, go to next flash page if any.
    00787   if (dest.u8ptr < AVR32_FLASH)
    00788   {
    00789     dest.u8ptr = AVR32_FLASH;
    00790   }
    00791   else if (flash_array_end.u8ptr <= dest.u8ptr && dest.u8ptr < AVR32_FLASHCDW_USER_PAGE)
    00792   {
    00793     dest.u8ptr = AVR32_FLASHCDW_USER_PAGE;
    00794   }
    00795
    00796   // If end of destination is outside flash, move it to the end of the previous flash page if any.
    00797   if (dest_end.u8ptr > AVR32_FLASHCDW_USER_PAGE + AVR32_FLASHCDW_USER_PAGE_SIZE)
    00798   {
    00799     dest_end.u8ptr = AVR32_FLASHCDW_USER_PAGE + AVR32_FLASHCDW_USER_PAGE_SIZE;
    00800   }
    00801   else if (AVR32_FLASHCDW_USER_PAGE >= dest_end.u8ptr && dest_end.u8ptr > flash_array_end.u8ptr)
    00802   {
    00803     dest_end.u8ptr = flash_array_end.u8ptr;
    00804   }
    00805
    00806   // Align each end of destination pointer with its natural boundary.
    00807   dest_end.u16ptr = (U16 *)Align_down((U32)dest_end.u8ptr, sizeof(U16));
    00808   dest_end.u32ptr = (U32 *)Align_down((U32)dest_end.u16ptr, sizeof(U32));
    00809   dest_end.u64ptr = (U64 *)Align_down((U32)dest_end.u32ptr, sizeof(U64));
    00810
    00811   // While end of destination is not reached...
    00812   while (dest.u8ptr < dest_end.u8ptr)
    00813   {
    00814     // Clear the page buffer in order to prepare data for a flash page write.
    00815     flashcdw_clear_page_buffer();
    00816     error_status |= flashcdw_error_status;
    00817
    00818     // Determine where the source data will end in the current flash page.
    00819     flash_page_source_end.u64ptr =
    00820       (U64 *)min((U32)dest_end.u64ptr,
    00821                  Align_down((U32)dest.u8ptr, AVR32_FLASHCDW_PAGE_SIZE) + AVR32_FLASHCDW_PAGE_SIZE);
    00822
    00823     // Determine if the current destination page has an incomplete end.
    00824     incomplete_flash_page_end = (Align_down((U32)dest.u8ptr, AVR32_FLASHCDW_PAGE_SIZE) >=
    00825                                  Align_down((U32)dest_end.u8ptr, AVR32_FLASHCDW_PAGE_SIZE));
    00826
    00827     // Use a flash double-word buffer to manage unaligned accesses.
    00828     flash_dword.u64 = source.u64;
    00829
    00830     // If destination does not point to the beginning of the current flash page...
    00831     if (!Test_align((U32)dest.u8ptr, AVR32_FLASHCDW_PAGE_SIZE))
    00832     {
    00833       // Fill the beginning of the page buffer with the current flash page data.
    00834       // This is required by the hardware, even if page erase is not requested,
    00835       // in order to be able to write successfully to erased parts of flash
    00836       // pages that have already been written to.
    00837       for (tmp.u8ptr = (U8 *)Align_down((U32)dest.u8ptr, AVR32_FLASHCDW_PAGE_SIZE);
    00838            tmp.u64ptr < (U64 *)Align_down((U32)dest.u8ptr, sizeof(U64));
    00839            tmp.u64ptr++)
    00840         *tmp.u64ptr = *tmp.u64ptr;
    00841
    00842       // If destination is not 64-bit aligned...
    00843       if (!Test_align((U32)dest.u8ptr, sizeof(U64)))
    00844       {
    00845         // Fill the beginning of the flash double-word buffer with the current
    00846         // flash page data.
    00847         // This is required by the hardware, even if page erase is not
    00848         // requested, in order to be able to write successfully to erased parts
    00849         // of flash pages that have already been written to.
    00850         for (i = 0; i < Get_align((U32)dest.u8ptr, sizeof(U64)); i++)
    00851           flash_dword.u8[i] = *tmp.u8ptr++;
    00852
    00853         // Align the destination pointer with its 64-bit boundary.
    00854         dest.u64ptr = (U64 *)Align_down((U32)dest.u8ptr, sizeof(U64));
    00855
    00856         // If the current destination double-word is not the last one...
    00857         if (dest.u64ptr < dest_end.u64ptr)
    00858         {
    00859           // Write the flash double-word buffer to the page buffer and reinitialize it.
    00860           *dest.u64ptr++ = flash_dword.u64;
    00861           flash_dword.u64 = source.u64;
    00862         }
    00863       }
    00864     }
    00865
    00866     // Write the source data to the page buffer with 64-bit alignment.
    00867     for (i = flash_page_source_end.u64ptr - dest.u64ptr; i; i--)
    00868       *dest.u64ptr++ = source.u64;
    00869
    00870     // If the current destination page has an incomplete end...
    00871     if (incomplete_flash_page_end)
    00872     {
    00873       // This is required by the hardware, even if page erase is not requested,
    00874       // in order to be able to write successfully to erased parts of flash
    00875       // pages that have already been written to.
    00876       {
    00877         tmp.u8ptr = (volatile U8 *)dest_end.u8ptr;
    00878
    00879         // If end of destination is not 64-bit aligned...
    00880         if (!Test_align((U32)dest_end.u8ptr, sizeof(U64)))
    00881         {
    00882           // Fill the end of the flash double-word buffer with the current flash page data.
    00883           for (i = Get_align((U32)dest_end.u8ptr, sizeof(U64)); i < sizeof(U64); i++)
    00884             flash_dword.u8[i] = *tmp.u8ptr++;
    00885
    00886           // Write the flash double-word buffer to the page buffer.
    00887           *dest.u64ptr++ = flash_dword.u64;
    00888         }
    00889
    00890         // Fill the end of the page buffer with the current flash page data.
    00891         for (; !Test_align((U32)tmp.u64ptr, AVR32_FLASHCDW_PAGE_SIZE); tmp.u64ptr++)
    00892           *tmp.u64ptr = *tmp.u64ptr;
    00893       }
    00894     }
    00895
    00896     // If the current flash page is in the flash array...
    00897     if (dest.u8ptr <= AVR32_FLASHCDW_USER_PAGE)
    00898     {
    00899       // Erase the current page if requested and write it from the page buffer.
    00900       if (erase)
    00901       {
    00902         flashcdw_erase_page(-1, FALSE);
    00903         error_status |= flashcdw_error_status;
    00904       }
    00905       flashcdw_write_page(-1);
    00906       error_status |= flashcdw_error_status;
    00907
    00908       // If the end of the flash array is reached, go to the User page.
    00909       if (dest.u8ptr >= flash_array_end.u8ptr)
    00910         dest.u8ptr = AVR32_FLASHCDW_USER_PAGE;
    00911     }
    00912     // If the current flash page is the User page...
    00913     else
    00914     {
    00915       // Erase the User page if requested and write it from the page buffer.
    00916       if (erase)
    00917       {
    00918         flashcdw_erase_user_page(FALSE);
    00919         error_status |= flashcdw_error_status;
    00920       }
    00921       flashcdw_write_user_page();
    00922       error_status |= flashcdw_error_status;
    00923     }
    00924   }
    00925
    00926   // Update the FLASHC error status.
    00927   flashcdw_error_status = error_status;
    00928
    00929   // Return the initial destination pointer as the standard memset function does.
    00930   return dst;
    00931 }

volatile void* flashcdw_memset8 | ( | volatile void * | dst,
|  | U8 | src,
|  | size_t | nbytes,
|  | Bool | erase |
| ) |  |  |

Copies nbytes bytes to the flash destination pointed to by dst from the repeated src source byte.

The destination areas that are not within the flash array or the User page are ignored.

All pointer and size alignments are supported.

Parameters:

  | dst | Pointer to flash destination.
  | src | Source byte.
  | nbytes | Number of bytes to set.
  | erase | Whether to erase before writing: `TRUE` or `FALSE`.

Returns:
  The value of dst.

Warning:
  This function may be called with erase set to `FALSE` only if the destination consists only of erased words, i.e. this function cannot be used to write only one bit of a previously written word. E.g., if `0x00000001` then `0xFFFFFFFE` are written to a word, the resulting value in flash may be different from `0x00000000`.
  A Lock Error is issued if the command is applied to pages belonging to a locked region or to the bootloader protected area or to a secure area requiring secure privileges.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.

Definition at line 743 of file flashcdw.c.

References flashcdw_memset16().

    00744 {
    00745   return flashcdw_memset16(dst, src | (U16)src << 8, nbytes, erase);
    00746 }

void flashcdw_no_operation | ( | void |  | ) |

Issues a No Operation command to the FLASHCDW.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.

Definition at line 323 of file flashcdw.c.

References flashcdw_issue_command().

    00324 {
    00325   flashcdw_issue_command(AVR32_FLASHCDW_FCMD_CMD_NOP, -1);
    00326 }

Bool flashcdw_quick_page_read | ( | int | page_number | ) |

Applies the Quick Page Read command to a page.

Parameters:

  | page_number | The page number: `0` to `(flashcdw_get_page_count() - 1)`: a page number within the flash array; `< 0`: the current page number.

Returns:
  Whether the specified page is erased.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.

Definition at line 681 of file flashcdw.c.

References flashcdw_is_page_erased(), and flashcdw_issue_command().

Referenced by flashcdw_erase_page().

    00682 {
    00683   flashcdw_issue_command(AVR32_FLASHCDW_FCMD_CMD_QPR, page_number);
    00684   return flashcdw_is_page_erased();
    00685 }

Bool flashcdw_quick_user_page_read | ( | void |  | ) |

Issues a Quick Page Read User Page command to the FLASHCDW.

Returns:
  Whether the User page is erased.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.

Definition at line 723 of file flashcdw.c.

References flashcdw_is_page_erased(), and flashcdw_issue_command().

Referenced by flashcdw_erase_user_page().

    00724 {
    00725   flashcdw_issue_command(AVR32_FLASHCDW_FCMD_CMD_QPRUP, -1);
    00726   return flashcdw_is_page_erased();
    00727 }

U64 flashcdw_read_all_gp_fuses | ( | void |  | ) |

Reads all general-purpose fuses.

Returns:
  The value of all general-purpose fuses as a word.

Note:
  The actual number of general-purpose fuse bits implemented by hardware is given by `AVR32_FLASHCDW_GPF_NUM`. The other bits among the 64 are fixed at 1 by hardware.

Definition at line 500 of file flashcdw.c.

Referenced by flashcdw_erase_all_gp_fuses(), flashcdw_erase_gp_fuse_byte(), flashcdw_read_gp_fuse_bit(), flashcdw_read_gp_fuse_bitfield(), and flashcdw_read_gp_fuse_byte().

    00501 {
    00502   return AVR32_FLASHCDW.fgpfrlo | (U64)AVR32_FLASHCDW.fgpfrhi << 32;
    00503 }

Bool flashcdw_read_gp_fuse_bit | ( | unsigned int | gp_fuse_bit | ) |

Reads a general-purpose fuse bit.

Parameters:

  | gp_fuse_bit | The general-purpose fuse bit: `0` to `63`.

Returns:
  The value of the specified general-purpose fuse bit.

Note:
  The actual number of general-purpose fuse bits implemented by hardware is given by `AVR32_FLASHCDW_GPF_NUM`. The other bits among the 64 are fixed at 1 by hardware.

Definition at line 482 of file flashcdw.c.

References flashcdw_read_all_gp_fuses().

Referenced by flashcdw_erase_gp_fuse_bit(), flashcdw_is_external_privileged_fetch_locked(), and flashcdw_is_jtag_user_protection_enabled().

    00483 {
    00484   return ((flashcdw_read_all_gp_fuses() & 1ULL << (gp_fuse_bit & 0x3F)) != 0);
    00485 }

U64 flashcdw_read_gp_fuse_bitfield | ( | unsigned int | pos,
|  | unsigned int | width |
| ) |  |  |

Reads a general-purpose fuse bit-field.

Parameters:

  | pos | The bit-position of the general-purpose fuse bit-field: `0` to `63`.
  | width | The bit-width of the general-purpose fuse bit-field: `0` to `64`.

Returns:
  The value of the specified general-purpose fuse bit-field.

Note:
  The actual number of general-purpose fuse bits implemented by hardware is given by `AVR32_FLASHCDW_GPF_NUM`. The other bits among the 64 are fixed at 1 by hardware.

Definition at line 488 of file flashcdw.c.

References flashcdw_read_all_gp_fuses(), and min.

Referenced by flashcdw_erase_gp_fuse_bitfield(), flashcdw_get_bootloader_protected_size(), flashcdw_is_secure_state_debug_enabled(), and flashcdw_is_secure_state_enabled().

    00489 {
    00490   return flashcdw_read_all_gp_fuses() >> (pos & 0x3F) & ((1ULL << min(width, 64)) - 1);
    00491 }

U8 flashcdw_read_gp_fuse_byte | ( | unsigned int | gp_fuse_byte | ) |

Reads a general-purpose fuse byte.

Parameters:

  | gp_fuse_byte | The general-purpose fuse byte: `0` to `7`.

Returns:
  The value of the specified general-purpose fuse byte.

Note:
  The actual number of general-purpose fuse bits implemented by hardware is given by `AVR32_FLASHCDW_GPF_NUM`. The other bits among the 64 are fixed at 1 by hardware.

Definition at line 494 of file flashcdw.c.

References flashcdw_read_all_gp_fuses().

Referenced by flashcdw_erase_gp_fuse_byte().

    00495 {
    00496   return flashcdw_read_all_gp_fuses() >> ((gp_fuse_byte & 0x07) << 3);
    00497 }

void flashcdw_set_all_gp_fuses | ( | U64 | value | ) |

Sets all general-purpose fuses with the appropriate erase and write operations.

Parameters:

  | value | The value of all general-purpose fuses as a word.

Warning:
  A Lock Error is issued if the Security bit is active or if the SECURE mode is active.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.
  The actual number of general-purpose fuse bits implemented by hardware is given by `AVR32_FLASHCDW_GPF_NUM`. The other bits among the 64 are fixed at 1 by hardware.

Definition at line 641 of file flashcdw.c.

References FALSE, flashcdw_erase_all_gp_fuses(), flashcdw_error_status, and flashcdw_write_all_gp_fuses().

    00642 {
    00643   unsigned int error_status;
    00644   switch (value)
    00645   {
    00646   case 0xFFFFFFFFFFFFFFFFULL:
    00647     flashcdw_erase_all_gp_fuses(FALSE);
    00648     break;
    00649   case 0x0000000000000000ULL:
    00650     flashcdw_write_all_gp_fuses(0x0000000000000000ULL);
    00651     break;
    00652   default:
    00653     flashcdw_erase_all_gp_fuses(FALSE);
    00654     error_status = flashcdw_error_status;
    00655     flashcdw_write_all_gp_fuses(value);
    00656     flashcdw_error_status |= error_status;
    00657   }
    00658 }

unsigned int flashcdw_set_bootloader_protected_size | ( | unsigned int | bootprot_size | ) |

Sets the bootloader protected size.

Parameters:

  | bootprot_size | The wanted bootloader protected size in bytes. If this size is not supported, the actual size will be the nearest greater available size or the maximal possible size if the requested size is too large.

Returns:
  The actual bootloader protected size in bytes.

Warning:
  A Lock Error is issued if the Security bit is active.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.

Definition at line 364 of file flashcdw.c.

References clz, flashcdw_get_bootloader_protected_size(), flashcdw_set_gp_fuse_bitfield(), max, and min.

    00365 {
    00366   flashcdw_set_gp_fuse_bitfield(AVR32_FLASHCDW_FGPFRLO_BOOTPROT_OFFSET,
    00367                               AVR32_FLASHCDW_FGPFRLO_BOOTPROT_SIZE,
    00368                               (1 << AVR32_FLASHCDW_FGPFRLO_BOOTPROT_SIZE) - 1 -
    00369                               ((bootprot_size) ?
    00370                                32 - clz((((min(max(bootprot_size, AVR32_FLASHCDW_PAGE_SIZE << 1),
    00371                                                AVR32_FLASHCDW_PAGE_SIZE <<
    00372                                                ((1 << AVR32_FLASHCDW_FGPFRLO_BOOTPROT_SIZE) - 1)) +
    00373                                            AVR32_FLASHCDW_PAGE_SIZE - 1) /
    00374                                           AVR32_FLASHCDW_PAGE_SIZE) << 1) - 1) - 1 :
    00375                                0));
    00376   return flashcdw_get_bootloader_protected_size();
    00377 }

void flashcdw_set_flash_waitstate_and_readmode | ( | unsigned long | cpu_f_hz | ) |

Depednding to the CPU frequency, set the wait states of flash read accesses and enable or disable the High speed read mode.

Parameters:

  | cpu_f_hz | The CPU frequency

Definition at line 148 of file flashcdw.c.

References AVR32_FLASHCDW_FWS_0_MAX_FREQ, AVR32_FLASHCDW_FWS_1_MAX_FREQ, flashcdw_issue_command(), and flashcdw_set_wait_state().

Referenced by pcl_configure_synchronous_clocks(), and pcl_switch_to_osc().

    00149 {
    00150   if(cpu_f_hz > AVR32_FLASHCDW_FWS_0_MAX_FREQ)    // > 15MHz
    00151   {
    00152     if(cpu_f_hz <= AVR32_FLASHCDW_FWS_1_MAX_FREQ) // <= 30MHz
    00153     {
    00154       // Set a wait-state, disable the high-speed read mode.
    00155       flashcdw_set_wait_state(1);
    00156       flashcdw_issue_command(AVR32_FLASHCDW_FCMD_CMD_HSDIS, -1);
    00157     }
    00158     else
    00159     {
    00160       // Set a wait-state, enable the high-speed read mode.
    00161       flashcdw_set_wait_state(1);
    00162       flashcdw_issue_command(AVR32_FLASHCDW_FCMD_CMD_HSEN, -1);
    00163     }
    00164   }
    00165   else  // <= 15MHz
    00166   {
    00167     // No wait-state, disable the high-speed read mode
    00168     flashcdw_set_wait_state(0);
    00169     flashcdw_issue_command(AVR32_FLASHCDW_FCMD_CMD_HSDIS, -1);
    00170   }
    00171 }

void flashcdw_set_gp_fuse_bit | ( | unsigned int | gp_fuse_bit,
|  | Bool | value |
| ) |  |  |

Sets a general-purpose fuse bit with the appropriate erase and write operations.

Parameters:

  | gp_fuse_bit | The general-purpose fuse bit: `0` to `63`.
  | value | The value of the specified general-purpose fuse bit.

Warning:
  A Lock Error is issued:
  - if the Security bit is active and the command is applied to BOOTPROT or EPFL or UPROT fuses,
  - if the SECURE mode is active and the command is applied to the SECURE bits.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.
  The actual number of general-purpose fuse bits implemented by hardware is given by `AVR32_FLASHCDW_GPF_NUM`. The other bits among the 64 are fixed at 1 by hardware.

Definition at line 597 of file flashcdw.c.

References FALSE, flashcdw_erase_gp_fuse_bit(), and flashcdw_write_gp_fuse_bit().

Referenced by flashcdw_disable_jtag_user_protection(), flashcdw_enable_jtag_user_protection(), flashcdw_lock_external_privileged_fetch(), and flashcdw_set_gp_fuse_bitfield().

    00598 {
    00599   if (value)
    00600     flashcdw_erase_gp_fuse_bit(gp_fuse_bit, FALSE);
    00601   else
    00602     flashcdw_write_gp_fuse_bit(gp_fuse_bit, FALSE);
    00603 }

void flashcdw_set_gp_fuse_bitfield | ( | unsigned int | pos,
|  | unsigned int | width,
|  | U64 | value |
| ) |  |  |

Sets a general-purpose fuse bit-field with the appropriate erase and write operations.

Parameters:

  | pos | The bit-position of the general-purpose fuse bit-field: `0` to `63`.
  | width | The bit-width of the general-purpose fuse bit-field: `0` to `64`.
  | value | The value of the specified general-purpose fuse bit-field.

Warning:
  A Lock Error is issued:
  - if the Security bit is active and the command is applied to BOOTPROT or EPFL or UPROT fuses,
  - if the SECURE mode is active and the command is applied to the SECURE bits.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.
  The actual number of general-purpose fuse bits implemented by hardware is given by `AVR32_FLASHCDW_GPF_NUM`. The other bits among the 64 are fixed at 1 by hardware.

Definition at line 606 of file flashcdw.c.

References flashcdw_error_status, flashcdw_set_gp_fuse_bit(), and min.

Referenced by flashcdw_set_bootloader_protected_size().

    00607 {
    00608   unsigned int error_status = 0;
    00609   unsigned int gp_fuse_bit;
    00610   pos &= 0x3F;
    00611   width = min(width, 64);
    00612   for (gp_fuse_bit = pos; gp_fuse_bit < pos + width; gp_fuse_bit++, value >>= 1)
    00613   {
    00614     flashcdw_set_gp_fuse_bit(gp_fuse_bit, value & 0x01);
    00615     error_status |= flashcdw_error_status;
    00616   }
    00617   flashcdw_error_status = error_status;
    00618 }

void flashcdw_set_gp_fuse_byte | ( | unsigned int | gp_fuse_byte,
|  | U8 | value |
| ) |  |  |

Sets a general-purpose fuse byte with the appropriate erase and write operations.

Parameters:

  | gp_fuse_byte | The general-purpose fuse byte: `0` to `7`.
  | value | The value of the specified general-purpose fuse byte.

Warning:
  A Lock Error is issued if the Security bit is active or if the SECURE mode is active and the SECURE bits are in the target fuse byte.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.
  The actual number of general-purpose fuse bits implemented by hardware is given by `AVR32_FLASHCDW_GPF_NUM`. The other bits among the 64 are fixed at 1 by hardware.

Definition at line 621 of file flashcdw.c.

References FALSE, flashcdw_erase_gp_fuse_byte(), flashcdw_error_status, and flashcdw_write_gp_fuse_byte().

    00622 {
    00623   unsigned int error_status;
    00624   switch (value)
    00625   {
    00626   case 0xFF:
    00627     flashcdw_erase_gp_fuse_byte(gp_fuse_byte, FALSE);
    00628     break;
    00629   case 0x00:
    00630     flashcdw_write_gp_fuse_byte(gp_fuse_byte, 0x00);
    00631     break;
    00632   default:
    00633     flashcdw_erase_gp_fuse_byte(gp_fuse_byte, FALSE);
    00634     error_status = flashcdw_error_status;
    00635     flashcdw_write_gp_fuse_byte(gp_fuse_byte, value);
    00636     flashcdw_error_status |= error_status;
    00637   }
    00638 }

void flashcdw_set_wait_state | ( | unsigned int | wait_state | ) |

Sets the number of wait states of flash read accesses.

Parameters:

  | wait_state | The number of wait states of flash read accesses: `0` to `1`.

Definition at line 140 of file flashcdw.c.

References u_avr32_flashcdw_fcr_t::fcr, and u_avr32_flashcdw_fcr_t::FCR.

Referenced by flashcdw_set_flash_waitstate_and_readmode().

    00141 {
    00142   u_avr32_flashcdw_fcr_t u_avr32_flashcdw_fcr = {AVR32_FLASHCDW.fcr};
    00143   u_avr32_flashcdw_fcr.FCR.fws = wait_state;
    00144   AVR32_FLASHCDW.fcr = u_avr32_flashcdw_fcr.fcr;
    00145 }

void flashcdw_write_all_gp_fuses | ( | U64 | value | ) |

Writes all general-purpose fuses.

Parameters:

  | value | The value of all general-purpose fuses as a word.

Warning:
  A Lock Error is issued if the Security bit is active or if the SECURE mode is active.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.
  A write operation can only clear bits.
  The actual number of general-purpose fuse bits implemented by hardware is given by `AVR32_FLASHCDW_GPF_NUM`. The other bits among the 64 are fixed at 1 by hardware.

Definition at line 584 of file flashcdw.c.

References flashcdw_error_status, and flashcdw_write_gp_fuse_byte().

Referenced by flashcdw_set_all_gp_fuses().

    00585 {
    00586   unsigned int error_status = 0;
    00587   unsigned int gp_fuse_byte;
    00588   for (gp_fuse_byte = 0; gp_fuse_byte < 8; gp_fuse_byte++, value >>= 8)
    00589   {
    00590     flashcdw_write_gp_fuse_byte(gp_fuse_byte, value);
    00591     error_status |= flashcdw_error_status;
    00592   }
    00593   flashcdw_error_status = error_status;
    00594 }

void flashcdw_write_gp_fuse_bit | ( | unsigned int | gp_fuse_bit,
|  | Bool | value |
| ) |  |  |

Writes a general-purpose fuse bit.

Parameters:

  | gp_fuse_bit | The general-purpose fuse bit: `0` to `63`.
  | value | The value of the specified general-purpose fuse bit.

Warning:
  A Lock Error is issued:
  - if the Security bit is active and the command is applied to BOOTPROT or EPFL or UPROT fuses,
  - if the SECURE mode is active and the command is applied to the SECURE bits.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.
  A write operation can only clear bits.
  The actual number of general-purpose fuse bits implemented by hardware is given by `AVR32_FLASHCDW_GPF_NUM`. The other bits among the 64 are fixed at 1 by hardware.

Definition at line 556 of file flashcdw.c.

References flashcdw_issue_command().

Referenced by flashcdw_set_gp_fuse_bit(), and flashcdw_write_gp_fuse_bitfield().

    00557 {
    00558   if (!value)
    00559     flashcdw_issue_command(AVR32_FLASHCDW_FCMD_CMD_WGPB, gp_fuse_bit & 0x3F);
    00560 }

void flashcdw_write_gp_fuse_bitfield | ( | unsigned int | pos,
|  | unsigned int | width,
|  | U64 | value |
| ) |  |  |

Writes a general-purpose fuse bit-field.

Parameters:

  | pos | The bit-position of the general-purpose fuse bit-field: `0` to `63`.
  | width | The bit-width of the general-purpose fuse bit-field: `0` to `64`.
  | value | The value of the specified general-purpose fuse bit-field.

Warning:
  A Lock Error is issued:
  - if the Security bit is active and the command is applied to BOOTPROT or EPFL or UPROT fuses,
  - if the SECURE mode is active and the command is applied to the SECURE bits.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.
  A write operation can only clear bits.
  The actual number of general-purpose fuse bits implemented by hardware is given by `AVR32_FLASHCDW_GPF_NUM`. The other bits among the 64 are fixed at 1 by hardware.

Definition at line 563 of file flashcdw.c.

References flashcdw_error_status, flashcdw_write_gp_fuse_bit(), and min.

Referenced by flashcdw_enable_secure_state_no_debug(), and flashcdw_enable_secure_state_with_debug().

    00564 {
    00565   unsigned int error_status = 0;
    00566   unsigned int gp_fuse_bit;
    00567   pos &= 0x3F;
    00568   width = min(width, 64);
    00569   for (gp_fuse_bit = pos; gp_fuse_bit < pos + width; gp_fuse_bit++, value >>= 1)
    00570   {
    00571     flashcdw_write_gp_fuse_bit(gp_fuse_bit, value & 0x01);
    00572     error_status |= flashcdw_error_status;
    00573   }
    00574   flashcdw_error_status = error_status;
    00575 }

void flashcdw_write_gp_fuse_byte | ( | unsigned int | gp_fuse_byte,
|  | U8 | value |
| ) |  |  |

Writes a general-purpose fuse byte.

Parameters:

  | gp_fuse_byte | The general-purpose fuse byte: `0` to `7`.
  | value | The value of the specified general-purpose fuse byte.

Warning:
  A Lock Error is issued if the Security bit is active or if the SECURE mode is active and the SECURE bits are in the target fuse byte.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.
  A write operation can only clear bits.
  The actual number of general-purpose fuse bits implemented by hardware is given by `AVR32_FLASHCDW_GPF_NUM`. The other bits among the 64 are fixed at 1 by hardware.

Definition at line 578 of file flashcdw.c.

References flashcdw_issue_command().

Referenced by flashcdw_erase_gp_fuse_byte(), flashcdw_set_gp_fuse_byte(), and flashcdw_write_all_gp_fuses().

    00579 {
    00580   flashcdw_issue_command(AVR32_FLASHCDW_FCMD_CMD_PGPFB, (gp_fuse_byte & 0x07) | value << 3);
    00581 }

void flashcdw_write_page | ( | int | page_number | ) |

Writes a page from the page buffer.

Parameters:

  | page_number | The page number: `0` to `(flashcdw_get_page_count() - 1)`: a page number within the flash array; `< 0`: the current page number.

Warning:
  A Lock Error is issued if the command is applied to a page belonging to a locked region or to the bootloader protected area or to a secure area requiring secure privileges.
  The page buffer is not automatically reset after a page write.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.
  A write operation can only clear bits.

Definition at line 717 of file flashcdw.c.

References flashcdw_issue_command().

Referenced by flashcdw_memcpy(), and flashcdw_memset64().

    00718 {
    00719   flashcdw_issue_command(AVR32_FLASHCDW_FCMD_CMD_WP, page_number);
    00720 }

void flashcdw_write_user_page | ( | void |  | ) |

Writes the User page from the page buffer.

Warning:
  The page buffer is not automatically reset after a page write.

Note:
  The FLASHCDW error status returned by flashcdw_is_lock_error and flashcdw_is_programming_error is updated.
  A write operation can only clear bits.

Definition at line 737 of file flashcdw.c.

References flashcdw_issue_command().

Referenced by flashcdw_memcpy(), and flashcdw_memset64().

    00738 {
    00739   flashcdw_issue_command(AVR32_FLASHCDW_FCMD_CMD_WUP, -1);
    00740 }

## Variable Documentation

void(*volatile flashcdw_wait_until_ready)(void)

Pointer to the function used by the driver when it needs to wait until the FLASHCDW is ready to run a new command.

The default function is flashcdw_default_wait_until_ready. The user may change this pointer to use another implementation.

Referenced by flashcdw_issue_command().