Skip to content

Commit

Permalink
Move to CMSIS-OS v2
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed Oct 28, 2019
1 parent e568d53 commit b9b856d
Show file tree
Hide file tree
Showing 56 changed files with 823 additions and 1,173 deletions.
3 changes: 0 additions & 3 deletions esp_at_lib/src/include/system/esp_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ typedef void (*esp_sys_thread_fn)(void *);

#define ESP_SYS_PORT_CMSIS_OS 1 /*!< CMSIS-OS based port for OS systems capable of ARM CMSIS standard */
#define ESP_SYS_PORT_WIN32 2 /*!< WIN32 based port to use ESP library with Windows applications */
#define ESP_SYS_PORT_CMSIS_OS2 3 /*!< CMSIS-OS v2 based port for OS systems capable of ARM CMSIS standard */
#define ESP_SYS_PORT_USER 99 /*!< User custom implementation.
When port is selected to user mode, user must provide "esp_sys_user.h" file,
which is not provided with library. Refer to `system/esp_sys_template.h` file for more information
Expand All @@ -76,8 +75,6 @@ typedef void (*esp_sys_thread_fn)(void *);
/* Decide which port to include */
#if ESP_CFG_SYS_PORT == ESP_SYS_PORT_CMSIS_OS
#include "system/esp_sys_cmsis_os.h"
#elif ESP_CFG_SYS_PORT == ESP_SYS_PORT_CMSIS_OS2
#include "system/esp_sys_cmsis_os2.h"
#elif ESP_CFG_SYS_PORT == ESP_SYS_PORT_WIN32
#include "system/esp_sys_win32.h"
#elif ESP_CFG_SYS_PORT == ESP_SYS_PORT_USER
Expand Down
16 changes: 8 additions & 8 deletions esp_at_lib/src/include/system/esp_sys_cmsis_os.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ extern "C" {

#include "cmsis_os.h"

typedef osMutexId esp_sys_mutex_t;
typedef osSemaphoreId esp_sys_sem_t;
typedef osMessageQId esp_sys_mbox_t;
typedef osThreadId esp_sys_thread_t;
typedef osPriority esp_sys_thread_prio_t;
#define ESP_SYS_MBOX_NULL (esp_sys_mbox_t)0
#define ESP_SYS_SEM_NULL (esp_sys_sem_t)0
#define ESP_SYS_MUTEX_NULL (esp_sys_mutex_t)0
typedef osMutexId_t esp_sys_mutex_t;
typedef osSemaphoreId_t esp_sys_sem_t;
typedef osMessageQueueId_t esp_sys_mbox_t;
typedef osThreadId_t esp_sys_thread_t;
typedef osPriority_t esp_sys_thread_prio_t;
#define ESP_SYS_MBOX_NULL ((esp_sys_mbox_t)0)
#define ESP_SYS_SEM_NULL ((esp_sys_sem_t)0)
#define ESP_SYS_MUTEX_NULL ((esp_sys_mutex_t)0)
#define ESP_SYS_TIMEOUT ((uint32_t)osWaitForever)
#define ESP_SYS_THREAD_PRIO (osPriorityNormal)
#define ESP_SYS_THREAD_SS (256)
Expand Down
68 changes: 0 additions & 68 deletions esp_at_lib/src/include/system/esp_sys_cmsis_os2.h

This file was deleted.

38 changes: 18 additions & 20 deletions esp_at_lib/src/system/esp_ll_stm32.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,26 @@ static uint8_t is_running, initialized;
static size_t old_pos;

/* USART thread */
static void usart_ll_thread(void const * arg);
static osThreadDef(usart_ll_thread, usart_ll_thread, osPriorityNormal, 0, 1024);
static void usart_ll_thread(void* arg);
static osThreadId usart_ll_thread_id;

/* Message queue */
static osMessageQDef(usart_ll_mbox, 10, uint8_t);
static osMessageQId usart_ll_mbox_id;

/**
* \brief USART data processing
*/
static void
usart_ll_thread(void const * arg) {
osEvent evt;
size_t pos;
usart_ll_thread(void* arg) {
static size_t old_pos;

size_t pos;

ESP_UNUSED(arg);

while (1) {
void* d;
/* Wait for the event message from DMA or USART */
evt = osMessageGet(usart_ll_mbox_id, osWaitForever);
if (evt.status != osEventMessage) {
continue;
}
osMessageQueueGet(usart_ll_mbox_id, &d, NULL, osWaitForever);

/* Read data */
#if defined(ESP_USART_DMA_RX_STREAM)
Expand Down Expand Up @@ -284,10 +279,13 @@ configure_uart(uint32_t baudrate) {

/* Create mbox and start thread */
if (usart_ll_mbox_id == NULL) {
usart_ll_mbox_id = osMessageCreate(osMessageQ(usart_ll_mbox), NULL);
usart_ll_mbox_id = osMessageQueueNew(10, sizeof(void *), NULL);
}
if (usart_ll_thread_id == NULL) {
usart_ll_thread_id = osThreadCreate(osThread(usart_ll_thread), usart_ll_mbox_id);
const osThreadAttr_t attr = {
.stack_size = 1024
};
usart_ll_thread_id = osThreadNew(usart_ll_thread, usart_ll_mbox_id, &attr);
}
}

Expand All @@ -307,7 +305,7 @@ reset_device(uint8_t state) {
#endif /* defined(ESP_RESET_PIN) */

/**
* \brief Send data to ESP device
* \brief Send data to GSM device
* \param[in] data: Pointer to data to send
* \param[in] len: Number of bytes to send
* \return Number of bytes sent
Expand Down Expand Up @@ -342,7 +340,7 @@ esp_ll_init(esp_ll_t* ll) {
esp_mem_assignmemory(mem_regions, ESP_ARRAYSIZE(mem_regions)); /* Assign memory for allocations */
}
#endif /* !ESP_CFG_MEM_CUSTOM */

if (!initialized) {
ll->send_fn = send_data; /* Set callback function to send data */
#if defined(ESP_RESET_PIN)
Expand All @@ -363,12 +361,12 @@ esp_ll_init(esp_ll_t* ll) {
espr_t
esp_ll_deinit(esp_ll_t* ll) {
if (usart_ll_mbox_id != NULL) {
osMessageQId tmp = usart_ll_mbox_id;
osMessageQueueId_t tmp = usart_ll_mbox_id;
usart_ll_mbox_id = NULL;
osMessageDelete(tmp);
osMessageQueueDelete(tmp);
}
if (usart_ll_thread_id != NULL) {
osThreadId tmp = usart_ll_thread_id;
osThreadId_t tmp = usart_ll_thread_id;
usart_ll_thread_id = NULL;
osThreadTerminate(tmp);
}
Expand All @@ -389,7 +387,7 @@ ESP_USART_IRQHANDLER(void) {
LL_USART_ClearFlag_NE(ESP_USART);

if (usart_ll_mbox_id != NULL) {
osMessagePut(usart_ll_mbox_id, 0, 0);
osMessageQueuePut(usart_ll_mbox_id, (void *)1, 0, 0);
}
}

Expand All @@ -402,7 +400,7 @@ ESP_USART_DMA_RX_IRQHANDLER(void) {
ESP_USART_DMA_RX_CLEAR_HT;

if (usart_ll_mbox_id != NULL) {
osMessagePut(usart_ll_mbox_id, 0, 0);
osMessageQueuePut(usart_ll_mbox_id, (void *)1, 0, 0);
}
}

Expand Down
73 changes: 30 additions & 43 deletions esp_at_lib/src/system/esp_sys_cmsis_os.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
#include "cmsis_os.h"

#if !__DOXYGEN__
static osMutexId sys_mutex;

static osMutexId_t sys_mutex;

uint8_t
esp_sys_init(void) {
Expand All @@ -48,8 +49,6 @@ esp_sys_now(void) {
return osKernelSysTick();
}

#if ESP_CFG_OS

uint8_t
esp_sys_protect(void) {
esp_sys_mutex_lock(&sys_mutex);
Expand All @@ -64,8 +63,10 @@ esp_sys_unprotect(void) {

uint8_t
esp_sys_mutex_create(esp_sys_mutex_t* p) {
osMutexDef(MUT);
*p = osRecursiveMutexCreate(osMutex(MUT));
const osMutexAttr_t attr = {
.attr_bits = osMutexRecursive
};
*p = osMutexNew(&attr);
return *p != NULL;
}

Expand All @@ -76,12 +77,12 @@ esp_sys_mutex_delete(esp_sys_mutex_t* p) {

uint8_t
esp_sys_mutex_lock(esp_sys_mutex_t* p) {
return osRecursiveMutexWait(*p, osWaitForever) == osOK;
return osMutexAcquire(*p, osWaitForever) == osOK;
}

uint8_t
esp_sys_mutex_unlock(esp_sys_mutex_t* p) {
return osRecursiveMutexRelease(*p) == osOK;
return osMutexRelease(*p) == osOK;
}

uint8_t
Expand All @@ -97,13 +98,7 @@ esp_sys_mutex_invalid(esp_sys_mutex_t* p) {

uint8_t
esp_sys_sem_create(esp_sys_sem_t* p, uint8_t cnt) {
osSemaphoreDef(SEM);
*p = osSemaphoreCreate(osSemaphore(SEM), 1);

if (*p != NULL && !cnt) {
osSemaphoreWait(*p, 0);
}
return *p != NULL;
return (*p = osSemaphoreNew(1, cnt > 0 ? 1 : 0, NULL)) != NULL;
}

uint8_t
Expand All @@ -114,7 +109,7 @@ esp_sys_sem_delete(esp_sys_sem_t* p) {
uint32_t
esp_sys_sem_wait(esp_sys_sem_t* p, uint32_t timeout) {
uint32_t tick = osKernelSysTick();
return (osSemaphoreWait(*p, !timeout ? osWaitForever : timeout) == osOK) ? (osKernelSysTick() - tick) : ESP_SYS_TIMEOUT;
return (osSemaphoreAcquire(*p, timeout == 0 ? osWaitForever : timeout) == osOK) ? (osKernelSysTick() - tick) : ESP_SYS_TIMEOUT;
}

uint8_t
Expand All @@ -135,53 +130,37 @@ esp_sys_sem_invalid(esp_sys_sem_t* p) {

uint8_t
esp_sys_mbox_create(esp_sys_mbox_t* b, size_t size) {
osMessageQDef(MBOX, size, void *);
*b = osMessageCreate(osMessageQ(MBOX), NULL);
return *b != NULL;
return (*b = osMessageQueueNew(size, sizeof(void *), NULL)) != NULL;
}

uint8_t
esp_sys_mbox_delete(esp_sys_mbox_t* b) {
if (osMessageWaiting(*b)) {
if (osMessageQueueGetCount(*b) > 0) {
return 0;
}
return osMessageDelete(*b) == osOK;
return osMessageQueueDelete(*b) == osOK;
}

uint32_t
esp_sys_mbox_put(esp_sys_mbox_t* b, void* m) {
uint32_t tick = osKernelSysTick();
return osMessagePut(*b, (uint32_t)m, osWaitForever) == osOK ? (osKernelSysTick() - tick) : ESP_SYS_TIMEOUT;
return osMessageQueuePut(*b, m, 0, osWaitForever) == osOK ? (osKernelSysTick() - tick) : ESP_SYS_TIMEOUT;
}

uint32_t
esp_sys_mbox_get(esp_sys_mbox_t* b, void** m, uint32_t timeout) {
osEvent evt;
uint32_t time = osKernelSysTick();

evt = osMessageGet(*b, !timeout ? osWaitForever : timeout);
if (evt.status == osEventMessage) {
*m = evt.value.p;
return osKernelSysTick() - time;
}
return ESP_SYS_TIMEOUT;
uint32_t tick = osKernelSysTick();
return osMessageQueueGet(*b, m, NULL, timeout == 0 ? osWaitForever : timeout) == osOK ? (osKernelSysTick() - tick) : ESP_SYS_TIMEOUT;
}

uint8_t
esp_sys_mbox_putnow(esp_sys_mbox_t* b, void* m) {
return osMessagePut(*b, (uint32_t)m, 0) == osOK;
return osMessageQueuePut(*b, m, 0, 0) == osOK;
}

uint8_t
esp_sys_mbox_getnow(esp_sys_mbox_t* b, void** m) {
osEvent evt;

evt = osMessageGet(*b, 0);
if (evt.status == osEventMessage) {
*m = evt.value.p;
return 1;
}
return 0;
return osMessageQueueGet(*b, m, NULL, 0) == osOK;
}

uint8_t
Expand All @@ -198,8 +177,13 @@ esp_sys_mbox_invalid(esp_sys_mbox_t* b) {
uint8_t
esp_sys_thread_create(esp_sys_thread_t* t, const char* name, esp_sys_thread_fn thread_func, void* const arg, size_t stack_size, esp_sys_thread_prio_t prio) {
esp_sys_thread_t id;
const osThreadDef_t thread_def = {(char *)name, (os_pthread)thread_func, (osPriority)prio, 0, stack_size ? stack_size : ESP_SYS_THREAD_SS }; /* Create thread description */
id = osThreadCreate(&thread_def, arg);
const osThreadAttr_t thread_attr = {
.name = (char *)name,
.priority = (osPriority)prio,
.stack_size = stack_size ? stack_size : ESP_SYS_THREAD_SS
};

id = osThreadNew(thread_func, arg, &thread_attr);
if (t != NULL) {
*t = id;
}
Expand All @@ -208,7 +192,11 @@ esp_sys_thread_create(esp_sys_thread_t* t, const char* name, esp_sys_thread_fn t

uint8_t
esp_sys_thread_terminate(esp_sys_thread_t* t) {
osThreadTerminate(t != NULL ? *t : NULL);
if (t != NULL) {
osThreadTerminate(*t);
} else {
osThreadExit();
}
return 1;
}

Expand All @@ -218,5 +206,4 @@ esp_sys_thread_yield(void) {
return 1;
}

#endif /* ESP_CFG_OS */
#endif /* !__DOXYGEN__ */
Loading

0 comments on commit b9b856d

Please sign in to comment.