68 #if (SAM4E || SAM4N || SAM4S || SAM4C || SAMG || SAM4CP || SAM4CM || \ 69 SAMV71 || SAMV70 || SAMS70 || SAME70) 71 # define FLASH_USER_SIG_SIZE (512) 77 #if (SAM4SD16 || SAM4SD32) 79 # define GPNVM_NUM_MAX 3 82 # define GPNVM_NUM_MAX 2 85 # define IFLASH_ADDR IFLASH0_ADDR 87 # define IFLASH_PAGE_SIZE IFLASH0_PAGE_SIZE 89 # define IFLASH_LOCK_REGION_SIZE IFLASH0_LOCK_REGION_SIZE 90 #elif (SAM3XA || SAM3U4) 94 # define GPNVM_NUM_MAX 3 96 # define IFLASH_ADDR IFLASH0_ADDR 98 # define IFLASH_PAGE_SIZE IFLASH0_PAGE_SIZE 100 # define IFLASH_LOCK_REGION_SIZE IFLASH0_LOCK_REGION_SIZE 107 # define GPNVM_NUM_MAX 2 109 # define IFLASH_ADDR IFLASH0_ADDR 111 # define IFLASH_PAGE_SIZE IFLASH0_PAGE_SIZE 113 # define IFLASH_LOCK_REGION_SIZE IFLASH0_LOCK_REGION_SIZE 114 #elif (SAM3S8 || SAM3SD8) 116 # define GPNVM_NUM_MAX 3 118 # define IFLASH_PAGE_SIZE IFLASH0_PAGE_SIZE 120 # define IFLASH_LOCK_REGION_SIZE IFLASH0_LOCK_REGION_SIZE 121 #elif (SAM4C32 || SAM4CMP32 || SAM4CMS32) 123 # define GPNVM_NUM_MAX 3 126 # define GPNVM_NUM_MAX 8 127 #elif (SAMV71 || SAMV70 || SAMS70 || SAME70) 129 # define GPNVM_NUM_MAX 9 132 # define GPNVM_NUM_MAX 2 135 #if (SAM4C || SAM4CP || SAM4CM) 136 #if (SAM4C32 || SAM4CMP32 || SAM4CMS32) 139 # define IFLASH0_ADDR IFLASH0_CNC_ADDR 140 # define IFLASH1_ADDR IFLASH1_CNC_ADDR 141 # define IFLASH_ADDR IFLASH0_ADDR 143 # define IFLASH_PAGE_SIZE IFLASH0_PAGE_SIZE 145 # define IFLASH_LOCK_REGION_SIZE IFLASH0_LOCK_REGION_SIZE 147 #define IFLASH_ADDR IFLASH_CNC_ADDR 165 uint16_t *pus_page, uint16_t *pus_offset)
171 #if (SAM3XA || SAM3U4) 172 if (ul_addr >= IFLASH1_ADDR) {
174 us_page = (ul_addr - IFLASH1_ADDR) / IFLASH1_PAGE_SIZE;
175 us_offset = (ul_addr - IFLASH1_ADDR) % IFLASH1_PAGE_SIZE;
178 us_page = (ul_addr - IFLASH0_ADDR) / IFLASH0_PAGE_SIZE;
179 us_offset = (ul_addr - IFLASH0_ADDR) % IFLASH0_PAGE_SIZE;
181 #elif (SAM4SD16 || SAM4SD32 || SAM4C32 || SAM4CMP32 || SAM4CMS32) 184 if (ul_addr >= IFLASH1_ADDR) {
190 us_page = (ul_addr - IFLASH1_ADDR) / IFLASH1_PAGE_SIZE;
191 us_offset = (ul_addr - IFLASH1_ADDR) % IFLASH1_PAGE_SIZE;
198 us_page = (ul_addr - IFLASH0_ADDR) / IFLASH0_PAGE_SIZE;
199 us_offset = (ul_addr - IFLASH0_ADDR) % IFLASH0_PAGE_SIZE;
201 #elif (SAM3S8 || SAM3SD8) 203 us_page = (ul_addr - IFLASH0_ADDR) / IFLASH0_PAGE_SIZE;
204 us_offset = (ul_addr - IFLASH0_ADDR) % IFLASH0_PAGE_SIZE;
224 *pus_offset = us_offset;
244 #if (SAM4SD16 || SAM4SD32 || SAM4C32 || SAM4CMP32 || SAM4CMS32) 261 ul_addr = (p_efc == EFC0) ?
274 if (pul_addr !=
NULL) {
288 uint32_t *pul_actual_start, uint32_t *pul_actual_end)
290 uint32_t ul_actual_start, ul_actual_end;
296 if (pul_actual_start) {
297 *pul_actual_start = ul_actual_start;
300 if (pul_actual_end) {
301 *pul_actual_end = ul_actual_end;
364 #if (SAM3XA || SAM3U) 370 #elif (SAM4S || SAM4E || SAM4N || SAM4C || SAM4CP || SAM4CM || \ 371 SAMV71 || SAMV70 || SAMS70 || SAME70) 413 uint32_t *pul_flash_descriptor, uint32_t ul_size)
426 for (ul_cnt = 0;; ul_cnt++) {
428 if ((ul_size > ul_cnt) && (ul_tmp != 0)) {
429 *pul_flash_descriptor++ = ul_tmp;
451 return (pul_flash_descriptor[1] / pul_flash_descriptor[2]);
466 return (pul_flash_descriptor[4] / pul_flash_descriptor[2]);
481 return (pul_flash_descriptor[3]);
508 #if (SAM3S8 || SAM3SD8) 519 uint32_t flash_erase_plane(uint32_t ul_address)
534 #if (SAM4S || SAM4E || SAM4N || SAM4C || SAMG || SAM4CP || SAM4CM || \ 535 SAMV71 || SAMV70 || SAMS70 || SAME70) 543 uint32_t flash_erase_page(uint32_t ul_address, uint8_t uc_page_num)
559 (us_page | uc_page_num))) {
576 uint32_t flash_erase_sector(uint32_t ul_address)
608 uint32_t ul_size, uint32_t ul_erase_flag)
611 uint32_t ul_fws_temp;
615 uint32_t ul_page_addr;
619 uint32_t *p_aligned_dest;
624 #if SAM3S || SAM3N || SAM3XA || SAM3U 633 while (ul_size > 0) {
641 memcpy(puc_page_buffer, (
void *)ul_page_addr, us_offset);
644 memcpy(puc_page_buffer + us_offset, p_buffer, writeSize);
647 memcpy(puc_page_buffer + us_offset + writeSize,
648 (
void *)(ul_page_addr + us_offset + writeSize),
656 p_aligned_dest = (uint32_t *) ul_page_addr;
660 *p_aligned_dest = tmp;
677 p_buffer = (
void *)((uint32_t) p_buffer + writeSize);
678 ul_size -= writeSize;
683 #if SAM3S || SAM3N || SAM3XA || SAM3U 704 uint32_t *pul_actual_start, uint32_t *pul_actual_end)
707 uint32_t ul_actual_start, ul_actual_end;
708 uint16_t us_start_page, us_end_page;
710 uint16_t us_num_pages_in_region =
716 if (pul_actual_start !=
NULL) {
717 *pul_actual_start = ul_actual_start;
720 if (pul_actual_end !=
NULL) {
721 *pul_actual_end = ul_actual_end;
729 while (us_start_page < us_end_page) {
735 us_start_page += us_num_pages_in_region;
753 uint32_t *pul_actual_start, uint32_t *pul_actual_end)
756 uint32_t ul_actual_start, ul_actual_end;
757 uint16_t us_start_page, us_end_page;
759 uint16_t us_num_pages_in_region =
764 if (pul_actual_start !=
NULL) {
765 *pul_actual_start = ul_actual_start;
767 if (pul_actual_end !=
NULL) {
768 *pul_actual_end = ul_actual_end;
776 while (us_start_page < us_end_page) {
782 us_start_page += us_num_pages_in_region;
799 uint16_t us_start_page, us_end_page;
800 uint8_t uc_start_region, uc_end_region;
801 uint16_t us_num_pages_in_region;
804 uint32_t ul_num_locked_regions = 0;
805 uint32_t ul_count = 0;
808 Assert(ul_end >= ul_start);
811 Assert(((ul_start >= IFLASH0_ADDR)
812 && (ul_end <= IFLASH0_ADDR + IFLASH0_SIZE))
813 || ((ul_start >= IFLASH1_ADDR)
814 && (ul_end <= IFLASH1_ADDR + IFLASH1_SIZE)));
826 uc_start_region = us_start_page / us_num_pages_in_region;
827 uc_end_region = us_end_page / us_num_pages_in_region;
838 while (!(ul_count <= uc_start_region &&
839 uc_start_region < (ul_count + 32))) {
845 ul_bit = uc_start_region - ul_count;
848 ul_count = uc_end_region - uc_start_region + 1;
850 while (ul_count > 0) {
851 if (ul_status & (1 << (ul_bit))) {
852 ul_num_locked_regions++;
863 return ul_num_locked_regions;
925 uint32_t ul_gpnvm_bits;
936 if (ul_gpnvm_bits & (1 << ul_gpnvm)) {
988 for (ul_idx = 0; ul_idx < ul_size; ul_idx++) {
989 pul_data[ul_idx] = uid_buf[ul_idx];
995 #if (SAM4S || SAM4E || SAM4N || SAM4C || SAMG || SAM4CP || SAM4CM || \ 996 SAMV71 || SAMV70 || SAMS70 || SAME70) 1005 uint32_t flash_read_user_signature(uint32_t *p_data, uint32_t ul_size)
1007 if (ul_size > (FLASH_USER_SIG_SIZE /
sizeof(uint32_t))) {
1009 ul_size = FLASH_USER_SIG_SIZE /
sizeof(uint32_t);
1014 EFC_FCMD_SPUS, p_data, ul_size)) {
1029 uint32_t flash_write_user_signature(
const void *p_buffer, uint32_t ul_size)
1041 ul_size *
sizeof(uint32_t));
1050 *p_dest++ = gs_ul_page_buffer[ul_idx];
1066 uint32_t flash_erase_user_signature(
void)
1081
#define UNUSED(v)
Marking v as a unused parameter or value.
uint32_t flash_enable_security_bit(void)
Set security bit.
uint32_t efc_init(Efc *p_efc, uint32_t ul_access_mode, uint32_t ul_fws)
Initialize the EFC controller.
uint32_t efc_get_wait_state(Efc *p_efc)
Get flash wait state.
#define EFC_FCMD_STUI
Start unique ID.
static uint32_t sysclk_get_cpu_hz(void)
Return the current rate in Hz of the CPU clock.
uint32_t flash_init(uint32_t ul_mode, uint32_t ul_fws)
Initialize the flash service.
Embedded Flash service for SAM.
#define EFC_FCMD_CLB
Clear Lock Bit.
#define Min(a, b)
Takes the minimal value of a and b.
uint32_t flash_get_page_count(const uint32_t *pul_flash_descriptor)
Get flash total page count for the specified bank.
#define EFC_FCMD_SGPB
Set GPNVM Bit.
uint32_t efc_perform_command(Efc *p_efc, uint32_t ul_command, uint32_t ul_argument)
Perform the given command and wait until its completion (or an error).
#define EFC_FCMD_GGPB
Get GPNVM Bit.
void efc_set_wait_state(Efc *p_efc, uint32_t ul_fws)
Set flash wait state.
#define IFLASH_LOCK_REGION_SIZE
#define CHIP_FREQ_FWS_0
Maximum operating frequency when FWS is 0.
#define EFC_FCMD_EWP
Erase page and write page.
uint32_t flash_lock(uint32_t ul_start, uint32_t ul_end, uint32_t *pul_actual_start, uint32_t *pul_actual_end)
Lock all the regions in the given address range. The actual lock range is reported through two output...
__no_inline RAMFUNC uint32_t efc_perform_read_sequence(Efc *p_efc, uint32_t ul_cmd_st, uint32_t ul_cmd_sp, uint32_t *p_ul_buf, uint32_t ul_size)
Perform read sequence. Supported sequences are read Unique ID and read User Signature.
#define EFC_FCMD_WP
Write page.
uint32_t flash_clear_gpnvm(uint32_t ul_gpnvm)
Clear the given GPNVM bit.
uint32_t flash_is_locked(uint32_t ul_start, uint32_t ul_end)
Get the number of locked regions inside the given address range.
uint32_t flash_erase_all(uint32_t ul_address)
Erase the entire flash.
#define EFC_FCMD_SLB
Set Lock Bit.
static uint32_t gs_ul_page_buffer[IFLASH_PAGE_SIZE/sizeof(uint32_t)]
static void compute_lock_range(uint32_t ul_start, uint32_t ul_end, uint32_t *pul_actual_start, uint32_t *pul_actual_end)
Compute the lock range associated with the given address range.
uint32_t efc_get_result(Efc *p_efc)
Get the result of the last executed command.
#define EFC_FCMD_SPUI
Stop unique ID.
static void translate_address(Efc **pp_efc, uint32_t ul_addr, uint16_t *pus_page, uint16_t *pus_offset)
Translate the given flash address to page and offset values.
uint32_t flash_set_wait_state_adaptively(uint32_t ul_address)
Set flash wait state.
static void compute_address(Efc *p_efc, uint16_t us_page, uint16_t us_offset, uint32_t *pul_addr)
Compute the address of a flash by the given page and offset.
#define EFC_FCMD_GETD
Get Flash Descriptor.
uint32_t flash_get_page_count_per_region(const uint32_t *pul_flash_descriptor)
Get flash page count per region (plane) for the specified bank.
#define CHIP_FREQ_FWS_3
Maximum operating frequency when FWS is 3.
#define EFC
(EFC ) Base Address
uint32_t flash_write(uint32_t ul_address, const void *p_buffer, uint32_t ul_size, uint32_t ul_erase_flag)
Write a data buffer on flash.
uint32_t flash_is_gpnvm_set(uint32_t ul_gpnvm)
Check if the given GPNVM bit is set or not.
uint32_t flash_get_descriptor(uint32_t ul_address, uint32_t *pul_flash_descriptor, uint32_t ul_size)
Get flash descriptor.
#define CHIP_FREQ_FWS_2
Maximum operating frequency when FWS is 2.
#define CHIP_FREQ_FWS_4
Maximum operating frequency when FWS is 4.
uint32_t flash_get_wait_state(uint32_t ul_address)
Get flash wait state.
uint32_t flash_is_security_bit_enabled(void)
Check if the security bit is set or not.
uint32_t flash_set_gpnvm(uint32_t ul_gpnvm)
Set the given GPNVM bit.
#define EFC_FCMD_EA
Erase all.
#define Assert(expr)
This macro is used to test fatal errors.
uint32_t flash_set_wait_state(uint32_t ul_address, uint32_t ul_fws)
Set flash wait state.
#define CHIP_FREQ_FWS_1
Maximum operating frequency when FWS is 1.
uint32_t flash_get_region_count(const uint32_t *pul_flash_descriptor)
Get flash region (plane) count for the specified bank.
uint32_t flash_unlock(uint32_t ul_start, uint32_t ul_end, uint32_t *pul_actual_start, uint32_t *pul_actual_end)
Unlock all the regions in the given address range. The actual unlock range is reported through two ou...
#define EFC_FCMD_CGPB
Clear GPNVM Bit.
#define EFC_FCMD_GLB
Get Lock Bit.
uint32_t flash_read_unique_id(uint32_t *pul_data, uint32_t ul_size)
Read the flash unique ID.