dwt_gdb.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. def read(self, address, bits=32):
  32. """Read from memory (using print) and return an integer"""
  33. value = gdb.selected_inferior().read_memory(address, bits / 8)
  34. return struct.unpack_from("<i", value)[0]
  35. def write(self, address, value, bits=32):
  36. """Set a value in memory"""
  37. gdb.selected_inferior().write_memory(address, bytes(value), bits / 8)
  38. def invoke(self, args, from_tty):
  39. if not self.is_init:
  40. self.write(0xE000EDFC, self.read(0xE000EDFC) | (1 << 24))
  41. self.write(DWT_CTRL, 0)
  42. self.is_init = True
  43. s = map(lambda x: x.lower(), str(args).split(" "))
  44. # Check for empty command
  45. if s[0] in ["", "help"]:
  46. self.print_help()
  47. return ()
  48. if s[0] == "cyccnt":
  49. if len(s) > 1:
  50. if s[1][:2] == "en":
  51. self.cyccnt_en()
  52. elif s[1][0] == "r":
  53. self.cyccnt_reset()
  54. elif s[1][0] == "d":
  55. self.cyccnt_dis()
  56. gdb.write(
  57. prefix
  58. + "CYCCNT ({}): ".format("ON" if (self.read(DWT_CTRL) & 1) else "OFF")
  59. + self.cycles_str(self.read(DWT_CYCCNT))
  60. )
  61. elif s[0] == "reset":
  62. if len(s) > 1:
  63. if s[1] == "cyccnt":
  64. self.cyccnt_reset()
  65. gdb.write(prefix + "CYCCNT reset\n")
  66. if s[1] == "counters":
  67. self.cyccnt_reset()
  68. gdb.write(prefix + "CYCCNT reset\n")
  69. else:
  70. self.cyccnt_reset()
  71. gdb.write(prefix + "CYCCNT reset\n")
  72. else:
  73. # Reset everything
  74. self.cyccnt_reset()
  75. gdb.write(prefix + "CYCCNT reset\n")
  76. elif s[0] == "configclk":
  77. if len(s) == 2:
  78. try:
  79. self.clk = float(s[1])
  80. except:
  81. self.print_help()
  82. else:
  83. self.print_help()
  84. else:
  85. # Try to figure out what stupid went on here
  86. gdb.write(args)
  87. self.print_help()
  88. def complete(self, text, word):
  89. text = str(text).lower()
  90. s = text.split(" ")
  91. commands = ["configclk", "reset", "cyccnt"]
  92. reset_commands = ["counters", "cyccnt"]
  93. cyccnt_commands = ["enable", "reset", "disable"]
  94. if len(s) == 1:
  95. return filter(lambda x: x.startswith(s[0]), commands)
  96. if len(s) == 2:
  97. if s[0] == "reset":
  98. return filter(lambda x: x.startswith(s[1]), reset_commands)
  99. if s[0] == "cyccnt":
  100. return filter(lambda x: x.startswith(s[1]), cyccnt_commands)
  101. def cycles_str(self, cycles):
  102. if self.clk:
  103. return "%d cycles, %.3es\n" % (cycles, cycles * 1.0 / self.clk)
  104. else:
  105. return "%d cycles"
  106. def cyccnt_en(self):
  107. self.write(DWT_CTRL, self.read(DWT_CTRL) | 1)
  108. def cyccnt_dis(self):
  109. self.write(DWT_CTRL, self.read(DWT_CTRL) & 0xFFFFFFFE)
  110. def cyccnt_reset(self, value=0):
  111. self.write(DWT_CYCCNT, value)
  112. def cpicnt_reset(self, value=0):
  113. self.write(DWT_CPICNT, value & 0xFF)
  114. def print_help(self):
  115. gdb.write("Usage:\n")
  116. gdb.write("=========\n")
  117. gdb.write("dwt:\n")
  118. gdb.write("\tList available peripherals\n")
  119. gdb.write("dwt configclk [Hz]:\n")
  120. gdb.write("\tSet clock for rendering time values in seconds\n")
  121. gdb.write("dwt reset:\n")
  122. gdb.write("\tReset everything in DWT\n")
  123. gdb.write("dwt reset counters:\n")
  124. gdb.write("\tReset all DWT counters\n")
  125. gdb.write("dwt cyccnt\n")
  126. gdb.write("\tDisplay the cycle count\n")
  127. gdb.write("\td(default):decimal, x: hex, o: octal, b: binary\n")
  128. return ()