1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
/*
* poll i2c RDS receiver [Philips saa6588]
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/i2c.h>
int debug;
/* ----------------------------------------------------------------- */
char rds_psn[9];
char rds_txt[65];
void rds_decode(int blkno,int b1, int b2)
{
static int group,spare,c1,c2;
switch (blkno) {
case 0:
if (debug)
fprintf(stderr,"block A - id=%d\n",
(b1 << 8) | b2);
break;
case 1:
if (debug)
fprintf(stderr,"block B - group=%d%c tp=%d pty=%d spare=%d\n",
(b1 >> 4) & 0x0f,
((b1 >> 3) & 0x01) + 'A',
(b1 >> 2) & 0x01,
((b1 << 3) & 0x18) | ((b2 >> 5) & 0x07),
b2 & 0x1f);
group = (b1 >> 3) & 0x1f;
spare = b2 & 0x1f;
break;
case 2:
if (debug)
fprintf(stderr,"block C - 0x%02x 0x%02x\n",b1,b2);
c1 = b1;
c2 = b2;
break;
case 3:
if (debug)
fprintf(stderr,"block D - 0x%02x 0x%02x\n",b1,b2);
switch (group) {
case 0:
rds_psn[2*(spare & 0x03)+0] = b1;
rds_psn[2*(spare & 0x03)+1] = b2;
if ((spare & 0x03) == 0x03)
fprintf(stderr,"PSN #>%s<#\n",rds_psn);
break;
case 4:
rds_txt[4*(spare & 0x0f)+0] = c1;
rds_txt[4*(spare & 0x0f)+1] = c2;
rds_txt[4*(spare & 0x0f)+2] = b1;
rds_txt[4*(spare & 0x0f)+3] = b2;
if ((spare & 0x0f) == 0x0f)
fprintf(stderr,"TXT #>%s<#\n",rds_txt);
break;
}
break;
default:
fprintf(stderr,"unknown block [%d]\n",blkno);
}
}
int
main(int argc, char *argv[])
{
int c,f,rc, no, lastno = -1;
unsigned char b[40];
char *device = "/dev/i2c-0";
/* parse options */
while (-1 != (c=getopt(argc,argv,"hvd:"))) {
switch (c){
case 'd':
if (optarg)
device = optarg;
break;
case 'v':
debug = 1;
break;
case 'h':
default:
printf("poll i2c RDS receiver [saa6588] via chardev\n");
printf("usage: %s [ -d i2c-device ]\n",argv[0]);
exit(1);
}
}
if (-1 == (f = open(device,O_RDWR))) {
fprintf(stderr,"open %s: %s\n",device,strerror(errno));
exit(1);
}
ioctl(f,I2C_SLAVE,0x20 >> 1);
for (;;) {
memset(b,0,sizeof(b));
rc = read(f,b,6);
if (6 != rc) {
fprintf(stderr,"oops: read: rc=%d, expected 6 [%s]\n",
rc,strerror(errno));
break;
}
if (0 == (b[0] & 0x10)) {
fprintf(stderr,"no signal\r");
continue;
}
if (1 == (b[0] & 0x08)) {
fprintf(stderr,"overflow detected\n");
}
if (1 == (b[0] & 0x04)) {
fprintf(stderr,"reset detected\n");
}
if (debug)
fprintf(stderr,"raw: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
b[0],b[1],b[2],b[3],b[4],b[5]);
no = b[0] >> 5;
if (lastno != no) {
rds_decode(no, b[1], b[2]);
lastno = no;
}
usleep(10*1000);
}
close(f);
exit(0);
}
|