mirror of
https://github.com/adrcs/ip400.git
synced 2025-04-12 07:13:44 +03:00
Delete rpi directory
This commit is contained in:
parent
6433c3ee60
commit
a9f25770da
|
@ -1 +0,0 @@
|
|||
Coming soon: firmware for the RPI HAT with an E04 module.
|
|
@ -1,18 +0,0 @@
|
|||
# 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
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
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.
|
@ -1,67 +0,0 @@
|
|||
########################################################################
|
||||
# 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
|
|
@ -1,27 +0,0 @@
|
|||
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.
|
|
@ -1,81 +0,0 @@
|
|||
# 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.
Before Width: | Height: | Size: 6.4 KiB |
|
@ -1,88 +0,0 @@
|
|||
# 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.**
|
|
@ -1,92 +0,0 @@
|
|||
# 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.
|
|
@ -1,63 +0,0 @@
|
|||
#!/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.
Before Width: | Height: | Size: 9.2 KiB |
|
@ -1,102 +0,0 @@
|
|||
# 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).
|
|
@ -1,14 +0,0 @@
|
|||
# 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.
|
@ -1,87 +0,0 @@
|
|||
########################################################################
|
||||
# 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
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
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.
|
@ -1,230 +0,0 @@
|
|||
#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;
|
||||
}
|
|
@ -1,171 +0,0 @@
|
|||
#!/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.
|
@ -1,620 +0,0 @@
|
|||
/*
|
||||
* 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;
|
||||
}
|
|
@ -1,90 +0,0 @@
|
|||
########################################################################
|
||||
# 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
|
|
@ -1,119 +0,0 @@
|
|||
#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.
Before Width: | Height: | Size: 20 KiB |
Binary file not shown.
|
@ -1,230 +0,0 @@
|
|||
#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;
|
||||
}
|
|
@ -1,171 +0,0 @@
|
|||
#!/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.
|
@ -1,620 +0,0 @@
|
|||
/*
|
||||
* 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;
|
||||
}
|
|
@ -1,119 +0,0 @@
|
|||
#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;
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
# ---------- 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
|
||||
|
||||
|
|
@ -1,41 +0,0 @@
|
|||
Authors ordered by first contribution.
|
||||
|
||||
Geoffrey McRae
|
||||
Bret Olmsted
|
||||
Tormod Volden
|
||||
Jakob Malm
|
||||
Reuben Dowle
|
||||
Matthias Kubisch
|
||||
Paul Fertser
|
||||
Daniel Strnad
|
||||
Jérémie Rapin
|
||||
Christian Pointner
|
||||
Mats Erik Andersson
|
||||
Alexey Borovik
|
||||
Antonio Borneo
|
||||
Armin van der Togt
|
||||
Brian Silverman
|
||||
Georg Hofmann
|
||||
Luis Rodrigues
|
||||
Jeff Epler
|
||||
Alexander O. Anisimov
|
||||
Seth LaForge
|
||||
Johan Hellman
|
||||
Matthias Weisser
|
||||
Tilman Sauerbeck
|
||||
Mateusz Spychała
|
||||
Ernst Schwab
|
||||
Amir Hammad
|
||||
Luc Hondareyte
|
||||
Roman Savrulin
|
||||
Mickael Gardet
|
||||
Uwe Bonnes
|
||||
Patrick José Pereira
|
||||
Dominik Schmidt
|
||||
Emil Melar
|
||||
Paolo Bassi
|
||||
Guillaume Revaillot
|
||||
Luca Amati
|
||||
Yann Sionneau
|
||||
Andy Little
|
||||
Renaud Fivet
|
|
@ -1,20 +0,0 @@
|
|||
TOP_LOCAL_PATH := $(call my-dir)
|
||||
|
||||
include $(call all-named-subdir-makefiles, parsers)
|
||||
|
||||
LOCAL_PATH := $(TOP_LOCAL_PATH)
|
||||
|
||||
include $(CLEAR_VARS)
|
||||
LOCAL_MODULE := stm32flash
|
||||
LOCAL_SRC_FILES := \
|
||||
dev_table.c \
|
||||
i2c.c \
|
||||
init.c \
|
||||
main.c \
|
||||
port.c \
|
||||
serial_common.c \
|
||||
serial_platform.c \
|
||||
stm32.c \
|
||||
utils.c
|
||||
LOCAL_STATIC_LIBRARIES := libparsers
|
||||
include $(BUILD_EXECUTABLE)
|
|
@ -1,35 +0,0 @@
|
|||
0.7 (2022-03-26)
|
||||
|
||||
- Fix I2C page-by-page erase (Yann Sionneau)
|
||||
- Fix hex file base address not used, extraneous base padding (Renaud Fivet)
|
||||
- Fix truncated read from stdin (Renaud Fivet)
|
||||
- Fix input buffer flush on Windows (Jan Belohoubek)
|
||||
- More baud rates available, up to 4000000
|
||||
- Allow skipping serial port setup with -b 0
|
||||
- Support STM32L412xx/422xx (Andy Little)
|
||||
- Support STM32G05xxx/061xx (Luca Amati), STM32G0B0/B1/C1xx, STM32G491xx/A1xx
|
||||
|
||||
|
||||
0.6 (2021-02-12)
|
||||
|
||||
stm32flash 0.6 is a maintenance release with these highlights:
|
||||
- Many more devices supported G0/G4/F7/H7/L4 (various contributors)
|
||||
- GPIO sequencing reworked (Roman Savrulin)
|
||||
- Serial port handling improved (Mickael Gardet)
|
||||
- Many code improvements (Antonio Borneo)
|
||||
- Only erase needed pages if flashing from file (Tormod Volden)
|
||||
|
||||
|
||||
0.5 (2016-02-11)
|
||||
|
||||
- support for multiple bank sizes
|
||||
- improved serial port support on Windows
|
||||
- improved erase functionality
|
||||
- improved hex parser
|
||||
- many new devices and device info corrections
|
||||
|
||||
|
||||
0.4 (2014-10-04)
|
||||
- support for programming over I2C (Antonio Borneo)
|
||||
- code refactored to make it easier to add other transports (Antonio Borneo)
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
Add new interfaces:
|
||||
=====================================================================
|
||||
stm32flash currently supports the following interfaces:
|
||||
- UART Windows (either "COMn" and "\\.\COMn");
|
||||
- UART posix/Linux (e.g. "/dev/ttyUSB0");
|
||||
- I2C Linux through standard driver "i2c-dev" (e.g. "/dev/i2c-n").
|
||||
|
||||
Starting from version 0.4, the back-end of stm32flash is modular and
|
||||
ready to be expanded to support new interfaces.
|
||||
I'm planning adding SPI on Linux through standard driver "spidev".
|
||||
You are invited to contribute with more interfaces.
|
||||
|
||||
To add a new interface you need to add a new file, populate the struct
|
||||
port_interface (check at the end of files i2c.c, serial_posix.c and
|
||||
serial_w32.c) and provide the relative functions to operate on the
|
||||
interface: open/close, read/write, get_cfg_str and the optional gpio.
|
||||
The include the new drive in Makefile and register the new struct
|
||||
port_interface in file port.c in struct port_interface *ports[].
|
||||
|
||||
There are several USB-I2C adapter in the market, each providing its
|
||||
own libraries to communicate with the I2C bus.
|
||||
Could be interesting to provide as back-end a bridge between stm32flash
|
||||
and such libraries (I have no plan on this item).
|
||||
|
||||
|
||||
Add new STM32 devices:
|
||||
=====================================================================
|
||||
Add a new line in file dev_table.c, in table devices[].
|
||||
The fields of the table are listed in stm32.h, struct stm32_dev.
|
||||
|
||||
|
||||
Cross compile on Linux host for Windows target with MinGW:
|
||||
=====================================================================
|
||||
I'm using a 64 bit Arch Linux machines, and I usually run:
|
||||
make CC=x86_64-w64-mingw32-gcc AR=x86_64-w64-mingw32-ar
|
||||
|
||||
2014 Antonio Borneo
|
|
@ -1,108 +0,0 @@
|
|||
About I2C back-end communication in stm32flash
|
||||
==========================================================================
|
||||
|
||||
Starting from version v0.4, beside the serial communication port,
|
||||
stm32flash adds support for I2C port to talk with STM32 bootloader.
|
||||
|
||||
The current I2C back-end supports only the API provided by Linux kernel
|
||||
driver "i2c-dev", so only I2C controllers with Linux kernel driver can be
|
||||
used.
|
||||
In Linux source code, most of the drivers for I2C and SMBUS controllers
|
||||
are in
|
||||
./drivers/i2c/busses/
|
||||
Only I2C is supported by STM32 bootloader, so check the section below
|
||||
about SMBUS.
|
||||
No I2C support for Windows is available in stm32flash v0.4.
|
||||
|
||||
Thanks to the new modular back-end, stm32flash can be easily extended to
|
||||
support new back-ends and API. Check HOWTO file in stm32flash source code
|
||||
for details.
|
||||
|
||||
In the market there are several USB-to-I2C dongles; most of them are not
|
||||
supported by kernel drivers. Manufacturer provide proprietary userspace
|
||||
libraries using not standardized API. Such API and dongles could in
|
||||
principle be supported in feature versions of stm32flash.
|
||||
|
||||
The open-hardware i2c-tiny-usb device and its reimplementation on the
|
||||
STM32, the i2c-star, are fully supported by the Linux kernel.
|
||||
- https://www.fischl.de/i2c-mp-usb/
|
||||
- https://github.com/daniel-thompson/i2c-star
|
||||
|
||||
There is also the I2CDriver which has open-source drivers and
|
||||
available design files. Although there is no kernel driver,
|
||||
there is a userspace compatibility interface available.
|
||||
- https://github.com/jamesbowman/i2cdriver
|
||||
- https://github.com/mbenkmann/i2cdriver/tree/master/c/linux
|
||||
|
||||
There are currently 3 versions of STM32 bootloader for I2C communications:
|
||||
- v1.0 using I2C clock stretching synchronization between host and STM32;
|
||||
- v1.1 superset of v1.0, adds non stretching commands;
|
||||
- v1.2 superset of v1.1, adds CRC command and compatibility with i2cdetect.
|
||||
Details in ST application note AN2606.
|
||||
All the bootloaders above are tested and working with stm32flash.
|
||||
|
||||
|
||||
SMBUS controllers
|
||||
==========================================================================
|
||||
|
||||
Almost 50% of the drivers in Linux source code folder
|
||||
./drivers/i2c/busses/
|
||||
are for controllers that "only" support SMBUS protocol. They can NOT
|
||||
operate with STM32 bootloader.
|
||||
To identify if your controller supports I2C, use command:
|
||||
i2cdetect -F n
|
||||
where "n" is the number of the I2C interface (e.g. n=3 for "/dev/i2c-3").
|
||||
Controllers that supports I2C will report
|
||||
I2C yes
|
||||
Controller that support both I2C and SMBUS are ok.
|
||||
|
||||
If you are interested on details about SMBUS protocol, you can download
|
||||
the current specs from
|
||||
http://smbus.org/specs/smbus20.pdf
|
||||
and you can read the files in Linux source code
|
||||
./Documentation/i2c/i2c-protocol
|
||||
./Documentation/i2c/smbus-protocol
|
||||
|
||||
|
||||
About bootloader v1.0
|
||||
==========================================================================
|
||||
|
||||
(Used on F0, F3 and F4 up to STM32F401xB(C) devices)
|
||||
|
||||
Version v1.0 can have issues with some I2C controllers due to use of clock
|
||||
stretching during commands that require long operations, like flash erase
|
||||
and programming.
|
||||
|
||||
Clock stretching is a technique to synchronize host and I2C device. When
|
||||
I2C device wants to force a delay in the communication, it push "low" the
|
||||
I2C clock; the I2C controller detects it and waits until I2C clock returns
|
||||
"high".
|
||||
Most I2C controllers set a "timeout" for clock stretching, ranging from
|
||||
few milli-seconds to seconds depending on specific HW or SW driver.
|
||||
|
||||
It is possible that the timeout in your I2C controller is smaller than the
|
||||
delay required for flash erase or programming. In this case the I2C
|
||||
controller will timeout and report error to stm32flash.
|
||||
There is no possibility for stm32flash to retry, so it can only signal the
|
||||
error and exit.
|
||||
|
||||
To by-pass the issue with bootloader v1.0 you can modify the kernel driver
|
||||
of your I2C controller. Not an easy job, since every controller has its own
|
||||
way to handle the timeout.
|
||||
|
||||
In my case I'm using the I2C controller integrated in the VGA port of my
|
||||
laptop HP EliteBook 8460p. I built the 0.25$ VGA-to-I2C adapter reported in
|
||||
https://web.archive.org/web/20160506154718/http://www.paintyourdragon.com/?p=43
|
||||
|
||||
To change the timeout of the I2C controller I had to modify the kernel file
|
||||
drivers/gpu/drm/radeon/radeon_i2c.c
|
||||
line 969
|
||||
- i2c->bit.timeout = usecs_to_jiffies(2200); /* from VESA */
|
||||
+ i2c->bit.timeout = msecs_to_jiffies(5000); /* 5s for STM32 */
|
||||
and recompile it.
|
||||
Then
|
||||
$> modprobe i2c-dev
|
||||
$> chmod 666 /dev/i2c-7
|
||||
#> stm32flash -a 0x39 /dev/i2c-7
|
||||
|
||||
2014-09-16 Antonio Borneo
|
|
@ -1,31 +0,0 @@
|
|||
|
||||
Building stm32flash
|
||||
|
||||
A set of static makefiles is provided that should work on most operating
|
||||
systems with a standard build environment, for instance GNU make and gcc.
|
||||
|
||||
1. Build executable
|
||||
|
||||
make
|
||||
|
||||
2. Install executable and manual page (optional)
|
||||
|
||||
make install
|
||||
|
||||
The install location base can be set with the PREFIX flag (default
|
||||
is /usr/local), e.g. make install PREFIX=/opt
|
||||
|
||||
|
||||
Using autotools
|
||||
|
||||
If using a git checkout, you must have autoconf installed and run:
|
||||
|
||||
autoreconf -i -v
|
||||
|
||||
to generate configure and various macro files. If using the release
|
||||
tarballs this step is not necessary.
|
||||
|
||||
Then run ./configure to generate makefiles. Use ./configure --help to
|
||||
list general options.
|
||||
|
||||
Finally run "make" and "make install" as described above.
|
|
@ -1,48 +0,0 @@
|
|||
PREFIX = /usr/local
|
||||
CFLAGS += -Wall -g
|
||||
|
||||
ifndef CC
|
||||
$(error CC is not defined)
|
||||
endif
|
||||
|
||||
ifndef AR
|
||||
$(error AR is not defined)
|
||||
endif
|
||||
|
||||
INSTALL = install
|
||||
|
||||
OBJS = dev_table.o \
|
||||
i2c.o \
|
||||
init.o \
|
||||
main.o \
|
||||
port.o \
|
||||
serial_common.o \
|
||||
serial_platform.o \
|
||||
stm32.o \
|
||||
utils.o
|
||||
|
||||
LIBOBJS = parsers/parsers.a
|
||||
|
||||
all: stm32flash
|
||||
|
||||
serial_platform.o: serial_posix.c serial_w32.c
|
||||
|
||||
parsers/parsers.a: force
|
||||
cd parsers && $(MAKE) parsers.a
|
||||
|
||||
stm32flash: $(OBJS) $(LIBOBJS)
|
||||
$(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBOBJS)
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS) stm32flash
|
||||
cd parsers && $(MAKE) $@
|
||||
|
||||
install: all
|
||||
$(INSTALL) -d $(DESTDIR)$(PREFIX)/bin
|
||||
$(INSTALL) -m 755 stm32flash $(DESTDIR)$(PREFIX)/bin
|
||||
$(INSTALL) -d $(DESTDIR)$(PREFIX)/share/man/man1
|
||||
$(INSTALL) -m 644 stm32flash.1 $(DESTDIR)$(PREFIX)/share/man/man1
|
||||
|
||||
force:
|
||||
|
||||
.PHONY: all clean install force
|
|
@ -1,59 +0,0 @@
|
|||
|
||||
SUBDIRS = parsers
|
||||
|
||||
bin_PROGRAMS = stm32flash
|
||||
|
||||
AUTOMAKE_OPTIONS = subdir-objects
|
||||
|
||||
|
||||
stm32flash_SOURCES = \
|
||||
compiler.h \
|
||||
dev_table.c \
|
||||
i2c.c \
|
||||
init.h \
|
||||
init.c \
|
||||
main.c \
|
||||
port.h \
|
||||
port.c \
|
||||
serial.h \
|
||||
serial_common.c \
|
||||
serial_platform.c\
|
||||
stm32.h \
|
||||
stm32.c \
|
||||
utils.h \
|
||||
utils.c
|
||||
|
||||
EXTRA_stm32flash_SOURCES = \
|
||||
serial_posix.c \
|
||||
serial_w32.c
|
||||
|
||||
EXTRA_DIST = Android.mk parsers/Android.mk \
|
||||
stm32flash.1 gpl-2.0.txt \
|
||||
protocol.txt HOWTO I2C.txt
|
||||
|
||||
# If we want to ship these handmade ones, they must have
|
||||
# been copied before running configure and make dist
|
||||
dist-hook:
|
||||
-cp Makefile.handmade $(distdir)/Makefile
|
||||
-cp parsers/Makefile.handmade $(distdir)/parsers/Makefile
|
||||
|
||||
stm32flash_LDADD = ${top_builddir}/parsers/parsers.la
|
||||
|
||||
stm32flash_CFLAGS = \
|
||||
-g3 \
|
||||
-Os \
|
||||
-Wall \
|
||||
-Wextra \
|
||||
-I$(srcdir)/parsers
|
||||
|
||||
|
||||
stm32flash_LDFLAGS = \
|
||||
-Wall \
|
||||
-g3 \
|
||||
-Wextra
|
||||
|
||||
all:
|
||||
|
||||
|
||||
.PHONY: all
|
||||
.SILENT: all
|
|
@ -1,7 +0,0 @@
|
|||
GPIO pin sequences:
|
||||
- Set up all pins in one first pass before starting changing their values
|
||||
to minimize timing delays due to exported device nodes getting ready
|
||||
|
||||
AUTHORS:
|
||||
- Add contributors from Geoffrey's commits
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
/*
|
||||
stm32flash - Open Source ST STM32 flash program for *nix
|
||||
Copyright (C) 2017 Antonio Borneo <borneo.antonio@gmail.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#ifndef _H_COMPILER
|
||||
#define _H_COMPILER
|
||||
|
||||
#if defined(__GNUC__)
|
||||
#undef __unused
|
||||
#undef __maybe_unused
|
||||
#define __unused __attribute__ ((unused))
|
||||
#define __maybe_unused __attribute__ ((unused))
|
||||
#endif
|
||||
|
||||
#ifndef __unused
|
||||
#define __unused
|
||||
#endif
|
||||
|
||||
#ifndef __maybe_unused
|
||||
#define __maybe_unused
|
||||
#endif
|
||||
|
||||
#endif /* _H_COMPILER */
|
|
@ -1,33 +0,0 @@
|
|||
# -*- Autoconf -*-
|
||||
# Process this file with autoconf to produce a configure script.
|
||||
|
||||
AC_PREREQ([2.68])
|
||||
AC_INIT([stm32flash], [0.7], [])
|
||||
|
||||
# Checks for programs.
|
||||
AM_INIT_AUTOMAKE([foreign -Wall])
|
||||
AC_PROG_CC
|
||||
AM_PROG_CC_C_O
|
||||
AM_PROG_AR
|
||||
AM_PROG_AS
|
||||
LT_INIT
|
||||
AC_CONFIG_MACRO_DIR([m4])
|
||||
|
||||
# Checks for programs.
|
||||
|
||||
# Checks for libraries.
|
||||
|
||||
# Checks for header files.
|
||||
|
||||
# Checks for typedefs, structures, and compiler characteristics.
|
||||
|
||||
# Checks for library functions.
|
||||
|
||||
|
||||
AC_CONFIG_FILES(
|
||||
[
|
||||
Makefile
|
||||
parsers/Makefile
|
||||
])
|
||||
|
||||
AC_OUTPUT
|
|
@ -1,156 +0,0 @@
|
|||
/*
|
||||
stm32flash - Open Source ST STM32 flash program for *nix
|
||||
Copyright (C) 2010 Geoffrey McRae <geoff@spacevs.com>
|
||||
Copyright (C) 2014-2015 Antonio Borneo <borneo.antonio@gmail.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include "stm32.h"
|
||||
|
||||
#define SZ_128 0x00000080
|
||||
#define SZ_256 0x00000100
|
||||
#define SZ_1K 0x00000400
|
||||
#define SZ_2K 0x00000800
|
||||
#define SZ_4K 0x00001000
|
||||
#define SZ_8K 0x00002000
|
||||
#define SZ_16K 0x00004000
|
||||
#define SZ_32K 0x00008000
|
||||
#define SZ_64K 0x00010000
|
||||
#define SZ_128K 0x00020000
|
||||
#define SZ_256K 0x00040000
|
||||
|
||||
/*
|
||||
* Page-size for page-by-page flash erase.
|
||||
* Arrays are zero terminated; last non-zero value is automatically repeated
|
||||
*/
|
||||
|
||||
/* fixed size pages */
|
||||
static uint32_t p_128[] = { SZ_128, 0 };
|
||||
static uint32_t p_256[] = { SZ_256, 0 };
|
||||
static uint32_t p_1k[] = { SZ_1K, 0 };
|
||||
static uint32_t p_2k[] = { SZ_2K, 0 };
|
||||
static uint32_t p_4k[] = { SZ_4K, 0 };
|
||||
static uint32_t p_8k[] = { SZ_8K, 0 };
|
||||
static uint32_t p_128k[] = { SZ_128K, 0 };
|
||||
/* F2 and F4 page size */
|
||||
static uint32_t f2f4[] = { SZ_16K, SZ_16K, SZ_16K, SZ_16K, SZ_64K, SZ_128K, 0 };
|
||||
/* F4 dual bank page size */
|
||||
static uint32_t f4db[] = {
|
||||
SZ_16K, SZ_16K, SZ_16K, SZ_16K, SZ_64K, SZ_128K, SZ_128K, SZ_128K,
|
||||
SZ_16K, SZ_16K, SZ_16K, SZ_16K, SZ_64K, SZ_128K, 0
|
||||
};
|
||||
/* F7 page size */
|
||||
static uint32_t f7[] = { SZ_32K, SZ_32K, SZ_32K, SZ_32K, SZ_128K, SZ_256K, 0 };
|
||||
|
||||
/*
|
||||
* Device table, corresponds to the "Bootloader device-dependent parameters"
|
||||
* table in ST document AN2606.
|
||||
* Note that the option bytes upper range is inclusive!
|
||||
*
|
||||
* When adding new devices, please double-check agaist the chip-specific
|
||||
* sections and reference manuals, where also flash address and option
|
||||
* byte ranges can be found. In the commit message, please reference the
|
||||
* source documents including their revision.
|
||||
*/
|
||||
const stm32_dev_t devices[] = {
|
||||
/* ID "name" SRAM-address-range FLASH-address-range PPS PSize Option-byte-addr-range System-mem-addr-range Flags */
|
||||
/* C0 */
|
||||
{0x443, "STM32C011xx" , 0x20001000, 0x20003000, 0x08000000, 0x08008000, 4, p_2k , 0x1FFF7800, 0x1FFF787F, 0x1FFF0000, 0x1FFF1800, 0},
|
||||
/* {0x453, "STM32C031xx" , 0x20001000, 0x20001800, 0x08000000, x , x, x , x , x , 0x1FFF0000, 0x1FFF1800, 0} */
|
||||
/* F0 */
|
||||
{0x440, "STM32F030x8/F05xxx" , 0x20000800, 0x20002000, 0x08000000, 0x08010000, 4, p_1k , 0x1FFFF800, 0x1FFFF80F, 0x1FFFEC00, 0x1FFFF800, 0},
|
||||
{0x444, "STM32F03xx4/6" , 0x20000800, 0x20001000, 0x08000000, 0x08008000, 4, p_1k , 0x1FFFF800, 0x1FFFF80F, 0x1FFFEC00, 0x1FFFF800, 0},
|
||||
{0x442, "STM32F030xC/F09xxx" , 0x20001800, 0x20008000, 0x08000000, 0x08040000, 2, p_2k , 0x1FFFF800, 0x1FFFF80F, 0x1FFFD800, 0x1FFFF800, F_OBLL},
|
||||
{0x445, "STM32F04xxx/F070x6" , 0x20001800, 0x20001800, 0x08000000, 0x08008000, 4, p_1k , 0x1FFFF800, 0x1FFFF80F, 0x1FFFC400, 0x1FFFF800, 0},
|
||||
{0x448, "STM32F070xB/F071xx/F72xx" , 0x20001800, 0x20004000, 0x08000000, 0x08020000, 2, p_2k , 0x1FFFF800, 0x1FFFF80F, 0x1FFFC800, 0x1FFFF800, 0},
|
||||
/* F1 */
|
||||
{0x412, "STM32F10xxx Low-density" , 0x20000200, 0x20002800, 0x08000000, 0x08008000, 4, p_1k , 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800, 0},
|
||||
{0x410, "STM32F10xxx Medium-density" , 0x20000200, 0x20005000, 0x08000000, 0x08020000, 4, p_1k , 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800, 0},
|
||||
{0x414, "STM32F10xxx High-density" , 0x20000200, 0x20010000, 0x08000000, 0x08080000, 2, p_2k , 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800, 0},
|
||||
{0x420, "STM32F10xxx Medium-density VL" , 0x20000200, 0x20002000, 0x08000000, 0x08020000, 4, p_1k , 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800, 0},
|
||||
{0x428, "STM32F10xxx High-density VL" , 0x20000200, 0x20008000, 0x08000000, 0x08080000, 2, p_2k , 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800, 0},
|
||||
{0x418, "STM32F105xx/F107xx" , 0x20001000, 0x20010000, 0x08000000, 0x08040000, 2, p_2k , 0x1FFFF800, 0x1FFFF80F, 0x1FFFB000, 0x1FFFF800, 0},
|
||||
{0x430, "STM32F10xxx XL-density" , 0x20000800, 0x20018000, 0x08000000, 0x08100000, 2, p_2k , 0x1FFFF800, 0x1FFFF80F, 0x1FFFE000, 0x1FFFF800, 0},
|
||||
/* F2 */
|
||||
{0x411, "STM32F2xxxx" , 0x20002000, 0x20020000, 0x08000000, 0x08100000, 1, f2f4 , 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF7800, 0},
|
||||
/* F3 */
|
||||
{0x432, "STM32F373xx/F378xx" , 0x20001400, 0x20008000, 0x08000000, 0x08040000, 2, p_2k , 0x1FFFF800, 0x1FFFF80F, 0x1FFFD800, 0x1FFFF800, 0},
|
||||
{0x422, "STM32F302xB(C)/F303xB(C)/F358xx" , 0x20001400, 0x2000A000, 0x08000000, 0x08040000, 2, p_2k , 0x1FFFF800, 0x1FFFF80F, 0x1FFFD800, 0x1FFFF800, 0},
|
||||
{0x439, "STM32F301xx/F302x4(6/8)/F318xx" , 0x20001800, 0x20004000, 0x08000000, 0x08010000, 2, p_2k , 0x1FFFF800, 0x1FFFF80F, 0x1FFFD800, 0x1FFFF800, 0},
|
||||
{0x438, "STM32F303x4(6/8)/F334xx/F328xx" , 0x20001800, 0x20003000, 0x08000000, 0x08010000, 2, p_2k , 0x1FFFF800, 0x1FFFF80F, 0x1FFFD800, 0x1FFFF800, 0},
|
||||
{0x446, "STM32F302xD(E)/F303xD(E)/F398xx" , 0x20001800, 0x20010000, 0x08000000, 0x08080000, 2, p_2k , 0x1FFFF800, 0x1FFFF80F, 0x1FFFD800, 0x1FFFF800, 0},
|
||||
/* F4 */
|
||||
{0x413, "STM32F40xxx/41xxx" , 0x20003000, 0x20020000, 0x08000000, 0x08100000, 1, f2f4 , 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF7800, 0},
|
||||
{0x419, "STM32F42xxx/43xxx" , 0x20003000, 0x20030000, 0x08000000, 0x08200000, 1, f4db , 0x1FFEC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF7800, 0},
|
||||
{0x423, "STM32F401xB(C)" , 0x20003000, 0x20010000, 0x08000000, 0x08040000, 1, f2f4 , 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF7800, 0},
|
||||
{0x433, "STM32F401xD(E)" , 0x20003000, 0x20018000, 0x08000000, 0x08080000, 1, f2f4 , 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF7800, 0},
|
||||
{0x458, "STM32F410xx" , 0x20003000, 0x20008000, 0x08000000, 0x08020000, 1, f2f4 , 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF7800, 0},
|
||||
{0x431, "STM32F411xx" , 0x20003000, 0x20020000, 0x08000000, 0x08080000, 1, f2f4 , 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF7800, 0},
|
||||
{0x441, "STM32F412xx" , 0x20003000, 0x20040000, 0x08000000, 0x08100000, 1, f2f4 , 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF7800, 0},
|
||||
{0x421, "STM32F446xx" , 0x20003000, 0x20020000, 0x08000000, 0x08080000, 1, f2f4 , 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF7800, 0},
|
||||
{0x434, "STM32F469xx/479xx" , 0x20003000, 0x20060000, 0x08000000, 0x08200000, 1, f4db , 0x1FFEC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF7800, 0},
|
||||
{0x463, "STM32F413xx/423xx" , 0x20003000, 0x20050000, 0x08000000, 0x08180000, 1, f2f4 , 0x1FFFC000, 0x1FFFC00F, 0x1FFF0000, 0x1FFF7800, 0},
|
||||
/* F7 */
|
||||
{0x452, "STM32F72xxx/73xxx" , 0x20004000, 0x20040000, 0x08000000, 0x08080000, 1, f2f4 , 0x1FFF0000, 0x1FFF001F, 0x1FF00000, 0x1FF0EDC0, 0},
|
||||
{0x449, "STM32F74xxx/75xxx" , 0x20004000, 0x20050000, 0x08000000, 0x08100000, 1, f7 , 0x1FFF0000, 0x1FFF001F, 0x1FF00000, 0x1FF0EDC0, 0},
|
||||
{0x451, "STM32F76xxx/77xxx" , 0x20004000, 0x20080000, 0x08000000, 0x08200000, 1, f7 , 0x1FFF0000, 0x1FFF001F, 0x1FF00000, 0x1FF0EDC0, 0},
|
||||
/* G0 */
|
||||
{0x466, "STM32G03xxx/04xxx" , 0x20001000, 0x20002000, 0x08000000, 0x08010000, 1, p_2k , 0x1FFF7800, 0x1FFF787F, 0x1FFF0000, 0x1FFF2000, 0},
|
||||
{0x460, "STM32G07xxx/08xxx" , 0x20002700, 0x20009000, 0x08000000, 0x08020000, 1, p_2k , 0x1FFF7800, 0x1FFF787F, 0x1FFF0000, 0x1FFF7000, 0},
|
||||
{0x467, "STM32G0B0/B1/C1xx" , 0x20004000, 0x20020000, 0x08000000, 0x08080000, 1, p_2k , 0x1FFF7800, 0x1FFF787F, 0x1FFF0000, 0x1FFF7000, 0},
|
||||
{0x456, "STM32G05xxx/061xx" , 0x20001000, 0x20004800, 0x08000000, 0x08010000, 1, p_2k , 0x1FFF7800, 0x1FFF787F, 0x1FFF0000, 0x1FFF2000, 0},
|
||||
/* G4 */
|
||||
{0x468, "STM32G431xx/441xx" , 0x20004000, 0x20005800, 0x08000000, 0x08020000, 1, p_2k , 0x1FFF7800, 0x1FFF782F, 0x1FFF0000, 0x1FFF7000, 0},
|
||||
{0x469, "STM32G47xxx/48xxx" , 0x20004000, 0x20018000, 0x08000000, 0x08080000, 1, p_2k , 0x1FFF7800, 0x1FFF782F, 0x1FFF0000, 0x1FFF7000, 0},
|
||||
{0x479, "STM32G491xx/A1xx" , 0x20004000, 0x2001C000, 0x08000000, 0x08080000, 1, p_2k , 0x1FFF7800, 0x1FFF782F, 0x1FFF0000, 0x1FFF7000, 0},
|
||||
/* H7 */
|
||||
{0x483, "STM32H72xxx/73xxx" , 0x20004100, 0x20020000, 0x08000000, 0x08100000, 1, p_128k, 0 , 0 , 0x1FF00000, 0x1FF1E800, 0},
|
||||
{0x450, "STM32H74xxx/75xxx" , 0x20004100, 0x20020000, 0x08000000, 0x08200000, 1, p_128k, 0 , 0 , 0x1FF00000, 0x1FF1E800, 0},
|
||||
{0x480, "STM32H7A3xx/B3xx" , 0x20004100, 0x20020000, 0x08000000, 0x08100000, 1, p_8k , 0 , 0 , 0x1FF00000, 0x1FF14000, 0},
|
||||
/* L0 */
|
||||
{0x457, "STM32L01xxx/02xxx" , 0x20000800, 0x20000800, 0x08000000, 0x08004000, 32, p_128 , 0x1FF80000, 0x1FF8001F, 0x1FF00000, 0x1FF01000, F_NO_ME},
|
||||
{0x425, "STM32L031xx/041xx" , 0x20001000, 0x20002000, 0x08000000, 0x08008000, 32, p_128 , 0x1FF80000, 0x1FF8001F, 0x1FF00000, 0x1FF01000, F_NO_ME},
|
||||
{0x417, "STM32L05xxx/06xxx" , 0x20001000, 0x20002000, 0x08000000, 0x08010000, 32, p_128 , 0x1FF80000, 0x1FF8001F, 0x1FF00000, 0x1FF01000, F_NO_ME},
|
||||
{0x447, "STM32L07xxx/08xxx" , 0x20002000, 0x20005000, 0x08000000, 0x08030000, 32, p_128 , 0x1FF80000, 0x1FF8001F, 0x1FF00000, 0x1FF02000, F_NO_ME},
|
||||
/* L1 */
|
||||
{0x416, "STM32L1xxx6(8/B)" , 0x20000800, 0x20004000, 0x08000000, 0x08020000, 16, p_256 , 0x1FF80000, 0x1FF8001F, 0x1FF00000, 0x1FF01000, F_NO_ME},
|
||||
{0x429, "STM32L1xxx6(8/B)A" , 0x20001000, 0x20008000, 0x08000000, 0x08020000, 16, p_256 , 0x1FF80000, 0x1FF8001F, 0x1FF00000, 0x1FF01000, F_NO_ME},
|
||||
{0x427, "STM32L1xxxC" , 0x20001000, 0x20008000, 0x08000000, 0x08040000, 16, p_256 , 0x1FF80000, 0x1FF8001F, 0x1FF00000, 0x1FF02000, F_NO_ME},
|
||||
{0x436, "STM32L1xxxD" , 0x20001000, 0x2000C000, 0x08000000, 0x08060000, 16, p_256 , 0x1FF80000, 0x1FF8001F, 0x1FF00000, 0x1FF02000, F_NO_ME},
|
||||
{0x437, "STM32L1xxxE" , 0x20001000, 0x20014000, 0x08000000, 0x08080000, 16, p_256 , 0x1FF80000, 0x1FF8001F, 0x1FF00000, 0x1FF02000, F_NO_ME},
|
||||
/* L4 */
|
||||
{0x464, "STM32L412xx/422xx" , 0x20003100, 0x20008000, 0x08000000, 0x08020000, 1, p_2k , 0x1FFF7800, 0x1FFF780F, 0x1FFF0000, 0x1FFF7000, 0},
|
||||
{0x435, "STM32L43xxx/44xxx" , 0x20003100, 0x2000C000, 0x08000000, 0x08040000, 1, p_2k , 0x1FFF7800, 0x1FFF780F, 0x1FFF0000, 0x1FFF7000, 0},
|
||||
{0x462, "STM32L45xxx/46xxx" , 0x20003100, 0x20020000, 0x08000000, 0x08080000, 1, p_2k , 0x1FFF7800, 0x1FFF780F, 0x1FFF0000, 0x1FFF7000, F_PEMPTY},
|
||||
{0x415, "STM32L47xxx/48xxx" , 0x20003100, 0x20018000, 0x08000000, 0x08100000, 1, p_2k , 0x1FFF7800, 0x1FFFF80F, 0x1FFF0000, 0x1FFF7000, 0},
|
||||
{0x461, "STM32L496xx/4A6xx" , 0x20003100, 0x20040000, 0x08000000, 0x08100000, 1, p_2k , 0x1FFF7800, 0x1FFFF80F, 0x1FFF0000, 0x1FFF7000, 0},
|
||||
{0x470, "STM32L4Rxx/4Sxx" , 0x20003200, 0x200A0000, 0x08000000, 0x08100000, 1, p_2k , 0x1FFF7800, 0x1FFFF80F, 0x1FFF0000, 0x1FFF7000, 0},
|
||||
{0x471, "STM32L4P5xx/Q5xx" , 0x20004000, 0x20050000, 0x08000000, 0x08100000, 1, p_4k , 0x1FF00000, 0x1FF0000F, 0x1FFF0000, 0x1FFF7000, 0}, /* dual-bank */
|
||||
/* L5 */
|
||||
{0x472, "STM32L552xx/562xx" , 0x20004000, 0x20040000, 0x08000000, 0x08080000, 1, p_2k , 0 , 0 , 0x0BF90000, 0x0BF98000, 0}, /* dual-bank */
|
||||
/* WB */
|
||||
{0x494, "STM32WB10xx/15xx" , 0x20005000, 0x20040000, 0x08000000, 0x08050000, 1, p_2k , 0x1FFF7800, 0x1FFF787F, 0x1FFF0000, 0x1FFF7000, 0},
|
||||
{0x495, "STM32WB30(5)xx/50(5)xx" , 0x20004000, 0x2000C000, 0x08000000, 0x08100000, 1, p_4k , 0x1FFF8000, 0x1FFF807F, 0x1FFF0000, 0x1FFF7000, 0},
|
||||
/* WL */
|
||||
{0x497, "STM32WLE5xx/WL55xx" , 0x20002000, 0x20010000, 0x08000000, 0x08040000, 1, p_2k , 0x1FFF7800, 0x1FFF7FFF, 0x1FFF0000, 0x1FFF4000, 0},
|
||||
/* U5 */
|
||||
{0x482, "STM32U575xx/585xx" , 0x20004000, 0x200C0000, 0x08000000, 0x08200000, 1, p_8k , 0 , 0 , 0x0BF90000, 0x0BFA0000, 0},
|
||||
/* These are not (yet) in AN2606: */
|
||||
{0x641, "Medium_Density PL" , 0x20000200, 0x20005000, 0x08000000, 0x08020000, 4, p_1k , 0x1FFFF800, 0x1FFFF80F, 0x1FFFF000, 0x1FFFF800, 0},
|
||||
{0x9a8, "STM32W-128K" , 0x20000200, 0x20002000, 0x08000000, 0x08020000, 4, p_1k , 0x08040800, 0x0804080F, 0x08040000, 0x08040800, 0},
|
||||
{0x9b0, "STM32W-256K" , 0x20000200, 0x20004000, 0x08000000, 0x08040000, 4, p_2k , 0x08040800, 0x0804080F, 0x08040000, 0x08040800, 0},
|
||||
{ /* sentinel */ }
|
||||
};
|
Binary file not shown.
|
@ -1,339 +0,0 @@
|
|||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The licenses for most software are designed to take away your
|
||||
freedom to share and change it. By contrast, the GNU General Public
|
||||
License is intended to guarantee your freedom to share and change free
|
||||
software--to make sure the software is free for all its users. This
|
||||
General Public License applies to most of the Free Software
|
||||
Foundation's software and to any other program whose authors commit to
|
||||
using it. (Some other Free Software Foundation software is covered by
|
||||
the GNU Lesser General Public License instead.) You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
this service if you wish), that you receive source code or can get it
|
||||
if you want it, that you can change the software or use pieces of it
|
||||
in new free programs; and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid
|
||||
anyone to deny you these rights or to ask you to surrender the rights.
|
||||
These restrictions translate to certain responsibilities for you if you
|
||||
distribute copies of the software, or if you modify it.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must give the recipients all the rights that
|
||||
you have. You must make sure that they, too, receive or can get the
|
||||
source code. And you must show them these terms so they know their
|
||||
rights.
|
||||
|
||||
We protect your rights with two steps: (1) copyright the software, and
|
||||
(2) offer you this license which gives you legal permission to copy,
|
||||
distribute and/or modify the software.
|
||||
|
||||
Also, for each author's protection and ours, we want to make certain
|
||||
that everyone understands that there is no warranty for this free
|
||||
software. If the software is modified by someone else and passed on, we
|
||||
want its recipients to know that what they have is not the original, so
|
||||
that any problems introduced by others will not reflect on the original
|
||||
authors' reputations.
|
||||
|
||||
Finally, any free program is threatened constantly by software
|
||||
patents. We wish to avoid the danger that redistributors of a free
|
||||
program will individually obtain patent licenses, in effect making the
|
||||
program proprietary. To prevent this, we have made it clear that any
|
||||
patent must be licensed for everyone's free use or not licensed at all.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. This License applies to any program or other work which contains
|
||||
a notice placed by the copyright holder saying it may be distributed
|
||||
under the terms of this General Public License. The "Program", below,
|
||||
refers to any such program or work, and a "work based on the Program"
|
||||
means either the Program or any derivative work under copyright law:
|
||||
that is to say, a work containing the Program or a portion of it,
|
||||
either verbatim or with modifications and/or translated into another
|
||||
language. (Hereinafter, translation is included without limitation in
|
||||
the term "modification".) Each licensee is addressed as "you".
|
||||
|
||||
Activities other than copying, distribution and modification are not
|
||||
covered by this License; they are outside its scope. The act of
|
||||
running the Program is not restricted, and the output from the Program
|
||||
is covered only if its contents constitute a work based on the
|
||||
Program (independent of having been made by running the Program).
|
||||
Whether that is true depends on what the Program does.
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Program's
|
||||
source code as you receive it, in any medium, provided that you
|
||||
conspicuously and appropriately publish on each copy an appropriate
|
||||
copyright notice and disclaimer of warranty; keep intact all the
|
||||
notices that refer to this License and to the absence of any warranty;
|
||||
and give any other recipients of the Program a copy of this License
|
||||
along with the Program.
|
||||
|
||||
You may charge a fee for the physical act of transferring a copy, and
|
||||
you may at your option offer warranty protection in exchange for a fee.
|
||||
|
||||
2. You may modify your copy or copies of the Program or any portion
|
||||
of it, thus forming a work based on the Program, and copy and
|
||||
distribute such modifications or work under the terms of Section 1
|
||||
above, provided that you also meet all of these conditions:
|
||||
|
||||
a) You must cause the modified files to carry prominent notices
|
||||
stating that you changed the files and the date of any change.
|
||||
|
||||
b) You must cause any work that you distribute or publish, that in
|
||||
whole or in part contains or is derived from the Program or any
|
||||
part thereof, to be licensed as a whole at no charge to all third
|
||||
parties under the terms of this License.
|
||||
|
||||
c) If the modified program normally reads commands interactively
|
||||
when run, you must cause it, when started running for such
|
||||
interactive use in the most ordinary way, to print or display an
|
||||
announcement including an appropriate copyright notice and a
|
||||
notice that there is no warranty (or else, saying that you provide
|
||||
a warranty) and that users may redistribute the program under
|
||||
these conditions, and telling the user how to view a copy of this
|
||||
License. (Exception: if the Program itself is interactive but
|
||||
does not normally print such an announcement, your work based on
|
||||
the Program is not required to print an announcement.)
|
||||
|
||||
These requirements apply to the modified work as a whole. If
|
||||
identifiable sections of that work are not derived from the Program,
|
||||
and can be reasonably considered independent and separate works in
|
||||
themselves, then this License, and its terms, do not apply to those
|
||||
sections when you distribute them as separate works. But when you
|
||||
distribute the same sections as part of a whole which is a work based
|
||||
on the Program, the distribution of the whole must be on the terms of
|
||||
this License, whose permissions for other licensees extend to the
|
||||
entire whole, and thus to each and every part regardless of who wrote it.
|
||||
|
||||
Thus, it is not the intent of this section to claim rights or contest
|
||||
your rights to work written entirely by you; rather, the intent is to
|
||||
exercise the right to control the distribution of derivative or
|
||||
collective works based on the Program.
|
||||
|
||||
In addition, mere aggregation of another work not based on the Program
|
||||
with the Program (or with a work based on the Program) on a volume of
|
||||
a storage or distribution medium does not bring the other work under
|
||||
the scope of this License.
|
||||
|
||||
3. You may copy and distribute the Program (or a work based on it,
|
||||
under Section 2) in object code or executable form under the terms of
|
||||
Sections 1 and 2 above provided that you also do one of the following:
|
||||
|
||||
a) Accompany it with the complete corresponding machine-readable
|
||||
source code, which must be distributed under the terms of Sections
|
||||
1 and 2 above on a medium customarily used for software interchange; or,
|
||||
|
||||
b) Accompany it with a written offer, valid for at least three
|
||||
years, to give any third party, for a charge no more than your
|
||||
cost of physically performing source distribution, a complete
|
||||
machine-readable copy of the corresponding source code, to be
|
||||
distributed under the terms of Sections 1 and 2 above on a medium
|
||||
customarily used for software interchange; or,
|
||||
|
||||
c) Accompany it with the information you received as to the offer
|
||||
to distribute corresponding source code. (This alternative is
|
||||
allowed only for noncommercial distribution and only if you
|
||||
received the program in object code or executable form with such
|
||||
an offer, in accord with Subsection b above.)
|
||||
|
||||
The source code for a work means the preferred form of the work for
|
||||
making modifications to it. For an executable work, complete source
|
||||
code means all the source code for all modules it contains, plus any
|
||||
associated interface definition files, plus the scripts used to
|
||||
control compilation and installation of the executable. However, as a
|
||||
special exception, the source code distributed need not include
|
||||
anything that is normally distributed (in either source or binary
|
||||
form) with the major components (compiler, kernel, and so on) of the
|
||||
operating system on which the executable runs, unless that component
|
||||
itself accompanies the executable.
|
||||
|
||||
If distribution of executable or object code is made by offering
|
||||
access to copy from a designated place, then offering equivalent
|
||||
access to copy the source code from the same place counts as
|
||||
distribution of the source code, even though third parties are not
|
||||
compelled to copy the source along with the object code.
|
||||
|
||||
4. You may not copy, modify, sublicense, or distribute the Program
|
||||
except as expressly provided under this License. Any attempt
|
||||
otherwise to copy, modify, sublicense or distribute the Program is
|
||||
void, and will automatically terminate your rights under this License.
|
||||
However, parties who have received copies, or rights, from you under
|
||||
this License will not have their licenses terminated so long as such
|
||||
parties remain in full compliance.
|
||||
|
||||
5. You are not required to accept this License, since you have not
|
||||
signed it. However, nothing else grants you permission to modify or
|
||||
distribute the Program or its derivative works. These actions are
|
||||
prohibited by law if you do not accept this License. Therefore, by
|
||||
modifying or distributing the Program (or any work based on the
|
||||
Program), you indicate your acceptance of this License to do so, and
|
||||
all its terms and conditions for copying, distributing or modifying
|
||||
the Program or works based on it.
|
||||
|
||||
6. Each time you redistribute the Program (or any work based on the
|
||||
Program), the recipient automatically receives a license from the
|
||||
original licensor to copy, distribute or modify the Program subject to
|
||||
these terms and conditions. You may not impose any further
|
||||
restrictions on the recipients' exercise of the rights granted herein.
|
||||
You are not responsible for enforcing compliance by third parties to
|
||||
this License.
|
||||
|
||||
7. If, as a consequence of a court judgment or allegation of patent
|
||||
infringement or for any other reason (not limited to patent issues),
|
||||
conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot
|
||||
distribute so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you
|
||||
may not distribute the Program at all. For example, if a patent
|
||||
license would not permit royalty-free redistribution of the Program by
|
||||
all those who receive copies directly or indirectly through you, then
|
||||
the only way you could satisfy both it and this License would be to
|
||||
refrain entirely from distribution of the Program.
|
||||
|
||||
If any portion of this section is held invalid or unenforceable under
|
||||
any particular circumstance, the balance of the section is intended to
|
||||
apply and the section as a whole is intended to apply in other
|
||||
circumstances.
|
||||
|
||||
It is not the purpose of this section to induce you to infringe any
|
||||
patents or other property right claims or to contest validity of any
|
||||
such claims; this section has the sole purpose of protecting the
|
||||
integrity of the free software distribution system, which is
|
||||
implemented by public license practices. Many people have made
|
||||
generous contributions to the wide range of software distributed
|
||||
through that system in reliance on consistent application of that
|
||||
system; it is up to the author/donor to decide if he or she is willing
|
||||
to distribute software through any other system and a licensee cannot
|
||||
impose that choice.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to
|
||||
be a consequence of the rest of this License.
|
||||
|
||||
8. If the distribution and/or use of the Program is restricted in
|
||||
certain countries either by patents or by copyrighted interfaces, the
|
||||
original copyright holder who places the Program under this License
|
||||
may add an explicit geographical distribution limitation excluding
|
||||
those countries, so that distribution is permitted only in or among
|
||||
countries not thus excluded. In such case, this License incorporates
|
||||
the limitation as if written in the body of this License.
|
||||
|
||||
9. The Free Software Foundation may publish revised and/or new versions
|
||||
of the General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Program
|
||||
specifies a version number of this License which applies to it and "any
|
||||
later version", you have the option of following the terms and conditions
|
||||
either of that version or of any later version published by the Free
|
||||
Software Foundation. If the Program does not specify a version number of
|
||||
this License, you may choose any version ever published by the Free Software
|
||||
Foundation.
|
||||
|
||||
10. If you wish to incorporate parts of the Program into other free
|
||||
programs whose distribution conditions are different, write to the author
|
||||
to ask for permission. For software which is copyrighted by the Free
|
||||
Software Foundation, write to the Free Software Foundation; we sometimes
|
||||
make exceptions for this. Our decision will be guided by the two goals
|
||||
of preserving the free status of all derivatives of our free software and
|
||||
of promoting the sharing and reuse of software generally.
|
||||
|
||||
NO WARRANTY
|
||||
|
||||
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
||||
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
||||
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
||||
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
||||
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
||||
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
||||
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
||||
REPAIR OR CORRECTION.
|
||||
|
||||
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
||||
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
||||
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
||||
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
||||
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
||||
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
convey the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along
|
||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program is interactive, make it output a short notice like this
|
||||
when it starts in an interactive mode:
|
||||
|
||||
Gnomovision version 69, Copyright (C) year name of author
|
||||
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, the commands you use may
|
||||
be called something other than `show w' and `show c'; they could even be
|
||||
mouse-clicks or menu items--whatever suits your program.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or your
|
||||
school, if any, to sign a "copyright disclaimer" for the program, if
|
||||
necessary. Here is a sample; alter the names:
|
||||
|
||||
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
||||
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
||||
|
||||
<signature of Ty Coon>, 1 April 1989
|
||||
Ty Coon, President of Vice
|
||||
|
||||
This General Public License does not permit incorporating your program into
|
||||
proprietary programs. If your program is a subroutine library, you may
|
||||
consider it more useful to permit linking proprietary applications with the
|
||||
library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License.
|
|
@ -1,218 +0,0 @@
|
|||
/*
|
||||
stm32flash - Open Source ST STM32 flash program for *nix
|
||||
Copyright (C) 2014 Antonio Borneo <borneo.antonio@gmail.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "compiler.h"
|
||||
#include "serial.h"
|
||||
#include "port.h"
|
||||
|
||||
|
||||
#if !defined(__linux__)
|
||||
|
||||
static port_err_t i2c_open(struct port_interface __unused *port,
|
||||
struct port_options __unused *ops)
|
||||
{
|
||||
return PORT_ERR_NODEV;
|
||||
}
|
||||
|
||||
struct port_interface port_i2c = {
|
||||
.name = "i2c",
|
||||
.open = i2c_open,
|
||||
};
|
||||
|
||||
#else
|
||||
|
||||
#ifdef __ANDROID__
|
||||
#define I2C_SLAVE 0x0703 /* Use this slave address */
|
||||
#define I2C_FUNCS 0x0705 /* Get the adapter functionality mask */
|
||||
/* To determine what functionality is present */
|
||||
#define I2C_FUNC_I2C 0x00000001
|
||||
#else
|
||||
#include <linux/i2c.h>
|
||||
#include <linux/i2c-dev.h>
|
||||
#endif
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
struct i2c_priv {
|
||||
int fd;
|
||||
int addr;
|
||||
};
|
||||
|
||||
static port_err_t i2c_open(struct port_interface *port,
|
||||
struct port_options *ops)
|
||||
{
|
||||
struct i2c_priv *h;
|
||||
int fd, addr, ret;
|
||||
unsigned long funcs;
|
||||
|
||||
/* 1. check device name match */
|
||||
if (strncmp(ops->device, "/dev/i2c-", strlen("/dev/i2c-")))
|
||||
return PORT_ERR_NODEV;
|
||||
|
||||
/* 2. check options */
|
||||
addr = ops->bus_addr;
|
||||
if (addr < 0x03 || addr > 0x77) {
|
||||
fprintf(stderr, "I2C address out of range [0x03-0x77]\n");
|
||||
return PORT_ERR_UNKNOWN;
|
||||
}
|
||||
|
||||
/* 3. open it */
|
||||
h = calloc(sizeof(*h), 1);
|
||||
if (h == NULL) {
|
||||
fprintf(stderr, "End of memory\n");
|
||||
return PORT_ERR_UNKNOWN;
|
||||
}
|
||||
fd = open(ops->device, O_RDWR);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "Unable to open special file \"%s\"\n",
|
||||
ops->device);
|
||||
free(h);
|
||||
return PORT_ERR_UNKNOWN;
|
||||
}
|
||||
|
||||
/* 3.5. Check capabilities */
|
||||
ret = ioctl(fd, I2C_FUNCS, &funcs);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "I2C ioctl(funcs) error %d\n", errno);
|
||||
close(fd);
|
||||
free(h);
|
||||
return PORT_ERR_UNKNOWN;
|
||||
}
|
||||
if ((funcs & I2C_FUNC_I2C) == 0) {
|
||||
fprintf(stderr, "Error: controller is not I2C, only SMBUS.\n");
|
||||
close(fd);
|
||||
free(h);
|
||||
return PORT_ERR_UNKNOWN;
|
||||
}
|
||||
|
||||
/* 4. set options */
|
||||
ret = ioctl(fd, I2C_SLAVE, addr);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "I2C ioctl(slave) error %d\n", errno);
|
||||
close(fd);
|
||||
free(h);
|
||||
return PORT_ERR_UNKNOWN;
|
||||
}
|
||||
|
||||
h->fd = fd;
|
||||
h->addr = addr;
|
||||
port->private = h;
|
||||
return PORT_ERR_OK;
|
||||
}
|
||||
|
||||
static port_err_t i2c_close(struct port_interface *port)
|
||||
{
|
||||
struct i2c_priv *h;
|
||||
|
||||
h = (struct i2c_priv *)port->private;
|
||||
if (h == NULL)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
close(h->fd);
|
||||
free(h);
|
||||
port->private = NULL;
|
||||
return PORT_ERR_OK;
|
||||
}
|
||||
|
||||
static port_err_t i2c_read(struct port_interface *port, void *buf,
|
||||
size_t nbyte)
|
||||
{
|
||||
struct i2c_priv *h;
|
||||
int ret;
|
||||
|
||||
h = (struct i2c_priv *)port->private;
|
||||
if (h == NULL)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
ret = read(h->fd, buf, nbyte);
|
||||
if (ret != (int)nbyte)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
return PORT_ERR_OK;
|
||||
}
|
||||
|
||||
static port_err_t i2c_write(struct port_interface *port, void *buf,
|
||||
size_t nbyte)
|
||||
{
|
||||
struct i2c_priv *h;
|
||||
int ret;
|
||||
|
||||
h = (struct i2c_priv *)port->private;
|
||||
if (h == NULL)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
ret = write(h->fd, buf, nbyte);
|
||||
if (ret != (int)nbyte)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
return PORT_ERR_OK;
|
||||
}
|
||||
|
||||
static port_err_t i2c_gpio(struct port_interface __unused *port,
|
||||
serial_gpio_t __unused n,
|
||||
int __unused level)
|
||||
{
|
||||
return PORT_ERR_OK;
|
||||
}
|
||||
|
||||
static const char *i2c_get_cfg_str(struct port_interface *port)
|
||||
{
|
||||
struct i2c_priv *h;
|
||||
static char str[11];
|
||||
|
||||
h = (struct i2c_priv *)port->private;
|
||||
if (h == NULL)
|
||||
return "INVALID";
|
||||
snprintf(str, sizeof(str), "addr 0x%2x", h->addr);
|
||||
return str;
|
||||
}
|
||||
|
||||
static struct varlen_cmd i2c_cmd_get_reply[] = {
|
||||
{0x10, 11},
|
||||
{0x11, 17},
|
||||
{0x12, 18},
|
||||
{ /* sentinel */ }
|
||||
};
|
||||
|
||||
static port_err_t i2c_flush(struct port_interface __unused *port)
|
||||
{
|
||||
/* We shouldn't need to flush I2C */
|
||||
return PORT_ERR_OK;
|
||||
}
|
||||
|
||||
struct port_interface port_i2c = {
|
||||
.name = "i2c",
|
||||
.flags = PORT_STRETCH_W | PORT_NPAG_CSUM,
|
||||
.open = i2c_open,
|
||||
.close = i2c_close,
|
||||
.flush = i2c_flush,
|
||||
.read = i2c_read,
|
||||
.write = i2c_write,
|
||||
.gpio = i2c_gpio,
|
||||
.cmd_get_reply = i2c_cmd_get_reply,
|
||||
.get_cfg_str = i2c_get_cfg_str,
|
||||
};
|
||||
|
||||
#endif
|
Binary file not shown.
|
@ -1,321 +0,0 @@
|
|||
/*
|
||||
stm32flash - Open Source ST STM32 flash program for *nix
|
||||
Copyright (C) 2010 Geoffrey McRae <geoff@spacevs.com>
|
||||
Copyright (C) 2013 Antonio Borneo <borneo.antonio@gmail.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "compiler.h"
|
||||
#include "init.h"
|
||||
#include "serial.h"
|
||||
#include "stm32.h"
|
||||
#include "port.h"
|
||||
#include "utils.h"
|
||||
|
||||
extern FILE *diag;
|
||||
|
||||
struct gpio_list {
|
||||
struct gpio_list *next;
|
||||
int gpio;
|
||||
int input; /* 1 if direction of gpio should be changed back to input. */
|
||||
int exported; /* 0 if gpio should be unexported. */
|
||||
};
|
||||
|
||||
#if defined(__linux__)
|
||||
static int write_to(const char *filename, const char *value)
|
||||
{
|
||||
int fd, ret;
|
||||
|
||||
fd = open(filename, O_WRONLY);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "Cannot open file \"%s\"\n", filename);
|
||||
return 0;
|
||||
}
|
||||
ret = write(fd, value, strlen(value));
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "Error writing in file \"%s\"\n", filename);
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
close(fd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int read_from(const char *filename, char *buf, size_t len)
|
||||
{
|
||||
int fd, ret;
|
||||
size_t n = 0;
|
||||
|
||||
fd = open(filename, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "Cannot open file \"%s\"\n", filename);
|
||||
return 0;
|
||||
}
|
||||
|
||||
do {
|
||||
ret = read(fd, buf + n, len - n);
|
||||
if (ret < 0) {
|
||||
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
|
||||
continue; /* try again */
|
||||
fprintf(stderr, "Error reading in file \"%s\"\n", filename);
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
n += ret;
|
||||
} while (n < len && ret);
|
||||
|
||||
close(fd);
|
||||
return n;
|
||||
}
|
||||
|
||||
static int drive_gpio(int n, int level, struct gpio_list **gpio_to_release)
|
||||
{
|
||||
char num[16]; /* sized to carry MAX_INT */
|
||||
char file[48]; /* sized to carry longest filename */
|
||||
char dir;
|
||||
struct stat buf;
|
||||
struct gpio_list *new;
|
||||
int ret;
|
||||
int exported = 1;
|
||||
int input = 0;
|
||||
|
||||
sprintf(file, "/sys/class/gpio/gpio%d/value", n);
|
||||
ret = stat(file, &buf);
|
||||
if (ret) {
|
||||
/* file miss, GPIO not exported yet */
|
||||
sprintf(num, "%d", n);
|
||||
ret = write_to("/sys/class/gpio/export", num);
|
||||
if (!ret)
|
||||
return 0;
|
||||
ret = stat(file, &buf);
|
||||
if (ret) {
|
||||
fprintf(stderr, "GPIO %d not available\n", n);
|
||||
return 0;
|
||||
}
|
||||
/* give udevd a chance to set permissions */
|
||||
if (access(file, W_OK))
|
||||
usleep(10);
|
||||
exported = 0;
|
||||
}
|
||||
|
||||
sprintf(file, "/sys/class/gpio/gpio%d/direction", n);
|
||||
ret = stat(file, &buf);
|
||||
if (!ret)
|
||||
if (read_from(file, &dir, sizeof(dir)))
|
||||
if (dir == 'i')
|
||||
input = 1;
|
||||
|
||||
if (exported == 0 || input == 1) {
|
||||
new = (struct gpio_list *)malloc(sizeof(struct gpio_list));
|
||||
if (new == NULL) {
|
||||
fprintf(stderr, "Out of memory\n");
|
||||
return 0;
|
||||
}
|
||||
new->gpio = n;
|
||||
new->exported = exported;
|
||||
new->input = input;
|
||||
new->next = *gpio_to_release;
|
||||
*gpio_to_release = new;
|
||||
}
|
||||
|
||||
return write_to(file, level ? "high" : "low");
|
||||
}
|
||||
|
||||
static int release_gpio(int n, int input, int exported)
|
||||
{
|
||||
char num[16]; /* sized to carry MAX_INT */
|
||||
char file[48]; /* sized to carry longest filename */
|
||||
|
||||
sprintf(num, "%d", n);
|
||||
if (input) {
|
||||
sprintf(file, "/sys/class/gpio/gpio%d/direction", n);
|
||||
write_to(file, "in");
|
||||
}
|
||||
if (!exported)
|
||||
write_to("/sys/class/gpio/unexport", num);
|
||||
|
||||
return 1;
|
||||
}
|
||||
#else
|
||||
static int drive_gpio(int __unused n, int __unused level,
|
||||
struct gpio_list __unused **gpio_to_release)
|
||||
{
|
||||
fprintf(stderr, "GPIO control only available in Linux\n");
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int gpio_sequence(struct port_interface *port, const char *seq, size_t len_seq)
|
||||
{
|
||||
struct gpio_list *gpio_to_release = NULL;
|
||||
#if defined(__linux__)
|
||||
struct gpio_list *to_free;
|
||||
#endif
|
||||
int ret = 0, level, gpio;
|
||||
int sleep_time = 0;
|
||||
int delimiter = 0;
|
||||
const char *sig_str = NULL;
|
||||
const char *s = seq;
|
||||
size_t l = len_seq;
|
||||
|
||||
fprintf(diag, "\nGPIO sequence start\n");
|
||||
while (ret == 0 && *s && l > 0) {
|
||||
sig_str = NULL;
|
||||
sleep_time = 0;
|
||||
delimiter = 0;
|
||||
|
||||
if (*s == '-') {
|
||||
level = 0;
|
||||
s++;
|
||||
l--;
|
||||
} else
|
||||
level = 1;
|
||||
|
||||
if (isdigit(*s)) {
|
||||
gpio = atoi(s);
|
||||
while (isdigit(*s)) {
|
||||
s++;
|
||||
l--;
|
||||
}
|
||||
} else if (l >= 3 && !strncmp(s, "rts", 3)) {
|
||||
sig_str = s;
|
||||
gpio = -GPIO_RTS;
|
||||
s += 3;
|
||||
l -= 3;
|
||||
} else if (l >= 3 && !strncmp(s, "dtr", 3)) {
|
||||
sig_str = s;
|
||||
gpio = -GPIO_DTR;
|
||||
s += 3;
|
||||
l -= 3;
|
||||
} else if (l >= 3 && !strncmp(s, "brk", 3)) {
|
||||
sig_str = s;
|
||||
gpio = -GPIO_BRK;
|
||||
s += 3;
|
||||
l -= 3;
|
||||
} else if (*s && (l > 0)) {
|
||||
delimiter = 1;
|
||||
/* The ',' delimiter adds a 100 ms delay between signal toggles.
|
||||
* i.e -rts,dtr will reset rts, wait 100 ms, set dtr.
|
||||
*
|
||||
* The '&' delimiter adds no delay between signal toggles.
|
||||
* i.e -rts&dtr will reset rts and immediately set dtr.
|
||||
*
|
||||
* Example: -rts&dtr,,,rts,-dtr will reset rts and set dtr
|
||||
* without delay, then wait 300 ms, set rts, wait 100 ms, reset dtr.
|
||||
*/
|
||||
if (*s == ',') {
|
||||
s++;
|
||||
l--;
|
||||
sleep_time = 100000;
|
||||
} else if (*s == '&') {
|
||||
s++;
|
||||
l--;
|
||||
} else {
|
||||
fprintf(stderr, "Character \'%c\' is not a valid signal or separator\n", *s);
|
||||
ret = 1;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
/* E.g. modifier without signal */
|
||||
fprintf(stderr, "Invalid sequence %.*s\n", (int) len_seq, seq);
|
||||
ret = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!delimiter) { /* actual gpio/port signal driving */
|
||||
if (gpio < 0) {
|
||||
gpio = -gpio;
|
||||
fprintf(diag, " setting port signal %.3s to %i... ", sig_str, level);
|
||||
ret = (port->gpio(port, gpio, level) != PORT_ERR_OK);
|
||||
printStatus(diag, ret);
|
||||
} else {
|
||||
fprintf(diag, " setting gpio %i to %i... ", gpio, level);
|
||||
ret = (drive_gpio(gpio, level, &gpio_to_release) != 1);
|
||||
printStatus(diag, ret);
|
||||
}
|
||||
}
|
||||
|
||||
if (sleep_time) {
|
||||
fprintf(diag, " delay %i us\n", sleep_time);
|
||||
usleep(sleep_time);
|
||||
}
|
||||
}
|
||||
#if defined(__linux__)
|
||||
while (gpio_to_release) {
|
||||
release_gpio(gpio_to_release->gpio, gpio_to_release->input, gpio_to_release->exported);
|
||||
to_free = gpio_to_release;
|
||||
gpio_to_release = gpio_to_release->next;
|
||||
free(to_free);
|
||||
}
|
||||
#endif
|
||||
fprintf(diag, "GPIO sequence end\n\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int gpio_bl_entry(struct port_interface *port, const char *seq)
|
||||
{
|
||||
char *s;
|
||||
|
||||
if (seq == NULL || seq[0] == ':')
|
||||
return 1;
|
||||
|
||||
s = strchr(seq, ':');
|
||||
if (s == NULL)
|
||||
return gpio_sequence(port, seq, strlen(seq));
|
||||
|
||||
return gpio_sequence(port, seq, s - seq);
|
||||
}
|
||||
|
||||
int gpio_bl_exit(struct port_interface *port, const char *seq)
|
||||
{
|
||||
char *s;
|
||||
|
||||
if (seq == NULL)
|
||||
return 1;
|
||||
|
||||
s = strchr(seq, ':');
|
||||
if (s == NULL || s[1] == '\0')
|
||||
return 1;
|
||||
|
||||
return gpio_sequence(port, s + 1, strlen(s + 1));
|
||||
}
|
||||
|
||||
int init_bl_entry(struct port_interface *port, const char *seq)
|
||||
{
|
||||
if (seq)
|
||||
return gpio_bl_entry(port, seq);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int init_bl_exit(stm32_t *stm, struct port_interface *port, const char *seq)
|
||||
{
|
||||
if (seq && strchr(seq, ':'))
|
||||
return gpio_bl_exit(port, seq);
|
||||
|
||||
return stm32_reset_device(stm);
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
/*
|
||||
stm32flash - Open Source ST STM32 flash program for *nix
|
||||
Copyright (C) 2010 Geoffrey McRae <geoff@spacevs.com>
|
||||
Copyright (C) 2013 Antonio Borneo <borneo.antonio@gmail.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _INIT_H
|
||||
#define _INIT_H
|
||||
|
||||
#include "stm32.h"
|
||||
#include "port.h"
|
||||
|
||||
int init_bl_entry(struct port_interface *port, const char *seq);
|
||||
int init_bl_exit(stm32_t *stm, struct port_interface *port, const char *seq);
|
||||
int gpio_bl_exit(struct port_interface *port, const char *seq);
|
||||
|
||||
#endif
|
Binary file not shown.
|
@ -1,988 +0,0 @@
|
|||
/*
|
||||
stm32flash - Open Source ST STM32 flash program for *nix
|
||||
Copyright 2010 Geoffrey McRae <geoff@spacevs.com>
|
||||
Copyright 2011 Steve Markgraf <steve@steve-m.de>
|
||||
Copyright 2012-2016 Tormod Volden <debian.tormod@gmail.com>
|
||||
Copyright 2013-2016 Antonio Borneo <borneo.antonio@gmail.com>
|
||||
Copyright 2021 Renaud Fivet <renaud.fivet@gmail.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include "init.h"
|
||||
#include "utils.h"
|
||||
#include "serial.h"
|
||||
#include "stm32.h"
|
||||
#include "parsers/parser.h"
|
||||
#include "port.h"
|
||||
|
||||
#include "parsers/binary.h"
|
||||
#include "parsers/hex.h"
|
||||
|
||||
#if defined(__WIN32__) || defined(__CYGWIN__)
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#define VERSION "0.7"
|
||||
|
||||
/* device globals */
|
||||
stm32_t *stm = NULL;
|
||||
void *p_st = NULL;
|
||||
parser_t *parser = NULL;
|
||||
struct port_interface *port = NULL;
|
||||
|
||||
/* settings */
|
||||
struct port_options port_opts = {
|
||||
.device = NULL,
|
||||
.baudRate = SERIAL_BAUD_57600,
|
||||
.serial_mode = "8e1",
|
||||
.bus_addr = 0,
|
||||
.rx_frame_max = STM32_MAX_RX_FRAME,
|
||||
.tx_frame_max = STM32_MAX_TX_FRAME,
|
||||
};
|
||||
|
||||
enum actions {
|
||||
ACT_NONE,
|
||||
ACT_READ,
|
||||
ACT_WRITE,
|
||||
ACT_WRITE_UNPROTECT,
|
||||
ACT_READ_PROTECT,
|
||||
ACT_READ_UNPROTECT,
|
||||
ACT_ERASE_ONLY,
|
||||
ACT_CRC
|
||||
};
|
||||
|
||||
enum actions action = ACT_NONE;
|
||||
int npages = 0;
|
||||
int spage = 0;
|
||||
int no_erase = 0;
|
||||
char verify = 0;
|
||||
int retry = 10;
|
||||
char exec_flag = 0;
|
||||
uint32_t execute = 0;
|
||||
char init_flag = 1;
|
||||
int use_stdinout = 0;
|
||||
char force_binary = 0;
|
||||
FILE *diag;
|
||||
char reset_flag = 0;
|
||||
char *filename;
|
||||
char *gpio_seq = NULL;
|
||||
uint32_t start_addr = 0;
|
||||
uint32_t readwrite_len = 0;
|
||||
|
||||
/* functions */
|
||||
int parse_options(int argc, char *argv[]);
|
||||
void show_help(char *name);
|
||||
|
||||
static const char *action2str(enum actions act)
|
||||
{
|
||||
switch (act) {
|
||||
case ACT_READ:
|
||||
return "memory read";
|
||||
case ACT_WRITE:
|
||||
return "memory write";
|
||||
case ACT_WRITE_UNPROTECT:
|
||||
return "write unprotect";
|
||||
case ACT_READ_PROTECT:
|
||||
return "read protect";
|
||||
case ACT_READ_UNPROTECT:
|
||||
return "read unprotect";
|
||||
case ACT_ERASE_ONLY:
|
||||
return "flash erase";
|
||||
case ACT_CRC:
|
||||
return "memory crc";
|
||||
default:
|
||||
return "";
|
||||
};
|
||||
}
|
||||
|
||||
static void err_multi_action(enum actions new)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"ERROR: Invalid options !\n"
|
||||
"\tCan't execute \"%s\" and \"%s\" at the same time.\n",
|
||||
action2str(action), action2str(new));
|
||||
}
|
||||
|
||||
static int is_addr_in_ram(uint32_t addr)
|
||||
{
|
||||
return addr >= stm->dev->ram_start && addr < stm->dev->ram_end;
|
||||
}
|
||||
|
||||
static int is_addr_in_flash(uint32_t addr)
|
||||
{
|
||||
return addr >= stm->dev->fl_start && addr < stm->dev->fl_end;
|
||||
}
|
||||
|
||||
static int is_addr_in_opt_bytes(uint32_t addr)
|
||||
{
|
||||
/* option bytes upper range is inclusive in our device table */
|
||||
return addr >= stm->dev->opt_start && addr <= stm->dev->opt_end;
|
||||
}
|
||||
|
||||
static int is_addr_in_sysmem(uint32_t addr)
|
||||
{
|
||||
return addr >= stm->dev->mem_start && addr < stm->dev->mem_end;
|
||||
}
|
||||
|
||||
/* returns the page that contains address "addr" */
|
||||
static int flash_addr_to_page_floor(uint32_t addr)
|
||||
{
|
||||
int page;
|
||||
uint32_t *psize;
|
||||
|
||||
if (!is_addr_in_flash(addr))
|
||||
return 0;
|
||||
|
||||
page = 0;
|
||||
addr -= stm->dev->fl_start;
|
||||
psize = stm->dev->fl_ps;
|
||||
|
||||
while (addr >= psize[0]) {
|
||||
addr -= psize[0];
|
||||
page++;
|
||||
if (psize[1])
|
||||
psize++;
|
||||
}
|
||||
|
||||
return page;
|
||||
}
|
||||
|
||||
/* returns the first page whose start addr is >= "addr" */
|
||||
int flash_addr_to_page_ceil(uint32_t addr)
|
||||
{
|
||||
int page;
|
||||
uint32_t *psize;
|
||||
|
||||
if (!(addr >= stm->dev->fl_start && addr <= stm->dev->fl_end))
|
||||
return 0;
|
||||
|
||||
page = 0;
|
||||
addr -= stm->dev->fl_start;
|
||||
psize = stm->dev->fl_ps;
|
||||
|
||||
while (addr >= psize[0]) {
|
||||
addr -= psize[0];
|
||||
page++;
|
||||
if (psize[1])
|
||||
psize++;
|
||||
}
|
||||
|
||||
return addr ? page + 1 : page;
|
||||
}
|
||||
|
||||
/* returns the lower address of flash page "page" */
|
||||
static uint32_t flash_page_to_addr(int page)
|
||||
{
|
||||
int i;
|
||||
uint32_t addr, *psize;
|
||||
|
||||
addr = stm->dev->fl_start;
|
||||
psize = stm->dev->fl_ps;
|
||||
|
||||
for (i = 0; i < page; i++) {
|
||||
addr += psize[0];
|
||||
if (psize[1])
|
||||
psize++;
|
||||
}
|
||||
|
||||
return addr;
|
||||
}
|
||||
|
||||
|
||||
#if defined(__WIN32__) || defined(__CYGWIN__)
|
||||
BOOL CtrlHandler( DWORD fdwCtrlType )
|
||||
{
|
||||
fprintf(stderr, "\nCaught signal %lu\n",fdwCtrlType);
|
||||
if (p_st && parser ) parser->close(p_st);
|
||||
if (stm ) stm32_close (stm);
|
||||
if (port) port->close(port);
|
||||
exit(1);
|
||||
}
|
||||
#else
|
||||
void sighandler(int s){
|
||||
fprintf(stderr, "\nCaught signal %d\n",s);
|
||||
if (p_st && parser ) parser->close(p_st);
|
||||
if (stm ) stm32_close (stm);
|
||||
if (port) port->close(port);
|
||||
exit(1);
|
||||
}
|
||||
#endif
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
int ret = 1;
|
||||
stm32_err_t s_err;
|
||||
parser_err_t perr;
|
||||
diag = stdout;
|
||||
|
||||
if (parse_options(argc, argv) != 0)
|
||||
goto close;
|
||||
|
||||
if (action == ACT_READ && use_stdinout) {
|
||||
diag = stderr;
|
||||
}
|
||||
|
||||
fprintf(diag, "stm32flash " VERSION "\n\n");
|
||||
fprintf(diag, "http://stm32flash.sourceforge.net/\n\n");
|
||||
|
||||
#if defined(__WIN32__) || defined(__CYGWIN__)
|
||||
SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, TRUE );
|
||||
#else
|
||||
struct sigaction sigIntHandler;
|
||||
|
||||
sigIntHandler.sa_handler = sighandler;
|
||||
sigemptyset(&sigIntHandler.sa_mask);
|
||||
sigIntHandler.sa_flags = 0;
|
||||
|
||||
sigaction(SIGINT, &sigIntHandler, NULL);
|
||||
#endif
|
||||
|
||||
if (action == ACT_WRITE) {
|
||||
/* first try hex */
|
||||
if (!force_binary) {
|
||||
parser = &PARSER_HEX;
|
||||
p_st = parser->init();
|
||||
if (!p_st) {
|
||||
fprintf(stderr, "%s Parser failed to initialize\n", parser->name);
|
||||
goto close;
|
||||
}
|
||||
}
|
||||
|
||||
if (force_binary || (perr = parser->open(p_st, filename, 0)) != PARSER_ERR_OK) {
|
||||
if (force_binary || perr == PARSER_ERR_INVALID_FILE) {
|
||||
if (!force_binary) {
|
||||
parser->close(p_st);
|
||||
p_st = NULL;
|
||||
}
|
||||
|
||||
/* now try binary */
|
||||
parser = &PARSER_BINARY;
|
||||
p_st = parser->init();
|
||||
if (!p_st) {
|
||||
fprintf(stderr, "%s Parser failed to initialize\n", parser->name);
|
||||
goto close;
|
||||
}
|
||||
perr = parser->open(p_st, filename, 0);
|
||||
}
|
||||
|
||||
/* if still have an error, fail */
|
||||
if (perr != PARSER_ERR_OK) {
|
||||
fprintf(stderr, "%s ERROR: %s\n", parser->name, parser_errstr(perr));
|
||||
if (perr == PARSER_ERR_SYSTEM) perror(filename);
|
||||
goto close;
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(diag, "Using Parser : %s\n", parser->name);
|
||||
|
||||
/* We may know from the file how much data there is */
|
||||
if (!use_stdinout) {
|
||||
if (!start_addr)
|
||||
start_addr = parser->base(p_st);
|
||||
if (start_addr)
|
||||
fprintf(diag, "Location : %#08x\n", start_addr);
|
||||
|
||||
if (!readwrite_len)
|
||||
readwrite_len = parser->size(p_st);
|
||||
fprintf(diag, "Size : %u\n", readwrite_len);
|
||||
}
|
||||
} else {
|
||||
parser = &PARSER_BINARY;
|
||||
p_st = parser->init();
|
||||
if (!p_st) {
|
||||
fprintf(stderr, "%s Parser failed to initialize\n", parser->name);
|
||||
goto close;
|
||||
}
|
||||
}
|
||||
|
||||
if (port_open(&port_opts, &port) != PORT_ERR_OK) {
|
||||
fprintf(stderr, "Failed to open port: %s\n", port_opts.device);
|
||||
goto close;
|
||||
}
|
||||
|
||||
fprintf(diag, "Interface %s: %s\n", port->name, port->get_cfg_str(port));
|
||||
if (init_flag && init_bl_entry(port, gpio_seq)){
|
||||
ret = 1;
|
||||
fprintf(stderr, "Failed to send boot enter sequence\n");
|
||||
goto close;
|
||||
}
|
||||
|
||||
port->flush(port);
|
||||
|
||||
stm = stm32_init(port, init_flag);
|
||||
if (!stm)
|
||||
goto close;
|
||||
|
||||
fprintf(diag, "Version : 0x%02x\n", stm->bl_version);
|
||||
if (port->flags & PORT_GVR_ETX) {
|
||||
fprintf(diag, "Option 1 : 0x%02x\n", stm->option1);
|
||||
fprintf(diag, "Option 2 : 0x%02x\n", stm->option2);
|
||||
}
|
||||
fprintf(diag, "Device ID : 0x%04x (%s)\n", stm->pid, stm->dev->name);
|
||||
fprintf(diag, "- RAM : Up to %dKiB (%db reserved by bootloader)\n", (stm->dev->ram_end - 0x20000000) / 1024, stm->dev->ram_start - 0x20000000);
|
||||
fprintf(diag, "- Flash : Up to %dKiB (size first sector: %dx%d)\n", (stm->dev->fl_end - stm->dev->fl_start ) / 1024, stm->dev->fl_pps, stm->dev->fl_ps[0]);
|
||||
fprintf(diag, "- Option bytes : %db\n", stm->dev->opt_end - stm->dev->opt_start + 1);
|
||||
fprintf(diag, "- System memory : %dKiB\n", (stm->dev->mem_end - stm->dev->mem_start) / 1024);
|
||||
|
||||
uint8_t buffer[256];
|
||||
uint32_t addr, start, end;
|
||||
unsigned int len;
|
||||
int failed = 0;
|
||||
int first_page, num_pages;
|
||||
|
||||
/*
|
||||
* Cleanup addresses:
|
||||
*
|
||||
* Starting from options
|
||||
* start_addr, readwrite_len, spage, npages
|
||||
* and using device memory size, compute
|
||||
* start, end, first_page, num_pages
|
||||
*/
|
||||
if (start_addr || readwrite_len) {
|
||||
if (start_addr == 0)
|
||||
/* default */
|
||||
start = stm->dev->fl_start;
|
||||
else if (start_addr == 1)
|
||||
/* if specified to be 0 by user */
|
||||
start = 0;
|
||||
else
|
||||
start = start_addr;
|
||||
|
||||
if (is_addr_in_flash(start))
|
||||
end = stm->dev->fl_end;
|
||||
else {
|
||||
no_erase = 1;
|
||||
if (is_addr_in_ram(start))
|
||||
end = stm->dev->ram_end;
|
||||
else if (is_addr_in_opt_bytes(start))
|
||||
end = stm->dev->opt_end + 1;
|
||||
else if (is_addr_in_sysmem(start))
|
||||
end = stm->dev->mem_end;
|
||||
else {
|
||||
/* Unknown territory */
|
||||
if (readwrite_len)
|
||||
end = start + readwrite_len;
|
||||
else
|
||||
end = start + sizeof(uint32_t);
|
||||
}
|
||||
}
|
||||
|
||||
if (readwrite_len && (end > start + readwrite_len))
|
||||
end = start + readwrite_len;
|
||||
|
||||
first_page = flash_addr_to_page_floor(start);
|
||||
if (!first_page && end == stm->dev->fl_end)
|
||||
num_pages = STM32_MASS_ERASE;
|
||||
else
|
||||
num_pages = flash_addr_to_page_ceil(end) - first_page;
|
||||
} else if (!spage && !npages) {
|
||||
start = stm->dev->fl_start;
|
||||
end = stm->dev->fl_end;
|
||||
first_page = 0;
|
||||
num_pages = STM32_MASS_ERASE;
|
||||
} else {
|
||||
first_page = spage;
|
||||
start = flash_page_to_addr(first_page);
|
||||
if (start > stm->dev->fl_end) {
|
||||
fprintf(stderr, "Address range exceeds flash size.\n");
|
||||
goto close;
|
||||
}
|
||||
|
||||
if (npages) {
|
||||
num_pages = npages;
|
||||
end = flash_page_to_addr(first_page + num_pages);
|
||||
if (end > stm->dev->fl_end)
|
||||
end = stm->dev->fl_end;
|
||||
} else {
|
||||
end = stm->dev->fl_end;
|
||||
num_pages = flash_addr_to_page_ceil(end) - first_page;
|
||||
}
|
||||
|
||||
if (!first_page && end == stm->dev->fl_end)
|
||||
num_pages = STM32_MASS_ERASE;
|
||||
}
|
||||
|
||||
if (action == ACT_READ) {
|
||||
unsigned int max_len = port_opts.rx_frame_max;
|
||||
|
||||
fprintf(diag, "Memory read\n");
|
||||
|
||||
perr = parser->open(p_st, filename, 1);
|
||||
if (perr != PARSER_ERR_OK) {
|
||||
fprintf(stderr, "%s ERROR: %s\n", parser->name, parser_errstr(perr));
|
||||
if (perr == PARSER_ERR_SYSTEM)
|
||||
perror(filename);
|
||||
goto close;
|
||||
}
|
||||
|
||||
fflush(diag);
|
||||
addr = start;
|
||||
while(addr < end) {
|
||||
uint32_t left = end - addr;
|
||||
len = max_len > left ? left : max_len;
|
||||
s_err = stm32_read_memory(stm, addr, buffer, len);
|
||||
if (s_err != STM32_ERR_OK) {
|
||||
fprintf(stderr, "Failed to read memory at address 0x%08x, target write-protected?\n", addr);
|
||||
goto close;
|
||||
}
|
||||
if (parser->write(p_st, buffer, len) != PARSER_ERR_OK)
|
||||
{
|
||||
fprintf(stderr, "Failed to write data to file\n");
|
||||
goto close;
|
||||
}
|
||||
addr += len;
|
||||
|
||||
fprintf(diag,
|
||||
"\rRead address 0x%08x (%.2f%%) ",
|
||||
addr,
|
||||
(100.0f / (float)(end - start)) * (float)(addr - start)
|
||||
);
|
||||
fflush(diag);
|
||||
}
|
||||
fprintf(diag, "Done.\n");
|
||||
ret = 0;
|
||||
goto close;
|
||||
} else if (action == ACT_READ_PROTECT) {
|
||||
fprintf(diag, "Read-Protecting flash\n");
|
||||
/* the device automatically performs a reset after the sending the ACK */
|
||||
reset_flag = 0;
|
||||
s_err = stm32_readprot_memory(stm);
|
||||
if (s_err != STM32_ERR_OK) {
|
||||
fprintf(stderr, "Failed to read-protect flash\n");
|
||||
goto close;
|
||||
}
|
||||
fprintf(diag, "Done.\n");
|
||||
ret = 0;
|
||||
} else if (action == ACT_READ_UNPROTECT) {
|
||||
fprintf(diag, "Read-UnProtecting flash\n");
|
||||
/* the device automatically performs a reset after the sending the ACK */
|
||||
reset_flag = 0;
|
||||
s_err = stm32_runprot_memory(stm);
|
||||
if (s_err != STM32_ERR_OK) {
|
||||
fprintf(stderr, "Failed to read-unprotect flash\n");
|
||||
goto close;
|
||||
}
|
||||
fprintf(diag, "Done.\n");
|
||||
ret = 0;
|
||||
} else if (action == ACT_ERASE_ONLY) {
|
||||
ret = 0;
|
||||
fprintf(diag, "Erasing flash\n");
|
||||
|
||||
if (num_pages != STM32_MASS_ERASE &&
|
||||
(start != flash_page_to_addr(first_page)
|
||||
|| end != flash_page_to_addr(first_page + num_pages))) {
|
||||
fprintf(stderr, "Specified start & length are invalid (must be page aligned)\n");
|
||||
ret = 1;
|
||||
goto close;
|
||||
}
|
||||
|
||||
s_err = stm32_erase_memory(stm, first_page, num_pages);
|
||||
if (s_err != STM32_ERR_OK) {
|
||||
fprintf(stderr, "Failed to erase memory\n");
|
||||
ret = 1;
|
||||
goto close;
|
||||
}
|
||||
ret = 0;
|
||||
} else if (action == ACT_WRITE_UNPROTECT) {
|
||||
fprintf(diag, "Write-unprotecting flash\n");
|
||||
/* the device automatically performs a reset after the sending the ACK */
|
||||
reset_flag = 0;
|
||||
s_err = stm32_wunprot_memory(stm);
|
||||
if (s_err != STM32_ERR_OK) {
|
||||
fprintf(stderr, "Failed to write-unprotect flash\n");
|
||||
goto close;
|
||||
}
|
||||
fprintf(diag, "Done.\n");
|
||||
ret = 0;
|
||||
} else if (action == ACT_WRITE) {
|
||||
fprintf(diag, "Write to memory\n");
|
||||
|
||||
unsigned int offset = 0;
|
||||
unsigned int r;
|
||||
unsigned int size;
|
||||
unsigned int max_wlen, max_rlen;
|
||||
|
||||
max_wlen = port_opts.tx_frame_max - 2; /* skip len and crc */
|
||||
max_wlen &= ~3; /* 32 bit aligned */
|
||||
|
||||
max_rlen = port_opts.rx_frame_max;
|
||||
max_rlen = max_rlen < max_wlen ? max_rlen : max_wlen;
|
||||
|
||||
/* Assume data from stdin is whole device */
|
||||
if (use_stdinout)
|
||||
size = end - start;
|
||||
else
|
||||
size = parser->size(p_st);
|
||||
|
||||
// TODO: It is possible to write to non-page boundaries, by reading out flash
|
||||
// from partial pages and combining with the input data
|
||||
// if ((start % stm->dev->fl_ps[i]) != 0 || (end % stm->dev->fl_ps[i]) != 0) {
|
||||
// fprintf(stderr, "Specified start & length are invalid (must be page aligned)\n");
|
||||
// goto close;
|
||||
// }
|
||||
|
||||
// TODO: If writes are not page aligned, we should probably read out existing flash
|
||||
// contents first, so it can be preserved and combined with new data
|
||||
if (!no_erase && num_pages) {
|
||||
fprintf(diag, "Erasing memory\n");
|
||||
s_err = stm32_erase_memory(stm, first_page, num_pages);
|
||||
if (s_err != STM32_ERR_OK) {
|
||||
fprintf(stderr, "Failed to erase memory\n");
|
||||
goto close;
|
||||
}
|
||||
}
|
||||
|
||||
fflush(diag);
|
||||
addr = start;
|
||||
while(addr < end && offset < size) {
|
||||
uint32_t left = end - addr;
|
||||
len = max_wlen > left ? left : max_wlen;
|
||||
len = len > size - offset ? size - offset : len;
|
||||
unsigned int reqlen = len ;
|
||||
|
||||
if (parser->read(p_st, buffer, &len) != PARSER_ERR_OK)
|
||||
goto close;
|
||||
|
||||
if (len == 0) {
|
||||
if (use_stdinout) {
|
||||
break;
|
||||
} else {
|
||||
fprintf(stderr, "Failed to read input file\n");
|
||||
goto close;
|
||||
}
|
||||
}
|
||||
|
||||
again:
|
||||
s_err = stm32_write_memory(stm, addr, buffer, len);
|
||||
if (s_err != STM32_ERR_OK) {
|
||||
fprintf(stderr, "Failed to write memory at address 0x%08x\n", addr);
|
||||
goto close;
|
||||
}
|
||||
|
||||
if (verify) {
|
||||
uint8_t compare[len];
|
||||
unsigned int offset, rlen;
|
||||
|
||||
offset = 0;
|
||||
while (offset < len) {
|
||||
rlen = len - offset;
|
||||
rlen = rlen < max_rlen ? rlen : max_rlen;
|
||||
s_err = stm32_read_memory(stm, addr + offset, compare + offset, rlen);
|
||||
if (s_err != STM32_ERR_OK) {
|
||||
fprintf(stderr, "Failed to read memory at address 0x%08x\n", addr + offset);
|
||||
goto close;
|
||||
}
|
||||
offset += rlen;
|
||||
}
|
||||
|
||||
for(r = 0; r < len; ++r)
|
||||
if (buffer[r] != compare[r]) {
|
||||
if (failed == retry) {
|
||||
fprintf(stderr, "Failed to verify at address 0x%08x, expected 0x%02x and found 0x%02x\n",
|
||||
(uint32_t)(addr + r),
|
||||
buffer [r],
|
||||
compare[r]
|
||||
);
|
||||
goto close;
|
||||
}
|
||||
++failed;
|
||||
goto again;
|
||||
}
|
||||
|
||||
failed = 0;
|
||||
}
|
||||
|
||||
addr += len;
|
||||
offset += len;
|
||||
|
||||
fprintf(diag,
|
||||
"\rWrote %saddress 0x%08x (%.2f%%) ",
|
||||
verify ? "and verified " : "",
|
||||
addr,
|
||||
(100.0f / size) * offset
|
||||
);
|
||||
fflush(diag);
|
||||
|
||||
if( len < reqlen) /* Last read already reached EOF */
|
||||
break ;
|
||||
}
|
||||
|
||||
fprintf(diag, "Done.\n");
|
||||
ret = 0;
|
||||
goto close;
|
||||
} else if (action == ACT_CRC) {
|
||||
uint32_t crc_val = 0;
|
||||
|
||||
fprintf(diag, "CRC computation\n");
|
||||
|
||||
s_err = stm32_crc_wrapper(stm, start, end - start, &crc_val);
|
||||
if (s_err != STM32_ERR_OK) {
|
||||
fprintf(stderr, "Failed to read CRC\n");
|
||||
goto close;
|
||||
}
|
||||
fprintf(diag, "CRC(0x%08x-0x%08x) = 0x%08x\n", start, end,
|
||||
crc_val);
|
||||
ret = 0;
|
||||
goto close;
|
||||
} else
|
||||
ret = 0;
|
||||
|
||||
close:
|
||||
if (stm && exec_flag && ret == 0) {
|
||||
if (execute == 0)
|
||||
execute = stm->dev->fl_start;
|
||||
|
||||
fprintf(diag, "\nStarting execution at address 0x%08x... ", execute);
|
||||
fflush(diag);
|
||||
if (stm32_go(stm, execute) == STM32_ERR_OK) {
|
||||
reset_flag = 0;
|
||||
fprintf(diag, "done.\n");
|
||||
} else
|
||||
fprintf(diag, "failed.\n");
|
||||
}
|
||||
|
||||
if (stm && reset_flag) {
|
||||
fprintf(diag, "\nResetting device... \n");
|
||||
fflush(diag);
|
||||
if (init_bl_exit(stm, port, gpio_seq)) {
|
||||
ret = 1;
|
||||
fprintf(diag, "Reset failed.\n");
|
||||
} else
|
||||
fprintf(diag, "Reset done.\n");
|
||||
} else if (port) {
|
||||
/* Always run exit sequence if present */
|
||||
if (gpio_seq && strchr(gpio_seq, ':'))
|
||||
ret = gpio_bl_exit(port, gpio_seq) || ret;
|
||||
}
|
||||
|
||||
if (p_st ) parser->close(p_st);
|
||||
if (stm ) stm32_close (stm);
|
||||
if (port)
|
||||
port->close(port);
|
||||
|
||||
fprintf(diag, "\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
int parse_options(int argc, char *argv[])
|
||||
{
|
||||
int c;
|
||||
char *pLen;
|
||||
|
||||
while ((c = getopt(argc, argv, "a:b:m:r:w:e:vn:g:jkfcChuos:S:F:i:R")) != -1) {
|
||||
switch(c) {
|
||||
case 'a':
|
||||
port_opts.bus_addr = strtoul(optarg, NULL, 0);
|
||||
break;
|
||||
|
||||
case 'b':
|
||||
port_opts.baudRate = serial_get_baud(strtoul(optarg, NULL, 0));
|
||||
if (port_opts.baudRate == SERIAL_BAUD_INVALID) {
|
||||
serial_baud_t baudrate;
|
||||
fprintf(stderr, "Invalid baud rate, valid options are:\n");
|
||||
for (baudrate = SERIAL_BAUD_1200; baudrate != SERIAL_BAUD_INVALID; ++baudrate)
|
||||
fprintf(stderr, " %d\n", serial_get_baud_int(baudrate));
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'm':
|
||||
if (strlen(optarg) != 3
|
||||
|| serial_get_bits(optarg) == SERIAL_BITS_INVALID
|
||||
|| serial_get_parity(optarg) == SERIAL_PARITY_INVALID
|
||||
|| serial_get_stopbit(optarg) == SERIAL_STOPBIT_INVALID) {
|
||||
fprintf(stderr, "Invalid serial mode\n");
|
||||
return 1;
|
||||
}
|
||||
port_opts.serial_mode = optarg;
|
||||
break;
|
||||
|
||||
case 'r':
|
||||
case 'w':
|
||||
if (action != ACT_NONE) {
|
||||
err_multi_action((c == 'r') ? ACT_READ : ACT_WRITE);
|
||||
return 1;
|
||||
}
|
||||
action = (c == 'r') ? ACT_READ : ACT_WRITE;
|
||||
filename = optarg;
|
||||
if (filename[0] == '-' && filename[1] == '\0') {
|
||||
use_stdinout = 1;
|
||||
force_binary = 1;
|
||||
}
|
||||
break;
|
||||
case 'e':
|
||||
if (readwrite_len || start_addr) {
|
||||
fprintf(stderr, "ERROR: Invalid options, can't specify start page / num pages and start address/length\n");
|
||||
return 1;
|
||||
}
|
||||
npages = strtoul(optarg, NULL, 0);
|
||||
if (npages > STM32_MAX_PAGES || npages < 0) {
|
||||
fprintf(stderr, "ERROR: You need to specify a page count between 0 and 0xffff");
|
||||
return 1;
|
||||
}
|
||||
if (!npages)
|
||||
no_erase = 1;
|
||||
break;
|
||||
case 'u':
|
||||
if (action != ACT_NONE) {
|
||||
err_multi_action(ACT_WRITE_UNPROTECT);
|
||||
return 1;
|
||||
}
|
||||
action = ACT_WRITE_UNPROTECT;
|
||||
break;
|
||||
|
||||
case 'j':
|
||||
if (action != ACT_NONE) {
|
||||
err_multi_action(ACT_READ_PROTECT);
|
||||
return 1;
|
||||
}
|
||||
action = ACT_READ_PROTECT;
|
||||
break;
|
||||
|
||||
case 'k':
|
||||
if (action != ACT_NONE) {
|
||||
err_multi_action(ACT_READ_UNPROTECT);
|
||||
return 1;
|
||||
}
|
||||
action = ACT_READ_UNPROTECT;
|
||||
break;
|
||||
|
||||
case 'o':
|
||||
if (action != ACT_NONE) {
|
||||
err_multi_action(ACT_ERASE_ONLY);
|
||||
return 1;
|
||||
}
|
||||
action = ACT_ERASE_ONLY;
|
||||
break;
|
||||
|
||||
case 'v':
|
||||
verify = 1;
|
||||
break;
|
||||
|
||||
case 'n':
|
||||
retry = strtoul(optarg, NULL, 0);
|
||||
break;
|
||||
|
||||
case 'g':
|
||||
exec_flag = 1;
|
||||
execute = strtoul(optarg, NULL, 0);
|
||||
if (execute % 4 != 0) {
|
||||
fprintf(stderr, "ERROR: Execution address must be word-aligned\n");
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
case 's':
|
||||
if (readwrite_len || start_addr) {
|
||||
fprintf(stderr, "ERROR: Invalid options, can't specify start page / num pages and start address/length\n");
|
||||
return 1;
|
||||
}
|
||||
spage = strtoul(optarg, NULL, 0);
|
||||
break;
|
||||
case 'S':
|
||||
if (spage || npages) {
|
||||
fprintf(stderr, "ERROR: Invalid options, can't specify start page / num pages and start address/length\n");
|
||||
return 1;
|
||||
} else {
|
||||
start_addr = strtoul(optarg, &pLen, 0);
|
||||
if (start_addr % 4 != 0) {
|
||||
fprintf(stderr, "ERROR: Start address must be word-aligned\n");
|
||||
return 1;
|
||||
}
|
||||
/* we decode 0 as 1 (which is unaligned and thus invalid anyway)
|
||||
* to flag that it was set by the user */
|
||||
if (pLen != optarg && start_addr == 0)
|
||||
start_addr = 1;
|
||||
if (*pLen == ':') {
|
||||
pLen++;
|
||||
readwrite_len = strtoul(pLen, NULL, 0);
|
||||
if (readwrite_len == 0) {
|
||||
fprintf(stderr, "ERROR: Invalid options, can't specify zero length\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'F':
|
||||
port_opts.rx_frame_max = strtoul(optarg, &pLen, 0);
|
||||
if (*pLen == ':') {
|
||||
pLen++;
|
||||
port_opts.tx_frame_max = strtoul(pLen, NULL, 0);
|
||||
}
|
||||
if (port_opts.rx_frame_max < 0
|
||||
|| port_opts.tx_frame_max < 0) {
|
||||
fprintf(stderr, "ERROR: Invalid negative value for option -F\n");
|
||||
return 1;
|
||||
}
|
||||
if (port_opts.rx_frame_max == 0)
|
||||
port_opts.rx_frame_max = STM32_MAX_RX_FRAME;
|
||||
if (port_opts.tx_frame_max == 0)
|
||||
port_opts.tx_frame_max = STM32_MAX_TX_FRAME;
|
||||
if (port_opts.rx_frame_max < 20
|
||||
|| port_opts.tx_frame_max < 6) {
|
||||
fprintf(stderr, "ERROR: current code cannot work with small frames.\n");
|
||||
fprintf(stderr, "min(RX) = 20, min(TX) = 6\n");
|
||||
return 1;
|
||||
}
|
||||
if (port_opts.rx_frame_max > STM32_MAX_RX_FRAME) {
|
||||
fprintf(stderr, "WARNING: Ignore RX length in option -F\n");
|
||||
port_opts.rx_frame_max = STM32_MAX_RX_FRAME;
|
||||
}
|
||||
if (port_opts.tx_frame_max > STM32_MAX_TX_FRAME) {
|
||||
fprintf(stderr, "WARNING: Ignore TX length in option -F\n");
|
||||
port_opts.tx_frame_max = STM32_MAX_TX_FRAME;
|
||||
}
|
||||
break;
|
||||
case 'f':
|
||||
force_binary = 1;
|
||||
break;
|
||||
|
||||
case 'c':
|
||||
init_flag = 0;
|
||||
break;
|
||||
|
||||
case 'h':
|
||||
show_help(argv[0]);
|
||||
exit(0);
|
||||
|
||||
case 'i':
|
||||
gpio_seq = optarg;
|
||||
break;
|
||||
|
||||
case 'R':
|
||||
reset_flag = 1;
|
||||
break;
|
||||
|
||||
case 'C':
|
||||
if (action != ACT_NONE) {
|
||||
err_multi_action(ACT_CRC);
|
||||
return 1;
|
||||
}
|
||||
action = ACT_CRC;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (c = optind; c < argc; ++c) {
|
||||
if (port_opts.device) {
|
||||
fprintf(stderr, "ERROR: Invalid parameter specified\n");
|
||||
show_help(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
port_opts.device = argv[c];
|
||||
}
|
||||
|
||||
if (port_opts.device == NULL) {
|
||||
fprintf(stderr, "ERROR: Device not specified\n");
|
||||
show_help(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ((action != ACT_WRITE) && verify) {
|
||||
fprintf(stderr, "ERROR: Invalid usage, -v is only valid when writing\n");
|
||||
show_help(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void show_help(char *name) {
|
||||
fprintf(stderr,
|
||||
"Usage: %s [-bvngfhc] [-[rw] filename] [tty_device | i2c_device]\n"
|
||||
" -a bus_address Bus address (e.g. for I2C port)\n"
|
||||
" -b rate Baud rate (default 57600)\n"
|
||||
" -m mode Serial port mode (default 8e1)\n"
|
||||
" -r filename Read flash to file (or - stdout)\n"
|
||||
" -w filename Write flash from file (or - stdout)\n"
|
||||
" -C Compute CRC of flash content\n"
|
||||
" -u Disable the flash write-protection\n"
|
||||
" -j Enable the flash read-protection\n"
|
||||
" -k Disable the flash read-protection\n"
|
||||
" -o Erase only\n"
|
||||
" -e n Only erase n pages before writing the flash\n"
|
||||
" -v Verify writes\n"
|
||||
" -n count Retry failed writes up to count times (default 10)\n"
|
||||
" -g address Start execution at specified address (0 = flash start)\n"
|
||||
" -S address[:length] Specify start address and optionally length for\n"
|
||||
" read/write/erase operations\n"
|
||||
" -F RX_length[:TX_length] Specify the max length of RX and TX frame\n"
|
||||
" -s start_page Flash at specified page (0 = flash start)\n"
|
||||
" -f Force binary parser\n"
|
||||
" -h Show this help\n"
|
||||
" -c Resume the connection (don't send initial INIT)\n"
|
||||
" *Baud rate must be kept the same as the first init*\n"
|
||||
" This is useful if the reset fails\n"
|
||||
" -R Reset device at exit.\n"
|
||||
" -i GPIO_string GPIO sequence to enter/exit bootloader mode\n"
|
||||
" GPIO_string=[entry_seq][:[exit_seq]]\n"
|
||||
" sequence=[[-]signal]&|,[sequence]\n"
|
||||
"\n"
|
||||
"GPIO sequence:\n"
|
||||
" The following signals can appear in a sequence:\n"
|
||||
" Integer number representing GPIO pin\n"
|
||||
" 'dtr', 'rts' or 'brk' representing serial port signal\n"
|
||||
" The sequence can use the following delimiters:\n"
|
||||
" ',' adds 100 ms delay between signals\n"
|
||||
" '&' adds no delay between signals\n"
|
||||
" The following modifiers can be prepended to a signal:\n"
|
||||
" '-' reset signal (low) instead of setting it (high)\n"
|
||||
"\n"
|
||||
"Examples:\n"
|
||||
" Get device information:\n"
|
||||
" %s /dev/ttyS0\n"
|
||||
" or:\n"
|
||||
" %s /dev/i2c-0\n"
|
||||
"\n"
|
||||
" Write with verify and then start execution:\n"
|
||||
" %s -w filename -v -g 0x0 /dev/ttyS0\n"
|
||||
"\n"
|
||||
" Read flash to file:\n"
|
||||
" %s -r filename /dev/ttyS0\n"
|
||||
"\n"
|
||||
" Read 100 bytes of flash from 0x1000 to stdout:\n"
|
||||
" %s -r - -S 0x1000:100 /dev/ttyS0\n"
|
||||
"\n"
|
||||
" Start execution:\n"
|
||||
" %s -g 0x0 /dev/ttyS0\n"
|
||||
"\n"
|
||||
" GPIO sequence:\n"
|
||||
" - entry sequence: GPIO_3=low, GPIO_2=low, 100ms delay, GPIO_2=high\n"
|
||||
" - exit sequence: GPIO_3=high, GPIO_2=low, 300ms delay, GPIO_2=high\n"
|
||||
" %s -i '-3&-2,2:3&-2,,,2' /dev/ttyS0\n"
|
||||
" GPIO sequence adding delay after port opening:\n"
|
||||
" - entry sequence: delay 500ms\n"
|
||||
" - exit sequence: rts=high, dtr=low, 300ms delay, GPIO_2=high\n"
|
||||
" %s -R -i ',,,,,:rts&-dtr,,,2' /dev/ttyS0\n",
|
||||
name,
|
||||
name,
|
||||
name,
|
||||
name,
|
||||
name,
|
||||
name,
|
||||
name,
|
||||
name,
|
||||
name
|
||||
);
|
||||
}
|
||||
|
Binary file not shown.
|
@ -1,6 +0,0 @@
|
|||
LOCAL_PATH := $(call my-dir)
|
||||
|
||||
include $(CLEAR_VARS)
|
||||
LOCAL_MODULE := libparsers
|
||||
LOCAL_SRC_FILES := binary.c hex.c
|
||||
include $(BUILD_STATIC_LIBRARY)
|
|
@ -1,12 +0,0 @@
|
|||
|
||||
CFLAGS += -Wall -g
|
||||
|
||||
all: parsers.a
|
||||
|
||||
parsers.a: binary.o hex.o
|
||||
$(AR) rc $@ binary.o hex.o
|
||||
|
||||
clean:
|
||||
rm -f *.o parsers.a
|
||||
|
||||
.PHONY: all clean
|
|
@ -1,11 +0,0 @@
|
|||
noinst_LTLIBRARIES = parsers.la
|
||||
|
||||
|
||||
parsers_la_SOURCES = binary.h binary.c hex.h hex.c parser.h
|
||||
|
||||
parsers_la_CXXFLAGS = -Wall -g
|
||||
|
||||
|
||||
parsers_la_LDFLAGS = -module -avoid-version
|
||||
|
||||
|
|
@ -1,148 +0,0 @@
|
|||
/*
|
||||
stm32flash - Open Source ST STM32 flash program for *nix
|
||||
Copyright (C) 2010 Geoffrey McRae <geoff@spacevs.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "binary.h"
|
||||
|
||||
typedef struct {
|
||||
int fd;
|
||||
char write;
|
||||
struct stat stat;
|
||||
} binary_t;
|
||||
|
||||
void* binary_init() {
|
||||
return calloc(sizeof(binary_t), 1);
|
||||
}
|
||||
|
||||
parser_err_t binary_open(void *storage, const char *filename, const char write) {
|
||||
binary_t *st = storage;
|
||||
if (write) {
|
||||
if (filename[0] == '-' && filename[1] == '\0')
|
||||
st->fd = 1;
|
||||
else
|
||||
st->fd = open(
|
||||
filename,
|
||||
#ifndef __WIN32__
|
||||
O_WRONLY | O_CREAT | O_TRUNC,
|
||||
#else
|
||||
O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
|
||||
#endif
|
||||
#ifndef __WIN32__
|
||||
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
|
||||
#else
|
||||
0
|
||||
#endif
|
||||
);
|
||||
st->stat.st_size = 0;
|
||||
} else {
|
||||
if (filename[0] == '-' && filename[1] == '\0') {
|
||||
st->fd = 0;
|
||||
} else {
|
||||
if (stat(filename, &st->stat) != 0)
|
||||
return PARSER_ERR_INVALID_FILE;
|
||||
st->fd = open(filename,
|
||||
#ifndef __WIN32__
|
||||
O_RDONLY
|
||||
#else
|
||||
O_RDONLY | O_BINARY
|
||||
#endif
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
st->write = write;
|
||||
return st->fd == -1 ? PARSER_ERR_SYSTEM : PARSER_ERR_OK;
|
||||
}
|
||||
|
||||
parser_err_t binary_close(void *storage) {
|
||||
binary_t *st = storage;
|
||||
|
||||
if (st->fd) close(st->fd);
|
||||
free(st);
|
||||
return PARSER_ERR_OK;
|
||||
}
|
||||
|
||||
unsigned int binary_base(void *storage) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned int binary_size(void *storage) {
|
||||
binary_t *st = storage;
|
||||
return st->stat.st_size;
|
||||
}
|
||||
|
||||
parser_err_t binary_read(void *storage, void *data, unsigned int *len) {
|
||||
binary_t *st = storage;
|
||||
unsigned int left = *len;
|
||||
unsigned char *d = data;
|
||||
|
||||
if (st->write) return PARSER_ERR_WRONLY;
|
||||
|
||||
ssize_t r;
|
||||
while(left > 0) {
|
||||
r = read(st->fd, d, left);
|
||||
if (r == 0)
|
||||
break ;
|
||||
else if ( r < 0)
|
||||
return PARSER_ERR_SYSTEM;
|
||||
|
||||
left -= r;
|
||||
d += r;
|
||||
}
|
||||
|
||||
*len = *len - left;
|
||||
return PARSER_ERR_OK;
|
||||
}
|
||||
|
||||
parser_err_t binary_write(void *storage, void *data, unsigned int len) {
|
||||
binary_t *st = storage;
|
||||
unsigned char *d = data;
|
||||
|
||||
if (!st->write) return PARSER_ERR_RDONLY;
|
||||
|
||||
ssize_t r;
|
||||
while(len > 0) {
|
||||
r = write(st->fd, d, len);
|
||||
if (r < 1) return PARSER_ERR_SYSTEM;
|
||||
st->stat.st_size += r;
|
||||
|
||||
len -= r;
|
||||
d += r;
|
||||
}
|
||||
|
||||
return PARSER_ERR_OK;
|
||||
}
|
||||
|
||||
parser_t PARSER_BINARY = {
|
||||
"Raw BINARY",
|
||||
binary_init,
|
||||
binary_open,
|
||||
binary_close,
|
||||
binary_base,
|
||||
binary_size,
|
||||
binary_read,
|
||||
binary_write
|
||||
};
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
/*
|
||||
stm32flash - Open Source ST STM32 flash program for *nix
|
||||
Copyright (C) 2010 Geoffrey McRae <geoff@spacevs.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _PARSER_BINARY_H
|
||||
#define _PARSER_BINARY_H
|
||||
|
||||
#include "parser.h"
|
||||
|
||||
extern parser_t PARSER_BINARY;
|
||||
#endif
|
Binary file not shown.
|
@ -1,251 +0,0 @@
|
|||
/*
|
||||
stm32flash - Open Source ST STM32 flash program for *nix
|
||||
Copyright (C) 2010 Geoffrey McRae <geoff@spacevs.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "hex.h"
|
||||
#include "../compiler.h"
|
||||
#include "../utils.h"
|
||||
|
||||
extern FILE *diag;
|
||||
|
||||
typedef struct {
|
||||
size_t data_len, offset;
|
||||
uint8_t *data;
|
||||
uint32_t base;
|
||||
} hex_t;
|
||||
|
||||
void* hex_init() {
|
||||
return calloc(sizeof(hex_t), 1);
|
||||
}
|
||||
|
||||
parser_err_t hex_open(void *storage, const char *filename, const char write) {
|
||||
hex_t *st = storage;
|
||||
if (write) {
|
||||
return PARSER_ERR_RDONLY;
|
||||
} else {
|
||||
char mark;
|
||||
int fd;
|
||||
uint8_t checksum;
|
||||
unsigned int c, i;
|
||||
uint32_t base = 0;
|
||||
unsigned int last_address = 0x0;
|
||||
|
||||
fd = open(filename, O_RDONLY);
|
||||
if (fd < 0)
|
||||
return PARSER_ERR_SYSTEM;
|
||||
|
||||
/* read in the file */
|
||||
|
||||
while(read(fd, &mark, 1) != 0) {
|
||||
if (mark == '\n' || mark == '\r') continue;
|
||||
if (mark != ':')
|
||||
return PARSER_ERR_INVALID_FILE;
|
||||
|
||||
char buffer[9];
|
||||
unsigned int reclen, address, type;
|
||||
uint8_t *record = NULL;
|
||||
|
||||
/* get the reclen, address, and type */
|
||||
buffer[8] = 0;
|
||||
if (read(fd, &buffer, 8) != 8) return PARSER_ERR_INVALID_FILE;
|
||||
if (sscanf(buffer, "%2x%4x%2x", &reclen, &address, &type) != 3) {
|
||||
close(fd);
|
||||
return PARSER_ERR_INVALID_FILE;
|
||||
}
|
||||
|
||||
/* setup the checksum */
|
||||
checksum =
|
||||
reclen +
|
||||
((address & 0xFF00) >> 8) +
|
||||
((address & 0x00FF) >> 0) +
|
||||
type;
|
||||
|
||||
switch(type) {
|
||||
/* data record */
|
||||
case 0:
|
||||
if (st->data_len == 0) {
|
||||
st->base |= address;
|
||||
last_address = address;
|
||||
}
|
||||
|
||||
c = address - last_address;
|
||||
st->data = realloc(st->data, st->data_len + c + reclen);
|
||||
|
||||
/* if there is a gap, set it to 0xff and increment the length */
|
||||
if (c > 0) {
|
||||
memset(&st->data[st->data_len], 0xff, c);
|
||||
st->data_len += c;
|
||||
}
|
||||
|
||||
last_address = address + reclen;
|
||||
record = &st->data[st->data_len];
|
||||
st->data_len += reclen;
|
||||
break;
|
||||
|
||||
/* extended segment address record */
|
||||
case 2:
|
||||
base = 0;
|
||||
break;
|
||||
|
||||
/* extended linear address record */
|
||||
case 4:
|
||||
base = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
buffer[2] = 0;
|
||||
for(i = 0; i < reclen; ++i) {
|
||||
if (read(fd, &buffer, 2) != 2 || sscanf(buffer, "%2x", &c) != 1) {
|
||||
close(fd);
|
||||
return PARSER_ERR_INVALID_FILE;
|
||||
}
|
||||
|
||||
/* add the byte to the checksum */
|
||||
checksum += c;
|
||||
|
||||
switch(type) {
|
||||
case 0:
|
||||
if (record != NULL) {
|
||||
record[i] = c;
|
||||
} else {
|
||||
return PARSER_ERR_INVALID_FILE;
|
||||
}
|
||||
break;
|
||||
|
||||
case 2:
|
||||
case 4:
|
||||
base = (base << 8) | c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* read, scan, and verify the checksum */
|
||||
if (
|
||||
read(fd, &buffer, 2 ) != 2 ||
|
||||
sscanf(buffer, "%2x", &c) != 1 ||
|
||||
(uint8_t)(checksum + c) != 0x00
|
||||
) {
|
||||
close(fd);
|
||||
return PARSER_ERR_INVALID_FILE;
|
||||
}
|
||||
|
||||
switch(type) {
|
||||
/* EOF */
|
||||
case 1:
|
||||
close(fd);
|
||||
return PARSER_ERR_OK;
|
||||
|
||||
/* address record */
|
||||
case 4: base = base << 12;
|
||||
/* fall-through */
|
||||
case 2: base = base << 4;
|
||||
/* Reset last_address since our base changed */
|
||||
last_address = 0;
|
||||
|
||||
/* Only assign the program's base address once, and only
|
||||
* do so if we haven't seen any data records yet.
|
||||
* If there are any data records before address records,
|
||||
* the program's base address must be zero.
|
||||
*/
|
||||
if (st->base == 0 && st->data_len == 0) {
|
||||
st->base = base;
|
||||
break;
|
||||
}
|
||||
|
||||
/* we cant cope with files out of order */
|
||||
if (base < st->base) {
|
||||
close(fd);
|
||||
return PARSER_ERR_INVALID_FILE;
|
||||
}
|
||||
|
||||
/* if there is a gap, enlarge and fill with 0xff */
|
||||
size_t len = base - st->base;
|
||||
if (len > st->data_len) {
|
||||
size_t gap = len - st->data_len;
|
||||
if (gap > 16384) /* arbitrary limit for warning */
|
||||
fprintf(diag, "Warning: Filling gap of %zu bytes at 0x%08zx\n", gap, st->base + st->data_len);
|
||||
st->data = realloc(st->data, len);
|
||||
if (st->data == NULL) {
|
||||
fprintf(diag, "Error: Cannot reallocate memory\n");
|
||||
return PARSER_ERR_SYSTEM;
|
||||
}
|
||||
memset(&st->data[st->data_len], 0xff, gap);
|
||||
st->data_len = len;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return PARSER_ERR_OK;
|
||||
}
|
||||
}
|
||||
|
||||
parser_err_t hex_close(void *storage) {
|
||||
hex_t *st = storage;
|
||||
if (st) free(st->data);
|
||||
free(st);
|
||||
return PARSER_ERR_OK;
|
||||
}
|
||||
|
||||
unsigned int hex_base(void *storage) {
|
||||
hex_t *st = storage;
|
||||
return st->base;
|
||||
}
|
||||
|
||||
unsigned int hex_size(void *storage) {
|
||||
hex_t *st = storage;
|
||||
return st->data_len;
|
||||
}
|
||||
|
||||
parser_err_t hex_read(void *storage, void *data, unsigned int *len) {
|
||||
hex_t *st = storage;
|
||||
unsigned int left = st->data_len - st->offset;
|
||||
unsigned int get = left > *len ? *len : left;
|
||||
|
||||
memcpy(data, &st->data[st->offset], get);
|
||||
st->offset += get;
|
||||
|
||||
*len = get;
|
||||
return PARSER_ERR_OK;
|
||||
}
|
||||
|
||||
parser_err_t hex_write(void __unused *storage, void __unused *data, unsigned int __unused len) {
|
||||
return PARSER_ERR_RDONLY;
|
||||
}
|
||||
|
||||
parser_t PARSER_HEX = {
|
||||
"Intel HEX",
|
||||
hex_init,
|
||||
hex_open,
|
||||
hex_close,
|
||||
hex_base,
|
||||
hex_size,
|
||||
hex_read,
|
||||
hex_write
|
||||
};
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
/*
|
||||
stm32flash - Open Source ST STM32 flash program for *nix
|
||||
Copyright (C) 2010 Geoffrey McRae <geoff@spacevs.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _PARSER_HEX_H
|
||||
#define _PARSER_HEX_H
|
||||
|
||||
#include "parser.h"
|
||||
|
||||
extern parser_t PARSER_HEX;
|
||||
#endif
|
Binary file not shown.
|
@ -1,57 +0,0 @@
|
|||
/*
|
||||
stm32flash - Open Source ST STM32 flash program for *nix
|
||||
Copyright (C) 2010 Geoffrey McRae <geoff@spacevs.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _H_PARSER
|
||||
#define _H_PARSER
|
||||
|
||||
enum parser_err {
|
||||
PARSER_ERR_OK,
|
||||
PARSER_ERR_SYSTEM,
|
||||
PARSER_ERR_INVALID_FILE,
|
||||
PARSER_ERR_WRONLY,
|
||||
PARSER_ERR_RDONLY
|
||||
};
|
||||
typedef enum parser_err parser_err_t;
|
||||
|
||||
struct parser {
|
||||
const char *name;
|
||||
void* (*init )(); /* initialise the parser */
|
||||
parser_err_t (*open )(void *storage, const char *filename, const char write); /* open the file for read|write */
|
||||
parser_err_t (*close)(void *storage); /* close and free the parser */
|
||||
unsigned int (*base )(void *storage); /* get the base address */
|
||||
unsigned int (*size )(void *storage); /* get the total data size */
|
||||
parser_err_t (*read )(void *storage, void *data, unsigned int *len); /* read a block of data */
|
||||
parser_err_t (*write)(void *storage, void *data, unsigned int len); /* write a block of data */
|
||||
};
|
||||
typedef struct parser parser_t;
|
||||
|
||||
static inline const char* parser_errstr(parser_err_t err) {
|
||||
switch(err) {
|
||||
case PARSER_ERR_OK : return "OK";
|
||||
case PARSER_ERR_SYSTEM : return "System Error";
|
||||
case PARSER_ERR_INVALID_FILE: return "Invalid File";
|
||||
case PARSER_ERR_WRONLY : return "Parser can only write";
|
||||
case PARSER_ERR_RDONLY : return "Parser can only read";
|
||||
default:
|
||||
return "Unknown Error";
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
Binary file not shown.
|
@ -1,61 +0,0 @@
|
|||
/*
|
||||
stm32flash - Open Source ST STM32 flash program for *nix
|
||||
Copyright (C) 2014 Antonio Borneo <borneo.antonio@gmail.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "serial.h"
|
||||
#include "port.h"
|
||||
|
||||
|
||||
extern struct port_interface port_serial;
|
||||
extern struct port_interface port_i2c;
|
||||
|
||||
static struct port_interface *ports[] = {
|
||||
&port_serial,
|
||||
&port_i2c,
|
||||
NULL,
|
||||
};
|
||||
|
||||
|
||||
port_err_t port_open(struct port_options *ops, struct port_interface **outport)
|
||||
{
|
||||
int ret;
|
||||
static struct port_interface **port;
|
||||
|
||||
for (port = ports; *port; port++) {
|
||||
ret = (*port)->open(*port, ops);
|
||||
if (ret == PORT_ERR_NODEV)
|
||||
continue;
|
||||
if (ret == PORT_ERR_OK)
|
||||
break;
|
||||
if (ret == PORT_ERR_BAUD)
|
||||
fprintf(stderr, "Unsupported baud rate\n");
|
||||
fprintf(stderr, "Error probing interface \"%s\"\n",
|
||||
(*port)->name);
|
||||
}
|
||||
if (*port == NULL) {
|
||||
fprintf(stderr, "Cannot handle device \"%s\"\n",
|
||||
ops->device);
|
||||
return PORT_ERR_UNKNOWN;
|
||||
}
|
||||
|
||||
*outport = *port;
|
||||
return PORT_ERR_OK;
|
||||
}
|
|
@ -1,78 +0,0 @@
|
|||
/*
|
||||
stm32flash - Open Source ST STM32 flash program for *nix
|
||||
Copyright (C) 2014 Antonio Borneo <borneo.antonio@gmail.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _H_PORT
|
||||
#define _H_PORT
|
||||
|
||||
typedef enum {
|
||||
PORT_ERR_OK = 0,
|
||||
PORT_ERR_NODEV, /* No such device */
|
||||
PORT_ERR_TIMEDOUT, /* Operation timed out */
|
||||
PORT_ERR_BAUD, /* Unsupported baud rate */
|
||||
PORT_ERR_UNKNOWN,
|
||||
} port_err_t;
|
||||
|
||||
/* flags */
|
||||
#define PORT_BYTE (1 << 0) /* byte (not frame) oriented */
|
||||
#define PORT_GVR_ETX (1 << 1) /* cmd GVR returns protection status */
|
||||
#define PORT_CMD_INIT (1 << 2) /* use INIT cmd to autodetect speed */
|
||||
#define PORT_RETRY (1 << 3) /* allowed read() retry after timeout */
|
||||
#define PORT_STRETCH_W (1 << 4) /* warning for no-stretching commands */
|
||||
#define PORT_NPAG_CSUM (1 << 5) /* checksum after number of pages to erase */
|
||||
|
||||
/* all options and flags used to open and configure an interface */
|
||||
struct port_options {
|
||||
const char *device;
|
||||
serial_baud_t baudRate;
|
||||
const char *serial_mode;
|
||||
int bus_addr;
|
||||
int rx_frame_max;
|
||||
int tx_frame_max;
|
||||
};
|
||||
|
||||
/*
|
||||
* Specify the length of reply for command GET
|
||||
* This is helpful for frame-oriented protocols, e.g. i2c, to avoid time
|
||||
* consuming try-fail-timeout-retry operation.
|
||||
* On byte-oriented protocols, i.e. UART, this information would be skipped
|
||||
* after read the first byte, so not needed.
|
||||
*/
|
||||
struct varlen_cmd {
|
||||
uint8_t version;
|
||||
uint8_t length;
|
||||
};
|
||||
|
||||
struct port_interface {
|
||||
const char *name;
|
||||
unsigned flags;
|
||||
port_err_t (*open)(struct port_interface *port, struct port_options *ops);
|
||||
port_err_t (*close)(struct port_interface *port);
|
||||
port_err_t (*flush)(struct port_interface *port);
|
||||
port_err_t (*read)(struct port_interface *port, void *buf, size_t nbyte);
|
||||
port_err_t (*write)(struct port_interface *port, void *buf, size_t nbyte);
|
||||
port_err_t (*gpio)(struct port_interface *port, serial_gpio_t n, int level);
|
||||
const char *(*get_cfg_str)(struct port_interface *port);
|
||||
struct varlen_cmd *cmd_get_reply;
|
||||
void *private;
|
||||
};
|
||||
|
||||
port_err_t port_open(struct port_options *ops, struct port_interface **outport);
|
||||
|
||||
#endif
|
Binary file not shown.
|
@ -1,19 +0,0 @@
|
|||
The communication protocol used by ST bootloader is documented in following ST
|
||||
application notes, depending on communication port.
|
||||
|
||||
In current version of stm32flash are supported only UART and I2C ports.
|
||||
|
||||
* AN3154: CAN protocol used in the STM32 bootloader
|
||||
http://www.st.com/web/en/resource/technical/document/application_note/CD00264321.pdf
|
||||
|
||||
* AN3155: USART protocol used in the STM32(TM) bootloader
|
||||
http://www.st.com/web/en/resource/technical/document/application_note/CD00264342.pdf
|
||||
|
||||
* AN4221: I2C protocol used in the STM32 bootloader
|
||||
http://www.st.com/web/en/resource/technical/document/application_note/DM00072315.pdf
|
||||
|
||||
* AN4286: SPI protocol used in the STM32 bootloader
|
||||
http://www.st.com/web/en/resource/technical/document/application_note/DM00081379.pdf
|
||||
|
||||
Boot mode selection for STM32 is documented in ST application note AN2606, available in ST website:
|
||||
http://www.st.com/web/en/resource/technical/document/application_note/CD00167594.pdf
|
|
@ -1,98 +0,0 @@
|
|||
/*
|
||||
stm32flash - Open Source ST STM32 flash program for *nix
|
||||
Copyright (C) 2010 Geoffrey McRae <geoff@spacevs.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _SERIAL_H
|
||||
#define _SERIAL_H
|
||||
|
||||
typedef struct serial serial_t;
|
||||
|
||||
typedef enum {
|
||||
SERIAL_PARITY_NONE,
|
||||
SERIAL_PARITY_EVEN,
|
||||
SERIAL_PARITY_ODD,
|
||||
|
||||
SERIAL_PARITY_INVALID
|
||||
} serial_parity_t;
|
||||
|
||||
typedef enum {
|
||||
SERIAL_BITS_5,
|
||||
SERIAL_BITS_6,
|
||||
SERIAL_BITS_7,
|
||||
SERIAL_BITS_8,
|
||||
|
||||
SERIAL_BITS_INVALID
|
||||
} serial_bits_t;
|
||||
|
||||
typedef enum {
|
||||
SERIAL_BAUD_1200,
|
||||
SERIAL_BAUD_1800,
|
||||
SERIAL_BAUD_2400,
|
||||
SERIAL_BAUD_4800,
|
||||
SERIAL_BAUD_9600,
|
||||
SERIAL_BAUD_14400,
|
||||
SERIAL_BAUD_19200,
|
||||
SERIAL_BAUD_38400,
|
||||
SERIAL_BAUD_56000,
|
||||
SERIAL_BAUD_57600,
|
||||
SERIAL_BAUD_115200,
|
||||
SERIAL_BAUD_128000,
|
||||
SERIAL_BAUD_230400,
|
||||
SERIAL_BAUD_256000,
|
||||
SERIAL_BAUD_460800,
|
||||
SERIAL_BAUD_500000,
|
||||
SERIAL_BAUD_576000,
|
||||
SERIAL_BAUD_921600,
|
||||
SERIAL_BAUD_1000000,
|
||||
SERIAL_BAUD_1152000,
|
||||
SERIAL_BAUD_1500000,
|
||||
SERIAL_BAUD_2000000,
|
||||
SERIAL_BAUD_2500000,
|
||||
SERIAL_BAUD_3000000,
|
||||
SERIAL_BAUD_3500000,
|
||||
SERIAL_BAUD_4000000,
|
||||
|
||||
SERIAL_BAUD_INVALID,
|
||||
SERIAL_BAUD_KEEP
|
||||
} serial_baud_t;
|
||||
|
||||
typedef enum {
|
||||
SERIAL_STOPBIT_1,
|
||||
SERIAL_STOPBIT_2,
|
||||
|
||||
SERIAL_STOPBIT_INVALID
|
||||
} serial_stopbit_t;
|
||||
|
||||
typedef enum {
|
||||
GPIO_RTS = 1,
|
||||
GPIO_DTR,
|
||||
GPIO_BRK,
|
||||
} serial_gpio_t;
|
||||
|
||||
/* common helper functions */
|
||||
serial_baud_t serial_get_baud(const unsigned int baud);
|
||||
unsigned int serial_get_baud_int(const serial_baud_t baud);
|
||||
serial_bits_t serial_get_bits(const char *mode);
|
||||
unsigned int serial_get_bits_int(const serial_bits_t bits);
|
||||
serial_parity_t serial_get_parity(const char *mode);
|
||||
char serial_get_parity_str(const serial_parity_t parity);
|
||||
serial_stopbit_t serial_get_stopbit(const char *mode);
|
||||
unsigned int serial_get_stopbit_int(const serial_stopbit_t stopbit);
|
||||
|
||||
#endif
|
|
@ -1,169 +0,0 @@
|
|||
/*
|
||||
stm32flash - Open Source ST STM32 flash program for *nix
|
||||
Copyright (C) 2010 Geoffrey McRae <geoff@spacevs.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include "serial.h"
|
||||
|
||||
serial_baud_t serial_get_baud(const unsigned int baud) {
|
||||
switch(baud) {
|
||||
case 0: return SERIAL_BAUD_KEEP ;
|
||||
case 1200: return SERIAL_BAUD_1200 ;
|
||||
case 1800: return SERIAL_BAUD_1800 ;
|
||||
case 2400: return SERIAL_BAUD_2400 ;
|
||||
case 4800: return SERIAL_BAUD_4800 ;
|
||||
case 9600: return SERIAL_BAUD_9600 ;
|
||||
case 14400: return SERIAL_BAUD_14400 ;
|
||||
case 19200: return SERIAL_BAUD_19200 ;
|
||||
case 38400: return SERIAL_BAUD_38400 ;
|
||||
case 56000: return SERIAL_BAUD_56000 ;
|
||||
case 57600: return SERIAL_BAUD_57600 ;
|
||||
case 115200: return SERIAL_BAUD_115200;
|
||||
case 128000: return SERIAL_BAUD_128000;
|
||||
case 230400: return SERIAL_BAUD_230400;
|
||||
case 256000: return SERIAL_BAUD_256000;
|
||||
case 460800: return SERIAL_BAUD_460800;
|
||||
case 500000: return SERIAL_BAUD_500000;
|
||||
case 576000: return SERIAL_BAUD_576000;
|
||||
case 921600: return SERIAL_BAUD_921600;
|
||||
case 1000000: return SERIAL_BAUD_1000000;
|
||||
case 1152000: return SERIAL_BAUD_1152000;
|
||||
case 1500000: return SERIAL_BAUD_1500000;
|
||||
case 2000000: return SERIAL_BAUD_2000000;
|
||||
case 2500000: return SERIAL_BAUD_2500000;
|
||||
case 3000000: return SERIAL_BAUD_3000000;
|
||||
case 3500000: return SERIAL_BAUD_3500000;
|
||||
case 4000000: return SERIAL_BAUD_4000000;
|
||||
|
||||
default:
|
||||
return SERIAL_BAUD_INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int serial_get_baud_int(const serial_baud_t baud) {
|
||||
switch(baud) {
|
||||
case SERIAL_BAUD_1200 : return 1200 ;
|
||||
case SERIAL_BAUD_1800 : return 1800 ;
|
||||
case SERIAL_BAUD_2400 : return 2400 ;
|
||||
case SERIAL_BAUD_4800 : return 4800 ;
|
||||
case SERIAL_BAUD_9600 : return 9600 ;
|
||||
case SERIAL_BAUD_14400 : return 14400 ;
|
||||
case SERIAL_BAUD_19200 : return 19200 ;
|
||||
case SERIAL_BAUD_38400 : return 38400 ;
|
||||
case SERIAL_BAUD_56000 : return 56000 ;
|
||||
case SERIAL_BAUD_57600 : return 57600 ;
|
||||
case SERIAL_BAUD_115200: return 115200;
|
||||
case SERIAL_BAUD_128000: return 128000;
|
||||
case SERIAL_BAUD_230400: return 230400;
|
||||
case SERIAL_BAUD_256000: return 256000;
|
||||
case SERIAL_BAUD_460800: return 460800;
|
||||
case SERIAL_BAUD_500000: return 500000;
|
||||
case SERIAL_BAUD_576000: return 576000;
|
||||
case SERIAL_BAUD_921600: return 921600;
|
||||
case SERIAL_BAUD_1000000: return 1000000;
|
||||
case SERIAL_BAUD_1152000: return 1152000;
|
||||
case SERIAL_BAUD_1500000: return 1500000;
|
||||
case SERIAL_BAUD_2000000: return 2000000;
|
||||
case SERIAL_BAUD_2500000: return 2500000;
|
||||
case SERIAL_BAUD_3000000: return 3000000;
|
||||
case SERIAL_BAUD_3500000: return 3500000;
|
||||
case SERIAL_BAUD_4000000: return 4000000;
|
||||
|
||||
case SERIAL_BAUD_INVALID:
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
serial_bits_t serial_get_bits(const char *mode) {
|
||||
if (!mode)
|
||||
return SERIAL_BITS_INVALID;
|
||||
switch(mode[0]) {
|
||||
case '5': return SERIAL_BITS_5;
|
||||
case '6': return SERIAL_BITS_6;
|
||||
case '7': return SERIAL_BITS_7;
|
||||
case '8': return SERIAL_BITS_8;
|
||||
|
||||
default:
|
||||
return SERIAL_BITS_INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int serial_get_bits_int(const serial_bits_t bits) {
|
||||
switch(bits) {
|
||||
case SERIAL_BITS_5: return 5;
|
||||
case SERIAL_BITS_6: return 6;
|
||||
case SERIAL_BITS_7: return 7;
|
||||
case SERIAL_BITS_8: return 8;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
serial_parity_t serial_get_parity(const char *mode) {
|
||||
if (!mode || !mode[0])
|
||||
return SERIAL_PARITY_INVALID;
|
||||
switch(mode[1]) {
|
||||
case 'N':
|
||||
case 'n':
|
||||
return SERIAL_PARITY_NONE;
|
||||
case 'E':
|
||||
case 'e':
|
||||
return SERIAL_PARITY_EVEN;
|
||||
case 'O':
|
||||
case 'o':
|
||||
return SERIAL_PARITY_ODD;
|
||||
|
||||
default:
|
||||
return SERIAL_PARITY_INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
char serial_get_parity_str(const serial_parity_t parity) {
|
||||
switch(parity) {
|
||||
case SERIAL_PARITY_NONE: return 'N';
|
||||
case SERIAL_PARITY_EVEN: return 'E';
|
||||
case SERIAL_PARITY_ODD : return 'O';
|
||||
|
||||
default:
|
||||
return ' ';
|
||||
}
|
||||
}
|
||||
|
||||
serial_stopbit_t serial_get_stopbit(const char *mode) {
|
||||
if (!mode || !mode[0] || !mode[1])
|
||||
return SERIAL_STOPBIT_INVALID;
|
||||
switch(mode[2]) {
|
||||
case '1': return SERIAL_STOPBIT_1;
|
||||
case '2': return SERIAL_STOPBIT_2;
|
||||
|
||||
default:
|
||||
return SERIAL_STOPBIT_INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int serial_get_stopbit_int(const serial_stopbit_t stopbit) {
|
||||
switch(stopbit) {
|
||||
case SERIAL_STOPBIT_1: return 1;
|
||||
case SERIAL_STOPBIT_2: return 2;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
Binary file not shown.
|
@ -1,5 +0,0 @@
|
|||
#if defined(__WIN32__) || defined(__CYGWIN__)
|
||||
# include "serial_w32.c"
|
||||
#else
|
||||
# include "serial_posix.c"
|
||||
#endif
|
Binary file not shown.
|
@ -1,448 +0,0 @@
|
|||
/*
|
||||
stm32flash - Open Source ST STM32 flash program for *nix
|
||||
Copyright (C) 2010 Geoffrey McRae <geoff@spacevs.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/file.h>
|
||||
|
||||
/* To detect BSD for baud rate encoding */
|
||||
#if defined(__unix__) || defined(__UNIX__) || (defined(__APPLE__) && defined(__MACH__))
|
||||
# include <sys/param.h>
|
||||
#endif
|
||||
|
||||
#include "serial.h"
|
||||
#include "port.h"
|
||||
|
||||
#ifndef TERMIOS_TIMEOUT_MS
|
||||
#define TERMIOS_TIMEOUT_MS 500
|
||||
#endif
|
||||
|
||||
#define TERMIOS_TIMEOUT ((TERMIOS_TIMEOUT_MS)/100)
|
||||
|
||||
struct serial {
|
||||
int fd;
|
||||
struct termios oldtio;
|
||||
struct termios newtio;
|
||||
char setup_str[11];
|
||||
};
|
||||
|
||||
static serial_t *serial_open(const char *device)
|
||||
{
|
||||
serial_t *h = calloc(sizeof(serial_t), 1);
|
||||
|
||||
h->fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
|
||||
if (h->fd < 0) {
|
||||
free(h);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(lockf(h->fd,F_TLOCK,0) != 0)
|
||||
{
|
||||
fprintf(stderr, "Error: %s is already open\n", device);
|
||||
free(h);
|
||||
return NULL;
|
||||
}
|
||||
fcntl(h->fd, F_SETFL, 0);
|
||||
|
||||
tcgetattr(h->fd, &h->oldtio);
|
||||
tcgetattr(h->fd, &h->newtio);
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
static void serial_flush(const serial_t *h)
|
||||
{
|
||||
tcflush(h->fd, TCIFLUSH);
|
||||
}
|
||||
|
||||
static void serial_close(serial_t *h)
|
||||
{
|
||||
serial_flush(h);
|
||||
tcsetattr(h->fd, TCSANOW, &h->oldtio);
|
||||
lockf(h->fd, F_ULOCK, 0);
|
||||
close(h->fd);
|
||||
free(h);
|
||||
}
|
||||
|
||||
static port_err_t serial_setup(serial_t *h, const serial_baud_t baud,
|
||||
const serial_bits_t bits,
|
||||
const serial_parity_t parity,
|
||||
const serial_stopbit_t stopbit)
|
||||
{
|
||||
speed_t port_baud;
|
||||
tcflag_t port_bits;
|
||||
tcflag_t port_parity;
|
||||
tcflag_t port_stop;
|
||||
struct termios settings;
|
||||
|
||||
switch (baud) {
|
||||
case SERIAL_BAUD_1200: port_baud = B1200; break;
|
||||
case SERIAL_BAUD_1800: port_baud = B1800; break;
|
||||
case SERIAL_BAUD_2400: port_baud = B2400; break;
|
||||
case SERIAL_BAUD_4800: port_baud = B4800; break;
|
||||
case SERIAL_BAUD_9600: port_baud = B9600; break;
|
||||
case SERIAL_BAUD_19200: port_baud = B19200; break;
|
||||
case SERIAL_BAUD_38400: port_baud = B38400; break;
|
||||
case SERIAL_BAUD_57600: port_baud = B57600; break;
|
||||
case SERIAL_BAUD_115200: port_baud = B115200; break;
|
||||
case SERIAL_BAUD_230400: port_baud = B230400; break;
|
||||
#ifdef BSD
|
||||
case SERIAL_BAUD_460800: port_baud = 460800; break;
|
||||
case SERIAL_BAUD_500000: port_baud = 500000; break;
|
||||
case SERIAL_BAUD_576000: port_baud = 576000; break;
|
||||
case SERIAL_BAUD_921600: port_baud = 921600; break;
|
||||
case SERIAL_BAUD_1000000: port_baud = 1000000; break;
|
||||
case SERIAL_BAUD_1152000: port_baud = 1152000; break;
|
||||
case SERIAL_BAUD_1500000: port_baud = 1500000; break;
|
||||
case SERIAL_BAUD_2000000: port_baud = 2000000; break;
|
||||
case SERIAL_BAUD_2500000: port_baud = 2500000; break;
|
||||
case SERIAL_BAUD_3000000: port_baud = 3000000; break;
|
||||
case SERIAL_BAUD_3500000: port_baud = 3500000; break;
|
||||
case SERIAL_BAUD_4000000: port_baud = 4000000; break;
|
||||
#else
|
||||
#ifdef B460800
|
||||
case SERIAL_BAUD_460800: port_baud = B460800; break;
|
||||
#endif /* B460800 */
|
||||
#ifdef B921600
|
||||
case SERIAL_BAUD_921600: port_baud = B921600; break;
|
||||
#endif /* B921600 */
|
||||
#ifdef B500000
|
||||
case SERIAL_BAUD_500000: port_baud = B500000; break;
|
||||
#endif /* B500000 */
|
||||
#ifdef B576000
|
||||
case SERIAL_BAUD_576000: port_baud = B576000; break;
|
||||
#endif /* B576000 */
|
||||
#ifdef B1000000
|
||||
case SERIAL_BAUD_1000000: port_baud = B1000000; break;
|
||||
#endif /* B1000000 */
|
||||
#ifdef B1152000
|
||||
case SERIAL_BAUD_1152000: port_baud = B1152000; break;
|
||||
#endif /* B1152000 */
|
||||
#ifdef B1500000
|
||||
case SERIAL_BAUD_1500000: port_baud = B1500000; break;
|
||||
#endif /* B1500000 */
|
||||
#ifdef B2000000
|
||||
case SERIAL_BAUD_2000000: port_baud = B2000000; break;
|
||||
#endif /* B2000000 */
|
||||
#ifdef B2500000
|
||||
case SERIAL_BAUD_2500000: port_baud = B2500000; break;
|
||||
#endif /* B2500000 */
|
||||
#ifdef B3000000
|
||||
case SERIAL_BAUD_3000000: port_baud = B3000000; break;
|
||||
#endif /* B3000000 */
|
||||
#ifdef B3500000
|
||||
case SERIAL_BAUD_3500000: port_baud = B3500000; break;
|
||||
#endif /* B3500000 */
|
||||
#ifdef B4000000
|
||||
case SERIAL_BAUD_4000000: port_baud = B4000000; break;
|
||||
#endif /* B4000000 */
|
||||
#endif /* BSD */
|
||||
|
||||
case SERIAL_BAUD_INVALID:
|
||||
default:
|
||||
return PORT_ERR_BAUD;
|
||||
}
|
||||
|
||||
switch (bits) {
|
||||
case SERIAL_BITS_5: port_bits = CS5; break;
|
||||
case SERIAL_BITS_6: port_bits = CS6; break;
|
||||
case SERIAL_BITS_7: port_bits = CS7; break;
|
||||
case SERIAL_BITS_8: port_bits = CS8; break;
|
||||
|
||||
default:
|
||||
return PORT_ERR_UNKNOWN;
|
||||
}
|
||||
|
||||
switch (parity) {
|
||||
case SERIAL_PARITY_NONE: port_parity = 0; break;
|
||||
case SERIAL_PARITY_EVEN: port_parity = PARENB; break;
|
||||
case SERIAL_PARITY_ODD: port_parity = PARENB | PARODD; break;
|
||||
|
||||
default:
|
||||
return PORT_ERR_UNKNOWN;
|
||||
}
|
||||
|
||||
switch (stopbit) {
|
||||
case SERIAL_STOPBIT_1: port_stop = 0; break;
|
||||
case SERIAL_STOPBIT_2: port_stop = CSTOPB; break;
|
||||
|
||||
default:
|
||||
return PORT_ERR_UNKNOWN;
|
||||
}
|
||||
|
||||
/* reset the settings */
|
||||
#ifndef __sun /* Used by GNU and BSD. Ignore __SVR4 in test. */
|
||||
cfmakeraw(&h->newtio);
|
||||
#else /* __sun */
|
||||
h->newtio.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR
|
||||
| IGNCR | ICRNL | IXON);
|
||||
h->newtio.c_oflag &= ~OPOST;
|
||||
h->newtio.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
|
||||
h->newtio.c_cflag &= ~(CSIZE | PARENB);
|
||||
h->newtio.c_cflag |= CS8;
|
||||
#endif /* __sun */
|
||||
#ifdef __QNXNTO__
|
||||
h->newtio.c_cflag &= ~(CSIZE | IHFLOW | OHFLOW);
|
||||
#else
|
||||
h->newtio.c_cflag &= ~(CSIZE | CRTSCTS);
|
||||
#endif
|
||||
h->newtio.c_cflag &= ~(CSIZE | CRTSCTS);
|
||||
h->newtio.c_cflag &= ~(PARENB | PARODD);
|
||||
h->newtio.c_cflag &= ~CSTOPB;
|
||||
h->newtio.c_iflag &= ~(IXON | IXOFF | IXANY | IGNPAR);
|
||||
h->newtio.c_lflag &= ~(ECHOK | ECHOCTL | ECHOKE);
|
||||
h->newtio.c_oflag &= ~(OPOST | ONLCR);
|
||||
|
||||
/* setup the new settings */
|
||||
if (cfsetispeed(&h->newtio, port_baud) ||
|
||||
cfsetospeed(&h->newtio, port_baud)) {
|
||||
fprintf(stderr, "Failed to specify baud rate\n");
|
||||
return PORT_ERR_BAUD;
|
||||
}
|
||||
|
||||
h->newtio.c_cflag |=
|
||||
port_parity |
|
||||
port_bits |
|
||||
port_stop |
|
||||
CLOCAL |
|
||||
CREAD;
|
||||
if ( port_parity != 0 )
|
||||
h->newtio.c_iflag |= INPCK;
|
||||
|
||||
h->newtio.c_cc[VMIN] = 0;
|
||||
h->newtio.c_cc[VTIME] = TERMIOS_TIMEOUT; /* in units of 0.1 s */
|
||||
|
||||
/* set the settings */
|
||||
serial_flush(h);
|
||||
if (tcsetattr(h->fd, TCSANOW, &h->newtio) != 0) {
|
||||
fprintf(stderr, "Error: Failed to apply settings\n");
|
||||
return PORT_ERR_UNKNOWN;
|
||||
}
|
||||
|
||||
/* confirm they were set */
|
||||
tcgetattr(h->fd, &settings);
|
||||
|
||||
if (cfgetispeed(&settings) != port_baud ||
|
||||
cfgetospeed(&settings) != port_baud) {
|
||||
fprintf(stderr, "Failed to set baud rate\n");
|
||||
return PORT_ERR_BAUD;
|
||||
}
|
||||
|
||||
if (settings.c_iflag != h->newtio.c_iflag ||
|
||||
settings.c_oflag != h->newtio.c_oflag ||
|
||||
settings.c_cflag != h->newtio.c_cflag ||
|
||||
settings.c_lflag != h->newtio.c_lflag) {
|
||||
fprintf(stderr, "Failed to set terminal flags\n");
|
||||
return PORT_ERR_UNKNOWN;
|
||||
}
|
||||
|
||||
snprintf(h->setup_str, sizeof(h->setup_str), "%u %d%c%d",
|
||||
serial_get_baud_int(baud),
|
||||
serial_get_bits_int(bits),
|
||||
serial_get_parity_str(parity),
|
||||
serial_get_stopbit_int(stopbit));
|
||||
return PORT_ERR_OK;
|
||||
}
|
||||
|
||||
static port_err_t serial_posix_open(struct port_interface *port,
|
||||
struct port_options *ops)
|
||||
{
|
||||
serial_t *h;
|
||||
port_err_t ret;
|
||||
|
||||
/* user takes care of all port setup */
|
||||
if (ops->baudRate == SERIAL_BAUD_KEEP) {
|
||||
h = serial_open(ops->device);
|
||||
if (h == NULL)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
port->private = h;
|
||||
return PORT_ERR_OK;
|
||||
}
|
||||
|
||||
/* 1. check options */
|
||||
if (ops->baudRate == SERIAL_BAUD_INVALID)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
if (serial_get_bits(ops->serial_mode) == SERIAL_BITS_INVALID)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
if (serial_get_parity(ops->serial_mode) == SERIAL_PARITY_INVALID)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
if (serial_get_stopbit(ops->serial_mode) == SERIAL_STOPBIT_INVALID)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
|
||||
/* 2. open it */
|
||||
h = serial_open(ops->device);
|
||||
if (h == NULL)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
|
||||
/* 3. check for tty (but only warn) */
|
||||
if (!isatty(h->fd))
|
||||
fprintf(stderr, "Warning: Not a tty: %s\n", ops->device);
|
||||
|
||||
/* 4. set options */
|
||||
ret = serial_setup(h, ops->baudRate,
|
||||
serial_get_bits(ops->serial_mode),
|
||||
serial_get_parity(ops->serial_mode),
|
||||
serial_get_stopbit(ops->serial_mode));
|
||||
if (ret != PORT_ERR_OK) {
|
||||
serial_close(h);
|
||||
return ret;
|
||||
}
|
||||
|
||||
port->private = h;
|
||||
return PORT_ERR_OK;
|
||||
}
|
||||
|
||||
static port_err_t serial_posix_close(struct port_interface *port)
|
||||
{
|
||||
serial_t *h;
|
||||
|
||||
h = (serial_t *)port->private;
|
||||
if (h == NULL)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
|
||||
serial_close(h);
|
||||
port->private = NULL;
|
||||
return PORT_ERR_OK;
|
||||
}
|
||||
|
||||
static port_err_t serial_posix_read(struct port_interface *port, void *buf,
|
||||
size_t nbyte)
|
||||
{
|
||||
serial_t *h;
|
||||
ssize_t r;
|
||||
uint8_t *pos = (uint8_t *)buf;
|
||||
|
||||
h = (serial_t *)port->private;
|
||||
if (h == NULL)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
|
||||
while (nbyte) {
|
||||
r = read(h->fd, pos, nbyte);
|
||||
if (r == 0)
|
||||
return PORT_ERR_TIMEDOUT;
|
||||
if (r < 0)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
|
||||
nbyte -= r;
|
||||
pos += r;
|
||||
}
|
||||
return PORT_ERR_OK;
|
||||
}
|
||||
|
||||
static port_err_t serial_posix_write(struct port_interface *port, void *buf,
|
||||
size_t nbyte)
|
||||
{
|
||||
serial_t *h;
|
||||
ssize_t r;
|
||||
const uint8_t *pos = (const uint8_t *)buf;
|
||||
|
||||
h = (serial_t *)port->private;
|
||||
if (h == NULL)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
|
||||
while (nbyte) {
|
||||
r = write(h->fd, pos, nbyte);
|
||||
if (r < 1)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
|
||||
nbyte -= r;
|
||||
pos += r;
|
||||
}
|
||||
return PORT_ERR_OK;
|
||||
}
|
||||
|
||||
static port_err_t serial_posix_gpio(struct port_interface *port,
|
||||
serial_gpio_t n, int level)
|
||||
{
|
||||
serial_t *h;
|
||||
int bit, lines;
|
||||
|
||||
h = (serial_t *)port->private;
|
||||
if (h == NULL)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
|
||||
switch (n) {
|
||||
case GPIO_RTS:
|
||||
bit = TIOCM_RTS;
|
||||
break;
|
||||
|
||||
case GPIO_DTR:
|
||||
bit = TIOCM_DTR;
|
||||
break;
|
||||
|
||||
case GPIO_BRK:
|
||||
if (level == 0)
|
||||
return PORT_ERR_OK;
|
||||
if (tcsendbreak(h->fd, 1))
|
||||
return PORT_ERR_UNKNOWN;
|
||||
return PORT_ERR_OK;
|
||||
|
||||
default:
|
||||
return PORT_ERR_UNKNOWN;
|
||||
}
|
||||
|
||||
/* handle RTS/DTR */
|
||||
if (ioctl(h->fd, TIOCMGET, &lines))
|
||||
return PORT_ERR_UNKNOWN;
|
||||
lines = level ? lines | bit : lines & ~bit;
|
||||
if (ioctl(h->fd, TIOCMSET, &lines))
|
||||
return PORT_ERR_UNKNOWN;
|
||||
|
||||
return PORT_ERR_OK;
|
||||
}
|
||||
|
||||
static const char *serial_posix_get_cfg_str(struct port_interface *port)
|
||||
{
|
||||
serial_t *h;
|
||||
|
||||
h = (serial_t *)port->private;
|
||||
return h ? h->setup_str : "INVALID";
|
||||
}
|
||||
|
||||
static port_err_t serial_posix_flush(struct port_interface *port)
|
||||
{
|
||||
serial_t *h;
|
||||
h = (serial_t *)port->private;
|
||||
if (h == NULL)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
|
||||
serial_flush(h);
|
||||
|
||||
return PORT_ERR_OK;
|
||||
}
|
||||
|
||||
struct port_interface port_serial = {
|
||||
.name = "serial_posix",
|
||||
.flags = PORT_BYTE | PORT_GVR_ETX | PORT_CMD_INIT | PORT_RETRY,
|
||||
.open = serial_posix_open,
|
||||
.close = serial_posix_close,
|
||||
.flush = serial_posix_flush,
|
||||
.read = serial_posix_read,
|
||||
.write = serial_posix_write,
|
||||
.gpio = serial_posix_gpio,
|
||||
.get_cfg_str = serial_posix_get_cfg_str,
|
||||
};
|
|
@ -1,376 +0,0 @@
|
|||
/*
|
||||
stm32flash - Open Source ST STM32 flash program for *nix
|
||||
Copyright (C) 2010 Geoffrey McRae <geoff@spacevs.com>
|
||||
Copyright (C) 2010 Gareth McMullin <gareth@blacksphere.co.nz>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <windows.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "compiler.h"
|
||||
#include "serial.h"
|
||||
#include "port.h"
|
||||
|
||||
struct serial {
|
||||
HANDLE fd;
|
||||
DCB oldtio;
|
||||
DCB newtio;
|
||||
char setup_str[11];
|
||||
};
|
||||
|
||||
static serial_t *serial_open(const char *device)
|
||||
{
|
||||
serial_t *h = calloc(sizeof(serial_t), 1);
|
||||
char *devName;
|
||||
|
||||
/* timeout in ms */
|
||||
COMMTIMEOUTS timeouts = {MAXDWORD, MAXDWORD, 500, 0, 0};
|
||||
|
||||
/* Fix the device name if required */
|
||||
if (strlen(device) > 4 && device[0] != '\\') {
|
||||
devName = calloc(1, strlen(device) + 5);
|
||||
sprintf(devName, "\\\\.\\%s", device);
|
||||
} else {
|
||||
devName = (char *)device;
|
||||
}
|
||||
|
||||
/* Create file handle for port */
|
||||
h->fd = CreateFile(devName, GENERIC_READ | GENERIC_WRITE,
|
||||
0, /* Exclusive access */
|
||||
NULL, /* No security */
|
||||
OPEN_EXISTING,
|
||||
0, /* No overlap */
|
||||
NULL);
|
||||
|
||||
if (devName != device)
|
||||
free(devName);
|
||||
|
||||
if (h->fd == INVALID_HANDLE_VALUE) {
|
||||
if (GetLastError() == ERROR_FILE_NOT_FOUND)
|
||||
fprintf(stderr, "File not found: %s\n", device);
|
||||
free(h);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SetupComm(h->fd, 4096, 4096); /* Set input and output buffer size */
|
||||
|
||||
SetCommTimeouts(h->fd, &timeouts);
|
||||
|
||||
SetCommMask(h->fd, EV_ERR); /* Notify us of error events */
|
||||
|
||||
/* DCBlength should be initialized before calling GetCommState */
|
||||
h->oldtio.DCBlength = sizeof(DCB);
|
||||
h->newtio.DCBlength = sizeof(DCB);
|
||||
GetCommState(h->fd, &h->oldtio); /* Retrieve port parameters */
|
||||
GetCommState(h->fd, &h->newtio); /* Retrieve port parameters */
|
||||
|
||||
/* PurgeComm(h->fd, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR); */
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
static void serial_flush(const serial_t __unused *h)
|
||||
{
|
||||
PurgeComm(h->fd, PURGE_RXCLEAR);
|
||||
}
|
||||
|
||||
static void serial_close(serial_t *h)
|
||||
{
|
||||
serial_flush(h);
|
||||
SetCommState(h->fd, &h->oldtio);
|
||||
CloseHandle(h->fd);
|
||||
free(h);
|
||||
}
|
||||
|
||||
static port_err_t serial_setup(serial_t *h,
|
||||
const serial_baud_t baud,
|
||||
const serial_bits_t bits,
|
||||
const serial_parity_t parity,
|
||||
const serial_stopbit_t stopbit)
|
||||
{
|
||||
switch (baud) {
|
||||
case SERIAL_BAUD_1200: h->newtio.BaudRate = CBR_1200; break;
|
||||
case SERIAL_BAUD_2400: h->newtio.BaudRate = CBR_2400; break;
|
||||
case SERIAL_BAUD_4800: h->newtio.BaudRate = CBR_4800; break;
|
||||
case SERIAL_BAUD_9600: h->newtio.BaudRate = CBR_9600; break;
|
||||
case SERIAL_BAUD_14400: h->newtio.BaudRate = CBR_14400; break;
|
||||
case SERIAL_BAUD_19200: h->newtio.BaudRate = CBR_19200; break;
|
||||
case SERIAL_BAUD_38400: h->newtio.BaudRate = CBR_38400; break;
|
||||
case SERIAL_BAUD_56000: h->newtio.BaudRate = CBR_56000; break;
|
||||
case SERIAL_BAUD_57600: h->newtio.BaudRate = CBR_57600; break;
|
||||
case SERIAL_BAUD_115200: h->newtio.BaudRate = CBR_115200; break;
|
||||
case SERIAL_BAUD_128000: h->newtio.BaudRate = CBR_128000; break;
|
||||
case SERIAL_BAUD_256000: h->newtio.BaudRate = CBR_256000; break;
|
||||
/* These are not defined in WinBase.h and might work or not */
|
||||
case SERIAL_BAUD_230400: h->newtio.BaudRate = 230400; break;
|
||||
case SERIAL_BAUD_460800: h->newtio.BaudRate = 460800; break;
|
||||
case SERIAL_BAUD_500000: h->newtio.BaudRate = 500000; break;
|
||||
case SERIAL_BAUD_576000: h->newtio.BaudRate = 576000; break;
|
||||
case SERIAL_BAUD_921600: h->newtio.BaudRate = 921600; break;
|
||||
case SERIAL_BAUD_1000000: h->newtio.BaudRate = 1000000; break;
|
||||
case SERIAL_BAUD_1152000: h->newtio.BaudRate = 1152000; break;
|
||||
case SERIAL_BAUD_1500000: h->newtio.BaudRate = 1500000; break;
|
||||
case SERIAL_BAUD_2000000: h->newtio.BaudRate = 2000000; break;
|
||||
case SERIAL_BAUD_2500000: h->newtio.BaudRate = 2500000; break;
|
||||
case SERIAL_BAUD_3000000: h->newtio.BaudRate = 3000000; break;
|
||||
case SERIAL_BAUD_3500000: h->newtio.BaudRate = 3500000; break;
|
||||
case SERIAL_BAUD_4000000: h->newtio.BaudRate = 4000000; break;
|
||||
|
||||
case SERIAL_BAUD_INVALID:
|
||||
default:
|
||||
return PORT_ERR_BAUD;
|
||||
}
|
||||
|
||||
switch (bits) {
|
||||
case SERIAL_BITS_5: h->newtio.ByteSize = 5; break;
|
||||
case SERIAL_BITS_6: h->newtio.ByteSize = 6; break;
|
||||
case SERIAL_BITS_7: h->newtio.ByteSize = 7; break;
|
||||
case SERIAL_BITS_8: h->newtio.ByteSize = 8; break;
|
||||
|
||||
default:
|
||||
return PORT_ERR_UNKNOWN;
|
||||
}
|
||||
|
||||
switch (parity) {
|
||||
case SERIAL_PARITY_NONE: h->newtio.Parity = NOPARITY; break;
|
||||
case SERIAL_PARITY_EVEN: h->newtio.Parity = EVENPARITY; break;
|
||||
case SERIAL_PARITY_ODD: h->newtio.Parity = ODDPARITY; break;
|
||||
|
||||
default:
|
||||
return PORT_ERR_UNKNOWN;
|
||||
}
|
||||
|
||||
switch (stopbit) {
|
||||
case SERIAL_STOPBIT_1: h->newtio.StopBits = ONESTOPBIT; break;
|
||||
case SERIAL_STOPBIT_2: h->newtio.StopBits = TWOSTOPBITS; break;
|
||||
|
||||
default:
|
||||
return PORT_ERR_UNKNOWN;
|
||||
}
|
||||
|
||||
/* reset the settings */
|
||||
h->newtio.fOutxCtsFlow = FALSE;
|
||||
h->newtio.fOutxDsrFlow = FALSE;
|
||||
h->newtio.fDtrControl = DTR_CONTROL_DISABLE;
|
||||
h->newtio.fDsrSensitivity = FALSE;
|
||||
h->newtio.fTXContinueOnXoff = FALSE;
|
||||
h->newtio.fOutX = FALSE;
|
||||
h->newtio.fInX = FALSE;
|
||||
h->newtio.fErrorChar = FALSE;
|
||||
h->newtio.fNull = FALSE;
|
||||
h->newtio.fRtsControl = RTS_CONTROL_DISABLE;
|
||||
h->newtio.fAbortOnError = FALSE;
|
||||
|
||||
/* set the settings */
|
||||
serial_flush(h);
|
||||
if (!SetCommState(h->fd, &h->newtio))
|
||||
return PORT_ERR_UNKNOWN;
|
||||
|
||||
snprintf(h->setup_str, sizeof(h->setup_str), "%u %d%c%d",
|
||||
serial_get_baud_int(baud),
|
||||
serial_get_bits_int(bits),
|
||||
serial_get_parity_str(parity),
|
||||
serial_get_stopbit_int(stopbit)
|
||||
);
|
||||
return PORT_ERR_OK;
|
||||
}
|
||||
|
||||
static port_err_t serial_w32_open(struct port_interface *port,
|
||||
struct port_options *ops)
|
||||
{
|
||||
serial_t *h;
|
||||
port_err_t ret;
|
||||
|
||||
/* user takes care of all port setup */
|
||||
if (ops->baudRate == SERIAL_BAUD_KEEP) {
|
||||
h = serial_open(ops->device);
|
||||
if (h == NULL)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
port->private = h;
|
||||
return PORT_ERR_OK;
|
||||
}
|
||||
|
||||
/* 1. check device name match */
|
||||
if (!(!strncmp(ops->device, "COM", 3) && isdigit(ops->device[3]))
|
||||
&& !(!strncmp(ops->device, "\\\\.\\COM", strlen("\\\\.\\COM"))
|
||||
&& isdigit(ops->device[strlen("\\\\.\\COM")])))
|
||||
return PORT_ERR_NODEV;
|
||||
|
||||
/* 2. check options */
|
||||
if (ops->baudRate == SERIAL_BAUD_INVALID)
|
||||
return PORT_ERR_BAUD;
|
||||
if (serial_get_bits(ops->serial_mode) == SERIAL_BITS_INVALID)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
if (serial_get_parity(ops->serial_mode) == SERIAL_PARITY_INVALID)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
if (serial_get_stopbit(ops->serial_mode) == SERIAL_STOPBIT_INVALID)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
|
||||
/* 3. open it */
|
||||
h = serial_open(ops->device);
|
||||
if (h == NULL)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
|
||||
/* 4. set options */
|
||||
ret = serial_setup(h, ops->baudRate,
|
||||
serial_get_bits(ops->serial_mode),
|
||||
serial_get_parity(ops->serial_mode),
|
||||
serial_get_stopbit(ops->serial_mode));
|
||||
if (ret != PORT_ERR_OK) {
|
||||
serial_close(h);
|
||||
return ret;
|
||||
}
|
||||
|
||||
port->private = h;
|
||||
return PORT_ERR_OK;
|
||||
}
|
||||
|
||||
static port_err_t serial_w32_close(struct port_interface *port)
|
||||
{
|
||||
serial_t *h;
|
||||
|
||||
h = (serial_t *)port->private;
|
||||
if (h == NULL)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
|
||||
serial_close(h);
|
||||
port->private = NULL;
|
||||
return PORT_ERR_OK;
|
||||
}
|
||||
|
||||
static port_err_t serial_w32_read(struct port_interface *port, void *buf,
|
||||
size_t nbyte)
|
||||
{
|
||||
serial_t *h;
|
||||
DWORD r;
|
||||
uint8_t *pos = (uint8_t *)buf;
|
||||
|
||||
h = (serial_t *)port->private;
|
||||
if (h == NULL)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
|
||||
while (nbyte) {
|
||||
ReadFile(h->fd, pos, nbyte, &r, NULL);
|
||||
if (r == 0)
|
||||
return PORT_ERR_TIMEDOUT;
|
||||
|
||||
nbyte -= r;
|
||||
pos += r;
|
||||
}
|
||||
return PORT_ERR_OK;
|
||||
}
|
||||
|
||||
static port_err_t serial_w32_write(struct port_interface *port, void *buf,
|
||||
size_t nbyte)
|
||||
{
|
||||
serial_t *h;
|
||||
DWORD r;
|
||||
uint8_t *pos = (uint8_t *)buf;
|
||||
|
||||
h = (serial_t *)port->private;
|
||||
if (h == NULL)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
|
||||
while (nbyte) {
|
||||
if (!WriteFile(h->fd, pos, nbyte, &r, NULL))
|
||||
return PORT_ERR_UNKNOWN;
|
||||
if (r < 1)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
|
||||
nbyte -= r;
|
||||
pos += r;
|
||||
}
|
||||
return PORT_ERR_OK;
|
||||
}
|
||||
|
||||
static port_err_t serial_w32_gpio(struct port_interface *port,
|
||||
serial_gpio_t n, int level)
|
||||
{
|
||||
serial_t *h;
|
||||
int bit;
|
||||
|
||||
h = (serial_t *)port->private;
|
||||
if (h == NULL)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
|
||||
switch (n) {
|
||||
case GPIO_RTS:
|
||||
bit = level ? SETRTS : CLRRTS;
|
||||
break;
|
||||
|
||||
case GPIO_DTR:
|
||||
bit = level ? SETDTR : CLRDTR;
|
||||
break;
|
||||
|
||||
case GPIO_BRK:
|
||||
if (level == 0)
|
||||
return PORT_ERR_OK;
|
||||
if (EscapeCommFunction(h->fd, SETBREAK) == 0)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
usleep(500000);
|
||||
if (EscapeCommFunction(h->fd, CLRBREAK) == 0)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
return PORT_ERR_OK;
|
||||
|
||||
default:
|
||||
return PORT_ERR_UNKNOWN;
|
||||
}
|
||||
|
||||
/* handle RTS/DTR */
|
||||
if (EscapeCommFunction(h->fd, bit) == 0)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
|
||||
return PORT_ERR_OK;
|
||||
}
|
||||
|
||||
static const char *serial_w32_get_cfg_str(struct port_interface *port)
|
||||
{
|
||||
serial_t *h;
|
||||
|
||||
h = (serial_t *)port->private;
|
||||
return h ? h->setup_str : "INVALID";
|
||||
}
|
||||
|
||||
static port_err_t serial_w32_flush(struct port_interface *port)
|
||||
{
|
||||
serial_t *h;
|
||||
h = (serial_t *)port->private;
|
||||
if (h == NULL)
|
||||
return PORT_ERR_UNKNOWN;
|
||||
|
||||
serial_flush(h);
|
||||
|
||||
return PORT_ERR_OK;
|
||||
}
|
||||
|
||||
struct port_interface port_serial = {
|
||||
.name = "serial_w32",
|
||||
.flags = PORT_BYTE | PORT_GVR_ETX | PORT_CMD_INIT | PORT_RETRY,
|
||||
.open = serial_w32_open,
|
||||
.close = serial_w32_close,
|
||||
.flush = serial_w32_flush,
|
||||
.read = serial_w32_read,
|
||||
.write = serial_w32_write,
|
||||
.gpio = serial_w32_gpio,
|
||||
.get_cfg_str = serial_w32_get_cfg_str,
|
||||
};
|
File diff suppressed because it is too large
Load diff
|
@ -1,94 +0,0 @@
|
|||
/*
|
||||
stm32flash - Open Source ST STM32 flash program for *nix
|
||||
Copyright (C) 2010 Geoffrey McRae <geoff@spacevs.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _STM32_H
|
||||
#define _STM32_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "serial.h"
|
||||
|
||||
#define STM32_MAX_RX_FRAME 256 /* cmd read memory */
|
||||
#define STM32_MAX_TX_FRAME (1 + 256 + 1) /* cmd write memory */
|
||||
|
||||
#define STM32_MAX_PAGES 0x0000ffff
|
||||
#define STM32_MASS_ERASE 0x00100000 /* > 2 x max_pages */
|
||||
|
||||
typedef enum {
|
||||
STM32_ERR_OK = 0,
|
||||
STM32_ERR_UNKNOWN, /* Generic error */
|
||||
STM32_ERR_NACK,
|
||||
STM32_ERR_NO_CMD, /* Command not available in bootloader */
|
||||
} stm32_err_t;
|
||||
|
||||
typedef enum {
|
||||
F_NO_ME = 1 << 0, /* Mass-Erase not supported */
|
||||
F_OBLL = 1 << 1, /* OBL_LAUNCH required */
|
||||
F_PEMPTY = 1 << 2, /* clear PEMPTY bit required */
|
||||
} flags_t;
|
||||
|
||||
typedef struct stm32 stm32_t;
|
||||
typedef struct stm32_cmd stm32_cmd_t;
|
||||
typedef struct stm32_dev stm32_dev_t;
|
||||
|
||||
struct stm32 {
|
||||
const serial_t *serial;
|
||||
struct port_interface *port;
|
||||
uint8_t bl_version;
|
||||
uint8_t version;
|
||||
uint8_t option1, option2;
|
||||
uint16_t pid;
|
||||
stm32_cmd_t *cmd;
|
||||
const stm32_dev_t *dev;
|
||||
};
|
||||
|
||||
struct stm32_dev {
|
||||
uint16_t id;
|
||||
const char *name;
|
||||
uint32_t ram_start, ram_end;
|
||||
uint32_t fl_start, fl_end;
|
||||
uint16_t fl_pps; // pages per sector
|
||||
uint32_t *fl_ps; // page size
|
||||
uint32_t opt_start, opt_end;
|
||||
uint32_t mem_start, mem_end;
|
||||
uint32_t flags;
|
||||
};
|
||||
|
||||
stm32_t *stm32_init(struct port_interface *port, const char init);
|
||||
void stm32_close(stm32_t *stm);
|
||||
stm32_err_t stm32_read_memory(const stm32_t *stm, uint32_t address,
|
||||
uint8_t data[], unsigned int len);
|
||||
stm32_err_t stm32_write_memory(const stm32_t *stm, uint32_t address,
|
||||
const uint8_t data[], unsigned int len);
|
||||
stm32_err_t stm32_wunprot_memory(const stm32_t *stm);
|
||||
stm32_err_t stm32_wprot_memory(const stm32_t *stm);
|
||||
stm32_err_t stm32_erase_memory(const stm32_t *stm, uint32_t spage,
|
||||
uint32_t pages);
|
||||
stm32_err_t stm32_go(const stm32_t *stm, uint32_t address);
|
||||
stm32_err_t stm32_reset_device(const stm32_t *stm);
|
||||
stm32_err_t stm32_readprot_memory(const stm32_t *stm);
|
||||
stm32_err_t stm32_runprot_memory(const stm32_t *stm);
|
||||
stm32_err_t stm32_crc_memory(const stm32_t *stm, uint32_t address,
|
||||
uint32_t length, uint32_t *crc);
|
||||
stm32_err_t stm32_crc_wrapper(const stm32_t *stm, uint32_t address,
|
||||
uint32_t length, uint32_t *crc);
|
||||
uint32_t stm32_sw_crc(uint32_t crc, uint8_t *buf, unsigned int len);
|
||||
|
||||
#endif
|
||||
|
Binary file not shown.
Binary file not shown.
|
@ -1,440 +0,0 @@
|
|||
.TH "STM32FLASH" "1" "March 2022" stm32flash "User command"
|
||||
.SH NAME
|
||||
stm32flash \- flashing utility for STM32 through UART or I2C
|
||||
.SH SYNOPSIS
|
||||
.B stm32flash
|
||||
.RB [ \-cfhjkouvCR ]
|
||||
.RB [ \-a
|
||||
.IR bus_address ]
|
||||
.RB [ \-b
|
||||
.IR baud_rate ]
|
||||
.RB [ \-m
|
||||
.IR serial_mode ]
|
||||
.RB [ \-r
|
||||
.IR filename ]
|
||||
.RB [ \-w
|
||||
.IR filename ]
|
||||
.RB [ \-e
|
||||
.IR num ]
|
||||
.RB [ \-n
|
||||
.IR count ]
|
||||
.RB [ \-g
|
||||
.IR address ]
|
||||
.RB [ \-s
|
||||
.IR start_page ]
|
||||
.RB [ \-S
|
||||
.IR address [: length ]]
|
||||
.RB [ \-F
|
||||
.IR RX_length [: TX_length ]]
|
||||
.RB [ \-i
|
||||
.IR GPIO_string ]
|
||||
.RI [ tty_device
|
||||
|
|
||||
.IR i2c_device ]
|
||||
|
||||
.SH DESCRIPTION
|
||||
.B stm32flash
|
||||
reads or writes the flash memory of STM32.
|
||||
|
||||
It requires the STM32 to embed a bootloader compliant with ST
|
||||
application note AN3155 or AN4221.
|
||||
.B stm32flash
|
||||
uses the serial port
|
||||
.I tty_device
|
||||
or the i2c port
|
||||
.I i2c_device
|
||||
to interact with the bootloader of STM32.
|
||||
|
||||
.SH OPTIONS
|
||||
.TP
|
||||
.BI "\-a" " bus_address"
|
||||
Specify address on bus for
|
||||
.IR i2c_device .
|
||||
This option is mandatory for I2C interface.
|
||||
|
||||
.TP
|
||||
.BI "\-b" " baud_rate"
|
||||
Specify baud rate speed of
|
||||
.IR tty_device .
|
||||
Please notice that the ST bootloader can automatically detect the baud rate,
|
||||
as explained in chapter 2 of AN3155.
|
||||
This option could be required together with option
|
||||
.B "\-c"
|
||||
or if following interaction with bootloader is expected.
|
||||
Default is
|
||||
.IR 57600 .
|
||||
|
||||
.TP
|
||||
.BI "\-m" " mode"
|
||||
Specify the format of UART data.
|
||||
.I mode
|
||||
is a three characters long string where each character specifies, in
|
||||
this strict order, character size, parity and stop bits.
|
||||
The only values currently used are
|
||||
.I 8e1
|
||||
for standard STM32 bootloader and
|
||||
.I 8n1
|
||||
for standard STM32W bootloader.
|
||||
Default is
|
||||
.IR 8e1 .
|
||||
|
||||
.TP
|
||||
.BI "\-r" " filename"
|
||||
Specify to read the STM32 flash and write its content in
|
||||
.I filename
|
||||
in raw binary format (see below
|
||||
.BR "FORMAT CONVERSION" ).
|
||||
|
||||
.TP
|
||||
.BI "\-w" " filename"
|
||||
Specify to write the STM32 flash with the content of
|
||||
.IR filename .
|
||||
File format can be either raw binary or intel hex (see below
|
||||
.BR "FORMAT CONVERSION" ).
|
||||
The file format is automatically detected.
|
||||
To by\-pass format detection and force binary mode (e.g. to
|
||||
write an intel hex content in STM32 flash), use
|
||||
.B \-f
|
||||
option.
|
||||
|
||||
.TP
|
||||
.B \-u
|
||||
Specify to disable write\-protection from STM32 flash.
|
||||
The STM32 will be reset after this operation.
|
||||
|
||||
.TP
|
||||
.B \-j
|
||||
Enable the flash read\-protection.
|
||||
|
||||
.TP
|
||||
.B \-k
|
||||
Disable the flash read\-protection.
|
||||
|
||||
.TP
|
||||
.B \-o
|
||||
Erase only.
|
||||
|
||||
.TP
|
||||
.BI "\-e" " num"
|
||||
Specify to erase
|
||||
.I num
|
||||
pages before writing the flash. Default is to erase the necessary pages based on the file content size and/or parameters provided with the -S option, or the whole flash if this cannot be determined. With
|
||||
.B \-e 0
|
||||
the flash would not be erased.
|
||||
|
||||
.TP
|
||||
.B \-v
|
||||
Specify to verify flash content after write operation.
|
||||
|
||||
.TP
|
||||
.BI "\-n" " count"
|
||||
Specify to retry failed writes up to
|
||||
.I count
|
||||
times. Default is 10 times.
|
||||
|
||||
.TP
|
||||
.BI "\-g" " address"
|
||||
Specify address to start execution from (0 = flash start).
|
||||
|
||||
.TP
|
||||
.BI "\-s" " start_page"
|
||||
Specify flash page offset (0 = flash start).
|
||||
|
||||
.TP
|
||||
.BI "\-S" " address" "[:" "length" "]"
|
||||
Specify start address and optionally length for read/write/erase/crc operations.
|
||||
|
||||
.TP
|
||||
.BI "\-F" " RX_length" "[:" "TX_length" "]"
|
||||
Specify the maximum frame size for the current interface.
|
||||
Due to STM32 bootloader protocol, host will never handle frames bigger than
|
||||
256 byte in RX or 258 byte in TX.
|
||||
Due to current code, lowest limit in RX is 20 byte (to read a complete reply
|
||||
of command GET). Minimum limit in TX is 5 byte, required by protocol.
|
||||
|
||||
.TP
|
||||
.B \-f
|
||||
Force binary parser while reading file with
|
||||
.BR "\-w" "."
|
||||
|
||||
.TP
|
||||
.B \-h
|
||||
Show help.
|
||||
|
||||
.TP
|
||||
.B \-c
|
||||
Specify to resume the existing UART connection and don't send initial
|
||||
INIT sequence to detect baud rate. Baud rate must be kept the same as the
|
||||
existing connection. This is useful if the reset fails.
|
||||
|
||||
.TP
|
||||
.BI "\-i" " GPIO_string"
|
||||
Specify the GPIO sequences on the host to force STM32 to enter and
|
||||
exit bootloader mode. GPIO can either be real GPIO connected from host to
|
||||
STM32 beside the UART connection, or UART's modem signals used as
|
||||
GPIO. (See below
|
||||
.B BOOTLOADER GPIO SEQUENCE
|
||||
for the format of
|
||||
.I GPIO_string
|
||||
and further explanation).
|
||||
|
||||
.TP
|
||||
.B \-C
|
||||
Specify to compute CRC on memory content.
|
||||
By default the CRC is computed on the whole flash content.
|
||||
Use
|
||||
.B "\-S"
|
||||
to provide different memory address range.
|
||||
|
||||
.TP
|
||||
.B \-R
|
||||
Specify to reset the device at exit.
|
||||
This option is ignored if either
|
||||
.BR "\-g" ","
|
||||
.BR "\-j" ","
|
||||
.B "\-k"
|
||||
or
|
||||
.B "\-u"
|
||||
is also specified.
|
||||
|
||||
.SH BOOTLOADER GPIO SEQUENCE
|
||||
This feature is currently available on Linux host only.
|
||||
|
||||
As explained in ST application note AN2606, after reset the STM32 will
|
||||
execute either the application program in user flash or the bootloader,
|
||||
depending on the level applied at specific pins of STM32 during reset.
|
||||
|
||||
STM32 bootloader is automatically activated by configuring the pins
|
||||
BOOT0="high" and BOOT1="low" and then by applying a reset.
|
||||
Application program in user flash is activated by configuring the pin
|
||||
BOOT0="low" (the level on BOOT1 is ignored) and then by applying a reset.
|
||||
|
||||
When GPIO from host computer are connected to either configuration and
|
||||
reset pins of STM32,
|
||||
.B stm32flash
|
||||
can control the host GPIO to reset STM32 and to force execution of
|
||||
bootloader or execution of application program.
|
||||
|
||||
The sequence of GPIO values to entry to and exit from bootloader mode is
|
||||
provided with command line option
|
||||
.B "\-i"
|
||||
.IR "GPIO_string" .
|
||||
|
||||
.PD 0
|
||||
The format of
|
||||
.IR "GPIO_string" " is:"
|
||||
.RS
|
||||
GPIO_string = [entry sequence][:[exit sequence]]
|
||||
.P
|
||||
sequence = [[\-]signal]&|,[sequence]
|
||||
.RE
|
||||
.PD
|
||||
.P
|
||||
In the above sequences, negative numbers correspond to GPIO at "low" level;
|
||||
numbers without sign correspond to GPIO at "high" level.
|
||||
The value "n" can either be the GPIO number on the host system or the
|
||||
string "rts", "dtr" or "brk". The strings "rts" and "dtr" drive the
|
||||
corresponding UART's modem lines RTS and DTR as GPIO.
|
||||
The string "brk" forces the UART to send a BREAK sequence on TX line;
|
||||
after BREAK the UART is returned in normal "non\-break" mode.
|
||||
Note: the string "\-brk" has no effect and is ignored.
|
||||
.PD
|
||||
.P
|
||||
The ',' delimiter adds 100 ms of delay between signal toggles, whereas
|
||||
the '&' delimiter adds no delay.
|
||||
An empty signal, thus repeated ',' delimiters, can be used to insert larger
|
||||
delays in multiples of 100 ms.
|
||||
E.g. "rts,,,,\-dtr" will set RTS, then wait 400 ms, then reset DTR.
|
||||
"rts&\-dtr" will set RTS and reset DTR without delay. You can use ',' delimiters
|
||||
alone to simply add a delay between opening port and starting to flash.
|
||||
.DP
|
||||
.P
|
||||
Note that since version 0.6, an exit sequence will always be executed if
|
||||
specified, regardless of the -R option, to ensure the signals are reset.
|
||||
If -R is specified, but no exit sequence, a software-triggered reset will
|
||||
be performed.
|
||||
|
||||
.PD 0
|
||||
As an example, let's suppose the following connection between host and STM32:
|
||||
.IP \(bu 2
|
||||
host GPIO_3 connected to reset pin of STM32;
|
||||
.IP \(bu 2
|
||||
host GPIO_4 connected to STM32 pin BOOT0;
|
||||
.IP \(bu 2
|
||||
host GPIO_5 connected to STM32 pin BOOT1.
|
||||
.PD
|
||||
.P
|
||||
|
||||
In this case, the sequence to enter in bootloader mode is: first put
|
||||
GPIO_4="high" and GPIO_5="low"; then send reset pulse by GPIO_3="low"
|
||||
followed by GPIO_3="high".
|
||||
The corresponding string for
|
||||
.I GPIO_string
|
||||
is "4,\-5,\-3,3".
|
||||
|
||||
To exit from bootloader and run the application program, the sequence is:
|
||||
put GPIO_4="low"; then send reset pulse.
|
||||
The corresponding string for
|
||||
.I GPIO_string
|
||||
is "\-4,\-3,3".
|
||||
|
||||
The complete command line flag is "\-i 4,\-5,\-3,3:\-4,\-3,3".
|
||||
|
||||
STM32W uses pad PA5 to select boot mode; if during reset PA5 is "low" then
|
||||
STM32W will enter in bootloader mode; if PA5 is "high" it will execute the
|
||||
program in flash.
|
||||
|
||||
As an example, suppose GPIO_3 is connected to PA5 and GPIO_2 to STM32W's reset.
|
||||
The command:
|
||||
.PD 0
|
||||
.RS
|
||||
stm32flash \-i '\-3&\-2,2:3&\-2,,,2' /dev/ttyS0
|
||||
.RE
|
||||
provides:
|
||||
.IP \(bu 2
|
||||
entry sequence: GPIO_3=low, GPIO_2=low, 100 ms delay, GPIO_2=high
|
||||
.IP \(bu 2
|
||||
exit sequence: GPIO_3=high, GPIO_2=low, 300 ms delay, GPIO_2=high
|
||||
.PD
|
||||
.P
|
||||
|
||||
Another example, introducing a delay after port opening.
|
||||
The command:
|
||||
.PD 0
|
||||
.RS
|
||||
stm32flash \-i ',,,,,:rts&\-dtr,,,2' /dev/ttyS0\n",
|
||||
.RE
|
||||
provides:
|
||||
.IP \(bu 2
|
||||
entry sequence: delay 500 ms
|
||||
.IP \(bu 2
|
||||
exit sequence: RTS=high, DTR=low, 300 ms delay, GPIO_2=high
|
||||
.PD
|
||||
|
||||
|
||||
.SH EXAMPLES
|
||||
Get device information:
|
||||
.RS
|
||||
.PD 0
|
||||
.P
|
||||
stm32flash /dev/ttyS0
|
||||
.PD
|
||||
.RE
|
||||
|
||||
Write with verify and then start execution:
|
||||
.RS
|
||||
.PD 0
|
||||
.P
|
||||
stm32flash \-w filename \-v \-g 0x0 /dev/ttyS0
|
||||
.PD
|
||||
.RE
|
||||
|
||||
Read flash to file:
|
||||
.RS
|
||||
.PD 0
|
||||
.P
|
||||
stm32flash \-r filename /dev/ttyS0
|
||||
.PD
|
||||
.RE
|
||||
|
||||
Start execution:
|
||||
.RS
|
||||
.PD 0
|
||||
.P
|
||||
stm32flash \-g 0x0 /dev/ttyS0
|
||||
.PD
|
||||
.RE
|
||||
|
||||
Specify:
|
||||
.PD 0
|
||||
.IP \(bu 2
|
||||
entry sequence: RTS=low, DTR=low, DTR=high
|
||||
.IP \(bu 2
|
||||
exit sequence: RTS=high, DTR=low, DTR=high
|
||||
.P
|
||||
.RS
|
||||
stm32flash \-i \-rts,\-dtr,dtr:rts,\-dtr,dtr /dev/ttyS0
|
||||
.PD
|
||||
.RE
|
||||
|
||||
.SH FORMAT CONVERSION
|
||||
Flash images provided by ST or created with ST tools are often in file
|
||||
format Motorola S\-Record.
|
||||
Conversion between raw binary, intel hex and Motorola S\-Record can be
|
||||
done through software package SRecord.
|
||||
|
||||
.SH AUTHORS
|
||||
The original software package
|
||||
.B stm32flash
|
||||
is written by
|
||||
.I Geoffrey McRae
|
||||
and is since 2012 maintained by
|
||||
.I Tormod Volden.
|
||||
See AUTHORS file in source code for more contributors.
|
||||
|
||||
Man page and extension to STM32W and I2C are written by
|
||||
.I Antonio Borneo.
|
||||
|
||||
Please report any bugs at the project homepage
|
||||
http://stm32flash.sourceforge.net .
|
||||
|
||||
.SH SEE ALSO
|
||||
.BR "srec_cat" "(1)," " srec_intel" "(5)," " srec_motorola" "(5)."
|
||||
|
||||
The communication protocol used by ST bootloader is documented in
|
||||
following ST application notes, depending on communication port.
|
||||
The current version of
|
||||
.B stm32flash
|
||||
only supports
|
||||
.I UART
|
||||
and
|
||||
.I I2C
|
||||
ports.
|
||||
.PD 0
|
||||
.P
|
||||
.IP \(bu 2
|
||||
AN3154: CAN protocol used in the STM32 bootloader
|
||||
.P
|
||||
.RS
|
||||
http://www.st.com/web/en/resource/technical/document/application_note/CD00264321.pdf
|
||||
.RE
|
||||
|
||||
.P
|
||||
.IP \(bu 2
|
||||
AN3155: USART protocol used in the STM32(TM) bootloader
|
||||
.P
|
||||
.RS
|
||||
http://www.st.com/web/en/resource/technical/document/application_note/CD00264342.pdf
|
||||
.RE
|
||||
|
||||
.P
|
||||
.IP \(bu 2
|
||||
AN4221: I2C protocol used in the STM32 bootloader
|
||||
.P
|
||||
.RS
|
||||
http://www.st.com/web/en/resource/technical/document/application_note/DM00072315.pdf
|
||||
.RE
|
||||
|
||||
.P
|
||||
.IP \(bu 2
|
||||
AN4286: SPI protocol used in the STM32 bootloader
|
||||
.P
|
||||
.RS
|
||||
http://www.st.com/web/en/resource/technical/document/application_note/DM00081379.pdf
|
||||
.RE
|
||||
|
||||
.PD
|
||||
|
||||
|
||||
Boot mode selection for STM32 is documented in ST application note
|
||||
AN2606, available from the ST website:
|
||||
.PD 0
|
||||
.P
|
||||
http://www.st.com/web/en/resource/technical/document/application_note/CD00167594.pdf
|
||||
.PD
|
||||
|
||||
.SH LICENSE
|
||||
.B stm32flash
|
||||
is distributed under GNU GENERAL PUBLIC LICENSE Version 2.
|
||||
Copy of the license is available within the source code in the file
|
||||
.IR "gpl\-2.0.txt" .
|
|
@ -1,52 +0,0 @@
|
|||
/*
|
||||
stm32flash - Open Source ST STM32 flash program for *nix
|
||||
Copyright (C) 2010 Geoffrey McRae <geoff@spacevs.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include "utils.h"
|
||||
|
||||
/* detect CPU endian */
|
||||
char cpu_le() {
|
||||
const uint32_t cpu_le_test = 0x12345678;
|
||||
return ((const unsigned char*)&cpu_le_test)[0] == 0x78;
|
||||
}
|
||||
|
||||
uint32_t be_u32(const uint32_t v) {
|
||||
if (cpu_le())
|
||||
return ((v & 0xFF000000) >> 24) |
|
||||
((v & 0x00FF0000) >> 8) |
|
||||
((v & 0x0000FF00) << 8) |
|
||||
((v & 0x000000FF) << 24);
|
||||
return v;
|
||||
}
|
||||
|
||||
uint32_t le_u32(const uint32_t v) {
|
||||
if (!cpu_le())
|
||||
return ((v & 0xFF000000) >> 24) |
|
||||
((v & 0x00FF0000) >> 8) |
|
||||
((v & 0x0000FF00) << 8) |
|
||||
((v & 0x000000FF) << 24);
|
||||
return v;
|
||||
}
|
||||
|
||||
void printStatus(FILE *fd, int condition){
|
||||
if(condition)
|
||||
fprintf(fd, "Error!\n");
|
||||
else
|
||||
fprintf(fd, "OK\n");
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
/*
|
||||
stm32flash - Open Source ST STM32 flash program for *nix
|
||||
Copyright (C) 2010 Geoffrey McRae <geoff@spacevs.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _H_UTILS
|
||||
#define _H_UTILS
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
char cpu_le();
|
||||
uint32_t be_u32(const uint32_t v);
|
||||
uint32_t le_u32(const uint32_t v);
|
||||
|
||||
void printStatus(FILE *fd, int condition);
|
||||
|
||||
#endif
|
Binary file not shown.
Loading…
Reference in a new issue