Giter VIP home page Giter VIP logo

Comments (3)

newAM avatar newAM commented on August 23, 2024 1

exception has a special meaning for Cortex-M CPUs. Some exceptions are used for error status', but more generally they are just built-in interrupts.

This is the exception number definition for ARMv6-M: https://developer.arm.com/documentation/ddi0419/c/System-Level-Architecture/System-Level-Programmers--Model/ARMv6-M-exception-model/Exception-number-definition?lang=en

2 is HardFault, that occurs only when there's an error condition, but 15 is SysTick, and that occurs as part of normal operation. All the interrupts added by ST then get allocation to number 16+

from stm32f0xx-hal.

newAM avatar newAM commented on August 23, 2024

I gave it a quick skim, it looks like the SysTick is simply used to calculate the speed of the encoder: stm32f1xx_it.c#L197

The actual position comes from a different mechanism, HAL_TIM_IC_CaptureCallback (if I am reading that right, I did skim this fairly quickly 😅 )

Assuming you have that position the SysTick handler could be written in rust along the lines of:

// probably missing some use statements, I didn't actually compile this.
use cortex_m_rt::{entry, exception};

static OLDPOS: i16 = 0;
static INDX: i32 = 0;

#[exception]
fn SysTick() {

    unsafe {
        INDX += 1;
        if (INDX == 500) {
            SPEED = ((POSITION - OLDPOS) * 2);
            OLDPOS = POSITION;
            INDX = 0;
        }
    }

    // whatever else here
}

#[entry]
fn main() -> ! {
    let p = cortex_m::Peripherals::take().unwrap();
    let mut syst = p.SYST;

    // configures the system timer to trigger a SysTick exception every second
    syst.set_clock_source(SystClkSource::Core);
    // CPU clock of 8 MHz
    syst.set_reload(8_000_000);
    syst.clear_current();
    syst.enable_counter();
    syst.enable_interrupt();

    loop {
        compiler_fence(SeqCst);
    }
}

Actually getting the position is a bit more effort that I don't know about off the top of my head.

That C example you linked isn't too big, you could probably recreate that using the PAC to get started.

from stm32f0xx-hal.

samehmohamed88 avatar samehmohamed88 commented on August 23, 2024

@newAM Thanks very much for this!

I saw in the examples that the #[exception] macro was used, but based on the comments it seems like it 's only called when an error occurs in the interrupt. But based on your example, it's the interrupt routine itself?

from stm32f0xx-hal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.