外设测试 - EEPROM 芯片测试

原理

EEPROM 挂载在 I2C 总线下,程序基于 Linux 的 I2C 子系统进行数据读写,并校验。

参考代码

 

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <getopt.h>
#include <stdbool.h>
#include <libgen.h>
#include <signal.h>
#include <string.h>

#define INADEQUATE_CONDITIONS 3

/* SH_EEPROM_DEV  : shell commnad for finding eeprom device path */
#define SH_EEPROM_DEV "find /sys/devices | grep eeprom"

/* Short option names */
static const char g_shortopts [] = ":vh";

/* Option names */
static const struct option g_longopts [] = {
    { "version",     no_argument,            NULL,        'v' },
    { "help",        no_argument,            NULL,        'h' },
    { 0, 0, 0, 0 }
};

static void usage(FILE *fp, int argc, char **argv) {
    fprintf(fp,
            "Usage: %s [options]\n\n"
            "Options:\n"
            " -v | --version       Display version information\n"
            " -h | --help          Show help content\n\n"
            "", basename(argv[0]));
}

static void opt_parsing_err_handle(int argc, char **argv, int flag) {
    /* Exit if no input parameters are entered  */
    int state = 0;
    /* Feedback Error parameter information then exit */
    if (optind < argc || flag) {
        printf("Error:  Parameter parsing failed\n");
        if (flag)
            printf("\tunrecognized option '%s'\n", argv[optind-1]);

        while (optind < argc) {
            printf("\tunrecognized option '%s'\n", argv[optind++]);
        }

        state = -1;
    }

    if (state == -1) {
        printf("Tips: '-h' or '--help' to get help\n\n");
        exit(EXIT_FAILURE);
    }
}

/* retrival the device path of eeprom */
static int get_device_path(char dev[]) {
    FILE *f;

    /* the popen() function opens a process by creating a pipe, forking, and invoking the shell */
    f = popen(SH_EEPROM_DEV, "r");
    if (! f) 
        goto err;

    if (fscanf(f, "%s", dev) == EOF)
        goto err;

    pclose(f);
    return 0;

err:
    fprintf(stderr, "Error : maybe there's no eeprom device\n");
    if (f)
        pclose(f);
    return -1;
}

static int get_device_storage_size(char dev[]) {
    char path[128];
    char cmd[256];
    char size_info[10];
    char *ret = NULL;
    const char ch = '/';

    memcpy(path, dev, strlen(dev));
    ret = strrchr(path, ch);
    strncpy(ret, "/name\0", 6);
    snprintf(cmd, sizeof(cmd), "cat %s | awk -F c \'{print $2*1024/8}\'", path);

    FILE *f;
    f = popen(cmd, "r");
    if (! f) 
        goto err;

    if (fscanf(f, "%s", size_info) == EOF)
        goto err;

    pclose(f);
    return atoi(size_info);

err:
    fprintf(stderr, "Error : maybe there's no eeprom device\n");
    if (f)
        pclose(f);
    return -1;
}

/* write $len length string @str to @dev at the offset of @offset */
int write_string(const char *dev, char *str, int len, int offset) {
    FILE *f;

    f = fopen(dev, "w+");
    if (! f) 
        goto err;

    if (fseek(f, offset, SEEK_SET) < 0)
        goto err;

    if (fwrite(str, len, 1, f) != 1)
        goto err;

    fclose(f);
    return 0;

err:
    fprintf(stderr, "Error: %s\n", strerror(errno));
    if (f)
        fclose(f);
    return -1;
}

/* read $len length string @str to @dev at the offset of @offset */
int read_string(const char *dev, char str[], int len, int offset) {
    FILE *f;

    f = fopen(dev, "r+");
    if (! f) 
        goto err;

    if (fseek(f, offset, SEEK_SET) < 0)
        goto err;

    if (fread(str, len, 1, f) != 1)
        goto err;

    fclose(f);
    return 0;

err:
    fprintf(stderr, "Error: %s\n", strerror(errno));
    if (f)
        fclose(f);
    return -1;
}

int main(int argc, char **argv) {
    int c = 0;
    int flag = 0;
    int ret = -1;
    int offset = 0;
    char dev[128]; 

    /* Parsing input parameters */
    while ((c = getopt_long(argc, argv, g_shortopts, g_longopts, NULL)) != -1) {
        switch (c) {
        case 'v':
            /* Display the version */
            printf("version : 1.0\n");
            exit(EXIT_SUCCESS);

        case 'h':
            usage(stdout, argc, argv);
            exit(EXIT_SUCCESS);

        default :
            flag = 1;
            break;
        }
    }

    opt_parsing_err_handle(argc, argv, flag);

    /* get path of device */
    ret = get_device_path(dev);
    if (ret < 0)
        return INADEQUATE_CONDITIONS;

    int size = get_device_storage_size(dev);
    if (size < 0) {
        ret = 1;
        goto release;
    }
    char *write_buf = (char*)malloc(size);
    char *read_buf = (char*)malloc(size);

    memset(write_buf, rand() % 26 + 65, size);
    memset(read_buf, 0, size);

    printf("eeprom: %s, size: %d\n", dev, size);

    /* write string to device */
    ret = write_string(dev, write_buf, size, offset);
    if (ret < 0)
        goto release;
    printf("write size: %d\n", strlen(write_buf));

    /* read string from device */
    ret = read_string(dev, read_buf, size, offset);
    if (ret < 0)
        goto release;
    printf("read size: %d\n", strlen(read_buf));

    ret = memcmp(read_buf, write_buf, size);
    if (ret != 0) {
        printf("Result : Inconsistent data\n\n");
        goto release;
    } else {
        printf("Result : Consistent data\n\n");
    }

release:
    free(write_buf);
    free(read_buf);
    if (ret != 0) {
        printf("failed.\n");
        return INADEQUATE_CONDITIONS;
    }
    else {
        printf("pass.\n");
        return 0;
    }
}

参考

I2C 内核官方文档: https://www.kernel.org/doc/html/latest/i2c/index.html

2020年10月30日

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页