diff --git a/stm32basic.hpp b/stm32basic.hpp index 9f6aac6..014df34 100644 --- a/stm32basic.hpp +++ b/stm32basic.hpp @@ -3,4 +3,6 @@ #include "mutex.hpp" #include "zadc.hpp" #include "zspi.hpp" -#include "zgpio.hpp" \ No newline at end of file +#include "zgpio.hpp" +#include "ticket.hpp" +#include "zqueue.hpp" \ No newline at end of file diff --git a/ticket.cpp b/ticket.cpp new file mode 100644 index 0000000..8e1eca7 --- /dev/null +++ b/ticket.cpp @@ -0,0 +1,20 @@ +#include "ticket.hpp" + +#include "cmsis_os.h" +#include "cmsis_version.h" +extern "C" { +uint32_t zos_get_tick(void) { return osKernelSysTick(); } +uint32_t zos_haspassedms2(uint32_t lastticket, uint32_t nowticket) { + if (nowticket >= lastticket) { + return nowticket - lastticket; + } + return UINT32_MAX - lastticket + nowticket; +} +uint32_t zos_haspassedms(uint32_t ticket) { + uint32_t nowticket = zos_get_tick(); + if (nowticket >= ticket) { + return nowticket - ticket; + } + return UINT32_MAX - ticket + nowticket; +} +} diff --git a/ticket.hpp b/ticket.hpp new file mode 100644 index 0000000..9ad99d6 --- /dev/null +++ b/ticket.hpp @@ -0,0 +1,8 @@ +#pragma once +#include + +extern "C" { +uint32_t zos_get_tick(void); +uint32_t zos_haspassedms(uint32_t ticket); +uint32_t zos_haspassedms2(uint32_t lastticket, uint32_t nowticket); +} \ No newline at end of file diff --git a/zqueue.cpp b/zqueue.cpp new file mode 100644 index 0000000..8b52292 --- /dev/null +++ b/zqueue.cpp @@ -0,0 +1,5 @@ +#include "zqueue.hpp" + +#include "basic/zlog.h" +using namespace iflytop; +#define TAG "ZQueue" diff --git a/zqueue.hpp b/zqueue.hpp new file mode 100644 index 0000000..b6c676d --- /dev/null +++ b/zqueue.hpp @@ -0,0 +1,42 @@ +#pragma once +#include "cmsis_os.h" +#include "basic/zlog.h" +namespace iflytop { +template +class ZQueue { + private: + int32_t m_num = 0; + int32_t m_eachsize = 0; + QueueHandle_t xQueue; + + public: + void initialize(int32_t num, int32_t eachsize) { + xQueue = xQueueCreate(num, eachsize); + ZASSERT(xQueue); + } + void clear() { xQueueReset(xQueue); } + + bool send(T *data, int32_t overtime) { return _send((uint8_t *)data, sizeof(T), overtime); } + bool receive(T *data, int32_t overtime) { return _receive((uint8_t *)data, overtime); } + + bool isFull() { + BaseType_t xStatus = xQueueIsQueueFullFromISR(xQueue); + return xStatus == pdTRUE; + } + bool isEmpty() { + BaseType_t xStatus = xQueueIsQueueEmptyFromISR(xQueue); + return xStatus == pdTRUE; + } + + private: + bool _send(uint8_t *data, int32_t len, int32_t overtime) { + BaseType_t xStatus = xQueueSend(xQueue, data, overtime); + return xStatus == pdPASS; + } + bool _receive(uint8_t *data, int32_t overtime) { + BaseType_t xStatus = xQueueReceive(xQueue, data, overtime); + return xStatus == pdPASS; + } +}; + +} // namespace iflytop