Porting guide¶
High level of LwGSM library is platform independent, written in ANSI C99, however there is an important part where middleware needs to communicate with target GSM 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 GSM 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
lwgsm_ll_init()
andlwgsm_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 GSM middleware. GSM 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 functions, 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 lwgsm_sys_
.
It must also prepare header file for standard types in order to support OS types within GSM middleware.
An example code is provided latter section of this page for WIN32 and STM32.
Note
Check System functions for function prototypes.
Steps to follow¶
Copy
lwgsm/src/system/lwgsm_sys_template.c
to the same folder and rename it to application port, eg.lwgsm_sys_win32.c
Open newly created file and implement all system functions
Copy folder
lwgsm/src/include/system/port/template/*
to the same folder and rename folder name to application port, eg.cmsis_os
Open
lwgsm_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
lwgsm_input_process()
orlwgsm_input()
functions, based on application configuration ofLWGSM_CFG_INPUT_USE_PROCESS
parameter.When
LWGSM_CFG_INPUT_USE_PROCESS
is disabled, dedicated receive buffer is created by LwGSM library andlwgsm_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
LWGSM_CFG_INPUT_USE_PROCESS
is enabled,lwgsm_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 ofLWGSM_MEM_SIZE
sizeIt sets send and reset callback functions for LwGSM 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 | /**
* \file lwgsm_ll_win32.c
* \brief Low-level communication with GSM 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 LwGSM - Lightweight GSM-AT library.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v0.1.0
*/
#include "system/lwgsm_ll.h"
#include "lwgsm/lwgsm.h"
#include "lwgsm/lwgsm_mem.h"
#include "lwgsm/lwgsm_input.h"
#if !__DOXYGEN__
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 GSM device, function called from GSM stack when we have data to send
* \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) {
DWORD written;
if (com_port != NULL) {
#if !LWGSM_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 /* !LWGSM_CFG_AT_ECHO */
/* Write data to AT port */
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) {
static const LPCWSTR com_ports[] = {
L"\\\\.\\COM23",
L"\\\\.\\COM12",
L"\\\\.\\COM9",
L"\\\\.\\COM8",
L"\\\\.\\COM4"
};
for (size_t i = 0; i < sizeof(com_ports) / sizeof(com_ports[0]); ++i) {
com_port = CreateFile(com_ports[i],
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
NULL
);
if (GetCommState(com_port, &dcb)) {
printf("COM PORT %s opened!\r\n", (const char*)com_ports[i]);
break;
}
}
}
/* 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) {
thread_handle = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)uart_thread, NULL, 0, 0);
}
}
/**
* \brief UART thread
*/
static void
uart_thread(void* param) {
DWORD bytes_read;
lwgsm_sys_sem_t sem;
FILE* file = NULL;
lwgsm_sys_sem_create(&sem, 0); /* Create semaphore for delay functions */
while (com_port == NULL) {
lwgsm_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);
/* Send received data to input processing module */
#if LWGSM_CFG_INPUT_USE_PROCESS
lwgsm_input_process(data_buffer, (size_t)bytes_read);
#else /* LWGSM_CFG_INPUT_USE_PROCESS */
lwgsm_input(data_buffer, (size_t)bytes_read);
#endif /* !LWGSM_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 */
lwgsm_sys_sem_wait(&sem, 1);
}
}
/**
* \brief Callback function called from initialization process
*
* \note This function may be called multiple times if AT baudrate is changed from application.
* It is important that every configuration except AT baudrate is configured only once!
*
* \note This function may be called from different threads in GSM stack when using OS.
* When \ref LWGSM_CFG_INPUT_USE_PROCESS is set to 1, this function may be called from user UART thread.
*
* \param[in,out] ll: Pointer to \ref lwgsm_ll_t structure to fill data for communication functions
* \return \ref lwgsmOK on success, member of \ref lwgsmr_t enumeration otherwise
*/
lwgsmr_t
lwgsm_ll_init(lwgsm_ll_t* ll) {
#if !LWGSM_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
*/
lwgsm_mem_region_t mem_regions[] = {
{ memory, sizeof(memory) }
};
if (!initialized) {
lwgsm_mem_assignmemory(mem_regions, LWGSM_ARRAYSIZE(mem_regions)); /* Assign memory for allocations to GSM library */
}
#endif /* !LWGSM_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 */
}
/* Step 3: Configure AT port to be able to send/receive data to/from GSM device */
configure_uart(ll->uart.baudrate); /* Initialize UART for communication */
initialized = 1;
return lwgsmOK;
}
#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
lwgsm_input_process()
function to directly process received data without using intermediate receive bufferMemory manager has been assigned to
1
region ofLWGSM_MEM_SIZE
sizeIt sets send and reset callback functions for LwGSM 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 | /**
* \file lwgsm_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 LwGSM - Lightweight GSM-AT library.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v0.1.0
*/
/*
* How it works
*
* On first call to \ref lwgsm_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 LWGSM_CFG_INPUT_USE_PROCESS must be enabled in `lwgsm_config.h` to use this driver.
*/
#include "lwgsm/lwgsm.h"
#include "lwgsm/lwgsm_mem.h"
#include "lwgsm/lwgsm_input.h"
#include "system/lwgsm_ll.h"
#if !__DOXYGEN__
#if !LWGSM_CFG_INPUT_USE_PROCESS
#error "LWGSM_CFG_INPUT_USE_PROCESS must be enabled in `lwgsm_config.h` to use this driver."
#endif /* LWGSM_CFG_INPUT_USE_PROCESS */
#if !defined(LWGSM_USART_DMA_RX_BUFF_SIZE)
#define LWGSM_USART_DMA_RX_BUFF_SIZE 0x1000
#endif /* !defined(LWGSM_USART_DMA_RX_BUFF_SIZE) */
#if !defined(LWGSM_MEM_SIZE)
#define LWGSM_MEM_SIZE 0x1000
#endif /* !defined(LWGSM_MEM_SIZE) */
#if !defined(LWGSM_USART_RDR_NAME)
#define LWGSM_USART_RDR_NAME RDR
#endif /* !defined(LWGSM_USART_RDR_NAME) */
/* USART memory */
static uint8_t usart_mem[LWGSM_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;
LWGSM_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(LWGSM_USART_DMA_RX_STREAM)
pos = sizeof(usart_mem) - LL_DMA_GetDataLength(LWGSM_USART_DMA, LWGSM_USART_DMA_RX_STREAM);
#else
pos = sizeof(usart_mem) - LL_DMA_GetDataLength(LWGSM_USART_DMA, LWGSM_USART_DMA_RX_CH);
#endif /* defined(LWGSM_USART_DMA_RX_STREAM) */
if (pos != old_pos && is_running) {
if (pos > old_pos) {
lwgsm_input_process(&usart_mem[old_pos], pos - old_pos);
} else {
lwgsm_input_process(&usart_mem[old_pos], sizeof(usart_mem) - old_pos);
if (pos > 0) {
lwgsm_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 */
LWGSM_USART_CLK;
LWGSM_USART_DMA_CLK;
LWGSM_USART_TX_PORT_CLK;
LWGSM_USART_RX_PORT_CLK;
#if defined(LWGSM_RESET_PIN)
LWGSM_RESET_PORT_CLK;
#endif /* defined(LWGSM_RESET_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(LWGSM_RESET_PIN)
/* Configure RESET pin */
gpio_init.Pin = LWGSM_RESET_PIN;
LL_GPIO_Init(LWGSM_RESET_PORT, &gpio_init);
#endif /* defined(LWGSM_RESET_PIN) */
/* Configure USART pins */
gpio_init.Mode = LL_GPIO_MODE_ALTERNATE;
/* TX PIN */
gpio_init.Alternate = LWGSM_USART_TX_PIN_AF;
gpio_init.Pin = LWGSM_USART_TX_PIN;
LL_GPIO_Init(LWGSM_USART_TX_PORT, &gpio_init);
/* RX PIN */
gpio_init.Alternate = LWGSM_USART_RX_PIN_AF;
gpio_init.Pin = LWGSM_USART_RX_PIN;
LL_GPIO_Init(LWGSM_USART_RX_PORT, &gpio_init);
/* Configure UART */
LL_USART_DeInit(LWGSM_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(LWGSM_USART, &usart_init);
/* Enable USART interrupts and DMA request */
LL_USART_EnableIT_IDLE(LWGSM_USART);
LL_USART_EnableIT_PE(LWGSM_USART);
LL_USART_EnableIT_ERROR(LWGSM_USART);
LL_USART_EnableDMAReq_RX(LWGSM_USART);
/* Enable USART interrupts */
NVIC_SetPriority(LWGSM_USART_IRQ, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0x07, 0x00));
NVIC_EnableIRQ(LWGSM_USART_IRQ);
/* Configure DMA */
is_running = 0;
#if defined(LWGSM_USART_DMA_RX_STREAM)
LL_DMA_DeInit(LWGSM_USART_DMA, LWGSM_USART_DMA_RX_STREAM);
dma_init.Channel = LWGSM_USART_DMA_RX_CH;
#else
LL_DMA_DeInit(LWGSM_USART_DMA, LWGSM_USART_DMA_RX_CH);
dma_init.PeriphRequest = LWGSM_USART_DMA_RX_REQ_NUM;
#endif /* defined(LWGSM_USART_DMA_RX_STREAM) */
dma_init.PeriphOrM2MSrcAddress = (uint32_t)&LWGSM_USART->LWGSM_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(LWGSM_USART_DMA_RX_STREAM)
LL_DMA_Init(LWGSM_USART_DMA, LWGSM_USART_DMA_RX_STREAM, &dma_init);
#else
LL_DMA_Init(LWGSM_USART_DMA, LWGSM_USART_DMA_RX_CH, &dma_init);
#endif /* defined(LWGSM_USART_DMA_RX_STREAM) */
/* Enable DMA interrupts */
#if defined(LWGSM_USART_DMA_RX_STREAM)
LL_DMA_EnableIT_HT(LWGSM_USART_DMA, LWGSM_USART_DMA_RX_STREAM);
LL_DMA_EnableIT_TC(LWGSM_USART_DMA, LWGSM_USART_DMA_RX_STREAM);
LL_DMA_EnableIT_TE(LWGSM_USART_DMA, LWGSM_USART_DMA_RX_STREAM);
LL_DMA_EnableIT_FE(LWGSM_USART_DMA, LWGSM_USART_DMA_RX_STREAM);
LL_DMA_EnableIT_DME(LWGSM_USART_DMA, LWGSM_USART_DMA_RX_STREAM);
#else
LL_DMA_EnableIT_HT(LWGSM_USART_DMA, LWGSM_USART_DMA_RX_CH);
LL_DMA_EnableIT_TC(LWGSM_USART_DMA, LWGSM_USART_DMA_RX_CH);
LL_DMA_EnableIT_TE(LWGSM_USART_DMA, LWGSM_USART_DMA_RX_CH);
#endif /* defined(LWGSM_USART_DMA_RX_STREAM) */
/* Enable DMA interrupts */
NVIC_SetPriority(LWGSM_USART_DMA_RX_IRQ, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0x07, 0x00));
NVIC_EnableIRQ(LWGSM_USART_DMA_RX_IRQ);
old_pos = 0;
is_running = 1;
/* Start DMA and USART */
#if defined(LWGSM_USART_DMA_RX_STREAM)
LL_DMA_EnableStream(LWGSM_USART_DMA, LWGSM_USART_DMA_RX_STREAM);
#else
LL_DMA_EnableChannel(LWGSM_USART_DMA, LWGSM_USART_DMA_RX_CH);
#endif /* defined(LWGSM_USART_DMA_RX_STREAM) */
LL_USART_Enable(LWGSM_USART);
} else {
osDelay(10);
LL_USART_Disable(LWGSM_USART);
usart_init.BaudRate = baudrate;
LL_USART_Init(LWGSM_USART, &usart_init);
LL_USART_Enable(LWGSM_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(LWGSM_RESET_PIN)
/**
* \brief Hardware reset callback
*/
static uint8_t
reset_device(uint8_t state) {
if (state) { /* Activate reset line */
LL_GPIO_ResetOutputPin(LWGSM_RESET_PORT, LWGSM_RESET_PIN);
} else {
LL_GPIO_SetOutputPin(LWGSM_RESET_PORT, LWGSM_RESET_PIN);
}
return 1;
}
#endif /* defined(LWGSM_RESET_PIN) */
/**
* \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
*/
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(LWGSM_USART, *d);
while (!LL_USART_IsActiveFlag_TXE(LWGSM_USART)) {}
}
return len;
}
/**
* \brief Callback function called from initialization process
* \note This function may be called multiple times if AT baudrate is changed from application
* \param[in,out] ll: Pointer to \ref lwgsm_ll_t structure to fill data for communication functions
* \param[in] baudrate: Baudrate to use on AT port
* \return Member of \ref lwgsmr_t enumeration
*/
lwgsmr_t
lwgsm_ll_init(lwgsm_ll_t* ll) {
#if !LWGSM_CFG_MEM_CUSTOM
static uint8_t memory[LWGSM_MEM_SIZE];
lwgsm_mem_region_t mem_regions[] = {
{ memory, sizeof(memory) }
};
if (!initialized) {
lwgsm_mem_assignmemory(mem_regions, LWGSM_ARRAYSIZE(mem_regions)); /* Assign memory for allocations */
}
#endif /* !LWGSM_CFG_MEM_CUSTOM */
if (!initialized) {
ll->send_fn = send_data; /* Set callback function to send data */
#if defined(LWGSM_RESET_PIN)
ll->reset_fn = reset_device; /* Set callback for hardware reset */
#endif /* defined(LWGSM_RESET_PIN) */
}
configure_uart(ll->uart.baudrate); /* Initialize UART for communication */
initialized = 1;
return lwgsmOK;
}
/**
* \brief Callback function to de-init low-level communication part
* \param[in,out] ll: Pointer to \ref lwgsm_ll_t structure to fill data for communication functions
* \return \ref lwgsmOK on success, member of \ref lwgsmr_t enumeration otherwise
*/
lwgsmr_t
lwgsm_ll_deinit(lwgsm_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;
LWGSM_UNUSED(ll);
return lwgsmOK;
}
/**
* \brief UART global interrupt handler
*/
void
LWGSM_USART_IRQHANDLER(void) {
LL_USART_ClearFlag_IDLE(LWGSM_USART);
LL_USART_ClearFlag_PE(LWGSM_USART);
LL_USART_ClearFlag_FE(LWGSM_USART);
LL_USART_ClearFlag_ORE(LWGSM_USART);
LL_USART_ClearFlag_NE(LWGSM_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
LWGSM_USART_DMA_RX_IRQHANDLER(void) {
LWGSM_USART_DMA_RX_CLEAR_TC;
LWGSM_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 lwgsm_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 LwGSM - Lightweight GSM-AT library.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v0.1.0
*/
#ifndef LWGSM_HDR_SYSTEM_PORT_H
#define LWGSM_HDR_SYSTEM_PORT_H
#include <stdint.h>
#include <stdlib.h>
#include "lwgsm/lwgsm_opt.h"
#include "windows.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#if LWGSM_CFG_OS && !__DOXYGEN__
typedef HANDLE lwgsm_sys_mutex_t;
typedef HANDLE lwgsm_sys_sem_t;
typedef HANDLE lwgsm_sys_mbox_t;
typedef HANDLE lwgsm_sys_thread_t;
typedef int lwgsm_sys_thread_prio_t;
#define LWGSM_SYS_MUTEX_NULL ((HANDLE)0)
#define LWGSM_SYS_SEM_NULL ((HANDLE)0)
#define LWGSM_SYS_MBOX_NULL ((HANDLE)0)
#define LWGSM_SYS_TIMEOUT (INFINITE)
#define LWGSM_SYS_THREAD_PRIO (0)
#define LWGSM_SYS_THREAD_SS (4096)
#endif /* LWGSM_CFG_OS && !__DOXYGEN__ */
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* LWGSM_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 | /**
* \file lwgsm_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 LwGSM - Lightweight GSM-AT library.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v0.1.0
*/
#include <string.h>
#include <stdlib.h>
#include "system/lwgsm_sys.h"
#if !__DOXYGEN__
/**
* \brief Custom message queue implementation for WIN32
*/
typedef struct {
lwgsm_sys_sem_t sem_not_empty; /*!< Semaphore indicates not empty */
lwgsm_sys_sem_t sem_not_full; /*!< Semaphore indicates not full */
lwgsm_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 lwgsm_sys_mutex_t sys_mutex; /* Mutex ID for main protection */
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;
}
static uint8_t
mbox_is_empty(win32_mbox_t* m) {
return m->in == m->out;
}
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
lwgsm_sys_init(void) {
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&sys_start_time);
lwgsm_sys_mutex_create(&sys_mutex);
return 1;
}
uint32_t
lwgsm_sys_now(void) {
return osKernelSysTick();
}
uint8_t
lwgsm_sys_protect(void) {
lwgsm_sys_mutex_lock(&sys_mutex);
return 1;
}
uint8_t
lwgsm_sys_unprotect(void) {
lwgsm_sys_mutex_unlock(&sys_mutex);
return 1;
}
uint8_t
lwgsm_sys_mutex_create(lwgsm_sys_mutex_t* p) {
*p = CreateMutex(NULL, FALSE, NULL);
return *p != NULL;
}
uint8_t
lwgsm_sys_mutex_delete(lwgsm_sys_mutex_t* p) {
return CloseHandle(*p);
}
uint8_t
lwgsm_sys_mutex_lock(lwgsm_sys_mutex_t* p) {
DWORD ret;
ret = WaitForSingleObject(*p, INFINITE);
if (ret != WAIT_OBJECT_0) {
return 0;
}
return 1;
}
uint8_t
lwgsm_sys_mutex_unlock(lwgsm_sys_mutex_t* p) {
return (uint8_t)ReleaseMutex(*p);
}
uint8_t
lwgsm_sys_mutex_isvalid(lwgsm_sys_mutex_t* p) {
return p != NULL && *p != NULL;
}
uint8_t
lwgsm_sys_mutex_invalid(lwgsm_sys_mutex_t* p) {
*p = LWGSM_SYS_MUTEX_NULL;
return 1;
}
uint8_t
lwgsm_sys_sem_create(lwgsm_sys_sem_t* p, uint8_t cnt) {
HANDLE h;
h = CreateSemaphore(NULL, !!cnt, 1, NULL);
*p = h;
return *p != NULL;
}
uint8_t
lwgsm_sys_sem_delete(lwgsm_sys_sem_t* p) {
return CloseHandle(*p);
}
uint32_t
lwgsm_sys_sem_wait(lwgsm_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 LWGSM_SYS_TIMEOUT;
}
}
}
uint8_t
lwgsm_sys_sem_release(lwgsm_sys_sem_t* p) {
return ReleaseSemaphore(*p, 1, NULL);
}
uint8_t
lwgsm_sys_sem_isvalid(lwgsm_sys_sem_t* p) {
return p != NULL && *p != NULL;
}
uint8_t
lwgsm_sys_sem_invalid(lwgsm_sys_sem_t* p) {
*p = LWGSM_SYS_SEM_NULL;
return 1;
}
uint8_t
lwgsm_sys_mbox_create(lwgsm_sys_mbox_t* b, size_t size) {
win32_mbox_t* mbox;
*b = NULL;
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 */
lwgsm_sys_sem_create(&mbox->sem, 1);
lwgsm_sys_sem_create(&mbox->sem_not_empty, 0);
lwgsm_sys_sem_create(&mbox->sem_not_full, 0);
*b = mbox;
}
return *b != NULL;
}
uint8_t
lwgsm_sys_mbox_delete(lwgsm_sys_mbox_t* b) {
win32_mbox_t* mbox = *b;
lwgsm_sys_sem_delete(&mbox->sem);
lwgsm_sys_sem_delete(&mbox->sem_not_full);
lwgsm_sys_sem_delete(&mbox->sem_not_empty);
free(mbox);
return 1;
}
uint32_t
lwgsm_sys_mbox_put(lwgsm_sys_mbox_t* b, void* m) {
win32_mbox_t* mbox = *b;
uint32_t time = osKernelSysTick(); /* Get start time */
lwgsm_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)) {
lwgsm_sys_sem_release(&mbox->sem); /* Release semaphore */
lwgsm_sys_sem_wait(&mbox->sem_not_full, 0); /* Wait for semaphore indicating not full */
lwgsm_sys_sem_wait(&mbox->sem, 0); /* Wait availability again */
}
mbox->entries[mbox->in] = m;
if (++mbox->in >= mbox->size) {
mbox->in = 0;
}
lwgsm_sys_sem_release(&mbox->sem_not_empty);/* Signal non-empty state */
lwgsm_sys_sem_release(&mbox->sem); /* Release access for other threads */
return osKernelSysTick() - time;
}
uint32_t
lwgsm_sys_mbox_get(lwgsm_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 (lwgsm_sys_sem_wait(&mbox->sem, timeout) == LWGSM_SYS_TIMEOUT) {
return LWGSM_SYS_TIMEOUT;
}
while (mbox_is_empty(mbox)) {
lwgsm_sys_sem_release(&mbox->sem);
if (lwgsm_sys_sem_wait(&mbox->sem_not_empty, timeout) == LWGSM_SYS_TIMEOUT) {
return LWGSM_SYS_TIMEOUT;
}
lwgsm_sys_sem_wait(&mbox->sem, timeout);
}
*m = mbox->entries[mbox->out];
if (++mbox->out >= mbox->size) {
mbox->out = 0;
}
lwgsm_sys_sem_release(&mbox->sem_not_full);
lwgsm_sys_sem_release(&mbox->sem);
return osKernelSysTick() - time;
}
uint8_t
lwgsm_sys_mbox_putnow(lwgsm_sys_mbox_t* b, void* m) {
win32_mbox_t* mbox = *b;
lwgsm_sys_sem_wait(&mbox->sem, 0);
if (mbox_is_full(mbox)) {
lwgsm_sys_sem_release(&mbox->sem);
return 0;
}
mbox->entries[mbox->in] = m;
if (mbox->in == mbox->out) {
lwgsm_sys_sem_release(&mbox->sem_not_empty);
}
if (++mbox->in >= mbox->size) {
mbox->in = 0;
}
lwgsm_sys_sem_release(&mbox->sem);
return 1;
}
uint8_t
lwgsm_sys_mbox_getnow(lwgsm_sys_mbox_t* b, void** m) {
win32_mbox_t* mbox = *b;
lwgsm_sys_sem_wait(&mbox->sem, 0); /* Wait exclusive access */
if (mbox->in == mbox->out) {
lwgsm_sys_sem_release(&mbox->sem); /* Release access */
return 0;
}
*m = mbox->entries[mbox->out];
if (++mbox->out >= mbox->size) {
mbox->out = 0;
}
lwgsm_sys_sem_release(&mbox->sem_not_full); /* Queue not full anymore */
lwgsm_sys_sem_release(&mbox->sem); /* Release semaphore */
return 1;
}
uint8_t
lwgsm_sys_mbox_isvalid(lwgsm_sys_mbox_t* b) {
return b != NULL && *b != NULL; /* Return status if message box is valid */
}
uint8_t
lwgsm_sys_mbox_invalid(lwgsm_sys_mbox_t* b) {
*b = LWGSM_SYS_MBOX_NULL; /* Invalidate message box */
return 1;
}
uint8_t
lwgsm_sys_thread_create(lwgsm_sys_thread_t* t, const char* name, lwgsm_sys_thread_fn thread_func, void* const arg, size_t stack_size, lwgsm_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
lwgsm_sys_thread_terminate(lwgsm_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
lwgsm_sys_thread_yield(void) {
/* Not implemented */
return 1;
}
#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 lwgsm_sys_port.h
* \brief System dependent functions for CMSIS-OS 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 LwGSM - Lightweight GSM-AT library.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v0.1.0
*/
#ifndef LWGSM_HDR_SYSTEM_PORT_H
#define LWGSM_HDR_SYSTEM_PORT_H
#include <stdint.h>
#include <stdlib.h>
#include "lwgsm/lwgsm_opt.h"
#include "cmsis_os.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#if LWGSM_CFG_OS && !__DOXYGEN__
typedef osMutexId_t lwgsm_sys_mutex_t;
typedef osSemaphoreId_t lwgsm_sys_sem_t;
typedef osMessageQueueId_t lwgsm_sys_mbox_t;
typedef osThreadId_t lwgsm_sys_thread_t;
typedef osPriority_t lwgsm_sys_thread_prio_t;
#define LWGSM_SYS_MUTEX_NULL ((lwgsm_sys_mutex_t)0)
#define LWGSM_SYS_SEM_NULL ((lwgsm_sys_sem_t)0)
#define LWGSM_SYS_MBOX_NULL ((lwgsm_sys_mbox_t)0)
#define LWGSM_SYS_TIMEOUT ((uint32_t)osWaitForever)
#define LWGSM_SYS_THREAD_PRIO (osPriorityNormal)
#define LWGSM_SYS_THREAD_SS (512)
#endif /* LWGSM_CFG_OS && !__DOXYGEN__ */
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* LWGSM_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 lwgsm_sys_cmsis_os.c
* \brief System dependent functions for CMSIS-OS 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 LwGSM - Lightweight GSM-AT library.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v0.1.0
*/
#include "system/lwgsm_sys.h"
#include "cmsis_os.h"
#if !__DOXYGEN__
static osMutexId_t sys_mutex;
uint8_t
lwgsm_sys_init(void) {
lwgsm_sys_mutex_create(&sys_mutex);
return 1;
}
uint32_t
lwgsm_sys_now(void) {
return osKernelSysTick();
}
uint8_t
lwgsm_sys_protect(void) {
lwgsm_sys_mutex_lock(&sys_mutex);
return 1;
}
uint8_t
lwgsm_sys_unprotect(void) {
lwgsm_sys_mutex_unlock(&sys_mutex);
return 1;
}
uint8_t
lwgsm_sys_mutex_create(lwgsm_sys_mutex_t* p) {
const osMutexAttr_t attr = {
.attr_bits = osMutexRecursive
};
*p = osMutexNew(&attr);
return *p != NULL;
}
uint8_t
lwgsm_sys_mutex_delete(lwgsm_sys_mutex_t* p) {
return osMutexDelete(*p) == osOK;
}
uint8_t
lwgsm_sys_mutex_lock(lwgsm_sys_mutex_t* p) {
return osMutexAcquire(*p, osWaitForever) == osOK;
}
uint8_t
lwgsm_sys_mutex_unlock(lwgsm_sys_mutex_t* p) {
return osMutexRelease(*p) == osOK;
}
uint8_t
lwgsm_sys_mutex_isvalid(lwgsm_sys_mutex_t* p) {
return p != NULL && *p != NULL;
}
uint8_t
lwgsm_sys_mutex_invalid(lwgsm_sys_mutex_t* p) {
*p = LWGSM_SYS_MUTEX_NULL;
return 1;
}
uint8_t
lwgsm_sys_sem_create(lwgsm_sys_sem_t* p, uint8_t cnt) {
return (*p = osSemaphoreNew(1, cnt > 0 ? 1 : 0, NULL)) != NULL;
}
uint8_t
lwgsm_sys_sem_delete(lwgsm_sys_sem_t* p) {
return osSemaphoreDelete(*p) == osOK;
}
uint32_t
lwgsm_sys_sem_wait(lwgsm_sys_sem_t* p, uint32_t timeout) {
uint32_t tick = osKernelSysTick();
return (osSemaphoreAcquire(*p, timeout == 0 ? osWaitForever : timeout) == osOK) ? (osKernelSysTick() - tick) : LWGSM_SYS_TIMEOUT;
}
uint8_t
lwgsm_sys_sem_release(lwgsm_sys_sem_t* p) {
return osSemaphoreRelease(*p) == osOK;
}
uint8_t
lwgsm_sys_sem_isvalid(lwgsm_sys_sem_t* p) {
return p != NULL && *p != NULL;
}
uint8_t
lwgsm_sys_sem_invalid(lwgsm_sys_sem_t* p) {
*p = LWGSM_SYS_SEM_NULL;
return 1;
}
uint8_t
lwgsm_sys_mbox_create(lwgsm_sys_mbox_t* b, size_t size) {
return (*b = osMessageQueueNew(size, sizeof(void*), NULL)) != NULL;
}
uint8_t
lwgsm_sys_mbox_delete(lwgsm_sys_mbox_t* b) {
if (osMessageQueueGetCount(*b) > 0) {
return 0;
}
return osMessageQueueDelete(*b) == osOK;
}
uint32_t
lwgsm_sys_mbox_put(lwgsm_sys_mbox_t* b, void* m) {
uint32_t tick = osKernelSysTick();
return osMessageQueuePut(*b, &m, 0, osWaitForever) == osOK ? (osKernelSysTick() - tick) : LWGSM_SYS_TIMEOUT;
}
uint32_t
lwgsm_sys_mbox_get(lwgsm_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) : LWGSM_SYS_TIMEOUT;
}
uint8_t
lwgsm_sys_mbox_putnow(lwgsm_sys_mbox_t* b, void* m) {
return osMessageQueuePut(*b, &m, 0, 0) == osOK;
}
uint8_t
lwgsm_sys_mbox_getnow(lwgsm_sys_mbox_t* b, void** m) {
return osMessageQueueGet(*b, m, NULL, 0) == osOK;
}
uint8_t
lwgsm_sys_mbox_isvalid(lwgsm_sys_mbox_t* b) {
return b != NULL && *b != NULL;
}
uint8_t
lwgsm_sys_mbox_invalid(lwgsm_sys_mbox_t* b) {
*b = LWGSM_SYS_MBOX_NULL;
return 1;
}
uint8_t
lwgsm_sys_thread_create(lwgsm_sys_thread_t* t, const char* name, lwgsm_sys_thread_fn thread_func, void* const arg, size_t stack_size, lwgsm_sys_thread_prio_t prio) {
lwgsm_sys_thread_t id;
const osThreadAttr_t thread_attr = {
.name = (char*)name,
.priority = (osPriority)prio,
.stack_size = stack_size > 0 ? stack_size : LWGSM_SYS_THREAD_SS
};
id = osThreadNew(thread_func, arg, &thread_attr);
if (t != NULL) {
*t = id;
}
return id != NULL;
}
uint8_t
lwgsm_sys_thread_terminate(lwgsm_sys_thread_t* t) {
if (t != NULL) {
osThreadTerminate(*t);
} else {
osThreadExit();
}
return 1;
}
uint8_t
lwgsm_sys_thread_yield(void) {
osThreadYield();
return 1;
}
#endif /* !__DOXYGEN__ */
|