Day 13 of 30DaysOfCreativity - Radio PI part 5
Today I started working on the radio part of the raspberry pi radio. I hooked up an AR1010 based breakout board from sparkfun to the i2c ports of the raspberry pi and wrote a short python program to initialize the chip and tune it to a radio station.
It took me a while to figure out how to access the chip and after searching through tons of tutorials example-code and a really really really extremely bad datasheet I finally managed to get my code running.
The main reason why it didn´t work in the beginning was that I didn´t recognize that the smbus library sends long values in LSB,MSB order while the AR1010 wants them in MSB,LSB order :-/. Who would have thought that the littleendian/bigendian thing still exists in 2013
This is the code I use to initalize the radio after booting the pi
import smbus
import time
bus = smbus.SMBus(1)
adr = 0x10
init = [ 0xFFFB, 0x5B15, 0xD0B9, 0xA010, 0x0780, 0x28AB, 0x6400, 0x1EE7,
0x7141, 0x007D, 0x82C6, 0x4E55, 0x970C, 0xB845, 0xFC2D, 0x8097, 0x04A1,
0xDF6A ]
def swap( a ):
return (a & 0xFF) << 0x8 | (a >> 0x8)
def write( chan, d ):
bus.write_word_data( adr, chan, swap(d))
def read( chan ):
tmp = bus.read_word_data( adr, chan )
return swap( tmp )
def setbit( chan, bit ):
tmp = read( chan )
write( chan, tmp | 1 << bit )
def clearbit( chan, bit ):
tmp = read( chan )
write( chan, tmp & ~(1 << bit ))
for i in range( 1,18 ):
print i
write( i, init[i] )
write( 0, init[0] )
print "getstatus"
tmp = 0
while ( tmp == 0 ):
tmp = read( 0x13 ) & 0x20
and here is the code that tunes the radio to a frequency (103.8 in this case)
import smbus
import time
bus = smbus.SMBus(1)
adr = 0x10
def swap( a ):
return (a & 0xFF) << 0x8 | (a >> 0x8)
def write( chan, d ):
bus.write_word_data( adr, chan, swap(d))
def read( chan ):
tmp = bus.read_word_data( adr, chan )
return swap( tmp )
def setbit( chan, bit ):
tmp = read( chan )
write( chan, tmp | 1 << bit )
def clearbit( chan, bit ):
tmp = read( chan )
write( chan, tmp & ~(1 << bit ))
clearbit( 1, 1)
clearbit( 1, 2)
time.sleep(1)
f = 1038
clearbit( 2, 9 )
setbit(1,1)
print "freq"
tmp = read( 2 )
print hex(tmp)
tmp = tmp & 0xFE00
tmp = tmp | ( f - 690 )
print hex(tmp)
write( 2, tmp )
print "tuneon"
setbit( 2, 9 )
print "getstatus"
tmp = 0
while ( tmp == 0 ):
tmp = read( 0x13 ) & 0x20
print bin(tmp)
clearbit(1,1)
Please note that this code is not considered production ready. I will try to access more advanced features of the radio module tomorrow and will publish the final radio part code later
See also:
RadioPI - debugging
RadioPI hardware part 1
RadioPI - finalizing the audio module
Day 30 of 30DaysOfCreativity - RadioPI interface module