uTasker - Implementing Modbus over TCPIP

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

uTasker - Implementing Modbus over TCPIP

2,247 Views
mpazzi
Contributor III

Hi Mark,

I have downloaded files and I was looking how to use the file about TCPIP/Modbus on my project. I found it a little bit difficult, so I'm asking if you could help me ? I use K66 kinetis on MCUXpresso with RTOS.

I focused on file "MODBUS.c",  "modbus_app.c", "config.h", "packages.h", "types.h" and "drivers.h" but I have a lot of error for missing define.

Thanks so much.

Best Regards

Marco

Labels (1)
10 Replies

1,815 Views
mjbcswitzerland
Specialist V

Hi Marco

The first thing that you need to do is build the project for the K66 with Modbus and TCP/IP. I have attached a 2 minute video showing how to do this from Visual Studio, test that it works and then build in MCUXpresso (the MCUXpresso binary is also attached so that you can verify that it operates on your board).

The reference modbus application is in the file modbus_app.c which sets up some discretes, coils, inputs, holding registers and all details to this and how to modify it to represent a specific project application are in the Modbus user's guide at http://www.utasker.com/docs/MODBUS/uTasker_MODBUS.PDF

As seen in the video (and the attached file) the application is 80k in size and allows 5 concurrent Modbus masters to connect to it (#define MODBUS_SOCKETS_0      5). In fact it also has a web server (supports 4 parallel sessions), Telnet, FTP, parameter and file systems plus Modbus slaves on UART1 (115k RTU) and UART2 (115k ASCII), as well as command line interface on the VCOM and Telnet; the RAM consumption is around 28k total.
K66 [Flash 80k/2Meg / 28k/256k] shows that it is a small-footprint solution allowing large amounts of functionality in addition.

To do the same on any Kinetis board just select the appropriate target (in config.h) and follow the particular IDE setup guide www.utasker.com/kinetis/compilers.html to immediately have operation on virtually any Kinetis part without porting requirements.

From your question it looks like you want to take just the Modbus files and locate them into a different environment (note that if you enable the define RUN_IN_FREE_RTOS the complete operation will  be integrated into a FreeRTOS project instead of pure uTasker OS).
If this is the case you will need to decide which TCP/IP stack you want to use together with it and make any necessary interface modifications (to modbus.c) to hook the two up. Since this would lose compatibility, portability and simulation capability (and generally results in much larger memory requirements and lower performance), with further development effort it is usually advised to use the full environment and simply locate your application there instead.

Regards

Mark

0 Kudos

1,815 Views
mpazzi
Contributor III

Hi Mark,

I'm trying to run the project without succes, also I do not have FRDM-K66, I have only K66 on my board.

So, I would use only modbus because I have already implemented tcpip on my project, so I need to implement modbus over tcp. Which file I have to keep and include to do that ?

Regards

Marco

0 Kudos

1,815 Views
mjbcswitzerland
Specialist V

Hi Marco

All Modbus code is in the two files (in \uTasker\MODBUS)
MODBUS.c
modbus.h

These are the only two needed for Modbus operation, but you will need to supply all routines that are otherwise missing, such as the TCP API and call backs (you will also need to supply missing defines or delete/replace the code using them if not of relevance).
You will also need to call the configuration routines to install and initialise the instances (see the reference Modbus application Applications\uTaskerV1.4\modbus_app.c as reference).

Regards

Mark

1,815 Views
mpazzi
Contributor III

Hello Mark,

I have some question to ask. At the moment the part of my project pertinent to modbus tcp and RTU is not working.

So, if I start to all project there is any document (about to configure linker and so on...) to compile the project for MCUXpresso kinetis K66 ?

Other solution : if I pay for have project modbus/tcp and RTU you give me the only file to insert in my project already to be run  ?

Thanks a lot for your help.

Marco

0 Kudos

1,815 Views
mjbcswitzerland
Specialist V

Hi Marco

If you would like to build the reference project with MCUXpresso you can follow the compiler guide at http://www.utasker.com/kinetis/compilers.html#MCUXpresso
You just need to
1) Import the project to MCUXpresso
2) Select the target (eg. FRDM_K66F) [and options like TCP and Modbus]
3( Check that the compiler is set up for Cortex M4 (best with FPU also for K66)
4) Select the linker script to match the K66 (K_2M_256.ld)
5) Build, load and run.

Originally you wrote that you needed Modbus TCP but now you also mention RTU, which is binary over UART. The TCP operation should be possible in any environment that supplies the OS and TCP/IP stack (with adaptations to interfaces as discussed). The RTU operation is however more difficult since it involves also having suitable UART drivers respecting RTU timing requirements, scheduling and software timer techniques to respond accordingly, which would need more work in an environment that hasn't such integrated.

What you haven't mentioned is which RTOS you are using (if it is FreeRTOS the uTasker project includes integration of Modbus - also RTU - in it). As mentioned before the files that you have contain Modbus TCP, ASCII and RTU support but to adapt these to be able to operate in any environment may require additional diver adaptation as well. First thing that needs to be known is what environment you actually have.

Regards

Mark

0 Kudos

1,815 Views
mpazzi
Contributor III

Hi Mark,

thanks and sorry for missing some information or not exactly mentioned, but I'm quite new about RTOS, Modbus and TCP.

So, I'm using FreeRTOS, MCUXpresso and I have my board with MK66FX1M0. I have already implemted TCP echo example and it work fine.

Now I have to implement Modbus over TCP and Modbus RTU.

Regards

Marco

0 Kudos

1,815 Views
mjbcswitzerland
Specialist V

Marco

- Which TCP/IP stack do you use? What other functionality do you need apart from Modbus TCP over Ethernet? (eg. web server, FTP, Telnet, MQTT etc.)
- Do you use RMII or MII connection to the PHY and which PHY?
- Which crystal frequency do you use and how is the Ethernet clock generated?

Regards

Mark

0 Kudos

1,815 Views
mpazzi
Contributor III

Hi Mark,

The TCP/IP stack is  'lwip'  the one of Nxp Kinetis.

I need only Modbus TCP over Ethernet and Modbus RTU.

The connection interface between micro and LAN8740 is MII.

The crystal frequency on LAN8740 is 25MHz

Regards

Marco

0 Kudos

1,815 Views
mjbcswitzerland
Specialist V

Hi Marco

I think that you may be complicating things by using LWIP since the uTasker TCP/IP stack that is integrated into the framework has all the features (and much more) that you need and works immediately with Modbus TCP, plus it works with FreeRTOS (as does the the Modbus RTU interface).

If I understand the reasoning for LWIP it is that you have it operating with your HW, using MII and the LAN8740, whereby it is also easy to use your HW with the uTasker Ethernet because it is a question of configuration. A small amount of time to configure it so that you have a complete solution will almost certainly be much faster than the present route.

If you would like to get the project with TCP/IP, Modbus, web server, telnet, FTP etc. (you can reduce the features as a second simple step) you just need to get the LAN8740 operating. This is how it is possible:

1) In app_hw_kinetis.h find the FRDM_66F Ethernet configuration (which is of course for the Micrel KSZ8081RNA in RMII mode)
2) Now change it to this configuration instead

    #define _LAN8740
    #define PHY_ADDRESS            0x00                                  // address of PHY (MII mode)
    #define VNDR_MDL               0x011                                 // vendor model number
    #define MDL_REV                0x0                                   // model revision number
    #define PHY_IDENTIFIER         (0x0007c000 | (VNDR_MDL << 4) | MDL_REV) // SMSC identifier
    #define MII_MANAGEMENT_CLOCK_SPEED    2500000                        // typ. 2.5MHz Speed
    #define PHY_POLL_LINK                                                // activate polling of the link state
    #define INTERRUPT_TASK_PHY     TASK_NETWORK_INDICATOR                // link status reported to this task (do not use together with LAN_REPORT_ACTIVITY)
    #define FNRESETPHY()                                                 // dummy

3) I never used this PHY with Kinetis parts but have with STM32 parts (but in RMII mode) so I have the settings for polling its link state (since I don't know how you have connected its interrupt).
In kinetis_ENET.h you can add the following just before the line with

#elif defined _KSZ8081RNA || defined _KSZ8051RNL                         // {84}{102}

#elif defined _LAN8720 || defined _LAN8740 || defined _LAN8742
    #define PHY_INTERRUPT_REGISTER (0x1d)
    #define PHY_LINK_STATE_CHANGE  (0x0050)                              // auto-negotiation complete or link-down
    #define PHY_LINK_STATUS_REG    (0x1f)
    #define PHY_LINK_MASK          (0x0014)
    #define PHY_LINK_10BASE_T_FULL_DUPLEX   0x0014
    #define PHY_LINK_100BASE_TX_FULL_DUPLEX 0x0018
    #define PHY_LINK_100BASE_TX_HALF_DUPLEX 0x0008
    #define PHY_LINK_10BASE_T_HALF_DUPLEX   0x0004

4) You will also need to verify that the PHY connections are correct. It will do this but you may also need to connect a clock [ENET_1588_CLKIN?] (it depends on the HW which I haven't seen).
In fnConfigEthernet() in kinetis_ENET.h

    _CONFIG_PERIPHERAL(B, 0, PB_0_MII0_MDIO);                            // MII0_MDIO on PB.0 (alt. function 4/8)
    _CONFIG_PERIPHERAL(B, 1, PB_1_MII0_MDC);                             // MII0_MDC on PB.1 (alt. function 4/8)
    _CONFIG_PERIPHERAL(A, 5, PA_5_MII0_RXER);                            // MII0_RXER on PA.5 (alt. function 4)
    _CONFIG_PERIPHERAL(A, 12, PA_12_MII0_RXD1);                          // MII0_RXD1 on PA.12 (alt. function 4/5)
    _CONFIG_PERIPHERAL(A, 13, PA_13_MII0_RXD0);                          // MII0_RXD0 on PA.13 (alt. function 4/5)
    _CONFIG_PERIPHERAL(A, 14, PA_14_RMII0_CRS_DV);                       // RMII0_CRS_DV/ MII0_RXDV on PA.14 (alt. function 4/5)
    _CONFIG_PERIPHERAL(A, 15, PA_15_MII0_TXEN);                          // MII0_TXEN on PA.15 (alt. function 4/5)
    _CONFIG_PERIPHERAL(A, 16, PA_16_MII0_TXD0);                          // MII0_TXD0 on PA.16 (alt. function 4/5)
    _CONFIG_PERIPHERAL(A, 17, PA_17_MII0_TXD1);                          // MII0_TXD1 on PA.17 (alt. function 4/5)
    _CONFIG_PERIPHERAL(A, 9,  PA_9_MII0_RXD3);                           // MII0_RXD3 on PA.9  (alt. function 4/5)
    _CONFIG_PERIPHERAL(A, 10, PA_10_MII0_RXD2);                          // MII0_RXD2 on PA.10 (alt. function 4/5)
    _CONFIG_PERIPHERAL(A, 11, PA_11_MII0_RXCLK);                         // MII0_RXCLK on PA.11 (alt. function 4/5)
    _CONFIG_PERIPHERAL(A, 24, PA_24_MII0_TXD2);                          // MII0_TXD2 on PA.24 (alt. function 4/5)
    _CONFIG_PERIPHERAL(A, 25, PA_25_MII0_TXCLK);                         // MII0_TXCLK on PA.25 (alt. function 4/5)
    _CONFIG_PERIPHERAL(A, 26, PA_26_MII0_TXD3);                          // MII0_TXD3 on PA.26 (alt. function 4/5)
    _CONFIG_PERIPHERAL(A, 27, PA_27_MII0_CRS);                           // MII0_CRS  on PA.27 (alt. function 4/5)
    _CONFIG_PERIPHERAL(A, 28, PA_28_MII0_TXER);                          // MII0_TXER on PA.28 (alt. function 4/5)
    _CONFIG_PERIPHERAL(A, 29, PA_29_MII0_COL);                           // MII0_COL  on PA.29 (alt. function 4/5)

It may work first time but it may also need some further adjustment, but it should be possible to identify if there is still something not matching.

If this step is possible you will then have complete FreeRTOS, Ethernet TCP/IP with Modbus TCP and Modbus RTU. You will then be able to work with the rest of the project that you already have too.

Good luck

Regards

Mark

0 Kudos

1,815 Views
mpazzi
Contributor III

Hi Mark,

thanks a lot for your help.

Regards

0 Kudos