USB OTG as an Extra USB Host in Allwinner A10, A20 etc

USB OTG port on Allwinner SoC based boards is a very useful tool. It can be used to flash an image to the flash memory or connect to a Host PC and making the board to act as a device.

Although there are many scenarios where USB OTG is useful, at times it is more useful to have an extra USB HOST on the board. All the settings of hardware configurations of allwinner SoCs are stored in a file named ` script.bin` that is compiled from a text file called fex file.We can learn more about the fex file from Sunxi Wiki in the Fex Guide.

A FEX file defines various aspects of how the SoC works. It configures the GPIO pins and sets up DRAM, Display, etc parameters.

Each line consists of a key = value pair combination under a [sectionheader]. All three, [sectionheader], key and value are case-sensitive. For comments a semi-colon (;) is used and everything following a semi-colon is ignored. The chip does not parse a textual version of a fex file, it gets cleaned and compiled by a fex-compiler. A reverse engineerd open source version exists in the sunxi-tools repository. Also a de-compiler which takes a binary script.bin and creates a textual script.fex. Usually, script.bin can be found on the nanda boot partition on A10 devices.

Changing script.bin file without removing the microSD card

The tools for script.bin changing are located in /opt/sunxi-tools directory:

  # cd /opt/sunxi-tools
  # ./chscr.sh

This will convert script.bin file from sdcard to script.fex file and the file will be opened using nano editor. Now you can change the board modules and parameters, save the changes (“CTRL”+”X”; confirm with “Y”) and exit (“CTRL”+”X” again) from nano editor.

  # ./wrscr.sh

this will convert script.fex to script.bin and the script.bin file will be written to sdcard.

  reboot

Reboot the board and the new settings would be enabled.

Changing script.bin file by removing the microSD card

The biggest part of the board configuration might be edited, changed or improved in a file called script.bin

The script.bin file can usually be found in the main directory of a microSD card prepared with official Debian image. The folder containing the script can be inspected under both Windows, Linux or Mac.

You can’t directly edit binary file so you would need to convert it to text format (it is called fex in this case), then edit the parameters via a text editor and finally switch it back to binary format.

IMPORTANT! ADJUSTING SCRIPT.BIN WITH IMPROPER VALUES MIGHT BREAK YOUR DEBIAN IMAGE AND IT IS ALWAYS RECOMMENDED TO KEEP A BACK-UP OF YOUR DEFAULT SCRIPT.BIN

To convert back and forth the script.bin you might use different tools. You can find Windows tools here: SUNXI TOOLS FOR WINDOWS . For Linux convertors please check the sunxi tools here: SUNXI TOOLS

Editing Fex File for USB OTG

Now open the Fex file and locate the USB section.

;——————————————————————————- 
;[usbc0]:  The configuration of controller 0 
;usb_used: Enable bit. If USB is enabled or not. 1: enable; 0: disable. 
;usb_port_type: Type of USB port. 0:device only;1:host only;2:OTG 
;usb_detect_type: Detect type. 0: Not detect; 1: detect vbus/id; 2: Detect id/dpdm
;——————————————————————————- 
;——————————————————————————- 

;——————————————————————————-
[usbc0]
usb_used = 1 
usb_port_type = 0 
usb_detect_type = 0 
usb_id_gpio = port:PH04<0><1><default><default> 
usb_det_vbus_gpio = “axp_ctrl” 
usb_drv_vbus_gpio = port:PB09<1><0><default><0> 
;usb_restrict_gpio = port:PH00<1><0><default><0> 
usb_host_init_state = 0 
usb_restric_flag = 0 
usb_restric_voltage = 3550000 
usb_restric_capacity= 5

We just need to change usb_port_type from 0 to 1 to make it a USB host.

Converting fex to script.bin:

The following commands are executed at sunxi-tools directory:

$./fex2bin script.fex >script.bin
$sudo cp script.bin /boot/script.bin
$sudo umount /boot
Now restart the board and you can use USB OTG as USB host.
 

Getting power and battery info from AXP209

Here is a script that gives some info about battery (when one is plugged)

I think that this page has mistakes (http://linux-sunxi.org/AXP209#Spec_Sheets) because discharging and charging current are inverted.

#!/bin/sh
# This program gets the battery info from PMU
# Voltage and current charging/discharging
#
# Nota : temperature can be more than real because of self heating
#######################################################################
# Copyright (c) 2014 by RzBo, Bellesserre, France
#
# Permission is granted to use the source code within this
# file in whole or in part for any use, personal or commercial,
# without restriction or limitation.
#
# No warranties, either explicit or implied, are made as to the
# suitability of this code for any purpose. Use at your own risk.
#######################################################################

# force ADC enable for battery voltage and current
i2cset -y -f 0 0x34 0x82 0xC3

################################
#read Power status register @00h
POWER_STATUS=$(i2cget -y -f 0 0x34 0x00)
#echo $POWER_STATUS

BAT_STATUS=$(($(($POWER_STATUS&0x02))/2))  # divide by 2 is like shifting rigth 1 times
#echo $(($POWER_STATUS&0x02))
echo "BAT_STATUS="$BAT_STATUS
# echo $BAT_STATUS

################################
#read Power OPERATING MODE register @01h
POWER_OP_MODE=$(i2cget -y -f 0 0x34 0x01)
#echo $POWER_OP_MODE

CHARG_IND=$(($(($POWER_OP_MODE&0x40))/64))  # divide by 64 is like shifting rigth 6 times
#echo $(($POWER_OP_MODE&0x40))
echo "CHARG_IND="$CHARG_IND
# echo $CHARG_IND

BAT_EXIST=$(($(($POWER_OP_MODE&0x20))/32))  # divide by 32 is like shifting rigth 5 times
#echo $(($POWER_OP_MODE&0x20))
echo "BAT_EXIST="$BAT_EXIST
# echo $BAT_EXIST

################################
#read Charge control register @33h
CHARGE_CTL=$(i2cget -y -f 0 0x34 0x33)
echo "CHARGE_CTL="$CHARGE_CTL
# echo $CHARGE_CTL


################################
#read Charge control register @34h
CHARGE_CTL2=$(i2cget -y -f 0 0x34 0x34)
echo "CHARGE_CTL2="$CHARGE_CTL2
# echo $CHARGE_CTL2


################################
#read battery voltage	79h, 78h	0 mV -> 000h,	1.1 mV/bit	FFFh -> 4.5045 V
BAT_VOLT_LSB=$(i2cget -y -f 0 0x34 0x79)
BAT_VOLT_MSB=$(i2cget -y -f 0 0x34 0x78)

#echo $BAT_VOLT_MSB $BAT_VOLT_LSB

BAT_BIN=$(( $(($BAT_VOLT_MSB << 4)) | $(($(($BAT_VOLT_LSB & 0xF0)) >> 4)) ))

BAT_VOLT=$(echo "($BAT_BIN*1.1)"|bc)
echo "Battery voltage = "$BAT_VOLT"mV"

###################
#read Battery Discharge Current	7Ah, 7Bh	0 mV -> 000h,	0.5 mA/bit	FFFh -> 4.095 V
BAT_IDISCHG_LSB=$(i2cget -y -f 0 0x34 0x7B)
BAT_IDISCHG_MSB=$(i2cget -y -f 0 0x34 0x7A)

#echo $BAT_IDISCHG_MSB $BAT_IDISCHG_LSB

BAT_IDISCHG_BIN=$(( $(($BAT_IDISCHG_MSB << 4)) | $(($(($BAT_IDISCHG_LSB & 0xF0)) >> 4)) ))

BAT_IDISCHG=$(echo "($BAT_IDISCHG_BIN*0.5)"|bc)
echo "Battery discharge current = "$BAT_IDISCHG"mA"

###################
#read Battery Charge Current	7Ch, 7Dh	0 mV -> 000h,	0.5 mA/bit	FFFh -> 4.095 V
BAT_ICHG_LSB=$(i2cget -y -f 0 0x34 0x7D)
BAT_ICHG_MSB=$(i2cget -y -f 0 0x34 0x7C)

#echo $BAT_ICHG_MSB $BAT_ICHG_LSB

BAT_ICHG_BIN=$(( $(($BAT_ICHG_MSB << 4)) | $(($(($BAT_ICHG_LSB & 0xF0)) >> 4)) ))

BAT_ICHG=$(echo "($BAT_ICHG_BIN*0.5)"|bc)
echo "Battery charge current = "$BAT_ICHG"mA"

this script gives the AC power status :

#!/bin/sh
# This program gets the power status (AC IN or BAT) for pcDuino3
# I2C interface with AXP209
#
#######################################################################
# Copyright (c) 2014 by RzBo, Bellesserre, France
#
# Permission is granted to use the source code within this
# file in whole or in part for any use, personal or commercial,
# without restriction or limitation.
#
# No warranties, either explicit or implied, are made as to the
# suitability of this code for any purpose. Use at your own risk.
#######################################################################

#read Power status register @00h
POWER_STATUS=$(i2cget -y -f 0 0x34 0x00)
#echo $POWER_STATUS

# bit 7 : Indicates ACIN presence 0: ACIN does not exist; 1: ACIN present
#echo "bit 7 : Indicates ACIN presence 0: ACIN does not exist; 1: ACIN present"
#echo "bit 2 : Indicates that the battery current direction 0: battery discharge; 1: The battery is charged"

AC_STATUS=$(($(($POWER_STATUS&0x80))/128))  # divide by 128 is like shifting rigth 8 times
#echo $(($POWER_STATUS&0x80))
echo "AC_STATUS="$AC_STATUS
# echo $AC_STATUS

Example :

battery charging

rzbo@ubuntu:~$ ./battery_info.sh
BAT_STATUS=0
CHARG_IND=1
BAT_EXIST=1
CHARGE_CTL=0xd0
CHARGE_CTL2=0x47
Battery voltage = 3895.1mV
Battery discharge current = 304.0mA
Battery charge current = 0mA

rzbo@ubuntu:~$ ./power_status.sh
AC_STATUS=1

Battery discharging (USB power unplugged)

rzbo@ubuntu:~$ ./battery_info.sh
BAT_STATUS=0
CHARG_IND=0
BAT_EXIST=1
CHARGE_CTL=0xd0
CHARGE_CTL2=0x47
Battery voltage = 3678.4mV
Battery discharge current = 0mA
Battery charge current = 176.0mA

rzbo@ubuntu:~$ ./power_status.sh
AC_STATUS=0

IMG_20140916_183651 IMG_20140916_183625