关闭 uart0 让出给其它外设使用

一、禁止打印

bootable/bootloader/lk/app/mt_boot/mt_boot.c

把里面的cmdline传参的printk.disable_uart修改为printk.disable_uart=1

#ifdef USER_BUILD
         sprintf(cmdline,"%s%s",cmdline," printk.disable_uart=1");
 #else
-        sprintf(cmdline,"%s%s",cmdline," printk.disable_uart=0 ddebug_query=\"file *mediatek* +p ; file *gpu* =_\"");
+        sprintf(cmdline,"%s%s",cmdline," printk.disable_uart=1 ddebug_query=\"file *mediatek* +p ; file *gpu* =_\"");
 #endif

二、禁止android服务

把system/core/rootdir/init.rc的服务给屏蔽


#service console /system/bin/sh
  #  class core
  #  console
    #disabled
   #user shell
    #seclabel u:r:shell:s0

#on property:ro.debuggable=1
#    start console             

三、修改CMDLINE波特率

bootable/bootloader/lk/platform/mt6735/include/platform/mt_reg_base.h

-#define COMMANDLINE_TO_KERNEL  "console=tty0 console=ttyMT3,921600n1 root=/dev/ram vmalloc=496M androidboot.hardware=mt6735 slub_max_order=0 slub_debug=O"
+#define COMMANDLINE_TO_KERNEL  "console=tty0 console=ttyMT3,115200n1 root=/dev/ram vmalloc=496M androidboot.hardware=mt6735 slub_max_order=0 slub_debug=O"

四、修改pl、lk 打印口uart0波特率

pl  (可不修改)

bootable/bootloader/preloader/custom/LN1/cust_bldr.mak

+CFG_LOG_BAUDRATE :=115200
lk (可不修改)

bootable/bootloader/lk/platform/mt6735/uart.c

-#define CONFIG_BAUDRATE                921600
+#define CONFIG_BAUDRATE                115200

五、禁止内核打印信息

xxx_debug_defconfig /xxx_defconfig

-CONFIG_MT_PRINTK_UART_CONSOLE=y
+# CONFIG_MT_PRINTK_UART_CONSOLE is not set


六、测试

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_SRC_FILES:= readRadar.c
LOCAL_MODULE := readRadar
LOCAL_SHARED_LIBRARIES:= libcutils libutils libtinyalsa
LOCAL_MODULE_TAGS := optional
include $(BUILD_EXECUTABLE)

test.c

    #include <sys/types.h>  
      
    #include <sys/stat.h>  
    #include <fcntl.h>  
    #include <termios.h>  
    #include <stdio.h>  
    #define BAUDRATE        921600  
    #define UART_DEVICE     "/dev/ttyMT0"  
      
    #define FALSE  -1  
    #define TRUE   0  
    ////////////////////////////////////////////////////////////////////////////////  
    /** 
    *@brief  设置串口通信速率 
    *@param  fd     类型 int  打开串口的文件句柄 
    *@param  speed  类型 int  串口速度 
    *@return  void 
    */  
    int speed_arr[] = {B921600, B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,  
                       B921600, B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300, };  
    int name_arr[] = {921600, 115200, 38400, 19200, 9600, 4800, 2400, 1200,  300,   
                      921600, 115200, 38400, 19200, 9600, 4800, 2400, 1200,  300, };  
    void set_speed(int fd, int speed){  
      int   i;   
      int   status;   
      struct termios   Opt;  
      tcgetattr(fd, &Opt);   
      for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++) {   
        if  (speed == name_arr[i]) {       
          tcflush(fd, TCIOFLUSH);       
          cfsetispeed(&Opt, speed_arr[i]);    
          cfsetospeed(&Opt, speed_arr[i]);     
          status = tcsetattr(fd, TCSANOW, &Opt);    
          if  (status != 0) {          
            perror("tcsetattr fd1");    
            return;       
          }      
          tcflush(fd,TCIOFLUSH);     
        }    
      }  
    }  
    ////////////////////////////////////////////////////////////////////////////////  
    /** 
    *@brief   设置串口数据位,停止位和效验位 
    *@param  fd     类型  int  打开的串口文件句柄 
    *@param  databits 类型  int 数据位   取值 为 7 或者8 
    *@param  stopbits 类型  int 停止位   取值为 1 或者2 
    *@param  parity  类型  int  效验类型 取值为N,E,O,,S 
    */  
    int set_Parity(int fd,int databits,int stopbits,int parity)  
    {   
        struct termios options;   
        if  ( tcgetattr( fd,&options)  !=  0) {   
            perror("SetupSerial 1");       
            return(FALSE);    
        }  
        options.c_cflag &= ~CSIZE;   
        switch (databits) /*设置数据位数*/  
        {     
        case 7:       
            options.c_cflag |= CS7;   
            break;  
        case 8:       
            options.c_cflag |= CS8;  
            break;     
        default:      
            fprintf(stderr,"Unsupported data size\n"); return (FALSE);    
        }  
        switch (parity)   
        {     
            case 'n':  
            case 'N':      
                options.c_cflag &= ~PARENB;   /* Clear parity enable */  
                options.c_iflag &= ~INPCK;     /* Enable parity checking */   
                break;    
            case 'o':     
            case 'O':       
                options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/    
                options.c_iflag |= INPCK;             /* Disnable parity checking */   
                break;    
            case 'e':    
            case 'E':     
                options.c_cflag |= PARENB;     /* Enable parity */      
                options.c_cflag &= ~PARODD;   /* 转换为偶效验*/       
                options.c_iflag |= INPCK;       /* Disnable parity checking */  
                break;  
            case 'S':   
            case 's':  /*as no parity*/     
                options.c_cflag &= ~PARENB;  
                options.c_cflag &= ~CSTOPB;break;    
            default:     
                fprintf(stderr,"Unsupported parity\n");      
                return (FALSE);    
            }    
        /* 设置停止位*/    
        switch (stopbits)  
        {     
            case 1:      
                options.c_cflag &= ~CSTOPB;    
                break;    
            case 2:      
                options.c_cflag |= CSTOPB;    
               break;  
            default:      
                 fprintf(stderr,"Unsupported stop bits\n");    
                 return (FALSE);   
        }   
        /* Set input parity option */   
        if (parity != 'n')     
            options.c_iflag |= INPCK;   
        tcflush(fd,TCIFLUSH);  
        options.c_cc[VTIME] = 5; /* 设置超时15 seconds*/     
        options.c_cc[VMIN] = 100; /* Update the options and do it NOW */  

        options.c_lflag  &= ~(ICANON | ECHO | ECHOE | ISIG);  /*Input*/  
        options.c_oflag  &= ~OPOST;   /*Output*/  

		options.c_cflag &= ~CRTSCTS;

        if (tcsetattr(fd,TCSANOW,&options) != 0)     
        {   
            perror("SetupSerial 3");     
            return (FALSE);    
        }   
        return (TRUE);    
    }  
    ////////////////////////////////////////////////////////////////////////////////  
    int main(int argc, char *argv[])  
    {  
      
        int    fd, c=0, res;  
      
        char  buf[256];  
      
        printf("Start...\n");  
        fd = open(UART_DEVICE, O_RDWR /*| O_NDELAY*/ | O_NOCTTY);  
      
        if (fd < 0) {  
            perror(UART_DEVICE);  
            exit(1);  
        }  
#if 0

		if(fcntl(fd,F_SETFL,0) < 0){   //阻塞,即使前面在open串口设备时设置的是非阻塞的,这里设为阻塞后,以此为准  
        	printf("fcntl failed\n");     
        }     
        else{     
        	printf("fcntl=%d\n",fcntl(fd,F_SETFL,0));     
        }  
#endif
      
#if 1
        printf("Open...\n");  
        set_speed(fd,BAUDRATE);  
        if (set_Parity(fd,8,1,'N') == FALSE)  {  
            printf("Set Parity Error\n");  
            exit (0);  
        }  
#endif
      
#if 0
        printf("Reading...\n");  
        while(1) {  
			int j;
            res = read(fd, buf, 255);  
			printf("read return %d\n", res);
      
            if(res==0)  
                continue;  
            buf[res]=0;  
      
            //printf("first %X\n", buf[0]);  
			for(j = 0; j < res; j ++)
				printf("%02X", buf[j]);
			printf("\n");
            printf("%s", buf);  
              
            if (buf[0] == 0x0d)  
                printf("\n");  
              
            //if (buf[0] == '@') break;  
        }  
#endif
#if 1

		printf("write\n");
		while(1) {
			buf[0] = 0x01;//0xAA;
			buf[1] = 0x03;//0xAA;
			res = write(fd, buf, 2);
			printf("write %d\n", res);
			sleep(1);
		}
#endif

		while(1);
      
        printf("Close...\n");  
        close(fd);  
      
        return 0;  
    }  



发布了154 篇原创文章 · 获赞 12 · 访问量 52万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览