aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/e300/battery/i2c_twi.c
blob: f2c0491d370f4c21afdfa11d29fc9f2b9d3e4a1c (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* USRP E310 Firmware Atmel AVR TWI driver
 * Copyright (C) 2014 Ettus Research
 * This file is part of the USRP E310 Firmware
 * The USRP E310 Firmware 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 2 of the License, or
 * (at your option) any later version.
 * The USRP E310 Firmware 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 the USRP E310 Firmware. If not, see <http://www.gnu.org/licenses/>.
 */

#include "i2c_twi.h"
#include "mcu_settings.h"
#include "utils.h"

#include <stdbool.h>
#include <avr/io.h>
#include <util/twi.h>
#include <util/delay.h>

static const uint8_t I2C_TIMEOUT = 10;

static inline uint8_t I2C_READ_ADDR(const uint8_t x)
{
	return (x << 1) | 0x1;
}

static inline uint8_t I2C_WRITE_ADDR(const uint8_t x)
{
	return (x << 1) & 0xfe;
}

void i2c_twi_init_calc(uint32_t rate)
{
	uint8_t twbr;
	twbr = ((F_CPU/rate)-16)/2;

	TWBR = twbr;

	PRR &= ~BIT(PRTWI);

	/* www.mikrocontroller.net/articles/AVR_TWI says this might help ... */
	TWCR &= ~(BIT(TWSTO) | BIT(TWEN));
	TWCR |=	BIT(TWEN);
}

void i2c_twi_init(i2c_speed_t speed)
{
	switch (speed) {
	case I2C_SPEED_400K:
		TWBR = 16;
		break;
	case I2C_SPEED_100K:
		TWBR = 32;
		break;
	default:
		TWBR = 32;
		break;
	}

	/* reset potential prescalers */
	TWSR = 0;

	/* www.mikrocontroller.net/articles/AVR_TWI says this might help ... */
	TWCR &= ~(BIT(TWSTO) | BIT(TWEN));
	TWCR |=	BIT(TWEN);
}

static void i2c_twi_wait_for_complete(void)
{
	uint8_t timeout = 100;

	do {
		_delay_us(10);
		timeout--;
	} while(timeout && !(TWCR & (1<<TWINT)));
}

static void i2c_twi_start(void)
{
	TWCR = BIT(TWINT) | BIT(TWEN) | BIT(TWSTA);
	i2c_twi_wait_for_complete();
}

static void i2c_twi_stop(void)
{
	TWCR = BIT(TWINT) | BIT(TWEN) | BIT(TWSTO);
}


static uint8_t i2c_twi_recv_byte(bool ack)
{
	TWCR = BIT(TWINT) | BIT(TWEN) | (ack ? BIT(TWEA) : 0);
	i2c_twi_wait_for_complete();
	return TWDR;
}

static void i2c_twi_send_byte(uint8_t data)
{
	TWDR = data;
	TWCR = BIT(TWINT) | BIT(TWEN);
	i2c_twi_wait_for_complete();
}

int8_t i2c_twi_read(uint8_t addr, uint8_t reg, uint8_t *value)
{
	/* start the write transaction to select the register */
	i2c_twi_start();
	i2c_twi_send_byte(I2C_WRITE_ADDR(addr));
	i2c_twi_send_byte(reg);

	/* (re)start for the actual read transaction to read back */
	i2c_twi_start();
	i2c_twi_send_byte(I2C_READ_ADDR(addr));
	*value = i2c_twi_recv_byte(false);
	i2c_twi_stop();

	return 0;
}

int8_t i2c_twi_write(uint8_t addr, uint8_t reg, uint8_t value)
{
	i2c_twi_start();
	i2c_twi_send_byte(I2C_WRITE_ADDR(addr));
	i2c_twi_send_byte(reg);
	i2c_twi_send_byte(value);
	i2c_twi_stop();

	return 0;
}

int8_t i2c_twi_read16(uint8_t addr, uint8_t reg, uint16_t *value)
{
	uint8_t msb, lsb;

	/* start the write transaction to select the register */
	i2c_twi_start();
	i2c_twi_send_byte(I2C_WRITE_ADDR(addr));
	i2c_twi_send_byte(reg);

	/* (re)start for the actual read transaction to read back MSB
	 * then LSB, fortunately the datashit describes the opposite w.r.t ACKs*/
	i2c_twi_start();
	i2c_twi_send_byte(I2C_READ_ADDR(addr));
	msb = i2c_twi_recv_byte(true);
	lsb = i2c_twi_recv_byte(false);
	i2c_twi_stop();

	*value = (msb << 8) | lsb;

	return 0;
}

int8_t i2c_twi_write16(uint8_t addr, uint8_t reg, uint16_t value)
{
	uint8_t msb, lsb;

	msb = value >> 8;
	lsb = value & 0xff;

	i2c_twi_start();
	i2c_twi_send_byte(I2C_WRITE_ADDR(addr));
	i2c_twi_send_byte(reg);
	i2c_twi_send_byte(msb);
	i2c_twi_send_byte(lsb);
        i2c_twi_stop();

	return 0;
}

/*
static const uint8_t I2C_ARA_ADDR = 0x0c;
uint8_t i2c_twi_smbus_ara(void)
{
	volatile uint8_t addr;;

	i2c_twi_start();
	i2c_twi_send_byte(I2C_READ_ADDR(I2C_ARA_ADDR));
	addr = i2c_twi_recv_byte(false);
	i2c_twi_stop();
	return addr;
}
*/