How to add thirdparty libraries to an ASF4 project.

I had to add MQTT functionality to the TCP server demo from START examples. I found that the mqtt chat example from ASF3 is not available yet on the START platform, so I had to manually add that library to the TCP server example. This how to explains the steps i performed to have it working.

A summary of the process:

  • Copy library files to the project.
  • Update makefile with the new files routes.
  • Resolve routes for include files on the added library.
  • Integrate the library application code to the new application.
  • Resolve variables or missing data.
  • Enjoy.

I did this using Ubuntu 19.04 and the gcc toolchain (no IDE).

Copy library files to the project.

In this case I required to add MQTT capabilities to the winc_tcp_server demo. After download the START example and decomprese the folder structure looks like this:

Original

I created a pahomqtt folder on root example folder.

PahoFolder

Now from the ASF3 sdk I copied the pahomqtt folder contents and pasted on the folder that was just created.

This is how those files look after copied. PahoContents

Update makefile.

This is the trickier part, after the files were copied we need to tell the project that those files were added, so the Makefile need to be updated. Makefile is located on the gcc folder.

Makefile_location

The Makefile generated by the START tool seems messy, so before it is updated we do some manual clean. This is an example on the SUB_DIRS section on how the file looks.

makefile_original

Arrange alfabetically the lines at the SUB_DIRS, OBJS, OBJS_AS_ARGS, DIR_INCLUDES, DEPS_AS_ARGS sections based on files and folders. On each of the previous sections add the paths and files required for the mqtt library.

  • Every folder from the original library must appear on the ASF4 implementation.
  • For each .c file a .o or .d file must appear.
  • include sections must reflect all folders that have a .h file.

SUB_DIRS section.

sub_dirs

OBJ section.

objs

OBJS_AS_ARGS section.

objs_as_args

DIR_INCLUDES section.

dir_includes

DEP_AS_ARGS section.

dep_as_args

Resolve routes on includes.

The copied files were arranged on ASF3 fashion, so when imported some include paths will not be correct. The way I fixed this is to run make on the gcc file and wait for the errors raised at compilation time, some of them will show that the file is unknown. So go there and fix those paths.

Caution, only modify the includes starting with “MQTT”.

Files to modify:

pahomqtt/MQTTClient/MQTT_Client.h
pahomqtt/MQTTClient/Wrapper/mqtt.h

includes_fix

On the mqtt.h file I had to add the since it is not include on the base example and is required for that file to work.

At this point compiling the code must complete correctly.

Integrate the library application code to the new application.

Application files (main.h and main.c) are located on applications folder on root.

  • From the ASF3 mqtt_chat_example I went to main21.c file and copied all the mqtt related code.
  • From the ASF4 tcp_server example, removed all the socket related functions and calls from original example, except the ones inside main().
  • On wifi_cb call mqtt_disconnect() on else if section of STATE_CHANGE.
  • On wifi_cb call mqtt_connect() on DHCP_CONF.
  • Initialize the mqtt drivers by calling configure_mqtt() on main.
  • Change registerSocketCallback parameters with the mqtt socket_event_handle and the socket_resolve_handler

registerSocketCallback(socket_event_handler, socket_resolve_handler);

  • Modify SysTick_config numerator to 480000000 ( need to fix this to correct define)
if (SysTick_Config(48000000/ 1000)) 
{
    printf("ERR>> Systick configuration error\r\n");
    while (1);
}
  • Add on the main loop (while(1)) the mqtt_yield.
if(mqtt_inst.isConnected){
    mqtt_yield(&mqtt_inst, 0);
}

From main.h on ASF3 copy the configuration defines and integrate on the main.h from the ASF4 (on applications folder). Make sure to put the correct mqtt broker ip. Update the WIFI credentials.

TODO

I need to create a video of this process to show the flow and the results. There are some steps that require their own howto:

  • Get a START example up and running.
  • The toolchains availables for working with the SAM microprocesors.
  • How to debug an START applicacion.

Hope to upload these in the next days.

Edén Candelas.

@elmundoverdees.

Written on February 3, 2020