Using The TCPlite Library in MPLAB® Code Configurator

Last modified by Microchip on 2024/01/29 18:03

Objective

This guide shows how to use the MPLAB® Code Configurator (MCC) TCP/IP Lite Stack library to create a TCP client project. With the demo code provided, a TCP connection can be made between your device running the TCP client project and the Microchip TCP/IP Demo Server application running on a PC. This allows the server application to control LEDs on the Curiosity board.

The set-up uses a PIC18F27K42, Curiosity HPC Development board (DM164136) and a MikroE ETH Click (MIKROE-971) with an ENC28J60 Ethernet Controller on-board.

Hardware Tools

Software Tools

Exercise Files

Back to Top

Procedure

Set up the Hardware
  • Connect the ETH Click board to the Curiosity HPC Development board (connector J28).
  • Connect the ENC28J60 on the ETH Click board to an Ethernet network using an Ethernet cable.
  • Connect the Curiosity HPC Board to the computer using the on-board micro-USB connector (connector J12).
the Curiosity HPC Development board
Click image to enlarge.

Back to Top


Create an MPLAB X IDE Project for the PIC18F27K42
  • Choose Project: Standalone Project
  • Select Device: PIC18F27K42
  • Select Tool: Curiosity/Starter Kit (PKOB4)
  • Select Compiler: XC8
  • Select Project Name and Folder.
Project window after the wizard is complete
Click image to enlarge.

Back to Top


Open MCC in MPLAB X IDE
MCC now comes pre-installed with MPLAB X IDE. Use either the MCC logo button near the center of the toolbar or Tools > Embedded > MCC from the main menu.
MCC button on the toolbar
Click image to enlarge.
Here is the Layout of the MCC windows. To learn more about MCC start here.
mcc main view layout
Click image to enlarge.

Back to Top


Configure the System Clock
  • Open the System Module from the Project Resources System menu.
  • Select HFINTOSC from the Oscillator Select drop-down box.
  • Select 8_MHz from the HF Internal Clock drop-down box.
  • Choose a division factor of 1 from the Clock Divider drop-down box.
  • This will result in an 8 MHz system clock.
Open the System Module from the Project Resources System menu
Click image to enlarge.

Back to Top


Load the TCP/IP Lite Library
From the Device Resources > Libraries menu, double-click to add the TCP/IP Lite library.
shows the TCP/IP Lite library
Click image to enlarge.
  • The MAC module from the Ethernet library will be loaded automatically.
  • The Serial Peripheral Interface (SPI) module from the Peripheral library and the SPIMASTER module from the Foundation Services library are loaded automatically to support communication with the Ethernet controller.
  • The TMR1 module from the Peripherals library is loaded automatically to provide the 1-second timer required for the TCP/IP Lite to work.
Project zResouces window showing the libaries loaded
Click image to enlarge.

Back to Top


Configure the TCP/IP Lite Library for a TCP Application

Enable the following protocols by checking the corresponding box:

  • UDP
  • DHCP
  • IPV4
  • TCP
  • ICMP
  • ARP

Although this is a TCP demo project, the UDP module must also be included as both the Dynamic Host Configuration Protocol (DHCP) module and the SysLog protocol call UDP functions. If you want to generate a TCP-only project through MCC, the DHCP module must be excluded and a static IP configuration provided. Furthermore, the files log_syslog.clog_syslog.h must be manually removed from the generated project.

Configure the TCP/IP Lite Library for a TCP Application
Click image to enlarge.

Back to Top


Configure Timer1 Module

The TCP/IP Lite Stack must be serviced every 1 second. Timer1 is used to generate this one-second clock.

  • Open the TMR1 module from the Project Resources > Peripherals menu.
  • In the Hardware Settings pane:
    • Select FOSC/4 from the Clock Source drop-down box.
    • Select 1:1 from the Prescaler drop-down box.
    • Set the Timer Period to 25 ms.
    • Enable Timer Interrupts by checking the box.
  • In the Software Settings pane:
    • Set the Callback Function Rate to 40 x Time Period
Configure Timer1 Module
Click image to enlarge.

Back to Top


Configure SPI Module

The PIC18F27K42 microcontroller communicates with the ENC28J60 Ethernet controller through an SPI. The ENC28J60 SPI supports clock speeds up to 20 MHz and only supports SPI mode 0,0.

  • Open the SPI1 module from the Project Resources > Peripherals menu.
  • In the Hardware Settings pane:
    • Select Master from the Mode drop-down box.
    • Select FOSC from the Clock Source Selection drop-down box.
    • Set the Clock Divider to 0.
Open the SPI1 module from the Project Resources then Peripherals menu.
Click image to enlarge.
  • Open the SPIMASTER module from the Project Resources > Libraries > Foundation Services menu.
  • In the Hardware Settings pane:
    • Select MODE0 from the SPI Mode drop-down box.
    • Select END from the Input Data Sampled At drop-down box.
    • Set the Speed to 8000 kHz.
    • Select SPI1 from the SPI drop-down box.
    • This will result in a 2000 kHz SPI clock.
Open the SPIMASTER module
Click image to enlarge.

Back to Top


Configure MAC Module
  • Open the MAC module from Project Resources > Libraries > Ethernet menu
  • Select the ENC28J60 controller from the drop-down box.
Configure MAC Module
Click image to enlarge.

Back to Top


Configure Pin Module:

From the Pin Manager: Grid View, configure the following pins:

  • SPI1 Module
    • SCK1 – output pin RB1
    • SDI1 – input pin RB2
    • SDO1 – output pin RB3
  • MAC Module
    • ETH_CS – output pin RA3
  • Pin Module
    • GPIO – output pins RA4, RA5, RA6, RA7
    • These pins are connected to LEDs on the Curiosity board.
From the Pin Manager, Grid View, configure the following pins
Click image to enlarge.
Give these General Purpose Input/Output (GPIO) pins custom names as shown.
Give these GPIO pins custom names as below
Click image to enlarge.

Back to Top


Generate Code
  • Click on the Generate button.
  • After the code has been generated, click on the MCC logo again to close the plugin.
Generate Code
Click image to enlarge.

Back to Top


Add Application Files to Project
  • Download the app_files folder containing tcp_client_demo.c and tcp_client_demo.h.
  • Move the app_files folder into the project folder.
  • Add tcp_client_demo.c and tcp_client_demo.h to the project in the Projects tab.
Add Application Files to Project
Click image to enlarge.

Back to Top


Add Code to main.c
  • Open the generated main.c file.
  • Include the following header files:
    • app_files/tcp_client_demo.h
    • mcc_generated_files/TCPIPLibrary/ip_database.h
  • In the main() function:
    • Declare variable IP as type unsigned long.
    • Uncomment the function INTERRUPT_GlobalInterruptEnable() to enable global interrupts.
    • Place TCP_Client_Initialize() before the while loop.
    • Add the following line of code to the while loop:
Network_Manage();
ip = ipdb_getAddress();
if(ip)
{
   TCP_Demo_Client();
}

Back to Top


Modify the server IP address
The PC’s IP address can be found by opening Window’s Command Prompt on your PC and typing the command "ipconfig".
ipconfig results in windows
Click image to enlarge.
  • Open tcp_client_demo.c.
  • In the TCP_Client_Initialize() function:
    • Modify the server IP address with the PC’s IP address from above.
Modify the server IP address with the PC’s IP address
Click image to enlarge.

Back to Top


Program the Board

After connecting the board to the PC with a USB cable, click the Program button to program the board.

Program the Board
Click image to enlarge.

Running the Demo:

  • Open the Java application TCP_Demo.exe. Java must be installed on the PC to run this application.
  • Go to the TCP Server Demo tab and assign the port number as 65534.
  • Click on Listen to wait for a connection from the device.

Go to the TCP Server Demo tab and assign the port number as 65534
After the connection is established:

  • Type text inside the Send text box and click on the Send button. The text sent is displayed inside the Send/Received Databox.

Type text inside the Send text box and click on the Send button

  • In Wireshark, the TCP packets can be viewed by setting the filter to tcp.port == 65534.

the TCP packets can be viewed by setting the filter to tcp.port == 65534

  • Click on the Led buttons {0,1,2,3}. This will toggle LEDs on the Curiosity board.

This will toggle LEDs on the Curiosity board

  • Push the Disconnect button to close the TCP connection. A client disconnected message will appear on the STATUS text box.

Back to Top