Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
msg_queue.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016 Gautier Hattenberger, Alexandre Bustico
3 *
4 * This file is part of paparazzi.
5 *
6 * paparazzi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * paparazzi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with paparazzi; see the file COPYING. If not, write to
18 * the Free Software Foundation, 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22/*
23 * @file modules/loggers/sdlog_chibios/msg_queue.c
24 *
25 */
28#include <string.h>
29
30#define MSGQ_HEAP HEAP_CCM
31
32
33typedef union {
34 struct {
35 uint16_t ptrOfst;
36 uint16_t len;
37 };
39} MsgPtrLen;
40
41
43 msg_t *mb_buf, const cnt_t mb_size)
44{
46 memset(mb_buf, 0, mb_size * sizeof(msg_t));
47 que->heap = heap;
48}
49
51{
52 chSysLock();
53 const bool queue_full = chMBGetFreeCountI(&que->mb) <= 0;
55
56 return queue_full;
57}
58
60{
61 chSysLock();
62 bool queue_empty = (chMBGetUsedCountI(&que->mb) <= 0);
64
65 return queue_empty;
66}
67
68
74
75
78{
79 // this code prevents compiler warning about a missing initializer of mpl
80 MsgPtrLen m;
81 m.len = msgLen;
83 const MsgPtrLen mpl = m;
84
85#if CH_DBG_ENABLE_CHECKS
87 ((uint32_t) msg >= ((uint32_t) tlsf_get_heap_addr(&MSGQ_HEAP) + 65535))) {
88#if CH_DBG_ENABLE_ASSERTS
89 chSysHalt("MsgQueue_INVALID_PTR");
90#else
92#endif
93 }
94#endif
95
97 if (chMBPostTimeout(&que->mb, mpl.msg_ptr_len, timout) != MSG_OK) {
98 goto fail;
99 }
100 } else {
101 if (chMBPostAheadTimeout(&que->mb, mpl.msg_ptr_len, timout) != MSG_OK) {
102 goto fail;
103 }
104 }
105 return msgLen;
106
107fail:
108
109 tlsf_free_r(que->heap, msg);
110
112}
113
116{
118
119 if (dst == NULL) {
121 }
122
123 memcpy(dst, msg, msgLen);
125}
126
132
133
138
140{
141 MsgPtrLen mpl = {.ptrOfst = 0, .len = 0};
142
144 do {
145 status = chMBFetchTimeout(&que->mb, (msg_t *) &mpl.msg_ptr_len, timout);
146 } while (status == MSG_RESET);
147
148 if (status != MSG_OK) {
150 }
151
152 *msgPtr = (void *)(mpl.ptrOfst + (uint32_t) tlsf_get_heap_addr(&MSGQ_HEAP));
153 return mpl.len;
154}
155
156
157
158
159static const char *_strerror[] = {"MsgQueue_MAILBOX_FULL",
160 "MsgQueue_MAILBOX_FAIL",
161 "MsgQueue_MAILBOX_TIMEOUT",
162 "MsgQueue_MAILBOX_NOT_EMPTY",
163 "MsgQueue_OK",
164 "MsgQueue_INVALID_PTR",
165 "MsgQueue_INVALID_ERRNO"
166 };
167
169{
171 const size_t arrayLen = sizeof(_strerror) / sizeof(_strerror[0]);
172 indice = (indice < arrayLen) ? indice : arrayLen - 1;
173 return _strerror[indice];
174}
175
176
177
178
static uint8_t status
uint8_t msg[10]
Buffer used for general comunication over SPI (out buffer)
uint16_t foo
Definition main_demo5.c:58
bool msgqueue_is_empty(MsgQueue *que)
test if queue is empty
Definition msg_queue.c:59
int32_t msgqueue_pop_timeout(MsgQueue *que, void **msgPtr, const systime_t timout)
receive message specifying timeout
Definition msg_queue.c:139
int32_t msgqueue_send(MsgQueue *que, void *msg, const uint16_t msgLen, const MsgQueueUrgency urgency)
send a buffer previously allocated by msgqueue_malloc_before_send
Definition msg_queue.c:69
int32_t msgqueue_pop(MsgQueue *que, void **msgPtr)
wait then receive message
Definition msg_queue.c:134
msg_t msg_ptr_len
Definition msg_queue.c:38
bool msgqueue_is_full(MsgQueue *que)
test if queue is full
Definition msg_queue.c:50
void msgqueue_init(MsgQueue *que, tlsf_memory_heap_t *heap, msg_t *mb_buf, const cnt_t mb_size)
initialise MsgQueue
Definition msg_queue.c:42
int32_t msgqueue_send_timeout(MsgQueue *que, void *msg, const uint16_t msgLen, const MsgQueueUrgency urgency, const systime_t timout)
send a buffer previously allocated by msgqueue_malloc_before_send
Definition msg_queue.c:76
int32_t msgqueue_copy_send_timeout(MsgQueue *que, const void *msg, const uint16_t msgLen, const MsgQueueUrgency urgency, const systime_t timout)
send a buffer NOT previously allocated
Definition msg_queue.c:114
static const char * _strerror[]
Definition msg_queue.c:159
const char * msgqueue_strerror(const MsgQueueStatus _errno)
debug api
Definition msg_queue.c:168
int32_t msgqueue_copy_send(MsgQueue *que, const void *msg, const uint16_t msgLen, const MsgQueueUrgency urgency)
send a buffer NOT previously allocated
Definition msg_queue.c:127
#define MSGQ_HEAP
Definition msg_queue.c:30
MsgQueueUrgency
Definition msg_queue.h:47
@ MsgQueue_REGULAR
Definition msg_queue.h:47
MsgQueueStatus
Definition msg_queue.h:38
@ MsgQueue_MAILBOX_FULL
Definition msg_queue.h:39
@ MsgQueue_MAILBOX_TIMEOUT
Definition msg_queue.h:41
@ MsgQueue_INVALID_PTR
Definition msg_queue.h:43
Dynamic memory allocation based on TLSF library.
void tlsf_free_r(tlsf_memory_heap_t *heap, void *ptr)
void * tlsf_get_heap_addr(const tlsf_memory_heap_t *heap)
void * tlsf_malloc_r(tlsf_memory_heap_t *heap, size_t bytes)
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
int int32_t
Typedef defining 32 bit int type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.