You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
7.1 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * at24c256_i2c.c
  3. *
  4. * This script is a demo for AT24C256 EEPROM access via I2C on Cubieboard
  5. *
  6. * Author: soloforce, forum.cubietech.com
  7. * Date: 2013.11.18
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  10. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  11. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  12. * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  13. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  14. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  15. *
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <sys/ioctl.h>
  22. #include <linux/i2c-dev.h>
  23. // Cubieboard IO port PB18 & PB19 are configured to TWI1-SCK & TWI1-SDA (I2C-1)
  24. #define DEFAULT_AT24C256_DEV "/dev/i2c-1"
  25. // ATmega AT24C series EEPROM slave devices are addressed from 0x50~0x57
  26. // As for AT24C256, should be 0x50~0x53
  27. #define DEFAULT_AT24C256_ADDRESS 0x50
  28. // AT24C256 has 512 pages, 64 bytes each
  29. #define PAGE_BYTES 64
  30. typedef unsigned char uint8;
  31. typedef unsigned short uint16;
  32. /*
  33. * Open the i2c bus device
  34. */
  35. int init_at24c256_i2c(const char* device)
  36. {
  37. int fd=open(device, O_RDWR);
  38. if(fd<0){
  39. perror("Failed to open I2C device!");
  40. return -1;
  41. }
  42. ioctl(fd, I2C_TIMEOUT, 3); // i2c timeout, 1 for 10ms ; if too small, i2c may lose response
  43. ioctl(fd, I2C_RETRIES, 3); // i2c retry limit
  44. return fd;
  45. }
  46. /*
  47. * Page write (or partially page write), data_len should less than PAGE_BYTES
  48. *
  49. */
  50. int at24c256_write(int fd, int device_addr, uint16 offset, const char* data, int data_len)
  51. {
  52. struct i2c_rdwr_ioctl_data at24c256; // structure pass to i2c driver
  53. struct i2c_msg i2cmsg;
  54. if(data_len>PAGE_BYTES){
  55. printf("*** Warning: Page write mode should not exceed %d bytes, otherwise the current page will be rolled over!\n",PAGE_BYTES);
  56. }
  57. at24c256.nmsgs = 1; // message count in the structure
  58. at24c256.msgs = &i2cmsg;
  59. at24c256.msgs[0].len = 2 + data_len; // the offset address word is 2 bytes long, plus the buf_len
  60. at24c256.msgs[0].addr = device_addr; // i2c slave device address
  61. at24c256.msgs[0].flags = 0; // 0 for write; 1 for read
  62. at24c256.msgs[0].buf = (uint8 *)malloc( at24c256.msgs[0].len );
  63. if(!at24c256.msgs[0].buf){
  64. printf("at24c256_write(): Failed to allocate memory for i2c message buffer!\n");
  65. return -1;
  66. }
  67. at24c256.msgs[0].buf[0] = (uint8)(offset >> 8); // MSB first
  68. at24c256.msgs[0].buf[1] = (uint8)(offset); // LSB
  69. strncpy( at24c256.msgs[0].buf+2, data, data_len); // copy user's data to the structure buffer
  70. if(ioctl(fd, I2C_RDWR, (unsigned long)&at24c256)<0 ){ // write ioctl data to the i2c bus
  71. printf("at24c256_write(): Failed to write data to i2c device!\n");
  72. free(at24c256.msgs[0].buf);
  73. return -1;
  74. }
  75. free(at24c256.msgs[0].buf);
  76. return 0;
  77. }
  78. /*
  79. * Sequential read from AT24C256, data_len should less than PAGE_BYTES
  80. */
  81. int at24c256_read(int fd, int device_addr, uint16 offset, char* data, int data_len)
  82. {
  83. struct i2c_rdwr_ioctl_data at24c256; // structure pass to i2c driver
  84. struct i2c_msg i2cmsg[2]; // message count in the structure
  85. char dummy_write_buf[2];
  86. if(data_len>PAGE_BYTES){
  87. printf("*** Warning: Sequential read should not exceed %d bytes, otherwise the read data will be rolled over!\n",PAGE_BYTES);
  88. }
  89. // A dummy write operation should be done according to the AT24C256 i2c protocol
  90. at24c256.nmsgs = 2; // message count in the structure
  91. at24c256.msgs = i2cmsg;
  92. at24c256.msgs[0].len = 2; // the offset address word is 2 bytes long
  93. at24c256.msgs[0].addr = device_addr;
  94. at24c256.msgs[0].flags = 0; // 0 for write;
  95. at24c256.msgs[0].buf = dummy_write_buf;
  96. at24c256.msgs[0].buf[0] = (unsigned char)(offset >> 8); // write address
  97. at24c256.msgs[0].buf[1] = (unsigned char)(offset); // write address
  98. // read operation
  99. at24c256.msgs[1].len = data_len;
  100. at24c256.msgs[1].addr = device_addr;
  101. at24c256.msgs[1].flags = 1; // 1 for read; 0 for write
  102. at24c256.msgs[1].buf = data;
  103. if (ioctl(fd, I2C_RDWR, (unsigned long)&at24c256) < 0) {
  104. printf("at24c256_read(): Failed to read data from EEPROM via i2c!\n");
  105. return -1;
  106. }
  107. return 0;
  108. }
  109. /*
  110. * Show help message
  111. */
  112. void show_usage(char* argv[])
  113. {
  114. printf("%s <-d device> <-a address> [-o offset] [-r length] [-w data] [-h]\n",argv[0]);
  115. printf("\t-d device: i2c device file, like /dev/i2c-1 by default\n");
  116. printf("\t-a address: i2c slave device base address, like 0x50 by default\n");
  117. printf("\t-o offset: offset from base address, like 0x00, 0x10 ...\n");
  118. printf("\t-r length: read length bytes from at24c256 eeprom\n");
  119. printf("\t-w data: write to at24c256 eeprom\n");
  120. printf("\t-h: show help message\n\n");
  121. }
  122. int main(int argc, char* argv[])
  123. {
  124. char* device=DEFAULT_AT24C256_DEV;
  125. int slave_address=DEFAULT_AT24C256_ADDRESS;
  126. int offset=0x00; // default offset is 0
  127. int length=32; // read 32 bytes by default
  128. char* data=NULL;
  129. int rw_mode=1; // 0 for write, 1 for read;
  130. if(argc==1){
  131. show_usage(argv);
  132. return 0;
  133. }
  134. int i;
  135. while ((i = getopt(argc, argv, "d:a:o:r:w:n:h")) >= 0){
  136. switch(i){
  137. case 'd':
  138. device=optarg;
  139. break;
  140. case 'a':
  141. if (sscanf(optarg, "0x%x", &slave_address) != 1) {
  142. fprintf(stderr, "Cannot parse '%s' as i2c slave device address, example: '0x50'\n", optarg);
  143. return -1;
  144. }
  145. break;
  146. case 'o':
  147. if (sscanf(optarg, "%d", &offset) != 1) {
  148. fprintf(stderr, "Cannot parse '%s' as address offset, example: '32'\n", optarg);
  149. return -1;
  150. }
  151. break;
  152. case 'r':
  153. rw_mode=1;
  154. if (sscanf(optarg, "%d", &length) != 1) {
  155. fprintf(stderr, "Cannot parse '%s' as length, example: '17'\n", optarg);
  156. return -1;
  157. }
  158. break;
  159. case 'w':
  160. rw_mode=0;
  161. data=optarg;
  162. length=strlen(data);
  163. break;
  164. case 'h':
  165. show_usage(argv);
  166. return 0;
  167. break;
  168. }
  169. }
  170. int fd=init_at24c256_i2c(device);
  171. if(fd<0) return -1;
  172. printf("i2c device file:%s\nslave_address:0x%X\nR/W offset:0x%X\n",device, slave_address, offset);
  173. if(rw_mode==0){
  174. // test at24c256 write
  175. at24c256_write(fd, slave_address, offset, data, length);
  176. printf("try to write: %s (%d bytes)!\n",data,length);
  177. usleep(10000); // some delay is necessary for the write operation done
  178. }else if(rw_mode==1){
  179. // test at24c256 read
  180. char *buf=(char*)malloc(length+1);
  181. memset(buf,0,length+1);
  182. at24c256_read(fd, slave_address, offset, buf, length);
  183. usleep(10000); // necessary for the read operation done
  184. printf("read from at24c256 eeprom: %s (%d bytes)\n", buf, strlen(buf));
  185. free(buf);
  186. }
  187. close(fd);
  188. }