| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| |
| 5 |
|
| #include <stdio.h> |
| 6 |
|
| #include <stdlib.h> |
| 7 |
|
| #include <stdint.h> |
| 8 |
|
| #include <string.h> |
| 9 |
|
| #include <unistd.h> |
| 10 |
|
| #include <fcntl.h> |
| 11 |
|
| #include <errno.h> |
| 12 |
|
| #include <termios.h> |
| 13 |
|
| #include <sys/ioctl.h> |
| 14 |
|
| |
| 15 |
|
| #define POLY 0x8408 |
| 16 |
|
| |
| 17 |
0 |
| unsigned short crc16(char *data_p, unsigned short length){ |
| 18 |
|
| unsigned char i; |
| 19 |
|
| unsigned int data; |
| 20 |
0 |
| unsigned int crc = 0xffff; |
| 21 |
|
| |
| 22 |
0 |
▶ | if (length == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (length == 0) |
0 |
0 |
0 |
|
| 23 |
0 |
| return (~crc); |
| 24 |
|
| do { |
| 25 |
0 |
▶ | for (i=0, data=(unsigned int)0xff & *data_p++; i < 8; i++, data >>= 1){ |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0, data=(unsigned int)0xff & *data_p++; i < 8; i++, data >>= 1){ |
0 |
0 |
0 |
|
| 26 |
0 |
▶ | if ((crc & 0x0001) ^ (data & 0x0001)) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((crc & 0x0001) ^ (data & 0x0001)) |
0 |
0 |
0 |
|
| 27 |
0 |
| crc = (crc >> 1) ^ POLY; |
| 28 |
0 |
| else crc >>= 1; |
| 29 |
|
| } |
| 30 |
0 |
▶ | } while (--length); |
Branch: 1 branch
| Branch | True | False | Total |
| } while (--length); |
0 |
0 |
0 |
|
| 31 |
|
| |
| 32 |
0 |
| crc = ~crc; |
| 33 |
0 |
| data = crc; |
| 34 |
0 |
| crc = (crc << 8) | (data >> 8 & 0xff); |
| 35 |
|
| |
| 36 |
0 |
| return crc; |
| 37 |
|
| } |
| 38 |
|
| |
| 39 |
0 |
| void tty_close (int fd){ |
| 40 |
0 |
| close (fd); |
| 41 |
0 |
| } |
| 42 |
|
| |
| 43 |
0 |
| int tty_available (int fd){ |
| 44 |
|
| int bytes_available; |
| 45 |
0 |
| ioctl(fd, FIONREAD, &bytes_available); |
| 46 |
0 |
| return bytes_available; |
| 47 |
|
| } |
| 48 |
|
| |
| 49 |
0 |
| int tty_putc(int fd, char b){ |
| 50 |
0 |
| int n = write(fd,&b,1); |
| 51 |
0 |
▶ | if( n!=1) |
Branch: 1 branch
| Branch | True | False | Total |
| if( n!=1) |
0 |
0 |
0 |
|
| 52 |
0 |
| return -1; |
| 53 |
0 |
| return 0; |
| 54 |
|
| } |
| 55 |
|
| |
| 56 |
0 |
| int tty_puts(int fd, const char* str){ |
| 57 |
0 |
| int len = strlen(str); |
| 58 |
0 |
| int n = write(fd, str, len); |
| 59 |
0 |
▶ | if( n!=len ) |
Branch: 1 branch
| Branch | True | False | Total |
| if( n!=len ) |
0 |
0 |
0 |
|
| 60 |
0 |
| return -1; |
| 61 |
0 |
| return 0; |
| 62 |
|
| } |
| 63 |
|
| |
| 64 |
0 |
| int tty_getc (int fd){ |
| 65 |
|
| uint8_t x; |
| 66 |
|
| |
| 67 |
0 |
▶ | if (read (fd, &x, 1) != 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (read (fd, &x, 1) != 1) |
0 |
0 |
0 |
|
| 68 |
0 |
| return -1; |
| 69 |
|
| |
| 70 |
0 |
| return ((int)x) & 0xFF; |
| 71 |
|
| } |
| 72 |
|
| |
| 73 |
0 |
| int tty_open(const char *serialport, int baud){ |
| 74 |
|
| struct termios toptions; |
| 75 |
|
| int fd; |
| 76 |
|
| |
| 77 |
0 |
| fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY); |
| 78 |
|
| |
| 79 |
0 |
▶ | if (fd == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd == -1) { |
0 |
0 |
0 |
|
| 80 |
0 |
| perror("open(): Unable to open port "); |
| 81 |
0 |
| return -1; |
| 82 |
|
| } |
| 83 |
|
| |
| 84 |
0 |
▶ | if (tcgetattr(fd, &toptions) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tcgetattr(fd, &toptions) < 0) { |
0 |
0 |
0 |
|
| 85 |
0 |
| perror("init_serialport: Couldn't get term attributes"); |
| 86 |
0 |
| return -1; |
| 87 |
|
| } |
| 88 |
0 |
| speed_t brate = baud; |
| 89 |
0 |
| switch(baud) { |
| 90 |
0 |
| case 4800: brate=B4800; break; |
| 91 |
0 |
| case 9600: brate=B9600; break; |
| 92 |
|
| #ifdef B14400 |
| 93 |
|
| case 14400: brate=B14400; break; |
| 94 |
|
| #endif |
| 95 |
0 |
| case 19200: brate=B19200; break; |
| 96 |
|
| #ifdef B28800 |
| 97 |
|
| case 28800: brate=B28800; break; |
| 98 |
|
| #endif |
| 99 |
0 |
| case 38400: brate=B38400; break; |
| 100 |
0 |
| case 57600: brate=B57600; break; |
| 101 |
0 |
| case 115200: brate=B115200; break; |
| 102 |
|
| } |
| 103 |
|
| |
| 104 |
0 |
| cfsetispeed(&toptions, brate); |
| 105 |
0 |
| cfsetospeed(&toptions, brate); |
| 106 |
|
| |
| 107 |
|
| // 8N1 |
| 108 |
0 |
| toptions.c_cflag &= ~PARENB; |
| 109 |
0 |
| toptions.c_cflag &= ~CSTOPB; |
| 110 |
0 |
| toptions.c_cflag &= ~CSIZE; |
| 111 |
0 |
| toptions.c_cflag |= CS8; |
| 112 |
|
| // no flow control |
| 113 |
0 |
| toptions.c_cflag &= ~CRTSCTS; |
| 114 |
|
| |
| 115 |
0 |
| toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines |
| 116 |
0 |
| toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl |
| 117 |
|
| |
| 118 |
0 |
| toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw |
| 119 |
0 |
| toptions.c_oflag &= ~OPOST; // make raw |
| 120 |
|
| |
| 121 |
0 |
| toptions.c_cc[VMIN] = 0; |
| 122 |
0 |
| toptions.c_cc[VTIME] = 10; |
| 123 |
|
| |
| 124 |
0 |
▶ | if( tcsetattr(fd, TCSANOW, &toptions) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if( tcsetattr(fd, TCSANOW, &toptions) < 0) { |
0 |
0 |
0 |
|
| 125 |
0 |
| perror("init_serialport: Couldn't set term attributes"); |
| 126 |
0 |
| return -1; |
| 127 |
|
| } |
| 128 |
|
| |
| 129 |
0 |
| return fd; |
| 130 |
|
| } |
| 131 |
|
| |
| 132 |
|
| MODULE = RPi::Serial PACKAGE = RPi::Serial |
| 133 |
|
| |
| 134 |
|
| PROTOTYPES: DISABLE |
| 135 |
|
| |
| 136 |
|
| int |
| 137 |
|
| tty_available (fd) |
| 138 |
|
| int fd |
| 139 |
|
| |
| 140 |
|
| int |
| 141 |
|
| tty_putc (fd, b) |
| 142 |
|
| int fd |
| 143 |
|
| char b |
| 144 |
|
| |
| 145 |
|
| int |
| 146 |
|
| tty_puts (fd, str) |
| 147 |
|
| int fd |
| 148 |
|
| const char *str |
| 149 |
|
| |
| 150 |
|
| int |
| 151 |
|
| tty_getc (fd) |
| 152 |
|
| int fd |
| 153 |
|
| |
| 154 |
|
| void |
| 155 |
|
| tty_gets (fd, nbytes) |
| 156 |
|
| int fd |
| 157 |
|
| int nbytes |
| 158 |
|
| PREINIT: |
| 159 |
|
| char *buf; |
| 160 |
0 |
| int got = 0; |
| 161 |
|
| int flags; |
| 162 |
|
| int result; |
| 163 |
|
| PPCODE: |
| 164 |
0 |
▶ | if (nbytes < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nbytes < 0) |
0 |
0 |
0 |
|
| 165 |
0 |
| croak("tty_gets: nbytes must be a non-negative integer"); |
| 166 |
|
| /* tty_open() opens with O_NDELAY (non-blocking), which defeats the |
| 167 |
|
| port's VMIN/VTIME read timeout. Clear it so a read blocks up to |
| 168 |
|
| that timeout instead of returning EAGAIN immediately. */ |
| 169 |
0 |
| flags = fcntl(fd, F_GETFL, 0); |
| 170 |
0 |
▶ | if (flags != -1 && (flags & O_NONBLOCK)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (flags != -1 && (flags & O_NONBLOCK)) |
0 |
0 |
0 |
| if (flags != -1 && (flags & O_NONBLOCK)) |
0 |
0 |
0 |
|
| 171 |
0 |
| fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); |
| 172 |
0 |
▶ | Newx(buf, nbytes > 0 ? nbytes : 1, char); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(buf, nbytes > 0 ? nbytes : 1, char); |
0 |
0 |
0 |
|
| 173 |
0 |
▶ | while (got < nbytes) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (got < nbytes) { |
0 |
0 |
0 |
|
| 174 |
0 |
| result = read(fd, buf + got, nbytes - got); |
| 175 |
0 |
▶ | if (result > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (result > 0) { |
0 |
0 |
0 |
|
| 176 |
0 |
| got += result; |
| 177 |
0 |
| continue; |
| 178 |
|
| } |
| 179 |
0 |
▶ | if (result == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (result == 0) |
0 |
0 |
0 |
|
| 180 |
0 |
| break; /* VTIME timeout or EOF */ |
| 181 |
0 |
▶ | if (errno == EINTR) |
Branch: 1 branch
| Branch | True | False | Total |
| if (errno == EINTR) |
0 |
0 |
0 |
|
| 182 |
0 |
| continue; /* interrupted by a signal; retry */ |
| 183 |
0 |
| Safefree(buf); |
| 184 |
0 |
| croak("tty_gets: read error: %s", strerror(errno)); |
| 185 |
|
| } |
| 186 |
0 |
| ST(0) = sv_2mortal(newSVpvn(buf, got)); |
| 187 |
0 |
| Safefree(buf); |
| 188 |
0 |
| XSRETURN(1); |
| 189 |
|
| |
| 190 |
|
| int |
| 191 |
|
| tty_open (serialport, baud) |
| 192 |
|
| const char *serialport |
| 193 |
|
| int baud |
| 194 |
|
| |
| 195 |
|
| void |
| 196 |
|
| tty_close (fd) |
| 197 |
|
| int fd |
| 198 |
|
| |
| 199 |
|
| unsigned short |
| 200 |
|
| crc16(data_p, length) |
| 201 |
|
| char *data_p |
| 202 |
|
| unsigned short length |