Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | /* * Copyright (c) 2017 Linaro Limited. * * SPDX-License-Identifier: Apache-2.0 */ #include <stdlib.h> #include <zephyr.h> #include <flash.h> #include <misc/printk.h> #include <shell/shell.h> /* Assumption: our devices have less than 64MB of memory */ #define RESERVED_MEM_MAP (CONFIG_SRAM_BASE_ADDRESS + 0x4000000) #define FLASH_MEM CONFIG_FLASH_BASE_ADDRESS #define RAM_MEM CONFIG_SRAM_BASE_ADDRESS /* MPU test command help texts */ #define READ_CMD_HELP "Read from a reserved address in the memory map" #define WRITE_CMD_HELP "Write in to boot FLASH/ROM" #define RUN_CMD_HELP "Run code located in RAM" #define MTEST_CMD_HELP "Memory Test writes or reads a memory location" static int shell_cmd_read(int argc, char *argv[]) { ARG_UNUSED(argc); ARG_UNUSED(argv); u32_t *p_mem = (u32_t *) RESERVED_MEM_MAP; /* Reads from an address that is reserved in the memory map */ printk("The value is: %d", *p_mem); return 0; } #if defined(CONFIG_SOC_FLASH_MCUX) static int shell_cmd_write_mcux(int argc, char *argv[]) { ARG_UNUSED(argc); ARG_UNUSED(argv); struct device *flash_dev; flash_dev = device_get_binding(FLASH_DEV_NAME); u32_t value[2]; /* 128K reserved to the application */ u32_t offset = FLASH_MEM + 0x20000; value[0] = 0xBADC0DE; value[1] = 0xBADC0DE; printk("write address: 0x%x\n", offset); flash_write_protection_set(flash_dev, false); if (flash_write(flash_dev, offset, value, sizeof(value)) != 0) { printk("Flash write failed!\n"); return 1; } flash_write_protection_set(flash_dev, true); return 0; } #elif defined(CONFIG_SOC_FLASH_STM32) static int shell_cmd_write_stm32(int argc, char *argv[]) { ARG_UNUSED(argc); ARG_UNUSED(argv); struct device *flash_dev; flash_dev = device_get_binding(FLASH_DEV_NAME); /* 16K reserved to the application */ u32_t offset = FLASH_MEM + 0x4000; u32_t value = 0xBADC0DE; printk("write address: 0x%x\n", offset); flash_write_protection_set(flash_dev, false); if (flash_write(flash_dev, offset, &value, sizeof(value)) != 0) { printk("Flash write failed!\n"); return 1; } flash_write_protection_set(flash_dev, true); return 0; } #else static int shell_cmd_write(int argc, char *argv[]) { ARG_UNUSED(argc); ARG_UNUSED(argv); /* 16K reserved to the application */ u32_t *p_mem = (u32_t *) (FLASH_MEM + 0x4000); printk("write address: 0x%x\n", FLASH_MEM + 0x4000); /* Write in to boot FLASH/ROM */ *p_mem = 0xBADC0DE; return 0; } #endif /* SOC_FLASH_MCUX */ static int shell_cmd_run(int argc, char *argv[]) { ARG_UNUSED(argc); ARG_UNUSED(argv); void (*func_ptr)(void) = (void (*)(void)) RAM_MEM; /* Run code located in RAM */ func_ptr(); return 0; } static int shell_cmd_mtest(int argc, char *argv[]) { if (argc > 3) { printk("mtest accepts 1 (Read) or 2 (Write) parameters\n"); return 1; } u32_t val = (u32_t)strtol(argv[1], NULL, 16); u32_t *mem = (u32_t *) val; if (argc == 2) { printk("The value is: 0x%x\n", *mem); } else { *mem = (u32_t) strtol(argv[2], NULL, 16); } return 0; } #define SHELL_MODULE "mpu_test" static struct shell_cmd commands[] = { { "read", shell_cmd_read, READ_CMD_HELP}, #if defined(CONFIG_SOC_FLASH_MCUX) { "write", shell_cmd_write_mcux, WRITE_CMD_HELP }, #elif defined(CONFIG_SOC_FLASH_STM32) { "write", shell_cmd_write_stm32, WRITE_CMD_HELP }, #else { "write", shell_cmd_write, WRITE_CMD_HELP }, #endif /* SOC_FLASH_MCUX*/ { "run", shell_cmd_run, RUN_CMD_HELP }, { "mtest", shell_cmd_mtest, MTEST_CMD_HELP }, { NULL, NULL, NULL } }; void main(void) { SHELL_REGISTER(SHELL_MODULE, commands); } |