Android下使用W25Q32

一路做下来,感觉过程还是满复杂的,特意写一篇文章,方便大家用到类似的外设。
刚开始是想着自己写spi驱动,操作W25Q32的寄存器,本来已经读到芯片的ID了,后来因为cs脚的原因,以为自己写的程序有问题,转而找其他的方法,发现linux驱动中是支持这个系列的,路径在\drivers\mtd\devices\m25p80.c。具体操作为打开CONFIG_MTD_M25P80驱动,打开MTD相关的驱动,在dtd中加入
m25p80{
	compatible = "m25p80,w25q32";
	status = "okay";
	partition@0 {
		label = "JEDEC";
		reg = <0x0 0x1000>;
		read-only;
	};
	
	partition@1000 {
		label = "user";
		reg = <0x1000 0x3ff000>;
	};
};
内容精简掉了一些与平台相关的。
其实linux是把这种存储外设映射成mtd设备,我把它再分区一下,因为第一个块里存储着设备的id,所以设置成只读。
确认spi通信没问题,正常的话,就像下面这样
ls dev/block/mtdblock
ls dev/mtd/mtd

cat proc/mtd                                                 
dev:    size   erasesize  name
mtd0: 00001000 00001000 "JEDEC"
mtd1: 003ff000 00001000 "user"

接下来是在应用中操作/dev/mtd/mtd1节点。linux程序网上有很多,这里只给出android应用如何操作的具体代码
首先是jni里的
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <mtd/mtd-user.h>
#include <jni.h>

#include "android/log.h"
static const char *TAG="25q32";
#define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO,  TAG, fmt, ##args)
#define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, TAG, fmt, ##args)
#define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, TAG, fmt, ##args)

int fd=-1;

JNIEXPORT void JNICALL Java_w25q32_open
        (JNIEnv *env, jclass thiz)
{
    /* Opening device */
    const char *path_utf = "/dev/mtd/mtd1";
    LOGD("Opening %s with flags 0x%x", path_utf, O_SYNC | O_RDWR);
    fd = open(path_utf, O_SYNC | O_RDWR);
    LOGD("open() fd = %d", fd);
    if (fd == -1)
    {
        /* Throw an exception */
        LOGE("Cannot open 25q32");
        /* TODO: throw an exception */
    }
}

JNIEXPORT void JNICALL Java_w25q32_close
(JNIEnv *env, jobject thiz)
{
    LOGD("close(fd = %d)", fd);
    if (fd != -1)
    {
        close(fd);
    }
}

#define BUF_SIZE	(64 * 1024)

JNIEXPORT jint JNICALL JNICALL Java_w25q32_eraseFlash
(JNIEnv *env, jclass thiz, jint offset, jint bytes)
{
    int err;
    struct erase_info_user erase;
    erase.start = offset;
    erase.length = bytes;
    err = ioctl (fd,MEMERASE,&erase);
    if (err < 0)
    {
        LOGE ("MEMERASE");
        return 1;
    }
    LOGD ("Erased %d bytes from address 0x%.8x in flash\n",bytes,offset);
    return 0;
}

JNIEXPORT jbyteArray JNICALL JNICALL Java_w25q32_readFlash
 (JNIEnv *env, jclass thiz, jint offset, jint len)
{
    jbyte *buf = NULL;
    int size = len;
    int err;
    jbyteArray result;

retry:
    if ((buf = (jbyte *) malloc (size)) == NULL)
    {
        LOGD ("%s: malloc(%#x)\n", __FUNCTION__, size);
        if (size != BUF_SIZE) {
            size = BUF_SIZE;
            LOGE ("%s: trying buffer size %#x\n", __FUNCTION__, size);
            goto retry;
        }
        LOGE ("malloc()");
        goto err0;
    }

    if (offset != lseek (fd,offset,SEEK_SET))
    {
        LOGE ("lseek()");
        goto err1;
    }

    err = read (fd,buf,len);
    LOGD ("read %d", err);
    if (err < 0)
    {
        goto err1;
    }

    result = (*env)->NewByteArray(env, err);
    if (result == NULL)
    {
        LOGE ("NewByteArray()");
        goto err1;
    }

    (*env)->SetByteArrayRegion(env, result, 0, err, buf);

    LOGD ("Copied %zu bytes from address 0x%.8x in flash\n",err,offset);
    free (buf);
    return result;

err1:
    if (buf != NULL)
        free (buf);

err0:
    return NULL;
}

JNIEXPORT jint JNICALL JNICALL Java_w25q32_writeFlash
(JNIEnv *env, jclass thiz, jint offset, jbyteArray array, jint len)
{
    jbyte *buf = NULL;
    int err;
    int size = len;

    if (offset != lseek (fd,offset,SEEK_SET))
    {
        LOGE ("lseek()");
        goto err0;
    }

    buf = (*env)->GetByteArrayElements(env, array, NULL);
    err = write (fd,buf,len);
    if (err < 0)
    {
        LOGE ("%s: write, size %#x\n", __FUNCTION__, err);
        LOGE ("write()");
        goto err1;
    }

    LOGD ("Copied %d bytes to address 0x%.8x in flash\n", err, offset);
    (*env)->ReleaseByteArrayElements(env, array, buf, 0);
    return err;

err1:
    (*env)->ReleaseByteArrayElements(env, array, buf, 0);
err0:
    return -1;
}

然后是java里的
public class w25q32 {
    /*
	 * Do not remove or rename the field mFd: it is used by native method
	 * close();
	 */
    final int ROM_SIZE = 4190208;//0x3ff000
    final int PAGE_SIZE = 4096;//0x1000

    public w25q32() throws SecurityException
    {
        open();
    }

    public int w25q32_erase(String addr, int len){
        String hex = addr.replace("0x", "");
        int offset = Integer.parseInt(hex, 16);
        if(offset+len > ROM_SIZE)
            return -1;

        int times = len/PAGE_SIZE + (len%PAGE_SIZE>0?1:0);
        int erase_size = times*PAGE_SIZE;

        return eraseFlash(offset, erase_size);
    }

    public byte[] w25q32_read(String addr, int len){
        if(len<1)
            return null;

        String hex = addr.replace("0x", "");
        int offset = Integer.parseInt(hex, 16);
        int size = len;
        if(offset+size > ROM_SIZE) {
            size = ROM_SIZE - offset;
        }

        return readFlash(offset, size);
    }

    public int w25q32_write(String addr, byte[] buf, int len){
        if(buf == null)
            return -1;

        int ret = w25q32_erase(addr, len);
        if(ret != 0)
            return ret;

        String hex = addr.replace("0x", "");
        int offset = Integer.parseInt(hex, 16);

        return writeFlash(offset, buf, len);
    }

    public void releasePort(){close();}

    // JNI
    private native void open();
    private native void close();
    private native int eraseFlash(int offset, int bytes);
    private native byte[] readFlash(int offset, int len);
    private native int writeFlash(int offset, byte[] buf, int len);

    static {
        System.loadLibrary("xx");
    }
}
在应用中使用
w25q32 w25qxx = new w25q32();
w25qxx.w25q32_erase("0x0", 0x3ff000);
byte[] BUFFER1 = {0x0c,0x00,0x29,0x0b,0x01,0x00,(byte)0xfc,(byte)0xff,(byte)0xfa,0x00,
                            (byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,0x0d,0x0a};
w25qxx.w25q32_write("0x1000", BUFFER1, BUFFER1.length);
byte[] BUFFER2 = w25qxx.w25q32_read("0x1000", BUFFER1.length);
w25qxx.releasePort();

以上。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值