2010-02-25 14:31:33     How to use SPI bus?

Document created by Aaronwu Employee on Aug 20, 2013
Version 1Show Document
  • View in full screen mode

2010-02-25 14:31:33     How to use SPI bus?

Joonas Reunamo (FINLAND)

Message: 86554   

 

Hardware: cm-bf537e V3.1, bf core versio 0.3

 

Software:  uClinux-dist Version: 2009R1.1 , toolchain 2009r1-rc10

 

 

My goal is to read(/write) several different sensores which use SPI bus.

 

Currently Im missing any spi device node from /dev. I read spi-wiki-page, looked through kernel spi documentation and searched through forum for help. In forum I caught a useful thread by Lukasz (this). Unfortunately I still miss even the spi device node.

 

I need to admit Im generally bit confused by all registrations and declaration stuff I hitted. So I would appreciate somewhat "for dummies" help and links. I have previoulsy used I2C but it obviously works so differntly.

 

Here is what I did:

 

My make config options

 

--- SPI support                                                   │ │

  │ │          *** SPI Master Controller Drivers ***                       │ │

  │ │    <*>   SPI controller driver for ADI Blackfin5xx                   │ │

  │ │    [ ]     SPI bus lock                                              │ │

  │ │    < >   emulate SPI bus with Blackfin SPORT                         │ │

  │ │    < >   Bitbanging SPI master                                       │ │

  │ │          *** SPI Protocol Masters ***                                │ │

  │ │    <M>   SPI EEPROMs from most vendors                               │ │

  │ │    < >   Infineon TLE62X0 (for power switching) 

 

Lukasz had "User mode SPI device driver support" option. I don't. Does this make a difference?

 

 

 

I added to board header file the same thing as he did:

 

#if defined(CONFIG_SPI_SPIDEV) || defined(CONFIG_SPI_SPIDEV_MODULE)

static struct bfin5xx_spi_chip spidev_chip_info = {              

        .ctl_reg = 0x4,                                          

        .enable_dma = 0,                                         

        .bits_per_word = 16,                                     

        .cs_change_per_word = 0,                                 

        .cs_chg_udelay = 0,                                      

        .cs_gpio = 0,                                            

};                                                               

#endif

 

#if defined(CONFIG_SPI_SPIDEV) || defined(CONFIG_SPI_SPIDEV_MODULE)

        {                                                        

                .modalias = "spidev",                            

                .max_speed_hz = 12500000,     /* max spi clock (SCK) speed in HZ */

                .bus_num = 1,                                                    

                .chip_select = 0,                                                

                .platform_data = NULL,                                           

                .controller_data = &spidev_chip_info,                            

                .mode = SPI_MODE_1,                                              

        },                                                                       

#endif     

 

Where could I find more information about those .ctl_reg etc. options? Should I modify them somehow to according to my hardware? Also stuff coming after .modalias = "spidev", how I deermine that from my hw?

 

I added to board header file:

#define CFG_SPI_SPIDEV

 

CM-BF537E_defconfig says:

# CONFIG_SPI_SPIDEV is not set

 

Should this just be set "CONFIG_SPI_SPIDEV=y" and save the file?

 

After booting compiled kernel:

 

root:/dev> find / -name '*spi*'

/lib/modules/2.6.28.10-ADI-2009R1.1/kernel/drivers/spi

/sys/devices/platform/bfin-spi.0

/sys/devices/platform/bfin-spi.0/spi_master

/sys/devices/platform/bfin-spi.0/spi_master/spi0

/sys/bus/platform/devices/bfin-spi.0

/sys/bus/platform/drivers/bfin-spi

/sys/bus/platform/drivers/bfin-spi/bfin-spi.0

/sys/bus/spi

/sys/class/spi_master

/sys/class/spi_master/spi0

/bin/spidev_test

 

 

root:/dev> ls

console             mtd2ro              ram10               ram7

cpu_dma_latency     mtdblock0           ram11               ram8

full                mtdblock1           ram12               ram9

i2c-0               mtdblock2           ram13               random

kmsg                network_latency     ram14               sport0

mem                 network_throughput  ram15               sport1

mtd0                null                ram2                tty

mtd0ro              ptmx                ram3                ttyBF0

mtd1                pts                 ram4                urandom

mtd1ro              ram0                ram5                zero

mtd2                ram1                ram6

 

 

 

mdev -s command does not help?

 

Any help is appreciated.

QuoteReplyEditDelete

 

 

2010-02-26 06:33:54     Re: How to use SPI bus?

Mike Frysinger (UNITED STATES)

Message: 86600   

 

you need to enable the SPIDEV driver.  if you dont, then nothing else you do matters.

 

did you disable the EXPERIMENTAL config option ?

QuoteReplyEditDelete

 

 

2010-02-26 08:45:43     Re: How to use SPI bus?

Joonas Reunamo (FINLAND)

Message: 86609   

 

Thanks, Mike!  But...

 

I enabled experimental config and got   "<*>   User mode SPI device driver support" option and selected it. Anyway, after make menuconfig and setting USer mode SPI devicedriver support, CM-BF537E_defconfig -file still had "# CONFIG_SPI_SPIDEV is not set" and compiled kernel didn't show spiX.X "nodes"

 

Then I defaulted config, set everything from beginning and also I modified CM-BF537E_defconfig -file in editor (is this how it is supposed to be done?) and set

 

CONFIG_SPI_SPIDEV=y

 

But still after booting new kernel spidev node is nowhere. Gpios which I set in menuconfig appeared to /dev.

 

see:

 

bfin> tftp 0x1000000 uImage.initramfs

Using Blackfin EMAC device

TFTP from server 192.168.1.1; our IP address is 192.168.1.2

Filename 'uImage.initramfs'.

Load address: 0x1000000

Loading: #################################################################

         #################################################################

         #################################################################

         ###################################

done

Bytes transferred = 3372099 (337443 hex)

bfin> bootm 0x1000000

## Booting kernel from Legacy Image at 01000000 ...

   Image Name:   Linux-2.6.28.10-ADI-2009R1.1

   Created:      2010-02-26  13:29:44 UTC

   Image Type:   Blackfin Linux Kernel Image (gzip compressed)

   Data Size:    3372035 Bytes =  3.2 MB

   Load Address: 00001000

   Entry Point:  001902b0

   Verifying Checksum ... OK

   Uncompressing Kernel Image ... OK

Starting Kernel at = 001902b0

Linux version 2.6.28.10-ADI-2009R1.1 (joonas@nuhamasiina) (gcc version 4.1.2 (ADI svn)) #24 Fri Feb 26 15:29:39 EET 2010

Board Memory: 32MB

Kernel Managed Memory: 32MB

Memory map:

  fixedcode = 0x00000400-0x00000490

  text      = 0x00001000-0x0010d600

  rodata    = 0x0010d600-0x00162700

  bss       = 0x00163000-0x00173770

  data      = 0x00173770-0x00182000

    stack   = 0x00180000-0x00182000

  init      = 0x00182000-0x006a2000

  available = 0x006a2000-0x01eff000

  DMA Zone  = 0x01f00000-0x02000000

Hardware Trace Active and Enabled

Boot Mode: 0

Blackfin support (C) 2004-2009 Analog Devices, Inc.

Compiled for ADSP-BF537 Rev 0.3

Blackfin Linux support by   blackfin.uclinux.org/

Processor Speed: 525 MHz core clock and 131 MHz System Clock

NOMPU: setting up cplb tables

Instruction Cache Enabled for CPU0

Data Cache Enabled for CPU0 (write-back)

Built 1 zonelists in Zone order, mobility grouping off.  Total pages: 7873

Kernel command line: root=/dev/mtdblock0 rw clkin_hz=25000000 earlyprintk=serial,uart0,115200 console=ttyBF0,115200

Configuring Blackfin Priority Driven Interrupts

PID hash table entries: 128 (order: 7, 512 bytes)

console [ttyBF0] enabled

Dentry cache hash table entries: 4096 (order: 2, 16384 bytes)

Inode-cache hash table entries: 2048 (order: 1, 8192 bytes)

Memory available: 24660k/32768k RAM, (5248k init code, 1073k kernel code, 467k data, 1024k dma, 292k reserved)

Calibrating delay loop... 1042.43 BogoMIPS (lpj=521216)

Security Framework initialized

Mount-cache hash table entries: 512

Blackfin Scratchpad data SRAM: 4 KB

Blackfin L1 Data A SRAM: 16 KB (9 KB free)

Blackfin L1 Data B SRAM: 16 KB (16 KB free)

Blackfin L1 Instruction SRAM: 48 KB (38 KB free)

net_namespace: 288 bytes

NET: Registered protocol family 16

Blackfin DMA Controller

cm_bf537e_init(): registering device resources

NET: Registered protocol family 2

IP route cache hash table entries: 1024 (order: 0, 4096 bytes)

TCP established hash table entries: 1024 (order: 1, 8192 bytes)

TCP bind hash table entries: 1024 (order: 0, 4096 bytes)

TCP: Hash tables configured (established 1024 bind 1024)

TCP reno registered

NET: Registered protocol family 1

JFFS2 version 2.2. (NAND) �© 2001-2006 Red Hat, Inc.

msgmni has been set to 48

io scheduler noop registered (default)

simple-gpio: now handling 48 GPIOs: 0 - 47

Serial: Blackfin serial driver

bfin-uart.1: ttyBF0 at MMIO 0xffc00400 (irq = 18) is a BFIN-UART

bfin-uart.1: ttyBF1 at MMIO 0xffc02000 (irq = 20) is a BFIN-UART

brd: module loaded

bfin_mii_bus: probed

bfin_mac: attached PHY driver [Generic PHY] (mii_bus:phy_addr=0:01, irq=-1, mdc_clk=2500000Hz(mdc_div=25)@sclk=131MHz)

bfin_mac bfin_mac.0: Blackfin on-chip Ethernet MAC driver, Version 1.1

gpio-addr-flash: probing 16-bit flash bus

gpio-addr-flash: Found 1 x16 devices at 0x0 in 16-bit bank

NOR chip too large to fit in mapping. Attempting to cope...

Intel/Sharp Extended Query Table at 0x010A

Intel/Sharp Extended Query Table at 0x010A

Intel/Sharp Extended Query Table at 0x010A

Intel/Sharp Extended Query Table at 0x010A

Intel/Sharp Extended Query Table at 0x010A

Using buffer write method

Using auto-unlock on power-up/resume

cfi_cmdset_0001: Erase suspend on write enabled

Reducing visibility of 8192KiB chip to 4096KiB

RedBoot partition parsing not available

gpio-addr-flash: Using board partition definition

Creating 3 MTD partitions on "gpio-addr-flash":

0x00000000-0x00040000 : "bootloader(nor)"

0x00040000-0x00140000 : "linux kernel(nor)"

0x00140000-0x00400000 : "file system(nor)"

bfin-spi bfin-spi.0: Blackfin on-chip SPI Controller Driver, Version 1.0, regs_base@ffc00500, dma channel@17

i2c /dev entries driver

i2c-bfin-twi i2c-bfin-twi.0: Blackfin BF5xx on-chip I2C TWI Contoller, regs_base@ffc01400

TCP cubic registered

NET: Registered protocol family 17

drivers/rtc/hctosys.c: unable to open rtc device (rtc0)

Freeing unused kernel memory: 5248k freed

dma_alloc_init: dma_page @ 0x01adc000 - 256 pages at 0x01f00000

 

Welcome to:

       ____ _  _                 

      /  __| ||_|             _  _

_   _| |  | | _ ____  _   _ \ \/ /

| | | | |  | || |  _ \| | | | \  /

| |_| | |__| || | | | | |_| | /  \

|  ___\____|_||_|_| |_|\____|/_/\_\

|_|                                 

 

For further information see:

  www.uclinux.org/

  blackfin.uclinux.org/

 

  www.bluetechnix.at

 

 

BusyBox v1.13.4 (2010-02-26 15:18:36 EET) built-in shell (msh)

Enter 'help' for a list of built-in commands.

 

root:/> ls dev/

console             gpio27              gpio5               ram1

cpu_dma_latency     gpio28              gpio6               ram10

full                gpio29              gpio7               ram11

gpio0               gpio3               gpio8               ram12

gpio1               gpio30              gpio9               ram13

gpio10              gpio31              i2c-0               ram14

gpio11              gpio32              kmsg                ram15

gpio12              gpio33              mem                 ram2

gpio13              gpio34              mtd0                ram3

gpio14              gpio35              mtd0ro              ram4

gpio15              gpio36              mtd1                ram5

gpio16              gpio37              mtd1ro              ram6

gpio17              gpio38              mtd2                ram7

gpio18              gpio39              mtd2ro              ram8

gpio19              gpio4               mtdblock0           ram9

gpio2               gpio40              mtdblock1           random

gpio20              gpio41              mtdblock2           sport0

gpio21              gpio42              network_latency     sport1

gpio22              gpio43              network_throughput  tty

gpio23              gpio44              null                ttyBF0

gpio24              gpio45              ptmx                ttyBF1

gpio25              gpio46              pts                 urandom

gpio26              gpio47              ram0                zero

 

 

root:/> find /sys/ -name '*spidev*'

/sys/bus/spi/drivers/spidev

/sys/class/spidev

/sys/module/spidev

root:/>

QuoteReplyEditDelete

 

 

2010-02-27 04:37:38     Re: How to use SPI bus?

Mike Frysinger (UNITED STATES)

Message: 86633   

 

you shouldnt be touching any of the defconfig files.  their contents dont matter -- *your* config file is in linux-2.6.x/.config.

 

if the devices arent in /dev/, then your board resources are incomplete.  make sure you have proper spidev entries in it.

QuoteReplyEditDelete

 

 

2010-03-02 10:41:10     Re: How to use SPI bus?

Joonas Reunamo (FINLAND)

Message: 86729   

 

Thanks Mike!

 

I changed controller and framework resources as added below and got the node spidev0.1 to /dev

 

No I will go on testing and see how to hook up my devices.

 

Thanks again!

 

--------

 

 

 

#if defined(CONFIG_SPI_SPIDEV) || defined(CONFIG_SPI_SPIDEV_MODULE)

static struct bfin5xx_spi_chip spidev_chip_info = {                                                          

        .enable_dma = 0,                                         

        .bits_per_word = 16,                                     

        .cs_change_per_word = 0,                                 

        .cs_chg_udelay = 0,                                       

};                                                               

#endif

 

 

 

#if defined(CONFIG_SPI_SPIDEV) || defined(CONFIG_SPI_SPIDEV_MODULE)

        {                                                        

                .modalias = "spidev",                            

                .max_speed_hz = 3125000,     /* max spi clock (SCK) speed in HZ */

                .bus_num = 0,                                                    

                .chip_select = 1,                                                                                        

                .controller_data = &spidev_chip_info,                            

                //.mode = SPI_MODE_1,                                              

        },                                                                       

#endif    

QuoteReplyEditDelete

 

 

2010-03-29 23:18:36     Re: How to use SPI bus?

Hugh Shane (UNITED STATES)

Message: 87808   

 

Did you ever get this to work? I've been following this thread and have imitated the steps you've take so far. When I boot my kernel I can see /dev/spidev0.1

 

I wrote a test application which should repeatedly write to the SPI bus, however, it hangs at the open() invocation below. I'm stumped!

 

 

main() {

    int spi;

    uint16_t sample;

    spi = open("/dev/spidev0.1",O_RDWR);

    if (spi == -1)

        return 0;

    printf("testing SPI output...");

    uint16_t* temp;

    *temp = 0x55aa;

    while (1){

        usleep(100);

        if (write(spi,temp,sizeof(sample)) < 0)

        {

             printf ("\nSPI write failed");

             return 0;

        }

    }

}

Attachments

    Outcomes