Last active
July 1, 2018 14:43
-
-
Save cr1901/5fa37836788a52236c8e to your computer and use it in GitHub Desktop.
NetBSD Raspberry Pi I2C IOCTL Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
#include <stdint.h> | |
/* POSIX headers */ | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <dev/i2c/i2c_io.h> | |
int write_eeprom(int fd, uint16_t addr, uint8_t val); | |
int read_eeprom_rand(int fd, uint16_t addr, uint8_t * val); | |
int main() | |
{ | |
int i2cfd, i; | |
uint8_t byte_read, byte_written = 0xAA; | |
unsigned char read_val; | |
i2c_ioctl_exec_t iie; | |
i2cfd = open("/dev/iic1", O_RDWR); | |
if(i2cfd == -1) | |
{ | |
printf("File open for I2C bus failed! (%s)\n", strerror(errno)); | |
return EXIT_FAILURE; | |
} | |
for(i = 0; i < 512; i++) | |
{ | |
/* For a fresh EEPROM, 0xFF should be the default value. | |
Let's rewrite it! */ | |
if(write_eeprom(i2cfd, i, (uint8_t) 0xFF)) | |
{ | |
printf("I2C write failed! (%s) \n", strerror(errno)); | |
close(i2cfd); | |
return EXIT_FAILURE; | |
} | |
usleep(50000); /* 10 times the maximum time for internal writes to | |
register according to AT24HC04B datasheet (5ms) */ | |
} | |
for(i = 0; i < 512; i++) | |
{ | |
if(read_eeprom_rand(i2cfd, i, &byte_read)) | |
{ | |
printf("I2C read failed! (%s) \n", strerror(errno)); | |
close(i2cfd); | |
return EXIT_FAILURE; | |
} | |
else | |
{ | |
printf("%d\t", byte_read); | |
} | |
} | |
close(i2cfd); | |
return EXIT_SUCCESS; | |
} | |
int write_eeprom(int fd, uint16_t addr, uint8_t val) | |
{ | |
i2c_ioctl_exec_t iie; | |
uint8_t page, byte_no; | |
page = ((addr & 0x0100) >> 8); | |
byte_no = (addr & 0x00FF); | |
printf("w: page: %X, byte_no: %X, value: %X\n", page, byte_no, val); | |
iie.iie_op = I2C_OP_WRITE_WITH_STOP; | |
iie.iie_addr = 0x50 | page; /* Default for ATMEL EEPROMs, | |
A0 is used for addr of 256b page .*/ | |
iie.iie_cmd = &byte_no; /* Send the write byte address first. */ | |
iie.iie_cmdlen = 1; | |
iie.iie_buf = &val; | |
iie.iie_buflen = 1; | |
return ioctl(fd, I2C_IOCTL_EXEC, &iie); | |
} | |
int read_eeprom_rand(int fd, uint16_t addr, uint8_t * val) | |
{ | |
i2c_ioctl_exec_t iie; | |
uint8_t page, byte_no; | |
page = ((addr & 0x0100) >> 8); | |
byte_no = (addr & 0x00FF); | |
//printf("r: page: %X, byte_no: %X\n", page, byte_no); | |
iie.iie_op = I2C_OP_READ_WITH_STOP; | |
iie.iie_addr = 0x50 | page; /* Default for ATMEL EEPROMs, | |
A0 is used for addr of 256b page .*/ | |
iie.iie_cmd = &byte_no; /* To do a random read, we need to send the | |
byte address first. */ | |
iie.iie_cmdlen = 1; | |
iie.iie_buf = val; | |
iie.iie_buflen = 1; | |
return ioctl(fd, I2C_IOCTL_EXEC, &iie); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CFLAGS= | |
CPPFLAGS= | |
LDFLAGS= | |
LIBS= | |
LIBPATH= | |
RM=rm | |
RMFLAGS=-f | |
INSTALL=install | |
gpio_test: gpio_test.c | |
# $(CC) $(LDFLAGS) $(LIBPATH) -o $@ $(GPIOBJS) $(LIBS) | |
.c: | |
$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(LIBPATH) -o $@ $< $(LIBS) | |
.c.o: | |
$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $< | |
clean: | |
$(RM) $(RMFLAGS) *.o gpio_test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment