樹莓派 HATs ︰ I2C 拾遺《一》

若以 HATs add-on board 之目的而言,前面文本以及軟體工具已經具足。假使應用上需要直接讀寫 I2C ID EEPROM ,那麼務須先了解設計指南裡有關 ID EEPROM 選擇考慮的一段文字︰

ID EEPROM

Within the set of pins available on the J8 GPIO header, ID_SC and ID_SD (GPIO0/SCL and GPIO1/SDA) are reserved for board detection / identification. The only allowed connections to the ID_ pins are an ID EEPROM plus 3.9K pull up resistors. Do not connect anything else to these pins!

The ID EEPROM is interrogated at boot time and provides the Pi with the vendor information, the required GPIO setup (pin settings and functions) for the board as well as a binary Linux device tree fragment which also specifies which hardware is used and therefore which drivers need loading. EEPROM information is also available to userland Linux software for identifying attached boards (probably via a sysfs interface but this is TBD).

Pull-ups must be provided on the top board for ID_SC and ID_SD (SCL and SDA respectively) to 3V3 if using an EEPROM. The required pull-up value is 3.9K.

EEPROM Device Specification

  • 24Cxx type 3.3V I2C EEPROM must be used (some types are 5V only, do not use these).
  • The EEPROM must be of the 16-bit addressable type (do not use ones with 8-bit addressing)
  • Do not use ‘paged’ type EEPROMs where the I2C lower address bit(s) select the EEPROM page.
  • Only required to support 100kHz I2C mode.
  • Devices that perform I2C clock stretching are not supported.
  • Write protect pin must be supported and protect the entire device memory.

Note that due to the restrictions above (only using non-paged 16-bit addressable devices is allowed), many of the smaller I2C EEPROMs are ruled out – please check datasheets carefully when choosing a suitable EEPROM for your HAT.

A recommended part that satisfies the above constraints is OnSemi CAT24C32 which is a 32kbit (4kbyte) device. The minimum EEPROM size required is variable and depends on the size of the vendor data strings in the EEPROM and whether a device tree data blob is included (and its size) and whether any other vendor specific data is included.

It is recommended that EEPROM WP (write protect) pin be connected to a test point on the board and pulled up to 3V3 with a 1K resistor. The idea is that at board test/probe the EEPROM can be written (WP pin can be driven LOW), but there is no danger of a user accidentally changing the device contents once the board leaves the factory. Note that the recommended device has an internal pull down hence the stiff (1K) pull up is required. Note that on some devices WP does not write protect the entire array (e.g. some Microchip variants) – avoid using these.

It may be desirable for a board to have the ability for its EEPROM to be reflashed by an end user, in this case it is recommended to also include a user settable jumper to short WP to GND and make the EEPROM writable once more. At least this way a user has to perform a specific action to make the EEPROM writeable again before being able to re-flash it and a suitable warning process can be put in place to make sure the correct image is used.

Address pins where present on a device should be set to make sure the EEPROM I2C address is 0x50. This usually means tying them all to zero. (NB reduced pin count variants of the recommended device – e.g. SOT23-5 package – usually have A[2:0] set to 0 anyway).

Details of the EEPROM data format can be found in the EEPROM format specification. Software tools are available for creation of valid EEPROM images, to flash an image or read and dump and image to/from an attached ID EEPROM.

The following schematic fragment is an example of connecting an EEPROM including a jumper and probe point to disable write protect.

eeprom-circuit

 

由於 CAT24CXX 這種 EEPROM 之 I2C 運作模式,

【參考圖示】

cat24c32-1

 

cat24c32-2

 

cat24c32-3

 

與 SMBus 想法的差異,過去在 x86 PC 上,就有此類讀寫相容之『軟體工具』設計︰

eeprog is a Linux C program that allows you to read and write to 24Cxx EEPROM.

24Cxx EEPROM use the I2C protocol but most common controllers found in most PC hardware only support SMBus (a superset of I2C).

What eeprog does is using SMBus commands to read/write to I2C EEPROM so almost every controller could be used.

To use it you have to connect someway a 24Cxx EEPROM to the SMBus bus of your PC.

I connected a 24C32(32kbit EEPROM) to the I2C connector of my Via Epia board but you could use one of those parallel-to-I2C devices (I’ve never seen/used one of those).

 

後來有人將之癹 POST 到樹莓派來︰

Rasberry Pi I2C EEPROM Program

eeprog

eeprog-0.7.6-tear5.tar.gz NOT the original eeprog as it wont program (anything I’ve tried) on a Pi, you will get this without a write cycle time delay

  Writing stdin starting at address 0x0
..Error i2c_write_3b: Input/output error
Error at line 150: write error

write

root@raspberrypi:/tmp/eeprog-0.7.6-tear12# date | ./eeprog -f -16 -w 0 -t 5 /dev/i2c-0 0x53
eeprog 0.7.6-tear12, a 24Cxx EEPROM reader/writer
Copyright (c) 2003-2004 by Stefano Barbato - All rights reserved.
Copyright (c) 2011 by Kris Rusocki - All rights reserved.
  Bus: /dev/i2c-0, Address: 0x53, Mode: 16bit
  Operation: write at offset 0, Input file: <stdin>
  Write cycle time: 5 milliseconds
  Writing <stdin> starting at address 0x0
.............................

read

root@raspberrypi:/tmp/eeprog-0.7.6-tear12# ./eeprog -xf /dev/i2c-0 0x53 -16 -r 0x00:0x100
eeprog 0.7.6-tear12, a 24Cxx EEPROM reader/writer
Copyright (c) 2003-2004 by Stefano Barbato - All rights reserved.
Copyright (c) 2011 by Kris Rusocki - All rights reserved.
  Bus: /dev/i2c-0, Address: 0x53, Mode: 16bit
  Operation: read 256 bytes from offset 0, Output file: <stdout>
  Reading 256 bytes from 0x0
 
 0000|  46 72 69 20 41 75 67 20   31 35 20 32 32 3a 32 36 
 0010|  3a 33 35 20 55 54 43 20   32 30 31 34 0a 00 00 00 
 0020|  00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00 
 0030|  00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00 
 0040|  00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00 
<SNIP>

 

還請讀者先行閱讀相關資料,來趟 24C32 I2C EEPROM 讀寫之旅。