Serial Port

This tutorial will teach you how to setup and use a serial port in BeRTOS.

Struct serial

Serial ports are accesses through the Serial interface. You need to declare a struct Serial for each serial port you want to use, then you access it by using the KFile interface.

int main(void)
{
  Serial ser_port;
  IRQ_ENABLE;

  ser_init(&ser_port, SER_UART0);
  ser_setbaudrate(&ser_port, 115200);
  //...

First, initialize the structure with a call to ser_init(), in which you specify the port you want to use. You should use the values SER_UARTn because they are CPU independent. You then set the baudrate with a call to ser_setbaudrate(). These are the only methods you need to start using the serial port.

When you have initialized a serial port, you can use the KFile interface to read and write from it. However, read the full serial documentation for other serial-specific functions.

SPI master protocol

You can use the SPI master protocol through the Serial interface. All you need to do is to call a different initialization function:

int main(void)
{
  Serial spi_port;
  IRQ_ENABLE;

  spimaster_init(&spi_port, SER_SPI0);
  ser_setbaudrate(500000L);
  // use spi_port with kfile interface
  uint8_t buf[50];
  kfile_read(&spi_port.fd, buf, sizeof(buf));
}

SPI with DMA support (AT91SAM7 only)

With Atmel SAM7 CPUs, you can also use a DMA version of SPI, which doesn't use the Serial interface. You must define a SpiDmaAt91 structure and initialize it with a different function. Warning: you can have only one SpiDmaAt91 structure for each application.

int main(void)
{
  SpiDmaAt91 spi_port;
  IRQ_ENABLE;

  spi_dma_init(&spi_port);
  spi_dma_setclock(20000000L);
  // use the kfile interface as usual
  kfile_write(&spi_port.fd, buf, sizeof(buf));
}

Have a look at SPI DMA documentation.