Embedded World 2021 - Infineon ModusToolbox PSoC 6 Drone 

 

Summary

In lesson 3 we will add a task to manage the “Cloud”, this is IoT after all.  We will start by adding the connection to the WiFi network using the AnyCloud Wireless Connection Manager within ModusToolbox.  At the end of lesson three you will have the “Cloud Task” colored green – well actually partially green.  Here is the architecture:

Learning Objectives

  1. AnyCloud Wireless Libraries
  2. Architecture of the PSoC 6 + 43xxxx WiFi Combo
  3. Wireless Connection Manager

Procedure

In this lesson we will follow these steps:

  1. Create a new project by copying lesson 2
  2. Add the WiFi Libraries
  3. Copy the wireless configuration templates files into your project
  4. Look at the Wireless Library Documentation
  5. Update the Makefile
  6. Add cloud_task.h
  7. Add cloud_task.c
  8. Update main.c
  9. Test

1. Create a new project by copying lesson 2

Start the new project creator and select the CY8CPROTO-062-4343W

Click the import button.  Then select your previous project and press open.

Select your new template and give it a reasonable name.

Now program your project to make sure that things are still working.

Here is the serial terminal.

2. Add the WiFi Libraries

Start up the library manager and go to the Libraries tab.  Click on wireless connection manager.  Notice that it will bring in a bunch of other libraries that it depends on.

3. Copy the wireless configuration templates into your project

MBED TLS and LWIP have about a billion options.  We provide a template for those options which you will need to copy into your project.  The templates are located in the wifi-mw-core library in the “config” directory.  Copy and paste them into your project.

Here is a clip of the lwip configuration options.  I wouldn’t recommend changing anything unless you are sure you know what you are doing.

4. Look at the Wireless Library Documentation

All of the Infineon libraries will have a directory called “doc” and inside of that directory there will be a file called api_reference_manual.html

When you open the file you will  see our doxygen generated documentation.  Here the WiFi connection manager documentation.

5. Update the Makefile

The makefile controls all of the libraries.  To enable these libraries you need to add the following “components” to the list if they aren’t already there. (Note that all of this information is in the Quick Start section of the library’s documentation).

COMPONENTS= FREERTOS LWIP MBEDTLS

And you need to tell the system about the configuration.

DEFINES+= MBEDTLS_USER_CONFIG_FILE='"mbedtls_user_config.h"' 
DEFINES+=CYBSP_WIFI_CAPABLE
DEFINES+=CY_RTOS_AWARE

6. Add the cloud_task.h

Just like the other lessons you will need to create a public header file for the cloud task.

Inside of that file there will be only one function prototype for the task.

#pragma once

void cloud_task(void *param);

7. Add the cloud_task.c

Make the cloud_task.c file to add the cloud infrastructure to your project.

At the top of the file we will have some includes.  And I will define some macros that tell WiFi which AP to attach to. When you do this on your own, you will need to update those to match your WiFi AP SSID and password. The task itself is really simple for now, just connect to WiFi and then do nothing.

#include <stdio.h>

#include "FreeRTOS.h"
#include "task.h"

#include "cy_wcm.h"

#include "cloud_task.h"

#define CLOUD_WIFI_AP        "ew2021"
#define CLOUD_WIFI_PW        "ew2021ap"
#define CLOUD_WIFI_SECURITY  CY_WCM_SECURITY_WPA2_MIXED_PSK
#define CLOUD_WIFI_BAND      CY_WCM_WIFI_BAND_ANY

static void cloud_connectWifi();

void cloud_task(void* param)
{
    (void)param;

    cloud_connectWifi();

    while(1)
    {
        vTaskDelay(1);

    }
}

The connect to WiFi function will

  1. Setup the Access Point information
  2. Initialize the wireless connection manager
  3. Then go into a loop that will attempt to connect until there is success.
  4. Once a connection is made, it will print out the IP information.
static void cloud_connectWifi()
{
    cy_rslt_t result;

    cy_wcm_connect_params_t connect_param = {
        .ap_credentials.SSID = CLOUD_WIFI_AP,
        .ap_credentials.password = CLOUD_WIFI_PW,
        .ap_credentials.security = CLOUD_WIFI_SECURITY,
        .BSSID = {0},
        .band = CLOUD_WIFI_BAND,
    };
    cy_wcm_config_t config = {.interface = CY_WCM_INTERFACE_TYPE_STA}; // We are a station (not a Access Point)

    cy_wcm_init(&config); // Initialize the connection manager

    printf("\nWi-Fi Connection Manager initialized.\n");

    do
    {
        cy_wcm_ip_address_t ip_address;

        printf("Connecting to Wi-Fi AP '%s'\n", connect_param.ap_credentials.SSID);
        result = cy_wcm_connect_ap(&connect_param, &ip_address);

        if (result == CY_RSLT_SUCCESS)
        {
            printf("Successfully connected to Wi-Fi network '%s'.\n",
                    connect_param.ap_credentials.SSID);

            // Print IP Address
            if (ip_address.version == CY_WCM_IP_VER_V4)
            {
                printf("IPv4 Address Assigned: %d.%d.%d.%d\n", (uint8_t)ip_address.ip.v4,
                        (uint8_t)(ip_address.ip.v4 >> 8), (uint8_t)(ip_address.ip.v4 >> 16),
                        (uint8_t)(ip_address.ip.v4 >> 24));
            }
            else if (ip_address.version == CY_WCM_IP_VER_V6)
            {
                printf("IPv6 Address Assigned: %0X:%0X:%0X:%0X\n", (unsigned int)ip_address.ip.v6[0],
                        (unsigned int)ip_address.ip.v6[1], (unsigned int)ip_address.ip.v6[2],
                        (unsigned int)ip_address.ip.v6[3]);
            }
            break; /* Exit the for loop once the connection has been made */
        }
        else
        {
            printf("WiFi Connect Failed Retrying\n");
            vTaskDelay(2000); // wait 2 seconds and try again;
        }

    } while (result != CY_RSLT_SUCCESS);
}

8. Update main.c

In main.c you need to include the cloud task

#include "cloud_task.h"

Then start the cloud task.

xTaskCreate(cloud_task,    "Cloud"     ,configMINIMAL_STACK_SIZE*8  , NULL, 2, 0);  

9. Test

Program your project and make sure that it connects, and the CapSense and joystick still work.

Resources for Project

You can find this projects in your project creator dialog by filtering for “IoT Expert Embedded”.  This is lesson3

You can also clone this project at git@github.com:iotexpert/ew21-lesson3.git or https://github.com/iotexpert/ew21-lesson3

Recommended Posts

No comment yet, add your voice below!


Add a Comment

Your email address will not be published.