
Recently, the DLMS / COSEM protocol has been actively used in metering devices (electricity, heat, water, gas meters) domestically produced. Almost every company specializing in the production of microcontrollers has a certified DLMS / COSEM stack, using which you can reduce the cost and development time of the metering device that supports this protocol. This article focuses on the DLMS / COSEM stack for the MS Instruments MSP430 microcontroller from Texas Instruments.
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 high-security mode is performed using 4-step authentication based on the AES128 algorithm.
- Only LN (long name) addressing is supported.
- One, two, and four byte addressing is supported.
- The following services are supported: GET, SET, GET WITH BLOCK, SET WITH BLOCK, ACTION, as well as selective access for objects of the Profile Generic class.
- The required memory size for storing the stack is 24 KB.
- The required amount of RAM is 1.8kB.
In order to "feel" the stack, we need:
')
- Development environment IAR Embedded Workbench for MSP430;
- DLMS / COSEM client, take the free open source DLMSDirector from the company Gurux;
- Evaluation Board EVM430-F6779;
- MSP-FET430UIF debugger / programmer.
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 its installation, the installation folder will contain the “DLMS_Object” zip folder in which the stack files are located.
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 levels are implemented in this file;
- server_msgs.r43 : The application layer COSEM is implemented in this file;
- config.c : Main configuration file. In this file, you can delete or add parameters for the list of objects (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 the config.c file;
- 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 collected in the project under the name dlms_obj.eww.
Launch of the project
In this section, we will launch a demo project and see how 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 this is MSP430F67791.

We collect the project and we program the controller. Open the DLMSDirector program and add a new device with the following parameters:

Press the "OK" button. Then in the “Devices” tree we select our device, click the “Connect” button and ... we get this error:

It is easily
fixed , we open the
uart_comms.c file of the
dlms_obj.eww project and in line 132 we see that when configuring the UART a “typo” was made:

The correct line should be:
P3SEL0 |=(BIT0|BIT1);
After the correction, the connection with the metering device is successfully established, with the result that the “Read” button becomes available, and in the status line we see “Ready”:

To download information from the metering device, 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 open access, 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 number 1;
- 0.0.41.0.0.255 - displays the so-called SAP destination;
- 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 metering device is as follows:

We can not only find out the time, but also get information about the time zone, the source of clocking, the date and time of daylight saving time and back, and in high secrecy mode, it is possible to set these parameters.
To access the metering device in low privacy mode, you must use the following settings (The default password is 00000000):

In this mode, many more COSEM objects are available:

Adding a new COSEM object
To add a new object, open the file
config.c of the project
dlms_obj.eww , 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 open access mode, and in access mode with a low level of secrecy and in access mode with a high level of secrecy;
- CLASS_ID_DATA - identifier of the interface class, in this case the object belongs to the class Data;
- 0 - class version (0);
- {0, 0, 96, 1, 0, 255} is the logical name of the object;
- 2 - the number of attributes (2);
- Obj_Meter_Sr_No - pointer to attribute list;
- 0 - the number of methods (0);
- NULL - pointer to the list of methods (no methods).
Then we 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 an EEPROM of memory. More information can be obtained from the user manual.
In our case, the object does not have a callback function, and a byte string 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 object. Result:

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