/* * Copyright 2018 NXP * All rights reserved. * * SPDX-License-Identifier: BSD-3-Clause */ #include "fsl_debug_console.h" #include "board.h" #include "fsl_iap.h" #include "fsl_iap_ffr.h" #include "fsl_common.h" #include "pin_mux.h" #include "fsl_power.h" #include "fsl_shell.h" //////////////////////////////////////////////////////////////////////////////// // Definitions //////////////////////////////////////////////////////////////////////////////// #define SHELL_Printf PRINTF //////////////////////////////////////////////////////////////////////////////// // Prototypes //////////////////////////////////////////////////////////////////////////////// static void verify_status(status_t status); static void error_trap(); static shell_status_t ProtectFlashCfg(shell_handle_t shellHandle, int32_t argc, char **argv); //////////////////////////////////////////////////////////////////////////////// // Variables //////////////////////////////////////////////////////////////////////////////// #define BUFFER_LEN 512 / 4 static uint32_t s_buffer_rbc[BUFFER_LEN]; volatile uint32_t g_UUID[4]; volatile uint32_t g_CMPAData[512/4]; SHELL_COMMAND_DEFINE(flash, "\r\n\"flash arg1\":\r\n Usage:\r\n arg1: 0|1|2... \r\n" " 0: read chip UUID, 1: disable flash protect 2: enable flash protect\r\n", ProtectFlashCfg, 1); SDK_ALIGN(static uint8_t s_shellHandleBuffer[SHELL_HANDLE_SIZE], 4); static shell_handle_t s_shellHandle; extern serial_handle_t g_serialHandle; //////////////////////////////////////////////////////////////////////////////// // Code //////////////////////////////////////////////////////////////////////////////// flash_config_t flashInstance; static shell_status_t ProtectFlashCfg(shell_handle_t shellHandle, int32_t argc, char **argv) { int32_t kCommandIndex = ((int32_t)atoi(argv[1])); static uint32_t status; switch (kCommandIndex) { /* 0, Read chip UUID */ case 0: PRINTF("Read CHIP UUID and default value...\r\n"); /* Initialize flash driver */ FLASH_Init(&flashInstance); if (FFR_Init(&flashInstance) == kStatus_Success) { PRINTF("Flash init successfull!!. Halting...\r\n"); } else { error_trap(); } status = FFR_GetUUID(&flashInstance, (uint8_t *)g_UUID); PRINTF("UUID 0x%8x 0x%8x 0x%8x 0x%8x\r\n", g_UUID[0], g_UUID[1], g_UUID[2], g_UUID[3]); status = FFR_GetCustomerData(&flashInstance, (uint8_t *)g_CMPAData, 0x0, 512); PRINTF("CMPA 0x%8x 0x%8x 0x%8x 0x%8x\r\n", g_CMPAData[0], g_CMPAData[1], g_CMPAData[2], g_CMPAData[3]); PRINTF("CMPA 0x%8x 0x%8x 0x%8x 0x%8x\r\n", g_CMPAData[4], g_CMPAData[5], g_CMPAData[6], g_CMPAData[7]); PRINTF("CMPA 0x%8x 0x%8x 0x%8x 0x%8x\r\n", g_CMPAData[8], g_CMPAData[9], g_CMPAData[10], g_CMPAData[11]); PRINTF("CMPA Read = %x Done!\r\n", status); break; /* 1, disable flash protect */ case 1: PRINTF("Disable Flash Protect...\r\n"); /* Initialize flash driver */ FLASH_Init(&flashInstance); if (FFR_Init(&flashInstance) == kStatus_Success) { PRINTF("Flash init successfull!!. Halting...\r\n"); } else { error_trap(); } status = FFR_GetUUID(&flashInstance, (uint8_t *)g_UUID); PRINTF("UUID 0x%8x 0x%8x 0x%8x 0x%8x\r\n", g_UUID[0], g_UUID[1], g_UUID[2], g_UUID[3]); status = FFR_GetCustomerData(&flashInstance, (uint8_t *)g_CMPAData, 0x0, 512); PRINTF("CMPA 0x%8x 0x%8x 0x%8x 0x%8x\r\n", g_CMPAData[0], g_CMPAData[1], g_CMPAData[2], g_CMPAData[3]); PRINTF("CMPA 0x%8x 0x%8x 0x%8x 0x%8x\r\n", g_CMPAData[4], g_CMPAData[5], g_CMPAData[6], g_CMPAData[7]); PRINTF("CMPA 0x%8x 0x%8x 0x%8x 0x%8x\r\n", g_CMPAData[8], g_CMPAData[9], g_CMPAData[10], g_CMPAData[11]); PRINTF("CMPA Read = %x Done!\r\n", status); /* Clean-up CMPA area */ g_CMPAData[4] = 0; // 4 and 5 must clean same time g_CMPAData[5] = 0; FFR_CustFactoryPageWrite(&flashInstance, (uint8_t *)g_CMPAData, 0); PRINTF("CMPA Write = %x Done!\r\n", status); status = FFR_GetCustomerData(&flashInstance, (uint8_t *)g_CMPAData, 0x0, 512); PRINTF("CMPA 0x%8x 0x%8x 0x%8x 0x%8x\r\n", g_CMPAData[0], g_CMPAData[1], g_CMPAData[2], g_CMPAData[3]); PRINTF("CMPA 0x%8x 0x%8x 0x%8x 0x%8x\r\n", g_CMPAData[4], g_CMPAData[5], g_CMPAData[6], g_CMPAData[7]); PRINTF("CMPA 0x%8x 0x%8x 0x%8x 0x%8x\r\n", g_CMPAData[8], g_CMPAData[9], g_CMPAData[10], g_CMPAData[11]); break; /* Enable flash protect */ case 2: /* Initialize flash driver */ PRINTF("Enable Flash protect...\r\n"); FLASH_Init(&flashInstance); if (FFR_Init(&flashInstance) == kStatus_Success) { PRINTF("Flash init successfull!!. Halting...\r\n"); } else { error_trap(); } status = FFR_GetUUID(&flashInstance, (uint8_t *)g_UUID); PRINTF("UUID 0x%8x 0x%8x 0x%8x 0x%8x\r\n", g_UUID[0], g_UUID[1], g_UUID[2], g_UUID[3]); status = FFR_GetCustomerData(&flashInstance, (uint8_t *)g_CMPAData, 0x0, 512); PRINTF("CMPA 0x%8x 0x%8x 0x%8x 0x%8x\r\n", g_CMPAData[0], g_CMPAData[1], g_CMPAData[2], g_CMPAData[3]); PRINTF("CMPA 0x%8x 0x%8x 0x%8x 0x%8x\r\n", g_CMPAData[4], g_CMPAData[5], g_CMPAData[6], g_CMPAData[7]); PRINTF("CMPA 0x%8x 0x%8x 0x%8x 0x%8x\r\n", g_CMPAData[8], g_CMPAData[9], g_CMPAData[10], g_CMPAData[11]); PRINTF("CMPA Read = %x Done!\r\n", status); // Erase Dis ISP Dis JTAG Dis DGBEN Dis NIDEN uint16_t CMPAData = 0x0000; // (0 << 8) | (1 << 6) | (1 << 4) | (1 << 1) | (1 << 0); uint16_t CMPADataReverse = ~CMPAData; #if 0 g_CMPAData[4] |= (0 << 8) | (1 << 6) | (1 << 4) | (1 << 1); FFR_CustFactoryPageWrite(&flashInstance, (uint8_t *)g_CMPAData, 0); PRINTF("CMPA Write = %x Done!\r\n", status); #else uint32_t CMPALockData = (CMPADataReverse<<16)|(CMPAData); g_CMPAData[4] = CMPALockData; // 4 and 5 must set as same value g_CMPAData[5] = CMPALockData; FFR_CustFactoryPageWrite(&flashInstance, (uint8_t *)g_CMPAData, 0); PRINTF("CMPA Write = %x %x %x Done!\r\n", CMPAData, CMPADataReverse, CMPALockData); #endif status = FFR_GetCustomerData(&flashInstance, (uint8_t *)g_CMPAData, 0x0, 512); PRINTF("CMPA 0x%8x 0x%8x 0x%8x 0x%8x\r\n", g_CMPAData[0], g_CMPAData[1], g_CMPAData[2], g_CMPAData[3]); PRINTF("CMPA 0x%8x 0x%8x 0x%8x 0x%8x\r\n", g_CMPAData[4], g_CMPAData[5], g_CMPAData[6], g_CMPAData[7]); PRINTF("CMPA 0x%8x 0x%8x 0x%8x 0x%8x\r\n", g_CMPAData[8], g_CMPAData[9], g_CMPAData[10], g_CMPAData[11]); break; default: SHELL_Printf("Command index is wrong\r\n"); break; } return kStatus_SHELL_Success; } /*! @brief Main function */ int main() { uint32_t destAdrss; /* Address of the target location */ const uint32_t s_buffer[BUFFER_LEN] = {1, 2, 3, 4}; /* Init board hardware. */ /* set BOD VBAT level to 1.65V */ POWER_SetBodVbatLevel(kPOWER_BodVbatLevel1650mv, kPOWER_BodHystLevel50mv, false); /* attach 12 MHz clock to FLEXCOMM0 (debug console) */ CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH); /* enable clock for GPIO*/ CLOCK_EnableClock(kCLOCK_Gpio0); CLOCK_EnableClock(kCLOCK_Gpio1); BOARD_InitPins(); BOARD_BootClockFROHF96M(); BOARD_InitDebugConsole(); /* Init SHELL */ s_shellHandle = &s_shellHandleBuffer[0]; SHELL_Init(s_shellHandle, g_serialHandle, "SHELL>> "); /* Add new command to commands list */ SHELL_RegisterCommand(s_shellHandle, SHELL_COMMAND(flash)); #if !(defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U)) SHELL_Task(s_shellHandle); #endif /* Print basic information for Flash Driver API.*/ PRINTF("\r\nEnable/Disable LPC55S6x Flash Read example...\r\n"); while (1) { } } void verify_status(status_t status) { char *tipString = "Unknown status"; switch (status) { case kStatus_Success: tipString = "Done."; break; case kStatus_InvalidArgument: tipString = "Invalid argument."; break; case kStatus_FLASH_AlignmentError: tipString = "Alignment Error."; break; case kStatus_FLASH_AccessError: tipString = "Flash Access Error."; break; case kStatus_FLASH_CommandNotSupported: tipString = "This API is not supported in current target."; break; default: break; } PRINTF("%s\r\n\r\n", tipString); } /* * @brief Gets called when an error occurs. */ void error_trap(void) { PRINTF("\r\n\r\n\r\n\t---- HALTED DUE TO FLASH ERROR! ----"); while (1) { } } //////////////////////////////////////////////////////////////////////////////// // EOF ////////////////////////////////////////////////////////////////////////////////