summaryrefslogtreecommitdiff
path: root/tools/trace/tinter/thost.py
blob: bbbdf0d0cbaec7a10b3073618e68bc8ac6f2dd96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import serial
import time
from proto import *
from utils import *

FLASH_MEMORY_HIGH = 0x1fffff
FLASH_BUFFER_SIZE = 128
FLASH_CMD_INIT = 0
FLASH_CMD_READ = 1

def flash_memory_addr (val):
    return (val & FLASH_MEMORY_HIGH)

def log (x):
    print x

class THost:
    """Class to connect to the flash memory."""
    def __init__(self):
        self.__proto = Proto (serial.Serial ('/dev/ttyACM0'), time.time, 0.1)
        self.__memory = list()
        self.__status = False
        self.__addr_init = 0
        self.__addr_end = 0

    def __dump_callback (self, *memory):
        """Callback call on each data reception"""
        for i in range(len (memory)):
            self.__memory.append (memory[i])

    def __flash_status (self, status):
        """Get the flash status."""
        print "Flash activate : ", status
        self.__status = status

    def __flash_log_init (self, *addr):
        """Get the flash log start address."""
        # Don't request the code start
        addr = reverse_tupple (addr)
        self.__addr_init = get_size (addr, 3)

    def __flash_log_end (self, *addr):
        """Get the flash log end address."""
        addr = reverse_tupple (addr)
        self.__addr_end = get_size (addr, 3)

    def dump_memory(self):
        """Dump the flash memory."""
        # Initialise the flash access.
        self.__proto.register ('r', FLASH_BUFFER_SIZE * 'B', self.__dump_callback)
        self.__proto.register ('s', 'b', self.__flash_status)
        self.__proto.register ('i', 3*'B', self.__flash_log_init)
        self.__proto.register ('e', 3*'B', self.__flash_log_end)
        print "Callback registered"
        print "Initialise the Flash memory"
        self.__proto.send ('l', 'b', FLASH_CMD_INIT)
        self.__proto.wait (lambda: True)

        if self.__status == True:
            print  "Dumping from " +  str (self.__addr_init) + " to " + str (self.__addr_end)

            i = self.__addr_init
            while i != self.__addr_end:
                self.__proto.send ('l', 'bI', FLASH_CMD_READ, (i << 8) | FLASH_BUFFER_SIZE)
                i = flash_memory_addr (i + FLASH_BUFFER_SIZE)
            self.__proto.wait (lambda: True)
        else:
            self.__memory = None

    def get_trace (self):
        """Return the traces dumped from the flash memory."""
        return reverse_tupple (self.__memory[4:len (self.__memory)])