/* * I2C LCD display AQM0802A test for NetBSD/evbarm on RPI2 * Copyright (C) Yasushi Oshima. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #include #include #include #include #include int i2c_write(int fd, int addr, uint8_t *cmd, size_t cmdlen, uint8_t *buf, size_t buflen) { i2c_ioctl_exec_t i2cop; i2cop.iie_op = I2C_OP_WRITE_WITH_STOP; /* データの連続書き込み */ i2cop.iie_addr = (i2c_addr_t)addr; /* スレーブアドレス */ i2cop.iie_cmd = cmd; /* コマンドポインタ */ i2cop.iie_cmdlen = cmdlen; /* コマンドの長さ */ i2cop.iie_buf = buf; /* データバッファポインタ */ i2cop.iie_buflen = buflen; /* データ長 */ return ioctl( fd, I2C_IOCTL_EXEC, &i2cop); /* ioctl発行 */ } int setup(int fd) { int addr = 0x3e; uint8_t cmd = 0x00; uint8_t setup1[] = { 0x38, 0x39, 0x14, 0x70,0x56, 0x6c }; uint8_t setup2[] = { 0x38, 0x0C, 0x01 }; if (i2c_write(fd, addr, &cmd, 1, setup1, 6)==-1) return -1; usleep (200 * 1000); /* 200ms */ if (i2c_write(fd, addr, &cmd, 1, setup2, 3)==-1) return -2; usleep (2 * 1000); /* 2ms */ return 0; } int display(int fd) { int addr = 0x3e; uint8_t cmd = 0x00; uint8_t pos; char text1[]="NetBSD"; char text2[]="evbarm"; pos = (uint8_t)0x81; /* 位置 (0x80 | (row << 6) | col) */ if (i2c_write(fd, addr, &cmd, 1, &pos, 1)==-1) return -1; cmd = 0x40; /* データ設定コマンド*/ if (i2c_write(fd, addr, &cmd, 1, text1, strlen(text1))==-1) return -2; cmd = 0x00; /* 制御コマンド */ pos = (uint8_t)(0x80 | (1<<6) | 2); /* 下行の3文字目から */ if (i2c_write(fd, addr, &cmd, 1, &pos, 1)==-1) return -3; cmd = 0x40; /* データ設定コマンド */ if (i2c_write(fd, addr, &cmd, 1, text2, strlen(text2)) ==-1) return -4; return 0; } int main() { int i2cfd; int r; struct timespec t = { 0, 300000000}; if ((i2cfd = open("/dev/iic1", O_RDWR)) == -1 ) err(1,"open"); if (r=setup(i2cfd)) err(1,"setup %d",r); if (r=display(i2cfd)) err(1,"display %d",r); }