From 1da807e7a8bb61a8eea1052075037b402335674d Mon Sep 17 00:00:00 2001 From: Peter Mitsis Date: Thu, 6 Oct 2016 11:36:59 -0400 Subject: [PATCH] unified: Tweak msgq API parameters - Reorders parameters where necessary - Adds alignment parameter to K_MSGQ_DEFINE() for buffer alignment - Renames parameters where necessary so they are more intuitive Change-Id: I0b53105c04109127897bf4790e6908082f82da4e Signed-off-by: Peter Mitsis --- doc/kernel_v2/data_passing/message_queues.rst | 6 +-- include/kernel.h | 37 ++++++++++++++----- include/legacy.h | 2 +- kernel/unified/msg_q.c | 6 +-- scripts/sysgen | 4 +- 5 files changed, 36 insertions(+), 19 deletions(-) diff --git a/doc/kernel_v2/data_passing/message_queues.rst b/doc/kernel_v2/data_passing/message_queues.rst index 84155035f8a..4fb08a46e83 100644 --- a/doc/kernel_v2/data_passing/message_queues.rst +++ b/doc/kernel_v2/data_passing/message_queues.rst @@ -73,10 +73,10 @@ that is capable of holding 10 items. ... }; - char my_msgq_buffer[10 * sizeof(data_item_type)]; + char __aligned(4) my_msgq_buffer[10 * sizeof(data_item_type)]; struct k_msgq my_msgq; - k_msgq_init(&my_msgq, 10, sizeof(data_item_type), my_msgq_buffer); + k_msgq_init(&my_msgq, my_msgq_buffer, sizeof(data_item_type), 10); Alternatively, a message queue can be defined and initialized at compile time by calling :c:macro:`K_MSGQ_DEFINE()`. @@ -86,7 +86,7 @@ that the macro defines both the message queue and its buffer. .. code-block:: c - K_MSGQ_DEFINE(my_msgq, 10, sizeof(data_item_type)); + K_MSGQ_DEFINE(my_msgq, sizeof(data_item_type), 10, 4); Writing to a Message Queue ========================== diff --git a/include/kernel.h b/include/kernel.h index 1af556aab81..96358881e38 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -856,26 +856,43 @@ struct k_msgq { _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq); }; -#define K_MSGQ_INITIALIZER(obj, q_depth, q_width, q_buffer) \ +#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \ { \ .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ - .max_msgs = q_depth, \ - .msg_size = q_width, \ + .max_msgs = q_max_msgs, \ + .msg_size = q_msg_size, \ .buffer_start = q_buffer, \ - .buffer_end = q_buffer + (q_depth * q_width), \ + .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \ .read_ptr = q_buffer, \ .write_ptr = q_buffer, \ .used_msgs = 0, \ _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ } -#define K_MSGQ_DEFINE(name, q_depth, q_width) \ - static char __noinit _k_fifo_buf_##name[(q_depth) * (q_width)]; \ - struct k_msgq name = \ - K_MSGQ_INITIALIZER(name, q_depth, q_width, _k_fifo_buf_##name) +/** + * @brief Define a message queue + * + * This declares and initializes a message queue whose buffer is aligned to + * a @a q_align -byte boundary. The new message queue can be passed to the + * kernel's message queue functions. + * + * Note that for each of the mesages in the message queue to be aligned to + * @a q_align bytes, then @a q_msg_size must be a multiple of @a q_align. + * + * @param q_name Name of the message queue + * @param q_msg_size The size in bytes of each message + * @param q_max_msgs Maximum number of messages the queue can hold + * @param q_align Alignment of the message queue's buffer (power of 2) + */ +#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \ + static char __noinit __aligned(q_align) \ + _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \ + struct k_msgq q_name = \ + K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \ + q_msg_size, q_max_msgs) -extern void k_msgq_init(struct k_msgq *q, uint32_t msg_size, - uint32_t max_msgs, char *buffer); +extern void k_msgq_init(struct k_msgq *q, char *buffer, + uint32_t msg_size, uint32_t max_msgs); extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout); extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout); extern void k_msgq_purge(struct k_msgq *q); diff --git a/include/legacy.h b/include/legacy.h index 33081c38a6a..5b22b03ec46 100644 --- a/include/legacy.h +++ b/include/legacy.h @@ -445,7 +445,7 @@ static inline int task_fifo_size_get(kfifo_t queue) } #define DEFINE_FIFO(name, q_depth, q_width) \ - K_MSGQ_DEFINE(_k_fifo_obj_##name, q_depth, q_width); \ + K_MSGQ_DEFINE(_k_fifo_obj_##name, q_width, q_depth, 4); \ struct k_msgq * const name = &_k_fifo_obj_##name /* mailboxes */ diff --git a/kernel/unified/msg_q.c b/kernel/unified/msg_q.c index 4195041cdff..a6776d59966 100644 --- a/kernel/unified/msg_q.c +++ b/kernel/unified/msg_q.c @@ -33,14 +33,14 @@ * @brief Initialize a message queue. * * @param q Pointer to the message queue object. + * @param buffer Pointer to memory area that holds queued messages. * @param msg_size Message size, in bytes. * @param max_msgs Maximum number of messages that can be queued. - * @param buffer Pointer to memory area that holds queued messages. * * @return N/A */ -void k_msgq_init(struct k_msgq *q, uint32_t msg_size, uint32_t max_msgs, - char *buffer) +void k_msgq_init(struct k_msgq *q, char *buffer, + uint32_t msg_size, uint32_t max_msgs) { q->msg_size = msg_size; q->max_msgs = max_msgs; diff --git a/scripts/sysgen b/scripts/sysgen index 0b792b0d3ec..9efdd78b3f1 100755 --- a/scripts/sysgen +++ b/scripts/sysgen @@ -684,8 +684,8 @@ def kernel_main_c_fifos(): name = fifo[0] depth = fifo[1] width = fifo[2] - kernel_main_c_out("K_MSGQ_DEFINE(_k_fifo_obj_%s, %s, %s);\n" % - (name, depth, width)) + kernel_main_c_out("K_MSGQ_DEFINE(_k_fifo_obj_%s, %s, %s, 4);\n" % + (name, width, depth)) def kernel_main_c_pipes():