u32 EraseLocalFlashKeyValues_u32 (void)
{
  u32 i;
  u32 i1;

// Clear user page
  for (i1=0;i1<5;i1++)
  {
    for (i=0;i<256;i++)
    {
      EraseStoreData_au8[i] = (u8)(rand () % 256);
    }
    flashc_memcpy((void*)AVR32_FLASHC_USER_PAGE,EraseStoreData_au8,256,TRUE);
  }

  flashc_erase_user_page (TRUE);

// Set default values
  InitStickConfigurationToUserPage_u8 ();

  DFU_DisableFirmwareUpdate ();     // Stick always starts in application mode
  CheckForNewSdCard ();             // Get SD ID

// Clear password safe
  for (i1=0;i1<5;i1++)
  {
    for (i=0;i<256;i++)
    {
      EraseStoreData_au8[i] = (u8)(rand () % 256);
    }
    flashc_memcpy((void*)(PWS_FLASH_START_ADDRESS    ),EraseStoreData_au8,256,TRUE);
    flashc_memcpy((void*)(PWS_FLASH_START_ADDRESS+256),EraseStoreData_au8,256,TRUE);
  }

  flashc_erase_page(PWS_FLASH_START_PAGE,TRUE);

// Clear OTP
  for (i1=0;i1<5;i1++)
  {
    for (i=0;i<256;i++)
    {
      EraseStoreData_au8[i] = (u8)(rand () % 256);
    }
    for (i=0;i<10;i++)
    {
      flashc_memcpy((void*)(SLOTS_ADDRESS+i*512    ),EraseStoreData_au8,256,TRUE);
      flashc_memcpy((void*)(SLOTS_ADDRESS+i*512+256),EraseStoreData_au8,256,TRUE);
    }
  }

  for (i=0;i<10;i++)
  {
    flashc_erase_page(OTP_FLASH_START_PAGE+i,TRUE);
  }

// Clear hidden volumes
  for (i1=0;i1<5;i1++)
  {
    for (i=0;i<256;i++)
    {
      EraseStoreData_au8[i] = (u8)(rand () % 256);
    }
    for (i=0;i<2;i++)
    {
      flashc_memcpy((void*)(HV_MAGIC_NUMBER_ADDRESS+i*512    ),EraseStoreData_au8,256,TRUE);
      flashc_memcpy((void*)(HV_MAGIC_NUMBER_ADDRESS+i*512+256),EraseStoreData_au8,256,TRUE);
    }
  }
  for (i=0;i<10;i++)
  {
    flashc_erase_page(HV_FLASH_START_PAGE+i,TRUE);
  }

  return (TRUE);
}
void IBN_DFU_Tests (unsigned char nParamsGet_u8,unsigned char CMD_u8,unsigned int Param_u32,unsigned char *String_pu8)
{
  u8 DFU_String_au8[4];

  if (0 == nParamsGet_u8)
  {
    CI_LocalPrintf ("DFU test functions\r\n");
    CI_LocalPrintf ("\r\n");
    CI_LocalPrintf ("0   Show ISP Config 1 word\r\n");
    CI_LocalPrintf ("1   Enable DFU at next start\r\n");
    CI_LocalPrintf ("2   Disable DFU at next start\r\n");
    CI_LocalPrintf ("3   Show security bit\r\n");
    CI_LocalPrintf ("4   Set security bit\r\n");
    CI_LocalPrintf ("5   Show bootloader protected size\r\n");
    CI_LocalPrintf ("6   Set bootloader protected size = 0x2000\r\n");
    CI_LocalPrintf ("7   Reset system\r\n");
    CI_LocalPrintf ("\r\n");
    return;
  }

  switch (CMD_u8)
  {
    case 0:
      memcpy (DFU_String_au8,(void*)TOOL_DFU_ISP_CONFIG_ADDR_1,4);
      CI_LocalPrintf ("ISP Config 1 word : ");
      HexPrint (4,DFU_String_au8);
      CI_LocalPrintf ("\r\n");
      break;

    case 1 :
      CI_LocalPrintf ("Enable DFU\r\n");
      DFU_EnableFirmwareUpdate ();
      break;

    case 2 :
      CI_LocalPrintf ("Disable DFU\r\n");
      DFU_DisableFirmwareUpdate ();
      break;

    case 3 :
      CI_LocalPrintf ("Security bit : %d\r\n",flashc_is_security_bit_active ());
      break;

    case 4 :
      CI_LocalPrintf ("Activate security bit\r\n");
      flashc_activate_security_bit ();
      break;

    case 5 :
      CI_LocalPrintf ("Bootloader protected : 0x%04x\r\n",flashc_get_bootloader_protected_size ());
      break;

    case 6 :
      CI_LocalPrintf ("Set bootloader protected 0x2000\r\n");
      flashc_set_bootloader_protected_size (0x2000);
      break;

    case 7 :
      DFU_ResetCPU ();
      break;

  }
}
int main (void)
{
    pcl_freq_param_t local_pcl_freq_param;

    /*
       USART_Int_Test ();

       return (0); */

    // Configure system clocks.
    local_pcl_freq_param = pcl_freq_param;

    if (pcl_configure_clocks (&local_pcl_freq_param) != PASS)
    {
        return 42;
    }

    /* Load the Exception Vector Base Address in the corresponding system register. */
    Set_system_register (AVR32_EVBA, (int) &_evba);

    /* Enable exceptions. */
    ENABLE_ALL_EXCEPTIONS ();

    /* Initialize interrupt handling. */
    INTC_init_interrupts ();

#if ((defined SD_MMC_MCI_0_MEM) && (SD_MMC_MCI_0_MEM == ENABLE)) || ((defined SD_MMC_MCI_1_MEM) && (SD_MMC_MCI_1_MEM == ENABLE))
    sd_mmc_mci_resources_init ();
#endif


#ifdef FREERTOS_USED
    if (!ctrl_access_init ())
    {
        return 42;
    }
#endif // FREERTOS_USED

    // Init Hmatrix bus
    init_hmatrix ();

    // Initialize USB clock.
    pcl_configure_usb_clock ();

    /*
       SmartCard_test (); return 42;

       Test_ALL_Pins ();

       Test_MCI_Pins ();

       Test_LEDs_Pins ();

       TestUart0 ();

       while (1); return (0); */

#ifdef TIME_MEASURING_ENABLE
    TIME_MEASURING_Init ();
#endif


    // For debugging
    BUFFERED_SIO_Init ();

#ifdef INTERPRETER_ENABLE
    IDF_task_init ();
#endif

    // Internal work task - FAT Access
    IW_task_init ();

    // Initialize USB tasks.
    usb_task_init ();




#if USB_DEVICE_FEATURE == ENABLED
#ifdef  USB_MSD
    device_mass_storage_task_init ();
#endif // USB_MSD
#endif


    // CCID_Test_task_init ();
#ifdef  USB_CCID
    USB_CCID_task_init ();
#endif

    DFU_DisableFirmwareUpdate ();   // Stick always starts in application mode

     /**/
        // Protect bootloader
#ifdef STICK_20_A_MUSTER_PROD   //
        flashc_set_bootloader_protected_size (0x2000);
//        flashc_activate_security_bit ();    // Debugging disabled, only chip erase works (bootloader is save) , AES storage keys and setup
                                        // are erased
        flashc_lock_external_privileged_fetch (TRUE);     // Disable external instruction fetch
#endif

    // DFU_FirmwareResetUserpage ();


    // Set BOD33 detection reset to 3.06 Volt
    pm_bod33_ResetDisable ();
    pm_bod33_set_level (AVR32_PM_BOD33_LEVEL_306_VOLT);
    pm_bod33_ResetEnable ();

    // ushell_task_init(pcl_freq_param.pba_f);


    // Create the semaphore
    vSemaphoreCreateBinary (AES_semphr);


    // Wait for the semaphore
    while (!xSemaphoreTake (AES_semphr, portMAX_DELAY));

    // Release the semaphore in order to start a new device/host task
    portBASE_TYPE task_woken = pdFALSE;
/*
    taskENTER_CRITICAL ();
    xSemaphoreGiveFromISR (AES_semphr, &task_woken);
    taskEXIT_CRITICAL ();
*/
    taskENTER_CRITICAL ();
    xSemaphoreGive (AES_semphr);
    taskEXIT_CRITICAL ();


    // Start stick
    vTaskStartScheduler ();

    // It never gets to this point
    return 42;
}