Continue work on Device config
This commit is contained in:
parent
4851bfc50a
commit
11deae5454
2 changed files with 273 additions and 222 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -6,3 +6,4 @@ dist
|
|||
__pycache__
|
||||
*.egg-info
|
||||
_build
|
||||
.idea
|
||||
|
|
@ -1,14 +1,15 @@
|
|||
import ctypes
|
||||
from enum import Enum
|
||||
|
||||
uint8_t = ctypes.c_uint8
|
||||
uint16_t = ctypes.c_uint16
|
||||
uint32_t = ctypes.c_uint32
|
||||
import spidev
|
||||
|
||||
|
||||
class TCAN4550:
|
||||
|
||||
def __init__(self, bus:int, device:int) -> None:
|
||||
|
||||
def __init__(self, bus: int, device: int) -> None:
|
||||
# SPI initialization
|
||||
self.spi = spidev.SpiDev(bus, device)
|
||||
self.spi.max_speed_hz = 2000000
|
||||
|
|
@ -16,36 +17,75 @@ class TCAN4550:
|
|||
self.spi.mode = 0b00
|
||||
self.spi.cshigh = True
|
||||
|
||||
|
||||
# TCAN device initialization
|
||||
|
||||
# Begin by clearing any potential SPI errors
|
||||
self.TCAN_clearSPIerr()
|
||||
|
||||
self.dev_ie = TCAN4550_device_interrupt_enable()
|
||||
self.dev_ie = TCAN4x5x_device_interrupt_enable()
|
||||
self.dev_ie = 0x0 # Initialize to 0 to all bits are set to 0.
|
||||
self.TCAN_configure_interrupt_enable(self.dev_ie) # Disable all non-MCAN related interrupts for simplicity
|
||||
|
||||
self.dev_ir = TCAN4550_device_interrupt()
|
||||
self.dev.ir = 0x0 # Setup a new MCAN IR object for easy interrupt checking
|
||||
self.TCAN_read_interrupts(self.dev_ir) # Request that the struct be updated with current DEVICE (not MCAN) interrupt values
|
||||
self.dev_ir = TCAN4x5x_Device_Interrupts()
|
||||
self.dev_ir = 0x0 # Setup a new MCAN IR object for easy interrupt checking
|
||||
self.TCAN_read_interrupts(
|
||||
self.dev_ir) # Request that the struct be updated with current DEVICE (not MCAN) interrupt values
|
||||
|
||||
if self.dev_ir.b.PWRON:
|
||||
self.TCAN4x5x_Device_ClearInterrupts(self.dev_ir)
|
||||
|
||||
# Configure the CAN bus speeds
|
||||
self.TCANNomTiming = TCAN4550_MCAN_nominal_timing_simple() # 500k arbitration with a 40 MHz crystal ((40E6 / 2) / (32 + 8) = 500E3)
|
||||
self.TCANNomTiming = TCAN4x5x_MCAN_Nominal_Timing_Simple() # 500k arbitration with a 40 MHz crystal ((40E6 / 2) / (32 + 8) = 500E3)
|
||||
self.TCANNomTiming = 0x0
|
||||
self.TCANNomTiming.NominalBitRatePrescaler = 2
|
||||
self.TCANNomTiming.NominalTqBeforeSamplePoint = 32
|
||||
self.TCANNomTiming.NominalTqAfterSamplePoint = 8
|
||||
|
||||
self.TCANDataTiming = TCAN4550_MCAN_nominal_timing_simple() # 2 Mbps CAN FD with a 40 MHz crystal (40E6 / (15 + 5) = 2E6)
|
||||
self.TCANDataTiming = TCAN4x5x_MCAN_Nominal_Timing_Simple() # 2 Mbps CAN FD with a 40 MHz crystal (40E6 / (15 + 5) = 2E6)
|
||||
self.TCANDataTiming = 0x0
|
||||
self.TCANDataTiming.DataBitRatePrescaler = 1;
|
||||
self.TCANDataTiming.DataTqBeforeSamplePoint = 15;
|
||||
self.TCANDataTiming.DataTqAfterSamplePoint = 5;
|
||||
self.TCANDataTiming.DataBitRatePrescaler = 1
|
||||
self.TCANDataTiming.DataTqBeforeSamplePoint = 15
|
||||
self.TCANDataTiming.DataTqAfterSamplePoint = 5
|
||||
|
||||
# Configure the MCAN core settings
|
||||
self.cccrConfig = TCAN4x5x_MCAN_CCCR_Config()
|
||||
self.cccrConfig = 0x0 # Remember to initialize to 0, or you'll get random garbage!
|
||||
self.cccrConfig.b.FDOE = 1 # CAN FD mode enable
|
||||
self.cccrConfig.b.BRSE = 1 # CAN FD Bit rate switch enable
|
||||
|
||||
# Configure the default CAN packet filtering settings
|
||||
self.gfc = TCAN4x5x_MCAN_Global_Filter_Configuration()
|
||||
self.gfc.b.RRFE = 1 # Reject remote frames (TCAN4x5x doesn't support this)
|
||||
self.gfc.b.RRFS = 1 # Reject remote frames (TCAN4x5x doesn't support this)
|
||||
self.gfc.b.ANFE = TCAN4x5x_GFC_NO_MATCH_BEHAVIOR.TCAN4x5x_GFC_ACCEPT_INTO_RXFIFO0.value # Default behavior if incoming message doesn't match a filter is to accept into RXFIO0 for extended ID messages (29 bit IDs)
|
||||
self.gfc.b.ANFS = TCAN4x5x_GFC_NO_MATCH_BEHAVIOR.TCAN4x5x_GFC_ACCEPT_INTO_RXFIFO0.value # Default behavior if incoming message doesn't match a filter is to accept into RXFIO0 for standard ID messages (11 bit IDs)
|
||||
|
||||
'''
|
||||
In the next configuration block, we will set the MCAN core up to have:
|
||||
- 1 SID filter element
|
||||
- 1 XID Filter element
|
||||
- 5 RX FIFO 0 elements
|
||||
- RX FIFO 0 supports data payloads up to 64 bytes
|
||||
- RX FIFO 1 and RX Buffer will not have any elements, but we still set their data payload sizes, even though it's not required
|
||||
- No TX Event FIFOs
|
||||
- 2 Transmit buffers supporting up to 64 bytes of data payload
|
||||
'''
|
||||
self.MRAMConfiguration = TCAN4x5x_MRAM_Config()
|
||||
self.MRAMConfiguration = 0x0
|
||||
self.MRAMConfiguration.XIDNumElements = 1 # Extended ID number of elements, you MUST have a filter written to MRAM for each element defined
|
||||
self.MRAMConfiguration.SIDNumElements = 1 # Standard ID number of elements, you MUST have a filter written to MRAM for each element defined
|
||||
self.MRAMConfiguration.Rx0NumElements = 5 # RX0 Number of elements
|
||||
self.MRAMConfiguration.Rx0ElementSize = TCAN4x5x_MRAM_Element_Data_Size.MRAM_64_Byte_Data # RX0 data payload size
|
||||
self.MRAMConfiguration.Rx1NumElements = 0 # RX1 number of elements
|
||||
self.MRAMConfiguration.Rx1ElementSize = TCAN4x5x_MRAM_Element_Data_Size.MRAM_64_Byte_Data # RX1 data payload size
|
||||
self.MRAMConfiguration.RxBufNumElements = 0 # RX buffer number of elements
|
||||
self.MRAMConfiguration.RxBufElementSize = TCAN4x5x_MRAM_Element_Data_Size.MRAM_64_Byte_Data # RX buffer data payload size
|
||||
self.MRAMConfiguration.TxEventFIFONumElements = 0 # TX Event FIFO number of elements
|
||||
self.MRAMConfiguration.TxBufferNumElements = 2 # TX buffer number of elements
|
||||
self.MRAMConfiguration.TxBufferElementSize = TCAN4x5x_MRAM_Element_Data_Size.MRAM_64_Byte_Data # TX buffer data payload size
|
||||
|
||||
def TCAN4x5x_Device_ClearInterrupts(self, ir):
|
||||
pass
|
||||
|
||||
def TCAN_clearSPIerr(self) -> None:
|
||||
pass
|
||||
|
|
@ -54,7 +94,6 @@ class TCAN4550:
|
|||
return True
|
||||
|
||||
def TCAN_read_interrupts(self, ir) -> bool:
|
||||
|
||||
return True
|
||||
|
||||
def TCAN_write_32(self, address, data) -> None:
|
||||
|
|
@ -63,21 +102,19 @@ class TCAN4550:
|
|||
def TCAN_write(self, data, address, no_words) -> None:
|
||||
pass
|
||||
|
||||
|
||||
def TCAN_read_32(self, address, data) -> uint32_t:
|
||||
return 0
|
||||
|
||||
def TCAN_read(self,address, data, no_words) -> uint32_t:
|
||||
def TCAN_read(self, address, data, no_words) -> uint32_t:
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
|
||||
class TCAN4x5x_GFC_NO_MATCH_BEHAVIOR(Enum): #No Comments provided in original file
|
||||
class TCAN4x5x_GFC_NO_MATCH_BEHAVIOR(Enum): # No Comments provided in original file
|
||||
TCAN4x5x_GFC_ACCEPT_INTO_RXFIFO0 = 0,
|
||||
TCAN4x5x_GFC_ACCEPT_INTO_RXFIFO1 = 1,
|
||||
TCAN4x5x_GFC_REJECT = 2
|
||||
|
||||
|
||||
class TCAN4x5x_MRAM_Element_Data_Size(Enum):
|
||||
# 8 bytes of data payload
|
||||
MRAM_8_Byte_Data = 0,
|
||||
|
|
@ -103,7 +140,8 @@ class TCAN4x5x_MRAM_Element_Data_Size(Enum):
|
|||
# 64 bytes of data payload
|
||||
MRAM_64_Byte_Data = 0x7
|
||||
|
||||
class TCAN4x5x_MCAN_Data_Timing_Simple(ctypes.Structure):
|
||||
|
||||
class TCAN4x5x_MCAN_Data_Timing_Simple(ctypes.Structure):
|
||||
_fields_ = [
|
||||
# Prescaler value, interpreted as 1:x
|
||||
# Valid range is: 1 to 32
|
||||
|
|
@ -118,8 +156,9 @@ class TCAN4x5x_MCAN_Data_Timing_Simple(ctypes.Structure):
|
|||
("DataTqAfterSamplePoint", uint8_t, 5)
|
||||
]
|
||||
|
||||
|
||||
class TCAN4x5x_MCAN_Data_Timing_Raw(ctypes.Structure):
|
||||
_fields_= [
|
||||
_fields_ = [
|
||||
# DBRP: The prescaler value from the MCAN system clock. Interpreted by MCAN as the value is this field + 1
|
||||
# Valid range is: 0 to 31
|
||||
("DataBitRatePrescaler", uint8_t, 5),
|
||||
|
|
@ -145,6 +184,7 @@ class TCAN4x5x_MCAN_Data_Timing_Raw(ctypes.Structure):
|
|||
("TDCFilter", uint8_t, 7)
|
||||
]
|
||||
|
||||
|
||||
class TCAN4x5x_MCAN_Nominal_Timing_Simple(ctypes.Structure):
|
||||
_fields_ = [
|
||||
# NBRP: The prescaler value from the MCAN system clock. Value interpreted as 1:x
|
||||
|
|
@ -160,6 +200,7 @@ class TCAN4x5x_MCAN_Nominal_Timing_Simple(ctypes.Structure):
|
|||
("NominalTqAfterSamplePoint", uint8_t, 8)
|
||||
]
|
||||
|
||||
|
||||
class TCAN4x5x_MCAN_Nominal_Timing_Raw(ctypes.Structure):
|
||||
_fields_ = [
|
||||
# NBRP: The prescaler value from the MCAN system clock. Interpreted by MCAN as the value is this field + 1
|
||||
|
|
@ -179,11 +220,12 @@ class TCAN4x5x_MCAN_Nominal_Timing_Raw(ctypes.Structure):
|
|||
("NominalSyncJumpWidth", uint8_t, 7)
|
||||
]
|
||||
|
||||
|
||||
class TCAN4x5x_MRAM_Config(ctypes.Structure):
|
||||
_fields_ = [
|
||||
# ************************
|
||||
# Filter Elements *
|
||||
# ************************
|
||||
# ************************
|
||||
# Filter Elements *
|
||||
# ************************
|
||||
# Standard ID Number of Filter Elements: The number of 11-bit filters the user would like
|
||||
# Valid range is: 0 to 128
|
||||
("SIDNumElements", uint8_t, 8),
|
||||
|
|
@ -192,9 +234,9 @@ class TCAN4x5x_MRAM_Config(ctypes.Structure):
|
|||
# Valid range is: 0 to 64
|
||||
("XIDNumElements", uint8_t, 7),
|
||||
|
||||
# /************************
|
||||
# RX FIFO Elements *
|
||||
# ************************/
|
||||
# /************************
|
||||
# RX FIFO Elements *
|
||||
# ************************/
|
||||
|
||||
# RX FIFO 0 number of elements: The number of elements for the RX FIFO 0
|
||||
# Valid range is: 0 to 64
|
||||
|
|
@ -218,7 +260,7 @@ class TCAN4x5x_MRAM_Config(ctypes.Structure):
|
|||
("RxBufElementSize", TCAN4x5x_MRAM_Element_Data_Size, 3),
|
||||
|
||||
# /************************
|
||||
#TX Buffer Elements *
|
||||
# TX Buffer Elements *
|
||||
# ************************/
|
||||
|
||||
# TX Event FIFO number of elements: The number of elements for the TX Event FIFO
|
||||
|
|
@ -232,10 +274,9 @@ class TCAN4x5x_MRAM_Config(ctypes.Structure):
|
|||
# TX Buffers element size: The number of bytes for the TX Buffers (data payload)
|
||||
("TxBufferElementSize", TCAN4x5x_MRAM_Element_Data_Size, 3)
|
||||
|
||||
|
||||
|
||||
]
|
||||
|
||||
|
||||
class TCAN4x5x_MCAN_Interrupt_Enable_bits(ctypes.Structure):
|
||||
_fields_ = [
|
||||
# IE[0] RF0NE: Rx FIFO 0 new message
|
||||
|
|
@ -332,9 +373,11 @@ class TCAN4x5x_MCAN_Interrupt_Enable_bits(ctypes.Structure):
|
|||
("reserved", uint8_t, 2)
|
||||
]
|
||||
|
||||
class TCAN4x5x_MCAN_Interrupt_Enable(ctypes.Union):
|
||||
_fields_= [("b", TCAN4x5x_MCAN_Interrupt_Enable_bits),
|
||||
("word", uint32_t)]
|
||||
|
||||
class TCAN4x5x_MCAN_Interrupt_Enable(ctypes.Union):
|
||||
_fields_ = [("b", TCAN4x5x_MCAN_Interrupt_Enable_bits),
|
||||
("word", uint32_t)]
|
||||
|
||||
|
||||
class TCAN4x5x_MCAN_Interrupts_bits(ctypes.Structure):
|
||||
_fields_ = [
|
||||
|
|
@ -432,10 +475,12 @@ class TCAN4x5x_MCAN_Interrupts_bits(ctypes.Structure):
|
|||
("reserved", uint8_t, 1)
|
||||
]
|
||||
|
||||
|
||||
class TCAN4x5x_MCAN_Interrupts(ctypes.Union):
|
||||
_fields_ = [("b", TCAN4x5x_MCAN_Interrupts_bits),
|
||||
("words", uint32_t)]
|
||||
|
||||
|
||||
class TCAN4x5x_MCAN_RX_Header(ctypes.Structure):
|
||||
_fields_ = [
|
||||
# CAN ID received
|
||||
|
|
@ -472,6 +517,7 @@ class TCAN4x5x_MCAN_RX_Header(ctypes.Structure):
|
|||
("ANMF", uint8_t, 1)
|
||||
]
|
||||
|
||||
|
||||
class TCAN4x5x_Device_Interrupts_bits(ctypes.Structure):
|
||||
_fields_ = {
|
||||
# DEV_IR[0] VTWD: Global Voltage, Temp, or Watchdog (if equipped) Interrupt
|
||||
|
|
@ -571,9 +617,10 @@ class TCAN4x5x_Device_Interrupts_bits(ctypes.Structure):
|
|||
("CANBUSNORM", uint8_t, 1)
|
||||
}
|
||||
|
||||
|
||||
class TCAN4x5x_Device_Interrupts(ctypes.Union):
|
||||
_fields_ = [("b", TCAN4x5x_Device_Interrupts_bits),
|
||||
("word", uint32_t)]
|
||||
("word", uint32_t)]
|
||||
|
||||
|
||||
class TCAN4x5x_MCAN_CCCR_Config_bits(ctypes.Structure):
|
||||
|
|
@ -625,10 +672,10 @@ class TCAN4x5x_MCAN_CCCR_Config_bits(ctypes.Structure):
|
|||
("NISO", uint8_t, 1)
|
||||
]
|
||||
|
||||
class TCAN4x5x_MCAN_CCCR_Config(ctypes.Union):
|
||||
_fields_ = [("b", TCAN4x5x_MCAN_CCCR_Config_bits),
|
||||
("word", uint32_t)]
|
||||
|
||||
class TCAN4x5x_MCAN_CCCR_Config(ctypes.Union):
|
||||
_fields_ = [("b", TCAN4x5x_MCAN_CCCR_Config_bits),
|
||||
("word", uint32_t)]
|
||||
|
||||
|
||||
class TCAN4x5x_MCAN_Global_Filter_Configuration_bits(ctypes.Structure):
|
||||
|
|
@ -656,6 +703,8 @@ class TCAN4x5x_MCAN_Global_Filter_Configuration_bits(ctypes.Structure):
|
|||
# Reserved
|
||||
("reserved", uint32_t, 26)
|
||||
]
|
||||
|
||||
|
||||
class TCAN4x5x_MCAN_Global_Filter_Configuration(ctypes.Union):
|
||||
_fields_ = [("b", TCAN4x5x_MCAN_Global_Filter_Configuration_bits),
|
||||
("word", uint32_t)]
|
||||
|
|
@ -664,81 +713,82 @@ class TCAN4x5x_MCAN_Global_Filter_Configuration(ctypes.Union):
|
|||
class TCAN4x5x_device_interrupt_enable_bits(ctypes.BigEndianStructure):
|
||||
_fields_ = [
|
||||
# @brief DEV_IE[0:7] : RESERVED
|
||||
("RESERVED1", uint8_t , 8),
|
||||
("RESERVED1", uint8_t, 8),
|
||||
|
||||
# @brief DEV_IE[8] : CANDOM, Can bus stuck dominant
|
||||
("CANDOMEN", uint8_t , 1),
|
||||
("CANDOMEN", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[9] : RESERVED
|
||||
("RESERVED2", uint8_t , 1),
|
||||
("RESERVED2", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[10] : CANTO, CAN Timeout
|
||||
("CANTOEN", uint8_t , 1),
|
||||
("CANTOEN", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[11] : RESERVED
|
||||
("RESERVED3", uint8_t , 1),
|
||||
("RESERVED3", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[12] : FRAME_OVF, Frame Error Overflow (If Selective Wake is equipped)
|
||||
("FRAME_OVFEN", uint8_t , 1),
|
||||
("FRAME_OVFEN", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[13] : WKERR, Wake Error
|
||||
("WKERREN", uint8_t , 1),
|
||||
("WKERREN", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[14] : LWU, Local Wake Up
|
||||
("LWUEN", uint8_t , 1),
|
||||
("LWUEN", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[15] : CANINT, CAN Bus Wake Up Interrupt
|
||||
("CANINTEN", uint8_t , 1),
|
||||
("CANINTEN", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[16] : ECCERR, MRAM ECC Error
|
||||
("ECCERREN", uint8_t , 1),
|
||||
("ECCERREN", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[17] : Reserved
|
||||
("RESERVED4", uint8_t , 1),
|
||||
("RESERVED4", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[18] : WDTO, Watchdog Time Out
|
||||
("WDTOEN", uint8_t , 1),
|
||||
("WDTOEN", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[19] : TSD, Thermal Shut Down
|
||||
("TSDEN", uint8_t , 1),
|
||||
("TSDEN", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[20] : PWRON, Power On Interrupt
|
||||
("PWRONEN", uint8_t , 1),
|
||||
("PWRONEN", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[21] : UVIO, Undervoltage on UVIO
|
||||
("UVIOEN", uint8_t , 1),
|
||||
("UVIOEN", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[22] : UVSUP, Undervoltage on VSUP and VCCOUT
|
||||
("UVSUPEN", uint8_t , 1),
|
||||
("UVSUPEN", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[23] : SMS, Sleep Mode Status Flag. Set when sleep mode is entered due to WKERR, UVIO, or TSD faults
|
||||
("SMSEN", uint8_t , 1),
|
||||
("SMSEN", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[24] : CANBUSBAT, CAN Shorted to VBAT
|
||||
("CANBUSBATEN", uint8_t , 1),
|
||||
("CANBUSBATEN", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[25] : CANBUSGND, CAN Shorted to GND
|
||||
("CANBUSGNDEN", uint8_t , 1),
|
||||
("CANBUSGNDEN", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[26] : CANBUSOPEN, CAN Open fault
|
||||
("CANBUSOPENEN", uint8_t , 1),
|
||||
("CANBUSOPENEN", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[27] : CANLGND, CANL GND
|
||||
("CANLGNDEN", uint8_t , 1),
|
||||
("CANLGNDEN", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[28] : CANHBAT, CANH to VBAT
|
||||
("CANHBATEN", uint8_t , 1),
|
||||
("CANHBATEN", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[29] : CANHCANL, CANH and CANL shorted
|
||||
("CANHCANLEN", uint8_t , 1),
|
||||
("CANHCANLEN", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[30] : CANBUSTERMOPEN, CAN Bus has termination point open
|
||||
("CANBUSTERMOPENEN", uint8_t , 1),
|
||||
("CANBUSTERMOPENEN", uint8_t, 1),
|
||||
|
||||
# @brief DEV_IE[31] : CANBUSNOM, CAN Bus is normal flag
|
||||
("CANBUSNORMEN", uint8_t , 1)
|
||||
("CANBUSNORMEN", uint8_t, 1)
|
||||
]
|
||||
|
||||
|
||||
class TCAN4x5x_device_interrupt_enable(ctypes.Union):
|
||||
_fields_ = [("b", TCAN4x5x_device_interrupt_enable_bits),
|
||||
("word", uint32_t)]
|
||||
Loading…
Add table
Add a link
Reference in a new issue