return (int)(unsigned char)fifo_pop_locked(&port->rxfifo);
}
-#if 0
/**
* Preleva un carattere dal buffer di ricezione.
* Se il buffer e' vuoto, ser_getchar_nowait() ritorna
* immediatamente EOF.
*/
-int ser_getchar_nowait(struct Serial *port)
+int ser_getchar_nowait(struct KFileSerial *fd)
{
- if (fifo_isempty_locked(&port->rxfifo))
+ if (fifo_isempty_locked(&fd->ser->rxfifo))
return EOF;
/* NOTE: the double cast prevents unwanted sign extension */
- return (int)(unsigned char)fifo_pop_locked(&port->rxfifo);
+ return (int)(unsigned char)fifo_pop_locked(&fd->ser->rxfifo);
}
-#endif
/**
size_t i = 0;
char *buf = (char *)_buf;
int c;
-
+
while (i < size)
{
if ((c = ser_getchar(fds->ser)) == EOF)
static int ser_flush(struct KFile *fd)
{
KFileSerial *fds = KFILESERIAL(fd);
-
+
/*
* Wait until the FIFO becomes empty, and then until the byte currently in
* the hardware register gets shifted out.
/**
* Initialize a serial port.
*
+ * \param fd KFile Serial struct interface.
* \param unit Serial unit to open. Possible values are architecture dependant.
*/
static struct Serial *ser_open(struct KFileSerial *fd, unsigned int unit)
* Since we are master, we have to trigger slave by sending
* fake chars on the bus.
*/
-static size_t spimaster_read(struct KFile *fd, void *buf, size_t size)
+static size_t spimaster_read(struct KFile *fd, void *_buf, size_t size)
{
KFileSerial *fd_spi = KFILESERIAL(fd);
ser_flush(&fd_spi->fd);
ser_purgeRx(fd_spi);
- for (size_t i = 0; i < size; i++)
+ size_t total_rd = 0;
+ uint8_t *buf = (uint8_t *)_buf;
+ int c;
+
+ while (size--)
+ {
+ /*
+ * Send and receive chars 1 by 1, otherwise the rxfifo
+ * will overrun.
+ */
ser_putchar(0, fd_spi->ser);
- return ser_read(&fd_spi->fd, buf, size);
+ if ((c = ser_getchar(fd_spi->ser)) == EOF)
+ break;
+
+ *buf++ = c;
+ total_rd++;
+ }
+ return total_rd;
}
/**
static size_t spimaster_write(struct KFile *fd, const void *buf, size_t size)
{
KFileSerial *fd_spi = KFILESERIAL(fd);
-
+
ser_purgeRx(fd_spi);
return ser_write(&fd_spi->fd, buf, size);