connectors/LPC/LPCAppObject.py
author Edouard Tisserant
Sat, 19 May 2012 12:41:48 +0200
changeset 731 4fc681ed0c61
parent 593 726f58cf97e3
permissions -rw-r--r--
refecored library extension machanism
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
     1
#!/usr/bin/env python
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
     3
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
     4
#This file is part of Beremiz, a Integrated Development Environment for
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
     5
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. 
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
     6
#
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
     8
#
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
     9
#See COPYING file for copyrights details.
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    10
#
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    15
#
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    19
#General Public License for more details.
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    20
#
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    24
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    25
import ctypes
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    26
from LPCAppProto import *
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    27
from LPCObject import *
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents: 578
diff changeset
    28
from targets.typemapping import SameEndianessTypeTranslator as TypeTranslator
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    29
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    30
class LPCAppObject(LPCObject):
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    31
    def connect(self,comport):
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    32
        self.SerialConnection = LPCAppProto(comport,#number
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    33
                                         115200, #speed
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    34
                                         2)      #timeout
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    35
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    36
    def StartPLC(self, debug=False):
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    37
        self.HandleSerialTransaction(STARTTransaction())
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    38
            
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    39
    def StopPLC(self):
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    40
        self.HandleSerialTransaction(STOPTransaction())
558
e7ce2b3f1d5d (no commit message)
Lolitech
parents: 557
diff changeset
    41
        return True
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    42
548
1428fd553988 Some fixes in LPCApp{Obj,Proto}, to be continued.
Lolitech
parents: 545
diff changeset
    43
    def ResetPLC(self):
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    44
        self.HandleSerialTransaction(RESETTransaction())
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    45
        return self.PLCStatus
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    46
548
1428fd553988 Some fixes in LPCApp{Obj,Proto}, to be continued.
Lolitech
parents: 545
diff changeset
    47
    def GetPLCstatus(self):
575
6765adf58711 Removed status polling for LPC, as status cannot change by itself, fixed MD5 comparison in app mode
edouard
parents: 564
diff changeset
    48
        self.HandleSerialTransaction(GET_PLCIDTransaction())
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    49
        return self.PLCStatus
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    50
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    51
    def MatchMD5(self, MD5):
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    52
        data = self.HandleSerialTransaction(GET_PLCIDTransaction())
562
2c8f08f4f874 Fix MatchMD5 result if no serial connection available
laurent
parents: 558
diff changeset
    53
        if data is not None:
575
6765adf58711 Removed status polling for LPC, as status cannot change by itself, fixed MD5 comparison in app mode
edouard
parents: 564
diff changeset
    54
            return data[:32] == MD5[:32]
562
2c8f08f4f874 Fix MatchMD5 result if no serial connection available
laurent
parents: 558
diff changeset
    55
        return False
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    56
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    57
    def SetTraceVariablesList(self, idxs):
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    58
        """
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    59
        Call ctype imported function to append 
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    60
        these indexes to registred variables in PLC debugger
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    61
        """
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    62
        if idxs:
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    63
            buff = ""
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    64
            # keep a copy of requested idx
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    65
            self._Idxs = idxs[:]
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    66
            for idx,iectype,force in idxs:
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    67
                idxstr = ctypes.string_at(
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    68
                          ctypes.pointer(
576
7fcdc0d3d8d9 Some typo fixes to make debug related methods in LPCAppOject stop throwing exceptions, less agressive error message when unplugging LPC
edouard
parents: 575
diff changeset
    69
                           ctypes.c_uint32(idx)),4)
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    70
                if force !=None:
593
726f58cf97e3 fixed typo in latest debugger changes
edouard
parents: 592
diff changeset
    71
                    c_type,unpack_func, pack_func = TypeTranslator.get(iectype, (None,None,None))
576
7fcdc0d3d8d9 Some typo fixes to make debug related methods in LPCAppOject stop throwing exceptions, less agressive error message when unplugging LPC
edouard
parents: 575
diff changeset
    72
                    forced_type_size = ctypes.sizeof(c_type)
577
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 576
diff changeset
    73
                    forced_type_size_str = chr(forced_type_size)
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    74
                    forcestr = ctypes.string_at(
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    75
                                ctypes.pointer(
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    76
                                 pack_func(c_type,force)),
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    77
                                 forced_type_size)
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    78
                    buff += idxstr + forced_type_size_str + forcestr
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    79
                else:
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    80
                    buff += idxstr + chr(0)
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    81
        else:
578
6f5795bdee49 Making debugger more stable with LPC
edouard
parents: 577
diff changeset
    82
            buff = ""
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    83
            self._Idxs =  []
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    84
578
6f5795bdee49 Making debugger more stable with LPC
edouard
parents: 577
diff changeset
    85
        self.HandleSerialTransaction(SET_TRACE_VARIABLETransaction(buff))
6f5795bdee49 Making debugger more stable with LPC
edouard
parents: 577
diff changeset
    86
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    87
    def GetTraceVariables(self):
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    88
        """
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    89
        Return a list of variables, corresponding to the list of required idx
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    90
        """
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    91
        offset = 0
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    92
        strbuf = self.HandleSerialTransaction(
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    93
                                     GET_TRACE_VARIABLETransaction())
576
7fcdc0d3d8d9 Some typo fixes to make debug related methods in LPCAppOject stop throwing exceptions, less agressive error message when unplugging LPC
edouard
parents: 575
diff changeset
    94
        if strbuf is not None and len(strbuf) > 4 and self.PLCStatus == "Started":
577
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 576
diff changeset
    95
            res=[]
576
7fcdc0d3d8d9 Some typo fixes to make debug related methods in LPCAppOject stop throwing exceptions, less agressive error message when unplugging LPC
edouard
parents: 575
diff changeset
    96
            size = len(strbuf) - 4
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    97
            tick = ctypes.cast(
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    98
                    ctypes.c_char_p(strbuf[:4]),
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
    99
                    ctypes.POINTER(ctypes.c_int)).contents
577
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 576
diff changeset
   100
            buff = ctypes.cast(
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
   101
                      ctypes.c_char_p(strbuf[4:]),
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
   102
                      ctypes.c_void_p)
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
   103
            for idx, iectype, forced in self._Idxs:
577
04baf6607a44 LPC : Completed initial debug support
edouard
parents: 576
diff changeset
   104
                cursor = ctypes.c_void_p(buff.value + offset)
593
726f58cf97e3 fixed typo in latest debugger changes
edouard
parents: 592
diff changeset
   105
                c_type,unpack_func, pack_func = TypeTranslator.get(iectype, (None,None,None))
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
   106
                if c_type is not None and offset < size:
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
   107
                    res.append(unpack_func(ctypes.cast(cursor,
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
   108
                                                       ctypes.POINTER(c_type)).contents))
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
   109
                    offset += ctypes.sizeof(c_type)
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
   110
                else:
557
b155ada1c14a Removed PLCprint, not existing in LPC connector context.
Lolitech
parents: 548
diff changeset
   111
                    #if c_type is None:
b155ada1c14a Removed PLCprint, not existing in LPC connector context.
Lolitech
parents: 548
diff changeset
   112
                        #PLCprint("Debug error - " + iectype + " not supported !")
b155ada1c14a Removed PLCprint, not existing in LPC connector context.
Lolitech
parents: 548
diff changeset
   113
                    #if offset >= size:
b155ada1c14a Removed PLCprint, not existing in LPC connector context.
Lolitech
parents: 548
diff changeset
   114
                        #PLCprint("Debug error - buffer too small !")
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
   115
                    break
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
   116
            if offset and offset == size:
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
   117
                return self.PLCStatus, tick.value, res
557
b155ada1c14a Removed PLCprint, not existing in LPC connector context.
Lolitech
parents: 548
diff changeset
   118
            #PLCprint("Debug error - wrong buffer unpack !")
578
6f5795bdee49 Making debugger more stable with LPC
edouard
parents: 577
diff changeset
   119
        return self.PLCStatus, None, [] 
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
diff changeset
   120