components/
atecc508a.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! Components for the ATECC508A CryptoAuthentication Device.
6//!
7//! Usage
8//! -----
9//! ```rust
10//!     let atecc508a =
11//!         Atecc508aComponent::new(mux_i2c, 0x60).finalize(components::atecc508a_component_static!());
12//! ```
13
14use capsules_core::virtualizers::virtual_i2c::{I2CDevice, MuxI2C};
15use capsules_extra::atecc508a::Atecc508a;
16use core::mem::MaybeUninit;
17use kernel::component::Component;
18use kernel::hil::i2c;
19
20// Setup static space for the objects.
21#[macro_export]
22macro_rules! atecc508a_component_static {
23    ($I:ty $(,)?) => {{
24        let i2c_device =
25            kernel::static_buf!(capsules_core::virtualizers::virtual_i2c::I2CDevice<'static, $I>);
26        let i2c_buffer = kernel::static_buf!([u8; 140]);
27        let entropy_buffer = kernel::static_buf!([u8; 32]);
28        let digest_buffer = kernel::static_buf!([u8; 64]);
29        let atecc508a = kernel::static_buf!(capsules_extra::atecc508a::Atecc508a<'static>);
30
31        (
32            i2c_device,
33            i2c_buffer,
34            entropy_buffer,
35            digest_buffer,
36            atecc508a,
37        )
38    };};
39}
40
41pub struct Atecc508aComponent<I: 'static + i2c::I2CMaster<'static>> {
42    i2c_mux: &'static MuxI2C<'static, I>,
43    i2c_address: u8,
44    wakeup_device: fn(),
45}
46
47impl<I: 'static + i2c::I2CMaster<'static>> Atecc508aComponent<I> {
48    pub fn new(i2c: &'static MuxI2C<'static, I>, i2c_address: u8, wakeup_device: fn()) -> Self {
49        Atecc508aComponent {
50            i2c_mux: i2c,
51            i2c_address,
52            wakeup_device,
53        }
54    }
55}
56
57impl<I: 'static + i2c::I2CMaster<'static>> Component for Atecc508aComponent<I> {
58    type StaticInput = (
59        &'static mut MaybeUninit<I2CDevice<'static, I>>,
60        &'static mut MaybeUninit<[u8; 140]>,
61        &'static mut MaybeUninit<[u8; 32]>,
62        &'static mut MaybeUninit<[u8; 64]>,
63        &'static mut MaybeUninit<Atecc508a<'static>>,
64    );
65    type Output = &'static Atecc508a<'static>;
66
67    fn finalize(self, s: Self::StaticInput) -> Self::Output {
68        let atecc508a_i2c = s.0.write(I2CDevice::new(self.i2c_mux, self.i2c_address));
69
70        let i2c_buffer = s.1.write([0; 140]);
71        let entropy_buffer = s.2.write([0; 32]);
72        let digest_buffer = s.3.write([0; 64]);
73
74        let atecc508a = s.4.write(Atecc508a::new(
75            atecc508a_i2c,
76            i2c_buffer,
77            entropy_buffer,
78            digest_buffer,
79            self.wakeup_device,
80        ));
81
82        atecc508a_i2c.set_client(atecc508a);
83        atecc508a
84    }
85}