Пример #1
0
    def __init__(self,
                 port=2,
                 data_rate=None,
                 host_id=2,
                 module_id=1,
                 debug=False,
                 can_mode=CanModeNormal()):
        del data_rate
        tmcl_interface.__init__(self, host_id, module_id, debug)
        tmcl_host_interface.__init__(self, host_id, module_id, debug)

        self.__silent = Pin(Pin.cpu.B14, Pin.OUT_PP)
        self.__mode = can_mode
        self.__flag_recv = False

        self.__set_mode()

        CAN.initfilterbanks(14)

        self.__can = CAN(port, CAN.NORMAL)
        # PCLK1 = 42 MHz, Module_Bitrate = 1000 kBit/s
        # With prescaler = 3, bs1 = 11, bs2 = 2
        # Sample point at 85.7 %, accuracy = 100 %
        self.__can.init(CAN.NORMAL,
                        prescaler=3,
                        bs1=11,
                        bs2=2,
                        auto_restart=True)
        self.__can.setfilter(0, CAN.LIST16, 0,
                             (host_id, host_id, host_id, host_id))
        self.__can.rxcallback(0, self.__callback_recv)
Пример #2
0
    def __init__(self,
                 port=1,
                 data_rate=None,
                 host_id=2,
                 module_id=1,
                 debug=False,
                 can_mode=CanModeNormal()):
        del data_rate
        tmcl_module_interface.__init__(self, host_id, module_id, debug)
        tmcl_host_interface.__init__(self, host_id, module_id, debug)

        self.__silent = Pin(Pin.cpu.B10, Pin.OUT_PP)
        self.__mode = can_mode
        self.__flag_recv = False
        self.__can = None

        CAN.initfilterbanks(14)

        # PCLK1 = 42 MHz, Module_Bitrate = 1000 kBit/s
        # With prescaler = 3, bs1 = 11, bs2 = 2
        # Sample point at 85.7 %, accuracy = 100 %

        if (isinstance(self.__mode, CanModeNormal)):
            self.__silent.low()
            self.__can = CAN(port, CAN.NORMAL)
            self.__can.init(CAN.NORMAL,
                            prescaler=3,
                            bs1=11,
                            bs2=2,
                            auto_restart=True)
            self.__can.setfilter(0, CAN.LIST16, 0,
                                 (host_id, host_id, module_id, module_id))
        elif (isinstance(self.__mode, CanModeSilent)):
            self.__silent.high()
            self.__can = CAN(port, CAN.SILENT)
            self.__can.init(CAN.SILENT,
                            prescaler=3,
                            bs1=11,
                            bs2=2,
                            auto_restart=True)
            self.__can.setfilter(0, CAN.LIST16, 0,
                                 (host_id, host_id, module_id, module_id))
        elif (isinstance(self.__mode, CanModeOff)):
            raise ValueError()  # Not supported by TJA1051T/3

        self.__can.rxcallback(0, self.__callback_recv)
Пример #3
0
from pyb import CAN
import pyb

# test we can correctly create by id or name
for bus in (-1, 0, 1, 2, 3, "YA", "YB", "YC"):
    try:
        CAN(bus, CAN.LOOPBACK)
        print("CAN", bus)
    except ValueError:
        print("ValueError", bus)

CAN.initfilterbanks(14)
can = CAN(1)
print(can)

can.init(CAN.LOOPBACK)
print(can)
print(can.any(0))

# Catch all filter
can.setfilter(0, CAN.MASK16, 0, (0, 0, 0, 0))

can.send('abcd', 123, timeout=5000)
print(can.any(0))
print(can.recv(0))

can.send('abcd', -1, timeout=5000)
print(can.recv(0))

can.send('abcd', 0x7FF + 1, timeout=5000)
print(can.recv(0))
Пример #4
0
    raise SystemExit

from uarray import array
import micropython
import pyb

# test we can correctly create by id (2 handled in can2.py test)
for bus in (-1, 0, 1, 3):
    try:
        CAN(bus, CAN.LOOPBACK)
        print("CAN", bus)
    except ValueError:
        print("ValueError", bus)
CAN(1).deinit()

CAN.initfilterbanks(14)
can = CAN(1)
print(can)

# Test state when de-init'd
print(can.state() == can.STOPPED)

can.init(CAN.LOOPBACK)
print(can)
print(can.any(0))

# Test state when freshly created
print(can.state() == can.ERROR_ACTIVE)

# Test that restart can be called
can.restart()
Пример #5
0
class canIf(object):
    def __init__(self,canId = 1):
   #     self._canIf = CAN(1, CAN.NORMAL, extframe=False, prescaler=16, sjw=1, bs1=14, bs2=6)
   #     self._canIf.setfilter(0, CAN.LIST16, 0, (124, 124, 124, 124))
        self._canIf = None
        self._canId = canId
        self._canAddr = 255

        self._rxTimeout = 5

      #  self._FRM_BYTE = 0x7D
       # self._ESC_BYTE = 0x7E

        self._txByte = 0
        self._rxByte = 0
        self._txFrame = 0
        self._rxFrame = 0

    def bitrate(self, bitrate = 125):
        if bitrate == 125:
            # 125kpbs, PCLK1@42000000
            self._canIf = CAN(self._canId, CAN.NORMAL, extframe=False, prescaler=16, sjw=1, bs1=14, bs2=6)
            print('set can speed', bitrate)
        elif bitrate == 250:
            ''' Init for 250 kbps <=> bit time 4 µs. If 42 MHz clk is prescaled by 21, we get 8 subparts 'tq'
            of a bit time from theese parts (# tq): sync(1) + bs1(5) + bs2(2) = 8 * tq = 4µs => 250 kbps'''
            self._canIf = CAN(self._canId, CAN.NORMAL, extframe=False, prescaler=21, sjw=1, bs1=5, bs2=2)
        else:
            print('CAN speed not found')

        self._canIf.initfilterbanks(1)
        return True

    def filter(self, address):
        canfilterList = []
        for x in range(0, 4):
            canfilterList.append(address)
            #  mylist.append(123)
        self._canAddr = address
        self._canIf.setfilter(0, CAN.LIST16, 0, canfilterList)
        print('Filter',self._canAddr)
        return True

    def any(self):
        self._canIf.any(0)

    def recv(self):
        self._canIf.recv(0)


    def send(self, buffer):
        # print('canSend',buffer)
        result = True
        try:
            self._canIf.send(buffer, self._canAddr, timeout=100)
            self._txByte += len(buffer)
            self._txFrame += 1
        except:
            result = False
            print('Failed to Send')

        return result
Пример #6
0
class canIf(object):
    def __init__(self, canId=1):
        #     self._canIf = CAN(1, CAN.NORMAL, extframe=False, prescaler=16, sjw=1, bs1=14, bs2=6)
        #     self._canIf.setfilter(0, CAN.LIST16, 0, (124, 124, 124, 124))
        self._canIf = None
        self._canId = canId
        self._canAddr = 255

        self._rxTimeout = 5

        self._FRM_BYTE = 0x7D
        self._ESC_BYTE = 0x7E

        self._txByte = 0
        self._rxByte = 0
        self._txFrame = 0
        self._rxFrame = 0

    def bitrate(self, bitrate=125):
        if bitrate == 125:
            # 125kpbs, PCLK1@42000000
            self._canIf = CAN(self._canId,
                              CAN.NORMAL,
                              extframe=False,
                              prescaler=16,
                              sjw=1,
                              bs1=14,
                              bs2=6)
            print('set can speed', bitrate)
        elif bitrate == 250:
            ''' Init for 250 kbps <=> bit time 4 µs. If 42 MHz clk is prescaled by 21, we get 8 subparts 'tq'
            of a bit time from theese parts (# tq): sync(1) + bs1(5) + bs2(2) = 8 * tq = 4µs => 250 kbps'''
            self._canIf = CAN(self._canId,
                              CAN.NORMAL,
                              extframe=False,
                              prescaler=21,
                              sjw=1,
                              bs1=5,
                              bs2=2)
        else:
            print('CAN speed not found')

        self._canIf.initfilterbanks(1)
        return True

    def filter(self, address):
        canfilterList = []
        for x in range(0, 4):
            canfilterList.append(address)
            #  mylist.append(123)
        self._canAddr = address
        self._canIf.setfilter(0, CAN.LIST16, 0, canfilterList)
        print('Filter', self._canAddr)
        return True

    def buffer(self, buffer=0):
        return self._canIf.any(buffer)

    def receive(self):
        timeout = False
        timeoutValue = utime.time() + self._rxTimeout

        state = 'INIT'
        data = []

        if self._canIf.any(0):
            print('Data')
            while not (timeout):
                if self._canIf.any(0):
                    (id, type, fmi, canframe) = self._canIf.recv(0)
                    self._rxFrame += 1
                    for item in canframe:
                        self._rxByte += 1

                        #                    ''' Start Frame '''
                        if item == self._FRM_BYTE and state in 'INIT':
                            state = 'RUN'
                            print(state, item)
                        #  data.append(item)
#                    ''' End Frame '''
                        elif item == self._FRM_BYTE and state in 'RUN':
                            state = 'COMPLET'
                            print(state, item)
                            #  data.append(item)
                            return (state, data)
#                    ''' Stuffin Byte'''
                        elif item == self._ESC_BYTE and state in 'RUN':
                            state = 'STUFFING'
                            print(state, item)
#                   ''' un-Stuffing'''
                        elif state in 'STUFFING':
                            data.append(item ^ 0x20)
                            state = 'RUN'
                            print(state, item)
                        else:
                            data.append(item)
#          ''' Timeout '''
                elif utime.time() > timeoutValue:
                    state = 'TIMEOUT'
                    timeout = True
                    return (state, data)
        else:
            state = 'NODATA'

    #     print('NoData')

        return (state, data)

    def transmit(self, data):
        size = len(data)
        #   print('Transmit',size,data)
        # self._arrayTx =[]
        state = 'INIT'
        buffer = array.array('B', [])
        buffer.append(self._FRM_BYTE)
        state = 'RUN'
        length = 0

        for item in data:
            # print('tt',size,item,len(self._arrayTx))
            size = size - 1
            #        print(len(buffer),item)

            if len(buffer) == 8:
                #        print('test', buffer)
                #self._canIf.send(buffer, self._address, timeout=500)
                self.canSend(buffer)

                buffer = array.array('B', [])
                #     print('array',len(self._arrayTx))
                #print('state', self._state,self._arrayTx)

            #if 'INIT' in state:
            #    buffer.append(self._FRM_BYTE)
            #    buffer.append(item)
            #    state = 'RUN'
            if 'RUN' in state:
                if item in (self._FRM_BYTE, self._ESC_BYTE):
                    buffer.append(self._ESC_BYTE)
                    #           print(len(buffer),buffer)
                    if len(buffer) == 8:
                        # self._canIf.send(buffer, self._address, timeout=500)
                        self.canSend(buffer)
                        buffer = array.array('B', [])

                    buffer.append(item ^ 0x20)
                    if len(buffer) == 8:
                        self.canSend(buffer)
                        buffer = array.array('B', [])

                else:
                    buffer.append(item)
                    #  elif size <= 0:
                    #     print('end')

        buffer.append(self._FRM_BYTE)
        self.canSend(buffer)
        #  self._canIf.send(buffer, self._address, timeout=500)

        return size

    def canSend(self, buffer):
        # print('canSend',buffer)
        self._canIf.send(buffer, self._canAddr, timeout=500)
        self._txByte += len(buffer)
        self._txFrame += 1
        return True

    def maintenance(self):
        maint = {}
        maint['TX-BYTE'] = self._txByte
        maint['RX-BYTE'] = self._rxByte
        maint['TX-FRAME'] = self._txFrame
        maint['RX-FRAME'] = self._rxFrame

        return maint

    def txString(self, str):
        data = []
        for item in str:
            #print(item)
            data.append(ord(item))

        return (self.transmit(data))

    def rxString(self):
        #print('StringRx')
        str = ''
        (state, data) = self.receive()

        if 'COMPLET' in state:
            str = ''.join(chr(i) for i in data)
            print('String', str)

        return (state, str)