loopback/loopback.rs
1// SPDX-FileCopyrightText: 2018-2022 Joonas Javanainen <joonas.javanainen@gmail.com>
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5extern crate mcp2210;
6
7use hidapi::HidApi;
8use mcp2210::{open_first, Commands, SpiMode, SpiTransferSettings};
9
10fn main() {
11 //! ##################################################################################
12 //! ## ⚠️ WARNING ⚠️ ##
13 //! ## This code sends 0xaa55 on the MCP2210's SPI bus. ##
14 //! ## If you have a device connected to the SPI bus, ensure this will not harm it. ##
15 //! ##################################################################################
16 //! #
17 //! This code sends 0xaa55 on the MCP2210's SPI bus MOSI pin and asserts that the same
18 //! data is simultaneously recieved at the MISO pin. The circuit required for this is
19 //! simply a wire between the MOSI and MISO pins of the MCP2210 and no real slave device.
20
21 let hidapi_context = HidApi::new().expect("Could not create hidapi context");
22 let mut mcp = open_first(&hidapi_context).expect("Failed to connect");
23 mcp.set_spi_transfer_settings(&SpiTransferSettings {
24 bit_rate: 1_000,
25 bytes_per_tx: 2,
26 spi_mode: SpiMode::Mode0,
27 ..Default::default()
28 })
29 .expect("Failed to set settings");
30 let mut buf = Vec::new();
31 mcp.spi_transfer_to_end(&[0xaa, 0x55], &mut buf)
32 .expect("SPI transfer failed");
33 assert_eq!(buf, [0xaa, 0x55]);
34}