def SetDac(val1, val2): print("Jestem AFE_SetDac()\n") #convert data val1conv = (1 - ((val1 - 50) / 5.2)) * 255 val2conv = (1 - ((val2 - 50) / 5.2)) * 255 print("dac1: ", int(val1conv), "dac2: ", int(val2conv)) can = pyb.CAN(1) can.init(pyb.CAN.NORMAL, extframe=False, prescaler=8, sjw=1, bs1=7, bs2=2, auto_restart=True) #Set filer - all responses to FIFO 0 can.setfilter(0, 0, 0, (0x00, 0x7ff)) #Send the command to AFE: buf = bytearray(4) buf[0] = 0x00 buf[1] = 0x12 buf[2] = int(val1conv) buf[3] = int(val2conv) can.send(buf, 1) time.sleep(1) buf2 = bytearray(8) lst = [0, 0, 0, memoryview(buf)] # No heap memory is allocated in the following call print(can.recv(0)) #can.recv(0, lst)
def SetDacRAW(id, val1, val2): print("Jestem AFE_SetDacRaw()\n") #convert data #val1conv = ((val1 - 60)/5.2)*255 #val2conv = ((val2 - 60)/5.2)*255 #print("dac1: ",int(val1conv),"dac2: ",int(val2conv)) can = pyb.CAN(1) can.init(pyb.CAN.NORMAL,extframe=False,prescaler=54,sjw=1,bs1=7,bs2=2,auto_restart=True) #Set filer - all responses to FIFO 0 #can.setfilter(0, can.LIST16, 0, (0, 1, 2, 4)) can.setfilter(0, can.MASK16, 0, (0, 0, 0, 0)) #Send the command to AFE: buf = bytearray(6) buf[0] = 0x00 buf[1] = 0x12 buf[2] = (val1 >> 8) & 0xFF buf[3] = val1 & 0xFF buf[4] = (val2 >> 8) & 0xFF buf[5] = val2 & 0xFF #buf[2] = int(val1conv) #buf[3] = int(val2conv) can.send(buf,id) time.sleep(1) buf2 = bytearray(8) lst = [0, 0, 0, memoryview(buf)] # No heap memory is allocated in the following call print(can.recv(0))
def __init__(self, itf): self.itf = itf self.can = pyb.CAN(itf) self.buf = FrameBuffer(64) self.send_caller = self.sendcb self.init()
def GetAdc(chn): print("Jestem AFE_GetAdc()\n") can = pyb.CAN(1) can.init(pyb.CAN.NORMAL, extframe=False, prescaler=8, sjw=1, bs1=7, bs2=2, auto_restart=True) #Set filer - all responses to FIFO 0 can.setfilter(0, 0, 0, (0x00, 0x7ff)) #Send the command to AFE: if chn >= 1 and chn <= 3: can.send("\x00\x10", 1) elif chn >= 4 and chn <= 6: can.send("\x00\x11", 1) #Wait and read response time.sleep(1) buf = bytearray(8) lst = [0, 0, 0, memoryview(buf)] # No heap memory is allocated in the following call can.recv(0, lst) print("ID: ", lst[0]) print("RTR: ", lst[1]) print("FMI: ", lst[2]) if chn == 1: AdcValue = (lst[3][2] << 8) | (lst[3][3] & 0xff) print("adc value of ch", chn, ":", AdcValue, "V") elif chn == 2: AdcValue = (lst[3][4] << 8) | (lst[3][5] & 0xff) print("adc value of ch", chn, ":", AdcValue, "V") elif chn == 3: AdcValue = (lst[3][6] << 8) | (lst[3][6] & 0xff) AdcValue = AdcValue * (70 / 4095) print("adc value of ch", chn, ":", AdcValue, "V") elif chn == 4: AdcValue = (lst[3][2] << 8) | (lst[3][3] & 0xff) AdcValue = AdcValue * (70 / 4095) print("adc value of ch", chn, ":", AdcValue, "V") elif chn == 5: AdcValue = (lst[3][4] << 8) | (lst[3][5] & 0xff) print("adc value of ch", chn, ":", AdcValue, "I") elif chn == 6: AdcValue = (lst[3][6] << 8) | (lst[3][6] & 0xff) print("adc value of ch", chn, ":", AdcValue, "I") print(hex(lst[3][0])) print(hex(lst[3][1])) print(hex(lst[3][2])) print(hex(lst[3][3])) print(hex(lst[3][4])) print(hex(lst[3][5])) print(hex(lst[3][6])) print(hex(lst[3][7]))
def __init__(self, message_id=0x7FF, bit_rate=250000, sample_point=75, can_bus=2): self.__message_id = message_id self.__can = pyb.CAN(can_bus, pyb.CAN.NORMAL, baudrate=bit_rate, sample_point=sample_point, auto_restart=True) self.__can.setfilter( 0, pyb.CAN.DUAL if omv.board_type() == "H7" else pyb.CAN.LIST32, 0, [message_id, message_id]) rpc_slave.__init__(self)
def GetCal(id, val): print("Jestem GetCal()\n") can = pyb.CAN(1) can.init(pyb.CAN.NORMAL,extframe=False,prescaler=54,sjw=1,bs1=7,bs2=2,auto_restart=True) #Set filer - all responses to FIFO 0 #can.setfilter(0, can.LIST16, 0, (0, 1, 2, 4)) can.setfilter(0, can.MASK16, 0, (0, 0, 0, 0)) can.send("\x00\x42",id) time.sleep(1) # print(can.recv(0)) buf = bytearray(6) lst = [0, 0, 0, memoryview(buf)] # No heap memory is allocated in the following call can.recv(0, lst) print("ID: ", lst[0]) print("RTR: ", lst[1]) print("FMI: ", lst[2]) HVVal = ((lst[3][5] >> (val+2)) & 0x1) print("HV val: ", HVVal)
def SetDigRes(id, ch, val): print("Jestem SetDigRes()\n") #convert data can = pyb.CAN(1) can.init(pyb.CAN.NORMAL,extframe=False,prescaler=54,sjw=1,bs1=7,bs2=2,auto_restart=True) #Set filer - all responses to FIFO 0 #can.setfilter(0, can.LIST16, 0, (0, 1, 2, 4)) can.setfilter(0, can.MASK16, 0, (0, 0, 0, 0)) #Send the command to AFE: buf = bytearray(4) buf[0] = 0x00 buf[1] = 0xA0 buf[2] = ((ch) & 0xFF) buf[3] = ((val) & 0xFF) can.send(buf,id) time.sleep(1) buf2 = bytearray(8) lst = [0, 0, 0, memoryview(buf)] # No heap memory is allocated in the following call print(can.recv(0))
def GetTemp(id): print("Jestem AFE_Temp()\n") can = pyb.CAN(1) can.init(pyb.CAN.NORMAL,extframe=False,prescaler=54,sjw=1,bs1=7,bs2=2,auto_restart=True) #Set filer - all responses to FIFO 0 #can.setfilter(0, can.LIST16, 0, (0, 1, 2, 4)) can.setfilter(0, can.MASK16, 0, (0, 0, 0, 0)) can.send("\x00\x13",id) time.sleep(1) # print(can.recv(0)) buf = bytearray(6) lst = [0, 0, 0, memoryview(buf)] # No heap memory is allocated in the following call can.recv(0, lst) print("ID: ", lst[0]) print("RTR: ", lst[1]) print("FMI: ", lst[2]) TempVal1 = (lst[3][2] << 8) | (lst[3][3] & 0xff) print("temp value 1: ", TempVal1, "bits") TempVal2 = (lst[3][4] << 8) | (lst[3][5] & 0xff) print("temp value 2: ", TempVal2, "bits")
def ClrAllCal(id): print("Jestem ClrCal()\n") #convert data can = pyb.CAN(1) can.init(pyb.CAN.NORMAL,extframe=False,prescaler=54,sjw=1,bs1=7,bs2=2,auto_restart=True) #Set filer - all responses to FIFO 0 #can.setfilter(0, can.LIST16, 0, (0, 1, 2, 4)) can.setfilter(0, can.MASK16, 0, (0, 0, 0, 0)) #Send the command to AFE: buf = bytearray(6) buf[0] = 0x00 buf[1] = 0x41 buf[2] = 0 buf[3] = 0 buf[4] = 0 buf[5] = 0xC can.send(buf,id) time.sleep(1) # buf2 = bytearray(8) # lst = [0, 0, 0, memoryview(buf)] # No heap memory is allocated in the following call print(can.recv(0))
def config_can(send, bus, mode='normal', extframe=False, prescaler=100, sjw=1, bs1=6, bs2=8, auto_restart=False): """ Configure CAN bus :param bus: 1 or 2 (for each CAN controller on the pyboard) :type bus: :class:`int` """ _mode = { 'normal': pyb.CAN.NORMAL, 'loopback': pyb.CAN.LOOPBACK, 'silent': pyb.CAN.SILENT, 'silent_loopback': pyb.CAN.SILENT_LOOPBACK, }[mode] can = pyb.CAN( bus, mode=_mode, extframe=extframe, prescaler=prescaler, sjw=sjw, bs1=bs1, bs2=bs2, auto_restart=auto_restart ) can_bus_map[bus] = can
def GetVer(id): print("Jestem GetVer()\n") can = pyb.CAN(1) can.init(pyb.CAN.NORMAL,extframe=False,prescaler=54,sjw=1,bs1=7,bs2=2,auto_restart=True) #Set filer - all responses to FIFO 0 #can.setfilter(0, can.LIST16, 0, (0, 1, 2, 4)) can.setfilter(0, can.MASK16, 0, (0, 0, 0, 0)) #can.clearfilter(0) can.send("\x00\x01",id) #time.sleep(1) #print(can.recv(0)) buf = bytearray(8) lst = [0, 0, 0, memoryview(buf)] # No heap memory is allocated in the following call can.recv(0, lst) print("ID: ", lst[0]) print("RTR: ", lst[1]) print("FMI: ", lst[2]) VerH = (lst[3][2] << 8) | (lst[3][3] & 0xff) print("VerH: ", VerH) VerL = (lst[3][4] << 8) | (lst[3][5] & 0xff) print("VerL: ", VerL) VerD = (lst[3][6] << 8) | (lst[3][7] & 0xff) print("VerD: ", VerD)
for bus in (1, 2, 3, 4, 5, 6, 7, "XA", "XB", "YA", "YB", "Z"): try: pyb.UART(bus, 9600) print("UART", bus) except ValueError: print("ValueError", bus) # test creating SPI by id/name for bus in (1, 2, 3, "X", "Y", "Z"): try: pyb.SPI(bus) print("SPI", bus) except ValueError: print("ValueError", bus) # test creating I2C by id/name for bus in (2, 3, "X", "Y", "Z"): try: pyb.I2C(bus) print("I2C", bus) except ValueError: print("ValueError", bus) # test creating CAN by id/name for bus in (1, 2, 3, "YA", "YB", "YC"): try: pyb.CAN(bus, pyb.CAN.LOOPBACK) print("CAN", bus) except ValueError: print("ValueError", bus)
while can.any(0): print(can.recv(0)) # Testing rtr messages bus1 = CAN(1, CAN.LOOPBACK) while bus1.any(0): bus1.recv(0) bus1.setfilter(0, CAN.LIST16, 0, (1, 2, 3, 4)) bus1.setfilter(1, CAN.LIST16, 0, (5, 6, 7, 8), rtr=(True, True, True, True)) bus1.setfilter(2, CAN.MASK16, 0, (64, 64, 32, 32), rtr=(False, True)) bus1.send("", 1, rtr=True) print(bus1.any(0)) bus1.send("", 5, rtr=True) print(bus1.recv(0)) bus1.send("", 6, rtr=True) print(bus1.recv(0)) bus1.send("", 7, rtr=True) print(bus1.recv(0)) bus1.send("", 16, rtr=True) print(bus1.any(0)) bus1.send("", 32, rtr=True) print(bus1.recv(0)) # test HAL error, timeout can = pyb.CAN(1, pyb.CAN.NORMAL) try: can.send("1", 1, timeout=50) except OSError as e: print(repr(e))
import time import pyb import afedrv print("run HV regulator\n") pyb.Pin.cpu.E12.init(pyb.Pin.OUT_PP, pyb.Pin.PULL_NONE) pyb.Pin.cpu.E12.value(1) print("switch CAL mux to external input\n") pyb.Pin.cpu.E8.init(pyb.Pin.OUT_PP, pyb.Pin.PULL_NONE) pyb.Pin.cpu.E8.value(0) print("set default voltages\n") afedrv.SetDac(52, 51.9) can = pyb.CAN(1) can.init(pyb.CAN.NORMAL, extframe=False, prescaler=8, sjw=1, bs1=7, bs2=2, auto_restart=True) #Set filer - all responses to FIFO 0 can.setfilter(0, 0, 0, (0x00, 0x7ff)) while False: # Change False to True to send the messages in a loop can.send("1234", 0x30) time.sleep(0.1) print("tick\n") #Send the command to AFE:
def __init__(self, message_id=0x7FF, bit_rate=250000, sampling_point=75): self.__message_id = message_id can_prescaler, can_bs1, can_bs2 = __get_can_settings(bit_rate, sampling_point) self.__can = pyb.CAN(2, pyb.CAN.NORMAL, prescaler=can_prescaler, bs1=can_bs1, bs2=can_bs2, auto_restart=True) self.__can.setfilter(0, pyb.CAN.DUAL if omv.board_type() == "H7" else pyb.CAN.LIST32, 0, [message_id, message_id]) rpc_slave.__init__(self)
import pyb import time # Setup timing to achieve target baud-rate baud = 250000 # 25k pclk1 = pyb.freq()[2] (bs1, bs2) = (5, 8) prescaler = int(pclk1 / (baud * (1 + bs1 + bs2))) # Initialize CAN controller extframe = False can1 = pyb.CAN(1, pyb.CAN.NORMAL, extframe=extframe, prescaler=prescaler, bs1=bs1, bs2=bs2) #can2 = pyb.CAN(2, pyb.CAN.NORMAL, extframe=extframe, prescaler=prescaler, bs1=bs1, bs2=bs2) def can2rx_cb(bus, reason): print('cb2', bus, reason) #can1.setfilter(0, pyb.CAN.LIST32, 0, (0x211, 0x212)) can1.setfilter(0, pyb.CAN.LIST16, 0, (0x211, 0x212, 0x213, 0x214)) #while not can1.any(0): # time.sleep(0.01) # #print(can1.recv(0))
# test of pyb stub file import pyb x = pyb.ADC pyb.stop() pyb.hard_reset() i2c = pyb.I2C() i2c.init('x') c = pyb.CAN() c.init() import sys sys.path.insert(1, "C:\\develop\\MyPython\\Stubber\\stubs\\esp32_LoBo_3_2_24") sys.path
# bs1, bs2 should total 10-1 TqCt = 10 BS1 = int(0.875*TqCt) BS2 = TqCt - 1 - BS1 # bittime = (1+ bs1 + bs2) * tq tq = bittime / (1+BS1+BS2) PCLK1 = pyb.freq()[2] # 30,000,000 # tq = prescaler/PCLK1 (it seems that only integer prescaler values work) prescaler = PCLK1 * tq print('tq {} BS1 {} BS2 {} prescaler {}'.format(tq, BS1, BS2, prescaler)) can = pyb.CAN(1, pyb.CAN.NORMAL, auto_restart=True, extframe=False, prescaler=int(prescaler), bs1=BS1, bs2=BS2) can.setfilter(0, pyb.CAN.LIST16, 0, (123, 124, 125, 126)) message = b'message' print('sending ', message) can.send(message, 123) buf = '' ary = [-1, -1, -1, memoryview(buf), buf] def cb(bus, reason): global ary try: bus.recv(0, ary)