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
Geoff's site: Thinkpad X62

# 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}

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

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

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

# 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
linux  laptop  thinkpad 
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.
9 weeks ago
« earlier      
2010 3d actuator actuators adc ajax amp amplifier arduino art ascii asterisk atmega attiny audio avr backup bash berlin bib bike bittorrent blenano blog bluetooth bm bob book books btle businesscard c ca cable calculator car cave cg clock cnc code collaboration color conductive connectors cook cooking css cv da dance dart339 db debian design diy django dnb dns dockstar doku driver eagle electronics embedded ems erm fabric filetype:pdf filter final flash fm fonts food fritzbox fun fusion gallery games gentoo git google gtd hack haptic haptics hardware hexayurt hiking howto html html5 human_powered i2c iceland images immigration imu insole insoles inspiration ir jabber java js kalman kde keyboard kinect la lab lasercut latex learning led linux lipo logic_analyzer lra mail make manufacturing map maps marg markdown math matplotlib matrix mcgill media:document media:image micromag3 ml montreal multichannel music muzaq n900 network neuro neuroplasticity news nixie nrf51 openni opera osc oscilloscope osx packliste parts pcb pdf performance pgmka phd philly photo photography photos piezo plot poster power presentation presentations pressure productivity programmer programming pwm python r raspi rbg recipes reference repair reqs rss ruby sc science search security sensor sensors sensory_substitution serial share shell shop slackline sleep slides smalarm smartlet software solderinskaters spi ssh statistics stereo storage studio synthesis tables tactile teensy terminal text thinkpad time tm tobuy toloan tools toread toshop towatch transmitter travel tutorial tw typography ubuntu uni unix usb vcs video vim vis visualization voip vpn w2b watchman wearables weather web webdesign wiki windows wire wireless wood wordpress work writing wrt x100s x240 yoga

Copy this bookmark: