aboutsummaryrefslogtreecommitdiff
path: root/lib/stm32/i2c.c
blob: e161b7f3d4ce776accbd8bed1716cebf08a23106 (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
/*
 * This file is part of the libopenstm32 project.
 *
 * Copyright (C) 2010 Thomas Otto <tommi@viadmin.org>
 *
 * 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 <libopenstm32/i2c.h>

void i2c_peripheral_enable(u32 i2c)
{
	I2C_CR1(i2c) |= I2C_CR1_PE;
}

void i2c_peripheral_disable(u32 i2c)
{
	I2C_CR1(i2c) &= ~I2C_CR1_PE;
}

void i2c_send_start(u32 i2c)
{
	I2C_CR1(i2c) |= I2C_CR1_START;
}

void i2c_send_stop(u32 i2c)
{
	I2C_CR1(i2c) |= I2C_CR1_STOP;
}

void i2c_set_own_7bit_slave_address(u32 i2c, u8 slave)
{
	I2C_OAR1(i2c) = (u16)(slave << 1);
	I2C_OAR1(i2c) &= ~I2C_OAR1_ADDMODE;
	I2C_OAR1(i2c) |= (1 << 14); /* Datasheet: always keep 1 by software. */
}

void i2c_set_own_10bit_slave_address(u32 i2c, u16 slave)
{
	I2C_OAR1(i2c) = (u16)(I2C_OAR1_ADDMODE | slave);
}

void i2c_set_fast_mode(u32 i2c)
{
	I2C_CCR(i2c) |= I2C_CCR_FS;
}

void i2c_set_standard_mode(u32 i2c)
{
	I2C_CCR(i2c) &= ~I2C_CCR_FS;
}

void i2c_set_clock_frequency(u32 i2c, u8 freq)
{
	u16 reg16;
	reg16 = I2C_CR2(i2c) & 0xffc0; /* Clear bits [5:0]. */
	reg16 |= freq;
	I2C_CR2(i2c) = reg16;
}

void i2c_set_ccr(u32 i2c, u16 freq)
{
	u16 reg16;
	reg16 = I2C_CCR(i2c) & 0xf000; /* Clear bits [11:0]. */
	reg16 |= freq;
	I2C_CCR(i2c) = reg16;
}

void i2c_set_trise(u32 i2c, u16 trise)
{
	I2C_TRISE(i2c) = trise;
}

void i2c_send_7bit_address(u32 i2c, u8 slave, u8 readwrite)
{
	I2C_DR(i2c) = (u8)((slave << 1) | readwrite);
}

void i2c_send_data(u32 i2c, u8 data)
{
	I2C_DR(i2c) = data;
}