SPI sound card - Raspberry Pi Forums


good morning.

looking advice experiment doing now. spi easy , speedy interface. used interconnect rpi2 , pic 16f1718. rpi2 os jessie-lite.

on rpi2 software looks this: mpd => linux fifo => c program => spi interface
on pic there is: spi => pic fifo => dac

dac followed amplifier loudspeaker.
mpc client has been used select internet radio station.
after lot of debugging listening radio !
want receive low quality sound because ultimatie goal retransmit on medium wave. audio frequencies above 4 khz not allowed. therefore mpd configured 8 khz sampling rate , sound data reduced 8 bit. because linux isn’t real time system spi byte rate not constant. fifo on pic has been programmed output sound @ stable 8 khz rate. pic has limited ram. fifo 1 kbyte long. of time works fine under- , overflow occurs. c program on rpi2 straightforward program. bytes sent 1 one. no interrupt or dma has been used.

looking advice how go on. should ? (changing linux priorities / use distro / rpi2 interrupt / dma / other soft - hardware ?)

because mpd takes little cpu load possible run serveral instances simultaneously. ulitmate goal construct low power modulator retransmit old mw stations @ wavelength belong. if done without antenna direct connection entry of radio not forbidden law. if project should work open project , published in magazines of antique radio. there no commercial aspect. have choosen 16f1718 pic external 16 mhz clock because there nummerical controlled oscillator on board. have tried multiple usb soundcards hardware of pic looks fine such system. (see earlier posts on forum)

cordialy,

eduard

hello,

seems nobody has ever made simple spi soundcard. after many hours mine working. please find rpi2 , pic source code. pic 16f1718 working soundcard. fore sure 8 bit dac no hifi. next step modulation on medium wave carrier. not raspberry job programmed on pic. add code being helpful somewhere somehow.

on rpi2:

include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include <bcm2835.h>


#define pin rpi_gpio_p1_11

static const char *device = "/dev/spidev0.0";
static uint8_t mode;
static uint8_t bits = 8;
static uint32_t speed =800000;
static uint16_t delay;



int main()
{
// dit programma leest de fifo gevuld door mpd, verwerkt de 16bit naar 8 bit en
// stuurt de data door naar de fifo op een pic 16f1718

uint8_t b,w;
int ret = 0;
int fd_spi;
#define buflen 2
uint8_t tx[buflen];
uint8_t rx[buflen];
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx,
.rx_buf = (unsigned long)rx,
.len = buflen,
.delay_usecs = delay,
.speed_hz = speed,
.bits_per_word = bits,
};

int fd;
int16_t s16;
int32_t s32;
char * myfifo = "/var/tmp/mpdfif01";



if (!bcm2835_init()) return 1;

// set pin input
bcm2835_gpio_fsel(pin, bcm2835_gpio_fsel_inpt);

fd = open(myfifo, o_rdonly);
fd_spi = open(device, o_rdwr);

if (fd) {
while (1) {
read(fd, &s16, 2); // lees signed 16bit integer u$
s32 = s16 + 31767; // sign wegwerken
tx[0] = (s32 & 0xff00)>>8; // meest beduidende byte ophalen
while(w) w = bcm2835_gpio_lev(pin); // wacht op pic fifo ready
ret = ioctl(fd_spi, spi_ioc_message(1), &tr); // verzenden naar pic
usleep(4); // deze tijd kritisch. te klein overflow pic fifo, te groot stotteren
}; // end while

}; // endif
return 0;
}

on pic:

include "mcc_generated_files/mcc.h"


uint32_t inpnt=0;
uint32_t uitpnt=0;
uint16_t inpnt2, uitpnt2;
uint16_t m1023 = 1023;
static uint8_t fifo[1024];


void interrupt clk8k(void){
tmr0=11; //12;
intconbits.t0if=0;
if (uitpnt<inpnt) {
uitpnt2 = uitpnt & m1023;
dac1_setoutput(fifo[uitpnt2]);
uitpnt++;
};
if((inpnt-uitpnt)<100) portabits.ra0 = 0;
}



void main(void)
{
// initialize device
system_initialize();


option_regbits.tmr0cs=0;
option_regbits.psa=0;
option_regbits.ps=0;

intconbits.gie = 1;
intconbits.tmr0ie =1;
intconbits.tmr0if = 0;


uint8_t dummy=0;
portabits.ra0=0;

while(1) {
inpnt2 = inpnt & m1023;
fifo[inpnt2] = spi_exchange8bit(dummy);
inpnt++;
if((inpnt-uitpnt)>999) portabits.ra0=1;
};
}

cordially,

eduard


raspberrypi



Comments