-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make queue interrupt safe for 1 producer + consumer (#80)
* Queue interrupt safe if 1 prod + cons * CAN Parse generated updates
- Loading branch information
Showing
16 changed files
with
119 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,72 @@ | ||
#include "queue.h" | ||
|
||
// @funcname: qConstruct | ||
// | ||
// @brief: Initializes queue for specific item type | ||
// | ||
// @param: q: Handle for queue | ||
// @param: size: Size of each item in queue in bytes | ||
/** | ||
* @brief Initializes queue for specific item type | ||
* | ||
* @param q Handle for queue | ||
* @param size Size of each item in queue in bytes | ||
*/ | ||
void qConstruct(q_handle_t* q, uint8_t size) | ||
{ | ||
q->item_count = 0; // Initialize number of items to 0 | ||
q->size = size; // Initialize size to sizeof(struct) | ||
q->start = q->buffer; // Set start pointer to beginning of buffer | ||
q->current = q->buffer; // Set current pointer to beginning of buffer | ||
q->max_items = (uint8_t) (MEM_SIZE / (float) size); // Calculate maximum number of items | ||
q->max_items = (MEM_SIZE / (float) size); // Calculate maximum number of items | ||
q->head = q->tail = 0; | ||
} | ||
|
||
// @funcname: qIsFull | ||
// | ||
// @brief: Returns 1 if the queue is full, 0 else | ||
// | ||
// @param: q: Handle for queue | ||
// | ||
// @return: 1 if queue is full, 0 else | ||
/** | ||
* @brief Returns 1 if the queue is full, 0 else | ||
* | ||
* @param q Handle for queue | ||
* @return 1 if queue is full, 0 else | ||
*/ | ||
uint8_t qIsFull(q_handle_t* q) | ||
{ | ||
return q->item_count == q->max_items ? 1 : 0; // Return 1 if full, 0 else | ||
uint32_t next_head = (q->head + 1) % q->max_items; | ||
return next_head == q->tail; | ||
} | ||
|
||
// @funcname: qSendToBack | ||
// | ||
// @brief: Adds an item to the queue (enqueue) | ||
// | ||
// @param: q: Handle for queue | ||
// @param: item: Item to be added to queue | ||
// | ||
// @return: SUCCESS if value is added, FAILURE if queue is full | ||
success_t qSendToBack(q_handle_t* q, const void* item) | ||
/** | ||
* @brief Returns 1 if the queue is empty, 0 else | ||
* | ||
* @param q Handle for queue | ||
* @return 1 if queue is empty, 0 else | ||
*/ | ||
uint8_t qIsEmpty(q_handle_t* q) | ||
{ | ||
if (q->item_count == q->max_items) // Ensure that we have a location for the item | ||
{ | ||
return FAILURE_G; // We don't, so let the user know it failed | ||
} | ||
|
||
if ((q->current - q->buffer) == q->size * q->max_items) // Check if the current pointer location is at the end of the buffer | ||
{ | ||
q->current = q->buffer; // Reset the buffer location | ||
} | ||
|
||
memcpy(q->current, item, q->size); // Copy item into queue at current location | ||
q->current = q->current + q->size; // Increment pointer by number of bytes corresponding to item size | ||
++q->item_count; // Increment number of items in queue | ||
return q->head == q->tail; | ||
} | ||
|
||
/** | ||
* @brief Adds an item to the queue (enqueue) | ||
* @note Interrupt safe ONLY if ONE producer and consumer | ||
* | ||
* @param q Handle for queue | ||
* @param item Item to be added to queue | ||
* @return success_t SUCCESS if value is added, FAILURE if queue is full | ||
*/ | ||
success_t qSendToBack(q_handle_t* q, const void* item) | ||
{ | ||
uint32_t next_head = (q->head + 1) % q->max_items; // Calculate next head | ||
if (next_head == q->tail) return FAILURE_G; // No room | ||
volatile uint8_t* dst = q->buffer + (q->head * q->size); | ||
for (uint32_t i = 0; i < q->size; ++i) dst[i] = ((uint8_t *)item)[i]; | ||
q->head = next_head; // Update head | ||
return SUCCESS_G; | ||
} | ||
|
||
// @funcname: qReceive | ||
// | ||
// @brief: Removes an item to the queue (dequeue) | ||
// | ||
// @param: q: Handle for queue | ||
// @param: rx_buf: Location for the item to be "moved" to | ||
// | ||
// @return: SUCCESS if value is removed, FAILURE if queue is empty | ||
/** | ||
* @brief Removes an item to the queue (dequeue) | ||
* @note Interrupt safe ONLY if ONE producer and consumer | ||
* | ||
* @param q Handle for queue | ||
* @param rx_buf Location for the item to be "moved" to | ||
* @return success_t SUCCESS if value is removed, FAILURE if queue is empty | ||
*/ | ||
success_t qReceive(q_handle_t* q, void* rx_buf) | ||
{ | ||
if (q->item_count == 0) // Ensure that we actually have an item in the queue | ||
{ | ||
return FAILURE_G; // We don't, so let the user know it failed | ||
} | ||
|
||
if ((q->start - q->buffer) == q->size * q->max_items) // Check if the start pointer location is at the end of the buffer | ||
{ | ||
q->start = q->buffer; // Reset the buffer location | ||
} | ||
|
||
memcpy(rx_buf, q->start, q->size); // Copy item from queue into buffer | ||
q->start = q->start + q->size; // Increment pointer by number of bytes corresponding to item size | ||
--q->item_count; // Decrement number of items in queue | ||
|
||
if (q->tail == q->head) return FAILURE_G; // Empty | ||
volatile uint8_t* src = q->buffer + (q->tail * q->size); | ||
for (uint32_t i = 0; i < q->size; ++i) ((uint8_t *)rx_buf)[i] = src[i]; | ||
q->tail = (q->tail + 1) % q->max_items; // Update tail | ||
return SUCCESS_G; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.