halfluck
Posts: 13
Joined: Mon Sep 24, 2012 2:27 am

I2C, wiringPi & tea5767

Sat Aug 24, 2013 2:08 am

Hey Guys,

first post here I think!

I have a Raspberry Pi and a tea5767 (Philips FM radio receiver which works over I2C)

I have Installed the wiringPi library and it is detecting my tea5767 as 0x60

[email protected] ~ $ cd wiringPi
[email protected] ~/wiringPi $ gpio load i2c
[email protected] ~/wiringPi $ gpio i2cd

0 1 2 3 4 5 6 7 8 9 a b c d e f

00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
[email protected] ~/wiringPi $

Now I have a simple sketch working on my Arduino, i'd like to get going under WiringPi
==============

#include <Wire.h>

unsigned char frequencyH = 0;
unsigned char frequencyL = 0;

unsigned int frequencyB;
double frequency = 0;

void setup()
{
Wire.begin();
Serial.begin(9600);
}

void loop()
{
frequency=99.3; //starting frequency

frequencyB=4*(frequency*1000000+225000)/32768; //calculating PLL word

frequencyH=frequencyB>>8;

frequencyL=frequencyB&0XFF;

delay(100);

Wire.beginTransmission(0x60); //writing TEA5767

Wire.write(frequencyH);
Wire.write(frequencyL);
Wire.write(0xB0);
Wire.write(0x10);
Wire.write(0x00);
Wire.endTransmission();
}

===============================
I'm trying to rewrite it in C as below but it's not working i know i'm 90% there . Any ideas would be much appreciated :)

#include <wiringPi.h>
#include <wiringPiI2C.h>

#include <stdio.h>
#include <stdlib.h>

int main (void){
printf ("RPi - tea5767 Philips FM Tuner v0.1 \n") ;

unsigned char test[5] ={0};
int fd,e;
int dID = 0x60;

unsigned char frequencyH = 0;
unsigned char frequencyL = 0;

unsigned int frequencyB;
double frequency = 99.3;

frequencyB=4*(frequency*1000000+225000)/32768; //calculating PLL word
frequencyH=frequencyB>>8;
frequencyL=frequencyB&0XFF;

printf ("Frequency = ");
printf("%f",frequency);
printf("\n");

printf ("FrequencyB = ");
printf("%u",frequencyB);
printf("\n");

printf ("FrequencyH = ");
printf("%u",frequencyH);

printf ("FrequencyL = ");
printf("%u",frequencyL);
printf("\n");

// data to be sent

test[0]=frequencyH; //FREQUENCY H
test[1]=frequencyL; //FREQUENCY L
test[2]=0xB0; //3 byte (0xB0): high side LO injection is on,.
test[3]=0x10; //4 byte (0x10) : Xtal is 32.768 kHz
test[4]=0x00; //5 byte0x00)

printf("Info to send = ");
//printf("%u",test);
printf("%u", (unsigned int)test);
printf("\n");

//int wiringPiI2CSetup (int devId) ;
if((fd=wiringPiI2CSetup(dID))<0){
printf("error opening i2c channel\n\r");
}

//int wiringPiI2CWrite (int fd, int data) ;
;
if((e=wiringPiI2CWrite(fd,test))<0){
printf("error writing to slave\n\r");
}
return 0;
}

halfluck
Posts: 13
Joined: Mon Sep 24, 2012 2:27 am

Re: I2C, wiringPi & tea5767

Sat Aug 24, 2013 2:42 am

Got it working! Problem in bold!

#include <wiringPi.h>
#include <wiringPiI2C.h>

#include <stdio.h>
#include <stdlib.h>

int main (void){
printf ("RPi - tea5767 Philips FM Tuner v0.1 \n") ;

unsigned char test[5] = {0};
int fd,e;
int dID = 0x60; // i2c Channel the device is on

unsigned char frequencyH = 0;
unsigned char frequencyL = 0;

unsigned int frequencyB;
double frequency = 99.3; // Radio Station you would like to listen too TODO

frequencyB=4*(frequency*1000000+225000)/32768; //calculating PLL word
frequencyH=frequencyB>>8;
frequencyL=frequencyB&0XFF;

printf ("Frequency = ");
printf("%f",frequency);
printf("\n");

printf ("FrequencyB = ");
printf("%u",frequencyB);
printf("\n");

printf ("FrequencyH = ");
//printf("%u",frequencyH);
printf("%u", (unsigned int)frequencyH);
printf("\n");

printf ("FrequencyL = ");
//printf("%u",frequencyL);
printf("%u", (unsigned int)frequencyL);
printf("\n");

// data to be sent

test[0]=frequencyH; //FREQUENCY H
test[1]=frequencyL; //FREQUENCY L
test[2]=0xB0; //3 byte (0xB0): high side LO injection is on,.
test[3]=0x10; //4 byte (0x10) : Xtal is 32.768 kHz
test[4]=0x00; //5 byte0x00)

printf("Data to be sent = ");
//printf("%u",test);

printf("%u", (unsigned int)test);
printf("\n");

//int wiringPiI2CSetup (int devId) ;
if((fd=wiringPiI2CSetup(dID))<0){
printf("error opening i2c channel\n\r");
}

//write (fd, (unsigned int)test, 5) ;

//int wiringPiI2CWrite (int fd, int data) ;

if((e=wiringPiI2CWrite(fd, (unsigned int)test))<0){
printf("error writing to slave\n\r");
}
return 0;
}

trivalent
Posts: 5
Joined: Wed Jul 10, 2013 6:27 am

Re: I2C, wiringPi & tea5767

Tue Sep 10, 2013 2:05 pm

Hi halfluck,

So are you able to tune the tea5767 on raspberry PI and listen up to the radio? I am trying to do it from long but not able to get it done.

Please let me know if your source code works well and you are able to listen up radio. :) :) I would like to use your code with your permission then :)

halfluck
Posts: 13
Joined: Mon Sep 24, 2012 2:27 am

Re: I2C, wiringPi & tea5767

Tue Sep 10, 2013 10:44 pm

Hello Trivalent,

Yes it is working 100%, Been using it for the last couple of weeks!

Here is a link to the project http://halfluckbot.tumblr.com/post/5936 ... -up-an-old

First open a text editor, then type =

sudo nano radio.c

then paste the latest source code and save =

#include <wiringPi.h>
#include <wiringPiI2C.h>

#include <stdio.h>
#include <stdlib.h>

int main( int argc, char *argv[])
{
printf ("RPi - tea5767 Philips FM Tuner v0.3 \n") ;

unsigned char radio[5] = {0};
int fd;
int dID = 0x60; // i2c Channel the device is on

unsigned char frequencyH = 0;
unsigned char frequencyL = 0;

unsigned int frequencyB;
double frequency = strtod(argv[1],NULL);

frequencyB=4*(frequency*1000000+225000)/32768; //calculating PLL word
frequencyH=frequencyB>>8;
frequencyL=frequencyB&0XFF;

printf ("Frequency = ");
printf("%f",frequency);
printf("\n");

// data to be sent
radio[0]=frequencyH; //FREQUENCY H
radio[1]=frequencyL; //FREQUENCY L
radio[2]=0xB0; //3 byte (0xB0): high side LO injection is on,.
radio[3]=0x10; //4 byte (0x10) : Xtal is 32.768 kHz
radio[4]=0x00; //5 byte0x00)

if((fd=wiringPiI2CSetup(dID))<0){
printf("error opening i2c channel\n\r");
}

write (fd, (unsigned int)radio, 5) ;

return 0;
}

Compile the source code, by typing =

gcc -Wall -o radio radio.c -lwiringPi


Then whatever radio station you would like to listen to e.g. 92.9 type the following command =

sudo ./radio 92.9

Please let me know if you get stuck

Cheers & Goodluck Rob.

trivalent
Posts: 5
Joined: Wed Jul 10, 2013 6:27 am

Re: I2C, wiringPi & tea5767

Wed Sep 11, 2013 6:06 am

Thanks for prompt reply halfluck :)

I'll give it a try this weekend and let you know.

packetgeek
Posts: 5
Joined: Sun Jan 19, 2014 6:47 am

Re: I2C, wiringPi & tea5767

Sun Jan 19, 2014 6:53 am

Halfluck, thanks for the above work! It also works with a RDA5807SP-based receiver that I purchased on Amazon for less than $2. I gave you credit for the code over on: http://www.element14.com/community/blog ... spberry-pi

For everyone else, one of the key things in the above is compiling/installing wiringPi and running "gpio load i2c" before attempting to run halfluck's code. It appears to reset the i2c interface and causes the receiver to "listen up".

-pg

halfluck
Posts: 13
Joined: Mon Sep 24, 2012 2:27 am

Re: I2C, wiringPi & tea5767

Mon Jan 20, 2014 12:55 am

Hey PacketGeek,

Awesome Work! Glad I posted it now.

Mine is still chugging along beautifully. I've added a rather simple web frontend and mute feature.

http://halfluckbot.tumblr.com/post/5936 ... -up-an-old

Keep us posted.

Cheers Rob.

User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Re: I2C, wiringPi & tea5767

Mon Jan 20, 2014 9:33 am

Halfluck, thanks for the above work! It also works with a RDA5807SP-based receiver that I purchased on Amazon for less than $2.
This isn't surprising the RDA5807 was designed to provide backwards compatibility to the TEA5767 although some commands don't work and every time I requesting RSSI information the audio mutes temporarily.
Nevertheless the RDA5807 is vastly superior to the TEA5767, better sound quality, better noise immunity, can drive headphones directly (with DC blocking capacitors), bass boost, volume control, stereo blending level and soft mute with respect to signal strength (only available through using native code).

Richard S.

dacod
Posts: 2
Joined: Sun May 04, 2014 6:45 pm

Re: I2C, wiringPi & tea5767

Sun May 04, 2014 6:48 pm

worked like a charm :D , but the volume was low , any tips?

halfluck
Posts: 13
Joined: Mon Sep 24, 2012 2:27 am

Re: I2C, wiringPi & tea5767

Sun May 04, 2014 11:42 pm

Hi Dacod,

Great to hear, nice work!

I had the same issues, try these commands:

amixer cset numid=1 400 (for volume 100%)
alsamixer (and set the volumes accordingly ...
sudo alsactl store (store volume to survive reboot?)

Cheers Rob.

trivalent
Posts: 5
Joined: Wed Jul 10, 2013 6:27 am

Re: I2C, wiringPi & tea5767

Mon May 05, 2014 6:04 pm

Hi halfluck,

I tried your code, but can't get my radio working. All I hear is a buzzing sound.

Can you please help me to get it work. Also, I am trying to directly drive a JBL Creature 2.1 Speakers with Subwoofer with the L/R output from TEA5767. Is this advisable or I must have an audio amplifier first.

trivalent
Posts: 5
Joined: Wed Jul 10, 2013 6:27 am

Re: I2C, wiringPi & tea5767

Mon May 05, 2014 6:10 pm

Ok halfluck... I got it working finally... The code was OK... The audio output connection were not proper. Its working.

Thanks for the code. :) :) :) :) :)

dacod
Posts: 2
Joined: Sun May 04, 2014 6:45 pm

Re: I2C, wiringPi & tea5767

Thu May 08, 2014 10:33 pm

Halfluck,

thanks for your reply, possibly I'm still doing something wrong :-(

This is the output of amixer command

[email protected]:~# amixer cset numid=1 400
numid=1,iface=MIXER,name='PCM Playback Volume'
; type=INTEGER,access=rw---R--,values=1,min=-10239,max=400,step=0
: values=400
| dBscale-min=-102.39dB,step=0.01dB,mute=1


with the PC speaker, my mortal ears, hear nothing, however, with this speaker amplified C3, I can listen to the radio with a very low volume, exactly the same result

attached a picture of my circuit, thanks again

dacod
Attachments
2014-05-08 19.12.32.jpg
2014-05-08 19.12.32.jpg (58.63 KiB) Viewed 25444 times

User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Re: I2C, wiringPi & tea5767

Fri May 09, 2014 9:14 am

What amplifier are you using to drive the speaker, is this connected to L+R and Gnd, or L and R??

The TEA5767 output is very weak if your amplifier has low input impedance this could reduce the audio levels.

Perhaps the RDA5807 maybe a better alternative this chip can drive headphones (with AC coupling capacitors per channel) without additional amplification.

Richard S.

packetgeek
Posts: 5
Joined: Sun Jan 19, 2014 6:47 am

Re: I2C, wiringPi & tea5767

Thu Nov 20, 2014 9:46 am

I haven't had a chance to play with the RDA5807SP code lately but plan on getting back into it over the holidays (too many tests coming up between now and then). A couple people have asked for improving the 5807 code (e.g., adding a soft mute and scanning). In reading the 5807 docs, it looks like it's possible but I'm only guessing as the programming guide is written almost entirely in Chinese. Like your retro radio with the web interface. Looking to add a Jabber front end to mine.

packetgeek
Posts: 5
Joined: Sun Jan 19, 2014 6:47 am

Re: I2C, wiringPi & tea5767

Sun Jan 04, 2015 3:24 am

I now have working code for the following functions: tune, mute, unmute, search up, search down, and status. Halfluck did most of the work for me (his code above). I only had to translate to the proper wiringPi function and relearn some bitwise operators.

The bad news: I only recently found out that the 5807 chips have two modes, one for TEA5767 emulation and another mode with a larger set of functions. It means that I'm not yet done experimenting with the chip. The biggest obstacle appears to be finding an English-language programming guide.

The TEA mode is a good place to start if anyone's learning to write code. With the wiring library (wiringPi for the Pi), there's only a little extra work required (i.e., figuring out how to use AND, OR, and XOR functions and when to apply them (hint: if you use an OR function to set a flag, use the same value in an XOR function to unset the flag).

LinuxCircle
Posts: 26
Joined: Sat May 04, 2013 2:03 am
Contact: Website

Re: I2C, wiringPi & tea5767

Sun Aug 09, 2015 3:14 am

Guys, I'm doing my head-in (pulling my hair out, and banging my head on the project table :x ) when I try to write Python 3 code to drive Tea5767. I am begging you to save my head, by leading me to the right path with the following code:

Code: Select all

#!/usr/bin/python3 

import smbus as smbus 
import subprocess
import time

i2c = smbus.SMBus(1) # newer version RASP (512 megabytes) 
#bus = smbus.SMBus (0) # RASP older version (256MB) 

freq =  101.1# desired frequency in MHz (at 101.1 popular music station in Melbourne) 

# Frequency distribution for two bytes (according to the data sheet) 
freq14bit = int (4 * (freq * 1000000 + 225000) / 32768) 
freqH = freq14bit>>8 #int (freq14bit / 256) 
freqL = freq14bit & 0xFF 

data = [0 for i in range(4)]
                              # Descriptions of individual bits in a byte - viz.  catalog sheets 
add = 0x60 # I2C address circuit 
init = freqH # freqH # 1.bajt (MUTE bit; Frequency H)  // MUTE is 0x80
data[0] = freqL # 2.bajt (frequency L) 
data[1] = 0xB0 #0b10110000 # 3.bajt (SUD; SSL1, SSL2; HLSI, MS, MR, ML; SWP1) 
data[2] = 0x10 #0b00010000 # 4.bajt (SWP2; STBY, BL; XTAL; smut; HCC, SNC, SI) 
data[3] = 0x00 #0b00000000 # 5.bajt (PLREFF; DTC; 0; 0; 0; 0; 0; 0) 



i2c.write_quick(add)
time.sleep(0.1)
print(i2c.read_byte(add))
try:
  print(add)
  i2c.write_i2c_block_data (add, init, data) # Setting a new frequency to the circuit 
except IOError:
  subprocess.call(['i2cdetect', '-y', '1'])
  flag = 1     #optional flag to signal your code to resend or something
else:
  print("can work")

I tried the C version, it works smoothly, but with Python it always gives me the crazy IOError No 5. It turns on the chip, with noise on it and did not tune in to the desired frequency, along with the nice:
IOError: [Errno 5] Input/output error
I could also mute it if I change the init bit to 0x80, again along with the nice:
IOError: [Errno 5] Input/output error
I have also tried a more complete library posted here: https://github.com/pcnate/fm-radio-pyth ... tea5767.py

It again gives me the same IOError No 5:
[email protected] ~/Projects $ sudo python3 radio.py
BOARD 2
Traceback (most recent call last):
File "radio.py", line 4, in <module>
a.test()
File "/home/pi/Projects/tea5767.py", line 238, in test
self.writeBytes()
File "/home/pi/Projects/tea5767.py", line 200, in writeBytes
writing_bytes(self.address, 0x80, byteTwo,0xB0, 0x10,0x00)
File "/usr/local/lib/python3.2/dist-packages/quick2wire_api-0.0.0.2-py3.2.egg/quick2wire/i2c.py", line 78, in transaction
ioctl(self.fd, I2C_RDWR, ioctl_arg)
IOError: [Errno 5] Input/output error
None of the SMBus or Quick2Wire libraries work for me. I start to suspect it's got to do with Python 3 itself, or the bytes I'm attempting to send to the device.

When I run i2cdetect -y 1 it's all good, it shows the device available on address 0x60. The C code posted above in this forum proves nothing wrong with the physical pin wiring. It's just Python 3 that drives me almost nuts. :o Help?
www.linuxcircle.com

LinuxCircle
Posts: 26
Joined: Sat May 04, 2013 2:03 am
Contact: Website

Re: I2C, wiringPi & tea5767

Mon Aug 10, 2015 11:12 pm

Hi All

I think I solved my own problem with Python 3 I2C communication bug by implementing a workaround using while loop for sending a byte block command to change frequency until there is no more IO exception caught.

Oddly enough it takes between 15000-20000 continous and rapid attempts of writing into the device before one block of bytes hits and understood by TEA5757.

It could be because a bug in Python 3.2 or a bad compile of smBus library I am using. Any other i2c library sitting on top including C-based PigPio still relies on Python3 to interpret the bytecodes.

I can read and write to the device. It took around 1-1.5 seconds each time for TE5767 to respond to a frequency change command and tune in into the desired FM station eventually which is good enough for my purpose.

Confused but happy. :roll:
www.linuxcircle.com

Jan1
Posts: 42
Joined: Tue Nov 13, 2012 7:22 pm

Re: I2C, wiringPi & tea5767

Wed Aug 26, 2015 7:58 pm

@ LinuxCircle
I use a different module and use I2C to control it and also experience the same error.

Code: Select all

>>> bus.write_i2c_block_data(0x08,0x03,[0xAA,0xBB])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 5] Input/output error
Did you find a solution?

LinuxCircle
Posts: 26
Joined: Sat May 04, 2013 2:03 am
Contact: Website

Re: I2C, wiringPi & tea5767

Tue Sep 01, 2015 4:42 am

My solution was to install the latest smbus library, using:

Code: Select all

sudo apt-get update
sudo apt-get install python3-smbus
And then if there is discrepancies between the Pi and the device baudrates, I would just keep reading a byte from the device in a loop, to trigger the first connection, until a ready flag appears (or any other byte-code indicating a different result), then it would run smoothly afterwards.
www.linuxcircle.com

holaygrail
Posts: 1
Joined: Thu Dec 10, 2015 9:10 pm

Re: I2C, wiringPi & tea5767

Thu Dec 10, 2015 9:13 pm

is there a way to redirect the audio-output to hdmi?

User avatar
DougieLawson
Posts: 36308
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: I2C, wiringPi & tea5767

Fri Dec 11, 2015 7:39 pm

holaygrail wrote:is there a way to redirect the audio-output to hdmi?
Audio output defaults to HDMI if your TV/monitor supports it.

Take a look with a tvservice -a command.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

rohanxtreme
Posts: 4
Joined: Mon Feb 29, 2016 3:08 pm

Re: I2C, wiringPi & tea5767

Tue Dec 20, 2016 10:35 pm

Hi HalfLuck,
I'm a bit late to the party but thanks for the lovely article.

Thanks to your article and other code(s) given on @packetgeek article I was able to start with the project.

I'm using RPi3 with RASPBIAN JESSIE LITE and a TEA5767.

I was able to complete the connections and compile your code (thoe it gave one minor warning).
(screenshot attached- warning.png)

I am also able to detect using i2cdetect. (screenshot attached - i2cdetect.png)

The issue is all I can hear is static noise even when i'm entering the correct frequency of radio station.

Any idea what can be the culprit ? Is it because of the new updates to i2c feature in RPi3 or can it be because of my pathetic soldering skills (but i guess i2cdetect will not give proper output if any connecions / soldering was a issue rite?)

PS.. I am able to mute/unmute & get status using code from @packetgeek article too.

Thanks,
Rohan W.
Attachments
i2cDetect.PNG
i2cDetect.PNG (3.42 KiB) Viewed 12801 times
warning.PNG
warning.PNG (4.31 KiB) Viewed 12801 times

User avatar
DougieLawson
Posts: 36308
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: I2C, wiringPi & tea5767

Tue Dec 20, 2016 10:52 pm

Don't hold your breath waiting for a reply from Halfluck, he/she hasn't visited the forum since Thu 22 May 2014 @ 00:24
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

rohanxtreme
Posts: 4
Joined: Mon Feb 29, 2016 3:08 pm

Re: I2C, wiringPi & tea5767

Wed Dec 21, 2016 3:34 am

Oopsies !
Didn't see that [:(]

Lets hope someone else might have some pointers.. I'll keep exploring in the mean time...
Hopefully I'll answer my query by myself 8-)

Return to “Interfacing (DSI, CSI, I2C, etc.)”