Skip to content

Commit

Permalink
docs(v0.0.10): tags and simplified usage example
Browse files Browse the repository at this point in the history
  • Loading branch information
zpg6 committed Jul 6, 2024
1 parent fcb49d6 commit bc24bef
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 68 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mcp2003a"
version = "0.0.9"
version = "0.0.10"
description = "Rust no_std LIN Bus communication using the MCP2003A LIN transceiver."
edition = "2021"
license = "MIT"
Expand Down
93 changes: 27 additions & 66 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# mcp2003a

<br>
<a href="https://crates.io/crates/mcp2003a">
<img src="https://img.shields.io/crates/v/mcp2003a.svg" alt="Crates.io">
</a>
<a href="https://docs.rs/mcp2003a">
<img src="https://docs.rs/mcp2003a/badge.svg" alt="Documentation">
</a>
<a href="https://github.com/zpg6/mcp2003a">
<img src="https://img.shields.io/badge/github-zpg6/mcp2003a-black" alt="GitHub Repo">
</a>
<br><br>

Rust crate for basic `no_std` LIN Bus communications with MCP2003A LIN Transceiver. Uses `embedded-hal` digital traits for GPIO and `embedded-hal-nb` Serial traits for UART.

- `embedded-hal = "1.0.0"` - Major breaking changes versus 0.2.x implementations.
Expand All @@ -20,76 +32,25 @@ Full Documentation: [https://docs.rs/mcp2003a/latest/mcp2003a/](https://docs.rs/
cargo add mcp2003a
```

### Tested Examples

- [ESP-32 via ESP-RS](https://github.com/zpg6/mcp2003a/tree/main/examples/mcp2003a-esp-rs) - Example using the MCP2003A with an ESP-32 microcontroller using the ESP-RS HAL.
### Example

### Pseudo-Code Example
```rust
let mut mcp2003a = Mcp2003a::new(uart2_driver, break_pin_driver, delay, lin_bus_config);
```

Here is an pseudo-code example of how to use to send and receive LIN frames with the MCP2003A LIN Transceiver:
Then you can use the `mcp2003a` instance to send and receive LIN frames.

```rust
use mcp2003a::{
config::{
LinBreakDuration, LinBusConfig, LinBusSpeed, LinInterFrameSpace,
LinReadDeviceResponseTimeout, LinWakeupDuration,
},
Mcp2003a, Mcp2003aError,
};

let uart = // Your embedded-hal-nb UART driver (usually same baudrate as LIN Bus)
let break_pin = // Your embedded-hal GPIO output pin driver
let delay_ns = // Your embedded-hal delay driver

// Configure the LIN Bus with the following parameters:
let lin_bus_config = LinBusConfig {
speed: LinBusSpeed::Baud19200,
break_duration: LinBreakDuration::Minimum13BitsPlus(1), // Test for your application
wakeup_duration: LinWakeupDuration::Minimum250Microseconds, // Test for your application
read_device_response_timeout: LinReadDeviceResponseTimeout::DelayMilliseconds(5), // Test for your application
inter_frame_space: LinInterFrameSpace::DelayMilliseconds(2), // Test for your application
};

// Now control the MCP2003A LIN Transceiver with LIN configuration and driver
let mut mcp2003a = Mcp2003a::new(uart, break_pin, delay_ns, lin_bus_config);

// Wakeup the LIN Bus
mcp2003a.send_wakeup();

// Send a frame on the LIN bus to a device with Command frame of 0x00:
// - Id: 0x00
// - Data: [0x02, 0x03]
// - Checksum: 0x05
match mcp2003a.send_frame(0x00, &[0x02, 0x03], 0x05) {
Ok(frame) => {
// Frame sent
log::info!("Sent data to LIN Id 0x00: {:?}", frame);
}
Err(e) => {
// Error sending the frame
log::error!("Error sending frame 0x00: {:?}", e);
}
}

// Read the feedback / diagnostic frame with Id 0x01:
// - Id: 0x01
// - Data: We provide an 8-byte buffer to store the data
let mut data = [0u8; 8];
match mcp2003a.read_frame(0x01, &mut data) {
Ok(len) => {
// Data is stored in the buffer
log::info!("Received data from LIN Id 0x01: {:?}", &data[..len]);
}
Err(e) => {
// Error reading the frame
match e {
Mcp2003aError::LinDeviceNoResponse => {
log::warn!("No response from frame 0x01... this device may be offline.");
}
_ => {
log::error!("Error reading frame 0x01: {:?}", e);
}
}
}
}
mc2003a.send_frame(0x01, &[0x02, 0x03], 0x05).unwrap();

let mut read_buffer = [0u8; 11];
let len = mcp2003a.read_frame(0xC1, &mut read_buffer).unwrap();
```

### Full Examples

(More coming soon)

- [ESP-32 via ESP-RS](https://github.com/zpg6/mcp2003a/tree/main/examples/mcp2003a-esp-rs) - Example using the MCP2003A with an ESP-32 microcontroller using the ESP-RS HAL.
28 changes: 27 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
//! MCP2003A LIN Transceiver Library
//!
//! ⚠️ WORK IN PROGRESS
//! <br>
//! <a href="https://crates.io/crates/mcp2003a">
//! <img src="https://img.shields.io/crates/v/mcp2003a.svg" alt="Crates.io">
//! </a>
//! <a href="https://docs.rs/mcp2003a">
//! <img src="https://docs.rs/mcp2003a/badge.svg" alt="Documentation">
//! </a>
//! <a href="https://github.com/zpg6/mcp2003a">
//! <img src="https://img.shields.io/badge/github-zpg6/mcp2003a-black" alt="GitHub Repo">
//! </a>
//! <br><br>
//!
//!
//! This library provides an `embedded-hal` abstraction for the MCP2003A LIN transceiver using UART
//! and a GPIO output pin for the break signal.
Expand All @@ -18,6 +29,21 @@
//! - [MCP2003A Product Page](https://www.microchip.com/wwwproducts/en/MCP2003A)
//! - [MCP2003A Datasheet](https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/20002230G.pdf)
//!
//! # Usage
//! ```rust
//! let mut mcp2003a = Mcp2003a::new(uart2_driver, break_pin_driver, delay, lin_bus_config);
//! ```
//!
//! Then you can use the `mcp2003a` instance to send and receive LIN frames.
//!
//! ```rust
//! mcp2003a.send_wakeup();
//!
//! mc2003a.send_frame(0x01, &[0x02, 0x03], 0x05).unwrap();
//!
//! let mut read_buffer = [0u8; 11];
//! let len = mcp2003a.read_frame(0xC1, &mut read_buffer).unwrap();
//! ```

#![no_std]

Expand Down

0 comments on commit bc24bef

Please sign in to comment.