common: Add the i2c module.
[gps-watch.git] / src / common / i2c.rs
1 /*
2  * Copyright (c) 2019 Tilman Sauerbeck (tilman at code-monkey de)
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23
24 use register;
25
26 type Reg8 = register::Register<u8>;
27 type Reg32 = register::Register<u32>;
28
29 const I2C_BASE: u32 = 0x40066000;
30
31 pub const I2C0: u32 = I2C_BASE + 0x0000;
32 pub const I2C1: u32 = I2C_BASE + 0x1000;
33
34 const SIM_SCGC4: u32 = 0x40048034;
35
36 const SIM_SCGC4_I2C0: u32 = 1 << 6;
37 const SIM_SCGC4_I2C1: u32 = 1 << 7;
38
39 const I2C_F   : u32 = 0x01;
40 const I2C_C1  : u32 = 0x02;
41 const I2C_S1  : u32 = 0x03;
42 const I2C_D   : u32 = 0x04;
43
44 const I2C_C1_TX   : u8 = 1 << 4;
45 const I2C_C1_MST  : u8 = 1 << 5;
46 const I2C_C1_IICEN: u8 = 1 << 7;
47
48 const I2C_S1_IICIF: u8 = 1 << 1;
49
50 const I2C_F_ICR_SHIFT: u8 = 0;
51
52 pub fn configure(i2c: u32) {
53     {
54         let mut scgc4 = Reg32::new(SIM_SCGC4);
55
56         if i2c == I2C0 {
57             scgc4.modify(|v| v | SIM_SCGC4_I2C0);
58         } else {
59             scgc4.modify(|v| v | SIM_SCGC4_I2C1);
60         }
61     }
62
63     let mut f = Reg8::new(i2c + I2C_F);
64     f.write(0x12 << I2C_F_ICR_SHIFT);
65
66     let mut c1 = Reg8::new(i2c + I2C_C1);
67     c1.write(I2C_C1_IICEN);
68 }
69
70 fn prepare_tx(i2c: u32, slave_address: u8) {
71     let mut c1 = Reg8::new(i2c + I2C_C1);
72
73     c1.modify(|v| v | I2C_C1_TX);
74     c1.modify(|v| v | I2C_C1_MST);
75
76     let mut d = Reg8::new(i2c + I2C_D);
77     d.write((slave_address << 1) | 0);
78 }
79
80 fn finish_tx(i2c: u32) {
81     let mut c1 = Reg8::new(i2c + I2C_C1);
82
83     c1.modify(|v| v & !I2C_C1_MST);
84     c1.modify(|v| v & !I2C_C1_TX);
85 }
86
87 fn wait_for_and_ack_interrupt(i2c: u32) {
88     let mut s1 = Reg8::new(i2c + I2C_S1);
89
90     // Wait for interrupt to occur.
91     while (s1.read() & I2C_S1_IICIF) == 0 {
92     }
93
94     // Acknowledge interrupt.
95     s1.modify(|v| v | I2C_S1_IICIF);
96 }
97
98 fn tx(i2c: u32, data: u8) {
99     wait_for_and_ack_interrupt(i2c);
100
101     let mut d = Reg8::new(i2c + I2C_D);
102     d.write(data);
103 }
104
105 pub fn tx16(i2c: u32, slave_address: u8, data: u16) {
106     prepare_tx(i2c, slave_address);
107
108     tx(i2c, (data >> 0) as u8);
109     tx(i2c, (data >> 8) as u8);
110
111     wait_for_and_ack_interrupt(i2c);
112
113     finish_tx(i2c);
114 }