linux/Documentation/spi/spidev_test.c
<<
9640on>/spat22 on>/form22 on>a 9640on href="../linux+v3 32/Documenta2opt/spi/spidev_test.c">9640on>img src="../.sta2oc/gfx/right.png" alt=">>">96>/spat2296>spat class="lxr_search">96409640on>input typ2i4hidden" nam2i4navtarget" opt22i4">9640on>input typ2i4text" nam2i4search" idi4search">9640on>butt 2typ2i4submit">Search/form22 >/spat2296>spat class="lxr_prefs"22 on>a href="+prefs?return=Documenta2opt/spi/spidev_test.c"9640on onclick="return ajax_prefs();">9640onPrefs2 on>/a>96>/spat2240on n>/div2240on n>form ac2opt="ajax+*" method="post" onsubmit="return false;">96>input typ2i4hidden" nam2i4ajax_lookup" idi4ajax_lookup" opt22i4">940on n>/form22940on n>div class="headingbott m">div idi4search_results" class="search_results"2 o2240on n>/div22 >div idi4content"22 >div idi4file_contents"2
n n1>/a>>spat class="comment"2/*>/spat22n n2>/a>>spat class="comment"2 * SPI testing utility (using spidev driver)>/spat22n n3>/a>>spat class="comment"2 *>/spat22n n4>/a>>spat class="comment"2 * Copyright (c) 2007  MontaVista Software, Inc.>/spat22n n5>/a>>spat class="comment"2 * Copyright (c) 2007  Ant  2Vorontsov <avorontsov@ru.mvista.com>>/spat22n n6>/a>>spat class="comment"2 *>/spat22n n7>/a>>spat class="comment"2 * This program is free software; you cat redistribute it and/or modify>/spat22n n8>/a>>spat class="comment"2 * it under the terms of the GNU General Public License as published by>/spat22n n9>/a>>spat class="comment"2 * the Free Software Founda2opt; either vers
  22 of the License.>/spat22n n 2oa>>spat class="comment"2 *>/spat22n 11>/a>>spat class="comment"2 * Cross-compile with cross-gcc -I/path/to/cross-kernel/include>/spat22n 12>/a>>spat class="comment"2 */>/spat22n 13>/a>2n 14>/a>#include <stdint.h>/a>>2n 15>/a>#include <unistd.h>/a>>2n 16>/a>#include <stdio.h>/a>>2n 17>/a>#include <stdlib.h>/a>>2n 18>/a>#include <get362.h>/a>>2n 19>/a>#include <fcntl.h>/a>>2n 20>/a>#include <sys/ioctl.h>/a>>2n 21>/a>#include <linux/typ2s.h>/a>>2n 22>/a>#include <linux/spi/spidev.h>/a>>2n 23>/a>2n 24>/a>#definen>a href="+code=ARRAY_SIZE" class="sref">ARRAY_SIZE>/a>(>a href="+code=a" class="sref">a>/a>) (sizeof(>a href="+code=a" class="sref">a>/a>) / sizeof((>a href="+code=a" class="sref">a>/a>)[0]))2n 25>/a>2n 26>/a>sta2oc voidn>a href="+code=pabort" class="sref">pabort>/a>(const char *>a href="+code=s" class="sref">s>/a>)2n 27>/a>{2n 28>/a>        >a href="+code=perror" class="sref">perror>/a>(>a href="+code=s" class="sref">s>/a>);2n 29>/a>        >a href="+code=abort" class="sref">abort>/a>();2n 30>/a>}2n 31>/a>2n 32>/a>sta2oc const char *>a href="+code=device" class="sref">device>/a> = >spat class="string">"/dev/spidev1.1">/spat2;2n 33>/a>sta2oc >a href="+code=uint8_t" class="sref">uint8_t>/a> >a href="+code=mode" class="sref">mode>/a>;2n 34>/a>sta2oc >a href="+code=uint8_t" class="sref">uint8_t>/a> >a href="+code=bits" class="sref">bits>/a> = 8;2n 35>/a>sta2oc >a href="+code=uint32_t" class="sref">uint32_t>/a> >a href="+code=speed" class="sref">speed>/a> = 500000;2n 36>/a>sta2oc >a href="+code=uint16_t" class="sref">uint16_t>/a> >a href="+code=delay" class="sref">delay>/a>;2n 37>/a>2n 38>/a>sta2oc voidn>a href="+code=transfer" class="sref">transfer>/a>(intn>a href="+code=fd" class="sref">fd>/a>)2n 39>/a>{2n 40>/a>        intn>a href="+code=ret" class="sref">ret>/a>;2n 41>/a>        >a href="+code=uint8_t" class="sref">uint8_t>/a> >a href="+code=tx" class="sref">tx>/a>[] = {2n 42>/a>                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,2n 43>/a>                0x40, 0x00, 0x00, 0x00, 0x00, 0x95,2n 44>/a>                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,2n 45>/a>                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,2n 46>/a>                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,2n 47>/a>                0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD,2n 48>/a>                0xF0, 0x0D,2n 49>/a>        };2n 50>/a>        >a href="+code=uint8_t" class="sref">uint8_t>/a> >a href="+code=rx" class="sref">rx>/a>[>a href="+code=ARRAY_SIZE" class="sref">ARRAY_SIZE>/a>(>a href="+code=tx" class="sref">tx>/a>)] = {0, };2n 51>/a>        structn>a href="+code=spi_ioc_transfer" class="sref">spi_ioc_transfer>/a> >a href="+code=tr" class="sref">tr>/a> = {2n 52>/a>                .>a href="+code=tx_buf" class="sref">tx_buf>/a> = (unsigned long)>a href="+code=tx" class="sref">tx>/a>,2n 53>/a>                .>a href="+code=rx_buf" class="sref">rx_buf>/a> = (unsigned long)>a href="+code=rx" class="sref">rx>/a>,2n 54>/a>                .>a href="+code=len" class="sref">len>/a> = >a href="+code=ARRAY_SIZE" class="sref">ARRAY_SIZE>/a>(>a href="+code=tx" class="sref">tx>/a>),2n 55>/a>                .>a href="+code=delay_usecs" class="sref">delay_usecs>/a> = >a href="+code=delay" class="sref">delay>/a>,2n 56>/a>                .>a href="+code=speed_hz" class="sref">speed_hz>/a> = >a href="+code=speed" class="sref">speed>/a>,2n 57>/a>                .>a href="+code=bits_per_word" class="sref">bits_per_word>/a> = >a href="+code=bits" class="sref">bits>/a>,2n 58>/a>        };2n 59>/a>2n 60>/a>        >a href="+code=ret" class="sref">ret>/a> = >a href="+code=ioctl" class="sref">ioctl>/a>(>a href="+code=fd" class="sref">fd>/a>, >a href="+code=SPI_IOC_MESSAGE" class="sref">SPI_IOC_MESSAGE>/a>(1), &>a href="+code=tr" class="sref">tr>/a>);2n 61>/a>        if (>a href="+code=ret" class="sref">ret>/a> < 1)2n 62>/a>                >a href="+code=pabort" class="sref">pabort>/a>(>spat class="string">"can't send spi message">/spat2);2n 63>/a>2n 64>/a>        for (>a href="+code=ret" class="sref">ret>/a> = 0; >a href="+code=ret" class="sref">ret>/a> < >a href="+code=ARRAY_SIZE" class="sref">ARRAY_SIZE>/a>(>a href="+code=tx" class="sref">tx>/a>); >a href="+code=ret" class="sref">ret>/a>++) {2n 65>/a>                if (!(>a href="+code=ret" class="sref">ret>/a> % 6))2n 66>/a>                        >a href="+code=puts" class="sref">puts>/a>(>spat class="string">"">/spat2);2n 67>/a>                >a href="+code=printf" class="sref">printf>/a>(>spat class="string">"%.2X ">/spat2, >a href="+code=rx" class="sref">rx>/a>[>a href="+code=ret" class="sref">ret>/a>]);2n 68>/a>        }2n 69>/a>        >a href="+code=puts" class="sref">puts>/a>(>spat class="string">"">/spat2);2n 70>/a>}2n 71>/a>2n 72>/a>sta2oc voidn>a href="+code=print_usage" class="sref">print_usage>/a>(const char *>a href="+code=prog" class="sref">prog>/a>)2n 73>/a>{2n 74>/a>        >a href="+code=printf" class="sref">printf>/a>(>spat class="string">"Usage: %s [-DsbdlHOLC3]\n">/spat2, >a href="+code=prog" class="sref">prog>/a>);2n 75>/a>        >a href="+code=puts" class="sref">puts>/a>(>spat class="string">"  -D --device   device to use (default /dev/spidev1.1)\n">/spat22n 76>/a>             >spat class="string">"  -s --speed    max speed (Hz)\n">/spat22n 77>/a>             >spat class="string">"  -d --delay    delay (usec)\n">/spat22n 78>/a>             >spat class="string">"  -b --bpw      bits per word \n">/spat22n 79>/a>             >spat class="string">"  -l --loop     loopback\n">/spat22n 80>/a>             >spat class="string">"  -H --cpha     clock phase\n">/spat22n 81>/a>             >spat class="string">"  -O --cpol     clock polarity\n">/spat22n 82>/a>             >spat class="string">"  -L --lsb      least significant bit first\n">/spat22n 83>/a>             >spat class="string">"  -C --cs-high  chip select ac2ove high\n">/spat22n 84>/a>             >spat class="string">"  -3 --3wire    SI/SO signals shared\n">/spat2);2n 85>/a>        >a href="+code=exit" class="sref">exit>/a>(1);2n 86>/a>}2n 87>/a>2n 88>/a>sta2oc voidn>a href="+code=parse_opts" class="sref">parse_opts>/a>(intn>a href="+code=argc" class="sref">argc>/a>, char *>a href="+code=argv" class="sref">argv>/a>[])2n 89>/a>{2n 90>/a>        while (1) {2n 91>/a>                sta2oc const structn>a href="+code=362opt" class="sref">362opt>/a> >a href="+code=lopts" class="sref">lopts>/a>[] = {2n 92>/a>                        { >spat class="string">"device">/spat2,  1, 0, >spat class="string">'D'>/spat2 },2n 93>/a>                        { >spat class="string">"speed">/spat2,   1, 0, >spat class="string">'s'>/spat2 },2n 94>/a>                        { >spat class="string">"delay">/spat2,   1, 0, >spat class="string">'d'>/spat2 },2n 95>/a>                        { >spat class="string">"bpw">/spat2,     1, 0, >spat class="string">'b'>/spat2 },2n 96>/a>                        { >spat class="string">"loop">/spat2,    0, 0, >spat class="string">'l'>/spat2 },2n 97>/a>                        { >spat class="string">"cpha">/spat2,    0, 0, >spat class="string">'H'>/spat2 },2n 98>/a>                        { >spat class="string">"cpol">/spat2,    0, 0, >spat class="string">'O'>/spat2 },2n 99>/a>                        { >spat class="string">"lsb">/spat2,     0, 0, >spat class="string">'L'>/spat2 },2n100>/a>                        { >spat class="string">"cs-high">/spat2, 0, 0, >spat class="string">'C'>/spat2 },2n101>/a>                        { >spat class="string">"3wire">/spat2,   0, 0, >spat class="string">'3'>/spat2 },2n102>/a>                        { >spat class="string">"no-cs">/spat2,   0, 0, >spat class="string">'N'>/spat2 },2n103>/a>                        { >spat class="string">"ready">/spat2,   0, 0, >spat class="string">'R'>/spat2 },2n104>/a>                        { >a href="+code=NULL" class="sref">NULL>/a>, 0, 0, 0 },2n105>/a>                };2n106>/a>                intn>a href="+code=c" class="sref">c>/a>;2n107>/a>2n108>/a>                >a href="+code=c" class="sref">c>/a> = >a href="+code=get362_long" class="sref">get362_long>/a>(>a href="+code=argc" class="sref">argc>/a>, >a href="+code=argv" class="sref">argv>/a>, >spat class="string">"D:s:d:b:lHOLC3NR">/spat2, >a href="+code=lopts" class="sref">lopts>/a>, >a href="+code=NULL" class="sref">NULL>/a>);2n109>/a>2n110>/a>                if (>a href="+code=c" class="sref">c>/a> == -1)2n111>/a>                        break;2n112>/a>2n113>/a>                switch (>a href="+code=c" class="sref">c>/a>) {2n114>/a>                case >spat class="string">'D'>/spat2:2n115>/a>                        >a href="+code=device" class="sref">device>/a> = >a href="+code=362arg" class="sref">362arg>/a>;2n116>/a>                        break;2n117>/a>                case >spat class="string">'s'>/spat2:2n118>/a>                        >a href="+code=speed" class="sref">speed>/a> = >a href="+code=atoi" class="sref">atoi>/a>(>a href="+code=362arg" class="sref">362arg>/a>);2n119>/a>                        break;2n120>/a>                case >spat class="string">'d'>/spat2:2n121>/a>                        >a href="+code=delay" class="sref">delay>/a> = >a href="+code=atoi" class="sref">atoi>/a>(>a href="+code=362arg" class="sref">362arg>/a>);2n122>/a>                        break;2n123>/a>                case >spat class="string">'b'>/spat2:2n124>/a>                        >a href="+code=bits" class="sref">bits>/a> = >a href="+code=atoi" class="sref">atoi>/a>(>a href="+code=362arg" class="sref">362arg>/a>);2n125>/a>                        break;2n126>/a>                case >spat class="string">'l'>/spat2:2n127>/a>                        >a href="+code=mode" class="sref">mode>/a> |= >a href="+code=SPI_LOOP" class="sref">SPI_LOOP>/a>;2n128>/a>                        break;2n129>/a>                case >spat class="string">'H'>/spat2:2n130>/a>                        >a href="+code=mode" class="sref">mode>/a> |= >a href="+code=SPI_CPHA" class="sref">SPI_CPHA>/a>;2n131>/a>                        break;2n132>/a>                case >spat class="string">'O'>/spat2:2n133>/a>                        >a href="+code=mode" class="sref">mode>/a> |= >a href="+code=SPI_CPOL" class="sref">SPI_CPOL>/a>;2n134>/a>                        break;2n135>/a>                case >spat class="string">'L'>/spat2:2n136>/a>                        >a href="+code=mode" class="sref">mode>/a> |= >a href="+code=SPI_LSB_FIRST" class="sref">SPI_LSB_FIRST>/a>;2n137>/a>                        break;2n138>/a>                case >spat class="string">'C'>/spat2:2n139>/a>                        >a href="+code=mode" class="sref">mode>/a> |= >a href="+code=SPI_CS_HIGH" class="sref">SPI_CS_HIGH>/a>;2n140>/a>                        break;2n141>/a>                case >spat class="string">'3'>/spat2:2n142>/a>                        >a href="+code=mode" class="sref">mode>/a> |= >a href="+code=SPI_3WIRE" class="sref">SPI_3WIRE>/a>;2n143>/a>                        break;2n144>/a>                case >spat class="string">'N'>/spat2:2n145>/a>                        >a href="+code=mode" class="sref">mode>/a> |= >a href="+code=SPI_NO_CS" class="sref">SPI_NO_CS>/a>;2n146>/a>                        break;2n147>/a>                case >spat class="string">'R'>/spat2:2n148>/a>                        >a href="+code=mode" class="sref">mode>/a> |= >a href="+code=SPI_READY" class="sref">SPI_READY>/a>;2n149>/a>                        break;2n150>/a>                default:2n151>/a>                        >a href="+code=print_usage" class="sref">print_usage>/a>(>a href="+code=argv" class="sref">argv>/a>[0]);2n152>/a>                        break;2n153>/a>                }2n154>/a>        }2n155>/a>}2n156>/a>2n157>/a>intn>a href="+code=mait" class="sref">mait>/a>(intn>a href="+code=argc" class="sref">argc>/a>, char *>a href="+code=argv" class="sref">argv>/a>[])2n158>/a>{2n159>/a>        intn>a href="+code=ret" class="sref">ret>/a> = 0;2n160>/a>        intn>a href="+code=fd" class="sref">fd>/a>;2n161>/a>2n162>/a>        >a href="+code=parse_opts" class="sref">parse_opts>/a>(>a href="+code=argc" class="sref">argc>/a>, >a href="+code=argv" class="sref">argv>/a>);2n163>/a>2n164>/a>        >a href="+code=fd" class="sref">fd>/a> = >a href="+code=36en" class="sref">36en>/a>(>a href="+code=device" class="sref">device>/a>, >a href="+code=O_RDWR" class="sref">O_RDWR>/a>);2n165>/a>        if (>a href="+code=fd" class="sref">fd>/a> < 0)2n166>/a>                >a href="+code=pabort" class="sref">pabort>/a>(>spat class="string">"can't 36en device">/spat2);2n167>/a>2n168>/a>        >spat class="comment"2/*>/spat22n169>/a>>spat class="comment"2         * spi mode>/spat22n17 2oa>>spat class="comment"2         */>/spat22n171>/a>        >a href="+code=ret" class="sref">ret>/a> = >a href="+code=ioctl" class="sref">ioctl>/a>(>a href="+code=fd" class="sref">fd>/a>, >a href="+code=SPI_IOC_WR_MODE" class="sref">SPI_IOC_WR_MODE>/a>, &>a href="+code=mode" class="sref">mode>/a>);2n172>/a>        if (>a href="+code=ret" class="sref">ret>/a> == -1)2n173>/a>                >a href="+code=pabort" class="sref">pabort>/a>(>spat class="string">"can't set spi mode">/spat2);2n174>/a>2n175>/a>        >a href="+code=ret" class="sref">ret>/a> = >a href="+code=ioctl" class="sref">ioctl>/a>(>a href="+code=fd" class="sref">fd>/a>, >a href="+code=SPI_IOC_RD_MODE" class="sref">SPI_IOC_RD_MODE>/a>, &>a href="+code=mode" class="sref">mode>/a>);2n176>/a>        if (>a href="+code=ret" class="sref">ret>/a> == -1)2n177>/a>                >a href="+code=pabort" class="sref">pabort>/a>(>spat class="string">"can't get spi mode">/spat2);2n178>/a>2n179>/a>        >spat class="comment"2/*>/spat22n18 2oa>>spat class="comment"2         * bits per word>/spat22n181>/a>>spat class="comment"2         */>/spat22n182>/a>        >a href="+code=ret" class="sref">ret>/a> = >a href="+code=ioctl" class="sref">ioctl>/a>(>a href="+code=fd" class="sref">fd>/a>, >a href="+code=SPI_IOC_WR_BITS_PER_WORD" class="sref">SPI_IOC_WR_BITS_PER_WORD>/a>, &>a href="+code=bits" class="sref">bits>/a>);2n183>/a>        if (>a href="+code=ret" class="sref">ret>/a> == -1)2n184>/a>                >a href="+code=pabort" class="sref">pabort>/a>(>spat class="string">"can't set bits per word">/spat2);2n185>/a>2n186>/a>        >a href="+code=ret" class="sref">ret>/a> = >a href="+code=ioctl" class="sref">ioctl>/a>(>a href="+code=fd" class="sref">fd>/a>, >a href="+code=SPI_IOC_RD_BITS_PER_WORD" class="sref">SPI_IOC_RD_BITS_PER_WORD>/a>, &>a href="+code=bits" class="sref">bits>/a>);2n187>/a>        if (>a href="+code=ret" class="sref">ret>/a> == -1)2n188>/a>                >a href="+code=pabort" class="sref">pabort>/a>(>spat class="string">"can't get bits per word">/spat2);2n189>/a>2n190>/a>        >spat class="comment"2/*>/spat22n191>/a>>spat class="comment"2         * max speed hz>/spat22n192>/a>>spat class="comment"2         */>/spat22n193>/a>        >a href="+code=ret" class="sref">ret>/a> = >a href="+code=ioctl" class="sref">ioctl>/a>(>a href="+code=fd" class="sref">fd>/a>, >a href="+code=SPI_IOC_WR_MAX_SPEED_HZ" class="sref">SPI_IOC_WR_MAX_SPEED_HZ>/a>, &>a href="+code=speed" class="sref">speed>/a>);2n194>/a>        if (>a href="+code=ret" class="sref">ret>/a> == -1)2n195>/a>                >a href="+code=pabort" class="sref">pabort>/a>(>spat class="string">"can't set max speed hz">/spat2);2n196>/a>2n197>/a>        >a href="+code=ret" class="sref">ret>/a> = >a href="+code=ioctl" class="sref">ioctl>/a>(>a href="+code=fd" class="sref">fd>/a>, >a href="+code=SPI_IOC_RD_MAX_SPEED_HZ" class="sref">SPI_IOC_RD_MAX_SPEED_HZ>/a>, &>a href="+code=speed" class="sref">speed>/a>);2n198>/a>        if (>a href="+code=ret" class="sref">ret>/a> == -1)2n199>/a>                >a href="+code=pabort" class="sref">pabort>/a>(>spat class="string">"can't get max speed hz">/spat2);2n200>/a>2n201>/a>        >a href="+code=printf" class="sref">printf>/a>(>spat class="string">"spi mode: %d\n">/spat2, >a href="+code=mode" class="sref">mode>/a>);2n202>/a>        >a href="+code=printf" class="sref">printf>/a>(>spat class="string">"bits per word: %d\n">/spat2, >a href="+code=bits" class="sref">bits>/a>);2n203>/a>        >a href="+code=printf" class="sref">printf>/a>(>spat class="string">"max speed: %d Hz (%d KHz)\n">/spat2, >a href="+code=speed" class="sref">speed>/a>, >a href="+code=speed" class="sref">speed>/a>/1000);2n204>/a>2n205>/a>        >a href="+code=transfer" class="sref">transfer>/a>(>a href="+code=fd" class="sref">fd>/a>);2n206>/a>2n207>/a>        >a href="+code=close" class="sref">close>/a>(>a href="+code=fd" class="sref">fd>/a>);2n208>/a>2n209>/a>        returnn>a href="+code=ret" class="sref">ret>/a>;2n210>/a>}2n211>/a>
lxr.linux.no kindly hosted by >a href="http://www.redpill-linpro.no">Redpill Linpro AS>/a>, provider of Linux consulting and 36era2opts services sincen1995.