This is my eleventh week in AESTE. This week my task is to download a file from the network. There are a lot of protocols can be used to download the file via network. At the end we choose TFTP(Trivial File Transfer Protocol) due to TFTP will transfer maximum 512 bytes data. It will be easier for us to store the data to flash memory (flash memory write by WORD,QWORD, ROW and PAGE). After I had studied the concept of TFTP, I started to write a C program of TFTP (harmony didn’t provide demo application for TFTP) and finally it worked! I had faced some problems during writing this program, here I would like to share some of my experience.

Concept of TFTP:

TFTP is a technology of transferring file between network device, it is simple version of FTP (File Transfer Protocol). TFTP is lightweight compare to FTP. The format of TFTP is very simple, it contains 5 different opcode.

  1. opcode 01- RRQ( Read Request)
  2. opcode 02- WRQ( Write Request)
  3. opcode 03- Data
  4. opcode 04- ACK( Acknowledgment)
  5. opcode 05- ERROR

First of all, we have to send either opcode 01(RRQ) or opcode 02(WRQ). In my project, I use RRQ because my PIC32 will download the new firmware from server side. After server receives the RRQ, it will send the PIC32 data (opcode 03). After the PIC32 receives the data from server, PIC32 send a ACK to server in order to get the next block of data. If the data is less then 512 bytes, it means that is the last block of data.  Furthermore, TFTP will shows a error message “opcode 05” if the file not found. I think this is a good feature for us to do the debug. More info can click here RFC 1350.

Problems and Solutions:

The first part of the code is to open the connection connect with server.

IPV4_ADDR addr = {.Val = 0x0109A8C0};


The IP address for our server is, convert it to hex number is 0xC0A80901. If put in this way, the code will return “invalid socket”. The proper way to write is 0x0109A8C0 due to it has the endian issue.

Image result for endian issues

source from:

After able to connected to server port, I have sent the RRQ and viewed by wireshark. Wireshark is a very good software for network troubleshooting. From the wireshark, I can see the RRQ successfully sent by the PIC32 and server also sent back the DATA block. But PIC32 didn’t send the ACK after the server sent the DATA block. After I did a lot of testing and debug, I noticed that the destination of my ACK is going to port 69. Port 69 is not the destination of the DATA block send by server. When Dr Shawn came, I had discussed this problem with him. Dr Shawn just used a few minutes to read through the tftpc.c (TFTP client library from microchip), he noticed that we should add one more function


This function will match the source port to the destination port. So, it can able to communicate/ transfer the file. Next, the last problem is also about the endian issue. The opcode of DATA should put 0x0300 not 0x0003.


Next week is my last week work at AESTE. I have one more task haven’t finish yet which is swap the program flash after download the new fimware. I hope I am able to finish the last part on time, so I can see the PIC32 download the new firmware and use  the new firmware after restart.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.