msp432/
i2c.rs

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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

use crate::usci::{self, UsciBRegisters};
use core::cell::Cell;
use kernel::hil::i2c::{self, Error};
use kernel::utilities::cells::{OptionalCell, TakeCell};
use kernel::utilities::registers::interfaces::{ReadWriteable, Readable, Writeable};
use kernel::utilities::StaticRef;

#[derive(Copy, Clone, PartialEq)]
pub enum Speed {
    K100, // 100kHz
    K375, // 375kHz
}

#[derive(Copy, Clone, PartialEq)]
enum OperatingMode {
    Unconfigured,
    Disabled,
    Idle,
    Write,
    Read,
    WriteReadWrite,
    WriteReadRead,
}

pub struct I2c<'a> {
    registers: StaticRef<UsciBRegisters>,
    mode: Cell<OperatingMode>,
    read_len: Cell<usize>,
    write_len: Cell<usize>,
    buf_idx: Cell<usize>,
    buffer: TakeCell<'static, [u8]>,
    master_client: OptionalCell<&'a dyn i2c::I2CHwMasterClient>,
}

impl I2c<'_> {
    pub fn new(registers: StaticRef<UsciBRegisters>) -> Self {
        Self {
            registers,
            mode: Cell::new(OperatingMode::Unconfigured),
            read_len: Cell::new(0),
            write_len: Cell::new(0),
            buf_idx: Cell::new(0),
            buffer: TakeCell::empty(),
            master_client: OptionalCell::empty(),
        }
    }

    fn set_module_to_reset(&self) {
        // Set USCI module to reset in order to make a certain configurations possible
        self.registers.ctlw0.modify(usci::UCBxCTLW0::UCSWRST::SET);
    }

    fn clear_module_reset(&self) {
        // Set USCI module to reset in order to make a certain configurations possible
        self.registers.ctlw0.modify(usci::UCBxCTLW0::UCSWRST::CLEAR);

        // Setting the module to reset clears the enabled interrupts -> enable them again
        self.enable_interrupts();
    }

    fn set_slave_address(&self, addr: u8) {
        self.registers.i2csa.set(addr as u16);
    }

    fn generate_start_condition(&self) {
        self.registers
            .ctlw0
            .modify(usci::UCBxCTLW0::UCTXSTT::GenerateSTARTCondition);
    }

    fn generate_stop_condition(&self) {
        self.registers
            .ctlw0
            .modify(usci::UCBxCTLW0::UCTXSTP::GenerateSTOP);
    }

    fn set_stop_condition_automatically(&self, val: bool) {
        if val {
            self.registers
                .ctlw1
                .modify(usci::UCBxCTLW1::UCASTP::ByteCounterStopCondition)
        } else {
            self.registers.ctlw1.modify(usci::UCBxCTLW1::UCASTP::Manual);
        }
    }

    fn enable_interrupts(&self) {
        // Enable interrupts
        //
        // Enable NACK interrupt
        // Enable RX interrupt
        // Enable Stop condition interrupt
        // Enable Start condition interrupt
        // Enable 'arbitration lost' interrupt
        self.registers.ie.modify(
            usci::UCBxIE::UCNACKIE::SET
                + usci::UCBxIE::UCRXIE0::SET
                + usci::UCBxIE::UCSTPIE::SET
                + usci::UCBxIE::UCSTTIE::SET
                + usci::UCBxIE::UCALIE::SET,
        );
    }

    fn enable_transmit_mode(&self) {
        self.registers
            .ctlw0
            .modify(usci::UCBxCTLW0::UCTR::Transmitter);
    }

    fn enable_receive_mode(&self) {
        self.registers.ctlw0.modify(usci::UCBxCTLW0::UCTR::Receiver);
    }

    fn enable_transmit_interrupt(&self) {
        self.registers.ie.modify(usci::UCBxIE::UCTXIE0::SET);
    }

    fn disable_transmit_interrupt(&self) {
        self.registers.ie.modify(usci::UCBxIE::UCTXIE0::CLEAR);
    }

    fn set_byte_counter(&self, val: usize) {
        self.registers.tbcnt.set(val as u16);
    }

    fn invoke_callback(&self, status: Result<(), Error>) {
        // Reset buffer index and set mode to Idle in order to start a new transfer properly
        self.buf_idx.set(0);
        self.mode.set(OperatingMode::Idle);

        self.buffer.take().map(|buf| {
            self.master_client
                .map(move |cl| cl.command_complete(buf, status))
        });
    }

    fn setup(&self) {
        self.set_module_to_reset();

        // Use 7 bit addresses
        // Setup to master mode
        // Setup to single master environment
        // Configure USCI module to I2C mode
        // Enable Synchronous mode
        // Set clock source to SMCLK (1.5MHz)
        self.registers.ctlw0.modify(
            usci::UCBxCTLW0::UCSLA10::AddressSlaveWith7BitAddress
                + usci::UCBxCTLW0::UCMST::MasterMode
                + usci::UCBxCTLW0::UCMM::SingleMasterEnvironment
                + usci::UCBxCTLW0::UCMODE::I2CMode
                + usci::UCBxCTLW0::UCSYNC::SynchronousMode
                + usci::UCBxCTLW0::UCSSEL::SMCLK,
        );

        // Disable clock low timeout
        // Send a NACK before a stop condition
        // Generate the ACK bit by hardware
        // Set glitch filtering to 50ns (according to I2C standard)
        self.registers.ctlw1.modify(
            usci::UCBxCTLW1::UCCLTO::CLEAR
                + usci::UCBxCTLW1::UCSTPNACK::NackBeforeStop
                + usci::UCBxCTLW1::UCSWACK::HardwareTriggered
                + usci::UCBxCTLW1::UCGLIT::_50ns,
        );

        // Don't clear the module reset here since we set the state to Disabled
        self.mode.set(OperatingMode::Disabled);
    }

    pub fn set_speed(&self, speed: Speed) {
        self.set_module_to_reset();

        // SMCLK is running at 1.5MHz
        // In order to achieve a speed of 100kHz or 375kHz, it's necessary to divide the clock
        // by either 15 (100kHz) or 4 (375kHz)
        if speed == Speed::K100 {
            self.registers.brw.set(15);
        } else if speed == Speed::K375 {
            self.registers.brw.set(4);
        }

        self.clear_module_reset();
    }

    pub fn handle_interrupt(&self) {
        let ifg = self.registers.ifg.get();
        let mode = self.mode.get();
        let idx = self.buf_idx.get();

        // clear all interrupts
        self.registers.ifg.set(0);

        if (ifg & (1 << usci::UCBxIFG::UCTXIFG0.shift)) > 0 {
            // TX interrupt
            if idx < self.write_len.get() {
                // Transmit another byte
                self.buffer
                    .map(|buf| self.registers.txbuf.set(buf[idx] as u16));
                self.buf_idx.set(idx + 1);
            } else {
                self.disable_transmit_interrupt();
                if mode == OperatingMode::WriteReadWrite {
                    // Finished write part -> switch to reading
                    self.mode.set(OperatingMode::WriteReadRead);
                    self.buf_idx.set(0);

                    // Switch to receiving and send a restart condition
                    self.enable_receive_mode();
                    self.generate_start_condition();
                    if self.read_len.get() == 1 {
                        // In this mode the stop condition is set automatically and has to be
                        // requested 1 byte before the last byte was received. If only one byte will
                        // be received request the stop condition immediately after the start.
                        self.generate_stop_condition();
                    }
                }
            }
        } else if (ifg & (1 << usci::UCBxIFG::UCRXIFG0.shift)) > 0 {
            // RX interrupt
            if idx < self.read_len.get() {
                if idx == (self.read_len.get() - 1) && mode == OperatingMode::WriteReadRead {
                    // In this mode we don't use the byte counter to generate an automatic stop
                    // condition, further, the stop condition has to be set before the last byte was
                    // received
                    self.generate_stop_condition();
                }
                // Store received byte in buffer
                self.buffer
                    .map(|buf| buf[idx] = self.registers.rxbuf.get() as u8);
                self.buf_idx.set(idx + 1);
            } else if mode == OperatingMode::WriteReadRead {
                // For some reason generating a stop condition manually in receive mode doesn't
                // trigger a stop condition interrupt -> invoke the callback here when all bytes
                // were received
                self.invoke_callback(Ok(()));
            }
        } else if (ifg & (1 << usci::UCBxIFG::UCSTTIFG.shift)) > 0 {
            // Start condition interrupt
            if mode == OperatingMode::Write || mode == OperatingMode::WriteReadWrite {
                self.buffer
                    .map(|buf| self.registers.txbuf.set(buf[idx] as u16));
                self.buf_idx.set(idx + 1);
            }
        } else if (ifg & (1 << usci::UCBxIFG::UCSTPIFG.shift)) > 0 {
            // Stop condition interrupt

            // This interrupt is the default indicator that a transaction finished, thus raise the
            // callback here and prepare for another transfer
            self.invoke_callback(Ok(()));
        } else if (ifg & (1 << usci::UCBxIFG::UCNACKIFG.shift)) > 0 {
            // NACK interrupt
            // TODO: use byte counter to detect address NAK

            // Cancel i2c transfer
            self.generate_stop_condition();
            self.invoke_callback(Err(Error::DataNak));
        } else if (ifg & (1 << usci::UCBxIFG::UCALIFG.shift)) > 0 {
            // Arbitration lost  interrupt

            // Cancel i2c transfer
            self.generate_stop_condition();
            self.invoke_callback(Err(Error::Busy));
        } else {
            panic!("I2C: unhandled interrupt, ifg: {}", ifg);
        }
    }
}

impl<'a> i2c::I2CMaster<'a> for I2c<'a> {
    fn set_master_client(&self, master_client: &'a dyn i2c::I2CHwMasterClient) {
        self.master_client.replace(master_client);
    }

    fn enable(&self) {
        if self.mode.get() == OperatingMode::Unconfigured {
            self.setup();
        }

        self.clear_module_reset();
        self.mode.set(OperatingMode::Idle);
    }

    fn disable(&self) {
        self.set_module_to_reset();
        self.mode.set(OperatingMode::Disabled);
    }

    fn write(
        &self,
        addr: u8,
        data: &'static mut [u8],
        len: usize,
    ) -> Result<(), (Error, &'static mut [u8])> {
        if self.mode.get() != OperatingMode::Idle {
            // Module is busy or not activated
            return Err((Error::Busy, data));
        }

        self.buffer.replace(data);
        self.write_len.set(len);

        // Set module to reset since some of the registers cannot be modified in running state
        self.set_module_to_reset();

        // Setup the byte counter in order to automatically generate a stop condition after the
        // desired number of bytes were transmitted
        self.set_byte_counter(len);

        // Create stop condition automatically after the number of bytes in the byte counter
        // register were transmitted
        self.set_stop_condition_automatically(true);
        self.clear_module_reset();

        self.set_slave_address(addr);
        self.enable_transmit_mode();
        self.enable_transmit_interrupt();

        self.mode.set(OperatingMode::Write);

        // Start transfer
        self.generate_start_condition();

        Ok(())
    }

    fn read(
        &self,
        addr: u8,
        buffer: &'static mut [u8],
        len: usize,
    ) -> Result<(), (Error, &'static mut [u8])> {
        if self.mode.get() != OperatingMode::Idle {
            // Module is busy or not activated
            return Err((Error::Busy, buffer));
        }

        self.buffer.replace(buffer);
        self.read_len.set(len);

        // Set module to reset since some of the registers cannot be modified in running state
        self.set_module_to_reset();

        // Setup the byte counter in order to automatically generate a stop condition after the
        // desired number of bytes were transmitted
        self.set_byte_counter(len);

        // Generate a stop condition automatically after the number of bytes in the byte counter
        // register were transmitted
        self.set_stop_condition_automatically(true);
        self.clear_module_reset();

        self.set_slave_address(addr);
        self.enable_receive_mode();
        self.mode.set(OperatingMode::Read);

        // Start transfer
        self.generate_start_condition();
        Ok(())
    }

    fn write_read(
        &self,
        addr: u8,
        data: &'static mut [u8],
        write_len: usize,
        read_len: usize,
    ) -> Result<(), (Error, &'static mut [u8])> {
        if self.mode.get() != OperatingMode::Idle {
            // Module is busy or not activated
            return Err((Error::Busy, data));
        }

        self.buffer.replace(data);
        self.write_len.set(write_len);
        self.read_len.set(read_len);

        // Set module to reset since some of the registers cannot be modified in running state
        self.set_module_to_reset();

        // Disable generating a stop condition automatically since after the write, a repeated
        // start condition will be generated in order to continue reading from the slave
        self.set_stop_condition_automatically(false);
        self.clear_module_reset();

        self.set_slave_address(addr);
        self.enable_transmit_mode();
        self.enable_transmit_interrupt();
        self.mode.set(OperatingMode::WriteReadWrite);

        // Start transfer
        self.generate_start_condition();

        Ok(())
    }
}