Sunday, November 27, 2016

OrangePi PC

The OrangePi PC based on Allwinner H3 SOC, a quad core ARM Cortex-A7 processor, with 1GB DDR3 SDRAM, selling for $15 (plus $3.35 shipping) is possibly the lowest cost PC with the similar capabilities.  (Cheaper boards have less RAM which limits the usability.)


The board has the same form factor as the more famous Raspberry Pi (not the same mounting holes) and the same expansion 40-pin header; the OrangePi PC also features an OTG USB port, a microphone and an IR receiver. Indeed, H3 is an impressive chip -- the board only needs DRAM, voltage regulators and connectors; no need for ethernet phy chip, USB hub etc.  The OTG micro-USB port can be turned into a serial port, an ethernet port etc through USB gadget support.  Also see https://linux-sunxi.org/Orange_Pi_PC.

To power the board, we need a power plug and a 5V power adapter capable of 2A.  The best OS flash image is Debian Jessie Desktop from armbian.com.  Connect the HDMI port to a TV for display.  It is good to connect to 3-pin UART serial console to see the boot message.  Once it boots up; the display resolution can be changed with the command h3disp and it is capable of 1920x1080p60.   The desktop environment is Xfce.  At least a USB mouse is required; without a physical keyboard, a virtual keyboard can be used.  I use a Bluetooth keyboard/touchpad, with a Bluetooth dongle, which it supports nicely.

The performance is OK for most of applications; but youtube video playing is a little jerky.  The audio output can be the HDMI or the 3.5mm jack.  The H3 gets a little hot when playing video; still it is impressive not needing a heat sink.

To test it as for code development, I try to build the recent version of KiCad.  A number prerequisite packages have to be downloaded.  The entire build takes almost 10 hours.  The speed of the micro SD card is probably a factor here.  It is also close to running out of memory.  The build is successful, but this probably is not a platform for serious software development.

While it is not a replacement for a good laptop or desktop, it is certainly good enough for seriously embedded applications.  The 40-pin expansion port has 28 signals, 5V, 3.3V and ground.  The signals can be GPIOs or 2 I2Cs, 3 UARTs, 1 SPI and 1 PWM.

There is a port of WiringPi call WiringOP that supports the I/O functions of the expansion header.



There is also a port of python package pyA20. 

For a more capable 64-bit version, see OrangePi Prime.

Also see this post for adding a small LCD screen.

Monday, October 24, 2016

Bluetooth Headset Battery

I have a Bluetooth headset that is 7-8 years old.  I used it occasionally in the car.  It just quit completely and could not be charged.  I never threw out a piece of electronics without opening and studying it.  So I peeled the cover off and took a look inside.  It is designed around Philips BGB204 Bluetooth 1.2 system-in-a-package radio chip that includes the Bluetooth core and an ARM7 processor.


The first thing to check was the battery.  I was surprised to find the battery voltage was at dead zero.  I was expecting it had at least some voltage albeit low, but it should not be at exact zero volt.  I took the battery off the circuit board and powered the board with external supply at 3.7V.  Everything seemed to work fine: it connected to the PC and transmitted sound through Bluetooth.  So the culprit had to be the battery.

I carefully peeled the tapes off the package and exposed the battery contacts and the protection circuit.  I measured the voltage across the battery contacts and there was some voltage as I expected.  So it was the protection circuit that completely cut off the power.  Probably the voltage had dropped too low to even power the protection circuit.  Also the pouch that contained the battery cells looked a little puffy.  It appeared that the battery cells might have released gas.  I was tempted to pop the pouch to vent the gas, but I resisted, thinking that it could possibly cause fire because of lithium built up.  I started to charge the battery directly at 100mA to 4.2V; it took about one hour.  And at about the half way, I switched the charging through the protection circuit and it charged fine the rest of the time.  Then I tested the battery by connecting it back to the headset.  It worked and seemed to hold charge.

So it is curious what happened to the battery.  And since the battery pouch is a little puffy, it is also a little worrisome.  I have to be careful to continue using it.

After some research online, it seems that the battery puffing is caused by excess oxygen which is released from the organic solvents in the electrolyte likely because of overcharging.

Tuesday, July 12, 2016

USB Serial Keyboard/Mouse

Previously, we made a PS2 keyboard emulator.  Here we are going to create a USB keyboard that makes easy to control another PC from a laptop.  (Alternatively, we can turn a tablet into a keyboard/mouse, that's a project for another time.) We'll use the USB serial port from the laptop to transmitt keystrokes to a microcontroller that provides the USB keyboard device interface.  The microcontroller module we choose is the TI's EK-TM4C123GXL LaunchPad.  It features the TI's Tiva series microcontroller with an 80MHz Cortex M4F core.  The Cortex M4F adds more computational capabilities to Cortex M3 with DSP and single precision floating number instructions.  The board actually has two Tiva M4C123 microcontrollers: one serves as the In-Circuit Debug Interface (ICDI) and a USB serial interface and the other is programmable and has a USB OTG/Host/Device port with a USB micro-A/B connector as well as the other pins broken out to the Launchpad BoosterPack XL expansion connectors: two 2x10 headers that include 35 signals and power/ground.  In addition, there are two pushbutton switches and 3-color LEDs.  The board cost is $12.99, a very capable board at a very attractive price.


TI Code Composer Studio (Ver 6.1.1) with TI ARM compiler 5.2.6 is used as the development tools and TivaWare C Series (2.1.2)  as the device library.  In the CCS, we create a new project by selecting the device TM4C123GH6PM, Stellaris In-Circuit Debug Interface and TI v5.2.6 compiler version.  The startup code and the linker commands are automatically generated.  The startup code sets up the interrupt vectors, including the reset handler which jumps to c_int00 to start the application.  The linker script sets up the memory allocation.  This device has 256K Flash and 32K SRAM.  At this point, we can make sure things are set up correctly by compiling the skeleton code.  We can also take a look at the default compiler options.

The first thing to add is the UART.  We simply borrow code from the examples in the TivaWare.  One of the interesting features of Tiva is that the TivaWare Peripheral Driver Library (DriverLib) code resides in the internal ROM.   These functions are prefixed with ROM_.   Also there are the MAP_ version of the functions that make it easy to switch between the ROM version and the Flash version. We have to define a compiler variable TARGET_IS_TM4C123_RB1 in order to resolve these functions (by mapping to ROM addresses).  Here RB1 refers to Silicon Revision B1; it is not clear how to tell the silicon revision.  If the flash version of the driver functions are used, driverlib.lib has to be linked in.  It is relatively easy to modify compilation options from the project properties.  As a debugging aid, we print out __DATE__ and __TIME__ macros so that we know if the microcontroller is flashed with the latest code.

Next is to initialize the USB in the device mode and set up as the USB HID device class.  Again we borrow from the examples.  Thanks to the usblib, it is fairly easy to set up the HID keyboard device.  Be sure to enable the USB peripheral and configure the pins and add USB0DeviceIntHandler to the interrupt vectors.  A callback function is defined to receive the events from the HID keyboard driver; the events includes connected/disconnected, suspend/resume, transmit complete.  Finally the key press and release are sent by calling the key state change function.  For starter, the ascii character received from the UART has to be translated into USB HID keyboard usage code and we have to transmit both key press and key release.  Only a subset of the usage code is defined in the TivaWare; the complete code can easily be found on the web.  Microsoft has a USB HID to PS/2 scan code translation table.  Once the USB is plugged in, to a computer, it is recognized as USB HID Keyboard and the key strokes are received as we type on a serial terminal.  I have to set the force device mode, otherwise it is not detected.  By default, the VBUS and ID pins are not monitored;   And as a side effect, the disconnected event is not reported in the mode.

Now we need to develop an application to transmit key press and key release in a raw form.  The USB keyboard reports up to 6 keys along with keyboard modifiers (CTRL/SHIFT/ALT/GUI).  We'll have to see if we need to send modifiers together with the keys or separately.  Also multiple bytes has to be sent, so we need a way to synchronize.  The serial protocol has to be a little more complex: three bytes may be needed, one for modifiers, one for key code and one to indicate key press or release.  Some unused bits may be used for sync.  Timing should also be used: the three bytes are transmitted together.  Tests indicate that the modifiers' states have to be transmitted with the keys, so the modifiers have to be maintained.  Also note that you may have to turn the legacy USB keyboard/mouse mode in the BIOS; that was the case for the GRUB running on one of my older computers.

A further modification is to turn it into a keyboard and mouse composite device.  We simply call the composite initialization functions, then we deal with two separate devices.   The mouse device reports the pointer delta movements and the button states.  Using Python tkinter, we bind ButtonPress, ButtonRelease and Motion events.  From the event coordinates, we calculate the delta movements.  Note that the deltas are signed 8-bit numbers.  In the Tk event, the left/middle/right buttons are numbered 1, 2 and 3 respectively.  But it appears that the middle and the right buttons are swapped at the receiving end.  It might be a configuration issue.  I tested it with both Linux and Windows; it worked fine.

In the future, more input devices can be integrated.

Another version is created using Cypress PSoC5LP kit CY8CKIT-059, a small $10 board, which is more convenient to carry.   The implementation effort is similar.  But I had to dig a little deeper into setting up the USB descriptors for a composite HID device and the endpoint programming.  Most of the USB device code is auto generated.

Tuesday, June 14, 2016

Power White LED Light Properly


The cheap LED light simply drives a parallel string of white LEDs with two or three batteries.  There is no other circuit elements, not even a current limiting resistor.  The most annoying problem is that the light gets dim fast as the battery voltage drops when there is still plenty of battery capacity left.  The proper way of doing it is a constant current boost converter,  LT1932 is such as an IC that comes in a small package, works down to 1V and is simple to hook up.  Here is the circuit diagram,

Here is the actual circuit board,


LT1932 can supply up to 40mA.  For higher current, we can take a boost regulator like LTC3429 (600mA 500KHz synchronous boost) and close the loop with current sensing.  The following circuit sources 120mA and works down to 1.8V.  It has about 90% efficiency.


The actual circuit board,


Another incarnation with higher current (250mA) and in the size of a flashlight bulb,



Thursday, May 26, 2016

Headphone/Microphone Combo Jack

The notebook computers nowadays tend to have only one audio jack, a headphone and microphone combo.  It has four connections and requires a 4-ring 3.5mm plug.  The pinouts are as follows




1. Tip - Left audio
2. Ring 1 - Right audio
3. Ring 2 - Ground
4. Sleeve - Microphone

The measurements on a computer show that Pin 4 is +3.6V relative to Pin 3 and can supply 1.86mA (equivalently about 2KOhm pullup),  Pin 1 to Pin 3  and Pin 2 to Pin 3 are 32 Ohms (this is surprisingly low).  When the common earphone is plugged, the sleeve is shorted to the ground, and the earphone works normally.  But a normal microphone usually has only two connection, the tip and the sleeve, so it would not work.  A 4-ring splitter is needed.  Only one audio input channel (mono) is possible.  The voltage on the sleeve is to supply the FET amplifier in an electret condenser microphone.

The audio input and output are sometimes useful as a signal generator and a data acquisition channel.  The frequency response is usually 20Hz - 20KHz.   The output can be up to 24 bits at 192KHz and the input up to 24 bits at 96KHz.  They usually have excellent noise characteristics.

Here is a 1000Hz sine wave generated by the PC headphone output with 48KHz sample rate and captured in by a NI DAQ module at 100KHz sample rate.  All this can be done in Python, using the sounddevice and PyDAQmx modules.  The volume control affects the amplitude; +/-2V seems to be the range for my computer.


And 0.2Vpp 1KHz sine wave captured by the microphone at 48000 samples per second, 
The microphone input seems to saturate around 0.4Vpp.  And the input should be AC coupled.

Sunday, May 22, 2016

Ideal Transistor

The basic solid state amplification devices are bipolar, JFET and MOSFET transistors.  All of them can be considered as transconductance device, which is a current source controlled by a voltage.

A bipolar transistor has a diode input structure: the input voltage between the base and the emitter sees a small signal resistance of $ V_T/I_b $, where $ V_T= kT/q $ is the thermal voltage, about 26mV at the room temperature and the $I_b$ is the base bias current, usually in the 10$\mu$A range, so the resistance $r_b$ is about $2.6K \Omega$.   A voltage $v_{be}$, that generates $i_b = v_{be}/r_b$, would cause a collector-emitter current $i_c = \beta i_b = \beta v_{be}/r_b$; so the transconductance is $g_m = \beta/r_b$. With $\beta$ around 100, the transductance is about 40mA/V. Another way to look at it is an ideal transistor with $r_e = 1/g_m$ at its emitter. An ideal transistor is to have an infinite transconductance, which implies that $v_{be}$ approaches zero.  Let's use this model for the three basic amplifier configurations.  The gain of the common-emitter amplifier with emitter degeneration, $- R_C/(r_e + R_E)$. $r_e$ is about $25 \Omega$, which is generally much less than $R_E$, so the gain simplifies to $-R_C/R_E$, independent of the transistor parameters.  For the common-collection amplifier or the follower, the gain is a simple voltage divider, $R_E/(r_e + R_E) = 1/(1 + r_e/R_E) \approx 1$.  For the common-base amplifier, the gain is $r_e/R_C$.

Now we have raised the concept of an ideal transistor, let's be more clear.  The transconductance is infinite.  Because we have been using the small-signal model, which is a linearized model around the bias point, we have ignored the biasing.  An ideal transistor would require no biasing, no input current into the base  and the emitter current is bi-directional, flowing both in and out the emitter.   Such an ideal transistor has been approximated by an integrated circuit that is called an operational transconductance amplifier (OTA) or a diamond transistor.   One example is Burr-Brown (TI)'s OPA860.  It has a high impedance input as the base and a bi-directional emitter and requires no bias.  The transconductance is adjustable, but is around 100 mA/V or equivalently $r_e = 10 \Omega$.  So the transconductance gain is not quite infinite, but $r_e$ is small enough compared to $R_E$.


Wednesday, April 20, 2016

USB Charger

When charging my tablet using a bench supply, I notice that it only draws 0.45A, staying within the 0.5A limit.  But the tablet is capable of charging at higher current.  After researching on line, according to Battery Charging Specification, I found if the data lines were short to a resistance of 200 Ohms or less, it would be detected as a Charging Downstream Port, which supplies a minimum current of 1.5A.  I connected a 130-Ohm between D+ and D-; the charging current now reaches 1.1A.
The tablet charging circuitry seems also to check the voltage and dials down the current if the voltage drops.  When I set the supply voltage to 5V (the voltage at the tablet is less because of the drop on the wires), the current drops to 0.84A and 0.43A at 4.5V.  It reaches max 1.13A at 5.2V.