liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <malloc.h> |
| 3 | #include <string.h> |
| 4 | #include <linux/types.h> |
| 5 | #include <linux/spi/spidev.h> |
| 6 | |
| 7 | #include <stdint.h> |
| 8 | #include <unistd.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <getopt.h> |
| 12 | #include <fcntl.h> |
| 13 | #include <sys/ioctl.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/spi/spidev.h> |
| 16 | #include <errno.h> |
| 17 | |
| 18 | #include "ql/ql_spi.h" |
| 19 | #include "mbtk_log.h" |
| 20 | |
| 21 | #if 0 |
| 22 | static const char *device = "/dev/spidev1.0\0"; |
| 23 | static uint8_t mode = 3; /* SPI通信使用全双工,设置CPOL=0,CPHA=0。 */ |
| 24 | static uint8_t bits = 8; /* 8bits读写,MSB first。*/ |
| 25 | static uint32_t speed = 100 * 1000;/* 设置0.5M传输速度 */ |
| 26 | static uint16_t delay = 500; |
| 27 | |
| 28 | int SPI_Transfer(const uint8_t *TxBuf, uint8_t *RxBuf, int len) |
| 29 | { |
| 30 | int ret; |
| 31 | int fd = g_SPI_Fd; |
| 32 | |
| 33 | |
| 34 | struct spi_ioc_transfer tr ; |
| 35 | memset(&tr,0x00,sizeof(tr)); |
| 36 | tr.tx_buf = (unsigned long) TxBuf, |
| 37 | tr.rx_buf = (unsigned long) RxBuf, |
| 38 | tr.len =len, |
| 39 | tr.delay_usecs = delay; |
| 40 | tr.speed_hz=speed; |
| 41 | tr.bits_per_word=bits; |
| 42 | ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); |
| 43 | if (ret < 1) |
| 44 | { |
| 45 | printf("can't send spi message"); |
| 46 | } |
| 47 | else |
| 48 | { |
| 49 | //printf("Send spi message OK %d\n",RxBuf[0]); |
| 50 | } |
| 51 | return 1; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * 功 能:关闭SPI模块 |
| 56 | */ |
| 57 | int SPI_Close(void) |
| 58 | { |
| 59 | int fd = g_SPI_Fd; |
| 60 | |
| 61 | |
| 62 | if (fd == 0) /* SPI是否已经打开*/ |
| 63 | return 0; |
| 64 | close(fd); |
| 65 | g_SPI_Fd = 0; |
| 66 | |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | int SPI_Write(uint8_t *TxBuf, int len) |
| 72 | { |
| 73 | int ret; |
| 74 | int fd = g_SPI_Fd; |
| 75 | |
| 76 | printf("fd : %d\n",fd); |
| 77 | ret = write(fd, TxBuf, len); |
| 78 | if (ret < 0) |
| 79 | printf("SPI Write errorn"); |
| 80 | |
| 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | int SPI_Open(void) |
| 85 | { |
| 86 | int fd; |
| 87 | int ret = 0; |
| 88 | |
| 89 | printf("open spi dev:%s \r\n", device); |
| 90 | fd = open(device, O_RDWR); |
| 91 | if (fd < 0) |
| 92 | { |
| 93 | printf("can't open device \n"); |
| 94 | return -1; |
| 95 | } |
| 96 | else |
| 97 | printf("SPI - Open Succeed. Start Init SPI...n\n"); |
| 98 | |
| 99 | /* |
| 100 | * spi mode |
| 101 | */ |
| 102 | ret = ioctl(fd, SPI_IOC_WR_MODE, &mode); |
| 103 | if (ret == -1) |
| 104 | printf("can't set spi mode\n"); |
| 105 | |
| 106 | |
| 107 | ret = ioctl(fd, SPI_IOC_RD_MODE, &mode); |
| 108 | if (ret == -1) |
| 109 | printf("can't get spi mode\n"); |
| 110 | |
| 111 | |
| 112 | /* |
| 113 | * bits per word |
| 114 | */ |
| 115 | ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits); |
| 116 | if (ret == -1) |
| 117 | printf("can't set bits per word\n"); |
| 118 | |
| 119 | |
| 120 | ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits); |
| 121 | if (ret == -1) |
| 122 | printf("can't get bits per word\n"); |
| 123 | |
| 124 | |
| 125 | /* |
| 126 | * max speed hz |
| 127 | */ |
| 128 | ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); |
| 129 | if (ret == -1) |
| 130 | printf("can't set max speed hz\n"); |
| 131 | |
| 132 | |
| 133 | ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed); |
| 134 | if (ret == -1) |
| 135 | printf("can't get max speed hz\n"); |
| 136 | |
| 137 | g_SPI_Fd=fd; |
| 138 | return ret; |
| 139 | } |
| 140 | #endif |
| 141 | |
| 142 | /** |
| 143 | * Function: Ql_SPI_Init |
| 144 | * Description: spi init |
| 145 | * Parameters: dev_name---device name |
| 146 | * mode---spi mode |
| 147 | * bits---spi per word |
| 148 | * speed---spi transfer clock |
| 149 | * Return: spi fd |
| 150 | **/ |
| 151 | int Ql_SPI_Init(const char *dev_name, SPI_MODE mode, unsigned char bits, SPI_SPEED speed) |
| 152 | { |
| 153 | int fd; |
| 154 | int ret = 0; |
| 155 | |
| 156 | LOGD("open spi dev:%s.", dev_name); |
| 157 | fd = open(dev_name, O_RDWR); |
| 158 | if (fd < 0) |
| 159 | { |
| 160 | LOGE("can't open device, errno - %d", errno); |
| 161 | return -1; |
| 162 | } |
| 163 | else |
| 164 | LOGD("SPI - Open Succeed. Start Init SPI..."); |
| 165 | |
| 166 | /* |
| 167 | * spi mode |
| 168 | */ |
| 169 | ret = ioctl(fd, SPI_IOC_WR_MODE, &mode); |
| 170 | if (ret == -1) { |
| 171 | LOGE("can't set spi mode"); |
| 172 | goto error; |
| 173 | } |
| 174 | |
| 175 | #if 0 |
| 176 | ret = ioctl(fd, SPI_IOC_RD_MODE, &mode); |
| 177 | if (ret == -1) { |
| 178 | LOGE("can't get spi mode"); |
| 179 | goto error; |
| 180 | } |
| 181 | #endif |
| 182 | |
| 183 | /* |
| 184 | * bits per word |
| 185 | */ |
| 186 | ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits); |
| 187 | if (ret == -1) { |
| 188 | LOGE("can't set bits per word"); |
| 189 | goto error; |
| 190 | } |
| 191 | |
| 192 | #if 0 |
| 193 | ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits); |
| 194 | if (ret == -1) { |
| 195 | LOGE("can't get bits per word"); |
| 196 | goto error; |
| 197 | } |
| 198 | #endif |
| 199 | |
| 200 | /* |
| 201 | * max speed hz |
| 202 | */ |
| 203 | ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); |
| 204 | if (ret == -1) { |
| 205 | LOGE("can't set max speed hz"); |
| 206 | goto error; |
| 207 | } |
| 208 | |
| 209 | #if 0 |
| 210 | ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed); |
| 211 | if (ret == -1) { |
| 212 | LOGE("can't get max speed hz"); |
| 213 | goto error; |
| 214 | } |
| 215 | #endif |
| 216 | |
| 217 | return fd; |
| 218 | error: |
| 219 | if(fd > 0) |
| 220 | { |
| 221 | close(fd); |
| 222 | } |
| 223 | return -1; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Function: Ql_SPI_DeInit |
| 228 | * Description: spi deinit |
| 229 | * Parameters: fd---spi fd |
| 230 | * Return: |
| 231 | */ |
| 232 | int Ql_SPI_DeInit(int fd) |
| 233 | { |
| 234 | if (fd <= 0) |
| 235 | return -1; |
| 236 | |
| 237 | close(fd); |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Function: Ql_SPI_Write_Read |
| 243 | * Description: spi write read function |
| 244 | * Parameters: fd---spi fd |
| 245 | * w_buf---write buffer |
| 246 | * r_buf---read buffer |
| 247 | * len---spi transfer length |
| 248 | * Return: 0---transfer success |
| 249 | * other---failed |
| 250 | **/ |
| 251 | int Ql_SPI_Write_Read(int fd, unsigned char *w_buf, unsigned char *r_buf, unsigned int len) |
| 252 | { |
| 253 | int ret; |
| 254 | struct spi_ioc_transfer tr ; |
| 255 | memset(&tr,0x00,sizeof(tr)); |
| 256 | tr.tx_buf = (unsigned long) w_buf, |
| 257 | tr.rx_buf = (unsigned long) r_buf, |
| 258 | tr.len =len, |
| 259 | tr.delay_usecs = 500; |
| 260 | //tr.speed_hz=speed; |
| 261 | //tr.bits_per_word=bits; |
| 262 | ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); |
| 263 | if (ret < 1) |
| 264 | { |
| 265 | LOGE("can't send spi message"); |
| 266 | return -1; |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | return 0; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | |
| 275 | #if 0 |
| 276 | int main(int argc, char *argv[]) |
| 277 | { |
| 278 | |
| 279 | char send_data[64] = {0}; |
| 280 | char read_data[64] = {0}; |
| 281 | char crc = 0; |
| 282 | int i = 0; |
| 283 | int j = 0; |
| 284 | |
| 285 | system("echo PB6 > /sys/kernel/debug/sunxi_pinctrl/sunxi_pin"); |
| 286 | system("echo PB6 1 > /sys/kernel/debug/sunxi_pinctrl/function"); |
| 287 | system("echo PB6 0 > /sys/kernel/debug/sunxi_pinctrl/data"); |
| 288 | |
| 289 | /* spi 初始化程序 */ |
| 290 | SPI_Open(); |
| 291 | |
| 292 | send_data[0] = 0x55; |
| 293 | send_data[1] = 0x00; |
| 294 | send_data[2] = 0x84; |
| 295 | send_data[3] = 0x00; |
| 296 | send_data[4] = 0x08; |
| 297 | send_data[5] = 0x00; |
| 298 | send_data[6] = 0x00; |
| 299 | |
| 300 | crc = send_data[1]; |
| 301 | for (i = 2; i < 7; i++) |
| 302 | { |
| 303 | crc ^= send_data[i]; |
| 304 | } |
| 305 | crc = ~crc; |
| 306 | |
| 307 | send_data[7] = crc; |
| 308 | |
| 309 | printf("send data:"); |
| 310 | for (i = 0; i < 8; i++) |
| 311 | { |
| 312 | printf("%#x, ", send_data[i]); |
| 313 | } |
| 314 | printf("\n"); |
| 315 | |
| 316 | /* spi 发送数据 */ |
| 317 | SPI_Transfer(send_data,read_data,8); |
| 318 | |
| 319 | printf("read data:"); |
| 320 | for (j = 0; j < 20; j++) |
| 321 | { |
| 322 | printf("%#x, ", read_data[j]); |
| 323 | } |
| 324 | |
| 325 | usleep(10000); |
| 326 | |
| 327 | |
| 328 | memset(read_data, 0, sizeof(read_data)); |
| 329 | memset(send_data, 0, sizeof(send_data)); |
| 330 | |
| 331 | /* spi 读取数据 */ |
| 332 | SPI_Transfer(send_data,read_data,16); |
| 333 | |
| 334 | printf("read data:"); |
| 335 | for (j = 0; j < 20; j++) |
| 336 | { |
| 337 | printf("%#x, ", read_data[j]); |
| 338 | } |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | #endif |