[Previous] [Contents] [Index] [Next]

devc-hspi

Serial driver for Hitachi protocol interface


Note: You must be root to start this driver.

Syntax:

devc-hspi [options] [intr] &

Runs on:

SH4 Hitachi 7760

Options:

The options are position-dependent and affect the subsequent ports.

-a
Set the automatic chip select. The default is set to a manual chip select.
-c number
Set the CLCK clock rate value (default 0x0, max 0x1F).
-i
Set the IDIV clock division value (default OFF).
-I number
Size of raw input buffer (default 2048).
-l
Set least significant bit LSB (default MSB).
-O number
Size of output buffer (default 2048).
-p
Set CLKP serial clock polarity value (default OFF).
-t number
Maximum number of bytes to transmit each interrupt (default 8).
-u unit
Set serial unit number (default 1).
-v
Set device as a slave (default master).
-x
Set the FBS first bit start (default OFF).

Description:

The devc-hspi driver is generic and intended to be used with any SPI device on the Biscayne HSPI device bus. It is up to the application to implement the device specific protocol.

All communication sequences involve first a write of a buffer containing a byte command sequence, followed by a read. For each byte transmitted, a character is to be read. Some received characters will be data, and some will be dummy information, depending on the device's protocol.

To read data, a dummy write must be performed for each character to be read from the device. It is up to the author of this program to determine what is valid and what is dummy data read from the HSPI interface, since this protocol is device dependent.

Examples:

Start devc-hspi by setting the FSB and the serial unit number (default 1).

devc-hspi -x &

This command creates:

/dev/spi1

The following example shows how to write an application to use the HSPI driver to access the DS1305 RTC on the HSPI bus:

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <devctl.h>
#include <sys/dcmd_chr.h>

 #define DUMMY_WRITE_DATA    0x00 // NULL transmit character used for reading

// DS1305 Addresses
#define DS1305_READ_CONTROL_REG     0x0F
#define DS1305_WRIT_CONTROL_REG     0x8F

// Chip select values for devctl
#define _CTL_CS_CHG                 _CTL_RTS_CHG
#define _CTL_CS_LOW                 _CTL_RTS
#define _CTL_CS_HIGH                0

int main(void)
{
 int fd;
 unsigned char hspi_write[2] = {DS1305_READ_CONTROL_REG, DUMMY_WRITE_DATA};
 unsigned char hspi_read[2];
 int read_size = 0;
 int write_size = 0;
 int data = 0;
 int cs_status;

 // Open the HSPI port
 if((fd = open ("/dev/spi1", O_RDWR)) == -1)
 {
     fprintf(stderr, "Error with open() on /dev/spi1.  Make sure exists.\n");
     perror (NULL);
     exit(EXIT_FAILURE);
 }

 // Enable the Chip Select
 data = _CTL_CS_CHG | _CTL_CS_LOW;
 devctl(fd, DCMD_CHR_SERCTL, &data, sizeof(data), NULL);

 // Send the HSPI command to read the DS1305 control register
 write_size = write( fd, hspi_write, 2 );
 if( write_size < 2 )
 {
     printf("WARNING: Failed to write all data\n");
 }

 // Read the data sent back from the device (Note: some data is dummy data)
 read_size =  readcond( fd, hspi_read, 2, 2, 0, 10 );
 if( read_size < 2 )
 {
     printf("WARNING: Failed to read all data\n");
     printf("         (%d out of %d bytes read)\n", read_size, 2 );
 }

 // Disable the Chip Select
 data = _CTL_CS_CHG | _CTL_CS_HIGH;
 devctl(fd, DCMD_CHR_SERCTL, &data, sizeof(data), NULL);

 // Print out data read from the device
 printf("RX data = 0x%X (dummy)\n", hspi_read[0]);
 printf("RX data = 0x%X (DS1305 control register)\n", hspi_read[1]);
    // Read the current chip select linestatus
    cs_status = 0;
    devctl(fd, DCMD_CHR_LINESTATUS, &cs_status, sizeof(cs_status), NULL);
    if(cs_status) {
        printf("Chip Select Line High\n");
    }
    else {
        printf("Chip Select Line Low\n");
    }

    // Close the HSPI port
    close(fd);

    return 0;
}

See also:

Character I/O drivers (devc-*) in the Utilities Summary


[Previous] [Contents] [Index] [Next]