Solaris Software
Toggle main menu visibility
Loading...
Searching...
No Matches
uart.c
Go to the documentation of this file.
1
/**
2
* @file uart.c
3
* @brief UART telemetry consumer service implementation.
4
*
5
* The service keeps a small FIFO of SPP packets delivered by the PUBSUB
6
* router. Every superloop tick, @ref SPP_SERVICES_UART_consumeData transmits
7
* the oldest pending packet over the UART HAL, byte-for-byte.
8
*/
9
10
/* ----------------------------------------------------------------
11
* INCLUDES
12
* ---------------------------------------------------------------- */
13
#include "
spp/services/uart/uart.h
"
14
15
#include "
spp/hal/uart/uart.h
"
16
17
/* ----------------------------------------------------------------
18
* STATIC FUNCTIONS DECLARATIONS
19
* ---------------------------------------------------------------- */
20
static
SPP_RetVal_t
SPP_SERVICES_UART_init
(
void
);
21
static
SPP_RetVal_t
SPP_SERVICES_UART_deliverToMailbox
(
const
SPP_Packet_t
packet);
22
static
SPP_RetVal_t
SPP_SERVICES_UART_consumeData
(
void
*p_data);
23
24
/* ----------------------------------------------------------------
25
* VARIABLES
26
* ---------------------------------------------------------------- */
27
static
SPP_Packet_t
s_mailbox
[
K_UART_MAILBOX_SIZE
];
28
static
spp_uint8_t
s_mailboxHead
= 0U;
29
static
spp_uint8_t
s_mailboxTail
= 0U;
30
static
spp_uint8_t
s_mailboxCount
= 0U;
31
32
static
SPP_RetVal_t
s_initStatus
=
K_SPP_ERROR_NOT_INITIALIZED
;
33
34
static
const
SPP_SERVICE_ConsumerContract_t
g_uartConsumerContract
= {
35
.priority =
K_UART_CONSUMER_PRIO
,
36
.p_nameConsumer =
"uart"
,
37
.tiemoutMs =
K_UART_TIMEOUT_MS
,
38
.init =
SPP_SERVICES_UART_init
,
39
.deliverToMailbox =
SPP_SERVICES_UART_deliverToMailbox
,
40
.consumeData =
SPP_SERVICES_UART_consumeData
,
41
};
42
43
/* ----------------------------------------------------------------
44
* PUBLIC FUNCTIONS
45
* ---------------------------------------------------------------- */
46
const
SPP_SERVICE_ConsumerContract_t
*
SPP_SERVICES_UART_getConsumerContract
(
void
)
47
{
48
return
&
g_uartConsumerContract
;
49
}
50
51
SPP_RetVal_t
SPP_SERVICES_UART_getInitStatus
(
void
)
52
{
53
return
s_initStatus
;
54
}
55
56
/* ----------------------------------------------------------------
57
* STATIC FUNCTIONS
58
* ---------------------------------------------------------------- */
59
60
/**
61
* @brief Initialise the UART port and clear the packet mailbox.
62
* @return K_SPP_OK on success, error code from the HAL otherwise.
63
*/
64
static
SPP_RetVal_t
SPP_SERVICES_UART_init
(
void
)
65
{
66
s_mailboxHead
= 0U;
67
s_mailboxTail
= 0U;
68
s_mailboxCount
= 0U;
69
70
s_initStatus
=
SPP_HAL_UART_portInit
();
71
return
s_initStatus
;
72
}
73
74
/**
75
* @brief Store a packet delivered by the PUBSUB router.
76
* @param packet Packet dispatched by the router.
77
* @return K_SPP_OK always; when the FIFO is full the oldest packet is dropped.
78
*/
79
static
SPP_RetVal_t
SPP_SERVICES_UART_deliverToMailbox
(
const
SPP_Packet_t
packet)
80
{
81
s_mailbox
[
s_mailboxTail
] = packet;
82
s_mailboxTail
= (
spp_uint8_t
)((
s_mailboxTail
+ 1U) %
K_UART_MAILBOX_SIZE
);
83
84
if
(
s_mailboxCount
<
K_UART_MAILBOX_SIZE
)
85
{
86
s_mailboxCount
++;
87
}
88
else
89
{
90
s_mailboxHead
= (
spp_uint8_t
)((
s_mailboxHead
+ 1U) %
K_UART_MAILBOX_SIZE
);
91
}
92
93
return
K_SPP_OK
;
94
}
95
96
/**
97
* @brief Transmit the oldest pending SPP packet over UART.
98
* @param p_data Unused (context pointer reserved for future use).
99
* @return K_SPP_OK on success (or empty mailbox), error code from the HAL otherwise.
100
*/
101
static
SPP_RetVal_t
SPP_SERVICES_UART_consumeData
(
void
*p_data)
102
{
103
(void)p_data;
104
105
if
(
s_mailboxCount
== 0U)
106
{
107
return
K_SPP_OK
;
108
}
109
110
SPP_RetVal_t
ret =
SPP_HAL_UART_transmit
(&
s_mailbox
[
s_mailboxHead
], (
spp_uint16_t
)
sizeof
(
SPP_Packet_t
));
111
if
(ret ==
K_SPP_OK
)
112
{
113
s_mailboxHead
= (
spp_uint8_t
)((
s_mailboxHead
+ 1U) %
K_UART_MAILBOX_SIZE
);
114
s_mailboxCount
--;
115
}
116
117
return
ret;
118
}
SPP_HAL_UART_transmit
SPP_RetVal_t SPP_HAL_UART_transmit(const void *p_data, spp_uint16_t len)
Perform UART transaction.
Definition
uart.c:44
SPP_HAL_UART_portInit
SPP_RetVal_t SPP_HAL_UART_portInit(void)
Initialise the UART port.
Definition
uart.c:22
uart.h
SPP UART HAL API — dispatches through the registered HAL port.
SPP_Packet_t
SPP_Packet_t
Definition
packet.h:79
SPP_RetVal_t
SPP_RetVal_t
Unified return type for all SPP API functions.
Definition
returnTypes.h:24
K_SPP_ERROR_NOT_INITIALIZED
@ K_SPP_ERROR_NOT_INITIALIZED
Definition
returnTypes.h:31
K_SPP_OK
@ K_SPP_OK
Definition
returnTypes.h:25
SPP_SERVICES_UART_getConsumerContract
const SPP_SERVICE_ConsumerContract_t * SPP_SERVICES_UART_getConsumerContract(void)
Get the UART consumer contract.
Definition
uart.c:46
s_mailboxHead
static spp_uint8_t s_mailboxHead
Definition
uart.c:28
s_mailbox
static SPP_Packet_t s_mailbox[K_UART_MAILBOX_SIZE]
Definition
uart.c:27
SPP_SERVICES_UART_getInitStatus
SPP_RetVal_t SPP_SERVICES_UART_getInitStatus(void)
Get the result of the last UART service init.
Definition
uart.c:51
s_initStatus
static SPP_RetVal_t s_initStatus
Definition
uart.c:32
s_mailboxTail
static spp_uint8_t s_mailboxTail
Definition
uart.c:29
s_mailboxCount
static spp_uint8_t s_mailboxCount
Definition
uart.c:30
SPP_SERVICES_UART_consumeData
static SPP_RetVal_t SPP_SERVICES_UART_consumeData(void *p_data)
Transmit the oldest pending SPP packet over UART.
Definition
uart.c:101
g_uartConsumerContract
static const SPP_SERVICE_ConsumerContract_t g_uartConsumerContract
Definition
uart.c:34
SPP_SERVICES_UART_deliverToMailbox
static SPP_RetVal_t SPP_SERVICES_UART_deliverToMailbox(const SPP_Packet_t packet)
Store a packet delivered by the PUBSUB router.
Definition
uart.c:79
SPP_SERVICES_UART_init
static SPP_RetVal_t SPP_SERVICES_UART_init(void)
Initialise the UART port and clear the packet mailbox.
Definition
uart.c:64
uart.h
UART telemetry consumer service.
K_UART_TIMEOUT_MS
#define K_UART_TIMEOUT_MS
Definition
uart.h:21
K_UART_MAILBOX_SIZE
#define K_UART_MAILBOX_SIZE
Definition
uart.h:22
K_UART_CONSUMER_PRIO
#define K_UART_CONSUMER_PRIO
Definition
uart.h:20
SPP_SERVICE_ConsumerContract_t
Describes the contract of a consumer module. This module needs to have:
Definition
service.h:40
spp_uint16_t
uint16_t spp_uint16_t
Definition
types.h:29
spp_uint8_t
uint8_t spp_uint8_t
Definition
types.h:27
solaris-v2
spp
services
uart
uart.c
Generated by
1.18.0