Skip to content

Commit b9c115e

Browse files
jaz1-nordicrlubos
authored andcommitted
sdp: egpio: backends functionality improvements
Add MBOX improvements to ensuring that data has been read by the remote core. Add general structure code improvements to header files. Signed-off-by: Jakub Zymelka <jakub.zymelka@nordicsemi.no>
1 parent 3ab71f2 commit b9c115e

File tree

6 files changed

+29
-26
lines changed

6 files changed

+29
-26
lines changed

applications/sdp/gpio/src/backend/backend.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
#include <drivers/gpio/nrfe_gpio.h>
1111

12-
#if !defined(CONFIG_GPIO_NRFE_EGPIO_BACKEND_ICMSG) && \
13-
!defined(CONFIG_GPIO_NRFE_EGPIO_BACKEND_MBOX) && \
14-
!defined(CONFIG_GPIO_NRFE_EGPIO_BACKEND_ICBMSG)
12+
#if !IS_ENABLED(CONFIG_GPIO_NRFE_EGPIO_BACKEND_ICMSG) && \
13+
!IS_ENABLED(CONFIG_GPIO_NRFE_EGPIO_BACKEND_MBOX) && \
14+
!IS_ENABLED(CONFIG_GPIO_NRFE_EGPIO_BACKEND_ICBMSG)
1515
#error "Define communication backend type"
1616
#endif
1717

applications/sdp/gpio/src/backend/backend_mbox.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ static void mbox_callback(const struct device *instance, uint32_t channel, void
3434
nrfe_gpio_mbox_data_t *rx_data = (nrfe_gpio_mbox_data_t *)user_data;
3535

3636
/* Try and get lock for the shared data structure */
37-
if (atomic_flag_test_and_set(&rx_data->lock.locked)) {
38-
/* Return in case lock is not acquired (used by other core)*/
37+
if (!atomic_cas(&rx_data->lock.locked, DATA_LOCK_STATE_WITH_DATA, DATA_LOCK_STATE_BUSY)) {
38+
/* Return in case buffer is without data */
39+
atomic_set(&rx_data->lock.locked, DATA_LOCK_STATE_READY);
3940
return;
4041
}
4142

@@ -49,7 +50,7 @@ static void mbox_callback(const struct device *instance, uint32_t channel, void
4950
rx_data->lock.data_size = 0;
5051

5152
/* We are finished with the shared data structure, so we can release the lock */
52-
atomic_flag_clear(&rx_data->lock.locked);
53+
atomic_set(&rx_data->lock.locked, DATA_LOCK_STATE_READY);
5354
}
5455

5556
/**
@@ -88,8 +89,8 @@ int backend_init(backend_callback_t callback)
8889
}
8990

9091
/* clear the buffer locks and their size holders */
91-
atomic_flag_clear(&rx_data->lock.locked);
9292
rx_data->lock.data_size = 0;
93+
atomic_set(&rx_data->lock.locked, DATA_LOCK_STATE_READY);
9394

9495
return 0;
9596
}

drivers/gpio/gpio_nrfe_icmsg.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "gpio_nrfe.h"
1212

1313
#if defined(CONFIG_MULTITHREADING)
14-
K_SEM_DEFINE(bound_sem, 0, 1);
14+
static K_SEM_DEFINE(bound_sem, 0, 1);
1515
#else
1616
static volatile uint32_t bound_sem = 1;
1717
#endif

drivers/gpio/gpio_nrfe_mbox.c

+4-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <zephyr/drivers/gpio.h>
88
#include <zephyr/drivers/gpio/gpio_utils.h>
99
#include <zephyr/drivers/mbox.h>
10-
#include <zephyr/sys/printk.h>
1110

1211
#include "gpio_nrfe.h"
1312

@@ -18,19 +17,15 @@ static nrfe_gpio_mbox_data_t *tx_data =
1817

1918
int gpio_send(nrfe_gpio_data_packet_t *msg)
2019
{
21-
printk("Sending opcode: %d, pin %d, port %d, flag: %d\n", msg->opcode, msg->pin, msg->port,
22-
msg->flags);
23-
/* Try and get lock */
24-
if (atomic_flag_test_and_set(&tx_data->lock.locked)) {
25-
/* Return -1 in case lock is not acquired (used by other core)*/
26-
return -1;
20+
/* Wait for the access to the shared data structure */
21+
while (!atomic_cas(&tx_data->lock.locked, DATA_LOCK_STATE_READY, DATA_LOCK_STATE_BUSY)) {
2722
}
2823

2924
memcpy((void *)&tx_data->data, (void *)msg, sizeof(nrfe_gpio_data_packet_t));
3025
tx_data->lock.data_size = sizeof(nrfe_gpio_data_packet_t);
3126

32-
/* Release lock */
33-
atomic_flag_clear(&tx_data->lock.locked);
27+
/* Inform the consumer that new data is available */
28+
atomic_set(&tx_data->lock.locked, DATA_LOCK_STATE_WITH_DATA);
3429

3530
return mbox_send_dt(&tx_channel, NULL);
3631
}

include/drivers/gpio/nrfe_gpio.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ typedef struct __packed {
3333
*/
3434
} nrfe_gpio_data_packet_t;
3535

36-
typedef struct __packed {
37-
nrfe_shared_data_lock_t lock;
36+
typedef struct {
37+
struct nrfe_shared_data_lock lock;
3838
nrfe_gpio_data_packet_t data;
3939
} nrfe_gpio_mbox_data_t;
4040

include/sdp/nrfe_common.h

+14-7
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,29 @@
44
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
*/
66

7-
#ifndef NRFE_COMMON_H__
8-
#define NRFE_COMMON_H__
7+
#ifndef SDP_NRFE_COMMON_H__
8+
#define SDP_NRFE_COMMON_H__
99

10-
#include <stdatomic.h>
10+
#include <zephyr/sys/atomic.h>
1111

1212
#ifdef __cplusplus
1313
extern "C" {
1414
#endif
1515

16-
typedef struct __packed {
17-
atomic_bool locked;
16+
enum data_lock_state {
17+
DATA_LOCK_STATE_OFF = 0,
18+
DATA_LOCK_STATE_BUSY,
19+
DATA_LOCK_STATE_WITH_DATA,
20+
DATA_LOCK_STATE_READY,
21+
};
22+
23+
struct nrfe_shared_data_lock {
1824
uint32_t data_size;
19-
} nrfe_shared_data_lock_t;
25+
atomic_t locked;
26+
};
2027

2128
#ifdef __cplusplus
2229
}
2330
#endif
2431

25-
#endif /* NRFE_COMMON_H__ */
32+
#endif /* SDP_NRFE_COMMON_H__ */

0 commit comments

Comments
 (0)