|
| 1 | +/* |
| 2 | + ============================================================================ |
| 3 | + Name : application.c |
| 4 | + Author : Daniel Bedard |
| 5 | + Version : |
| 6 | + Copyright : Your copyright notice |
| 7 | + Description : Hello World in C, Ansi-style |
| 8 | + ============================================================================ |
| 9 | + */ |
| 10 | + |
| 11 | +#include <sys/termios.h> |
| 12 | +#include <stdio.h> |
| 13 | +#include <string.h> |
| 14 | +#include <fcntl.h> |
| 15 | +#include <stdlib.h> |
| 16 | +#include <unistd.h> |
| 17 | +#include <stdlib.h> |
| 18 | +#include <stdint.h> |
| 19 | + |
| 20 | +#define VERSION_H 0 |
| 21 | +#define VERSION_L 1 |
| 22 | +#define OVF_FLAG 7 |
| 23 | +#define TIME_FLAG 6 |
| 24 | +#define DONE_FLAG 5 |
| 25 | + |
| 26 | +#define BAUDRATE B1000000 |
| 27 | +#define V_FULLSCALE 26.52 |
| 28 | +#define R_SENSE 0.00422 /* found empirically */ |
| 29 | +#define I_FULLSCALE .10584 |
| 30 | + |
| 31 | +int g_serial_fd; |
| 32 | +FILE *g_serial_fp; |
| 33 | +struct termios g_oldtio, g_newtio; |
| 34 | + |
| 35 | +int powermon_init(); |
| 36 | + |
| 37 | +void cleanup() { |
| 38 | + /* fprintf (stderr, "cleanup\n");*/ |
| 39 | + powermon_init(); |
| 40 | + tcflush(g_serial_fd, TCIFLUSH); |
| 41 | + tcsetattr(g_serial_fd, TCSANOW, &g_oldtio); |
| 42 | + close(g_serial_fd); |
| 43 | +} |
| 44 | + |
| 45 | +int powermon_init() { |
| 46 | + char buffer[32]; |
| 47 | + fprintf(g_serial_fp, "d\n"); |
| 48 | + usleep(100000); |
| 49 | + fflush(g_serial_fp); |
| 50 | + usleep(100000); |
| 51 | + tcflush(g_serial_fd, TCIFLUSH); |
| 52 | + fprintf(g_serial_fp, "\n"); |
| 53 | + fgets(buffer, sizeof(buffer), g_serial_fp); |
| 54 | + return strcmp(buffer, "OK\r\n"); |
| 55 | +} |
| 56 | + |
| 57 | +int powermon_set_mask(uint16_t mask) { |
| 58 | + char buffer[32]; |
| 59 | + unsigned int length; |
| 60 | + unsigned int setval; |
| 61 | + length = sprintf(buffer, "m %u\n", mask); |
| 62 | + fwrite(buffer, 1, length, g_serial_fp); |
| 63 | + fgets(buffer, sizeof(buffer), g_serial_fp); |
| 64 | + sscanf(buffer, "M=%u\r", &setval); |
| 65 | + printf("Mask set to %u.\n", setval); |
| 66 | + fgets(buffer, sizeof(buffer), g_serial_fp); |
| 67 | + return strcmp(buffer, "OK\r\n"); |
| 68 | +} |
| 69 | + |
| 70 | +int powermon_set_samples(uint16_t interval, uint32_t num_samples) { |
| 71 | + char buffer[32]; |
| 72 | + unsigned int length; |
| 73 | + unsigned int set_interval, set_num_samples; |
| 74 | + length = sprintf(buffer, "s %u %u\n", interval, num_samples); |
| 75 | + fwrite(buffer, 1, length, g_serial_fp); |
| 76 | + fgets(buffer, sizeof(buffer), g_serial_fp); |
| 77 | + sscanf(buffer, "S=%u,%u\r", &set_interval, &set_num_samples); |
| 78 | + printf("Sample interval set to %u. %u sample", set_interval, |
| 79 | + set_num_samples); |
| 80 | + if (set_num_samples != 1) |
| 81 | + printf("s"); |
| 82 | + printf(" to be collected.\n"); |
| 83 | + fgets(buffer, sizeof(buffer), g_serial_fp); |
| 84 | + return strcmp(buffer, "OK\r\n"); |
| 85 | +} |
| 86 | + |
| 87 | +int powermon_set_time(uint32_t now) { |
| 88 | + char buffer[32]; |
| 89 | + unsigned int length; |
| 90 | + unsigned int setval; |
| 91 | + length = sprintf(buffer, "t %u\n", now); |
| 92 | + fwrite(buffer, 1, length, g_serial_fp); |
| 93 | + fgets(buffer, sizeof(buffer), g_serial_fp); |
| 94 | + sscanf(buffer, "T=%u\r", &setval); |
| 95 | + printf("Time set to %u.\n", setval); |
| 96 | + fgets(buffer, sizeof(buffer), g_serial_fp); |
| 97 | + return strcmp(buffer, "OK\r\n"); |
| 98 | +} |
| 99 | + |
| 100 | +int powermon_get_samples() { |
| 101 | + unsigned char buffer[4]; |
| 102 | + unsigned int time; /* , reading; */ |
| 103 | + uint16_t voltage, current; |
| 104 | + uint8_t flags, sensor; |
| 105 | + double v_double, i_double; |
| 106 | + fprintf(g_serial_fp, "e\n"); |
| 107 | + for (;;) { |
| 108 | + fread(buffer, 1, 4, g_serial_fp); |
| 109 | + flags = buffer[0]; |
| 110 | + /*reading = (((uint32_t)buffer[0] << 24) | ((uint32_t)buffer[1] << 16) |
| 111 | + | ((uint32_t)buffer[2] << 8) | ((uint32_t)buffer[3])); |
| 112 | + printf("bytes: %#x\n", reading);*/ |
| 113 | + if (!(flags&(1<<TIME_FLAG)) && (flags&(1<<DONE_FLAG))) { |
| 114 | + break; |
| 115 | + } |
| 116 | + if(flags& (1 << OVF_FLAG)) { |
| 117 | + printf("*"); |
| 118 | + } |
| 119 | + if (flags & (1 << TIME_FLAG)) { |
| 120 | + time = (((uint32_t)buffer[0] << 24) | ((uint32_t)buffer[1] << 16) |
| 121 | + | ((uint32_t)buffer[2] << 8) | ((uint32_t)buffer[3])) |
| 122 | + & 0x3FFFFFFF; |
| 123 | + printf("timestamp: %u\n", time); |
| 124 | + } else { |
| 125 | + sensor = (uint8_t)buffer[0] & 0x0F; |
| 126 | + voltage = ((uint16_t)buffer[1] << 4) | ((buffer[3] >> 4) & 0x0F); |
| 127 | + current = ((uint16_t)buffer[2] << 4) | (buffer[3] & 0x0F); |
| 128 | + v_double = (double)voltage / 4096 * V_FULLSCALE; |
| 129 | + i_double = (double)current / 4096 * I_FULLSCALE / R_SENSE; |
| 130 | + printf("sensor = %d, voltage = %fV, current = %fA\n", sensor, v_double, i_double); |
| 131 | + } |
| 132 | + } |
| 133 | + return 0; |
| 134 | +} |
| 135 | + |
| 136 | +int configure_tty(const char *dev) { |
| 137 | + g_serial_fd = open(dev, O_RDWR | O_NOCTTY); |
| 138 | + if (g_serial_fd < 0) { |
| 139 | + perror(dev); |
| 140 | + return 1; |
| 141 | + } |
| 142 | + if ((g_serial_fp = fdopen(g_serial_fd, "r+")) == NULL) { |
| 143 | + perror("fdopen()"); |
| 144 | + return 2; |
| 145 | + } |
| 146 | + tcgetattr(g_serial_fd, &g_oldtio); /* save current port settings */ |
| 147 | + bzero(&g_newtio, sizeof (g_newtio)); |
| 148 | + |
| 149 | + /* |
| 150 | + CRTSCTS == hardware flow control |
| 151 | + CS8 = 8 bits |
| 152 | + CLOCAL = ignore modem control lines |
| 153 | + CREAD = enable receiver |
| 154 | + B38400 = baud rate |
| 155 | + */ |
| 156 | + g_newtio.c_iflag = IGNPAR; |
| 157 | + g_newtio.c_cflag = BAUDRATE | CS8 | CSTOPB | CLOCAL | CREAD; |
| 158 | + g_newtio.c_oflag = 0; |
| 159 | + |
| 160 | + /* set input mode (non-canonical, no echo,...) */ |
| 161 | + g_newtio.c_lflag = 0; |
| 162 | + |
| 163 | + g_newtio.c_cc[VTIME] = 0; /* inter-character timer unused */ |
| 164 | + g_newtio.c_cc[VMIN] = 1; /* blocking read until 1 char received */ |
| 165 | + |
| 166 | + tcflush(g_serial_fd, TCIFLUSH); |
| 167 | + tcsetattr(g_serial_fd, TCSANOW, &g_newtio); |
| 168 | + |
| 169 | + return 0; |
| 170 | +} |
| 171 | + |
| 172 | +int main(int argc, char **argv) { |
| 173 | + char dev[256]; |
| 174 | + uint16_t sensor_mask; |
| 175 | + uint16_t sample_period; |
| 176 | + uint32_t num_samples = 0; |
| 177 | + if ((argc < 4) || (argc > 5)) { |
| 178 | + fprintf (stderr, |
| 179 | + "Usage:\n %s <port_dev> <mask> <sample_pd> [<num_samples>]\n\n", |
| 180 | + argv[0]); |
| 181 | + fprintf (stderr, "Example:\n%s /dev/ttyUSB1 1024 1 200\n", argv[0]); |
| 182 | + return 1; |
| 183 | + } |
| 184 | + strcpy (dev, argv[1]); |
| 185 | + sensor_mask = atoi(argv[2]); |
| 186 | + sample_period = atoi(argv[3]); |
| 187 | + if(argc == 5) { |
| 188 | + num_samples = atoi(argv[4]); |
| 189 | + } |
| 190 | + if (configure_tty(dev)) |
| 191 | + exit(1); |
| 192 | + if (powermon_init()) |
| 193 | + exit(1); |
| 194 | + if (powermon_set_mask(sensor_mask)) |
| 195 | + exit(1); |
| 196 | + if (powermon_set_samples(sample_period, num_samples)) |
| 197 | + exit(1); |
| 198 | + if (powermon_set_time(1)) |
| 199 | + exit(1); |
| 200 | + powermon_get_samples(); |
| 201 | + atexit(cleanup); |
| 202 | + return 0; |
| 203 | + |
| 204 | +} |
0 commit comments