aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/e300/battery/i2c_twi.h
blob: dd88a683de9adafa0574b2fbdf5412dc6e6dfc2e (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
/* 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/>.
 */

/**
 * \file i2c_twi.h
 * \brief Atmel AVR TWI driver
 */

#ifndef I2C_TWI_H
#define I2C_TWI_H

#include <stdlib.h>
#include <stdio.h>

/**
 * \brief Used for initializing the TWI/I2C module
 */
typedef enum i2c_speed_t {
	I2C_SPEED_100K,
	I2C_SPEED_400K,
} i2c_speed_t;

/**
 * \brief Initialize and calculate the TWBR value based on F_CPU and given rate
 *
 * \param[in] rate Target rate
 */
void i2c_twi_init_calc(uint32_t rate);

/**
 * \brief Initializes the AVR TWI/I2C module
 *
 * \param[in] speed Can be either 100KHz or 400Khz
 */
void i2c_twi_init(i2c_speed_t speed);

/**
 * \brief Read I2C register from I2C slave
 * \param[in] addr I2C slave address
 * \param[in] reg  Register address in slave register map
 * \param[out] value Output value
 * \return 0 on success, negative error code otherwise
 */
int8_t i2c_twi_read(uint8_t addr, uint8_t reg, uint8_t *value);

/**
 * \brief Read 2 byte I2C register from I2C slave
 *
 * This is behaving a bit funny but is required for getting
 * the 2 byte values from the LTC294x chip.
 *
 * \param[in] addr I2C slave address
 * \param[in] reg  Register address in slave register map
 * \param[out] value Output value
 * \return 0 on success, negative error code otherwise
 */
int8_t i2c_twi_read16(uint8_t addr, uint8_t reg, uint16_t *value);

/**
 * \brief Write I2C register in I2C slave
 * \param[in] addr I2C slave address
 * \param[in] reg  Register address in slave register map
 * \param[in] value Value to be written
 * \return 0 on success, negative error code otherwise
 */
int8_t i2c_twi_write(uint8_t addr, uint8_t reg, uint8_t value);

/**
 * \brief Write 2 byte I2C register in I2C slave
 * \param[in] addr I2C slave address
 * \param[in] reg  Register address in slave register map
 * \param[in] value Value to be written
 * \return 0 on success, negative error code otherwise
 */
int8_t i2c_twi_write16(uint8_t addr, uint8_t reg, uint16_t value);

/**
 * \brief Handle SMBus alert response
 * \return 0 on success, negative error code otherwise
 */
uint8_t i2c_twi_smbus_ara(void);

#endif /* I2C_TWI_H */