Editing Programming N800 FM radio receiver
Warning: You are not logged in.
Your IP address will be recorded in this page's edit history.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
+ | {{Midgard article}} | ||
+ | |||
+ | = How to control the FM tuner chip in the N800 = | ||
+ | |||
+ | == Introduction == | ||
+ | |||
The [[Nokia N800|N800]] internet tablet features a tea5761 FM tuner chip for listening | The [[Nokia N800|N800]] internet tablet features a tea5761 FM tuner chip for listening | ||
to FM radio. The kernel driver for this chip was written by Nokia and is GPL'd open source. You can look at it by downloading the Maemo kernel source code. | to FM radio. The kernel driver for this chip was written by Nokia and is GPL'd open source. You can look at it by downloading the Maemo kernel source code. | ||
Line 6: | Line 12: | ||
== How to do it in Python == | == How to do it in Python == | ||
- | By using | + | By using Pythons `ioctl` function, you can directly talk to the driver, so there's no need for wrapping some C library. |
+ | |||
=== Prerequisites === | === Prerequisites === | ||
Line 12: | Line 19: | ||
Let's first import the required modules: | Let's first import the required modules: | ||
- | |||
import os # for opening the device files | import os # for opening the device files | ||
import struct # for packing and unpacking C structs | import struct # for packing and unpacking C structs | ||
from fcntl import ioctl # for invoking ioctl calls on the device files | from fcntl import ioctl # for invoking ioctl calls on the device files | ||
- | + | ||
Since we are going to talk to kernel drivers, we have to use weird numbers. C users would just include the appropriate Linux kernel header files, but Python users have to do it by hand. So let's make some defines for kernel level ioctl stuff: | Since we are going to talk to kernel drivers, we have to use weird numbers. C users would just include the appropriate Linux kernel header files, but Python users have to do it by hand. So let's make some defines for kernel level ioctl stuff: | ||
- | < | + | <pre> |
# kernel definitions for ioctl commands | # kernel definitions for ioctl commands | ||
_IOC_NRBITS = 8 | _IOC_NRBITS = 8 | ||
Line 40: | Line 46: | ||
_IOR = lambda t,nr,size: _IOC(_IOC_READ, t, nr, size) | _IOR = lambda t,nr,size: _IOC(_IOC_READ, t, nr, size) | ||
_IOWR = lambda t,nr,size: _IOC(_IOC_READ | _IOC_WRITE, t, nr, size) | _IOWR = lambda t,nr,size: _IOC(_IOC_READ | _IOC_WRITE, t, nr, size) | ||
- | </ | + | </pre> |
- | The functions | + | The functions ''_IOW'', ''_IOR'', and ''_IOWR'' (macros in C) are used for write-access, read-access, and read-write-access, respectively. They basically just take some input values and compute a number which can be sent to the device driver. You can think of the number as the signature of the function which you want to call on the driver. |
Now we can generate those weird numbers for the V4L2 functions we are | Now we can generate those weird numbers for the V4L2 functions we are | ||
going to invoke on the driver: | going to invoke on the driver: | ||
- | < | + | <pre> |
# V4L2 stuff for accessing the tuner driver | # V4L2 stuff for accessing the tuner driver | ||
_VIDIOC_G_TUNER = _IOWR('V', 29, 84) | _VIDIOC_G_TUNER = _IOWR('V', 29, 84) | ||
Line 61: | Line 67: | ||
_V4L2_CID_BASE = _V4L2_CTRL_CLASS_USER | 0x900 | _V4L2_CID_BASE = _V4L2_CTRL_CLASS_USER | 0x900 | ||
_V4L2_CID_AUDIO_MUTE = _V4L2_CID_BASE + 9 | _V4L2_CID_AUDIO_MUTE = _V4L2_CID_BASE + 9 | ||
- | </ | + | </pre> |
And last, let's define some numbers for working with the mixer device: | And last, let's define some numbers for working with the mixer device: | ||
- | < | + | <pre> |
# mixer control constants | # mixer control constants | ||
_SOUND_MIXER_FMRADIO = 0x06 | _SOUND_MIXER_FMRADIO = 0x06 | ||
_SOUND_MIXER_READ = 0x80044D00 | _SOUND_MIXER_READ = 0x80044D00 | ||
_SOUND_MIXER_WRITE = 0xC0044D00 | _SOUND_MIXER_WRITE = 0xC0044D00 | ||
- | </ | + | |
+ | </pre> | ||
+ | |||
=== Opening the device === | === Opening the device === | ||
- | You gain access to the radio driver by opening the | + | You gain access to the radio driver by opening the ''/dev/radio'' |
+ | device. Since you need a system FD instead of a native Python FD, we use | ||
+ | ''os.open'' instead of the built-in function ''open'': | ||
- | |||
radio_fd = os.open("/dev/radio", os.O_RDONLY) | radio_fd = os.open("/dev/radio", os.O_RDONLY) | ||
- | |||
- | If there's no tuner chip available ( | + | If there's no tuner chip available (e.g. if you run this code on the 770 or the N810), this line will throw an `OSError`. Don't forget to catch it. |
+ | |||
=== Retrieving information about the FM tuner === | === Retrieving information about the FM tuner === | ||
- | Now it's time to talk to the FM tuner. We do this by placing an | + | Now it's time to talk to the FM tuner. We do this by placing an ''ioctl'' call on the device. ''ioctl'' calls consist of a number (those we have defined above), and some argument. The argument is a pointer for passing a C structure to the driver. The driver may read from this structure or write into it. |
- | A C structure can be built in Python with the | + | A C structure can be built in Python with the ''struct'' module. Because the following ''ioctl'' call only retrieves information from the driver, we may provide an empty structure of the right size, like this: |
- | |||
info = struct.pack("84x") | info = struct.pack("84x") | ||
- | |||
- | Let's place the | + | Let's place the ''ioctl'' call, and retrieve the results. For unpacking the |
+ | structure, we have to provide the correct format string (see ''help(struct)'' for details): | ||
- | < | + | <pre> |
data = ioctl(radio_fd, _VIDIOC_G_TUNER, info) | data = ioctl(radio_fd, _VIDIOC_G_TUNER, info) | ||
Line 109: | Line 117: | ||
signal = fields[8] | signal = fields[8] | ||
afc = fields[9] | afc = fields[9] | ||
- | </ | + | </pre> |
+ | |||
+ | We should save the ''tuner_index'' and ''tuner_name'' for later, because this is how we address that particular tuner. | ||
- | |||
=== Setting the frequency === | === Setting the frequency === | ||
- | Next, we are going to tune into a radio station. For this, we have to set a frequency on the tuner. This is done by the V4L2 | + | Next, we are going to tune into a radio station. For this, we have to set a frequency on the tuner. This is done by the V4L2 ''_VIDIOC_G_FREQUENCY'' ''ioctl'' invokation. |
- | But a word of warning first: the driver | + | But a word of warning first: the driver doesn't take the frequency in KHz. You have to multiply your value with a constant factor first. The factor is either 0.016 or 16, depending on the device driver. On the N800, it is 16: |
- | |||
FREQ_FACTOR = 16 | FREQ_FACTOR = 16 | ||
- | |||
We are going to tune into 107.6 MHz now: | We are going to tune into 107.6 MHz now: | ||
- | |||
freq = 107600 * FREQ_FACTOR | freq = 107600 * FREQ_FACTOR | ||
- | |||
Set up the C structure to pass to the driver: | Set up the C structure to pass to the driver: | ||
- | |||
data = struct.pack("LLL8L", tuner_index, tuner_type, freq, 0, 0, 0, 0, 0, 0, 0, 0) | data = struct.pack("LLL8L", tuner_index, tuner_type, freq, 0, 0, 0, 0, 0, 0, 0, 0) | ||
- | |||
Pass it to the driver: | Pass it to the driver: | ||
- | |||
ioctl(radio_fd, _VIDIOC_S_FREQUENCY, data) | ioctl(radio_fd, _VIDIOC_S_FREQUENCY, data) | ||
- | + | ||
=== Unmuting the tuner and setting the volume === | === Unmuting the tuner and setting the volume === | ||
- | You still | + | You still don't hear anything from the radio. This is because the tuner is muted and its volume is set to 0. Let's change this: |
Unmuting is easy: | Unmuting is easy: | ||
- | |||
data = struct.pack("Ll", _V4L2_CID_AUDIO_MUTE, 0) | data = struct.pack("Ll", _V4L2_CID_AUDIO_MUTE, 0) | ||
ioctl(radio_fd, _VIDIOC_S_CTRL, data) | ioctl(radio_fd, _VIDIOC_S_CTRL, data) | ||
- | |||
Changing the volume requires us to open the mixer device: | Changing the volume requires us to open the mixer device: | ||
- | |||
mixer_fd = os.open("/dev/mixer", os.O_RDONLY) | mixer_fd = os.open("/dev/mixer", os.O_RDONLY) | ||
- | |||
Now we can set the volume for the left and right speaker: | Now we can set the volume for the left and right speaker: | ||
- | |||
left_volume = 50 | left_volume = 50 | ||
right_volume = 50 | right_volume = 50 | ||
- | |||
... and send it to the driver: | ... and send it to the driver: | ||
- | |||
data = struct.pack("bb", left_volume, right_volume) | data = struct.pack("bb", left_volume, right_volume) | ||
ioctl(mixer, _SOUND_MIXER_WRITE | _SOUND_MIXER_FMRADIO, data) | ioctl(mixer, _SOUND_MIXER_WRITE | _SOUND_MIXER_FMRADIO, data) | ||
- | |||
- | + | Don't forget to close the mixer device after this: | |
- | |||
os.close(mixer_fd) | os.close(mixer_fd) | ||
- | |||
Now you can listen to the radio. | Now you can listen to the radio. | ||
+ | |||
=== Cleaning up === | === Cleaning up === | ||
Line 187: | Line 180: | ||
It's important to mute the radio, or you will hear it forever. | It's important to mute the radio, or you will hear it forever. | ||
- | |||
data = struct.pack("Ll", _V4L2_CID_AUDIO_MUTE, 1) | data = struct.pack("Ll", _V4L2_CID_AUDIO_MUTE, 1) | ||
ioctl(radio_fd, _VIDIOC_S_CTRL, data) | ioctl(radio_fd, _VIDIOC_S_CTRL, data) | ||
- | |||
And don't forget to close the device: | And don't forget to close the device: | ||
- | |||
os.close(radio_fd) | os.close(radio_fd) | ||
- | + | ||
+ | |||
== Code ready for use == | == Code ready for use == | ||
- | There is a pure Python module available for controlling the FM tuner. It | + | There is a pure Python module available for controlling the FM tuner. It's based on the information here. |
+ | You can get it from [http://pyfmradio.garage.maemo.org the PyFMRadio project page]. | ||
[[Category:Development]] | [[Category:Development]] | ||
[[Category:N8x0]] | [[Category:N8x0]] | ||
- | [[Category: | + | [[Category:Midgard wiki]] |
Learn more about Contributing to the wiki.