blob: 212307b15fc655f8b15b67efd93bae1120d9a0e2 [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.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
Martin Rothb54d6ba2015-09-29 14:49:37 -060015 * Foundation, Inc.
Stefan Reinauerc566d202015-08-25 09:52:42 -070016 */
17
18#include <stdio.h>
19#include <string.h>
20#include "em100.h"
21
22/* System level operations */
23
24/**
25 * get_version: fetch firmware version information
26 * @param em100: initialized em100 device structure
27 *
28 * out(16 bytes): 0x10 0 .. 0
29 * in(len + 4 bytes): 0x04 fpga_major fpga_minor mcu_major mcu_minor
30 */
31int get_version(struct em100 *em100)
32{
33 unsigned char cmd[16];
34 unsigned char data[512];
35 memset(cmd, 0, 16);
36 cmd[0] = 0x10; /* version */
37 if (!send_cmd(em100->dev, cmd)) {
38 return 0;
39 }
40 int len = get_response(em100->dev, data, 512);
41 if ((len == 5) && (data[0] == 4)) {
42 em100->mcu = (data[3] << 8) | data[4];
43 em100->fpga = (data[1] << 8) | data[2];
44 return 1;
45 }
46 return 0;
47}
48
49int set_voltage(struct em100 *em100, set_voltage_channel_t channel, int mV)
50{
51 unsigned char cmd[16];
52
53 if ((channel == out_buffer_vcc) &&
54 (mV != 18 && mV != 25 && mV != 33)) {
Martin Rothb54d6ba2015-09-29 14:49:37 -060055 printf("Error: For Buffer VCC, voltage needs to be 1.8V, 2.5V"
56 " or 3.3V.\n");
Stefan Reinauerc566d202015-08-25 09:52:42 -070057 return 0;
58 }
59
60 memset(cmd, 0, 16);
61 cmd[0] = 0x11; /* set voltage */
62 cmd[1] = channel;
63 cmd[2] = mV >> 8;
64 cmd[3] = mV & 0xff;
65 if (!send_cmd(em100->dev, cmd)) {
66 return 0;
67 }
68 return 1;
69}
70
71int get_voltage(struct em100 *em100, get_voltage_channel_t channel)
72{
73 unsigned char cmd[16];
74 unsigned char data[512];
75 int voltage = 0;
76
77 memset(cmd, 0, 16);
78 cmd[0] = 0x12; /* measure voltage */
79 cmd[1] = channel;
80 if (!send_cmd(em100->dev, cmd)) {
81 return 0;
82 }
83 int len = get_response(em100->dev, data, 512);
84 if ((len == 3) && (data[0] == 2)) {
85 voltage = (data[1] << 8) + data[2];
86 return voltage;
87 }
88 return 0;
89}
90
91int set_led(struct em100 *em100, led_state_t led_state)
92{
93 unsigned char cmd[16];
94 memset(cmd, 0, 16);
95 cmd[0] = 0x13; /* set LED */
96 cmd[1] = led_state;
97 if (!send_cmd(em100->dev, cmd)) {
98 return 0;
99 }
100 return 1;
101}
102