slave-testunit-backend.rst 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ================================
  3. Linux I2C slave testunit backend
  4. ================================
  5. by Wolfram Sang <[email protected]> in 2020
  6. This backend can be used to trigger test cases for I2C bus masters which
  7. require a remote device with certain capabilities (and which are usually not so
  8. easy to obtain). Examples include multi-master testing, and SMBus Host Notify
  9. testing. For some tests, the I2C slave controller must be able to switch
  10. between master and slave mode because it needs to send data, too.
  11. Note that this is a device for testing and debugging. It should not be enabled
  12. in a production build. And while there is some versioning and we try hard to
  13. keep backward compatibility, there is no stable ABI guaranteed!
  14. Instantiating the device is regular. Example for bus 0, address 0x30:
  15. # echo "slave-testunit 0x1030" > /sys/bus/i2c/devices/i2c-0/new_device
  16. After that, you will have a write-only device listening. Reads will just return
  17. an 8-bit version number of the testunit. When writing, the device consists of 4
  18. 8-bit registers and, except for some "partial" commands, all registers must be
  19. written to start a testcase, i.e. you usually write 4 bytes to the device. The
  20. registers are:
  21. 0x00 CMD - which test to trigger
  22. 0x01 DATAL - configuration byte 1 for the test
  23. 0x02 DATAH - configuration byte 2 for the test
  24. 0x03 DELAY - delay in n * 10ms until test is started
  25. Using 'i2cset' from the i2c-tools package, the generic command looks like:
  26. # i2cset -y <bus_num> <testunit_address> <CMD> <DATAL> <DATAH> <DELAY> i
  27. DELAY is a generic parameter which will delay the execution of the test in CMD.
  28. While a command is running (including the delay), new commands will not be
  29. acknowledged. You need to wait until the old one is completed.
  30. The commands are described in the following section. An invalid command will
  31. result in the transfer not being acknowledged.
  32. Commands
  33. --------
  34. 0x00 NOOP (reserved for future use)
  35. 0x01 READ_BYTES (also needs master mode)
  36. DATAL - address to read data from (lower 7 bits, highest bit currently unused)
  37. DATAH - number of bytes to read
  38. This is useful to test if your bus master driver is handling multi-master
  39. correctly. You can trigger the testunit to read bytes from another device on
  40. the bus. If the bus master under test also wants to access the bus at the same
  41. time, the bus will be busy. Example to read 128 bytes from device 0x50 after
  42. 50ms of delay:
  43. # i2cset -y 0 0x30 0x01 0x50 0x80 0x05 i
  44. 0x02 SMBUS_HOST_NOTIFY (also needs master mode)
  45. DATAL - low byte of the status word to send
  46. DATAH - high byte of the status word to send
  47. This test will send an SMBUS_HOST_NOTIFY message to the host. Note that the
  48. status word is currently ignored in the Linux Kernel. Example to send a
  49. notification after 10ms:
  50. # i2cset -y 0 0x30 0x02 0x42 0x64 0x01 i
  51. 0x03 SMBUS_BLOCK_PROC_CALL (partial command)
  52. DATAL - must be '1', i.e. one further byte will be written
  53. DATAH - number of bytes to be sent back
  54. DELAY - not applicable, partial command!
  55. This test will respond to a block process call as defined by the SMBus
  56. specification. The one data byte written specifies how many bytes will be sent
  57. back in the following read transfer. Note that in this read transfer, the
  58. testunit will prefix the length of the bytes to follow. So, if your host bus
  59. driver emulates SMBus calls like the majority does, it needs to support the
  60. I2C_M_RECV_LEN flag of an i2c_msg. This is a good testcase for it. The returned
  61. data consists of the length first, and then of an array of bytes from length-1
  62. to 0. Here is an example which emulates i2c_smbus_block_process_call() using
  63. i2ctransfer (you need i2c-tools v4.2 or later):
  64. # i2ctransfer -y 0 w3@0x30 0x03 0x01 0x10 r?
  65. 0x10 0x0f 0x0e 0x0d 0x0c 0x0b 0x0a 0x09 0x08 0x07 0x06 0x05 0x04 0x03 0x02 0x01 0x00