---
# System prepended metadata

title: STM32 Code Structure
tags: [firmware_hardware]

---

# STM32 Code Structure
###### tags: `firmware_hardware`

## Front box

### Peripherals

- [ ] CAN
- [ ] Analog
- [ ] External Interrupt
- [ ] I2C (?
- [ ] Timer (?

### Devices

- [ ] Pedal encoder: Analog
- [ ] Pdeal micro switch: maybe polling is enough
- [ ] Hall sensor: External Interrupt
- [ ] Tire pressure/temp: No idea, maybe I2C?
- [ ] Steering encoder: no idea
- [ ] Potentiometer (for suspension): Analog


```
/******** variables *******/
pedalData[3] // 2 throttle encoder, 1 brake encoder
potentiometer[2]
tire_pressure[]
tire_temperature[]
wheel_speed[2]
steer_data
last_time[2] // for hall sensor


/******** CAN part ********/
function can_polling_receive() {
    receive()
    // maybe do something...
}

set timer:= for CAN transmit and receive

function handle_timeup () {
    can_transmit(id, data)    
    can_polling_receive()
}


/******* Hall sensor ******/
function interrupt_handler_L () {
    period:= time_now - last_time[L]
    wheel_speed[L] = pi*2*radius*(1/period)/teeth; // sort of...
    last_time[L] = time_now
}

function interrupt_handler_R () {
    period:= time_now - last_time[R]
    wheel_speed[R] = pi*2*radius*(1/period)/teeth; // sort of...
    last_time[R] = time_now
}

/****** main loop ********/
while (true) {
    // update sensor values
    if (micro_throttle_on) {
        update pedalData[throttle1], pedalData[throttle2]
    }
    
    if (micro_brake_on) {
        update pedalData[brake]
    }
    
    update potentiometer...
    update tire_pressure...
    update tire_temperature...
    update steering_encoder...
}
    
```



