One 、CSM300 summary

CSM300(A) Series is a product that can support SPI / UART Interface CAN modular .

1. brief introduction

CSM300(A) Series isolation SPI / UART turn CAN Modules are integrated microprocessors 、 CAN Transceiver 、 DC-DC
Isolate the power supply 、 Signal isolation in one communication module , The chip can be easily embedded into a device with SPI or UART Interface in the device , Without changing the original hardware structure, the device can get CAN communication interface , Realization SPI Equipment or UART Equipment and CAN Data communication between bus networks .

appearance

CSM300

2. Parameters

  1. Realization SPI or UART And CAN Interface of two-way data communication ;
  2. CAN The bus conforms to “ISO 11898-2” standard ;
  3. Integrate 1 road SPI Interface , Support user defined rate , Up to 1.5Mbit/s( Non custom protocol conversion ) , or 1Mbit/s( Custom protocol conversion ) ;
  4. Integrate 1 road UART Interface , Multiple rates are supported , Up to 921600bps;
  5. Integrate 1 road CAN communication interface , Supports multiple baud rates , Up to 1Mbps;
  6. Isolation and withstand voltage 2500VDC;
  7. working temperature : -40℃~+85℃;
  8. Electromagnetic radiation EME The lower ;
  9. Electromagnetic immunity EMS Higher ;

 model

As shown in the figure above CSM300 yes 5V Working voltage ,CSM300A yes 3.3V Working voltage .

If MCU、MPU The side operating voltage is not 1.8V It takes so long to add one level shift For voltage conversion .

This debugging board uses CSM300A, Use only one of them SPI Interface .

3. Pin definition and reference circuit

Use SPI turn CAN When the function , Need to put MODE The pin is connected to the high level .MCU Of SPI Interface and CSM300(A) Of SPI Interface connection , meanwhile MCU Need to provide GPIO And RST、 INT、 CTL0、 CTL1 Pin connection , Realize to CSM300(A) Effective monitoring and control of . If you need to pass MCU Yes CSM300(A) To configure , Additional GPIO And CFG Pin connection .

SPI turn CAN Reference circuit (CSM300A)
Pin description :

 Pin description
The pin function is described as follows :

  1. MODE The pin is directly connected to the high voltage ( High level corresponds to SPI Pattern , The low level corresponds to UART Pattern );
  2. 10、11、12 external CAN Bus , It is mainly used for CAN signal communication ;
  3. 3、6、7、24、19 Pin connection MCU/MPU, To configure CSM300A The mode and read-write operation of the system depend on these pins ;
  4. 18、21、22、23 this 4 Two pins need to be connected to MCU/MPU Of SPI Controller pins , Mainly CPU Side send configuration information and read and write data SPI access ;
  5. 20 yes INT Pin ,CSM300A After receiving the data , If certain conditions are met, the pin will be pulled down , Generate interrupt signal , notice CPU Reading data .

Two 、 Working mode

1. Classification of work patterns

CSM300(A) After power on , MODE、 CFG The pin level will determine whether the product is in 4 One of the different modes of work : SPI turn CAN Pattern 、 UART turn CAN Pattern 、 SPI Configuration mode 、 UART Configuration mode .

CSM300(A) Working mode As shown in the table above :

  1. If we want to configure CSM300A, It's about setting up CSM300A The model is SPI Configuration mode , So you're going to have to MODE The pin is set to 1,CFG Set as 0,RST Set as 1;
  2. If we want to pass CSM300A Read and write data , It's about setting up CSM300A The model is SPI turn CAN Pattern , So you're going to have to MODE The pin is set to 1,CFG Set as 1,RST Set as 1;
  3. The operation of reading and writing data , All belong to SPI turn CAN Pattern , No need to switch modes .
  4. If you need to switch the working mode of the product , After changing the pin level , The product must be reset , In order to make it enter the design
    Set the working mode . It should be noted that , To ensure a successful reset , The minimum holding time of reset is 100us, After reset ,
    Minimum waiting time for product initialization 3ms, After the product is initialized , For normal operation .

The following is a sequence diagram of different mode switching .
 Working mode switching sequence

2. SPI turn CAN Pattern ( Data reading and writing )

In this working mode , CSM300(A) Always as SPI Slave , SPI Limit working in mode 3(CPOL、 CPHA
Are all 1), The data length is limited to 8 position , MSB High bit first transmission . Transparent conversion 、 The highest communication under the transparent band identification conversion
Rate is 1.5Mbps, The maximum communication rate of custom protocol conversion is 1Mbps.

SPI The host can send data to CAN Bus terminal , And can receive CAN Data received at the bus end . here UART
Invalid interface , It doesn't deal with anything that appears in UART Interface data , And will not return CAN The data received by the bus terminal
to UART.

  1. SPI frame
    SPI The data between a valid and invalid selection is defined as a frame of data . Read and write buffer data between frames should be 40us Time interval of .

 Host reads data frame  Host writes data frame

3. SPI Configuration mode

In this mode , CSM300(A) Waiting for configuration , Unable to contact CAN Send or receive data at the end . In this mode, only SPI Interface .

3、 ... and 、 Host control

CSM300(A) There are two SPI Host control pin CTL0, CTL1, It is controlled by the host . The host computer controls CTL0,
CTL1 Pin , send CSM300(A) Into different functional states , Realize to CSM300(A) Different operation purposes . The control pin level of the host terminal is different, and the corresponding functions are shown in the table below :

SPI The host control function in mode
The host can get the number of bytes that can be read and written by reading the current status of the slave
Count . The host selects the function as host read state , And then through SPI read out 4 Bytes , It's the status code . Status code from
32 A bit makes up , The specific definition is shown in the table below .
 Status code
If defined status[] The array is 8 An integer , adopt SPI The data read out in turn is status[0]、 status[1]、
status[2]、 status[3], The data structure is shown in the figure below :

 Status byte data structure

Four 、 feedback mechanism ( interrupt )

CSM300(A) Only as SPI Slave , Can't actively control other SPI Bus devices , So if you receive CAN After the data frame , We have to take the initiative to return to CPU Side .

CSM300(A) On hardware INT Feedback pins , This pin is connected to the host , In the following two cases , INT
The pin will change from high level to low level , Inform the host to read data ( To avoid data loss , It is recommended that the host use low
Level trigger mode detection ):

  1. CAN buffer CAN When the number of frames reaches the set trigger point
    When the product CAN Received by bus receiving buffer CAN When the number of frames reaches the trigger point , INT The pin level is set low ,
    Until the buffer is cleared , INT The pin will return to high level . Users can get INT After the signal CSM300(A)
    The state of , Get the number of readable bytes , Then read the buffer CAN data .

  2. CAN The buffer data is less than the number of trigger frames , And when the host does not read in the set time
    CAN When the buffer has data but less than the number of trigger frames , If there is no new data on the bus for a long time , And the host is not reading
    During the operation , CAN The data in the receiving buffer may not be processed for a long time , This leads to low real-time performance of data .
    In order to solve the real-time problem of a small amount of data , CSM300(A) There's a timer inside , if CAN The number of buffers
    The data has not been read for a certain period of time , Will trigger INT Pin down , Read data to inform the host . CSM300(A) Receiving
    By the last frame of data , The timer starts , Reset the timer when the host is reading .

5、 ... and 、 Networking mode

CAN The bus generally uses linear wiring , The number of bus nodes is up to 110 individual . Shielded twisted pair is recommended for wiring , CANH、 CANL Connect with twisted pair core , CGND Connect to the shield , Finally, the shield is grounded at a single point .

Thanks to the CSM300(A) The lowest baud rate 5kbps, The longest communication distance of the bus can reach 10km.
 Recommended networking diagram

6、 ... and 、 transplant

1. Hardware connection diagram

 Hardware connection diagram

As shown in the figure above :

  1. SOC It's integrated on the Internet SPI controller , Manufacturer's sdk Already included spi Device tree and driver information of the controller ;
  2. SOC Of SPI The controller pins need to be connected first level shift Boost the pressure , The board voltage is 1.8V, and CSM300 The required voltage is 3.3V;
  3. SOC Of GPIO 76/107/113/114 adopt level shift Separate connection CSM300A Of RST/CFG/CTL1/CTL0;
  4. stay PC Up operation CAN-Test Software , Can pass USB turn CAN Equipment from CAN Read and send data on the bus .

【 notes 】USB turn CAN equipment , You can search for , Stop advertising .

2. Device tree

Here is the official device tree :

[email protected] {
pinctrl-names = "default";
pinctrl-0 =<&pinctrl_csm300>;
gpios=<&gpio3 21 0    /*ctl0*/&gpio3 22 0 /*ctl1*/&gpio3 30 0 /*rst*/&gpio3 31 0 /*cfg*/>;
interrupt-parent = <&gpio3>;
interrupts = <26 IRQ_LEVEL_LOW>;
compatible = "zhiyuan,csm300";
spi-max-frequency = <500000>;
reg = <1>;
status = "okay";};

The following is the result of modification according to your own platform , Readers need to transplant according to their own platform , No dogma .

[email protected] {
pinctrl-names = "default";
gpios=<&gpio 114 0    /*ctl0*/&gpio 113 0 /*ctl1*/&gpio 76 0 /*rst*/&gpio 107 0 /*cfg*/>;
interrupt-parent = <&gpio>;
interrupts = <196 IRQ_LEVEL_LOW>;
compatible = "zhiyuan,csm300";
spi-max-frequency = <500000>;
reg = <0>;
status = "okay";};

3. drive

The official will provide the driver csm300.c, The concrete practical principle , This article will not discuss .

Copy to the following directory :

drivers/net/can/spi

Modify the Makefile

 obj-$(CONFIG_CAN_CSM300)+= csm300.o

Modify the Kconfig

config CAN_CSM300
tristate "Microchip CSM300 driver"
depends on SPI 
---help---
  Driver for the Microchip CSM300  .

perform make menuconfig
The driving position is as follows :
menuconfig
Select the driver :

menuconfig Recompile the kernel .

Be careful : The driver also needs to rely on CAN and SPI, Be sure to choose .

4. Add debug interface

In the course of debugging , There are various reasons for csm300 Driver failed to register successfully , Then how to judge is spi There's something wrong with the controller drive , still csm300 There's something wrong with the drive ?

For the convenience of passing through spi The controller sends out the waveform , We add the following code , Used in board subdirectories /sys/bus/spi/drivers/csm300 Created in state The file nodes , By writing different values to produce spi data , Or control RST、 CFG、 CTL0、 CTL1 this 4 One pin .

  1. Add function csm300_spi_store()
     Test interface Focus on the function **check_csm300()** It's built-in for testing CSM300 Of SPI Function of communication function .

This function will first put CSM300A Set to SPI Configuration mode , And then write 9 Data , And then read out the data , Check whether the data is correct .

  1. modify probe function
struct net_device *global_net = NULL ;csm_probe(){
……
global_net = net;
ret = check_csm300(net);
……
ret = driver_create_file(&(csm300_can_driver.driver),&driver_attr_state);if(ret < 0){
ret = -ENOENT;goto out_free;}
……}

  1. The test command
    Get into csm300 Module directory
cd /sys/bus/spi/drivers/csm300

  1. produce spi data
echo 3 > state

  1. pull up RST、 CFG、 CTL0、 CTL1
echo 1 > state

  1. Pull it down RST、 CFG、 CTL0、 CTL1
echo 0 > state

5. Right boot up log And waveform

The driver will call the check_csm300() To test spi passageway , The data sent is F7:F8:02……
 Right boot up log
The following is a SPI Interface CLK and MOSI The waveform of the pin :
 Turn it on SPI Waveform of You can see that the data is consistent with what we sent .

6. Receiving data waveform

The steps of receiving data are as follows :

  1. To run on PC Upper CAN Test Software sends data 00 01 02 03 04 05 06 07,
  2. after USB turn CAN After equipment , Converted to a differential signal ,
  3. arrive CSM300A after , The signal is modulated into a square wave ,
  4. CSM300A By pulling down the pins INT towards cpu Send interrupt signal , call CSM300A Registered interrupt function ,
  5. To run on CPU Upper CSM300A Interrupt program through SPI Interface read and go CSM300A The data on the ,
  6. CSM300A After the buffer data is read , pull up INT,
  7. The driver uploads the received data to the application layer , therefore candump The order was given CAN Frame data .

 Receiving data flow The data transmission process is similar to the above process .

7. CAN command

If there is no can command , It needs to be self transplanted .

1) Set baud rate and turn on can0 mouth

ip link set can0 up type can bitrate 800000

2) send data

cansend can0 1F334455#1122334455667788

3) View the data received

candump can0

7、 ... and 、 Error records

There are a lot of errors during debugging ,CSM300A Steps to locate the problem :

  1. First, test with an oscilloscope CSM300 Of MOSI The waveform of the pin , Is it the same as chapter seven 5 The waveforms of the nodes are the same , If it's not consistent , explain SPI The controller driver is not loaded correctly ;
  2. To pass the SPI The controller generates data , Use command echo 3 > state;
  3. If the waveforms are consistent , Just measure RST、 CFG、 CTL0、 CTL1 These four pins , Check if the level is correct ;
  4. RST、 CFG、 CTL0、 CTL1 Is the control correct , It can be used echo 0 > state、echo 1 > state Lower and higher, respectively , Check whether the control of these pins is normal .

Basically, debugging according to this idea can quickly locate the problem .

Here are the driver loading errors log, The main reason for the error is to call check_csm300() Functional direction CSM300A After the data is written and read out, the data does not match , So as to determine the loading error .
 error log

1. CFG Abnormal pin pull down

The phenomenon :
check_csm300() Functions always report errors .

analysis :
check You don't succeed , Basically, the reason is SPI Controller and CSM300 There's something wrong with the communication .
First, use an oscilloscope , see SPI Whether the sent data arrives normally CSM300( Grab... With an oscilloscope SSEL、CLK、MOSI), The results are normal .

So the test RST、 CFG、 CTL0、 CTL1 Four pins .
As shown in the figure below , Use echo 0 > state Pull it down CFG Pin , I found that I didn't pull 0V.
 Insert picture description here Solution :

Give it to the hardware engineer . This brother gave CFG Add a reverse resistor , The driver needs to set all the settings CFG Code for , It's all reversed .

gpio_set_value(priv->CFG,0); Modified into gpio_set_value(priv->RST,1);

gpio_set_value(priv->CFG,1); Modified into gpio_set_value(priv->RST,0);

2. RST The delay is not enough

The phenomenon :
echo 0 > state You can pull it down , The measurement is also correct , however CSM300 Never receive data frame .

analysis :
General data is not received , There are two possibilities : Namely CSM300 The interrupt signal given CPU No interception of ,CSM300 Not in SPI turn CAN Pattern .

Use the oscilloscope first ,USB turn CAN Our data has arrived successfully CSM300, So the corresponding pin level is detected RST、 CFG、 CTL0、 CTL1, Discovery is also right .

Check the interrupt count , use cat /proc/interrupts see CSM300 Whether there is an interrupt count , It turns out that the data is 0.

doubt CSM300 No, rst success , Then perform echo 3 > state, see rst Is it set correctly , It turns out that the following waveforms , Make sure that the pin is pulled up slowly , therefore CSM300 We can't sample this level .

 Insert picture description here

Modification method :
Every time in the drive rst operation , Increase the delay time :

gpio_set_value(priv->RST,0);usleep_range(2000,2300);gpio_set_value(priv->RST,1);

After modification , perform echo 3 > state,RST The waveform is as follows .
 Insert picture description here