Quectel MC60-OpenCPU Series User Guide
Quectel MC60-OpenCPU Series User Guide
User Guide
Rev. MC60-OpenCPU_Series_User_Guide_V2.0
Date: 2017-07-05
www.quectel.com
GSM/GPRS/GNSS Module Series
MC60-OpenCPU Series User Guide
Our aim is to provide customers with timely and comprehensive service. For any
assistance, please contact our company headquarters:
GENERAL NOTES
QUECTEL OFFERS THE INFORMATION AS A SERVICE TO ITS CUSTOMERS. THE INFORMATION
PROVIDED IS BASED UPON CUSTOMERS‟ REQUIREMENTS. QUECTEL MAKES EVERY EFFORT
TO ENSURE THE QUALITY OF THE INFORMATION IT MAKES AVAILABLE. QUECTEL DOES NOT
MAKE ANY WARRANTY AS TO THE INFORMATION CONTAINED HEREIN, AND DOES NOT ACCEPT
ANY LIABILITY FOR ANY INJURY, LOSS OR DAMAGE OF ANY KIND INCURRED BY USE OF OR
RELIANCE UPON THE INFORMATION. ALL INFORMATION SUPPLIED HEREIN IS SUBJECT TO
CHANGE WITHOUT PRIOR NOTICE.
COPYRIGHT
THE INFORMATION CONTAINED HERE IS PROPRIETARY TECHNICAL INFORMATION OF
QUECTEL CO., LTD. TRANSMITTING, REPRODUCTION, DISSEMINATION AND EDITING OF THIS
DOCUMENT AS WELL AS UTILIZATION OF THE CONTENT ARE FORBIDDEN WITHOUT
PERMISSION. OFFENDERS WILL BE HELD LIABLE FOR PAYMENT OF DAMAGES. ALL RIGHTS
ARE RESERVED IN THE EVENT OF A PATENT GRANT OR REGISTRATION OF A UTILITY MODEL
OR DESIGN.
Copyright © Quectel Wireless Solutions Co., Ltd. 2017. All rights reserved.
History
Contents
1 Introduction ..................................................................................................................................... 14
5.7.7.3.3. Ql_SPI_Write..................................................................................................112
5.7.7.3.4. Ql_SPI_Read .................................................................................................112
5.7.7.3.5. Ql_SPI_WriteRead .........................................................................................113
5.7.7.3.6. Ql_SPI_Uninit .................................................................................................114
5.7.7.4. Example .........................................................................................................114
5.8. GPRS APIs ..........................................................................................................................115
5.8.1. Overview .....................................................................................................................115
5.8.2. Usage .........................................................................................................................115
5.8.3. API Functions .............................................................................................................116
5.8.3.1. Ql_GPRS_Register ........................................................................................116
5.8.3.2. Callback_GPRS_Actived ................................................................................116
5.8.3.3. CallBack_GPRS_Deactived ...........................................................................117
5.8.3.4. Ql_GPRS_Config ...........................................................................................118
5.8.3.5. Ql_GPRS_Activate .........................................................................................119
5.8.3.6. Ql_GPRS_ActivateEx .................................................................................... 120
5.8.3.7. Ql_GPRS_Deactivate .................................................................................... 121
5.8.3.8. Ql_GPRS_DeactivateEx ............................................................................... 122
5.8.3.9. Ql_GPRS_GetLocalIPAddress ...................................................................... 123
5.8.3.10. Ql_GPRS_GetDNSAddress .......................................................................... 124
5.8.3.11. Ql_GPRS_SetDNS Address .......................................................................... 124
5.9. Socket APIs ........................................................................................................................ 125
5.9.1. Overview .................................................................................................................... 125
5.9.2. Usage ........................................................................................................................ 125
5.9.2.1. TCP Client Socket Usage .............................................................................. 125
5.9.2.2. TCP Server Socket Usage ............................................................................ 126
5.9.2.3. UDP Service Socket Usage........................................................................... 126
5.9.3. API Functions ............................................................................................................ 127
5.9.3.1. Ql_SOC_Register.......................................................................................... 127
5.9.3.2. Callback_Socket_Read ................................................................................. 129
5.9.3.3. Callback_Socket_Write ................................................................................. 129
5.9.3.4. Ql_SOC_Create ............................................................................................ 130
5.9.3.5. Ql_SOC_Close .............................................................................................. 130
5.9.3.6. Ql_SOC_Connect.......................................................................................... 131
5.9.3.7. Ql_SOC_ConnectEx ..................................................................................... 132
5.9.3.8. Ql_SOC_Send............................................................................................... 133
5.9.3.9. Ql_SOC_Recv ............................................................................................... 134
5.9.3.10. Ql_SOC_GetAckNumber .............................................................................. 135
5.9.3.11. Ql_SOC_SendTo ........................................................................................... 135
5.9.3.12. Ql_SOC_RecvFrom ...................................................................................... 136
5.9.3.13. Ql_SOC_Bind ................................................................................................ 137
5.9.3.14. Ql_SOC_Listen ............................................................................................. 137
5.9.3.15. Ql_SOC_Accept ............................................................................................ 138
5.9.3.16. Ql_IpHelper_GetIPByHostName ................................................................... 138
5.9.3.17. Ql_IpHelper_ConvertIpAddr .......................................................................... 139
Table Index
Figure Index
1 Introduction
OpenCPU is an embedded development solution for M2M applications where GSM/GPRS/GNSS
modules can be designed as the main processor. It has been designed to facilitate the design and
accelerate the application development. OpenCPU makes it possible to create innovative applications
and embed them directly into Quectel GSM/GPRS/GNSS modules to run without external MCU. It has
been widely used in M2M field, such as tracker & tracing, automotive, energy, wearable devices, etc.
2 OpenCPU Platform
The following figure shows the fundamental principle of OpenCPU software architecture.
OpenCPU Core System is a combination of hardware and software of GSM/GPRS/GNSS module. It has
built-in ARM7EJ-S processor, and has been built over Nucleus operating system, which has the
characteristics of micro-kernel, real-time, multi-tasking, etc.
OpenCPU User APIs are designed for accessing to hardware resources, radio communications resources,
user file system, or external devices. All APIs are introduced in Chapter 5.
OpenCPU RIL is an open source layer, which enables developers to simply call API to send AT and get
the response when API returns. Additionally, developers can easily add a new API to implement an AT
command. For more details, please refer to document Quectel_OpenCPU_RIL_Application_Note.
In OpenCPU RIL, all URC messages of module have already been reinterpreted and the result is
informed to App by system message. App will receive the message MSG_ID_URC_INDICATION when
an URC arrives.
2.2.1. Processor
MC60-OpenCPU (OC: MC60CA-04-STD) module builds in 4MB flash and 4MB RAM.
User App Code Space: 320KB space available for image bin.
RAM Space: 100KB static memory and 500KB dynamic memory.
User File System Space: 120KB available.
MC60-OpenCPU (OC: MC60ECA-04-BLE) module builds in 4MB flash and 4MB RAM.
User App Code Space: 280KB space available for image bin.
RAM Space: 100KB static memory and 500KB dynamic memory.
User File System Space: Not supported.
2.3. Interfaces
OpenCPU provides 2 UART ports: MAIN UART and DEBUG UART. They are also named as UART1 and
UART2 respectively. Please refer to Chapter 5.7.1 for software API functions.
UART1 is a 9-pin serial interface with RTS/CTS HW handshake. UART2 is a 3-wire interface. UART2 has
debug function that can debug the Core System. Please refer to Chapter 5.12 for details.
2.3.2. GPIO
There are 21 I/O pins that can be configured for general purpose I/O. All pins can be accessed under
OpenCPU by API functions. Please refer to Chapter 5.7.2 for details.
2.3.3. EINT
OpenCPU supports external interrupt input. There are three I/O pins that can be configured for external
interrupt input. But the EINT cannot be used for the purpose of highly frequent interrupt detection, which
causes module‟s unstable working. The EINT pins can be accessed by APIs. Please refer to
Chapter 5.7.3 for details.
2.3.4. PWM
There is one I/O pin that can be configured for PWM. There are 32K and 13M clock sources that are
available. The PWM pin can be configured and controlled by APIs. Please refer to Chapter 5.7.4 for
details.
2.3.5. ADC
There is an analogue input pin that can be configured for ADC. The sampling period and count can be
configured by an API. Please refer to Chapter 5.7.5.
2.3.6. IIC
MC60-OpenCPU series module provides a hardware IIC interface. Please refer to Chapter 5.7.6 for
programming API functions.
2.3.7. SPI
MC60-OpenCPU series module provides a hardware SPI interface. The SPI interface is multiplexed with
PCM interface. And also both of them are multiplexed with GPIOs. Please refer to Chapter 5.7.7 for
programming API functions.
In OpenCPU, App can catch the behavior that power key is pressed down or released. Then developers
may redefine the behavior of pressing power key. Please refer to Chapters 4.3.1, 5.4.2.2 and 5.4.2.3.
2.4.1. SDK
Compile environment.
Development guide and other related documents.
A set of header files that defines all API functions and type declaration.
Source code for examples.
Open source code for RIL.
Download tool for application image bin file.
Pack tool for FOTA upgrade.
Customers may get the latest SDK package from sales channel.
2.4.2. Editor
Any text editor is available for editing codes, such as Source Insight, Visual Studio and even Notepad.
The Source Insight tool is recommended to be used to edit and manage codes. It is an advanced code
editor and browser with built-in analysis for C/C++ program, and provides syntax highlighting, code
navigation and customizable keyboard shortcuts.
2.4.3.1. Complier
OpenCPU uses GCC as the compiler, and the compiler edition is “Sourcery CodeBench”. The document
Quectel_OpenCPU_GCC_Installation_Guide tells the ways of establishing GCC environment.
2.4.3.2. Compiling
In OpenCPU, compiling commands are executed in command line. The compiling and clean commands
are defined as follows.
make clean
make new
In command-line, some compiler processing information will be outputted during compiling. All
WARNINGs and ERRORs are recorded in \SDK\build\gcc\build.log.
Therefore, if there exists any compiling error during compiling, please check the build.log for the error line
number and the error hints.
When compiling this example program, a compiling error will be thrown out. In build.log, it goes like this:
If there is no any compiling error during compiling, the prompt for successful compiling is given.
2.4.4. Download
The document Quectel_QFlash_User_Guide introduces the download tool and the way to use it to
download application bin.
By default, the custom directory has been designed to store the developers‟ source code files in SDK.
Item Description
The following codes are the least codes that comprise an OpenCPU Embedded Application.
/**
* The entrance of this application
*/
void proc_main_task(s32 taskId)
{
ST_MSG msg;
//Before using the RIL feature, developers must initialize it by calling the following APIs.
//After receiving the MSG_ID_RIL_READY message.
Ql_RIL_Initialize();
The proc_main_task function is the entrance of Embedded Application, just like the main() in C
application.
Ql_OS_GetMessage is an important system function that the Embedded Application receives messages
from message queue of the task.
2.4.5.3. Makefile
In OpenCPU, the compiler compiles program according to the definitions in makefile. The profile of
makefile has been pre-designed and is ready for use. However, developers need to change some settings
before compiling program according to native conditions, such as compiler environment path.
Suppose that the new file is in custom directory, and the newly added .c files will be compiled
automatically.
If developers need to add new directory in custom, please follow the steps below.
First, add the new directory name in variable “SRC_DIRS” in \SDK\make\gcc\gcc _makefile\gcc_makefile,
and define the source code files to compile.
Secondly, define the source code files to compile in the new directory.
In OpenCPU, the base data types are defined in the ql_type.h header file.
Type Description
Boolean variable (should be TRUE or FALSE).
bool This variable is declared as follows:
typedef unsigned char bool;
8-bit signed integer.
s8 This variable is declared as follows:
typedef signed char s8;
8-bit unsigned integer.
u8 This variable is declared as follows:
typedef unsigned char u8;
16-bit signed integer.
s16 This variable is declared as follows:
typedef signed short s16;
16-bit unsigned integer.
u16 This variable is declared as follows:
typedef unsigned short u16;
32-bit signed integer.
s32 This variable is declared as follows:
typedef int s32;
32-bit unsigned integer.
u32 This variable is declared as follows:
typedef unsigned int u32;
64-bit unsigned integer.
u64
This variable is declared as follows:
Floating-point variable.
float
This variable is declared in math.h.
4 System Configuration
In the \SDK\custom\config directory, developers can reconfigure the application according to their
requirements for heap memory size, tasks addition and task stack size configuration, as well as GPIO
initialization status. All config files for developers are named with prefix “custom_”.
Other system configurations, including power key, specified GPIO pin for
custom_sys_cfg.c
external watchdog, and setting working mode of debug port.
OpenCPU supports multitask processing. Developers only need to simply follow suit to add a record in
custom_task_cfg.h file to define a new task. OpenCPU supports one main task, and maximum TEN
subtasks.
If there are file operations in task, the stack size must be set to at least 5KB.
In OpenCPU, there are two ways to initialize GPIOs. One is to configure GPIO list initialization in
custom_gpio_cfg.h; the other way is to call GPIO related APIs (Please refer to Chapter 5.7.2) to initialize
after App starts. But the former one is earlier than the latter one on time sequence. The following figure
shows the time sequence relationship.
App starts.
Initialize GPIOs according
to the configurations in Initialize GPIOs by calling
custom_gpio_cfg.h. GPIO related APIs.
All customization items are configured in TLV (Type-Length-Value) in custom_sys_cfg.c. Developers may
change App‟s features by changing the value.
Default
Item Type (T) Length (L) Possible Value Description
Value
App SYS_CONFIG_APP_ APP_ APP_ENABLE App enabling
4
Enabling ENABLE_ID ENABLE APP_DISABLE configuration
Power on/off
PWRKEY
SYS_CONFIG_ TRUE working mode.
Pin 2 TRUE/FALSE
PWRKEY_DATA_ID TRUE Refer to
Config
Chapter 4.3.1
GPIO for
GPIO for SYS_CONFIG_
PINNAME_ One value of feeding WTD.
WTD WATCHDOG_DATA_ 8
GPIO0 Enum_PinName Refer to
Config ID
Chapter 4.3.2
Working Application
Mode SYS_CONFIG_ BASIC_ BASIC_MODE mode or debug
4
for Debug DEBUG_MODE_ID MODE ADVANCE_MODE mode for
Port debug port
When set to FALSE, the callback that Ql_PwrKey_Register registers will be trigged. Application
must call Ql_LockPower() to lock power supply, or module will lose power when the PWRKEY pin
is at high level.
*/
When set to FALSE, the callback that Ql_PwrKey_Register registers will be trigged.
Application may do post processing before switching off the module.
*/
};
When switching on/off the module by feeding a low level to PWRKEY pin, the callback in application will
be triggered. The example codes are shown below.
…
//Register a callback function for pressing PWRKEY pin.
Ql_PwrKey_Register((Callback_PowerKey_Ind)callback_pwrKey_ind);
…
//Callback definition
void Callback_PowerKey_Hdlr(s32 param1, s32 param2)
{
Ql_Debug_Trace("<-- Power Key: %s, %s -->\r\n",
param1==POWER_OFF ? "Power Off":"Power On",
param2==KEY_DOWN ? "Key Down":"Key Up"
);
if (POWER_ON==param1)
{
Ql_Debug_Trace("<-- App Lock Power Key! -->\r\n");
Ql_LockPower();
}
else if (POWER_OFF==param1)
{
//Post processing before power-down
//...
//Power down
Ql_PowerDown();
}
}
When an external watchdog is adopted to monitor the APP, the module has to feed the watchdog in the
whole period of the module‟s power-on, including the processess of startup, App activation and upgrade.
Therefore, developers just need to specify which GPIO is designed to feed the external watchdog.
The serial debug port (UART2) may work as a common serial port (BASIC_MODE), or a special debug
port (ADVANCE_MODE) that can debug some issues during application.
Usually developers do not need to use ADVANCE_MODE when there are no requirements from support
engineer. If needed, please refer to document Quectel_Catcher_Operation_UGD for the usage of the
special debug port.
5 API Functions
The header file ql_system.h declares system-related API functions. These functions are essential to any
customers‟ applications. Make sure the header file is included when using these functions.
OpenCPU provides interfaces that support multitasking, message, mutex, semaphore and event
mechanism functions. These interfaces are used for multitask programming. The example
example_multitask.c in OpenCPU SDK shows the proper usages of these API functions.
5.1.1. Usage
This section introduces some important operations and the API functions in system-level programming.
Developers can call Ql_OS_GetMessage to retrieve a message from the current task's message queue.
The message can be a system message, and also can be a customized message.
Developers can call Ql_OS_SendMessage to send messages to other tasks. To send message,
developers have to define a message ID. In OpenCPU, user message ID must greater than 0x1000.
5.1.1.3. Mutex
A mutex object is a synchronization object whose state is set to signaled when it is not owned by any task,
and non-signaled when it is owned. A task can only own one mutex object at a time. For example, to
prevent two tasks from being written to shared memory at the same time, each task waits for ownership of
a mutex object before the code that accesses the memory is executed. After writing to the shared memory,
the task releases the mutex object.
5.1.1.4. Semaphore
A semaphore object is a synchronization object that maintains a count between zero and a specified
maximum value. The count is decremented each time a task completes waiting for the semaphore object
and is incremented each time a task releases the semaphore. When the count reaches zero, no more
tasks can successfully wait for the semaphore object state to be signaled. The state of a semaphore is set
to signaled when its count is greater than zero and non-signaled when its count is zero.
5.1.1.5. Event
An event object is a synchronization object, which is useful in sending a signal to a thread indicating that a
particular event has occurred. A task uses Ql_OS_CreateEvent function to create an event object, whose
state can be explicitly set to signaled by use of the Ql_OS_SetEvent function.
OpenCPU has designed 13 blocks of system storage space to backup critical user data. Among the
storage blocks, blocks 1~8 can store 50 bytes data for each block, blocks 9~12 can store 100 bytes data
for each block, and block 13 can store 500 bytes data.
Developers may call Ql_SecureData_Store to backup data, and call Ql_Userdata_Read() to read back
data from backup space.
5.1.2.1. Ql_Reset
Prototype
Parameters
resetType:
[In] Reset type. It must be 0.
Return Value
None.
5.1.2.2. Ql_Sleep
This function suspends the execution of the current task until the timeout interval elapses. The sleep time
should not exceed 500ms since if the task is suspended for too long, it may receive too many messages
to be crushed.
Prototype
Parameters
msec:
[In] The time interval for the execution to be suspended. Unit: ms.
Return Value
None.
5.1.2.3. Ql_GetUID
This function gets the module UID. UID is a 20-byte serial number identification. The probability that
different modules have the same UID is 1ppm (1/10000000).
Prototype
Parameters
ptrUID:
[In] Pointer to the buffer that is used to store the UID. The buffer length needs to be at least 20 bytes.
len:
[In] The “ptrUID” buffer length. The value must be less than or equal to the size of the buffer that “ptrVer”
points to.
Return Value
If the ptrUID is null, this function will return QL_RET_ERR_INVALID_PARAMETER. If this function reads
the UID successfully, the length of UID will be returned.
5.1.2.4. Ql_GetCoreVer
This function gets the version ID of the core. The core version ID is a string with no more than 35
characters, and is end with „\0‟.
Prototype
Parameters
ptrVer:
[In] Pointer to the buffer that is used to store the version ID of the core. The buffer length needs to be at
least 35 bytes
len:
[In] The “ptrVer” buffer length. The value must be less than or equal to the size of the buffer that “ptrVer”
points to.
Return Value
The return value is the length of version ID of the core if this function succeeds. Otherwise, the return
value is an error code. To get extended error information, please refer to the ERROR CODES in header
file Ql_error.h.
5.1.2.5. Ql_GetSDKVer
This function gets the version ID of SDK. The SDK version ID is a string with no more than 20 characters,
and is end with „\0‟.
Prototype
Parameters
ptrVer:
[In] Pointer to the buffer that is used to store the version ID of SDK. The buffer length needs to be at least
20 bytes.
len:
[In] The “ptrVer” length. The value must be less than or equal to the size of the buffer that “ptrVer” points
to.
Return Value
The return value is the length of version ID if this function succeeds. Otherwise, the return value is an
error code. To get extended error information, please refer to the ERROR CODES in header file
Ql_error.h.
5.1.2.6. Ql_GetMsSincePwrOn
This function returns the number of milliseconds since the device has been booted.
Prototype
Parameters
Void.
Return Value
Number of milliseconds.
5.1.2.7. Ql_OS_GetMessage
This function gets a message from the current task's message queue. When there is no message in task‟s
message queue, the task is in waiting state.
Prototype
typedef struct {
u32 message;
u32 param1;
u32 param2;
u32 srcTaskId;
} ST_MSG;
Parameters
msg:
[In] Pointer to the “ST_MSG” struct.
Return Value
5.1.2.8. Ql_OS_SendMessage
This function sends messages between tasks. The destination task receives messages with
Ql_OS_GetMessage.
Prototype
s32 Ql_OS_SendMessage (s32 destTaskId, u32 msgId, u32 param1, u32 param2)
Parameters
desttaskid:
[In] The maximum value is 10. The destination task is main task if the value is 0. The destination task is
subtask if the value is between 1 and 10.
param1:
[In] User data.
param2:
[In] User data.
Return Value
5.1.2.9. Ql_OS_CreateMutex
This function creates a mutex. A handle of created mutex will be returned if creation succeeds. 0 means
failure. If the same mutex has already been created, this function may return a valid handle also. But the
Ql_GetLastError function returns ERROR_ALREADY_EXISTS.
Prototype
Parameters
mutexName:
[In] Name of the mutex to be created.
Return Value
5.1.2.10. Ql_OS_TakeMutex
This function obtains an instance of the specified mutex. If the mutex ID is invalid, the system may be
crushed.
Prototype
Parameters
mutexid:
[In] Destination mutex to be taken.
Return Value
None.
5.1.2.11. Ql_OS_GiveMutex
Prototype
Parameters
mutexid:
[In] Destination mutex to be given.
Return Value
None.
5.1.2.12. Ql_OS_CreateSemaphore
This function creates a counting semaphore. A handle of created semaphore will be returned, if creation
succeeds. 0 means failure. If the same semaphore has already been created, this function may return a
valid handle also. But the Ql_GetLastError function returns ERROR_ALREADY_EXISTS.
Prototype
Parameters
semname:
[In] Name of the semaphore to be created.
maxCount:
[In] The maximum count of the semaphore.
Return Value
5.1.2.13. Ql_OS_TakeSemaphore
This function obtains an instance of the specified semaphore. If the mutex ID is invalid, the system may
be crushed.
Prototype
Parameters
semId:
[In] The destination semaphore to be taken.
wait:
[In] The waiting style determining if a task waits infinitely (TRUE) or returns immediately (FALSE).
Return Value
5.1.2.14. Ql_OS_CreateEvent
This function waits until the specified type of event is in the signaled state. Developers can specify
different types of events for purposes. The event flags are defined in Enum_EventFlag.
Prototype
Parameters
evtName:
[In] Event name.
Return Value
5.1.2.15. Ql_OS_WaitEvent
This function waits until the specified type of event is in signaled state. Developers can specify different
types of events for purposes. The event flags are defined in Enum_EventFlag.
Prototype
Parameters
evtId:
Event ID that is returned by calling Ql_OS_CreateEvent().
evtFlag:
Event flag type. See Enum_EventFlag.
Return Value
Zero indicates the function is executed successfully and nonzero indicates failed to execute the function.
5.1.2.16. Ql_OS_SetEvent
This function sets the specified event flag. Any task waiting on the event whose event flag request is
satisfied is resumed.
Prototype
Parameters
evtId:
Event ID that is returned by calling Ql_OS_CreateEvent().
evtFlag:
Event flag type. See Enum_EventFlag.
Return Value
Zero indicates the function is executed successfully and nonzero indicates failed to execute the function.
5.1.2.17. Ql_OS_GiveSemaphore
Parameters
semid:
[In] The destination semaphore to be given.
Return Value
None.
5.1.2.18. Ql_SetLastErrorCode
Prototype
Parameters
errCode:
[In] Error code.
Return Value
5.1.2.19. Ql_GetLastErrorCode
This function retrieves the calling task's last error code value.
Prototype
s32 Ql_GetLastErrorCode(void)
Parameters
Void.
Return Value
The return value is the calling task's last error code value.
5.1.2.20. Ql_OS_GetCurrenTaskLeftStackSize
This function gets the number of bytes left in the current task stack.
Prototype
u32 Ql_OS_GetCurrenTaskLeftStackSize(void)
Parameters
Void.
Return Value
The return value is the number of bytes if this function succeeds. Otherwise an error code is returned.
The frequent error codes, which could be returned by APIs in multitask programming, are enumerated in
the Enum_OS_ErrCode.
/**************************************************************
* Error Code Definition
**************************************************************/
typedef enum {
OS_SUCCESS,
OS_ERROR,
OS_Q_FULL,
OS_Q_EMPTY,
OS_SEM_NOT_AVAILABLE,
OS_WOULD_BLOCK,
OS_MESSAGE_TOO_BIG,
OS_INVALID_ID,
OS_NOT_INITIALIZED,
OS_INVALID_LENGHT,
OS_NULL_ADDRESS,
OS_NOT_RECEIVE,
OS_NOT_SEND,
OS_MEMORY_NOT_VALID,
OS_NOT_PRESENT,
OS_MEMORY_NOT_RELEASE
} Enum_OS_ErrCode;
5.1.4. Examples
1. Mutex Example:
void MutextTest(int iTaskId) //Two tasks run this function at the same time.
{
2. Semaphore Example:
Ql_Sleep(3000);
OpenCPU provides time-related APIs including setting local time, getting local time, converting the
calendar time into seconds or converting seconds into the calendar time, etc.
5.2.1. Usage
Calendar time is measured from a standard point in time to the current time elapsed seconds, generally
set at 00:00:00 on January 1st, 1970 as a standard point in time.
typedef struct {
s32 year; //Range: 2000~2127
s32 month;
s32 day;
s32 hour; //In 24-hour time system
s32 minute;
s32 second;
s32 timezone; //Range: -12~12
}ST_Time;
The field “timezone” defines the time zone. A negative number indicates the Western Time zone, and a
positive number indicates the Eastern Time zone. For example: the time zone of Beijing is East Area 8,
then timezone=8; the time zone of Washington is West Zone 5, the timezone=-5.
5.2.2.1. Ql_SetLocalTime
Prototype
Parameters
datetime:
[In] Pointer to the “ST_Time” struct.
Return Value
5.2.2.2. Ql_GetLocalTime
Prototype
Parameters
dateTime:
[Out] Pointer to the “ST_Time” struct.
Return Value
If the function is executed successfully, the current local date and time are returned. NULL means failure.
5.2.2.3. Ql_Mktime
This function gets the total seconds elapsed since 00:00:00 on January 1st, 1970.
Prototype
Parameters
dateTime:
[In] Pointer to the “ST_Time” struct.
Return Value
5.2.2.4. Ql_MKTime2CalendarTime
This function converts the seconds elapsed since 00:00:00 on January 1st, 1970 to the local date and
time.
Prototype
Parameters
seconds:
[In] The seconds elapsed since 00:00:00 on January 1st, 1970.
pOutDateTime:
[Out] Pointer to the “ST_Time” struct.
Return Value
If the function is executed successfully, the current local date and time are returned. NULL means failure.
5.2.3. Example
s32 ret;
u64 sec;
ST_Time datetime, *tm;
datetime.year=2013;
datetime.month=6;
datetime.day=12;
datetime.hour=18;
datetime.minute=12;
datetime.second=13;
datetime.timezone=-8;
//Convert the seconds elapsed since 00:00:00 on January 1st, 1970 to local date and time.
tm=Ql_MKTime2CalendarTime(sec, & datetime);
Ql_Debug_Trace("<--%d/%d/%d %d:%d:%d %d -->\r\n",tm->year, tm->month, tm->day, tm->hour, tm
->minute, tm->second, tm->timezone);
OpenCPU provides two kinds of timers. One is “Common Timer”, and the other is “Fast Timer”. OpenCPU
system allows maximum 10 Common Timers running at the same time in a task. The system provides
only one Fast Timer for application. The accuracy of the Fast Timer is relatively higher than a common
timer.
5.3.1. Usage
Developer uses Ql_Timer_Register() to create a common timer, and register the interrupt handler. And a
timer ID, which is an unsigned integer, must be specified. Ql_Timer_Start() can start the created timer,
and Ql_Timer_Stop() can stop the running timer.
Developers may call Ql_Timer_RegisterFast() to create the Fast Timer, and register the interrupt handler.
Ql_Timer_Start() can start the created timer, and Ql_Timer_Stop() can stop the running timer. The
minimum interval for Fast Timer should be an integral multiple of 10ms.
5.3.2.1. Ql_Timer_Register
This function registers a Common Timer. Each task supports 10 timers running at the same time. Only the
task which registers the timer can start and stop the timer.
Prototype
Parameters
timerId:
[In] Timer ID. It must be ensured that the ID is the only one under OpenCPU task. Of course, the ID that
registered by Ql_Timer_RegisterFast also cannot be the same with it.
callback_onTimer:
[Out] Notify developers when the timer arrives.
param:
[In] One customized parameter that can be passed into the callback functions.
Return Value
5.3.2.2. Ql_Timer_RegisterFast
This function registers a Fast Timer. It only supports one timer for App. Please do not add any task
schedule in the interrupt handler of the Fast Timer.
Prototype
Parameters
timerId:
[In] Timer ID. It must be ensured that the ID is not the same as the one that registered by
Ql_Timer_Register.
callback_onTimer:
[Out] Notify developers when the timer arrives.
param:
[In] One customized parameter that can be passed into the callback functions.
Return Value
5.3.2.3. Ql_Timer_Start
This function starts up the specified timer. When start or stop a specified timer in a task, the task must be
the same as the one that registers the timer.
Prototype
Parameters
timerId:
[In] Timer ID, which must be registered.
interval:
[In] Set the interval of the timer. Unit: ms. If developers start a Common Timer, the interval must be
greater than or equal to 1ms. If developers start a Fast Timer, the interval must be an integer multiple
of 10ms.
autoRepeat:
[In] TRUE or FALSE, which indicates the timer is executed once or repeatedly.
Return Value
5.3.2.4. Ql_Timer_Stop
This function stops the specified timer. When start or stop a specified timer in a task, the task must be the
same as the one that registers the timer.
Prototype
Parameters
timerId:
[In] Timer ID. The timer has been started by calling Ql_Timer_Start previously.
Return Value
5.3.3. Example
The following codes show how to register and start a Common Timer.
s32 ret;
u32 timerId=999; //Timer ID is 999
u32 interval=2 * 1000; //2 seconds
bool autoRepeat=TRUE;
u32 param=555;
//Callback function.
void Callback_Timer(u32 timerId, void* param)
{
ret=Ql_Timer_Stop(timerId);
Ql_Debug_Trace("\r\n<--Stop: timerId=%d,ret = %d -->\r\n", timerId ,ret);
}
Power management contains the power-related operations, such as power-down, power key control and
low power consumption enabling/disabling.
5.4.1. Usage
Developers may call Ql_PowerDown function to power off the module when PWRKEY pin has not been
short-circuited to ground. And this action will reset the module when PWRKEY pin has been
short-circuited to ground.
The Ql_ SleepEnable function can enable the sleep mode of module. The module enters into sleep mode
when it is idle.
The timeout of timer, coming call, coming SMS, GPRS data and an interrupt event can wake up the
module from sleep mode. The Ql_SleepDisable function can disable the sleep mode when module is
woken up.
5.4.2.1. Ql_PowerDown
This function powers off the module. When call this API to power down the module, the module will
complete the network anti-registration first. So powering off the module will need more time.
Prototype
Parameters
pwrDwnType:
[In] Power-off type of this function. 1 means normal power-off.
Return Value
None.
5.4.2.2. Ql_LockPower
When getting the control right of power key, application must call Ql_LockPower to lock power supply, or
the module will lose power when the level of PWRKEY pin goes high. Please refer to Chapter 4.3.1.
Prototype
void Ql_LockPower(void);
Parameters
Void.
Return Value
None.
5.4.2.3. Ql_PwrKey_Register
This function registers the callback for PWRKEY indication. The callback function will be triggered when
the power key is pressed down or released (including power-on and power-off). The configuration for
power key in sys_config.c should be set to FALSE, or else, the callback will not be triggered. Please refer
to Chapter 4.3.1.
Prototype
Parameters
Callback_pwrKey:
[In] Callback function for PWRKEY indication.
param1:
[Out] One value of Enum_PowerKeyOpType.
param2:
[Out] One value of Enum_KeyState.
Return Value
The return value is QL_RET_OK if this function is executed succeeds. Otherwise, the return value is an
error code. To get extended error information, please refer to the ERROR CODES in header file
Ql_error.h.
5.4.2.4. Ql_SleepEnable
This function enables the sleep mode of module. The module will enter into sleep mode when it is under
idle state.
Prototype
Parameters
Void.
Return Value
5.4.2.5. Ql_SleepDisable
Prototype
s32 Ql_SleepDisable(void)
Parameters
Void.
Return Value
5.4.3. Example
The following sample codes show how to enter into and quit from sleep mode in the interrupt handler.
}else{
SYS_DEBUG( DBG_Buffer,"DTR set to high=%d Sleep \r\n", level);
Ql_SleepEnable(); //Quit from sleep mode.
}
}
The dynamic memory is system heap space. And the maximum available system heap of application is
500KB.
Ql_MEM_Alloc and QL_MEM_Free must be present in pairs. Otherwise, memory leakage arises.
5.5.1. Usage
Step 1: Call Ql_MEM_Alloc() to apply for a block of memory with the specified size. The memory allocate
by Ql_MEM_Alloc() is from system heap.
Step 2: If the memory block is not needed anymore, please call Ql_MEM_Free() to free the memory
block that is previously allocated by calling Ql_MEM_Alloc().
5.5.2.1. Ql_MEM_Alloc
This function allocates memory with the specified size in memory heap.
Prototype
Parameters
Size:
[In] Number of memory bytes to be allocated.
Return Value
A pointer of void type to the address of allocated memory. NULL will be returned if the allocation fails.
5.5.2.2. Ql_MEM_Free
Prototype
Parameters
Ptr:
[In] Previously allocated memory block to be free.
Return Value
None.
5.5.3. Example
The following codes show how to allocate and free a specified size memory.
char *pch=NULL;
OpenCPU supports user file system, and provides a set of complete API functions to create, access and
delete files and directories. This section describes these APIs and their usages.
The storage can be flash (UFS) and RAM (RAM file). The RAM file does not support directory structure.
5.6.1. Usage
The type of storage is divided into two kinds. One is the UFS in the flash, and the other is RAM file system.
The RAM file does not support directory structure. Developers can select the storage location according
to their own needs. When they want to create/open a file or directory, they must use a relative path. For
example, if they want to create a file in the root of the UFS, they can set the file as filename.ext for
instance.
The Ql_FS_GetTotalSpace function is used to obtain the amount of total space on flash or SD card.
The Ql_FS_GetFreeSpace function is used to obtain the amount of free space on flash or SD card.
The Ql_FS_GetSize function is used to get the size of the specified file, and the size is in bytes.
The Ql_FS_Open function is used to create or open a file. Developers must define the file's opening
and access modes. If developers want to know the usage of this function, please refer to the detailed
descriptions of it.
The Ql_FS_Read and Ql_FS_Write functions are used to read and write a file. Developers must
ensure that the file has been opened.
The Ql_FS_Seek and Ql_FS_GetFilePosition functions are used to set and get the position of the file
pointer. Developers must ensure that the file has been opened.
The Ql_FS_Truncate function is used to truncate the specified file to zero length.
The Ql_FS_Delete and Ql_FS_Check functions are used to delete and check a file.
The Ql_FS_CreateDir, Ql_FS_DeleteDir and Ql_FS_CheckDir functions are used to create, delete
and check a specified directory.
The Ql_FS_FindFirst, Ql_FS_FindNext and Ql_FS_XDelete functions are used to traverse all files
and directories in the specified directory. The three functions are usually used together.
The Ql_FS_XDelete function is multi-functional. It can be used to delete a specified file or an empty
directory. Developers can also delete all files and directories in the specified directory by recursive
way.
The Ql_FS_XMove function is used to move or copy a file or folder.
The Ql_FS_Format function is used to format the UFS.
NOTES
5.6.2.1. Ql_FS_Open
Prototype
Parameters
lpFileName:
[In] The File name. The name is limited to 252 characters. Developers must use a relative path, such as
filename.ext or dirname\filename.ext.
flag:
[In] An u32 data type that defines the file's opening and access modes. The possible values are shown as
follows:
Return Value
The return value specifies a file handle if this function succeeds. Otherwise an error code is returned.
5.6.2.2. Ql_FS_OpenRAMFile
This function opens or creates a file with a specified name in the RAM. Developers need to add prefix
“RAM:” to the file name. Developers can create 15 files at most.
Prototype
Parameters
lpFileName:
[In] The file name. The name is limited to 252 characters. Developers must use a relative path such as
RAM: filename.ext.
flag:
[In] An u32 data type that defines the file‟s opening and access modes. The possible values are shown as
follows:
ramFileSize:
[In] The size of the specified file that developers want to create.
Return Value
The return value specifies a file handle if this function succeeds. Otherwise an error code is returned.
5.6.2.3. Ql_FS_Read
This function reads the data that from the specified file from the position indicated by the file pointer. After
the reading operation has been completed, the file pointer is adjusted by the number of bytes actually
read.
Prototype
Parameters
fileHandle:
[In] The file handle to be read, which is a return value of Ql_FS_Open function.
readBuffer:
[Out] Pointer to the buffer that is used to receive the data read from the file.
numberOfBytesToRead:
[In] Number of bytes to read.
numberOfBytesRead:
[Out] Number of bytes that has been read. Set this value to zero before taking read action or checking
errors.
Return Value
5.6.2.4. Ql_FS_Write
This function writes data from a buffer to the specifed file, and returns the actual number of written bytes.
Prototype
Parameters
fileHandle:
[In] The file handle to be written, which is a return value of Ql_FS_Open function.
writeBuffer:
[In] Pointer to the buffer that that is used to contain the data to be written to the file.
numberOfBytesToWrite:
[In] Number of bytes to be written to the file.
numberOfBytesToWritten:
[Out] Pointer to the number of bytes to be written by the function calling.
Return Value
5.6.2.5. Ql_FS_Seek
Prototype
Parameters
fileHandle:
[In] The file handle, which is the return value of Ql_FS_Open function.
offset:
[In] Number of bytes to move the file pointer.
whence:
[In] Pointer movement mode. It must be one of the following values.
typedef enum
{
QL_FS_FILE_BEGIN,
QL_FS_FILE_CURRENT,
QL_FS_FILE_END
} Enum_FsSeekPos;
Return Value
5.6.2.6. Ql_FS_GetFilePosition
Prototype
Parameters
fileHandle:
[In] The file handle, which is the return value of Ql_FS_Open function.
Return Value
The return value is the current offset from the beginning of the file if this function succeeds. Otherwise, the
return value is an error code.
5.6.2.7. Ql_FS_Truncate
Prototype
Parameters
fileHandle:
[In] The file handle, which is the return value of Ql_FS_Open function.
Return Value
5.6.2.8. Ql_FS_Flush
This function forces the data remaining in the file buffer to be written to the file.
Prototype
Parameters
fileHandle:
[In] The file handle, which is the return value of Ql_FS_Open function.
Return Value
None.
5.6.2.9. Ql_FS_Close
This function closes the file associated with the file handle and makes the file unavailable for reading or
writing.
Prototype
Parameters
fileHandle:
[In] The file handle, which is the return value of Ql_FS_Open function.
Return Value
None.
5.6.2.10. Ql_FS_GetSize
This function retrieves the size of the specified file and the size is in bytes.
Prototype
Parameters
lpFileName:
[In] The file name. The name is limited to 252 characters. Developers must use a relative path, such as
filename.ext or dirname\filename.ext.
Return Value
The return value is the bytes of the file if this function succeeds. Otherwise, the return value is an error
code.
5.6.2.11. Ql_FS_Delete
Prototype
Parameters
lpFileName:
[In] The file name. The name is limited to 252 characters. Developers must use a relative path, such as
filename.ext or dirname\filename.ext.
Return Value
5.6.2.12. Ql_FS_Check
Prototype
Parameters
lpFileName:
[In] The file name. The name is limited to 252 characters. Developers must use a relative path, such as
filename.ext or dirname\filename.ext.
Return Value
5.6.2.13. Ql_FS_Rename
Prototype
Parameters
lpFileName:
[In] The current name of the file. The name is limited to 252 characters. Developers must use a relative
path, such as filename.ext or dirname\filename.ext.
newLpFileName:
[In] The new name of the file. The new name is different from the existing names and is limited to 252
characters. Developers must use a relative path, such as filename.ext or dirname\filename.ext.
Return Value
5.6.2.14. Ql_FS_CreateDir
Prototype
Parameters
lpDirName:
[In] The name of the directory. The name is limited to 252 characters. Developers must use a relative path,
such as dirname1 or dirname1\dirname2.
Return Value
5.6.2.15. Ql_FS_DeleteDir
Prototype
Parameters
lpDirName:
[In] The name of the directory. The name is limited to 252 characters. Developers must use a relative path,
such as dirname1 or dirname1\dirname2.
Return Value
5.6.2.16. Ql_FS_CheckDir
Prototype
Parameters
lpDirName:
[In] The name of the directory. The name is limited to 252 characters. Developers must use a relative path,
such as dirname1 or dirname1\dirname2.
Return Value
5.6.2.17. Ql_FS_FindFirst
This function searchs for a directory for a file or subdirectory whose name matches the specified file
name.
Prototype
s32 Ql_FS_FindFirst(char *lpPath, char *lpFileName, u32 fileNameLength, u32 *fileSize, bool *isDir)
Parameters
lpPath:
[In] Pointer to a null-terminated string that specifies a valid directory or path.
lpFileName:
[In] Pointer to a null-terminated string that specifies a valid file name, which can contain wildcard
characters, such as „*‟ and „?‟.
fileNameLength:
[In] The maximum name length of a file to be received.
fileSize:
[Out] Pointer to the variable that represents the size specified by the file.
isDir:
[Out] Pointer to the variable that represents the type specified by the file.
Return Value
The return value is a search handle that can be used in a subsequent calling to the Ql_FindNextFile or
Ql_FindClose function if this function succeeds.
5.6.2.18. Ql_FS_FindNext
This function finds the next file continuously according to the handle which is a return value of
Ql_FS_FindFirst function.
Prototype
s32 Ql_FS_FindNext(s32 handle, char *lpFileName, u32 fileNameLength, u32 *fileSize, bool *isDir)
Parameters
handle:
[In] The handle is a return value of Ql_FS_FindFirst function.
lpFileName:
[In] Pointer to a null-terminated string that specifies a valid file name, which can contain wildcard
characters, such as „*‟ and „?‟.
fileNameLength:
[In] The maximum name length of the file to be received.
fileSize:
[Out] Pointer to the variable that represents the size specified by the file.
isDir:
[Out] Pointer to the variable whose type is specified by the file.
Return Value
5.6.2.19. Ql_FS_FindClose
Prototype
Parameters
handle:
[In] Find handle. It is returned by a previous calling of Ql_FS_FindFirst function.
Return Value
None.
5.6.2.20. Ql_FS_XDelete
Prototype
Parameters
lpPath:
[In] The file path to be deleted.
flag:
[In] An u32 data type that defines the file's opening and access modes. The possible values are shown as
follows.
Return Value
5.6.2.21. Ql_FS_XMove
Prototype
Parameters
lpSrcPath:
[In] Source path to be moved or copied.
lpDestPath:
[In] Destination path.
flag:
[In] An u32 data type that defines the file's opening and access modes.The possible values are shown as
follows:
Return Value
Prototype
Parameters
storage:
[In] The type of storage. One value of Enum_FSStorage.
typedef enum
{
Ql_FS_UFS = 1,
Ql_FS_SD = 2,
Ql_FS_RAM = 3,
}Enum_FSStorage;
Return Value
The return value is the total number of bytes of the free space in the specified storage if this function
succeeds. Otherwise, the return value is an error code.
5.6.2.23. Ql_FS_GetTotalSpace
Prototype
Parameters
storage:
[In] The type of storage. One value of Enum_FSStorage.
Return Value
The return value is the total number of bytes in the specified storage if this function succeeds. Otherwise,
the return value is an error code.
5.6.2.24. Ql_FS_Format
Prototype
Parameters
storage:
[In] The format storage. One value of Enum_FSStorage.
Return Value
5.6.3. Example
#define MEMORY_TYPE 1
#define FILE_NAME "test.txt"
#define NEW_FILE_NAME "file.txt"
#define DIR_NAME "DIR\\"
#define LPPATH "\\*"
#define LPPATH2 "\\DIR\\*"
#define XDELETE_PATH "\\"
#define WRITE_DATA "1234567890"
#define OFFSET 0
void API_TEST_File(void)
{
s32 ret;
s64 size;
s32 filehandle, findfile;
u32 writeedlen, readedlen ;
u8 strBuf[100];
s32 position;
s32 filesize;
bool isdir;
ret=Ql_FS_Check(FILE_NAME);
Ql_Debug_Trace("Ql_FS_Check(%s)=%d\r\n", FILE_NAME, ret);
//Create a directory.
ret=Ql_FS_CreateDir(DIR_NAME);
Ql_Debug_Trace("Ql_FS_CreateDir(%s)=%d\r\n", DIR_NAME, ret);
//Create a directory.
ret=Ql_FS_CreateDir(DIR_NAME);
Ql_Debug_Trace("Ql_FS_CreateDir(%s)=%d\r\n", DIR_NAME, ret);
//List all files and directories under the root of the UFS.
Ql_memset(strBuf,0,100);
findfile=Ql_FS_FindFirst(LPPATH, strBuf, 100, &filesize, &isdir);
Ql_Debug_Trace("\r\nLater:strBuf=[%s]",strBuf);
if(findfile < 0)
{
Ql_Debug_Trace("Failed Ql_FS_FindFirst(%s)=%d\r\n", LPPATH, findfile);
}else{
Ql_Debug_Trace("Sueecss Ql_FS_FindFirst(%s)\r\n", LPPATH);
}
ret=findfile;
while(ret >=0)
{
Ql_Debug_Trace("filesize(%d),isdir(%d),Name(%s)\r\n", filesize, isdir, strBuf);
ret=Ql_FS_FindNext(findfile, strBuf, 100, &filesize, &isdir);
if(ret !=QL_RET_OK)
break;
}
Ql_FS_FindClose(findfile);
ret=findfile;
while(ret>=0)
{
Ql_Debug_Trace("filesize(%d),isdir(%d),Name(%s)\r\n", filesize, isdir, strBuf);
ret=Ql_FS_FindNext(findfile, strBuf, 100, &filesize, &isdir);
if(ret !=QL_RET_OK)
break;
}
Ql_FS_FindClose(findfile);
//Delete all files and directories under the root of the UFS by recursive way.
ret=Ql_FS_XDelete(XDELETE_PATH,QL_FS_FILE_TYPE
|QL_FS_DIR_TYPE|QL_FS_RECURSIVE_TYPE);
Ql_Debug_Trace("\r\nQl_FS_XDelete(%s,%x)=%d\r\n",XDELETE_PATH,
QL_FS_RECURSIVE_TYPE, ret);
Ql_memset(strBuf,0,100);
Findfile=Ql_FS_FindFirst(LPPATH, strBuf, 100, &filesize, &isdir);
Ql_Debug_Trace("Later:strBuf=[%s]",strBuf);
if(findfile < 0)
{
Ql_Debug_Trace("Failed Ql_FS_FindFirst(%s)=%d\r\n", LPPATH, findfile);
}else{
Ql_Debug_Trace("Sueecss Ql_FS_FindFirst(%s)\r\n", LPPATH);
}
ret=findfile;
while(ret>=0)
{
Ql_Debug_Trace("filesize(%d),isdir(%d),Name(%s)\r\n", filesize, isdir, strBuf);
ret=Ql_FS_FindNext(findfile, strBuf, 100, &filesize, &isdir);
if(ret !=QL_RET_OK)
break;
}
Ql_FS_FindClose(findfile);
}
5.7.1. UART
In OpenCPU, UART ports include physical UART ports and virtual UART ports. The physical UART ports
can be connected to external devices, and the virtual UART ports are used to communicate between
application and the bottom operating system.
One of the physical UART ports has hardware handshaking function, and others have three-wire
interfaces.
OpenCPU provides two virtual UART ports that are used for communication between App and Core.
These virtual ports are designed according to the features of physical serial interface. They have their RI
and DCD information. The level of DCD can be used to indicate the virtual port is in data mode or AT
command mode.
OpenCPU Virtual
Send AT Command
Virtual OpenCPU
Core UART UART
System Receive AT Response Application
/URC
Open/Write/Read/Close
Physical UART
UART Program
Signal Notification
TX RX
RX TX
Physical UART
External Device
For physical UART or virtual UART initialization and usage, developers can accomplish by following
simple steps.
5.7.1.3.1. Ql_UART_Register
This function registers the callback function for the the specified serial port. UART callback function is
used to receive the UART notification from core system.
Prototype
Parameters
port:
[In] Port name.
callback_uart:
[In] Pointer of the UART callback function.
event:
[Out] Indication of the event type of UART callback. One value of Enum_UARTEventType.
pinLevel:
[Out] If the event type is EVENT_UART_RI_IND, EVENT_UART_DCD_IND or EVENT_UART_DTR_IND,
the pinLevel indicates the related pin's current level. Otherwise this parameter has no meaning, and
just ignore it.
customizePara:
[In] Customized parameter. If not used, just it set to NULL.
Return Value
The return value is QL_RET_OK if this function succeeds. Otherwise, the return value is an error code. To
get extended error information, please refer to the ERROR CODES in header file Ql_error.h.
5.7.1.3.2. Ql_UART_Open
This function opens a specified UART port with the specified flow control mode. The task that calls this
function will own the specified UART port.
Prototype
typedef enum {
FC_NONE=1, //None Flow Control
FC_HW, //Hardware Flow Control
FC_SW //Software Flow Control
} Enum_FlowCtrl;
Parameters
port:
[In] Port name.
baudrate:
[In] The baud rate of the UART to be opened.
The physical UART supports baud rates as follows: 75bps, 150bps, 300bps, 600bps, 1200bps, 2400bps,
4800bps, 7200bps, 9600bps, 14400bps, 19200bps, 28800bps, 38400bps, 57600bps, 115200bps,
230400bps and 460800bps. The parameter does not take effect for VIRTUAL_PORT1 and
VIRTUAL_PORT2, so just it set to 0.
flowCtrl:
[In] See Enum_flowCtrl for the physical UART ports. Only UART_PORT1 supports hardware flow control
(FC_HW).
Return Value
The return value is QL_RET_OK if this function succeeds. Otherwise, the return value is an error code. To
get extended error information, please refer to the ERROR CODES in header file Ql_error.h.
5.7.1.3.3. Ql_UART_OpenEx
This function opens a specified UART port with the specified DCB parameters. The task that calls this
function will own the specified UART port.
Prototype
Parameters
port:
[In] Port name.
dcb:
[In] Pointer to the UART DCB settings, including baud rate, data bits, stop bits, parity, and flow control.
Only physical serial port1 (UART_PORT1) supports hardware flow control. This parameter does not
take effect for VIRTUAL_PORT1 and VIRTUAL_PORT2, so just set it to NULL.
Return Value
The return value is QL_RET_OK if this function succeeds. Otherwise, the return value is an error code. To
get extended error information, please refer to the ERROR CODES in header file Ql_error.h.
5.7.1.3.4. Ql_UART_Write
This function is used to send data to the specified UART port. When the number of bytes actually sent is
less than that to be sent, application should stop sending data. And application (in callback function) will
receive an event EVENT_UART_READY_TO_WRITE later. After receiving this event, application can
continue to send data, and the previously unsent data should be resent.
Prototype
Parameters
port:
[In] Port name.
data:
[In] Pointer to data to write.
writeLen:
[In] The length of the data to write. For VIRTUAL_UART1 and VIRTUAL_UART2, the maximum length
that can be written at one time is 1023 bytes which cannot be modified programmatically in
application.
Return Value
Actual number of bytes written. If this function fails to write data, a negative number will be returned. To
get extended error information, please refer to the ERROR CODES in header file Ql_error.h.
5.7.1.3.5. Ql_UART_Read
This function reads data from the specified UART port. When the UART callback is invoked, and the
notification is EVENT_UART_READY_TO_READ, developers should read out all data in the UART RX
buffer by calling this function in loop; otherwise, there will not be such notification to be reported to
Prototype
Parameters
port:
[In] Port name
data:
[In] Pointer to the buffer for the read data.
readLen:
[In] The length of the data to be read. The maximum data length of the receive buffer for physical UART
buffer is 3584 bytes, and 1023 bytes for virtual UART. The buffer size cannot be modified
programmatically in application.
Return Value
Actual number of read bytes. If readLen equals to the actual read length, developers need to continue
reading the UART until the actual read length is less than the readLen. To get extended error information,
please refer to the ERROR CODES in header file Ql_error.h.
5.7.1.3.6. Ql_UART_SetDCBConfig
This function sets the parameters of the specified UART port and works only for physical UART ports.
Prototype
typedef enum {
DB_5BIT = 5,
DB_6BIT,
DB_7BIT,
DB_8BIT
} Enum_DataBits;
typedef enum {
SB_ONE=1,
SB_TWO,
SB_ONE_DOT_FIVE
} Enum_StopBits;
typedef enum {
PB_NONE=0,
PB_ODD,
PB_EVEN,
PB_SPACE,
PB_MARK
} Enum_ParityBits;
typedef enum {
FC_NONE=1, //None Flow Control
FC_HW, //Hardware Flow Control
FC_SW //Software Flow Control
} Enum_FlowCtrl;
typedef struct {
u32 baudrate;
Enum_DataBits dataBits;
Enum_StopBits stopBits;
Enum_ParityBits parity;
Enum_FlowCtrl flowCtrl;
}ST_UARTDCB;
Parameter
port:
[In] Port name.
dcb:
[In] Pointer to the UART DCB struct, which includes baud rate, databits, stopbits and parity.
Return Value
The return value is QL_RET_OK if this function succeeds. Otherwise, the return value is an error code. To
get extended error information, please refer to the ERROR CODES in header file Ql_error.h.
5.7.1.3.7. Ql_UART_GetDCBConfig
This function gets the configuration parameters of the specified UART port and works only for physical
UART ports.
Prototype
Parameters
port:
[In] Port name.
dcb:
[In] The specified UART port‟s current DCB configration parameters, which includes baud rate, databits,
stopbits and parity.
Return Value
The return value is QL_RET_OK if this function succeeds. Otherwise, the return value is an error code. To
get extended error information, please refer to the ERROR CODES in header file Ql_error.h.
5.7.1.3.8. Ql_UART_ClrRxBuffer
This function clears the receive buffer of the specified UART port.
Prototype
Parameters
port:
[In] Port name.
Return Value
None.
5.7.1.3.9. Ql_UART_ClrTxBuffer
This function clears the send buffer of the specified UART port.
Prototype
Parameters
port:
[In] Port name.
Return Value
None.
5.7.1.3.10.Ql_UART_GetPinStatus
This function gets the status indication pins (including RI, DCD and DTR) of the virtual UART port and
does not work for the physical UART ports
Prototype
typedef enum {
UART_PIN_RI=0, //RI read operator is only valid on the virtual UART.
//RI set operator is invalid both on virtual and physical UART.
UART_PIN_DCD, //DCD read operator is only valid on the virtual UART.
//DCD set operatir is invalid both on virtual and physical UART.
} Enum_UARTPinType;
Parameters
port:
[In] Virtual UART port name.
pin:
[In] Pin name. One value of Enum_UARTPinType.
Return Value
If the return value ≥ 0, then it indicates the function is executed successfully, and a special pin level value
is returned: 0 means low level, and 1 means high level. If the return value ≤ 0, then it indicates failed to
execute the function.
5.7.1.3.11.Ql_UART_SetPinStatus
This function sets the pin level status of the virtual UART port. It does not work for the physical UART
ports.
Prototype
Parameters
port:
[In] Virtual UART port name.
pin:
[In] Pin name. One value of Enum_UARTPinType.
pinLevel:
[In] The pin level to be set. 0 means low level and 1 means high level.
Return Value
The return value is QL_RET_OK if this function succeeds. Otherwise, the return value is an error code. To
get extended error information, please refer to the ERROR CODES in header file Ql_error.h.
5.7.1.3.12.Ql_UART_SendEscap
This function notifies the virtual serial port to quit from Data Mode, and return back to Command Mode.
This function works only for virtual ports.
Prototype
Parameters
port:
[In] Port name.
Return Value
The return value is QL_RET_OK if this function succeeds. Otherwise, the return value is an error code. To
get extended error information, please refer to the ERROR CODES in header file Ql_error.h.
5.7.1.3.13.Ql_UART_Close
Prototype
Parameters
port:
[In] Port name.
Return Value
None.
5.7.1.4. Example
This chapter gives the example of how to use the UART port APIs.
//Write the callback function for dealing with the UART notifications.
static void CallBack_UART_Hdlr(Enum_SerialPort port, Enum_UARTEventType msg, bool level, void*
customizedPara); //Callback function.
{
switch(msg)
case EVENT_UART_READ_TO_READ:
//Read data from the UART port.
Ql_UART_Read (port,buffer,rlen);
break;
case EVENT_UART_READ_TO_WRITE:
//Resume the operation of writing data to UART.
QL_UART_Write(port,buffer,wlen);
break;
case EVENT _UART_RI_CHANGE:
break;
case EVENT _UART_DCD_CHANGE
break;
case EVENT _UART_DTR_CHANGE:
break;
case EVENT _UART_FE_IND:
break;
default:
break;
}
5.7.2. GPIO
There are 21 I/O pins that can be designed for general purpose I/O. All pins can be accessed under
OpenCPU by API functions.
Pin
Pin Name RESET MODE1 MODE2 MODE3 MODE4
No.
7 PINNAME_SD_CMD I/PD SD_CMD GPIO
Pin
Pin Name RESET MODE1 MODE2 MODE3 MODE4
No.
59 PINNAME_PCM_CLK HO/- PCM_CLK GPIO SPI_CS
.
The “MODE1” defines the original status of pin in standard module.
“RESET” column defines the default status of every pin after system is powered on.
“I” means input.
“O” means output.
“HO” means high output.
“PU” means internal pull-up circuit.
“PD” means internal pull-down circuit.
“EINT” means external interrupt input.
“PWM_OUT” means PWM output function.
NOTES
1. If pins PINNAME_SD_CMD, PINNAME_SD_CLK and PINNAME_SD_DATA are designed as SD
card interface, please do not configure these pins in customers‟ applications.
2. If PINNAME_SIM2_DATA, PINNAME_SIM2_RST, PINNAME_SIM2_CLK are designed as (U)SIM
card interface, please do not configure these pins in customers‟ applications.
In OpenCPU, there are two ways to initialize GPIOs. One is to configure initial GPIO list in
custom_gpio_cfg.h, please refer to Chapter 4.3; the other way is to call GPIO related APIs to initialize
after App starts.
/*---------------------------------------------------------------------------------------------------------------------------------------
{ Pin Name | Direction | Level | Pull
Selection }
*---------------------------------------------------------------------------------------------------------------------------------------*/
#if 1 //If needed, configure GPIOs here
GPIO_ITEM(PINNAME_NETLIGHT, PINDIRECTION_OUT, PINLEVEL_LOW,
PINPULLSEL_PULLDOWN)
GPIO_ITEM(PINNAME_PCM_IN, PINDIRECTION_OUT, PINLEVEL_LOW,
PINPULLSEL_PULLDOWN)
GPIO_ITEM(PINNAME_PCM_OUT, PINDIRECTION_OUT, PINLEVEL_LOW,
PINPULLSEL_PULLUP)
#else if 0
…
#endif
Step 1: GPIO initialization. Call Ql_GPIO_Init function sets the specified pin as the GPIO function, and
initializes the configurations, which includes direction, level and pull selection.
Step 2: GPIO control. When the pin is initialized as GPIO, the developers can call the GPIO related
APIs to change the GPIO level.
Step 3: Release the pin. If developers do not want use this pin no longer, and need to use this pin for
other purposes (such as PWM, EINT), they must call Ql_GPIO_Uninit to release the pin first.
This step is optional.
5.7.2.5.1. Ql_GPIO_Init
This function enables the GPIO function of the specified pin, and initializes the configurations, which
includes direction, level and pull selection.
Prototype
Parameters
pinName:
[In] Pin name. One value of Enum_PinName.
dir:
[In] The initial direction of GPIO. One value of Enum_PinDirection.
pullsel:
[In] Pull selection. One value of Enum_PinPullSel.
level:
[In] The initial level of GPIO. One value of Enum_PinLevel.
Return Value
QL_RET_OK: indicates this function is executed successfully. Negative integer indicates this function
fails.
5.7.2.5.2. Ql_GPIO_GetLevel
Prototype
Parameters
pinName:
[In] Pin name. One value of Enum_PinName.
Return Value
Return the level of the specified GPIO. 1 means high level, and 0 means low level.
5.7.2.5.3. Ql_GPIO_SetLevel
Prototype
Parameters
pinName:
[In] Pin name. One value of Enum_PinName.
level:
[In] The initial level of GPIO. One value of Enum_PinLevel.
Return Value
QL_RET_OK: indicates this function is executed successfully. Negative integer indicates this function
fails.
5.7.2.5.4. Ql_GPIO_GetDirection
Prototype
Parameters
pinName:
[In] Pin name. One value of Enum_PinName.
Return Value
The direction of the specified GPIO. 1 means output and 0 means input.
5.7.2.5.5. Ql_GPIO_SetDirection
Prototype
Parameters
pinName:
[In] Pin name. One value of Enum_PinName.
dir:
[In] The initial direction of GPIO. One value of Enum_PinDirection.
Return Value
QL_RET_OK: indicates this function is executed successfully. Negative integer indicates this function
fails.
5.7.2.5.6. Ql_GPIO_GetPullSelection
Prototype
Parameters
pinName:
[In] Pin name. One value of Enum_PinName.
Return Value
Return the pull selection of the specified GPIO. One value of Enum_PinPullSel.
5.7.2.5.7. Ql_GPIO_SetPullSelection
Prototype
Parameters
pinName:
[In] Pin name. One value of Enum_PinName.
pullSel:
[In] Pull selection. One value of Enum_PinPullSel.
Return Value
QL_RET_OK: indicates this function is executed successfully. Negative integer indicates this function
fails.
5.7.2.5.8. Ql_GPIO_Uninit
This function releases the specified GPIO that was initialized by calling Ql_GPIO_Init previously. After
releasing, the GPIO can be used for other purposes.
Prototype
Parameters
pinName:
[In] Pin name. One value of Enum_PinName.
Return Value
QL_RET_OK: indicates this function is executed successfully. Negative integer indicates this function
fails.
5.7.2.6. Example
This chapter gives the example of how to use the GPIO APIs.
void API_TEST_gpio(void)
{
s32 ret;
Ql_Debug_Trace("\r\n<*********** GPIO API Test ***********>\r\n");
ret=Ql_GPIO_SetLevel(PINNAME_NETLIGHT,PINLEVEL_HIGH);
Ql_Debug_Trace("\r\n<--pin(%d) Ql_GPIO_SetLevel =%d ret=%d-->\r\n",
PINNAME_NETLIGHT,PINLEVEL_HIGH,ret);
ret=Ql_GPIO_SetDirection(PINNAME_NETLIGHT,PINDIRECTION_IN);
Ql_Debug_Trace("\r\n<--pin(%d) Ql_GPIO_SetDirection =%d ret=%d-->\r\n",
PINNAME_NETLIGHT,PINDIRECTION_IN,ret);
ret=Ql_GPIO_GetLevel(PINNAME_NETLIGHT);
Ql_Debug_Trace("\r\n<--pin(%d) Ql_GPIO_GetLevel =%d ret=%d-->\r\n",
PINNAME_NETLIGHT,ret,ret);
ret=Ql_GPIO_GetDirection(PINNAME_NETLIGHT);
ret=Ql_GPIO_SetPullSelection(PINNAME_NETLIGHT,PINPULLSEL_PULLDOWN);
Ql_Debug_Trace("\r\n<--pin(%d) Ql_GPIO_SetPullSelection =%d ret=%d-->\r\n",
PINNAME_NETLIGHT,PINPULLSEL_PULLDOWN,ret);
ret=Ql_GPIO_GetPullSelection(PINNAME_NETLIGHT);
Ql_Debug_Trace("\r\n<--pin(%d) Ql_GPIO_GetPullSelection =%d ret=%d-->\r\n",
PINNAME_NETLIGHT,ret,ret);
ret=Ql_GPIO_Uninit(PINNAME_NETLIGHT);
Ql_Debug_Trace("\r\n<--pin(%d) Ql_GPIO_Uninit ret=%d-->\r\n",PINNAME_NETLIGHT,ret);
}
5.7.3. EINT
OpenCPU module has three external interrupt pins, please refer to Chapter 5.7.2.2 for details. The
interrupt trigger mode just support level-triggered mode. The software debounce for external interrupt
sources is used to minimize the possibility of false activations. External interrupt has higher priority, so
frequent interrupt is not allowed. It is strongly recommended that the interrupt frequency is not more than
2, and too frequent interrupt will cause other tasks cannot be scheduled, which probably leads to
unexpected exception.
NOTE
The interrupt response time is 50ms by default, and can be re-programmed to a greater value in
OpenCPU. However, it is strongly recommended that the interrupt frequency cannot be more than 3Hz so
as to ensure stable working of the module.
The following steps show how to use the external interrupt function:
Step 1: Register an external interrupt function. Developers must choose one external interrupt pin and
use Ql_EINT_Register (or Ql_EINT_RegisterFast) to register an interrupt handler function.
Step 2: Initialize the interrupt configurations. Call Ql_EINT_Init function to configure the software
debounce time and set the level-triggered interrupt mode.
Step 3: Interrupt handle. The interrupt callback function will be called if the level has changed.
Developers can process something in the handler.
Step 4: Mask the interrupt. When developers do not want external interrupt. they can use
Ql_EINT_Mask function to disable the external interrupt, and call the Ql_EINT_Unmask function
to enable the external interrupt.
Step 5: Release the specified EINT pin. Call Ql_EINT_Uninit function to release the specified EINT pin,
and the pin can be used for other purposes after it is released. This step is optional.
5.7.3.3.1. Ql_EINT_Register
This function registers an EINT I/O, and specifies the interrupt handler.
Prototype
Parameters
eintPinName:
[In] EINT pin name. One value of Enum_PinName that has the interrupt function.
callback_eint:
[In] The interrupt handler.
pinLevel:
[In] The EINT pin level value. One value of Enum_PinLevel.
customParam:
[In] Customized parameter. If not used, just set it to NULL.
Return Value
QL_RET_OK: indicates this function is executed successfully. Negative integer indicates this function
fails.
5.7.3.3.2. Ql_EINT_RegisterFast
This function registers an EINT I/O, and specifies the interrupt handler. The EINT that is registered by
calling this function is a top half interrupt. The response to interrupt request is timelier. Please do not add
any task schedule in the interrupt handler which cannot consume much CPU time, or else, system
exceptions or resetting may be caused.
Prototype
Parameters
eintPinName:
[In] EINT pin name. One value of Enum_PinName that has the interrupt function.
callback_eint:
[In] The interrupt handler.
pinLevel:
[In] The EINT pin level value. One value of Enum_PinLevel.
customParam:
[In] Customized parameter. If not used, just set it to NULL.
Return Value
QL_RET_OK: indicates this function is executed successfully. Negative integer indicates this function
fails.
5.7.3.3.3. Ql_EINT_Init
Prototype
Parameters
eintPinName:
[In] EINT pin name. One value of Enum_PinName that has the interrupt function.
eintType:
[In] Interrupt type, level-triggered or edge-triggered. Now, only level-triggered interrupt is supported.
hwDebounce:
[In] Hardware debounce. Unit: 10ms. It is not supported now.
swDebounce:
[In] Software debounce. Unit: 10ms. The minimum value for this parameter is 5, and this means the
autoMask:
[In] Whether automatically mask the external interrupt after the interrupt happens. 0 means no, and 1
means yes.
Return Value
QL_RET_OK: indicates this function is executed successfully. Negative integer indicates this function
fails.
5.7.3.3.4. Ql_EINT_Uninit
Prototype
Parameters
eintPinName:
[In] EINT pin name.
Return Value
QL_RET_OK: indicates this function is executed successfully. Negative integer indicates this function
fails.
5.7.3.3.5. Ql_EINT_GetLevel
Prototype
Parameters
eintPinName:
[In] EINT pin name.
Return Value
5.7.3.3.6. Ql_EINT_Mask
Prototype
Parameters
eintPinName:
[In] EINT pin name.
Return Value
None.
5.7.3.3.7. Ql_EINT_Unmask
Prototype
Parameters
eintPinName:
[In] EINT pin name.
Return Value
None.
5.7.3.4. Example
The following sample codes show how to use the EINT function.
//Developers need to unmask the interrupt again, because PINNAME_DTR pin interrupt is
initialized as auto mask.
Ql_EINT_Unmask(eintPinName);
if(*((s32*)customParam) >= 3)
{
//If developers do not want the interrupt, mask it now!!!
Ql_EINT_Mask(eintPinName);
}
}
else if(PINNAME_SIM_PRESENCE==eintPinName)
{
ret=Ql_EINT_GetLevel(eintPinName);
Ql_Debug_Trace("\r\n<--Ql_EINT_GetLevel pin(%d) levle(%d)-->\r\n",eintPinName,ret);
void API_TEST_eint(void)
{
s32 ret;
5.7.4. PWM
OpenCPU module has one PWM pin, please refer to Chapter 5.7.2.2 for details. The PWM has two clock
sources: one is 32K (the exact value is 32768Hz) and the other is 13M. When the module is in sleep
mode, the 13M clock source will be disabled, but the 32K clock source works normally.
Step 1: Initialize a PWM pin. Call Ql_PWM_Init function to configure the PWM duty cycle and frequency.
Step 2: PWM waveform control. Call Ql_PWM_Output to switch on/off the PWM waveform output.
Step 3: Release the PWM pin. Call Ql_PWM_Uninit to release the PWM pin. This step is optional.
5.7.4.3.1. Ql_PWM_Init
Prototype
Parameters
pwmPinName:
[In] PWM pin name, and only can be PINNAME_NETLIGHT.
pwmSrcClk:
[In] PWM clock source. One value of Enum_PwmSource.
pwmDiv:
[In] Clock source frequency division. One value of Enum_PwmSourceDiv.
lowPulseNum:
[In] Set the number of clock cycles to stay at low level. The result of lowPulseNum plus highPulse Num is
less than 8193.
highPulseNum:
[In] Set the number jof clock cycles to stay at high level. The result of lowPulseNum plus highPulseNum is
less than 8193.
NOTES
Return Value
QL_RET_OK: indicates this function is executed successfully. Negative integer indicates this function
fails.
5.7.4.3.2. Ql_PWM_Uninit
Prototype
Parameters
pwmPinName:
[In] PWM pin name. One value of Enum_PinName.
Return Value
QL_RET_OK: indicates this function is executed successfully. Negative integer indicates this function
fails.
5.7.4.3.3. Ql_PWM_Output
Prototype
Parameters
pwmPinName:
[In] PWM pin name. One value of Enum_PinName.
pwmOnOff:
[In] PWM enabling or disabling. Control the enabling/disabling and waveform output of PWM function.
Return Value
QL_RET_OK: indicates this function is executed successfully. Negative integer indicates this function
fails.
5.7.4.4. Example
This following sample codes show how to use the PWM function.
void API_TEST_pwm(void)
{
s32 ret;
Ql_Sleep(3000);
//Stop PWM waveform output.
ret=Ql_PWM_Output(PINNAME_NETLIGHT, 0);
Ql_Debug_Trace("\r\n<--pin(%d) Ql_PWM_Output stop ret=%d-->\r\n",PINNAME_NETLIGHT,ret);
5.7.5. ADC
OpenCPU module provides an analogue input pin that can be used to detect the external voltage. Please
refer to document [2] for the pin definitions and ADC hardware characteristics. The voltage range that
can be detected is 0mV~2800mV.
Step 1: Register an ADC sampling function. Call Ql_ADC_Register function to register a callback
function which will be called when the module outputs the ADC value.
Step 2: ADC sampling parameter initialization. Call Ql_ADC_Init function to set the sampling count and
the interval of each sampling.
Step 3: Start/stop ADC sampling. Use Ql_ADC_Sampling function with an enabling parameter to start
ADC sampling, and then ADC callback function will be invoked cyclically to report the ADC value.
Call this API function again with a disabling parameter may stop the ADC sampling.
5.7.5.3.1. Ql_ADC_Register
This function registers an ADC callback function. The callback function will be called when the module
outputs the ADC value.
Prototype
Parameters
adcPin:
[In] ADC pin name. One value of Enum_ADCPin.
callback_adc:
[In] Callback funtion, which will be called when the module outputs the ADC value.
customParam:
[In] Customized parameter. If not used, just set it to NULL.
adcValue:
[In] The average value of ADC sampling. The range is 0mV~2800mV.
Return Value
QL_RET_OK: indicates this function is executed successfully. Negative integer indicates this function
fails.
5.7.5.3.2. Ql_ADC_Init
This function initializes the configurations for ADC, including sampling count and the interval of each
internal sampling. The ADC callback function will be called when the module outputs the ADC value, and
the value is the average of the sampling value.
Prototype
Parameters
adcpin:
[In] ADC pin name. One value of Enum_ADCPin.
count:
[In] Internal sampling times for each reporting ADC value. The minimum value is 5.
interval:
[In] Interval of each internal sampling. Unit: ms. The minimum value is 200 and this means the ADC report
frequency must be less than 1Hz.
Return Value
QL_RET_OK: indicates this function is executed successfully. Negative integer indicates this function
fails.
5.7.5.3.3. Ql_ADC_Sampling
Prototype
Parameters
adcPin:
[In] ADC pin name. One value of Enum_ADCPin.
enable:
[In] Sampling control. 1 means start sampling, and 0 means stop sampling.
Return Value
QL_RET_OK: indicates this function is executed successfully. Negative integer indicates this function
fails.
5.7.5.4. Example
5.7.6. IIC
The module provides a hardware IIC interface. The IIC interface can be simulated by GPIO pins, which
can be any two GPIOs in the GPIO list in Chapter 5.7.2.2. Therefore, one or more IIC interfaces are
possible.
Step 1: Initialize IIC interface. Call Ql_IIC_Init function to initialize an IIC channel, including the specified
GPIO pins for IIC and an IIC channel number.
Step 2: Configure IIC interface. Call Ql_IIC_Config to configure parameters that the slave device needs.
Please refer to the API decription for extended information.
Step 3: Read data from slave. Developers can use Ql_IIC_Read function to read data from the specified
slave. The following figure shows the data exchange direction.
Step 4: Write data to slave. Developers can use Ql_IIC_Write function to write data to the specified slave.
The following figure shows the data exchange direction.
Step 5: Write the data to the register (or the specified address) of the slave. Developers can use
Ql_IIC_Write function to write the data to a register of the slave. The following figure shows the
data exchange direction.
Step 6: Read the data from the register (or the specified address) of the slave. Developers can use
Ql_IIC_Write_Read function to read the data from a register of the slave. The following figure
shows the data exchange direction.
Step 7: Release the IIC channel. Call Ql_IIC_Uninit function to release the specified IIC channel.
5.7.6.3.1. Ql_IIC_Init
This function initializes the configurations for an IIC channel, including the specified pins for IIC, IIC type,
and IIC channel number.
Prototype
Parameters
chnnlNo:
[In] IIC channel number. The range is 0~254.
pinSCL:
[In] IIC SCL pin.
pinSDA:
[In] IIC SDA pin.
IICtype:
[In] IIC type. FALSE means simulated IIC, and TRUE means hardware IIC.
Return Value
QL_RET_OK: indicates this function is executed successfully. Negative integer indicates this function
fails.
5.7.6.3.2. Ql_IIC_Config
Prototype
Parameters
chnnlNo:
[In] IIC channel number. It is specified by Ql_IIC_Init function.
isHost:
[In] Whether use host mode or not. It must be TRUE and just support host mode.
slaveAddr:
[In] Slave address.
speed:
[In] IIC communication speed. The parameter is just for IIC controller, and can be ingored if developers
use simulated IIC.
Return Value
QL_RET_OK: indicates this function is executed successfully. Negative integer indicates this function
fails.
5.7.6.3.3. Ql_IIC_Write
Prototype
Parameters
chnnlNo:
[In] IIC channel number. It is specified by Ql_IIC_Init function.
slaveAddr:
[In] Slave address.
pData:
[In] Setting value to be written to the slave.
Len:
[In] Number of bytes to write. If IICtype=1, then 1<len<8 because Quectel‟s IIC controller supports 8 bytes
at most for transmission at a time.
Return Value
If no error occurs, the length of the write data will be returned. Negative integer indicates this function
fails.
5.7.6.3.4. Ql_IIC_Read
This function reads data from specified slave through IIC interface.
Prototype
Parameters
chnnlNo:
[In] IIC channel number. It is specified by Ql_IIC_Init function.
slaveAddr:
[In] Slave address.
pBuffer:
[Out] The buffer that stores the data read from a specific slave.
Len:
[Out] Number of bytes to read. If IICtype=1, then 1<len<8 because Quectel‟s IIC controller supports 8
bytes at most for one-time transmission.
Return Value
If no error occurs, the length of the red data will be returned. Negative integer indicates this function fails.
5.7.6.3.5. Ql_IIC_WriteRead
This function reads data from the specified register (or address) of the specified slave.
Prototype
Parameters
chnnlNo:
[In] IIC channel number. It is specified by Ql_IIC_Init function.
slaveAddr:
[In] Slave address.
pData:
[In] Setting values of the specified register of the slave.
wrtLen:
[In] Number of bytes to write. If IICtype=1, then 1<wrtLen<8.
pBuffer:
[Out] The buffer that stores the data read from a specific slave
rdLen:
[Out] Number of bytes to read. If IICtype=1, then 1<wrtLen<8.
Return Value
If no error occurs, the length of the read data will be returned. Negative integer indicates this function fails.
5.7.6.3.6. Ql_IIC_Uninit
Prototype
Parameters
chnnlNo:
[In] IIC channel number. It is specified by Ql_IIC_Init function.
Return Value
QL_RET_OK: indicates this function is executed successfully. Negative integer indicates this function
fails.
5.7.6.4. Example
void API_TEST_iic(void)
{
s32 ret;
u8 write_buffer[4]={0x10,0x02,0x50,0x0a};
u8 read_buffer[6]={0x14,0x22,0x33,0x44,0x55,0x66};
u8 registerAdrr[2]={0x01,0x45};
Ql_Debug_Trace("\r\n<*********** IIC API Test ***********>\r\n");
ret=Ql_IIC_Uninit(1);
}
5.7.7. SPI
The module provides a hardware SPI interface. The interface can also be simulated by GPIO pins, which
can be any GPIO in the GPIO list in Chapter 5.7.2.2.
Step 1: Initialize SPI Interface. Call Ql_SPI_Init function to initialize the configurations for a SPI channel,
including the specified pins for SPI, SPI type, and SPI channel number.
Step 2: Configure parameters. Call Ql_SPI_Config function to configure some parameters for the SPI
interface, including the clock polarity and clock phase.
Step 3: Write data. Call Ql_SPI_Write function to write bytes to the specified slave bus.
Step 4: Read data. Call Ql_SPI_Read function to read bytes from the specified slave bus.
Step 5: Write and read. The Ql_SPI_WriteRead function is used for SPI full-duplex communication that
can read and write data at a time.
Step 6: Release SPI interface. Invoke Ql_SPI_Uniti function to release the SPI pins. This step is
optional.
5.7.7.3.1. Ql_SPI_Init
This function initializes the configurations for a SPI channel, including the SPI channel number and the
specified GPIO pins for SPI.
Prototype
Parameters
chnnlNo:
[In] SPI channel number. The range is 0~254
pinClk:
[In] SPI CLK pin.
pinMiso:
[In] SPI MISO pin.
pinMosi:
[In] SPI MOSI pin.
spiType:
[In] SPI type. It must be 0.
Return Value
QL_RET_OK: indicates this function is executed successfully. Negative integer indicates this function
fails.
5.7.7.3.2. Ql_SPI_Config
Prototype
s32 Ql_SPI_Config (u32 chnnlNo, bool isHost, bool cpol, bool cpha, u32 clkSpeed)
Parameters
chnnlNo:
[In] SPI channel number. It is specified by Ql_SPI_Init function.
isHost:
[In] Whether use host mode or not. It must be TRUE and just support host mode.
cpol:
[In] Clock polarity. Please refer to the SPI standard protocol for more information.
cpha:
[In] Clock phase. Please refer to the SPI standard protocol for more information.
clkSpeed:
[In] SPI speed. It is not supported now, so the input argument will be ignored.
Return Value
If no error occurs, the length of the write data will be returned. Negative integer indicates this function fails
5.7.7.3.3. Ql_SPI_Write
This function writes data to the specified slave through SPI interface.
Prototype
Parameters
chnnlNo:
[In] SPI channel number. It is specified by Ql_SPI_Init function.
pData:
[In] Setting value to be written to the slave.
len:
[In] Number of bytes to be written.
Return Value
If no error occurs, the length of the write data will be returned. Negative integer indicates this function
fails.
5.7.7.3.4. Ql_SPI_Read
This function reads data from the specified slave through SPI interface.
Prototype
Parameters
chnnlNo:
[In] SPI channel number. It is specified by Ql_SPI_Init function.
pBuffer:
[Out] The buffer that stores the data read from a specific slave.
rdLen:
[Out] Number of bytes to be read.
Return Value
If no error occurs, the length of the read data will be returned. Negative integer indicates this function fails.
5.7.7.3.5. Ql_SPI_WriteRead
Prototype
Parameters
chnnlNo:
[In] SPI channel number. It is specified by Ql_SPI_Init function.
pData:
[In] Setting value to be written to the slave.
wrtLen:
[In] Number of bytes to be written.
pBuffer:
[Out] The buffer that stores the data read from a specific slave.
rdLen:
[Out] Number of bytes to be read.
NOTES
Return Value
If no error occurs, the length of the read data will be returned. Negative integer indicates this function fails.
5.7.7.3.6. Ql_SPI_Uninit
Prototype
Parameters
chnnlNo:
[In] SPI channel number. It is specified by Ql_SPI_Init function.
Return Value
QL_RET_OK: indicates this function is executed successfully. Negative integer indicates this function fails
5.7.7.4. Example
void API_TEST_spi(void)
{
s32 ret;
u32 rdLen=0;
u32 wdLen=0;
u8 spi_write_buffer[]={0x01,0x02,0x03,0x0a,0x11,0xaa};
u8 spi_read_buffer[100];
Ql_Debug_Trace("\r\n<*********** TEST API Test ***********>\r\n");
ret=Ql_SPI_Init(1,PINNAME_PCM_IN,PINNAME_PCM_SYNC,PINNAME_PCM_OUT,PINNAME_PC
M_CLK,1);
Ql_Debug_Trace("\r\n<--SPI channel 1 Ql_SPI_Init ret=%d-->\r\n",ret);
wdLen=Ql_SPI_Write(1,spi_write_buffer,6);
Ql_Debug_Trace("\r\n<--SPI channel 1 Ql_SPI_Write data len =%d-->\r\n",wdLen);
rdLen=Ql_SPI_Read(1,spi_read_buffer,6);
Ql_Debug_Trace("\r\n<--SPI channel 1 Ql_SPI_Read data len =%d-->\r\n",rdLen);
rdLen=Ql_SPI_WriteRead(1,spi_write_buffer,6,spi_read_buffer,3);
Ql_Debug_Trace("\r\n<--SPI channel 1 Ql_SPI_WriteRead Read data len =%d-->\r\n",rdLen);
ret=Ql_SPI_Uninit(1);
Ql_Debug_Trace("\r\n<--SPI channel 1 Ql_SPI_Uninit ret =%d-->\r\n",ret);
}
5.8.1. Overview
The module supports defining and activating 2 PDP contexts at the same time. Each PDP context
supports at most 6 client socket connections and 5 server socket connections.
The examples in the example_tcpclient.c and example_tcpserver.c of OpenCPU SDK show the proper
usages of these methods.
5.8.2. Usage
The following steps tell how to work with GPRS PDP context:
Step 1: Register PDP callback. Call function Ql_GPRS_Register to register the GPRS‟s callback
function.
Step 2: Set PDP context. Call function Ql_GPRS_Config to configure the GPRS PDP context, including
APN name, user name and password.
Step 3: Activate PDP. Call function Ql_GPRS_Activate to activate the GPRS PDP context. The result for
activating GPRS will usually be informed in Callback_GPRS_Actived. See also the description
for Ql_GPRS_Activate below.
Calling of Ql_GPRS_AcitvateEx may activate the GPRS and get the result when this API
function returns. The callback function Callback_GPRS_Actived will not be invoked. It means
this API function will be executed in blocking mode. See also the description for
Ql_GPRS_ActivateEx below.
function returns. The callback function Callback_GPRS_Deactived will not be invoked. It means
this API function will be executed in blocking mode. See also the description for
Ql_GPRS_DeactivateEx below.
5.8.3.1. Ql_GPRS_Register
This function registers the GPRS related callback functions. And these callback functions will be invoked
only in the registered task.
Prototype
typedef struct {
void (*Callback_GPRS_Actived)(u8 contexId, s32 errCode, void* customParam);
void (*CallBack_GPRS_Deactived)(u8 contextId, s32 errCode, void* customParam );
} ST_PDPContxt_Callback;
Parameters
contextid:
[In] Module supports two PDP contexts at the same time. It can be 0 or 1
callback_func:
[In] Callback function, which is called by OpenCPU to inform Embedded Application whether this function
succeeds or not. It should be implemented by Embedded Application.
customerParam:
[In] One customized parameter that can be passed into callback functions.
Return Value
The return value is 0 if this function succeeds. Otherwise, a value of Enum_SocError is returned.
5.8.3.2. Callback_GPRS_Actived
When the return value of Ql_GPRS_Activate is SOC_WOULDBLOCK, this callback function will be
invoked later.
Prototype
Parameters
contextId:
[Out] PDP context ID that is specified when calling Ql_GPRS_Activate. It can be 0 or 1.
errCode:
[Out] The result code of activating GPRS. 0 means successful GPRS activation.
customerParam:
[Out] One customized parameter that can be passed into Ql_GPRS_Register. It may be NULL.
Return Value
None.
5.8.3.3. CallBack_GPRS_Deactived
When the return value of Ql_GPRS_Deactivate is SOC_WOULDBLOCK, this callback function will be
invoked by Core System later.
Prototype
Parameters
contextId:
[Out] PDP context ID that is specified when calling Ql_GPRS_Activate. It may be 0 or 1.
errCode:
[Out] The result code of activating GPRS. 0 indicates successful GPRS activating.
customerParam:
[Out] One customized parameter that can be passed into Ql_GPRS_Register. It may be NULL.
Return Value
None.
5.8.3.4. Ql_GPRS_Config
This function configures GPRS parameters including APN name, user name, password and
authentication type for the specified PDP context.
Prototype
typedef struct {
u8 apnName[MAX_GPRS_APN_LEN];
u8 apnUserId[MAX_GPRS_USER_NAME_LEN];
u8 apnPasswd[MAX_GPRS_PASSWORD_LEN];
u8 authtype; //PAP or CHAP
void* Reserved1; //QoS
void* Reserved2; //
} ST_GprsConfig;
Parameters
apnName:
[In] APN name. Null-terminated characters.
apnUserId:
[In] APN user ID. Null-terminated characters.
apnPasswd:
[In] APN password. Null-terminated characters.
Authtype:
[In] Authentication method. 1 means PAP authentification, and 2 means CHAP authentification.
Return Value
5.8.3.5. Ql_GPRS_Activate
This function activates GPRS PDP context. On the basis of network status, the PDP context activation will
take some time, and the longest activation time is 150s. When the PDP activation succeeds or fails,
Callback_GPRS_Actived callback function will be called and gives the activation result.
Prototype
Parameters
contextId:
[In] Module supports two PDP contexts at the same time. It can be 0 or 1.
Return Value
Example
{
s32 ret;
ret=Ql_GPRS_Activate(0);
if (GPRS_PDP_SUCCESS==ret)
{
//Activated GPRS successfully.
}
else if (GPRS_PDP_WOULDBLOCK==ret)
{
//GPRS is being activated, and module needs to wait for the reault of calling
Callback_GPRS_Actived.
}
else if (GPRS_PDP_ALREADY==ret)
{
//GPRS has been activated.
}else{
//Failed to activate GPRS, and the error code is in "ret".
//Developers may retry to activate GPRS, and reset the module after 3 successive failures.
}
}
5.8.3.6. Ql_GPRS_ActivateEx
This function activates the specified GPRS PDP context. The maximum possible time for activating GPRS
is 180s.
Non-blocking Mode
When isBlocking is set to FALSE, this function works under non-blocking mode. The result will be
returned even if the operation is not done, and the result will be reported in callback.
Blocking Mode
When isBlocking is set to TRUE, this function works under blocking mode. The result will be returned only
after the operation is done.
Prototype
Parameters
contextId:
[In] Module supports two PDP contexts at the same time. It can be 0 or 1.
isBlocking:
[In] Mode the function works in. TRUE means blocking mode, and FALSE means non-blocking mode.
Return Value
Example
{
s32 ret;
ret=Ql_GPRS_Activate(0, TRUE);
if (GPRS_PDP_SUCCESS==ret)
{
//Activated GPRS successfully.
}
else if (GPRS_PDP_ALREADY==ret)
{
//GPRS has been activated.
}else{
//Failed to activate GPRS, and the error code is in "ret".
//Developers may retry to activate GPRS, and reset the module after 3 successive failures.
}
}
5.8.3.7. Ql_GPRS_Deactivate
This function deactivates the specified PDP context. On the basis of the network status, PDP deactivation
will take some time and the longest time is 90s. When the PDP deactivation succeeds or fails,
CallBack_GPRS_Deactived callback function will be called and gives the activation result.
Prototype
Parameters
contextId:
[In] PDP context ID that is specified when calling Ql_GPRS_Activate.
Return Value
The return value is 0 if this function succeeds. Otherwise, a value of ql_soc_error_enum is returned.
Please refer to the Possible Error Codes in Chapter 5.9.4.
Example
{
s32 ret;
ret=Ql_GPRS_Deactivate(0);
if (GPRS_PDP_SUCCESS==ret)
{
//Deactivated GPRS successfully.
}
else if (GPRS_PDP_WOULDBLOCK==ret)
{
//GPRS is being activated, and module needs to wait for the reault of calling
Callback_GPRS_Deactived.
}else{
//Failed to deactivate GPRS, and the error code is in "ret".
}
}
5.8.3.8. Ql_GPRS_DeactivateEx
This function deactivates the specified PDP context. The maximum possible time for activating GPRS is
90s.
Non-blocking Mode
When "isBlocking" is set to FALSE, this function works under non-blocking mode. The result will be
returned even if the operation is not done, and the result will be reported in callback.
Blocking Mode
When "isBlocking" is set to TRUE, this function works under blocking mode. The result will be returned
only after the operation is done.
Prototype
Parameters
contextId:
[In] PDP context ID that is specified when calling Ql_GPRS_Activate.
isBlocking:
[In] Mode the function works in. TRUE means blocking mode, and FALSE means non-blocking mode.
Return Value
Example
{
s32 ret;
ret=Ql_GPRS_Deactivate(0, TRUE);
if (GPRS_PDP_SUCCESS==ret)
{
//Deactivated GPRS successfully.
}else{
//Failed to deactivate GPRS, and the error code is in "ret".
}
}
5.8.3.9. Ql_GPRS_GetLocalIPAddress
Prototype
Parameters
contextId:
[In] PDP context ID that is specified when calling Ql_GPRS_Activate.
ipAddr:
[Out] Pointer to the buffer that is used to store the local IPv4 address.
Return Value
If no error occurs, this return value will be SOC_SUCCESS (0). Otherwise, a value of Enum_SocError is
returned.
5.8.3.10. Ql_GPRS_GetDNSAddress
This function retrieves the DNS server‟s IP addresses, which include the first DNS addess and the
second DNS addess.
Prototype
Parameters
contextId:
[In] PDP context ID that is specified when calling Ql_GPRS_Activate.
firstAddr:
[Out] Pointer to the buffer that is used to store the primary DNS server‟s IP address.
secondAddr:
[Out] Pointer to the buffer that is used to store the secondary DNS server‟s IP address.
Return Value
If no error occurs, this return value will be SOC_SUCCESS (0). Otherwise, a value of Enum_SocError is
returned.
Prototype
Parameters
contextid:
[In] PDP context ID that is specified when calling Ql_GPRS_Activate.
firstAddr:
[In] An u32 integer that stores the IPv4 address.
secondAddr:
[In] An u32 integer that stores the IPv4 address.
Return Value
If no error occurs, this return value will be SOC_SUCCESS (0). Otherwise, a value of Enum_SocError is
returned.
5.9.1. Overview
Socket program implements the TCP and UDP protocols. In OpenCPU, developers use the API functions
to program TCP/UDP instead of using AT commands. Each PDP context supports at most 6 client socket
connections and 5 server socket connections.
5.9.2. Usage
The following steps tell how to work with TCP client socket:
Step 1: Register socket related callback functions. Call function Ql_SOC_Register to register the socket
related callback functions.
Step 2: Create a socket. Call function Ql_SOC_Create to create a socket. The “contextId” argument
should be the same as the one that Ql_GPRS_Register uses, and the “socketType” should be
set as “SOCK_TCP”.
Step 3: Connet to socket. Call Ql_SOC_Connect to request a socket connection. The
Callback_Socket_Connect function will be invoked no matter the connection is successful or not.
Step 4: Send data to socket. Call function Ql_SOC_Send to send data to socket. After the data is sent
out, developers can call Ql_SOC_GetAckNumber function to check whether the data is received
by the server. If Ql_SOC_Send retruns SOC_WOULDBLOCK, the application must wait for
Callback_Socket_Write function to send data again.
Step 5: Receive data from socket. When there is data coming from the socket, the callback_socket_read
function will be invoked to inform App. When received the notificatioin, application may call
Ql_SocketRecv to receive the data. Application must read out all the data. Otherwise, the
callback function will not be invoked when new data comes.
Step 6: Close the socket. Application can call function Ql_SOC_Close to close the socket. When
application receives the notification that the server side has closed the socket, application has to
call Ql_SOC_Close to close the socket from the client side.
The following steps tell how to work with the TCP Server:
Step 1: Register the socket related callback functions. Call function Ql_SOC_Register to register the
socket related callback functions.
Step 2: Create a socket. Call function Ql_SOC_Create to create a socket.
Step 3: Bind. Call function Ql_SOC_Bind to associate a local address with a socket.
Step 4: Listen. Call function Ql_SOC_Listen to start to listen to the connection request from listening
port.
Step 5: Accept connection request. When a connection request comes, Callback_Socket_Accept will be
invoked to inform App. Application can call function Ql_SOC_Accept to accept the connection
request.
Step 6: Send data to socket. Call function Ql_SOC_Send to send data to socket. After the data is sent out,
developers can call Ql_SOC_GetAckNumber function to check whether the data is received by
the client. When this function retruns SOC_WOULDBLOCK, the application has to wait till
Callback_Socket_Write is invoked, and then application can continue to send data.
Step 7: Receive data from socket. When data comes from the socket, the Callback_Socket_Read will be
invoked to inform application, and application can call Ql_SocketRecv to receive the data.
Application must read out all the data. Otherwise, the callback function will not be invoked when
new data comes.
Step 8: Close socket. Application can call function Ql_SOC_Close to close the socket. When application
receives the notification the client side has closed the socket, it has to call Ql_SOC_Close to
close the socket from the server side.
Step 1: Register the socket related callback functions. Call function Ql_SOC_Register to register the
socket related callback functions.
Step 2: Create a socket. Call function Ql_SOC_Create to create a socket. The „contextId‟ argument
should be the same as the one that Ql_GPRS_Register uses, and the „socketType‟ should be
set as ‟SOCK_UDP‟.
Step 3: Bind. Call function Ql_SOC_Bind to associate a local address with a socket.
Step 4: Send data to socket. Call function Ql_SOC_SendTo to send data. When this function retruns
SOC_WOULDBLOCK, the application has to wait till Callback_Socket_Write is invoked, and
then App can continue to send data.
Step 5: Receive data from socket. When data comes from the socket, the Callback_Socket_Read
function will be invoked to inform application and application can call Ql_SocketRecvFrom to
receive the data. App must read out all the data. Otherwise, the callback function will not be
invoked when new data comes.
Step 6: Close socket. Call function Ql_SOC_Close to close the socket. App can call function
Ql_SOC_Close to close the socket.
5.9.3.1. Ql_SOC_Register
Prototype
typedef struct {
void (*callback_socket_connect)(s32 socketId, s32 errCode, void* customParam );
void (*callback_socket_close)(s32 socketId, s32 errCode, void* customParam );
void (*callback_socket_accept)(s32 listenSocketId, s32 errCode, void* customParam );
void (*callback_socket_read)(s32 socketId, s32 errCode, void* customParam );
void (*callback_socket_write)(s32 socketId, s32 errCode, void* customParam );
}ST_SOC_Callback;
Parameters
cb:
[In] Pointer of the socket related callback function.
customParam:
[In] Customized parameter. If not used, just set it to NULL.
Callback_Socket_Connect
This callback function is invoked by Ql_SocketConnect when the return value of Ql_SocketConnect is
SOC_WOULDBLOCK.
Prototype
Parameters
socketId:
[Out] Socket ID that is returned when calling Ql_SOC_Create.
errCode:
[Out] Error code.
customParam:
[Out] Customized parameter. If not used, just set it to NULL.
Callback_Socket_Close
This callback function will be invoked when the socket connection is closed by the remote side. This
function is valid for TCP socket only. If the socket connection is closed by the module, this function will not
be invoked.
Prototype
Parameters
socketId:
[Out] Socket ID that is returned when calling Ql_SOC_Create.
errCode:
[Out] Error code.
customParam:
[Out] Customized parameter. If not used, just set it to NULL.
Callback_Socket_Accept
Accept a connection on a socket when the module is a server. This function is valid when the module is
used as TCP server only.
Prototype
Parameters
listenSocketId:
[Out] Socket ID that is returned when calling Ql_SOC_Create.
error_code:
[Out] Error code.
customParam:
[Out] Customized parameter. If not used, just set it to NULL.
Return Value
None.
5.9.3.2. Callback_Socket_Read
This function will be invoked when received data from the socket. Then developers can read the data via
Ql_SOC_Recv (for TCP) or Ql_SOC_RecvFrom (for UDP) APIs.
Prototype
Parameters
socketId:
[Out] Socket ID that is returned when calling Ql_SOC_Create.
error_code:
[Out] Error code.
customParam:
[Out] Customized parameter. If not used, just set it to NULL.
Return Value
None.
5.9.3.3. Callback_Socket_Write
When the return value of Ql_SOC_Send is SOC_WOULDBLOCK, this callback function will be invoked to
enable application to continue to send TCP data.
Prototype
Parameters
socketId:
[Out] Socket ID that is returned when calling Ql_SOC_Create.
errCode:
[Out] Error code.
customParam:
[Out] Customized parameter. If not used, just set it to NULL.
Return Value
None.
5.9.3.4. Ql_SOC_Create
This function creates a socket with the specified socket ID on the specified PDP context.
Prototype
Parameters
contextId:
[In] PDP context ID that is specified when calling Ql_GPRS_Activate. It can be 0 or 1.
socketType:
[In] Socket type. One value of Enum_SocketType.
typedef enum{
SOCK_TCP = 0, //Stream socket, TCP.
SOCK_UDP, //Datagram socket, UDP.
} Enum_SocketType;
Return Value
The return value is the socket ID. Otherwise, a value of Enum_SocError is returned. The possible return
values are as follows:
5.9.3.5. Ql_SOC_Close
Prototype
Parameters
socketId:
[In] Socket ID that is returned when calling Ql_SOC_Create.
Return Value
This return value will be SOC_SUCCESS (0) if this function succeeds. Otherwise, a value of
Enum_SocError is returned.
5.9.3.6. Ql_SOC_Connect
This function establishes a socket connection to the host. The host is specified by an IP address and a
port number. This function is used for the TCP client only. The connecting process will take some time,
and the longest time is 75s, which depends on the network quality. When the TCP socket connection
succeeds, the Callback_Socket_Connect callback function will be invoked.
Prototype
Parameters
socketId:
[In] Socket ID that is returned when calling Ql_SOC_Create.
remoteIP:
[In] Peer IPv4 address.
remotePort:
[In] Peer IPv4 port.
Return Value
This return value will be SOC_SUCCESS (0) if this function succeeds. Otherwise, a value of
Enum_SocError is returned.The possible return values are as follows:
5.9.3.7. Ql_SOC_ConnectEx
This function establishes a socket connection to the host. The host is specified by an IP address and a
port number. This function is used for the TCP client only. The connecting processing will take some time,
and the longest time is 75s, which depends on the network quality. After the TCP socket connection
succeeds or fails, this function returns, and the Callback_Socket_Connect callback function will not be
invoked.
Non-blocking Mode
When isBlocking is set to FALSE, this function works under non-blocking mode. The result will be
returned even if the operation is not done, and the result will be reported in callback.
Blocking Mode
When isBlocking is set to TRUE, this function works in blocking mode. The result will be returned only
after the operation is done.
Prototype
Parameters
socketId:
[In] Socket ID that is returned when calling Ql_SOC_Create.
remoteIP:
[In] Peer IPv4 address.
remotePort:
[In] Peer IPv4 port.
isBlocking:
[In] Mode the function works in. TRUE=blocking mode, FALSE=non-blocking mode.
Return Value
This return value will be SOC_SUCCESS (0) if this function succeeds. Otherwise, a value of
Enum_SocError is returned. The possible return values are as follows:
5.9.3.8. Ql_SOC_Send
This function sends data to a host which has already connected previously. It is used for TCP socket only.
If developers call Ql_SOC_Send function to send many data to the socket buffer, this function will return
SOC_WOULDBLOCK. Then developers must stop sending data. After the socket buffer has enough
space, the Callback_Socket_Write function will be called, and developers can continue to send the data.
This function just sends data to the network, but whether the data is received by the server is unknown.
So developers may need to call Ql_SOC_GetAckNumber function to check whether the data has been
received by the server.
Prototype
Parameters
socketId:
[In] Socket ID that is returned when calling Ql_SOC_Create.
pData:
[In] Pointer to the data to be sent.
dataLen:
[In] Number of bytes to send.
Return Value
If no error occurs, Ql_SOC_Send returns the total number of bytes sent, which can be less than the
number requested to be sent by the dataLen parameter. Otherwise, a value of Enum_SocError is
returned.
NOTES
1. The application should call Ql_SOC_Send circularly to send data till all the data in pData are sent out.
If the number of bytes actually sent is less than the number requested to be sent in the dataLen
parameter, the application should keep sending out the left data.
2. If the Ql_SocketSend returns a negative number, but not SOC_WOULDBLOCK, which indicates
some error happened to the socket, the application has to close the socket by calling
Ql_SocketClose and reestablish a connection to the socket. If the return value is
SOC_WOULDBLOCK, embedded application should stop sending data, and wait for the
Ql_Callback_Socket_Write() to be invoked to continue to send data.
5.9.3.9. Ql_SOC_Recv
This function receives the TCP socket data from a connected or bound socket. When the TCP data
comes from the network, the Callback_Socket_Read function will be called. Developers can use
Ql_SOC_Recv to read the data cyclically until it returns SOC_WOULDBLOCK in the callback function.
The Callback_Socket_Read function will be called if the new data is from the network again.
Prototype
Parameters
socketId:
[In] Socket ID that is returned when calling Ql_SOC_Create.
pData:
[Out] Pointer to the buffer that is used to store the received data.
dataLen:
[Out] Length of pData. It is in bytes.
Return Value
If no error occurs, Ql_SOC_Recv returns the total number of bytes received. Otherwise, a value of
Enum_SocError is returned.
NOTES
5.9.3.10. Ql_SOC_GetAckNumber
Prototype
Parameters
socketId:
[In] Socket ID that is returned when calling Ql_SOC_Create.
ackNum:
[Out] Pointer to an u64 data type that is the storage space for the TCP ACK number.
Return Value
If no error occurs, this return value will be SOC_SUCCESS (0). Otherwise, a value of Enum_SocError is
returned.
5.9.3.11. Ql_SOC_SendTo
Prototype
s32 Ql_SOC_SendTo(s32 socketId, u8* pData, s32 dataLen, u32 remoteIP, u16 remotePort)
Parameters
socketId:
[In] Socket ID that is returned when calling Ql_SOC_Create.
pData:
[In] Buffer containing the data to be transmitted.
dataLen:
[In] Length of pData. It is in bytes.
remoteIP:
[In] Pointer to the address of the target socket.
remotePort:
[In] The target port number.
Return Value
If no error occurs, this function returns the number of bytes actually sent. Otherwise, a value of
Enum_SocError is returned.
5.9.3.12. Ql_SOC_RecvFrom
Prototype
s32 Ql_SOC_RecvFrom(s32 socketId, u8* pData, s32 recvLen, u32* remoteIP, u16* remotePort)
Parameters
socketId:
[In] Socket ID that is returned when calling Ql_SOC_Create.
pData:
[Out] Pointer to the buffer that is used to store the received data.
rcvLen:
[Out] Length of pData. It is in bytes.
remoteIP:
[Out] An optional pointer to the buffer that receives the address of the connecting entity.
remotePort:
[Out] An optional pointer to an integer that contains the port number of the connecting entity.
Return Value
If no error occurs, this function returns the number of bytes received. Otherwise, a value of
Enum_SocError is returned.
5.9.3.13. Ql_SOC_Bind
Prototype
Parameters
socketId:
[In] Socket ID that is returned when calling Ql_SOC_Create.
localPort:
[In] Socket local port number.
Return Value
If no error occurs, this function returns SOC_SUCCESS (0). Otherwise, a value of Enum_SocError is
returned.
5.9.3.14. Ql_SOC_Listen
Prototype
Parameters
listenSocketId:
[In] Socket ID that is returned when calling Ql_SOC_Create.
maxClientNum:
[In] Maximum connection number. It limits the maximum length of the request queue. The maximum value
is 5.
Return Value
If no error occurs, this function returns SOC_SUCCESS (0). Otherwise, a value of Enum_SocError is
returned.
5.9.3.15. Ql_SOC_Accept
This function permits an incoming connection attempt on a socket. When the TCP server is started, and
there is a client coming, the Callback_Socket_Accept function will be called. App can call this function in
the Callback_Socket_Accept function to accept the connection request.The socket ID is allocated by the
operating system.
Prototype
Parameters
listenSocketId:
[In] The listen socket ID.
remoteIP:
[Out] An optional pointer to a buffer that receives the address of the connecting entity.
remotePort:
[Out] An optional pointer to an integer that contains the port number of the connecting entity.
Return Value
If no error occurs, this function returns a socket ID, which is greater than or equal to zero. Otherwise, a
value of Enum_SocError is returned.
5.9.3.16. Ql_IpHelper_GetIPByHostName
Prototype
Parameters
contextId:
[In] Module supports two PDP contexts at the same time. It can be 0 or 1
requestId:
[Out] Embedded in response message.
hostname:
[In] The host name.
callback_getIpByName:
[In] This callback is called by Core System to notify whether this function retrieves host IP successfully or
not.
errCode:
[Out] Error code.
ipAddrCnt:
[Out] Get the number of address.
ipAddr:
[Out] The host IPv4 address.
Return Value
If no error occurs, this return value will be SOC_SUCCESS (0). Otherwise, a value of Enum_SocError is
returned. However, if the SOC_WOULDBLOCK is returned, the application will have to wait till the
callback_getipByName is called to know whether this function retrieves host IP successfully or not.
5.9.3.17. Ql_IpHelper_ConvertIpAddr
This function checks whether an IP address is valid or not. If yes, each segment of the IP address string
will be converted into integer to be stored in ipaddr parameter.
Prototype
Parameters
addressstring:
[In] IP address string.
ipaddr:
[Out] Pointer to u32 data type. Each byte stores the IP digit converted from the corresponding IP string.
Return Value
typedef enum
{
SOC_SUCCESS = 0,
SOC_ERROR = -1,
SOC_WOULDBLOCK = -2,
SOC_LIMIT_RESOURCE = -3, //Limited resource
SOC_INVALID_SOCKET = -4, //Invalid socket
SOC_INVALID_ACCOUNT = -5, //Invalid account ID
SOC_NAMETOOLONG = -6, //Address is too long
SOC_ALREADY = -7, //Operation is already in progress
SOC_OPNOTSUPP = -8, //Operation is not supported
SOC_CONNABORTED = -9, //Software caused connection abortion
SOC_INVAL = -10, //Invalid argument
SOC_PIPE = -11, //Broken pipe
SOC_NOTCONN = -12, //Socket is not connected
SOC_MSGSIZE = -13, //MSG is too long
SOC_BEARER_FAIL = -14, //Bearer is broken
SOC_CONNRESET = -15, //TCP half-write close, i.e., FINED
SOC_DHCP_ERROR = -16,
SOC_IP_CHANGED = -17,
SOC_ADDRINUSE = -18,
SOC_CANCEL_ACT_BEARER = -19 //Cancel the activation of bearer
} Enum_SocErrCode;
5.9.5. Example
OpenCPU provides FOTA (Firmware over the Air) function that can upgrade App remotely. This section
defines and describes related API functions, and demonstrates how to program with FOTA.
5.11.1. Usage
This function initializes FOTA related functions. It is a simple API. Programers only need to pass the
simple parameters to this API.
Prototype
Parameters
pFotaCfg:
[In] Pointer to “ST_FotaConfig” struct.
Return Value
5.11.2.2. Ql_FOTA_WriteData
This function writes the delta data of applications to the special space in the module.
Prototype
Parameters
length:
[In] The length of data to write. Unit: byte. Recommend to be 512 bytes
buffer:
[In] Pointer to the data buffer.
Return Value
5.11.2.3. Ql_FOTA_ReadData
This function reads data from the data region which Ql_FOTA_WriteData writes to. If developers need to
check the whole data package after writing, this API can read back the data.
Prototype
Parameters
offset:
[In] The offset value to the data region.
len:
[In] The length of the data to read. Unit: byte. Recommend to be 512 bytes.
pBuffer:
[Out] Pointer to the buffer that is used to store the data read.
Return Value
If the function is executed successfully, the actual number of bytes read will be returned.
5.11.2.4. Ql_FOTA_Finish
This function compares calculated checksum with image checksum in the header after the whole image is
written.
Prototype
s32 Ql_FOTA_Finish(void)
Parameters
Void.
Return Value
5.11.2.5. Ql_FOTA_Update
Prototype
s32 Ql_FOTA_Update(void);
Parameters
Void.
Return Value
5.11.3. Example
return -1;
}
Ql_Debug_Trace("Ql_Fota_Init OK!\r\n");
while(iFileSize > 0)
{
Ql_memset(buf, 0, sizeof(buf));
if (iFileSize <=READ_SIZE)
{
iReadSize=iFileSize;
}
else
{
iReadSize=READ_SIZE;
}
iRet=Ql_FS_Read(hFile, buf, iReadSize, &iReadLen); //Read upgrade data from file system.
if(QL_RET_OK != iRet)
{
Ql_Debug_Trace("Read file failed!(iRet = %x)\r\n", iRet);
return -1;
}
//Write upgrade data to FOTA cache region.
iRet=Ql_FOTA_WriteData(iReadSize,(s8*)buf);
if(QL_RET_OK !=iRet)
{
Ql_Debug_Trace(“Fota write file failed!(iRet=%d)\r\n", iRet);
return -1;
}else
{
s_iSizeRem +=iReadSize;
}
iFileSize -= iReadLen;
Ql_Sleep(5); //Sleep 5ms for outputing catcher log!!!
}
Ql_FS_Close(hFile);
Please refer to example_fota_ftp.c and example_fota_http.c for the complete sample code in
SDK\example\.
The head file ql_trace.h must be included so that the debug functions can be called. All examples in
OpenCPU SDK show the proper usages of these APIs.
5.12.1. Usage
There are two working modes for UART2 (DEBUG port): BASIC_MODE and ADVANCE_MODE.
Developers can configure the working mode of UART2 by the “debugPortCfg” variable in the
custom_sys_cfg.c file.
Under basic mode, application debug messages will be outputted as text through UART2 port. The
UART2 port works as common serial port with RX, TX and GND. In this case, UART2 can be used as
common serial port for application.
Under ADVANCE_MODE, both application debug messages and system debug messages will be
outputted through UART2 port with special format. The “Catcher Tool” provided by Quectel can be used to
capture and analyze these messages. Usually developers do not need to use ADVANCE_MODE without
the requirements from support engineer. If needed, please refer to document
Quectel_Catcher_Operation_UGD for the usage of the special debug mode.
5.12.2.1. Ql_Debug_Trace
This function formats and prints a series of characters and values through the debug serial port (UART2).
Its function is the same as that of standard “sprintf”.
Prototype
Parameters
format:
Pointer to a null-terminated multibyte string that specifies how to interpret the data. The maximum
string length is 512 bytes. Format-control string. A format specification has the following form:
%type:
A character that determines whether the associated argument is interpreted as a character, a string, or a
number.
Return Value
NOTES
1. The string to be printed must not be larger than the maximum number of bytes allowed in buffer.
Otherwise, a buffer overrun can occur.
2. The maximum allowed number of characters to be outputted is 512.
3. To print a 64-bit integer, please first convert it to characters using Ql_sprintf().
OpenCPU RIL related API functions respectively implement the corresponding AT command‟s function.
Developers can simply call APIs to send AT commands and get the response when APIs return.
Developers can refer to document Quectel_OpenCPU_RIL_Application_Note for OpenCPU RIL
mechanism.
NOTE
The APIs defined in this section work normally only after calling Ql_RIL_Initialize(), and
Ql_RIL_Initialize() is used to initialize RIL option after App receives the message MSG_ID_RIL_READY.
5.13.1. AT APIs
The API functions in this section are declared in header file ril.h.
5.13.1.1. Ql_RIL_SendATCmd
This function is used to send AT command with the result being returned synchronously. Before this
function returns, the responses for AT command will be handled in the callback function atRsp_callback,
and the paring results of AT responses can be stored in the space that the parameter userData points to.
All AT responses string will be passed into the callback line by line. So the callback function may be called
for times.
Prototype
Parameter
atCmd:
[In] AT command string.
atCmdLen:
[In] The length of AT command string.
atRsp_callBack:
[In] Callback function for handling the response of AT command.
userData:
[Out] Used to transfer the users‟ parameters.
timeOut:
[In] Timeout for the AT command. Unit: ms. If it is set to 0, RIL uses the default timeout time (3min).
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
If this callback parameter is set to NULL, a default callback function will be called. But the default callback
function only handles the simple AT response. Please refer to Default_atRsp_callback in
ril_atResponse.c.
The following codes are the implementation for default callback function.
This section defines telephony related API functions that are implemented based on OpenCPU RIL.
These APIs imeplement the equivalent functions as AT commands ATD, ATA, ATH.
5.13.2.1. RIL_Telephony_Dial
Prototype
Parameters
type:
[In] Dialing type. It must be 0 and just support voice call.
phoneNumber:
[In] Phone number. Null-terminated string.
result:
[Out] Result for dialing. One value of Enum_CallState.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.13.2.2. RIL_Telephony_Answer
Prototype
Parameters
result:
[Out] Result for dialing. One value of Enum_CallState.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.13.2.3. RIL_Telephony_Hangup
Prototype
s32 RIL_Telephony_Hangup(void);
Parameters
Void.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
This section defines short message related API functions that are implemented bassed on OpenCPU RIL.
These APIs imeplement the same functionas as AT commands AT+CMGR, AT+CMGS, AT+CMGD, etc.
The API functions in this section are declared in ril_sms.h.
5.13.3.1. RIL_SMS_ReadSMS_Text
This function reads a short message of text format with the specified index.
Prototype
Parameters
uIndex:
[In] The SMS index in current SMS storage.
eCharset:
[In] Character set. One value of LIB_SMS_CharSetEnum.
pTextInfo:
[In] Pointer of SMS information of text format.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.13.3.2. RIL_SMS_ReadSMS_PDU
This function reads a short message of PDU format with the specified index.
Prototype
Parameters
index:
[In] SMS index in current SMS storage.
pduInfo:
[In] Pointer of “ST_RIL_SMS_PDUInfo” struct.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.13.3.3. RIL_SMS_SendSMS_Text
Prototype
Parameters
pNumber:
[In] Pointer of phone number.
uNumberLen:
[In] The length of phone number.
eCharset:
[In] Character set. One value of LIB_SMS_CharSetEnum.
pMsg:
[In] Pointer of message content.
uMsgLen:
[In] The length of message content.
pMsgRef:
[Out] Pointer of message reference number.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.13.3.4. RIL_SMS_SendSMS_PDU
Prototype
Parameters
pPDUStr:
[In] Pointer of PDU string.
uPDUStrLen:
[In] The length of PDU string.
pMsgRef:
[Out] Pointer of message reference number.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.13.3.5. RIL_SMS_DeleteSMS
This function deletes one short message or more messages in current SMS storage with the specified
rule.
Prototype
Parameters
index:
[In] The index number of SMS message.
flag:
[In] Delete flag. One value of Enum_RIL_SMS_DeleteFlag.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.13.4.1. RIL_SIM_GetSimState
Prototype
Parameters
state:
[Out] (U)SIM card state code. One value of Enum_SIMState.
Return Value
5.13.4.2. RIL_SIM_GetIMSI
Prototype
Parameters
imsi:
[Out] IMSI number. A string of 15 bytes.
Return Value
5.13.4.3. RIL_SIM_GetCCID
Prototype
Parameters
state:
[Out] CCID number. A string of 20 bytes.
Return Value
5.13.5.1. RIL_NW_GetGSMState
Prototype
Parameters
stat:
[Out] GSM state.
Return Value
Network registeration state code. One value of Enum_NetworkState. -1 indicates failed to get the network
state.
5.13.5.2. RIL_NW_GetGPRSState
Prototype
Parameters
stat:
[Out] GPRS State.
Return Value
Network registeration state code. One value of Enum_NetworkState. -1 indicates failed to get the network
state.
5.13.5.3. RIL_NW_GetSignalQuality
This function gets the signal quality level and bit error rate.
Prototype
Parameters
rssi:
[Out] Signal quality level. 0~31 or 99. 99 indicates the module is not registered on GSM network.
ber:
[Out] Bit error code of the signal.
Return Value
5.13.5.4. RIL_NW_SetGPRSContext
Prototype
Parameters
foregroundContext:
[In] Foreground context. Anumeric indicates which context will be set as foreground context. The range is
0~1.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.13.5.5. RIL_NW_SetAPN
Prototype
Parameters
mode:
[In] Netwrok mode. 0 means CSD, and 1 means GPRS.
apn:
[In] APN string.
userName:
[In] User name for APN.
password:
[In] Password for APN.
Return Value
5.13.5.6. RIL_NW_OpenPDPContext
This function opens/activates the PDP foreground context. The PDP context ID is specified by
RIL_NW_SetGPRSContext().
Prototype
s32 RIL_NW_OpenPDPContext(void);
Parameters
Void.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.13.5.7. RIL_NW_ClosePDPContext
This function closes/deactivates the PDP foreground context. The PDP context ID is specified by
RIL_NW_SetGPRSContext().
Prototype
s32 RIL_NW_ClosePDPContext(void);
Parameters
Void.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.13.5.8. RIL_NW_GetOperator
This function gets the network operator that the module is registered to.
Prototype
Parameters
operator:
[Out] A string with maximum 16 characters, which indicates the network operator that the module
registered to.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.13.6.1. RIL_GetLocation
This function retrieves the longitude and latitude of the current place of the module.
Prototype
Parameters
cb_loc:
Pointer to a callback function that tells the location information.
Return Value
5.13.7.1. Ql_SecureData_Store
This function stores some critical user data to prevent them from losing.
NOTES
1. OpenCPU has been designed with 13 blocks of system storage space to backup critical user data.
Developers may specify the first parameter index [1-13] to specify different storage block. Among
the storage blocks, 1~8 blocks can store 50 bytes for each block, 9~12 blocks can store 100 bytes
for each block, and the 13th block can store 500 bytes.
2. Developers should not call this API function frequently, which is not good for life cycle of flash.
Prototype
Parameters
index:
[In] The index of the secure data block. The range is 1~13.
pData:
[In] The data to be backed up. In groups 1~8, each group can save 50 bytes at most. In groups 9~12,
each group can save 100 bytes at most. If the index of secure data block is 13, the user data can
save 500 bytes at most.
len:
[In] The length of the user data. If the index of secure data block is 1~8, then len ≤ 50. If the index is 9~12,
then len ≤ 100. If the index is 13, then len ≤ 500.
Return Value
5.13.7.2. Ql_SecureData_Read
Prototype
Parameters
index:
[In] The index of the secure data block. The range is 1~13.
len:
[In] The length of the user data. If the index of secure data block is 1~8, then len ≤ 50. If the index is 9~12,
then len ≤ 100. If the index is 13, then len ≤ 500.
Return Value
The return value will be real read length If this function succeeds.
5.13.8.1. RIL_QuerySysInitStatus
Prototype
Parameters
SysInitStatus:
[Out] System initialization status. It can be 0, 1, 2 and 3. One value of Enum_SysInitState. Please refer to
AT+QINISTAT in Quectel_MC60_AT_Commands_Manual for details.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.13.8.2. RIL_GetPowerSupply
Prototype
Parameters
capacity:
[Out] Battery balance. A percentage and ranges from 1~100.
voltage:
[Out] Battery voltage, Unit: mV.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.13.8.3. RIL_GetIMEI
Prototype
Parameters
imei:
[Out] Buffer to store the IMEI number. The length of the buffer should be at least 15 bytes..
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.13.9.1. RIL_AUD_SetChannel
Prototype
Parameters
audChannel:
[Out] Audio channel. See Enum_AudChannel.
Return Value
5.13.9.2. RIL_AUD_GetChannel
Prototype
Parameters
pChannel:
[Out] Audio channel, see Enum_AudChannel.
Return Value
5.13.9.3. RIL_AUD_SetVolume
This function sets the volume level with the specified volume type.
Prototype
Parameters
volType:
[In] Volume type. See Enum_VolumeType.
volLevel:
[In] Volume level.
Return Value
5.13.9.4. RIL_AUD_GetVolume
This function gets the volume level with the specified volume type.
Prototype
Parameters
volType:
[In] Volume type. See Enum_VolumeType.
pvolLevel:
[In] Volume level.
Return Value
5.13.9.5. RIL_AUD_RegisterPlayCB
This function registers a callback function that will be invoked to indicate the playing result.
If developers want to get a feedback (end indication or error code) for playing when calling APIs
RIL_AUD_PlayFile and RIL_AUD_PlayMem, they can call this API to register a callback function before
calling playing API.
Prototype
Parameters
audCB:
[In] The callback function for playing.
errcode:
[Out] Error code for audio playing, which is defined in AT+QAUDPLAY.
Return Value
5.13.9.6. RIL_AUD_PlayFile
Prototype
Parameters
filePath:
[In] Source code file name with file path.
isRepeated:
[In] Repeat play mode.
Return Value
5.13.9.7. RIL_AUD_StopPlay
Prototype
s32 RIL_AUD_StopPlay(void);
Parameters
Void.
Return Value
5.13.9.8. RIL_AUD_PlayMem
Prototype
Parameters
mem_addr:
[In] RAM address of audio data.
mem_size:
[In] Size of audio data.
aud_format:
[In] Audio data format.
repeat:
[In] Play audio data circularly or not.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, or see Enum_ATSndError.
5.13.9.9. RIL_AUD_StopPlayMem
Prototype
s32 RIL_AUD_StopPlayMem(void);
Parameters
Void.
Return Value
5.13.9.10. RIL_AUD_StartRecord
This function starts to record with the spedifed audio format. The recording data will be recorded into the
specified file in UFS.
Prototype
Parameters
fileName:
[In] Name of the file, which is used to store record data.
format:
[In] Recording data format. One value of Enum_AudRecordFormat.
Return Value
5.13.9.11. RIL_AUD_StopRecord
Prototype
s32 RIL_AUD_StopRecord(void);
Parameters
Void.
Return Value
5.13.9.12. RIL_AUD_GetRecordState
Prototype
Parameters
pState:
[Out] Recording state. 0 indicates the recorder is in idle state; 1 indicates the recorder is recording.
Return Value
5.14.1.1. RIL_GPS_Open
Prototype
Parameters
op:
[In] Operation of powering on/off GNSS. 0 means powering off GNSS and 1 means powering on GNSS.
Return Value
5.14.1.2. RIL_GPS_Read
Prototype
Parameters
item:
[In] Pointer to the item to be queried.
rdBuff:
[In] Pointer to the buffer that is used to store the navigation information.
Return Value
Quectel provides a set of API functions to support basic bluetooth operations, including scanning, pairing,
connection and so on.
5.15.1. RIL_BT_Switch
Prototype
Parameters
On_off:
[In] Bluetooth turing on/off. 0 means bluetooth is turned off; 1 means bluetooth is turned on.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.15.2. RIL_BT_GetPwrState
Prototype
Parameters
p_on_off:
[Out] Bluetooth powering on/off. 0 means the bluetooth is powered off; 1 means the bluetooth is powered
on.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.15.3. RIL_BT_Initialize
This fucntion initializes bluetooth, registers callback and updates pairing information after powering on
bluetooth.
Prototype
Parameters
cb:
[In] Callback function to be registered.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.15.4. RIL_BT_SetName
Parameters
name:
[In] Bluetooth name to be set.
len:
[In] Length of parameter name. Unit: byte.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.15.5. RIL_BT_GetName
Prototype
Parameters
name:
[Out] Bluetooth name to be got.
len:
[In] Length of parameter name. Unit: byte.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.15.6. RIL_BT_GetLocalAddr
Prototype
Parameters
ptrAddr:
[Out] Bluetooth local address to be got. The length is 13 bytes including '\0' and is fixed.
len:
[In] Length of parameter ptrAddr. Unit: byte.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.15.7. RIL_BT_SetVisble
Prototype
Parameters
mode:
[In] Visiblity mode. 0 means bluetooth is set to be invisible; 1 means bluetooth is set to be visible forever;
2 means bluetooth is set to be visible temporarily in the time period that bluetooth can be found by
other devices. See Enum_VisibleMode.
timeout:
[In] When mode is set as 2, this parameter decides the the time period that bluetooth can be found by
other devices. Unit: s. The range is 1~255. After timeout, MSG_BT_INVISIBLE will be triggered, and
bluetooth will be set to invisible mode. The parameter will be omitted when it equals to 0 or 1.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.15.8. RIL_BT_GetVisble
Prototype
Parameters
mode:
[Out] Visibility mode. 0 means bluetooth is set to be invisible; 1 means bluetooth is set to be visible forever;
2 means bluetooth is set to be visible temporarily. See Enum_VisibleMode.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.15.9. RIL_BT_StartScan
Prototype
Parameters
maxDevCount:
[In] Maximum number of device supported. The range is 0~20. The default value is 20.
CoD:
[In] The class of device/service. The range is 0~0xFFFFFFFF. The default value is 0.
timeout:
[In] Timeout. The range is 1~255. The default value is 60. Unit: s.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.15.10. RIL_BT_GetDevListInfo
s32 RIL_BT_GetDevListInfo(void);
Parameters
Void.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.15.11. RIL_BT_GetDevListPointer
Prototype
ST_BT_DevInfo ** RIL_BT_GetDevListPointer(void);
Parameters
Void.
Return Value
The return value is the the pointer of the array that stores the device list information.
For example:
ptr = RIL_BT_GetDevListPointer();
ptr[i]->btDevice.devHdl
5.15.12. RIL_BT_StopScan
Prototype
s32 RIL_BT_StopScan(void);
Parameters
Void.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.15.13. RIL_BT_QueryState
This function querys the current state and the updated pairing list of bluetooth.
Prototype
Parameters
status:
[Out] Current bluetooth status. See Enum_BTDevStatus.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.15.14. RIL_BT_PairReq
This function requests to pair a bluetooth device. For paired device, ignore this step and connect it to local
device directely.
Prototype
Parameters
hdIDevice:
[In] Bluetooth handle to be paired.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
5.15.15. RIL_BT_PairConfirm
Prototype
Parameters
accept:
[In] Whether to accept the pairing request. 0 means reject the pairing requiest; 1 means accept the pairing
request.
pinCode:
[In] Passkey used to pair the bluetooth device. If the pairing mode is SSP, this parameter can be omitted.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.15.16. RIL_BT_Unpair
Prototype
Parameters
hdlDevice:
[In] Bluetooth handle to be unpaired.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.15.17. RIL_BT_GetSupportedProfile
This function gets the profile supported by both the local device and the paired bluetooth device.
Parameters
hdlDevice:
[In] Bluetooth handle that supports the profile to be got.
profile_support:
[Out] Supported profile to be got. See Enum_BTProfileId.
len:
[In] Length of the array of the supported profile by both local device and the paired device. Unit: byte.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.15.18. RIL_BT_ConnReq
Prototype
Parameters
hdlDevice:
[In] Bluetooth handle to be connected.
profileId:
[In] Profile type when connecting. See Enum_BTProfileId.
mode:
[In] Connection type. See Enum_BT_SPP_ConnMode.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.15.19. RIL_BT_SPP_DirectConn
This function uses bluetooth address to request a connection to bluetooth device directly, so there is no
need to scan for the device and concern the paring process. This function only supports SPP connection.
Prototype
Parameters
btMacAddr:
[In] Bluetooth device address to be connected.
mode:
[In] Connection type. See Enum_BT_SPP_ConnMode.
pinCode:
[In] Passkey used to pair the bluetooth device.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.15.20. RIL_BT_ConnAccept
Prototype
Parameters
accept:
[In] Whether to accept the connection request. 0 means reject the connection request; 1 means accept
the connection request.
mode:
[In] Connection type. See Enum_BT_SPP_ConnMode.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.15.21. RIL_BT_Disconnect
This function disconnects the local device and the bluetooth device.
Prototype
Parameters
hdlDevice:
[In] Bluetooth handle to be disconnected.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.15.22. RIL_BT_SPP_Send
This function sends data from module to the connected device in SPP mode.
Prototype
Parameters
hdlDevice:
[In] Bluetooth handle to be sent data to.
ptrData:
[In] Pointer to the buffer that is used to store the data to be sent.
lenToSend:
[In] Length of data to be sent. Unit: byte.
actualSend:
[Out] Actual length of data sent. Unit: byte.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.15.23. RIL_BT_SPP_Read
This function reads data sent from module to the connected device in SPP mode.
Prototype
Parameters
hdlDevice:
[In] Bluetooth handle to read data.
ptrBuffer:
[In] Pointer to the buffer that is used to store the data read.
lenToRead:
[In] Length of data to be read. Unit: byte.
actualReadlen:
[Out] Actual length of data read. Unit: byte.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
MC60-OpenCPU (OC: MC60ECA-04-BLE) module adopts dual-mode chip, and supports BT4.0
specification. BT4.0 supports BT3.0 and Bluetooth Low Power (BLE) technology, which is low cost,
short-range and interoperable wireless technology, and uses intelligent means to minimize power
consumption.
The BLE of MC60-OpenCPU (OC: MC60ECA-04-BLE) can only works as a server, and the following APIs
are used for creating a server.
NOTE
BLE APIs can only be used for MC60-OpenCPU (OC: MC60ECA-04-BLE) modules.
Please refer to the example_ble.c for the complete sample codes in SDK\example\.
typedef struct
{
u8 sid;
char gserv_id[32];
s32 result;
ST_BLE_WRreq wrreq_param;
ST_BLE_ConnStatus conn_status;
ST_BLE_Service service_id[SERVICE_NUM];
} ST_BLE_Server;
The parameter gserv_id[32] used for registering a GATT server must be a hex value string (string should
be included in quotation marks). Each character of it should be in set {„0‟~‟9‟,‟a‟~‟f‟,‟A‟~‟F‟}.
5.16.1. RIL_BT_Gatsreg
Prototype
Parameters
op:
[In] Register or deregister a GATT server. 0 means deregister and 1 means register.
gserv:
[In] Pointer to the “ST_BLE_Server” struct.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.16.2. RIL_BT_Gatss
Prototype
Parameters
op:
[In] Add or remove a service. 0 means remove and 1 means add.
gserv:
[In] Pointer to the “ST_BLE_Serve” struct.
typedef struct
{
u8 num_handles;
u8 is_primary;
u8 inst;
u8 transport;
u8 cid;
u8 is_used;
u8 is_started;
u16 service_uuid;
s32 service_handle;
s32 in_service;
ST_BLE_Char char_id[CHAR_NUM];
}ST_BLE_Service;
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.16.3. RIL_BT_Gatsc
Prototype
Parameters
op:
[In] Adding one characteristic at a time is supported and deleting characteristic is not supported currently.
gserv:
[In] Pointer to the “ST_BLE_Server” struct.
typedef struct
{
u8 inst;
u8 is_used;
u16 char_uuid;
s32 char_handle;
u32 permission; //Permission of this characteristic. For more details, please refer to Table 10.
u32 prop; //Properties of this characteristic. For more details, please refer to Table 10.
u32 did;
s32 trans_id;
ST_BLE_Desc desc_id[DESC_NUM];
}ST_BLE_Char;
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.16.4. RIL_BT_Gatsd
Prototype
Parameters
op:
[In] Adding one descriptor at a time is supported and deleting descriptor is not supported currently.
gserv:
[In] Pointer to the “ST_BLE_Server” struct.
typedef struct
{
u8 inst;
u8 is_used;
u16 desc_uuid;
s32 desc_handle;
u32 permission;
}ST_BLE_Desc;
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.16.5. RIL_BT_Gatsst
Prototype
Parameters
op:
[In] Start or stop a service. 0 means stop and 1 means start.
gserv:
[In] Pointer to the “ST_BLE_Server” struct.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.16.6. RIL_BT_Gatsind
Prototype
Parameters
gserv:
[In] Pointer to the “ST_BLE_Server” struct.
typedef struct
{
s32 trans_id;
s32 need_cnf; //Set whether the client needs to reply after receiving data from server. 1 means yes,
and 0 means no.
s32 need_rsp; //Set whether the server needs to reply after receiving data from client. 1 means
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.16.7. RIL_BT_Gatsrsp
Prototype
Parameters
gserv:
[In] Pointer to the “ST_BLE_Server” struct.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.16.8. RIL_BT_Gatsl
Prototype
Parameters
op:
[In] Start or stop advertising. 0 means stop and 1 means start.
gserv:
[In] Pointer to the “ST_BLE_Server” struct.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.16.9. RIL_BT_QBTFMPsreg*
Prototype
Parameters
op:
[In] Register or deregister an FMP service. 0 means deregister and 1 means register.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
NOTE
5.16.10. RIL_BT_QBTPXPsreg*
Prototype
Parameters
op:
[In] Register or deregister a PXP service. 0 means deregister and 1 means register.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
NOTE
5.16.11. RIL_BT_QBTGatadv
Prototype
Parameters
min_interval:
[In] Minimum advertising interval for undirected and low duty cycle directed advertising. The range is
32~16384.
max_interval:
[In] Maximum advertising interval for undirected and low duty cycle directed advertising. The range is
32~16384.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
5.16.12. RIL_BT_Gatcpu
Prototype
Parameters
bt_addr:
[In] Address of the peer device.
min_interval:
[In] Minimum value of the connection interval. The range is 6~3200.
max_interval:
[In] Maximum value of the connection interval. The range is 6~3200.
timeout:
[In] Supervision timeout for the connection. The range is 10~3200.
latency:
[In] Maximum slave latency allowed for the connection specified as the number of connection. The range
is 0~499.
Return Value
RIL_AT_SUCCESS: indicates the AT command is executed successfully, and the response is OK.
RIL_AT_FAILED: indicates failed to execute the AT command or the response is ERROR.
RIL_AT_TIMEOUT: indicates sending AT command timed out.
RIL_AT_BUSY: indicates the AT command is being sent.
RIL_AT_INVALID_PARAM: indicates invalid input parameter.
RIL_AT_UNINITIALIZED: indicates RIL is not ready, and module needs to wait for MSG_ID_RIL_READY
and then call Ql_RIL_Initialize() to initialize RIL.
6 Appendix A References
SN Document Name
[1] Quectel_MC60_AT_Commands_Manual
[2] Quectel_MC60-OpenCPU_Series_Hardware_Design
[3] Quectel_QFlash_User_Guide
[4] Quectel_OpenCPU_FOTA_Application_Note
[5] Quectel_OpenCPU_GCC_Installation_Guide
[6] Quectel_OpenCPU_RIL_Application_Note
[7] Quectel_OpenCPU_Watchdog_Application_Note
[8] Quectel_OpenCPU_Security_Data_Application_Note
Table 9: Abbreviations
Abbreviation Description
ACK Acknowledgement
I/O Input/Output
KB Kilobytes
M2M Machine-to-Machine
MB Megabytes
WTD Watchdog
Default 0
Broadcast 1
Read 2
Write 8
Notify 16
Indicate 32
Signed write 64
Read 1
Write 16