def init_cans(): can1 = CAN(1, CAN.NORMAL) can2 = CAN(2, CAN.NORMAL) can1.setfilter(0, CAN.LIST16, 0, (23, 24, 25, 26)) can1.setfilter(1, CAN.LIST16, 1, (33, 34, 35, 36)) return can1, can2
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 init_cans(): can1 = CAN(1, CAN.NORMAL, prescaler=2, sjw=1, bs1=14, bs2=6) can2 = CAN(2, CAN.NORMAL, prescaler=2, sjw=1, bs1=14, bs2=6) can1.setfilter(0, CAN.LIST16, 0, (23, 24, 25, 26)) can1.setfilter(1, CAN.LIST16, 1, (33, 34, 35, 36)) # can1.rxcallback(0, cb10) # can1.rxcallback(1, cb11) # can2.rxcallback(0, cb2) # can2.rxcallback(1, cb2) return can1, can2
def _init_cans(): can1 = CAN(1, CAN.NORMAL, prescaler=2, sjw=1, bs1=14, bs2=6) can2 = CAN(2, CAN.NORMAL, prescaler=2, sjw=1, bs1=14, bs2=6) can1.setfilter(0, CAN.LIST16, 0, (11, 12, 13, 14)) can1.setfilter(1, CAN.LIST16, 1, (21, 22, 23, 24)) can1.rxcallback(0, cb10) # can1.rxcallback(1, cb11) # can2.rxcallback(0, cb2) # can2.rxcallback(1, cb2) return can1, can2
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)
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)
def __init__(self, devid): self.can = CAN(2, CAN.NORMAL) # Device id [0-63] self.devid = devid # Mode of the device self.mode = 0 # Configuration data self.config_simple_targets = 0 self.config_line_segments = 0 self.config_color_detect = 0 self.config_advanced_targets = 0 self.frame_counter = 0 # Buffer for receiving data in our callback. self.read_buffer = bytearray(8) self.read_data = [0, 0, 0, memoryview(self.read_buffer)] # Initialize CAN based on which type of board we're on if omv.board_type() == "H7": print("H7 CAN Interface") self.can.init(CAN.NORMAL, extframe=True, baudrate=1000000, sampling_point=75) # 1000Kbps H7 #self.can.init(CAN.NORMAL, extframe=True, prescaler=4, sjw=1, bs1=8, bs2=3) elif omv.board_type() == "M7": self.can.init(CAN.NORMAL, extframe=True, prescaler=3, sjw=1, bs1=10, bs2=7) # 1000Kbps on M7 self.can.setfilter(0, CAN.LIST32, 0, [ self.my_arb_id(self.api_id(1, 3)), self.my_arb_id(self.api_id(1, 4)) ]) print("M7 CAN Interface") else: print("CAN INTERFACE NOT INITIALIZED!") self.can.restart()
try: from pyb import CAN except ImportError: print("SKIP") raise SystemExit from array 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 = CAN(1) print(can) # Test state when de-init'd print(can.state() == can.STOPPED) can.init(CAN.LOOPBACK, num_filter_banks=14) print(can) print(can.any(0)) # Test state when freshly created print(can.state() == can.ERROR_ACTIVE)
def write_command(cmd): """ write single command byte to ssd1306 """ dc.low() spi.send(cmd) filterMsgIDs = (123, 124, 125, 126) #for i in range(0, 100, 1): # filterMsgIDs[i] = i+100 displayText = strtobit accel = pyb.Accel() #can1 = CAN(1, CAN.NORMAL, extframe=False, prescaler=16, sjw=1, bs1=14, bs2=6) #125kbps can1 = CAN(1, CAN.NORMAL, extframe=False, prescaler=8, sjw=1, bs1=14, bs2=6) #250kbps #can1 = CAN(1, CAN.NORMAL, extframe=False, prescaler=4, sjw=1, bs1=14, bs2=6) #500kbps #can1 = CAN(1, CAN.NORMAL, extframe=False, prescaler=2, sjw=1, bs1=14, bs2=6) #1000kbps <- not wornikg #can1 = CAN(1, CAN.LOOPBACK, extframe=False, prescaler=16, sjw=1, bs1=14, bs2=6) can1.setfilter(0, CAN.LIST16, 0, filterMsgIDs) recData_au8 = bytearray(8) uart1 = UART(2, 115200) uart1.init(115200, bits=8, parity=None, stop=1, timeout=20, flow=0, timeout_char=20, read_buf_len=8)
from pyb import CAN import pyb 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)) # Test too long message try: can.send('abcdefghi', 0x7FF, timeout=5000) except ValueError: print('passed') else: print('failed')
from pyb import CAN buf = bytearray(8) data_lst = [0, 0, 0, memoryview(buf)] def can_cb1(bus, reason, fifo_num): if reason == CAN.CB_REASON_RX: bus.recv(fifo=fifo_num, list=data_lst) print(data_lst) if reason == CAN.CB_REASON_ERROR_WARNING: print('Error Warning') if reason == CAN.CB_REASON_ERROR_PASSIVE: print('Error Passive') if reason == CB_REASON_ERROR_BUS_OFF: print('Bus off') can0 = CAN(0, mode=CAN.NORMAL, extframe=True, baudrate=500000) can1 = CAN(1, mode=CAN.NORMAL, extframe=True, baudrate=500000) can1.setfilter(id=0x55, fifo=10, mask=0xf0) can0.send(data='mess 1!', id=0x50) can1.recv(fifo=10) can1.rxcallback(can_cb1) can0.send(data='mess 2!', id=0x50)
# CAN Shield Example # # This example demonstrates CAN communications between two cameras. # NOTE: you need two CAN transceiver shields and DB9 cable to run this example. import time, omv from pyb import CAN # NOTE: Set to False on receiving node. TRANSMITTER = True can = CAN(2, CAN.NORMAL, baudrate=125_000, sample_point=75) # NOTE: uncomment to set bit timing manually, for example: #can.init(CAN.NORMAL, prescaler=32, sjw=1, bs1=8, bs2=3) can.restart() if (TRANSMITTER): while (True): # Send message with id 1 can.send('Hello', 1) time.sleep_ms(1000) else: # Runs on the receiving node. if (omv.board_type() == 'H7'): # FDCAN # Set a filter to receive messages with id=1 -> 4 # Filter index, mode (RANGE, DUAL or MASK), FIFO (0 or 1), params can.setfilter(0, CAN.RANGE, 0, (1, 4)) else: # Set a filter to receive messages with id=1, 2, 3 and 4 # Filter index, mode (LIST16, etc..), FIFO (0 or 1), params
def __init__(self): # Constants and variables # # UART cmd to en-/disable the GPS self.GPS_OFF = (0xB5, 0x62, 0x06, 0x04, 0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x16, 0x74) self.GPS_ON = (0xB5, 0x62, 0x06, 0x04, 0x04, 0x00, 0x00, 0x00, 0x09, 0x00, 0x17, 0x76) self.SIM_DISABLED = False self.GPS_LOG_TIME = 5000 # 5s self.SHUTOFF_TIME = 30000 # 30s of no CAN activity self.TOKEN = "REDACTED" self.VERSION = 1.0 if 'sd' in os.listdir('/'): self.PATH = '/sd/' else: self.PATH = '' self.CAN_FILE = open(self.PATH + 'can.log', 'a+') # This will hold CAN IDs to be filtered for in the can log self.can_filter = [] self.allowed_users = ["610574975"] self.interrupt = False self.shutdown = False # Init modules # # GPS init self.gps_uart = UART(1, 9600) # init with given baudrate self.gps_uart.init(9600, bits=8, parity=None, stop=1, read_buf_len=512 // 2) # init with given parameters self.gps = MicropyGPS() # CAN init (500 MHz) self.can = CAN(1, CAN.NORMAL) # recv self.can2 = CAN(2, CAN.NORMAL) # send self.can.init(CAN.NORMAL, prescaler=4, sjw=1, bs1=14, bs2=6, auto_restart=True) self.can2.init(CAN.NORMAL, prescaler=4, sjw=1, bs1=14, bs2=6, auto_restart=True) self.can.setfilter(0, CAN.MASK16, 0, (0, 0, 0, 0)) # SIM800L init sim_uart = UART(4, 9600, timeout=1000, read_buf_len=2048 // 4) self.modem = Modem(sim_uart) self.modem.initialize() try: self.modem.connect('internet.eplus.de') except: self.SIM_DISABLED = True print("LOG ONLY MODE (NO GSM)") # Clock init self.rtc = RTC() self.rtc.wakeup(5000) # wakeup call every 5s # Interrupt Flag init self.interrupt = False pyb.ExtInt('X5', pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_UP, self.incoming_call) # Sleep pins for GSM self.gsm_sleep = pyb.Pin('X6', pyb.Pin.OUT_PP) self.gsm_sleep.value(0) if not self.SIM_DISABLED: # Software Update self.ota() # Telegram Bot self.telegram = TelegramBot(token=self.TOKEN, modem=self.modem)
from pyb import CAN from pyb import LED red_led = LED(1) can = CAN(2, CAN.LOOPBACK) #can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126)) # set a filter to receive messages with id=123, 124, 125 and 126 can.send('message!', 123) # send a message with id 123 value = can.recv(0) # receive message on FIFO 0 if value[0] == 123: red_led.LED.on() print(value[3])
""" @author: Ivo Weihert @date: 14.04.2018 @organisation: TU Darmstadt Space Technology e.V. Test script transmitting data from the accelometer via can bus on pyboard Note: run receiver first! can class documentation: https://docs.micropython.org/en/latest/pyboard/library/pyb.CAN.html Accel class documentation: http://docs.micropython.org/en/v1.9.3/pyboard/library/pyb.Accel.html """ from pyb import CAN accel = pyb.Accel() can = CAN(1, CAN.NORMAL) #select can 1 (Y3,Y4) in normal mode can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126)) #(bank, mode, fifo, params, *, rtr) - important: fifo & ID's while (True): Data_X=str(accel.x()) #get the x value from the accelerometer can.send(Data_X,123) #send x-value via can bus, Axis Information is put in can ID 123=X, 124=Y, 125=Z pyb.delay(500) #delay for better visualization Data_Y=str(accel.y()) can.send(Data_Y,124) pyb.delay(500) Data_Z=str(accel.z()) can.send(Data_Z,125) pyb.delay(500)
def send(): global send_flag #消除抖动,sw按下返回1,松开返回0。 if sw.value() == 1: delay(10) if sw.value() == 1: send_flag = 1 sw = Switch() #定义按键对象名字为sw sw.callback(send) #当按键被按下时,执行函数send() can = CAN(1, CAN.NORMAL) #设置CAN1为普通模式(RX-->PB8,TX-->PB9) #设置接收相关配置 id=123, 124, 125 和 126 can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126)) can.send('message!', 123) #发送id=123的信息 num = 0 while True: #判断有无收到信息 if can.any(0): text = can.recv(0) #读取数据 print(text) #通过REPL打印串口3接收的数据 if send_flag == 1: can.send(str(num), 123) #发送id=123的信息
# CAN Shield Example # # This example demonstrates CAN communications between two cameras. # NOTE: you need two CAN transceiver shields and DB9 cable to run this example. import time, omv from pyb import CAN # NOTE: Set to False on receiving node. TRANSMITTER = True can = CAN(2, CAN.NORMAL) # Set a different baudrate (default is 250Kbps) # NOTE: The following parameters are for the H7 only. # # can.init(CAN.NORMAL, prescaler=32, sjw=1, bs1=8, bs2=3) # 125Kbps # can.init(CAN.NORMAL, prescaler=16, sjw=1, bs1=8, bs2=3) # 250Kbps # can.init(CAN.NORMAL, prescaler=8, sjw=1, bs1=8, bs2=3) # 500Kbps # can.init(CAN.NORMAL, prescaler=4, sjw=1, bs1=8, bs2=3) # 1000Kbps can.restart() if (TRANSMITTER): while (True): # Send message with id 1 can.send('Hello', 1) time.sleep(1000) else: # Runs on the receiving node. if (omv.board_type() == 'H7'): # FDCAN
try: from pyb import CAN CAN(2) except (ImportError, ValueError): print("SKIP") raise SystemExit # Testing rtr messages bus2 = CAN(2, CAN.LOOPBACK, extframe=True) while bus2.any(0): bus2.recv(0) bus2.setfilter(0, CAN.LIST32, 0, (1, 2), rtr=(True, True)) bus2.setfilter(1, CAN.LIST32, 0, (3, 4), rtr=(True, False)) bus2.setfilter(2, CAN.MASK32, 0, (16, 16), rtr=(False, )) bus2.setfilter(2, CAN.MASK32, 0, (32, 32), rtr=(True, )) bus2.send("", 1, rtr=True) print(bus2.recv(0)) bus2.send("", 2, rtr=True) print(bus2.recv(0)) bus2.send("", 3, rtr=True) print(bus2.recv(0)) bus2.send("", 4, rtr=True) print(bus2.any(0))
#16x2 Display v1.0p test code from pyb import Pin, delay, millis, CAN from pyb_gpio_lcd import GpioLcd import utime print("16x2 Display v1.0p test code") print("v1.0") print("initializing") can = CAN(1, CAN.NORMAL, extframe=True, prescaler=12, bs1=11, bs2=2) can.setfilter(0, CAN.MASK32, 0, (0, 0)) #Setup Pins hbt_led = Pin("D13", Pin.OUT) func_butt = Pin("D5", Pin.IN, Pin.PULL_UP) can_wakeup = Pin("D6", Pin.OUT) can_wakeup.value(0) #Setup hbt timer hbt_state = 0 hbt_interval = 500 start = utime.ticks_ms() next_hbt = utime.ticks_add(start, hbt_interval) hbt_led.value(hbt_state) lcd_mess = '' print("starting") #Setup LCD lcd = GpioLcd(rs_pin=Pin.board.D1, enable_pin=Pin.board.D0,
''' description: 根据传家宝007_Elmo改写的MPY平台上的Elmo驱动 status: 未完成 issue: 报文发送失败,FIFO溢出 ''' from pyb import Pin, CAN can = CAN(1) can.init(mode=CAN.NORMAL, prescaler=3, sjw=1, bs1=9, bs2=4) # Baudrate is pyb.freq() / (sjw + bs1 + bs2) / prescaler = 1Mbps can.setfilter(bank=0, mode=CAN.MASK32, fifo=0, params=(0x0, 0x0)) COBID_NMT = 0x000 COBID_RSDO = 0x600 COBID_RPDO2 = 0x300 NMT_OPERATIONAL = 1 NMT_RESET_COMMUNICATION = 130 def NMT(elmoID, cmd): nmt = bytearray(2) nmt[0] = cmd nmt[1] = elmoID # Elmo ID can.send(nmt, id=COBID_NMT) def RSDO(elmoID, index, subIndex, data): rsdo = bytearray(8) rsdo[0] = 0x22 rsdo[1] = index & 0xFF
try: from pyb import CAN CAN(2) except (ImportError, ValueError): print("SKIP") raise SystemExit # Testing rtr messages bus2 = CAN(2, CAN.LOOPBACK) while bus2.any(0): bus2.recv(0) bus2.setfilter(0, CAN.LIST32, 0, (1, 2), rtr=(True, True), extframe=True) bus2.setfilter(1, CAN.LIST32, 0, (3, 4), rtr=(True, False), extframe=True) bus2.setfilter(2, CAN.MASK32, 0, (16, 16), rtr=(False, ), extframe=True) bus2.setfilter(2, CAN.MASK32, 0, (32, 32), rtr=(True, ), extframe=True) bus2.send("", 1, rtr=True, extframe=True) print(bus2.recv(0)) bus2.send("", 2, rtr=True, extframe=True) print(bus2.recv(0)) bus2.send("", 3, rtr=True, extframe=True) print(bus2.recv(0)) bus2.send("", 4, rtr=True, extframe=True) print(bus2.any(0))
try: from pyb import CAN except ImportError: print("SKIP") 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
# https://github.com/hmaerki/micropython/tree/master/canbus_example from pyb import CAN from pyb import LED import pyb import micropython DESTINATION_CAN_ID = 123 ledBlue = LED(1) switch = pyb.Switch() # 50kBaud can = CAN(1, CAN.NORMAL, extframe=False, prescaler=40, sjw=1, bs1=14, bs2=6) print("Press Switch to send CAN-telegrams, press <ctrl-c> to abort.") lastValue = None while True: newValue = switch.value() if lastValue != newValue: lastValue = newValue if newValue: ledBlue.on() telegram = b'on' else: ledBlue.off() telegram = b'off'
from time import sleep_ms import struct k = 0 def cb0(bus, reason): ''' print('cb0') if reason == 0: print('pending') if reason == 1: print('full') if reason == 2: print('overflow') ''' global k k = 1 CAN(1).deinit() can = CAN(1) can.init(mode=CAN.NORMAL, extframe=False, prescaler=3, sjw=1, bs1=9, bs2=4) # Baudrate is pyb.freq() / (sjw + bs1 + bs2) / prescaler = 1Mbps can.setfilter(bank=0, mode=CAN.MASK16, fifo=0, params=(0, 0, 0, 0)) can.rxcallback(0, cb0) while True: if k == 1: print(struct.unpack('<L', can.recv(0)[3])[0]) k = 0
from pyb import CAN 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) print(can.any(0)) print(can.recv(0)) can.send('abcd', -1) print(can.recv(0)) can.send('abcd', 0x7FF + 1) print(can.recv(0)) # Test too long message try: can.send('abcdefghi', 0x7FF) except ValueError: print('passed') else: print('failed')
#Stack Light v1.0p test code from machine import Pin from pyb import CAN, ADC import utime print("starting stack light board test") print("v1.0") print("initializing") can = CAN(1, CAN.NORMAL) can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126)) #Setup Pins hbt_led = Pin("D5", Pin.OUT) func_butt = Pin("E7", Pin.IN, Pin.PULL_UP) can_wakeup = Pin("D6", Pin.OUT) can_wakeup.value(0) BUZZER = Pin("D12", Pin.OUT) WHITE = Pin("D11", Pin.OUT) BLUE = Pin("E13", Pin.OUT) GREEN = Pin("E12", Pin.OUT) YELLOW = Pin("E11", Pin.OUT) RED = Pin("E10", Pin.OUT) #Setup hbt timer hbt_state = 0 hbt_interval = 500 start = utime.ticks_ms() next_hbt = utime.ticks_add(start, hbt_interval) hbt_led.value(hbt_state)
i2s.write(buf) # write buffer of audio samples to I2S device i2s = I2S(1, sck=Pin("X5"), ws=Pin("X6"), sd=Pin("Y4"), mode=I2S.RX, bits=16, format=I2S.MONO, rate=22050, ibuf=40000) # create I2S object i2s.readinto(buf) # fill buffer with audio samples from I2S device # CAN bus (controller area network)¶ # See pyb.CAN. from pyb import CAN can = CAN(1, CAN.LOOPBACK) can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126)) can.send("message!", 123) # send a message with id 123 can.recv(0) # receive message on FIFO 0 # Internal accelerometer¶ # See pyb.Accel. from pyb import Accel accel = Accel() print(accel.x(), accel.y(), accel.z(), accel.tilt())