1 /* $NetBSD: icom.c,v 1.1.1.1 2009/12/13 16:55:03 kardel Exp $ */ 2 3 /* 4 * Program to control ICOM radios 5 * 6 * This is a ripoff of the utility routines in the ICOM software 7 * distribution. The only function provided is to load the radio 8 * frequency. All other parameters must be manually set before use. 9 */ 10 #include "icom.h" 11 #include <unistd.h> 12 #include <stdio.h> 13 #include <fcntl.h> 14 #include <errno.h> 15 16 #include "ntp_tty.h" 17 #include "l_stdlib.h" 18 19 /* 20 * Packet routines 21 * 22 * These routines send a packet and receive the response. If an error 23 * (collision) occurs on transmit, the packet is resent. If an error 24 * occurs on receive (timeout), all input to the terminating FI is 25 * discarded and the packet is resent. If the maximum number of retries 26 * is not exceeded, the program returns the number of octets in the user 27 * buffer; otherwise, it returns zero. 28 * 29 * ICOM frame format 30 * 31 * Frames begin with a two-octet preamble PR-PR followyd by the 32 * transceiver address RE, controller address TX, control code CN, zero 33 * or more data octets DA (depending on command), and terminator FI. 34 * Since the bus is bidirectional, every octet output is echoed on 35 * input. Every valid frame sent is answered with a frame in the same 36 * format, but with the RE and TX fields interchanged. The CN field is 37 * set to NAK if an error has occurred. Otherwise, the data are returned 38 * in this and following DA octets. If no data are returned, the CN 39 * octet is set to ACK. 40 * 41 * +------+------+------+------+------+--//--+------+ 42 * | PR | PR | RE | TX | CN | DA | FI | 43 * +------+------+------+------+------+--//--+------+ 44 */ 45 /* 46 * Scraps 47 */ 48 #define DICOM /dev/icom/ /* ICOM port link */ 49 50 /* 51 * Local function prototypes 52 */ 53 static void doublefreq (double, u_char *, int); 54 55 56 /* 57 * icom_freq(fd, ident, freq) - load radio frequency 58 */ 59 int 60 icom_freq( /* returns 0 (ok), EIO (error) */ 61 int fd, /* file descriptor */ 62 int ident, /* ICOM radio identifier */ 63 double freq /* frequency (MHz) */ 64 ) 65 { 66 u_char cmd[] = {PAD, PR, PR, 0, TX, V_SFREQ, 0, 0, 0, 0, FI, 67 FI}; 68 int temp; 69 cmd[3] = ident; 70 if (ident == IC735) 71 temp = 4; 72 else 73 temp = 5; 74 doublefreq(freq * 1e6, &cmd[6], temp); 75 temp = write(fd, cmd, temp + 7); 76 return (0); 77 } 78 79 80 /* 81 * doublefreq(freq, y, len) - double to ICOM frequency with padding 82 */ 83 static void 84 doublefreq( /* returns void */ 85 double freq, /* frequency */ 86 u_char *x, /* radio frequency */ 87 int len /* length (octets) */ 88 ) 89 { 90 int i; 91 char s1[11]; 92 char *y; 93 94 sprintf(s1, " %10.0f", freq); 95 y = s1 + 10; 96 i = 0; 97 while (*y != ' ') { 98 x[i] = *y-- & 0x0f; 99 x[i] = x[i] | ((*y-- & 0x0f) << 4); 100 i++; 101 } 102 for (; i < len; i++) 103 x[i] = 0; 104 x[i] = FI; 105 } 106 107 /* 108 * icom_open() - open and initialize serial interface 109 * 110 * This routine opens the serial interface for raw transmission; that 111 * is, character-at-a-time, no stripping, checking or monkeying with the 112 * bits. For Unix, an input operation ends either with the receipt of a 113 * character or a 0.5-s timeout. 114 */ 115 int 116 icom_init( 117 char *device, /* device name/link */ 118 int speed, /* line speed */ 119 int trace /* trace flags */ ) 120 { 121 TTY ttyb; 122 int fd, flags; 123 124 flags = trace; 125 fd = open(device, O_RDWR, 0777); 126 if (fd < 0) 127 return (fd); 128 129 tcgetattr(fd, &ttyb); 130 ttyb.c_iflag = 0; /* input modes */ 131 ttyb.c_oflag = 0; /* output modes */ 132 ttyb.c_cflag = IBAUD|CS8|CLOCAL; /* control modes (no read) */ 133 ttyb.c_lflag = 0; /* local modes */ 134 ttyb.c_cc[VMIN] = 0; /* min chars */ 135 ttyb.c_cc[VTIME] = 5; /* receive timeout */ 136 cfsetispeed(&ttyb, (u_int)speed); 137 cfsetospeed(&ttyb, (u_int)speed); 138 tcsetattr(fd, TCSANOW, &ttyb); 139 return (fd); 140 } 141 142 /* end program */ 143