10. Hardware semaphore (HSEM)

10.1 HSEM introduction

The hardware semaphore block provides 16 (32-bit) register based semaphores.

The semaphores can be used to ensure synchronization between different processes running on the core. The HSEM provides a non-blocking mechanism to lock semaphores in an atomic way. The following functions are provided:

10.2 HSEM main features

The HSEM includes the following features:

10.3 Functional description

10.3.1 HSEM block diagram

As shown in Figure 70 , the HSEM is based on three sub-blocks:

Figure 70. HSEM block diagram

Figure 70. HSEM block diagram. The diagram shows the internal structure of the HSEM block. It consists of three main sub-blocks: 'Semaphore block', 'Semaphore interface', and 'Interrupt interface'. The 'Semaphore block' contains multiple semaphores (Semaphore 0, Semaphore 1, ..., Semaphore x). The 'Semaphore interface' contains registers HSEM_R0, HSEM_RLR0, HSEM_R1, HSEM_RLR1, ..., HSEM_Rx, HSEM_RLRx. The 'Interrupt interface' contains registers HSEM_ISR, HSEM_IER, HSEM_MISR, and HSEM_ICR. The 'Semaphore block' is connected to the 'Semaphore interface' via 'Sem_Ints' signals. The 'Semaphore interface' is connected to the 'Interrupt interface' via 'Sem_Ints' signals. The 'Interrupt interface' is connected to the 'hsem_int_it' output. The 'hsem_hclk' input is connected to the 'Semaphore block'. The '32-bit AHB bus' and 'Bus master ID' are connected to the 'Semaphore interface'.
Figure 70. HSEM block diagram. The diagram shows the internal structure of the HSEM block. It consists of three main sub-blocks: 'Semaphore block', 'Semaphore interface', and 'Interrupt interface'. The 'Semaphore block' contains multiple semaphores (Semaphore 0, Semaphore 1, ..., Semaphore x). The 'Semaphore interface' contains registers HSEM_R0, HSEM_RLR0, HSEM_R1, HSEM_RLR1, ..., HSEM_Rx, HSEM_RLRx. The 'Interrupt interface' contains registers HSEM_ISR, HSEM_IER, HSEM_MISR, and HSEM_ICR. The 'Semaphore block' is connected to the 'Semaphore interface' via 'Sem_Ints' signals. The 'Semaphore interface' is connected to the 'Interrupt interface' via 'Sem_Ints' signals. The 'Interrupt interface' is connected to the 'hsem_int_it' output. The 'hsem_hclk' input is connected to the 'Semaphore block'. The '32-bit AHB bus' and 'Bus master ID' are connected to the 'Semaphore interface'.

10.3.2 HSEM internal signals

Table 70. HSEM internal input/output signals

Signal nameSignal typeDescription
AHB busDigital input/outputAHB register access bus
BusMasterIDDigital inputAHB bus master ID
hsem_int_itDigital outputinterrupt line

10.3.3 HSEM lock procedures

There are two lock procedures, namely 2-step (write) and 1-step (read) lock. The two procedures can be used concurrently.

The semaphore is free when its LOCK bit is 0. In this case, the MASTERID and PROCID are also 0. When the LOCK bit is 1, the semaphore is locked and the MASTERID indicates which AHB bus master ID has locked it. The PROCID indicates which process of that AHB bus master ID has locked the semaphore.

When write locking a semaphore, the written MASTERID must match the AHB bus master ID, and the PROCID is written by the AHB bus master software process taking the lock.

When read locking the semaphore, the MASTERID is taken from the AHB bus master ID, and the PROCID is forced to 0 by hardware. There is no PROCID available with read lock.

Figure 71. Procedure state diagram

Figure 71. Procedure state diagram showing two states: Free and Locked. Transitions are labeled 'Lock' and 'Unlock'.
stateDiagram-v2
    [*] --> Free
    [*] --> Locked
    Free --> Free : WRITE (LOCK = 0)
    Free --> Free : READ
    Free --> Locked : WRITE (MASTERID & PROCID & LOCK = 1) or READ LOCK
    Locked --> Free : WRITE (MASTERID & PROCID & LOCK = 0)
    Locked --> Locked : WRITE (LOCK = 1) or WRITE (MASTERID or PROCID)
    Locked --> Locked : READ or READ LOCK
  
Figure 71. Procedure state diagram showing two states: Free and Locked. Transitions are labeled 'Lock' and 'Unlock'.

2-step (write) lock procedure

The 2-step lock procedure consists in a write to lock the semaphore, followed by a read to check if the lock has been successful, carried out from the HSEM_Rx register.

A semaphore can be locked only when it is free.

A semaphore can be locked when the PROCID = 0.

Consecutive write attempts with LOCK = 1 to a locked semaphore are ignored.

1-step (read) lock procedure

The 1-step procedure consists in a read to lock and check the semaphore in a single step, carried out from the HSEM_RLRx register.

A semaphore can only be locked when it is free. When read locking a free semaphore, PROCID is 0. Read locking a locked semaphore returns the MASTERID and PROCID that locked it. All read locks, including the first one that locks the semaphore, return the MASTERID that locks or locked the semaphore.

Note: The 1-step procedure must not be used when running multiple processes of the same AHB bus master ID. All processes using the same semaphore read the same status. When only one process locks the semaphore, each process of that AHB bus master ID reads the semaphore as locked by itself with the MASTERID.

10.3.4 HSEM write/read/read lock register address

For each semaphore, two AHB register addresses are provided, separated in two banks of 32-bit semaphore registers, spaced by a 0x80 address offset.

In the first register address bank the semaphore can be written (locked/unlocked) and read through the HSEM_Rx registers.

In the second register address bank the semaphore can be read (locked) through the HSEM_RLRx registers.

10.3.5 HSEM unlock procedures

Unlocking a semaphore is a protected process, to prevent accidental clearing by a AHB bus master ID or by a process not having the semaphore lock right. The procedure consists in writing to the semaphore HSEM_Rx register with the corresponding MASTERID and PROCID and LOCK = 0. When unlocked, the MASTERID, and the PROCID are all 0.

When unlocked, an interrupt may be generated to signal the event. To this end, the semaphore interrupt must be enabled.

The unlock procedure consists in a write to the semaphore HSEM_Rx register with matching MASTERID regardless on how the semaphore has been locked (1- or 2-step).

Note: Different processes of the AHB bus master ID can write any PROCID value. Preventing other processes of the AHB bus master ID from unlocking a semaphore must be ensured by software, handling the PROCID correctly.

10.3.6 HSEM MASTERID semaphore clear

All semaphores locked by a MASTERID can be unlocked at once by using the HSEM_CR register. Write MASTERID and correct KEY value in HSEM_CR. All locked semaphores with a matching MASTERID are unlocked, and may generate an interrupt when enabled.

An interrupt may be generated for the unlocked semaphore(s). To this end, the semaphore interrupt must be enabled in the HSEM_IER register.

10.3.7 HSEM interrupts

An interrupt line hsem_int_it allows each semaphore to generate an interrupt.

An interrupt line provides the following features per semaphore:

With the interrupt enable (HSEM_IER) the semaphores affecting the interrupt line can be enabled. Disabled (masked) semaphore interrupts do not set the masked interrupt status MISF for that semaphore, and do not generate an interrupt on the interrupt line.

The interrupt clear (HSEM_ICR) clears the interrupt status ISF and masked interrupt status MISF of the associated semaphore for the interrupt line.

The interrupt status (HSEM_ISR) mirrors the semaphore interrupt status ISF before the enable.

The masked interrupt status (HSEM_MISR) only mirrors the semaphore enabled interrupt status MISF on the interrupt line. All masked interrupt status MISF of the enabled semaphores must be cleared to clear the interrupt line.

Figure 72. Interrupt state diagram

Interrupt state diagram for semaphore x. The flow starts at 'Semaphore x locked', proceeds to 'WRITE (MASTERID & PROCID & LOCK = 0)', then to 'Interrupt Semaphore x Status = 1'. A decision diamond 'Interrupt Semaphore x Enabled' follows. If 'No', it goes to 'Semaphore x free'. If 'Yes', it goes to 'Interrupt Semaphore x MaskedStatus = 1 & Interrupt x generated', which then leads to 'Semaphore x free'.
graph TD
    Start([Semaphore x locked]) --> WRITE[WRITE
(MASTERID & PROCID & LOCK = 0)] WRITE --> InterruptStatus[Interrupt
Semaphore x
Status = 1] InterruptStatus --> Enabled{Interrupt
Semaphore x
Enabled} Enabled -- No --> Free([Semaphore x free]) Enabled -- Yes --> InterruptGenerated[Interrupt
Semaphore x
MaskedStatus = 1 & Interrupt x generated] InterruptGenerated --> Free

MSV41951V3

Interrupt state diagram for semaphore x. The flow starts at 'Semaphore x locked', proceeds to 'WRITE (MASTERID & PROCID & LOCK = 0)', then to 'Interrupt Semaphore x Status = 1'. A decision diamond 'Interrupt Semaphore x Enabled' follows. If 'No', it goes to 'Semaphore x free'. If 'Yes', it goes to 'Interrupt Semaphore x MaskedStatus = 1 & Interrupt x generated', which then leads to 'Semaphore x free'.

The procedure to get an interrupt when a semaphore becomes free is described hereafter.

Try to lock semaphore x

On semaphore x free interrupt, try to lock semaphore x

Note: An interrupt does not lock the semaphore. After an interrupt, either the AHB bus master or the process must still perform the lock procedure to lock the semaphore.

10.3.8 AHB bus master ID verification

The HSEM allows only authorized AHB bus master ID to lock and unlock semaphores.

The relation between bus master/processor and MASTERID is detailed in Table 71 .

Table 71. Authorized AHB bus master ID

Bus master 0 (CPU)
MASTERID = 1

Note: Accesses from unauthorized AHB bus master IDs to other registers are granted.

10.4 HSEM registers

Registers must be accessed using word format. Byte and half-word accesses are ignored and have no effect on the semaphores, they generate a bus error.

10.4.1 HSEM register semaphore x (HSEM_Rx)

Address offset: \( 0x000 + 0x4 * x \) ( \( x = 0 \) to 15)

Reset value: 0x0000 0000

The HSEM_Rx must be used to perform a 2-step write lock, read back, and for unlocking a semaphore. Only write accesses with authorized AHB bus master ID is granted. Write accesses with unauthorized AHB bus master IDs are discarded.

31302928272625242322212019181716
LOCKRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
rw
1514131211109876543210
Res.Res.Res.Res.MASTERID[3:0]PROCID[7:0]
rwrwrwrwrwrwrwrwrwrwrwrw

Bit 31 LOCK : Lock indication

This bit can be written and read by software.

0: On write free semaphore (only when MASTERID and PROCID match), on read semaphore is free.

1: On write try to lock semaphore, on read semaphore is locked.

Bits 30:13 Reserved, must be kept at reset value.

Bit 12 Reserved, must be kept at reset value.

Bits 11:8 MASTERID[3:0] : Semaphore MASTERID

Written by software

Bits 7:0 PROCID[7:0] : Semaphore PROCID

Written by software

10.4.2 HSEM read lock register semaphore x (HSEM_RLRx)

Address offset: \( 0x080 + 0x4 * x \) ( \( x = 0 \) to \( 15 \) )

Reset value: \( 0x0000\ 0000 \)

Accesses the same physical bits as HSEM_Rx. The HSEM_RLRx must be used to perform a 1-step read lock. Only read accesses with authorized AHB bus master ID is granted. Read accesses with unauthorized AHB bus master IDs are discarded and return 0.

31302928272625242322212019181716
LOCKRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
r
1514131211109876543210
Res.Res.Res.Res.MASTERID[3:0]PROCID[7:0]
rrrrrrrrrrrr

Bit 31 LOCK : Lock indication

This bit is read only by software at this address.

- When the semaphore is free:

A read with a valid AHB bus master ID locks the semaphore and returns 1.

- When the semaphore is locked:

A read with a valid AHB bus master ID returns 1 (the MASTERID and PROCID reflect the already locked semaphore information).

Bits 30:13 Reserved, must be kept at reset value.

Bit 12 Reserved, must be kept at reset value.

Bits 11:8 MASTERID[3:0] : Semaphore MASTERID

This field is read only by software at this address.

On a read, when the semaphore is free, the hardware sets the MASTERID to the AHB bus master ID reading the semaphore. The MASTERID of the AHB bus master locking the semaphore is read.

On a read when the semaphore is locked, this field returns the MASTERID of the AHB bus master that has locked the semaphore.

Bits 7:0 PROCID[7:0] : Semaphore processor ID

This field is read only by software at this address.

- On a read when the semaphore is free:

A read with a valid AHB bus master ID locks the semaphore and hardware sets the PROCID to 0.

- When the semaphore is locked:

A read with a valid AHB bus master ID returns the PROCID of the AHB bus master that has locked the semaphore.

10.4.3 HSEM interrupt enable register (HSEM_IER)

Address offset: 0x100

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
ISE[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:16 Reserved, must be kept at reset value.

Bits 15:0 ISE[15:0] : Interrupt semaphore x enable bit (x = 0 to 15)

This bit is read and written by software.

10.4.4 HSEM interrupt clear register (HSEM_ICR)

Address offset: 0x104

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
ISC[15:0]
rc_w1rc_w1rc_w1rc_w1rc_w1rc_w1rc_w1rc_w1rc_w1rc_w1rc_w1rc_w1rc_w1rc_w1rc_w1rc_w1

Bits 31:16 Reserved, must be kept at reset value.

Bits 15:0 ISC[15:0] : Interrupt semaphore x clear bit (x = 0 to 15)

This bit is written by software, and is always read 0.

10.4.5 HSEM interrupt status register (HSEM_ISR)

Address offset: 0x108

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
ISF[15:0]
rrrrrrrrrrrrrrrr

Bits 31:16 Reserved, must be kept at reset value.

Bits 15:0 ISF[15:0] : Interrupt semaphore x status bit before enable (mask) (x = 0 to 15)

This bit is set by hardware, and reset only by software. This bit is cleared by software writing the corresponding HSEM_ICR bit.

0: Interrupt semaphore x status, no interrupt pending

1: Interrupt semaphore x status, interrupt pending

10.4.6 HSEM interrupt status register (HSEM_MISR)

Address offset: 0x10C

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
MISF[15:0]
rrrrrrrrrrrrrrrr

Bits 31:16 Reserved, must be kept at reset value.

Bits 15:0 MISF[15:0] : Masked interrupt semaphore x status bit after enable (mask) (x = 0 to 15)

This bit is set by hardware and read only by software. This bit is cleared by software writing the corresponding HSEM_ICR bit. This bit is read as 0 when semaphore x status is masked in HSEM_IER bit x.

0: interrupt semaphore x status after masking not pending

1: interrupt semaphore x status after masking pending

10.4.7 HSEM clear register (HSEM_CR)

Address offset: 0x140

Reset value: 0x0000 0000

Only write accesses with authorized AHB bus master ID are granted. Write accesses with unauthorized AHB bus master ID are discarded.

31302928272625242322212019181716
KEY[15:0]
wwwwwwwwwwwwwwww
1514131211109876543210
Res.Res.Res.Res.MASTERID[3:0]Res.Res.Res.Res.Res.Res.Res.Res.
wwww

Bits 31:16 KEY[15:0] : Semaphore clear key

This field can be written by software and is always read 0.

If this key value does not match HSEM_KEYR.KEY, semaphores are not affected.

If this key value matches HSEM_KEYR.KEY, all semaphores matching the MASTERID are cleared to the free state.

Bits 15:13 Reserved, must be kept at reset value.

Bit 12 Reserved, must be kept at reset value.

Bits 11:8 MASTERID[3:0] : MASTERID of semaphores to be cleared

This field can be written by software and is always read 0.

This field indicates the MASTERID for which the semaphores are cleared when writing the HSEM_CR.

Bits 7:0 Reserved, must be kept at reset value.

10.4.8 HSEM clear semaphore key register (HSEM_KEYR)

Address offset: 0x144

Reset value: 0x0000 0000

31302928272625242322212019181716
KEY[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.

Bits 31:16 KEY[15:0] : Semaphore clear key

This field can be written and read by software.

Key value to match when clearing semaphores.

Bits 15:0 Reserved, must be kept at reset value.

10.4.9 HSEM register map

Table 72. HSEM register map and reset values

OffsetRegister name313029282726252423222120191817161514131211109876543210
0x000HSEM_R0LOCKRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.MASTERID [3:0]PROCID[7:0]
Reset value0000000000000
0x004HSEM_R1LOCKRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.MASTERID [3:0]PROCID[7:0]
Reset value0000000000000
0x03CHSEM_R15LOCKRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.MASTERID [3:0]PROCID[7:0]
Reset value0000000000000
0x080HSEM_RLR0LOCKRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.MASTERID [3:0]PROCID[7:0]
Reset value0000000000000
0x084HSEM_RLR1LOCKRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.MASTERID [3:0]PROCID[7:0]
Reset value0000000000000
0x0BCHSEM_RLR15LOCKRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.MASTERID [3:0]PROCID[7:0]
Reset value0000000000000
0x100HSEM_IERRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.ISE[15:0]
Reset value000000000000
0x104HSEM_ICRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.ISC[15:0]
Reset value000000000000
0x108HSEM_ISRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.ISF[15:0]
Reset value000000000000
0x10CHSEM_MISRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.MISF[15:0]
Reset value000000000000
0x140HSEM_CRKEY[15:0]Res.Res.Res.Res.MASTERID[3:0]Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
Reset value00000000000000000000
0x144HSEM_KEYRKEY[15:0]Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
Reset value0000000000000000

Refer to Section 2.3: Memory organization for the register boundary addresses.