lundi 18 avril 2011

Adding a PCA9555 GPIO extender on a Linux based machine

Today I'm adding a PCA9555 http://www.nxp.com/documents/data_sheet/PCA9555.pdf to my Blackfin based board.

The PCA9555 is a 16 GPIO extender on I2C bus.

The new linux GPIO lib is very smart and easy to extend. What you need to do is just wire the chip on the I2C bus (sometimes called TWI), and add the configuration in your machine code.

To be added to your arch/xxx/mach-xxxx/mach-mymachine.c :

to includes :
#include

The platform data of your chip :

#if defined(CONFIG_I2C) || defined(CONFIG_GPIO_PCA953X)
/* PCA9555 */
static struct pca953x_platform_data stamp_gpio_ext_pdata = {
        .gpio_base = -1, // auto-guess the GPIO number
};
Of course you need to add one for each PCA9555 you want to add on the bus.

Now add the device to your I2C bus declaration.
You probably have something like :

static struct i2c_board_info __initdata bfin_i2c_board_info[] = {
 { ... }, { ... }

Just add to the array your chip :
{
  I2C_BOARD_INFO("pca9555",0x20),
  .platform_data = &stamp_gpio_ext_pdata,
 },
0x20 is the I2C address of the chip, you can guess it reading the datasheet or using the "i2cdetect" utility. For the PCA9555 the address is configured by the pin A0,A1,A2, with all threee are connected to zero volt, then the address is 0x20.

Check the PCA support in the kernel config menu (device->gpio), compile, run it and it's done !
Be carefull the new GPIO will be added at some very high number (240 in my case), you can find it by looking in /sys/class/gpio/ the new GPIO extender should be there.

dimanche 1 novembre 2009

Winter

ALSA

I ported an old Linux audio program, a simple Voice-over-IP & stereo demuxing application, from OSS to ALSA.

First impression of ALSA libasound : the documentation sucks, well, no, the documentation really sucks monkey rods ! I wonder why the wannabe 1st Linux audio API have such a pathetic API doc, example : snd_pcm_hw_params_set_period_time_near. And this is not an exception, the whole API go such poor doxygen comments. Very frustating for someone who is maintaining a library, and made a huge effort on javadocing lazy coder code.

About the API itself, well.. it's looking a bit over-complex near OSS, but it's no rocket science once you found the good examples for your poll based full duplex program : speexclient. But once it was ported the perfs are awesome, far of OSS and far of Windows sound system. I was able to achieve 5ms latency with a 8 inputs/8 outputs audio card, without using a rt patched kernel, nor huge optimization.

Well it's a pity you got a such technically impressive audio system ruined by poor doc & API.. no wonder why we have Arts, ESD, Pulseaudio, Jackd (the guys who thinks everybody have floating point CPU).

My version of speexclient, transformed for being a simple fullduplex passthru audio program (I removed all the speex encoding/decoding part and the network code for simplicity).

You still need speex for it's very good jitter buffer and alsa_device.c (can be found of speex GIT repository)

/***************************************************************************
Copyright (C) 2004-2006 by Jean-Marc Valin
Copyright (C) 2006 Commonwealth Scientific and Industrial Research
Organisation (CSIRO) Australia

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

- Neither the name of the Xiph.org Foundation nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

****************************************************************************/

#ifdef HAVE_CONFIG_H
#include
#endif

#include
#include
#include
#include
#include
#include
#include
#include /* close() */
#include /* memset() */

#include "alsa_device.h"

#include

#include

#define MAX_MSG 1500

#define SAMPLING_RATE 16000
#define FRAME_SIZE 320

int main(int argc, char *argv[]) {


int nfds;
struct pollfd *pfds;

AlsaDevice *audio_dev;

JitterBuffer * jitterBuffer = jitter_buffer_init(FRAME_SIZE * 2);
int jitterTime = 0;

/* Setup audio device, forced to pughw for avoiding pulseaudio */
audio_dev = alsa_device_open("plughw:0,0", SAMPLING_RATE, 1, FRAME_SIZE);

struct sched_param param;
/*param.sched_priority = 40; */
param.sched_priority = sched_get_priority_min(SCHED_FIFO);
if (sched_setscheduler(0, SCHED_FIFO, ¶m))
perror("sched_setscheduler");

/* Setup all file descriptors for poll()ing */
nfds = alsa_device_nfds(audio_dev);
pfds = malloc(sizeof(*pfds) * (nfds));
alsa_device_getfds(audio_dev, pfds, nfds);

alsa_device_start(audio_dev);

/* Infinite loop on capture, playback */
while (1) {
/* Wait for either 1) capture 2) playback */
poll(pfds, nfds, -1);

/* Ready to play a frame (playback) */
if (alsa_device_playback_ready(audio_dev, pfds, nfds)) {
short pcm[FRAME_SIZE];

/* Get audio from the jitter buffer */
JitterBufferPacket p;
p.data = (char *)pcm;
p.len = FRAME_SIZE * 2;

// eat a buffer
int res=jitter_buffer_get(jitterBuffer,&p,FRAME_SIZE * 2,NULL);
if(res != JITTER_BUFFER_OK ) {
memset(pcm,0,FRAME_SIZE*2);
}

jitter_buffer_tick(jitterBuffer);


/* Playback the audio and reset the echo canceller if we got an underrun */
if (alsa_device_write(audio_dev, pcm, FRAME_SIZE)) {
printf("underrun\n");
}
}
/* Audio available from the soundcard (capture) */
if (alsa_device_capture_ready(audio_dev, pfds, nfds)) {
short pcm[FRAME_SIZE];

/* Get audio from the soundcard */
alsa_device_read(audio_dev, pcm, FRAME_SIZE);

JitterBufferPacket p;
p.data = (char*)(pcm);
p.len = FRAME_SIZE*2;
jitterTime += FRAME_SIZE*2;
p.timestamp = jitterTime;
p.span = FRAME_SIZE*2;
jitter_buffer_put(jitterBuffer, &p);
}
}

return 0;
}

lundi 26 octobre 2009

GCC intrinsics for SIMD

I made some test with gcc intrinsics, for generating SIMD code (aka MMX, SSE, SSE2, SSE3, Neon). first impression documentation is really sparse don't count on official gcc docs.

I'll post an example of my experiments : applying a level modification to eight channels of audio samples. Typically a good usage of SIMD code. Once you understand the few doc and examples there is present on Internet, it's quite easy to write vectorized code. Much easier than writing it in assembly. But... there is a huge but performances aren't there ! Intrinsic code is confusing gcc. Basicly : gcc SIMD code run slower than gcc non-SIMD code. And the SIMD code is slower with full optimization turned on... perhaps the bad result explain why the documentation is not completed ;) Look like gcc optimizator is confusing himself.. To be continued when I find some time to make some assembly post-mortem.



typedef float v4sf __attribute__ ((vector_size (16))); // vector of four single float

// this structure is here for help you to access the different vector values
// the gcc v4sf type isn't able to provide simple acessors..

union f4vector
{
v4sf v;
float f[4];
} ;

// declaration of a float vector of four 32bit float
union f4vector values;

// load the values using the convenient union
values.f[0]=1.0;
values.f[1]=2.0;
values.f[2]=3.0;
values.f[3]=4.0;

// coeficient
coef.f[0]=0.3;
coef.f[1]=0.4;
coef.f[2]=0.5;
coef.f[3]=0.6;

// now multiply the vectors (using the .v accessor):
values.v = values.v * coef.v;


If you use the good flags for gcc (-mcpu=pentium3 -mmmx -msse),
simd code will be generated.