Last active
December 27, 2015 05:39
-
-
Save rutles/7276151 to your computer and use it in GitHub Desktop.
AQM1248 hadling example for Raspberry Pi via register 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 register version | |
| // compile: cc aqm1248.c -o aqm1248 | |
| // execute: sudo ./aqm1248 | |
| #include <stdint.h> | |
| #include <unistd.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <errno.h> | |
| #include <fcntl.h> | |
| #include <string.h> | |
| #include <sys/mman.h> | |
| #include <sys/ioctl.h> | |
| #include <linux/spi/spidev.h> | |
| #define OFF_PRT 0x20200000 // port registers | |
| #define AQM_CONT 0x1c // AQM1248 contrust 0-0x3f | |
| #define AQM_RS 25 // AQM1248 register selet | |
| uint32_t *regs_map(off_t offset){ | |
| int fd; | |
| void *map; | |
| fd = open("/dev/mem", O_RDWR | O_SYNC); | |
| if(fd < 0) | |
| return MAP_FAILED; | |
| map = mmap(NULL, | |
| getpagesize(), | |
| PROT_READ | PROT_WRITE, | |
| MAP_SHARED, | |
| fd, | |
| offset); | |
| close(fd); | |
| return map; | |
| } | |
| void fnc_wr(uint32_t *regs, int port, int val){ | |
| regs[port / 10] &= ~(7 << ((port % 10) * 3)); | |
| regs[port / 10] |= (val << ((port % 10) * 3)); | |
| } | |
| void prt_wr(uint32_t *regs, int port, int val){ | |
| if(val) | |
| regs[7] = (1 << port); | |
| else | |
| regs[10] = (1 << port); | |
| } | |
| 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; | |
| } | |
| int main(int argc, char *argv[]){ | |
| int i, j; | |
| int ret = 0; | |
| int fd; | |
| uint32_t *regs; | |
| regs = regs_map(OFF_PRT); | |
| if(regs == MAP_FAILED){ | |
| fprintf(stderr, "fail to get registers map. %s\n", strerror(errno)); | |
| return -1; | |
| } | |
| fnc_wr(regs, AQM_RS, 1); | |
| fd = open("/dev/spidev0.0", O_RDWR); | |
| prt_wr(regs, AQM_RS, 0); | |
| transfer(fd, 0xae); | |
| transfer(fd, 0xa0); | |
| transfer(fd, 0xc8); | |
| transfer(fd, 0xa3); | |
| transfer(fd, 0x2c); usleep(2000); | |
| transfer(fd, 0x2e); usleep(2000); | |
| transfer(fd, 0x2f); | |
| transfer(fd, 0x23); | |
| transfer(fd, 0x81); | |
| transfer(fd, AQM_CONT); | |
| transfer(fd, 0xa4); | |
| transfer(fd, 0x40); | |
| transfer(fd, 0xa6); | |
| transfer(fd, 0xaf); | |
| for(i = 0; i < 6; i++){ | |
| transfer(fd, 0xb0 | i);//page | |
| transfer(fd, 0x10);//column 0 | |
| transfer(fd, 0x00); | |
| prt_wr(regs, AQM_RS, 1); | |
| for(j = 0; j < 64; j++){ | |
| transfer(fd, 0x55); | |
| transfer(fd, 0xaa); | |
| } | |
| prt_wr(regs, AQM_RS, 0); | |
| } | |
| munmap(regs, getpagesize()); | |
| close(fd); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment