LINUX2.6内核下交叉编译成功的sin(x)测试函数

网上参考资料:

[root@linux ~]# vi sin.c

#include

int main(void)

{

        float value;

        value = sin ( 3.14 / 2 );

        printf("%f/n",value);

}

# 这个文件的内容可以在下面取得。

# http://linux.vbird.org/linux_basic/0520source/sin.c

我们先直接进行编译:

[root@linux ~]# gcc sin.c

sin.c: In function 'main':

sin.c:5: warning: incompatible implicit declaration of built-in function 'sin'

/tmp/cciDlilg.o(.text+0x2c): In function `main':

sin.c: undefined reference to `sin'

collect2: ld returned 1 exit status

# 注意上面最后一行,有个错误消息,表示没有成功。

特别注意上面的错误消息,说明没有编译成功,意指“undefined reference to sin”,表示“没有sin的相关定义参考值”,这是因为C语言里的sin函数是写在libm.so函数库中,而我们并没有在源码里加入相关的说明,所以需要在编译与连接的时候,将这个函数库连接进执行文件。我们可以这样做:

[root@linux ~]# gcc sin.c -lm -L/lib -L/usr/lib

# 特别注意, -lm可以拆成两部分来看:

# -l :表示“加入某个函数库(library)”,

# m  :表示libm.so函数库,其中,lib与扩展名(.a .so)不需要写

# 所以 -lm表示使用libm.so(或libm.a)函数库的意思。

#  -L后面接的路径。上述内容表示:

#我要的函数库libm.so请到 /lib /usr/lib中搜索”。

[root@linux ~]# ./a.out

1.000000

要注意,由于Linux默认是将函数库放在 /lib /usr/lib中,所以不写 -L/lib -L/usr/lib也没有关系。不过,如果使用的函数库并非放在这两个目录下,那么 -L/path就很重要了,否则会找不到函数库。

除了连接的函数库之外,还要注意sin.c中的第1行“#include ”,这行说的是要由stdio.h文件读入一些定义数据,这包括printf的相关设置。这个文件其实是放在 /usr/include/ stdio.h中的。如果这个文件并非放在这里,我们就可以使用下面的方式来定义要读取的包含文件放置的目录:

[root@linux ~]# gcc sin.c -lm -I/usr/include

-I/path后面的路径(path)就是相关包含文件的目录。同样的,默认值是放在 /usr/include下面,除非包含文件放在其他路径,否则也可以略过它。

 

该文件调用双口RAM的驱动程序,输出12路模拟信号:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_DRAM                "/dev/dram_driver"

#define pi 3.1415926
#define U  1655
#define IOWRITE                1
#define IOCLEAR                2

unsigned short sincode60[]={
2048,2064,2081,2097,2114,2131,2147,2164,2180,2197,
2214,2230,2247,2263,2280,2296,2313,2329,2345,2362,
2378,2394,2411,2427,2443,2459,2475,2491,2507,2523,
2539,2555,2571,2587,2602,2618,2633,2649,2664,2680,
2695,2711,2726,2741,2756,2771,2786,2801,2815,2830,
2845,2859,2874,2888,2902,2917,2931,2945,2959,2973,
2986,3000,3014,3027,3040,3054,3067,3080,3093,3106,
3118,3131,3144,3156,3168,3180,3192,3204,3216,3228,
3240,3251,3262,3274,3285,3296,3307,3317,3328,3338,
3349,3359,3369,3379,3389,3399,3408,3417,3427,3436,
3445,3454,3462,3471,3479,3488,3496,3504,3512,3519,
3527,3534,3541,3549,3555,3562,3569,3575,3582,3588,
3594,3600,3605,3611,3616,3621,3627,3631,3636,3641,
3645,3649,3654,3657,3661,3665,3668,3672,3675,3678,
3680,3683,3686,3688,3690,3692,3694,3695,3697,3698,
3699,3700,3701,3702,3702,3702,3702,3702,3702,3702,
3701,3701,3700,3699,3697,3696,3695,3693,3691,3689,
3687,3684,3682,3679,3676,3673,3670,3667,3663,3659,
3656,3652,3647,3643,3639,3634,3629,3624,3619,3614,
3608,3603,3597,3591,3585,3579,3572,3566,3559,3552,
3545,3538,3531,3523,3515,3508,3500,3492,3484,3475,
3467,3458,3449,3440,3431,3422,3413,3403,3394,3384,
3374,3364,3354,3344,3333,3323,3312,3301,3290,3279,
3268,3257,3245,3234,3222,3210,3198,3186,3174,3162,
3150,3137,3125,3112,3099,3086,3073,3060,3047,3034,
3020,3007,2993,2979,2966,2952,2938,2924,2910,2895,
2881,2867,2852,2838,2823,2808,2793,2778,2763,2748,
2733,2718,2703,2688,2672,2657,2641,2626,2610,2594,
2579,2563,2547,2531,2515,2499,2483,2467,2451,2435,
2419,2402,2386,2370,2354,2337,2321,2304,2288,2271,
2255,2238,2222,2205,2189,2172,2156,2139,2122,2106,
2089,2072,2056,2039,2023,2006,1989,1973,1956,1939,
1923,1906,1890,1873,1857,1840,1824,1807,1791,1774,
1758,1741,1725,1709,1693,1676,1660,1644,1628,1612,
1596,1580,1564,1548,1532,1516,1501,1485,1469,1454,
1438,1423,1407,1392,1377,1362,1347,1332,1317,1302,
1287,1272,1257,1243,1228,1214,1200,1185,1171,1157,
1143,1129,1116,1102,1088,1075,1061,1048,1035,1022,
1009,996,983,970,958,945,933,921,909,897,
885,873,861,850,838,827,816,805,794,783,
772,762,751,741,731,721,711,701,692,682,
673,664,655,646,637,628,620,611,603,595,
587,580,572,564,557,550,543,536,529,523,
516,510,504,498,492,487,481,476,471,466,
461,456,452,448,443,439,436,432,428,425,
422,419,416,413,411,408,406,404,402,400,
399,398,396,395,394,394,393,393,393,393,
393,393,393,394,395,396,397,398,400,401,
403,405,407,409,412,415,417,420,423,427,
430,434,438,441,446,450,454,459,464,468,
474,479,484,490,495,501,507,513,520,526,
533,540,546,554,561,568,576,583,591,599,
607,616,624,633,641,650,659,668,678,687,
696,706,716,726,736,746,757,767,778,788,
799,810,821,833,844,855,867,879,891,903,
915,927,939,951,964,977,989,1002,1015,1028,
1041,1055,1068,1081,1095,1109,1122,1136,1150,1164,
1178,1193,1207,1221,1236,1250,1265,1280,1294,1309,
1324,1339,1354,1369,1384,1400,1415,1431,1446,1462,
1477,1493,1508,1524,1540,1556,1572,1588,1604,1620,
1636,1652,1668,1684,1701,1717,1733,1750,1766,1782,
1799,1815,1832,1848,1865,1881,1898,1915,1931,1948,
1964,1981,1998,2014,2031
};
unsigned short sincode50[]={
614,606,598,590,582,574,567,560,552,545,
538,532,525,519,512,506,500,494,489,483,
478,473,468,463,458,453,449,445,441,437,
433,430,426,423,420,417,414,411,409,407,
405,403,401,399,398,397,396,395,394,393,
393,393,393,393,393,393,394,395,395,396,
398,399,401,402,404,406,409,411,414,416,
419,422,426,429,433,436,440,444,448,453,
457,462,467,472,477,482,488,493,499,505,
511,518,524,531,537,544,551,558,566,573,
581,589,597,605,613,621,630,638,647,656,
665,674,684,693,703,713,723,733,743,753,
764,774,785,796,807,818,829,840,852,863,
875,887,899,911,923,935,947,960,972,985,
998,1011,1024,1037,1050,1064,1077,1090,1104,1118,
1132,1146,1160,1174,1188,1202,1216,1231,1245,1260,
1275,1289,1304,1319,1334,1349,1364,1379,1395,1410,
1425,1441,1456,1472,1488,1503,1519,1535,1551,1566,
1582,1598,1614,1631,1647,1663,1679,1695,1712,1728,
1744,1761,1777,1793,1810,1826,1843,1859,1876,1892,
1909,1926,1942,1959,1975,1992,2009,2025,2042,2059,
2075,2092,2108,2125,2142,2158,2175,2192,2208,2225,
2241,2258,2274,2291,2307,2324,2340,2356,2373,2389,
2405,2421,2438,2454,2470,2486,2502,2518,2534,2550,
2566,2581,2597,2613,2628,2644,2659,2675,2690,2705,
2721,2736,2751,2766,2781,2796,2811,2825,2840,2855,
2869,2883,2898,2912,2926,2940,2954,2968,2982,2995,
3009,3023,3036,3049,3062,3075,3088,3101,3114,3127,
3139,3152,3164,3176,3188,3200,3212,3224,3236,3247,
3259,3270,3281,3292,3303,3314,3324,3335,3345,3356,
3366,3376,3386,3395,3405,3414,3424,3433,3442,3451,
3460,3468,3477,3485,3493,3501,3509,3517,3524,3532,
3539,3546,3553,3560,3567,3573,3580,3586,3592,3598,
3603,3609,3615,3620,3625,3630,3635,3639,3644,3648,
3652,3656,3660,3664,3667,3671,3674,3677,3680,3682,
3685,3687,3689,3691,3693,3695,3696,3698,3699,3700,
3701,3701,3702,3702,3702,3702,3702,3702,3702,3701,
3700,3699,3698,3697,3695,3693,3692,3690,3687,3685,
3683,3680,3677,3674,3671,3668,3664,3661,3657,3653,
3649,3644,3640,3635,3631,3626,3621,3615,3610,3604,
3599,3593,3587,3581,3574,3568,3561,3554,3547,3540,
3533,3526,3518,3510,3502,3494,3486,3478,3470,3461,
3452,3443,3434,3425,3416,3407,3397,3387,3377,3367,
3357,3347,3337,3326,3316,3305,3294,3283,3272,3261,
3249,3238,3226,3214,3202,3190,3178,3166,3154,3141,
3129,3116,3104,3091,3078,3065,3051,3038,3025,3011,
2998,2984,2970,2956,2942,2928,2914,2900,2886,2871,
2857,2842,2828,2813,2798,2783,2768,2753,2738,2723,
2708,2693,2677,2662,2646,2631,2615,2600,2584,2568,
2552,2536,2521,2505,2489,2472,2456,2440,2424,2408,
2392,2375,2359,2343,2326,2310,2293,2277,2260,2244,
2227,2211,2194,2178,2161,2144,2128,2111,2095,2078,
2061,2045,2028,2011,1995,1978,1962,1945,1928,1912,
1895,1879,1862,1846,1829,1813,1796,1780,1763,1747,
1731,1714,1698,1682,1666,1649,1633,1617,1601,1585,
1569,1553,1537,1522,1506,1490,1475,1459,1443,1428,
1413,1397,1382,1367,1352,1337,1322,1307,1292,1277,
1262,1248,1233,1219,1204,1190,1176,1162,1148,1134,
1120,1106,1093,1079,1066,1052,1039,1026,1013,1000,
987,975,962,949,937,925,913,901,889,877,
865,853,842,831,819,808,797,787,776,765,
755,744,734,724,714,705,695,685,676,667,
658,649,640,631,623
};
unsigned short sincode40[]={
3481,3472,3464,3455,3446,3437,3428,3419,3410,3400,
3390,3381,3371,3361,3351,3340,3330,3319,3308,3298,
3287,3276,3264,3253,3242,3230,3218,3206,3194,3182,
3170,3158,3146,3133,3120,3108,3095,3082,3069,3056,
3043,3029,3016,3002,2989,2975,2961,2947,2933,2919,
2905,2891,2876,2862,2847,2833,2818,2803,2788,2773,
2758,2743,2728,2713,2698,2682,2667,2652,2636,2620,
2605,2589,2573,2558,2542,2526,2510,2494,2478,2462,
2446,2429,2413,2397,2381,2364,2348,2332,2315,2299,
2282,2266,2249,2233,2216,2200,2183,2167,2150,2133,
2117,2100,2084,2067,2050,2034,2017,2000,1984,1967,
1951,1934,1917,1901,1884,1868,1851,1835,1818,1802,
1785,1769,1752,1736,1720,1703,1687,1671,1655,1639,
1623,1606,1590,1574,1559,1543,1527,1511,1495,1480,
1464,1449,1433,1418,1402,1387,1372,1357,1342,1327,
1312,1297,1282,1267,1253,1238,1224,1209,1195,1181,
1167,1153,1139,1125,1111,1097,1084,1070,1057,1044,
1030,1017,1004,991,979,966,954,941,929,917,
905,893,881,869,857,846,834,823,812,801,
790,779,769,758,748,738,728,718,708,698,
688,679,670,661,652,643,634,625,617,609,
601,593,585,577,569,562,555,548,541,534,
527,521,514,508,502,496,491,485,480,474,
469,464,460,455,451,446,442,438,434,431,
427,424,421,418,415,412,410,408,405,403,
402,400,398,397,396,395,394,393,393,393,
393,393,393,393,394,394,395,396,397,399,
400,402,404,406,408,410,413,415,418,421,
424,428,431,435,439,443,447,451,456,460,
465,470,475,480,486,492,497,503,509,515,
522,528,535,542,549,556,563,571,578,586,
594,602,610,618,627,635,644,653,662,671,
681,690,700,709,719,729,739,750,760,771,
781,792,803,814,825,836,848,859,871,883,
895,907,919,931,943,956,968,981,994,1007,
1020,1033,1046,1059,1072,1086,1100,1113,1127,1141,
1155,1169,1183,1197,1212,1226,1240,1255,1270,1284,
1299,1314,1329,1344,1359,1374,1390,1405,1420,1436,
1451,1467,1482,1498,1514,1529,1545,1561,1577,1593,
1609,1625,1641,1657,1674,1690,1706,1722,1739,1755,
1771,1788,1804,1821,1837,1854,1870,1887,1903,1920,
1937,1953,1970,1987,2003,2020,2036,2053,2070,2086,
2103,2120,2136,2153,2169,2186,2203,2219,2236,2252,
2269,2285,2302,2318,2334,2351,2367,2383,2400,2416,
2432,2448,2464,2481,2497,2513,2529,2544,2560,2576,
2592,2607,2623,2639,2654,2670,2685,2700,2716,2731,
2746,2761,2776,2791,2806,2820,2835,2850,2864,2879,
2893,2907,2921,2935,2949,2963,2977,2991,3005,3018,
3031,3045,3058,3071,3084,3097,3110,3123,3135,3148,
3160,3172,3184,3196,3208,3220,3232,3243,3255,3266,
3277,3288,3299,3310,3321,3331,3342,3352,3362,3372,
3382,3392,3402,3411,3421,3430,3439,3448,3457,3465,
3474,3482,3490,3498,3506,3514,3522,3529,3537,3544,
3551,3558,3564,3571,3577,3584,3590,3596,3602,3607,
3613,3618,3623,3628,3633,3638,3642,3647,3651,3655,
3659,3662,3666,3669,3673,3676,3679,3681,3684,3686,
3689,3691,3693,3694,3696,3697,3699,3700,3700,3701,
3702,3702,3702,3702,3702,3702,3702,3701,3700,3699,
3698,3697,3696,3694,3692,3690,3688,3686,3684,3681,
3678,3675,3672,3669,3665,3662,3658,3654,3650,3646,
3642,3637,3632,3627,3622,3617,3612,3606,3601,3595,
3589,3583,3576,3570,3563,3557,3550,3543,3535,3528,
3521,3513,3505,3497,3489
};

unsigned short node=sizeof(sincode50)/sizeof(unsigned short);
#define MAXLEN    1000
#define data_len  12*MAXLEN
unsigned short data[data_len];
unsigned short data1[data_len];
int main()
{
        int fd;
        int val=-1,i=0;
 unsigned short *pt=NULL;
      
        if( (fd=open(DEVICE_DRAM,O_RDWR)) < 0 )
        {
                perror("can not open device");
                exit(1);
        }
 printf("open device success!DA point is %d/n",node);
 
       for(i=0;i {
  data[i]=0;
  data1[i]=0;
 }
 pt=data1;
 *pt++=node; //DA points
 for(i=0;i  *pt++=sincode60[i];
  *pt++=sincode50[i];
  *pt++=sincode40[i];

  *pt++=sincode60[i];
  *pt++=sincode50[i];
  *pt++=sincode40[i];

  *pt++=sincode60[i];
  *pt++=sincode50[i];
  *pt++=sincode40[i];

  *pt++=sincode60[i];
  *pt++=sincode50[i];
  *pt++=sincode40[i];

 }

        while(1)
        {
             printf("0:quit,1:half,2:full,3:standor 50HZ output,4:input DA points,5:input voltage/n");
                scanf("%d",&val);
              
                if(val==1)
                        ioctl(fd,1,0);
                else if(val==2)
                        ioctl(fd,2,0);
  else if(val==3)
                        write(fd,data1,12*node);
  else if(val==4)
                        {
    int n=0,i=0;
    unsigned int ya=0,yb=0,yc=0;
    printf("pleas input DA points(1-680) : ");
    scanf("%d",&n);
    if(n<0||n>680) exit(1);
    pt=data;
    *pt++=n; //DA points
    for(i=0;i    {
     ya=sin(i*2*pi/n)*U+2048; //a
     yb=sin(i*2*pi/n-2*pi/3)*U+2048; //b
     yc=sin(i*2*pi/n+2*pi/3)*U+2048;  //c
     *pt++=ya;
     *pt++=yb;
     *pt++=yc;

     *pt++=ya;
     *pt++=yb;
     *pt++=yc;

     *pt++=ya;
     *pt++=yb;
     *pt++=yc;

     *pt++=ya;
     *pt++=yb;
     *pt++=yc;
    }
    write(fd,data,12*n);

   }
  else if(val==5)
                        {
    int i=0,u=0,n=0;
    unsigned int ya=0,yb=0,yc=0;
    printf("pleas input DA votage(0-2048) : ");
    scanf("%d",&u);
    if(n<0||u>2048) exit(1);
    n=node;
    pt=data;
    *pt++=n; //DA points
    for(i=0;i    {
     ya=sin(i*2*pi/n)*u+2048; //a
     yb=sin(i*2*pi/n-2*pi/3)*u+2048; //b
     yc=sin(i*2*pi/n+2*pi/3)*u+2048;  //c
     *pt++=ya;
     *pt++=yb;
     *pt++=yc;

     *pt++=ya;
     *pt++=yb;
     *pt++=yc;

     *pt++=ya;
     *pt++=yb;
     *pt++=yc;

     *pt++=ya;
     *pt++=yb;
     *pt++=yc;
    }
    write(fd,data,12*n);

   }
                else if(val==0)
                  {
                        close(fd);
                        exit(1);
                }  
        }
}

编译之:

/opt/crosstool/arm-unknown-linux-gnueabi/arm-unknown-linux-gnueabi/bin/gcc  /home/wfg/driver/dram/dram_test.c -lm -o dram_test

将该测试文件和dram_driver.ko驱动程序放到JFFS2文件系统中,重新压缩成rootfs.jffs2文件,其中dram_driver.ko一定要放在/lib/modules/2.6.26.6目录下,否则会报找不到目录或设备文件。

将rootfs.jffs2文件烧写到nandflash中去:

U-Boot> nand erase (NAND FLASH在编程时记得擦除)

NAND erase: device 0 whole chip
Skipping bad block at  0x08480000
Erasing at 0xffe0000 -- 100% complete.
OK
U-Boot> tftp 0x20000000 rootfs.jffs2
TFTP from server 172.21.26.148; our IP address is 172.21.26.1
Filename 'rootfs.jffs2'.
Load address: 0x20000000
Loading: #################################################################
         #################################################################
         #################################################################
         #################################################################
         #################################################################
         #################################################################
         #################################
done
Bytes transferred = 6208668 (5ebc9c hex)
U-Boot> nand write.jffs2 0x20000000 0x400000 5ebc9c

NAND write: device 0 offset 0x400000, size 0x5ebc9c

Writing data at 0x9eb800 -- 100% complete.
 6208668 bytes written: OK
U-Boot>

重新上电,并加载模块,创建设备文件,执行测试程序,如下画面:

root@WK:ls
aImage20008000  dram_test       mnt             tmp
aImage_old      etc             proc            usr
bin             example         root            var
boot            home            sbin
dev             lib             sys
root@WK:insmod dram_driver
root@WK:lsmod
dram_driver 2913 0 - Live 0xbf000000
root@WK:mknod /dev/dram_driver c 221 1
root@WK:ls -l /dev/dram_driver
crw-r--r--    1 root     root     221,   1 Jan  1 00:02 /dev/dram_driver
root@WK:./dram_test
open device success!DA point is 625
0:quit,1:half,2:full,3:standor 50HZ output,4:input DA points,5:input voltage
4
pleas input DA points(1-680) : 625
0:quit,1:half,2:full,3:standor 50HZ output,4:input DA points,5:input voltage

在修改该文件后,可以通过TFTP下载到板子中,重新执行测试程序,如下画面:

tftp -gr dram_test 172.21.26.148

./dram_test

open device success!DA point is 625
0:quit,1:half,2:full,3:standor 50HZ output,4:input DA points,5:input voltage
4
pleas input DA points(1-680) : 625
0:quit,1:half,2:full,3:standor 50HZ output,4:input DA points,5:input voltage

至此,双口RAM驱动程序测试成功!

 

你可能感兴趣的:(LINUX2.6内核下交叉编译成功的sin(x)测试函数)