【MediaTek X20开发板试用体验】3.MTX20开发板串口测试方法,adb+wifi,添加新的驱动和应用程序验证。  

分享到:

xble
 xble
Level 1
注册时间:8年 前
帖子数: 15
2017年1月20日 上午11:07  
嵌入式开发调试方法很重要,我们先来看看MTX20板子的调试方式。
一)串口log,本来以为96boards的板子应该是一样,可实际结果不一样(MTX20使用android系统,另外一块板子使用linux系统,难道是这个原因?),注意USB转TTL线是1.8V
 
PC端串口调试助手设置如下,可选择的最大波特率,比较特殊。说明一下此时串口输出的log比较多,还没有完全搞清楚输出机制,不一定每次开机都可以有串口log输出原因未知。
 
二)adb+wifi调试
adb+usb方式很简单,注意MTX20开发板USB Device和HOST模式切换。
adb+wifi设置步骤:
1.adb+usb输入情况下:
adb shell
getprop service.adb.tcp.port 5555
setprop service.adb.tcp.port 5555
stop adbd 
(此时adb连接自动断开)
2.不断电,将usb切换到host模式
设置wifi静态ip地址: 192.168.9.233
3.设置setting-》Developer options-》USB debugging开启
4.adb+wifi连接:
adb connect 192.168.9.233:5555

adb shell可以正常连接开发板。
三)添加新驱动和应用测试
1.驱动源代码和位置

  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/device.h>
  4. #include <linux/platform_device.h>
  5. #include <linux/sysfs.h>
  6. #include <linux/slab.h> 
  7. #include <linux/kernel.h>
  8. #include <linux/kobject.h>
  9. #include <linux/string.h>
  10. #include <linux/of.h>
  11. #include <linux/cdev.h>  
  12. #include <linux/fs.h>  
  13. #include <linux/vmalloc.h>
  14. struct xble_device_data {
  15.         struct block_device *binding;
  16.         int inuse;
  17. };
  18. #define mtx20_unflatten_dts_TIP "mtx20_unflatten_dts_tip"
  19. #define DEV_NAME        "ds1302_cdev"  
  20. char mtx20_unflatten_dts_code[64] = "mtx20_unflatten_dts, mtx20_unflatten_dts_show";
  21. int     ds1302_major = 217;  
  22. dev_t   ds1302_devno;  
  23. struct ds1302_cdev_t {  
  24.     struct cdev cdev;  
  25. } ds1302_dev;  
  26. static struct class *xble_class;
  27. static struct xble_device_data *xble_devices;
  28. static DEFINE_MUTEX(xble_mutex);
  29. static int max_xble_minors = 2;
  30. module_param(max_xble_minors, int, 0);
  31. MODULE_PARM_DESC(max_xble_minors, "Maximum number of xble devices (1-65536)");
  32. int g_nNum = 99;
  33. int ds1302_open(struct inode *inode, struct file *filep)  
  34.         int nNum = 99;
  35.         pr_warn("20170116 open nNum=%d g_nNum=%d\n", nNum, g_nNum);
  36.         printk( "20170116 open =11= g_nNum=%d\n", g_nNum);
  37.         return 0;  
  38. }  
  39.   
  40. static const struct file_operations ds1302_fops = {  
  41.     .owner = THIS_MODULE,  
  42.     .open = ds1302_open,  
  43. };  
  44. static ssize_t show(struct device_driver *driver, char *buf)
  45. {
  46.         if(NULL != buf)
  47.         {
  48.                 /* Newline is not appended on purpose, for convenience of reader programs */
  49.                 snprintf(buf, PAGE_SIZE, "%s\n", mtx20_unflatten_dts_code);
  50.                 return strlen(buf);
  51.         }
  52.         return 0;
  53. }
  54. DRIVER_ATTR(mtx20_unflatten_dts, 0444, show, NULL);
  55. static struct attribute *mtx20_unflatten_dts_attrs[] = {
  56.         &driver_attr_mtx20_unflatten_dts.attr,
  57.         NULL,
  58. };
  59. static struct attribute_group mtx20_unflatten_dts_group = {
  60.         .name ="mtx20_unflatten_dts",
  61.         .attrs = mtx20_unflatten_dts_attrs,
  62. }; 
  63. static const struct attribute_group *mtx20_unflatten_dts_groups[] = {
  64.         &mtx20_unflatten_dts_group,
  65.         NULL,
  66. };
  67. static char *xble_devnode(struct device *dev, umode_t *mode)
  68. {
  69.         return kasprintf(GFP_KERNEL, "xble/%s", dev_name(dev));
  70. }
  71. static int mtx20_unflatten_dts_probe(struct platform_device *pdev)
  72. {
  73.         int nNum;
  74.         int         ret;  
  75.         const char *m_string;
  76.         printk("-0000-mtx20_unflatten_dts_probe\n");
  77.         if(NULL == pdev)
  78.         {
  79.                 printk("xiaomi_test: mtx20_unflatten_dts_probe failed, pdev is NULL\n");
  80.                 return 0;
  81.         }
  82.         pr_warn("mtx20_unflatten_dts_probe 0004\n");
  83.         if(NULL == pdev->dev.of_node)
  84.         {
  85.                 printk( "xiaomi_test: mtx20_unflatten_dts_probe failed, of_node is NULL\n");
  86.                 return 0;
  87.         }
  88.         /* mtx20_unflatten_dts_TIP*/
  89.         if(of_property_read_bool(pdev->dev.of_node, mtx20_unflatten_dts_TIP))
  90.         {
  91.                 printk( "mtx20_unflatten_dts_probe: dtsi<%s> is existing\n", mtx20_unflatten_dts_TIP);
  92.         }
  93.         if (of_property_read_u32(pdev->dev.of_node, "linux,code", &nNum))
  94.         {
  95.                 printk(KERN_INFO "=11= nNum=%d\n", nNum);
  96.         }
  97.         else
  98.         {
  99.                 printk(KERN_INFO "=0101= nNum=%d\n", nNum);
  100.         }
  101.         g_nNum = nNum;
  102.         if (of_property_read_string(pdev->dev.of_node, "linux,string", &m_string))
  103.         {
  104.                 printk(KERN_INFO "=11= m_string=%s\n", m_string);
  105.         }
  106.         else
  107.         {
  108.                 printk(KERN_INFO "=12= m_string=%s\n", m_string);
  109.         }
  110.                 printk(KERN_INFO "=0102= nNum=%d\n", nNum);
  111.     if (ds1302_major) {  
  112.         ds1302_devno = MKDEV(ds1302_major, 0);  
  113.         register_chrdev_region(ds1302_devno, 1, DEV_NAME);  
  114.     } else {  
  115.         alloc_chrdev_region(&ds1302_devno, 0, 1, DEV_NAME);  
  116.     }  
  117.         printk("-00001-mtx20_unflatten_dts_probe max_xble_minors=%d\n", max_xble_minors);
  118.                 printk(KERN_INFO "=0103= nNum=%d\n", nNum);
  119.         xble_devices = vzalloc(sizeof(struct xble_device_data) * max_xble_minors);
  120.         if (!xble_devices) {
  121.                 printk(KERN_ERR "Not enough memory for xble device structures\n");
  122.                 ret = -ENOMEM;
  123.         printk("-000011-mtx20_unflatten_dts_probe max_xble_minors=%d\n", max_xble_minors);
  124.                 goto error;
  125.         } 
  126.                 printk(KERN_INFO "=0104= nNum=%d\n", nNum);
  127.    cdev_init(&ds1302_dev.cdev, &ds1302_fops);  
  128.     ds1302_dev.cdev.owner = THIS_MODULE;  
  129.   
  130.     if ((ret = cdev_add(&ds1302_dev.cdev, ds1302_devno, 1) < 0)) {  
  131.         unregister_chrdev_region(ds1302_devno, 1);  
  132.         goto error;  
  133.     }  
  134.                 printk(KERN_INFO "=0105= nNum=%d\n", nNum);
  135.                 xble_class = class_create(THIS_MODULE, "xble");
  136.         if (IS_ERR(xble_class)) {
  137.                 printk(KERN_ERR "Error creating xble class.\n");
  138.                 cdev_del(&ds1302_dev.cdev);
  139.                 ret = PTR_ERR(xble_class);
  140.                 goto error_region;
  141.         }
  142.                 printk(KERN_INFO "=0106= nNum=%d\n", nNum);
  143.         xble_class->devnode = xble_devnode;
  144.         device_create(xble_class, NULL, MKDEV(ds1302_major, 0), NULL, "xblectl");
  145.                 printk(KERN_INFO "=0107= nNum=%d\n", nNum);
  146. error_region:
  147.         unregister_chrdev_region(ds1302_devno, max_xble_minors);
  148. error:
  149.         vfree(xble_devices);
  150.         return 0;
  151. }
  152. static struct of_device_id mtx20_unflatten_dts_info_match_table[] = {
  153.         { .compatible = "mediatek,mtx20_unflatten_dts",},
  154.         { },
  155. };
  156. static struct platform_driver mtx20_unflatten_dts = {
  157.         .driver = {
  158.                 .name  = "mtx20_unflatten_dts",
  159.                 .owner  = THIS_MODULE,
  160.                 .groups = mtx20_unflatten_dts_groups,
  161.                 .of_match_table = mtx20_unflatten_dts_info_match_table,
  162.         },
  163.         .probe = mtx20_unflatten_dts_probe,
  164.         .remove = NULL,
  165. };
  166. static int __init mtx20_unflatten_dts_init(void)
  167. {
  168.         pr_warn("mtx20_unflatten_dts_init 0004\n");
  169.         return platform_driver_register(&mtx20_unflatten_dts);
  170. }
  171. static void __exit mtx20_unflatten_dts_exit(void)
  172. {
  173.         device_destroy(xble_class, ds1302_devno);
  174.         class_destroy(xble_class);
  175.   cdev_del(&ds1302_dev.cdev);  
  176.   unregister_chrdev_region(ds1302_devno, 1);  
  177.         platform_driver_unregister(&mtx20_unflatten_dts);
  178. }
  179. module_init(mtx20_unflatten_dts_init);
  180. module_exit(mtx20_unflatten_dts_exit);
  181. MODULE_LICENSE("GPL");

复制代码

位置:android6.0.0/kernel-3.18/drivers/pinctrl/mediatek下,这个位置可以任意只要可以编译即可。
2.dts设置

  1.   mtx20_unflatten_dts: mtx20_unflatten_dts@10030000 {
  2.                         compatible = "mediatek,mtx20_unflatten_dts";
  3.                         #address-cells = <3>;
  4.                         #size-cells = <1>;
  5.                         mtx20_unflatten_dts_tip;
  6.                         linux,code = <20170111>;
  7.                         linux,string = "MediaTek X20-0000-";
  8.                 };

复制代码

android-6.0.0/kernel-3.18/arch/arm64/boot/dts下mt6797.dtsi文件中。
3.linux kernel编译开关设置
android-6.0.0/kernel-3.18/arch/arm64下Kconfig文件中添加
select MTX20_UNFLATTEN_DTS
然后编译更新boot.img。
4.应用程序源代码

  1. #include <stdlib.h> 
  2. #include <time.h> 
  3. #include <string.h> 
  4. #include <wchar.h>
  5. #include <fcntl.h>
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <utils/Log.h>
  9. #define CHIP_ADDR 0x50   //24CXX
  10. //#define I2C_DEV "/sys/class/xble/xblectl"
  11. #define I2C_DEV "/dev/xblectl"
  12. //static int read_eeprom(int fd,char buff[],int addr,int count)
  13. //{
  14. //     int res;
  15. //     lseek(fd, 64, SEEK_SET);
  16. //     if (write(fd,&addr,1)!=1)
  17. //     {
  18. //    printf("Can't write %s's addr %d\n",I2C_DEV,addr);
  19. //    return -1;
  20. //     }
  21. //     res = read(fd,buff,count);
  22. //     printf("read %d bytes at 0x%02x\n\r",res,addr);
  23. //     return res;
  24. //}
  25. //static int write_eeprom(int fd,char buff[],int addr,int count)
  26. //{
  27. //   int res;
  28. //   int i;
  29. //   static char sendbuffer[PAGE_SIZE+1];
  30. //  
  31. //   memcpy(sendbuffer+1,buff,count);
  32. //   sendbuffer[0]=addr;
  33. //   
  34. //   res = write(fd,&sendbuffer,count+1);
  35. //   printf("write %d bytes at 0x%02x buff[0]=%c\n",res,sendbuffer[0], buff[0]);
  36. //   return res;
  37. //}
  38. int main(void)
  39. {
  40. int fd,i,res;
  41. unsigned char str1[PAGE_SIZE] = "aaaaaaaaaaaaaaaaaaaaaaaaa";
  42. unsigned char s2[PAGE_SIZE] = "11111111111111111111111111111111";
  43. unsigned char buf[PAGE_SIZE] = {0};
  44. fd=open(I2C_DEV,O_RDWR);
  45. if(fd<0)
  46.    {
  47.     printf("Can't Open %s !!!\n\r",I2C_DEV);
  48.     return -1;
  49.    }
  50.         //LOGE("==20170109 open =buf = %s\n 0x4d=<%c>\n", buf, 0x4d);
  51.   printf("=^^^^^^^^^^^^=buf = %s\n 0x4d=<%c>\r", buf, 0x4d);
  52.   close(fd);
  53.   return 0;
  54. }

复制代码

5应用程序编译,随意找一个目录,修改一下Android.mk文件,把我们的程序添加进去,编译即可。
 
6.测试
 
 
这三部分掌握后,基本上就可以为MTX20开发板添加任何驱动和应用程序。


DragonCat liked
回复引用
  
Working

登陆 或者 注册