Porting guide¶
High level of ESP-AT library is platform independent, written in ANSI C99, however there is an important part where middleware needs to communicate with target ESP device and it must work under different optional operating systems selected by final customer.
Porting consists of:
Implementation of low-level part, for actual communication between host device and ESP device
Implementation of system functions, link between target operating system and middleware functions
Assignment of memory for allocation manager
Implement low-level driver¶
To successfully prepare all parts of low-level driver, application must take care of:
Implementing
lwesp_ll_init()
andlwesp_ll_deinit()
callback functionsImplement and assign send data and optional hardware reset function callbacks
Assign memory for allocation manager when using default allocator or use custom allocator
Process received data from ESP device and send it to input module for further processing
Tip
Port examples are available for STM32 and WIN32 architectures. Both actual working and up-to-date implementations are available within the library.
Note
Check Input module for more information about direct & indirect input processing.
Implement system functions¶
System functions are bridge between operating system calls and ESP middleware. ESP library relies on stable operating system features and its implementation and does not require any special features which do not normally come with operating systems.
Operating system must support:
Thread management functions
Mutex management functions
Binary semaphores only, no need for counting semaphores
Message queue management functions
Warning
If any of the features are not available within targeted operating system, customer needs to resolve it with care. As an example, message queue is not available in WIN32 OS API therefore custom message queue has been implemented using binary semaphores
Application needs to implement all system call functions, starting with lwesp_sys_
.
It must also prepare header file for standard types in order to support OS types within ESP middleware.
An example code is provided latter section of this page for WIN32 and STM32.
Steps to follow¶
Copy
lwesp/src/system/lwesp_sys_template.c
to the same folder and rename it to application port, eg.lwesp_sys_win32.c
Open newly created file and implement all system functions
Copy folder
lwesp/src/include/system/port/template/*
to the same folder and rename folder name to application port, eg.cmsis_os
Open
lwesp_sys_port.h
file from newly created folder and implement all typedefs and macros for specific targetAdd source file to compiler sources and add path to header file to include paths in compiler options
Note
Check System functions for function prototypes.
Example: Low-level driver for WIN32¶
Example code for low-level porting on WIN32 platform. It uses native Windows features to open COM port and read/write from/to it.
Notes:
It uses separate thread for received data processing. It uses
lwesp_input_process()
orlwesp_input()
functions, based on application configuration ofLWESP_CFG_INPUT_USE_PROCESS
parameter.When
LWESP_CFG_INPUT_USE_PROCESS
is disabled, dedicated receive buffer is created by ESP-AT library andlwesp_input()
function just writes data to it and does not process received characters immediately. This is handled by Processing thread at later stage instead.When
LWESP_CFG_INPUT_USE_PROCESS
is enabled,lwesp_input_process()
is used, which directly processes input data and sends potential callback/event functions to application layer.
Memory manager has been assigned to
1
region ofLWESP_MEM_SIZE
sizeIt sets send and reset callback functions for ESP-AT library
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | /**
* \file lwesp_ll_win32.c
* \brief Low-level communication with ESP device for WIN32
*/
/*
* Copyright (c) 2020 Tilen MAJERLE
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* This file is part of LwESP - Lightweight ESP-AT parser library.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v1.0.0
*/
#include "system/lwesp_ll.h"
#include "lwesp/lwesp.h"
#include "lwesp/lwesp_mem.h"
#include "lwesp/lwesp_input.h"
#if !__DOXYGEN__
volatile uint8_t lwesp_ll_win32_driver_ignore_data;
static uint8_t initialized = 0;
static HANDLE thread_handle;
static volatile HANDLE com_port; /*!< COM port handle */
static uint8_t data_buffer[0x1000]; /*!< Received data array */
static void uart_thread(void* param);
/**
* \brief Send data to ESP device, function called from ESP stack when we have data to send
*/
static size_t
send_data(const void* data, size_t len) {
DWORD written;
if (com_port != NULL) {
#if !LWESP_CFG_AT_ECHO
const uint8_t* d = data;
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
for (DWORD i = 0; i < len; ++i) {
printf("%c", d[i]);
}
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
#endif /* !LWESP_CFG_AT_ECHO */
WriteFile(com_port, data, len, &written, NULL);
FlushFileBuffers(com_port);
return written;
}
return 0;
}
/**
* \brief Configure UART (USB to UART)
*/
static void
configure_uart(uint32_t baudrate) {
DCB dcb = { 0 };
dcb.DCBlength = sizeof(dcb);
/*
* On first call,
* create virtual file on selected COM port and open it
* as generic read and write
*/
if (!initialized) {
com_port = CreateFile(L"\\\\.\\COM4",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
NULL
);
}
/* Configure COM port parameters */
if (GetCommState(com_port, &dcb)) {
COMMTIMEOUTS timeouts;
dcb.BaudRate = baudrate;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
if (!SetCommState(com_port, &dcb)) {
printf("Cannot set COM PORT info\r\n");
}
if (GetCommTimeouts(com_port, &timeouts)) {
/* Set timeout to return immediately from ReadFile function */
timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.ReadTotalTimeoutConstant = 0;
timeouts.ReadTotalTimeoutMultiplier = 0;
if (!SetCommTimeouts(com_port, &timeouts)) {
printf("Cannot set COM PORT timeouts\r\n");
}
GetCommTimeouts(com_port, &timeouts);
} else {
printf("Cannot get COM PORT timeouts\r\n");
}
} else {
printf("Cannot get COM PORT info\r\n");
}
/* On first function call, create a thread to read data from COM port */
if (!initialized) {
lwesp_sys_thread_create(&thread_handle, "lwesp_ll_thread", uart_thread, NULL, 0, 0);
}
}
/**
* \brief UART thread
*/
static void
uart_thread(void* param) {
DWORD bytes_read;
lwesp_sys_sem_t sem;
FILE* file = NULL;
lwesp_sys_sem_create(&sem, 0); /* Create semaphore for delay functions */
while (com_port == NULL) {
lwesp_sys_sem_wait(&sem, 1); /* Add some delay with yield */
}
fopen_s(&file, "log_file.txt", "w+"); /* Open debug file in write mode */
while (1) {
/*
* Try to read data from COM port
* and send it to upper layer for processing
*/
do {
ReadFile(com_port, data_buffer, sizeof(data_buffer), &bytes_read, NULL);
if (bytes_read > 0) {
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
for (DWORD i = 0; i < bytes_read; ++i) {
printf("%c", data_buffer[i]);
}
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
if (lwesp_ll_win32_driver_ignore_data) {
printf("IGNORING..\r\n");
continue;
}
/* Send received data to input processing module */
#if LWESP_CFG_INPUT_USE_PROCESS
lwesp_input_process(data_buffer, (size_t)bytes_read);
#else /* LWESP_CFG_INPUT_USE_PROCESS */
lwesp_input(data_buffer, (size_t)bytes_read);
#endif /* !LWESP_CFG_INPUT_USE_PROCESS */
/* Write received data to output debug file */
if (file != NULL) {
fwrite(data_buffer, 1, bytes_read, file);
fflush(file);
}
}
} while (bytes_read == (DWORD)sizeof(data_buffer));
/* Implement delay to allow other tasks processing */
lwesp_sys_sem_wait(&sem, 1);
}
}
/**
* \brief Reset device GPIO management
*/
static uint8_t
reset_device(uint8_t state) {
return 0; /* Hardware reset was not successful */
}
/**
* \brief Callback function called from initialization process
*/
lwespr_t
lwesp_ll_init(lwesp_ll_t* ll) {
#if !LWESP_CFG_MEM_CUSTOM
/* Step 1: Configure memory for dynamic allocations */
static uint8_t memory[0x10000]; /* Create memory for dynamic allocations with specific size */
/*
* Create memory region(s) of memory.
* If device has internal/external memory available,
* multiple memories may be used
*/
lwesp_mem_region_t mem_regions[] = {
{ memory, sizeof(memory) }
};
if (!initialized) {
lwesp_mem_assignmemory(mem_regions, LWESP_ARRAYSIZE(mem_regions)); /* Assign memory for allocations to ESP library */
}
#endif /* !LWESP_CFG_MEM_CUSTOM */
/* Step 2: Set AT port send function to use when we have data to transmit */
if (!initialized) {
ll->send_fn = send_data; /* Set callback function to send data */
ll->reset_fn = reset_device;
}
/* Step 3: Configure AT port to be able to send/receive data to/from ESP device */
configure_uart(ll->uart.baudrate); /* Initialize UART for communication */
initialized = 1;
return lwespOK;
}
/**
* \brief Callback function to de-init low-level communication part
*/
lwespr_t
lwesp_ll_deinit(lwesp_ll_t* ll) {
if (thread_handle != NULL) {
lwesp_sys_thread_terminate(&thread_handle);
thread_handle = NULL;
}
initialized = 0; /* Clear initialized flag */
return lwespOK;
}
#endif /* !__DOXYGEN__ */
|
Example: Low-level driver for STM32¶
Example code for low-level porting on STM32 platform. It uses CMSIS-OS based application layer functions for implementing threads & other OS dependent features.
Notes:
It uses separate thread for received data processing. It uses
lwesp_input_process()
function to directly process received data without using intermediate receive bufferMemory manager has been assigned to
1
region ofLWESP_MEM_SIZE
sizeIt sets send and reset callback functions for ESP-AT library
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | /**
* \file lwesp_ll_stm32.c
* \brief Generic STM32 driver, included in various STM32 driver variants
*/
/*
* Copyright (c) 2020 Tilen MAJERLE
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* This file is part of LwESP - Lightweight ESP-AT parser library.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v1.0.0
*/
/*
* How it works
*
* On first call to \ref lwesp_ll_init, new thread is created and processed in usart_ll_thread function.
* USART is configured in RX DMA mode and any incoming bytes are processed inside thread function.
* DMA and USART implement interrupt handlers to notify main thread about new data ready to send to upper layer.
*
* More about UART + RX DMA: https://github.com/MaJerle/stm32-usart-dma-rx-tx
*
* \ref LWESP_CFG_INPUT_USE_PROCESS must be enabled in `lwesp_config.h` to use this driver.
*/
#include "lwesp/lwesp.h"
#include "lwesp/lwesp_mem.h"
#include "lwesp/lwesp_input.h"
#include "system/lwesp_ll.h"
#if !__DOXYGEN__
#if !LWESP_CFG_INPUT_USE_PROCESS
#error "LWESP_CFG_INPUT_USE_PROCESS must be enabled in `lwesp_config.h` to use this driver."
#endif /* LWESP_CFG_INPUT_USE_PROCESS */
#if !defined(LWESP_USART_DMA_RX_BUFF_SIZE)
#define LWESP_USART_DMA_RX_BUFF_SIZE 0x1000
#endif /* !defined(LWESP_USART_DMA_RX_BUFF_SIZE) */
#if !defined(LWESP_MEM_SIZE)
#define LWESP_MEM_SIZE 0x1000
#endif /* !defined(LWESP_MEM_SIZE) */
#if !defined(LWESP_USART_RDR_NAME)
#define LWESP_USART_RDR_NAME RDR
#endif /* !defined(LWESP_USART_RDR_NAME) */
/* USART memory */
static uint8_t usart_mem[LWESP_USART_DMA_RX_BUFF_SIZE];
static uint8_t is_running, initialized;
static size_t old_pos;
/* USART thread */
static void usart_ll_thread(void* arg);
static osThreadId_t usart_ll_thread_id;
/* Message queue */
static osMessageQueueId_t usart_ll_mbox_id;
/**
* \brief USART data processing
*/
static void
usart_ll_thread(void* arg) {
size_t pos;
LWESP_UNUSED(arg);
while (1) {
void* d;
/* Wait for the event message from DMA or USART */
osMessageQueueGet(usart_ll_mbox_id, &d, NULL, osWaitForever);
/* Read data */
#if defined(LWESP_USART_DMA_RX_STREAM)
pos = sizeof(usart_mem) - LL_DMA_GetDataLength(LWESP_USART_DMA, LWESP_USART_DMA_RX_STREAM);
#else
pos = sizeof(usart_mem) - LL_DMA_GetDataLength(LWESP_USART_DMA, LWESP_USART_DMA_RX_CH);
#endif /* defined(LWESP_USART_DMA_RX_STREAM) */
if (pos != old_pos && is_running) {
if (pos > old_pos) {
lwesp_input_process(&usart_mem[old_pos], pos - old_pos);
} else {
lwesp_input_process(&usart_mem[old_pos], sizeof(usart_mem) - old_pos);
if (pos > 0) {
lwesp_input_process(&usart_mem[0], pos);
}
}
old_pos = pos;
if (old_pos == sizeof(usart_mem)) {
old_pos = 0;
}
}
}
}
/**
* \brief Configure UART using DMA for receive in double buffer mode and IDLE line detection
*/
static void
configure_uart(uint32_t baudrate) {
static LL_USART_InitTypeDef usart_init;
static LL_DMA_InitTypeDef dma_init;
LL_GPIO_InitTypeDef gpio_init;
if (!initialized) {
/* Enable peripheral clocks */
LWESP_USART_CLK;
LWESP_USART_DMA_CLK;
LWESP_USART_TX_PORT_CLK;
LWESP_USART_RX_PORT_CLK;
#if defined(LWESP_RESET_PIN)
LWESP_RESET_PORT_CLK;
#endif /* defined(LWESP_RESET_PIN) */
#if defined(LWESP_GPIO0_PIN)
LWESP_GPIO0_PORT_CLK;
#endif /* defined(LWESP_GPIO0_PIN) */
#if defined(LWESP_GPIO2_PIN)
LWESP_GPIO2_PORT_CLK;
#endif /* defined(LWESP_GPIO2_PIN) */
#if defined(LWESP_CH_PD_PIN)
LWESP_CH_PD_PORT_CLK;
#endif /* defined(LWESP_CH_PD_PIN) */
/* Global pin configuration */
LL_GPIO_StructInit(&gpio_init);
gpio_init.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
gpio_init.Pull = LL_GPIO_PULL_UP;
gpio_init.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
gpio_init.Mode = LL_GPIO_MODE_OUTPUT;
#if defined(LWESP_RESET_PIN)
/* Configure RESET pin */
gpio_init.Pin = LWESP_RESET_PIN;
LL_GPIO_Init(LWESP_RESET_PORT, &gpio_init);
#endif /* defined(LWESP_RESET_PIN) */
#if defined(LWESP_GPIO0_PIN)
/* Configure GPIO0 pin */
gpio_init.Pin = LWESP_GPIO0_PIN;
LL_GPIO_Init(LWESP_GPIO0_PORT, &gpio_init);
LL_GPIO_SetOutputPin(LWESP_GPIO0_PORT, LWESP_GPIO0_PIN);
#endif /* defined(LWESP_GPIO0_PIN) */
#if defined(LWESP_GPIO2_PIN)
/* Configure GPIO2 pin */
gpio_init.Pin = LWESP_GPIO2_PIN;
LL_GPIO_Init(LWESP_GPIO2_PORT, &gpio_init);
LL_GPIO_SetOutputPin(LWESP_GPIO2_PORT, LWESP_GPIO2_PIN);
#endif /* defined(LWESP_GPIO2_PIN) */
#if defined(LWESP_CH_PD_PIN)
/* Configure CH_PD pin */
gpio_init.Pin = LWESP_CH_PD_PIN;
LL_GPIO_Init(LWESP_CH_PD_PORT, &gpio_init);
LL_GPIO_SetOutputPin(LWESP_CH_PD_PORT, LWESP_CH_PD_PIN);
#endif /* defined(LWESP_CH_PD_PIN) */
/* Configure USART pins */
gpio_init.Mode = LL_GPIO_MODE_ALTERNATE;
/* TX PIN */
gpio_init.Alternate = LWESP_USART_TX_PIN_AF;
gpio_init.Pin = LWESP_USART_TX_PIN;
LL_GPIO_Init(LWESP_USART_TX_PORT, &gpio_init);
/* RX PIN */
gpio_init.Alternate = LWESP_USART_RX_PIN_AF;
gpio_init.Pin = LWESP_USART_RX_PIN;
LL_GPIO_Init(LWESP_USART_RX_PORT, &gpio_init);
/* Configure UART */
LL_USART_DeInit(LWESP_USART);
LL_USART_StructInit(&usart_init);
usart_init.BaudRate = baudrate;
usart_init.DataWidth = LL_USART_DATAWIDTH_8B;
usart_init.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
usart_init.OverSampling = LL_USART_OVERSAMPLING_16;
usart_init.Parity = LL_USART_PARITY_NONE;
usart_init.StopBits = LL_USART_STOPBITS_1;
usart_init.TransferDirection = LL_USART_DIRECTION_TX_RX;
LL_USART_Init(LWESP_USART, &usart_init);
/* Enable USART interrupts and DMA request */
LL_USART_EnableIT_IDLE(LWESP_USART);
LL_USART_EnableIT_PE(LWESP_USART);
LL_USART_EnableIT_ERROR(LWESP_USART);
LL_USART_EnableDMAReq_RX(LWESP_USART);
/* Enable USART interrupts */
NVIC_SetPriority(LWESP_USART_IRQ, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0x07, 0x00));
NVIC_EnableIRQ(LWESP_USART_IRQ);
/* Configure DMA */
is_running = 0;
#if defined(LWESP_USART_DMA_RX_STREAM)
LL_DMA_DeInit(LWESP_USART_DMA, LWESP_USART_DMA_RX_STREAM);
dma_init.Channel = LWESP_USART_DMA_RX_CH;
#else
LL_DMA_DeInit(LWESP_USART_DMA, LWESP_USART_DMA_RX_CH);
dma_init.PeriphRequest = LWESP_USART_DMA_RX_REQ_NUM;
#endif /* defined(LWESP_USART_DMA_RX_STREAM) */
dma_init.PeriphOrM2MSrcAddress = (uint32_t)&LWESP_USART->LWESP_USART_RDR_NAME;
dma_init.MemoryOrM2MDstAddress = (uint32_t)usart_mem;
dma_init.Direction = LL_DMA_DIRECTION_PERIPH_TO_MEMORY;
dma_init.Mode = LL_DMA_MODE_CIRCULAR;
dma_init.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
dma_init.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT;
dma_init.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_BYTE;
dma_init.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_BYTE;
dma_init.NbData = sizeof(usart_mem);
dma_init.Priority = LL_DMA_PRIORITY_MEDIUM;
#if defined(LWESP_USART_DMA_RX_STREAM)
LL_DMA_Init(LWESP_USART_DMA, LWESP_USART_DMA_RX_STREAM, &dma_init);
#else
LL_DMA_Init(LWESP_USART_DMA, LWESP_USART_DMA_RX_CH, &dma_init);
#endif /* defined(LWESP_USART_DMA_RX_STREAM) */
/* Enable DMA interrupts */
#if defined(LWESP_USART_DMA_RX_STREAM)
LL_DMA_EnableIT_HT(LWESP_USART_DMA, LWESP_USART_DMA_RX_STREAM);
LL_DMA_EnableIT_TC(LWESP_USART_DMA, LWESP_USART_DMA_RX_STREAM);
LL_DMA_EnableIT_TE(LWESP_USART_DMA, LWESP_USART_DMA_RX_STREAM);
LL_DMA_EnableIT_FE(LWESP_USART_DMA, LWESP_USART_DMA_RX_STREAM);
LL_DMA_EnableIT_DME(LWESP_USART_DMA, LWESP_USART_DMA_RX_STREAM);
#else
LL_DMA_EnableIT_HT(LWESP_USART_DMA, LWESP_USART_DMA_RX_CH);
LL_DMA_EnableIT_TC(LWESP_USART_DMA, LWESP_USART_DMA_RX_CH);
LL_DMA_EnableIT_TE(LWESP_USART_DMA, LWESP_USART_DMA_RX_CH);
#endif /* defined(LWESP_USART_DMA_RX_STREAM) */
/* Enable DMA interrupts */
NVIC_SetPriority(LWESP_USART_DMA_RX_IRQ, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0x07, 0x00));
NVIC_EnableIRQ(LWESP_USART_DMA_RX_IRQ);
old_pos = 0;
is_running = 1;
/* Start DMA and USART */
#if defined(LWESP_USART_DMA_RX_STREAM)
LL_DMA_EnableStream(LWESP_USART_DMA, LWESP_USART_DMA_RX_STREAM);
#else
LL_DMA_EnableChannel(LWESP_USART_DMA, LWESP_USART_DMA_RX_CH);
#endif /* defined(LWESP_USART_DMA_RX_STREAM) */
LL_USART_Enable(LWESP_USART);
} else {
osDelay(10);
LL_USART_Disable(LWESP_USART);
usart_init.BaudRate = baudrate;
LL_USART_Init(LWESP_USART, &usart_init);
LL_USART_Enable(LWESP_USART);
}
/* Create mbox and start thread */
if (usart_ll_mbox_id == NULL) {
usart_ll_mbox_id = osMessageQueueNew(10, sizeof(void*), NULL);
}
if (usart_ll_thread_id == NULL) {
const osThreadAttr_t attr = {
.stack_size = 1024
};
usart_ll_thread_id = osThreadNew(usart_ll_thread, usart_ll_mbox_id, &attr);
}
}
#if defined(LWESP_RESET_PIN)
/**
* \brief Hardware reset callback
*/
static uint8_t
reset_device(uint8_t state) {
if (state) { /* Activate reset line */
LL_GPIO_ResetOutputPin(LWESP_RESET_PORT, LWESP_RESET_PIN);
} else {
LL_GPIO_SetOutputPin(LWESP_RESET_PORT, LWESP_RESET_PIN);
}
return 1;
}
#endif /* defined(LWESP_RESET_PIN) */
/**
* \brief Send data to ESP device
* \param[in] data: Pointer to data to send
* \param[in] len: Number of bytes to send
* \return Number of bytes sent
*/
static size_t
send_data(const void* data, size_t len) {
const uint8_t* d = data;
for (size_t i = 0; i < len; ++i, ++d) {
LL_USART_TransmitData8(LWESP_USART, *d);
while (!LL_USART_IsActiveFlag_TXE(LWESP_USART)) {}
}
return len;
}
/**
* \brief Callback function called from initialization process
*/
lwespr_t
lwesp_ll_init(lwesp_ll_t* ll) {
#if !LWESP_CFG_MEM_CUSTOM
static uint8_t memory[LWESP_MEM_SIZE];
lwesp_mem_region_t mem_regions[] = {
{ memory, sizeof(memory) }
};
if (!initialized) {
lwesp_mem_assignmemory(mem_regions, LWESP_ARRAYSIZE(mem_regions)); /* Assign memory for allocations */
}
#endif /* !LWESP_CFG_MEM_CUSTOM */
if (!initialized) {
ll->send_fn = send_data; /* Set callback function to send data */
#if defined(LWESP_RESET_PIN)
ll->reset_fn = reset_device; /* Set callback for hardware reset */
#endif /* defined(LWESP_RESET_PIN) */
}
configure_uart(ll->uart.baudrate); /* Initialize UART for communication */
initialized = 1;
return lwespOK;
}
/**
* \brief Callback function to de-init low-level communication part
*/
lwespr_t
lwesp_ll_deinit(lwesp_ll_t* ll) {
if (usart_ll_mbox_id != NULL) {
osMessageQueueId_t tmp = usart_ll_mbox_id;
usart_ll_mbox_id = NULL;
osMessageQueueDelete(tmp);
}
if (usart_ll_thread_id != NULL) {
osThreadId_t tmp = usart_ll_thread_id;
usart_ll_thread_id = NULL;
osThreadTerminate(tmp);
}
initialized = 0;
LWESP_UNUSED(ll);
return lwespOK;
}
/**
* \brief UART global interrupt handler
*/
void
LWESP_USART_IRQHANDLER(void) {
LL_USART_ClearFlag_IDLE(LWESP_USART);
LL_USART_ClearFlag_PE(LWESP_USART);
LL_USART_ClearFlag_FE(LWESP_USART);
LL_USART_ClearFlag_ORE(LWESP_USART);
LL_USART_ClearFlag_NE(LWESP_USART);
if (usart_ll_mbox_id != NULL) {
void* d = (void*)1;
osMessageQueuePut(usart_ll_mbox_id, &d, 0, 0);
}
}
/**
* \brief UART DMA stream/channel handler
*/
void
LWESP_USART_DMA_RX_IRQHANDLER(void) {
LWESP_USART_DMA_RX_CLEAR_TC;
LWESP_USART_DMA_RX_CLEAR_HT;
if (usart_ll_mbox_id != NULL) {
void* d = (void*)1;
osMessageQueuePut(usart_ll_mbox_id, &d, 0, 0);
}
}
#endif /* !__DOXYGEN__ */
|
Example: System functions for WIN32¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | /**
* \file lwesp_sys_port.h
* \brief WIN32 based system file implementation
*/
/*
* Copyright (c) 2020 Tilen MAJERLE
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* This file is part of LwESP - Lightweight ESP-AT parser library.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v1.0.0
*/
#ifndef LWESP_HDR_SYSTEM_PORT_H
#define LWESP_HDR_SYSTEM_PORT_H
#include <stdint.h>
#include <stdlib.h>
#include "lwesp/lwesp_opt.h"
#include "windows.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#if LWESP_CFG_OS && !__DOXYGEN__
typedef HANDLE lwesp_sys_mutex_t;
typedef HANDLE lwesp_sys_sem_t;
typedef HANDLE lwesp_sys_mbox_t;
typedef HANDLE lwesp_sys_thread_t;
typedef int lwesp_sys_thread_prio_t;
#define LWESP_SYS_MBOX_NULL ((HANDLE)0)
#define LWESP_SYS_SEM_NULL ((HANDLE)0)
#define LWESP_SYS_MUTEX_NULL ((HANDLE)0)
#define LWESP_SYS_TIMEOUT (INFINITE)
#define LWESP_SYS_THREAD_PRIO (0)
#define LWESP_SYS_THREAD_SS (1024)
#endif /* LWESP_CFG_OS && !__DOXYGEN__ */
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* LWESP_HDR_SYSTEM_PORT_H */
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | /**
* \file lwesp_sys_win32.c
* \brief System dependant functions for WIN32
*/
/*
* Copyright (c) 2020 Tilen MAJERLE
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* This file is part of LwESP - Lightweight ESP-AT parser library.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v1.0.0
*/
#include <string.h>
#include <stdlib.h>
#include "system/lwesp_sys.h"
#include "windows.h"
#if !__DOXYGEN__
/**
* \brief Custom message queue implementation for WIN32
*/
typedef struct {
lwesp_sys_sem_t sem_not_empty; /*!< Semaphore indicates not empty */
lwesp_sys_sem_t sem_not_full; /*!< Semaphore indicates not full */
lwesp_sys_sem_t sem; /*!< Semaphore to lock access */
size_t in, out, size;
void* entries[1];
} win32_mbox_t;
static LARGE_INTEGER freq, sys_start_time;
static lwesp_sys_mutex_t sys_mutex; /* Mutex ID for main protection */
/**
* \brief Check if message box is full
* \param[in] m: Message box handle
* \return 1 if full, 0 otherwise
*/
static uint8_t
mbox_is_full(win32_mbox_t* m) {
size_t size = 0;
if (m->in > m->out) {
size = (m->in - m->out);
} else if (m->out > m->in) {
size = m->size - m->out + m->in;
}
return size == m->size - 1;
}
/**
* \brief Check if message box is empty
* \param[in] m: Message box handle
* \return 1 if empty, 0 otherwise
*/
static uint8_t
mbox_is_empty(win32_mbox_t* m) {
return m->in == m->out;
}
/**
* \brief Get current kernel time in units of milliseconds
*/
static uint32_t
osKernelSysTick(void) {
LONGLONG ret;
LARGE_INTEGER now;
QueryPerformanceFrequency(&freq); /* Get frequency */
QueryPerformanceCounter(&now); /* Get current time */
ret = now.QuadPart - sys_start_time.QuadPart;
return (uint32_t)(((ret) * 1000) / freq.QuadPart);
}
uint8_t
lwesp_sys_init(void) {
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&sys_start_time);
lwesp_sys_mutex_create(&sys_mutex);
return 1;
}
uint32_t
lwesp_sys_now(void) {
return osKernelSysTick();
}
#if LWESP_CFG_OS
uint8_t
lwesp_sys_protect(void) {
lwesp_sys_mutex_lock(&sys_mutex);
return 1;
}
uint8_t
lwesp_sys_unprotect(void) {
lwesp_sys_mutex_unlock(&sys_mutex);
return 1;
}
uint8_t
lwesp_sys_mutex_create(lwesp_sys_mutex_t* p) {
*p = CreateMutex(NULL, FALSE, NULL);
return *p != NULL;
}
uint8_t
lwesp_sys_mutex_delete(lwesp_sys_mutex_t* p) {
return CloseHandle(*p);
}
uint8_t
lwesp_sys_mutex_lock(lwesp_sys_mutex_t* p) {
DWORD ret;
ret = WaitForSingleObject(*p, INFINITE);
if (ret != WAIT_OBJECT_0) {
return 0;
}
return 1;
}
uint8_t
lwesp_sys_mutex_unlock(lwesp_sys_mutex_t* p) {
return ReleaseMutex(*p);
}
uint8_t
lwesp_sys_mutex_isvalid(lwesp_sys_mutex_t* p) {
return p != NULL && *p != NULL;
}
uint8_t
lwesp_sys_mutex_invalid(lwesp_sys_mutex_t* p) {
*p = LWESP_SYS_MUTEX_NULL;
return 1;
}
uint8_t
lwesp_sys_sem_create(lwesp_sys_sem_t* p, uint8_t cnt) {
HANDLE h;
h = CreateSemaphore(NULL, !!cnt, 1, NULL);
*p = h;
return *p != NULL;
}
uint8_t
lwesp_sys_sem_delete(lwesp_sys_sem_t* p) {
return CloseHandle(*p);
}
uint32_t
lwesp_sys_sem_wait(lwesp_sys_sem_t* p, uint32_t timeout) {
DWORD ret;
uint32_t tick = osKernelSysTick();
if (timeout == 0) {
ret = WaitForSingleObject(*p, INFINITE);
return 1;
} else {
ret = WaitForSingleObject(*p, timeout);
if (ret == WAIT_OBJECT_0) {
return 1;
} else {
return LWESP_SYS_TIMEOUT;
}
}
}
uint8_t
lwesp_sys_sem_release(lwesp_sys_sem_t* p) {
return ReleaseSemaphore(*p, 1, NULL);
}
uint8_t
lwesp_sys_sem_isvalid(lwesp_sys_sem_t* p) {
return p != NULL && *p != NULL;
}
uint8_t
lwesp_sys_sem_invalid(lwesp_sys_sem_t* p) {
*p = LWESP_SYS_SEM_NULL;
return 1;
}
uint8_t
lwesp_sys_mbox_create(lwesp_sys_mbox_t* b, size_t size) {
win32_mbox_t* mbox;
*b = 0;
mbox = malloc(sizeof(*mbox) + size * sizeof(void*));
if (mbox != NULL) {
memset(mbox, 0x00, sizeof(*mbox));
mbox->size = size + 1; /* Set it to 1 more as cyclic buffer has only one less than size */
lwesp_sys_sem_create(&mbox->sem, 1);
lwesp_sys_sem_create(&mbox->sem_not_empty, 0);
lwesp_sys_sem_create(&mbox->sem_not_full, 0);
*b = mbox;
}
return *b != NULL;
}
uint8_t
lwesp_sys_mbox_delete(lwesp_sys_mbox_t* b) {
win32_mbox_t* mbox = *b;
lwesp_sys_sem_delete(&mbox->sem);
lwesp_sys_sem_delete(&mbox->sem_not_full);
lwesp_sys_sem_delete(&mbox->sem_not_empty);
free(mbox);
return 1;
}
uint32_t
lwesp_sys_mbox_put(lwesp_sys_mbox_t* b, void* m) {
win32_mbox_t* mbox = *b;
uint32_t time = osKernelSysTick(); /* Get start time */
lwesp_sys_sem_wait(&mbox->sem, 0); /* Wait for access */
/*
* Since function is blocking until ready to write something to queue,
* wait and release the semaphores to allow other threads
* to process the queue before we can write new value.
*/
while (mbox_is_full(mbox)) {
lwesp_sys_sem_release(&mbox->sem); /* Release semaphore */
lwesp_sys_sem_wait(&mbox->sem_not_full, 0); /* Wait for semaphore indicating not full */
lwesp_sys_sem_wait(&mbox->sem, 0); /* Wait availability again */
}
mbox->entries[mbox->in] = m;
if (++mbox->in >= mbox->size) {
mbox->in = 0;
}
lwesp_sys_sem_release(&mbox->sem_not_empty);/* Signal non-empty state */
lwesp_sys_sem_release(&mbox->sem); /* Release access for other threads */
return osKernelSysTick() - time;
}
uint32_t
lwesp_sys_mbox_get(lwesp_sys_mbox_t* b, void** m, uint32_t timeout) {
win32_mbox_t* mbox = *b;
uint32_t time;
time = osKernelSysTick();
/* Get exclusive access to message queue */
if (lwesp_sys_sem_wait(&mbox->sem, timeout) == LWESP_SYS_TIMEOUT) {
return LWESP_SYS_TIMEOUT;
}
while (mbox_is_empty(mbox)) {
lwesp_sys_sem_release(&mbox->sem);
if (lwesp_sys_sem_wait(&mbox->sem_not_empty, timeout) == LWESP_SYS_TIMEOUT) {
return LWESP_SYS_TIMEOUT;
}
lwesp_sys_sem_wait(&mbox->sem, timeout);
}
*m = mbox->entries[mbox->out];
if (++mbox->out >= mbox->size) {
mbox->out = 0;
}
lwesp_sys_sem_release(&mbox->sem_not_full);
lwesp_sys_sem_release(&mbox->sem);
return osKernelSysTick() - time;
}
uint8_t
lwesp_sys_mbox_putnow(lwesp_sys_mbox_t* b, void* m) {
win32_mbox_t* mbox = *b;
lwesp_sys_sem_wait(&mbox->sem, 0);
if (mbox_is_full(mbox)) {
lwesp_sys_sem_release(&mbox->sem);
return 0;
}
mbox->entries[mbox->in] = m;
if (mbox->in == mbox->out) {
lwesp_sys_sem_release(&mbox->sem_not_empty);
}
if (++mbox->in >= mbox->size) {
mbox->in = 0;
}
lwesp_sys_sem_release(&mbox->sem);
return 1;
}
uint8_t
lwesp_sys_mbox_getnow(lwesp_sys_mbox_t* b, void** m) {
win32_mbox_t* mbox = *b;
lwesp_sys_sem_wait(&mbox->sem, 0); /* Wait exclusive access */
if (mbox->in == mbox->out) {
lwesp_sys_sem_release(&mbox->sem); /* Release access */
return 0;
}
*m = mbox->entries[mbox->out];
if (++mbox->out >= mbox->size) {
mbox->out = 0;
}
lwesp_sys_sem_release(&mbox->sem_not_full); /* Queue not full anymore */
lwesp_sys_sem_release(&mbox->sem); /* Release semaphore */
return 1;
}
uint8_t
lwesp_sys_mbox_isvalid(lwesp_sys_mbox_t* b) {
return b != NULL && *b != NULL;
}
uint8_t
lwesp_sys_mbox_invalid(lwesp_sys_mbox_t* b) {
*b = LWESP_SYS_MBOX_NULL;
return 1;
}
uint8_t
lwesp_sys_thread_create(lwesp_sys_thread_t* t, const char* name, lwesp_sys_thread_fn thread_func, void* const arg, size_t stack_size, lwesp_sys_thread_prio_t prio) {
HANDLE h;
DWORD id;
h = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)thread_func, arg, 0, &id);
if (t != NULL) {
*t = h;
}
return h != NULL;
}
uint8_t
lwesp_sys_thread_terminate(lwesp_sys_thread_t* t) {
HANDLE h = NULL;
if (t == NULL) { /* Shall we terminate ourself? */
h = GetCurrentThread(); /* Get current thread handle */
} else { /* We have known thread, find handle by looking at ID */
h = *t;
}
TerminateThread(h, 0);
return 1;
}
uint8_t
lwesp_sys_thread_yield(void) {
/* Not implemented */
return 1;
}
#endif /* LWESP_CFG_OS */
#endif /* !__DOXYGEN__ */
|
Example: System functions for CMSIS-OS¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | /**
* \file lwesp_sys_port.h
* \brief CMSIS-OS based system file
*/
/*
* Copyright (c) 2020 Tilen MAJERLE
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* This file is part of LwESP - Lightweight ESP-AT parser library.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v1.0.0
*/
#ifndef LWESP_HDR_SYSTEM_PORT_H
#define LWESP_HDR_SYSTEM_PORT_H
#include <stdint.h>
#include <stdlib.h>
#include "lwesp/lwesp_opt.h"
#include "cmsis_os.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#if LWESP_CFG_OS && !__DOXYGEN__
typedef osMutexId_t lwesp_sys_mutex_t;
typedef osSemaphoreId_t lwesp_sys_sem_t;
typedef osMessageQueueId_t lwesp_sys_mbox_t;
typedef osThreadId_t lwesp_sys_thread_t;
typedef osPriority_t lwesp_sys_thread_prio_t;
#define LWESP_SYS_MUTEX_NULL ((lwesp_sys_mutex_t)0)
#define LWESP_SYS_SEM_NULL ((lwesp_sys_sem_t)0)
#define LWESP_SYS_MBOX_NULL ((lwesp_sys_mbox_t)0)
#define LWESP_SYS_TIMEOUT ((uint32_t)osWaitForever)
#define LWESP_SYS_THREAD_PRIO (osPriorityNormal)
#define LWESP_SYS_THREAD_SS (512)
#endif /* LWESP_CFG_OS && !__DOXYGEN__ */
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* LWESP_HDR_SYSTEM_PORT_H */
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | /**
* \file lwesp_sys_cmsis_os.c
* \brief System dependent functions for CMSIS based operating system
*/
/*
* Copyright (c) 2020 Tilen MAJERLE
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* This file is part of LwESP - Lightweight ESP-AT parser library.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v1.0.0
*/
#include "system/lwesp_sys.h"
#include "cmsis_os.h"
#if !__DOXYGEN__
static osMutexId_t sys_mutex;
uint8_t
lwesp_sys_init(void) {
lwesp_sys_mutex_create(&sys_mutex);
return 1;
}
uint32_t
lwesp_sys_now(void) {
return osKernelSysTick();
}
uint8_t
lwesp_sys_protect(void) {
lwesp_sys_mutex_lock(&sys_mutex);
return 1;
}
uint8_t
lwesp_sys_unprotect(void) {
lwesp_sys_mutex_unlock(&sys_mutex);
return 1;
}
uint8_t
lwesp_sys_mutex_create(lwesp_sys_mutex_t* p) {
const osMutexAttr_t attr = {
.attr_bits = osMutexRecursive
};
*p = osMutexNew(&attr);
return *p != NULL;
}
uint8_t
lwesp_sys_mutex_delete(lwesp_sys_mutex_t* p) {
return osMutexDelete(*p) == osOK;
}
uint8_t
lwesp_sys_mutex_lock(lwesp_sys_mutex_t* p) {
return osMutexAcquire(*p, osWaitForever) == osOK;
}
uint8_t
lwesp_sys_mutex_unlock(lwesp_sys_mutex_t* p) {
return osMutexRelease(*p) == osOK;
}
uint8_t
lwesp_sys_mutex_isvalid(lwesp_sys_mutex_t* p) {
return p != NULL && *p != NULL;
}
uint8_t
lwesp_sys_mutex_invalid(lwesp_sys_mutex_t* p) {
*p = LWESP_SYS_MUTEX_NULL;
return 1;
}
uint8_t
lwesp_sys_sem_create(lwesp_sys_sem_t* p, uint8_t cnt) {
return (*p = osSemaphoreNew(1, cnt > 0 ? 1 : 0, NULL)) != NULL;
}
uint8_t
lwesp_sys_sem_delete(lwesp_sys_sem_t* p) {
return osSemaphoreDelete(*p) == osOK;
}
uint32_t
lwesp_sys_sem_wait(lwesp_sys_sem_t* p, uint32_t timeout) {
uint32_t tick = osKernelSysTick();
return (osSemaphoreAcquire(*p, timeout == 0 ? osWaitForever : timeout) == osOK) ? (osKernelSysTick() - tick) : LWESP_SYS_TIMEOUT;
}
uint8_t
lwesp_sys_sem_release(lwesp_sys_sem_t* p) {
return osSemaphoreRelease(*p) == osOK;
}
uint8_t
lwesp_sys_sem_isvalid(lwesp_sys_sem_t* p) {
return p != NULL && *p != NULL;
}
uint8_t
lwesp_sys_sem_invalid(lwesp_sys_sem_t* p) {
*p = LWESP_SYS_SEM_NULL;
return 1;
}
uint8_t
lwesp_sys_mbox_create(lwesp_sys_mbox_t* b, size_t size) {
return (*b = osMessageQueueNew(size, sizeof(void*), NULL)) != NULL;
}
uint8_t
lwesp_sys_mbox_delete(lwesp_sys_mbox_t* b) {
if (osMessageQueueGetCount(*b) > 0) {
return 0;
}
return osMessageQueueDelete(*b) == osOK;
}
uint32_t
lwesp_sys_mbox_put(lwesp_sys_mbox_t* b, void* m) {
uint32_t tick = osKernelSysTick();
return osMessageQueuePut(*b, &m, 0, osWaitForever) == osOK ? (osKernelSysTick() - tick) : LWESP_SYS_TIMEOUT;
}
uint32_t
lwesp_sys_mbox_get(lwesp_sys_mbox_t* b, void** m, uint32_t timeout) {
uint32_t tick = osKernelSysTick();
return (osMessageQueueGet(*b, m, NULL, timeout == 0 ? osWaitForever : timeout) == osOK) ? (osKernelSysTick() - tick) : LWESP_SYS_TIMEOUT;
}
uint8_t
lwesp_sys_mbox_putnow(lwesp_sys_mbox_t* b, void* m) {
return osMessageQueuePut(*b, &m, 0, 0) == osOK;
}
uint8_t
lwesp_sys_mbox_getnow(lwesp_sys_mbox_t* b, void** m) {
return osMessageQueueGet(*b, m, NULL, 0) == osOK;
}
uint8_t
lwesp_sys_mbox_isvalid(lwesp_sys_mbox_t* b) {
return b != NULL && *b != NULL;
}
uint8_t
lwesp_sys_mbox_invalid(lwesp_sys_mbox_t* b) {
*b = LWESP_SYS_MBOX_NULL;
return 1;
}
uint8_t
lwesp_sys_thread_create(lwesp_sys_thread_t* t, const char* name, lwesp_sys_thread_fn thread_func, void* const arg, size_t stack_size, lwesp_sys_thread_prio_t prio) {
lwesp_sys_thread_t id;
const osThreadAttr_t thread_attr = {
.name = (char*)name,
.priority = (osPriority)prio,
.stack_size = stack_size > 0 ? stack_size : LWESP_SYS_THREAD_SS
};
id = osThreadNew(thread_func, arg, &thread_attr);
if (t != NULL) {
*t = id;
}
return id != NULL;
}
uint8_t
lwesp_sys_thread_terminate(lwesp_sys_thread_t* t) {
if (t != NULL) {
osThreadTerminate(*t);
} else {
osThreadExit();
}
return 1;
}
uint8_t
lwesp_sys_thread_yield(void) {
osThreadYield();
return 1;
}
#endif /* !__DOXYGEN__ */
|