added utils

This commit is contained in:
adrcs 2025-01-29 11:17:14 -07:00 committed by GitHub
parent a9a3f739b4
commit a9b13d2c39
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
85 changed files with 9213 additions and 0 deletions

18
rpi/eeprom/Makefile Normal file
View file

@ -0,0 +1,18 @@
# Current tools are eepmake and eepdump
CC ?= gcc
all: eepmake eepdump
eepmake: eeptypes.h eepmake.c
$(CC) eepmake.c -o eepmake -Wno-format
eepdump: eeptypes.h eepdump.c
$(CC) eepdump.c -o eepdump -Wno-format
clean:
rm -f eepmake eepdump
eeprom: STM32wl33.txt
eepmake -v1 STM32wl33.txt STM32wl33.eep

12
rpi/eeprom/README.txt Normal file
View file

@ -0,0 +1,12 @@
Utilities to create, flash and dump HAT EEPROM images.
Edit eeprom_setting.txt for your particular board and run through
eepmake tool, then use eepflash tool to write to attached HAT ID EEPROM
Tools available:
eepmake: Parses EEPROM text file and creates binary .eep file
eepdump: Dumps a binary .eep file as human readable text (for debug)
eepflash: Write or read .eep binary image to/from HAT EEPROM

BIN
rpi/eeprom/STMwl33.eep Normal file

Binary file not shown.

67
rpi/eeprom/STMwl33.txt Normal file
View file

@ -0,0 +1,67 @@
########################################################################
# EEPROM settings text file
#
# Edit this file for your particular board and run through eepmake tool,
# then use eepflash tool to write to attached HAT ID EEPROM
#
# Tools available:
# eepmake Parses EEPROM text file and creates binary .eep file
# eepdump Dumps a binary .eep file as human readable text (for debug)
# eepflash Write or read .eep binary image to/from HAT EEPROM
#
########################################################################
########################################################################
# Vendor info
# 128 bit UUID. If left at zero eepmake tool will auto-generate
# RFC 4122 compliant UUID
product_uuid 00000000-0000-0000-0000-000000000000
# 16 bit product id
product_id 0x0001
# 16 bit product version
product_ver 0x0002
# ASCII vendor string (max 255 characters)
vendor "Alberta Digital Radio Communications Society"
# ASCII product string (max 255 characters)
product "IP400 E04 HAT Rev A"
########################################################################
# GPIO bank settings, set to nonzero to change from the default.
# NOTE these setting can only be set per BANK, uncommenting any of
# these will force the bank to use the custom setting.
# drive strength, 0=default, 1-8=2,4,6,8,10,12,14,16mA, 9-15=reserved
gpio_drive 0
# 0=default, 1=slew rate limiting, 2=no slew limiting, 3=reserved
gpio_slew 0
# 0=default, 1=hysteresis disabled, 2=hysteresis enabled, 3=reserved
gpio_hysteresis 0
# If board back-powers Pi via 5V GPIO header pins:
# 0 = board does not back-power
# 1 = board back-powers and can supply the Pi with a minimum of 1.3A
# 2 = board back-powers and can supply the Pi with a minimum of 2A
# 3 = reserved
# If back_power=2 then USB high current mode will be automatically
# enabled on the Pi
back_power 1
########################################################################
# GPIO pins, uncomment for GPIOs used on board
# Options for FUNCTION: INPUT, OUTPUT, ALT0-ALT5
# Options for PULL: DEFAULT, UP, DOWN, NONE
# GPIO FUNCTION PULL
# ---- -------- ----
setgpio 10 OUTPUT NONE # BOOT_ENA
setgpio 11 OUTPUT NONE # RESET
setgpio 14 ALT0 DEFAULT # TXD0
setgpio 15 ALT0 DEFAULT # RXD0

27
rpi/eeprom/docs/LICENCE Normal file
View file

@ -0,0 +1,27 @@
Copyright (c) 2014-2017, Raspberry Pi (Trading) Ltd.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

81
rpi/eeprom/docs/README.md Normal file
View file

@ -0,0 +1,81 @@
# ADD-ON BOARDS AND HATs
**NOTE** All references to GPIO numbers within this document are referring to the BCM283x GPIOs (**NOT** pin numbers on the Pi GPIO header).
## Introduction
The Raspberry Pi boards with 40W GPIO headers (Model B+ and onwards) and have been designed specifically with add-on boards in mind. These boards support 'HATs' (Hardware Attached on Top). A HAT is an add-on board that conforms to the HAT specifications. HATs are not backward compatible with original Raspberry Pi 1 models A and B.
In October 2018 Raspberry Pi introduced the Micro-HAT (uHAT) specification. uHATs must follow all of the standard electrical HAT rules as laid our for normal HATs, but they have a smaller mechanical form factor as specified [here](uhat-board-mechanical.pdf)
There are obviously a lot of add-on boards designed for the original model A and B boards (which interface to the original 26 way GPIO header). The first 26 pins of the 40W GPIO header are identical to those of the original models, so most existing boards will still work.
The biggest change with HAT add-on boards versus older boards designed for models A and B is that the 40W header has 2 special pins (ID_SC and ID_SD) that are reserved exclusively for attaching an 'ID EEPROM'. The ID EEPROM contains data that identifies the board, tells the Pi how the GPIOs need to be set up and what hardware is on the board. This allows the add-on board to be automatically identified and set up by the Pi software at boot time including loading all the necessary drivers.
While we cannot force anyone to follow our minimum requirements or HAT specification, doing so will make users lives easier, safer, and will make us more likely to recommend a product. Likewise if one of the minimum requirements is ignored we are unlikely to look on a product very favourably.
So why are we bothering with all this? Basically we want to ensure consistency and compatibility with future add-on boards, and to allow a much better end-user experience, especially for the less technically aware users.
Finally if you have any questions please head over to the [forums](http://www.raspberrypi.org/forums/viewforum.php?f=100) to ask them.
## New HATs / add-on boards basic requirements
If you are designing a new add-on board that takes advantage of the pins on the 40W GPIO header **other than the original 26** then you **must** follow the basic requirements:
1. The ID_SC and ID_SD pins must only be used for attaching a compatible ID EEPROM. **Do not use ID_SC and ID_SD pins for anything except connecting an ID EEPROM, if unused these pins must be left unconnected**
2. If back-powering via the 5V GPIO header pins you must make sure that it is safe to do so even if the Pi 5V supply is also connected. Adding an ideal 'safety' diode as per the relevant section of the [design guide](designguide.md) is the recommended way to do this.
3. The board must protect against old firmware accidentally driving GPIO6,14,16 at boot time if any of those pins are also driven by the board itself.
Note that for new designs that only use the original 26 way GPIO header pins it is still recommended to follow requirement 2. if the board supports back-powering a Pi.
## HAT requirements
A board can only be called a HAT if:
1. It conforms to the basic add-on board requirements
2. It has a valid ID EEPROM (including vendor info, GPIO map and valid device tree information).
3. It has a full size 40W GPIO connector.
4. It follows the HAT [mechanical specification](hat-board-mechanical.pdf)
5. It uses a GPIO connector that spaces the HAT at least 8mm from the Pi (i.e. uses spacers 8mm or larger - also see note on PoE header below)
6. If back powering via the GPIO connector the HAT must be able to supply a minimum of 1.3A continuously to the Pi (but ability to supply 2A continuously recommended).
Of course users are free to put an ID EEPROM on boards that don't otherwise conform to the remainder of the specifications - in fact we strongly encourage this; we just want things called HATs to be a known and well-specified entity to make life easier for customers, particularly the less technical ones.
NOTE that the Pi3B+ introduced a new 4-pin PoE header near the top-right corner mounting hole. Newly designed HATs that do not provide a connector for this header must avoid fouling it.
## Design Resources
Before designing any new add-on board (HAT compliant or not) please read the [design guide](designguide.md) carefully.
For what to flash into the ID EEPROM see the [ID EEPROM data format spec](eeprom-format.md).
There are tools and documentation on how to flash ID EEPROMs [here](./eepromutils).
## FAQ
**Q: I want to keep shipping an existing board / ship a new board that only connects to the original 26W GPIO pins.**
This is OK. You can't call it a HAT.
If the board will back-power the Pi we recommend adding the safety diode as per requirement 2. of the basic add-on board requirements.
**Q: I want to ship a board that attaches to the 40W GPIO header and covers ID_SD and ID_SC but does not include an EEPROM.**
This is OK as long as it meets the basic requirements. You can't call it a HAT.
**Q: I want to ship a board that has an ID EEPROM but does not conform to the remaining HAT specs.**
This is OK as long as it also meets the basic requirements. You can't call it a HAT but you **can** say it supports GPIO autoconfiguration if the EEPROM contains valid vendor, GPIO map and DT blob information.
**Q: I want to ship a HAT but the software for creating the EEPROM and/or DT blob isn't ready yet.**
We expect all HATs to have a correctly programmed EEPROM, but bugs can happen, therefore make sure the EEPROM is user flashable. You will need to add some ability for a user to un-write-protect the EEPROM to (re-)flash it themselves as suggested in the [design guide](designguide.md). Please provide instructions on your website / product packaging for how to reflash the board when any new image becomes available.
**Q: I'm using the HAT mechanical spec but don't want to / can't add the cutout / slot for the display / camera flex.**
This is OK and the board still conforms to the HAT specification. Some HATs will not be able to support the slot/cutout based on where the connectors and components must be placed (but it is recommended to support them if at all possible).
**Q: I want to create a board that connects to the 'RUN' or 'PEN' header pin(s).**
No problem but you can't call it a HAT.
HATs are designed to be easy to use. Using the RUN pin requires a user to solder a header onto the Pi hence this is not something we wish to include in the HAT spec.

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

View file

@ -0,0 +1,88 @@
# ADD-ON BOARD / HAT DESIGN GUIDE
---
## GPIO Pins
**NOTE** All references to GPIO numbers within this document are referring to the BCM283x GPIOs (**NOT** pin numbers on the GPIO header).
## GPIO Configuration Sequencing
The default state for all Bank 0 pins will be inputs with either a pull up or pull down. The default pull state can be found in the [BCM2835 peripherals specificaion](http://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2835/BCM2835-ARM-Peripherals.pdf) section 6.2 table 6-31 (see the "Pull" column). It will require positive software configuration to change the state of these pins.
The one exception to this rule is ID_SC and ID_SD. GPIO0 (ID\_SD) and GPIO1 (ID\_SC) will be switched to ALT0 (I2C-0) mode and probed for an EEPROM at boot time. These pins will revert to inputs once the probe sequence has completed.
### Videocore GPIO setup
**As of 11/03/15, this section has been (largely) implemented in firmware. Only the gpio_hysteresis, gpio_slew and back_power are currently unsupported. **
At a very early point in the boot process, either GPIOMAN (for recent firmware) or the Videocore platform code sets the initial state of GPIO pins.
Note: For legacy platform code, several defaults were hardcoded. A Pi with old firmware (firmware older than the model release date) will often assume that it is a Pi 1 Model B and as such up to three output pins may be driven as outputs on boot. See the following section for the GPIOs that may be driven as output by default and the recommended method of designing hardware to guard against this.
After the GPIOMAN/platform code stage the VC bootloader will attempt to probe for an EEPROM attached to GPIO0 and GPIO1. If successful, a GPIO setup map (as described in the EEPROM data format) will be applied to Bank 0 pins.
Note: For newer firmware, a config.txt option exists to enable the UART on GPIO14/GPIO15 prior to booting the ARM. The EEPROM probe routine will override this behaviour if an EEPROM is found.
**At this point the ARM is booted.** Linux now has notional exclusive control over the settings for Bank 0 pins (except GPIO0 ALT0 and GPIO1 ALT0) from this point onward.
### GPIO Requirements
GPIO pins ID_SC and ID_SD (GPIO0 and GPIO1) are reserved for use solely 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!**
Raspberry Pi models A and B use some bank 0 GPIOs for board control functions and UART output:
GPIO6 -> LAN_RUN
GPIO14 -> UART_TX
GPIO16 -> STATUS_LED
**If a user boots a Pi with legacy firmware these pins may get driven so an add-on board must avoid driving these, or use a current limiting resistor (or some other protection) if that is not possible. Note also that a board must not rely on the pull state of these pins during boot**
## ID EEPROM
Within the set of pins available on the 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](eeprom-format.md). [Software tools](./eepromutils) 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](eeprom-circuit.png) is an example of connecting an EEPROM including a jumper and probe point to disable write protect.
## Mechanical Specification
The [following drawing](hat-board-mechanical.pdf) gives the mechanical details for add-on boards which conform to the HAT specification.
## Back Powering the Pi via the GPIO Header
It is possible to power the Pi by supplying 5V through the GPIO header pins 2,4 and GND. The acceptable input voltage range is 5V ±5%.
Raspberry Pi Model A+, B+, Raspberry Pi 2B and 3B have an 'ideal' reverse current blocking diode (ZVD) circuit on their 5V input. The 5V GPIO header pins connect to the 5V net after the micro-USB input, polyfuse and input 'ideal' diode [made up of the PFET and matched PNP transistors](zvd-circuit.png). The ideal diode stops any appreciable current flowing back out of the 5V micro USB should the 5V net on the board be at a higher voltage than the 5V micro USB input.
If the add-on board uses any more GPIO connector pins than the first 26 and provides back-powering via the 5V GPIO header pins it is required to:
1. Implement a duplicate power safety diode before the HAT 5V net (which then feeds power back through the 5V GPIO pins) as shown in [this diagram](backpowering-diagram.png). Alternatively provide some other mechanism to guarantee that it is safe if both the Pi PSU and add-on board PSU are connected. It is still recommended to add this circuitry for new board designs that only implement the first 26 pins of the GPIO header but that also implement back powering (see below note)
2. Make sure that the supply that does the back-powering can supply 5V at 2.5A.
NOTE that the Raspberry Pi 3B+ and Pi Zero and ZeroW **do not** include an input ZVD. The micro-USB input on a Pi is expected to / almost universally is driven by a power source which does not sink current, i.e. it will not try to actively pull down a voltage higher than its regulated voltage. If a HAT back-powers a Pi and uses a power source that does not try to sink current (and will safely stop/pause regulation if its input voltage is higher than its regulation voltage) it is OK to not include a ZVD on a HAT. If you are unsure or don't know then please include the ZVD! The HAT designer is responsible for the safety of their product.
**Under no circumstances should a power source be connected to the GPIO header 3.3V pins.**

View file

@ -0,0 +1,92 @@
# HAT Device Tree Blob guide
Within the HAT specification is the requirement to include a data atom that contains either the name or the content of a Device Tree overlay.
First, read and understand what a Device Tree is and how it is used by system software (i.e. Linux).
[Device Tree Usage](http://elinux.org/Device_Tree_Usage)
[Device Tree For Dummies](http://events.linuxfoundation.org/sites/events/files/slides/petazzoni-device-tree-dummies.pdf)
An extension to this mechanism of hardware description is the introduction of an overlay: a partially-complete DT fragment that "joins up" to a higher-level Device Tree. The basic idea is that this overlay will be read on boot (by the VC bootloader), parsed and merged into the system-level Device Tree passed through to the ARM running Linux.
The purpose of this overlay is that it allows true automatic configuration of all devices attached to the HAT, as long as there are Linux device drivers available for the hardware in question.
The DT overlay will be of particular use to add-on board designers making use of the I2C-1, SPI and I2S buses that require chip selects / addresses to be specified in order to be probed and set up correctly by the respective Linux device driver, but it will also allow simple devices such as LEDs and buttons to be recognised by Linux and connected up to e.g. the kernel input events subsystem.
The process of writing an overlay is outside the scope of this document. Readers are instead referred to this Raspberry Pi document:
[Device Trees, Overlays and Parameters](http://www.raspberrypi.org/documentation/configuration/device-tree.md)
### Populating the Device Tree Atom
There are two approaches to providing an overlay for a HAT:
#### a) Embed the overlay in the EEPROM
You can build an overlay into the EEPROM image (`*.eep`) with the following command:
```
$ eepmake mysettings.txt myhat.eep myoverlay.dtbo
```
This assumes that your settings file is called `mysettings.txt`, the overlay is called `myoverlay.dtbo`, and you want the output to go to `myhat.eep` - modify the names as appropriate.
At boot time, the firmware will read the overlay directly from the EEPROM and apply it. Building the overlay in guarantees that the user has the it installed. However, all but the simplest hardware is likely to require a custom driver, and having an overlay without the necessary drivers is no better than not having the overlay, so users may still need to update their OS.
#### b) Put the name of the overlay in the EEPROM
Alternatively, you can build just the name of overlay into the EEPROM image like this:
```
$ echo myoverlay > myoverlay_name.txt
$ eepmake mysettings.txt myhat.eep myoverlay_name.txt
```
It doesn't matter about trailing whitespace in the text file - the firmware will ignore it.
When the firmware reads the EEPROM during boot it detects that the atom content doesn't look a `.dtbo` file, and tries to interpret it as the name of an overlay to load in the usual way. This relies on the overlay being installed, but has the advantage that it is easier to update the overlay should it become necessary.
### Adding overlays and drivers to the Raspberry Pi kernel
Some HATs will require custom drivers and/or an overlay. The source for the Raspberry Pi kernels and overlays can be found in the [Raspberry Pi Linux GitHub repo](https://github.com/raspberrypi/linux). Submitting code for inclusion in the Raspberry Pi kernel repo is fairly easy (if you understand git, harder if you don't):
1. Register for a GitHub account (free in most cases).
2. Clone the repo, make changes and commit them.
3. Fork the repo using the GitHub GUI.
4. Push the updates to your fork.
5. Create a Pull Request.
GitHub have [their own documentation for this](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests), but there is a more user-friendly introduction [here](https://opensource.com/article/19/7/create-pull-request-github).
### HAT identification
To avoid the need for modules and applications to access the EEPROM, the VC bootloader reads the vendor, product and custom data and populates the `/hat` node of the device tree. This can be read by kernel modules using the standard DT API (`of_*`), or via `/proc/device-tree/hat` from user-space. Here is an example of the sort of information you will find there:
```
/proc/device-tree/hat/vendor:
ACME Corp.
/proc/device-tree/hat/product:
Dunce HAT
/proc/device-tree/hat/product_id:
0x0001
/proc/device-tree/hat/product_ver:
0x0001
/proc/device-tree/hat/uuid:
c617cf6a-7de5-4948-0000-000001b86eb0
/proc/device-tree/hat/custom_0:
The boy stood on the burning deck
His lips were all a-quiver.
/proc/device-tree/hat/custom_1:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
```
The custom blobs are copied verbatim, and may contain arbitrary binary data.
It is permissible for the overlay to supplement this data with additional information, but the standard properties always take precedence, overwriting any user-supplied values. Conversely, if there is no HAT present then the `/hat` node is deleted just before boot.
### Pinctrl nodes vs GPIO map
Within the devicetree fragment is scope for adding pinctrl nodes that alter the setup of GPIO pins. The timing or sequencing used by pinctrl for GPIO setup may be non-trivial: for example a GPIO could be nominated as a reset pin for an external MCU that may require a reset pulse of a certain length, or a GPIO could be nominated to enable an external power chain that requires a certain period of stabilisation before attempting to talk to any attached devices. The Linux Pinctrl subsystem is designed to cater for complex requirements such as these.
The GPIO pin data map in the EEPROM is **still required** even if pinctrl configuration nodes are specified in the DT blob. The GPIO map is parsed by the Videocore bootloader prior to ARM boot and **not** Linux.

63
rpi/eeprom/docs/dtc.sh Normal file
View file

@ -0,0 +1,63 @@
#!/bin/sh -e
check_dpkg () {
LC_ALL=C dpkg --list | awk '{print $2}' | grep "^${pkg}" >/dev/null || deb_pkgs="${deb_pkgs}${pkg} "
}
unset deb_pkgs
pkg="bison"
check_dpkg
pkg="build-essential"
check_dpkg
pkg="flex"
check_dpkg
pkg="git-core"
check_dpkg
if [ "${deb_pkgs}" ] ; then
echo "Installing: ${deb_pkgs}"
sudo apt-get update
sudo apt-get -y install ${deb_pkgs}
sudo apt-get clean
fi
#git_sha="origin/master"
#git_sha="27cdc1b16f86f970c3c049795d4e71ad531cca3d"
#git_sha="fdc7387845420168ee5dd479fbe4391ff93bddab"
git_sha="65cc4d2748a2c2e6f27f1cf39e07a5dbabd80ebf"
project="dtc"
server="git://git.kernel.org/pub/scm/linux/kernel/git/jdl"
if [ ! -f ${HOME}/git/${project}/.git/config ] ; then
git clone ${server}/${project}.git ${HOME}/git/${project}/
fi
if [ ! -f ${HOME}/git/${project}/.git/config ] ; then
rm -rf ${HOME}/git/${project}/ || true
echo "error: git failure, try re-runing"
exit
fi
unset old_address
old_address=$(cat ${HOME}/git/${project}/.git/config | grep "jdl.com" || true)
if [ ! "x${old_address}" = "x" ] ; then
sed -i -e 's:git.jdl.com/software:git.kernel.org/pub/scm/linux/kernel/git/jdl:g' ${HOME}/git/${project}/.git/config
fi
cd ${HOME}/git/${project}/
make clean
git checkout master -f
git pull || true
test_for_branch=$(git branch --list ${git_sha}-build)
if [ "x${test_for_branch}" != "x" ] ; then
git branch ${git_sha}-build -D
fi
git checkout ${git_sha} -b ${git_sha}-build
git pull git://github.com/RobertCNelson/dtc.git dtc-fixup-65cc4d2
make clean
make PREFIX=/usr/local/ CC=gcc CROSS_COMPILE= all
echo "Installing into: /usr/local/bin/"
sudo make PREFIX=/usr/local/ install

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB

View file

@ -0,0 +1,102 @@
# B+ HAT ID EEPROM FORMAT SPECIFICATION
**CURRENTLY THIS SPEC IS PRELIMINARY AND THEREFORE STILL LIKELEY TO CHANGE**
Note that there are [software tools](./eepromutils) for creation of EEPROM images and for flashing / dumping images from attached HATs.
## EEPROM Structure
```
HEADER <- EEPROM header (Required)
ATOM1 <- Vendor info atom (Required)
ATOM2 <- GPIO map atom (Required)
ATOM3 <- DT blob atom (Required for compliance with the HAT specification)
...
ATOMn
```
## EEPROM Header Structure
```
Bytes Field
4 signature signature: 0x52, 0x2D, 0x50, 0x69 ("R-Pi" in ASCII)
1 version EEPROM data format version (0x00 reserved, 0x01 = first version)
1 reserved set to 0
2 numatoms total atoms in EEPROM
4 eeplen total length in bytes of all eeprom data (including this header)
```
## Atom Structure
```
Bytes Field
2 type atom type
2 count incrementing atom count
4 dlen length in bytes of data+CRC
N data N bytes, N = dlen-2
2 crc16 CRC-16-CCITT of entire atom (type, count, dlen, data)
```
## Atom Types
```
0x0000 = invalid
0x0001 = vendor info
0x0002 = GPIO map
0x0003 = Linux device tree blob
0x0004 = manufacturer custom data
0x0005-0xfffe = reserved for future use
0xffff = invalid
```
### Vendor info atom data (type=0x0001):
Note that the UUID is mandatory and must be filled in correctly according to RFC 4122
(every HAT can then be uniquely identified). It protects against the case where a user
accidentally stacks 2 identical HATs on top of each other - this error case is only
detectable if the EEPROM data in each is different. The UUID is also useful for
manufacturers as a per-board 'serial number'.
```
Bytes Field
16 uuid UUID (unique for every single board ever made)
2 pid product ID
2 pver product version
1 vslen vendor string length (bytes)
1 pslen product string length (bytes)
X vstr ASCII vendor string e.g. "ACME Technology Company"
Y pstr ASCII product string e.g. "Special Sensor Board"
```
### GPIO map atom data (type=0x0002):
GPIO map for bank 0 GPIO on 40W B+ header.
**NOTE** GPIO number refers to BCM2835 GPIO number and **NOT** J8 pin number!
```
Bytes Field
1 bank_drive bank drive strength/slew/hysteresis, BCM2835 can only set per bank, not per IO
Bits in byte:
[3:0] drive 0=leave at default, 1-8=drive*2mA, 9-15=reserved
[5:4] slew 0=leave at default, 1=slew rate limiting, 2=no slew limiting, 3=reserved
[7:6] hysteresis 0=leave at default, 1=hysteresis disabled, 2=hysteresis enabled, 3=reserved
1 power
[1:0] back_power 0=board does not back power Pi
1=board back powers and can supply up to 1.3A to the Pi
2=board back powers and can supply up to 2A to the Pi
3=reserved
If back_power=2 high current USB mode is automatically enabled.
[7:2] reserved set to 0
28 1 byte per IO pin
Bits in each byte:
[2:0] func_sel GPIO function as per FSEL GPIO register field in BCM2835 datasheet
[4:3] reserved set to 0
[6:5] pulltype 0=leave at default setting, 1=pullup, 2=pulldown, 3=no pull
[ 7] is_used 1=board uses this pin, 0=not connected and therefore not used
```
### Device Tree atom data (type=0x0003):
Binary data (the name or contents of a `.dtbo` overlay, for board hardware).
For more information on the Device Tree atom contents, see the [Device Tree Guide](devicetree-guide.md).

View file

@ -0,0 +1,14 @@
# Current tools are eepmake and eepdump
CC ?= gcc
all: eepmake eepdump
eepmake: eeptypes.h eepmake.c
$(CC) eepmake.c -o eepmake -Wno-format
eepdump: eeptypes.h eepdump.c
$(CC) eepdump.c -o eepdump -Wno-format
clean:
rm -f eepmake eepdump

Binary file not shown.

View file

@ -0,0 +1,87 @@
########################################################################
# EEPROM settings text file
#
# Edit this file for your particular board and run through eepmake tool,
# then use eepflash tool to write to attached HAT ID EEPROM
#
# Tools available:
# eepmake Parses EEPROM text file and creates binary .eep file
# eepdump Dumps a binary .eep file as human readable text (for debug)
# eepflash Write or read .eep binary image to/from HAT EEPROM
#
########################################################################
########################################################################
# Vendor info
# 128 bit UUID. If left at zero eepmake tool will auto-generate
# RFC 4122 compliant UUID
product_uuid 00000000-0000-0000-0000-000000000000
# 16 bit product id
product_id 0x0002
# 16 bit product version
product_ver 0x0003
# ASCII vendor string (max 255 characters)
vendor "Praebius Communications Inc"
# ASCII product string (max 255 characters)
product "MaplePi Radio Interface"
########################################################################
# GPIO bank settings, set to nonzero to change from the default.
# NOTE these setting can only be set per BANK, uncommenting any of
# these will force the bank to use the custom setting.
# drive strength, 0=default, 1-8=2,4,6,8,10,12,14,16mA, 9-15=reserved
gpio_drive 0
# 0=default, 1=slew rate limiting, 2=no slew limiting, 3=reserved
gpio_slew 0
# 0=default, 1=hysteresis disabled, 2=hysteresis enabled, 3=reserved
gpio_hysteresis 0
# If board back-powers Pi via 5V GPIO header pins:
# 0 = board does not back-power
# 1 = board back-powers and can supply the Pi with a minimum of 1.3A
# 2 = board back-powers and can supply the Pi with a minimum of 2A
# 3 = reserved
# If back_power=2 then USB high current mode will be automatically
# enabled on the Pi
back_power 1
########################################################################
# GPIO pins, uncomment for GPIOs used on board
# Options for FUNCTION: INPUT, OUTPUT, ALT0-ALT5
# Options for PULL: DEFAULT, UP, DOWN, NONE
# NB GPIO0 and GPIO1 are reserved for ID EEPROM so cannot be set
# GPIO FUNCTION PULL
# ---- -------- ----
setgpio 5 OUTPUT NONE # TMS
setgpio 6 OUTPUT NONE # TDI
setgpio 7 ALT0 DEFAULT # SPI0_CE1
setgpio 8 ALT0 DEFAULT # SPI0_CE0
setgpio 9 ALT0 DEFAULT # SPI0_MISO
setgpio 10 ALT0 DEFAULT # SPI0_MOSI
setgpio 11 ALT0 DEFAULT # SPI0_CLK
setgpio 12 OUTPUT NONE # TCK
setgpio 13 INPUT NONE # TDO
setgpio 14 ALT5 DEFAULT # TXD0
setgpio 15 ALT5 DEFAULT # RXD0
setgpio 16 ALT4 DEFAULT # SPI1_CE2
setgpio 17 ALT4 DEFAULT # SPI1_CE1
setgpio 18 ALT4 DEFAULT # SPI1_CE0
setgpio 19 ALT4 DEFAULT # SPI1_MISO
setgpio 20 ALT4 DEFAULT # SPI1_MOSI
setgpio 21 ALT4 DEFAULT # SPI1_SCLK

View file

@ -0,0 +1,12 @@
Utilities to create, flash and dump HAT EEPROM images.
Edit eeprom_setting.txt for your particular board and run through
eepmake tool, then use eepflash tool to write to attached HAT ID EEPROM
Tools available:
eepmake: Parses EEPROM text file and creates binary .eep file
eepdump: Dumps a binary .eep file as human readable text (for debug)
eepflash: Write or read .eep binary image to/from HAT EEPROM

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,230 @@
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include "eeptypes.h"
struct header_t header;
struct atom_t atom;
struct vendor_info_d vinf;
struct gpio_map_d gpiomap;
unsigned char* data;
int read_bin(char *in, char *outf) {
uint16_t crc;
FILE *fp, *out;
int i,j;
fp=fopen(in, "r");
if (!fp) {
printf("Error reading file %s\n", in);
return -1;
}
out=fopen(outf, "w");
if (!out) {
printf("Error writing file %s\n", outf);
return -1;
}
if (!fread(&header, sizeof(header), 1, fp)) goto err;
fprintf(out, "# ---------- Dump generated by eepdump handling format version 0x%02x ----------\n#\n", FORMAT_VERSION);
if (FORMAT_VERSION!=header.ver) fprintf(out, "# WARNING: format version mismatch!!!\n");
fprintf(out, "# --Header--\n# signature=0x%08x\n# version=0x%02x\n# reserved=%u\n# numatoms=%u\n# eeplen=%u\n# ----------\n\n\n", header.signature, header.ver, header.res, header.numatoms, header.eeplen);
for (i = 0; i<header.numatoms; i++) {
if (!fread(&atom, ATOM_SIZE-CRC_SIZE, 1, fp)) goto err;
printf("Reading atom %d...\n", i);
fprintf(out, "# Start of atom #%u of type 0x%04x and length %u\n", atom.count, atom.type, atom.dlen);
if (atom.count != i) {
printf("Error: atom count mismatch\n");
fprintf(out, "# Error: atom count mismatch\n");
}
long pos = ftell(fp);
char *atom_data = (char *) malloc(atom.dlen + ATOM_SIZE-CRC_SIZE);
memcpy(atom_data, &atom, ATOM_SIZE-CRC_SIZE);
if (!fread(atom_data+ATOM_SIZE-CRC_SIZE, atom.dlen, 1, fp)) goto err;
uint16_t calc_crc = getcrc(atom_data, atom.dlen-CRC_SIZE+ATOM_SIZE-CRC_SIZE);
fseek(fp, pos, SEEK_SET);
if (atom.type==ATOM_VENDOR_TYPE) {
//decode vendor info
if (!fread(&vinf, VENDOR_SIZE, 1, fp)) goto err;
fprintf(out, "# Vendor info\n");
fprintf(out, "product_uuid %08x-%04x-%04x-%04x-%04x%08x\n", vinf.serial_4, vinf.serial_3>>16, vinf.serial_3 & 0xffff, vinf.serial_2>>16, vinf.serial_2 & 0xffff, vinf.serial_1);
fprintf(out, "product_id 0x%04x\n", vinf.pid);
fprintf(out, "product_ver 0x%04x\n", vinf.pver);
vinf.vstr = (char *) malloc(vinf.vslen+1);
vinf.pstr = (char *) malloc(vinf.pslen+1);
if (!fread(vinf.vstr, vinf.vslen, 1, fp)) goto err;
if (!fread(vinf.pstr, vinf.pslen, 1, fp)) goto err;
//close strings
vinf.vstr[vinf.vslen] = 0;
vinf.pstr[vinf.pslen] = 0;
fprintf(out, "vendor \"%s\" # length=%u\n", vinf.vstr, vinf.vslen);
fprintf(out, "product \"%s\" # length=%u\n", vinf.pstr, vinf.pslen);
if (!fread(&crc, CRC_SIZE, 1, fp)) goto err;
} else if (atom.type==ATOM_GPIO_TYPE) {
//decode GPIO map
if (!fread(&gpiomap, GPIO_SIZE, 1, fp)) goto err;
fprintf(out, "# GPIO map info\n");
fprintf(out, "gpio_drive %d\n", gpiomap.flags & 15); //1111
fprintf(out, "gpio_slew %d\n", (gpiomap.flags & 48)>>4); //110000
fprintf(out, "gpio_hysteresis %d\n", (gpiomap.flags & 192)>>6); //11000000
fprintf(out, "back_power %d\n", gpiomap.power);
fprintf(out, "# GPIO FUNCTION PULL\n# ---- -------- ----\n");
for (j = 0; j<28; j++) {
if (gpiomap.pins[j] & (1<<7)) {
//board uses this pin
char *pull_str = "INVALID";
switch ((gpiomap.pins[j] & 96)>>5) { //1100000
case 0: pull_str = "DEFAULT";
break;
case 1: pull_str = "UP";
break;
case 2: pull_str = "DOWN";
break;
case 3: pull_str = "NONE";
break;
}
char *func_str = "INVALID";
switch ((gpiomap.pins[j] & 7)) { //111
case 0: func_str = "INPUT";
break;
case 1: func_str = "OUTPUT";
break;
case 4: func_str = "ALT0";
break;
case 5: func_str = "ALT1";
break;
case 6: func_str = "ALT2";
break;
case 7: func_str = "ALT3";
break;
case 3: func_str = "ALT4";
break;
case 2: func_str = "ALT5";
break;
}
fprintf(out, "setgpio %d %s %s\n", j, func_str, pull_str);
}
}
if (!fread(&crc, CRC_SIZE, 1, fp)) goto err;
} else if (atom.type==ATOM_DT_TYPE) {
//decode DT blob
fprintf(out, "dt_blob");
data = (char *) malloc(atom.dlen-CRC_SIZE);
if (!fread(data, atom.dlen-CRC_SIZE, 1, fp)) goto err;
for (j = 0; j<atom.dlen-CRC_SIZE; j++) {
if (j % 16 == 0) fprintf(out, "\n");
fprintf(out, "%02X ", *(data+j));
}
fprintf(out, "\n");
if (!fread(&crc, CRC_SIZE, 1, fp)) goto err;
} else if (atom.type==ATOM_CUSTOM_TYPE) {
//decode custom data
fprintf(out, "custom_data");
data = (char *) malloc(atom.dlen-CRC_SIZE);
if (!fread(data, atom.dlen-CRC_SIZE, 1, fp)) goto err;
for (j = 0; j<atom.dlen-CRC_SIZE; j++) {
if (j % 16 == 0) fprintf(out, "\n");
fprintf(out, "%02X ", *(data+j));
}
fprintf(out, "\n");
if (!fread(&crc, CRC_SIZE, 1, fp)) goto err;
} else {
printf("Error: unrecognised atom type\n");
fprintf(out, "# Error: unrecognised atom type\n");
goto err;
}
fprintf(out, "# End of atom. CRC16=0x%04x\n", crc);
if (calc_crc != crc) {
printf("Error: atom CRC16 mismatch\n");
fprintf(out, "# Error: atom CRC16 mismatch. Calculated CRC16=0x%02x", crc);
} else printf("CRC OK\n");
fprintf(out, "\n\n");
}
//Total length checks. We need header.eeplen=current_position=file_length.
long pos = ftell(fp);
fseek(fp, 0L, SEEK_END);
if (pos!=ftell(fp)) printf("Warning: Dump finished before EOF\n");
if (pos!=header.eeplen) printf("Warning: Dump finished before length specified in header\n");
if (ftell(fp)!=header.eeplen) printf("Warning: EOF does not match length specified in header\n");
printf("Done.\n");
fclose(fp);
fclose(out);
return 0;
err:
printf("Unexpected EOF or error occurred\n");
fclose(fp);
fclose(out);
return 0;
}
int main(int argc, char *argv[]) {
int ret;
int i;
if (argc<3) {
printf("Wrong input format.\n");
printf("Try 'eepdump input_file output_file'\n");
return 0;
}
ret = read_bin(argv[1], argv[2]);
if (ret) {
printf("Error reading input, aborting\n");
return 0;
}
return 0;
}

View file

@ -0,0 +1,171 @@
#!/bin/sh
MODE="NOT_SET"
FILE="NOT_SET"
TYPE="NOT_SET"
BUS="NOT_SET"
ADDR="NOT_SET"
usage()
{
echo "eepflash: Writes or reads .eep binary image to/from HAT EEPROM on a Raspberry Pi"
echo ""
echo "./eepflash.sh"
echo " -h --help: display this help message"
echo " -r --read: read .eep from the eeprom"
echo " -w --write: write .eep to the eeprom"
echo " -f=file_name --file=file_name: binary .eep file to read to/from"
echo " -d= --device= i2c bus number (ex if the eeprom is on i2c-0 set -d=0)"
echo " -a= --address= i2c eeprom address"
echo " -t=eeprom_type --type=eeprom_type: eeprom type to use"
echo " We support the following eeprom types:"
echo " -24c32"
echo " -24c64"
echo " -24c128"
echo " -24c256"
echo " -24c512"
echo " -24c1024"
echo ""
echo "Example:"
echo "./eepflash -w -f=crex0.1.eep -t=24c32 -d=1 -a=57"
echo "./eepflash -r -f=dump.eep -t=24c32 -d=1 -a=57"
echo ""
}
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
while [ "$1" != "" ]; do
PARAM=`echo $1 | awk -F= '{print $1}'`
VALUE=`echo $1 | awk -F= '{print $2}'`
case $PARAM in
-h | --help)
usage
exit
;;
-r | --read)
MODE="read"
;;
-w | --write)
MODE="write"
;;
-t | --type)
if [ "$VALUE" = "24c32" ] || [ "$VALUE" = "24c64" ] || [ "$VALUE" = "24c128" ] ||
[ "$VALUE" = "24c256" ] || [ "$VALUE" = "24c512" ] || [ "$VALUE" = "24c1024" ]; then
TYPE=$VALUE
else
echo "ERROR: Unrecognised eeprom type. Try -h for help"
exit 1
fi
;;
-d | --device)
BUS=$VALUE
;;
-a | --address)
ADDR=$VALUE
;;
-f | --file)
FILE=$VALUE
;;
*)
echo "ERROR: unknown parameter \"$PARAM\""
usage
exit 1
;;
esac
shift
done
if [ "$MODE" = "NOT_SET" ]; then
echo "You need to set mode (read or write). Try -h for help."
exit 1
elif [ "$FILE" = "NOT_SET" ]; then
echo "You need to set binary .eep file to read to/from. Try -h for help."
exit 1
elif [ "$TYPE" = "NOT_SET" ]; then
echo "You need to set eeprom type. Try -h for help."
exit 1
fi
echo "This will attempt to talk to an eeprom at i2c address 0x$ADDR on bus $BUS. Make sure there is an eeprom at this address."
echo "This script comes with ABSOLUTELY no warranty. Continue only if you know what you are doing."
while true; do
read -p "Do you wish to continue? (yes/no): " yn
case $yn in
yes | Yes ) break;;
no | No ) exit;;
* ) echo "Please type yes or no.";;
esac
done
modprobe i2c_dev
if [ "$BUS" = "NOT_SET" ]; then
if [ -e "/dev/i2c-0" ]; then
BUS=0
elif [ -e "/dev/i2c-10" ]; then
BUS=10
else
dtoverlay i2c-gpio i2c_gpio_sda=0 i2c_gpio_scl=1 bus=10
rc=$?
if [ $rc != 0 ]; then
echo "Loading of i2c-gpio dtoverlay failed. Do an rpi-update (and maybe apt-get update; apt-get upgrade)."
exit $rc
fi
if [ -e "/dev/i2c-10" ]; then
BUS=10
else
echo "Expected I2C bus (i2c-10) not found."
fi
fi
fi
if [ "$ADDR" = "NOT_SET" ]; then
ADDR=50
fi
modprobe at24
rc=$?
if [ $rc != 0 ]; then
echo "Modprobe of at24 failed. Do an rpi-update."
exit $rc
fi
SYS=/sys/class/i2c-adapter/i2c-$BUS
if [ ! -d "$SYS/$BUS-00$ADDR" ]; then
echo "$TYPE 0x$ADDR" > $SYS/new_device
fi
DD_VERSION=$(dd --version | grep coreutils | sed -e 's/\.//' | cut -d' ' -f 3)
if [ $DD_VERSION -ge 824 ]
then
DD_STATUS="progress"
else
DD_STATUS="none"
fi
if [ "$MODE" = "write" ]
then
echo "Writing..."
dd if=$FILE of=$SYS/$BUS-00$ADDR/eeprom status=$DD_STATUS
rc=$?
elif [ "$MODE" = "read" ]
then
echo "Reading..."
dd if=$SYS/$BUS-00$ADDR/eeprom of=$FILE status=$DD_STATUS
rc=$?
fi
echo "Closing EEPROM Device."
echo "0x$ADDR" > $SYS/delete_device
if [ $rc != 0 ]; then
echo "Error doing I/O operation."
exit $rc
else
echo "Done."
fi

Binary file not shown.

View file

@ -0,0 +1,620 @@
/*
* Parses EEPROM text file and createds binary .eep file
* Usage: eepmake input_file output_file
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include "eeptypes.h"
#define HEADER_SIGN 0x69502d52 //"R-Pi" in ASCII reversed for endianness
//todo: larger initial mallocs
struct header_t header;
struct atom_t *custom_atom, vinf_atom, gpio_atom, dt_atom;
struct vendor_info_d* vinf;
struct gpio_map_d* gpiomap;
bool product_serial_set, product_id_set, product_ver_set, vendor_set, product_set,
gpio_drive_set, gpio_slew_set, gpio_hysteresis_set, gpio_power_set;
bool data_receive, has_dt, receive_dt;
char **data;
char *current_atom; //rearranged to write out
unsigned int data_len, custom_ct, total_size, data_cap, custom_cap;
int write_binary(char* out) {
FILE *fp;
int i, offset;
short crc;
fp=fopen(out, "wb");
if (!fp) {
printf("Error writing file %s\n", out);
return -1;
}
fwrite(&header, sizeof(header), 1, fp);
current_atom = (char *) malloc(vinf_atom.dlen+ATOM_SIZE-CRC_SIZE);
offset = 0;
//vendor information atom first part
memcpy(current_atom, &vinf_atom, ATOM_SIZE-CRC_SIZE);
offset += ATOM_SIZE-2;
//data first part
memcpy(current_atom+offset, vinf_atom.data, VENDOR_SIZE);
offset += VENDOR_SIZE;
//data strings
memcpy(current_atom+offset, vinf->vstr, vinf->vslen);
offset += vinf->vslen;
memcpy(current_atom+offset, vinf->pstr, vinf->pslen);
offset += vinf->pslen;
//vinf last part
crc = getcrc(current_atom, offset);
memcpy(current_atom+offset, &crc, CRC_SIZE);
offset += CRC_SIZE;
fwrite(current_atom, offset, 1, fp);
free(current_atom);
current_atom = (char *) malloc(gpio_atom.dlen+ATOM_SIZE-CRC_SIZE);
offset = 0;
//GPIO map first part
memcpy(current_atom, &gpio_atom, ATOM_SIZE-CRC_SIZE);
offset += ATOM_SIZE-CRC_SIZE;
//GPIO data
memcpy(current_atom+offset, gpiomap, GPIO_SIZE);
offset += GPIO_SIZE;
//GPIO map last part
crc = getcrc(current_atom, offset);
memcpy(current_atom+offset, &crc, CRC_SIZE);
offset += CRC_SIZE;
fwrite(current_atom, offset, 1, fp);
free(current_atom);
if (has_dt) {
printf("Writing out DT...\n");
current_atom = (char *) malloc(dt_atom.dlen+ATOM_SIZE-CRC_SIZE);
offset = 0;
memcpy(current_atom, &dt_atom, ATOM_SIZE-CRC_SIZE);
offset += ATOM_SIZE-CRC_SIZE;
memcpy(current_atom+offset, dt_atom.data, dt_atom.dlen-CRC_SIZE);
offset += dt_atom.dlen-CRC_SIZE;
crc = getcrc(current_atom, offset);
memcpy(current_atom+offset, &crc, CRC_SIZE);
offset += CRC_SIZE;
fwrite(current_atom, offset, 1, fp);
free(current_atom);
}
for (i = 0; i<custom_ct; i++) {
custom_atom[i].count-=!has_dt;
current_atom = (char *) malloc(custom_atom[i].dlen+ATOM_SIZE-CRC_SIZE);
offset = 0;
memcpy(current_atom, &custom_atom[i], ATOM_SIZE-CRC_SIZE);
offset += ATOM_SIZE-CRC_SIZE;
memcpy(current_atom+offset, custom_atom[i].data, custom_atom[i].dlen-CRC_SIZE);
offset += custom_atom[i].dlen-CRC_SIZE;
crc = getcrc(current_atom, offset);
memcpy(current_atom+offset, &crc, CRC_SIZE);
offset += CRC_SIZE;
fwrite(current_atom, offset, 1, fp);
free(current_atom);
}
fflush(fp);
fclose(fp);
return 0;
}
void parse_data(char* c) {
int k;
char s;
char* i = c;
char* j = c;
while(*j != '\0')
{
*i = *j++;
if(isxdigit(*i))
i++;
}
*i = '\0';
int len = strlen(c);
if (len % 2 != 0) {
printf("Error: data must have an even number of hex digits\n");
} else {
for (k = 0; k<len/2; k++) {
//read a byte at a time
s = *(c+2);
*(c+2)='\0';
if (data_len==data_cap) {
data_cap *=2;
*data = (char *) realloc(*data, data_cap);
}
sscanf(c, "%2x", *data+data_len++);
*(c+2) = s;
c+=2;
}
}
}
void finish_data() {
if (data_receive) {
*data = (char *) realloc(*data, data_len);
total_size+=ATOM_SIZE+data_len;
if (receive_dt) {
dt_atom.type = ATOM_DT_TYPE;
dt_atom.count = ATOM_DT_NUM;
dt_atom.dlen = data_len+CRC_SIZE;
} else {
//finish atom description
custom_atom[custom_ct].type = ATOM_CUSTOM_TYPE;
custom_atom[custom_ct].count = 3+custom_ct;
custom_atom[custom_ct].dlen = data_len+CRC_SIZE;
custom_ct++;
}
}
}
void parse_command(char* cmd, char* c) {
int val;
uint32_t high1, high2;
char *fn, *pull;
char pin;
bool valid;
bool continue_data=false;
/* Vendor info related part */
if (strcmp(cmd, "product_uuid")==0) {
product_serial_set = true; //required field
high1 = 0; high2 = 0;
sscanf(c, "%100s %08x-%04x-%04x-%04x-%04x%08x\n", cmd, &vinf->serial_4,
&high1, &vinf->serial_3, &high2, &vinf->serial_2, &vinf->serial_1);
vinf->serial_3 |= high1<<16;
vinf->serial_2 |= high2<<16;
if ((vinf->serial_4==0) && (vinf->serial_3==0) && (vinf->serial_2==0) && (vinf->serial_1==0)) {
//read 128 random bits from /dev/urandom
int random_file = open("/dev/urandom", O_RDONLY);
ssize_t result = read(random_file, &vinf->serial_1, 16);
close(random_file);
if (result <= 0) printf("Unable to read from /dev/urandom to set up UUID");
else {
//put in the version
vinf->serial_3 = (vinf->serial_3 & 0xffff0fff) | 0x00004000;
//put in the variant
vinf->serial_2 = (vinf->serial_2 & 0x3fffffff) | 0x80000000;
printf("UUID=%08x-%04x-%04x-%04x-%04x%08x\n", vinf->serial_4, vinf->serial_3>>16, vinf->serial_3 & 0xffff, vinf->serial_2>>16, vinf->serial_2 & 0xffff, vinf->serial_1);
}
}
} else if (strcmp(cmd, "product_id")==0) {
product_id_set = true; //required field
sscanf(c, "%100s %hx", cmd, &vinf->pid);
} else if (strcmp(cmd, "product_ver")==0) {
product_ver_set = true; //required field
sscanf(c, "%100s %hx", cmd, &vinf->pver);
} else if (strcmp(cmd, "vendor")==0) {
vendor_set = true; //required field
vinf->vstr = (char*) malloc (256);
sscanf(c, "%100s \"%255[^\"]\"", cmd, vinf->vstr);
total_size-=vinf->vslen;
vinf_atom.dlen-=vinf->vslen;
vinf->vslen = strlen(vinf->vstr);
total_size+=vinf->vslen;
vinf_atom.dlen+=vinf->vslen;
} else if (strcmp(cmd, "product")==0) {
product_set = true; //required field
vinf->pstr = (char*) malloc (256);
sscanf(c, "%100s \"%255[^\"]\"", cmd, vinf->pstr);
total_size-=vinf->pslen;
vinf_atom.dlen-=vinf->pslen;
vinf->pslen = strlen(vinf->pstr);
total_size+=vinf->pslen;
vinf_atom.dlen+=vinf->pslen;
}
/* GPIO map related part */
else if (strcmp(cmd, "gpio_drive")==0) {
gpio_drive_set = true; //required field
sscanf(c, "%100s %1x", cmd, &val);
if (val>8 || val<0) printf("Warning: gpio_drive property in invalid region, using default value instead\n");
else gpiomap->flags |= val;
} else if (strcmp(cmd, "gpio_slew")==0) {
gpio_slew_set = true; //required field
sscanf(c, "%100s %1x", cmd, &val);
if (val>2 || val<0) printf("Warning: gpio_slew property in invalid region, using default value instead\n");
else gpiomap->flags |= val<<4;
} else if (strcmp(cmd, "gpio_hysteresis")==0) {
gpio_hysteresis_set = true; //required field
sscanf(c, "%100s %1x", cmd, &val);
if (val>2 || val<0) printf("Warning: gpio_hysteresis property in invalid region, using default value instead\n");
else gpiomap->flags |= val<<6;
} else if (strcmp(cmd, "back_power")==0) {
gpio_power_set = true; //required field
sscanf(c, "%100s %1x", cmd, &val);
if (val>2 || val<0) printf("Warning: back_power property in invalid region, using default value instead\n");
else gpiomap->power = val;
} else if (strcmp(cmd, "setgpio")==0) {
fn = (char*) malloc (101);
pull = (char*) malloc (101);
sscanf(c, "%100s %d %100s %100s", cmd, &val, fn, pull);
if (val<GPIO_MIN || val>=GPIO_COUNT) printf("Error: GPIO number out of bounds\n");
else {
valid = true;
pin = 0;
if (strcmp(fn, "INPUT")==0) {
//no action
} else if (strcmp(fn, "OUTPUT")==0) {
pin |= 1;
} else if (strcmp(fn, "ALT0")==0) {
pin |= 4;
} else if (strcmp(fn, "ALT1")==0) {
pin |= 5;
} else if (strcmp(fn, "ALT2")==0) {
pin |= 6;
} else if (strcmp(fn, "ALT3")==0) {
pin |= 7;
} else if (strcmp(fn, "ALT4")==0) {
pin |= 3;
} else if (strcmp(fn, "ALT5")==0) {
pin |= 2;
} else {
printf("Error at setgpio: function type not recognised\n");
valid=false;
}
if (strcmp(pull, "DEFAULT")==0) {
//no action
} else if (strcmp(pull, "UP")==0) {
pin |= 1<<5;
} else if (strcmp(pull, "DOWN")==0) {
pin |= 2<<5;
} else if (strcmp(pull, "NONE")==0) {
pin |= 3<<5;
} else {
printf("Error at setgpio: pull type not recognised\n");
valid=false;
}
pin |= 1<<7; //board uses this pin
if (valid) gpiomap->pins[val] = pin;
}
}
/* DT atom related part */
else if (strcmp(cmd, "dt_blob")==0) {
finish_data();
has_dt = true;
c+=strlen("dt_blob");
receive_dt=true;
data_receive=true;
data_len = 0;
data_cap = 4;
data = &dt_atom.data;
*data = (char *) malloc(data_cap);
parse_data(c);
continue_data = true;
}
/* Custom data related part */
else if (strcmp(cmd, "custom_data")==0) {
finish_data();
c+=strlen("custom_data");
if (custom_cap == custom_ct) {
custom_cap *= 2;
custom_atom = (struct atom_t*) realloc(custom_atom, custom_cap * sizeof(struct atom_t));
}
receive_dt=false;
data_receive=true;
data_len = 0;
data_cap = 4;
data = &custom_atom[custom_ct].data;
*data = (char *) malloc(data_cap);
parse_data(c);
continue_data = true;
} else if (strcmp(cmd, "end") ==0) {
//close last data atom
continue_data=false;
}
/* Incoming data */
else if (data_receive) {
parse_data(c);
continue_data = true;
}
if (!continue_data) finish_data();
}
int read_text(char* in) {
FILE * fp;
char * line = NULL;
char * c = NULL;
size_t len = 0;
ssize_t read;
char *comment = NULL;
int atomct = 2;
int linect = 0;
char * command = (char*) malloc (101);
int i;
has_dt = false;
printf("Opening file %s for read\n", in);
fp = fopen(in, "r");
if (fp == NULL) {
printf("Error opening input file\n");
return -1;
}
//allocating memory and setting up required atoms
custom_cap = 1;
custom_atom = (struct atom_t*) malloc(sizeof(struct atom_t) * custom_cap);
total_size=ATOM_SIZE*2+HEADER_SIZE+VENDOR_SIZE+GPIO_SIZE;
vinf_atom.type = ATOM_VENDOR_TYPE;
vinf_atom.count = ATOM_VENDOR_NUM;
vinf = (struct vendor_info_d *) calloc(1, sizeof(struct vendor_info_d));
vinf_atom.data = (char *)vinf;
vinf_atom.dlen = VENDOR_SIZE + CRC_SIZE;
gpio_atom.type = ATOM_GPIO_TYPE;
gpio_atom.count = ATOM_GPIO_NUM;
gpiomap = (struct gpio_map_d *) calloc(1, sizeof(struct gpio_map_d));
gpio_atom.data = (char *)gpiomap;
gpio_atom.dlen = GPIO_SIZE + CRC_SIZE;
while ((read = getline(&line, &len, fp)) != -1) {
linect++;
c = line;
for (i=0; i<read; i++) if (c[i]=='#') c[i]='\0';
while (isspace(*c)) ++c;
if (*c=='\0' || *c=='\n' || *c=='\r') {
//empty line, do nothing
} else if (isalnum (*c)) {
sscanf(c, "%100s", command);
#ifdef DEBUG
printf("Processing line %u: %s", linect, c);
if ((*(c+strlen(c)-1))!='\n') printf("\n");
#endif
parse_command(command, c);
} else printf("Can't parse line %u: %s", linect, c);
}
finish_data();
if (!product_serial_set || !product_id_set || !product_ver_set || !vendor_set || !product_set ||
!gpio_drive_set || !gpio_slew_set || !gpio_hysteresis_set || !gpio_power_set) {
printf("Warning: required fields missing in vendor information or GPIO map, using default values\n");
}
printf("Done reading\n");
return 0;
}
int read_dt(char* in) {
FILE * fp;
unsigned long size = 0;
printf("Opening DT file %s for read\n", in);
fp = fopen(in, "r");
if (fp == NULL) {
printf("Error opening input file\n");
return -1;
}
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
printf("Adding %lu bytes of DT data\n", size);
total_size+=ATOM_SIZE+size;
has_dt = true;
dt_atom.type = ATOM_DT_TYPE;
dt_atom.count = ATOM_DT_NUM;
dt_atom.dlen = size+CRC_SIZE;
dt_atom.data = (char *) malloc(size);
if (!fread(dt_atom.data, size, 1, fp)) goto err;
fclose(fp);
return 0;
err:
printf("Unexpected EOF or error occurred\n");
fclose(fp);
return 0;
}
int read_custom(char* in) {
FILE * fp;
unsigned long size = 0;
printf("Opening custom data file %s for read\n", in);
fp = fopen(in, "r");
if (fp == NULL) {
printf("Error opening input file\n");
return -1;
}
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
printf("Adding %lu bytes of custom data\n", size);
total_size+=ATOM_SIZE+size;
custom_atom[custom_ct].type = ATOM_CUSTOM_TYPE;
custom_atom[custom_ct].count = 3+custom_ct;
custom_atom[custom_ct].dlen = size+CRC_SIZE;
custom_atom[custom_ct].data = (char *) malloc(size);
if (!fread(custom_atom[custom_ct].data, size, 1, fp)) goto err;
custom_ct++;
fclose(fp);
return 0;
err:
printf("Unexpected EOF or error occurred\n");
fclose(fp);
return 0;
}
int main(int argc, char *argv[]) {
int ret;
int i, custom_o=0;
if (argc<3) {
printf("Wrong input format.\n");
printf("Try 'eepmake input_file output_file [dt_file] [-c custom_file_1 ... custom_file_n]'\n");
return 0;
}
ret = read_text(argv[1]);
if (ret) {
printf("Error reading and parsing input, aborting\n");
return 0;
}
if (argc>3) {
if (strcmp(argv[3], "-c")==0) {
custom_o=4;
} else {
//DT file specified
if (dt_atom.dlen) total_size-=(ATOM_SIZE +dt_atom.dlen - CRC_SIZE);
ret = read_dt(argv[3]);
if (ret) {
printf("Error reading DT file, aborting\n");
return 0;
}
}
}
if (argc>4 && strcmp(argv[4], "-c")==0) custom_o = 5;
if (custom_o)
for (i = custom_o; i<argc; i++) {
//new custom data file
ret = read_custom(argv[i]);
if (ret) {
printf("Error reading DT file, aborting\n");
return 0;
}
}
header.signature = HEADER_SIGN;
header.ver = FORMAT_VERSION;
header.res = 0;
header.numatoms = 2+has_dt+custom_ct;
header.eeplen = total_size;
printf("Writing out...\n");
ret = write_binary(argv[2]);
if (ret) {
printf("Error writing output\n");
return 0;
}
printf("Done.\n");
return 0;
}

View file

@ -0,0 +1,90 @@
########################################################################
# EEPROM settings text file
#
# Edit this file for your particular board and run through eepmake tool,
# then use eepflash tool to write to attached HAT ID EEPROM
#
# Tools available:
# eepmake Parses EEPROM text file and creates binary .eep file
# eepdump Dumps a binary .eep file as human readable text (for debug)
# eepflash Write or read .eep binary image to/from HAT EEPROM
#
########################################################################
########################################################################
# Vendor info
# 128 bit UUID. If left at zero eepmake tool will auto-generate
# RFC 4122 compliant UUID
product_uuid 00000000-0000-0000-0000-000000000000
# 16 bit product id
product_id 0x0000
# 16 bit product version
product_ver 0x0000
# ASCII vendor string (max 255 characters)
vendor "ACME Technology Company"
# ASCII product string (max 255 characters)
product "Special Sensor Board"
########################################################################
# GPIO bank settings, set to nonzero to change from the default.
# NOTE these setting can only be set per BANK, uncommenting any of
# these will force the bank to use the custom setting.
# drive strength, 0=default, 1-8=2,4,6,8,10,12,14,16mA, 9-15=reserved
gpio_drive 0
# 0=default, 1=slew rate limiting, 2=no slew limiting, 3=reserved
gpio_slew 0
# 0=default, 1=hysteresis disabled, 2=hysteresis enabled, 3=reserved
gpio_hysteresis 0
# If board back-powers Pi via 5V GPIO header pins:
# 0 = board does not back-power
# 1 = board back-powers and can supply the Pi with a minimum of 1.3A
# 2 = board back-powers and can supply the Pi with a minimum of 2A
# 3 = reserved
# If back_power=2 then USB high current mode will be automatically
# enabled on the Pi
back_power 0
########################################################################
# GPIO pins, uncomment for GPIOs used on board
# Options for FUNCTION: INPUT, OUTPUT, ALT0-ALT5
# Options for PULL: DEFAULT, UP, DOWN, NONE
# NB GPIO0 and GPIO1 are reserved for ID EEPROM so cannot be set
# GPIO FUNCTION PULL
# ---- -------- ----
#setgpio 2 INPUT DEFAULT
#setgpio 3 INPUT DEFAULT
#setgpio 4 INPUT DEFAULT
#setgpio 5 INPUT DEFAULT
#setgpio 6 INPUT DEFAULT
#setgpio 7 INPUT DEFAULT
#setgpio 8 INPUT DEFAULT
#setgpio 9 INPUT DEFAULT
#setgpio 10 INPUT DEFAULT
#setgpio 11 INPUT DEFAULT
#setgpio 12 INPUT DEFAULT
#setgpio 13 INPUT DEFAULT
#setgpio 14 INPUT DEFAULT
#setgpio 15 INPUT DEFAULT
#setgpio 16 INPUT DEFAULT
#setgpio 17 INPUT DEFAULT
#setgpio 18 INPUT DEFAULT
#setgpio 19 INPUT DEFAULT
#setgpio 20 INPUT DEFAULT
#setgpio 21 INPUT DEFAULT
#setgpio 22 INPUT DEFAULT
#setgpio 23 INPUT DEFAULT
#setgpio 24 INPUT DEFAULT
#setgpio 25 INPUT DEFAULT
#setgpio 26 INPUT DEFAULT
#setgpio 27 INPUT DEFAULT

View file

@ -0,0 +1,119 @@
#include <stdint.h>
/* Atom types */
#define ATOM_INVALID_TYPE 0x0000
#define ATOM_VENDOR_TYPE 0x0001
#define ATOM_GPIO_TYPE 0x0002
#define ATOM_DT_TYPE 0x0003
#define ATOM_CUSTOM_TYPE 0x0004
#define ATOM_HINVALID_TYPE 0xffff
#define ATOM_VENDOR_NUM 0x0000
#define ATOM_GPIO_NUM 0x0001
#define ATOM_DT_NUM 0x0002
//minimal sizes of data structures
#define HEADER_SIZE 12
#define ATOM_SIZE 10
#define VENDOR_SIZE 22
#define GPIO_SIZE 30
#define CRC_SIZE 2
#define GPIO_MIN 2
#define GPIO_COUNT 28
#define FORMAT_VERSION 0x01
#define CRC16 0x8005
/* EEPROM header structure */
struct header_t {
uint32_t signature;
unsigned char ver;
unsigned char res;
uint16_t numatoms;
uint32_t eeplen;
};
/* Atom structure */
struct atom_t {
uint16_t type;
uint16_t count;
uint32_t dlen;
char* data;
uint16_t crc16;
};
/* Vendor info atom data */
struct vendor_info_d {
uint32_t serial_1; //least significant
uint32_t serial_2;
uint32_t serial_3;
uint32_t serial_4; //most significant
uint16_t pid;
uint16_t pver;
unsigned char vslen;
unsigned char pslen;
char* vstr;
char* pstr;
};
/* GPIO map atom data */
struct gpio_map_d {
unsigned char flags;
unsigned char power;
unsigned char pins[GPIO_COUNT];
};
uint16_t getcrc(char* data, unsigned int size) {
uint16_t out = 0;
int bits_read = 0, bit_flag;
/* Sanity check: */
if((data == NULL) || size==0)
return 0;
while(size > 0)
{
bit_flag = out >> 15;
/* Get next bit: */
out <<= 1;
out |= (*data >> bits_read) & 1; // item a) work from the least significant bits
/* Increment bit counter: */
bits_read++;
if(bits_read > 7)
{
bits_read = 0;
data++;
size--;
}
/* Cycle check: */
if(bit_flag)
out ^= CRC16;
}
// item b) "push out" the last 16 bits
int i;
for (i = 0; i < 16; ++i) {
bit_flag = out >> 15;
out <<= 1;
if(bit_flag)
out ^= CRC16;
}
// item c) reverse the bits
uint16_t crc = 0;
i = 0x8000;
int j = 0x0001;
for (; i != 0; i >>=1, j <<= 1) {
if (i & out) crc |= j;
}
return crc;
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

BIN
rpi/eeprom/eepdump Normal file

Binary file not shown.

230
rpi/eeprom/eepdump.c Normal file
View file

@ -0,0 +1,230 @@
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include "eeptypes.h"
struct header_t header;
struct atom_t atom;
struct vendor_info_d vinf;
struct gpio_map_d gpiomap;
unsigned char* data;
int read_bin(char *in, char *outf) {
uint16_t crc;
FILE *fp, *out;
int i,j;
fp=fopen(in, "r");
if (!fp) {
printf("Error reading file %s\n", in);
return -1;
}
out=fopen(outf, "w");
if (!out) {
printf("Error writing file %s\n", outf);
return -1;
}
if (!fread(&header, sizeof(header), 1, fp)) goto err;
fprintf(out, "# ---------- Dump generated by eepdump handling format version 0x%02x ----------\n#\n", FORMAT_VERSION);
if (FORMAT_VERSION!=header.ver) fprintf(out, "# WARNING: format version mismatch!!!\n");
fprintf(out, "# --Header--\n# signature=0x%08x\n# version=0x%02x\n# reserved=%u\n# numatoms=%u\n# eeplen=%u\n# ----------\n\n\n", header.signature, header.ver, header.res, header.numatoms, header.eeplen);
for (i = 0; i<header.numatoms; i++) {
if (!fread(&atom, ATOM_SIZE-CRC_SIZE, 1, fp)) goto err;
printf("Reading atom %d...\n", i);
fprintf(out, "# Start of atom #%u of type 0x%04x and length %u\n", atom.count, atom.type, atom.dlen);
if (atom.count != i) {
printf("Error: atom count mismatch\n");
fprintf(out, "# Error: atom count mismatch\n");
}
long pos = ftell(fp);
char *atom_data = (char *) malloc(atom.dlen + ATOM_SIZE-CRC_SIZE);
memcpy(atom_data, &atom, ATOM_SIZE-CRC_SIZE);
if (!fread(atom_data+ATOM_SIZE-CRC_SIZE, atom.dlen, 1, fp)) goto err;
uint16_t calc_crc = getcrc(atom_data, atom.dlen-CRC_SIZE+ATOM_SIZE-CRC_SIZE);
fseek(fp, pos, SEEK_SET);
if (atom.type==ATOM_VENDOR_TYPE) {
//decode vendor info
if (!fread(&vinf, VENDOR_SIZE, 1, fp)) goto err;
fprintf(out, "# Vendor info\n");
fprintf(out, "product_uuid %08x-%04x-%04x-%04x-%04x%08x\n", vinf.serial_4, vinf.serial_3>>16, vinf.serial_3 & 0xffff, vinf.serial_2>>16, vinf.serial_2 & 0xffff, vinf.serial_1);
fprintf(out, "product_id 0x%04x\n", vinf.pid);
fprintf(out, "product_ver 0x%04x\n", vinf.pver);
vinf.vstr = (char *) malloc(vinf.vslen+1);
vinf.pstr = (char *) malloc(vinf.pslen+1);
if (!fread(vinf.vstr, vinf.vslen, 1, fp)) goto err;
if (!fread(vinf.pstr, vinf.pslen, 1, fp)) goto err;
//close strings
vinf.vstr[vinf.vslen] = 0;
vinf.pstr[vinf.pslen] = 0;
fprintf(out, "vendor \"%s\" # length=%u\n", vinf.vstr, vinf.vslen);
fprintf(out, "product \"%s\" # length=%u\n", vinf.pstr, vinf.pslen);
if (!fread(&crc, CRC_SIZE, 1, fp)) goto err;
} else if (atom.type==ATOM_GPIO_TYPE) {
//decode GPIO map
if (!fread(&gpiomap, GPIO_SIZE, 1, fp)) goto err;
fprintf(out, "# GPIO map info\n");
fprintf(out, "gpio_drive %d\n", gpiomap.flags & 15); //1111
fprintf(out, "gpio_slew %d\n", (gpiomap.flags & 48)>>4); //110000
fprintf(out, "gpio_hysteresis %d\n", (gpiomap.flags & 192)>>6); //11000000
fprintf(out, "back_power %d\n", gpiomap.power);
fprintf(out, "# GPIO FUNCTION PULL\n# ---- -------- ----\n");
for (j = 0; j<28; j++) {
if (gpiomap.pins[j] & (1<<7)) {
//board uses this pin
char *pull_str = "INVALID";
switch ((gpiomap.pins[j] & 96)>>5) { //1100000
case 0: pull_str = "DEFAULT";
break;
case 1: pull_str = "UP";
break;
case 2: pull_str = "DOWN";
break;
case 3: pull_str = "NONE";
break;
}
char *func_str = "INVALID";
switch ((gpiomap.pins[j] & 7)) { //111
case 0: func_str = "INPUT";
break;
case 1: func_str = "OUTPUT";
break;
case 4: func_str = "ALT0";
break;
case 5: func_str = "ALT1";
break;
case 6: func_str = "ALT2";
break;
case 7: func_str = "ALT3";
break;
case 3: func_str = "ALT4";
break;
case 2: func_str = "ALT5";
break;
}
fprintf(out, "setgpio %d %s %s\n", j, func_str, pull_str);
}
}
if (!fread(&crc, CRC_SIZE, 1, fp)) goto err;
} else if (atom.type==ATOM_DT_TYPE) {
//decode DT blob
fprintf(out, "dt_blob");
data = (char *) malloc(atom.dlen-CRC_SIZE);
if (!fread(data, atom.dlen-CRC_SIZE, 1, fp)) goto err;
for (j = 0; j<atom.dlen-CRC_SIZE; j++) {
if (j % 16 == 0) fprintf(out, "\n");
fprintf(out, "%02X ", *(data+j));
}
fprintf(out, "\n");
if (!fread(&crc, CRC_SIZE, 1, fp)) goto err;
} else if (atom.type==ATOM_CUSTOM_TYPE) {
//decode custom data
fprintf(out, "custom_data");
data = (char *) malloc(atom.dlen-CRC_SIZE);
if (!fread(data, atom.dlen-CRC_SIZE, 1, fp)) goto err;
for (j = 0; j<atom.dlen-CRC_SIZE; j++) {
if (j % 16 == 0) fprintf(out, "\n");
fprintf(out, "%02X ", *(data+j));
}
fprintf(out, "\n");
if (!fread(&crc, CRC_SIZE, 1, fp)) goto err;
} else {
printf("Error: unrecognised atom type\n");
fprintf(out, "# Error: unrecognised atom type\n");
goto err;
}
fprintf(out, "# End of atom. CRC16=0x%04x\n", crc);
if (calc_crc != crc) {
printf("Error: atom CRC16 mismatch\n");
fprintf(out, "# Error: atom CRC16 mismatch. Calculated CRC16=0x%02x", crc);
} else printf("CRC OK\n");
fprintf(out, "\n\n");
}
//Total length checks. We need header.eeplen=current_position=file_length.
long pos = ftell(fp);
fseek(fp, 0L, SEEK_END);
if (pos!=ftell(fp)) printf("Warning: Dump finished before EOF\n");
if (pos!=header.eeplen) printf("Warning: Dump finished before length specified in header\n");
if (ftell(fp)!=header.eeplen) printf("Warning: EOF does not match length specified in header\n");
printf("Done.\n");
fclose(fp);
fclose(out);
return 0;
err:
printf("Unexpected EOF or error occurred\n");
fclose(fp);
fclose(out);
return 0;
}
int main(int argc, char *argv[]) {
int ret;
int i;
if (argc<3) {
printf("Wrong input format.\n");
printf("Try 'eepdump input_file output_file'\n");
return 0;
}
ret = read_bin(argv[1], argv[2]);
if (ret) {
printf("Error reading input, aborting\n");
return 0;
}
return 0;
}

171
rpi/eeprom/eepflash.sh Normal file
View file

@ -0,0 +1,171 @@
#!/bin/sh
MODE="NOT_SET"
FILE="NOT_SET"
TYPE="NOT_SET"
BUS="NOT_SET"
ADDR="NOT_SET"
usage()
{
echo "eepflash: Writes or reads .eep binary image to/from HAT EEPROM on a Raspberry Pi"
echo ""
echo "./eepflash.sh"
echo " -h --help: display this help message"
echo " -r --read: read .eep from the eeprom"
echo " -w --write: write .eep to the eeprom"
echo " -f=file_name --file=file_name: binary .eep file to read to/from"
echo " -d= --device= i2c bus number (ex if the eeprom is on i2c-0 set -d=0)"
echo " -a= --address= i2c eeprom address"
echo " -t=eeprom_type --type=eeprom_type: eeprom type to use"
echo " We support the following eeprom types:"
echo " -24c32"
echo " -24c64"
echo " -24c128"
echo " -24c256"
echo " -24c512"
echo " -24c1024"
echo ""
echo "Example:"
echo "./eepflash -w -f=crex0.1.eep -t=24c32 -d=1 -a=57"
echo "./eepflash -r -f=dump.eep -t=24c32 -d=1 -a=57"
echo ""
}
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
while [ "$1" != "" ]; do
PARAM=`echo $1 | awk -F= '{print $1}'`
VALUE=`echo $1 | awk -F= '{print $2}'`
case $PARAM in
-h | --help)
usage
exit
;;
-r | --read)
MODE="read"
;;
-w | --write)
MODE="write"
;;
-t | --type)
if [ "$VALUE" = "24c32" ] || [ "$VALUE" = "24c64" ] || [ "$VALUE" = "24c128" ] ||
[ "$VALUE" = "24c256" ] || [ "$VALUE" = "24c512" ] || [ "$VALUE" = "24c1024" ]; then
TYPE=$VALUE
else
echo "ERROR: Unrecognised eeprom type. Try -h for help"
exit 1
fi
;;
-d | --device)
BUS=$VALUE
;;
-a | --address)
ADDR=$VALUE
;;
-f | --file)
FILE=$VALUE
;;
*)
echo "ERROR: unknown parameter \"$PARAM\""
usage
exit 1
;;
esac
shift
done
if [ "$MODE" = "NOT_SET" ]; then
echo "You need to set mode (read or write). Try -h for help."
exit 1
elif [ "$FILE" = "NOT_SET" ]; then
echo "You need to set binary .eep file to read to/from. Try -h for help."
exit 1
elif [ "$TYPE" = "NOT_SET" ]; then
echo "You need to set eeprom type. Try -h for help."
exit 1
fi
echo "This will attempt to talk to an eeprom at i2c address 0x$ADDR on bus $BUS. Make sure there is an eeprom at this address."
echo "This script comes with ABSOLUTELY no warranty. Continue only if you know what you are doing."
while true; do
read -p "Do you wish to continue? (yes/no): " yn
case $yn in
yes | Yes ) break;;
no | No ) exit;;
* ) echo "Please type yes or no.";;
esac
done
modprobe i2c_dev
if [ "$BUS" = "NOT_SET" ]; then
if [ -e "/dev/i2c-0" ]; then
BUS=0
elif [ -e "/dev/i2c-10" ]; then
BUS=10
else
dtoverlay i2c-gpio i2c_gpio_sda=0 i2c_gpio_scl=1 bus=10
rc=$?
if [ $rc != 0 ]; then
echo "Loading of i2c-gpio dtoverlay failed. Do an rpi-update (and maybe apt-get update; apt-get upgrade)."
exit $rc
fi
if [ -e "/dev/i2c-10" ]; then
BUS=10
else
echo "Expected I2C bus (i2c-10) not found."
fi
fi
fi
if [ "$ADDR" = "NOT_SET" ]; then
ADDR=50
fi
modprobe at24
rc=$?
if [ $rc != 0 ]; then
echo "Modprobe of at24 failed. Do an rpi-update."
exit $rc
fi
SYS=/sys/class/i2c-adapter/i2c-$BUS
if [ ! -d "$SYS/$BUS-00$ADDR" ]; then
echo "$TYPE 0x$ADDR" > $SYS/new_device
fi
DD_VERSION=$(dd --version | grep coreutils | sed -e 's/\.//' | cut -d' ' -f 3)
if [ $DD_VERSION -ge 824 ]
then
DD_STATUS="progress"
else
DD_STATUS="none"
fi
if [ "$MODE" = "write" ]
then
echo "Writing..."
dd if=$FILE of=$SYS/$BUS-00$ADDR/eeprom status=$DD_STATUS
rc=$?
elif [ "$MODE" = "read" ]
then
echo "Reading..."
dd if=$SYS/$BUS-00$ADDR/eeprom of=$FILE status=$DD_STATUS
rc=$?
fi
echo "Closing EEPROM Device."
echo "0x$ADDR" > $SYS/delete_device
if [ $rc != 0 ]; then
echo "Error doing I/O operation."
exit $rc
else
echo "Done."
fi

BIN
rpi/eeprom/eepmake Normal file

Binary file not shown.

620
rpi/eeprom/eepmake.c Normal file
View file

@ -0,0 +1,620 @@
/*
* Parses EEPROM text file and createds binary .eep file
* Usage: eepmake input_file output_file
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include "eeptypes.h"
#define HEADER_SIGN 0x69502d52 //"R-Pi" in ASCII reversed for endianness
//todo: larger initial mallocs
struct header_t header;
struct atom_t *custom_atom, vinf_atom, gpio_atom, dt_atom;
struct vendor_info_d* vinf;
struct gpio_map_d* gpiomap;
bool product_serial_set, product_id_set, product_ver_set, vendor_set, product_set,
gpio_drive_set, gpio_slew_set, gpio_hysteresis_set, gpio_power_set;
bool data_receive, has_dt, receive_dt;
char **data;
char *current_atom; //rearranged to write out
unsigned int data_len, custom_ct, total_size, data_cap, custom_cap;
int write_binary(char* out) {
FILE *fp;
int i, offset;
short crc;
fp=fopen(out, "wb");
if (!fp) {
printf("Error writing file %s\n", out);
return -1;
}
fwrite(&header, sizeof(header), 1, fp);
current_atom = (char *) malloc(vinf_atom.dlen+ATOM_SIZE-CRC_SIZE);
offset = 0;
//vendor information atom first part
memcpy(current_atom, &vinf_atom, ATOM_SIZE-CRC_SIZE);
offset += ATOM_SIZE-2;
//data first part
memcpy(current_atom+offset, vinf_atom.data, VENDOR_SIZE);
offset += VENDOR_SIZE;
//data strings
memcpy(current_atom+offset, vinf->vstr, vinf->vslen);
offset += vinf->vslen;
memcpy(current_atom+offset, vinf->pstr, vinf->pslen);
offset += vinf->pslen;
//vinf last part
crc = getcrc(current_atom, offset);
memcpy(current_atom+offset, &crc, CRC_SIZE);
offset += CRC_SIZE;
fwrite(current_atom, offset, 1, fp);
free(current_atom);
current_atom = (char *) malloc(gpio_atom.dlen+ATOM_SIZE-CRC_SIZE);
offset = 0;
//GPIO map first part
memcpy(current_atom, &gpio_atom, ATOM_SIZE-CRC_SIZE);
offset += ATOM_SIZE-CRC_SIZE;
//GPIO data
memcpy(current_atom+offset, gpiomap, GPIO_SIZE);
offset += GPIO_SIZE;
//GPIO map last part
crc = getcrc(current_atom, offset);
memcpy(current_atom+offset, &crc, CRC_SIZE);
offset += CRC_SIZE;
fwrite(current_atom, offset, 1, fp);
free(current_atom);
if (has_dt) {
printf("Writing out DT...\n");
current_atom = (char *) malloc(dt_atom.dlen+ATOM_SIZE-CRC_SIZE);
offset = 0;
memcpy(current_atom, &dt_atom, ATOM_SIZE-CRC_SIZE);
offset += ATOM_SIZE-CRC_SIZE;
memcpy(current_atom+offset, dt_atom.data, dt_atom.dlen-CRC_SIZE);
offset += dt_atom.dlen-CRC_SIZE;
crc = getcrc(current_atom, offset);
memcpy(current_atom+offset, &crc, CRC_SIZE);
offset += CRC_SIZE;
fwrite(current_atom, offset, 1, fp);
free(current_atom);
}
for (i = 0; i<custom_ct; i++) {
custom_atom[i].count-=!has_dt;
current_atom = (char *) malloc(custom_atom[i].dlen+ATOM_SIZE-CRC_SIZE);
offset = 0;
memcpy(current_atom, &custom_atom[i], ATOM_SIZE-CRC_SIZE);
offset += ATOM_SIZE-CRC_SIZE;
memcpy(current_atom+offset, custom_atom[i].data, custom_atom[i].dlen-CRC_SIZE);
offset += custom_atom[i].dlen-CRC_SIZE;
crc = getcrc(current_atom, offset);
memcpy(current_atom+offset, &crc, CRC_SIZE);
offset += CRC_SIZE;
fwrite(current_atom, offset, 1, fp);
free(current_atom);
}
fflush(fp);
fclose(fp);
return 0;
}
void parse_data(char* c) {
int k;
char s;
char* i = c;
char* j = c;
while(*j != '\0')
{
*i = *j++;
if(isxdigit(*i))
i++;
}
*i = '\0';
int len = strlen(c);
if (len % 2 != 0) {
printf("Error: data must have an even number of hex digits\n");
} else {
for (k = 0; k<len/2; k++) {
//read a byte at a time
s = *(c+2);
*(c+2)='\0';
if (data_len==data_cap) {
data_cap *=2;
*data = (char *) realloc(*data, data_cap);
}
sscanf(c, "%2x", *data+data_len++);
*(c+2) = s;
c+=2;
}
}
}
void finish_data() {
if (data_receive) {
*data = (char *) realloc(*data, data_len);
total_size+=ATOM_SIZE+data_len;
if (receive_dt) {
dt_atom.type = ATOM_DT_TYPE;
dt_atom.count = ATOM_DT_NUM;
dt_atom.dlen = data_len+CRC_SIZE;
} else {
//finish atom description
custom_atom[custom_ct].type = ATOM_CUSTOM_TYPE;
custom_atom[custom_ct].count = 3+custom_ct;
custom_atom[custom_ct].dlen = data_len+CRC_SIZE;
custom_ct++;
}
}
}
void parse_command(char* cmd, char* c) {
int val;
uint32_t high1, high2;
char *fn, *pull;
char pin;
bool valid;
bool continue_data=false;
/* Vendor info related part */
if (strcmp(cmd, "product_uuid")==0) {
product_serial_set = true; //required field
high1 = 0; high2 = 0;
sscanf(c, "%100s %08x-%04x-%04x-%04x-%04x%08x\n", cmd, &vinf->serial_4,
&high1, &vinf->serial_3, &high2, &vinf->serial_2, &vinf->serial_1);
vinf->serial_3 |= high1<<16;
vinf->serial_2 |= high2<<16;
if ((vinf->serial_4==0) && (vinf->serial_3==0) && (vinf->serial_2==0) && (vinf->serial_1==0)) {
//read 128 random bits from /dev/urandom
int random_file = open("/dev/urandom", O_RDONLY);
ssize_t result = read(random_file, &vinf->serial_1, 16);
close(random_file);
if (result <= 0) printf("Unable to read from /dev/urandom to set up UUID");
else {
//put in the version
vinf->serial_3 = (vinf->serial_3 & 0xffff0fff) | 0x00004000;
//put in the variant
vinf->serial_2 = (vinf->serial_2 & 0x3fffffff) | 0x80000000;
printf("UUID=%08x-%04x-%04x-%04x-%04x%08x\n", vinf->serial_4, vinf->serial_3>>16, vinf->serial_3 & 0xffff, vinf->serial_2>>16, vinf->serial_2 & 0xffff, vinf->serial_1);
}
}
} else if (strcmp(cmd, "product_id")==0) {
product_id_set = true; //required field
sscanf(c, "%100s %hx", cmd, &vinf->pid);
} else if (strcmp(cmd, "product_ver")==0) {
product_ver_set = true; //required field
sscanf(c, "%100s %hx", cmd, &vinf->pver);
} else if (strcmp(cmd, "vendor")==0) {
vendor_set = true; //required field
vinf->vstr = (char*) malloc (256);
sscanf(c, "%100s \"%255[^\"]\"", cmd, vinf->vstr);
total_size-=vinf->vslen;
vinf_atom.dlen-=vinf->vslen;
vinf->vslen = strlen(vinf->vstr);
total_size+=vinf->vslen;
vinf_atom.dlen+=vinf->vslen;
} else if (strcmp(cmd, "product")==0) {
product_set = true; //required field
vinf->pstr = (char*) malloc (256);
sscanf(c, "%100s \"%255[^\"]\"", cmd, vinf->pstr);
total_size-=vinf->pslen;
vinf_atom.dlen-=vinf->pslen;
vinf->pslen = strlen(vinf->pstr);
total_size+=vinf->pslen;
vinf_atom.dlen+=vinf->pslen;
}
/* GPIO map related part */
else if (strcmp(cmd, "gpio_drive")==0) {
gpio_drive_set = true; //required field
sscanf(c, "%100s %1x", cmd, &val);
if (val>8 || val<0) printf("Warning: gpio_drive property in invalid region, using default value instead\n");
else gpiomap->flags |= val;
} else if (strcmp(cmd, "gpio_slew")==0) {
gpio_slew_set = true; //required field
sscanf(c, "%100s %1x", cmd, &val);
if (val>2 || val<0) printf("Warning: gpio_slew property in invalid region, using default value instead\n");
else gpiomap->flags |= val<<4;
} else if (strcmp(cmd, "gpio_hysteresis")==0) {
gpio_hysteresis_set = true; //required field
sscanf(c, "%100s %1x", cmd, &val);
if (val>2 || val<0) printf("Warning: gpio_hysteresis property in invalid region, using default value instead\n");
else gpiomap->flags |= val<<6;
} else if (strcmp(cmd, "back_power")==0) {
gpio_power_set = true; //required field
sscanf(c, "%100s %1x", cmd, &val);
if (val>2 || val<0) printf("Warning: back_power property in invalid region, using default value instead\n");
else gpiomap->power = val;
} else if (strcmp(cmd, "setgpio")==0) {
fn = (char*) malloc (101);
pull = (char*) malloc (101);
sscanf(c, "%100s %d %100s %100s", cmd, &val, fn, pull);
if (val<GPIO_MIN || val>=GPIO_COUNT) printf("Error: GPIO number out of bounds\n");
else {
valid = true;
pin = 0;
if (strcmp(fn, "INPUT")==0) {
//no action
} else if (strcmp(fn, "OUTPUT")==0) {
pin |= 1;
} else if (strcmp(fn, "ALT0")==0) {
pin |= 4;
} else if (strcmp(fn, "ALT1")==0) {
pin |= 5;
} else if (strcmp(fn, "ALT2")==0) {
pin |= 6;
} else if (strcmp(fn, "ALT3")==0) {
pin |= 7;
} else if (strcmp(fn, "ALT4")==0) {
pin |= 3;
} else if (strcmp(fn, "ALT5")==0) {
pin |= 2;
} else {
printf("Error at setgpio: function type not recognised\n");
valid=false;
}
if (strcmp(pull, "DEFAULT")==0) {
//no action
} else if (strcmp(pull, "UP")==0) {
pin |= 1<<5;
} else if (strcmp(pull, "DOWN")==0) {
pin |= 2<<5;
} else if (strcmp(pull, "NONE")==0) {
pin |= 3<<5;
} else {
printf("Error at setgpio: pull type not recognised\n");
valid=false;
}
pin |= 1<<7; //board uses this pin
if (valid) gpiomap->pins[val] = pin;
}
}
/* DT atom related part */
else if (strcmp(cmd, "dt_blob")==0) {
finish_data();
has_dt = true;
c+=strlen("dt_blob");
receive_dt=true;
data_receive=true;
data_len = 0;
data_cap = 4;
data = &dt_atom.data;
*data = (char *) malloc(data_cap);
parse_data(c);
continue_data = true;
}
/* Custom data related part */
else if (strcmp(cmd, "custom_data")==0) {
finish_data();
c+=strlen("custom_data");
if (custom_cap == custom_ct) {
custom_cap *= 2;
custom_atom = (struct atom_t*) realloc(custom_atom, custom_cap * sizeof(struct atom_t));
}
receive_dt=false;
data_receive=true;
data_len = 0;
data_cap = 4;
data = &custom_atom[custom_ct].data;
*data = (char *) malloc(data_cap);
parse_data(c);
continue_data = true;
} else if (strcmp(cmd, "end") ==0) {
//close last data atom
continue_data=false;
}
/* Incoming data */
else if (data_receive) {
parse_data(c);
continue_data = true;
}
if (!continue_data) finish_data();
}
int read_text(char* in) {
FILE * fp;
char * line = NULL;
char * c = NULL;
size_t len = 0;
ssize_t read;
char *comment = NULL;
int atomct = 2;
int linect = 0;
char * command = (char*) malloc (101);
int i;
has_dt = false;
printf("Opening file %s for read\n", in);
fp = fopen(in, "r");
if (fp == NULL) {
printf("Error opening input file\n");
return -1;
}
//allocating memory and setting up required atoms
custom_cap = 1;
custom_atom = (struct atom_t*) malloc(sizeof(struct atom_t) * custom_cap);
total_size=ATOM_SIZE*2+HEADER_SIZE+VENDOR_SIZE+GPIO_SIZE;
vinf_atom.type = ATOM_VENDOR_TYPE;
vinf_atom.count = ATOM_VENDOR_NUM;
vinf = (struct vendor_info_d *) calloc(1, sizeof(struct vendor_info_d));
vinf_atom.data = (char *)vinf;
vinf_atom.dlen = VENDOR_SIZE + CRC_SIZE;
gpio_atom.type = ATOM_GPIO_TYPE;
gpio_atom.count = ATOM_GPIO_NUM;
gpiomap = (struct gpio_map_d *) calloc(1, sizeof(struct gpio_map_d));
gpio_atom.data = (char *)gpiomap;
gpio_atom.dlen = GPIO_SIZE + CRC_SIZE;
while ((read = getline(&line, &len, fp)) != -1) {
linect++;
c = line;
for (i=0; i<read; i++) if (c[i]=='#') c[i]='\0';
while (isspace(*c)) ++c;
if (*c=='\0' || *c=='\n' || *c=='\r') {
//empty line, do nothing
} else if (isalnum (*c)) {
sscanf(c, "%100s", command);
#ifdef DEBUG
printf("Processing line %u: %s", linect, c);
if ((*(c+strlen(c)-1))!='\n') printf("\n");
#endif
parse_command(command, c);
} else printf("Can't parse line %u: %s", linect, c);
}
finish_data();
if (!product_serial_set || !product_id_set || !product_ver_set || !vendor_set || !product_set ||
!gpio_drive_set || !gpio_slew_set || !gpio_hysteresis_set || !gpio_power_set) {
printf("Warning: required fields missing in vendor information or GPIO map, using default values\n");
}
printf("Done reading\n");
return 0;
}
int read_dt(char* in) {
FILE * fp;
unsigned long size = 0;
printf("Opening DT file %s for read\n", in);
fp = fopen(in, "r");
if (fp == NULL) {
printf("Error opening input file\n");
return -1;
}
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
printf("Adding %lu bytes of DT data\n", size);
total_size+=ATOM_SIZE+size;
has_dt = true;
dt_atom.type = ATOM_DT_TYPE;
dt_atom.count = ATOM_DT_NUM;
dt_atom.dlen = size+CRC_SIZE;
dt_atom.data = (char *) malloc(size);
if (!fread(dt_atom.data, size, 1, fp)) goto err;
fclose(fp);
return 0;
err:
printf("Unexpected EOF or error occurred\n");
fclose(fp);
return 0;
}
int read_custom(char* in) {
FILE * fp;
unsigned long size = 0;
printf("Opening custom data file %s for read\n", in);
fp = fopen(in, "r");
if (fp == NULL) {
printf("Error opening input file\n");
return -1;
}
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
printf("Adding %lu bytes of custom data\n", size);
total_size+=ATOM_SIZE+size;
custom_atom[custom_ct].type = ATOM_CUSTOM_TYPE;
custom_atom[custom_ct].count = 3+custom_ct;
custom_atom[custom_ct].dlen = size+CRC_SIZE;
custom_atom[custom_ct].data = (char *) malloc(size);
if (!fread(custom_atom[custom_ct].data, size, 1, fp)) goto err;
custom_ct++;
fclose(fp);
return 0;
err:
printf("Unexpected EOF or error occurred\n");
fclose(fp);
return 0;
}
int main(int argc, char *argv[]) {
int ret;
int i, custom_o=0;
if (argc<3) {
printf("Wrong input format.\n");
printf("Try 'eepmake input_file output_file [dt_file] [-c custom_file_1 ... custom_file_n]'\n");
return 0;
}
ret = read_text(argv[1]);
if (ret) {
printf("Error reading and parsing input, aborting\n");
return 0;
}
if (argc>3) {
if (strcmp(argv[3], "-c")==0) {
custom_o=4;
} else {
//DT file specified
if (dt_atom.dlen) total_size-=(ATOM_SIZE +dt_atom.dlen - CRC_SIZE);
ret = read_dt(argv[3]);
if (ret) {
printf("Error reading DT file, aborting\n");
return 0;
}
}
}
if (argc>4 && strcmp(argv[4], "-c")==0) custom_o = 5;
if (custom_o)
for (i = custom_o; i<argc; i++) {
//new custom data file
ret = read_custom(argv[i]);
if (ret) {
printf("Error reading DT file, aborting\n");
return 0;
}
}
header.signature = HEADER_SIGN;
header.ver = FORMAT_VERSION;
header.res = 0;
header.numatoms = 2+has_dt+custom_ct;
header.eeplen = total_size;
printf("Writing out...\n");
ret = write_binary(argv[2]);
if (ret) {
printf("Error writing output\n");
return 0;
}
printf("Done.\n");
return 0;
}

119
rpi/eeprom/eeptypes.h Normal file
View file

@ -0,0 +1,119 @@
#include <stdint.h>
/* Atom types */
#define ATOM_INVALID_TYPE 0x0000
#define ATOM_VENDOR_TYPE 0x0001
#define ATOM_GPIO_TYPE 0x0002
#define ATOM_DT_TYPE 0x0003
#define ATOM_CUSTOM_TYPE 0x0004
#define ATOM_HINVALID_TYPE 0xffff
#define ATOM_VENDOR_NUM 0x0000
#define ATOM_GPIO_NUM 0x0001
#define ATOM_DT_NUM 0x0002
//minimal sizes of data structures
#define HEADER_SIZE 12
#define ATOM_SIZE 10
#define VENDOR_SIZE 22
#define GPIO_SIZE 30
#define CRC_SIZE 2
#define GPIO_MIN 2
#define GPIO_COUNT 28
#define FORMAT_VERSION 0x01
#define CRC16 0x8005
/* EEPROM header structure */
struct header_t {
uint32_t signature;
unsigned char ver;
unsigned char res;
uint16_t numatoms;
uint32_t eeplen;
};
/* Atom structure */
struct atom_t {
uint16_t type;
uint16_t count;
uint32_t dlen;
char* data;
uint16_t crc16;
};
/* Vendor info atom data */
struct vendor_info_d {
uint32_t serial_1; //least significant
uint32_t serial_2;
uint32_t serial_3;
uint32_t serial_4; //most significant
uint16_t pid;
uint16_t pver;
unsigned char vslen;
unsigned char pslen;
char* vstr;
char* pstr;
};
/* GPIO map atom data */
struct gpio_map_d {
unsigned char flags;
unsigned char power;
unsigned char pins[GPIO_COUNT];
};
uint16_t getcrc(char* data, unsigned int size) {
uint16_t out = 0;
int bits_read = 0, bit_flag;
/* Sanity check: */
if((data == NULL) || size==0)
return 0;
while(size > 0)
{
bit_flag = out >> 15;
/* Get next bit: */
out <<= 1;
out |= (*data >> bits_read) & 1; // item a) work from the least significant bits
/* Increment bit counter: */
bits_read++;
if(bits_read > 7)
{
bits_read = 0;
data++;
size--;
}
/* Cycle check: */
if(bit_flag)
out ^= CRC16;
}
// item b) "push out" the last 16 bits
int i;
for (i = 0; i < 16; ++i) {
bit_flag = out >> 15;
out <<= 1;
if(bit_flag)
out ^= CRC16;
}
// item c) reverse the bits
uint16_t crc = 0;
i = 0x8000;
int j = 0x0001;
for (; i != 0; i >>=1, j <<= 1) {
if (i & out) crc |= j;
}
return crc;
}

36
rpi/eeprom/out Normal file
View file

@ -0,0 +1,36 @@
# ---------- Dump generated by eepdump handling format version 0x01 ----------
#
# --Header--
# signature=0x69502d52
# version=0x01
# reserved=0
# numatoms=2
# eeplen=147
# ----------
# Start of atom #0 of type 0x0001 and length 87
# Vendor info
product_uuid ba6b7b14-243d-4e8e-bb7c-88843db25e8e
product_id 0x0001
product_ver 0x0002
vendor "Alberta Digital Radio Communications Society" # length=44
product "IP400 E04 HAT Rev A" # length=19
# End of atom. CRC16=0x8639
# Start of atom #1 of type 0x0002 and length 32
# GPIO map info
gpio_drive 0
gpio_slew 0
gpio_hysteresis 0
back_power 1
# GPIO FUNCTION PULL
# ---- -------- ----
setgpio 10 OUTPUT NONE
setgpio 11 OUTPUT NONE
setgpio 14 ALT0 DEFAULT
setgpio 15 ALT0 DEFAULT
# End of atom. CRC16=0xc226