Archive for the ‘Uncategorized’ Category

Broken mac power supply

September 1, 2013

One of my computers, the one I use for web browsing is a Mac. Despite that, I’ve installed Linux on it and use it instead of Mac OS almost all the time, but this is another story.

Anyway, a few days ago, my Mac simply stopped charging. I was on holiday, so I had to wait until I got back home to troubleshoot what happened. Measuring the output voltage resulted in a zero volt reading, so I opened it up.

macbug-big

Inside I found a burnt resistor, and a blown fuse. The resistor appears to be the bleeder resistor in parallel with the input capacitor. It has likely failed resulting in a short circuit, and the fuse blew.

Now, Apple chargers are usually touted for their high quality compared to cheap chinese clones, however I wonder who thought that putting a tiny 0805 resistor in parallel on a high voltage line was a good idea…

Sure, the charger worked reliably for five years (I bought the Mac in 2008), and failed in a rather safe way (mainly thanks to the fuse) but still I think this failure mode could have been somehow forecasted, also considering that this is not the first time I see it. The other time was in an inverter, a device that produces a 230VAC output given a 12VDC input. The step-up circuit had a diode bridge rectifier, a capacitor, and a tiny 1/10W resistor (it was entirely through-hole, no SMD). One day I turned the inverter on, and heard a loud pop and a flash of light through the heat vents.

In the meantime I had to find a way to somehow power the Mac again. I cut the power cable and connected it to the only device I had lying around that could produce 16.5V at 4.6A… a cheap chinese power supply. That’s ironic, to say the least…

macfix

Synchronization primitives in Miosix

October 6, 2012

A small and backwards compatible change in Miosix 1.61 made me think about low-level synchronization primitives, and the fact that doumentation on how to use them was lacking. So, here’s a page dedicated to this topic.

Serial port classes moved to Gitorious

July 11, 2011

When I first published this article about using serial port with C++ I didn’t expect it to become one of the main reasons people visit my website. Over time, thanks to many comments, bug reports and suggestion, the serial port classes have been modified significantly, to the point that releasing them in a .tar.gz file is no longer a good idea.

I’ve therefore decided to create a git repository at http://gitorious.org/serial-port where you can get the latest version of my serial port classes.

Improving battery lifetime of Le Dominoux

April 26, 2011

Some time ago I saw on Hack a Day this post about Le Dominoux, an electronic based version of dominoes. At first it didn’t seem much fun, but watching the video I quickly changed my opinion.

In the end, the author also measures the power consumption of the device when the led is not lit, showing it’s around 100uA, and it’s from there that I thought this little device might be improved a bit.

Now, since this device was designed for a 555 contest, the author had to use a 555 IC, and given this constraint I think he has done a good job in optimizing it for low power, using a CMOS variant of the 555 and high brightness leds.

But set aside that constraint, it would be easy to optimize it event further. First of all let’s check the battery lifetime of the original device. Since the power cosumption when the led is lit (probably 5 to 10mA) differs greatly from the standby value of 100uA, battery lifetime largely depends on how much you play with it, but an upper bound is easy to calculate, and it’s 200mAh (CR2032 battery capacity) / 100uA = 2000h / 24 = 83 days. This isn’t very good since even if you almost never play with it, after ~80 days the battery is dead.

One possibility to improve it is to use standard 40xx CMOS logic gates to design the one shot timer, like this:

The fun of CMOS gates is that other than being very versatile, they draw next to zero current when they’re not switching. I built this circuit to measure the power consumption, ant that’s the result:

As you can see, it is drawing just 4uA. The actual standby current depends on how much light is shining on the phototransistor. For example in the dark it draws just 1uA, while if a bright light is pointed at the phototransistor it draws up to 300uA, which is 3V(battery voltage) /10K(R3). This happens also in the original author’s schematic, since the input stage with the phototransistor and the 10K resistor is the same, so that’s no worse than the original circuit. Anyway, in normal ambient light conditions very few light reaches the phototransistor, and the current is very low, around 5uA.

This gives an upper bound to the battery lifetime of 200mAh / 5uA = 40000h / 24 / 365 = 4.5 years!

Last notes about this circuit:

  • it uses the same number of components as the original one, just replaces the 555 with a 4011.
  • you have to connect pin 7 of the 4011 to ground, and pin 14 to Vcc, even if in the schematic these connections are omitted.

Fail over fail

January 7, 2011

Even more rant this time.

To go on programming microcontrollers as I’ve always done I have to disable two programs, gpsd and modem-manager (see my previous post) because hardware manufacturers fail. The first one, gpsd is done. To fix the second one without killing/removing NetworkManager I have to contact ModemManager developers. To do so I have to register to launchpad. To do so I have to get an OpenId. To do so I have to… find a way to avoid another fail:

See on the bottom of that webpage? “Type the two words into the field below”. I’d be glad to do that if there were two words, and one field. The HTML source of the page seems to indicate that a sort of captcha is supposed to be there, bu there isn’t… and without it I can’t complete the registration…

But wait, there’s more fail:

A quick look at http://openid.net/get-an-openid/ and I found out that since I have a wordpress blog I already have an OpenId. I tried to log in with that, but it failed. A look at the FAQs for launchpad and:

Can I log into Launchpad with my existing OpenID?

Not at this time. Launchpad is currently an OpenID Provider (OP) and not a consumer (RP).”

And that, in my opinion, is fail.

Linux today: too much plug and play

January 7, 2011

This title is probably not what you’d expect. Usually people complain for the opposite reason: they buy some hardware, connect it to Linux and it does nothing, so they have to spend time in forums to see what can be done even if manufacturers don’t officially support Linux.

In my case, it’s the plug and play system itself that is causing me problems, because it incorrectly identifies a device as another type of device…

But let’s start from the beginning. I’m an embedded developer, and therefore I deal with microcontrollers. Often, the easiest way to debug, program, and communicate with a microcontroller is a serial port. Given that serial ports disappeared from computers long ago, USB to serial adapters have been introduced to solve this problem. These are all tiny chips like the FT232 or PL2303 that connect upstream as USB and downstream as serial ports.

These chips can be used either as standalone products, like the blue dongle at the left of this picture which is PL2303 based

or directly embedded into the device. For example, the Miosix board that I designed has an FT232 on board. It’s the chip at the bottom right, next to the USB connector

Now, both these devices identify as serial ports, using the default VID and PID (which are the identifiers of the USB protocol) of the manufacturer. They do so because they are serial ports, the first one is obvious to see, but also the second, since I don’t need any program to automatically start when I plug one of those, just to sit there and wait till I open screen or minicom to watch debug messages and the like.

Now, I’ve recently upgraded my computer to Fedora 14. It works very well, except for serial ports. At first I noticed that immediately after connecting a USB/serial device, trying to talk to it resulted in errors. “Device ore resource busy”, it said. Retrying after a couple of seconds fixed this, but then it looks like the bootloader of the microcontroller (the first program that starts) has been upset by something, so the first programming cycle failed, the next one went well, and all the subsequent too. At least until the USB/serial adapter is disconnected and reattached, in this case the issue presents again.

What could it be?, I thought. Maybe a bug in the kernel driver for serial ports? It turned out to be something entirely different.

To understand what was happening, first we have to talk about crappy GPS mice and GSM usb sticks. Both have someting in common: those who manufacture the GSM/GPS module are different from people who design the final product. The GSM/GPS modules, for convenience, spit out their data through a serial port. But given that PCs have USB ports only, final product designers add an USB/serial converter into the device. Now, the issue is, that they don’t care about changing the default VID and PID of the USB/serial adapter. Therefore when plugged to a PC they appear as serial ports despite their products are not serial ports. A GPS can only understand NMEA sentences (plus some weird binary protocol), while a GSM “internet key” can only understand AT commands. And what’s worse, they require plug and play, they require a program that attaches to these devices as soon as they’re plugged in.

Linux on the other side, is growing fast even as a desktop operating system. So it’s natural that it is starting to support more and more hardware, in the easiest possible way. But how can it cope with GSM/GPS USB sticks?

Simple: for GPSes there’s a program, gpsd, that when a new device is connected, its VID and PID are checked against a list of devices, and if matches it tries to send NMEA sentences. If it responds, ok, otherwise the device is discarded. The same happens for GSM devices, there’s another program: ModemManager that does the same thing with AT commands.

But given that most devices appear as a PL2303 serial port, how can these programs know if after the PL2303 there’s a GPS unit, a GSM “internet key”, or none of these? Simple, they don’t. They try for a while to talk and if the device on the other side doesn’t seem to be what they expect, they give up.

This explains why trying to connect my USB/serial adapter to the PC and immedately open screen or minicom resulted in “device or resource busy”, while retrying a couple of seconds later worked.

The issue I’m having with the bootloader, is instead caused by the fact that these little program don’t just open the serial port and listen, they open the port and talk! The bootloader of my board probably gets upset by AT commands, or NMEA sentences or both.

But then I wonder, does a GPS unit like AT commands, or conversely, does a GSM internet key like NMEA sentences? This is trouble waiting to happen. Also on Windows, I suppose. Assume one buys a cheap GSM unit that identifies as a PL2303, and a cheap internet key that identifies too as PL2303, what does the official Windows drivers that come with them do? Conflict, I guess.

The first attempt to fix my issue has been to uninstall gpsd and modem-manager. Unfortunately, on Fedora, the whole KDE depends on gpsd, so it can’t be unistalled. I therefore contacted the gpsd developers on the mailing list. They have been very helpful in finding a workaround (they even blogged about it as I’m doing now, here), which is to download the gpsd sources and issue these commands:

./configure
make udev-uninstall

which leaves gpsd installed, but removes udev rules that cause the plug and play. It’s also interesting that if I ever buy a GPS unit I can still start gpsd manually. Nice.

The fun thing is that gpsd developers were not aware that some other opensource project, ModemManager, was lurking for the same USB VID and PIDs…

I still haven’t found a way to disable modem-manager. It is a daemon, but it auto-respawns when killed, there’s no “service modem-manager stop” and trying to uninstall it would also get rid of NetworkManager.

Ofcouse, the only way to fix the whole problem is that hardware manufacturers should change the VID and PID so that a GPS, a GSM modem and a serial port can be uniquely identified…

Update:

I found a way to disable modem-manager while leaving NetworkManager:

sudo service NetworkManager stop
sudo killall modem-manager
cd /usr/sbin
sudo rm modem-manager
sudo service NetworkManager start

Ofcourse any GSM internet key would probably stop working, but for now It’s enough. I can finally connect an USB/serial adapter and nothing is trying to speak with it by default. Finally.

Handling PNG images in C++

October 5, 2010

This blog post is about png++, one of my favourite C++ libraries.

It’s a wrapper of libpng in C++ that allows to handle PNG images so easily that you’ll never want to use bare libpng again.

All you need to start is to add these two lines in you source files:

#include "png++/png.hpp"
using namespace png;

This is the code to open an existing image:

image img("image.png");

The code to make a new image from scratch differs only in the parameter passed to image’s constructor:

image img(1024,768);

The image class has the get_pixel() and set_pixel() memeber functions to access the individual pixels:

rgb_pixel pixel=img.get_pixel(0,0);
pixel.red=min(255,pixel.red+10); //Increase red color of pixel
img.set_pixel(0,0,pixel);

The image height and width can be obtained with intuitive get_height() and get_width() memeber functions:

for(int i=0;i<img.get_width();i++)
{
    for(int j=0;j<img.get_height();j++)
    {
        //Do something
    }
}

The image can be written to file with the write() memebr function:

img.write("new_image.png");

Also, this library is header-only. What does it means? That you do not have to compile the library to start playing with it, just exptract the library in the folder where you have your source code, and include the header file “png.hpp”, just that.

Lastly, here is an example code including CMake scripts to show the capabilities of the library. png++.tar.gz

Digital voltmeter for power supply

October 2, 2010

This summer I finally found some time to fix my power supply.

It’s a rather old but relieable unit, and I have no intention of replacing it. Basically, it still works except for the voltage meter on the front panel. Over time, the pointer developed an offset of around 1V, which is visible in this image where the power supply is turned off. Instead of indicating exactly zero volt, the pointer is below the beginning of the scale.

This, together with the fact that today’s microcontroller require 3.3V to operate (while the voltmeter only has an 1V resolution), forced me to always use a multimeter when using it, to be able to precisely set the output voltage.

The solution I found was to simply replace the analog voltmeter with a digital one. Instead of using a voltmeter chip like the ICL7107 that usually require the measurement ground to be separated from the supply ground, these days it is easier to build a voltmeter using a microcontroller.

That’s because even the cheap and simple micros now have at least a 10 bit ADC which is more than enough for a voltage meter in the rage 3..15V (which is the range of my power supply). Since the task is easy there was no need to use an ARM microcontroller as I usually do, but instead an ATtiny26 proved more than enough, despite only having 2KBytes of FLASH and 128Bytes of RAM.

This is the result:

The circiut is simple, a 78L05 is used to reduce the 20V found in the power supply to 5V to power the microcontroller. A voltage divider made with 1% precision resistors is connected from the power supply input to an ADC capable GPIO on the microcontroller, and three LED displays show the voltage with 0.1V resolution.

The LED displays are ofcourse multiplexed so that the ATtiny, despite its low number of GPIOs, can drive the display with no other glue logic except for current limiting resistors.

Around 100 lines of C++ code keep the whole thing working.

Back online

June 21, 2010

Because of an issue with the hosting service, my website (together with other websites on the same provider) has been unavailable for three days. Now the problem is fixed and I’m back online.

CMake part 3: Finding libraries

March 7, 2010

Over time, I find myself more and more interested in CMake. That’s because I write many little programs, and those that use makefiles routinely break as I switch OS.

The last one was a program that depended on libpng. When I tried to compile it on Mac OS X, it failed to find the library. That’s because on Linux libpng.so is simply in /usr/lib and png.h is in /usr/include, both of which are in the compiler’s search path, so that all you need to do is add “-lpng” when linking.

But on a Mac libpng.dylib (yes, shared libraries on a Mac have .dylib extension) is in /usr/X11/lib and png.h is in /usr/X11/include that are not in the compiler’s search path, so that when compiling you need to add “-I/usr/X11/include” and when linking “-L/usr/X11/lib -lpng”.

The solution was not to keep two separate makefiles, but rather to throw away the makefile and replace it with a CMakeLists.txt

Which brings us to the question: how to link with libraries using CMake?

There are two ways, the first is the find_package command, the other is the find_library command.

Let’s start with find_package. CMake “knows” about many widely used libraries. For them, there is a script to find them in all supported platforms. So, to use a library all you need to do is find it with the find_package command. Here is a simple example of a program that uses threads and so depends on “-lpthread”, main.cpp:

#include <iostream>

using namespace std;

void *thread(void *argv)
{
    cout<<"Into a spawned thread"<<endl;
}

int main()
{
    pthread_t t;
    pthread_create(&t,NULL,thread,NULL);
    pthread_join(t,NULL);
    cout<<"Back in main thread"<<endl;
}

and here is the CMakeLists.txt file:

cmake_minimum_required(VERSION 2.6)
project(TEST)

## Target
set(TEST_SRCS main.cpp)
add_executable(test ${TEST_SRCS})

## Link libraries
find_package(Threads REQUIRED)
target_link_libraries(test ${CMAKE_THREAD_LIBS_INIT})

As can be seen, the first parameter passed to find_package is the name of the package, the second is “REQUIRED” and means that if the library could not be found, CMake should stop and print an error message.

Once the library is found, you have to say which executable needs it (because a single CMakeLists.txt can be used to produce many executable by just using more add_executable commands). This is achieved with the target_link_libraries command that appends a library to the list of libraries an executable needs. The first parameter is the executable name, the second is the library. Note that find_library generates a variable that contains the name of the library, in this case the name is CMAKE_THREAD_LIBS_INIT. This strange name is an exception, usually all find_package scripts create a variable with the name <libraryname>_LIBRARY.

Now a more complex example: the libpng issue I talked about earlier. It is more complex because you don’t just need to add a library when linking, you also need to tell the compiler where is the png.h file when compiling. Luckily, CMake has a package for libpng that does all that, and here is the CMakeLists.txt example:

cmake_minimum_required(VERSION 2.6)
project(TEST)

## Targets
set(TEST_SRCS test.cpp)
add_executable(test ${TEST_SRCS})

## Link libraries
find_package(PNG REQUIRED)
include_directories(${PNG_INCLUDE_DIR})
target_link_libraries(test ${PNG_LIBRARY})

The find_package command finds the PNG library, target_link_libraries adds the library to the list of libraries the executable needs, and include_directories adds the directory where the .h file is when compiling.

But this isn’t the end. Other than libraries there are collections of libraries. And CMake supports them too. One example are the boost libraries. There is no single libboost.so to link to; instead every sub-library has its .so file. So there should be a way to link only with the desired sub-libraries. This is an example CMakeLists.txt that does that:

cmake_minimum_required(VERSION 2.6)
project(TEST)

## Target
set(TEST_SRCS main.cpp)
add_executable(test ${TEST_SRCS})

## Link libraries
set(BOOST_LIBS thread date_time system)
find_package(Boost COMPONENTS ${BOOST_LIBS} REQUIRED)
target_link_libraries(test ${Boost_LIBRARIES})
find_package(Threads REQUIRED)
target_link_libraries(test ${CMAKE_THREAD_LIBS_INIT})

In this case we initialize a variable with the sub-libraries we want (boost.thread, boost.date_time and boost.system). Then we call find_package with the library name (Boost), the word COMPONENTS followed by the list of sub-libraries and as usual the REQUIRED word. Since boost.thread depends on the system’s thread library, we also use another find_package command to link with threads.

This ends the examples of find_package, but there is one last issue: what if we need a library for which there isn’t a package script? The solution is to use the find_library command. It will search in the system paths for the needed library. Here is an example that uses the command to find the Poco libraries:

cmake_minimum_required(VERSION 2.6)
project(TEST)

## Target
set(TEST_SRCS main.cpp)
add_executable(test ${TEST_SRCS})

## Link libraries
find_library(POCO_FOUNDATION PocoFoundation)
find_library(POCO_NET PocoNet)
target_link_libraries(test ${POCO_FOUNDATION} ${POCO_NET})
find_package(Threads REQUIRED)
target_link_libraries(test ${CMAKE_THREAD_LIBS_INIT})

The find_library command takes two parameters, the first is the variable where the found library will be stored, and the second is the library name (the name is camelcase in this example because Poco libraries are camelcase, the library name is really libPocoFoundation.so).
References: CMake wiki


Follow

Get every new post delivered to your Inbox.

Join 29 other followers