单片机解密AT91SAM9G25采用控制的 i2c 驱动
单片机解密单片机源程序如下:
- //i2c_test.c
- 单片机解密#include<stdio.h>
- 单片机解密#include<linux/types.h>
- 单片机解密#include<fcntl.h>
- #include<unistd.h>
- #include<stdlib.h>
- #include<sys/types.h>
- #include<sys/ioctl.h>
- #include<errno.h>
- #include<assert.h>
- #include<string.h>
- #include<linux/i2c.h>
- #include<linux/i2c-dev.h>
- #define CHIP_ADDR_START 0x40
- #define CHIP_NUM 1
- #define BUF_SIZE 32
- #define I2C_DEV "/dev/i2c-1"
- static int read_dth(int fd, unsigned char slave_address, unsigned char reg_addr, unsigned char buff[], int count)
- {
- int ret;
- struct i2c_rdwr_ioctl_data work_queue;
-
- work_queue.msgs = (struct i2c_msg *)malloc(work_queue.nmsgs *sizeof(struct i2c_msg));
- if(!work_queue.msgs)
- {
- printf("read_dth, Memery alloc error\n");
-
- return -1;
- }
- work_queue.nmsgs = 2;
- work_queue.msgs[0].len = 1;
- work_queue.msgs[0].addr = slave_address;
- // work_queue.msgs[0].flags = 0; /* write */
- work_queue.msgs[0].buf= ®_addr;
- work_queue.msgs[1].len= count;
- work_queue.msgs[1].addr= slave_address;
- work_queue.msgs[1].flags= 1; /* read */
- work_queue.msgs[1].buf= buff;
- ret= ioctl(fd, I2C_RDWR, (unsigned long)&work_queue);
- if(ret < 0)
- {
- printf("readerror\n");
- }
- else
- {
- printf("read data: %02x%02x from address: %02x\n", buff[0], buff[1], reg_addr);
- }
-
- return ret;
- }
- static int write_dth(int fd, unsigned char slave_address, unsigned char addr, char buff[], int count)
- {
- int ret;
- unsigned char buffer[2];
- struct i2c_rdwr_ioctl_data work_queue;
-
- work_queue.msgs = (struct i2c_msg *)malloc(work_queue.nmsgs *sizeof(struct i2c_msg));
- if(!work_queue.msgs)
- {
- printf("write_dth, Memery alloc error\n");
-
- return -1;
- }
-
- work_queue.nmsgs = 1;
- work_queue.msgs[0].len = 2;
- work_queue.msgs[0].addr = slave_address;
- work_queue.msgs[0].flags = 0; /* write */
-
- work_queue.msgs[0].buf = buffer;
- work_queue.msgs[0].buf[0]= addr; /* write address */
- work_queue.msgs[0].buf[1]= buff[0]; /* write data */
- ret= ioctl(fd, I2C_RDWR, (unsigned long)&work_queue);
- if(ret < 0)
- {
- printf("writedata error\n");
- }
- else
- {
- printf("writedata: %02x to address: %02x\n", buff[0], addr);
- }
- return ret;
- }

芯片解密