Hardware Abstraction Layer (HAL)

General Description

High level interface for interacting with reset and delays.

Features

This driver provides three categories of functionality:

Quick Start

Code Snippets

Snippet 1: Critical Section

Critical section is a portion in the code where all active interrupts are disabled. This is usually provided in places where the code execution must not be disturbed by an interrupt. An example is a firmware controlled communication protocol where timing of each byte must be maintained and any interrupt might cause loss of data.
cyhal_system_critical_section_enter returns the current state of interrupts which denote the active interrupts in the system. This must be passed as argument to cyhal_system_critical_section_exit while exiting the critical section.

// Disables all interrupts
// Application code where interrupts must be disabled
// Re-enable all interrupts that were enabled before

Snippet 2: Reset reason

cyhal_system_get_reset_reason must be called at the beginning of the main to determine the reason for reset. The return parameters are present in cyhal_reset_reason_t.

{
// WDT caused system reset
// Application code to perform some action to indicate that WDT caused the interrupt
// Clear the reset reason
}
else
{
// Reset reason is not WDT Could be POR or XRES. Specify application code to perform some
// action to indicate WDT was not the reset reaon
}

Typedefs

typedef void(* cyhal_irq_handler) (void)
 Function pointer for IRQ handlers ( cyhal_system_set_isr). More...
 

Enumerations

enum  cyhal_reset_reason_t {
  CYHAL_SYSTEM_RESET_NONE = 0,
  CYHAL_SYSTEM_RESET_WDT = 1 << 0,
  CYHAL_SYSTEM_RESET_ACTIVE_FAULT = 1 << 1,
  CYHAL_SYSTEM_RESET_DEEPSLEEP_FAULT = 1 << 2,
  CYHAL_SYSTEM_RESET_SOFT = 1 << 3,
  CYHAL_SYSTEM_RESET_HIB_WAKEUP = 1 << 4,
  CYHAL_SYSTEM_RESET_WCO_ERR = 1 << 5,
  CYHAL_SYSTEM_RESET_SYS_CLK_ERR = 1 << 6,
  CYHAL_SYSTEM_RESET_PROTECTION = 1 << 7
}
 Flags enum of possible system reset causes. More...
 

Functions

uint32_t cyhal_system_critical_section_enter (void)
 Enter a critical section. More...
 
void cyhal_system_critical_section_exit (uint32_t old_state)
 Exit a critical section. More...
 
cy_rslt_t cyhal_system_delay_ms (uint32_t milliseconds)
 Requests that the current operation delays for at least the specified length of time. More...
 
void cyhal_system_delay_us (uint16_t microseconds)
 Requests that the current operation delay for at least the specified number of micro-seconds. More...
 
cyhal_reset_reason_t cyhal_system_get_reset_reason (void)
 Gets the cause of the latest reset or resets that occurred in the system. More...
 
void cyhal_system_clear_reset_reason (void)
 Clears the reset cause registers. More...
 
cy_rslt_t cyhal_system_set_isr (int32_t irq_num, int32_t irq_src, uint8_t priority, cyhal_irq_handler handler)
 Registers the specified handler as the callback function for the specififed irq_num with the given priority. More...
 

Typedef Documentation

◆ cyhal_irq_handler

typedef void(* cyhal_irq_handler) (void)

Function pointer for IRQ handlers ( cyhal_system_set_isr).

Enumeration Type Documentation

◆ cyhal_reset_reason_t

Flags enum of possible system reset causes.

Enumerator
CYHAL_SYSTEM_RESET_NONE 

No cause.

CYHAL_SYSTEM_RESET_WDT 

A watchdog timer (WDT) reset has occurred.

CYHAL_SYSTEM_RESET_ACTIVE_FAULT 

The fault logging system requested a reset from its Active logic.

CYHAL_SYSTEM_RESET_DEEPSLEEP_FAULT 

The fault logging system requested a reset from its Deep-Sleep logic.

CYHAL_SYSTEM_RESET_SOFT 

The CPU requested a system reset through it's SYSRESETREQ.

CYHAL_SYSTEM_RESET_HIB_WAKEUP 

A reset has occurred due to a a wakeup from hibernate power mode.

CYHAL_SYSTEM_RESET_WCO_ERR 

A reset has occurred due to a watch-crystal clock error.

CYHAL_SYSTEM_RESET_SYS_CLK_ERR 

A reset has occurred due to a system clock error.

CYHAL_SYSTEM_RESET_PROTECTION 

A reset has occurred due to a protection violation.

Function Documentation

◆ cyhal_system_critical_section_enter()

uint32_t cyhal_system_critical_section_enter ( void  )

Enter a critical section.

Disables interrupts and returns a value indicating whether the interrupts were previously enabled.

Returns
Returns the state before entering the critical section. This value must be provided to cyhal_system_critical_section_exit() to properly restore the state

See Snippet 1: Critical Section for code snippet on critical section

◆ cyhal_system_critical_section_exit()

void cyhal_system_critical_section_exit ( uint32_t  old_state)

Exit a critical section.

Re-enables the interrupts if they were enabled before cyhal_system_critical_section_enter() was called. The argument should be the value returned from cyhal_system_critical_section_enter().

Parameters
[in]old_stateThe state of interrupts from cyhal_system_critical_section_enter()

See Snippet 1: Critical Section for code snippet on critical section

◆ cyhal_system_delay_ms()

cy_rslt_t cyhal_system_delay_ms ( uint32_t  milliseconds)

Requests that the current operation delays for at least the specified length of time.

If this is running in an RTOS aware environment (COMPONENTS+=RTOS_AWARE) or (DEFINES+=CY_RTOS_AWARE) it will attempt to have the RTOS suspend the current task so others can continue to run. If this is not run under an RTOS it will then defer to the standard system delay which is likely to be a busy loop. If this is part of an application that is build with RTOS awareness, but the delay should not depend on the RTOS for whatever reason, use cyhal_system_delay_us() with the appropriate 1000x multiplier to the delay time.

Parameters
[in]millisecondsThe number of milliseconds to delay for
Returns
Returns CY_RSLT_SUCCESS if the delay request was successful, otherwise error

◆ cyhal_system_delay_us()

void cyhal_system_delay_us ( uint16_t  microseconds)

Requests that the current operation delay for at least the specified number of micro-seconds.

This will generally keep the processor active in a loop for the specified length of time. If this is running under an RTOS, it will NOT attempt to run any other RTOS tasks, however if the scheduler or a high priority interrupt comes it they can take over anyway.

Parameters
[in]microsecondsThe number of micro-seconds to delay for

◆ cyhal_system_get_reset_reason()

cyhal_reset_reason_t cyhal_system_get_reset_reason ( void  )

Gets the cause of the latest reset or resets that occurred in the system.

Returns
Returns an enum of flags with the cause of the last reset(s)

Refer Snippet 2: Reset reason for code snippet on cyhal_system_get_reset_reason

◆ cyhal_system_clear_reset_reason()

void cyhal_system_clear_reset_reason ( void  )

Clears the reset cause registers.

This should be done after calling cyhal_system_get_reset_reason to make sure the reason does not persist between resets.

◆ cyhal_system_set_isr()

cy_rslt_t cyhal_system_set_isr ( int32_t  irq_num,
int32_t  irq_src,
uint8_t  priority,
cyhal_irq_handler  handler 
)

Registers the specified handler as the callback function for the specififed irq_num with the given priority.

For devices that mux interrupt sources into mcu interrupts, the irq_src defines the source of the interrupt.

Parameters
[in]irq_numThe MCU interrupt number to register the handler for.
[in]irq_srcThe interrupt source that feeds the MCU interrupt. For devices that have a 1:1 mapping between interrupt sources and MCU interrupts this should be the same as the irq_num.
[in]priorityThe MCU interrupt priority.
[in]handlerThe function pointer to call when the interrupt is triggered.
Returns
Returns CY_RSLT_SUCCESS if the set_isr request was successful, otherwise an error