微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 手机设计讨论 > MTK手机平台交流 > MT6572 SPI问题

MT6572 SPI问题

时间:10-02 整理:3721RD 点击:
本人现在要添加 一个SPI外部设备,目前按FAQ上说的编写一个驱动文件。目前在ADB终端上能看到节点,但是不昌在/dev/目录下,是在/sys/bus/spi/目录下。目前不知道怎样测试自发自收。linux的描述
文档里有一个SPI的测试例子。但是不知道怎样在mtk的环境下编译,以及在终端上测试

#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/ioport.h>
#include <linux/errno.h>
#include <linux/spi/spi.h>
#include <linux/workqueue.h>
#include <linux/dma-mapping.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <linux/irqreturn.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/io.h>
#include <linux/spinlock.h>
#include <linux/sched.h>
#include <linux/wakelock.h>
#include <mach/irqs.h>
#include <linux/kthread.h>
#include <mach/mt_spi.h>
#include <mach/mt_gpio.h>
#include <mach/emi_mpu.h>
#include "mach/memory.h"
//#include "mt_spi_hal.h"
#include <mach/mt_clkmgr.h>
#define SPI_DRV_NAME        "ata5428"
unsigned char *RX_BUF = NULL;
unsigned char *TX_BUF = NULL;
struct spi_transfer tr[2];
static struct spi_device *ata5428_spi_client;
static struct mt_chip_conf spi_conf;

static int __init ata5428_probe(struct spi_device *spi);
static struct spi_driver ata5428_driver = {
        .driver = {
                .name        = SPI_DRV_NAME,
                .bus        = &spi_bus_type,
                .owner        = THIS_MODULE,
        },
        .probe        = ata5428_probe,
};
static struct spi_board_info spi_board_ata5428[] __initdata = {
        [0] = {
                .modalias= SPI_DRV_NAME,
                .bus_num = 0,
                .chip_select=1,
                .mode = SPI_MODE_0,
                .max_speed_hz = 16000000,
        },
};
static void ata5428_set_spi_mode(int enable){
        if(enable){
        mt_set_gpio_mode(GPIO_SPI_CS_PIN, GPIO_SPI_CS_PIN_M_SPI_CS);
//        mt_set_gpio_pull_enable(GPIO_SPI_CS_PIN, GPIO_PULL_ENABLE);
//        mt_set_gpio_pull_select(GPIO_SPI_CS_PIN, GPIO_PULL_UP);
       
        mt_set_gpio_mode(GPIO_SPI_SCK_PIN, GPIO_SPI_SCK_PIN_M_SPI_SCK);
//        mt_set_gpio_pull_enable(GPIO_SPI_SCK_PIN, GPIO_PULL_ENABLE);
//        mt_set_gpio_pull_select(GPIO_SPI_SCK_PIN, GPIO_PULL_DOWN);
       
        mt_set_gpio_mode(GPIO_SPI_MISO_PIN, GPIO_SPI_MISO_PIN_M_SPI_MISO);
//        mt_set_gpio_pull_enable(GPIO_SPI_MISO_PIN, GPIO_PULL_ENABLE);
//        mt_set_gpio_pull_select(GPIO_SPI_MISO_PIN, GPIO_PULL_DOWN);
       
        mt_set_gpio_mode(GPIO_SPI_MOSI_PIN, GPIO_SPI_MOSI_PIN_M_SPI_MOSI);
//        mt_set_gpio_pull_enable(GPIO_SPI_MOSI_PIN, GPIO_PULL_ENABLE);
//        mt_set_gpio_pull_select(GPIO_SPI_MOSI_PIN, GPIO_PULL_DOWN);
        }else{
        mt_set_gpio_mode(GPIO_SPI_CS_PIN, GPIO_SPI_CS_PIN_M_GPIO);
//        mt_set_gpio_dir(GPIO_SPI_CS_PIN, GPIO_DIR_IN);
//        mt_set_gpio_pull_enable(GPIO_SPI_CS_PIN, GPIO_PULL_DISABLE);
       
        mt_set_gpio_mode(GPIO_SPI_SCK_PIN, GPIO_SPI_SCK_PIN_M_GPIO);
//        mt_set_gpio_dir(GPIO_SPI_SCK_PIN, GPIO_DIR_IN);
//        mt_set_gpio_pull_enable(GPIO_SPI_SCK_PIN, GPIO_PULL_DISABLE);
       
        mt_set_gpio_mode(GPIO_SPI_MISO_PIN, GPIO_SPI_MISO_PIN_M_GPIO);
//        mt_set_gpio_dir(GPIO_SPI_MISO_PIN, GPIO_DIR_IN);
//        mt_set_gpio_pull_enable(GPIO_SPI_MISO_PIN, GPIO_PULL_DISABLE);
       
        mt_set_gpio_mode(GPIO_SPI_MOSI_PIN, GPIO_SPI_MOSI_PIN_M_GPIO);
//        mt_set_gpio_dir(GPIO_SPI_MOSI_PIN, GPIO_DIR_IN);
//        mt_set_gpio_pull_enable(GPIO_SPI_MOSI_PIN, GPIO_PULL_DISABLE);
       
        }       
        return;
}
static int __init ata5428_probe(struct spi_device *spi)
{
        struct mt_chip_conf *spi_par;               
        ata5428_set_spi_mode(1);
        ata5428_spi_client  = spi;
        ata5428_spi_client->bits_per_word = 8;
        ata5428_spi_client->mode = SPI_MODE_0;
        if(ata5428_spi_client){
        ata5428_spi_client->controller_data = (void *)&spi_conf;
        spi_par = &spi_conf;
        if(!spi_par){
        printk("spi config fail");
        return 0;
                }
        spi_par->setuptime = 15;
        spi_par->holdtime = 15;
        spi_par->high_time = 10;
        spi_par->low_time = 10;
       
        spi_par->rx_mlsb = 1;
        spi_par->tx_mlsb = 1;
        spi_par->tx_endian = 0;
        spi_par->rx_endian = 0;
       
        spi_par->cpol = 0;
        spi_par->cpha = 0;
//        spi_par->com_mod =DMA_TRANSFER;        //DMA
        spi_par->com_mod = FIFO_TRANSFER;        //FIFO
        spi_par->pause = 0;
        spi_par->finish_intr = 1;
//        spi_par->deassert = 0;
        spi_par->ulthigh = 0;
        spi_par->tckdly = 0;
       
        printk("setuptime=%d \n",spi_conf.setuptime);
        if(spi_setup(ata5428_spi_client)){
        printk("spi_setup fail");
        return 0;
                }
        }else{
        printk("spi config fail");
        }
        TX_BUF = kzalloc(2,GFP_KERNEL);
        RX_BUF = kzalloc(0x400,GFP_KERNEL);
        return 0;
}
static int ata5428_spi_write_cmd(struct spi_device *spi,unsigned char reg_addr,unsigned char write_val){
        int error;
        u8 rx[32] = {0};
        TX_BUF[0] = reg_addr;
        TX_BUF[1] = write_val;
       
        struct spi_message m;
       
        tr[0].tx_buf = TX_BUF;
        tr[0].rx_buf = rx;
        tr[0].len = 2;
//        tr[0].tx_dma = 0;
//        tr[0].rx_dma = 0;
        tr[0].bits_per_word = 0;
        spi_message_init(&m);
        spi_message_add_tail(&tr[0],&m);
        error = spi_sync(spi,&m);
        if(error) printk("spi_sync failed.\n");
       
        printk("wrote %X to register %X\n",write_val,reg_addr);
        return error;
}
static int ata5428_spi_read_data(struct spi_device *spi,unsigned char *read_buf,int len){
        int error;
        u8 tx[32] = {0};
        struct spi_message m;
        tr[1].tx_buf = tx;
        tr[1].rx_buf = read_buf;
        tr[1].len = len;
//        tr[1].tx_dma = 0;
//        tr[1].rx_dma = 0;
//        tr[1].bits_per_word = 0;
        spi_message_init(&m);
        spi_message_add_tail(&tr[1],&m);
       
        error = spi_sync(spi,&m);
       
        if(error)        printk("spi_sync failed.\n");
       
        printk("read data after write register \n");
       
        return error;
}
static int __init mt_spi_init(void)
{       
        int ret;
        ret = spi_register_board_info(spi_board_ata5428,ARRAY_SIZE(spi_board_ata5428));
        if (spi_register_driver(&ata5428_driver))
                return -EINVAL;
     printk("ata5428_spi_init!\n");
        return 0;
}
static void __exit mt_spi_exit(void)
{
          spi_unregister_driver(&ata5428_driver);
         printk("ata5428_spi_exit!\n");
}
module_init(mt_spi_init);
module_exit(mt_spi_exit);
MODULE_DESCRIPTION("MT6572 SPI Controller Driver");
MODULE_AUTHOR("guo");
MODULE_LICENSE("GPL");
MODULE_ALIAS("ata5428");

上面就是我自己添加的一个ata5428.c驱动文件,目前还没有接芯片测试,只是想测试下自发自收,这一步没问题了再加上芯片稍修改下再测试。

测试程序我打算用linux 文档下的spidev_test.c文件修改下

目前还是不知道为什么我注册的SPI设备没有在/dev/下。是因为这句吗    .bus        = &spi_bus_type,

小编问题解决了吗?

我也发现有类似的问题

等待大神出来                     

你好,请问setuptime holdtime  high_time low_time 怎么调整这几个参数来确定SPI的clk的

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top