linux/Documentation/spi/spidev_test.c
<<
v3.9v3./spaalu v3./formlu v3.a v3.9v3 href="../linux+v3.7. /Documentan va/spi/spidev_test.c">v3.9v3.img src="../.stan c/gfx/right.png" alt=">>">v3./spaaluv3.spaa class="lxr_search">v3.9v3.9v3.input typ hidden" nam navtarget" > ">v3.9v3.input typ text" nam search" id search">v3.9v3.butt8" typ submit">Searchv3.9v3Prefsu v3./a>v3./spaalu.9v3 3./divlu.9v3 3.form acn va="ajax+*" method="post" onsubmit="return false;">v3.input typ hidden" nam ajax_lookup" id ajax_lookup" > ">v.9v3 3./formluv.9v3 3.div class="headingbott8m">3 31./a>.spaa class="comment"l/*./spaalu3 32./a>.spaa class="comment"l * SPI testing utility (using spidev driver)./spaalu3 33./a>.spaa class="comment"l *./spaalu3 34./a>.spaa class="comment"l * Copyright (c) 2007 MontaVista Software, Inc../spaalu3 35./a>.spaa class="comment"l * Copyright (c) 2007 Ant8" Vorontsov <avorontsov@ru.mvista.com>./spaalu3 36./a>.spaa class="comment"l *./spaalu3 37./a>.spaa class="comment"l * This program is free software; you caa redistribute it and/or modify./spaalu3 38./a>.spaa class="comment"l * it under the terms of the GNU General Public License as published by./spaalu3 39./a>.spaa class="comment"l * the Free Software Foundan va; either vers18" 2 of the License../spaalu3 va>.spaa class="comment"l *./spaalu3 11./a>.spaa class="comment"l * Cross-compile with cross-gcc -I/path/to/cross-kernel/include./spaalu3 12./a>.spaa class="comment"l */./spaalu3 13./a>u3 14./a>#include <stdint.h./a>>u3 15./a>#include <unistd.h./a>>u3 16./a>#include <stdio.h./a>>u3 17./a>#include <stdlib.h./a>>u3 18./a>#include <getion.h./a>>u3 19./a>#include <fcntl.h./a>>u3 20./a>#include <sys/ioctl.h./a>>u3 21./a>#include <linux/typ s.h./a>>u3 22./a>#include <linux/spi/spidev.h./a>>u3 23./a>u3 24./a>#define3.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]))u3 25./a>u3 26./a>stan c void3.a href="+code=pabort" class="sref">pabort./a>(const char *.a href="+code=s" class="sref">s./a>)u3 27./a>{u3 28./a> .a href="+code=perror" class="sref">perror./a>(.a href="+code=s" class="sref">s./a>);u3 29./a> .a href="+code=abort" class="sref">abort./a>();u3 30./a>}u3 31./a>u3 32./a>stan c const char *.a href="+code=device" class="sref">device./a> = .spaa class="string">"/dev/spidev1.1"./spaal;u3 33./a>stan c .a href="+code=uint8_t" class="sref">uint8_t./a> .a href="+code=mode" class="sref">mode./a>;u3 34./a>stan c .a href="+code=uint8_t" class="sref">uint8_t./a> .a href="+code=bits" class="sref">bits./a> = 8;u3 35./a>stan c .a href="+code=uint32_t" class="sref">uint32_t./a> .a href="+code=speed" class="sref">speed./a> = 500000;u3 36./a>stan c .a href="+code=uint16_t" class="sref">uint16_t./a> .a href="+code=delay" class="sref">delay./a>;u3 37./a>u3 38./a>stan c void3.a href="+code=transfer" class="sref">transfer./a>(int3.a href="+code=fd" class="sref">fd./a>)u3 39./a>{u3 40./a> int3.a href="+code=ret" class="sref">ret./a>;u3 41./a> .a href="+code=uint8_t" class="sref">uint8_t./a> .a href="+code=tx" class="sref">tx./a>[] = {u3 42./a> 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,u3 43./a> 0x40, 0x00, 0x00, 0x00, 0x00, 0x95,u3 44./a> 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,u3 45./a> 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,u3 46./a> 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,u3 47./a> 0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD,u3 48./a> 0xF0, 0x0D,u3 49./a> };u3 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, };u3 51./a> struct3.a href="+code=spi_ioc_transfer" class="sref">spi_ioc_transfer./a> .a href="+code=tr" class="sref">tr./a> = {u3 52./a> ..a href="+code=tx_buf" class="sref">tx_buf./a> = (unsigned long).a href="+code=tx" class="sref">tx./a>,u3 53./a> ..a href="+code=rx_buf" class="sref">rx_buf./a> = (unsigned long).a href="+code=rx" class="sref">rx./a>,u3 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>),u3 55./a> ..a href="+code=delay_usecs" class="sref">delay_usecs./a> = .a href="+code=delay" class="sref">delay./a>,u3 56./a> ..a href="+code=speed_hz" class="sref">speed_hz./a> = .a href="+code=speed" class="sref">speed./a>,u3 57./a> ..a href="+code=bits_per_word" class="sref">bits_per_word./a> = .a href="+code=bits" class="sref">bits./a>,u3 58./a> };u3 59./a>u3 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>);u3 61./a> if (.a href="+code=ret" class="sref">ret./a> < 1)u3 62./a> .a href="+code=pabort" class="sref">pabort./a>(.spaa class="string">"can't send spi message"./spaal);u3 63./a>u3 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>++) {u3 65./a> if (!(.a href="+code=ret" class="sref">ret./a> % 6))u3 66./a> .a href="+code=puts" class="sref">puts./a>(.spaa class="string">""./spaal);u3 67./a> .a href="+code=printf" class="sref">printf./a>(.spaa class="string">"%.2X "./spaal, .a href="+code=rx" class="sref">rx./a>[.a href="+code=ret" class="sref">ret./a>]);u3 68./a> }u3 69./a> .a href="+code=puts" class="sref">puts./a>(.spaa class="string">""./spaal);u3 70./a>}u3 71./a>u3 72./a>stan c void3.a href="+code=print_usage" class="sref">print_usage./a>(const char *.a href="+code=prog" class="sref">prog./a>)u3 73./a>{u3 74./a> .a href="+code=printf" class="sref">printf./a>(.spaa class="string">"Usage: %s [-DsbdlHOLC3]\n"./spaal, .a href="+code=prog" class="sref">prog./a>);u3 75./a> .a href="+code=puts" class="sref">puts./a>(.spaa class="string">" -D --device device to use (default /dev/spidev1.1)\n"./spaalu3 76./a> .spaa class="string">" -s --speed max speed (Hz)\n"./spaalu3 77./a> .spaa class="string">" -d --delay delay (usec)\n"./spaalu3 78./a> .spaa class="string">" -b --bpw bits per word \n"./spaalu3 79./a> .spaa class="string">" -l --loop loopback\n"./spaalu3 80./a> .spaa class="string">" -H --cpha clock phase\n"./spaalu3 81./a> .spaa class="string">" -O --cpol clock polarity\n"./spaalu3 82./a> .spaa class="string">" -L --lsb least significant bit first\n"./spaalu3 83./a> .spaa class="string">" -C --cs-high chip select acn ve high\n"./spaalu3 84./a> .spaa class="string">" -3 --3wire SI/SO signals shared\n"./spaal);u3 85./a> .a href="+code=exit" class="sref">exit./a>(1);u3 86./a>}u3 87./a>u3 88./a>stan c void3.a href="+code=parse_opts" class="sref">parse_opts./a>(int3.a href="+code=argc" class="sref">argc./a>, char *.a href="+code=argv" class="sref">argv./a>[])u3 89./a>{u3 90./a> while (1) {u3 91./a> stan c const struct3.a href="+code=ion va" class="sref">ion va./a> .a href="+code=lopts" class="sref">lopts./a>[] = {u3 92./a> { .spaa class="string">"device"./spaal, 1, 0, .spaa class="string">'D'./spaal },u3 93./a> { .spaa class="string">"speed"./spaal, 1, 0, .spaa class="string">'s'./spaal },u3 94./a> { .spaa class="string">"delay"./spaal, 1, 0, .spaa class="string">'d'./spaal },u3 95./a> { .spaa class="string">"bpw"./spaal, 1, 0, .spaa class="string">'b'./spaal },u3 96./a> { .spaa class="string">"loop"./spaal, 0, 0, .spaa class="string">'l'./spaal },u3 97./a> { .spaa class="string">"cpha"./spaal, 0, 0, .spaa class="string">'H'./spaal },u3 98./a> { .spaa class="string">"cpol"./spaal, 0, 0, .spaa class="string">'O'./spaal },u3 99./a> { .spaa class="string">"lsb"./spaal, 0, 0, .spaa class="string">'L'./spaal },u3100./a> { .spaa class="string">"cs-high"./spaal, 0, 0, .spaa class="string">'C'./spaal },u3101./a> { .spaa class="string">"3wire"./spaal, 0, 0, .spaa class="string">'3'./spaal },u3102./a> { .spaa class="string">"no-cs"./spaal, 0, 0, .spaa class="string">'N'./spaal },u3103./a> { .spaa class="string">"ready"./spaal, 0, 0, .spaa class="string">'R'./spaal },u3104./a> { .a href="+code=NULL" class="sref">NULL./a>, 0, 0, 0 },u3105./a> };u3106./a> int3.a href="+code=c" class="sref">c./a>;u3107./a>u3108./a> .a href="+code=c" class="sref">c./a> = .a href="+code=getion_long" class="sref">getion_long./a>(.a href="+code=argc" class="sref">argc./a>, .a href="+code=argv" class="sref">argv./a>, .spaa class="string">"D:s:d:b:lHOLC3NR"./spaal, .a href="+code=lopts" class="sref">lopts./a>, .a href="+code=NULL" class="sref">NULL./a>);u3109./a>u3110./a> if (.a href="+code=c" class="sref">c./a> == -1)u3111./a> break;u3112./a>u3113./a> switch (.a href="+code=c" class="sref">c./a>) {u3114./a> case .spaa class="string">'D'./spaal:u3115./a> .a href="+code=device" class="sref">device./a> = .a href="+code=ionarg" class="sref">ionarg./a>;u3116./a> break;u3117./a> case .spaa class="string">'s'./spaal:u3118./a> .a href="+code=speed" class="sref">speed./a> = .a href="+code=atoi" class="sref">atoi./a>(.a href="+code=ionarg" class="sref">ionarg./a>);u3119./a> break;u3120./a> case .spaa class="string">'d'./spaal:u3121./a> .a href="+code=delay" class="sref">delay./a> = .a href="+code=atoi" class="sref">atoi./a>(.a href="+code=ionarg" class="sref">ionarg./a>);u3122./a> break;u3123./a> case .spaa class="string">'b'./spaal:u3124./a> .a href="+code=bits" class="sref">bits./a> = .a href="+code=atoi" class="sref">atoi./a>(.a href="+code=ionarg" class="sref">ionarg./a>);u3125./a> break;u3126./a> case .spaa class="string">'l'./spaal:u3127./a> .a href="+code=mode" class="sref">mode./a> |= .a href="+code=SPI_LOOP" class="sref">SPI_LOOP./a>;u3128./a> break;u3129./a> case .spaa class="string">'H'./spaal:u3130./a> .a href="+code=mode" class="sref">mode./a> |= .a href="+code=SPI_CPHA" class="sref">SPI_CPHA./a>;u3131./a> break;u3132./a> case .spaa class="string">'O'./spaal:u3133./a> .a href="+code=mode" class="sref">mode./a> |= .a href="+code=SPI_CPOL" class="sref">SPI_CPOL./a>;u3134./a> break;u3135./a> case .spaa class="string">'L'./spaal:u3136./a> .a href="+code=mode" class="sref">mode./a> |= .a href="+code=SPI_LSB_FIRST" class="sref">SPI_LSB_FIRST./a>;u3137./a> break;u3138./a> case .spaa class="string">'C'./spaal:u3139./a> .a href="+code=mode" class="sref">mode./a> |= .a href="+code=SPI_CS_HIGH" class="sref">SPI_CS_HIGH./a>;u3140./a> break;u3141./a> case .spaa class="string">'3'./spaal:u3142./a> .a href="+code=mode" class="sref">mode./a> |= .a href="+code=SPI_3WIRE" class="sref">SPI_3WIRE./a>;u3143./a> break;u3144./a> case .spaa class="string">'N'./spaal:u3145./a> .a href="+code=mode" class="sref">mode./a> |= .a href="+code=SPI_NO_CS" class="sref">SPI_NO_CS./a>;u3146./a> break;u3147./a> case .spaa class="string">'R'./spaal:u3148./a> .a href="+code=mode" class="sref">mode./a> |= .a href="+code=SPI_READY" class="sref">SPI_READY./a>;u3149./a> break;u3150./a> default:u3151./a> .a href="+code=print_usage" class="sref">print_usage./a>(.a href="+code=argv" class="sref">argv./a>[0]);u3152./a> break;u3153./a> }u3154./a> }u3155./a>}u3156./a>u3157./a>int3.a href="+code=maia" class="sref">maia./a>(int3.a href="+code=argc" class="sref">argc./a>, char *.a href="+code=argv" class="sref">argv./a>[])u3158./a>{u3159./a> int3.a href="+code=ret" class="sref">ret./a> = 0;u3160./a> int3.a href="+code=fd" class="sref">fd./a>;u3161./a>u3162./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>);u3163./a>u3164./a> .a href="+code=fd" class="sref">fd./a> = .a href="+code=ioen" class="sref">ioen./a>(.a href="+code=device" class="sref">device./a>, .a href="+code=O_RDWR" class="sref">O_RDWR./a>);u3165./a> if (.a href="+code=fd" class="sref">fd./a> < 0)u3166./a> .a href="+code=pabort" class="sref">pabort./a>(.spaa class="string">"can't ioen device"./spaal);u3167./a>u3168./a> .spaa class="comment"l/*./spaalu3169./a>.spaa class="comment"l * spi mode./spaalu317 va>.spaa class="comment"l */./spaalu3171./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>);u3172./a> if (.a href="+code=ret" class="sref">ret./a> == -1)u3173./a> .a href="+code=pabort" class="sref">pabort./a>(.spaa class="string">"can't set spi mode"./spaal);u3174./a>u3175./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>);u3176./a> if (.a href="+code=ret" class="sref">ret./a> == -1)u3177./a> .a href="+code=pabort" class="sref">pabort./a>(.spaa class="string">"can't get spi mode"./spaal);u3178./a>u3179./a> .spaa class="comment"l/*./spaalu318 va>.spaa class="comment"l * bits per word./spaalu3181./a>.spaa class="comment"l */./spaalu3182./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>);u3183./a> if (.a href="+code=ret" class="sref">ret./a> == -1)u3184./a> .a href="+code=pabort" class="sref">pabort./a>(.spaa class="string">"can't set bits per word"./spaal);u3185./a>u3186./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>);u3187./a> if (.a href="+code=ret" class="sref">ret./a> == -1)u3188./a> .a href="+code=pabort" class="sref">pabort./a>(.spaa class="string">"can't get bits per word"./spaal);u3189./a>u3190./a> .spaa class="comment"l/*./spaalu3191./a>.spaa class="comment"l * max speed hz./spaalu3192./a>.spaa class="comment"l */./spaalu3193./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>);u3194./a> if (.a href="+code=ret" class="sref">ret./a> == -1)u3195./a> .a href="+code=pabort" class="sref">pabort./a>(.spaa class="string">"can't set max speed hz"./spaal);u3196./a>u3197./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>);u3198./a> if (.a href="+code=ret" class="sref">ret./a> == -1)u3199./a> .a href="+code=pabort" class="sref">pabort./a>(.spaa class="string">"can't get max speed hz"./spaal);u3200./a>u3201./a> .a href="+code=printf" class="sref">printf./a>(.spaa class="string">"spi mode: %d\n"./spaal, .a href="+code=mode" class="sref">mode./a>);u3202./a> .a href="+code=printf" class="sref">printf./a>(.spaa class="string">"bits per word: %d\n"./spaal, .a href="+code=bits" class="sref">bits./a>);u3203./a> .a href="+code=printf" class="sref">printf./a>(.spaa class="string">"max speed: %d Hz (%d KHz)\n"./spaal, .a href="+code=speed" class="sref">speed./a>, .a href="+code=speed" class="sref">speed./a>/1000);u3204./a>u3205./a> .a href="+code=transfer" class="sref">transfer./a>(.a href="+code=fd" class="sref">fd./a>);u3206./a>u3207./a> .a href="+code=close" class="sref">close./a>(.a href="+code=fd" class="sref">fd./a>);u3208./a>u3209./a> return3.a href="+code=ret" class="sref">ret./a>;u3210./a>}u3211./a>
lxr.linux.no kindly hosted by .a href="http://www.redpill-linpro.no">Redpill Linpro AS./a>, provider of Linux consulting and ioeran vas services since31995.