main.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /**
  2. * Copyright (c) 2014 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file
  41. * @defgroup flashwrite_example_main main.c
  42. * @{
  43. * @ingroup flashwrite_example
  44. *
  45. * @brief This file contains the source code for a sample application using the Flash Write Application.
  46. *a
  47. */
  48. #include <stdbool.h>
  49. #include <stdio.h>
  50. #include "nrf.h"
  51. #include "bsp.h"
  52. #include "app_error.h"
  53. #include "nrf_nvmc.h"
  54. #include "nordic_common.h"
  55. #include "nrf_log.h"
  56. #include "nrf_log_ctrl.h"
  57. #include "nrf_log_default_backends.h"
  58. #include "app_timer.h"
  59. #include "nrf_drv_clock.h"
  60. #include "nrf_cli.h"
  61. #include "nrf_cli_uart.h"
  62. #define FLASHWRITE_EXAMPLE_MAX_STRING_LEN (62u)
  63. #define FLASHWRITE_EXAMPLE_BLOCK_VALID (0xA55A5AA5)
  64. #define FLASHWRITE_EXAMPLE_BLOCK_INVALID (0xA55A0000)
  65. #define FLASHWRITE_EXAMPLE_BLOCK_NOT_INIT (0xFFFFFFFF)
  66. NRF_CLI_UART_DEF(m_cli_uart_transport, 0, 64, 16);
  67. NRF_CLI_DEF(m_cli_uart, "uart_cli:~$ ", &m_cli_uart_transport.transport, '\r', 4);
  68. typedef struct
  69. {
  70. uint32_t magic_number;
  71. uint32_t buffer[FLASHWRITE_EXAMPLE_MAX_STRING_LEN + 1]; // + 1 for end of string
  72. } flashwrite_example_flash_data_t;
  73. typedef struct
  74. {
  75. uint32_t addr;
  76. uint32_t pg_size;
  77. uint32_t pg_num;
  78. flashwrite_example_flash_data_t * m_p_flash_data;
  79. } flashwrite_example_data_t;
  80. static flashwrite_example_data_t m_data;
  81. static ret_code_t clock_config(void)
  82. {
  83. ret_code_t err_code;
  84. err_code = nrf_drv_clock_init();
  85. if (err_code != NRF_SUCCESS && err_code != NRF_ERROR_MODULE_ALREADY_INITIALIZED)
  86. {
  87. return err_code;
  88. }
  89. nrf_drv_clock_lfclk_request(NULL);
  90. return NRF_SUCCESS;
  91. }
  92. static void flash_page_init(void)
  93. {
  94. m_data.pg_num = NRF_FICR->CODESIZE - 1;
  95. m_data.pg_size = NRF_FICR->CODEPAGESIZE;
  96. m_data.addr = (m_data.pg_num * m_data.pg_size);
  97. m_data.m_p_flash_data = (flashwrite_example_flash_data_t *)m_data.addr;
  98. while (1)
  99. {
  100. if (m_data.m_p_flash_data->magic_number == FLASHWRITE_EXAMPLE_BLOCK_VALID)
  101. {
  102. return;
  103. }
  104. if (m_data.m_p_flash_data->magic_number == FLASHWRITE_EXAMPLE_BLOCK_INVALID)
  105. {
  106. ++m_data.m_p_flash_data;
  107. continue;
  108. }
  109. nrf_nvmc_page_erase(m_data.addr);
  110. return;
  111. }
  112. }
  113. /**
  114. * @brief Function for application main entry.
  115. */
  116. int main(void)
  117. {
  118. uint32_t err_code;
  119. APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
  120. err_code = app_timer_init();
  121. APP_ERROR_CHECK(err_code);
  122. err_code = clock_config();
  123. APP_ERROR_CHECK(err_code);
  124. flash_page_init();
  125. nrf_drv_uart_config_t uart_config = NRF_DRV_UART_DEFAULT_CONFIG;
  126. uart_config.pseltxd = TX_PIN_NUMBER;
  127. uart_config.pselrxd = RX_PIN_NUMBER;
  128. uart_config.hwfc = NRF_UART_HWFC_DISABLED;
  129. err_code = nrf_cli_init(&m_cli_uart, &uart_config, true, true, NRF_LOG_SEVERITY_INFO);
  130. APP_ERROR_CHECK(err_code);
  131. err_code = nrf_cli_start(&m_cli_uart);
  132. APP_ERROR_CHECK(err_code);
  133. NRF_LOG_RAW_INFO("Flashwrite example started.\r\n");
  134. NRF_LOG_RAW_INFO("Execute: <flash -h> for more information "
  135. "or press the Tab button to see all available commands.\r\n");
  136. while (true)
  137. {
  138. UNUSED_RETURN_VALUE(NRF_LOG_PROCESS());
  139. nrf_cli_process(&m_cli_uart);
  140. }
  141. }
  142. static void flash_string_write(uint32_t address, const char * src, uint32_t num_words)
  143. {
  144. uint32_t i;
  145. // Enable write.
  146. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen;
  147. while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
  148. {
  149. }
  150. for (i = 0; i < num_words; i++)
  151. {
  152. /* Only full 32-bit words can be written to Flash. */
  153. ((uint32_t*)address)[i] = 0x000000FFUL & (uint32_t)((uint8_t)src[i]);
  154. while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
  155. {
  156. }
  157. }
  158. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
  159. while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
  160. {
  161. }
  162. }
  163. static void flashwrite_erase_cmd(nrf_cli_t const * p_cli, size_t argc, char **argv)
  164. {
  165. nrf_nvmc_page_erase(m_data.addr);
  166. m_data.m_p_flash_data = (flashwrite_example_flash_data_t *)m_data.addr;
  167. }
  168. static void flashwrite_read_cmd(nrf_cli_t const * p_cli, size_t argc, char **argv)
  169. {
  170. flashwrite_example_flash_data_t * p_data = (flashwrite_example_flash_data_t *)m_data.addr;
  171. char string_buff[FLASHWRITE_EXAMPLE_MAX_STRING_LEN + 1]; // + 1 for end of string
  172. if ((p_data == m_data.m_p_flash_data) &&
  173. (p_data->magic_number != FLASHWRITE_EXAMPLE_BLOCK_VALID))
  174. {
  175. nrf_cli_fprintf(p_cli, NRF_CLI_WARNING, "Please write something first.\r\n");
  176. return;
  177. }
  178. while (p_data <= m_data.m_p_flash_data)
  179. {
  180. if ((p_data->magic_number != FLASHWRITE_EXAMPLE_BLOCK_VALID) &&
  181. (p_data->magic_number != FLASHWRITE_EXAMPLE_BLOCK_INVALID))
  182. {
  183. nrf_cli_fprintf(p_cli, NRF_CLI_WARNING, "Corrupted data found.\r\n");
  184. return;
  185. }
  186. uint8_t i;
  187. for (i = 0 ; i <= FLASHWRITE_EXAMPLE_MAX_STRING_LEN; i++)
  188. {
  189. string_buff[i] = (char)p_data->buffer[i];
  190. }
  191. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "%s\r\n", string_buff);
  192. ++p_data;
  193. }
  194. }
  195. static void flashwrite_write_cmd(nrf_cli_t const * p_cli, size_t argc, char **argv)
  196. {
  197. static uint16_t const page_size = 4096;
  198. if (argc < 2)
  199. {
  200. nrf_cli_fprintf(p_cli, NRF_CLI_ERROR, "%s:%s", argv[0], " bad parameter count\r\n");
  201. return;
  202. }
  203. if (argc > 2)
  204. {
  205. nrf_cli_fprintf(p_cli,
  206. NRF_CLI_WARNING,
  207. "%s:%s",
  208. argv[0],
  209. " bad parameter count - please use quotes\r\n");
  210. return;
  211. }
  212. uint32_t len = strlen(argv[1]);
  213. if (len > FLASHWRITE_EXAMPLE_MAX_STRING_LEN)
  214. {
  215. nrf_cli_fprintf(p_cli,
  216. NRF_CLI_ERROR,
  217. "Too long string. Please limit entered string to %d chars.\r\n",
  218. FLASHWRITE_EXAMPLE_MAX_STRING_LEN);
  219. return;
  220. }
  221. if ((m_data.m_p_flash_data->magic_number != FLASHWRITE_EXAMPLE_BLOCK_NOT_INIT) &&
  222. (m_data.m_p_flash_data->magic_number != FLASHWRITE_EXAMPLE_BLOCK_VALID))
  223. {
  224. nrf_cli_fprintf(p_cli, NRF_CLI_ERROR, "Flash corrupted, please errase it first.");
  225. return;
  226. }
  227. if (m_data.m_p_flash_data->magic_number == FLASHWRITE_EXAMPLE_BLOCK_VALID)
  228. {
  229. uint32_t new_end_addr = (uint32_t)(m_data.m_p_flash_data + 2);
  230. uint32_t diff = new_end_addr - m_data.addr;
  231. if (diff > page_size)
  232. {
  233. nrf_cli_fprintf(p_cli,
  234. NRF_CLI_WARNING,
  235. "Not enough space - please erase flash first.\r\n");
  236. return;
  237. }
  238. nrf_nvmc_write_word((uint32_t)&m_data.m_p_flash_data->magic_number,
  239. FLASHWRITE_EXAMPLE_BLOCK_INVALID);
  240. ++m_data.m_p_flash_data;
  241. }
  242. //++len -> store also end of string '\0'
  243. flash_string_write((uint32_t)m_data.m_p_flash_data->buffer, argv[1], ++len);
  244. nrf_nvmc_write_word((uint32_t)&m_data.m_p_flash_data->magic_number,
  245. FLASHWRITE_EXAMPLE_BLOCK_VALID);
  246. }
  247. static void flashwrite_cmd(nrf_cli_t const * p_cli, size_t argc, char **argv)
  248. {
  249. ASSERT(p_cli);
  250. ASSERT(p_cli->p_ctx && p_cli->p_iface && p_cli->p_name);
  251. if ((argc == 1) || nrf_cli_help_requested(p_cli))
  252. {
  253. nrf_cli_help_print(p_cli, NULL, 0);
  254. return;
  255. }
  256. nrf_cli_fprintf(p_cli, NRF_CLI_ERROR, "%s:%s%s\r\n", argv[0], " unknown parameter: ", argv[1]);
  257. }
  258. NRF_CLI_CREATE_STATIC_SUBCMD_SET(m_sub_flash)
  259. {
  260. NRF_CLI_CMD(erase, NULL, "Erase flash.", flashwrite_erase_cmd),
  261. NRF_CLI_CMD(read, NULL, "Read data from flash.", flashwrite_read_cmd),
  262. NRF_CLI_CMD(write, NULL, "Write data to flash.\n"
  263. "Limitations:\n"
  264. "- maximum 16 entries,\n"
  265. "- each entry is maximum 62 chars long.",
  266. flashwrite_write_cmd),
  267. NRF_CLI_SUBCMD_SET_END
  268. };
  269. NRF_CLI_CMD_REGISTER(flash, &m_sub_flash, "Flash access command.", flashwrite_cmd);
  270. /** @} */