SPI Examples

Started by wwarczak, October 24, 2012, 04:28:58 PM

Previous topic - Next topic

jlumme

Has anyone successfully used spidev to communicate over SPI from user land ? I would like to talk to micro controller over the bus.
I've seem to have got a new device added to the bus with this dtsi for SSP2:


ssp1: ssp@80034000 {
    #address-cells = <1>;
    #size-cells = <0>;
    compatible = "fsl,imx23-spi";
    pinctrl-names = "default";
    pinctrl-0 = <&spi2_pins_a &spi2_pin_cs1>;
    status = "okay";

    flash: m25p80@0 {
        #address-cells = <1>;
        #size-cells = <1>;
        compatible = "sst,sst25vf064b";
        spi-max-frequency = <10000000>;
         reg = <0>;
    };
    ucontroller: spidev@1 {
        compatible = "linux,spidev";
        spi-max-frequency = <1000000>;
        reg = <1>;
    };



I've also added CS1 for SSP2:

//Added for chip select in SPI2
spi2_pin_cs1: spi2@1 {
    reg = <0>;
    fsl,pinmux-ids = <
    0x0042 /* MX23_PAD_GPMI_D03__SSP2_DATA4 */
    >;
    fsl,drive-strength = <1>;
    fsl,voltage = <1>;
    fsl,pull-up = <1>;
};


Now, after boot when I mount devtmpfs, spidev is shown under /dev.
The whole system has this SPI stuff:

# find / -name "spi*"
/sys/bus/spi
/sys/bus/spi/devices/spi32766.0
/sys/bus/spi/devices/spi32766.1
/sys/bus/spi/drivers/spidev
/sys/bus/spi/drivers/spidev/spi32766.1
/sys/bus/platform/drivers/spi_gpio
/sys/devices/80000000.apb/80000000.apbh/80034000.ssp/spi_master
/sys/devices/80000000.apb/80000000.apbh/80034000.ssp/spi_master/spi32766
/sys/devices/80000000.apb/80000000.apbh/80034000.ssp/spi_master/spi32766/spi32766.0
/sys/devices/80000000.apb/80000000.apbh/80034000.ssp/spi_master/spi32766/spi32766.1
/sys/devices/80000000.apb/80000000.apbh/80034000.ssp/spi_master/spi32766/spi32766.1/spidev
/sys/devices/80000000.apb/80000000.apbh/80034000.ssp/spi_master/spi32766/spi32766.1/spidev/spidev32766.1
/sys/class/spi_master
/sys/class/spi_master/spi32766
/sys/class/spidev
/sys/class/spidev/spidev32766.1
/sys/module/spidev
/home/default/spitest
/dev/spidev32766.1


But, when trying to run the spidev_test.c code from Documentation/spi, I seem to get strange "Invalid argument" (I don't specify anything).
The file descriptor for spidev is correct (/dev/spidev32766.1), and when I launch the test program, I see some activity in both, spidev driver, and also spi-mxs.c when main function sets up the spi details (bits, speed and spi mode).
But for some reason, I always just get -22 when trying to access the spi bus through spidev..


jlumme

Mmkay, after some digging, it turns out spi.c was causing my problems.
I didn't check who was forcing (spi-mxs.c?) half-duplex transfers, but it looks correct accorging to mx23 datasheet ch. 21.5.1.

Anyhow, the problem was that spidev_test.c code was assingning rx and tx buffers, and spi.c was having none of that here:
static int __spi_async(struct spi_device *spi, struct spi_message *message)
{
   ....
   if ((master->flags & SPI_MASTER_HALF_DUPLEX)
|| (spi->mode & SPI_3WIRE)) {
      ....
      if (xfer->rx_buf && xfer->tx_buf) {
return -EINVAL;
      }


After zeroing rx buffer from my transfer data packet, it looks to work as expected.
As usual, user error  8)