dwt_gdb.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/usr/bin/env python
  2. """
  3. This file is part of PyCortexMDebug
  4. PyCortexMDebug is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. PyCortexMDebug is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with PyCortexMDebug. If not, see <http://www.gnu.org/licenses/>.
  14. """
  15. import gdb
  16. import struct
  17. DWT_CTRL = 0xE0001000
  18. DWT_CYCCNT = 0xE0001004
  19. DWT_CPICNT = 0xE0001008
  20. DWT_EXTCNT = 0xE000100C
  21. DWT_SLEEPCNT = 0xE0001010
  22. DWT_LSUCNT = 0xE0001014
  23. DWT_FOLDCNT = 0xE0001018
  24. DWT_PCSR = 0xE000101C
  25. prefix = "dwt : "
  26. class DWT(gdb.Command):
  27. clk = None
  28. is_init = False
  29. def __init__(self):
  30. gdb.Command.__init__(self, "dwt", gdb.COMMAND_DATA)
  31. @staticmethod
  32. def read(address, bits=32):
  33. """Read from memory (using print) and return an integer"""
  34. value = gdb.selected_inferior().read_memory(address, bits / 8)
  35. return struct.unpack_from("<i", value)[0]
  36. @staticmethod
  37. def write(address, value, bits=32):
  38. """Set a value in memory"""
  39. gdb.selected_inferior().write_memory(address, bytes(value), bits / 8)
  40. def invoke(self, args, from_tty):
  41. if not self.is_init:
  42. self.write(0xE000EDFC, self.read(0xE000EDFC) | (1 << 24))
  43. self.write(DWT_CTRL, 0)
  44. self.is_init = True
  45. s = list(map(lambda x: x.lower(), str(args).split(" ")))
  46. # Check for empty command
  47. if s[0] in ["", "help"]:
  48. self.print_help()
  49. return ()
  50. if s[0] == "cyccnt":
  51. if len(s) > 1:
  52. if s[1][:2] == "en":
  53. self.cyccnt_en()
  54. elif s[1][0] == "r":
  55. self.cyccnt_reset()
  56. elif s[1][0] == "d":
  57. self.cyccnt_dis()
  58. gdb.write(
  59. prefix
  60. + "CYCCNT ({}): ".format("ON" if (self.read(DWT_CTRL) & 1) else "OFF")
  61. + self.cycles_str(self.read(DWT_CYCCNT))
  62. )
  63. elif s[0] == "reset":
  64. if len(s) > 1:
  65. if s[1] == "cyccnt":
  66. self.cyccnt_reset()
  67. gdb.write(prefix + "CYCCNT reset\n")
  68. if s[1] == "counters":
  69. self.cyccnt_reset()
  70. gdb.write(prefix + "CYCCNT reset\n")
  71. else:
  72. self.cyccnt_reset()
  73. gdb.write(prefix + "CYCCNT reset\n")
  74. else:
  75. # Reset everything
  76. self.cyccnt_reset()
  77. gdb.write(prefix + "CYCCNT reset\n")
  78. elif s[0] == "configclk":
  79. if len(s) == 2:
  80. try:
  81. self.clk = float(s[1])
  82. except:
  83. self.print_help()
  84. else:
  85. self.print_help()
  86. else:
  87. # Try to figure out what stupid went on here
  88. gdb.write(args)
  89. self.print_help()
  90. @staticmethod
  91. def complete(text, word):
  92. text = str(text).lower()
  93. s = text.split(" ")
  94. commands = ["configclk", "reset", "cyccnt"]
  95. reset_commands = ["counters", "cyccnt"]
  96. cyccnt_commands = ["enable", "reset", "disable"]
  97. if len(s) == 1:
  98. return filter(lambda x: x.startswith(s[0]), commands)
  99. if len(s) == 2:
  100. if s[0] == "reset":
  101. return filter(lambda x: x.startswith(s[1]), reset_commands)
  102. if s[0] == "cyccnt":
  103. return filter(lambda x: x.startswith(s[1]), cyccnt_commands)
  104. def cycles_str(self, cycles):
  105. if self.clk:
  106. return "%d cycles, %.3es\n" % (cycles, cycles * 1.0 / self.clk)
  107. else:
  108. return "%d cycles"
  109. def cyccnt_en(self):
  110. self.write(DWT_CTRL, self.read(DWT_CTRL) | 1)
  111. def cyccnt_dis(self):
  112. self.write(DWT_CTRL, self.read(DWT_CTRL) & 0xFFFFFFFE)
  113. def cyccnt_reset(self, value=0):
  114. self.write(DWT_CYCCNT, value)
  115. def cpicnt_reset(self, value=0):
  116. self.write(DWT_CPICNT, value & 0xFF)
  117. @staticmethod
  118. def print_help():
  119. gdb.write("Usage:\n")
  120. gdb.write("=========\n")
  121. gdb.write("dwt:\n")
  122. gdb.write("\tList available peripherals\n")
  123. gdb.write("dwt configclk [Hz]:\n")
  124. gdb.write("\tSet clock for rendering time values in seconds\n")
  125. gdb.write("dwt reset:\n")
  126. gdb.write("\tReset everything in DWT\n")
  127. gdb.write("dwt reset counters:\n")
  128. gdb.write("\tReset all DWT counters\n")
  129. gdb.write("dwt cyccnt\n")
  130. gdb.write("\tDisplay the cycle count\n")
  131. gdb.write("\td(default):decimal, x: hex, o: octal, b: binary\n")
  132. return
  133. # Registers our class to GDB when sourced:
  134. DWT()