
Introducing the DLMS / COSEM Stack for Texas Instruments MSP430 Microcontrollers

Recently, the DLMS / COSEM protocol has been actively used in metering devices (meters of electric energy, heat, water, gas) of domestic production. Almost every company specializing in the production of microcontrollers has a certified DLMS / COSEM stack, which can be used to reduce the cost and development time of the meter that supports this protocol. This article will focus on the DLMS / COSEM stack for Texas Instruments MSP430 microcontrollers.
The TI DLMS / COSEM stack has the following features:
- All COSEM interface classes are supported.
- Three levels of association are supported: open access (no security), low security (low security) and high security (high security). Access to the metering device in a high level of secrecy mode is carried out using 4-step authentication based on the AES128 algorithm.
- Only LN (long name) addressing is supported.
- One, two and four byte addressing are supported.
- The services GET, SET, GET WITH BLOCK, SET WITH BLOCK, ACTION, as well as selective access for objects of the Profile Generic class are supported.
- The required memory size for stack storage is 24 kB.
- The required RAM size is 1.8kB.
In order to "touch" the stack we need:
- IAR Embedded Workbench for MSP430 Development Environment
- DLMS / COSEM client, take a free open source DLMSDirector from Gurux;
- Evaluation Board EVM430-F6779;
- Debugger / programmer MSP-FET430UIF.
Download and unpack
The DLMS / COSEM stack is available at ( http://www.ti.com/tool/dlmsobj-eval ), you must have a TI account to download it. The stack itself is packaged in a distribution called DLMS-4.0.6-windows-installer . After installing it, the zip-folder “DLMS_Object” in which the stack files are located will be in the installation folder.
The library consists of the following files:
- iec62056_demo.c : In this file, the entire periphery of the microcontroller and the HDLC state machine are initialized;
- uart_comms.c : Configuration file for the UART module;
- iec62056_link.r43 : HDLC and MAC layers are implemented in this file;
- server_msgs.r43 : This file implements the COSEM application layer;
- config.c : The main configuration file. In this file, you can delete or add parameters for the object list;
- app_server_msgs.c : Functions are written in this file to extract data from memory and provide it to the DLMS library;
- config.h : Macros and function prototypes for config.c;
- cosem.h : Definitions of all constants used in the COSEM application layer;
- iec_62056_link.h : Macros and function prototypes for the HDLC level;
All these files are already compiled into a project called dlms_obj.eww.
Launch of the project
In this part, we will launch a demo project and see how the COSEM objects are represented. To do this, open the dlms_obj.eww file in IAR for MSP430 and select the required microcontroller, in our case it is MSP430F67791.

We assemble the project and program the controller. We open the DLMSDirector program and add a new device with the following parameters:

Click OK. Then in the tree 'Devices' "choose our device, press the button," the Connect "and ... here we get this error:

Corrected it easy to open the file uart_comms.c project dlms_obj.eww in line 132 we see that the UART configuration was made" typo " : The

correct line should be:
P3SEL0 |=(BIT0|BIT1);
After correction, the connection with the meter is successfully established, as a result of which the “Read” button becomes available, and in the status line we see “Ready”:

To download information from the meter, press the “Read” button. This process is not fast, so you have to wait a bit. As a result, we get a tree from COSEM objects:

In this stack, in the public domain, by default, five objects are displayed:
- 0.0.1.0.0.255 - displays the current time in the meter;
- 0.0.40.0.0.255 - displays information about the current association;
- 0.0.40.0.1.255 - displays information about the association No. 1;
- 0.0.41.0.0.255 - displays the so-called SAP assignment;
- 0.0.42.0.0.255 - displays the logical name of the device, in fact - the serial number of the counter.
For example, information about the current time in the meter is presented as follows:

We can not only find out the time, but also get information about the time zone, the source of the clock, the date and time of daylight saving time and back, and in high security mode, it’s possible set these parameters.
To access the meter in low security mode, you need to use the following settings (The default password is 00000000):

In this mode, much more COSEM objects are available:

Adding a New COSEM Object
To add a new object, open the config.c file of the dlms_obj.eww project , find the structure:
const struct object_desc_s object_list[]
and add the following line to it:
{ASSOC_PC_MR_US, CLASS_ID_DATA, 0, { 0, 0, 96, 1, 0, 255}, 2, Obj_Meter_Sr_No, 0, NULL}
Where:
- ASSOC_PC_MR_US - determines the visibility of the object, in this case the object will be visible both in the open access mode, and in the access mode with a low security level and in the access mode with a high security level;
- CLASS_ID_DATA - interface class identifier, in this case, the object belongs to the Data class;
- 0 - class version (0);
- {0, 0, 96, 1, 0, 255} - the logical name of the object;
- 2 - number of attributes (2);
- Obj_Meter_Sr_No - a pointer to a list of attributes;
- 0 - number of methods (0);
- NULL - a pointer to a list of methods (no methods).
Then create a structure with a list of attributes in the same file:
static const struct attribute_desc_s Obj_Meter_Sr_No[] =
{
{1, ACCESS_PCR__MRR__USR_, TAG_OCTET_STRING, (void *) object_list[11].instance_id, NULL},
{2, ACCESS_PCR__MRR__USR_, TAG_OCTET_STRING, (void *) Meter_Sr_No, NULL},
};
Where:
- The first parameter is the attribute number;
- The second parameter is access rights;
- The third parameter is the attribute data type;
- The fourth parameter is a pointer to the data;
- The fifth parameter is the callback function. This function is called when data needs to be taken, for example, from the EEPROM memory. More information can be obtained from the user manual.
In our case, the object does not have a callback function, and a string of bytes is used as the data type.
Meter_Sr_No points to the following structure:
const uint8_t Meter_Sr_No[] =
{
8, 'A','B','C','D','1','2','3','4'
};
That's all the procedures for creating a new facility. Result:

Conclusion
This article does not provide a complete description of the DLMS / COSEM library for microcontrollers of the MSP430 family, since it is difficult to do this without highlighting the main points of the protocol. However, those who need such a description can familiarize themselves with it by downloading it from the TI website ( http://www.ti.com/tool/dlmsobj-eval ).