blob: 9286118bdd64f37e0c8d12a4065b13eb3f0436d6 [file] [log] [blame]
Stefan Reinauerc566d202015-08-25 09:52:42 -07001/*
2 * Copyright 2012-2015 Google Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
Stefan Reinauerc566d202015-08-25 09:52:42 -070012 */
13
14#include <stdio.h>
15#include <string.h>
16#include "em100.h"
17
18/* System level operations */
19
20/**
21 * get_version: fetch firmware version information
22 * @param em100: initialized em100 device structure
23 *
24 * out(16 bytes): 0x10 0 .. 0
25 * in(len + 4 bytes): 0x04 fpga_major fpga_minor mcu_major mcu_minor
26 */
27int get_version(struct em100 *em100)
28{
29 unsigned char cmd[16];
30 unsigned char data[512];
31 memset(cmd, 0, 16);
32 cmd[0] = 0x10; /* version */
33 if (!send_cmd(em100->dev, cmd)) {
34 return 0;
35 }
36 int len = get_response(em100->dev, data, 512);
37 if ((len == 5) && (data[0] == 4)) {
38 em100->mcu = (data[3] << 8) | data[4];
39 em100->fpga = (data[1] << 8) | data[2];
40 return 1;
41 }
42 return 0;
43}
44
45int set_voltage(struct em100 *em100, set_voltage_channel_t channel, int mV)
46{
47 unsigned char cmd[16];
48
49 if ((channel == out_buffer_vcc) &&
50 (mV != 18 && mV != 25 && mV != 33)) {
Martin Rothb54d6ba2015-09-29 14:49:37 -060051 printf("Error: For Buffer VCC, voltage needs to be 1.8V, 2.5V"
52 " or 3.3V.\n");
Stefan Reinauerc566d202015-08-25 09:52:42 -070053 return 0;
54 }
55
56 memset(cmd, 0, 16);
57 cmd[0] = 0x11; /* set voltage */
58 cmd[1] = channel;
59 cmd[2] = mV >> 8;
60 cmd[3] = mV & 0xff;
61 if (!send_cmd(em100->dev, cmd)) {
62 return 0;
63 }
64 return 1;
65}
66
67int get_voltage(struct em100 *em100, get_voltage_channel_t channel)
68{
69 unsigned char cmd[16];
70 unsigned char data[512];
71 int voltage = 0;
72
73 memset(cmd, 0, 16);
74 cmd[0] = 0x12; /* measure voltage */
75 cmd[1] = channel;
76 if (!send_cmd(em100->dev, cmd)) {
77 return 0;
78 }
79 int len = get_response(em100->dev, data, 512);
80 if ((len == 3) && (data[0] == 2)) {
81 voltage = (data[1] << 8) + data[2];
Stefan Reinauerad2eac82016-04-05 13:42:22 -070082 switch (channel) {
83 case in_v1_2:
84 case in_e_vcc:
85 case in_ref_plus:
86 case in_ref_minus:
87 /* each step is 5V/4096 (about 1.22mV) */
88 voltage = voltage * 12207 / 10000;
89 break;
90 default:
91 /* each step is 5V/1024 (about 4.88mV) */
92 voltage = voltage * 48828 / 10000;
93 break;
94 }
Stefan Reinauerc566d202015-08-25 09:52:42 -070095 return voltage;
96 }
97 return 0;
98}
99
100int set_led(struct em100 *em100, led_state_t led_state)
101{
102 unsigned char cmd[16];
103 memset(cmd, 0, 16);
104 cmd[0] = 0x13; /* set LED */
105 cmd[1] = led_state;
106 if (!send_cmd(em100->dev, cmd)) {
107 return 0;
108 }
109 return 1;
110}
111