I/O System

For some of the drivers, there is an I/O system on top of the HAL interface. The I/O system disconnects middleware from any driver dependency because the middleware only needs to rely on the interface that the I/O system provides. The I/O system has a write and a read method that are used to move data in/out of the underlying interface. An example is a file system stack that can be used with any of the external interfaces on the device to communicate with an external storage unit. The application will have to set up the interface, and then supply a pointer to the I/O object to the file system stack.

Figure 1. Overview of the I/O System

Example of I/O system code

/* This is conceptual code, and not copied from ASF4 framwork code */

#define BUF_SIZE  4

uint8_t buf[BUF_SIZE];
int8_t data_received;

io_handle_t   spi_io;
fs_handle_t   fs;
file_handle_t file;

hal_spi_init(&spi_io, hw);

do {
        data_received = io.read(buf, BUF_SIZE);
} while (data_recieved == 0);


if(data_received < 0) {
        /* Something wrong was detected */
        while(1) {
        };
}

/* Write something on the SPI bus */
spi_io.write(buf, data_received);

/* Using a storage device over the SPI interface */
fat_fs_init(fs, spi_io);
fat_fs_write_file(fs, file);