January 27, 2020

STM32F407VET6 devel board from eBay: bad layout = adc noise

I'm measuring a potentiometer using 12-bit ADC on an STM32F4VE board I've got from eBay, and I get plenty of noise in range of about +/-30 units.

+/- 30 units is about 48mV: 60 * 3.3V / 4096 =0.048V.

I assume that ADC is very accurate. The datasheet claims typical accuracy of 2LSB and maximum of 5LSB, which would mean the error would range between 1.6 4mV (or +/-3 units). See "Table 68. ADC accuracy at fADC = 30 MHz" in the datasheet.

Searching on stackoverlow, eevblog and elsewhere on the internet suggests board layout problems.

Luckily the schematics for this board are available, and we can take a closer look:
https://os.mbed.com/media/uploads/hudakz/stm32f407vet6_black_sch.pdf.
The schematics shows a pair of decoupling capacitors between VDD and AMS1117.3-3, but shows no decoupling of VDDA from VDD: VDDA is simply tied to VDD. Same is true for VREF+:
Contrast this with the suggested wiring from the datasheet. VDDA, while is tied to VDD, it is also decoupled from VDD via a pair of caps: 100nF and 1uF:


Also, the datasheet states (emphasis mine):
Each power supply pair must be decoupled with filtering ceramic capacitors as shown above. These capacitors must be placed as close as possible to, or below, the appropriate pins on the underside of the PCB to ensure the good functionality of the device
The VDD is decoupled from the ground only on pin 19. Other VDD and VSS pins are connected to 3v3 and ground respectively. And the present decoupling caps don't appear to be placed as close as possible:

So what does all this amount to? Does this result in 48uV noise on VREF+? Here is how it shows on my scope:

Vpp = 51.6mV seems close enough to the 48mV noise measured by the ADC.

STMicroelectronics makes a development board for STM32F407: STM32F4DISCOVERY.
Let's take a look at how the discover board is wired:

While the discovery board doesn't have precision reference voltage, VREF+ and VDDA are properly decoupled from VDD. And here is the location of decoupling caps:
Many are much closer to the power rails than on the eBay board, but caps for the VDDA and VREF+ (C21, C23, C22, C25 in the top left corner) are at a similar distance.

I can't provide oscilloscope trace for comparison at this time, but I believe this wiring should provide lower noise on VREF+ line.

Mouser sells the discovery board for $20 + shipping $8. It has various peripheries among which are an audio DAC and a mic, LEDs, and a built-in debugger, has 1mb of program memory (twice as much as the eBay board). While overall cost appears to be 3 times more of the board from eBay, the quality of the board and extra options it provides are worth it.

However, eBay board served well: it made me learn so many things about ADCs, board layouts, and oscilloscopes. Low quality eBay components make electrical engineers out of software engineers.

November 19, 2012

How to change keyboard layout in xmonad

Here is my ~/.xsessionrc which allows to change keyboard layouts in xmonad by pressing both shift keys.

#!/usr/bin/env bash
setxkbmap -option  -layout us,ru \
-option grp:shift_toggle \
-option grp_led:scroll :2

This should also work for other window managers.

Beware: .xinitrc doesn't work in Ubuntu 12.04; make sure to put your personal X session commands into ~/.xsessionrc

January 18, 2011

Git Completion under Screen in Cygwin

I experienced that git tab completion doesn't work under screen in Cygwin.

1. I have git completion file placed at /etc/bash_completion.d/git
2. Git completion works under plain Cygwin shell (without screen)

To solve this problem, I added following line to my ~/.bashrc file
source /etc/bash_completion.d/git
And added shell definition to the /etc/screenrc file.
shell /bin/bash

No it works, hooray!

Strange thing is, that before any of my changes screen had shell /bin/sh
echo $0
/bin/sh


And /bin/sh and /bin/bash has same size:
$ls -la /bin/sh /bin/bash
-rwxr-xr-x 1 Alan root 470542 Aug 13 20:58 /bin/bash
-rwxr-xr-x 1 Alan root 470542 Aug 13 20:58 /bin/sh


And they have same content too, the diff command produces empty output:
$ diff /bin/sh /bin/bash

However, despite of this similarity, I cannot source git completion file under /bin/sh, but I'm able to do this under /bin/bash.

$ /bin/sh
$ source /etc/bash_completion.d/git
sh: /etc/bash_completion.d/git: line 123: syntax error near unexpected token `<'
sh: /etc/bash_completion.d/git: line 123: ` done < <(git config -z --get-regexp '^(svn-remote\..*\.url
|bash\.showupstream)$' 2>/dev/null | tr '\0\n' '\n ')'


$ /bin/bash
$ source /etc/bash_completion.d/git
$


Weird.

October 22, 2010

Cat all files in directory

$ cat catall 
for i in $(ls -rt1 `find -type f`);
do
# type file name in red color
echo -e "\033[31;1m$i\033[0m"
cat $i;
done;


This script won't work for files with whitespaces within their names

June 19, 2010

Just a reminder

Always investigate the error messages.

June 16, 2010

Google Closure menu with one selected item

Google Closure provides some UI widgets which you can use on your pages. One of such widgets is a menu widget.

In short, to produce a menu you'd need two pieces of code:

Here is an HTML piece:

<ul id="menu">
   <li class="goog-menuitem goog-option" id="item1">Item 1</li>
   <li class="goog-menuitem goog-option" id="item2">Item 2</li>
   <li class="goog-menuitem goog-option" id="item3">Item 3</li>
</ul>

And here is a JavaScript piece using Closure Library:
var menu = new goog.ui.Menu();
menu.decorate(goog.dom.getElement('menu'));


This will produce a menu which will maintain styles and dispatch events. The goog-option class makes menu items selectable. When a user clicks on an any element, the clicked element will get goog-option-selected style.
But when a user clicks on another item, that item's element will get this style too and you will end up with two selected elements. This is the default behavior and it is suitable for most of menu-like cases.

But what if you have a menu with single-select options, like a plain old HTML radio-group?
Then you need to handle selected items manually. I chose to implement a menu listener to manually deselect all previously selected elements:


goog.events.listen(menu, goog.ui.Component.EventType.ACTION, function(e) {
   /** @type {string} */
   var text = goog.dom.getTextContent(e.target.getElement());
   menu.forEachChild(function(c) {
       if (c != e.target) {
           c.setSelected(false);
           c.setChecked(false);
       }
   });
   caption.getElement().innerHTML = text;
} );

June 15, 2010

A script to compile Google Closure based project

Here is my script to build Google Closure based project: << EOF
#!/bin/sh

function showHelp() {
echo "
Usage:
$0 MAIN_JS_FILE [MODE];
MAIN_JS_FILE - A JavaScript file to compile.
MODE - If set to \"debug\" will omit compilation step and produce human readable script. This mode will not perform any checks on your JS code
Examples:

Compile in debug mode:
./compile.sh js/test.js debug

Compile with javascript checks and optimizations:
./compile.sh js/test.js
";
}

test -z "$1" && echo "MAIN_JS_FILE is required" && showHelp && exit 1;
test -f "$1" || echo "MAIN_JS_FILE is not a file" && showHelp && exit 1;

export dir=..
export compiler=$dir/compiler.jar
export closureLibraryHome=$dir/closure-library/closure
export calcdeps=$closureLibraryHome/bin/calcdeps.py
export target=./www/compiled.js

export mode="compiled"
test "$2" == "debug" && export mode="script"

echo running in $dir in $mode mode

python $calcdeps -i "$1" -p "js" -p "$closureLibraryHome" -o $mode --output_file="www/compiled.js" -c "$compiler" -f "--js_output_file" -f "$target" -f "--compilation_level" -f "ADVANCED_OPTIMIZATIONS" -f "--warning_level" -f "VERBOSE"

EOF