串口AT命令的困窘

   最近在ARM11上整一个东西,用串口蓝牙模块,接到主板上,通过向蓝牙发送AT命令,读取反馈信息。
1.暂不带操作系统,我已经加入ramdisk文件系统。
2.在文件系统里运行linux应用程序。
这里是串口操作测试程序,与下列的驱动同步:
#include  
#include  
#include     //open(), read(), write()  
#define BUFSIZE 100  
int main(int argc, char* argv[])  
{  
    int fd, ret;  
    char buf[BUFSIZE];  
    char *msg = " ";// "AT\r\n";  
    fd = open("/dev/s3c2410_serial3", O_RDWR);//s3c2410_serial3
    printf("envi serial test program\n");
    //printf("fd:%d\n",fd);
    if(fd < 0)  
    {  
        printf("Error: open /dev/s3c2410_serial3 error!\n");  
        return 1;  
    }  
    ret = write(fd, msg, strlen(msg));  
    //printf("envi 1\n");
    if(ret < 0)  
    {  
        printf("Error: write msg error!\n");   
        return 1;  
    }  
    //printf("envi 2\n");
    while(1)  
    {  
        //printf("while envi \n");
        memset(buf, 0, sizeof(buf));  
        ret = read(fd, buf, BUFSIZE);
        //printf("envi 3\n");
        if(ret < 0)  
        {  
            printf("Error: read device error!\n");   
            return 1;  
        }  
       //printf("envi 4\n");
        if(buf[0] != '\0')  
        {  
            printf("%s\n", buf);   
        }  
    }  
    return 0;   
}
3.我自己也写了个串口驱动。UART3口接蓝牙。代码如下:
#if 1
#include
#include  
#include  
#include
#include           //copy_to_use, copy_from_user  
#include  
#include  
#include                //readl, readb, writel, writeb  
#define BUFSIZE 100  
#define ttyS3_MAJOR 240  
#define ELFIN_UART_BASE        0xEC800000
#define ELFIN_UART3_OFFSET    0x0c00
#define IOBASE      (ELFIN_UART_BASE + ELFIN_UART3_OFFSET)
#define UART_ULCON3 IOBASE  
#define UART_UCON3  (IOBASE + 0x4)  
#define UART_UFCON3 (IOBASE + 0x8)
#define UART_UMCON3 (IOBASE + 0x0C)   
#define UART_UTRSTAT3   (IOBASE + 0x10)
#define UART_UFSTAT3     (IOBASE + 0x18)  
#define UART_UTXH3  (IOBASE + 0x20)  
#define UART_URXH3  (IOBASE + 0x24)  
#define UART_UBRDIV3    (IOBASE + 0x28)  
#define UART_UDIVSLOT3  (IOBASE + 0x2C)
#define UART_UINTP3   (IOBASE + 0x30)
#define UART_UINTSP3  (IOBASE + 0x34)
#define UART_UINTM3   (IOBASE + 0x38)  
#if 0
#define ULCON_OFFSET        0x00
#define UCON_OFFSET        0x04
#define UFCON_OFFSET        0x08
#define UMCON_OFFSET        0x0C
#define UTRSTAT_OFFSET        0x10
#define UERSTAT_OFFSET        0x14
#define UFSTAT_OFFSET        0x18
#define UMSTAT_OFFSET        0x1C
#define UTXH_OFFSET        0x20
#define URXH_OFFSET        0x24
#define UBRDIV_OFFSET        0x28
#define UDIVSLOT_OFFSET        0x2C
#define UINTP_OFFSET        0x30
#define UINTSP_OFFSET        0x34
#define UINTM_OFFSET        0x38
#endif


/*void writel(char value,long reg)   {
    *(volatile unsigned char *)(reg) = value;
}
char read(long reg) (
          return *(volatile unsigned char *)(ELFIN_UART_CONSOLE_BASE3 + UTXH_OFFSET);
}*/
int envi_open(struct inode *inode, struct file *filp)  
{  
    
    /* 8N1 */  
    writel(3, UART_ULCON3);  
    /* poll mode */  
    writel(0x2c5, UART_UCON3);  
    writel(0x47, UART_UFCON3);
    writel(0x00, UART_UMCON3);
    /* 9600 */  
    writel(0x15b, UART_UBRDIV3);
    writel(0x0000, UART_UDIVSLOT3);  
    return 0;  
}  
ssize_t envi_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)  
{  
    char wbuf[BUFSIZE]={0};  
    int state;  
    int i = 0,temp;  
    copy_from_user(wbuf, buf, count);  
    while(wbuf[i] != '\0')  
    {  
         while(readl(UART_UFSTAT3) & (1<<14));
         state = readl(UART_UTRSTAT3);  
         if((0x02 & state) == 2)
        {  
            writeb(wbuf[i], UART_UTXH3);  
            printk("Rx data:%c\n",UART_UTXH3);
            i++;  
        }  
    }  
    for(temp = 0; temp < 600000 ; temp++){

        if(readl(UART_UFSTAT3) & (1<<14)){
         continue;    
    }
    else
        break;
    if(temp == 600000){

                printk("transmit error\n");    
        }
    }
    return 0;  
   
}  

//EXPORT_SYMBOL_GPL(uart3_write);

ssize_t envi_read(struct file *filp, char __user *buf, size_t count, loff_t *f_ops)  
{  
    /*char rbuf[1] = {0};  
    int state;  
    state = readl(UART_UTRSTAT3);  
    if((0x01 & state) == 1)  
    {  
        rbuf[0] = readb(UART_URXH3);  
        copy_to_user(buf, rbuf, 1);  
    }  
    else  
    {  
        set_current_state(TASK_INTERRUPTIBLE);  
        schedule_timeout(10);  
    }  
    return 0;  */


   //envi rewrite
   char rbuf[64]={0};
   int UFSTAT_REG,i;
   char FifoCount, UFSTAT3, RxDataReady,RxChar,UINTPReg,UINTMReg;
   UFSTAT_REG = readl(UART_UFSTAT3);
   FifoCount = UFSTAT_REG & 0x3f;
   UFSTAT3 = (UFSTAT_REG & (1<<6))&0xff;
   if(UFSTAT3 || FifoCount > 0)
           RxDataReady = 1;
   else
    RxDataReady = 0;

   while(RxDataReady){
           RxChar = readb(UART_URXH3);
                rbuf[i] = RxChar;
                i++;
                printk("Rx data:%c\n",RxChar);
                UFSTAT_REG = readl(UART_UFSTAT3);
           FifoCount = UFSTAT_REG & 0x3f;
           UFSTAT3 = UFSTAT_REG & (1<<6);
           if(UFSTAT3 || FifoCount > 0)
                   RxDataReady = 1;
           else
            RxDataReady = 0;
   }
   copy_to_user(buf, rbuf, i);
   printk("i:%d\n",i);
   writel((readl(UART_UINTSP3)|0x01),UART_UINTSP3);
   UINTPReg = readb(UART_UINTP3);
   UINTMReg = readb(UART_UINTM3);
   if((UINTPReg & 0x01)||(UINTPReg & 0x02)){
    printk("do not clear int mask\n");
   }
   else{
         UINTMReg |= 0x01;
   }
                        
     UINTMReg &= ~0x01;    
     return 0;
}  


//EXPORT_SYMBOL_GPL(uart3_read);

int envi_release(struct inode *inode, struct file *filp)  
{  
    return 0;  
}  
struct file_operations ttyS3_fops =   
{  
    .owner = THIS_MODULE,  
    .open = envi_open,  
    .write = envi_write,  
    .read = envi_read,  
    .release = envi_release,  
};  
static __init int envi_init(void){
 
    int rc;  
    printk("s5p6450 serial module loaded!envi-->\n");   
    rc = register_chrdev(ttyS3_MAJOR, "s3c2410_serial3", &ttyS3_fops); //s3c2410_serial3
    printk("envi --> rc:%d",rc);
    if(rc < 0)  
    {  
        printk("Error: register s3c2410_serial3 device error!\n") ;  
        return -1;  
    }  
    return 0;   
}  

static __exit void envi_exit(void){
 
    unregister_chrdev(ttyS3_MAJOR, "s3c2410_serial3");  
    printk("s5p6450 serial module exit!\n");   
    return;  
}  

module_init(envi_init);  
module_exit(envi_exit);  

MODULE_AUTHOR("envi");  
MODULE_DESCRIPTION("s5p6450 serial driver");  
MODULE_LICENSE("GPL");
#endif

有问题:
1.我的应用程序在ramdisk文件系统不能正常运行。报语法错误。纠结了很久。运行./app 报出下列信息:
modprobe used greatest stack depth: 5608 bytes left                             
./app: line 1: ELF: not found                                              
./app: line 2: L: not found                                                     
./app: line 1: syntax error: word unexpected (expecting ")")   
各位大虾,有什么好的建议吗?困惑ing.

你可能感兴趣的:(linux,module,struct,user,file,c,linux)