1309 views|1 replies

288

Posts

0

Resources
The OP
 

"Rust in Action" 4. Using Rust to write RC522 driver code for STM32 [Copy link]

In the previous chapter, we realized the use of Rust programming to control STM32F103 to realize the flashing function of LED. In this chapter, we will continue to improve it and realize the use of Rust programming to control STM32F103 to drive RC522 to read the card. As we all know, RC522 is a radio frequency identification chip. Its module is RFID-RC522, which is a small blue board. It uses SPI interface to communicate with MCU. It is a common and simple radio frequency communication module. Today we will use it to realize radio frequency identification.
code show as below:
#![deny(unsafe_code)]
#![no_main]
#![no_std]

use panic_itm as _;

use cortex_m::iprintln;
use cortex_m_rt::entry;
use embedded_hal::digital::v1_compat::OldOutputPin;
use embedded_hal::spi::{Mode, Phase, Polarity};
use mfrc522::Mfrc522;
use stm32f1xx_hal::{pac, prelude::*, spi::Spi};
pub const MODE: Mode = Mode {
    polarity: Polarity::IdleLow,
    phase: Phase::CaptureOnFirstTransition,
};
#[entry]
fn main() -> ! {
    let mut cp = cortex_m::Peripherals::take().unwrap();
    let dp = pac::Peripherals::take().unwrap();

    let _stim = &mut cp.ITM.stim[0];
    let rcc = dp.RCC.constrain();
    let mut afio = dp.AFIO.constrain();
    let mut flash = dp.FLASH.constrain();
    let mut gpioa = dp.GPIOA.split();
    let mut gpioc = dp.GPIOC.split();

    let clocks = rcc.cfgr.freeze(&mut flash.acr);

    let sck = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl);
    let miso = gpioa.pa6;
    let mosi = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl);
    let spi = Spi::spi1(
        dp.SPI1,
        (sck, miso, mosi),
        &mut afio.mapr,
        MODE,
        1.MHz(),
        clocks,
    );

    let nss = gpioa.pa4.into_push_pull_output(&mut gpioa.crl);
    let mut mfrc522 = Mfrc522::new(spi, OldOutputPin::from(nss)).unwrap();

    let mut delay = cp.SYST.delay(&clocks);

    let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
    led.set_high();

    loop {
        if let Ok(atqa) = mfrc522.reqa() {
            if let Ok(uid) = mfrc522.select(&atqa) {
                //println!(_stim, "* {:?}", uid.as_bytes());
                led.set_low();
                delay.delay_ms(1_000_u16);
            }
        }
        led.set_high();
        delay.delay_ms(1_000_u16);
    }
}

The main MCU interfaces used here are: NSS-PA4, SCK-PA5, MISO-PA6, MOSI-PA7 and LED-PC13. The main logic is that when the UID of the IC card is recognized by RC522, the LED is controlled to light up for 1 second. If the card is detected, it will continue to light up. If the card is not detected, the LED will go out and delay for 1 second before continuing to detect the IC card.
Figure 1 IC card detected
Figure 2: Normal state, no IC card detected
Summary: The main difficulty of this chapter is to call the peripheral SPI, which needs to be implemented using the function use stm32f1xx_hal::{pac, prelude::*, spi::Spi}; At the same time, I originally wanted to use the delay in the standard library module, but later found that there was a conflict, because the program has prohibited the use of standard library modules, so I need to use let mut delay = cp.SYST.delay(&clocks); to implement the delay function. In general, Rust is indeed a good language, but in terms of driving MCU, I think there is less information, and you need to have a certain Rust foundation to be very proficient in embedded development. This book mainly explains the background service development, which is indeed a rare practical book. I would also like to thank eeworld for the trial reading activity, which is very meaningful and can expand my knowledge a little bit~
This post is from Programming Basics

Latest reply

It’s getting stronger. This curve of progress is exciting! Keep going!   Details Published on 2024-7-4 07:47
 

6841

Posts

11

Resources
2
 

It’s getting stronger. This curve of progress is exciting! Keep going!

This post is from Programming Basics
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Featured Posts
LCD TV schematic diagram

LCD TV schematic diagram

Microelectronics Dictionary

Abrupt junction Accelerated testing Acceptor Acceptor atom Accumulation Accumulating contact Accumulation region Accumul ...

Motor drive control essence summary post

Since the establishment of this board, many netizens have posted many very valuable posts here. Xiaoguan took advantage ...

DM8148 sends Frames stream from DSP side to A8 side program configuration

I have been debugging DM8148 recently. According to the requirements, the data obtained by the camera needs to be sent t ...

ssd1306 Chinese and picture display

This post was last edited by lemon1394 on 2021-8-17 23:19 I have found many Chinese displays of ssd1306 introduced on t ...

31 "Millions of Miles" Raspberry Pi Car——Ubuntu MATE System Installation

Next, I was going to start learning ROS, but it was particularly difficult to install it on the Raspberry Pi operating ...

Pre-registration for the live broadcast with prizes | New opportunities in the UWB market

UWB is regarded as a new function of the next generation of human-computer interaction due to its high precision and hig ...

[Mil MYD-YG2LX development board] Application in industrial control field - EtherCAT master station based on SOEM

Preface This article introduces the application of in the field of industrial control , implement EtherCAT master based ...

[Digi-Key Electronics Follow me Issue 2] + Preliminary Task Construction

This post was last edited by Shi Xiaojie on 2023-11-2 22:32 Task construction: This time, flow me is developed using ci ...

LicheePi 4A problem record and solution Manually mount npu driver

Environment: debian system image 20231023 The error information is as follows: ``` (ort) root@lpi4a:/home/sipeed/Desktop ...

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list