Skip to content

Commit 04c77bb

Browse files
committed
Fix hw_is_claimed, and add xxx_is_claimed APIs
1 parent ae7f857 commit 04c77bb

File tree

12 files changed

+94
-20
lines changed

12 files changed

+94
-20
lines changed

src/rp2_common/hardware_claim/claim.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,13 @@ void hw_claim_unlock(uint32_t save) {
1414
spin_unlock(spin_lock_instance(PICO_SPINLOCK_ID_HARDWARE_CLAIM), save);
1515
}
1616

17-
bool hw_is_claimed(uint8_t *bits, uint bit_index) {
18-
bool rc;
19-
uint32_t save = hw_claim_lock();
20-
if (bits[bit_index >> 3u] & (1u << (bit_index & 7u))) {
21-
rc = false;
22-
} else {
23-
bits[bit_index >> 3u] |= (uint8_t)(1u << (bit_index & 7u));
24-
rc = true;
25-
}
26-
hw_claim_unlock(save);
27-
return rc;
17+
inline bool hw_is_claimed(const uint8_t *bits, uint bit_index) {
18+
return (bits[bit_index >> 3u] & (1u << (bit_index & 7u)));
2819
}
2920

3021
void hw_claim_or_assert(uint8_t *bits, uint bit_index, const char *message) {
3122
uint32_t save = hw_claim_lock();
32-
if (bits[bit_index >> 3u] & (1u << (bit_index & 7u))) {
23+
if (hw_is_claimed(bits, bit_index)) {
3324
panic(message, bit_index);
3425
} else {
3526
bits[bit_index >> 3u] |= (uint8_t)(1u << (bit_index & 7u));
@@ -42,7 +33,7 @@ int hw_claim_unused_from_range(uint8_t *bits, bool required, uint bit_lsb, uint
4233
uint32_t save = hw_claim_lock();
4334
int found_bit = -1;
4435
for(uint bit=bit_lsb; bit <= bit_msb; bit++) {
45-
if (!(bits[bit >> 3u] & (1u << (bit & 7u)))) {
36+
if (!hw_is_claimed(bits, bit)) {
4637
bits[bit >> 3u] |= (uint8_t)(1u << (bit & 7u));
4738
found_bit = (int)bit;
4839
break;
@@ -57,7 +48,7 @@ int hw_claim_unused_from_range(uint8_t *bits, bool required, uint bit_lsb, uint
5748

5849
void hw_claim_clear(uint8_t *bits, uint bit_index) {
5950
uint32_t save = hw_claim_lock();
60-
assert(bits[bit_index >> 3u] & (1u << (bit_index & 7u)));
51+
assert(hw_is_claimed(bits, bit_index));
6152
bits[bit_index >> 3u] &= (uint8_t) ~(1u << (bit_index & 7u));
6253
hw_claim_unlock(save);
6354
}

src/rp2_common/hardware_claim/include/hardware/claim.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ int hw_claim_unused_from_range(uint8_t *bits, bool required, uint bit_lsb, uint
6868
* \param bit_index resource to unclaim (bit index into array of bits)
6969
* \return true if the resource is claimed
7070
*/
71-
bool hw_is_claimed(uint8_t *bits, uint bit_index);
71+
bool hw_is_claimed(const uint8_t *bits, uint bit_index);
7272

7373
/*! \brief Atomically unclaim a resource
7474
* \ingroup hardware_claim

src/rp2_common/hardware_dma/dma.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ int dma_claim_unused_channel(bool required) {
3939
return hw_claim_unused_from_range((uint8_t*)&_claimed, required, 0, NUM_DMA_CHANNELS-1, "No DMA channels are available");
4040
}
4141

42+
bool dma_channel_is_claimed(uint channel) {
43+
check_dma_channel_param(channel);
44+
return hw_is_claimed((uint8_t *) &_claimed, channel);
45+
}
46+
4247
#ifndef NDEBUG
4348

4449
void print_dma_ctrl(dma_channel_hw_t *channel) {

src/rp2_common/hardware_dma/include/hardware/dma.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ void dma_channel_unclaim(uint channel);
9898
*/
9999
int dma_claim_unused_channel(bool required);
100100

101+
/*! \brief Determine if a dma channel is claimed
102+
* \ingroup hardware_dma
103+
*
104+
* \param channel the dma channel
105+
* \see dma_channel_claim
106+
* \see dma_channel_claim_mask
107+
*/
108+
bool dma_channel_is_claimed(uint channel);
109+
101110
/** \brief DMA channel configuration
102111
* \defgroup channel_config channel_config
103112
* \ingroup hardware_dma

src/rp2_common/hardware_interp/include/hardware/interp.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ typedef struct {
5555
} interp_config;
5656

5757
static inline uint interp_index(interp_hw_t *interp) {
58-
assert(interp == interp0 || interp == interp1);
58+
valid_params_if(INTERP, interp == interp0 || interp == interp1);
5959
return interp == interp1 ? 1 : 0;
6060
}
6161

@@ -70,6 +70,8 @@ static inline uint interp_index(interp_hw_t *interp) {
7070
* \param lane The lane number, 0 or 1.
7171
*/
7272
void interp_claim_lane(interp_hw_t *interp, uint lane);
73+
// The above really should be called this for consistency
74+
#define interp_lane_claim interp_claim_lane
7375

7476
/*! \brief Claim the interpolator lanes specified in the mask
7577
* \ingroup hardware_interp
@@ -86,6 +88,19 @@ void interp_claim_lane_mask(interp_hw_t *interp, uint lane_mask);
8688
* \param lane The lane number, 0 or 1
8789
*/
8890
void interp_unclaim_lane(interp_hw_t *interp, uint lane);
91+
// The above really should be called this for consistency
92+
#define interp_lane_unclaim interp_unclaim_lane
93+
94+
/*! \brief Determine if an interpolator lane is claimed
95+
* \ingroup hardware_interp
96+
*
97+
* \param interp Interpolator whose lane to check
98+
* \param lane The lane number, 0 or 1
99+
* \return true if claimed, false otherwise
100+
* \see interp_claim_lane
101+
* \see interp_claim_lane_mask
102+
*/
103+
bool interp_lane_is_claimed(interp_hw_t *interp, uint lane);
89104

90105
/*! \brief Set the interpolator shift value
91106
* \ingroup interp_config

src/rp2_common/hardware_interp/interp.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ static_assert(NUM_DMA_CHANNELS <= 16, "");
1616

1717
static uint8_t _claimed;
1818

19+
static inline uint interp_lane_bit(interp_hw_t * interp, uint lane) {
20+
return (interp_index(interp) << 1u) | lane;
21+
}
22+
1923
void interp_claim_lane(interp_hw_t *interp, uint lane) {
2024
valid_params_if(INTERP, lane < 2);
21-
uint bit = (interp_index(interp) << 1u) | lane;
22-
hw_claim_or_assert((uint8_t *) &_claimed, bit, "Lane is already claimed");
25+
hw_claim_or_assert((uint8_t *) &_claimed, interp_lane_bit(interp, lane), "Lane is already claimed");
2326
}
2427

2528
void interp_claim_lane_mask(interp_hw_t *interp, uint lane_mask) {
@@ -30,8 +33,12 @@ void interp_claim_lane_mask(interp_hw_t *interp, uint lane_mask) {
3033

3134
void interp_unclaim_lane(interp_hw_t *interp, uint lane) {
3235
valid_params_if(INTERP, lane < 2);
33-
uint bit = (interp_index(interp) << 1u) | lane;
34-
hw_claim_clear((uint8_t *) &_claimed, bit);
36+
hw_claim_clear((uint8_t *) &_claimed, interp_lane_bit(interp, lane));
37+
}
38+
39+
bool interp_lane_is_claimed(interp_hw_t *interp, uint lane) {
40+
valid_params_if(INTERP, lane < 2);
41+
return hw_is_claimed((uint8_t *) &_claimed, interp_lane_bit(interp, lane));
3542
}
3643

3744
void interp_save(interp_hw_t *interp, interp_hw_save_t *saver) {

src/rp2_common/hardware_pio/include/hardware/pio.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,17 @@ void pio_sm_unclaim(PIO pio, uint sm);
11361136
*/
11371137
int pio_claim_unused_sm(PIO pio, bool required);
11381138

1139+
/*! \brief Determine if a PIO state machine is claimed
1140+
* \ingroup hardware_pio
1141+
*
1142+
* \param pio The PIO instance; either \ref pio0 or \ref pio1
1143+
* \param sm State machine index (0..3)
1144+
* \return true if claimed, false otherwise
1145+
* \see pio_sm_claim
1146+
* \see pio_claim_sm_mask
1147+
*/
1148+
bool pio_sm_is_claimed(PIO pio, uint sm);
1149+
11391150
#ifdef __cplusplus
11401151
}
11411152
#endif

src/rp2_common/hardware_pio/pio.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void pio_claim_sm_mask(PIO pio, uint sm_mask) {
3535
if (sm_mask & 1u) pio_sm_claim(pio, i);
3636
}
3737
}
38+
3839
void pio_sm_unclaim(PIO pio, uint sm) {
3940
check_sm_param(sm);
4041
uint which = pio_get_index(pio);
@@ -50,6 +51,12 @@ int pio_claim_unused_sm(PIO pio, bool required) {
5051
return index >= (int)base ? index - (int)base : -1;
5152
}
5253

54+
bool pio_sm_is_claimed(PIO pio, uint sm) {
55+
check_sm_param(sm);
56+
uint which = pio_get_index(pio);
57+
return hw_is_claimed(&claimed, which * NUM_PIO_STATE_MACHINES + sm);
58+
}
59+
5360
static_assert(PIO_INSTRUCTION_COUNT <= 32, "");
5461
static uint32_t _used_instruction_space[2];
5562

src/rp2_common/hardware_sync/include/hardware/sync.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,16 @@ void spin_lock_unclaim(uint lock_num);
341341
*/
342342
int spin_lock_claim_unused(bool required);
343343

344+
/*! \brief Determine if a spin lock is claimed
345+
* \ingroup hardware_sync
346+
*
347+
* \param lock_num the spin lock number
348+
* \return true if claimed, false otherwise
349+
* \see spin_lock_claim
350+
* \see spin_lock_claim_mask
351+
*/
352+
bool spin_lock_is_claimed(uint lock_num);
353+
344354
#define remove_volatile_cast(t, x) ({__mem_fence_acquire(); (t)(x); })
345355

346356
#ifdef __cplusplus

src/rp2_common/hardware_sync/sync.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,8 @@ int spin_lock_claim_unused(bool required) {
5757
return hw_claim_unused_from_range((uint8_t*)&claimed, required, PICO_SPINLOCK_ID_CLAIM_FREE_FIRST, PICO_SPINLOCK_ID_CLAIM_FREE_END, "No spinlocks are available");
5858
}
5959

60+
bool spin_lock_is_claimed(uint lock_num) {
61+
check_lock_num(lock_num);
62+
return hw_is_claimed((uint8_t *) &claimed, lock_num);
63+
}
64+

0 commit comments

Comments
 (0)