building_arch.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. Building for specific architectures
  2. -----------------------------------
  3. The open62541 library can be build for many operating systems and embedded systems.
  4. This document shows a small excerpt of already tested architectures. Since the stack is only using the
  5. C99 standard, there are many more supported architectures.
  6. A full list of implemented architecture support can be found in the arch folder.
  7. Windows, Linux, MacOS
  8. ^^^^^^^^^^^^^^^^^^^^^
  9. These architectures are supported by default and are automatically chosen by CMake.
  10. Have a look into the previous sections on how to do that.
  11. freeRTOS + LwIP
  12. ^^^^^^^^^^^^^^^
  13. Credits to `@cabralfortiss <https://github.com/cabralfortiss>`_
  14. This documentation is based on the discussion of the PR https://github.com/open62541/open62541/pull/2511. If you have any doubts, please first check the discussion there.
  15. This documentation assumes that you have a basic example using LwIP and freeRTOS that works fine, and you only want to add an OPC UA task to it.
  16. There are two main ways to build open62541 for freeRTOS + LwIP:
  17. - Select the cross compiler in CMake, set the flags needed for compilation (different for each microcontroller so it can be difficult) and then run make in the folder and the library should be generated. This method can be hard to do because you need to specify the include files and some other configurations.
  18. - Generate the open6254.h and open6254.c files with the freeRTOSLWIP architecture and then put these files in your project in your IDE that you're using for compiling. This is the easiest way of doing it and the documentation only focus on this method.
  19. In CMake, select freertosLWIP using the variable UA_ARCHITECTURE, enable amalgamation using the UA_ENABLE_AMALGAMATION variable and just select the native compilers. Then try to compile as always. The compilation will fail, but the open62541.h and open62541.c will be generated.
  20. NOTE: If you are using the memory allocation functions from freeRTOS (pvPortMalloc and family) you will need also to set the variable UA_ARCH_FREERTOS_USE_OWN_MEMORY_FUNCTIONS to true. Many users had to implement pvPortCalloc and pvPortRealloc.
  21. If using the terminal, the command should look like this
  22. .. code-block:: bash
  23. mkdir build_freeRTOS
  24. cd build_freeRTOS
  25. cmake -DUA_ARCHITECTURE=freertosLWIP -DUA_ENABLE_AMALGAMATION=ON ../
  26. make
  27. Remember, the compilation will fail. That's not a problem, because you need only the generated files (open62541.h and open62541.c) found in the directory where you tried to compile. Import these in your IDE that you're using.
  28. There is no standard way of doing the following across all IDEs, but you need to do the following configurations in your project:
  29. - Add the open62541.c file for compilation
  30. - Add the variable UA_ARCHITECTURE_FREERTOSLWIP to the compilation
  31. - Make sure that the open62541.h is in a folder which is included in the compilation.
  32. When compiling LwIP you need a file called lwipopts.h. In this file, you put all the configuration variables. You need to make sure that you have the following configurations there:
  33. .. code-block:: c
  34. #define LWIP_COMPAT_SOCKETS 0 // Don't do name define-transformation in networking function names.
  35. #define LWIP_SOCKET 1 // Enable Socket API (normally already set)
  36. #define LWIP_DNS 1 // enable the lwip_getaddrinfo function, struct addrinfo and more.
  37. #define SO_REUSE 1 // Allows to set the socket as reusable
  38. #define LWIP_TIMEVAL_PRIVATE 0 // This is optional. Set this flag if you get a compilation error about redefinition of struct timeval
  39. For freeRTOS there's a similar file called FreeRTOSConfig.h. Usually, you should have an example project with this file. The only two variables that are recommended to check are:
  40. .. code-block:: c
  41. #define configCHECK_FOR_STACK_OVERFLOW 1
  42. #define configUSE_MALLOC_FAILED_HOOK 1
  43. Most problems when running the OPC UA server in freeRTOS + LwIP come from the fact that is usually deployed in embedded systems with a limited amount of memory, so these definitions will allow checking if there was a memory problem (will save a lot of effort looking for hidden problems).
  44. Now, you need to add the task that will start the OPC UA server.
  45. .. code-block:: c
  46. static void opcua_thread(void *arg){
  47. //The default 64KB of memory for sending and receicing buffer caused problems to many users. With the code below, they are reduced to ~16KB
  48. UA_UInt32 sendBufferSize = 16000; //64 KB was too much for my platform
  49. UA_UInt32 recvBufferSize = 16000; //64 KB was too much for my platform
  50. UA_UInt16 portNumber = 4840;
  51. UA_Server* mUaServer = UA_Server_new();
  52. UA_ServerConfig *uaServerConfig = UA_Server_getConfig(mUaServer);
  53. UA_ServerConfig_setMinimal(uaServerConfig, portNumber, 0, sendBufferSize, recvBufferSize);
  54. //VERY IMPORTANT: Set the hostname with your IP before starting the server
  55. UA_ServerConfig_setCustomHostname(uaServerConfig, UA_STRING("192.168.0.102"));
  56. //The rest is the same as the example
  57. UA_Boolean running = true;
  58. // add a variable node to the adresspace
  59. UA_VariableAttributes attr = UA_VariableAttributes_default;
  60. UA_Int32 myInteger = 42;
  61. UA_Variant_setScalarCopy(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  62. attr.description = UA_LOCALIZEDTEXT_ALLOC("en-US","the answer");
  63. attr.displayName = UA_LOCALIZEDTEXT_ALLOC("en-US","the answer");
  64. UA_NodeId myIntegerNodeId = UA_NODEID_STRING_ALLOC(1, "the.answer");
  65. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME_ALLOC(1, "the answer");
  66. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  67. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  68. UA_Server_addVariableNode(mUaServer, myIntegerNodeId, parentNodeId,
  69. parentReferenceNodeId, myIntegerName,
  70. UA_NODEID_NULL, attr, NULL, NULL);
  71. /* allocations on the heap need to be freed */
  72. UA_VariableAttributes_clear(&attr);
  73. UA_NodeId_clear(&myIntegerNodeId);
  74. UA_QualifiedName_clear(&myIntegerName);
  75. UA_StatusCode retval = UA_Server_run(mUaServer, &running);
  76. UA_Server_delete(mUaServer);
  77. }
  78. In your main function, after you initialize the TCP IP stack and all the hardware, you need to add the task:
  79. .. code-block:: c
  80. //8000 is the stack size and 8 is priority. This values might need to be changed according to your project
  81. if(NULL == sys_thread_new("opcua_thread", opcua_thread, NULL, 8000, 8))
  82. LWIP_ASSERT("opcua(): Task creation failed.", 0);
  83. And lastly, in the same file (or any actually) add:
  84. .. code-block:: c
  85. void vApplicationMallocFailedHook(){
  86. for(;;){
  87. vTaskDelay(pdMS_TO_TICKS(1000));
  88. }
  89. }
  90. void vApplicationStackOverflowHook( TaskHandle_t xTask, char *pcTaskName ){
  91. for(;;){
  92. vTaskDelay(pdMS_TO_TICKS(1000));
  93. }
  94. }
  95. And put a breakpoint in each of the vTaskDelay. These functions are called when there's a problem in the heap or the stack. If the program gets here, you have a memory problem.
  96. That's it. Your OPC UA server should run smoothly. If not, as said before, check the discussion in https://github.com/open62541/open62541/pull/2511. If you still have problems, ask there so the discussion remains centralized.