/
tps56520.c
88 lines (68 loc) · 1.85 KB
/
tps56520.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
Copyright (c) 2015-2016 NewAE Technology Inc. All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <asf.h>
#include "tps56520.h"
#include "i2c_util.h"
#define TPS56520_ADDR 0x34
uint8_t TPS_CONNECTED = 0;
unsigned char checkoddparity(unsigned char p);
/* Is current byte odd-parity already? */
unsigned char checkoddparity(unsigned char p)
{
p = p ^ (p >> 4 | p << 4);
p = p ^ (p >> 2);
p = p ^ (p >> 1);
return p & 1;
}
/* Init the TPS56520 chip, set to 1.00V output */
bool tps56520_init(void)
{
for(int retry = 3; retry > 0; retry--){
if(tps56520_set(1100)){
return true;
}
}
return false;
}
bool tps56520_detect(void)
{
return twi_probe(TWI0, TPS56520_ADDR);
}
/* Set voltage in mV for FPGA VCC_INT Voltage */
bool tps56520_set(uint16_t mv_output)
{
/* Validate output voltage is in range */
if ((mv_output < 600) || (mv_output > 1800)){
return false;
}
/* Avoid frying FPGA */
if (mv_output > 1200){
return false;
}
uint8_t setting = (mv_output - 600) / 10;
if (!checkoddparity(setting)){
setting |= 1<<7;
}
if (i2c_write(TPS56520_ADDR, 0, &setting, 1)) {
return false;
}
uint8_t volt_read;
if (i2c_read(TPS56520_ADDR, 0, &volt_read, 1)) {
return false;
}
if (volt_read == setting){
return true;
}
return false;
}