[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: SPI




----- Original Message -----
From: "Carlos Ojea" <carlosojea@xxxxxxx.es>
To: <johana@xxxxxxx.com>
Cc: "dev-etrax" <dev-etrax@xxxxxxx.com>
Sent: den 21 oktober 2003 10:58
Subject: SPI


>
>
> Hi,
>
>
> > Did you change the CODEC_VAl parameters to reflect the
> > 24 bits/sync, e.g: ?
> >    /* 64 kHz, word_sync: 24 bits/sync, frame_sync: 1 (no framing) */
> >    if (ioctl(devspi, SSP_SPEED, CODEC_VAL(FREQ_64kHz, 24, 1))) {
> >      fprintf(stderr, "Couldn't change sync SSP_SPEED\n");
> >    }
> > Make sure you read as much as you write, if that has something to do
> > with it. Perhaps somethings goes wrong if the receiver gets overrun.
>
>
> Yes, I changed CODEC_VAL and I read as much as I write.
> I don't know what I am doing wrong.
> This is my code:
>
>
> void Spi_init (void)
> {
>   int tmp;
>   int err = 0;
>
>   devspi = open ("/dev/syncser0", O_RDWR | O_SYNC);
>   if (devspi == -1)
>     {
>       fprintf (stderr, "\nError \n", strerror (errno));
>       exit (1);
>     }
>
>   ioctl (devspi, SSP_MODE,       MASTER_BIDIR);
>   tmp = NORMAL_SYNC | WORD_SYNC | SYNC_ON | WORD_SIZE_24 | BIT_ORDER_MSB
> |
> FLOW_CONTROL_DISABLE | CLOCK_NOT_GATED;
>   if (ioctl(devspi, SSP_FRAME_SYNC, tmp)) {
>     fprintf(stderr, "Couldn't change sync SSP_FRAME_SYNC\n");
>     err++;
>   }
>
>   tmp = CLOCK_INEGEDGE | FRAME_NORMAL | STATUS_NORMAL;
>   if (ioctl(devspi, SSP_IPOLARITY, tmp)) {
>     fprintf(stderr, "Couldn't change polarity SSP_IPOLARITY\n");
>     err++;
>   }
>   tmp = CLOCK_NORMAL | FRAME_INVERT | STATUS_NORMAL;
>   if (ioctl(devspi, SSP_OPOLARITY, tmp)) {
>     fprintf(stderr, "Couldn't change polarity SSP_OPOLARITY\n");
>     err++;
>   }
>
>   /* 64 kHz, word_sync: 24 bits/sync, frame_sync: 1 (no framing) */
>   if (ioctl(devspi, SSP_SPEED, CODEC_VAL(FREQ_64kHz, 24, 1))) {
>     fprintf(stderr, "Couldn't change sync SSP_SPEED\n");
>   }
>
>   /* 160 bytes (160 samples = 20 ms) buffering */
>   if (ioctl(devspi, SSP_INBUFCHUNK, 160)) {
>     fprintf(stderr, "Couldn't change sync SSP_BUFCHUNK\n");
>     err++;
>   }
> }
>
>
file://_____________________________________________________________________
_
> _______
> //
> //   int Spi_write (int byte):
>
file://_____________________________________________________________________
_
> _______
>
>
> int Spi_write (int byte)
>

>   int nbytes_written;
>   int pointer_byte [1];
>
>
>   nbytes_written = read (devspi, pointer_byte, 1);       // DUMMY READ
>   if (nbytes_written == -1)
>     {
>       fprintf (stderr, "\nError %s\n", strerror (errno));
>     }
>   else
>     {
>       fprintf (stdout, "Read: %X \n", pointer_byte [0]);
>     }
>   pointer_byte [0] = byte;
>
>
>   nbytes_written = write (devspi, pointer_byte, 1);
>   debug_writes ++;
>   fprintf (stdout, " %i \n", debug_writes);
>   return (nbytes_written);
> }

Since you have 24 bit words you must probably write and read 3 bytes
at the same time or I suspect the DMA will get confused and get
underrun error or something. (not tested)

E.g. do:

int spi_write3(unsigned char *data)
{
  int nbytes_written;
  nbytes_written = write(devspi, data, 3);
  return nbytes_written;
}
int spi_read3(unsigned char *data)
{
  int nbytes_read;
  nbytes_read = read(devspi, data, 3);
  return nbytes_read;
}


>
> And finally this is my loop:
> do
> {
>   Spi_write (0x03);        // READ command
>   Spi_write (0x0E);        // Register
>   Spi_write (0x00);        // Third write
> } while (1);

Try changing this to something like:
  unsigned char outdata[3];
  unsigned char indata[3];

  outdata[0] = 0x03;
  outdata[1] = 0x0E;
  outdata[2] = 0;

 do {
  spi_write3(outdata);
  spi_read3(indata);
 } while(1);


>
>
> I get this output:
> Read: 812FF
>  1
> Read: FF
>  2
> Read: FF
>
>
> And then program blocks in the third write. I don't know why...
> Thank you very much for your help. I really appreciate it.
>
> Regards,
> Carlos
>