Skip to main content

Interaction between Application and HAL

This section details the interaction between the Application Layer and HAL through the Glue Layer.

Application HAL Interaction

All hardware interactions can be divided into two types based on the initiator -

  1. Hardware initiated data-transfer/notifications: When a hardware event occurs (e.g. byte arrived on UART, DMA transfer complete etc.), an ISR in the HAL is triggered. The ISR passes data up the chain -
    ISR (in HAL)GlueAppManager (via FIFO/Mailbox)
    The App is responsible for routing the incoming data/notification to the correct Manager.
  2. Manager initiated Peripheral Access: When a Manager needs to access hardware (e.g. the Flight Control Manager needs to send motor speed commands, or the Led Manager needs to switch on/off an LED). The call chain is -
    Manager ThreadAppGlueHAL (Peripheral Access Functions)

This strict, two-way interface ensures that the Application Layer remains decoupled from the hardware, making portability easier.

Hardware Initiated Data Transfers

The following interfaces must be invoked in the ISRs to send data to the Managers -

FunctionDescription
void Glue_ReceiveRcChar(uint8_t data)Receives a character from the RC Receiver (via UART) to the Application. Must be invoked in the UART receive ISR for the UART used for the RC Receiver.
void Glue_ReceiveTelemChar(uint8_t data)Receives a character from the Telemetry Radio (via UART) to the Application. Must be invoked in the UART receive ISR for the UART used for the Telemetry Radio.
void Glue_ReceiveGnssChar(uint8_t data)Receives a character from the positioning module (via UART) to the Application. Must be invoked in the UART receive ISR for the UART used for the positioning module.
void Glue_FinishTransmissionConsole()Indicates to the Application that the transmission of the previously sent buffer to the Console (via call to Glue_StartTransmissionConsole) is complete.
void Glue_FinishTransmissionTelem()Indicates to the Application that the transmission of the previously sent buffer to the Telemetry Radio (via call to Glue_StartTransmissionTelem) is complete.

Manager Initiated Peripheral Access

The following sets of interfaces must be implemented in the HAL to allow Managers to access hardware -

High-resolution Timing

Used by sensor-fusion algorithms requiring precise, microsecond-level timing.

FunctionDescription
uint64_t Glue_GetEpochUs()Returns the number of microseconds that have passed since boot. As the function is primarily used for delta-calculation, the value does not have to reflect the microseconds since the boot, only from a fixed point before the App was initialized.

LEDs (GPIO)

Used to set the states of the indicator LEDs on the Flight Controller.

FunctionDescription
void Glue_LedOn(enum AppLed Led)Switches on the GPIO pin that controls the LED referred to by Led. If the LED was already on, do nothing.
void Glue_LedOff(enum AppLed Led)Switches off the GPIO pin that controls the LED referred to by Led. If the LED was already off, do nothing.
void Glue_LedToggle(enum AppLed Led)Toggles the state of the GPIO Pin that controls the LED referred to by Led. If the LED was already on, do nothing.

UART

Used for reception of data from the positioning module and RC receiver (usually through interrupts), and bi-directional communication with the Telemetry Radio (usually through DMA).

FunctionDescription
void Glue_StartTransmissionConsole(uint8_t *data, uint16_t len)Starts the transmission of the buffer pointed to by data with length len using DMA, on the UART peripheral used for console logging.
void Glue_StartTransmissionTelem(uint8_t *data, uint16_t len)Starts the transmission of the buffer pointed to by data with length len using DMA, on the UART peripheral connected to the Telemetry Radio.

Sensors

Used for reading/writing sensor values using I2C and SPI.

FunctionDescription
struct SensorStatus Glue_SensorRead(enum AppSensor sensor, uint8_t startReg, uint8_t *buf, uint32_t len, uint32_t timeout)Reads len number of bytes into the buffer pointed to by buf from the sensor referred to by sensor. If the operation takes longer than timeout microseconds, it gracefully fails and returns the error. The read must start from the register startReg within the sensor. The peripheral used (SPI or I2C) depends on the sensor referred to; its interpretation is left to the HAL.
struct SensorStatus Glue_SensorWrite(enum AppSensor, uint8_t *buf, uint32_t len, uint32_t timeout)Writes len number of bytes from the buffer pointed to by buf to the sensor referred to by sensor. If the operation takes longer than timeout microseconds, it gracefully fails and returns the error. The peripheral used (SPI or I2C) depends on the sensor referred to; its interpretation is left to the HAL.
void Glue_StrictDelay(uint32_t periodMs)Strictly delays for periodMs milliseconds without involving the RTOS scheduler.

Motors (PWM)

Used to send motor speed commands to the ESCs.

FunctionDescription
void Glue_MotorSet(uint16_t channel, uint16_t periodMs)Starts sending a PWM signal on the GPIO pin corresponding to the channel referred to by channel, with a period of periodMs milliseconds.