MT2523 API Reference  LinkIt SDK v4
PORT_SERVICE

This section introduces the Serial Port Service APIs including terms and acronyms, supported features, software architecture, details on how to use this module, enums, structures and functions. More...

Overview

This section introduces the Serial Port Service APIs including terms and acronyms, supported features, software architecture, details on how to use this module, enums, structures and functions.

The Serial Port Service APIs can be applied to either UART or USB interface. User can easily switch between different devices to match the board configuration through ATCI commands.

Terms and acronyms

Terms Details
ATCI AT command interface. For more details about the AT command, please refer to Hayes command set in Wikipedia.
NVDM Non-volatile Data Management is a middleware used to store user data.
UART Universal Asynchronous Receiver/Transmitter is usually an individual (or part of an) integrated circuit (IC) used for serial communications over a micro-controller unit (MCU) or peripheral device serial port. For an introduction to UART, please refer to UART in Wikipedia.
USB Universal Serial Bus. USB was designed to standardize the connection of computer peripherals (including keyboards, pointing devices, digital cameras, printers, portable media players, disk drives and network adapters) to personal computers, both to communicate and to supply electric power. It has become commonplace on other devices, such as smartphones, PDAs and video game consoles. USB has effectively replaced a variety of earlier interfaces, such as serial and parallel ports, as well as separate power chargers for portable devices. For more information, please refer to an introduction to the USB in Wikipedia.
OP Operation.

Supported features

Port_Service architecture

The software architecture is shown in the figure below.

port_service_architecture.png

How to use this module

Functions

serial_port_status_t serial_port_open (serial_port_dev_t device, serial_port_open_para_t *para, serial_port_handle_t *handle)
 This function initializes the specific device such as UART or USB. More...
 
serial_port_status_t serial_port_control (serial_port_handle_t handle, serial_port_ctrl_cmd_t command, serial_port_ctrl_para_t *para)
 This function controls a specific device to read or write data or perform other operations supported by the device. More...
 
serial_port_status_t serial_port_close (serial_port_handle_t handle)
 This function de-initializes a specific device after using the device to release the resources for other users. More...
 
serial_port_type_t serial_port_get_device_type (serial_port_dev_t device)
 This function gets the device type of the serial port device. More...
 
serial_port_status_t serial_port_config_dump_dev_number (uint32_t *user_count, serial_port_assign_t *port_assign)
 This function dumps device assign list in NVDM, for example, device UART port0 is assigned to user ATCI. More...
 
serial_port_status_t serial_port_config_read_dev_number (const char *user_name, serial_port_dev_t *device)
 This function reads assigned device for a specific user. More...
 
serial_port_status_t serial_port_config_write_dev_number (const char *user_name, serial_port_dev_t device)
 This function writes new assigned device for a specific user. More...
 
serial_port_status_t serial_port_config_read_dev_setting (serial_port_dev_t device, serial_port_dev_setting_t *dev_setting)
 This function reads current parameters for a specific device. More...
 
serial_port_status_t serial_port_config_write_dev_setting (serial_port_dev_t device, serial_port_dev_setting_t *dev_setting)
 This function writes new parameters for a specific device. More...
 

Modules

 Enums
 
 Structures
 
 Typedef
 

Function Documentation

serial_port_status_t serial_port_close ( serial_port_handle_t  handle)

This function de-initializes a specific device after using the device to release the resources for other users.

Parameters
[in]handleis the serial port handle for the Serial Port Service.
Returns
SERIAL_PORT_STATUS_INVALID_PARAMETER, an invalid parameter error.
SERIAL_PORT_STATUS_FAIL, the serial port device close operation failed.
SERIAL_PORT_STATUS_UNSUPPORTED, the port is not supported.
SERIAL_PORT_STATUS_OK, the close operation completed successfully.
Example
1 Sample code please refers to @ref Port_Service_Usage_Chapter.
serial_port_status_t serial_port_config_dump_dev_number ( uint32_t *  user_count,
serial_port_assign_t port_assign 
)

This function dumps device assign list in NVDM, for example, device UART port0 is assigned to user ATCI.

Apply this API to identify the user for each device.

Parameters
[in,out]user_countspecifies the count of the buffer space pointed to by #port_assign. Upon the return of this function, the value in this parameter means the actual count of the buffer space being used.
[out]port_assignthe serial port assigned to the user.
Returns
SERIAL_PORT_STATUS_FAIL, the function has failed.
SERIAL_PORT_STATUS_OK, the operation completed successfully.
Example
1 uint32_t user_count;
2 serial_port_assign_t serial_port_assign[SERIAL_PORT_DEV_MAX];
3 serial_port_status_t status;
4 status = serial_port_config_dump_dev_number(&user_count, serial_port_assign);
5 if (status != SERIAL_PORT_STATUS_OK)
6  //Error handler
serial_port_status_t serial_port_config_read_dev_number ( const char *  user_name,
serial_port_dev_t device 
)

This function reads assigned device for a specific user.

Parameters
[in]user_nameis an identifier for the user.
[out]deviceis the assigned device for the input user.
Returns
SERIAL_PORT_STATUS_INVALID_PARAMETER, an invalid parameter error.
SERIAL_PORT_STATUS_FAIL, the function has failed.
SERIAL_PORT_STATUS_OK, the operation completed successfully.
Example
1 serial_port_dev_t syslog_port;
2 serial_port_status_t status;
3 status = serial_port_config_read_dev_number("syslog", &syslog_port);
4 if (status != SERIAL_PORT_STATUS_OK)
5  //Error handler
serial_port_status_t serial_port_config_read_dev_setting ( serial_port_dev_t  device,
serial_port_dev_setting_t dev_setting 
)

This function reads current parameters for a specific device.

Parameters
[in]deviceis the device to read its current parameters.
[out]dev_settingis the current parameters for the input device.
Returns
SERIAL_PORT_STATUS_INVALID_PARAMETER, an invalid parameter error.
SERIAL_PORT_STATUS_OK, the operation completed successfully.
Example
1 serial_port_dev_t device;
2 serial_port_status_t status;
3 serial_port_dev_setting_t dev_setting;
4 status = serial_port_config_read_dev_setting(device, &dev_setting);
5 if (status != SERIAL_PORT_STATUS_OK)
6  // Error handler
serial_port_status_t serial_port_config_write_dev_number ( const char *  user_name,
serial_port_dev_t  device 
)

This function writes new assigned device for a specific user.

Parameters
[in]user_nameis an identifier for the user.
[out]deviceis the assigned device for the input user.
Returns
SERIAL_PORT_STATUS_INVALID_PARAMETER, an invalid parameter error.
SERIAL_PORT_STATUS_FAIL, the function has failed.
SERIAL_PORT_STATUS_OK, the operation completed successfully.
Example
1 serial_port_dev_t syslog_port;
2 serial_port_status_t status;
3 syslog_port = SERIAL_PORT_DEV_USB_COM2;
4 status = serial_port_config_write_dev_number("syslog", syslog_port);
5 if (status != SERIAL_PORT_STATUS_OK)
6  //Error handler
serial_port_status_t serial_port_config_write_dev_setting ( serial_port_dev_t  device,
serial_port_dev_setting_t dev_setting 
)

This function writes new parameters for a specific device.

Parameters
[in]deviceis the device to write its new parameters.
[in]dev_settingis the new parameters for the input device.
Returns
SERIAL_PORT_STATUS_INVALID_PARAMETER, an invalid parameter error.
SERIAL_PORT_STATUS_OK, the operation completed successfully.
Example
1 serial_port_dev_t device;
2 serial_port_status_t status;
3 serial_port_dev_setting_t dev_setting;
4 status = serial_port_config_read_dev_setting(device, &dev_setting);
5 if (status != SERIAL_PORT_STATUS_OK)
6  //Error handler
7 status = serial_port_config_write_dev_setting(device, &dev_setting);
8 if (status != SERIAL_PORT_STATUS_OK)
9  //Error handler
serial_port_status_t serial_port_control ( serial_port_handle_t  handle,
serial_port_ctrl_cmd_t  command,
serial_port_ctrl_para_t para 
)

This function controls a specific device to read or write data or perform other operations supported by the device.

This API must be called after serial_port_open(). User should specify the device with serial port handle. User must also define its callback to handle events reported by the Serial Port Service.

Parameters
[in]handleis the serial port handle for the Serial Port Service.
[in]commandis the command corresponding to a specific parameter. For more details about this parameter, please refer to serial_port_ctrl_cmd_t.
[in]paraspecifies the parameter corresponding to a specific command.
Returns
SERIAL_PORT_STATUS_INVALID_PARAMETER, an invalid parameter error.
SERIAL_PORT_STATUS_UNINITIALIZED, the serial port device is uninitialized.
SERIAL_PORT_STATUS_BUSY, the port is busy.
SERIAL_PORT_STATUS_UNSUPPORTED, the port is not supported.
SERIAL_PORT_STATUS_OK, the control operation completed successfully.
Example
1 Sample code, please refer to @ref Port_Service_Usage_Chapter.
serial_port_type_t serial_port_get_device_type ( serial_port_dev_t  device)

This function gets the device type of the serial port device.

Parameters
[in]deviceis the Serial Port Service device.
Returns
SERIAL_PORT_TYPE_UART, the Serial Port Service device type is UART.
SERIAL_PORT_TYPE_USB, the Serial Port Service device type is USB.
Example
1 serial_port_type_t device_type;
2 device_type = serial_port_get_device_type(device);
serial_port_status_t serial_port_open ( serial_port_dev_t  device,
serial_port_open_para_t para,
serial_port_handle_t handle 
)

This function initializes the specific device such as UART or USB.

Parameters
[in]deviceis the Serial Port Service device. For more details about this parameter, please refer to serial_port_dev_t.
[in]paraspecifies the callback function. It cannot be NULL.
[out]handleis the serial port handle for the Serial Port Service.
Returns
SERIAL_PORT_STATUS_INVALID_DEVICE, an invalid device error.
SERIAL_PORT_STATUS_INVALID_PARAMETER, an invalid parameter error.
SERIAL_PORT_STATUS_BUSY, the port is busy.
SERIAL_PORT_STATUS_UNSUPPORTED, the port is not supported.
SERIAL_PORT_STATUS_OK, the initialization completed successfully.
Example
1 serial_port_status_t status;
2 uint32_t g_serial_port_handle = 0;
3 serial_port_open_para_t serial_port_open_para;
4 serial_port_open_para.callback = user_callback;
5 status = serial_port_open(port, &serial_port_open_para, &g_serial_port_handle);
6 if (status != SERIAL_PORT_STATUS_OK)
7  //Error handler
1 void user_callback (serial_port_dev_t device, serial_port_callback_event_t event, void *parameter)
2 {
3  // User code to handle the serial port event
4 }