3992
Teensy 3.0 Software Update « Adafruit Industries – Makers, hackers, artists, designers and engineers!
ssues Running 8 Bit AVR Code on 32 Bit ARM
Since I started working on Teensy 3.0 over a year ago, and especially in the recent months as more people have heard about the project, I’ve regularly been asked what types of problems arise when adapting 8 bit Arduino code to a 32 bit platform.

So far, there are been remarkably few issues involving the different size of int, pointers and other basic data types.  Most code is really quite well written.  The relatively small number of issues are usually caught by the compiler.  But one turned up today….

Jonathan Sorensen reported a problem that turned out to be code that compiles without error, works on 8 bit AVR, but fails when run on 32 bit ARM.  It’s a library from Pololu for their LSM303-based Inertial Measurement Unit.

Here is the code.  a.x is a float, and xha and xla are bytes from an accelometer.  Can you spot the bug, without peeking at the explanation below?

a.x = (xha << 8 | xla) >> 4;

The 2 bytes, xha and xla, were read using the Wire library.  Together they form a 2’s complement number.  This code reassembles the 2 bytes, divides by 16, and then assigns the result to the float.

Both AVR and ARM combine the two halves to the correct 16 bit result.  The hidden gotcha is the right shift operation.  You might expect this to simply shift the bits to the right, but what goes into those “empty” most significant bits?  It’s not necessarily zeros.  On AVR, it’s handled as a signed integer, so if the number is negative, the left-most 1 bit is replicated to the 4 empty bits.

On ARM, it’s also handled as a signed integer.  But instead of an integer with a range of -32786 to +32767, it’s an integer with range -2147483648 to +2147483647, which happens to be using a small 0 to 65535 portion of that huge 32 bit range. When the sensor reads positive, the result is correct.  But when negative, the result is a wrong positive number.

Even without the right shift, the same problem would occur while converting the integer to a float (and does indeed happen elsewhere in this library).  To the compiler, the unsigned logical or only becomes a signed integer if the result exactly matches the size of the result.  A type cast is needed, so the desired 16 bit signed integer is always used.

a.x = (int16_t)(xha << 8 | xla) >> 4;

Normally, even code that packs bytes into large variables works fine.  But depending on sign extension from an intermediate result with implicit type is a hidden gotcha, which probably isn’t obvious to many programmers.
arm  teensy  atmega
17 days ago
#!/bin/sh

# Disable the NMI watchdog
echo '0' > '/proc/sys/kernel/nmi_watchdog';

# Runtime power management for I2C devices
for i in /sys/bus/i2c/devices/*/device/power/control ; do
echo auto > \${i}
done

# Runtime power-management for PCI devices
for i in /sys/bus/pci/devices/*/power/control ; do
echo auto > \${i}
done

# Runtime power-management for USB devices
for i in /sys/bus/usb/devices/*/power/control ; do
echo auto > \${i}
done

# Low power SATA
for i in /sys/class/scsi_host/*/link_power_management_policy ; do
echo min_power > \${i}
done

# Disable Wake-on-LAN on ethernet port
ethtool -s wlan0 wol d;
ethtool -s eth0 wol d

#Enable Audio codec power management
echo '1' > '/sys/module/snd_hda_intel/parameters/power_save';

# Low power wireless
iw dev wlan0 set power_save on
4 weeks ago
Gamasutra - In-depth: Functional programming in C++
Probably everyone reading this has heard "functional programming" put forth as something that is supposed to bring benefits to software development, or even heard it touted as a silver bullet. However, a trip to Wikipedia for some more information can be initially off-putting, with early references to lambda calculus and formal systems. It isn't immediately clear what that has to do with writing better software.

My pragmatic summary: A large fraction of the flaws in software development are due to programmers not fully understanding all the possible states their code may execute in. In a multithreaded environment, the lack of understanding and the resulting problems are greatly amplified, almost to the point of panic if you are paying attention. Programming in a functional style makes the state presented to your code explicit, which makes it much easier to reason about, and, in a completely pure system, makes thread race conditions impossible.
functional
9 weeks ago

Copy this bookmark:

description:

tags: