Debian Codegraph @

ports/esp32/boards/ARDUINO_NANO_ESP32/double_tap.c

raw
1 /*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2023 Arduino SA
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27 #include <string.h>
28 #include "py/mphal.h"
29
30 #include <esp32s3/rom/cache.h>
31
32 // for get_extram_data_high()
33 #include <esp_psram.h>
34 #include <esp_private/esp_psram_extram.h>
35
36 #include "double_tap.h"
37
38 #define NUM_TOKENS 3
39 static const uint32_t MAGIC_TOKENS[NUM_TOKENS] = {
40 0xf01681de, 0xbd729b29, 0xd359be7a,
41 };
42
43 static void *magic_area;
44 static uint32_t backup_area[NUM_TOKENS];
45
46 // Current IDF does not map external RAM to a fixed address.
47 // The actual VMA depends on other enabled devices, so the precise
48 // location must be discovered.
49 static uintptr_t get_extram_data_high(void) {
50 // get a pointer into SRAM area (only the address is useful)
51 void *psram_ptr = heap_caps_malloc(16, MALLOC_CAP_SPIRAM);
52 heap_caps_free(psram_ptr);
53
54 // keep moving backwards until leaving PSRAM area
55 uintptr_t psram_base_addr = (uintptr_t)psram_ptr;
56 psram_base_addr &= ~(CONFIG_MMU_PAGE_SIZE - 1); // align to start of page
57 while (esp_psram_check_ptr_addr((void *)psram_base_addr)) {
58 psram_base_addr -= CONFIG_MMU_PAGE_SIZE;
59 }
60
61 // offset is one page from start of PSRAM
62 return psram_base_addr + CONFIG_MMU_PAGE_SIZE + esp_psram_get_size();
63 }
64
65 void double_tap_init(void) {
66 // magic location block ends 0x20 bytes from end of PSRAM
67 magic_area = (void *)(get_extram_data_high() - 0x20 - sizeof(MAGIC_TOKENS));
68 }
69
70 void double_tap_mark() {
71 memcpy(backup_area, magic_area, sizeof(MAGIC_TOKENS));
72 memcpy(magic_area, MAGIC_TOKENS, sizeof(MAGIC_TOKENS));
73 Cache_WriteBack_Addr((uintptr_t)magic_area, sizeof(MAGIC_TOKENS));
74 }
75
76 void double_tap_invalidate() {
77 if (memcmp(backup_area, MAGIC_TOKENS, sizeof(MAGIC_TOKENS))) {
78 // different contents: restore backup
79 memcpy(magic_area, backup_area, sizeof(MAGIC_TOKENS));
80 } else {
81 // clear memory
82 memset(magic_area, 0, sizeof(MAGIC_TOKENS));
83 }
84 Cache_WriteBack_Addr((uintptr_t)magic_area, sizeof(MAGIC_TOKENS));
85 }
86
87 bool double_tap_check_match() {
88 return memcmp(magic_area, MAGIC_TOKENS, sizeof(MAGIC_TOKENS)) == 0;
89 }