System¶

group group_hal_system

High level interface for interacting with reset and delays.

Features

This driver provides three categories of functionality:

• Ability to get the last reset reason.

• Ability to delay for a period of time.

• The ability to disable interrupts during a critical section.

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
uint32_t state = cyhal_system_critical_section_enter();

// Application code where interrupts must be disabled

// Re-enable all interrupts that were enabled before
cyhal_system_critical_section_exit(state);


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.

    if (CYHAL_SYSTEM_RESET_WDT == cyhal_system_get_reset_reason())
{
// WDT caused system reset

// Application code to perform some action to indicate that WDT caused the interrupt

// Clear the reset reason
cyhal_system_clear_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).

Enums

enum cyhal_reset_reason_t

cyhal_reset_reason_t: Flags enum of possible system reset causes.

Values:

enumerator CYHAL_SYSTEM_RESET_NONE

No cause.

enumerator CYHAL_SYSTEM_RESET_WDT

A watchdog timer (WDT) reset has occurred.

enumerator CYHAL_SYSTEM_RESET_ACTIVE_FAULT

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

enumerator CYHAL_SYSTEM_RESET_DEEPSLEEP_FAULT

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

enumerator CYHAL_SYSTEM_RESET_SOFT

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

enumerator CYHAL_SYSTEM_RESET_HIB_WAKEUP

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

enumerator CYHAL_SYSTEM_RESET_WCO_ERR

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

enumerator CYHAL_SYSTEM_RESET_SYS_CLK_ERR

A reset has occurred due to a system clock error.

enumerator CYHAL_SYSTEM_RESET_PROTECTION

A reset has occurred due to a protection violation.

Functions

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.

See

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

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

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().

See

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

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.

Return

Returns CY_RSLT_SUCCESS if the delay request was successful, otherwise error

Parameters
• [in] milliseconds: The number of milliseconds to delay for

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] microseconds: The number of micro-seconds to delay for

cyhal_reset_reason_t cyhal_system_get_reset_reason(void)

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

Refer

Snippet 2: Reset reason for code snippet on cyhal_system_get_reset_reason
Return

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

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.

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.

Return

Returns CY_RSLT_SUCCESS if the set_isr request was successful, otherwise an error

Parameters
• [in] irq_num: The MCU interrupt number to register the handler for.

• [in] irq_src: The 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] priority: The MCU interrupt priority.

• [in] handler: The function pointer to call when the interrupt is triggered.