Library API

XS1 Library

Data types

  • clock

    Clock resource type.

    Clocks are declared as global variables and are initialized with the resource identifier of a clock block. When in a running state a clock provides rising and falling edges to ports configured using that clock.

Port Configuration Functions

  • void configure_in_port_handshake(void port p, in port readyin, out port readyout, clock
     clk)

    Configures a buffered port to be a clocked input port in handshake mode.

    If the ready-in or ready-out ports are not 1-bit ports, an exception is raised. The ready-out port is asserted on the falling edge of the clock when the port’s buffer is not full. The port samples its pins on its sampling edge when both the ready-in and ready-out ports are asserted.

    By default the port’s sampling edge is the rising edge of clock. This can be changed by the function set_port_sample_delay()
    .

    Parameters

    • p

      The buffered port to configure.

    • readyin

      A 1-bit port to use for the ready-in signal.

    • readyout

      A 1-bit port to use for the ready-out signal.

    • clk

      The clock used to configure the port.

  • void configure_out_port_handshake(void port p, in port readyin, out port readyout, clock
     clk, unsigned initial)

    Configures a buffered port to be a clocked output port in handshake mode.

    If the ready-in or ready-out ports are not 1-bit ports an exception is raised. The port drives the initial value on its pins until an output statement changes the value driven. The ready-in port is read on the sampling edge of the buffered port. Outputs are driven on the next falling edge of the clock where the previous value read from the ready-in port was high. On the falling edge of the clock the ready-out port is driven high if data is output on that edge, otherwise it is driven low. By default the port’s sampling edge is the rising edge of clock. This can be changed by the function set_port_sample_delay()
    .

    Parameters

    • p

      The buffered port to configure.

    • readyin

      A 1-bit port to use for the ready-in signal.

    • readyout

      A 1-bit port to use for the ready-out signal.

    • clk

      The clock used to configure the port.

    • initial

      The initial value to output on the port.

  • void configure_in_port_strobed_master(void port p, out port readyout, const clock
     clk)

    Configures a buffered port to be a clocked input port in strobed master mode.

    If the ready-out port is not a 1-bit port, an exception is raised. The ready-out port is asserted on the falling edge of the clock when the port’s buffer is not full. The port samples its pins on its sampling edge after the ready-out port is asserted.

    By default the port’s sampling edge is the rising edge of clock. This can be changed by the function set_port_sample_delay()
    .

    Parameters

    • p

      The buffered port to configure.

    • readyout

      A 1-bit port to use for the ready-out signal.

    • clk

      The clock used to configure the port.

  • void configure_out_port_strobed_master(void port p, out port readyout, const clock
     clk, unsigned initial)

    Configures a buffered port to be a clocked output port in strobed master mode.

    If the ready-out port is not a 1-bit port, an exception is raised. The port drives the initial value on its pins until an output statement changes the value driven. Outputs are driven on the next falling edge of the clock. On the falling edge of the clock the ready-out port is driven high if data is output on that edge, otherwise it is driven low.

    Parameters

    • p

      The buffered port to configure.

    • readyout

      A 1-bit port to use for the ready-out signal.

    • clk

      The clock used to configure the port.

    • initial

      The initial value to output on the port.

  • void configure_in_port_strobed_slave(void port p, in port readyin, clock
     clk)

    Configures a buffered port to be a clocked input port in strobed slave mode.

    If the ready-in port is not a 1-bit port, an exception is raised. The port samples its pins on its sampling edge when the ready-in signal is high. By default the port’s sampling edge is the rising edge of clock. This can be changed by the function set_port_sample_delay()
    .

    Parameters

    • p

      The buffered port to configure.

    • readyin

      A 1-bit port to use for the ready-in signal.

    • clk

      The clock used to configure the port.

  • void configure_out_port_strobed_slave(void port p, in port readyin, clock
     clk, unsigned initial)

    Configures a buffered port to be a clocked output port in strobed slave mode.

    If the ready-in port is not a 1-bit port, an exception is raised. The port drives the initial value on its pins until an output statement changes the value driven. The ready-in port is read on the buffered port’s sampling edge. Outputs are driven on the next falling edge of the clock where the previous value read from the ready-in port is high. By default the port’s sampling edge is the rising edge of clock. This can be changed by the function set_port_sample_delay()
    .

    Parameters

    • p

      The buffered port to configure.

    • readyin

      A 1-bit port to use for the ready-in signal.

    • clk

      The clock used to configure the port.

    • initial

      The initial value to output on the port.

  • void configure_in_port(void port p, const clock
     clk)

    Configures a port to be a clocked input port with no ready signals.

    This is the default mode of a port. The port samples its pins on its sampling edge. If the port is unbuffered, its direction can be changed by performing an output. This change occurs on the next falling edge of the clock. Afterwards, the port behaves as an output port with no ready signals.

    By default the port’s sampling edge is the rising edge of the clock. This can be changed by the function set_port_sample_delay()
    .

    Parameters

    • p

      The port to configure, which may be buffered or unbuffered.

    • clk

      The clock used to configure the port.

  • void configure_in_port_no_ready(void port p, const clock
     clk)

    Alias for configure_in_port()
    .

  • void configure_out_port(void port p, const clock
     clk, unsigned initial)

    Configures a port to be a clocked output port with no ready signals.

    The port drives the initial value on its pins until an input or output statement changes the value driven. Outputs are driven on the next falling edge of the clock and every port-width bits of data are held for one clock cycle. If the port is unbuffered, the direction of the port can be changed by performing an input. This change occurs on the falling edge of the clock after any pending outputs have been held for one clock period. Afterwards, the port behaves as an input port with no ready signals.

    Parameters

    • p

      The port to configure, which may be buffered or unbuffered.

    • clk

      The clock used to configure the port.

    • initial

      The initial value to output on the port.

  • void configure_out_port_no_ready(void port p, const clock
     clk, unsigned initial)

    Alias for configure_out_port()
    .

  • void configure_port_clock_output(void port p, const clock
     clk)

    Configures a 1-bit port to output a clock signal.

    If the port is not a 1-bit port, an exception is raised. Performing inputs or outputs on the port once it has been configured in this mode results in undefined behaviour.

    Parameters

    • p

      The 1-bit port to configure.

    • clk

      The clock to output.

  • void set_port_no_sample_delay(void port p)

    Sets a port to no sample delay mode.

    This causes the port to sample input data on the rising edge of its clock. This is the default state of the port.

    Parameters

    • p

      The port to configure.

  • void set_port_sample_delay(void port p)

    Sets a port to sample delay mode.

    This causes the port to sample input data on the falling edge of its clock.

    Parameters

    • p

      The port to configure.

  • void set_port_clock(void port p, const clock
     clk)

    Attaches a clock to a port.

    This corresponds to using the SETCLK instruction on a port. The edges of the clock are used to sample and output data. Usually the use of the configure_*_port_* functions is preferred since they ensure that all the port configuration changes required for the desired mode are performed in the correct order.

    Parameters

    • p

      The port to configure.

    • clk

      The clock to attach.

  • void set_port_ready_src(void port p, void port ready)

    Sets a 1-bit port as the ready-out for another port.

    This corresponds with using the SETRDY instruction on a port. If the ready-out port is not a 1-bit port then an exception is raised. The ready-out port is used to indicate that the port is ready to transfer data. Usually the use of the configure_*_port_* functions is preferred since they ensure that all the port configuration changes required for the desired mode are performed in the correct order.

    Parameters

    • p

      The port to configure.

    • ready

      The 1-bit port to use for the ready-out signal.

  • void set_port_use_on(void port p)

    Turns on a port.

    The port state is initialised to the default state for a port of its type. If the port is already turned on its state is reset to its default state.

    Parameters

    • p

      The port to turn on.

  • void set_port_use_off(void port p)

    Turns off a port.

    No action is performed if the port is already turned off. Any attempt to use the port while off will result in an exception being raised.

    Parameters

    • p

      The port to turn off.

  • void set_port_mode_data(void port p)

    Configures a port to be a data port.

    This is the default state of a port. Output operations on the port are use to control its output signal.

    Parameters

    • p

      The port to configure.

  • void set_port_mode_clock(void port p)

    Configures a 1-bit port to be a clock output port.

    The port will output the clock connected to it. If the port is not a 1-bit port, an exception is raised. The function set_port_mode_data()
    can be used to set the port back to its default state.

    Parameters

    • p

      The port to configure.

  • void set_port_mode_ready(void port p)

    Configures a 1-bit port to be a ready signal output port.

    The port will output the ready-out of a port connected with set_port_ready_src()
    . If the port is not a 1-bit port, an exception is raised. The function set_port_mode_data()
    can be used to set the port back to its default state. Usually the use of the configure_*_port_* functions is prefered since they ensure that all the port configuration changes required for the desired mode are performed in the correct order.

    Parameters

    • p

      The port to configure.

  • void set_port_drive(void port p)

    Configures a port in drive mode.

    Values output to the port are driven on the pins. This is the default drive state of a port. Calling set_port_drive()
    has the side effect disabling the port’s pull up or pull down resistor.

    Parameters

    • p

      The port to configure.

  • void set_port_drive_low(void port p)

    Configures a port in drive low mode.

    For 1-bit ports when 0 is output its pin is driven low and when 1 is output no value is driven. If the port is not a 1-bit port, the result of an output to the port is undefined. On XS1-G devices calling set_port_drive_low()
    has the side effect of enabling the port’s internal pull-up resistor. On XS1-L devices calling set_port_drive_low()
    has the side effect of enabling the port’s internal pull-down resistor.

    Parameters

    • p

      The port to configure.

  • void set_port_pull_up(void port p)

    Enables a port’s internal pull-up resistor.

    When nothing is driving a pin the pull-up resistor ensures that the value sampled by the port is 1. The pull-up is not strong enough to guarantee a defined external value. On XS1-G devices calling set_port_pull_up()
    has the side effect of configuring the port in drive low mode. On XS1-L devices no pull-up resistors are available and an exception will be raised if set_port_pull_up()
    is called.

    Parameters

    • p

      The port to configure.

  • void set_port_pull_down(void port p)

    Enables a port’s internal pull-down resistor.

    When nothing is driving a pin the pull-down resistor ensures that the value sampled by the port is 0. The pull-down is not strong enough to guarantee a defined external value. On XS1-G devices no pull-down resistors are available and an exception will be raised if set_port_pull_down()
    is called. On XS1-L devices calling set_port_pull_down()
    has the side effect of configuring the port in drive low mode.

    Parameters

    • p

      The port to configure.

  • void set_port_pull_none(void port p)

    Disables the port’s pull-up or pull-down resistor.

    This has the side effect of configuring the port in drive mode.

    Parameters

    • p

      The port to configure.

  • void set_port_master(void port p)

    Sets a port to master mode.

    This corresponds to using the SETC instruction on the port with the value XS1_SETC_MS_MASTER. Usually the use of the functions configure_in_port_strobed_master()
    and configure_out_port_strobed_master()
    is preferred since they ensure that all the port configuration changes required for the desired mode are performed in the correct order.

    Parameters

    • p

      The port to configure.

  • void set_port_slave(void port p)

    Sets a port to slave mode.

    This corresponds to using the SETC instruction on the port with the value XS1_SETC_MS_SLAVE. Usually the use of the functions configure_in_port_strobed_slave()
    and configure_out_port_strobed_slave()
    is preferred since they ensure that all the port configuration changes required for the desired mode are performed in the correct order.

    Parameters

    • p

      The port to configure.

  • void set_port_no_ready(void port p)

    Configures a port to not use ready signals.

    This corresponds to using the SETC instruction on the port with the value XS1_SETC_RDY_NOREADY. Usually the use of the functions configure_in_port()
    and configure_out_port()
    is preferred since they ensure that all the port configuration changes required for the desired mode are performed in the correct order.

    Parameters

    • p

      The port to configure.

  • void set_port_strobed(void port p)

    Sets a port to strobed mode.

    This corresponds to using the SETC instruction on the port with the value XS1_SETC_RDY_STROBED. Usually the use of the configure_*_port_strobed_* functions is preferred since they ensure that all the port configuration changes required for the desired mode are performed in the correct order.

    Parameters

    • p

      The port to configure.

  • void set_port_handshake(void port p)

    Sets a port to handshake mode.

    This corresponds to using the SETC instruction on the port with the value XS1_SETC_RDY_HANDSHAKE. Usually the use of the configure_*_port_handshake functions is preferred since they ensure that all the port configuration changes required for the desired mode are performed in the correct order.

    Parameters

    • p

      The port to configure.

  • void set_port_no_inv(void port p)

    Configures a port to not invert data that is sampled and driven on its pins.

    This is the default state of a port.

    Parameters

    • p

      The port to configure.

  • void set_port_inv(void port p)

    Configures a 1-bit port to invert data which is sampled and driven on its pin.

    If the port is not a 1-bit port, an exception is raised. If the port is used as the source for a clock then setting this mode has the effect of the swapping the rising and falling edges of the clock.

    Parameters

    • p

      The 1-bit port to configure.

  • void set_port_shift_count(void port p, unsigned n)

    Sets the shift count for a port.

    This corresponds with the SETPSC instruction. The new shift count must be less than the transfer width of the port, greater than zero and a multiple of the port width otherwise an exception is raised. For a port used for input this function will cause the next input to be ready when the specified amount of data has been shifted in. The next input will return transfer-width bits of data with the captured data in the most significant bits. For a port used for output this will cause the next output to shift out this number of bits. Usually the use of the functions partin()
    and partout()
    is preferred over setpsc() as they perform both the required configuration and the input or output together.

    Parameters

    • p

      The buffered port to configure.

    • n

      The new shift count.

  • void set_pad_delay(void port p, unsigned n)

    Sets the delay on the pins connected to the port.

    The input signals sampled on the port’s pins are delayed by this number of processor-clock cycles before they they are seen on the port. The default delay on the pins is 0. The delay must be set to values in the range 0 to 5 inclusive. If there are multiple enabled ports connected to the same pin then the delay on that pin is set by the highest priority port.

    Parameters

    • p

      The port to configure.

    • n

      The number of processor-clock cycles by which to delay the input signal.

Clock Configuration Functions

  • void configure_clock_src(clock
     clk, void port p)

    Configures a clock to use a 1-bit port as its source.

    This allows I/O operations on ports to be synchronised to an external clock signal. If the port is not a 1-bit port, an exception is raised.

    Parameters

    • clk

      The clock to configure.

    • p

      The 1-bit port to use as the clock source.

  • void configure_clock_ref(clock
     clk, unsigned char divide)

    Configures a clock to use the reference clock as it source.

    If the divide is set to zero the reference clock frequency is used, otherwise the reference clock frequency divided by 2 * is used. By default the reference clock is configured to run at 100 MHz.

    Parameters

    • clk

      The clock to configure.

    • divide

      The clock divide.

  • void configure_clock_rate(clock
     clk, unsigned a, unsigned b)

    Configures a clock to run at a rate of ( MHz.

    If the specified rate is not supported by the hardware, an exception is raised. The hardware supports rates of MHz and rates of the form ( MHz where is the reference clock frequency and is a number in the range 1 to 255 inclusive.

    Parameters

    • clk

      The clock to configure.

    • a

      The dividend of the desired rate.

    • b

      The divisor of the desired rate.

  • void configure_clock_rate_at_least(clock
     clk, unsigned a

XS1 Library Read More »

libflash API

The libflash library provides functions for reading and writing
data to SPI flash devices that use the xCORE format shown in the
diagram below.

Flash format diagram
/files/images/14584/8/flash-format.png

All functions are prototyped in the header file
<flash.h>. Except where otherwise stated, functions
return 0 on success and non-zero on failure.

General Operations

The program must explicitly open a connection to the SPI device
before attempting to use it, and must disconnect once finished
accessing the device.

The functions fl_connect and fl_connectToDevice require an
argument of type fl_SPIPorts, which defines the four ports and
clock block used to connect to the device.

typedef struct {
  in buffered port:8 spiMISO;
  out port spiSS;
  out port spiCLK;
  out buffered port:8 spiMOSI;
  clock spiClkblk;
} fl_SPIPorts;

  • int fl_connect(fl_SPIPorts *SPI)

    fl_connect opens a connection to the specified SPI device.

  • int fl_connectToDevice(fl_SPIPorts *SPI, fl_DeviceSpec spec[], unsigned n)

    fl_connectToDevice opens a connection to an SPI device. It
    iterates through an array of n SPI device specifications,
    attempting to connect using each specification until one succeeds.

  • int fl_getFlashType(void)

    fl_getFlashType returns an enum value for the flash device.
    The enumeration of devices known to libflash is given below.

    typedef enum {
      UNKNOWN = 0,
      ALTERA_EPCS1,
      ATMEL_AT25DF041A,
      ATMEL_AT25FS010,
      ST_M25PE10,
      ST_M25PE20,
      WINBOND_W25X40
    } fl_FlashId;
    

    If the function call fl_connectToDevice(p, spec, n) is used to
    connect to a flash device, fl_getFlashType returns the parameter
    value spec[i].flashId where i is the index of the
    connected device.

  • unsigned fl_getFlashSize(void)

    fl_getFlashSize returns the capacity of the SPI device in
    bytes.

  • int fl_disconnect(void)

    fl_disconnect closes the connection to the SPI device.

Boot Partition Functions

By default, the size of the boot partition is set to the size of
the flash device. Access to boot images is provided through an
iterator interface.

  • int fl_getFactoryImage(fl_BootImageInfo *bootImageInfo)

    fl_getFactoryImage provides information about the factory boot
    image.

  • int fl_getNextBootImage(fl_BootImageInfo *bootImageInfo)

    fl_getNextBootImage provides information about the next upgrade
    image. Once located, an image can be upgraded. Functions are also
    provided for reading the contents of an upgrade image.

  • unsigned fl_getImageVersion(fl_BootImageInfo *bootImageInfo)

    fl_getImageVersion returns the version number of the specified
    image.

  • int fl_startImageReplace(fl_BootImageInfo *, unsigned maxsize)

    fl_startImageReplace prepares the SPI device for replacing an
    image. The old image can no longer be assumed to exist after this
    call.

    Attempting to write into the data partition or the space of another
    upgrade image is invalid. A non-zero return value signifies that
    the preparation is not yet complete and that the function should be
    called again. This behavior allows the latency of a sector erase to
    be masked by the program.

  • int fl_startImageAdd(fl_BootImageInfo*, unsigned maxsize, unsigned padding)

    fl_startImageAdd prepares the SPI device for adding an image
    after the specified image. The start of the new image is at least
    padding bytes after the previous image.

    Attempting to write into the data partition or the space of another
    upgrade image is invalid. A non-zero return value signifies that
    the preparation is not yet complete and that the function must be
    called again. This behavior allows the latency of a sector erase to
    be masked by the program.

  • int fl_startImageAddAt(unsigned offset, unsigned maxsize)

    fl_startImageAddAt prepares the SPI device for adding an image
    at the specified address offset from the base of the first sector
    after the factory image.

    Attempting to write into the data partition or the space of another
    upgrade image is invalid. A non-zero return value signifies that
    the preparation is not yet complete and that the function must be
    called again.

  • int fl_writeImagePage(const unsigned char page[])

    fl_writeImagePage waits until the SPI device is able to accept
    a request and then outputs the next page of data to the device.
    Attempting to write past the maximum size passed to
    fl_startImageReplace, fl_startImageAdd or
    fl_startImageAddAt is invalid.

  • int fl_writeImageEnd(void)

    fl_writeImageEnd waits until the SPI device has written the
    last page of data to its memory.

  • int fl_startImageRead(fl_BootImageInfo *b)

    fl_startImageRead prepares the SPI device for reading the
    contents of the specified upgrade image.

  • int fl_readImagePage(unsigned char page[])

    fl_readImagePage inputs the next page of data from the SPI
    device and writes it to the array page.

  • int fl_deleteImage(fl_BootImageInfo* b)

    fl_deleteImage erases the upgrade image with the specified
    image.

Data Partition Functions

All flash devices are assumed to have uniform page sizes but are
not assumed to have uniform sector sizes. Read and write operations
occur at the page level, and erase operations occur at the sector
level. This means that to write part of a sector, a buffer size of
at least one sector is required to preserve other data.

In the following functions, writes to the data partition and
erasures from the data partition are not fail-safe. If the
operation is interrupted, for example due to a power failure, the
data in the page or sector is undefined.

  • unsigned fl_getDataPartitionSize(void)

    fl_getDataPartitionSize returns the size of the data partition
    in bytes.

  • int fl_readData(unsigned offset, unsigned size, unsigned char dst[])

    fl_readData reads a number of bytes from an offset into the
    data partition and writes them to the array dst.

  • unsigned fl_getWriteScratchSize(unsigned offset, unsigned size)

    fl_getWriteScratchSize returns the buffer size needed by
    fl_writeData for the given parameters.

  • int fl_writeData(unsigned offset, unsigned size, const unsigned char src[], unsigned char buffer[])

    fl_writeData writes the array src to the specified offset
    in the data partition. It uses the array buffer to preserve
    page data that must be re-written.

Page-Level Functions

  • unsigned fl_getPageSize(void)

    fl_getPageSize returns the page size in bytes.

  • unsigned fl_getNumDataPages(void)

    fl_getNumDataPages returns the number of pages in the data
    partition.

  • unsigned fl_writeDataPage(unsigned n, const unsigned char data[])

    fl_writeDataPage writes the array data to the n-th page in
    the data partition. The data array must be at least as big as the
    page size; if larger, the highest elements are ignored.

  • unsigned fl_readDataPage(unsigned n, unsigned char data[])

    fl_readDataPage reads the n-th page in the data partition and
    writes it to the array data. The size of data must be at least as
    large as the page size.

Sector-Level Functions

  • unsigned fl_getNumDataSectors(void)

    fl_getNumDataSectors returns the number of sectors in the data
    partition.

  • unsigned fl_getDataSectorSize(unsigned n)

    fl_getDataSectorSize returns the size of the n-th sector in
    the data partition in bytes.

  • unsigned fl_eraseDataSector(unsigned n)

    fl_eraseDataSector erases the n-th sector in the data
    partition.

  • unsigned fl_eraseAllDataSectors(void)

    fl_eraseAllDataSectors erases all sectors in the data
    partition.

libflash API Read More »

Scroll to Top