|
| 1 | +using BSPEngine; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Globalization; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Runtime.CompilerServices; |
| 8 | +using System.Text; |
| 9 | +using System.Text.RegularExpressions; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace ESP32ToolchainUpdater |
| 13 | +{ |
| 14 | + internal class MemoryMapUpdater |
| 15 | + { |
| 16 | + struct MemoryExtents |
| 17 | + { |
| 18 | + public uint Start, Length; |
| 19 | + public uint End => Start + Length; |
| 20 | + |
| 21 | + public override string ToString() => $"{Start:x8}..{End:x8}"; |
| 22 | + } |
| 23 | + |
| 24 | + static string MapMemoryName(string nameFromBSP) => nameFromBSP switch |
| 25 | + { |
| 26 | + "DATA_FLASH" => "drom0_0_seg", |
| 27 | + "INSTR_FLASH" => "iram0_2_seg", |
| 28 | + "INSTR_RAM" => "iram0_0_seg", |
| 29 | + "DATA_RAM" => "dram0_0_seg", |
| 30 | + |
| 31 | + "FLASH" => "irom_seg", |
| 32 | + "RAM" => "iram0_0_seg", |
| 33 | + "LPRAM" => "lp_reserved_seg", |
| 34 | + }; |
| 35 | + |
| 36 | + public static void UpdateMemoryMap(string bspXML, string deviceName, string mapFile) |
| 37 | + { |
| 38 | + Dictionary<string, MemoryExtents> memories = null; |
| 39 | + var rgMem = new Regex("([id]r[ao]m[^ \t]+|lp_reserved_seg)[ \t]+0x([0-9a-f]+)[ \t]+0x([0-9a-f]+)(.*)$"); |
| 40 | + foreach(var line in File.ReadAllLines(mapFile)) |
| 41 | + { |
| 42 | + if (line == "Memory Configuration") |
| 43 | + memories = new Dictionary<string, MemoryExtents>(); |
| 44 | + else if (line == "Linker script and memory map") |
| 45 | + break; |
| 46 | + else if (memories != null) |
| 47 | + { |
| 48 | + var m = rgMem.Match(line); |
| 49 | + if (m.Success) |
| 50 | + { |
| 51 | + var extents = new MemoryExtents { Start = uint.Parse(m.Groups[2].Value, NumberStyles.HexNumber), Length = uint.Parse(m.Groups[3].Value, NumberStyles.HexNumber) }; |
| 52 | + uint padding = extents.Start & 0xFFFFU; |
| 53 | + |
| 54 | + extents.Start -= padding; |
| 55 | + extents.Length += padding; |
| 56 | + |
| 57 | + memories[m.Groups[1].Value] = extents; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + var bsp = XmlTools.LoadObject<BoardSupportPackage>(bspXML); |
| 63 | + var dev = bsp.SupportedMCUs.FirstOrDefault(m => m.ID == deviceName) ?? throw new Exception("Unknown MCU:" + deviceName); |
| 64 | + foreach(var mem in dev.MemoryMap.Memories) |
| 65 | + { |
| 66 | + var extents = memories[MapMemoryName(mem.Name)]; |
| 67 | + mem.Address = extents.Start; |
| 68 | + mem.Size = extents.Length; |
| 69 | + } |
| 70 | + |
| 71 | + XmlTools.SaveObject(bsp, bspXML); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments