Created
November 2, 2013 06:31
-
-
Save rutles/7276207 to your computer and use it in GitHub Desktop.
AQM1248 hadling example for Raspberry Pi via file system version.fill gray all area.
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
| // AQM1248 hadling example for Raspberry Pi | |
| // via file system version | |
| // compile: cc aqm1248fs.c -o aqm1248fs | |
| // execute: sudo ./aqm1248fs | |
| #include <stdint.h> | |
| #include <unistd.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <errno.h> | |
| #include <fcntl.h> | |
| #include <string.h> | |
| #include <sys/ioctl.h> | |
| #include <linux/spi/spidev.h> | |
| #define CONTRUST 0x1c | |
| #define PORT_RS 25 | |
| int passign(int port){ | |
| int ret; | |
| int fd; | |
| char buf[40]; | |
| // enable port | |
| fd = open("/sys/class/gpio/export", O_WRONLY); | |
| if(fd == -1){ | |
| fprintf(stderr, "fail to open GPIO%d. %s\n", port, strerror(errno)); | |
| return -1; | |
| } | |
| sprintf(buf, "%d", port); | |
| ret = write(fd, buf, strlen(buf)); | |
| close(fd); | |
| if(ret <= 0){ | |
| fprintf(stderr, "fail to open GPIO%d. %s\n", port, strerror(errno)); | |
| return -1; | |
| } | |
| // set port as output | |
| sprintf(buf, "/sys/class/gpio/gpio%d/direction", port); | |
| fd = open(buf, O_WRONLY); | |
| write(fd, "out", 3); | |
| close(fd); | |
| // open port for write | |
| sprintf(buf, "/sys/class/gpio/gpio%d/value", port); | |
| fd = open(buf, O_WRONLY); | |
| return fd; | |
| } | |
| void pfree(int port){ | |
| int fd; | |
| char buf[40]; | |
| // disable port | |
| fd = open("/sys/class/gpio/unexport", O_WRONLY); | |
| sprintf(buf, "%d", port); | |
| write(fd, buf, strlen(buf)); | |
| close(fd); | |
| } | |
| int transfer(int fd, uint8_t value){ | |
| int ret; | |
| uint8_t tx; | |
| struct spi_ioc_transfer tr ={ | |
| .tx_buf = (uint32_t)&tx, | |
| .rx_buf = (uint32_t)NULL, | |
| .len = 1, | |
| .delay_usecs = 0, | |
| .speed_hz = 2000000, | |
| .bits_per_word = 8, | |
| }; | |
| tx = value; | |
| ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); | |
| if(ret < 1){ | |
| fprintf(stderr, "can't send spi message\n"); | |
| return -1; | |
| } | |
| return 0; | |
| } | |
| #define CONTRUST 0x1c | |
| int main(int argc, char *argv[]){ | |
| int i, j; | |
| int ret = 0; | |
| int pfd; | |
| int sfd; | |
| pfree(PORT_RS); | |
| pfd = passign(PORT_RS); | |
| sfd = open("/dev/spidev0.0", O_RDWR); | |
| if(pfd < 0 || sfd < 0){ | |
| fprintf(stderr, "can't open device\n"); | |
| return -1; | |
| } | |
| write(pfd, "0", 2); | |
| transfer(sfd, 0xae); | |
| transfer(sfd, 0xa0); | |
| transfer(sfd, 0xc8); | |
| transfer(sfd, 0xa3); | |
| transfer(sfd, 0x2c); usleep(2000); | |
| transfer(sfd, 0x2e); usleep(2000); | |
| transfer(sfd, 0x2f); | |
| transfer(sfd, 0x23); | |
| transfer(sfd, 0x81); | |
| transfer(sfd, CONTRUST); | |
| transfer(sfd, 0xa4); | |
| transfer(sfd, 0x40); | |
| transfer(sfd, 0xa6); | |
| transfer(sfd, 0xaf); | |
| for(i = 0; i < 6; i++){ | |
| transfer(sfd, 0xb0 | i);//page | |
| transfer(sfd, 0x10);//column 0 | |
| transfer(sfd, 0x00); | |
| write(pfd, "1", 2); | |
| for(j = 0; j < 64; j++){ | |
| transfer(sfd, 0x55); | |
| transfer(sfd, 0xaa); | |
| } | |
| write(pfd, "0", 2); | |
| } | |
| close(pfd); | |
| pfree(PORT_RS); | |
| close(sfd); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment