<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Search Results for &#8220;create component&#8221; &#8211; IoT Expert</title>
	<atom:link href="https://iotexpert.com/search/create+component/feed/rss2/" rel="self" type="application/rss+xml" />
	<link>https://iotexpert.com</link>
	<description>Engineering for the Internet of Things</description>
	<lastBuildDate>Sat, 19 Jun 2021 14:04:21 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://iotexpert.com/wp-content/uploads/2017/01/cropped-Avatar-32x32.jpg</url>
	<title>Search Results for &#8220;create component&#8221; &#8211; IoT Expert</title>
	<link>https://iotexpert.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>AnyCloud WiFi Template + WiFi Helper Library (Part 2): Enable the WiFi Network</title>
		<link>https://iotexpert.com/anycloud-wifi-template-wifi-helper-library-part-2-enable-the-wifi-network/</link>
					<comments>https://iotexpert.com/anycloud-wifi-template-wifi-helper-library-part-2-enable-the-wifi-network/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Mon, 28 Jun 2021 14:03:42 +0000</pubDate>
				<category><![CDATA[AnyCloud]]></category>
		<category><![CDATA[CY8CKIT-062S2-43012]]></category>
		<category><![CDATA[WIFI]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=11285</guid>

					<description><![CDATA[Summary Instructions on using the AnyCloud Wireless Connection manager to enable WiFi.  This article is Part 2 of a series that will build a new IoT Expert template project for WiFi. Story In the last article I got things going by starting from the old template, fixing up the Visual Studio Code configuration, adding the [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>Instructions on using the AnyCloud Wireless Connection manager to enable WiFi.  This article is Part 2 of a series that will build a new IoT Expert template project for WiFi.</p>
<p><span><p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >Article</th>
</tr>
</thead>
<tbody>
<tr><td >(Part 1) Create Basic Project &amp; Add Cypress Logging Functionality</td>
</tr>

<tr><td >(Part 2) Create New Thread to manage WiFi using the Wireless Connection Manager</td>
</tr>

<tr><td >(Part 3) Create a New Middleware Library with WiFi helper functions</td>
</tr>

<tr><td >(Part 4) Add WiFi Scan</td>
</tr>

<tr><td >Add WiFi Connect</td>
</tr>

<tr><td >Add WiFi Disconnect</td>
</tr>

<tr><td >Add WiFi Ping</td>
</tr>

<tr><td >Add Gethostbyname</td>
</tr>

<tr><td >Add MDNS</td>
</tr>

<tr><td >Add Status</td>
</tr>

<tr><td >Add StartAP</td>
</tr>

<tr><td >Make a new template project (update manifest)</td>
</tr>
</tbody></table></div></p></span></p>
<h1>Story</h1>
<p>In the last article I got things going by starting from the old template, fixing up the Visual Studio Code configuration, adding the new Cypress Logging functionality and then testing everything.</p>
<p>In this article I will</p>
<ol>
<li>Create new task to manage WiFi</li>
<li>Add Wireless Connection Manager to the project</li>
<li>Create wifi_task.h and wifi_task.c</li>
<li>Update usrcmd.c to send commands to the WiFi task</li>
</ol>
<h1>Create New Task to Manage WiFi</h1>
<p>I am going to start by creating new a new task (called wifi_task) that will be responsible for managing the WiFi connection.  In Visual Studio Code you can create a new file by pressing the little document with the + on it.  You will need a file &#8220;wifi_task.h&#8221; and one &#8220;wifi_task.c&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=11286" rel="attachment wp-att-11286"><img fetchpriority="high" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.32.14-PM-1024x1007.png" alt="" width="1024" height="1007" class="alignnone size-large wp-image-11286" srcset="https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.32.14-PM-1024x1007.png 1024w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.32.14-PM-300x295.png 300w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.32.14-PM-768x755.png 768w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.32.14-PM-600x590.png 600w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.32.14-PM.png 1068w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Once you have wifi_task.h you will need to add the function prototype for the wifi_task.  In addition add a &#8220;guard&#8221;.  I like to use &#8220;#pragma once&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=11287" rel="attachment wp-att-11287"><img decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.33.07-PM-1024x423.png" alt="" width="1024" height="423" class="alignnone size-large wp-image-11287" srcset="https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.33.07-PM-1024x423.png 1024w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.33.07-PM-300x124.png 300w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.33.07-PM-768x317.png 768w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.33.07-PM-1536x635.png 1536w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.33.07-PM-2048x846.png 2048w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.33.07-PM-600x248.png 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Here is copyable code.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">#pragma once
void wifi_task(void *arg);
</pre>
<p>In wifi_task.c Ill start with a simple blinking LED function.  Well actually it will do a print instead of a blink.  Here it is:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include "wifi_task.h"
#include "FreeRTOS.h"
#include "task.h"
#include &lt;stdio.h&gt;

void wifi_task(void *arg)
{
    while(1)
    {
        vTaskDelay(1000);
        printf("blink\n");

    }
}</pre>
<p>Now that I have a wifi_task (which doesn&#8217;t do much) lets update main.c.  First include the wifi_task.h</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include "wifi_task.h"</pre>
<p>Then create the task.  Notice that I start with a pretty big stack.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">xTaskCreate(wifi_task,   "WiFi"       , configMINIMAL_STACK_SIZE*20,0 /* args */ ,0 /* priority */, 0);
</pre>
<p>When you run this, you will have the &#8220;blink&#8221; interleaved with the blink from the last article.</p>
<p><a href="https://iotexpert.com/?attachment_id=11288" rel="attachment wp-att-11288"><img decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.43.08-PM-1024x801.jpg" alt="" width="1024" height="801" class="alignnone size-large wp-image-11288" srcset="https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.43.08-PM-1024x801.jpg 1024w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.43.08-PM-300x235.jpg 300w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.43.08-PM-768x601.jpg 768w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.43.08-PM-1536x1202.jpg 1536w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.43.08-PM-600x470.jpg 600w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.43.08-PM.jpg 1876w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Add Wireless Connection Manager</h1>
<p>You next step is to add the wireless connection manager.  Start the library browser by running &#8220;make modlibs&#8221;.  Then click on the wifi-connection-manager&#8221;.  Notice that when you do that, it will bring in a bunch of other libraries as well.  These are all libraries that it (the WiFi-Connection-Manager) is depend on.</p>
<p><a href="https://iotexpert.com/?attachment_id=11289" rel="attachment wp-att-11289"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.47.17-PM-784x1024.jpg" alt="" width="784" height="1024" class="alignnone size-large wp-image-11289" srcset="https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.47.17-PM-784x1024.jpg 784w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.47.17-PM-230x300.jpg 230w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.47.17-PM-768x1003.jpg 768w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.47.17-PM-600x784.jpg 600w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-12.47.17-PM.jpg 1130w" sizes="(max-width: 784px) 100vw, 784px" /></a></p>
<p>If you look in the wireless connection manager documentation you will find this nice note.  It says that the WCM uses the Cypress logging functionality and you can turn it on with a #define.  That&#8217;s cool.  So now I edit the Makefile and add the define.</p>
<p><a href="https://iotexpert.com/?attachment_id=11335" rel="attachment wp-att-11335"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-19-at-8.40.43-AM-1024x232.png" alt="" width="1024" height="232" class="alignnone size-large wp-image-11335" srcset="https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-19-at-8.40.43-AM-1024x232.png 1024w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-19-at-8.40.43-AM-300x68.png 300w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-19-at-8.40.43-AM-768x174.png 768w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-19-at-8.40.43-AM-1536x349.png 1536w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-19-at-8.40.43-AM-2048x465.png 2048w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-19-at-8.40.43-AM-600x136.png 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>The the documentation also says that this library depends on the &#8220;Wi-Fi Middleware Core&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=11337" rel="attachment wp-att-11337"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-19-at-8.41.35-AM-1024x108.png" alt="" width="1024" height="108" class="alignnone size-large wp-image-11337" srcset="https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-19-at-8.41.35-AM-1024x108.png 1024w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-19-at-8.41.35-AM-300x32.png 300w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-19-at-8.41.35-AM-768x81.png 768w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-19-at-8.41.35-AM-1536x162.png 1536w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-19-at-8.41.35-AM-2048x216.png 2048w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-19-at-8.41.35-AM-600x63.png 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>If you go to the Wi-Fi Middleware core library documentation you will see instructions that say that you need to</p>
<ol>
<li>Enable &amp; Configure LWIP</li>
<li>Enable &amp; Configure MBEDTLS</li>
<li>Enable &amp; Configure the Cypress RTOS Abstraction</li>
</ol>
<p>In order to do that you will need to two things</p>
<ol>
<li>Copy the configuration files into your project</li>
<li>Setup some options in the Makefile</li>
</ol>
<p>Start by copying the file.  They give you default configurations in mtb_share/wifi-mw-core/version/configs.  You will want to copy those files into your project.  This can be done in the Visual Studio Code interface using ctrl-c and ctrl-v</p>
<p><a href="https://iotexpert.com/?attachment_id=11290" rel="attachment wp-att-11290"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-1.28.18-PM-737x1024.png" alt="" width="737" height="1024" class="alignnone size-large wp-image-11290" srcset="https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-1.28.18-PM-737x1024.png 737w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-1.28.18-PM-216x300.png 216w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-1.28.18-PM-768x1068.png 768w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-1.28.18-PM-600x834.png 600w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-1.28.18-PM.png 948w" sizes="(max-width: 737px) 100vw, 737px" /></a></p>
<p>Notice that I now have two FreeRTOSConfig files.  So, delete the original file and rename the copied file.</p>
<p><a href="https://iotexpert.com/?attachment_id=11291" rel="attachment wp-att-11291"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-1.30.13-PM.png" alt="" width="948" height="390" class="alignnone size-full wp-image-11291" srcset="https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-1.30.13-PM.png 948w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-1.30.13-PM-300x123.png 300w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-1.30.13-PM-768x316.png 768w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-1.30.13-PM-600x247.png 600w" sizes="(max-width: 948px) 100vw, 948px" /></a></p>
<p>Now your project should look like this:</p>
<p><a href="https://iotexpert.com/?attachment_id=11292" rel="attachment wp-att-11292"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-1.31.12-PM.png" alt="" width="962" height="920" class="alignnone size-full wp-image-11292" srcset="https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-1.31.12-PM.png 962w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-1.31.12-PM-300x287.png 300w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-1.31.12-PM-768x734.png 768w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-1.31.12-PM-600x574.png 600w" sizes="(max-width: 962px) 100vw, 962px" /></a></p>
<p>The next step is to fix the Makefile by adding some defines.</p>
<p>&nbsp;</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">DEFINES=CY_RETARGET_IO_CONVERT_LF_TO_CRLF
DEFINES+=CYBSP_WIFI_CAPABLE CY_RTOS_AWARE
DEFINES+=MBEDTLS_USER_CONFIG_FILE='"mbedtls_user_config.h"'
DEFINES+=ENABLE_WIFI_MIDDLEWARE_LOGS
</pre>
<p>The add the required components</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">COMPONENTS=FREERTOS LWIP MBEDTLS PSOC6HAL
</pre>
<h1>Update wifi_task.c</h1>
<p>My wifi task is going to work by</p>
<ol>
<li>Sitting on Queue waiting for messages of &#8220;wifi_cmd_t&#8221;</li>
<li>When those messages come in, execute the right command.</li>
</ol>
<p>Start by adding some includes to the wifi_task.c</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include &lt;stdio.h&gt;
#include "wifi_task.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"

#include "cy_wcm.h"
</pre>
<p>Then define the legal commands.  I will add a bunch of more commands in the future articles.  But for this article there will be only one command.  Enable.  The command message is</p>
<ol>
<li>The command</li>
<li>Three args of unknown type</li>
</ol>
<p>In addition you will require</p>
<ol>
<li>The command queue</li>
<li>An initialize state variable</li>
<li>A way to keep track of what mode you are in (AP, STA or APSTA)</li>
</ol>
<pre class="EnlighterJSRAW" data-enlighter-language="c">typedef enum {
    WIFI_CMD_ENABLE,

} wifi_cmd_t;

typedef struct {
    wifi_cmd_t cmd;
    void *arg0;
    void *arg1;
    void *arg2;
} wifi_cmdMsg_t;


static QueueHandle_t wifi_cmdQueue;
static bool wifi_initialized=false;
static cy_wcm_interface_t wifi_network_mode;</pre>
<p>The first &#8220;command&#8221; that I will create is the enable.  This will</p>
<ol>
<li>Setup the interface</li>
<li>Initialize the WiFi.  The simple init command actually does a bunch of stuff, including powering on the wifi chip, downloading the firmware into it, setting up all of the tasks in the RTOS, enabling the LWIP and MBEDTLS</li>
</ol>
<pre class="EnlighterJSRAW" data-enlighter-language="c">static void wifi_enable(cy_wcm_interface_t interface)
{
    cy_rslt_t result;
    cy_wcm_config_t config = {.interface = interface}; 
    result = cy_wcm_init(&amp;config); // Initialize the connection manager
    CY_ASSERT(result == CY_RSLT_SUCCESS);

    result = cy_wcm_register_event_callback(wifi_network_event_cb);
    CY_ASSERT(result == CY_RSLT_SUCCESS);

    wifi_network_mode = interface;
    wifi_initialized = true;
    
    printf("\nWi-Fi Connection Manager initialized\n");
    
}
</pre>
<p>In the previous block of code notice that I register a callback.  The callback looks like a switch that prints out messages based on the event type.  Notice that there are three lines which are commented out &#8211; which we will fix in the next article.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">static void wifi_network_event_cb(cy_wcm_event_t event, cy_wcm_event_data_t *event_data)
{
    cy_wcm_ip_address_t ip_addr;

    switch(event)
    {
        case CY_WCM_EVENT_CONNECTING:            /**&lt; STA connecting to an AP.         */
            printf("Connecting to AP ... \n");
        break;
        case CY_WCM_EVENT_CONNECTED:             /**&lt; STA connected to the AP.         */
            printf("Connected to AP and network is up !! \n");
        break;
        case CY_WCM_EVENT_CONNECT_FAILED:        /**&lt; STA connection to the AP failed. */
            printf("Connection to AP Failed ! \n");
        break;
        case CY_WCM_EVENT_RECONNECTED:          /**&lt; STA reconnected to the AP.       */
            printf("Network is up again! \n");
        break;
        case CY_WCM_EVENT_DISCONNECTED:         /**&lt; STA disconnected from the AP.    */
            printf("Network is down! \n");
        break;
        case CY_WCM_EVENT_IP_CHANGED:           /**&lt; IP address change event. This event is notified after connection, re-connection, and IP address change due to DHCP renewal. */
                cy_wcm_get_ip_addr(wifi_network_mode, &amp;ip_addr, 1);
   //             printf("Station IP Address Changed: %s\n",wifi_ntoa(&amp;ip_addr));
        break;
        case CY_WCM_EVENT_STA_JOINED_SOFTAP:    /**&lt; An STA device connected to SoftAP. */
 //           printf("STA Joined: %s\n",wifi_mac_to_string(event_data-&gt;sta_mac));
        break;
        case CY_WCM_EVENT_STA_LEFT_SOFTAP:      /**&lt; An STA device disconnected from SoftAP. */
//            printf("STA Left: %s\n",wifi_mac_to_string(event_data-&gt;sta_mac));
        break;
    }

}</pre>
<p>Now I want to update the main loop of the WiFI task.  It is just an infinite loop that processes command messages (from other tasks).</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">void wifi_task(void *arg)
{
    wifi_cmdQueue = xQueueCreate(10,sizeof(wifi_cmdMsg_t));

    wifi_cmdMsg_t msg;

    while(1)
    {
        xQueueReceive(wifi_cmdQueue,&amp;msg,portMAX_DELAY);
        switch(msg.cmd)
        {
            case WIFI_CMD_ENABLE:
                printf("Received wifi enable message\n");
                wifi_enable((cy_wcm_interface_t)msg.arg0);
            break;

        }
    }
}</pre>
<p>In the other tasks in the system you &#8220;COULD&#8221; create a message and submit it to the queue.  I always think that it is easier if you create a function which can be called in the other threads.  Here is the wifi_enable function.  This function takes a char * of either &#8220;STA&#8221;, &#8220;AP&#8221;, or &#8220;APSTA&#8221; and then submits the right message to the queue.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">bool wifi_cmd_enable(char *interface)
{
    wifi_cmdMsg_t msg;
    msg.cmd = WIFI_CMD_ENABLE;
    msg.arg0 = (void *)CY_WCM_INTERFACE_TYPE_STA;

    if(strcmp(interface,"STA") == 0)
        msg.arg0 = (void *)CY_WCM_INTERFACE_TYPE_STA;

    else if(strcmp(interface,"AP") == 0)
        msg.arg0 = (void *)CY_WCM_INTERFACE_TYPE_AP;

    else if(strcmp(interface,"APSTA") == 0)
        msg.arg0 = (void *)CY_WCM_INTERFACE_TYPE_AP_STA;
    
    else
    {
        printf("Legal options are STA, AP, APSTA\n");
        return false;
    }

    xQueueSend(wifi_cmdQueue,&amp;msg,0);
    return true;
}
</pre>
<p>Once I have the nice function for the other tasks, I add it to the public interface in wifi_task.h</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#pragma once
#include &lt;stdbool.h&gt;
void wifi_task(void *arg);
bool wifi_cmd_enable(char *interface);</pre>
<h1>Add a new user command &#8220;net&#8221;</h1>
<p>Now that I have the wifi_task setup I want to add a &#8220;net&#8221; command to the command line shell.  Start by adding the include.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include "wifi_task.h"</pre>
<p>Then create a function prototype for a new command.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">static int usrcmd_net(int argc, char **argv);
</pre>
<p>Add the command to the list of commands that the shell knows.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">static const cmd_table_t cmdlist[] = {
    { "help", "This is a description text string for help command.", usrcmd_help },
    { "info", "This is a description text string for info command.", usrcmd_info },
    { "clear", "Clear the screen", usrcmd_clear },
    { "pargs","print the list of arguments", usrcmd_pargs},
#ifdef configUSE_TRACE_FACILITY 
#if configUSE_STATS_FORMATTING_FUNCTIONS ==1
    { "tasks","print the list of RTOS Tasks", usrcmd_list},
#endif
#endif
    { "net","net [help,enable]",usrcmd_net},
};
</pre>
<p>Then create the net command.  I want a BUNCH of net commands.  They will include help, enable, connect, disconnect, &#8230;. but for now we will start with enable.  This function just calls the wifi_enable command that we added to the wifi_task.h interface.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">static int usrcmd_net(int argc, char **argv)
{
    if(argc == 1 || strcmp("help",argv[1]) == 0)
    {
        printf("net [help,enable,connect,disconnect,mdns,scan,ping,lookup]\n");

        printf("%-35s %s\n","net enable","Enable the WiFi Driver &amp; load the WiFi Firmware");

        return 0;
    }

    if(strcmp("enable",argv[1])==0)
    {
            if(argc == 2)
                wifi_cmd_enable("STA");
            else
                wifi_cmd_enable(argv[2]);        
            return 0;
    }

    return 0;
}
</pre>
<h1>Test</h1>
<p>Program and test.  Now the &#8220;net enable&#8221; works.  Notice that it gives you the output about the wifi firmware being loaded into the chip.  Then it tells you that the chip is enabled and the connection manager is rolling.</p>
<p><a href="https://iotexpert.com/?attachment_id=11294" rel="attachment wp-att-11294"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-2.22.57-PM-1024x467.png" alt="" width="1024" height="467" class="alignnone size-large wp-image-11294" srcset="https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-2.22.57-PM-1024x467.png 1024w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-2.22.57-PM-300x137.png 300w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-2.22.57-PM-768x350.png 768w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-2.22.57-PM-1536x700.png 1536w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-2.22.57-PM-600x274.png 600w, https://iotexpert.com/wp-content/uploads/2021/06/Screen-Shot-2021-06-15-at-2.22.57-PM.png 1746w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>In the next article I will create a new library of helper functions for wifi.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/anycloud-wifi-template-wifi-helper-library-part-2-enable-the-wifi-network/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Tilt Hydrometer (Part 9) An LCD Display</title>
		<link>https://iotexpert.com/tilt-hydrometer-part-9-an-lcd-display/</link>
					<comments>https://iotexpert.com/tilt-hydrometer-part-9-an-lcd-display/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Mon, 22 Mar 2021 14:07:13 +0000</pubDate>
				<category><![CDATA[Tilt Hydrometer Data Collection]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=10424</guid>

					<description><![CDATA[Summary This article updates the Tilt Hydrometer project to include an LCD display using the Segger emWin library.  This includes a discussion of a &#8220;new&#8221; library in the suite of IoT Expert awesome sauce.  Specifically the IoT Expert Graphics library. Here is the architecture with the blocks we are editing marked green. Story I knew [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>This article updates the Tilt Hydrometer project to include an LCD display using the Segger emWin library.  This includes a discussion of a &#8220;new&#8221; library in the suite of IoT Expert awesome sauce.  Specifically the IoT Expert Graphics library.</p>
<p>This series is broken up into the following 12 articles with a few additional possible articles. </p>
<p><a href="https://iotexpert.com/tilt-hydrometer-overview-out-of-box-part-1/" target="_blank" rel="noopener">Tilt Hydrometer (Part 1) Overview &amp; Out-of-Box</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-architecture-part-2/" target="_blank" rel="noopener">Tilt Hydrometer (Part 2) Architecture</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-advertising-scanner-part-3/">Tilt Hydrometer (Part 3) Advertising Scanner</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-4-advertising-packet-error/" target="_blank" rel="noopener">Tilt Hydrometer (Part 4) Advertising Packet Error?</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-5-tilt-simulator-multi-advertising-ibeacons/">Tilt Hydrometer (Part 5) Tilt Simulator &amp; Multi Advertising iBeacons</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-6-tilt-simulator-an-upgrade/" target="_blank" rel="noopener">Tilt Hydrometer (Part 6) Tilt Simulator an Upgrade</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-7-advertising-database/">Tilt Hydrometer (Part 7) Advertising Database</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-8-read-the-database/">Tilt Hydrometer (Part 8) Read the Database</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-9-an-lcd-display/">Tilt Hydrometer (Part 9) An LCD Display</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-10-the-display-state-machine/">Tilt Hydrometer (Part 10) The Display State Machine</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-11-draw-the-display-screens/">Tilt Hydrometer (Part 11) Draw the Display Screens</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-12-capsense/">Tilt Hydrometer (Part 12) CapSense</a></p>
<p>Tilt Hydrometer: LittleFS &amp; SPI Flash (Part ?)</p>
<p>Tilt Hydrometer: WiFi Introducer (Part ?)</p>
<p>Tilt Hydrometer: WebServer (Part ?)</p>
<p>Tilt Hydrometer: Amazon MQTT (Part ?)</p>
<p>Tilt Hydrometer: Printed Circuit Board (Part ?)</p>
<p>You can get the source code from git@github.com:iotexpert/Tilt2.git  This repository has tags for each of the articles which can be accessed with "git checkout part12"  You can find the Tilt Simulator at  git@github.com:iotexpert/TiltSimulator.git.</p>
<p>&nbsp;</p>
<p>Here is the architecture with the blocks we are editing marked green.</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-9-an-lcd-display/part9/" rel="attachment wp-att-10514"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/12/part9-1024x514.png" alt="" width="1024" height="514" class="alignnone size-large wp-image-10514" srcset="https://iotexpert.com/wp-content/uploads/2020/12/part9-1024x514.png 1024w, https://iotexpert.com/wp-content/uploads/2020/12/part9-300x151.png 300w, https://iotexpert.com/wp-content/uploads/2020/12/part9-768x385.png 768w, https://iotexpert.com/wp-content/uploads/2020/12/part9-1536x771.png 1536w, https://iotexpert.com/wp-content/uploads/2020/12/part9-600x301.png 600w, https://iotexpert.com/wp-content/uploads/2020/12/part9.png 1614w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Story</h1>
<p>I knew that I wanted to use the TFT Display so that I could have this Tilt Hydrometer system sitting in my &#8220;brewery&#8221;.  Notice in the picture below you can see that the screen is currently displaying the data from the &#8220;black&#8221; tilt.  Also notice that you can see two fermenters in the background in the brewery.  Who needs a guest bathroom in the era of Covid <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-9-an-lcd-display/img_0321/" rel="attachment wp-att-10473"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/12/IMG_0321-675x1024.jpg" alt="" width="675" height="1024" class="alignnone size-large wp-image-10473" srcset="https://iotexpert.com/wp-content/uploads/2020/12/IMG_0321-675x1024.jpg 675w, https://iotexpert.com/wp-content/uploads/2020/12/IMG_0321-198x300.jpg 198w, https://iotexpert.com/wp-content/uploads/2020/12/IMG_0321-768x1164.jpg 768w, https://iotexpert.com/wp-content/uploads/2020/12/IMG_0321-1013x1536.jpg 1013w, https://iotexpert.com/wp-content/uploads/2020/12/IMG_0321-1351x2048.jpg 1351w, https://iotexpert.com/wp-content/uploads/2020/12/IMG_0321-600x910.jpg 600w, https://iotexpert.com/wp-content/uploads/2020/12/IMG_0321-scaled.jpg 1689w" sizes="(max-width: 675px) 100vw, 675px" /></a></p>
<p>Also notice that there is a Raspberry Pi (more on that in a future article).  And for all of you married people out there, yes I have a very frustrated but forgiving wife.</p>
<h1>Add Display Libraries &amp; Fix the Makefile</h1>
<p>The first step in building this is to add the new libraries.  Specifically the display-tft-st7789v library which knows how to talk to the lcd and the emWin library.  AND the new IoT Expert Graphics Library.</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-9-an-lcd-display/screen-shot-2020-12-02-at-5-56-14-am/" rel="attachment wp-att-10432"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-02-at-5.56.14-AM-1019x1024.png" alt="" width="1019" height="1024" class="alignnone size-large wp-image-10432" srcset="https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-02-at-5.56.14-AM-1019x1024.png 1019w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-02-at-5.56.14-AM-298x300.png 298w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-02-at-5.56.14-AM-150x150.png 150w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-02-at-5.56.14-AM-768x772.png 768w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-02-at-5.56.14-AM-300x300.png 300w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-02-at-5.56.14-AM-600x603.png 600w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-02-at-5.56.14-AM-100x100.png 100w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-02-at-5.56.14-AM.png 1116w" sizes="(max-width: 1019px) 100vw, 1019px" /></a></p>
<p>In order to use the emWin library you need to fix the Makefile to include the emWin component.  Notice that I chose OS  meaning an RTOS and NTS meaning no touch screen.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">COMPONENTS=FREERTOS WICED_BLE EMWIN_OSNTS</pre>
<h1>IoT Expert Graphics Library</h1>
<p>I wanted to have a splash screen on this project with my Logo.  So I started digging around on my computer to find the graphics, which I found in the file <span>IOTexpert_Logo_Vertical.png.  Unfortunately this is a 1091&#215;739 portable network graphics file, not exactly what emWin wants to draw with.  To fix this I opened it in GIMP</span></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-9-an-lcd-display/screen-shot-2020-12-06-at-7-32-57-am/" rel="attachment wp-att-10475"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.32.57-AM-1024x558.jpg" alt="" width="1024" height="558" class="alignnone size-large wp-image-10475" srcset="https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.32.57-AM-1024x558.jpg 1024w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.32.57-AM-300x163.jpg 300w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.32.57-AM-768x418.jpg 768w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.32.57-AM-1536x836.jpg 1536w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.32.57-AM-600x327.jpg 600w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.32.57-AM.jpg 2046w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Then I resized it to my screen size (320&#215;240)</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-9-an-lcd-display/screen-shot-2020-12-06-at-7-34-40-am/" rel="attachment wp-att-10476"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.34.40-AM.png" alt="" width="768" height="596" class="alignnone size-full wp-image-10476" srcset="https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.34.40-AM.png 768w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.34.40-AM-300x233.png 300w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.34.40-AM-600x466.png 600w" sizes="(max-width: 768px) 100vw, 768px" /></a></p>
<p>Now it looks like this:</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-9-an-lcd-display/screen-shot-2020-12-06-at-7-34-51-am/" rel="attachment wp-att-10477"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.34.51-AM-1024x561.png" alt="" width="1024" height="561" class="alignnone size-large wp-image-10477" srcset="https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.34.51-AM-1024x561.png 1024w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.34.51-AM-300x164.png 300w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.34.51-AM-768x420.png 768w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.34.51-AM-1536x841.png 1536w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.34.51-AM-600x328.png 600w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.34.51-AM.png 2046w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Then I saved it as a PNG.  Still doesn&#8217;t work with emWin.  But, if you look in the emWin library you will notice that there is a new directory called &#8220;Tool&#8221;</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-9-an-lcd-display/screen-shot-2020-12-06-at-7-36-40-am/" rel="attachment wp-att-10478"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.36.40-AM-472x1024.png" alt="" width="472" height="1024" class="alignnone size-large wp-image-10478" srcset="https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.36.40-AM-472x1024.png 472w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.36.40-AM-138x300.png 138w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.36.40-AM.png 572w" sizes="(max-width: 472px) 100vw, 472px" /></a></p>
<p>This directory contains the Segger tools for supporting their graphics library.  The one that I want is called &#8220;Bin2C.exe&#8221; (unfortunately it is a Windows only tool).  I run it and open the logo.</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-9-an-lcd-display/screen-shot-2020-12-06-at-7-39-58-am/" rel="attachment wp-att-10479"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.39.58-AM-1024x716.png" alt="" width="1024" height="716" class="alignnone size-large wp-image-10479" srcset="https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.39.58-AM-1024x716.png 1024w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.39.58-AM-300x210.png 300w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.39.58-AM-768x537.png 768w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.39.58-AM-600x420.png 600w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.39.58-AM.png 1124w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Then I click save and pick &#8220;C&#8221; bitmap file (*.c)</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-9-an-lcd-display/screen-shot-2020-12-06-at-7-40-44-am/" rel="attachment wp-att-10481"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.40.44-AM-1024x876.png" alt="" width="1024" height="876" class="alignnone size-large wp-image-10481" srcset="https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.40.44-AM-1024x876.png 1024w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.40.44-AM-300x257.png 300w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.40.44-AM-768x657.png 768w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.40.44-AM-600x513.png 600w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.40.44-AM.png 1122w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>For my display I am using 16-bit color, also known as High Color with Alpha 565.</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-9-an-lcd-display/screen-shot-2020-12-06-at-7-41-22-am/" rel="attachment wp-att-10482"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.41.22-AM.png" alt="" width="604" height="456" class="alignnone size-large wp-image-10482" srcset="https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.41.22-AM.png 604w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.41.22-AM-300x226.png 300w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.41.22-AM-600x453.png 600w" sizes="(max-width: 604px) 100vw, 604px" /></a></p>
<p>The tool nicely makes me a file with the right stuff in it.  First, the array of unsigned char (representing the bitmap) and the GUI_BITMAP structure.</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-9-an-lcd-display/screen-shot-2020-12-06-at-7-44-30-am/" rel="attachment wp-att-10483"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.44.30-AM-1024x928.jpg" alt="" width="1024" height="928" class="alignnone size-large wp-image-10483" srcset="https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.44.30-AM-1024x928.jpg 1024w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.44.30-AM-300x272.jpg 300w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.44.30-AM-768x696.jpg 768w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.44.30-AM-1536x1392.jpg 1536w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.44.30-AM-600x544.jpg 600w, https://iotexpert.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-06-at-7.44.30-AM.jpg 1644w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Unfortunately it didn&#8217;t create a header file which I do like this (notice I renamed the data)</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#pragma once
#include &lt;stdlib.h&gt;

#include "GUI.h"

extern GUI_CONST_STORAGE GUI_BITMAP bmIOTexpert_Logo_Vertical320x240;</pre>
<p>And, if you have been paying attention you might have noticed that I created a new IoT Expert Library &#8211; Graphics that contains these new files. (I have explained this process several times in the past)</p>
<h1>TFT Pins Discussion</h1>
<p>I am planning on making a custom circuit board for this whole project.  So, I didn&#8217;t want to use the whole CY8CKIT-028-TFT library.  In order to start the TFT you need to call the function &#8220;mtb_st7789v()&#8221; with an argument of type &#8220;mtb_st7789v_pins_t&#8221;.  When Cypress created the BSP for the TFT we defined a function like this in the header file.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">/**
 * Initializes GPIOs for the software i8080 interface.
 * @return CY_RSLT_SUCCESS if successfully initialized, else an error about
 * what went wrong
 */
static inline cy_rslt_t cy_tft_io_init(void)
{
    static const mtb_st7789v_pins_t PINS =
    {
        .db08 = CY8CKIT_028_TFT_PIN_DISPLAY_DB8,
        .db09 = CY8CKIT_028_TFT_PIN_DISPLAY_DB9,
        .db10 = CY8CKIT_028_TFT_PIN_DISPLAY_DB10,
        .db11 = CY8CKIT_028_TFT_PIN_DISPLAY_DB11,
        .db12 = CY8CKIT_028_TFT_PIN_DISPLAY_DB12,
        .db13 = CY8CKIT_028_TFT_PIN_DISPLAY_DB13,
        .db14 = CY8CKIT_028_TFT_PIN_DISPLAY_DB14,
        .db15 = CY8CKIT_028_TFT_PIN_DISPLAY_DB15,
        .nrd = CY8CKIT_028_TFT_PIN_DISPLAY_NRD,
        .nwr = CY8CKIT_028_TFT_PIN_DISPLAY_NWR,
        .dc = CY8CKIT_028_TFT_PIN_DISPLAY_DC,
        .rst = CY8CKIT_028_TFT_PIN_DISPLAY_RST,
    };
    return mtb_st7789v_init8(&amp;PINS);
}
</pre>
<p>Curiously we also made a static definition inside of the .c file.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">static const mtb_st7789v_pins_t tft_pins =
{
    .db08 = CY8CKIT_028_TFT_PIN_DISPLAY_DB8,
    .db09 = CY8CKIT_028_TFT_PIN_DISPLAY_DB9,
    .db10 = CY8CKIT_028_TFT_PIN_DISPLAY_DB10,
    .db11 = CY8CKIT_028_TFT_PIN_DISPLAY_DB11,
    .db12 = CY8CKIT_028_TFT_PIN_DISPLAY_DB12,
    .db13 = CY8CKIT_028_TFT_PIN_DISPLAY_DB13,
    .db14 = CY8CKIT_028_TFT_PIN_DISPLAY_DB14,
    .db15 = CY8CKIT_028_TFT_PIN_DISPLAY_DB15,
    .nrd = CY8CKIT_028_TFT_PIN_DISPLAY_NRD,
    .nwr = CY8CKIT_028_TFT_PIN_DISPLAY_NWR,
    .dc = CY8CKIT_028_TFT_PIN_DISPLAY_DC,
    .rst = CY8CKIT_028_TFT_PIN_DISPLAY_RST,
};
</pre>
<p>What I know is that I need those pin definitions, so I will copy them into my project (a bit later in this article)</p>
<h1>Make a New Display Task</h1>
<p>To get this going, make a file called displayManager.h to contain the public definition of the task.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#pragma once

void dm_task(void *arg);
</pre>
<p>Then add a file displayManager.c.  This file will be the focus of the rest of this article.  Before we get too far down the road building all of the functionality of the display, first lets setup a test task to verify the functionality of the libraries and the screen.  As I talked about earlier, define the pins for the driver.  These the pin names come out of the BSP and &#8220;should&#8221; work for all of the Cypress development kits (that have Arduino headers).</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

#include "GUI.h"
#include "mtb_st7789v.h"
#include "cybsp.h"

#include "FreeRTOS.h"
#include "task.h"

#include "displayManager.h"

#include "IOTexpert_Logo_Vertical320x240.h"

const mtb_st7789v_pins_t tft_pins =
{
    .db08 = (CYBSP_J2_2),
    .db09 = (CYBSP_J2_4),
    .db10 = (CYBSP_J2_6),
    .db11 = (CYBSP_J2_10),
    .db12 = (CYBSP_J2_12),
    .db13 = (CYBSP_D7),
    .db14 = (CYBSP_D8),
    .db15 = (CYBSP_D9),
    .nrd  = (CYBSP_D10),
    .nwr  = (CYBSP_D11),
    .dc   = (CYBSP_D12),
    .rst  = (CYBSP_D13)
};
</pre>
<p>Then make a simple task that</p>
<ol>
<li>Initializes the driver</li>
<li>Initialize emWin</li>
<li>Sets the background color</li>
<li>Clear the screen</li>
<li>Draw the IoT Expert Logo</li>
</ol>
<pre class="EnlighterJSRAW" data-enlighter-language="c">void dm_task(void *arg)
{
    /* Initialize the display controller */
    mtb_st7789v_init8(&amp;tft_pins);
    GUI_Init();
    GUI_SetBkColor(GUI_WHITE);
    GUI_Clear();
    GUI_DrawBitmap(&amp;bmIOTexpert_Logo_Vertical320x240,0,11);

    while(1)
        vTaskDelay(portMAX_DELAY);
}
</pre>
<h1>Test</h1>
<p>When I program this project I get a nice white background screen with my beautiful logo on it. Sweet.  In the next article I&#8217;ll do some work on displaying useful information on the screen.</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-9-an-lcd-display/img_0311/" rel="attachment wp-att-10434"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/12/IMG_0311-scaled-e1606907525730-1024x768.jpg" alt="" width="1024" height="768" class="alignnone wp-image-10434 size-large" srcset="https://iotexpert.com/wp-content/uploads/2020/12/IMG_0311-scaled-e1606907525730-1024x768.jpg 1024w, https://iotexpert.com/wp-content/uploads/2020/12/IMG_0311-scaled-e1606907525730-300x225.jpg 300w, https://iotexpert.com/wp-content/uploads/2020/12/IMG_0311-scaled-e1606907525730-768x576.jpg 768w, https://iotexpert.com/wp-content/uploads/2020/12/IMG_0311-scaled-e1606907525730-1536x1152.jpg 1536w, https://iotexpert.com/wp-content/uploads/2020/12/IMG_0311-scaled-e1606907525730-2048x1536.jpg 2048w, https://iotexpert.com/wp-content/uploads/2020/12/IMG_0311-scaled-e1606907525730-600x450.jpg 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/tilt-hydrometer-part-9-an-lcd-display/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>EW21: Lesson 3: WiFi</title>
		<link>https://iotexpert.com/ew21-lesson-3-wifi/</link>
					<comments>https://iotexpert.com/ew21-lesson-3-wifi/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Wed, 24 Feb 2021 15:25:45 +0000</pubDate>
				<category><![CDATA[Embedded World 2021]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=10782</guid>

					<description><![CDATA[Summary In lesson 3 we will add a task to manage the &#8220;Cloud&#8221;, 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 &#8220;Cloud Task&#8221; colored green &#8211; well actually partially [&#8230;]]]></description>
										<content:encoded><![CDATA[<div>
<p><span><h1>Embedded World 2021 - Infineon ModusToolbox PSoC 6 Drone </h1>
<p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >#</th>
<th >Description</th>
</tr>
</thead>
<tbody>
<tr><td >0</td>
<td ><a href="https://iotexpert.com/ew21-lesson-0-introduction-resources/" target="_blank" rel="noopener">Introduction &amp; Resources</a></td>
</tr>

<tr><td >1</td>
<td ><a href="https://iotexpert.com/ew21-lesson-1-freertos-capsense/" target="_blank" rel="noopener">FreeRTOS &amp; CapSense</a></td>
</tr>

<tr><td >2</td>
<td ><a href="https://iotexpert.com/ew21-lesson-2-joystick/" target="_blank" rel="noopener">3-D Magnetic Sensing XENSIV Joystick</a></td>
</tr>

<tr><td >3</td>
<td ><a href="https://iotexpert.com/ew21-lesson-3-wifi/" target="_blank" rel="noopener">WiFi</a></td>
</tr>

<tr><td >4</td>
<td ><a href="https://iotexpert.com/ew21-lesson-4-mqtt/" target="_blank" rel="noopener">MQTT</a></td>
</tr>

<tr><td >5</td>
<td ><a href="https://iotexpert.com/ew21-lesson-5-low-power/" target="_blank" rel="noopener">Low Power</a></td>
</tr>

<tr><td >6</td>
<td ><a href="https://iotexpert.com/ew21-lesson-6-motor-control/" target="_blank" rel="noopener">BLDC Motor Control</a></td>
</tr>

<tr><td >7</td>
<td ><a href="https://iotexpert.com/ew21-lesson-7-ws2812-led-strip/" target="_blank" rel="noopener">WS2812 Strip LEDs</a></td>
</tr>
</tbody></table></div></p>
<p>&nbsp;</p></span></p>
<h1>Summary</h1>
<p>In lesson 3 we will add a task to manage the &#8220;Cloud&#8221;, 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 &#8220;Cloud Task&#8221; colored green &#8211; well actually partially green.  Here is the architecture:</p>
<p><a href="https://iotexpert.com/ew21-lesson-3-wifi/l3-arch-2/" rel="attachment wp-att-10979"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/l3-arch-1-1024x433.png" alt="" width="1024" height="433" class="alignnone size-large wp-image-10979" srcset="https://iotexpert.com/wp-content/uploads/2021/02/l3-arch-1-1024x433.png 1024w, https://iotexpert.com/wp-content/uploads/2021/02/l3-arch-1-300x127.png 300w, https://iotexpert.com/wp-content/uploads/2021/02/l3-arch-1-768x324.png 768w, https://iotexpert.com/wp-content/uploads/2021/02/l3-arch-1-1536x649.png 1536w, https://iotexpert.com/wp-content/uploads/2021/02/l3-arch-1-600x253.png 600w, https://iotexpert.com/wp-content/uploads/2021/02/l3-arch-1.png 1631w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Learning Objectives</h1>
<ol>
<li>AnyCloud Wireless Libraries</li>
<li>Architecture of the PSoC 6 + 43xxxx WiFi Combo</li>
<li>Wireless Connection Manager</li>
</ol>
<h1>Procedure</h1>
<p>In this lesson we will follow these steps:</p>
<ol>
<li>Create a new project by copying lesson 2</li>
<li>Add the WiFi Libraries</li>
<li>Copy the wireless configuration templates files into your project</li>
<li>Look at the Wireless Library Documentation</li>
<li>Update the Makefile</li>
<li>Add cloud_task.h</li>
<li>Add cloud_task.c</li>
<li>Update main.c</li>
<li>Test</li>
</ol>
<h3>1. Create a new project by copying lesson 2</h3>
<p>Start the new project creator and select the CY8CPROTO-062-4343W</p>
<p><a href="https://iotexpert.com/ew21-lesson-3-wifi/screen-shot-2021-02-23-at-8-58-21-am/" rel="attachment wp-att-10885"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-8.58.21-AM-1024x706.jpg" alt="" width="1024" height="706" class="alignnone size-large wp-image-10885" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-8.58.21-AM-1024x706.jpg 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-8.58.21-AM-300x207.jpg 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-8.58.21-AM-768x530.jpg 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-8.58.21-AM-1536x1059.jpg 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-8.58.21-AM-2048x1413.jpg 2048w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-8.58.21-AM-600x414.jpg 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Click the import button.  Then select your previous project and press open.</p>
<p><a href="https://iotexpert.com/ew21-lesson-3-wifi/screen-shot-2021-02-23-at-8-58-49-am/" rel="attachment wp-att-10886"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-8.58.49-AM-1024x699.png" alt="" width="1024" height="699" class="alignnone size-large wp-image-10886" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-8.58.49-AM-1024x699.png 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-8.58.49-AM-300x205.png 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-8.58.49-AM-768x525.png 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-8.58.49-AM-600x410.png 600w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-8.58.49-AM.png 1306w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Select your new template and give it a reasonable name.</p>
<p><a href="https://iotexpert.com/ew21-lesson-3-wifi/screen-shot-2021-02-23-at-8-59-04-am/" rel="attachment wp-att-10887"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-8.59.04-AM-1024x143.png" alt="" width="1024" height="143" class="alignnone size-large wp-image-10887" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-8.59.04-AM-1024x143.png 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-8.59.04-AM-300x42.png 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-8.59.04-AM-768x107.png 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-8.59.04-AM-1536x214.png 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-8.59.04-AM-2048x286.png 2048w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-8.59.04-AM-600x84.png 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now program your project to make sure that things are still working.</p>
<p><a href="https://iotexpert.com/ew21-lesson-3-wifi/screen-shot-2021-02-23-at-9-03-00-am/" rel="attachment wp-att-10889"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.03.00-AM-1024x687.jpg" alt="" width="1024" height="687" class="alignnone size-large wp-image-10889" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.03.00-AM-1024x687.jpg 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.03.00-AM-300x201.jpg 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.03.00-AM-768x515.jpg 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.03.00-AM-600x403.jpg 600w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.03.00-AM.jpg 1434w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
</div>
<p>Here is the serial terminal.</p>
<p><a href="https://iotexpert.com/ew21-lesson-3-wifi/screen-shot-2021-02-23-at-9-03-21-am/" rel="attachment wp-att-10890"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.03.21-AM-1024x469.png" alt="" width="1024" height="469" class="alignnone size-large wp-image-10890" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.03.21-AM-1024x469.png 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.03.21-AM-300x137.png 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.03.21-AM-768x352.png 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.03.21-AM-1536x703.png 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.03.21-AM-600x275.png 600w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.03.21-AM.png 1560w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<div>
<h3>2. Add the WiFi Libraries</h3>
<p>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.</p>
<p><a href="https://iotexpert.com/ew21-lesson-3-wifi/screen-shot-2021-02-23-at-9-05-31-am/" rel="attachment wp-att-10891"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.05.31-AM-943x1024.jpg" alt="" width="943" height="1024" class="alignnone size-large wp-image-10891" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.05.31-AM-943x1024.jpg 943w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.05.31-AM-276x300.jpg 276w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.05.31-AM-768x834.jpg 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.05.31-AM-600x651.jpg 600w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.05.31-AM.jpg 1144w" sizes="(max-width: 943px) 100vw, 943px" /></a></p>
<h3>3. Copy the wireless configuration templates into your project</h3>
<p>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 &#8220;config&#8221; directory.  Copy and paste them into your project.</p>
<p><a href="https://iotexpert.com/ew21-lesson-3-wifi/screen-shot-2021-02-23-at-9-11-26-am/" rel="attachment wp-att-10892"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.11.26-AM-340x1024.jpg" alt="" width="340" height="1024" class="alignnone size-large wp-image-10892" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.11.26-AM-340x1024.jpg 340w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.11.26-AM-100x300.jpg 100w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.11.26-AM-768x2315.jpg 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.11.26-AM-510x1536.jpg 510w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.11.26-AM-600x1809.jpg 600w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.11.26-AM.jpg 826w" sizes="(max-width: 340px) 100vw, 340px" /></a></p>
</div>
<p>Here is a clip of the lwip configuration options.  I wouldn&#8217;t recommend changing anything unless you are sure you know what you are doing.</p>
<p><a href="https://iotexpert.com/ew21-lesson-3-wifi/screen-shot-2021-02-23-at-9-12-37-am/" rel="attachment wp-att-10893"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.12.37-AM-819x1024.jpg" alt="" width="819" height="1024" class="alignnone size-large wp-image-10893" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.12.37-AM-819x1024.jpg 819w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.12.37-AM-240x300.jpg 240w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.12.37-AM-768x960.jpg 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.12.37-AM-1228x1536.jpg 1228w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.12.37-AM-600x750.jpg 600w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.12.37-AM.jpg 1350w" sizes="(max-width: 819px) 100vw, 819px" /></a></p>
<div>
<h3>4. Look at the Wireless Library Documentation</h3>
<p>All of the Infineon libraries will have a directory called &#8220;doc&#8221; and inside of that directory there will be a file called api_reference_manual.html</p>
<p><a href="https://iotexpert.com/ew21-lesson-3-wifi/screen-shot-2021-02-23-at-9-41-17-am/" rel="attachment wp-att-10900"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.41.17-AM.png" alt="" width="700" height="810" class="alignnone size-full wp-image-10900" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.41.17-AM.png 700w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.41.17-AM-259x300.png 259w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.41.17-AM-600x694.png 600w" sizes="(max-width: 700px) 100vw, 700px" /></a></p>
<p>When you open the file you will  see our doxygen generated documentation.  Here the WiFi connection manager documentation.</p>
<p><a href="https://iotexpert.com/ew21-lesson-3-wifi/screen-shot-2021-02-23-at-9-41-42-am/" rel="attachment wp-att-10901"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.41.42-AM-1024x659.jpg" alt="" width="1024" height="659" class="alignnone size-large wp-image-10901" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.41.42-AM-1024x659.jpg 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.41.42-AM-300x193.jpg 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.41.42-AM-768x494.jpg 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.41.42-AM-1536x988.jpg 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.41.42-AM-2048x1318.jpg 2048w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.41.42-AM-600x386.jpg 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h3>5. Update the Makefile</h3>
</div>
<p>The makefile controls all of the libraries.  To enable these libraries you need to add the following &#8220;components&#8221; to the list if they aren&#8217;t already there. (Note that all of this information is in the Quick Start section of the library&#8217;s documentation).</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">COMPONENTS= FREERTOS LWIP MBEDTLS</pre>
<p>And you need to tell the system about the configuration.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">DEFINES+= MBEDTLS_USER_CONFIG_FILE='"mbedtls_user_config.h"' 
DEFINES+=CYBSP_WIFI_CAPABLE
DEFINES+=CY_RTOS_AWARE</pre>
<div>
<h3>6. Add the cloud_task.h</h3>
</div>
<p>Just like the other lessons you will need to create a public header file for the cloud task.</p>
<p><a href="https://iotexpert.com/ew21-lesson-3-wifi/screen-shot-2021-02-23-at-9-14-48-am/" rel="attachment wp-att-10895"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.14.48-AM.png" alt="" width="778" height="504" class="alignnone size-full wp-image-10895" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.14.48-AM.png 778w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.14.48-AM-300x194.png 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.14.48-AM-768x498.png 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.14.48-AM-600x389.png 600w" sizes="(max-width: 778px) 100vw, 778px" /></a></p>
<p>Inside of that file there will be only one function prototype for the task.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#pragma once

void cloud_task(void *param);</pre>
<div>
<h3>7. Add the cloud_task.c</h3>
<p>Make the cloud_task.c file to add the cloud infrastructure to your project.</p>
</div>
<p><a href="https://iotexpert.com/ew21-lesson-3-wifi/screen-shot-2021-02-23-at-9-15-42-am/" rel="attachment wp-att-10896"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.15.42-AM.png" alt="" width="760" height="532" class="alignnone size-full wp-image-10896" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.15.42-AM.png 760w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.15.42-AM-300x210.png 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.15.42-AM-600x420.png 600w" sizes="(max-width: 760px) 100vw, 760px" /></a></p>
<p>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.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include &lt;stdio.h&gt;

#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);

    }
}</pre>
<p>The connect to WiFi function will</p>
<ol>
<li>Setup the Access Point information</li>
<li>Initialize the wireless connection manager</li>
<li>Then go into a loop that will attempt to connect until there is success.</li>
<li>Once a connection is made, it will print out the IP information.</li>
</ol>
<pre class="EnlighterJSRAW" data-enlighter-language="c">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(&amp;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(&amp;connect_param, &amp;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 &gt;&gt; 8), (uint8_t)(ip_address.ip.v4 &gt;&gt; 16),
                        (uint8_t)(ip_address.ip.v4 &gt;&gt; 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);
}
</pre>
<div>
<h3>8. Update main.c</h3>
</div>
<p>In main.c you need to include the cloud task</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">#include "cloud_task.h"</pre>
<p>Then start the cloud task.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">xTaskCreate(cloud_task,    "Cloud"     ,configMINIMAL_STACK_SIZE*8  , NULL, 2, 0);  
</pre>
<div>
<h3>9. Test</h3>
<p>Program your project and make sure that it connects, and the CapSense and joystick still work.</p>
<p><a href="https://iotexpert.com/ew21-lesson-3-wifi/screen-shot-2021-02-23-at-9-23-40-am/" rel="attachment wp-att-10897"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.23.40-AM-1024x267.png" alt="" width="1024" height="267" class="alignnone size-large wp-image-10897" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.23.40-AM-1024x267.png 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.23.40-AM-300x78.png 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.23.40-AM-768x200.png 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.23.40-AM-1536x401.png 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.23.40-AM-600x156.png 600w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-9.23.40-AM.png 1710w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Resources for Project</h1>
<p>You can find this projects in your project creator dialog by filtering for &#8220;IoT Expert Embedded&#8221;.  This is lesson3</p>
<p><a href="https://iotexpert.com/ew21-lesson-1-freertos-capsense/screen-shot-2021-02-22-at-4-33-41-pm/" rel="attachment wp-att-10818"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-22-at-4.33.41-PM-1024x583.png" alt="" width="1024" height="583" class="alignnone size-large wp-image-10818" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-22-at-4.33.41-PM-1024x583.png 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-22-at-4.33.41-PM-300x171.png 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-22-at-4.33.41-PM-768x437.png 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-22-at-4.33.41-PM-1536x874.png 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-22-at-4.33.41-PM-2048x1165.png 2048w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-22-at-4.33.41-PM-600x341.png 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>You can also clone this project at git@github.com:iotexpert/ew21-lesson3.git or https://github.com/iotexpert/ew21-lesson3</p>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/ew21-lesson-3-wifi/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>EW21: Lesson 4: MQTT</title>
		<link>https://iotexpert.com/ew21-lesson-4-mqtt/</link>
					<comments>https://iotexpert.com/ew21-lesson-4-mqtt/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Wed, 24 Feb 2021 15:23:52 +0000</pubDate>
				<category><![CDATA[Embedded World 2021]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=10783</guid>

					<description><![CDATA[Summary In lesson 4 we will finish the cloud task by adding a connection to the MQTT broker.  Then I will make a very minor update to the CapSense and Joystick tasks to send their data to the Cloud.  At the end of this lesson you will have the Cloud truly colored green.  Here is [&#8230;]]]></description>
										<content:encoded><![CDATA[<div>
<p><span><h1>Embedded World 2021 - Infineon ModusToolbox PSoC 6 Drone </h1>
<p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >#</th>
<th >Description</th>
</tr>
</thead>
<tbody>
<tr><td >0</td>
<td ><a href="https://iotexpert.com/ew21-lesson-0-introduction-resources/" target="_blank" rel="noopener">Introduction &amp; Resources</a></td>
</tr>

<tr><td >1</td>
<td ><a href="https://iotexpert.com/ew21-lesson-1-freertos-capsense/" target="_blank" rel="noopener">FreeRTOS &amp; CapSense</a></td>
</tr>

<tr><td >2</td>
<td ><a href="https://iotexpert.com/ew21-lesson-2-joystick/" target="_blank" rel="noopener">3-D Magnetic Sensing XENSIV Joystick</a></td>
</tr>

<tr><td >3</td>
<td ><a href="https://iotexpert.com/ew21-lesson-3-wifi/" target="_blank" rel="noopener">WiFi</a></td>
</tr>

<tr><td >4</td>
<td ><a href="https://iotexpert.com/ew21-lesson-4-mqtt/" target="_blank" rel="noopener">MQTT</a></td>
</tr>

<tr><td >5</td>
<td ><a href="https://iotexpert.com/ew21-lesson-5-low-power/" target="_blank" rel="noopener">Low Power</a></td>
</tr>

<tr><td >6</td>
<td ><a href="https://iotexpert.com/ew21-lesson-6-motor-control/" target="_blank" rel="noopener">BLDC Motor Control</a></td>
</tr>

<tr><td >7</td>
<td ><a href="https://iotexpert.com/ew21-lesson-7-ws2812-led-strip/" target="_blank" rel="noopener">WS2812 Strip LEDs</a></td>
</tr>
</tbody></table></div></p>
<p>&nbsp;</p></span></p>
<h1>Summary</h1>
<p>In lesson 4 we will finish the cloud task by adding a connection to the MQTT broker.  Then I will make a very minor update to the CapSense and Joystick tasks to send their data to the Cloud.  At the end of this lesson you will have the Cloud truly colored green.  Here is the architecture:</p>
<p><a href="https://iotexpert.com/ew21-lesson-4-mqtt/l4-arch/" rel="attachment wp-att-10981"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/l4-arch-1024x433.png" alt="" width="1024" height="433" class="alignnone size-large wp-image-10981" srcset="https://iotexpert.com/wp-content/uploads/2021/02/l4-arch-1024x433.png 1024w, https://iotexpert.com/wp-content/uploads/2021/02/l4-arch-300x127.png 300w, https://iotexpert.com/wp-content/uploads/2021/02/l4-arch-768x324.png 768w, https://iotexpert.com/wp-content/uploads/2021/02/l4-arch-1536x649.png 1536w, https://iotexpert.com/wp-content/uploads/2021/02/l4-arch-600x253.png 600w, https://iotexpert.com/wp-content/uploads/2021/02/l4-arch.png 1631w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Learning Objectives</h1>
<ol>
<li>MQTT</li>
<li>AnyCloud use of AWS libraries</li>
</ol>
<h1>Procedure</h1>
<ol>
<li>Create a new project by copying lesson 3</li>
<li>Add the MQTT Library</li>
<li>Copy the core MQTT configuration</li>
<li>Update the Makefile</li>
<li>Update the cloud_task.h</li>
<li>Update the cloud_task.c</li>
<li>Update joystick_task.c</li>
<li>Update capsense_task.c</li>
<li>Test (using an MQTT Client)</li>
<li>Test (using our C++/Qt GUI)</li>
</ol>
<h3>1. Create a new project by copying lesson 3</h3>
<p>Start the new project creator.  Pick the CY8CPROTO-062-4343W and press next.</p>
<p><a href="https://iotexpert.com/ew21-lesson-4-mqtt/screen-shot-2021-02-23-at-10-07-52-am/" rel="attachment wp-att-10904"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.07.52-AM-1024x966.png" alt="" width="1024" height="966" class="alignnone size-large wp-image-10904" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.07.52-AM-1024x966.png 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.07.52-AM-300x283.png 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.07.52-AM-768x724.png 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.07.52-AM-600x566.png 600w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.07.52-AM.png 1194w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Click on the import button</p>
<p><a href="https://iotexpert.com/ew21-lesson-4-mqtt/screen-shot-2021-02-23-at-10-08-06-am/" rel="attachment wp-att-10905"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.08.06-AM-1024x225.png" alt="" width="1024" height="225" class="alignnone size-large wp-image-10905" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.08.06-AM-1024x225.png 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.08.06-AM-300x66.png 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.08.06-AM-768x169.png 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.08.06-AM-600x132.png 600w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.08.06-AM.png 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>And select project three to start from.</p>
<p><a href="https://iotexpert.com/ew21-lesson-4-mqtt/screen-shot-2021-02-23-at-10-08-21-am/" rel="attachment wp-att-10906"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.08.21-AM-1024x700.png" alt="" width="1024" height="700" class="alignnone size-large wp-image-10906" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.08.21-AM-1024x700.png 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.08.21-AM-300x205.png 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.08.21-AM-768x525.png 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.08.21-AM-600x410.png 600w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.08.21-AM.png 1320w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Give it a sensible name</p>
<p><a href="https://iotexpert.com/ew21-lesson-4-mqtt/screen-shot-2021-02-23-at-10-08-38-am/" rel="attachment wp-att-10907"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.08.38-AM-1024x707.jpg" alt="" width="1024" height="707" class="alignnone size-large wp-image-10907" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.08.38-AM-1024x707.jpg 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.08.38-AM-300x207.jpg 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.08.38-AM-768x530.jpg 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.08.38-AM-1536x1060.jpg 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.08.38-AM-2048x1413.jpg 2048w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.08.38-AM-600x414.jpg 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h3>2. Add the MQTT Library</h3>
<p>Start the library browser and pick out the MQTT library.  Notice that it brings in a bunch of other libraries including the AWS libraries.</p>
<p><a href="https://iotexpert.com/ew21-lesson-4-mqtt/screen-shot-2021-02-23-at-10-11-17-am/" rel="attachment wp-att-10908"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.11.17-AM-1024x795.jpg" alt="" width="1024" height="795" class="alignnone size-large wp-image-10908" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.11.17-AM-1024x795.jpg 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.11.17-AM-300x233.jpg 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.11.17-AM-768x596.jpg 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.11.17-AM-1536x1192.jpg 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.11.17-AM-2048x1589.jpg 2048w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.11.17-AM-600x466.jpg 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h3>3. Copy the core MQTT configuration</h3>
</div>
<p>When you look at the README for the MQTT it tells you to copy the file core_mqtt_config.h into your project.  Do so.</p>
<p><a href="https://iotexpert.com/ew21-lesson-4-mqtt/screen-shot-2021-02-23-at-10-29-54-am/" rel="attachment wp-att-10910"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.29.54-AM-1024x991.jpg" alt="" width="1024" height="991" class="alignnone size-large wp-image-10910" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.29.54-AM-1024x991.jpg 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.29.54-AM-300x290.jpg 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.29.54-AM-768x743.jpg 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.29.54-AM-1536x1486.jpg 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.29.54-AM-2048x1981.jpg 2048w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.29.54-AM-600x580.jpg 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<div>
<h3>4. Update the Makefile</h3>
</div>
<p>For this to work you need to make two changes to your Makefile.  First enable the secure sockets.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">COMPONENTS= FREERTOS LWIP MBEDTLS SECURE_SOCKETS</pre>
<p>Then ignore a few files from the AWS library.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">CY_IGNORE+=$(SEARCH_aws-iot-device-sdk-embedded-C)/libraries/standard/coreHTTP
CY_IGNORE+=libs/aws-iot-device-sdk-embedded-C/libraries/standard/coreHTTP
</pre>
<h3>5. Update the cloud_task.h</h3>
<div>
<p>In order for other tasks to send a message I will create a function where they can send a &#8220;speed&#8221; also known as a percent 0-100.  This function will be called from both the CapSense task as well as the JoyStick task.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">void cloud_sendMotorSpeed(int speed);
</pre>
<h3>6. Update the cloud_task.c</h3>
</div>
<p>In the previous lesson we created the cloud task which connects to the WiFI network.  In this lesson we add MQTT.  Start by adding the includes for the MQTT and the RTOS queue.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include "cy_mqtt_api.h"
#include "queue.h"</pre>
<p>Rather than have magic numbers (or values) hidden in my code I provide #defines for:</p>
<ol>
<li>The MQTT broker</li>
<li>My Client ID</li>
<li>The Topic to broadcast to</li>
<li>The JSON key for the motor speed</li>
</ol>
<p>For other tasks to talk to this task they will push a value of the speed into a queue.  So I need to define that queue.  I will also use a static local variable to hold the handle of the MQTT connection.</p>
<p>I then define functions to</p>
<ol>
<li>Connect to MQTT</li>
<li>Start MQTT</li>
<li>Publish to MQTT</li>
</ol>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#define CLOUD_MQTT_BROKER        "mqtt.eclipseprojects.io"


#define CLOUD_MQTT_CLIENT_PREFIX "remote"
#define CLOUD_MQTT_TOPIC         "motor_speed"

#define MOTOR_KEY                "motor"

static QueueHandle_t motor_value_q;
static cy_mqtt_t mqtthandle;

static void cloud_connectWifi();
static void cloud_startMQTT();
static void cloud_mqtt_event_cb( cy_mqtt_t mqtt_handle, cy_mqtt_event_t event, void *user_data);

static void cloud_publishMessage(char *topic,char *message);</pre>
<p>The updates to the cloud task are:</p>
<ol>
<li>Start WiFi</li>
<li>Start MQTT</li>
<li>Start the queue</li>
<li>In the main loop, wait for a message, then publish it.</li>
</ol>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">void cloud_task(void* param)
{
    (void)param;

    cloud_connectWifi();
    cloud_startMQTT();

    motor_value_q = xQueueCreate(1,sizeof(uint32_t));

    for(;;)
    {
        int motorSpeed;
        char message[32];

    	xQueueReceive(motor_value_q, &amp;motorSpeed, portMAX_DELAY);
        snprintf(message, sizeof(message)-1, "{\"%s\":%d}",MOTOR_KEY,motorSpeed);
        cloud_publishMessage(CLOUD_MQTT_TOPIC,message);
    }
}
</pre>
<p>This function is used in other tasks to send the motor speed into the queue.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">void cloud_sendMotorSpeed(int speed)
{
    if(motor_value_q)
        xQueueOverwrite(motor_value_q,&amp;speed);
}
</pre>
<p>To start MQTT you need to</p>
<ol>
<li>Define the MQTT broker structure</li>
<li>Create the MQTT system</li>
<li>Define your connection information</li>
<li>Create a random clientID (so there are no duplicated clients on the MQTT broker)</li>
<li>Make the connection</li>
</ol>
<pre class="EnlighterJSRAW" data-enlighter-language="c">static void cloud_startMQTT()
{
    static cy_mqtt_connect_info_t    	connect_info;
    static cy_mqtt_broker_info_t     	broker_info;
    static uint8_t buffer[1024];

    cy_rslt_t result;

    result = cy_mqtt_init();
    broker_info.hostname = CLOUD_MQTT_BROKER;
    broker_info.hostname_len = strlen(broker_info.hostname);
    broker_info.port = 1883;

    result = cy_mqtt_create( buffer, sizeof(buffer),
                              NULL, &amp;broker_info,
                              cloud_mqtt_event_cb, NULL,
                              &amp;mqtthandle );

    CY_ASSERT(result == CY_RSLT_SUCCESS);

    static char clientId[32];
    srand(xTaskGetTickCount());
    snprintf(clientId,sizeof(clientId),"%s%6d",CLOUD_MQTT_CLIENT_PREFIX,rand());
    memset( &amp;connect_info, 0, sizeof( cy_mqtt_connect_info_t ) );
    connect_info.client_id      = clientId;
    connect_info.client_id_len  = strlen(connect_info.client_id);
    connect_info.keep_alive_sec = 60;
    connect_info.will_info      = 0;
    connect_info.clean_session = true;


    result = cy_mqtt_connect( mqtthandle, &amp;connect_info );
    CY_ASSERT(result == CY_RSLT_SUCCESS);
    printf("MQTT Connect Success to %s Client=%s\n",CLOUD_MQTT_BROKER,clientId);

}</pre>
<p>You need an MQTT callback.  This is unused in this lesson but we will modify it for the drone.  Just copy it into your project</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">static void cloud_mqtt_event_cb( cy_mqtt_t mqtt_handle, cy_mqtt_event_t event, void *user_data )
{
    cy_mqtt_publish_info_t *received_msg;
    printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
    (void)user_data;
    switch( event.type )
    {
        case CY_MQTT_EVENT_TYPE_DISCONNECT :
            if( event.data.reason == CY_MQTT_DISCONN_TYPE_BROKER_DOWN )
            {
                printf( "\nCY_MQTT_DISCONN_TYPE_BROKER_DOWN .....\n" );
            }
            else
            {
                printf( "\nCY_MQTT_DISCONN_REASON_NETWORK_DISCONNECTION .....\n" );
            }
            break;
        case CY_MQTT_EVENT_TYPE_PUBLISH_RECEIVE :
            received_msg = &amp;(event.data.pub_msg.received_message);
            printf( "Incoming Publish Topic Name: %.*s\n", received_msg-&gt;topic_len, received_msg-&gt;topic );
            printf( "Incoming Publish message Packet Id is %u.\n", event.data.pub_msg.packet_id );
            printf( "Incoming Publish Message : %.*s.\n\n", ( int )received_msg-&gt;payload_len, ( const char * )received_msg-&gt;payload );
            break;
        default :
            printf( "\nUNKNOWN EVENT .....\n" );
            break;
    }
}</pre>
<p>To publish a message you</p>
<ol>
<li>Create the message</li>
<li>Send the publish</li>
</ol>
<pre class="EnlighterJSRAW" data-enlighter-language="c">static void cloud_publishMessage(char *topic,char *message)
{
    cy_mqtt_publish_info_t  pub_msg;
        
    pub_msg.qos = CY_MQTT_QOS0;
    pub_msg.topic = topic;
    pub_msg.topic_len = strlen(pub_msg.topic);
    pub_msg.payload = message;
    pub_msg.payload_len = strlen(message);
    
    cy_mqtt_publish( mqtthandle, &amp;pub_msg );
    printf("Published to Topic=%s Message=%s\n",topic,message);
    
}
</pre>
<div>
<h3>7. Update joystick_task.c</h3>
</div>
<p>We want the joystick task to be able to send joystick positions.  Include the cloud_task.h so that it gets access to the function to send messages.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">#include "cloud_task.h"</pre>
<p>Update the joystick function to call the send message function.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">/* Only update/print new value if it has changed by more than the hysteresis value */
if((joystick_curr.x &gt; (joystick_prev.x + JOYSTICK_HYSTERESIS)) || (joystick_curr.x &lt; (joystick_prev.x - JOYSTICK_HYSTERESIS)))
{
    printf("Joystick Position: %d\n", joystick_curr.x);
    cloud_sendMotorSpeed(joystick_curr.x); 

}</pre>
<div>
<h3>8. Update capsense_task.c</h3>
</div>
<p>We want the CapSense task to be able to send CapSense positions.  Include the cloud_task.h so that it gets access to the function to send messages.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">#include "cloud_task.h"</pre>
<p>Update the button code to send either 0 or 75 (0=STOP!!!#*##&amp;&amp;#) (75=take off)</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">if((0u != button0_status) &amp;&amp; (0u == button0_status_prev))
{
    printf("Button 0 pressed\n");
    cloud_sendMotorSpeed(0); // Stop the Motor
}

if((0u != button1_status) &amp;&amp; (0u == button1_status_prev))
{
    printf("Button 1 pressed\n");
    cloud_sendMotorSpeed(75); // Set the motor to 75%
}</pre>
<p>The slider just sends the current position.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">if((0u != slider_touched) &amp;&amp; (slider_pos_prev != slider_pos ))
{
    printf("Slider position %d\n",slider_pos);
    cloud_sendMotorSpeed(slider_pos);
}</pre>
<h3>9. Test (using an MQTT Client)</h3>
<p>You can download/install MQTTBox from <a href="http://workswithweb.com/mqttbox.html" target="_blank" rel="noopener">here</a>  This is a Mac/Linux/Windows MQTT client.  This will enable you to attach to the MQTT broker and then subscribe to the motor speed topic.</p>
<p><a href="https://iotexpert.com/ew21-lesson-4-mqtt/screen-shot-2021-02-23-at-10-53-16-am/" rel="attachment wp-att-10916"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.53.16-AM-1024x673.jpg" alt="" width="1024" height="673" class="alignnone size-large wp-image-10916" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.53.16-AM-1024x673.jpg 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.53.16-AM-300x197.jpg 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.53.16-AM-768x504.jpg 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.53.16-AM-1536x1009.jpg 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.53.16-AM-2048x1345.jpg 2048w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.53.16-AM-600x394.jpg 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Setup the configuration for the MQTT broker by pressing the gear button</p>
<p><a href="https://iotexpert.com/ew21-lesson-4-mqtt/screen-shot-2021-02-23-at-10-53-02-am/" rel="attachment wp-att-10915"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.53.02-AM-1024x485.png" alt="" width="1024" height="485" class="alignnone size-large wp-image-10915" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.53.02-AM-1024x485.png 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.53.02-AM-300x142.png 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.53.02-AM-768x364.png 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.53.02-AM-1536x728.png 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.53.02-AM-2048x971.png 2048w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.53.02-AM-600x284.png 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Then program your remote control and send some joystick and CapSense values.</p>
<p><a href="https://iotexpert.com/ew21-lesson-4-mqtt/screen-shot-2021-02-23-at-10-52-48-am/" rel="attachment wp-att-10914"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.52.48-AM-1024x564.jpg" alt="" width="1024" height="564" class="alignnone size-large wp-image-10914" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.52.48-AM-1024x564.jpg 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.52.48-AM-300x165.jpg 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.52.48-AM-768x423.jpg 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.52.48-AM-1536x847.jpg 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.52.48-AM-600x331.jpg 600w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.52.48-AM.jpg 1836w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>And you will see them coming out on the MQTTBox client</p>
<p><a href="https://iotexpert.com/ew21-lesson-4-mqtt/screen-shot-2021-02-23-at-10-52-39-am/" rel="attachment wp-att-10913"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.52.39-AM-1024x769.jpg" alt="" width="1024" height="769" class="alignnone size-large wp-image-10913" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.52.39-AM-1024x769.jpg 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.52.39-AM-300x225.jpg 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.52.39-AM-768x577.jpg 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.52.39-AM-1536x1154.jpg 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.52.39-AM-600x451.jpg 600w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-10.52.39-AM.jpg 1922w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h3>10. Test (using our C++/Qt GUI)</h3>
<p>My friend Butch created a C++/Qt GUI to &#8220;simulate&#8221; the drone.  You can find it in the &#8220;simulator&#8221; directory of project 4.  Here is what it looks like.  You will need to put in</p>
<ol>
<li>Which broker you are using</li>
<li>The name of your Topic</li>
</ol>
<p><a href="https://iotexpert.com/ew21-lesson-4-mqtt/screen-shot-2021-02-23-at-11-13-18-am/" rel="attachment wp-att-10919"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-11.13.18-AM-813x1024.png" alt="" width="813" height="1024" class="alignnone size-large wp-image-10919" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-11.13.18-AM-813x1024.png 813w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-11.13.18-AM-238x300.png 238w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-11.13.18-AM-768x968.png 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-11.13.18-AM-600x756.png 600w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-11.13.18-AM.png 838w" sizes="(max-width: 813px) 100vw, 813px" /></a></p>
<div>
<h1>Resources for Project</h1>
<p>You can find this completed project in your project creator dialog by filtering for &#8220;IoT Expert Embedded&#8221;.  This is lesson 4</p>
<p><a href="https://iotexpert.com/ew21-lesson-1-freertos-capsense/screen-shot-2021-02-22-at-4-33-54-pm/" rel="attachment wp-att-10817"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-22-at-4.33.54-PM-1024x581.png" alt="" width="1024" height="581" class="alignnone size-large wp-image-10817" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-22-at-4.33.54-PM-1024x581.png 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-22-at-4.33.54-PM-300x170.png 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-22-at-4.33.54-PM-768x436.png 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-22-at-4.33.54-PM-1536x872.png 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-22-at-4.33.54-PM-2048x1162.png 2048w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-22-at-4.33.54-PM-600x341.png 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>You can also clone this project at git@github.com:iotexpert/ew21-lesson4.git or https://github.com/iotexpert/ew21-lesson4</p>
</div>
<div>
<p>Here is the final cloud_task.c</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include &lt;stdio.h&gt;

#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"

#include "cy_wcm.h"
#include "cy_mqtt_api.h"

#include "cloud_task.h"


#define CLOUD_WIFI_AP        "ew2021"
#define CLOUD_WIFI_PW        "ew2021ap"
#define CLOUD_WIFI_SECURITY  CY_WCM_SECURITY_WPA2_AES_PSK
#define CLOUD_WIFI_BAND      CY_WCM_WIFI_BAND_ANY

#define CLOUD_MQTT_BROKER        "mqtt.eclipseprojects.io"


#define CLOUD_MQTT_CLIENT_PREFIX "remote"
#define CLOUD_MQTT_TOPIC         "motor_speed"

#define MOTOR_KEY                "motor"

static QueueHandle_t motor_value_q;
static cy_mqtt_t mqtthandle;

static void cloud_connectWifi();
static void cloud_startMQTT();
static void cloud_mqtt_event_cb( cy_mqtt_t mqtt_handle, cy_mqtt_event_t event, void *user_data);

static void cloud_publishMessage(char *topic,char *message);

void cloud_task(void* param)
{
    (void)param;

    cloud_connectWifi();
    cloud_startMQTT();

    motor_value_q = xQueueCreate(1,sizeof(uint32_t));

    for(;;)
    {
        int motorSpeed;
        char message[32];

    	xQueueReceive(motor_value_q, &amp;motorSpeed, portMAX_DELAY);
        snprintf(message, sizeof(message)-1, "{\"%s\":%d}",MOTOR_KEY,motorSpeed);
        cloud_publishMessage(CLOUD_MQTT_TOPIC,message);
    }
}

void cloud_sendMotorSpeed(int speed)
{
    if(motor_value_q)
        xQueueSend(motor_value_q,&amp;speed,0);
}

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,
    	.static_ip_settings = 0,
        .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(&amp;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(&amp;connect_param, &amp;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 &gt;&gt; 8), (uint8_t)(ip_address.ip.v4 &gt;&gt; 16),
                        (uint8_t)(ip_address.ip.v4 &gt;&gt; 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);

}

static void cloud_startMQTT()
{
    static cy_mqtt_connect_info_t    	connect_info;
    static cy_mqtt_broker_info_t     	broker_info;
    static uint8_t buffer[1024];

    cy_rslt_t result;

    result = cy_mqtt_init();
    broker_info.hostname = CLOUD_MQTT_BROKER;
    broker_info.hostname_len = strlen(broker_info.hostname);
    broker_info.port = 1883;

    result = cy_mqtt_create( buffer, sizeof(buffer),
                              NULL, &amp;broker_info,
                              cloud_mqtt_event_cb, NULL,
                              &amp;mqtthandle );

    CY_ASSERT(result == CY_RSLT_SUCCESS);

    static char clientId[32];
    srand(xTaskGetTickCount());
    snprintf(clientId,sizeof(clientId),"%s%6d",CLOUD_MQTT_CLIENT_PREFIX,rand());
    memset( &amp;connect_info, 0, sizeof( cy_mqtt_connect_info_t ) );
    connect_info.client_id      = clientId;
    connect_info.client_id_len  = strlen(connect_info.client_id);
    connect_info.keep_alive_sec = 60;
    connect_info.will_info      = 0;
    connect_info.clean_session = true;


    result = cy_mqtt_connect( mqtthandle, &amp;connect_info );
    CY_ASSERT(result == CY_RSLT_SUCCESS);
    printf("MQTT Connect Success to %s Client=%s\n",CLOUD_MQTT_BROKER,clientId);

}

static void cloud_mqtt_event_cb( cy_mqtt_t mqtt_handle, cy_mqtt_event_t event, void *user_data )
{
    cy_mqtt_publish_info_t *received_msg;
    printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
    (void)user_data;
    switch( event.type )
    {
        case CY_MQTT_EVENT_TYPE_DISCONNECT :
            if( event.data.reason == CY_MQTT_DISCONN_TYPE_BROKER_DOWN )
            {
                printf( "\nCY_MQTT_DISCONN_TYPE_BROKER_DOWN .....\n" );
            }
            else
            {
                printf( "\nCY_MQTT_DISCONN_REASON_NETWORK_DISCONNECTION .....\n" );
            }
            break;
        case CY_MQTT_EVENT_TYPE_PUBLISH_RECEIVE :
            received_msg = &amp;(event.data.pub_msg.received_message);
            printf( "Incoming Publish Topic Name: %.*s\n", received_msg-&gt;topic_len, received_msg-&gt;topic );
            printf( "Incoming Publish message Packet Id is %u.\n", event.data.pub_msg.packet_id );
            printf( "Incoming Publish Message : %.*s.\n\n", ( int )received_msg-&gt;payload_len, ( const char * )received_msg-&gt;payload );
            break;
        default :
            printf( "\nUNKNOWN EVENT .....\n" );
            break;
    }
}

static void cloud_publishMessage(char *topic,char *message)
{
    cy_mqtt_publish_info_t  pub_msg;
        
    pub_msg.qos = CY_MQTT_QOS0;
    pub_msg.topic = topic;
    pub_msg.topic_len = strlen(pub_msg.topic);
    pub_msg.payload = message;
    pub_msg.payload_len = strlen(message);
    
    cy_mqtt_publish( mqtthandle, &amp;pub_msg );
    printf("Published to Topic=%s Message=%s\n",topic,message);
    
}
</pre>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/ew21-lesson-4-mqtt/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>EW21: Lesson 5: Low Power</title>
		<link>https://iotexpert.com/ew21-lesson-5-low-power/</link>
					<comments>https://iotexpert.com/ew21-lesson-5-low-power/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Wed, 24 Feb 2021 15:23:04 +0000</pubDate>
				<category><![CDATA[Embedded World 2021]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=10784</guid>

					<description><![CDATA[Summary The FreeRTOS template we started with already enables low power mode transitions such as Sleep and Deep Sleep. In lesson 5 we will update our remote control project to have even lower power as remote controls are almost always driven by batteries.  We will do this by using lower power regulators, slowing down clocks, [&#8230;]]]></description>
										<content:encoded><![CDATA[<div>
<p><span><h1>Embedded World 2021 - Infineon ModusToolbox PSoC 6 Drone </h1>
<p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >#</th>
<th >Description</th>
</tr>
</thead>
<tbody>
<tr><td >0</td>
<td ><a href="https://iotexpert.com/ew21-lesson-0-introduction-resources/" target="_blank" rel="noopener">Introduction &amp; Resources</a></td>
</tr>

<tr><td >1</td>
<td ><a href="https://iotexpert.com/ew21-lesson-1-freertos-capsense/" target="_blank" rel="noopener">FreeRTOS &amp; CapSense</a></td>
</tr>

<tr><td >2</td>
<td ><a href="https://iotexpert.com/ew21-lesson-2-joystick/" target="_blank" rel="noopener">3-D Magnetic Sensing XENSIV Joystick</a></td>
</tr>

<tr><td >3</td>
<td ><a href="https://iotexpert.com/ew21-lesson-3-wifi/" target="_blank" rel="noopener">WiFi</a></td>
</tr>

<tr><td >4</td>
<td ><a href="https://iotexpert.com/ew21-lesson-4-mqtt/" target="_blank" rel="noopener">MQTT</a></td>
</tr>

<tr><td >5</td>
<td ><a href="https://iotexpert.com/ew21-lesson-5-low-power/" target="_blank" rel="noopener">Low Power</a></td>
</tr>

<tr><td >6</td>
<td ><a href="https://iotexpert.com/ew21-lesson-6-motor-control/" target="_blank" rel="noopener">BLDC Motor Control</a></td>
</tr>

<tr><td >7</td>
<td ><a href="https://iotexpert.com/ew21-lesson-7-ws2812-led-strip/" target="_blank" rel="noopener">WS2812 Strip LEDs</a></td>
</tr>
</tbody></table></div></p>
<p>&nbsp;</p></span></p>
<h1>Summary</h1>
<p>The FreeRTOS template we started with already enables low power mode transitions such as Sleep and Deep Sleep. In lesson 5 we will update our remote control project to have even lower power as remote controls are almost always driven by batteries.  We will do this by using lower power regulators, slowing down clocks, and by offloading some functionality to the WiFi chip. This will be the final lesson on the remote control.  Here is the architecture:</p>
<p><a href="https://iotexpert.com/ew21-lesson-5-low-power/l5-arch/" rel="attachment wp-att-10983"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/l5-arch-1024x433.png" alt="" width="1024" height="433" class="alignnone size-large wp-image-10983" srcset="https://iotexpert.com/wp-content/uploads/2021/02/l5-arch-1024x433.png 1024w, https://iotexpert.com/wp-content/uploads/2021/02/l5-arch-300x127.png 300w, https://iotexpert.com/wp-content/uploads/2021/02/l5-arch-768x324.png 768w, https://iotexpert.com/wp-content/uploads/2021/02/l5-arch-1536x649.png 1536w, https://iotexpert.com/wp-content/uploads/2021/02/l5-arch-600x253.png 600w, https://iotexpert.com/wp-content/uploads/2021/02/l5-arch.png 1631w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Learning Objectives</h1>
<p>This lesson will show you two important features of Modus Toolbox.</p>
<ol>
<li>How to reduce system power using the Low Power Assistant</li>
<li>How to create a custom board support package</li>
</ol>
<h1>Procedure</h1>
<ol>
<li>Create your project</li>
<li>Create a custom Board Support Package</li>
<li>Modify the Makefile to use the new BSP</li>
<li>Add the Low Power Assistant to the project</li>
<li>Use the Configurator to setup Low Power</li>
<li>Examine FreeRTOSConfig.h</li>
<li>Test the project</li>
</ol>
<h3>1. Create the project</h3>
<p>Start the project creator and make a copy of your 4th project.</p>
<p><a href="https://iotexpert.com/ew21-lesson-5-low-power/screen-shot-2021-02-23-at-5-27-11-pm/" rel="attachment wp-att-10987"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-5.27.11-PM-1024x708.jpg" alt="" width="1024" height="708" class="alignnone size-large wp-image-10987" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-5.27.11-PM-1024x708.jpg 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-5.27.11-PM-300x207.jpg 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-5.27.11-PM-768x531.jpg 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-5.27.11-PM-1536x1062.jpg 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-5.27.11-PM-2048x1416.jpg 2048w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-5.27.11-PM-600x415.jpg 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Call it Lesson_5</p>
<p><a href="https://iotexpert.com/ew21-lesson-5-low-power/screen-shot-2021-02-23-at-5-27-51-pm/" rel="attachment wp-att-10988"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-5.27.51-PM-1024x717.jpg" alt="" width="1024" height="717" class="alignnone size-large wp-image-10988" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-5.27.51-PM-1024x717.jpg 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-5.27.51-PM-300x210.jpg 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-5.27.51-PM-768x538.jpg 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-5.27.51-PM-1536x1076.jpg 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-5.27.51-PM-2048x1435.jpg 2048w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-5.27.51-PM-600x420.jpg 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h3>2. Override the BSP Configuration</h3>
<p>Make a directory called &#8220;COMPONENT_CUSTOM_DESIGN_MODUS&#8221; to contain the configuration files that will override the configuration from the BSP.  This will be brought into the project automatically by the build system.</p>
<p>Note: You can also create a complete custom BSP which allows changing things like linker scripts, startup code, etc. See the ModusToolbox User Guide for details on how to do that.</p>
<p><a href="https://iotexpert.com/ew21-lesson-5-low-power/screen-shot-2021-02-23-at-1-48-42-pm/" rel="attachment wp-att-10943"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.48.42-PM-1024x276.png" alt="" width="1024" height="276" class="alignnone size-large wp-image-10943" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.48.42-PM-1024x276.png 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.48.42-PM-300x81.png 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.48.42-PM-768x207.png 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.48.42-PM-600x161.png 600w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.48.42-PM.png 1152w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Make a directory called &#8220;TARGET_CY8CPROTO-062-4343W&#8221; in your project.  This directory will hold the custom BSP files.</p>
<p><a href="https://iotexpert.com/ew21-lesson-5-low-power/screen-shot-2021-02-23-at-1-49-09-pm/" rel="attachment wp-att-10944"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.49.09-PM-1024x258.png" alt="" width="1024" height="258" class="alignnone size-large wp-image-10944" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.49.09-PM-1024x258.png 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.49.09-PM-300x76.png 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.49.09-PM-768x194.png 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.49.09-PM-600x151.png 600w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.49.09-PM.png 1150w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Copy and paste the configuration files from the Modus Toolbox BSP directly into your project.</p>
<p><a href="https://iotexpert.com/ew21-lesson-5-low-power/screen-shot-2021-02-23-at-1-49-32-pm/" rel="attachment wp-att-10945"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.49.32-PM-988x1024.png" alt="" width="988" height="1024" class="alignnone size-large wp-image-10945" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.49.32-PM-988x1024.png 988w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.49.32-PM-289x300.png 289w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.49.32-PM-768x796.png 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.49.32-PM-600x622.png 600w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.49.32-PM.png 1140w" sizes="(max-width: 988px) 100vw, 988px" /></a></p>
<p>Now your project should have these files:</p>
</div>
<p><a href="https://iotexpert.com/ew21-lesson-5-low-power/screen-shot-2021-02-24-at-8-33-25-am/" rel="attachment wp-att-11048"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-24-at-8.33.25-AM-1024x317.png" alt="" width="1024" height="317" class="alignnone size-large wp-image-11048" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-24-at-8.33.25-AM-1024x317.png 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-24-at-8.33.25-AM-300x93.png 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-24-at-8.33.25-AM-768x238.png 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-24-at-8.33.25-AM-600x186.png 600w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-24-at-8.33.25-AM.png 1136w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<div>
<h3>3. Modify the Makefile</h3>
</div>
<p>You need to</p>
<ol>
<li>Enable your new BSP</li>
<li>Disable the existing BSP</li>
</ol>
<p>Do this by adding to the COMPONENTS and DISABLE_COMPONENTS line in the Makefile</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">COMPONENTS= FREERTOS LWIP MBEDTLS SECURE_SOCKETS CUSTOM_DESIGN_MODUS WCM
DISABLE_COMPONENTS=BSP_DESIGN_MODUS</pre>
<div>
<h3>4. Add the Low Power Assistant to the project</h3>
<p>Start the Library Manager and add the LPA (Low Power Assistant) library to your project</p>
<p><a href="https://iotexpert.com/ew21-lesson-5-low-power/screen-shot-2021-02-23-at-1-12-02-pm/" rel="attachment wp-att-10939"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.12.02-PM-1024x795.jpg" alt="" width="1024" height="795" class="alignnone size-large wp-image-10939" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.12.02-PM-1024x795.jpg 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.12.02-PM-300x233.jpg 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.12.02-PM-768x596.jpg 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.12.02-PM-1536x1193.jpg 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.12.02-PM-2048x1590.jpg 2048w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.12.02-PM-600x466.jpg 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h3>5. Use the configurator to Setup for Low Power</h3>
<p>Set the System Active Power Mode to ULP and enable the &#8220;Normal Current Buck&#8221;.</p>
<p>Notice that the System Idle Power Mode is already set to System Deep Sleep. This means that when the RTOS is not busy, it will attempt to enter Deep Sleep mode.</p>
<p><a href="https://iotexpert.com/ew21-lesson-5-low-power/screen-shot-2021-02-23-at-1-03-49-pm/" rel="attachment wp-att-10934"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.03.49-PM-1024x918.jpg" alt="" width="1024" height="918" class="alignnone size-large wp-image-10934" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.03.49-PM-1024x918.jpg 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.03.49-PM-300x269.jpg 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.03.49-PM-768x689.jpg 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.03.49-PM-1536x1378.jpg 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.03.49-PM-2048x1837.jpg 2048w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.03.49-PM-600x538.jpg 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Turn down the FLL to 50 Mhz</p>
<p><a href="https://iotexpert.com/ew21-lesson-5-low-power/screen-shot-2021-02-23-at-1-05-04-pm/" rel="attachment wp-att-10935"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.05.04-PM-1024x918.jpg" alt="" width="1024" height="918" class="alignnone size-large wp-image-10935" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.05.04-PM-1024x918.jpg 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.05.04-PM-300x269.jpg 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.05.04-PM-768x689.jpg 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.05.04-PM-1536x1377.jpg 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.05.04-PM-2048x1836.jpg 2048w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.05.04-PM-600x538.jpg 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Lower the frequency of the peripherals by cutting the divider in half.</p>
<p><a href="https://iotexpert.com/ew21-lesson-5-low-power/screen-shot-2021-02-23-at-1-05-16-pm/" rel="attachment wp-att-10936"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.05.16-PM-1024x465.jpg" alt="" width="1024" height="465" class="alignnone size-large wp-image-10936" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.05.16-PM-1024x465.jpg 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.05.16-PM-300x136.jpg 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.05.16-PM-768x348.jpg 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.05.16-PM-1536x697.jpg 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.05.16-PM-2048x929.jpg 2048w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.05.16-PM-600x272.jpg 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>In order to save power the host can sleep and wait for WiFi packets.  The WiFi chip needs to be able to wakeup the host PSoC.  Enable P0[4] as the wakeup pin and turn on the interrupt.</p>
<p><a href="https://iotexpert.com/ew21-lesson-5-low-power/screen-shot-2021-02-23-at-1-09-32-pm/" rel="attachment wp-att-10937"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.09.32-PM-1024x906.png" alt="" width="1024" height="906" class="alignnone size-large wp-image-10937" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.09.32-PM-1024x906.png 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.09.32-PM-300x265.png 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.09.32-PM-768x679.png 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.09.32-PM-600x531.png 600w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.09.32-PM.png 1318w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>&nbsp;</p>
<p>Click on the CYW4343WKUGB tab.  Enable the host wakeup option.  Set the pin to P0[4].  Enable ARP off loads.  You can also look through the other low power offload filters.</p>
<p><a href="https://iotexpert.com/ew21-lesson-5-low-power/screen-shot-2021-02-23-at-1-46-37-pm/" rel="attachment wp-att-10941"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.46.37-PM-1024x480.jpg" alt="" width="1024" height="480" class="alignnone size-large wp-image-10941" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.46.37-PM-1024x480.jpg 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.46.37-PM-300x141.jpg 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.46.37-PM-768x360.jpg 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.46.37-PM-1536x720.jpg 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.46.37-PM-2048x960.jpg 2048w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-1.46.37-PM-600x281.jpg 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h3>6. Examine FreeRTOSConfig.h</h3>
<p>Along with the System Idle Power Mode in the Configurator, this block of code tells FreeRTOS that when the system is idle that it should go to sleep.</p>
<p>The function vApplicationSleep is in the abstraction-rtos library and it allows the system to go to sleep and wake up cleanly in the context of FreeRTOS.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">#if CY_CFG_PWR_SYS_IDLE_MODE == CY_CFG_PWR_MODE_SLEEP || CY_CFG_PWR_SYS_IDLE_MODE == CY_CFG_PWR_MODE_DEEPSLEEP
extern void vApplicationSleep( uint32_t xExpectedIdleTime );
#define portSUPPRESS_TICKS_AND_SLEEP( xIdleTime ) vApplicationSleep( xIdleTime )
#define configUSE_TICKLESS_IDLE  2
#endif

#if CY_CFG_PWR_DEEPSLEEP_LATENCY&gt;0
#define configEXPECTED_IDLE_TIME_BEFORE_SLEEP CY_CFG_PWR_DEEPSLEEP_LATENCY
#endif
</pre>
<h3>7. Test</h3>
<p>Program and test to make sure that things still work.  If you have a DMM plug it in and find out how much power you saved.</p>
<p><a href="https://iotexpert.com/ew21-lesson-5-low-power/screen-shot-2021-02-23-at-2-11-04-pm/" rel="attachment wp-att-10947"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-2.11.04-PM-1024x552.jpg" alt="" width="1024" height="552" class="alignnone size-large wp-image-10947" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-2.11.04-PM-1024x552.jpg 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-2.11.04-PM-300x162.jpg 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-2.11.04-PM-768x414.jpg 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-2.11.04-PM-1536x828.jpg 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-2.11.04-PM-2048x1104.jpg 2048w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-23-at-2.11.04-PM-600x324.jpg 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Resources for Project</h1>
<p>You can find the completed project in your project creator dialog by filtering for &#8220;IoT Expert Embedded&#8221;.  This is lesson5</p>
<p><a href="https://iotexpert.com/ew21-lesson-1-freertos-capsense/screen-shot-2021-02-22-at-4-34-13-pm/" rel="attachment wp-att-10816"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-22-at-4.34.13-PM-1024x586.png" alt="" width="1024" height="586" class="alignnone size-large wp-image-10816" srcset="https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-22-at-4.34.13-PM-1024x586.png 1024w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-22-at-4.34.13-PM-300x172.png 300w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-22-at-4.34.13-PM-768x439.png 768w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-22-at-4.34.13-PM-1536x879.png 1536w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-22-at-4.34.13-PM-2048x1172.png 2048w, https://iotexpert.com/wp-content/uploads/2021/02/Screen-Shot-2021-02-22-at-4.34.13-PM-600x343.png 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>You can also clone this project at git@github.com:iotexpert/ew21-lesson5.git or https://github.com/iotexpert/ew21-lesson5</p>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/ew21-lesson-5-low-power/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Tilt Hydrometer (Part 5) Tilt Simulator &#038; Multi Advertising iBeacons</title>
		<link>https://iotexpert.com/tilt-hydrometer-part-5-tilt-simulator-multi-advertising-ibeacons/</link>
					<comments>https://iotexpert.com/tilt-hydrometer-part-5-tilt-simulator-multi-advertising-ibeacons/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Mon, 01 Feb 2021 14:20:47 +0000</pubDate>
				<category><![CDATA[Tilt Hydrometer Data Collection]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=10368</guid>

					<description><![CDATA[Summary Using the PSoC 6 AnyCloud Bluetooth Stack to send out &#8220;multi-adv&#8221; Bluetooth advertising packets with a PSoC 6 and CYW43012.  Multi-adv means sending out multiple different simultaneous Bluetooth Advertising packets.  This includes building a project to simulate a Tilt Hydrometer. Story As I worked on the Tilt project I came to realize that what [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>Using the PSoC 6 AnyCloud Bluetooth Stack to send out &#8220;multi-adv&#8221; Bluetooth advertising packets with a PSoC 6 and CYW43012.  Multi-adv means sending out multiple different simultaneous Bluetooth Advertising packets.  This includes building a project to simulate a <a href="https://tilthydrometer.com" target="_blank" rel="noopener noreferrer">Tilt Hydrometer</a>.</p>
<h1>Story</h1>
<p>As I worked on the Tilt project I came to realize that what I really needed what a Tilt Simulator.  That is, a PSoC programmed with a command line interface that could send out the iBeacon messages that looked like the ones that the Tilt actually sent out.  Why?  Simple, all three of my Tilts are busy doing their day job measuring beer and they don&#8217;t have time to fool around being test subjects for my IoT Expert Articles.</p>
<p>The CYW43012 controller that I am using is capable of automatically rotating through a list of at least 8 beacons (maybe more, but I don&#8217;t know the exact number. I suppose I should figure it out).  In other words I can simultaneously simulate 8 Tilts at one time with one PSoC.</p>
<p>For this project, I want the project to be able to</p>
<ol>
<li>Simultaneously broadcast 0-8 of the Tilt iBeacon messages</li>
<li>Have a command line interface to control the Tilt iBeacons</li>
<li>Be able to set a ramp rate for Temperature and Gravity on a per Tilt basis</li>
</ol>
<p>The commands will be:</p>
<ol>
<li>print &#8211; print out the table of the information about the Tilt iBeacons</li>
<li>set &#8211; set the temperature, gravity and txPower e.g. &#8220;set 0 77 1020 99&#8221; would turn on beacon 1 with the temperature set to 77 and the gravity set to 1020 and the txPower set to 99</li>
<li>update &#8211; configure the update rate for temperature and gravity e.g. &#8220;update 6 1000 0 -1&#8221; would set tilt 6 with an update to happen once per 1000ms with a +0 temperature per update and a -1 gravity points per update</li>
</ol>
<p>Here is an example of the display.</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-5-tilt-simulator-multi-advertising-ibeacons/screen-shot-2020-11-27-at-7-04-29-am/" rel="attachment wp-att-10395"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-27-at-7.04.29-AM-1024x713.png" alt="" width="1024" height="713" class="alignnone size-large wp-image-10395" srcset="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-27-at-7.04.29-AM-1024x713.png 1024w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-27-at-7.04.29-AM-300x209.png 300w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-27-at-7.04.29-AM-768x535.png 768w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-27-at-7.04.29-AM-600x418.png 600w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-27-at-7.04.29-AM.png 1192w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>This series is broken up into the following 12 articles with a few additional possible articles. </p>
<p><a href="https://iotexpert.com/tilt-hydrometer-overview-out-of-box-part-1/" target="_blank" rel="noopener">Tilt Hydrometer (Part 1) Overview &amp; Out-of-Box</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-architecture-part-2/" target="_blank" rel="noopener">Tilt Hydrometer (Part 2) Architecture</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-advertising-scanner-part-3/">Tilt Hydrometer (Part 3) Advertising Scanner</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-4-advertising-packet-error/" target="_blank" rel="noopener">Tilt Hydrometer (Part 4) Advertising Packet Error?</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-5-tilt-simulator-multi-advertising-ibeacons/">Tilt Hydrometer (Part 5) Tilt Simulator &amp; Multi Advertising iBeacons</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-6-tilt-simulator-an-upgrade/" target="_blank" rel="noopener">Tilt Hydrometer (Part 6) Tilt Simulator an Upgrade</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-7-advertising-database/">Tilt Hydrometer (Part 7) Advertising Database</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-8-read-the-database/">Tilt Hydrometer (Part 8) Read the Database</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-9-an-lcd-display/">Tilt Hydrometer (Part 9) An LCD Display</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-10-the-display-state-machine/">Tilt Hydrometer (Part 10) The Display State Machine</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-11-draw-the-display-screens/">Tilt Hydrometer (Part 11) Draw the Display Screens</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-12-capsense/">Tilt Hydrometer (Part 12) CapSense</a></p>
<p>Tilt Hydrometer: LittleFS &amp; SPI Flash (Part ?)</p>
<p>Tilt Hydrometer: WiFi Introducer (Part ?)</p>
<p>Tilt Hydrometer: WebServer (Part ?)</p>
<p>Tilt Hydrometer: Amazon MQTT (Part ?)</p>
<p>Tilt Hydrometer: Printed Circuit Board (Part ?)</p>
<p>You can get the source code from git@github.com:iotexpert/Tilt2.git  This repository has tags for each of the articles which can be accessed with "git checkout part12"  You can find the Tilt Simulator at  git@github.com:iotexpert/TiltSimulator.git.</p>
<p>&nbsp;</p>
<h1>Build the Basic Project</h1>
<p>Create a new project using the FreeRTOS NT Shell Template called Tilt Simulator</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-5-tilt-simulator-multi-advertising-ibeacons/screen-shot-2020-11-25-at-11-11-31-am/" rel="attachment wp-att-10369"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.11.31-AM-1024x653.png" alt="" width="1024" height="653" class="alignnone size-large wp-image-10369" srcset="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.11.31-AM-1024x653.png 1024w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.11.31-AM-300x191.png 300w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.11.31-AM-768x490.png 768w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.11.31-AM-1536x980.png 1536w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.11.31-AM-2048x1307.png 2048w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.11.31-AM-600x383.png 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Enable the bluetooth-freertos and btstack libraries.  Also enable the IoT Expert BT Utilities library.</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-5-tilt-simulator-multi-advertising-ibeacons/screen-shot-2020-11-25-at-11-19-14-am/" rel="attachment wp-att-10376"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.19.14-AM-899x1024.png" alt="" width="899" height="1024" class="alignnone size-large wp-image-10376" srcset="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.19.14-AM-899x1024.png 899w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.19.14-AM-263x300.png 263w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.19.14-AM-768x874.png 768w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.19.14-AM-600x683.png 600w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.19.14-AM.png 1126w" sizes="(max-width: 899px) 100vw, 899px" /></a></p>
<p>In the command line copy the template bluetoothManager.* into the project</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-5-tilt-simulator-multi-advertising-ibeacons/screen-shot-2020-11-25-at-11-14-30-am/" rel="attachment wp-att-10371"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.14.30-AM-1024x104.png" alt="" width="1024" height="104" class="alignnone size-large wp-image-10371" srcset="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.14.30-AM-1024x104.png 1024w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.14.30-AM-300x30.png 300w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.14.30-AM-768x78.png 768w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.14.30-AM-600x61.png 600w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.14.30-AM.png 1398w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Start the Bluetooth configurator with &#8220;make config_bt&#8221; on the command line.  Press the new project file (the blank paper)</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-5-tilt-simulator-multi-advertising-ibeacons/screen-shot-2020-11-25-at-11-15-37-am/" rel="attachment wp-att-10372"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.15.37-AM-1024x996.png" alt="" width="1024" height="996" class="alignnone size-large wp-image-10372" srcset="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.15.37-AM-1024x996.png 1024w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.15.37-AM-300x292.png 300w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.15.37-AM-768x747.png 768w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.15.37-AM-1536x1493.png 1536w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.15.37-AM-600x583.png 600w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.15.37-AM.png 1732w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Select the P6 connectivity device</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-5-tilt-simulator-multi-advertising-ibeacons/screen-shot-2020-11-25-at-11-15-49-am/" rel="attachment wp-att-10373"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.15.49-AM.png" alt="" width="806" height="246" class="alignnone size-large wp-image-10373" srcset="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.15.49-AM.png 806w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.15.49-AM-300x92.png 300w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.15.49-AM-768x234.png 768w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.15.49-AM-600x183.png 600w" sizes="(max-width: 806px) 100vw, 806px" /></a></p>
<p>Press save and call your file &#8220;TiltSimulator&#8221; (it actually doesn&#8217;t matter what you call it).  This will generate the configuration structures you need.</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-5-tilt-simulator-multi-advertising-ibeacons/screen-shot-2020-11-25-at-11-16-21-am/" rel="attachment wp-att-10374"></a> <a href="https://iotexpert.com/?attachment_id=10375" rel="attachment wp-att-10375"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.16.52-AM.png" alt="" width="900" height="426" class="alignnone size-large wp-image-10375" srcset="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.16.52-AM.png 900w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.16.52-AM-300x142.png 300w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.16.52-AM-768x364.png 768w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-25-at-11.16.52-AM-600x284.png 600w" sizes="(max-width: 900px) 100vw, 900px" /></a></p>
<p>In command line run &#8220;make vscode&#8221;.  Then edit the Makefile to include these components.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">COMPONENTS=FREERTOS WICED_BLE</pre>
<p>You should run a build/program right now as you will have a basic project with a blinking led, the Bluetooth stack running and a command line.</p>
<h1>Multiadv</h1>
<p>The CYW43012 has the ability to automatically rotate through a list of advertising packets.  Each packet can have different data including a different Bluetooth address (how crazy is that?)  Each packet can also be advertised at a different and configurable rate.  It does this all with no intervention by the host.</p>
<p>This functionality is documented in the Bluetooth Stack section called &#8220;MutiAdv&#8221; under Device Management.</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-5-tilt-simulator-multi-advertising-ibeacons/screen-shot-2020-11-27-at-7-50-32-am/" rel="attachment wp-att-10397"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-27-at-7.50.32-AM-1024x593.jpg" alt="" width="1024" height="593" class="alignnone size-large wp-image-10397" srcset="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-27-at-7.50.32-AM-1024x593.jpg 1024w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-27-at-7.50.32-AM-300x174.jpg 300w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-27-at-7.50.32-AM-768x445.jpg 768w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-27-at-7.50.32-AM-1536x890.jpg 1536w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-27-at-7.50.32-AM-2048x1187.jpg 2048w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-27-at-7.50.32-AM-600x348.jpg 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Build the Database</h1>
<p>The database is just an array of structures.  There are only 8 entries in the database, one for each color Tilt.  In addition to a string representing the name, the database will have</p>
<ol>
<li>Which advertising slot that this tilt is using (0 means that it isn&#8217;t active)</li>
<li>If the data is &#8220;dirty&#8221; meaning it has changed in this table but has not yet been copied to the controller for broadcast</li>
<li>rate = how often to update the data in the that row</li>
<li>tempUPDATE = how much to change the data by each time an update is called</li>
<li>gravUPDATE = how much to change the gravity by each time an update is called</li>
<li>The actual bytes of the advertising packet</li>
</ol>
<pre class="EnlighterJSRAW" data-enlighter-language="c">/*********************************************************************************
*
* Tilt Database Definition
* 
*********************************************************************************/

#define TILT_IBEACON_HEADER_LEN 25
#define TILT_IBEACON_DATA_LEN 5
#define TILT_IBEACON_LEN (TILT_IBEACON_HEADER_LEN + TILT_IBEACON_DATA_LEN)

typedef struct  {
    char *colorName;    // The color string
    int slot;           // Which Bluetooth ADV Slot this Tilt is using
    bool dirty;         // A flag to tell if there has been a change since the adv was last written
    int rate;           // rate in ms
    int tempUpdate;     // The update rate for temperature
    int gravUpdate;     // The update rate for gravity
    uint8_t advData[TILT_IBEACON_LEN];
} tilt_t;

// 02 01 04 = Flags
// 0x1A 0xFF = Manufacturer specific data
// 0x4c 0x00 = Apple
// 0x02 = iBeacon
// 0x15 = remaining data length
#define IBEACON_HEADER 0x02,0x01,0x04,0x1A,0xFF,0x4C,0x00,0x02,0x15

static tilt_t tiltDB [] =
{
    {"Red",    0, true, 0,0,0, {IBEACON_HEADER,0xA4,0x95,0xBB,0x10,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE, 0x00,0x00,0x00,0x00,0x00}},
    {"Green" , 0, true, 0,0,0, {IBEACON_HEADER,0xA4,0x95,0xBB,0x20,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE, 0x00,0x00,0x00,0x00,0x00}},
    {"Black" , 0, true, 0,0,0, {IBEACON_HEADER,0xA4,0x95,0xBB,0x30,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE, 0x00,0x00,0x00,0x00,0x00}},
    {"Purple", 0, true, 0,0,0, {IBEACON_HEADER,0xA4,0x95,0xBB,0x40,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE, 0x00,0x00,0x00,0x00,0x00}},
    {"Orange", 0, true, 0,0,0, {IBEACON_HEADER,0xA4,0x95,0xBB,0x50,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE, 0x00,0x00,0x00,0x00,0x00}},
    {"Blue"  , 0, true, 0,0,0, {IBEACON_HEADER,0xA4,0x95,0xBB,0x60,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE, 0x00,0x00,0x00,0x00,0x00}},
    {"Yellow", 0, true, 0,0,0, {IBEACON_HEADER,0xA4,0x95,0xBB,0x70,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE, 0x00,0x00,0x00,0x00,0x00}},
    {"Pink"  , 0, true, 0,0,0, {IBEACON_HEADER,0xA4,0x95,0xBB,0x80,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE, 0x00,0x00,0x00,0x00,0x00}},
};
#define NUM_TILT (sizeof(tiltDB)/sizeof(tilt_t))

#define IBEACON_TEMP_HI (25)
#define IBEACON_TEMP_LO (26)
#define IBEACON_GRAV_HI (27)
#define IBEACON_GRAV_LO (28)
#define IBEACON_TXPOWER (29)</pre>
<p>Now I will make &#8220;getters and setters&#8221;.  These functions will set the correct bytes in the adverting packets, and can retrieve the current values in those bytes.  Remember in the Apple iBeacon format there are 5 &#8220;user&#8221; bytes</p>
<ol>
<li>Major byte 0 and byte 1</li>
<li>Minor byte 0 and bytes 1</li>
<li>txPower</li>
</ol>
<p>Apple didnt specify what you stored in the major/minor.  In fact they also didnt specify the endianness.  The Tilt guys decided that the would store BIG endian &#8211; oh well.</p>
<ol>
<li>Major = Temperature in degrees F</li>
<li>Minor = Gravity points i.e. 1050 means 1.050</li>
</ol>
<p>If you look in my code the &#8220;set&#8221; of temperature and gravity they will &#8220;wrap around&#8221;.  If you set a value BELOW the minimum it will set the highest value and if you set a value ABOVE the maximum it will set it to the lowest.  This allows my automatic update code to just count one direction.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">int btm_getTemperature(int num)
{
    return (uint16_t)tiltDB[num].advData[IBEACON_TEMP_HI] &lt;&lt; 8 | tiltDB[num].advData[IBEACON_TEMP_LO];
}

void btm_setTemperature(int num,uint16_t temperature)
{
    if(temperature &gt; 150)
        temperature = 10;
    if(temperature&lt;10)
        temperature = 150;

    int oldtemp = btm_getTemperature(num);

    tiltDB[num].advData[IBEACON_TEMP_HI] = (temperature &amp; 0xFF00) &gt;&gt; 8;    
    tiltDB[num].advData[IBEACON_TEMP_LO] = temperature &amp; 0xFF;
    if(temperature != oldtemp)
        tiltDB[num].dirty = true;    
}

int btm_getGravity(int num)
{
    return (uint16_t)tiltDB[num].advData[IBEACON_GRAV_HI] &lt;&lt; 8 | tiltDB[num].advData[IBEACON_GRAV_LO];
}

void btm_setGravity(int num,uint16_t gravity)
{
    // These if's cause the gravity to "wrap around" at 1200 and 900
    if(gravity&gt;1200)
        gravity = 900;
    if(gravity &lt;900)
        gravity = 1200;

    int oldgrav = btm_getGravity(num);
    tiltDB[num].advData[IBEACON_GRAV_HI] = (uint8_t)((gravity &amp; 0xFF00) &gt;&gt; 8);
    tiltDB[num].advData[IBEACON_GRAV_LO] = (uint8_t)(gravity &amp; 0xFF);
    if(oldgrav != gravity)
        tiltDB[num].dirty = true;
}

void btm_setTxPower(int num, int8_t txPower)
{
    tiltDB[num].advData[IBEACON_TXPOWER] = txPower;
    tiltDB[num].dirty = true;
}</pre>
<h1>Transfer Database to Bluetooth Controller</h1>
<p>The next two functions are used to bridge between the database and the controller.  The first function &#8220;btm_setAdvPacket&#8221; probably should have been called &#8220;btm_setAdvPackets&#8221; because its function is to copy the database bytes into the controller for actual broadcast.  This function is simply a loop that looks at each of the Tilts in the database.</p>
<ol>
<li>If they are in a &#8220;slot&#8221;</li>
<li>AND they are dirty (something has been changed since the last time this was called)</li>
<li>Then copy the data into the controller</li>
<li>Set the parameters</li>
<li>Start it advertising</li>
</ol>
<pre class="EnlighterJSRAW" data-enlighter-language="c">// btm_setAdvPacket
//
// This function updates what the advertising packets and the state of the BT controller:
// It will loop through the table of iBeacons... then if they are in a slot &amp; they are dirty
// it will move them to the contoller and enable advertisements on that slot

void btm_setAdvPacket()
{
    static wiced_bt_ble_multi_adv_params_t myParams = {
    .adv_int_min       = BTM_BLE_ADVERT_INTERVAL_MIN,
    .adv_int_max       = 96,
    .adv_type          = MULTI_ADVERT_NONCONNECTABLE_EVENT,
    .channel_map       = BTM_BLE_ADVERT_CHNL_37 | BTM_BLE_ADVERT_CHNL_38 | BTM_BLE_ADVERT_CHNL_39,
    .adv_filter_policy = BTM_BLE_ADVERT_FILTER_ALL_CONNECTION_REQ_ALL_SCAN_REQ,
    .adv_tx_power      = MULTI_ADV_TX_POWER_MAX_INDEX,
    .peer_bd_addr      = {0},
    .peer_addr_type    = BLE_ADDR_PUBLIC,
    .own_bd_addr       = {0},
    .own_addr_type     = BLE_ADDR_PUBLIC,
    };

    for(int i=0;i&lt;NUM_TILT;i++)
    {
        if(tiltDB[i].slot &amp;&amp; tiltDB[i].dirty)
        {   
            tiltDB[i].dirty = false;
            wiced_set_multi_advertisement_data(tiltDB[i].advData,sizeof(tiltDB[i].advData),tiltDB[i].slot);
            wiced_set_multi_advertisement_params(tiltDB[i].slot,&amp;myParams);
            wiced_start_multi_advertisements( MULTI_ADVERT_START, tiltDB[i].slot );
        }
    }
}</pre>
<p>In order to &#8220;activate&#8221; a row in the database you need to assign it to a slot.  I do this by keeping track of the number of times I have assigned an entry in &#8220;btm_active&#8221; which is static to this function.  Now, shocker, I check to make sure that the caller didn&#8217;t accidentally call this before.  Finally I assign it to a slot and mark it as dirty (so that when the btm_setAdvPacket function is called the data will be put into the controller.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">/* btm_activate
*
* This function will put the tilt in the database entry num
* into the next available advertising slot
*
*/
void btm_activate(int num)
{
    // Keep track of the number of currently active iBeacons
    static int  btm_active=0;
    
    CY_ASSERT(num&lt;NUM_TILT);
    if(tiltDB[num].slot == 0) // Make sure that it is not already in a slow
    {
        btm_active += 1;
        tiltDB[num].slot = btm_active;
        tiltDB[num].dirty = true;
    }
}</pre>
<h1>Command Queue</h1>
<p>The command queue is used by the command line interface to submit changes to the Tilt database.  Specifically set, update and print.  The message is just the values for set &amp; update.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">/*********************************************************************************
* 
* Tilt Data Manager External Interface Queue
*
*********************************************************************************/
typedef enum {
    BTM_CMD_SET_DATA,
    BTM_CMD_PRINT_TABLE,
    BTM_CMD_SET_UPDATE,
} btm_cmd_t;

typedef struct {
    btm_cmd_t cmd;
    int num;
    int temperature;
    int gravity;
    int txPower;
} btm_cmdMsg_t;

static QueueHandle_t btm_cmdQueue=0;
static wiced_timer_ext_t btm_processDataTimer;</pre>
<p>The function to process the command queue has two functions in the systems</p>
<ol>
<li>Process commands from the command line</li>
<li>Cause the data to be updated (as configured by the command line)</li>
</ol>
<p>This function is run by a timer that is started when the Bluetooth stack turns on.  I have it set to run every 200ms.  Each time it runs it will read all of the messages in the queue and process them.  Once that is complete it will check to see if the Tilts need to be updated.</p>
<p>In my system there is a &#8220;base rate&#8221; which is set by the frequency of this function.  In other words the &#8220;count&#8221; variable counts the number of times this function has been called.  This is about &#8220;BTM_QUEUE_RATE&#8221;</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">/* btm_processCmdQueue
*
*  This function is called by a timer every BTM_QUEUE_RATE ms (around 200ms)
*  It processes commands from the GUI
*  It also updates the adverting packets if they are being updated at a rate
*
*/
void btm_processCmdQueue( wiced_timer_callback_arg_t cb_params )
{
    static int count = 0; // This counts the numbers of times the callback has happend

    btm_cmdMsg_t msg;
    while(xQueueReceive(btm_cmdQueue,&amp;msg,0) == pdTRUE)
    {
        switch(msg.cmd)
        {
            case BTM_CMD_SET_DATA:
                btm_setGravity(msg.num,msg.gravity);
                btm_setTemperature(msg.num,msg.temperature);
                btm_setTxPower(msg.num,msg.txPower);
                btm_activate(msg.num);
            break;

            case BTM_CMD_PRINT_TABLE:
                btm_printTable();
            break;

            case BTM_CMD_SET_UPDATE:
                tiltDB[msg.num].tempUpdate = msg.temperature;
                tiltDB[msg.num].gravUpdate = msg.gravity;
                tiltDB[msg.num].rate = msg.txPower / BTM_QUEUE_RATE;
            break;
        }
    }
    count = count + 1;

    // This block of code updates the current values with the update rate
    for(int i=0;i&lt;NUM_TILT;i++)
    {
        // If the slot active
        // and the update rate says that it is time
        if(tiltDB[i].slot &amp;&amp; count % tiltDB[i].rate  == 0)
        {
            btm_setTemperature(i,btm_getTemperature(i) + tiltDB[i].tempUpdate);
            btm_setGravity(i,btm_getGravity(i) + tiltDB[i].gravUpdate);
        }
    }
    btm_setAdvPacket();
}</pre>
<h1>Bluetooth Management</h1>
<p>When the Bluetooth Stack is turned on I need to do two things</p>
<ol>
<li>Initialize the command queue</li>
<li>Initialize a periodic timer that calls the &#8220;btm_processCmdQueue&#8221;</li>
</ol>
<p>Notice that every time something changes in the advertising packets the management callback is called with the event type &#8220;BTM_MULTI_ADVERT_RESP_EVENT&#8221;&#8230; to which I do NOTHING.  I suppose that I should check and make sure that whatever I did succeed.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">wiced_result_t app_bt_management_callback(wiced_bt_management_evt_t event, wiced_bt_management_evt_data_t *p_event_data)
{
    wiced_result_t result = WICED_BT_SUCCESS;

    switch (event)
    {
        case BTM_ENABLED_EVT:
            printf("Started BT Stack Succesfully\n");
            btm_cmdQueue = xQueueCreate(10,sizeof(btm_cmdMsg_t));
            wiced_init_timer_ext(&amp;btm_processDataTimer,btm_processCmdQueue,0,WICED_TRUE);
            wiced_start_timer_ext(&amp;btm_processDataTimer,BTM_QUEUE_RATE);
        break;

        case BTM_MULTI_ADVERT_RESP_EVENT: // Do nothing...
        break;

        default:
            printf("Unhandled Bluetooth Management Event: %s\n", btutil_getBTEventName( event));
        break;
    }

    return result;
}</pre>
<h1>Public Functions</h1>
<p>In the command line I want the ability to</p>
<ol>
<li>Print</li>
<li>Configure the data in Tilt advertising packet</li>
<li>Set the update rate</li>
</ol>
<p>That is what these three functions do.  Notice that I check that the command queue has been initialized.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">void btm_printTableCmd()
{
    if(btm_cmdQueue == 0)
        return;
    
    btm_cmdMsg_t msg;
    msg.cmd =    BTM_CMD_PRINT_TABLE;
    xQueueSend(btm_cmdQueue,&amp;msg,0);
}

void btm_setDataCmd(int num,int temperature,int gravity,int txPower)
{
    if(btm_cmdQueue == 0)
        return;

    btm_cmdMsg_t msg;
    msg.cmd =    BTM_CMD_SET_DATA;
    msg.num = num;
    msg.temperature = temperature;
    msg.gravity = gravity;
    msg.txPower = txPower;
    xQueueSend(btm_cmdQueue,&amp;msg,0);
}


void btm_updateDataCmd(int num,int rate ,int temperature,int gravity )
{
    if(btm_cmdQueue == 0)
        return;
    btm_cmdMsg_t msg;
    msg.cmd =    BTM_CMD_SET_UPDATE;
    msg.num = num;
    msg.temperature = temperature;
    msg.gravity = gravity;
    msg.txPower = rate;
    xQueueSend(btm_cmdQueue,&amp;msg,0);
}
</pre>
<h1>Update the Command Line</h1>
<p>In usrcmd.c I need the three functions which the command line will call.</p>
<ol>
<li>usrcmd_print &#8230; to print out the table</li>
<li>usrcmd_set to set the values in the Tilt database</li>
<li>usrcmd_update to set the update rate</li>
</ol>
<pre class="EnlighterJSRAW" data-enlighter-language="c">static int usrcmd_print(int argc, char **argv)
{
    btm_printTableCmd();
    return 0;
}

static int usrcmd_set(int argc, char **argv)
{
    int gravity;
    int temperature;
    int txPower;
    int num;
    if(argc == 5)
    {
        sscanf(argv[1],"%d",&amp;num);
        sscanf(argv[2],"%d",&amp;temperature);
        sscanf(argv[3],"%d",&amp;gravity);
        sscanf(argv[4],"%d",&amp;txPower);

        btm_setDataCmd(num,temperature,gravity,txPower);
    }
    return 0;
}

static int usrcmd_update(int argc, char **argv)
{
    int gravity;
    int temperature;
    int rate;
    int num;
    if(argc == 5)
    {
        sscanf(argv[1],"%d",&amp;num);
        sscanf(argv[2],"%d",&amp;rate);
        sscanf(argv[3],"%d",&amp;temperature);
        sscanf(argv[4],"%d",&amp;gravity);

        btm_updateDataCmd(num,rate,temperature,gravity);
    }
    return 0;
}</pre>
<p>And once you have those functions you need to turn them on in the command line</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">static const cmd_table_t cmdlist[] = {
    { "help", "This is a description text string for help command.", usrcmd_help },
    { "info", "This is a description text string for info command.", usrcmd_info },
    { "clear", "Clear the screen", usrcmd_clear },
    { "pargs","print the list of arguments", usrcmd_pargs},
#ifdef configUSE_TRACE_FACILITY 
#if configUSE_STATS_FORMATTING_FUNCTIONS ==1
    { "tasks","print the list of RTOS Tasks", usrcmd_list},
#endif
#endif
    { "print", "Print table", usrcmd_print },
    { "set", "set num temperature gravity txpower", usrcmd_set },
    { "update", "update num rate temperature gravity", usrcmd_update },
};</pre>
<p>In the next article I will be back to the &#8220;client&#8221; end of my system and will build a database.</p>
<h1>Full Functions</h1>
<p>Here are the full functions &amp; files.  You can also &#8220;git&#8221; this at git@github.com:iotexpert/TiltSimulator.git</p>
<ol>
<li>bluetoothManager.h</li>
<li>bluetoothManager.c</li>
<li>main.c</li>
<li>usrcmd.c</li>
</ol>
<p><strong>bluetoothManager.h</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#pragma once
#include "wiced_bt_stack.h"
#include "wiced_bt_dev.h"

wiced_result_t app_bt_management_callback(wiced_bt_management_evt_t event, wiced_bt_management_evt_data_t *p_event_data);
void btm_printTable();

void btm_printTableCmd();
void btm_setDataCmd(int num,int temperature,int gravity,int txPower);
void btm_updateDataCmd(int num,int rate ,int temperature,int gravity );
</pre>
<p><strong>bluetoothManager.c</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

#include "cybsp.h"

#include "FreeRTOS.h"

#include "bluetoothManager.h"
#include "wiced_bt_stack.h"
#include "wiced_bt_dev.h"
#include "wiced_bt_trace.h"
#include "wiced_timer.h"
#include "queue.h"
#include "btutil.h"

// Read the queue
#define BTM_QUEUE_RATE 200

/*********************************************************************************
*
* Tilt Database Definition
* 
*********************************************************************************/

#define TILT_IBEACON_HEADER_LEN 25
#define TILT_IBEACON_DATA_LEN 5
#define TILT_IBEACON_LEN (TILT_IBEACON_HEADER_LEN + TILT_IBEACON_DATA_LEN)

typedef struct  {
    char *colorName;    // The color string
    int slot;           // Which Bluetooth ADV Slot this Tilt is using
    bool dirty;         // A flag to tell if there has been a change since the adv was last written
    int rate;           // rate in ms
    int tempUpdate;     // The update rate for temperature
    int gravUpdate;     // The update rate for gravity
    uint8_t advData[TILT_IBEACON_LEN];
} tilt_t;

// 02 01 04 = Flags
// 0x1A 0xFF = Manufacturer specific data
// 0x4c 0x00 = Apple
// 0x02 = iBeacon
// 0x15 = remaining data length
#define IBEACON_HEADER 0x02,0x01,0x04,0x1A,0xFF,0x4C,0x00,0x02,0x15

static tilt_t tiltDB [] =
{
    {"Red",    0, true, 0,0,0, {IBEACON_HEADER,0xA4,0x95,0xBB,0x10,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE, 0x00,0x00,0x00,0x00,0x00}},
    {"Green" , 0, true, 0,0,0, {IBEACON_HEADER,0xA4,0x95,0xBB,0x20,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE, 0x00,0x00,0x00,0x00,0x00}},
    {"Black" , 0, true, 0,0,0, {IBEACON_HEADER,0xA4,0x95,0xBB,0x30,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE, 0x00,0x00,0x00,0x00,0x00}},
    {"Purple", 0, true, 0,0,0, {IBEACON_HEADER,0xA4,0x95,0xBB,0x40,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE, 0x00,0x00,0x00,0x00,0x00}},
    {"Orange", 0, true, 0,0,0, {IBEACON_HEADER,0xA4,0x95,0xBB,0x50,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE, 0x00,0x00,0x00,0x00,0x00}},
    {"Blue"  , 0, true, 0,0,0, {IBEACON_HEADER,0xA4,0x95,0xBB,0x60,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE, 0x00,0x00,0x00,0x00,0x00}},
    {"Yellow", 0, true, 0,0,0, {IBEACON_HEADER,0xA4,0x95,0xBB,0x70,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE, 0x00,0x00,0x00,0x00,0x00}},
    {"Pink"  , 0, true, 0,0,0, {IBEACON_HEADER,0xA4,0x95,0xBB,0x80,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE, 0x00,0x00,0x00,0x00,0x00}},
};
#define NUM_TILT (sizeof(tiltDB)/sizeof(tilt_t))

#define IBEACON_TEMP_HI (25)
#define IBEACON_TEMP_LO (26)
#define IBEACON_GRAV_HI (27)
#define IBEACON_GRAV_LO (28)
#define IBEACON_TXPOWER (29)

/*********************************************************************************
* 
* Tilt Data Manager External Interface Queue
*
*********************************************************************************/
typedef enum {
    BTM_CMD_SET_DATA,
    BTM_CMD_PRINT_TABLE,
    BTM_CMD_SET_UPDATE,
} btm_cmd_t;

typedef struct {
    btm_cmd_t cmd;
    int num;
    int temperature;
    int gravity;
    int txPower;
} btm_cmdMsg_t;

static QueueHandle_t btm_cmdQueue=0;
static wiced_timer_ext_t btm_processDataTimer;


/*********************************************************************************
* 
* These functions are used to interact with the Bluetooth Advertising Controller
*
*********************************************************************************/
// btm_setAdvPacket
//
// This function updates what the advertising packets and the state of the BT controller:
// It will loop through the table of iBeacons... then if they are in a slot &amp; they are dirty
// it will move them to the contoller and enable advertisements on that slot

void btm_setAdvPacket()
{
    static wiced_bt_ble_multi_adv_params_t myParams = {
    .adv_int_min       = BTM_BLE_ADVERT_INTERVAL_MIN,
    .adv_int_max       = 96,
    .adv_type          = MULTI_ADVERT_NONCONNECTABLE_EVENT,
    .channel_map       = BTM_BLE_ADVERT_CHNL_37 | BTM_BLE_ADVERT_CHNL_38 | BTM_BLE_ADVERT_CHNL_39,
    .adv_filter_policy = BTM_BLE_ADVERT_FILTER_ALL_CONNECTION_REQ_ALL_SCAN_REQ,
    .adv_tx_power      = MULTI_ADV_TX_POWER_MAX_INDEX,
    .peer_bd_addr      = {0},
    .peer_addr_type    = BLE_ADDR_PUBLIC,
    .own_bd_addr       = {0},
    .own_addr_type     = BLE_ADDR_PUBLIC,
    };

    for(int i=0;i&lt;NUM_TILT;i++)
    {
        if(tiltDB[i].slot &amp;&amp; tiltDB[i].dirty)
        {   
            tiltDB[i].dirty = false;
            wiced_set_multi_advertisement_data(tiltDB[i].advData,sizeof(tiltDB[i].advData),tiltDB[i].slot);
            wiced_set_multi_advertisement_params(tiltDB[i].slot,&amp;myParams);
            wiced_start_multi_advertisements( MULTI_ADVERT_START, tiltDB[i].slot );
        }
    }
}

/* btm_activate
*
* This function will put the tilt in the database entry num
* into the next available advertising slot
*
*/
void btm_activate(int num)
{
    // Keep track of the number of currently active iBeacons
    static int  btm_active=0;
    
    CY_ASSERT(num&lt;NUM_TILT);
    if(tiltDB[num].slot == 0) // Make sure that it is not already in a slow
    {
        btm_active += 1;
        tiltDB[num].slot = btm_active;
        tiltDB[num].dirty = true;
    }
}

/*********************************************************************************
* 
* This next set of functions is the API to the database
*
*********************************************************************************/


int btm_getTemperature(int num)
{
    return (uint16_t)tiltDB[num].advData[IBEACON_TEMP_HI] &lt;&lt; 8 | tiltDB[num].advData[IBEACON_TEMP_LO];
}

void btm_setTemperature(int num,uint16_t temperature)
{
    if(temperature &gt; 150)
        temperature = 10;
    if(temperature&lt;10)
        temperature = 150;

    int oldtemp = btm_getTemperature(num);

    tiltDB[num].advData[IBEACON_TEMP_HI] = (temperature &amp; 0xFF00) &gt;&gt; 8;    
    tiltDB[num].advData[IBEACON_TEMP_LO] = temperature &amp; 0xFF;
    if(temperature != oldtemp)
        tiltDB[num].dirty = true;    
}

int btm_getGravity(int num)
{
    return (uint16_t)tiltDB[num].advData[IBEACON_GRAV_HI] &lt;&lt; 8 | tiltDB[num].advData[IBEACON_GRAV_LO];
}

void btm_setGravity(int num,uint16_t gravity)
{
    // These if's cause the gravity to "wrap around" at 1200 and 900
    if(gravity&gt;1200)
        gravity = 900;
    if(gravity &lt;900)
        gravity = 1200;

    int oldgrav = btm_getGravity(num);
    tiltDB[num].advData[IBEACON_GRAV_HI] = (uint8_t)((gravity &amp; 0xFF00) &gt;&gt; 8);
    tiltDB[num].advData[IBEACON_GRAV_LO] = (uint8_t)(gravity &amp; 0xFF);
    if(oldgrav != gravity)
        tiltDB[num].dirty = true;
}

void btm_setTxPower(int num, int8_t txPower)
{
    tiltDB[num].advData[IBEACON_TXPOWER] = txPower;
    tiltDB[num].dirty = true;
}

void btm_printTable()
{
    printf("\n# Color   S   Rate T  UpT Grav UpG TxP\n");

    for(int i=0;i&lt;NUM_TILT;i++)
    {
        printf("%d %6s  %d %5d %3d %2d %4d %2d %3d\n",i,
            tiltDB[i].colorName,tiltDB[i].slot,
            tiltDB[i].rate*BTM_QUEUE_RATE,
            btm_getTemperature(i),tiltDB[i].tempUpdate,
            btm_getGravity(i),tiltDB[i].gravUpdate,
            tiltDB[i].advData[29]);
    }
}

/* btm_processCmdQueue
*
*  This function is called by a timer every BTM_QUEUE_RATE ms (around 200ms)
*  It processes commands from the GUI
*  It also updates the adverting packets if they are being updated at a rate
*
*/
void btm_processCmdQueue( wiced_timer_callback_arg_t cb_params )
{
    static int count = 0; // This counts the numbers of times the callback has happend

    btm_cmdMsg_t msg;
    while(xQueueReceive(btm_cmdQueue,&amp;msg,0) == pdTRUE)
    {
        switch(msg.cmd)
        {
            case BTM_CMD_SET_DATA:
                btm_setGravity(msg.num,msg.gravity);
                btm_setTemperature(msg.num,msg.temperature);
                btm_setTxPower(msg.num,msg.txPower);
                btm_activate(msg.num);
            break;

            case BTM_CMD_PRINT_TABLE:
                btm_printTable();
            break;

            case BTM_CMD_SET_UPDATE:
                tiltDB[msg.num].tempUpdate = msg.temperature;
                tiltDB[msg.num].gravUpdate = msg.gravity;
                tiltDB[msg.num].rate = msg.txPower / BTM_QUEUE_RATE;
            break;
        }
    }
    count = count + 1;

    // This block of code updates the current values with the update rate
    for(int i=0;i&lt;NUM_TILT;i++)
    {
        // If the slot active
        // and the update rate says that it is time
        if(tiltDB[i].slot &amp;&amp; count % tiltDB[i].rate  == 0)
        {
            btm_setTemperature(i,btm_getTemperature(i) + tiltDB[i].tempUpdate);
            btm_setGravity(i,btm_getGravity(i) + tiltDB[i].gravUpdate);
        }
    }
    btm_setAdvPacket();
}

/**************************************************************************************************
* Function Name: app_bt_management_callback()
***************************************************************************************************
* Summary:
*   This is a Bluetooth stack event handler function to receive management events from
*   the BLE stack and process as per the application.
*
* Parameters:
*   wiced_bt_management_evt_t event             : BLE event code of one byte length
*   wiced_bt_management_evt_data_t *p_event_data: Pointer to BLE management event structures
*
* Return:
*  wiced_result_t: Error code from WICED_RESULT_LIST or BT_RESULT_LIST
*
*************************************************************************************************/
wiced_result_t app_bt_management_callback(wiced_bt_management_evt_t event, wiced_bt_management_evt_data_t *p_event_data)
{
    wiced_result_t result = WICED_BT_SUCCESS;

    switch (event)
    {
        case BTM_ENABLED_EVT:
            printf("Started BT Stack Succesfully\n");
            btm_cmdQueue = xQueueCreate(10,sizeof(btm_cmdMsg_t));
            wiced_init_timer_ext(&amp;btm_processDataTimer,btm_processCmdQueue,0,WICED_TRUE);
            wiced_start_timer_ext(&amp;btm_processDataTimer,BTM_QUEUE_RATE);
        break;

        case BTM_MULTI_ADVERT_RESP_EVENT: // Do nothing...
        break;

        default:
            printf("Unhandled Bluetooth Management Event: %s\n", btutil_getBTEventName( event));
        break;
    }

    return result;
}

/*********************************************************************************
* 
* These are publicly callable functions to cause actions by the bluetooth manager
* These are called by the GUI
*
*********************************************************************************/

void btm_printTableCmd()
{
    if(btm_cmdQueue == 0)
        return;
    
    btm_cmdMsg_t msg;
    msg.cmd =    BTM_CMD_PRINT_TABLE;
    xQueueSend(btm_cmdQueue,&amp;msg,0);
}

void btm_setDataCmd(int num,int temperature,int gravity,int txPower)
{
    if(btm_cmdQueue == 0)
        return;

    btm_cmdMsg_t msg;
    msg.cmd =    BTM_CMD_SET_DATA;
    msg.num = num;
    msg.temperature = temperature;
    msg.gravity = gravity;
    msg.txPower = txPower;
    xQueueSend(btm_cmdQueue,&amp;msg,0);
}


void btm_updateDataCmd(int num,int rate ,int temperature,int gravity )
{
    if(btm_cmdQueue == 0)
        return;
    btm_cmdMsg_t msg;
    msg.cmd =    BTM_CMD_SET_UPDATE;
    msg.num = num;
    msg.temperature = temperature;
    msg.gravity = gravity;
    msg.txPower = rate;
    xQueueSend(btm_cmdQueue,&amp;msg,0);
}
</pre>
<p>&nbsp;</p>
<p><strong>main.c</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include "cyhal.h"
#include "cybsp.h"
#include "cy_retarget_io.h"
#include &lt;stdio.h&gt;
#include "FreeRTOS.h"
#include "task.h"
#include "usrcmd.h"

#include "bluetoothManager.h"
#include "cycfg_bt_settings.h"
#include "bt_platform_cfg_settings.h"

volatile int uxTopUsedPriority ;
TaskHandle_t blinkTaskHandle;


void blink_task(void *arg)
{
    cyhal_gpio_init(CYBSP_USER_LED,CYHAL_GPIO_DIR_OUTPUT,CYHAL_GPIO_DRIVE_STRONG,0);

    for(;;)
    {
    	cyhal_gpio_toggle(CYBSP_USER_LED);
    	vTaskDelay(500);
    }
}


int main(void)
{
    uxTopUsedPriority = configMAX_PRIORITIES - 1 ; // enable OpenOCD Thread Debugging

    /* Initialize the device and board peripherals */
    cybsp_init() ;
    __enable_irq();

    cy_retarget_io_init(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX, CY_RETARGET_IO_BAUDRATE);



    cybt_platform_config_init(&amp;bt_platform_cfg_settings);
    wiced_bt_stack_init (app_bt_management_callback, &amp;wiced_bt_cfg_settings);

    // Stack size in WORDs
    // Idle task = priority 0
    xTaskCreate(blink_task, "blinkTask", configMINIMAL_STACK_SIZE,0 /* args */ ,0 /* priority */, &amp;blinkTaskHandle);
    xTaskCreate(usrcmd_task, "usrcmd_task", configMINIMAL_STACK_SIZE*4,0 /* args */ ,0 /* priority */, 0);
    vTaskStartScheduler();
}

/* [] END OF FILE */
</pre>
<p>&nbsp;</p>
<p><strong>usrcmd.c</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">/**
 * @file usrcmd.c
 * @author CuBeatSystems
 * @author Shinichiro Nakamura
 * @copyright
 * ===============================================================
 * Natural Tiny Shell (NT-Shell) Version 0.3.1
 * ===============================================================
 * Copyright (c) 2010-2016 Shinichiro Nakamura
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */


#include "ntopt.h"
#include "ntlibc.h"
#include "ntshell.h"
#include &lt;stdio.h&gt;

#include "ntshell.h"
#include "ntlibc.h"
#include "psoc6_ntshell_port.h"

#include "FreeRTOS.h"
#include "task.h"
#include "bluetoothManager.h"

static ntshell_t ntshell;

typedef int (*USRCMDFUNC)(int argc, char **argv);

static int usrcmd_ntopt_callback(int argc, char **argv, void *extobj);
static int usrcmd_help(int argc, char **argv);
static int usrcmd_info(int argc, char **argv);
static int usrcmd_clear(int argc, char **argv);
static int usrcmd_pargs(int argc, char **argv);
#ifdef configUSE_TRACE_FACILITY
#if configUSE_STATS_FORMATTING_FUNCTIONS ==1
static int usrcmd_list(int argc, char **argv);
#endif
#endif
static int usrcmd_print(int argc, char **argv);
static int usrcmd_set(int argc, char **argv);
static int usrcmd_update(int argc, char **argv);

typedef struct {
    char *cmd;
    char *desc;
    USRCMDFUNC func;
} cmd_table_t;

static const cmd_table_t cmdlist[] = {
    { "help", "This is a description text string for help command.", usrcmd_help },
    { "info", "This is a description text string for info command.", usrcmd_info },
    { "clear", "Clear the screen", usrcmd_clear },
    { "pargs","print the list of arguments", usrcmd_pargs},
#ifdef configUSE_TRACE_FACILITY 
#if configUSE_STATS_FORMATTING_FUNCTIONS ==1
    { "tasks","print the list of RTOS Tasks", usrcmd_list},
#endif
#endif
    { "print", "Print table", usrcmd_print },
    { "set", "set num temperature gravity txpower", usrcmd_set },
    { "update", "update num temperature gravity", usrcmd_update },
};


void usrcmd_task()
{

  setvbuf(stdin, NULL, _IONBF, 0);
  printf("Started user command task with NT Shell\n");
  ntshell_init(
	       &amp;ntshell,
	       ntshell_read,
	       ntshell_write,
	       ntshell_callback,
	       (void *)&amp;ntshell);
  ntshell_set_prompt(&amp;ntshell, "AnyCloud&gt; ");
  vtsend_erase_display(&amp;ntshell.vtsend);
  ntshell_execute(&amp;ntshell);
}

int usrcmd_execute(const char *text)
{
    return ntopt_parse(text, usrcmd_ntopt_callback, 0);
}

static int usrcmd_ntopt_callback(int argc, char **argv, void *extobj)
{
    if (argc == 0) {
        return 0;
    }
    const cmd_table_t *p = &amp;cmdlist[0];
    for (unsigned int i = 0; i &lt; sizeof(cmdlist) / sizeof(cmdlist[0]); i++) {
        if (ntlibc_strcmp((const char *)argv[0], p-&gt;cmd) == 0) {
            return p-&gt;func(argc, argv);
        }
        p++;
    }
    printf("%s","Unknown command found.\n");
    return 0;
}

static int usrcmd_help(int argc, char **argv)
{
    const cmd_table_t *p = &amp;cmdlist[0];
    for (unsigned int i = 0; i &lt; sizeof(cmdlist) / sizeof(cmdlist[0]); i++) {
        printf("%s",p-&gt;cmd);
        printf("%s","\t:");
        printf("%s",p-&gt;desc);
        printf("%s","\n");
        p++;
    }
    return 0;
}


static int usrcmd_info(int argc, char **argv)
{
    if (argc != 2) {
        printf("%s","info sys\n");
        printf("%s","info ver\n");
        return 0;
    }
    if (ntlibc_strcmp(argv[1], "sys") == 0) {
        printf("%s","PSoC 6 MBED Monitor\n");
        return 0;
    }
    if (ntlibc_strcmp(argv[1], "ver") == 0) {
        printf("%s","Version 0.0.0\n");
        return 0;
    }
    printf("%s","Unknown sub command found\n");
    return -1;
}


static int usrcmd_clear(int argc, char **argv)
{
    vtsend_erase_display_home(&amp;ntshell.vtsend);
    return 0;
}

static int usrcmd_pargs(int argc, char **argv)
{
    printf("ARGC = %d\n",argc);

    for(int i =0;i&lt;argc;i++)
    {
        printf("argv[%d] = %s\n",i,argv[i]);
    }
    return 0;

}

#ifdef configUSE_TRACE_FACILITY
#if configUSE_STATS_FORMATTING_FUNCTIONS ==1
static int usrcmd_list(int argc,char **argv)
{
    // 40 bytes/task + some margin
    char buff[40*10 + 100];

    vTaskList( buff );
    printf("Name          State Priority   Stack  Num\n");
    printf("------------------------------------------\n");
    printf("%s",buff);

    printf("‘B’ – Blocked\n‘R’ – Ready\n‘D’ – Deleted (waiting clean up)\n‘S’ – Suspended, or Blocked without a timeout\n");
    printf("Stack = bytes free at highwater\n");
    return 0;
}
#endif
#endif


static int usrcmd_print(int argc, char **argv)
{
    btm_printTableCmd();
    return 0;
}

static int usrcmd_set(int argc, char **argv)
{
    int gravity;
    int temperature;
    int txPower;
    int num;
    if(argc == 5)
    {
        sscanf(argv[1],"%d",&amp;num);
        sscanf(argv[2],"%d",&amp;temperature);
        sscanf(argv[3],"%d",&amp;gravity);
        sscanf(argv[4],"%d",&amp;txPower);

        btm_setDataCmd(num,temperature,gravity,txPower);
    }
    return 0;
}

static int usrcmd_update(int argc, char **argv)
{
    int gravity;
    int temperature;
    int rate;
    int num;
    if(argc == 5)
    {
        sscanf(argv[1],"%d",&amp;num);
        sscanf(argv[2],"%d",&amp;rate);
        sscanf(argv[3],"%d",&amp;temperature);
        sscanf(argv[4],"%d",&amp;gravity);

        btm_updateDataCmd(num,rate,temperature,gravity);
    }
    return 0;
}
</pre>
<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/tilt-hydrometer-part-5-tilt-simulator-multi-advertising-ibeacons/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Tilt Hydrometer (Part 3) Advertising Scanner</title>
		<link>https://iotexpert.com/tilt-hydrometer-advertising-scanner-part-3/</link>
					<comments>https://iotexpert.com/tilt-hydrometer-advertising-scanner-part-3/#comments</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Mon, 18 Jan 2021 16:59:42 +0000</pubDate>
				<category><![CDATA[Tilt Hydrometer Data Collection]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=10279</guid>

					<description><![CDATA[Summary In this article I will build the basic project and then add the scanner code to look for Tilt Hydrometers that are broadcasting the iBeacon.  It will decode the data and print out Gravity and Temperature. Build the Basic Project Start by making a new project using the CY8CKIT-062S2-43012.  I chose this kit because [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>In this article I will build the basic project and then add the scanner code to look for Tilt Hydrometers that are broadcasting the iBeacon.  It will decode the data and print out Gravity and Temperature.</p>
<p><span><p>This series is broken up into the following 12 articles with a few additional possible articles. </p>
<p><a href="https://iotexpert.com/tilt-hydrometer-overview-out-of-box-part-1/" target="_blank" rel="noopener">Tilt Hydrometer (Part 1) Overview &amp; Out-of-Box</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-architecture-part-2/" target="_blank" rel="noopener">Tilt Hydrometer (Part 2) Architecture</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-advertising-scanner-part-3/">Tilt Hydrometer (Part 3) Advertising Scanner</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-4-advertising-packet-error/" target="_blank" rel="noopener">Tilt Hydrometer (Part 4) Advertising Packet Error?</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-5-tilt-simulator-multi-advertising-ibeacons/">Tilt Hydrometer (Part 5) Tilt Simulator &amp; Multi Advertising iBeacons</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-6-tilt-simulator-an-upgrade/" target="_blank" rel="noopener">Tilt Hydrometer (Part 6) Tilt Simulator an Upgrade</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-7-advertising-database/">Tilt Hydrometer (Part 7) Advertising Database</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-8-read-the-database/">Tilt Hydrometer (Part 8) Read the Database</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-9-an-lcd-display/">Tilt Hydrometer (Part 9) An LCD Display</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-10-the-display-state-machine/">Tilt Hydrometer (Part 10) The Display State Machine</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-11-draw-the-display-screens/">Tilt Hydrometer (Part 11) Draw the Display Screens</a></p>
<p><a href="https://iotexpert.com/tilt-hydrometer-part-12-capsense/">Tilt Hydrometer (Part 12) CapSense</a></p>
<p>Tilt Hydrometer: LittleFS &amp; SPI Flash (Part ?)</p>
<p>Tilt Hydrometer: WiFi Introducer (Part ?)</p>
<p>Tilt Hydrometer: WebServer (Part ?)</p>
<p>Tilt Hydrometer: Amazon MQTT (Part ?)</p>
<p>Tilt Hydrometer: Printed Circuit Board (Part ?)</p>
<p>You can get the source code from git@github.com:iotexpert/Tilt2.git  This repository has tags for each of the articles which can be accessed with "git checkout part12"  You can find the Tilt Simulator at  git@github.com:iotexpert/TiltSimulator.git.</p>
<p>&nbsp;</p></span></p>
<h1>Build the Basic Project</h1>
<p>Start by making a new project using the CY8CKIT-062S2-43012.  I chose this kit because that was the one sitting on my desk at the time.  Any of the kits with the Arduino headers where I can plug in the TFT will work.</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-advertising-scanner-part-3/screen-shot-2020-11-19-at-6-07-54-am/" rel="attachment wp-att-10280"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.07.54-AM-1024x845.jpg" alt="" width="1024" height="845" class="alignnone size-large wp-image-10280" srcset="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.07.54-AM-1024x845.jpg 1024w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.07.54-AM-300x248.jpg 300w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.07.54-AM-768x634.jpg 768w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.07.54-AM-1536x1267.jpg 1536w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.07.54-AM-600x495.jpg 600w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.07.54-AM.jpg 1852w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>I typically start from the IoT Expert FreeRTOS NTShell Template.  Ill do that here as well.  Notice that I call the project Tilt2.  You can find this project on GitHub.  git@github.com:iotexpert/Tilt2.git .  I will also be putting in tags for each article so you can look at the code for each article.  To see the tags do &#8220;git tags&#8221;</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-advertising-scanner-part-3/screen-shot-2020-11-19-at-6-08-37-am/" rel="attachment wp-att-10281"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.08.37-AM-1024x845.jpg" alt="" width="1024" height="845" class="alignnone size-large wp-image-10281" srcset="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.08.37-AM-1024x845.jpg 1024w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.08.37-AM-300x248.jpg 300w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.08.37-AM-768x634.jpg 768w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.08.37-AM-1536x1268.jpg 1536w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.08.37-AM-600x495.jpg 600w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.08.37-AM.jpg 1844w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>After the project is made run &#8220;make modlibs&#8221; so that you can add some libraries.  Specifically the bluetooth-freertos and btstack libraries.</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-advertising-scanner-part-3/screen-shot-2020-11-19-at-6-11-42-am/" rel="attachment wp-att-10282"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.11.42-AM-1024x784.jpg" alt="" width="1024" height="784" class="alignnone size-large wp-image-10282" srcset="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.11.42-AM-1024x784.jpg 1024w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.11.42-AM-300x230.jpg 300w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.11.42-AM-768x588.jpg 768w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.11.42-AM-1536x1177.jpg 1536w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.11.42-AM-2048x1569.jpg 2048w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.11.42-AM-600x460.jpg 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now that I have all of the libraries I need, run &#8220;make vscode&#8221; to build the project files for vscode.</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-advertising-scanner-part-3/screen-shot-2020-11-19-at-6-12-46-am/" rel="attachment wp-att-10283"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.12.46-AM-1024x890.jpg" alt="" width="1024" height="890" class="alignnone size-large wp-image-10283" srcset="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.12.46-AM-1024x890.jpg 1024w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.12.46-AM-300x261.jpg 300w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.12.46-AM-768x667.jpg 768w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.12.46-AM-1536x1335.jpg 1536w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.12.46-AM-600x521.jpg 600w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.12.46-AM.jpg 1956w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>If you remember from the architecture diagram, I will have a task to manage the bluetooth world.  Inside of the btutil library I have a template to start with.  It also includes the stuff you need in main.c to start the stack.</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-advertising-scanner-part-3/screen-shot-2020-11-19-at-6-15-39-am/" rel="attachment wp-att-10284"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.15.39-AM-1024x227.png" alt="" width="1024" height="227" class="alignnone size-large wp-image-10284" srcset="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.15.39-AM-1024x227.png 1024w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.15.39-AM-300x67.png 300w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.15.39-AM-768x170.png 768w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.15.39-AM-600x133.png 600w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.15.39-AM.png 1272w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>You need to make two changes to the Makefile.  Well you dont have to change the name of the App &#8230; but I typically do.  You do need to add the FREERTOS and WICED_BLE components to your project.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">APPNAME=Tilt2


....


COMPONENTS=FREERTOS WICED_BLE</pre>
<p>Now, edit main.c to include some stuff.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include "bluetoothManager.h"
#include "cycfg_bt_settings.h"
#include "bt_platform_cfg_settings.h"</pre>
<p>And start the bluetooth stack.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">    cybt_platform_config_init(&amp;bt_platform_cfg_settings);
    wiced_bt_stack_init (app_bt_management_callback, &amp;wiced_bt_cfg_settings);</pre>
<p>You need to get the bluetooth configuration files.  To do that run the Bluetooth Configurator by running &#8220;make config_bt&#8221;.  Click the paper to make a new configuration.</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-advertising-scanner-part-3/screen-shot-2020-11-19-at-6-19-43-am/" rel="attachment wp-att-10286"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.19.43-AM-1024x755.png" alt="" width="1024" height="755" class="alignnone size-large wp-image-10286" srcset="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.19.43-AM-1024x755.png 1024w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.19.43-AM-300x221.png 300w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.19.43-AM-768x566.png 768w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.19.43-AM-600x442.png 600w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.19.43-AM.png 1270w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Pick out the PSoC6 MCU with 43xxxx Connectivity.</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-advertising-scanner-part-3/screen-shot-2020-11-19-at-6-19-58-am/" rel="attachment wp-att-10287"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.19.58-AM.png" alt="" width="894" height="322" class="alignnone size-large wp-image-10287" srcset="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.19.58-AM.png 894w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.19.58-AM-300x108.png 300w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.19.58-AM-768x277.png 768w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.19.58-AM-600x216.png 600w" sizes="(max-width: 894px) 100vw, 894px" /></a> <a href="https://iotexpert.com/tilt-hydrometer-advertising-scanner-part-3/screen-shot-2020-11-19-at-6-20-19-am/" rel="attachment wp-att-10288"></a></p>
<p>Go to the GAP Settings tab and add the Observer configuration</p>
<p><a href="https://iotexpert.com/?attachment_id=10289" rel="attachment wp-att-10289"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.20.25-AM.png" alt="" width="516" height="330" class="alignnone size-large wp-image-10289" srcset="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.20.25-AM.png 516w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.20.25-AM-300x192.png 300w" sizes="(max-width: 516px) 100vw, 516px" /></a></p>
<p>Then set the low duty scan parameters to 60ms and turn off the timeout.</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-advertising-scanner-part-3/screen-shot-2020-11-19-at-6-22-32-am/" rel="attachment wp-att-10290"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.22.32-AM-1024x665.png" alt="" width="1024" height="665" class="alignnone size-large wp-image-10290" srcset="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.22.32-AM-1024x665.png 1024w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.22.32-AM-300x195.png 300w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.22.32-AM-768x499.png 768w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.22.32-AM-1536x997.png 1536w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.22.32-AM-600x390.png 600w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.22.32-AM.png 1722w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Save your configuration.  Notice that I call it tilt2.</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-advertising-scanner-part-3/screen-shot-2020-11-19-at-6-22-51-am/" rel="attachment wp-att-10291"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.22.51-AM.png" alt="" width="902" height="436" class="alignnone size-large wp-image-10291" srcset="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.22.51-AM.png 902w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.22.51-AM-300x145.png 300w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.22.51-AM-768x371.png 768w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.22.51-AM-600x290.png 600w" sizes="(max-width: 902px) 100vw, 902px" /></a></p>
<p>Make sure that everything works with a build it and program.  You will have a working shell, blinking LED and the Bluetooth Task started.  Here is what the output looks like.</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-advertising-scanner-part-3/screen-shot-2020-11-19-at-6-35-45-am/" rel="attachment wp-att-10285"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.35.45-AM-1024x376.png" alt="" width="1024" height="376" class="alignnone size-large wp-image-10285" srcset="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.35.45-AM-1024x376.png 1024w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.35.45-AM-300x110.png 300w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.35.45-AM-768x282.png 768w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.35.45-AM-600x220.png 600w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-19-at-6.35.45-AM.png 1248w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>iBeacon Packet Format</h1>
<p>On the Tilt Hydrometer <a href="https://tilthydrometer.com/pages/faqs" target="_blank" rel="noopener noreferrer">FAQ</a> they give you a link to Karl Urdevics <a href="https://kvurd.com/blog/tilt-hydrometer-ibeacon-data-format/" target="_blank" rel="noopener noreferrer">blog</a> where he describes the way that the Tilt works.  Pretty simple, each Tilt Hydrometer is hardcoded to a color.  Each &#8220;color&#8221; sends out an <a href="https://en.wikipedia.org/wiki/IBeacon" target="_blank" rel="noopener noreferrer">iBeacon</a> with the Temperature, Gravity and Transmit Power.  iBeacon is just an Apple specified format for a BLE advertising packet.  You can read about the format of the BLE advertising packet <a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-3/" target="_blank" rel="noopener noreferrer">here</a>.  But, in summary, a BLE device can advertise up to 31 bytes of data.  That data is divided into fields of the following form:</p>
<ul>
<li>Field Type (1 byte) &#8211; The type is one of the <a href="https://www.bluetooth.com/specifications/assigned-numbers/generic-access-profile/" target="_blank" rel="noopener noreferrer">Bluetooth GAP Assigned Numbers</a></li>
<li>Length (1 byte)</li>
<li>Length -1 bytes</li>
</ul>
<p>There are a bunch of specific format types.  However, the one that we are looking for is the iBeacon format.  This format was defined by Apple and is implemented as data in the &#8220;<span>«Manufacturer Specific Data»&#8221; which has the field code 0xFF.  The Manufacturer Specific Data field type is divided into</span></p>
<ul>
<li>16-bit Manufacturers ID</li>
<li>Data</li>
</ul>
<p>For the iBeacon the <span>Manufacturer</span> ID is 0X004C &#8211; which is Apple&#8217;s ID.  They further subdivided the data.  Here is the complete format of that data.</p>
<div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >Bytes</th>
<th >Data</th>
<th >Name</th>
<th >Comment</th>
</tr>
</thead>
<tbody>
<tr><td >1</td>
<td >0xFF</td>
<td >Manufacturer Data</td>
<td >Bluetooth GAP Assigned Number</td>
</tr>

<tr><td >1</td>
<td >0x1A</td>
<td >Length of Field</td>
</tr>

<tr><td >2</td>
<td >0&#215;04 0x0C</td>
<td ><span>Manufacturers'</span> UUID</td>
<td >Apples Bluetooth <span>Manufacturer </span>ID</td>
</tr>

<tr><td >1</td>
<td >02</td>
<td >Apple defined</td>
<td > 0x02=Subtype iBeacon</td>
</tr>

<tr><td >1</td>
<td >0&#215;15</td>
<td >Length</td>
<td >Apple defined length</td>
</tr>

<tr><td >16</td>
<td >????</td>
<td >UUID</td>
<td >The universally unique identifier for the data type of the iBeacon (these are defined by Tilt)</td>
</tr>

<tr><td >2</td>
<td >????</td>
<td >Major Value</td>
<td >Gravity in 1/1000 of SG</td>
</tr>

<tr><td >2</td>
<td >????</td>
<td >Minor Value</td>
<td >Temperature in degrees F (yup, Imperial!)</td>
</tr>

<tr><td >1</td>
<td >??</td>
<td >Signal Power</td>
<td >dBm Transmit Power as measured at 1m</td>
</tr>
</tbody></table></div>
<p>It turns out that Tilt &#8220;hardcodes&#8221; the UUID during manufacturing for the color of the Tilt (remember each Tilt is one of 8 colors)</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">Red:    A495BB10C5B14B44B5121370F02D74DE
Green:  A495BB20C5B14B44B5121370F02D74DE
Black:  A495BB30C5B14B44B5121370F02D74DE
Purple: A495BB40C5B14B44B5121370F02D74DE
Orange: A495BB50C5B14B44B5121370F02D74DE
Blue:   A495BB60C5B14B44B5121370F02D74DE
Yellow: A495BB70C5B14B44B5121370F02D74DE
Pink:   A495BB80C5B14B44B5121370F02D74DE</pre>
<h1>Add Advertising Observer</h1>
<p>Now that we have a working project template, and we know what we are looking for in the BLE advertising land, I&#8217;ll setup some data structures to map of the colors and UUIDs.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#define TILT_IBEACON_HEADER_LEN 20
#define TILT_IBEACON_DATA_LEN 5
typedef struct  {
    char *colorName;
    uint8_t uuid[TILT_IBEACON_HEADER_LEN];
} tilt_t;

// Apple Bluetooth Company Code 0x004C
// iBeacon Subtype = 0x02
// Length = 0x15
#define IBEACON_HEADER 0x4C,0x00,0x02,0x15

static tilt_t tiltDB [] =
{
    {"Red",    {IBEACON_HEADER,0xA4,0x95,0xBB,0x10,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE}},
    {"Green" , {IBEACON_HEADER,0xA4,0x95,0xBB,0x20,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE}},
    {"Black" , {IBEACON_HEADER,0xA4,0x95,0xBB,0x30,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE}},
    {"Purple", {IBEACON_HEADER,0xA4,0x95,0xBB,0x40,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE}},
    {"Orange", {IBEACON_HEADER,0xA4,0x95,0xBB,0x50,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE}},
    {"Blue"  , {IBEACON_HEADER,0xA4,0x95,0xBB,0x60,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE}},
    {"Yellow", {IBEACON_HEADER,0xA4,0x95,0xBB,0x70,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE}},
    {"Pink"  , {IBEACON_HEADER,0xA4,0x95,0xBB,0x80,0xC5,0xB1,0x4B,0x44,0xB5,0x12,0x13,0x70,0xF0,0x2D,0x74,0xDE}},
};
#define NUM_TILT (sizeof(tiltDB)/sizeof(tilt_t))</pre>
<p>Then I will create and advertising callback that will</p>
<ul>
<li>Look for the manufacturer specific data field in the advertising packet</li>
<li>If the length of that field is 25 then we have found a possible iBeacon</li>
<li>Loop through the different possible Tilt&#8217;s</li>
<li>Compare the data in the advertising packet against the data + UUID in the packet</li>
<li>If it matches then decode the Gravity, txPower and Temperature and print it</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="c">static void btm_advScanResultCback(wiced_bt_ble_scan_results_t *p_scan_result, uint8_t *p_adv_data )
{
	if (p_scan_result == 0)
		return;

	uint8_t mfgFieldLen;
	uint8_t *mfgFieldData;
	mfgFieldData = wiced_bt_ble_check_advertising_data(p_adv_data,BTM_BLE_ADVERT_TYPE_MANUFACTURER,&amp;mfgFieldLen);
    
	if(mfgFieldData &amp;&amp; mfgFieldLen == TILT_IBEACON_HEADER_LEN + TILT_IBEACON_DATA_LEN)
    {
        for(int i=0;i&lt;NUM_TILT;i++)
        {
            if(memcmp(mfgFieldData,tiltDB[i].uuid,TILT_IBEACON_HEADER_LEN)==0)
            {
                float gravity = ((float)((uint16_t)mfgFieldData[22] &lt;&lt; 8 | (uint16_t)mfgFieldData[23]))/1000;
		        int temperature = mfgFieldData[20] &lt;&lt; 8 | mfgFieldData[21];
                int8_t txPower = mfgFieldData[24];

                printf("Found Color=%s Gravity=%f Temperature = %d txPower=%d\n",tiltDB[i].colorName,gravity,temperature,txPower);
                break;
            }
        }
    }
}</pre>
<p>In the Bluetooth Management callback I want to turn on scanning (actually observing).</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">        case BTM_ENABLED_EVT:
            if (WICED_BT_SUCCESS == p_event_data-&gt;enabled.status)
            {
				wiced_bt_ble_observe(WICED_TRUE, 0,btm_advScanResultCback);
            }
            else
            {
            	printf("Error enabling BTM_ENABLED_EVENT\n");
            }</pre>
<p>To test this thing I brought a black tilt into my office.  As soon as the PSoC saw it, packets starting coming out on the screen about once per second.  The first thing to notice is that they broadcast some crazy data at the start.  That means I should be careful with the error checks (something which if you are a reader you know that I am not always perfect <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> .  It is also interesting to see that they broadcast 5 packets at 5dBm, then 5 at -59dBm.</p>
<p><a href="https://iotexpert.com/tilt-hydrometer-advertising-scanner-part-3/screen-shot-2020-11-20-at-10-30-23-am/" rel="attachment wp-att-10296"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-20-at-10.30.23-AM-1024x1021.jpg" alt="" width="1024" height="1021" class="alignnone size-large wp-image-10296" srcset="https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-20-at-10.30.23-AM-1024x1021.jpg 1024w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-20-at-10.30.23-AM-300x300.jpg 300w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-20-at-10.30.23-AM-150x150.jpg 150w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-20-at-10.30.23-AM-768x765.jpg 768w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-20-at-10.30.23-AM-500x500.jpg 500w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-20-at-10.30.23-AM-600x598.jpg 600w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-20-at-10.30.23-AM-100x100.jpg 100w, https://iotexpert.com/wp-content/uploads/2020/11/Screen-Shot-2020-11-20-at-10.30.23-AM.jpg 1196w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now that we have data coming out, in the next article Ill address a couple of funky things that I noticed.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/tilt-hydrometer-advertising-scanner-part-3/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>AnyCloud Bluetooth Advertising Scanner (Part 2)</title>
		<link>https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-2/</link>
					<comments>https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-2/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Mon, 26 Oct 2020 11:06:12 +0000</pubDate>
				<category><![CDATA[AnyCloud Advertising Scanner]]></category>
		<category><![CDATA[Bluetooth]]></category>
		<category><![CDATA[CY8CKIT-062-WiFi-BT]]></category>
		<category><![CDATA[CY8CKIT-062S2-43012]]></category>
		<category><![CDATA[ModusToolbox]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=10131</guid>

					<description><![CDATA[Summary The second article in a series discussing the creation of a PSoC 6 + CYW43xxx Advertising Scanner using the AnyCloud SDK.  This article will use the learning from Part 1 to create a template project that starts the BLE stack. Story In the previous article I discussed the structure of the Cypress/Infineon Bluetooth Stack [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>The second article in a series discussing the creation of a PSoC 6 + CYW43xxx Advertising Scanner using the AnyCloud SDK.  This article will use the learning from <a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-1/">Part 1</a> to create a template project that starts the BLE stack.</p>
<h1>Story</h1>
<p>In the <a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-1/">previous article</a> I discussed the structure of the Cypress/Infineon Bluetooth Stack and its integration into AnyCloud.  A bunch of &#8220;theory&#8221;, well I say BS to that.  Let&#8217;s build something.</p>
<p>There are</p>
<p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >Article</th>
<th >Topic</th>
</tr>
</thead>
<tbody>
<tr><td ><a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-1/" target="_blank" rel="noopener">AnyCloud Bluetooth Advertising Scanner (Part 1)</a></td>
<td >Introduction to AnyCloud Bluetooth Advertising</td>
</tr>

<tr><td ><a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-2/" target="_blank" rel="noopener">AnyCloud Bluetooth Advertising Scanner (Part 2)</a></td>
<td >Creating an AnyCloud Bluetooth project</td>
</tr>

<tr><td ><a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-3/" target="_blank" rel="noopener">AnyCloud Bluetooth Advertising Scanner (Part 3)</a></td>
<td >Adding Observing functionality to the project</td>
</tr>

<tr><td ><a class="row-title" href="https://iotexpert.com/anycloud-bluetooth-utilities-library/" aria-label="“AnyCloud Bluetooth Utilities Library” (Edit)" target="_blank" rel="noopener">AnyCloud Bluetooth Utilities Library</a></td>
<td >A set of APIs for enhancement of the AnyCloud Library</td>
</tr>

<tr><td ><a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-4/" target="_blank" rel="noopener">AnyCloud Bluetooth Advertising Scanner (Part 4)</a></td>
<td >Adding a command line to the scanner</td>
</tr>

<tr><td ><a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-5/" target="_blank" rel="noopener">AnyCloud Bluetooth Advertising Scanner (Part 5)</a></td>
<td >Adding a history database to the scanner</td>
</tr>

<tr><td ><a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-6/" target="_blank" rel="noopener">AnyCloud Bluetooth Advertising Scanner (Part 6)</a></td>
<td >Decoding advertising packets</td>
</tr>

<tr><td ><a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-7/" target="_blank" rel="noopener">AnyCloud Bluetooth Advertising Scanner (Part 7)</a></td>
<td >Adding recording commands to the command line</td>
</tr>

<tr><td ><a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-8/">AnyCloud Bluetooth Advertising Scanner (Part 8)</a></td>
<td >Adding filtering to the scanner</td>
</tr>

<tr><td ><a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-9/">AnyCloud Bluetooth Advertising Scanner (Part 9)</a></td>
<td >Improve the print and add packet age</td>
</tr>

<tr><td ><a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-10/">AnyCloud Bluetooth Advertising Scanner (Part 10)</a></td>
<td >Sort the database</td>
</tr>
</tbody></table></div></p>
<p>All of the code can be found at git@github.com:iotexpert/AnyCloudBLEScanner.git and https://github.com/iotexpert/AnyCloudBLEScanner.git</p>
<p>There are git tags in place starting at part 5 so that you can look at just that version of the code.  "git tag" to list the tags.  And "git checkout part6" to look at the part 6 version of the code.</p>
<p>You can also create a new project with this is a template if you have the IoT Expert Manifest Files installed</p>
<p>Recall from <a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-1/">Part 1</a> that you need three things to startup the Bluetooth Stack</p>
<ul>
<li>The Hardware Configuration Structure that matches : cybt_platform_config_t</li>
<li>The Bluetooth Stack Configuration Structure that matches : wiced_bt_cfg_settings_t</li>
<li>The Bluetooth Management Callback that matches : typedef wiced_result_t (wiced_bt_management_cback_t) (wiced_bt_management_evt_t event, wiced_bt_management_evt_data_t *p_event_data);</li>
</ul>
<p>Then you need to call</p>
<ul>
<li>The hardware initialization function : cybt_platform_config_init</li>
<li>The stack initialization function : wiced_bt_stack_init</li>
</ul>
<p>Ok let&#8217;s do this!</p>
<h1>Basic Project</h1>
<p>You can do all of these steps from the Eclipse IDE for ModusToolbox.  Or you can do it from the individual programs and the command line.  I like Visual Studio code, so this article will be done completely from the command line and individual configurators.</p>
<p>Run the new project creator from the start menu.  Start by creating a project for the development kit that you have, in my case the one currently plugged into my computer is the CY8CKIT-062S2-43012, so that is what I pick.  But, this project will work with any of the WiFI/BT combo chips attached to PSoC 6.</p>
<p><a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-2/screen-shot-2020-10-25-at-8-18-19-am/" rel="attachment wp-att-10143"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.18.19-AM-1024x613.jpg" alt="" width="1024" height="613" class="alignnone size-large wp-image-10143" srcset="https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.18.19-AM-1024x613.jpg 1024w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.18.19-AM-300x179.jpg 300w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.18.19-AM-768x459.jpg 768w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.18.19-AM-1536x919.jpg 1536w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.18.19-AM-2048x1225.jpg 2048w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.18.19-AM-600x359.jpg 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>In previous articles I discussed the template that I use to get things going with FreeRTOS.  I won&#8217;t discuss that here, but I want FreeRTOS and the NTShell, so pick the IoT Expert FreeRTOS NTShell Template.</p>
<p><a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-2/screen-shot-2020-10-25-at-8-19-14-am/" rel="attachment wp-att-10144"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.19.14-AM-1024x612.jpg" alt="" width="1024" height="612" class="alignnone size-large wp-image-10144" srcset="https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.19.14-AM-1024x612.jpg 1024w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.19.14-AM-300x179.jpg 300w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.19.14-AM-768x459.jpg 768w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.19.14-AM-1536x918.jpg 1536w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.19.14-AM-2048x1223.jpg 2048w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.19.14-AM-600x358.jpg 600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>After about a minute you should have a project.  I always like to build the project to make sure that everything is working before I get too far down the road of modifying anything.  Run &#8220;make -j build&#8221;</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">arh (master) AnyCloudBLEScanner $ make -j build
Tools Directory: /Applications/ModusToolbox/tools_2.2
CY8CKIT-062S2-43012.mk: ../mtb_shared/TARGET_CY8CKIT-062S2-43012/latest-v2.X/CY8CKIT-062S2-43012.mk

Prebuild operations complete
Commencing build operations...

Tools Directory: /Applications/ModusToolbox/tools_2.2
CY8CKIT-062S2-43012.mk: ../mtb_shared/TARGET_CY8CKIT-062S2-43012/latest-v2.X/CY8CKIT-062S2-43012.mk

Initializing build: MTBShellTemplate Debug CY8CKIT-062S2-43012 GCC_ARM

Auto-discovery in progress...
-&gt; Found 205 .c file(s)
-&gt; Found 46 .S file(s)
-&gt; Found 23 .s file(s)
-&gt; Found 0 .cpp file(s)
-&gt; Found 0 .o file(s)
-&gt; Found 6 .a file(s)
-&gt; Found 503 .h file(s)
-&gt; Found 0 .hpp file(s)
-&gt; Found 0 resource file(s)
Applying filters...
Auto-discovery complete

Constructing build rules...
Build rules construction complete

==============================================================================
= Building application =
==============================================================================
Generating compilation database file...
-&gt; ./build/compile_commands.json
Compilation database file generation complete
Building 193 file(s)
    Compiling app file lowPower.c
    Compiling app file main.c
    Compiling app file usrcmd.c
    Compiling ext file startup_psoc6_02_cm4.S

..... a bunch of lines deleted

    Compiling ext file psoc6_01_cm0p_sleep.c
    Compiling ext file psoc6_02_cm0p_sleep.c
    Compiling ext file psoc6_03_cm0p_sleep.c
    Compiling ext file psoc6_04_cm0p_sleep.c
    Compiling ext file cy_retarget_io.c
    Linking output file MTBShellTemplate.elf
==============================================================================
= Build complete =
==============================================================================

Calculating memory consumption: CY8C624ABZI-S2D44 GCC_ARM -Og

   ---------------------------------------------------- 
  | Section Name         |  Address      |  Size       | 
   ---------------------------------------------------- 
  | .cy_m0p_image        |  0x10000000   |  6044       | 
  | .text                |  0x10002000   |  54876      | 
  | .ARM.exidx           |  0x1000f65c   |  8          | 
  | .copy.table          |  0x1000f664   |  24         | 
  | .zero.table          |  0x1000f67c   |  8          | 
  | .data                |  0x080022e0   |  1688       | 
  | .cy_sharedmem        |  0x08002978   |  8          | 
  | .noinit              |  0x08002980   |  148        | 
  | .bss                 |  0x08002a14   |  2136       | 
  | .heap                |  0x08003270   |  1029520    | 
   ---------------------------------------------------- 

  Total Internal Flash (Available)          2097152    
  Total Internal Flash (Utilized)           64812      

  Total Internal SRAM (Available)           1046528    
  Total Internal SRAM (Utilized with heap)  1033500    

</pre>
<p>Then to be sure it is working, program the development kit.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">arh (master) AnyCloudBLEScanner $ make program
Tools Directory: /Applications/ModusToolbox/tools_2.2
CY8CKIT-062S2-43012.mk: ../mtb_shared/TARGET_CY8CKIT-062S2-43012/latest-v2.X/CY8CKIT-062S2-43012.mk

Prebuild operations complete
Commencing build operations...

Tools Directory: /Applications/ModusToolbox/tools_2.2
CY8CKIT-062S2-43012.mk: ../mtb_shared/TARGET_CY8CKIT-062S2-43012/latest-v2.X/CY8CKIT-062S2-43012.mk

Initializing build: MTBShellTemplate Debug CY8CKIT-062S2-43012 GCC_ARM

Auto-discovery in progress...
-&gt; Found 205 .c file(s)
-&gt; Found 46 .S file(s)
-&gt; Found 23 .s file(s)
-&gt; Found 0 .cpp file(s)
-&gt; Found 0 .o file(s)
-&gt; Found 6 .a file(s)
-&gt; Found 503 .h file(s)
-&gt; Found 0 .hpp file(s)
-&gt; Found 0 resource file(s)
Applying filters...
Auto-discovery complete

Constructing build rules...
Build rules construction complete

==============================================================================
= Building application =
==============================================================================
Generating compilation database file...
-&gt; ./build/compile_commands.json
Compilation database file generation complete
Building 193 file(s)
==============================================================================
= Build complete =
==============================================================================

Calculating memory consumption: CY8C624ABZI-S2D44 GCC_ARM -Og

   ---------------------------------------------------- 
  | Section Name         |  Address      |  Size       | 
   ---------------------------------------------------- 
  | .cy_m0p_image        |  0x10000000   |  6044       | 
  | .text                |  0x10002000   |  54876      | 
  | .ARM.exidx           |  0x1000f65c   |  8          | 
  | .copy.table          |  0x1000f664   |  24         | 
  | .zero.table          |  0x1000f67c   |  8          | 
  | .data                |  0x080022e0   |  1688       | 
  | .cy_sharedmem        |  0x08002978   |  8          | 
  | .noinit              |  0x08002980   |  148        | 
  | .bss                 |  0x08002a14   |  2136       | 
  | .heap                |  0x08003270   |  1029520    | 
   ---------------------------------------------------- 

  Total Internal Flash (Available)          2097152    
  Total Internal Flash (Utilized)           64812      

  Total Internal SRAM (Available)           1046528    
  Total Internal SRAM (Utilized with heap)  1033500    


Programming target device... 
Open On-Chip Debugger 0.10.0+dev-4.1.0.1058 (2020-08-11-03:45)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "swd". To override use 'transport select &lt;transport&gt;'.
adapter speed: 2000 kHz
adapter srst delay: 25
adapter srst pulse_width: 25
** Auto-acquire enabled, use "set ENABLE_ACQUIRE 0" to disable
cortex_m reset_config sysresetreq
cortex_m reset_config sysresetreq
Info : Using CMSIS loader 'CY8C6xxA_SMIF' for bank 'psoc6_smif0_cm0' (footprint 14672 bytes)
Warn : SFlash programming allowed for regions: USER, TOC, KEY
Info : CMSIS-DAP: SWD  Supported
Info : CMSIS-DAP: FW Version = 2.0.0
Info : CMSIS-DAP: Interface Initialised (SWD)
Info : SWCLK/TCK = 1 SWDIO/TMS = 1 TDI = 0 TDO = 0 nTRST = 0 nRESET = 1
Info : CMSIS-DAP: Interface ready
Info : KitProg3: FW version: 1.14.514
Info : KitProg3: Pipelined transfers disabled, please update the firmware
Info : VTarget = 3.215 V
Info : kitprog3: acquiring the device...
Info : clock speed 2000 kHz
Info : SWD DPIDR 0x6ba02477
Info : psoc6.cpu.cm0: hardware has 4 breakpoints, 2 watchpoints
***************************************
** Silicon: 0xE453, Family: 0x102, Rev.: 0x12 (A1)
** Detected Device: CY8C624ABZI-S2D44
** Detected Main Flash size, kb: 2048
** Flash Boot version: 3.1.0.378
** Chip Protection: NORMAL
***************************************
Info : psoc6.cpu.cm4: hardware has 6 breakpoints, 4 watchpoints
Info : starting gdb server for psoc6.cpu.cm0 on 3333
Info : Listening on port 3333 for gdb connections
Info : starting gdb server for psoc6.cpu.cm4 on 3334
Info : Listening on port 3334 for gdb connections
Info : SWD DPIDR 0x6ba02477
Info : kitprog3: acquiring the device...
psoc6.cpu.cm0 halted due to debug-request, current mode: Thread 
xPSR: 0x41000000 pc: 0x00000190 msp: 0x080ff800
** Device acquired successfully
** psoc6.cpu.cm4: Ran after reset and before halt...
psoc6.cpu.cm4 halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x0000012a msp: 0x080ff800
** Programming Started **
auto erase enabled
Info : Flash write discontinued at 0x1000179c, next section at 0x10002000
Info : Padding image section 0 at 0x1000179c with 100 bytes (bank write end alignment)
[100%] [################################] [ Erasing     ]
[100%] [################################] [ Programming ]
Info : Padding image section 1 at 0x1000fd24 with 220 bytes (bank write end alignment)
[100%] [################################] [ Erasing     ]
[100%] [################################] [ Programming ]
wrote 62976 bytes from file /Users/arh/mtw/AnyCloudBLEScanner/build/CY8CKIT-062S2-43012/Debug/MTBShellTemplate.hex in 2.069358s (29.719 KiB/s)
** Programming Finished **
** Verify Started **
verified 62656 bytes in 0.122208s (500.683 KiB/s)
** Verified OK **
** Resetting Target **
Info : SWD DPIDR 0x6ba02477
shutdown command invoked
Info : psoc6.dap: powering down debug domain...
  
arh (master) AnyCloudBLEScanner $</pre>
<p>When that is done, open up a terminal window and you should have a functioning base project.  Notice that I ran &#8220;help&#8221; and &#8220;tasks&#8221; from the command shell.</p>
<p><a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-2/screen-shot-2020-10-25-at-8-22-34-am/" rel="attachment wp-att-10145"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.22.34-AM-1024x700.png" alt="" width="1024" height="700" class="alignnone size-large wp-image-10145" srcset="https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.22.34-AM-1024x700.png 1024w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.22.34-AM-300x205.png 300w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.22.34-AM-768x525.png 768w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.22.34-AM-600x410.png 600w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.22.34-AM.png 1214w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now that we have a basic project working, add the Bluetooth libraries.  Run the library manager by typing &#8220;make modlibs&#8221;.  Then select &#8220;bluetooth-freertos&#8221; and the library manager will automatically select the other libraries you need.  Press Update then Close.</p>
<p><a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-2/screen-shot-2020-10-25-at-8-27-00-am/" rel="attachment wp-att-10146"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.27.00-AM-1024x799.jpg" alt="" width="1024" height="799" class="alignnone size-large wp-image-10146" srcset="https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.27.00-AM-1024x799.jpg 1024w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.27.00-AM-300x234.jpg 300w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.27.00-AM-768x599.jpg 768w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.27.00-AM-1536x1199.jpg 1536w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.27.00-AM-600x468.jpg 600w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.27.00-AM.jpg 1668w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Next, run the bluetooth configurator by running &#8220;make config_bt&#8221;  This tool will help you make the bluetooth stack configuration structure.  When the configurator starts, press &#8220;New&#8221;</p>
<p><a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-2/screen-shot-2020-10-25-at-8-34-00-am/" rel="attachment wp-att-10147"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.34.00-AM-1024x792.png" alt="" width="1024" height="792" class="alignnone size-large wp-image-10147" srcset="https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.34.00-AM-1024x792.png 1024w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.34.00-AM-300x232.png 300w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.34.00-AM-768x594.png 768w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.34.00-AM-1536x1187.png 1536w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.34.00-AM-600x464.png 600w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.34.00-AM.png 1568w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Then select our device (the PSoC 6 and the Combo chip)</p>
<p><a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-2/screen-shot-2020-10-25-at-8-34-13-am/" rel="attachment wp-att-10148"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.34.13-AM.png" alt="" width="796" height="242" class="alignnone size-large wp-image-10148" srcset="https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.34.13-AM.png 796w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.34.13-AM-300x91.png 300w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.34.13-AM-768x233.png 768w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.34.13-AM-600x182.png 600w" sizes="(max-width: 796px) 100vw, 796px" /></a></p>
<p>Click on the &#8220;GAP Settings&#8221;.  Then press the Plus and add &#8220;Observer configuration&#8221;</p>
<p><a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-2/screen-shot-2020-10-25-at-8-34-41-am/" rel="attachment wp-att-10149"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.34.41-AM.png" alt="" width="874" height="472" class="alignnone size-large wp-image-10149" srcset="https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.34.41-AM.png 874w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.34.41-AM-300x162.png 300w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.34.41-AM-768x415.png 768w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.34.41-AM-600x324.png 600w" sizes="(max-width: 874px) 100vw, 874px" /></a></p>
<p>Then setup the scan settings (more detail on these numbers in the next article)</p>
<ul>
<li>Low duty scan window (ms) = 60</li>
<li>Low duty scan interval (ms) = 60</li>
<li>Low duty scan timeout = deselected (meaning no timeout)</li>
</ul>
<p><a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-2/screen-shot-2020-10-25-at-8-37-48-am/" rel="attachment wp-att-10150"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.37.48-AM-1024x931.png" alt="" width="1024" height="931" class="alignnone size-large wp-image-10150" srcset="https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.37.48-AM-1024x931.png 1024w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.37.48-AM-300x273.png 300w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.37.48-AM-768x698.png 768w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.37.48-AM-1536x1397.png 1536w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.37.48-AM-600x546.png 600w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.37.48-AM.png 1630w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Then save your configuration file.  Notice that I called it &#8220;btconfig&#8221;</p>
<p><a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-2/screen-shot-2020-10-25-at-8-38-23-am/" rel="attachment wp-att-10151"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.38.23-AM.png" alt="" width="908" height="436" class="alignnone size-large wp-image-10151" srcset="https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.38.23-AM.png 908w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.38.23-AM-300x144.png 300w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.38.23-AM-768x369.png 768w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.38.23-AM-600x288.png 600w" sizes="(max-width: 908px) 100vw, 908px" /></a></p>
<p>When you are done you will have a directory called &#8220;GeneratedSource&#8221; inside of your project with the needed files.</p>
<p><a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-2/screen-shot-2020-10-25-at-8-38-57-am/" rel="attachment wp-att-10152"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.38.57-AM-1024x197.png" alt="" width="1024" height="197" class="alignnone size-large wp-image-10152" srcset="https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.38.57-AM-1024x197.png 1024w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.38.57-AM-300x58.png 300w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.38.57-AM-768x148.png 768w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.38.57-AM-600x116.png 600w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-8.38.57-AM.png 1266w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>The next step is to fix up the Makefile.  I like changing the name of the &#8220;App&#8221;.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c"># Name of application (used to derive name of final linked file).
APPNAME=AnyCloudBLEScanner</pre>
<p>Then you need the &#8220;FREERTOS WICED_BLE&#8221; components.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c"># Enable optional code that is ordinarily disabled by default.
#
# Available components depend on the specific targeted hardware and firmware
# in use. In general, if you have
#
#    COMPONENTS=foo bar
#
# ... then code in directories named COMPONENT_foo and COMPONENT_bar will be
# added to the build
#
COMPONENTS=FREERTOS WICED_BLE</pre>
<p>If you run make vscode it will update the workspace with all of the stuff needed for Visual Studio Code to be able to find all of the files.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">arh (master) AnyCloudBLEScanner $ make vscode
Tools Directory: /Applications/ModusToolbox/tools_2.2
CY8CKIT-062S2-43012.mk: ../mtb_shared/TARGET_CY8CKIT-062S2-43012/latest-v2.X/CY8CKIT-062S2-43012.mk

Prebuild operations complete
Commencing build operations...

Tools Directory: /Applications/ModusToolbox/tools_2.2
CY8CKIT-062S2-43012.mk: ../mtb_shared/TARGET_CY8CKIT-062S2-43012/latest-v2.X/CY8CKIT-062S2-43012.mk

Initializing build: MTBShellTemplate Debug CY8CKIT-062S2-43012 GCC_ARM

Auto-discovery in progress...
-&gt; Found 230 .c file(s)
-&gt; Found 46 .S file(s)
-&gt; Found 23 .s file(s)
-&gt; Found 0 .cpp file(s)
-&gt; Found 0 .o file(s)
-&gt; Found 22 .a file(s)
-&gt; Found 561 .h file(s)
-&gt; Found 0 .hpp file(s)
-&gt; Found 0 resource file(s)
Applying filters...
Auto-discovery complete

Constructing build rules...
Build rules construction complete

==============================================================================
= Generating IDE files =
==============================================================================

==============================================================================
= Building application =
==============================================================================
Generating compilation database file...
-&gt; ./build/compile_commands.json
Compilation database file generation complete

echo "The existing MTBShellTemplate.code-workspace file has been saved to .vscode/backup";
The existing MTBShellTemplate.code-workspace file has been saved to .vscode/backup
The existing c_cpp_properties.json file has been saved to .vscode/backup
The existing launch.json file has been saved to .vscode/backup
Modifying existing settings.json file. Check against the backup copy in .vscode/backup
The existing tasks.json file has been saved to .vscode/backup

Generated Visual Studio Code files: c_cpp_properties.json launch.json openocd.tcl settings.json tasks.json

J-Link users, please see the comments at the top of the launch.json
   file about setting the location of the gdb-server.

Instructions:
1. Review the modustoolbox.toolsPath property in .vscode/settings.json
2. Open VSCode
3. Install "C/C++" and "Cortex-Debug" extensions
4. File-&gt;Open Folder (Welcome page-&gt;Start-&gt;Open folder)
5. Select the app root directory and open
6. Builds: Terminal-&gt;Run Task
7. Debugging: "Bug icon" on the left-hand pane

  
arh (master) AnyCloudBLEScanner $</pre>
<p>Inside of Visual Studio Code, create a new file called &#8220;bt_platform_cfg_settings.h&#8221; and add:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include "cybt_platform_config.h"
#include "cybsp.h"
#include "wiced_bt_stack.h"

extern const cybt_platform_config_t bt_platform_cfg_settings;</pre>
<p>Inside of Visual Studio Code, create a new file called &#8220;bt_platform_cfg_settings.c&#8221; and add:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include "cybt_platform_config.h"
#include "cybsp.h"
#include "wiced_bt_stack.h"

const cybt_platform_config_t bt_platform_cfg_settings =
{
    .hci_config =
    {
        .hci_transport = CYBT_HCI_UART,

        .hci =
        {
            .hci_uart =
            {
                .uart_tx_pin = CYBSP_BT_UART_TX,
                .uart_rx_pin = CYBSP_BT_UART_RX,
                .uart_rts_pin = CYBSP_BT_UART_RTS,
                .uart_cts_pin = CYBSP_BT_UART_CTS,

                .baud_rate_for_fw_download = 115200,
                .baud_rate_for_feature     = 115200,

                .data_bits = 8,
                .stop_bits = 1,
                .parity = CYHAL_UART_PARITY_NONE,
                .flow_control = WICED_TRUE
            }
        }
    },

    .controller_config =
    {
        .bt_power_pin      = CYBSP_BT_POWER,
        .sleep_mode =
        {
            #if (bt_0_power_0_ENABLED == 1) /* BT Power control is enabled in the LPA */
            #if (CYCFG_BT_LP_ENABLED == 1) /* Low power is enabled in the LPA, use the LPA configuration */
            .sleep_mode_enabled = true,
            .device_wakeup_pin = CYCFG_BT_DEV_WAKE_GPIO,
            .host_wakeup_pin = CYCFG_BT_HOST_WAKE_GPIO,
            .device_wake_polarity = CYCFG_BT_DEV_WAKE_POLARITY,
            .host_wake_polarity = CYCFG_BT_HOST_WAKE_IRQ_EVENT
            #else /* Low power is disabled in the LPA, disable low power */
            .sleep_mode_enabled = false
            #endif
            #else /* BT Power control is disabled in the LPA – default to BSP low power configuration */
            .sleep_mode_enabled = true,
            .device_wakeup_pin = CYBSP_BT_DEVICE_WAKE,
            .host_wakeup_pin = CYBSP_BT_HOST_WAKE,
            .device_wake_polarity = CYBT_WAKE_ACTIVE_LOW,
            .host_wake_polarity = CYBT_WAKE_ACTIVE_LOW
            #endif
        }
    },

    .task_mem_pool_size    = 2048
};</pre>
<p>Inside of Visual Studio Code, create bluetoothManager.h.  Remember this is the Bluetooth Stack Management Callback</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#pragma once
#include "wiced_bt_stack.h"
#include "wiced_bt_dev.h"

wiced_result_t app_bt_management_callback(wiced_bt_management_evt_t event, wiced_bt_management_evt_data_t *p_event_data);
</pre>
<p>Inside of Visual Studio code, create bluetoothManager.c.  This function does a whole lotta nothin&#8230; except saying that things got started.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

#include "cybsp.h"

#include "FreeRTOS.h"

#include "bluetoothManager.h"
#include "wiced_bt_stack.h"
#include "wiced_bt_dev.h"
#include "wiced_bt_trace.h"


/**************************************************************************************************
* Function Name: app_bt_management_callback()
***************************************************************************************************
* Summary:
*   This is a Bluetooth stack event handler function to receive management events from
*   the BLE stack and process as per the application.
*
* Parameters:
*   wiced_bt_management_evt_t event             : BLE event code of one byte length
*   wiced_bt_management_evt_data_t *p_event_data: Pointer to BLE management event structures
*
* Return:
*  wiced_result_t: Error code from WICED_RESULT_LIST or BT_RESULT_LIST
*
*************************************************************************************************/
wiced_result_t app_bt_management_callback(wiced_bt_management_evt_t event, wiced_bt_management_evt_data_t *p_event_data)
{
    wiced_result_t result = WICED_BT_SUCCESS;

    switch (event)
    {
        case BTM_ENABLED_EVT:

            if (WICED_BT_SUCCESS == p_event_data-&gt;enabled.status)
            {
                printf("Started BT Stack Succesfully\n");
            }
            else
            {
            	printf("Error enabling BTM_ENABLED_EVENT\n");
            }

            break;

        default:
            printf("Unhandled Bluetooth Management Event: 0x%x\n", event);
            break;
    }

    return result;
}
</pre>
<p>Next, update main.c with the required includes.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include "bluetoothManager.h"
#include "cycfg_bt_settings.h"
#include "bt_platform_cfg_settings.h"</pre>
<p>Then update main.c to start the stack</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">    cybt_platform_config_init(&amp;bt_platform_cfg_settings);
    wiced_bt_stack_init (app_bt_management_callback, &amp;wiced_bt_cfg_settings);</pre>
<p>Now build and program it, remember &#8220;make -j build&#8221; and &#8220;make program&#8221;.  Look, we have a functioning stack with the two bluetooth thread running.</p>
<p><a href="https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-2/screen-shot-2020-10-25-at-10-53-51-am/" rel="attachment wp-att-10154"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-10.53.51-AM-1024x699.png" alt="" width="1024" height="699" class="alignnone size-large wp-image-10154" srcset="https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-10.53.51-AM-1024x699.png 1024w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-10.53.51-AM-300x205.png 300w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-10.53.51-AM-768x524.png 768w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-10.53.51-AM-600x410.png 600w, https://iotexpert.com/wp-content/uploads/2020/10/Screen-Shot-2020-10-25-at-10.53.51-AM.png 1210w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>In the next article Ill finally get around to building the Bluetooth Scanner.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/anycloud-bluetooth-advertising-scanner-part-2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>PSoC 6 SDK OneWire Bus (Part 1) : Build Basic Project</title>
		<link>https://iotexpert.com/psoc-6-sdk-onewire-bus-part-1-build-basic-project/</link>
					<comments>https://iotexpert.com/psoc-6-sdk-onewire-bus-part-1-build-basic-project/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Mon, 15 Jun 2020 12:00:28 +0000</pubDate>
				<category><![CDATA[PSoC 6]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=9269</guid>

					<description><![CDATA[Summary This is the first article in a series about my journey implementing PSoC 6 SDK libraries for the Maxxim One Wire Bus and the DS18B20 temperature sensor. As you can see there will be many parts to this story: Story I recently got a twitter message from a gentleman named &#8220;Neeraj Dhekale&#8221;.  He asked [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>This is the first article in a series about my journey implementing PSoC 6 SDK libraries for the Maxxim One Wire Bus and the DS18B20 temperature sensor.</p>
<p>As you can see there will be many parts to this story:</p>
<p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >Article</th>
</tr>
</thead>
<tbody>
<tr><td ><a href="https://iotexpert.com/2020/06/15/psoc-6-sdk-onewire-bus-part-1-build-basic-project/" target="_blank" rel="noopener">PSoC 6 SDK OneWire Bus (Part 1) : Build Basic Project</a></td>
</tr>

<tr><td ><a href="https://iotexpert.com/2020/06/22/psoc-6-sdk-onewire-bus-part-2-implement-read-write/" target="_blank" rel="noopener">PSoC 6 SDK OneWire Bus (Part 2) : Implement Read &amp; Write</a></td>
</tr>

<tr><td ><a href="https://iotexpert.com/2020/06/29/psoc-6-sdk-onewire-bus-part-3-remove-busy-wait-debug/" target="_blank" rel="noopener">PSoC 6 SDK OneWire Bus (Part 3) : Remove Busy Wait &amp; Debug</a></td>
</tr>

<tr><td ><a href="https://iotexpert.com/?p=9329" target="_blank" rel="noopener">PSoC 6 SDK OneWire Bus (Part 4): But, Can it Read the Temperature?</a></td>
</tr>

<tr><td >PSoC 6 SDK OneWire Bus (Part 5): Round out the OWB Driver</td>
</tr>
</tbody></table></div></p>
<h1>Story</h1>
<p>I recently got a twitter message from a gentleman named &#8220;Neeraj Dhekale&#8221;.  He asked about a library for a one wire sensor for PSoC, actually to be specific he asked about a component for a PSoC 4.</p>
<p><a href="https://iotexpert.com/?attachment_id=9272" rel="attachment wp-att-9272"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.17.25-AM-1024x255.png" alt="" width="1024" height="255" class="alignnone size-large wp-image-9272" srcset="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.17.25-AM-1024x255.png 1024w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.17.25-AM-600x149.png 600w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.17.25-AM-300x75.png 300w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.17.25-AM-768x191.png 768w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.17.25-AM.png 1230w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Then I asked him what sensor and responded that he wanted to use the <a href="https://www.mouser.com/datasheet/2/256/DS18B20-370043.pdf" target="_blank" rel="noopener noreferrer">Maxxim DS18B20</a> temperature sensor.</p>
<p><a href="https://iotexpert.com/?attachment_id=9274" rel="attachment wp-att-9274"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.24.33-AM-1024x193.png" alt="" width="1024" height="193" class="alignnone size-large wp-image-9274" srcset="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.24.33-AM-1024x193.png 1024w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.24.33-AM-600x113.png 600w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.24.33-AM-300x57.png 300w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.24.33-AM-768x145.png 768w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.24.33-AM.png 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>This sensor is a one wire temperature sensor, here is a bit of snapshot from the data sheet.</p>
<p><a href="https://iotexpert.com/?attachment_id=9275" rel="attachment wp-att-9275"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.47.17-AM-1024x632.png" alt="" width="1024" height="632" class="alignnone size-large wp-image-9275" srcset="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.47.17-AM-1024x632.png 1024w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.47.17-AM-600x370.png 600w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.47.17-AM-300x185.png 300w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.47.17-AM-768x474.png 768w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.47.17-AM-1536x948.png 1536w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.47.17-AM-2048x1264.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>After reading the data sheet I decided that I really didn&#8217;t want to implement a complete library for this sensor.  So, I started looking around for a driver library.  After googling around a little bit I found a library on GitHub (https://github.com/DavidAntliff/esp32-ds18b20) which looked promising, even though it is ESP32 specific.</p>
<p><a href="https://iotexpert.com/?attachment_id=9273" rel="attachment wp-att-9273"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.39.50-AM-1010x1024.png" alt="" width="1010" height="1024" class="alignnone size-large wp-image-9273" srcset="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.39.50-AM-1010x1024.png 1010w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.39.50-AM-100x100.png 100w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.39.50-AM-600x608.png 600w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.39.50-AM-296x300.png 296w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.39.50-AM-768x779.png 768w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.39.50-AM-1515x1536.png 1515w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-10.39.50-AM.png 1716w" sizes="(max-width: 1010px) 100vw, 1010px" /></a></p>
<p>But, after looking at this GitHub library a bit, I decide to start from there.</p>
<p>Before I get started two comments.</p>
<ol>
<li>He asked for PSoC 4 &#8230; but I am going to do PSoC 6 (because I can use Modus Toolbox).  There is no reason why this wouldn&#8217;t work on PSoC 4 &#8211; but it would take a bit of work</li>
<li>I can&#8217;t think of a good reason to use 1-wire, it sure seems like I2C would be simpler</li>
</ol>
<h1>Build a Base Project</h1>
<p>I start this whole effort by creating a new project for the CY8CKIT-062S2-43012 (because that happens to be the kit on my desk at the moment)</p>
<p><a href="https://iotexpert.com/?attachment_id=9279" rel="attachment wp-att-9279"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.24.06-AM-1024x773.png" alt="" width="1024" height="773" class="alignnone size-large wp-image-9279" srcset="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.24.06-AM-1024x773.png 1024w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.24.06-AM-600x453.png 600w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.24.06-AM-300x226.png 300w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.24.06-AM-768x580.png 768w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.24.06-AM-1536x1160.png 1536w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.24.06-AM.png 1820w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>I will use the IoT Expert FreeRTOS Template project</p>
<p><a href="https://iotexpert.com/?attachment_id=9280" rel="attachment wp-att-9280"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.24.38-AM-1024x772.png" alt="" width="1024" height="772" class="alignnone size-large wp-image-9280" srcset="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.24.38-AM-1024x772.png 1024w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.24.38-AM-600x452.png 600w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.24.38-AM-300x226.png 300w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.24.38-AM-768x579.png 768w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.24.38-AM-1536x1157.png 1536w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.24.38-AM.png 1826w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>For debugging this whole thing I will use a command line shell.  To get this into the project I add the ntshell library. Run &#8220;make modlibs&#8221; to start the library manager.</p>
<p><a href="https://iotexpert.com/?attachment_id=9282" rel="attachment wp-att-9282"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.27.43-AM-1024x645.png" alt="" width="1024" height="645" class="alignnone size-large wp-image-9282" srcset="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.27.43-AM-1024x645.png 1024w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.27.43-AM-600x378.png 600w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.27.43-AM-300x189.png 300w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.27.43-AM-768x483.png 768w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.27.43-AM.png 1166w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>In the library manager pick the &#8220;ntshell&#8221; library</p>
<p><a href="https://iotexpert.com/?attachment_id=9281" rel="attachment wp-att-9281"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.26.37-AM-1024x789.png" alt="" width="1024" height="789" class="alignnone size-large wp-image-9281" srcset="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.26.37-AM-1024x789.png 1024w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.26.37-AM-600x462.png 600w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.26.37-AM-300x231.png 300w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.26.37-AM-768x592.png 768w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.26.37-AM-1536x1183.png 1536w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.26.37-AM-2048x1578.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>To develop this project I want to use Visual Studio code.  So, I run &#8220;make vscode&#8221; (to create the configuration files) and start vscode by running &#8220;code .&#8221;</p>
<p><a href="https://iotexpert.com/?attachment_id=9283" rel="attachment wp-att-9283"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.29.46-AM-882x1024.png" alt="" width="882" height="1024" class="alignnone size-large wp-image-9283" srcset="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.29.46-AM-882x1024.png 882w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.29.46-AM-600x697.png 600w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.29.46-AM-258x300.png 258w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.29.46-AM-768x892.png 768w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.29.46-AM.png 1164w" sizes="(max-width: 882px) 100vw, 882px" /></a></p>
<p>When VSCODE starts up it looks like this:</p>
<p><a href="https://iotexpert.com/?attachment_id=9284" rel="attachment wp-att-9284"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.30.58-AM-1024x769.png" alt="" width="1024" height="769" class="alignnone size-large wp-image-9284" srcset="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.30.58-AM-1024x769.png 1024w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.30.58-AM-600x450.png 600w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.30.58-AM-300x225.png 300w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.30.58-AM-768x577.png 768w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.30.58-AM-1536x1153.png 1536w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.30.58-AM.png 2046w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>In order to use the ntshell library you need to shuffle the files around a little bit.  Move the ntshell.h/.c into the main project by doing a drag/drop in the explorer window.</p>
<p><a href="https://iotexpert.com/?attachment_id=9285" rel="attachment wp-att-9285"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.37.11-AM-574x1024.png" alt="" width="574" height="1024" class="alignnone size-large wp-image-9285" srcset="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.37.11-AM-574x1024.png 574w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.37.11-AM-600x1071.png 600w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.37.11-AM-168x300.png 168w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.37.11-AM-768x1370.png 768w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.37.11-AM.png 834w" sizes="(max-width: 574px) 100vw, 574px" /></a></p>
<p>It will ask you really want to move the files</p>
<p><a href="https://iotexpert.com/?attachment_id=9286" rel="attachment wp-att-9286"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.37.33-AM-1001x1024.png" alt="" width="1001" height="1024" class="alignnone size-large wp-image-9286" srcset="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.37.33-AM-1001x1024.png 1001w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.37.33-AM-600x614.png 600w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.37.33-AM-293x300.png 293w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.37.33-AM-768x785.png 768w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.37.33-AM.png 1498w" sizes="(max-width: 1001px) 100vw, 1001px" /></a></p>
<p>Once it is done, the ntshell functions which you need to customize will be part of the project.</p>
<p><a href="https://iotexpert.com/?attachment_id=9287" rel="attachment wp-att-9287"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.37.51-AM-567x1024.png" alt="" width="567" height="1024" class="alignnone size-large wp-image-9287" srcset="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.37.51-AM-567x1024.png 567w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.37.51-AM-600x1083.png 600w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.37.51-AM-166x300.png 166w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.37.51-AM-768x1386.png 768w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.37.51-AM-851x1536.png 851w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.37.51-AM.png 852w" sizes="(max-width: 567px) 100vw, 567px" /></a></p>
<p>To use the ntshell, you need to add the task to main.c</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include "cy_pdl.h"
#include "cyhal.h"
#include "cybsp.h"
#include "cy_retarget_io.h"
#include &lt;stdio.h&gt;
#include "FreeRTOS.h"
#include "task.h"

#include "ntshell.h"
#include "ntlibc.h"
#include "psoc6_ntshell_port.h"

// Global variable with a handle to the shell
ntshell_t ntshell;

void ntShellTask()
{

  printf("Started ntshell\n");
  setvbuf(stdin, NULL, _IONBF, 0);
  ntshell_init(
	       &amp;ntshell,
	       ntshell_read,
	       ntshell_write,
	       ntshell_callback,
	       (void *)&amp;ntshell);
  ntshell_set_prompt(&amp;ntshell, "DS18B20&gt; ");
  vtsend_erase_display(&amp;ntshell.vtsend);
  ntshell_execute(&amp;ntshell);
}
</pre>
<p>And start the task</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">    xTaskCreate(ntShellTask, "nt shell task", configMINIMAL_STACK_SIZE*2,0 /* args */ ,0 /* priority */, 0);</pre>
<p>Build and compile</p>
<p><a href="https://iotexpert.com/?attachment_id=9288" rel="attachment wp-att-9288"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.44.44-AM-1024x344.png" alt="" width="1024" height="344" class="alignnone size-large wp-image-9288" srcset="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.44.44-AM-1024x344.png 1024w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.44.44-AM-600x201.png 600w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.44.44-AM-300x101.png 300w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.44.44-AM-768x258.png 768w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.44.44-AM.png 1240w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>And you should have a working project.</p>
<p><a href="https://iotexpert.com/?attachment_id=9289" rel="attachment wp-att-9289"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.44.52-AM-1024x585.png" alt="" width="1024" height="585" class="alignnone size-large wp-image-9289" srcset="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.44.52-AM-1024x585.png 1024w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.44.52-AM-600x343.png 600w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.44.52-AM-300x171.png 300w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.44.52-AM-768x438.png 768w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.44.52-AM.png 1198w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Run &#8220;make program&#8221; to get the board going</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">arh (master *) OWB_DS18B20 $ make program
Tools Directory: /Applications/ModusToolbox/tools_2.1

Prebuild operations complete
Commencing build operations...

Tools Directory: /Applications/ModusToolbox/tools_2.1

Initializing build: mtb-example-psoc6-empty-app Debug CY8CKIT-062S2-43012 GCC_ARM

Auto-discovery in progress...
-&gt; Found 202 .c file(s)
-&gt; Found 50 .S file(s)
-&gt; Found 27 .s file(s)
-&gt; Found 0 .cpp file(s)
-&gt; Found 0 .o file(s)
-&gt; Found 4 .a file(s)
-&gt; Found 450 .h file(s)
-&gt; Found 0 .hpp file(s)
-&gt; Found 0 resource file(s)
Applying filters...
Auto-discovery complete

Constructing build rules...
Build rules construction complete

==============================================================================
= Building application =
==============================================================================
Building 181 file(s)
==============================================================================
= Build complete =
==============================================================================

Calculating memory consumption: CY8C624ABZI-D44 GCC_ARM -Og

   ---------------------------------------------------- 
  | Section Name         |  Address      |  Size       | 
   ---------------------------------------------------- 
  | .cy_m0p_image        |  0x10000000   |  5972       | 
  | .text                |  0x10002000   |  49532      | 
  | .ARM.exidx           |  0x1000e17c   |  8          | 
  | .copy.table          |  0x1000e184   |  24         | 
  | .zero.table          |  0x1000e19c   |  8          | 
  | .data                |  0x080022e0   |  1688       | 
  | .cy_sharedmem        |  0x08002978   |  8          | 
  | .noinit              |  0x08002980   |  148        | 
  | .bss                 |  0x08002a14   |  985176     | 
  | .heap                |  0x080f3270   |  46480      | 
   ---------------------------------------------------- 

  Total Internal Flash (Available)          2097152    
  Total Internal Flash (Utilized)           59468      

  Total Internal SRAM (Available)           1046528    
  Total Internal SRAM (Utilized)            1033500    


Programming target device... 
Open On-Chip Debugger 0.10.0+dev-3.0.0.665 (2020-03-20-17:12)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "swd". To override use 'transport select &lt;transport&gt;'.
adapter speed: 2000 kHz
** Auto-acquire enabled, use "set ENABLE_ACQUIRE 0" to disable
cortex_m reset_config sysresetreq
cortex_m reset_config sysresetreq
Info : Using CMSIS loader 'CY8C6xxA_SMIF' for bank 'psoc6_smif0_cm0' (footprint 6485 bytes)
Warn : SFlash programming allowed for regions: USER, TOC, KEY
Info : CMSIS-DAP: SWD  Supported
Info : CMSIS-DAP: FW Version = 2.0.0
Info : CMSIS-DAP: Interface Initialised (SWD)
Info : SWCLK/TCK = 1 SWDIO/TMS = 1 TDI = 0 TDO = 0 nTRST = 0 nRESET = 1
Info : CMSIS-DAP: Interface ready
Info : KitProg3: FW version: 1.11.159
Info : KitProg3: Pipelined transfers disabled, please update the firmware
Warn : *******************************************************************************************
Warn : * KitProg firmware is out of date, please update to the latest version using fw-loader at *
Warn : * ModusToolbox/tools/fw-loader                                                            *
Warn : *******************************************************************************************
Info : VTarget = 3.263 V
Info : kitprog3: acquiring PSoC device...
Info : clock speed 2000 kHz
Info : SWD DPIDR 0x6ba02477
Info : psoc6.cpu.cm0: hardware has 4 breakpoints, 2 watchpoints
Info : psoc6.cpu.cm0: external reset detected
***************************************
** Silicon: 0xE402, Family: 0x102, Rev.: 0x11 (A0)
** Detected Device: CY8C624ABZI-S2D44A0
** Detected Main Flash size, kb: 2048
** Flash Boot version: 3.1.0.45
** Chip Protection: NORMAL
***************************************
Info : psoc6.cpu.cm4: hardware has 6 breakpoints, 4 watchpoints
Info : psoc6.cpu.cm4: external reset detected
Info : Listening on port 3333 for gdb connections
Info : Listening on port 3334 for gdb connections
Info : kitprog3: acquiring PSoC device...
target halted due to debug-request, current mode: Thread 
xPSR: 0x41000000 pc: 0x00000190 msp: 0x080ff800
** Device acquired successfully
** psoc6.cpu.cm4: Ran after reset and before halt...
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x0000012a msp: 0x080ff800
** Programming Started **
auto erase enabled
Info : Flash write discontinued at 0x10001754, next section at 0x10002000
Info : Padding image section 0 at 0x10001754 with 172 bytes (bank write end alignment)
[100%] [################################] [ Erasing     ]
[100%] [################################] [ Programming ]
Info : Padding image section 1 at 0x1000e844 with 444 bytes (bank write end alignment)
[100%] [################################] [ Erasing     ]
[100%] [################################] [ Programming ]
wrote 57856 bytes from file /Users/arh/proj/xxx/OWB_DS18B20/build/CY8CKIT-062S2-43012/Debug/mtb-example-psoc6-empty-app.hex in 2.294771s (24.621 KiB/s)
** Programming Finished **
** Verify Started **
verified 57240 bytes in 0.155447s (359.598 KiB/s)
** Verified OK **
** Resetting Target **
shutdown command invoked

arh (master *) OWB_DS18B20 $</pre>
<p>And you should have a working project (with a blinking led and a command line)</p>
<p><a href="https://iotexpert.com/?attachment_id=9291" rel="attachment wp-att-9291"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.47.30-AM-1024x494.png" alt="" width="1024" height="494" class="alignnone size-large wp-image-9291" srcset="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.47.30-AM-1024x494.png 1024w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.47.30-AM-600x289.png 600w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.47.30-AM-300x145.png 300w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.47.30-AM-768x370.png 768w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.47.30-AM.png 1112w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Add the DS18B20 Library</h1>
<p>Now that I have a working project the next step is to clone the DS18B20 library into my project.  This is done using</p>
<ul>
<li>git clone https://github.com/DavidAntliff/esp32-ds18b20</li>
</ul>
<p><a href="https://iotexpert.com/?attachment_id=9293" rel="attachment wp-att-9293"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.53.38-AM-1024x216.png" alt="" width="1024" height="216" class="alignnone size-large wp-image-9293" srcset="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.53.38-AM-1024x216.png 1024w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.53.38-AM-600x126.png 600w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.53.38-AM-300x63.png 300w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.53.38-AM-768x162.png 768w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.53.38-AM.png 1434w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>The next step is to establish how bad things are with the new library.  So run the compiler.</p>
<p><a href="https://iotexpert.com/?attachment_id=9294" rel="attachment wp-att-9294"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.55.48-AM-1024x480.png" alt="" width="1024" height="480" class="alignnone size-large wp-image-9294" srcset="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.55.48-AM-1024x480.png 1024w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.55.48-AM-600x281.png 600w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.55.48-AM-300x141.png 300w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.55.48-AM-768x360.png 768w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-11.55.48-AM.png 1170w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>When I look at the ds18b20.c file I can see that there are some obvious problems</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_system.h"
#include "esp_log.h"

#include "ds18b20.h"
#include "owb.h"</pre>
<p>Fix those:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include "FreeRTOS.h"
#include "task.h"
//#include "driver/gpio.h"
//#include "esp_system.h"
//#include "esp_log.h"

#include "ds18b20.h"
//#include "owb.h"</pre>
<p>Run the compiler again.  Now I am missing owb.h which is the public header file for the one wire bus.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">In file included from esp32-ds18b20/ds18b20.c:47:0:
./esp32-ds18b20/include/ds18b20.h:37:10: fatal error: owb.h: No such file or directory
 #include "owb.h"</pre>
<p>I now make a directory to hold the new owb files called &#8220;p6sdk-onewire&#8221;.  Then I add a file &#8220;owb.h&#8221; (as a blank file)</p>
<p><a href="https://iotexpert.com/?attachment_id=9296" rel="attachment wp-att-9296"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-12.00.49-PM-530x1024.png" alt="" width="530" height="1024" class="alignnone size-large wp-image-9296" srcset="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-12.00.49-PM-530x1024.png 530w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-12.00.49-PM-600x1159.png 600w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-12.00.49-PM-155x300.png 155w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-12.00.49-PM.png 736w" sizes="(max-width: 530px) 100vw, 530px" /></a></p>
<p>When I run the compiler again, there are now two classes or error &#8230; owb and esp32 logging function. Here is an example of the ESP_ problems.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">esp32-ds18b20/ds18b20.c:249:17: warning: implicit declaration of function 'ESP_LOG_BUFFER_HEX_LEVEL' [-Wimplicit-function-declaration]
                 ESP_LOG_BUFFER_HEX_LEVEL(TAG, scratchpad, count, ESP_LOG_DEBUG);
                 ^~~~~~~~~~~~~~~~~~~~~~~~
esp32-ds18b20/ds18b20.c:249:66: error: 'ESP_LOG_DEBUG' undeclared (first use in this function)
                 ESP_LOG_BUFFER_HEX_LEVEL(TAG, scratchpad, count, ESP_LOG_DEBUG);</pre>
<p>And here is an example of the owb problems.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">esp32-ds18b20/ds18b20.c: In function 'ds18b20_wait_for_conversion':
esp32-ds18b20/ds18b20.c:504:30: error: request for member 'use_parasitic_power' in something not a structure or union
         if (ds18b20_info-&gt;bus-&gt;use_parasitic_power)
                              ^~
esp32-ds18b20/ds18b20.c: At top level:
esp32-ds18b20/ds18b20.c:568:54: error: unknown type name 'OneWireBus'
 DS18B20_ERROR ds18b20_check_for_parasite_power(const OneWireBus * bus, bool * present)
                                                      ^~~~~~~~~~
esp32-ds18b20/ds18b20.c: In function 'ds18b20_check_for_parasite_power':
esp32-ds18b20/ds18b20.c:577:44: error: 'OWB_ROM_SKIP' undeclared (first use in this function); did you mean 'CY_ROM_SIZE'?
             if ((err = owb_write_byte(bus, OWB_ROM_SKIP)) == DS18B20_OK)
                                            ^~~~~~~~~~~~
                                            CY_ROM_SIZE
make[1]: *** [/Users/arh/proj/xxx/OWB_DS18B20/build/CY8CKIT-062S2-4301</pre>
<p>To make this thing go, I edit ds18b20.c file and add logging templates (just stub functions that don&#8217;t do anything)</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">void ESP_LOGD(const char * code, char *val,...)
{

}

void ESP_LOGE(const char * code, ...)
{

}

void ESP_LOGW(const char *code,...)
{

}

#define ESP_LOG_DEBUG 0
void ESP_LOG_BUFFER_HEX_LEVEL(const char *code,...)
{

}

uint64_t esp_timer_get_time()
{
    return 0;
}</pre>
<h1>Create owb.h</h1>
<p>If I was smart, I would have started with David Antliff &#8220;owb&#8221; library.  But I don&#8217;t.  What I do is search through &#8220;ds18b20.c&#8221; and find every function call to owb_ and then copy those function calls into owb.h (my new file).  Then I fix the function calls to have correct prototypes based on what I see in the ds18b20 library.  Here is what my owb.h file looks like after that process.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#ifndef OWB_H
#define OWB_H

#ifdef __cplusplus
extern "C" {
#endif

#define OWB_ROM_MATCH 0
#define OWB_ROM_SKIP 0

#include &lt;stdint.h&gt;
#include "cyhal.h"
#include "FreeRTOS.h"
#include "semphr.h"

typedef struct  
{
    cyhal_gpio_t  pin;
    bool use_parasitic_power;
    SemaphoreHandle_t signalSemaphore;
    cyhal_timer_t bitTimer;
    bool detect;
    SemaphoreHandle_t owb_num_active;
} OneWireBus;        

typedef struct {
    uint8_t romAddr[8];

} OneWireBus_ROMCode;

typedef enum {
    OWB_STATUS_OK,
    OWB_STATUS_ERROR,
} owb_ret_t ;

owb_ret_t owb_init(OneWireBus *bus);
    
owb_ret_t owb_reset(OneWireBus *bus, bool *result);

owb_ret_t owb_write_bit( OneWireBus *bus, uint8_t val);
owb_ret_t owb_write_byte( OneWireBus *bus, uint8_t val);
owb_ret_t owb_write_bytes( OneWireBus *bus, uint8_t *buffer, uint32_t length);

owb_ret_t owb_write_rom_code(OneWireBus *bus, OneWireBus_ROMCode romcode);

owb_ret_t owb_read_bit( OneWireBus *bus, uint8_t *bit);
owb_ret_t owb_read_byte( OneWireBus *bus, uint8_t *byte);
owb_ret_t owb_read_bytes( OneWireBus *bus, uint8_t *buffer, uint32_t length);

owb_ret_t owb_crc8_bytes(uint32_t val, uint8_t *buffer, uint32_t length);

void   owb_set_strong_pullup( OneWireBus *bus, bool val);

#endif

#ifdef __cplusplus
}
#endif</pre>
<p>When I run the compiler again things look way better (just some complaining about const datatypes) and a complaint about the include path.  The include path thing is visual studio code not knowing that I added a new directory called p6sdk-onewire.</p>
<p><a href="https://iotexpert.com/?attachment_id=9297" rel="attachment wp-att-9297"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-12.06.30-PM-1024x417.png" alt="" width="1024" height="417" class="alignnone size-large wp-image-9297" srcset="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-12.06.30-PM-1024x417.png 1024w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-12.06.30-PM-600x244.png 600w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-12.06.30-PM-300x122.png 300w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-12.06.30-PM-768x313.png 768w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-12.06.30-PM-1536x625.png 1536w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-12.06.30-PM-2048x833.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>To fix the include path I run &#8220;make vscode&#8221; which tells VSCODE about the new directory.</p>
<p><a href="https://iotexpert.com/?attachment_id=9298" rel="attachment wp-att-9298"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-12.07.59-PM-1024x148.png" alt="" width="1024" height="148" class="alignnone size-large wp-image-9298" srcset="https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-12.07.59-PM-1024x148.png 1024w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-12.07.59-PM-600x87.png 600w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-12.07.59-PM-300x43.png 300w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-12.07.59-PM-768x111.png 768w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-12.07.59-PM-1536x222.png 1536w, https://iotexpert.com/wp-content/uploads/2020/06/Screen-Shot-2020-06-11-at-12.07.59-PM.png 1830w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>That is a good place to split this article.  In the next article I will add functions to read and write the bus.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/psoc-6-sdk-onewire-bus-part-1-build-basic-project/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>AnyCloud &#8211; Wireless Connection Manager (Part 1)</title>
		<link>https://iotexpert.com/anycloud-wireless-connection-manager-part-1/</link>
					<comments>https://iotexpert.com/anycloud-wireless-connection-manager-part-1/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Mon, 04 May 2020 12:00:44 +0000</pubDate>
				<category><![CDATA[4343W]]></category>
		<category><![CDATA[AnyCloud]]></category>
		<category><![CDATA[CY8CKIT-062S2-43012]]></category>
		<category><![CDATA[CY8CPROTO-062-4343W]]></category>
		<category><![CDATA[PSoC 6]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=9019</guid>

					<description><![CDATA[Summary A discussion of using the Infineon (Cypress) PSoC 6 with a CYW4343W and the AnyCloud Connection Manager with Modus Toolbox.  The AnyCloud Connection Manager is an RTOS thread that lets you manage a connection to a WiFi network.  It knows how to scan for networks, attach, detach etc. and was recently released for use [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>A discussion of using the Infineon (Cypress) PSoC 6 with a CYW4343W and the AnyCloud Connection Manager with Modus Toolbox.  The AnyCloud Connection Manager is an RTOS thread that lets you manage a connection to a WiFi network.  It knows how to scan for networks, attach, detach etc. and was recently released for use with PSoC6 and 43xxx WiFi combos.</p>
<h1>The Story</h1>
<p>In the WICED WiFI SDK there is an example program called &#8220;test.console&#8221; which allows you to use a UART command line to do networking &#8220;stuff&#8221;.  With the release of the new AnyCloud SDK I decided that I should rebuild some of that program using the PSoC and WiFi.  Basically a set of commands like &#8220;scan&#8221; to interact with the Radio World!  In the picture you below you can see that I use the command &#8220;scan&#8221; to list out the networks at my house.</p>
<p><a href="https://iotexpert.com/2020/05/04/anycloud-wireless-connection-manager-part-1/screen-shot-2020-04-27-at-1-04-36-pm/" rel="attachment wp-att-9021"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-1.04.36-PM-1024x623.png" alt="" width="1024" height="623" class="alignnone size-large wp-image-9021" srcset="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-1.04.36-PM-1024x623.png 1024w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-1.04.36-PM-600x365.png 600w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-1.04.36-PM-300x182.png 300w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-1.04.36-PM-768x467.png 768w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-1.04.36-PM-1536x934.png 1536w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-1.04.36-PM.png 1950w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>You can also use the command &#8220;help&#8221; to list out the commands.</p>
<p><a href="https://iotexpert.com/2020/05/04/anycloud-wireless-connection-manager-part-1/screen-shot-2020-04-27-at-1-04-58-pm/" rel="attachment wp-att-9022"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-1.04.58-PM-1024x299.png" alt="" width="1024" height="299" class="alignnone size-large wp-image-9022" srcset="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-1.04.58-PM-1024x299.png 1024w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-1.04.58-PM-600x175.png 600w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-1.04.58-PM-300x88.png 300w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-1.04.58-PM-768x224.png 768w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-1.04.58-PM-1536x449.png 1536w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-1.04.58-PM.png 1732w" sizes="(max-width: 1024px) 100vw, 1024px" /></a><a href="https://iotexpert.com/2020/05/04/anycloud-wireless-connection-manager-part-1/screen-shot-2020-04-27-at-1-04-58-pm/" rel="attachment wp-att-9022"></a></p>
<h1>Architecture</h1>
<p>My implementation will have three tasks</p>
<ol>
<li>A Blinking LED Task (which doesn&#8217;t interact with any other tasks)</li>
<li>The NT Shell &#8211; which will send commands to the network queue.</li>
<li>The Network Task which will receive commands from the NT Shell task and trigger the Wireless Connection Manager to do something</li>
<li>The Wireless Connection Manager which will interact with the WiFI Radio via the Wireless Host driver.</li>
</ol>
<p><a href="https://iotexpert.com/?attachment_id=9131" rel="attachment wp-att-9131"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/05/arch-1-1024x569.png" alt="" width="1024" height="569" class="alignnone size-large wp-image-9131" srcset="https://iotexpert.com/wp-content/uploads/2020/05/arch-1-1024x569.png 1024w, https://iotexpert.com/wp-content/uploads/2020/05/arch-1-600x333.png 600w, https://iotexpert.com/wp-content/uploads/2020/05/arch-1-300x167.png 300w, https://iotexpert.com/wp-content/uploads/2020/05/arch-1-768x426.png 768w, https://iotexpert.com/wp-content/uploads/2020/05/arch-1.png 1358w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Make the Basic Project</h1>
<p>To build this project start by creating a new project.  The development board that I had plugged into my computer when I began this project is a CY8CPROTO-062-4343W so this is the one I will select.</p>
<p><a href="https://iotexpert.com/2020/05/04/anycloud-wireless-connection-manager-part-1/img_1741/" rel="attachment wp-att-9045"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/04/IMG_1741-1024x768.jpg" alt="" width="1024" height="768" class="alignnone size-large wp-image-9045" srcset="https://iotexpert.com/wp-content/uploads/2020/04/IMG_1741-1024x768.jpg 1024w, https://iotexpert.com/wp-content/uploads/2020/04/IMG_1741-scaled-600x450.jpg 600w, https://iotexpert.com/wp-content/uploads/2020/04/IMG_1741-300x225.jpg 300w, https://iotexpert.com/wp-content/uploads/2020/04/IMG_1741-768x576.jpg 768w, https://iotexpert.com/wp-content/uploads/2020/04/IMG_1741-1536x1152.jpg 1536w, https://iotexpert.com/wp-content/uploads/2020/04/IMG_1741-2048x1536.jpg 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>In the new project creator pick the &#8220;CY8CPROTO-062-4343W&#8221;</p>
<p><a href="https://iotexpert.com/2020/05/04/anycloud-wireless-connection-manager-part-1/screen-shot-2020-04-27-at-2-52-43-pm/" rel="attachment wp-att-9031"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.52.43-PM-1024x757.png" alt="" width="1024" height="757" class="alignnone size-large wp-image-9031" srcset="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.52.43-PM-1024x757.png 1024w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.52.43-PM-600x443.png 600w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.52.43-PM-300x222.png 300w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.52.43-PM-768x567.png 768w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.52.43-PM-1536x1135.png 1536w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.52.43-PM-2048x1513.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>I created a FreeRTOS template project that does all of the right stuff.  I wrote an article about how to use the IoT Expert Manifestt <a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/">here</a>, and until you add the IoT Manifest to your setup you will not get the FreeRTOS Template project.</p>
<p><a href="https://iotexpert.com/2020/05/04/anycloud-wireless-connection-manager-part-1/screen-shot-2020-04-27-at-2-53-09-pm/" rel="attachment wp-att-9032"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.53.09-PM-1024x756.png" alt="" width="1024" height="756" class="alignnone size-large wp-image-9032" srcset="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.53.09-PM-1024x756.png 1024w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.53.09-PM-600x443.png 600w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.53.09-PM-300x222.png 300w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.53.09-PM-768x567.png 768w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.53.09-PM-1536x1134.png 1536w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.53.09-PM-2048x1512.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>After the new project work is done you should see</p>
<p><a href="https://iotexpert.com/2020/05/04/anycloud-wireless-connection-manager-part-1/screen-shot-2020-04-27-at-2-53-33-pm/" rel="attachment wp-att-9033"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.53.33-PM-1024x228.png" alt="" width="1024" height="228" class="alignnone size-large wp-image-9033" srcset="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.53.33-PM-1024x228.png 1024w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.53.33-PM-600x134.png 600w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.53.33-PM-300x67.png 300w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.53.33-PM-768x171.png 768w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.53.33-PM-1536x342.png 1536w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.53.33-PM-2048x456.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Add the nt-shell, which comes from my IoT Expert library (read about how to add the IoT Expert library <a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/">here</a>).</p>
<p><a href="https://iotexpert.com/2020/05/04/anycloud-wireless-connection-manager-part-1/screen-shot-2020-04-27-at-2-57-07-pm/" rel="attachment wp-att-9034"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.57.07-PM-1024x889.png" alt="" width="1024" height="889" class="alignnone size-large wp-image-9034" srcset="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.57.07-PM-1024x889.png 1024w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.57.07-PM-600x521.png 600w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.57.07-PM-300x261.png 300w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.57.07-PM-768x667.png 768w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.57.07-PM-1536x1334.png 1536w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.57.07-PM.png 1976w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>I have been on a kick of using Visual Studio code.  So, run &#8220;make vscode&#8221; to setup the project for Visual Studio Code.</p>
<p><a href="https://iotexpert.com/2020/05/04/anycloud-wireless-connection-manager-part-1/screen-shot-2020-04-27-at-2-57-32-pm/" rel="attachment wp-att-9035"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.57.32-PM-870x1024.png" alt="" width="870" height="1024" class="alignnone size-large wp-image-9035" srcset="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.57.32-PM-870x1024.png 870w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.57.32-PM-600x706.png 600w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.57.32-PM-255x300.png 255w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.57.32-PM-768x904.png 768w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.57.32-PM-1305x1536.png 1305w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.57.32-PM.png 1704w" sizes="(max-width: 870px) 100vw, 870px" /></a></p>
<p>In the finder, copy the template files from nt-shell into your project.</p>
<p><a href="https://iotexpert.com/2020/05/04/anycloud-wireless-connection-manager-part-1/screen-shot-2020-04-27-at-2-23-34-pm/" rel="attachment wp-att-9027"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.23.34-PM.png" alt="" width="1012" height="604" class="alignnone size-full wp-image-9027" srcset="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.23.34-PM.png 1012w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.23.34-PM-600x358.png 600w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.23.34-PM-300x179.png 300w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.23.34-PM-768x458.png 768w" sizes="(max-width: 1012px) 100vw, 1012px" /></a></p>
<p>You can also do it with the command line.</p>
<p><a href="https://iotexpert.com/2020/05/04/anycloud-wireless-connection-manager-part-1/screen-shot-2020-04-27-at-2-58-45-pm/" rel="attachment wp-att-9036"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.58.45-PM-1024x139.png" alt="" width="1024" height="139" class="alignnone size-large wp-image-9036" srcset="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.58.45-PM-1024x139.png 1024w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.58.45-PM-600x81.png 600w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.58.45-PM-300x41.png 300w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.58.45-PM-768x104.png 768w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.58.45-PM.png 1168w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Edit main.c to include the configuration.</p>
<pre class="start-line:9 EnlighterJSRAW" data-enlighter-language="c"">#include "ntshell.h"
#include "psoc6_ntshell_port.h"</pre>
<p>Update the main.c to have the ntShellThread</p>
<pre class="start-line:18 EnlighterJSRAW" data-enlighter-language="c"">// Global variable with a handle to the shell
ntshell_t ntshell;

void ntShellTask()
{

  printf("Started ntshell\n");
  setvbuf(stdin, NULL, _IONBF, 0);
  ntshell_init(
               &amp;ntshell,
               ntshell_read,
               ntshell_write,
               ntshell_callback,
               (void *)&amp;ntshell);
  ntshell_set_prompt(&amp;ntshell, "AnyCloud&gt; ");
  vtsend_erase_display(&amp;ntshell.vtsend);
  ntshell_execute(&amp;ntshell);
}</pre>
<p>And start the ntshell task.</p>
<pre class="start-line:71 EnlighterJSRAW" data-enlighter-language="c"">    xTaskCreate(ntShellTask, "nt shell task", configMINIMAL_STACK_SIZE*2,0 /* args */ ,4 /* priority */, 0);</pre>
<p>When you want to add a command to the shell you need to do three things</p>
<ol>
<li>Add a function prototype for the command</li>
<li>Add the command to the command list</li>
<li>Write the function</li>
</ol>
<pre class="start-line:54 EnlighterJSRAW" data-enlighter-language="c"">static int usrcmd_help(int argc, char **argv);
static int usrcmd_info(int argc, char **argv);
static int usrcmd_clear(int argc, char **argv);
static int usrcmd_printargs(int argc, char **argv);
static const cmd_table_t cmdlist[] = {
    { "help", "This is a description text string for help command.", usrcmd_help },
    { "info", "This is a description text string for info command.", usrcmd_info },
    { "clear", "Clear the screen", usrcmd_clear },
    { "printargs","print the list of arguments", usrcmd_printargs},
};</pre>
<p>Here is an example of of the printargs command.  The shell will call your function with a ARGC=number of arguments and ARGV[] an array of the pointers to the actual arguments.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">static int usrcmd_printargs(int argc, char **argv)
{
    printf("ARGC = %d\n",argc);

    for(int i =0;i&lt;argc;i++)
    {
        printf("argv[%d] = %s\n",i,argv[i]);
    }
    return 0;

}</pre>
<p>Build and test your program.</p>
<p><a href="https://iotexpert.com/2020/05/04/anycloud-wireless-connection-manager-part-1/screen-shot-2020-04-27-at-2-28-29-pm/" rel="attachment wp-att-9029"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.28.29-PM-1024x288.png" alt="" width="1024" height="288" class="alignnone size-large wp-image-9029" srcset="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.28.29-PM-1024x288.png 1024w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.28.29-PM-600x169.png 600w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.28.29-PM-300x84.png 300w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.28.29-PM-768x216.png 768w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.28.29-PM-1536x431.png 1536w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.28.29-PM.png 1666w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Turn on the Connection Manager and Core Middleware Library</h1>
<p>Now, add the wifi-mw-core library &amp; Connection Manager using the library manager.  You can start it with &#8220;make modlibs&#8221;.  These two libraries are in the &#8220;WiFi Middleware&#8221; category.</p>
<p><a href="https://iotexpert.com/2020/05/04/anycloud-wireless-connection-manager-part-1/screen-shot-2020-04-27-at-3-04-40-pm/" rel="attachment wp-att-9037"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-3.04.40-PM-1024x787.png" alt="" width="1024" height="787" class="alignnone size-large wp-image-9037" srcset="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-3.04.40-PM-1024x787.png 1024w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-3.04.40-PM-600x461.png 600w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-3.04.40-PM-300x231.png 300w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-3.04.40-PM-768x591.png 768w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-3.04.40-PM-1536x1181.png 1536w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-3.04.40-PM-2048x1575.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Copy the FreeRTOSConfig.h from the libs/wifi-mw-core/configs directory into your project (so you can modify it)</p>
<p><a href="https://iotexpert.com/2020/05/04/anycloud-wireless-connection-manager-part-1/screen-shot-2020-04-27-at-2-13-57-pm/" rel="attachment wp-att-9026"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.13.57-PM-1024x677.png" alt="" width="1024" height="677" class="alignnone size-large wp-image-9026" srcset="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.13.57-PM-1024x677.png 1024w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.13.57-PM-600x396.png 600w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.13.57-PM-300x198.png 300w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.13.57-PM-768x507.png 768w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.13.57-PM-1536x1015.png 1536w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-2.13.57-PM.png 1562w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Update the Makefile to include the WiFi components (line 71) and the MBED TLS configuration (line 86)</p>
<pre class="start-line:70 EnlighterJSRAW" data-enlighter-language="c"">COMPONENTS=FREERTOS PSOC6HAL LWIP MBEDTLS 4343W
# Like COMPONENTS, but disable optional code that was enabled by default.
DISABLE_COMPONENTS=

# By default the build system automatically looks in the Makefile's directory
# tree for source code and builds it. The SOURCES variable can be used to
# manually add source code to the build process from a location not searched
# by default, or otherwise not found by the build system.
SOURCES=

# Like SOURCES, but for include directories. Value should be paths to
# directories (without a leading -I).
INCLUDES=

# Add additional defines to the build process (without a leading -D).
DEFINES=MBEDTLS_USER_CONFIG_FILE='"configs/mbedtls_user_config.h"' CYBSP_WIFI_CAPABLE</pre>
<h1>Create networkTask.h/.c &amp; Start the Task in main.c</h1>
<p>Now let&#8217;s create the networkTask.  This task will control all of the networking functions in the system.  The first file, networkTask.h, is the public interface.  It declares a Queue where you can push messages (line 5) an enumeration of the messages (lines 7-14), a definition of the message data (lines 17-22) and finally the network task declaration (line 24).</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">#pragma once
#include "FreeRTOS.h"
#include "queue.h"

extern QueueHandle_t networkQueue;

typedef enum {
    net_scan,
    net_connect,
    net_disconnect,
    net_printip,
    net_printmac,

} networkCmd_t;


typedef struct {
    networkCmd_t cmd;
    uint32_t val0;
    uint32_t val1;

} networkQueueMsg_t;

void networkTask(void *arg);</pre>
<p>Ill go ahead and modify main.c to start the yet to be written network task.  You need to include the header file for the task and define a handle for the task.</p>
<pre class="start-line:11 EnlighterJSRAW" data-enlighter-language="c"">#include "networkTask.h"

TaskHandle_t networkTaskHandle;</pre>
<p>Finally in main function, start the task.</p>
<pre class="start-line:60 EnlighterJSRAW" data-enlighter-language="c"">    xTaskCreate(networkTask, "networkTask", configMINIMAL_STACK_SIZE*8,0 /* args */ ,4/* priority */, &amp;networkTaskHandle);</pre>
<p>Create the file networkTask.c.  Make a bunch of includes.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include "FreeRTOS.h"
#include "task.h"
#include "cy_wcm.h"
#include "cy_wcm_error.h"
#include "whd_types.h"
#include "queue.h"
#include "semphr.h"
#include "networkTask.h"</pre>
<p>Now let&#8217;s define the actual task function.  This function will</p>
<ol>
<li>Call the wcm_init function to make a WiFi Station (lines 201-203)</li>
<li>Tell the task that you would like to be called back when things happen (line 205)</li>
<li>Initialize the Queue to receive command messages from other tasks.</li>
<li>Make an infinite loop to receive the commands and process the messages.  Notice that I dont do anything with the message for now.  Ill add the code later.</li>
</ol>
<pre class="start-line:190 EnlighterJSRAW" data-enlighter-language="c"">// The networkTask will:
// - startup the wireless connection manager
// - sit waiting on the rtos queue... getting messages from other tasks
// - and do ( net_scan, net_connect, net_disconnect, net_printip, net_printmac,)

void networkTask(void *arg)
{
	cy_rslt_t result;
	cy_wcm_config_t config;
	cy_wcm_scan_filter_t scanFilter;

	memset(&amp;config, 0, sizeof(cy_wcm_config_t));
    config.interface = CY_WCM_INTERFACE_TYPE_STA;
	cy_wcm_init	(&amp;config);

	cy_wcm_register_event_callback(	wcmCallback	);

	networkQueue = xQueueCreate( 5, sizeof(networkQueueMsg_t));

	while(1)
	{
		networkQueueMsg_t msg;

		xQueueReceive(networkQueue,(void *)&amp;msg,portMAX_DELAY);  // Wait for commands from other tasks

		switch(msg.cmd)
		{
			case net_scan: // 0=stop scan !0=start scan
			break;

			case net_connect:
			break;

			case net_disconnect:
			break;

			case net_printip:
			break;

			case net_printmac:
			break;
		}
	}

}</pre>
<h1>Create Utilities for Printing out the IP and MAC Address</h1>
<p>It the network task I want to be able to print out IP address (both IPV4 and IPV6).  I also want to be able to print out 6-byte MAC address.  In the Cypress drivers, an IP address is a structure that holds either a IPV4 or and IPV6 address.  It then contains the 4 or 6 bytes of the address.  Here is the type definition.</p>
<pre class="start-line:387 EnlighterJSRAW" data-enlighter-language="c"">/**
 * IP Address Structure
 */
typedef struct
{
    cy_wcm_ip_version_t version;  /**&lt; IP version */
    union
    {
        uint32_t v4;     /**&lt; IPv4 address bytes */
        uint32_t v6[4];  /**&lt; IPv6 address bytes */
    } ip;                /**&lt; IP address bytes */
} cy_wcm_ip_address_t;</pre>
<p>The IP v ersion is just an enumeration.</p>
<pre class="start-line:164 EnlighterJSRAW" data-enlighter-language="c"">/**
 * IP Version
 */
typedef enum
{
    CY_WCM_IP_VER_V4 = 4,      /**&lt; Denotes IPv4 version */
    CY_WCM_IP_VER_V6 = 6       /**&lt; Denotes IPv6 version */
} cy_wcm_ip_version_t;</pre>
<p>To print an address, figure out which version you are working on.  Then dump the raw bytes.  Notice in the IPV4 case it is encoded into a uint32_t as 4 continuous bytes.</p>
<pre class="start-line:18 EnlighterJSRAW" data-enlighter-language="c"">void printIp(cy_wcm_ip_address_t *ipad)
{
	if(ip_addr.version == CY_WCM_IP_VER_V4)
	{
		printf("%d.%d.%d.%d",(int)ipad-&gt;ip.v4&gt;&gt;0&amp;0xFF,(int)ipad-&gt;ip.v4&gt;&gt;8&amp;0xFF,(int)ipad-&gt;ip.v4&gt;&gt;16&amp;0xFF,(int)ipad-&gt;ip.v4&gt;&gt;24&amp;0xFF);
	}
	else if (ip_addr.version == CY_WCM_IP_VER_V6)
	{
		for(int i=0;i&lt;4;i++)
		{
			printf("%0X:",(unsigned int)ip_addr.ip.v6[i]);
		}
	}
	else
	{
		printf("IP ERROR %d",ipad-&gt;version);
	}			
}</pre>
<p>A MAC address is just an array of uint8_t of length CY_WCM_MAC_ADDR_LEN  (which we pound defined to 6)</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">typedef uint8_t cy_wcm_mac_t[CY_WCM_MAC_ADDR_LEN];                   /**&lt; Unique 6-byte MAC address */</pre>
<p>So, the print is just a loop. (probably should have done something slightly better so I dont end up with the trailing : &#8211; oh well)</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">void printMac(cy_wcm_mac_t mac)
{
	for(int i=0;i&lt;CY_WCM_MAC_ADDR_LEN;i++)
	{
		uint8_t val = mac[i];
		printf("%02X:",val);
	}
}</pre>
<h1>Add the Scan Command</h1>
<p>For the scan I want to print out the:</p>
<ol>
<li>SSID</li>
<li>RSSI (in dBM)</li>
<li>Channel</li>
<li>Band</li>
<li>Speed</li>
<li>Type of Ap</li>
<li>Country Code</li>
<li>MAC address of the Access Point (AKA BSSID)</li>
<li>Type of Security</li>
</ol>
<p><a href="https://iotexpert.com/2020/05/04/anycloud-wireless-connection-manager-part-1/screen-shot-2020-04-27-at-1-04-36-pm/" rel="attachment wp-att-9021"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-1.04.36-PM-1024x623.png" alt="" width="1024" height="623" class="alignnone size-large wp-image-9021" srcset="https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-1.04.36-PM-1024x623.png 1024w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-1.04.36-PM-600x365.png 600w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-1.04.36-PM-300x182.png 300w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-1.04.36-PM-768x467.png 768w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-1.04.36-PM-1536x934.png 1536w, https://iotexpert.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-27-at-1.04.36-PM.png 1950w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>In order to have the WCM run a scan you need to call the function cy_wcm_start_scan.  What this will do is tell the 4343W to scan all the channels on the 2.4GHZ band and listen for access point beacons.  This function has three arguments</p>
<ol>
<li>A function pointer to call back when you find an AP</li>
<li>A user settable data pointer</li>
<li>A filter (which can limit to an SSID, BSSID or RSSI)</li>
</ol>
<pre class="EnlighterJSRAW" data-enlighter-language="c">cy_rslt_t cy_wcm_start_scan(cy_wcm_scan_result_callback_t callback, void *user_data, cy_wcm_scan_filter_t *scan_filter)</pre>
<p>Here is my version of the scanCallback which I put in the networkTask.c file.</p>
<pre class="start-line:60 EnlighterJSRAW" data-enlighter-language="c"">// This function is called back when the user asks for an overall scan.
// It just prints out the information about the networks that it hears about
void scanCallback( cy_wcm_scan_result_t *result_ptr, void *user_data, cy_wcm_scan_status_t status )</pre>
<p>The result_ptr is a pointer to a structure that contains the data for the found access point.</p>
<pre class="start-line:460 EnlighterJSRAW" data-enlighter-language="c" ">/**
 * Structure for storing scan results
 */
typedef struct
{
    cy_wcm_ssid_t                SSID;             /**&lt; Service Set Identification (i.e. Name of Access Point)                    */
    cy_wcm_mac_t                 BSSID;            /**&lt; Basic Service Set Identification (i.e. MAC address of Access Point)       */
    int16_t                      signal_strength;  /**&lt; Receive Signal Strength Indication in dBm. &lt;-90=Very poor, &gt;-30=Excellent */
    uint32_t                     max_data_rate;    /**&lt; Maximum data rate in kilobits/s                                           */
    cy_wcm_bss_type_t            bss_type;         /**&lt; Network type                                                              */
    cy_wcm_security_t            security;         /**&lt; Security type                                                             */
    uint8_t                      channel;          /**&lt; Radio channel that the AP beacon was received on                          */
    cy_wcm_wifi_band_t           band;             /**&lt; Radio band                                                                */
    uint8_t                      ccode[2];         /**&lt; Two letter ISO country code from AP                                       */
    uint8_t                      flags;            /**&lt; flags                                                                     */
    uint8_t                      *ie_ptr;          /**&lt; Pointer to received Beacon/Probe Response IE(Information Element)         */
    uint32_t                     ie_len;           /**&lt; Length of IE(Information Element)                                         */
} cy_wcm_scan_result_t;
</pre>
<p>The other parameter to the callback is the status.  The status will either be CY_WCM_SCAN_COMPLETE or CY_WCM_SCAN_INCOMPLETE.  The first thing that to do is see if this callback is the one that tells me that the scan is complete, if so I just return (and don&#8217;t print anything)</p>
<pre class="start-line:67 EnlighterJSRAW" data-enlighter-language="c"">	if(status == CY_WCM_SCAN_COMPLETE)
		return;</pre>
<p>Then I print out the raw SSID, Signal Strength and Channel.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">	printf("%32s\t%d\t%d\t",result_ptr-&gt;SSID,result_ptr-&gt;signal_strength,result_ptr-&gt;channel);</pre>
<p>Next I figure out what channel we are talking about.  The 4343W is single band 2.4GHZ, however, other Cypress chips have dual band.</p>
<pre class="start-line:68 EnlighterJSRAW" data-enlighter-language="c"">	switch(result_ptr-&gt;band)
	{
		case CY_WCM_WIFI_BAND_ANY:
			printf("ANY");
		break;
		case CY_WCM_WIFI_BAND_5GHZ:
			printf("5.0 GHZ");
		break;
		case CY_WCM_WIFI_BAND_2_4GHZ:
			printf("2.4 GHZ");
		break;
	}</pre>
<p>Then I printout the maximum data rate, which is 0 for all of my test cases.  I have no idea why.</p>
<pre class="start-line:82 EnlighterJSRAW" data-enlighter-language="c"">	printf("%d",(int)result_ptr-&gt;max_data_rate);</pre>
<p>Then I printout what type of AP we are talking about.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">	switch(result_ptr-&gt;bss_type)
	{
		case CY_WCM_BSS_TYPE_INFRASTRUCTURE:
			printf("INFR");
		break; 	
		case CY_WCM_BSS_TYPE_ADHOC: 
			printf("ADHOC");
		break;	
		case CY_WCM__BSS_TYPE_ANY: 	
			printf("ANY");
		break;
		case CY_WCM_BSS_TYPE_MESH:
			printf("MESG");
		break;
		case CY_WCM_BSS_TYPE_UNKNOWN: 
			printf("UNKWN");
		break;
	}</pre>
<p>Then the country code.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">	printf("%c%c",result_ptr-&gt;ccode[0],result_ptr-&gt;ccode[1]);
</pre>
<p>Then the Basic Service Set ID of the AP, which is also known as the MAC address of the AP.</p>
<pre class="start-line:105 EnlighterJSRAW" data-enlighter-language="c"">	printMac(result_ptr-&gt;BSSID);</pre>
<p>Then the security type.</p>
<pre class="start-line:108 EnlighterJSRAW" data-enlighter-language="c"">	switch(result_ptr-&gt;security)
	{
		case CY_WCM_SECURITY_OPEN:
			printf("OPEN");
		break;
    	case CY_WCM_SECURITY_WEP_PSK:
			printf("WEP_PSK");
		break;
		case CY_WCM_SECURITY_WEP_SHARED:
			printf("WEP_SHARED");
		break;
    	case CY_WCM_SECURITY_WPA_TKIP_PSK:
			printf("WPA_TKIP_PSK");
		break;
		case CY_WCM_SECURITY_WPA_AES_PSK:
			printf("WPA_AES_PSK");
		break;
		case CY_WCM_SECURITY_WPA_MIXED_PSK:
			printf("WPA_MIXED_PSK");
		break;
		case CY_WCM_SECURITY_WPA2_AES_PSK:
			printf("WPA2_AES_PSK");
		break;
		case CY_WCM_SECURITY_WPA2_TKIP_PSK:
			printf("WPA2_TKIP_PSK");
		break;
		case CY_WCM_SECURITY_WPA2_MIXED_PSK:
			printf("WPA2_MIXED_PSK");
		break;
		case CY_WCM_SECURITY_WPA2_FBT_PSK:
			printf("WPA2_FBT_PSK");
		break;
		case CY_WCM_SECURITY_WPA3_SAE:
			printf("WPA3_SAE");
		break;
		case CY_WCM_SECURITY_WPA3_WPA2_PSK:
			printf("WPA3_WPA2_PSK");
		break;
		case CY_WCM_SECURITY_IBSS_OPEN:
			printf("IBSS_OPEN");
		break;
		case CY_WCM_SECURITY_WPS_SECURE:
			printf("WPS_SECURE");
		break;
		case CY_WCM_SECURITY_UNKNOWN:
			printf("UNKNOWN");
		break;
		case CY_WCM_SECURITY_FORCE_32_BIT:
			printf("FORCE_32_BIT");
		break;
	}</pre>
<p>With a complete scanCallback function I can now update the networkTask to deal with the queued commands of net_scan type.  This simply either stops or starts the scan based on the message parameter.</p>
<pre class="start-line:212 EnlighterJSRAW" data-enlighter-language="c"">			case net_scan: // 0=stop scan !0=start scan
				if(msg.val0 == 0)
					cy_wcm_stop_scan();
				else
				{
					printf("\n");
					result = cy_wcm_start_scan(scanCallback,0,0);
					if(result != CY_RSLT_SUCCESS)
						printf("Scan error\n");
				}
			break;</pre>
<p>The last things to do is to add the scan command to the usrcmd.c</p>
<pre class="start-line:149 EnlighterJSRAW" data-enlighter-language="c"">static int usrcmd_scan(int argc, char **argv)
{

    static int state=0;

    if(argc==1)
    {
        state = (state ==0)?1:0;
    }
    else if(argc == 2)
    {
        if(strcmp(argv[1],"on") == 0)
        {
            state=1;
        } 
        else if(strcmp(argv[1],"off") == 0)
        {
            state = 0;
        }
        else
        {
            printf("usage: scan [on|off]\n");
            return 0;
        }
        
    }
    else
    {
        printf("usage scan: [on|off]\n");
        return 0;
    }
    
    networkQueueMsg_t msg;
    msg.cmd = net_scan;
    msg.val0 = state;
    xQueueSend(networkQueue,(const void *)&amp;msg,portMAX_DELAY);
    return 0;
}</pre>
<p>OK.  Let it rip.  You should be able to run network scans.  In the next Article I will add a connect and disconnect commands.</p>
<p>You can find all of this example code at https://github.com/iotexpert/wcm_example</p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/anycloud-wireless-connection-manager-part-1/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>PSoC 6 &#038; Using the MCWDT as a Deep Sleep Timer</title>
		<link>https://iotexpert.com/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/</link>
					<comments>https://iotexpert.com/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Mon, 06 Apr 2020 12:22:21 +0000</pubDate>
				<category><![CDATA[PSoC 6]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=8725</guid>

					<description><![CDATA[Summary This article walks you through the steps to use the PSoC 6 MultiCounter Watch Dog Timer (MCWDT) as a DeepSleep Timer.  This includes using the Cypress HAL (lptimer), PDL as well as the configurators to achieve this goal. The Story If you want to build a low power PSoC 6 project you need a [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>This article walks you through the steps to use the PSoC 6 MultiCounter Watch Dog Timer (MCWDT) as a DeepSleep Timer.  This includes using the Cypress HAL (lptimer), PDL as well as the configurators to achieve this goal.</p>
<p><span><p>This article is part of the "PSoC 6 Low Power Techniques" Series which covers a range of tools you have to lower the power of your system.  The following articles are (or will be) available:</p>
<p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >Articles</th>
</tr>
</thead>
<tbody>
<tr><td >PSoC 6 Low Power</td>
</tr>

<tr><td >PSoC 6 &amp; Using the MCWDT as a Deep Sleep Timer</td>
</tr>

<tr><td >PSoC 6 Deep Sleep Wakeup Time</td>
</tr>

<tr><td >PSoC 6 &amp; FreeRTOS Tickless</td>
</tr>

<tr><td >Managing the PSoC 6 Clock Frequency</td>
</tr>

<tr><td >Using the PSoC 6 LDO and SIMO Buck Regulators for Low Power</td>
</tr>

<tr><td >Using the PSoC 6 Always on Backup Domain</td>
</tr>

<tr><td >PSoC 6 Turning off block of RAM</td>
</tr>
</tbody></table></div></p>
<p>The following resources are available</p>
<p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >Resource</th>
</tr>
</thead>
<tbody>
<tr><td >Cypress Application Note: <a href="https://www.cypress.com/file/385936/download" target="_blank" rel="noopener"><span>PSoC 6 MCU Low-Power Modes and Power Reduction Techniques</span></a></td>
</tr>
</tbody></table></div></p></span></p>
<h1>The Story</h1>
<p>If you want to build a low power PSoC 6 project you need a method to wake up the chip from DeepSleep.  This is particularly true of projects that use an RTOS in Tickless mode where you need a mechanism to go to DeepSleep and wakeup at a specific time.  Why DeepSleep?  Because it burns way-way less power than the active modes.  Way less means something like 3 orders of magnitude less. <a href="https://iotexpert.com/2020/04/06/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/screen-shot-2020-03-15-at-5-42-34-am/" rel="attachment wp-att-8795"></a></p>
<p>In this article I will</p>
<ul>
<li>Describe the MultiCounter Watch Dog Timer (MCWDT)</li>
<li>Configure the MCWDT using the PSoC 6 Configurator</li>
<li>Configure the MCWDT using PDL</li>
<li>Configure the MCWDT using the HAL &#8211; AKA the lptimer</li>
<li>Show the LP Timer Deep Sleep Current vs. Active Current</li>
<li>Measure the Sleep Time &amp; Show Interactions with DeepSleep and rest of the PSoC6</li>
</ul>
<h1>The Multi Counter Watch Dog Timer (MWCDT)</h1>
<p>Inside of the PSoC 6 there are two blocks that have some variant of the name Watch Dog Timer.  Specifically the:</p>
<ul>
<li>Multi Counter Watch Dog Timer (MCWDT)</li>
<li>Watch Dog Timer (WDT)</li>
</ul>
<p>Here is a screenshot from the PSoC 6 block diagram.</p>
<p><a href="https://iotexpert.com/?attachment_id=8806" rel="attachment wp-att-8806"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-6.55.41-AM.png" alt="" width="500" height="472" class="alignnone size-full wp-image-8806" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-6.55.41-AM.png 500w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-6.55.41-AM-300x283.png 300w" sizes="(max-width: 500px) 100vw, 500px" /></a></p>
<p>As I was working on this article I spoke with the original architect of the PSoC 6 and he told me that you should use the WDT as a WDT (but that it could be used as a periodic timer) and you should use the MCWDT as a periodic timer (but that it could be used as a WDT).  This is a pretty common example of the Cypress design aesthetic of maximizing flexibility.</p>
<p>Bottom line:  You should use the MCWDT as a deep sleep timer.</p>
<p>What is the MCWDT?  It is actually three counters which can be used individually or cascaded.  The counters each up count to a specific match value (aka period) and reset or they optionally count continuously.  And each counter can generate an interrupt when the period is reached.  The one &#8220;weird&#8221; thing is that counter 2 doesn&#8217;t have a match value, it has a match BIT position meaning it will match every 2,4,8,16&#8230;  times depending on the bit position.</p>
<p>The counters are clocked by one of the 32kHz clocks in the PSoC 6 called &#8220;LFCLK&#8221; which can either be the WCO, the ILO or the PILO.  This means that each &#8220;count&#8221; is 1/32768 = 30uS-ish</p>
<p>This means you can have</p>
<ul>
<li>2&#215;16-bit counters and 1&#215;32 bit</li>
<li>2&#215;32-bit counters</li>
<li>1&#215;16-bit and 1&#215;48-bit counter</li>
<li>1&#215;64-bit counter</li>
</ul>
<p>Here is a picture of the block diagram from the TRM.</p>
<p><a href="https://iotexpert.com/2020/04/06/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/mcwdt/" rel="attachment wp-att-8739"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/mcwdt.png" alt="" width="900" height="432" class="alignnone size-full wp-image-8739" srcset="https://iotexpert.com/wp-content/uploads/2020/03/mcwdt.png 900w, https://iotexpert.com/wp-content/uploads/2020/03/mcwdt-600x288.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/mcwdt-300x144.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/mcwdt-768x369.png 768w" sizes="(max-width: 900px) 100vw, 900px" /></a></p>
<h1>PSoC 6 Configurator</h1>
<p>For the first project I will use the PSoC 6 Configurators to setup the PDL configuration structures.   I will make a project that will blink an LED at 1Hz.  To do this I will configure counter 0 to turn 30.5uS into milliseconds and counter 1 to count 500 milliseconds.  When counter 1 hits 500 it will toggle the LED.</p>
<p>Start by making a new project:</p>
<p><a href="https://iotexpert.com/2020/04/06/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/screen-shot-2020-03-15-at-8-33-56-am/" rel="attachment wp-att-8809"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.33.56-AM-1024x514.png" alt="" width="1024" height="514" class="alignnone size-large wp-image-8809" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.33.56-AM-1024x514.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.33.56-AM-600x301.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.33.56-AM-300x151.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.33.56-AM-768x386.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.33.56-AM.png 1434w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Give it a name and choose the Empty PSoC6 App as a template</p>
<p><a href="https://iotexpert.com/2020/04/06/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/screen-shot-2020-03-15-at-8-34-17-am/" rel="attachment wp-att-8810"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.34.17-AM-1024x513.png" alt="" width="1024" height="513" class="alignnone size-large wp-image-8810" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.34.17-AM-1024x513.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.34.17-AM-600x300.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.34.17-AM-300x150.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.34.17-AM-768x385.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.34.17-AM.png 1430w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>From the quick panel choose &#8220;Device Configurator (new configuration)&#8221;</p>
<p><a href="https://iotexpert.com/2020/04/06/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/screen-shot-2020-03-15-at-8-43-12-am/" rel="attachment wp-att-8814"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.43.12-AM.png" alt="" width="764" height="500" class="alignnone size-full wp-image-8814" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.43.12-AM.png 764w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.43.12-AM-600x393.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.43.12-AM-300x196.png 300w" sizes="(max-width: 764px) 100vw, 764px" /></a></p>
<p>Then click on Peripherals &#8211;&gt; System and enable Multi-Counter Watchdog Timer (MCWDT) 0.</p>
<p><a href="https://iotexpert.com/2020/04/06/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/screen-shot-2020-03-15-at-8-36-12-am/" rel="attachment wp-att-8811"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.36.12-AM-1024x870.png" alt="" width="1024" height="870" class="alignnone size-large wp-image-8811" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.36.12-AM-1024x870.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.36.12-AM-600x510.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.36.12-AM-300x255.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.36.12-AM-768x653.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.36.12-AM-1536x1306.png 1536w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.36.12-AM.png 1854w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Notice that you can configure it to count up to the max then rollover (also known as Free running) or you can have it reset the counters back to 0 when it hits the match value (also known as Clear on match)</p>
<p><a href="https://iotexpert.com/2020/04/06/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/screen-shot-2020-03-15-at-8-36-25-am/" rel="attachment wp-att-8812"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.36.25-AM-1024x161.png" alt="" width="1024" height="161" class="alignnone size-large wp-image-8812" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.36.25-AM-1024x161.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.36.25-AM-600x94.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.36.25-AM-300x47.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.36.25-AM-768x121.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.36.25-AM.png 1184w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>And when it his the end what do you want it to do?  Interrupt, Watchdog reset, 3xWatchdog reset or nothing?</p>
<p><a href="https://iotexpert.com/2020/04/06/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/screen-shot-2020-03-15-at-8-36-37-am/" rel="attachment wp-att-8813"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.36.37-AM-1024x176.png" alt="" width="1024" height="176" class="alignnone size-large wp-image-8813" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.36.37-AM-1024x176.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.36.37-AM-600x103.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.36.37-AM-300x52.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.36.37-AM-768x132.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.36.37-AM.png 1258w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Here is the configuration we want for this example:</p>
<p><a href="https://iotexpert.com/2020/04/06/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/screen-shot-2020-03-15-at-8-47-57-am/" rel="attachment wp-att-8815"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.47.57-AM-981x1024.png" alt="" width="981" height="1024" class="alignnone size-large wp-image-8815" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.47.57-AM-981x1024.png 981w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.47.57-AM-600x626.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.47.57-AM-287x300.png 287w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.47.57-AM-768x802.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.47.57-AM.png 1374w" sizes="(max-width: 981px) 100vw, 981px" /></a></p>
<p>When you hit &#8220;save&#8221; the configurator will update the files in &#8220;libs-&gt;TARGET_CY8CKIT-062-WiFi-BT-&gt;COMPONENT_BSP_DESIGN_MODUS-&gt;GeneratedSource&#8221;.  Specifically cycfg_peripherals.h/.c.</p>
<p><a href="https://iotexpert.com/2020/04/06/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/screen-shot-2020-03-15-at-8-50-03-am/" rel="attachment wp-att-8816"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.50.03-AM-670x1024.png" alt="" width="670" height="1024" class="alignnone size-large wp-image-8816" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.50.03-AM-670x1024.png 670w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.50.03-AM-600x917.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.50.03-AM-196x300.png 196w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.50.03-AM-768x1174.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.50.03-AM.png 852w" sizes="(max-width: 670px) 100vw, 670px" /></a></p>
<p>In the cycfg_peripherals.h it makes an #define alias called &#8220;mycounter_&#8221; which is sets you up for the MCWDT0</p>
<p><a href="https://iotexpert.com/2020/04/06/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/screen-shot-2020-03-15-at-8-50-27-am/" rel="attachment wp-att-8817"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.50.27-AM-1024x208.png" alt="" width="1024" height="208" class="alignnone size-large wp-image-8817" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.50.27-AM-1024x208.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.50.27-AM-600x122.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.50.27-AM-300x61.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.50.27-AM-768x156.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.50.27-AM.png 1064w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>And in the cycfg_peripherals.c it sets up the configuration structure</p>
<p><a href="https://iotexpert.com/2020/04/06/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/screen-shot-2020-03-15-at-8-51-12-am/" rel="attachment wp-att-8818"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.51.12-AM-1024x528.png" alt="" width="1024" height="528" class="alignnone size-large wp-image-8818" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.51.12-AM-1024x528.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.51.12-AM-600x309.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.51.12-AM-300x155.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.51.12-AM-768x396.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-8.51.12-AM.png 1378w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now you can write this simple program in main.c</p>
<ul>
<li>Lines 5-9 provide an ISR that toggles the LED and clears the MCWDT interrupt.  Notice that I assumed the interrupt was counter 1</li>
<li>Lines 18-27 setup interrupts for the MCWDT0</li>
<li>Lines 30-36 use the PDL configuration structure form the PSoC 6 configurator to setup the MCWDT</li>
</ul>
<p>The main loop just goes into DeepSleep to save power.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include "cybsp.h"
#include "cyhal.h"
#include &lt;stdio.h&gt;

void mcwdtISR()
{
	Cy_MCWDT_ClearInterrupt	(mycounter_HW,CY_MCWDT_CTR1);
	cyhal_gpio_toggle(CYBSP_USER_LED1);
}


int main(void)
{
	cybsp_init() ;

	cyhal_gpio_init(CYBSP_USER_LED1,CYHAL_GPIO_DIR_OUTPUT,CYHAL_GPIO_DRIVE_STRONG,1);

	cy_stc_sysint_t intrCfg =
	{
			.intrSrc = mycounter_IRQ,
			.intrPriority = 4UL
	};

	Cy_SysInt_Init(&amp;intrCfg, mcwdtISR);
	NVIC_EnableIRQ(mycounter_IRQ);

	__enable_irq();


	Cy_MCWDT_Unlock(mycounter_HW);
	Cy_MCWDT_Disable(mycounter_HW, CY_MCWDT_CTR0 | CY_MCWDT_CTR1, 100);

	Cy_MCWDT_Init(mycounter_HW,&amp;mycounter_config);
	Cy_MCWDT_SetInterruptMask(	MCWDT_STRUCT0,	CY_MCWDT_CTR1	);

	Cy_MCWDT_Enable(mycounter_HW, CY_MCWDT_CTR0 | CY_MCWDT_CTR1 , 100);

	for(;;)
	{
		Cy_SysPm_DeepSleep(CY_SYSPM_WAIT_FOR_INTERRUPT);
	}
}
</pre>
<h1>Basic HAL LP Timer</h1>
<p>Obviously you can do all of the PDL stuff&#8230; but we also provide a higher level abstraction called the &#8220;LPTIMER&#8221;  which does all of the configuration for you.   To demonstrate this I create the same program except using the HAL.  This program will use the LPTIMER and an event to blink the LED at 1hz (toggle every 500ms).</p>
<p>The main.c has:</p>
<ul>
<li>Lines 9-12 a simple function to turn milliseconds into ticks of the MCWDT (remember it is clocked with 32KHz)</li>
<li>Lines 14-18 is the event handler function which is called when the LPTIMER expires.  All it does is toggle the LED and reset the timer to MSDELAY</li>
<li>Lines 26-29 setup the LPTimer, enable the interrupt event, register the callback function and then set the timer</li>
</ul>
<p>The main loop just goes to DeepSleep.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include "cy_pdl.h"
#include "cyhal.h"
#include "cybsp.h"

#define MSDELAY 500

cyhal_lptimer_t myTimer;

static inline uint32_t msToTicks(uint32_t ms)
{
	return (uint32_t)(CY_SYSCLK_WCO_FREQ*(ms/1000));
}

void lptimer_event(void *callback_arg, cyhal_lptimer_event_t event)
{
	cyhal_gpio_toggle(CYBSP_LED8);
	cyhal_lptimer_set_delay(&amp;myTimer, msToTicks(MSDELAY));
}

int main(void)
{

	cybsp_init() ;
	cyhal_gpio_init(CYBSP_LED8,CYHAL_GPIO_DIR_OUTPUT,CYHAL_GPIO_DRIVE_STRONG,1);

	cyhal_lptimer_init(&amp;myTimer);
	cyhal_lptimer_enable_event (&amp;myTimer, CYHAL_LPTIMER_COMPARE_MATCH, 4, true);
	cyhal_lptimer_register_callback(&amp;myTimer, lptimer_event, 0);
	cyhal_lptimer_set_delay(&amp;myTimer, msToTicks(MSDELAY));

	__enable_irq();

	for(;;)
	{
		cyhal_system_deepsleep();

	}
}
</pre>
<h1>LP Timer Deep Sleep Current vs. Active Current</h1>
<p>For the next project I will use the LPTIMER to demonstrate the DeepSleep current versus the Active Current.  This project:</p>
<ul>
<li>Will use LED8 to indicate DeepSleep and LED9 to indicate Active</li>
<li>Go to DeepSleep for 2 seconds (so you can read the power)</li>
<li>Wait in a busy-wait loop for 2 seconds</li>
<li>Loop again</li>
</ul>
<p>The main.c has:</p>
<ul>
<li>Lines 8-12 a simple function to turn milliseconds into ticks of the MCWDT (remember it is clocked with 32KHz)</li>
<li>Lines 17-18 Setup the two LEDs to indicate the power state</li>
<li>Lines 20-21 initialize the low power timer.  Notice that you can enable the event (which will wakeup the chip) but you don&#8217;t have to provide an event handler.  The LPTIMER ISR will handle clearing the interrupts for you</li>
<li>Lines 27-29 will setup the LPTIMER delay, turn on LED8 and off LED9 and go to DeepSleep</li>
<li>Lines 31-32 will turn off LED8 and on LED9 then go into a busy wait loop.</li>
</ul>
<p>Notice that I did two three things weren&#8217;t aren&#8217;t awesome:</p>
<ol>
<li>I hardcoded the 0/1 for the LED state (they are active low)&#8230; this is too bad since the BSP actually provides CYBSP_LED_STATE_ON</li>
<li>I put two LED set on the same line (two statements on the same line is super dangerous)</li>
<li>No comments (yes Hassane will make commentary on this)</li>
</ol>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include "cyhal.h"
#include "cybsp.h"

cyhal_lptimer_t myTimer;

#define MSDELAY 2000

static inline uint32_t msToTicks(uint32_t ms)
{
	return (uint32_t)(CY_SYSCLK_WCO_FREQ*(ms/1000));
}

int main(void)
{
	cybsp_init() ;

	cyhal_gpio_init(CYBSP_LED8,CYHAL_GPIO_DIR_OUTPUT,CYHAL_GPIO_DRIVE_STRONG,1);
	cyhal_gpio_init(CYBSP_LED9,CYHAL_GPIO_DIR_OUTPUT,CYHAL_GPIO_DRIVE_STRONG,1);

	cyhal_lptimer_init(&amp;myTimer);
	cyhal_lptimer_enable_event (&amp;myTimer, CYHAL_LPTIMER_COMPARE_MATCH, 4, true);

	__enable_irq();

	for(;;)
	{
		cyhal_lptimer_set_delay(&amp;myTimer,msToTicks(MSDELAY));
		cyhal_gpio_write(CYBSP_LED8,0);	cyhal_gpio_write(CYBSP_LED9,1);
		cyhal_system_deepsleep();

		cyhal_gpio_write(CYBSP_LED8,1);	cyhal_gpio_write(CYBSP_LED9,0);
		Cy_SysLib_Delay(MSDELAY);

	}
}
</pre>
<p>On the back of my development kit I setup the multimeter to be in series with the PSoC 6 power.  Notice that I keep from loosing the jumper by attaching it to only 1 of the pins.</p>
<p><a href="https://iotexpert.com/2020/04/06/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/img_1619/" rel="attachment wp-att-8823"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/IMG_1619-768x1024.jpg" alt="" width="768" height="1024" class="alignnone size-large wp-image-8823" srcset="https://iotexpert.com/wp-content/uploads/2020/03/IMG_1619-768x1024.jpg 768w, https://iotexpert.com/wp-content/uploads/2020/03/IMG_1619-scaled-600x800.jpg 600w, https://iotexpert.com/wp-content/uploads/2020/03/IMG_1619-225x300.jpg 225w, https://iotexpert.com/wp-content/uploads/2020/03/IMG_1619-1152x1536.jpg 1152w, https://iotexpert.com/wp-content/uploads/2020/03/IMG_1619-1536x2048.jpg 1536w, https://iotexpert.com/wp-content/uploads/2020/03/IMG_1619-scaled.jpg 1920w" sizes="(max-width: 768px) 100vw, 768px" /></a></p>
<p>When I measure the power I get Active power of about 10mA</p>
<p><a href="https://iotexpert.com/2020/04/06/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/img_1610/" rel="attachment wp-att-8737"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/IMG_1610-768x1024.jpg" alt="" width="768" height="1024" class="alignnone size-large wp-image-8737" srcset="https://iotexpert.com/wp-content/uploads/2020/03/IMG_1610-768x1024.jpg 768w, https://iotexpert.com/wp-content/uploads/2020/03/IMG_1610-scaled-600x800.jpg 600w, https://iotexpert.com/wp-content/uploads/2020/03/IMG_1610-225x300.jpg 225w, https://iotexpert.com/wp-content/uploads/2020/03/IMG_1610-1152x1536.jpg 1152w, https://iotexpert.com/wp-content/uploads/2020/03/IMG_1610-1536x2048.jpg 1536w, https://iotexpert.com/wp-content/uploads/2020/03/IMG_1610-scaled.jpg 1920w" sizes="(max-width: 768px) 100vw, 768px" /></a></p>
<p>And DeepSleep power of 15uA&#8230; that&#8217;s cool almost 1000x difference.</p>
<p><a href="https://iotexpert.com/2020/04/06/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/img_1608/" rel="attachment wp-att-8736"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/IMG_1608-768x1024.jpg" alt="" width="768" height="1024" class="alignnone size-large wp-image-8736" srcset="https://iotexpert.com/wp-content/uploads/2020/03/IMG_1608-768x1024.jpg 768w, https://iotexpert.com/wp-content/uploads/2020/03/IMG_1608-scaled-600x800.jpg 600w, https://iotexpert.com/wp-content/uploads/2020/03/IMG_1608-225x300.jpg 225w, https://iotexpert.com/wp-content/uploads/2020/03/IMG_1608-1152x1536.jpg 1152w, https://iotexpert.com/wp-content/uploads/2020/03/IMG_1608-1536x2048.jpg 1536w, https://iotexpert.com/wp-content/uploads/2020/03/IMG_1608-scaled.jpg 1920w" sizes="(max-width: 768px) 100vw, 768px" /></a></p>
<p>The one things that took me a LONG time to figure out is that I was having problems with the PSoC 6 resetting.  My multimeter has a bunch of different ranges for current measurement.  Each of these different ranges have a different shunt-resistor to increase the accuracy of the meter.  The interesting part is that the meter actually uses a mechanical relay to switch between the ranges.  You can hear it clicking between the ranges.  It turns out that sometime the relay switching took long enough that the PSoC6 would loose power and reset.</p>
<p><a href="https://iotexpert.com/2020/04/06/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/img_1620/" rel="attachment wp-att-8824"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/IMG_1620-1024x768.jpg" alt="" width="1024" height="768" class="alignnone size-large wp-image-8824" srcset="https://iotexpert.com/wp-content/uploads/2020/03/IMG_1620-1024x768.jpg 1024w, https://iotexpert.com/wp-content/uploads/2020/03/IMG_1620-scaled-600x450.jpg 600w, https://iotexpert.com/wp-content/uploads/2020/03/IMG_1620-300x225.jpg 300w, https://iotexpert.com/wp-content/uploads/2020/03/IMG_1620-768x576.jpg 768w, https://iotexpert.com/wp-content/uploads/2020/03/IMG_1620-1536x1152.jpg 1536w, https://iotexpert.com/wp-content/uploads/2020/03/IMG_1620-2048x1536.jpg 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Measure the Sleep Time?</h1>
<p>The next thing that I was curious about was how long it actually slept.  So I created a new project which would print out the counter values before and after the sleeps using printf which was added to my project with &#8220;Retarget I/O&#8221;.  You can do this in the library manager.</p>
<p><a href="https://iotexpert.com/2020/04/06/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/screen-shot-2020-03-10-at-7-53-33-am/" rel="attachment wp-att-8742"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-7.53.33-AM-1024x767.png" alt="" width="1024" height="767" class="alignnone size-large wp-image-8742" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-7.53.33-AM-1024x767.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-7.53.33-AM-600x449.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-7.53.33-AM-300x225.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-7.53.33-AM-768x575.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-7.53.33-AM-1536x1150.png 1536w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-7.53.33-AM-2048x1533.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Once the library is added to your project you can enable it by adding the</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include "cy_retarget_io.h"</pre>
<p>And</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">cy_retarget_io_init(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX, CY_RETARGET_IO_BAUDRATE);</pre>
<p>In this project I add prints to look at the sleep time&#8230; on lines 42 &amp; 48</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include "cy_pdl.h"
#include "cyhal.h"
#include "cybsp.h"
#include "cy_retarget_io.h"

cyhal_lptimer_t myTimer;

#define MSDELAY 2000

static inline uint32_t msToTicks(uint32_t ms)
{
	return (uint32_t)(CY_SYSCLK_WCO_FREQ*(ms/1000));
}

int main(void)
{

	uint32_t sleepTime=0,wakeTime=0,totalTime=0;

	cybsp_init() ;


	cy_retarget_io_init(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX, CY_RETARGET_IO_BAUDRATE);
	printf("Started Project\n");

	cyhal_gpio_init(CYBSP_LED8,CYHAL_GPIO_DIR_OUTPUT,CYHAL_GPIO_DRIVE_STRONG,1);
	cyhal_gpio_init(CYBSP_LED9,CYHAL_GPIO_DIR_OUTPUT,CYHAL_GPIO_DRIVE_STRONG,1);


	cyhal_lptimer_init(&amp;myTimer);
	cyhal_lptimer_enable_event (&amp;myTimer, CYHAL_LPTIMER_COMPARE_MATCH, 4, true);

	__enable_irq();

	for(;;)
	{
		cyhal_lptimer_set_delay(&amp;myTimer,msToTicks(MSDELAY));
		cyhal_gpio_write(CYBSP_LED8,0);	cyhal_gpio_write(CYBSP_LED9,1);
		sleepTime = cyhal_lptimer_read(&amp;myTimer);
		cyhal_system_deepsleep();
		wakeTime = cyhal_lptimer_read(&amp;myTimer);
		printf("Deep Sleep Time = %ld Wake=%ld Sleep=%ld\n",wakeTime-sleepTime,wakeTime,sleepTime);

		cyhal_gpio_write(CYBSP_LED8,1);	cyhal_gpio_write(CYBSP_LED9,0);
		sleepTime = cyhal_lptimer_read(&amp;myTimer);
		Cy_SysLib_Delay(MSDELAY);
		wakeTime = cyhal_lptimer_read(&amp;myTimer);
		totalTime = (wakeTime&lt;sleepTime)?wakeTime+UINT32_MAX-sleepTime:wakeTime-sleepTime;
		printf("Sleep Time = %ld %ld %ld\n",totalTime,wakeTime,sleepTime);

	}
}
</pre>
<p>I immediately see that something is wrong.  Now my project never DeepSleeps.  But why?</p>
<p><a href="https://iotexpert.com/2020/04/06/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/screen-shot-2020-03-10-at-7-58-59-am/" rel="attachment wp-att-8743"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-7.58.59-AM-1024x714.png" alt="" width="1024" height="714" class="alignnone size-large wp-image-8743" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-7.58.59-AM-1024x714.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-7.58.59-AM-600x419.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-7.58.59-AM-300x209.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-7.58.59-AM-768x536.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-7.58.59-AM.png 1184w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>In order to figure this out I add the code to look at the return code from the cyhal_deep_sleep function.</p>
<pre class="start-line:42 EnlighterJSRAW" data-enlighter-language="c"">		cy_rslt_t failedCode = cyhal_system_deepsleep();
		wakeTime = cyhal_lptimer_read(&amp;myTimer);
		printf("Deep Sleep Time = %ld Wake=%ld Sleep=%ld\n",wakeTime-sleepTime,wakeTime,sleepTime);
		uint8_t type = CY_RSLT_GET_TYPE(failedCode);
		uint16_t module_id = CY_RSLT_GET_MODULE(failedCode);
		uint16_t error_code = CY_RSLT_GET_CODE(failedCode);
		printf("Type = %02X Module=%02X Code=%02X\n",type,module_id,error_code);
</pre>
<p>When I run this you see that I am getting error code 0xFF.  But where is that coming from?</p>
<p><a href="https://iotexpert.com/2020/04/06/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/screen-shot-2020-03-10-at-9-22-56-am/" rel="attachment wp-att-8744"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-9.22.56-AM-1024x430.png" alt="" width="1024" height="430" class="alignnone size-large wp-image-8744" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-9.22.56-AM-1024x430.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-9.22.56-AM-600x252.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-9.22.56-AM-300x126.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-9.22.56-AM-768x322.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-9.22.56-AM.png 1182w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now is a good time to examine the lower power callbacks.  When you initialize the UART in the HAL it will add a low power callback.  The low power call back is called in one of four situations:</p>
<p><a href="https://iotexpert.com/2020/04/06/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/screen-shot-2020-03-15-at-10-01-40-am/" rel="attachment wp-att-8826"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-10.01.40-AM-1024x209.png" alt="" width="1024" height="209" class="alignnone size-large wp-image-8826" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-10.01.40-AM-1024x209.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-10.01.40-AM-600x123.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-10.01.40-AM-300x61.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-10.01.40-AM-768x157.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-10.01.40-AM-1536x314.png 1536w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-15-at-10.01.40-AM-2048x419.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>When you look a the hal uart initialization function in cy</p>
<p>&nbsp;</p>
<pre class="start-line:178 EnlighterJSRAW" data-enlighter-language="c" ">cy_rslt_t cyhal_uart_init(cyhal_uart_t *obj, cyhal_gpio_t tx, cyhal_gpio_t rx, const cyhal_clock_divider_t *clk, const cyhal_uart_cfg_t *cfg)
{
    CY_ASSERT(NULL != obj);</pre>
<p>On line 278 it sets up the callback function &#8220;cyhal_uart_pm_callback&#8221;</p>
<pre class="start-line:276 EnlighterJSRAW" data-enlighter-language="c"">        obj-&gt;pm_params.base = obj-&gt;base;
        obj-&gt;pm_params.context = obj;
        obj-&gt;pm_callback.callback = &amp;cyhal_uart_pm_callback;
        obj-&gt;pm_callback.type = CY_SYSPM_DEEPSLEEP;
        obj-&gt;pm_callback.skipMode = 0;
        obj-&gt;pm_callback.callbackParams = &amp;(obj-&gt;pm_params);
        obj-&gt;pm_callback.prevItm = NULL;
        obj-&gt;pm_callback.nextItm = NULL;
        if (!Cy_SysPm_RegisterCallback(&amp;(obj-&gt;pm_callback)))
            result = CYHAL_UART_RSLT_ERR_PM_CALLBACK;</pre>
<p>Then when you look in cy_hal_uart.c you see that the function will call the function Cy_SCB_UART_DeepSleepCallback (line 103)</p>
<pre class="start-line:99 EnlighterJSRAW" data-enlighter-language="c"">static cy_en_syspm_status_t cyhal_uart_pm_callback(cy_stc_syspm_callback_params_t *params, cy_en_syspm_callback_mode_t mode)
{
    cyhal_uart_t *obj = params-&gt;context;
    cy_stc_syspm_callback_params_t pdl_params = { .base = obj-&gt;base, .context = &amp;(obj-&gt;context) };
    cy_en_syspm_status_t rslt = Cy_SCB_UART_DeepSleepCallback(&amp;pdl_params, mode);
    GPIO_PRT_Type *txport = obj-&gt;pin_tx != NC ? CYHAL_GET_PORTADDR(obj-&gt;pin_tx) : NULL,
                  *rtsport = obj-&gt;pin_rts != NC ? CYHAL_GET_PORTADDR(obj-&gt;pin_rts) : NULL;
    uint8_t txpin = (uint8_t)CYHAL_GET_PIN(obj-&gt;pin_tx), rtspin = (uint8_t)CYHAL_GET_PIN(obj-&gt;pin_rts);
    switch (mode)
    {
        case CY_SYSPM_CHECK_READY:
            if (rslt == CY_SYSPM_SUCCESS)
            {
                if (NULL != txport)
                {
                    obj-&gt;saved_tx_hsiom = Cy_GPIO_GetHSIOM(txport, txpin);
                    Cy_GPIO_Set(txport, txpin);
                    Cy_GPIO_SetHSIOM(txport, txpin, HSIOM_SEL_GPIO);
                }
                if (NULL != rtsport)
                {
                    obj-&gt;saved_rts_hsiom = Cy_GPIO_GetHSIOM(rtsport, rtspin);
                    Cy_GPIO_Set(rtsport, rtspin);
                    Cy_GPIO_SetHSIOM(rtsport, rtspin, HSIOM_SEL_GPIO);
                }
            }
            break;

        case CY_SYSPM_CHECK_FAIL: // fallthrough
        case CY_SYSPM_AFTER_TRANSITION:
            if (NULL != txport)
            {
                Cy_GPIO_SetHSIOM(txport, txpin, obj-&gt;saved_tx_hsiom);
            }
            if (NULL != rtsport)
            {
                Cy_GPIO_SetHSIOM(rtsport, rtspin, obj-&gt;saved_rts_hsiom);
            }
            break;

        default:
            break;
    }
    return rslt;
}</pre>
<p>And in the PDL file cy_scb_uart.c you can see that this function will fail if there is data currently being transmitted (line 315)</p>
<pre class="start-line:301 EnlighterJSRAW" data-enlighter-language="c"">cy_en_syspm_status_t Cy_SCB_UART_DeepSleepCallback(cy_stc_syspm_callback_params_t *callbackParams, cy_en_syspm_callback_mode_t mode)
{
    cy_en_syspm_status_t retStatus = CY_SYSPM_FAIL;

    CySCB_Type *locBase = (CySCB_Type *) callbackParams-&gt;base;
    cy_stc_scb_uart_context_t *locContext = (cy_stc_scb_uart_context_t *) callbackParams-&gt;context;

    switch(mode)
    {
        case CY_SYSPM_CHECK_READY:
        {
            /* Check whether the High-level API is not busy executing the transmit
            * or receive operation.
            */
            if ((0UL == (CY_SCB_UART_TRANSMIT_ACTIVE &amp; Cy_SCB_UART_GetTransmitStatus(locBase, locContext))) &amp;&amp;
                (0UL == (CY_SCB_UART_RECEIVE_ACTIVE  &amp; Cy_SCB_UART_GetReceiveStatus (locBase, locContext))))
            {
                /* If all data elements are transmitted from the TX FIFO and
                * shifter and the RX FIFO is empty: the UART is ready to enter
                * Deep Sleep mode.
                */
                if (Cy_SCB_UART_IsTxComplete(locBase))
                {
                    if (0UL == Cy_SCB_UART_GetNumInRxFifo(locBase))
                    {
                        /* Disable the UART. The transmitter stops driving the
                        * lines and the receiver stops receiving data until
                        * the UART is enabled.
                        * This happens when the device failed to enter Deep
                        * Sleep or it is awaken from Deep Sleep mode.
                        */
                        Cy_SCB_UART_Disable(locBase, locContext);

                        retStatus = CY_SYSPM_SUCCESS;
                    }
                }
            }
        }
        break;

        case CY_SYSPM_CHECK_FAIL:
        {
            /* The other driver is not ready for Deep Sleep mode. Restore the
            * Active mode configuration.
            */

            /* Enable the UART to operate */
            Cy_SCB_UART_Enable(locBase);

            retStatus = CY_SYSPM_SUCCESS;
        }
        break;

        case CY_SYSPM_BEFORE_TRANSITION:
            /* Do noting: the UART is not capable of waking up from
            * Deep Sleep mode.
            */
        break;

        case CY_SYSPM_AFTER_TRANSITION:
        {
            /* Enable the UART to operate */
            Cy_SCB_UART_Enable(locBase);

            retStatus = CY_SYSPM_SUCCESS;
        }
        break;

        default:
            break;
    }

    return (retStatus);
}</pre>
<p>But, where does the error code come from?  When you click on it, you find yourself on this enumeration:</p>
<pre class="start-line:1469 EnlighterJSRAW" data-enlighter-language="c"">typedef enum
{
    CY_SYSPM_SUCCESS         = 0x0U,                                         /**&lt; Successful. */
    CY_SYSPM_BAD_PARAM       = CY_SYSPM_ID | CY_PDL_STATUS_ERROR | 0x01U,    /**&lt; One or more invalid parameters. */
    CY_SYSPM_TIMEOUT         = CY_SYSPM_ID | CY_PDL_STATUS_ERROR | 0x02U,    /**&lt; A time-out occurred. */
    CY_SYSPM_INVALID_STATE   = CY_SYSPM_ID | CY_PDL_STATUS_ERROR | 0x03U,    /**&lt; The operation is not setup or is in an
                                                                                  improper state. */
    CY_SYSPM_CANCELED        = CY_SYSPM_ID | CY_PDL_STATUS_ERROR | 0x04U,    /**&lt; Operation canceled. */
    CY_SYSPM_SYSCALL_PENDING = CY_SYSPM_ID | CY_PDL_STATUS_ERROR | 0x05U,    /**&lt; Canceled due syscall operation pending. */
    CY_SYSPM_FAIL            = CY_SYSPM_ID | CY_PDL_STATUS_ERROR | 0xFFU     /**&lt; Unknown failure. */
} cy_en_syspm_status_t;</pre>
<p>And when you look at CY_SYSPM_ID your find:</p>
<pre class="start-line:1264 EnlighterJSRAW" data-enlighter-language="c" ">/** SysPm driver identifier */
#define CY_SYSPM_ID                      (CY_PDL_DRV_ID(0x10U))
</pre>
<p>OK&#8230; so the reason in our project that it doesn&#8217;t go into deep sleep is that the printf from earlier has not finished.  To fix this you update your project to keep trying until the DeepSleep call succeeds (on lines 43-45)</p>
<pre class="start-line:37 EnlighterJSRAW" data-enlighter-language="c" ">	for(;;)
	{
		cyhal_lptimer_set_delay(&amp;myTimer,msToTicks(MSDELAY));
		cyhal_gpio_write(CYBSP_LED8,0);	cyhal_gpio_write(CYBSP_LED9,1);
		sleepTime = cyhal_lptimer_read(&amp;myTimer);
		cy_rslt_t failedCode;
		do {
			failedCode = cyhal_system_deepsleep();
		} while(failedCode != CY_SYSPM_SUCCESS);

		wakeTime = cyhal_lptimer_read(&amp;myTimer);

		printf("Deep Sleep Time = %ld Wake=%ld Sleep=%ld\n",wakeTime-sleepTime,wakeTime,sleepTime);


		cyhal_gpio_write(CYBSP_LED8,1);	cyhal_gpio_write(CYBSP_LED9,0);
		sleepTime = cyhal_lptimer_read(&amp;myTimer);
		Cy_SysLib_Delay(MSDELAY);
		wakeTime = cyhal_lptimer_read(&amp;myTimer);
 		totalTime = (wakeTime&lt;sleepTime)?wakeTime+UINT32_MAX-sleepTime:wakeTime-sleepTime;
		printf("Sleep Time = %ld %ld %ld\n",totalTime,wakeTime,sleepTime);
	}</pre>
<p>Now this makes a lot more sense</p>
<p><a href="https://iotexpert.com/2020/04/06/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/screen-shot-2020-03-10-at-9-32-57-am/" rel="attachment wp-att-8745"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-9.32.57-AM-1024x217.png" alt="" width="1024" height="217" class="alignnone size-large wp-image-8745" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-9.32.57-AM-1024x217.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-9.32.57-AM-600x127.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-9.32.57-AM-300x64.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-9.32.57-AM-768x163.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-10-at-9.32.57-AM.png 1188w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/psoc-6-using-the-mcwdt-as-a-deep-sleep-timer/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>ModusToolbox 2.0 &#8211; Libraries an Example for emWin &#038; Super Manifests</title>
		<link>https://iotexpert.com/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/</link>
					<comments>https://iotexpert.com/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Mon, 30 Mar 2020 09:22:24 +0000</pubDate>
				<category><![CDATA[ModusToolbox]]></category>
		<category><![CDATA[PSoC 6]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=8380</guid>

					<description><![CDATA[Summary In this article I will walk you through creating a library for ModusToolbox 2.0 that will glue the Cypress RTOS Abstraction layer, emWin, the SSD1306 and the PSoC 6 together.  In the end, the library will become available in the Modus Toolbox Library Manager by creating a Super Manifest file. NOTE:  This blog was [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>In this article I will walk you through creating a library for ModusToolbox 2.0 that will glue the Cypress RTOS Abstraction layer, emWin, the SSD1306 and the PSoC 6 together.  In the end, the library will become available in the Modus Toolbox Library Manager by creating a Super Manifest file.</p>
<p><span style="color: #ff0000;"><strong>NOTE:  This blog was originally written about the Modus Toolbox 2.0 library scheme.  With the release of Modus Toolbox 2.2 this scheme was changed and this blog is now obsolete! Instead, you can use the Infineon display-oled-ssd1306 library.</strong></span></p>
<h1>The Story</h1>
<p>I like using the Segger emWin graphics library.  And, I have always hated getting the &#8220;glue&#8221; into my project that makes the library work.  The glue includes the configuration files for emWin plus the hardware driver functions to talk to the SSD1306 screen.  I have always thought that it would be really nice to have a simple library scheme &#8211; yes I know that there are lots of schemes out there but I wanted one that was neatly integrated into our tools.  Well, with ModusToolbox 2.0 my wish was granted.</p>
<p>In ModusToolbox 2.0 if you create a file called &#8220;someLibraryName.lib&#8221; that contains a URL to a GitHub (or Git) repository, the make system will know how to bring that library into your project.  The make target &#8220;getlibs&#8221; does exactly that.  And, once it is on your computer in your project the Modus Toolbox make system will know how to include it as part of your project.</p>
<p>For this article I will create a library called &#8220;<span>p6sdk-ssd1306-emWin-freerots-config&#8221; which will contain:</span></p>
<div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >Files</th>
<th >Purpose</th>
</tr>
</thead>
<tbody>
<tr><td >GUIConf.h</td>
<td >Configures emWins abilities, fonts etc.</td>
</tr>

<tr><td >GUIConf.c</td>
<td >Defines &amp; Assigns RAM for the GUI,initializes the font</td>
</tr>

<tr><td ><span>GUI_X_CYRTOS.c</td>
<td >Makes a connection between emWin and Cypress RTOS abstraction for things like timing, semaphore etc.</span></td>
</tr>

<tr><td ><span>LCDConf.h</td>
<td >A blank file</span></td>
</tr>

<tr><td ><span>LCDConf.c</td>
<td >Functions to configure screen,connect the emWin APIs to the I2C write functions, configures the display driver</span></td>
</tr>

<tr><td >SSD1306Driver.h</td>
<td >Functions prototypes to initialize the I2C and read/write the I2C</td>
</tr>

<tr><td >SSD1306Driver.c</td>
<td >Initialize the SSD1305 driver, write commands, write data, write datastream functions which are called by the LCDConf.c functions</td>
</tr>

<tr><td >template</td>
<td >A directory (which is not compiled) that contains template c files for use as an example</td>
</tr>
</tbody></table></div>
<h1>SSD1306 Driver</h1>
<p>In order to glue the hardware to the emWin library you need to provide functions that</p>
<ol>
<li>Initialize the driver &#8211; (Tell it what I2C hardware and I2C address the display is connected to)</li>
<li>Write data/command bytes and streams</li>
<li>Read data streams (which it actually never does)</li>
</ol>
<p>The SSD1306Driver.h provides a public interface for these functions which are used in the LCDConf.c file.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#ifndef SSD1306_DRIVER_H
#define SSD1306_DRIVER_H
#include "cyhal.h"


void SSD1306DriverInit(cyhal_i2c_t *obj,uint8_t oledAddress);


void          SSD1306_WriteCommandByte(unsigned char c);
void          SSD1306_WriteDataByte(unsigned char c);
void          SSD1306_WriteDataStream(unsigned char * pData, int NumBytes);
void          SSD1306_ReadDataStream(unsigned char * pData, int NumBytes);

#endif
</pre>
<p>The first part of SSD1306Driver.c makes a pointer to and Cypress HAL I2C object.  In the initialization code, it connects the provided I2C object and the static pointer.  The purpose of this is to allow the application developer to use the I2C for other devices on the bus, in other words the I2C bus is shared.  On most of my screens the I2C address is 0x3C,  so I let the user not provide an I2C address.  Probably in hindsight I should have just made them always provide an I2C address.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include "SSD1306Driver.h"
#include "GUI.h"
#include "cyhal.h"
#include "cybsp.h"
#include &lt;stdlib.h&gt;

/*********************************************************************
*
*       Defines: Configuration
*
**********************************************************************
  Needs to be adapted to custom hardware.
*/
/* I2C port to communicate with the OLED display controller */
static cyhal_i2c_t *I2C=0;

/* I2C slave address, Command and Data byte prefixes for the display controller */
#define OLED_CONTROL_BYTE_CMD       (0x00)
#define OLED_CONTROL_BYTE_DATA      (0x40)
static uint8_t OLED_I2C_ADDRESS     =    (0x3C);

void SSD1306DriverInit(cyhal_i2c_t *obj,uint8_t oledAddress)

{
	CY_ASSERT(obj);
	I2C=obj;
	if(oledAddress)
		OLED_I2C_ADDRESS = oledAddress;
}</pre>
<p>In order for emWin to update a display it needs to be able to write data to the display via &#8220;commands&#8221; and &#8220;data&#8221; writes.  However, it doesn&#8217;t know anything about the fact that these displays are typically attached via I2C.  The function SSD1306_WriteCommandByte uses the Cypress HAL master write API to send a command byte to the display.  This function is called by emWin via the configuration in LCDConf.c</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">void SSD1306_WriteCommandByte(unsigned char c)
{
    uint8_t buff[2];

    /* The first byte of the buffer tells the display that the following byte
        is a command */
    buff[0] = OLED_CONTROL_BYTE_CMD;
    buff[1] = (char)c;

    /* Write the buffer to display controller */
    cyhal_i2c_master_write(I2C, OLED_I2C_ADDRESS, buff, 2, 0, true);
}</pre>
<p>And the write data byte function works exactly the same way as the write command byte, except it send a different first byte.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">void SSD1306_WriteDataByte(unsigned char c)
{
	uint8_t buff[2];

    /* First byte of the buffer tells the display controller that the following byte
        is a data byte */
    buff[0] = OLED_CONTROL_BYTE_DATA;
    buff[1] = c;

    /* Write the buffer to display controller */
    cyhal_i2c_master_write(I2C, OLED_I2C_ADDRESS, buff, 2, 0, true);
}</pre>
<h1>emWin Configuration Files</h1>
<p>I have written extensively about how to configure emWin and specifically how to setup the files for the SSD1306.  You can see the articles <a href="https://iotexpert.com/?s=ssd1306" target="_blank" rel="noopener noreferrer">here</a>.  So, I am not going to describe those files in detail except to say that the ones that you need are GUIConf.h/.c, LCDConf.h/.c and GUI_X_CYRTOS.c.  The only changes from the previous configurations of LCDConf.c is to hookup the APIs we defined in the previous section.</p>
<pre class="start-line:235 EnlighterJSRAW" data-enlighter-language="c">    PortAPI.pfWrite8_A0  = SSD1306_WriteCommandByte;
    PortAPI.pfWrite8_A1  = SSD1306_WriteDataByte;
    PortAPI.pfWriteM8_A1 = SSD1306_WriteDataStream;</pre>
<h1>GitHub</h1>
<p>I checked in all of these files into GitHub at git@github.com:iotexpert/p6sdk-ssd1306-emwin-cyrtos-config.git  Now they can be used as a Modus Toolbox library.</p>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-24-at-2-51-55-pm/" rel="attachment wp-att-8960"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-2.51.55-PM-1024x809.png" alt="" width="1024" height="809" class="alignnone size-large wp-image-8960" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-2.51.55-PM-1024x809.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-2.51.55-PM-600x474.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-2.51.55-PM-300x237.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-2.51.55-PM-768x606.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-2.51.55-PM-1536x1213.png 1536w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-2.51.55-PM.png 1864w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Test Library</h1>
<p>Lets build a test project.  Start by creating  new project.  Notice that I am not using the built in project creator, but a standalone project creator which does not require Eclipse.  This is automatically installed for you as part of the Modus Toolbox installation (so, if you are one of the legions of people who don&#8217;t like Eclipse you don&#8217;t have to use it).  Run it from the start menu.</p>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-22-at-9-06-12-am/" rel="attachment wp-att-8926"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.06.12-AM-1024x681.png" alt="" width="1024" height="681" class="alignnone size-large wp-image-8926" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.06.12-AM-1024x681.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.06.12-AM-600x399.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.06.12-AM-300x199.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.06.12-AM-768x511.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.06.12-AM-1536x1021.png 1536w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.06.12-AM.png 1994w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>For this demo I will use the CY8CKIT-062-WiFi-Bt kit&#8230; but this works on all of of our PSoC 6 kits with Arduino headers.</p>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-22-at-9-06-24-am/" rel="attachment wp-att-8927"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.06.24-AM-1024x681.png" alt="" width="1024" height="681" class="alignnone size-large wp-image-8927" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.06.24-AM-1024x681.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.06.24-AM-600x399.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.06.24-AM-300x200.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.06.24-AM-768x511.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.06.24-AM-1536x1021.png 1536w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.06.24-AM.png 2006w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>I start using the IoT Expert FreeRTOS template.   And I store the project in the directory &#8220;~/iotexpert-projects/xxx&#8221;.  A long time ago I started using directories named &#8220;xxx&#8221; to mean that I can &#8220;rm -rf xxx&#8221; and I never store anything that matters in those files/directories.  Give the project a name then press next.</p>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-22-at-9-06-52-am/" rel="attachment wp-att-8928"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.06.52-AM-1024x678.png" alt="" width="1024" height="678" class="alignnone size-large wp-image-8928" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.06.52-AM-1024x678.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.06.52-AM-600x397.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.06.52-AM-300x199.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.06.52-AM-768x508.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.06.52-AM-1536x1016.png 1536w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.06.52-AM.png 2004w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now you have everything setup.  So click &#8220;Create&#8221;</p>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-22-at-9-07-05-am/" rel="attachment wp-att-8929"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.07.05-AM-1024x680.png" alt="" width="1024" height="680" class="alignnone size-large wp-image-8929" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.07.05-AM-1024x680.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.07.05-AM-600x398.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.07.05-AM-300x199.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.07.05-AM-768x510.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.07.05-AM-1536x1020.png 1536w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.07.05-AM.png 1994w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>And our software will do its thing.</p>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-22-at-9-07-51-am/" rel="attachment wp-att-8930"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.07.51-AM-1024x679.png" alt="" width="1024" height="679" class="alignnone size-large wp-image-8930" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.07.51-AM-1024x679.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.07.51-AM-600x398.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.07.51-AM-300x199.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.07.51-AM-768x509.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.07.51-AM-1536x1018.png 1536w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.07.51-AM.png 1998w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now if I look in the directory, I will have a complete project.</p>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-22-at-9-09-34-am/" rel="attachment wp-att-8931"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.09.34-AM-1024x720.png" alt="" width="1024" height="720" class="alignnone size-large wp-image-8931" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.09.34-AM-1024x720.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.09.34-AM-600x422.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.09.34-AM-300x211.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.09.34-AM-768x540.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.09.34-AM.png 1498w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now lets manually add the .lib for the p6sdk-ssd1306-emWin-cyrtos-config.  You can use whatever editor you want, but it&#8217;s emacs for me.</p>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-24-at-3-07-43-pm/" rel="attachment wp-att-8966"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.07.43-PM-1024x97.png" alt="" width="1024" height="97" class="alignnone size-large wp-image-8966" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.07.43-PM-1024x97.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.07.43-PM-600x57.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.07.43-PM-300x28.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.07.43-PM-768x73.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.07.43-PM-1536x145.png 1536w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.07.43-PM-2048x194.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Enter the URL for the library.  Then put a &#8220;/&#8221;.  Then a branch.</p>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-24-at-3-08-35-pm/" rel="attachment wp-att-8967"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.08.35-PM-1024x982.png" alt="" width="1024" height="982" class="alignnone size-large wp-image-8967" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.08.35-PM-1024x982.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.08.35-PM-600x576.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.08.35-PM-300x288.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.08.35-PM-768x737.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.08.35-PM.png 1176w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now that the file is updated you can run &#8220;make getlibs&#8221; which will search for all of the &#8220;.lib&#8221; files.  Then make sure those libraries are part of your project.</p>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-24-at-3-09-28-pm/" rel="attachment wp-att-8968"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.09.28-PM-1024x717.png" alt="" width="1024" height="717" class="alignnone size-large wp-image-8968" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.09.28-PM-1024x717.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.09.28-PM-600x420.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.09.28-PM-300x210.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.09.28-PM-768x538.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.09.28-PM-1536x1076.png 1536w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.09.28-PM-2048x1435.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>After the make getlibs is run you can see that the p6sdk-ssd1306-emWin-cyrtos-config directory is in your libs directory, with all of the stuff you need.</p>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-24-at-3-10-41-pm/" rel="attachment wp-att-8969"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.10.41-PM-1024x815.png" alt="" width="1024" height="815" class="alignnone size-large wp-image-8969" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.10.41-PM-1024x815.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.10.41-PM-600x477.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.10.41-PM-300x239.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.10.41-PM-768x611.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.10.41-PM-1536x1222.png 1536w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.10.41-PM.png 1704w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>The next step is to edit your main.  I like to use vscode.  If you run  &#8220;make vscode&#8221; our build system will create a vscode project for you.  You can start vscode by running &#8220;code .&#8221;</p>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-24-at-3-11-50-pm/" rel="attachment wp-att-8970"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.11.50-PM-1024x468.png" alt="" width="1024" height="468" class="alignnone size-large wp-image-8970" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.11.50-PM-1024x468.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.11.50-PM-600x274.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.11.50-PM-300x137.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.11.50-PM-768x351.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.11.50-PM-1536x702.png 1536w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.11.50-PM.png 1898w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Before the emWin library work you will need to add the EMWIN_OSNTS and FREERTOS components to your Makefile.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">COMPONENTS=EMWIN_OSNTS FREERTOS</pre>
<p>Here is what it looks like in vscode</p>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-24-at-3-13-19-pm/" rel="attachment wp-att-8971"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.13.19-PM-1024x877.png" alt="" width="1024" height="877" class="alignnone size-large wp-image-8971" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.13.19-PM-1024x877.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.13.19-PM-600x514.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.13.19-PM-300x257.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.13.19-PM-768x658.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.13.19-PM-1536x1315.png 1536w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.13.19-PM-2048x1753.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now, write the your main.c code to display a little message.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include "cybsp.h"
#include "GUI.h"
#include "SSD1306Driver.h"

int main(void)
{
        /* Set up the device based on configurator selections */
        cybsp_init();

        cyhal_i2c_t I2C;

        /* Configuration to initialize the I2C block */
        static cyhal_i2c_cfg_t i2c_config = {
                .is_slave = false,
                .frequencyhal_hz = 400000
        };
        cyhal_i2c_init(&amp;I2C, CYBSP_I2C_SDA, CYBSP_I2C_SCL, NULL);
        cyhal_i2c_configure(&amp;I2C, &amp;i2c_config);

        SSD1306DriverInit(&amp;I2C,0x3C);

        GUI_Init();
        GUI_SetColor(GUI_WHITE);
        GUI_SetBkColor(GUI_BLACK);
        GUI_SetFont(GUI_FONT_8_ASCII);
        GUI_SetTextAlign(GUI_TA_CENTER);
        GUI_DispStringAt("Hello World", GUI_GetScreenSizeX()/2,GUI_GetScreenSizeY()/2 - GUI_GetFontSizeY()/2);
}
</pre>
<p>You can build your project by running &#8220;Build&#8221; from vscode or your can built it on the command line with &#8220;make build&#8221;</p>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-22-at-9-30-51-am/" rel="attachment wp-att-8945"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.30.51-AM-1024x501.png" alt="" width="1024" height="501" class="alignnone size-large wp-image-8945" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.30.51-AM-1024x501.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.30.51-AM-600x294.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.30.51-AM-300x147.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.30.51-AM-768x376.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.30.51-AM-1536x752.png 1536w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.30.51-AM-2048x1002.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>After building you should have output like this:</p>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-24-at-3-14-36-pm/" rel="attachment wp-att-8972"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.14.36-PM-1024x585.png" alt="" width="1024" height="585" class="alignnone size-large wp-image-8972" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.14.36-PM-1024x585.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.14.36-PM-600x343.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.14.36-PM-300x171.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.14.36-PM-768x439.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.14.36-PM-1536x877.png 1536w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.14.36-PM.png 1562w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>You can program by Pressing selecting or by pressing F5 or selecting &#8220;Run-&gt;Start Debugging&#8221;</p>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-22-at-9-34-54-am/" rel="attachment wp-att-8950"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.34.54-AM-1024x158.png" alt="" width="1024" height="158" class="alignnone size-large wp-image-8950" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.34.54-AM-1024x158.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.34.54-AM-600x93.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.34.54-AM-300x46.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.34.54-AM-768x118.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.34.54-AM.png 1336w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now you can press the play button and you are off to the races</p>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-22-at-9-34-02-am/" rel="attachment wp-att-8949"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.34.02-AM-1024x903.png" alt="" width="1024" height="903" class="alignnone size-large wp-image-8949" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.34.02-AM-1024x903.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.34.02-AM-600x529.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.34.02-AM-300x265.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.34.02-AM-768x678.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.34.02-AM-1536x1355.png 1536w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.34.02-AM-2048x1807.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Or on the command line you can program with &#8220;make program&#8221;</p>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-22-at-9-32-44-am/" rel="attachment wp-att-8947"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.32.44-AM-1024x348.png" alt="" width="1024" height="348" class="alignnone size-large wp-image-8947" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.32.44-AM-1024x348.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.32.44-AM-600x204.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.32.44-AM-300x102.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.32.44-AM-768x261.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.32.44-AM-1536x522.png 1536w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.32.44-AM-2048x696.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a> <a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-22-at-9-32-54-am/" rel="attachment wp-att-8948"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.32.54-AM-1024x769.png" alt="" width="1024" height="769" class="alignnone size-large wp-image-8948" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.32.54-AM-1024x769.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.32.54-AM-600x451.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.32.54-AM-300x225.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.32.54-AM-768x577.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.32.54-AM-1536x1154.png 1536w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-9.32.54-AM-2048x1538.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>OK.  Looks like the library works!</p>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/img_1350/" rel="attachment wp-att-8637"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/02/IMG_1350.jpg" alt="" width="800" height="560" class="alignnone size-full wp-image-8637" srcset="https://iotexpert.com/wp-content/uploads/2020/02/IMG_1350.jpg 800w, https://iotexpert.com/wp-content/uploads/2020/02/IMG_1350-600x420.jpg 600w, https://iotexpert.com/wp-content/uploads/2020/02/IMG_1350-300x210.jpg 300w, https://iotexpert.com/wp-content/uploads/2020/02/IMG_1350-768x538.jpg 768w" sizes="(max-width: 800px) 100vw, 800px" /></a></p>
<h1>Updating the IoT Expert MTB2 Manifests</h1>
<p>I showed you how to manually add a library to your project.  But what if you want to have the library you built show up in the Library manager?  In the picture below you can see that is exactly what I did by adding a new category called &#8220;iotexpert&#8221;</p>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-24-at-3-36-32-pm/" rel="attachment wp-att-8973"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.36.32-PM-1024x811.png" alt="" width="1024" height="811" class="alignnone size-large wp-image-8973" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.36.32-PM-1024x811.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.36.32-PM-600x475.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.36.32-PM-300x238.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.36.32-PM-768x608.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.36.32-PM-1536x1216.png 1536w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-24-at-3.36.32-PM-2048x1622.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>To make this work you need a &#8220;Super Manifest&#8221; which is just a XML file that contains URLs references to your custom Application Templates manifest file(s), Middleware manifests file(s), and BSP manifest file(s). A &#8220;manifest&#8221; file is just an XML file with a list of URLs to Git Repos and some meta data.</p>
<p>In words you need:</p>
<ol>
<li>A file in your home directory called ~/.modustoolbox/manifest.loc which contains a URL for your cusom super manifest file</li>
<li>A super manifest file that optionally contains a references to your application templates manifest file(s)</li>
<li>And/Or contains a reference to your middleware manifest file(s)</li>
<li>And/Or contains a reference to your board support manifest(s)</li>
</ol>
<p>I start by creating the manifest.loc file to refer to a specific file on GitHub. &#8220;https://github.com/iotexpert/mtb2-iotexpert-manifests/raw/master/iotexpert-super-manifest.xml&#8221;.  Notice that it is stored in &#8220;.modustoolbo&#8221; which is a directory that starts with a &#8220;.&#8221; which is a PITA on Windows.  The only way I know how to edit/create this is by using the &#8220;modus shell&#8221; (which we provided as part of the Modus Toolbox installation.  Here is a screenshot from my Mac.  Notice that I use the GitHub URL mechanism to get to the &#8220;raw&#8221; file.</p>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-22-at-10-00-20-am/" rel="attachment wp-att-8952"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-10.00.20-AM-1024x136.png" alt="" width="1024" height="136" class="alignnone size-large wp-image-8952" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-10.00.20-AM-1024x136.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-10.00.20-AM-600x80.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-10.00.20-AM-300x40.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-10.00.20-AM-768x102.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-10.00.20-AM.png 1534w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>In my SuperManifest file I create references to the</p>
<ul>
<li>board-manifest-list (of which I have none)</li>
<li>app-manifest-list which links to my application template manifest</li>
<li>middleware-manifest which links to my middleware manifest file</li>
</ul>
<p>Notice that both of these files are on the same GitHub repository.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">&lt;super-manifest&gt;
  &lt;board-manifest-list&gt;
  &lt;/board-manifest-list&gt;
  
  &lt;app-manifest-list&gt;
    &lt;app-manifest&gt;
      &lt;uri&gt;https://github.com/iotexpert/mtb2-iotexpert-manifests/raw/master/iotexpert-app-manifest.xml&lt;/uri&gt;
      &lt;/app-manifest&gt;
   &lt;/app-manifest-list&gt;
  &lt;board-manifest-list&gt;
  &lt;/board-manifest-list&gt;
  &lt;middleware-manifest-list&gt;
    &lt;middleware-manifest&gt;
      &lt;uri&gt;https://github.com/iotexpert/mtb2-iotexpert-manifests/raw/master/iotexpert-mw-manifest.xml&lt;/uri&gt;
    &lt;/middleware-manifest&gt;
  &lt;/middleware-manifest-list&gt;
&lt;/super-manifest&gt;</pre>
<p>Then if you look at the actual middleware manifest you will see that I have two (currently) middleware repositories</p>
<ul>
<li>(The NTSHell) https://github.com/iotexpert/middleware-ntshell</li>
<li>(The SSD1306 Middleware in this article) https://github.com/iotexpert/p6sdk-ssd1306-emWin-cyrtos-config</li>
</ul>
<p>The rest of the XML is meta data which specifies how the middleware is presented by the library manager.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">&lt;middleware&gt;
  &lt;middleware&gt;
    &lt;name&gt;ntshell&lt;/name&gt;
    &lt;id&gt;ntshell&lt;/id&gt;
    &lt;uri&gt;https://github.com/iotexpert/middleware-ntshell&lt;/uri&gt;
    &lt;desc&gt;NT Shell&lt;/desc&gt;
    &lt;category&gt;iotexpert&lt;/category&gt;
    &lt;req_capabilities&gt;psoc6&lt;/req_capabilities&gt;
    &lt;versions&gt;
      &lt;version&gt;
        &lt;num&gt;Latest 1.X release&lt;/num&gt;
        &lt;commit&gt;master&lt;/commit&gt;
        &lt;desc&gt;Latest 1.X release&lt;/desc&gt;
      &lt;/version&gt;
    &lt;/versions&gt;
  &lt;/middleware&gt;
  &lt;middleware&gt;
    &lt;name&gt;SSD1306-emwin-cyrtos-config&lt;/name&gt;
    &lt;id&gt;SSD1306-emwin-cyrtos-config&lt;/id&gt;
    &lt;uri&gt;https://github.com/iotexpert/p6sdk-ssd1306-emwin-cyrtos-config&lt;/uri&gt;
    &lt;desc&gt;SSD1306-emwin-cyrtos-config&lt;/desc&gt;
    &lt;category&gt;iotexpert&lt;/category&gt;
    &lt;req_capabilities&gt;psoc6&lt;/req_capabilities&gt;
    &lt;versions&gt;
      &lt;version&gt;
        &lt;num&gt;Latest 1.X release&lt;/num&gt;
        &lt;commit&gt;master&lt;/commit&gt;
        &lt;desc&gt;Latest 1.X release&lt;/desc&gt;
      &lt;/version&gt;
    &lt;/versions&gt;
  &lt;/middleware&gt;
&lt;/middleware&gt;</pre>
<p>You can see that my GitHub repository contains</p>
<ol>
<li>iotexpert-super-manifest.xml  &#8211; amazingly enough the super manifest</li>
<li>iotexpert-mw-manifest.xml &#8211; my middleware manifest file</li>
<li>iotexpert-app-manifest.xml &#8211; my application template manifest file</li>
</ol>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-22-at-10-14-26-am/" rel="attachment wp-att-8953"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-10.14.26-AM-1024x937.png" alt="" width="1024" height="937" class="alignnone size-large wp-image-8953" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-10.14.26-AM-1024x937.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-10.14.26-AM-600x549.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-10.14.26-AM-300x274.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-10.14.26-AM-768x703.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-10.14.26-AM-1536x1405.png 1536w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-10.14.26-AM.png 1928w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now if you look at the bottom of the library manager you will see that when it starts up it read the Cypress Super Manifest file, as well as the IoT Expert Super Manifest.</p>
<p><a href="https://iotexpert.com/2020/03/30/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/screen-shot-2020-03-22-at-10-16-20-am/" rel="attachment wp-att-8954"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-10.16.20-AM-1024x170.png" alt="" width="1024" height="170" class="alignnone size-large wp-image-8954" srcset="https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-10.16.20-AM-1024x170.png 1024w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-10.16.20-AM-600x100.png 600w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-10.16.20-AM-300x50.png 300w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-10.16.20-AM-768x128.png 768w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-10.16.20-AM-1536x255.png 1536w, https://iotexpert.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-22-at-10.16.20-AM.png 1600w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>To be clear.  If you add my &#8220;manifest.loc&#8221; file to your ~/.modustoolbox directory you will access to all of my libraries.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/modustoolbox-2-0-libraries-an-example-for-emwin-super-manifests/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Mouser PSoC 6 WiFi/BT MBED: L5 CapSense Thread</title>
		<link>https://iotexpert.com/mouser-psoc-6-wifi-bt-mbed-l5-capsense-thread/</link>
					<comments>https://iotexpert.com/mouser-psoc-6-wifi-bt-mbed-l5-capsense-thread/#comments</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Mon, 09 Dec 2019 17:03:47 +0000</pubDate>
				<category><![CDATA[Mouser PSoC 6 WiFi/BT MBED]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=8001</guid>

					<description><![CDATA[Summary In Lesson 05 we will add a CapSense GUI to enable the user to change the setPoint of the thermostat. Specifically I will create a new Thread called “CapSense Thread” which will read the state of the CapSense Buttons and send messages to the temperatureThread. To implement this I will: Import Lesson 04 Add [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><span><h1>IoT Design with Cypress PSoC® 6 MCUs and Wi-Fi/Bluetooth using Arm® Mbed<img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2122.png" alt="™" class="wp-smiley" style="height: 1em; max-height: 1em;" /></h1>
<p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >#</th>
<th >Lesson</th>
<th >GitHub Project</th>
</tr>
</thead>
<tbody>
<tr><td >0</td>
<td ><a href="https://iotexpert.com/2019/12/08/mouser-psoc-6-wifi-bt-mbed-l0-introduction/" target="_blank" rel="noopener">Introduction</a></td>
<td ></td>
</tr>

<tr><td >1</td>
<td ><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l1-developer-resources/" target="_blank" rel="noopener">Developer Resources</a></td>
<td ></td>
</tr>

<tr><td >2</td>
<td ><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l2-the-blinking-thread/" target="_blank" rel="noopener">Your First Project &amp; The Blinking Thread</a></td>
<td >https://github.com/iotexpert/mouser-mbed-02.git</td>
</tr>

<tr><td >3</td>
<td ><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l3-display-thread/" target="_blank" rel="noopener">Display Thread</a></td>
<td >https://github.com/iotexpert/mouser-mbed-03.git</td>
</tr>

<tr><td >4</td>
<td ><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l4-temperature-thread/" target="_blank" rel="noopener">Temperature Thread</a></td>
<td >https://github.com/iotexpert/mouser-mbed-04.git</td>
</tr>

<tr><td >5</td>
<td ><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l5-capsense-thread/" target="_blank" rel="noopener">CapSense Thread</a></td>
<td >https://github.com/iotexpert/mouser-mbed-05.git</td>
</tr>

<tr><td >6</td>
<td ><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l6-wifi-ntp-thread/" target="_blank" rel="noopener">WiFi &amp; NTP Thread</a></td>
<td >https://github.com/iotexpert/mouser-mbed-06.git</td>
</tr>

<tr><td >7</td>
<td ><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l7-the-cy8ckit-062-wifi-bt/" target="_blank" rel="noopener">The CY8CKIT-062-WiFi-BT</a></td>
<td >https://github.com/iotexpert/mouser-mbed-07.git </td>
</tr>

<tr><td >8</td>
<td ><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l8-amazon-aws-mqtt-thread-part1/" target="_blank" rel="noopener">Amazon AWS MQTT Thread - Part1</a></td>
<td >https://github.com/iotexpert/mouser-mbed-08.git</td>
</tr>

<tr><td >9</td>
<td ><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l9-amazon-aws-mqtt-thread-part2/" target="_blank" rel="noopener">Amazon AWS MQTT Thread - Part2</a></td>
<td >https://github.com/iotexpert/mouser-mbed-09.git</td>
</tr>
</tbody></table></div></p>
<p>You can “mbed import https://github.com/iotexpert/mouser-mbed-09.git“ to make a copy of the project in your workspace.</p>
<p>The final architecture of the thermostat looks like this.</p>
<p><a href="https://iotexpert.com/?attachment_id=8027" rel="attachment wp-att-8027"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/overallarch-1024x651.png" alt="" width="1024" height="651" class="alignnone size-large wp-image-8027" srcset="https://iotexpert.com/wp-content/uploads/2019/12/overallarch-1024x651.png 1024w, https://iotexpert.com/wp-content/uploads/2019/12/overallarch-600x381.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/overallarch-300x191.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/overallarch-768x488.png 768w, https://iotexpert.com/wp-content/uploads/2019/12/overallarch.png 1662w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>&nbsp;</p></span></p>
<h1>Summary</h1>
<p>In Lesson 05 we will add a CapSense GUI to enable the user to change the setPoint of the thermostat.</p>
<p><a href="https://iotexpert.com/mouser-psoc-6-wifi-bt-mbed-l5-capsense-thread/lesson5/"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/lesson5-1024x651.png" alt="" class="alignnone wp-image-8032 size-large" width="1024" height="651" srcset="https://iotexpert.com/wp-content/uploads/2019/12/lesson5-1024x651.png 1024w, https://iotexpert.com/wp-content/uploads/2019/12/lesson5-600x381.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/lesson5-300x191.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/lesson5-768x488.png 768w, https://iotexpert.com/wp-content/uploads/2019/12/lesson5.png 1662w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Specifically I will create a new Thread called “CapSense Thread” which will read the state of the CapSense Buttons and send messages to the temperatureThread.</p>
<p>To implement this I will:</p>
<ol>
<li>Import Lesson 04</li>
<li>Add the Cypress CapSense library</li>
<li>Run the CapsSense configurator</li>
<li>Create and code capsenseThread.h</li>
<li>Create and code capsenseThread.cpp</li>
<li>Update main.cpp</li>
<li>Build, Program and Test</li>
</ol>
<h1>Import Lesson 04</h1>
<p>First import Lesson 04 and call your new project “mouser-mbed-05”</p>
<p>https://github.com/iotexpert/mouser-mbed-04.git</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l5-capsense-thread/screen-shot-2019-12-07-at-5-27-02-pm/" rel="attachment wp-att-8082"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.27.02-PM.png" alt="" class="alignnone size-full wp-image-8082" width="898" height="610" srcset="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.27.02-PM.png 898w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.27.02-PM-600x408.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.27.02-PM-300x204.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.27.02-PM-768x522.png 768w" sizes="(max-width: 898px) 100vw, 898px" /></a></p>
<h1>Add the Cypress CapSense library</h1>
<p>In order to use the CapSense library you will need to import it from the Cypress GitHub site.  To do this we will use the Mbed Studio Library manager.  Click on the “Libraries” tab. Then press the “+” button to add a new library.</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l5-capsense-thread/screen-shot-2019-12-07-at-5-28-41-pm/" rel="attachment wp-att-8085"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.28.41-PM-1024x202.png" alt="" class="alignnone size-large wp-image-8085" width="1024" height="202" srcset="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.28.41-PM-1024x202.png 1024w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.28.41-PM-600x118.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.28.41-PM-300x59.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.28.41-PM-768x151.png 768w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.28.41-PM.png 1582w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Give the Library manager the URL to the Cypress CapSense library:</p>
<p>https://github.com/cypresssemiconductorco/capsense.git</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l5-capsense-thread/screen-shot-2019-12-07-at-5-29-26-pm/" rel="attachment wp-att-8086"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.29.26-PM.png" alt="" class="alignnone size-large wp-image-8086" width="806" height="564" srcset="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.29.26-PM.png 806w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.29.26-PM-600x420.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.29.26-PM-300x210.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.29.26-PM-768x537.png 768w" sizes="(max-width: 806px) 100vw, 806px" /></a></p>
<p>Specify that you want your library to point to the master branch.</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l5-capsense-thread/screen-shot-2019-12-07-at-5-29-36-pm/" rel="attachment wp-att-8087"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.29.36-PM.png" alt="" class="alignnone size-large wp-image-8087" width="794" height="384" srcset="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.29.36-PM.png 794w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.29.36-PM-600x290.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.29.36-PM-300x145.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.29.36-PM-768x371.png 768w" sizes="(max-width: 794px) 100vw, 794px" /></a></p>
<p>Once that is done your library screen should look something like this.  When I took this screen shot I noticed that I was on the old version of mbed-os so I pressed the update button.</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l5-capsense-thread/screen-shot-2019-12-07-at-5-29-52-pm/" rel="attachment wp-att-8088"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.29.52-PM-1024x286.png" alt="" class="alignnone size-large wp-image-8088" width="1024" height="286" srcset="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.29.52-PM-1024x286.png 1024w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.29.52-PM-600x168.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.29.52-PM-300x84.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.29.52-PM-768x214.png 768w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.29.52-PM.png 1576w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Now both libraries are at the latest version.</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l5-capsense-thread/screen-shot-2019-12-07-at-5-38-05-pm/" rel="attachment wp-att-8092"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.38.05-PM-1024x295.png" alt="" class="alignnone size-large wp-image-8092" width="1024" height="295" srcset="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.38.05-PM-1024x295.png 1024w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.38.05-PM-600x173.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.38.05-PM-300x86.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.38.05-PM-768x221.png 768w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.38.05-PM.png 1576w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Run the CapSense Configurator</h1>
<p>First, lets run the CapSense configurator to look at the CapSense configuration.  You can find it in ModusToolbox/tools_2.0/capsense_configurator. Then use file&gt;open to open the Target&#8217;s CapSense configuration file from targets/TARGET_Cypress/TARGET_PSOC6/TARGET_CY8PROTO_062_4343w/COMPONENT_BSP_DESIGN_MODUS/design.cycapsense.</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l5-capsense-thread/screen-shot-2019-12-08-at-2-55-42-pm/" rel="attachment wp-att-8198"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.55.42-PM-1024x723.png" alt="" class="alignnone size-large wp-image-8198" width="1024" height="723" srcset="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.55.42-PM-1024x723.png 1024w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.55.42-PM-600x424.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.55.42-PM-300x212.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.55.42-PM-768x542.png 768w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.55.42-PM.png 1946w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Here is the advanced tab:</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l5-capsense-thread/screen-shot-2019-12-08-at-2-56-09-pm/" rel="attachment wp-att-8199"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.56.09-PM-1024x722.png" alt="" class="alignnone size-large wp-image-8199" width="1024" height="722" srcset="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.56.09-PM-1024x722.png 1024w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.56.09-PM-600x423.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.56.09-PM-300x211.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.56.09-PM-768x541.png 768w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.56.09-PM.png 1944w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>The BSP/Target as delivered inside of Mbed OS does not have the generated source code required to run CapSense.  I could press save in the CapSense configurator GUI, but this would modify my version of Mbed OS.  I would rather add these files to the make project.  In order to get this code into my project I run the CapSense Configurator from the command line and tell to save the files locally.</p>
<ul>
<li><span>/Applications/ModusToolbox/tools_2.0/capsense-configurator/capsense-configurator-cli -c mbed-os/targets/TARGET_Cypress/TARGET_PSOC6/TARGET_CY8CKIT_062_WIFI_BT/COMPONENT_BSP_DESIGN_MODUS/design.cycapsense -o `pwd` -g</span></li>
</ul>
<p>Here is what the output looks like:</p>
<h1><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l5-capsense-thread/screen-shot-2019-12-07-at-5-36-07-pm/" rel="attachment wp-att-8090"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.36.07-PM-1024x268.png" alt="" class="alignnone size-large wp-image-8090" width="1024" height="268" srcset="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.36.07-PM-1024x268.png 1024w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.36.07-PM-600x157.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.36.07-PM-300x79.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.36.07-PM-768x201.png 768w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.36.07-PM.png 1610w" sizes="(max-width: 1024px) 100vw, 1024px" /></a>Create and Code capsenseThread.h</h1>
<p>As with all of the other lessons, I want a new thread called “capsenseThread”  Start by making the “dot h”</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l5-capsense-thread/screen-shot-2019-12-07-at-5-27-43-pm/" rel="attachment wp-att-8083"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.27.43-PM.png" alt="" class="alignnone size-large wp-image-8083" width="800" height="296" srcset="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.27.43-PM.png 800w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.27.43-PM-600x222.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.27.43-PM-300x111.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.27.43-PM-768x284.png 768w" sizes="(max-width: 800px) 100vw, 800px" /></a></p>
<p>This will only have the capsenseThread function.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#ifndef CAPSENSE_THREAD_H
#define CAPSENSE_THREAD_H

void capsenseThread(void);

#endif
</pre>
<h1>Create and code capsenseThread.cpp</h1>
<p>Now make the cpp file.</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l5-capsense-thread/screen-shot-2019-12-07-at-5-28-02-pm/" rel="attachment wp-att-8084"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.28.02-PM.png" alt="" class="alignnone size-large wp-image-8084" width="792" height="304" srcset="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.28.02-PM.png 792w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.28.02-PM-600x230.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.28.02-PM-300x115.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-5.28.02-PM-768x295.png 768w" sizes="(max-width: 792px) 100vw, 792px" /></a></p>
<p>The Cypress CapSense system is a combination of hardware and firmware.  It works by:</p>
<ol>
<li>Initializing the Hardware</li>
<li>Initializing the Interrupt</li>
<li>Initializing the callback</li>
<li>Starting a scan (run the hardware block)</li>
<li>Processing the raw data when the scan is done and send a message</li>
<li>Starting another Scan</li>
</ol>
<p>I got the basis for this code by using the CapSense example project that Cypress has posted on GitHub. You can find that code example at:</p>
<p><a href="http://github.com/cypresssemiconductorco/mtb-example-psoc6-capsense-buttons-slider" target="_blank" rel="noopener noreferrer">github.com/cypresssemiconductorco/mtb-example-psoc6-capsense-buttons-slider</a></p>
<h2>Initializing the Hardware</h2>
<pre class="start-line:42 EnlighterJSRAW" data-enlighter-language="c">    /* Configure AMUX bus for CapSense */
    init_cycfg_routing();
    /* Configure PERI clocks for CapSense */
    Cy_SysClk_PeriphAssignDivider(PCLK_CSD_CLOCK, CYBSP_CSD_CLK_DIV_HW, CYBSP_CSD_CLK_DIV_NUM);
    Cy_SysClk_PeriphDisableDivider(CYBSP_CSD_CLK_DIV_HW, CYBSP_CSD_CLK_DIV_NUM);
    Cy_SysClk_PeriphSetDivider(CYBSP_CSD_CLK_DIV_HW, CYBSP_CSD_CLK_DIV_NUM, 0u);
    Cy_SysClk_PeriphEnableDivider(CYBSP_CSD_CLK_DIV_HW, CYBSP_CSD_CLK_DIV_NUM);
    

    /* Initialize the CSD HW block to the default state. */
    Cy_CapSense_Init(&amp;cy_capsense_context);
</pre>
<h2>Initializing the Interrupt</h2>
<p>This sets up the CapSense interrupt to work with the hardware block</p>
<pre class="start-line:54 EnlighterJSRAW" data-enlighter-language="c">    const cy_stc_sysint_t CapSense_ISR_cfg =
    {
        .intrSrc = CYBSP_CSD_IRQ,
        .intrPriority = 4u
    };
    /* Initialize CapSense interrupt */
    Cy_SysInt_Init(&amp;CapSense_ISR_cfg, &amp;CapSense_InterruptHandler);
    NVIC_ClearPendingIRQ(CapSense_ISR_cfg.intrSrc);
    NVIC_EnableIRQ(CapSense_ISR_cfg.intrSrc);
</pre>
<h2>Initializing the callback</h2>
<p>The callback is used to set the semaphore when the scan is done.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">    /* Initialize the CapSense firmware modules. */
    Cy_CapSense_Enable(&amp;cy_capsense_context);
    Cy_CapSense_RegisterCallback(CY_CAPSENSE_END_OF_SCAN_E, CapSenseEndOfScanCallback, &amp;cy_capsense_context);
</pre>
<h2>Starting a scan (run the hardware block)</h2>
<pre class="EnlighterJSRAW" data-enlighter-language="c">    Cy_CapSense_ScanAllWidgets(&amp;cy_capsense_context);  // Launch the initial scan   
</pre>
<h2>Processing Raw Data when the scan completes, and Send Message</h2>
<pre class="EnlighterJSRAW" data-enlighter-language="c">        if (CY_CAPSENSE_NOT_BUSY == Cy_CapSense_IsBusy(&amp;cy_capsense_context))
        {
            Cy_CapSense_ProcessAllWidgets(&amp;cy_capsense_context);
            uint32_t currBtn0Status = Cy_CapSense_IsSensorActive(CY_CAPSENSE_BUTTON0_WDGT_ID, CY_CAPSENSE_BUTTON0_SNS0_ID, &amp;cy_capsense_context);        
            uint32_t currBtn1Status = Cy_CapSense_IsSensorActive(CY_CAPSENSE_BUTTON1_WDGT_ID, CY_CAPSENSE_BUTTON1_SNS0_ID, &amp;cy_capsense_context);       

            if(currBtn0Status != prevBtn0Status &amp;&amp; currBtn0Status==1 )
            {
                tempSendDeltaSetpointF(-0.1);
            }
            prevBtn0Status = currBtn0Status;
    
            if(currBtn1Status != prevBtn1Status &amp;&amp; currBtn1Status == 1)
            {
                tempSendDeltaSetpointF(0.1);
            } 
            prevBtn1Status = currBtn1Status;

             
        } 
</pre>
<h2>Starting another Scan</h2>
<pre class="EnlighterJSRAW" data-enlighter-language="c">Cy_CapSense_ScanAllWidgets(&amp;cy_capsense_context);</pre>
<p>Here is the whole file:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include "mbed.h"
#include "cy_pdl.h"
#include "cycfg_capsense.h"
#include "cycfg.h"
#include "temperatureThread.h"

static Semaphore capsense_sem;


/*****************************************************************************
* Function Name: CapSense_InterruptHandler()
******************************************************************************
* Summary:
*  Wrapper function for handling interrupts from CSD block.
*
*****************************************************************************/
static void CapSense_InterruptHandler(void)
{
    Cy_CapSense_InterruptHandler(CYBSP_CSD_HW, &amp;cy_capsense_context);
}


/*****************************************************************************
* Function Name: CapSenseEndOfScanCallback()
******************************************************************************
* Summary:
*  This function releases a semaphore to indicate end of a CapSense scan.
*
* Parameters:
*  cy_stc_active_scan_sns_t* : pointer to active sensor details.
*
*****************************************************************************/
static void CapSenseEndOfScanCallback(cy_stc_active_scan_sns_t * ptrActiveScan)
{
    capsense_sem.release();
}


void capsenseThread(void)
{

    /* Configure AMUX bus for CapSense */
    init_cycfg_routing();
    /* Configure PERI clocks for CapSense */
    Cy_SysClk_PeriphAssignDivider(PCLK_CSD_CLOCK, CYBSP_CSD_CLK_DIV_HW, CYBSP_CSD_CLK_DIV_NUM);
    Cy_SysClk_PeriphDisableDivider(CYBSP_CSD_CLK_DIV_HW, CYBSP_CSD_CLK_DIV_NUM);
    Cy_SysClk_PeriphSetDivider(CYBSP_CSD_CLK_DIV_HW, CYBSP_CSD_CLK_DIV_NUM, 0u);
    Cy_SysClk_PeriphEnableDivider(CYBSP_CSD_CLK_DIV_HW, CYBSP_CSD_CLK_DIV_NUM);
    

    /* Initialize the CSD HW block to the default state. */
    Cy_CapSense_Init(&amp;cy_capsense_context);

    const cy_stc_sysint_t CapSense_ISR_cfg =
    {
        .intrSrc = CYBSP_CSD_IRQ,
        .intrPriority = 4u
    };
    /* Initialize CapSense interrupt */
    Cy_SysInt_Init(&amp;CapSense_ISR_cfg, &amp;CapSense_InterruptHandler);
    NVIC_ClearPendingIRQ(CapSense_ISR_cfg.intrSrc);
    NVIC_EnableIRQ(CapSense_ISR_cfg.intrSrc);

    /* Initialize the CapSense firmware modules. */
    Cy_CapSense_Enable(&amp;cy_capsense_context);
    Cy_CapSense_RegisterCallback(CY_CAPSENSE_END_OF_SCAN_E, CapSenseEndOfScanCallback, &amp;cy_capsense_context);

    Cy_CapSense_ScanAllWidgets(&amp;cy_capsense_context);  // Launch the initial scan   
    
    uint32_t prevBtn0Status = 0u; 
    uint32_t prevBtn1Status = 0u;
     
    while(1)
    {

        capsense_sem.acquire();
        if (CY_CAPSENSE_NOT_BUSY == Cy_CapSense_IsBusy(&amp;cy_capsense_context))
        {
            Cy_CapSense_ProcessAllWidgets(&amp;cy_capsense_context);
            uint32_t currBtn0Status = Cy_CapSense_IsSensorActive(CY_CAPSENSE_BUTTON0_WDGT_ID, CY_CAPSENSE_BUTTON0_SNS0_ID, &amp;cy_capsense_context);        
            uint32_t currBtn1Status = Cy_CapSense_IsSensorActive(CY_CAPSENSE_BUTTON1_WDGT_ID, CY_CAPSENSE_BUTTON1_SNS0_ID, &amp;cy_capsense_context);       

            if(currBtn0Status != prevBtn0Status &amp;&amp; currBtn0Status==1 )
            {
                tempSendDeltaSetpointF(-0.1);
            }
            prevBtn0Status = currBtn0Status;
    
            if(currBtn1Status != prevBtn1Status &amp;&amp; currBtn1Status == 1)
            {
                tempSendDeltaSetpointF(0.1);
            } 
            prevBtn1Status = currBtn1Status;

            Cy_CapSense_ScanAllWidgets(&amp;cy_capsense_context);  
        } 

    }
}
</pre>
<h1>Update main.cpp</h1>
<p>Inside of the main.cpp I will make the usual changes to turn on the CapSense thread.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include "mbed.h"
#include "blinkThread.h"
#include "displayThread.h"
#include "temperatureThread.h"
#include "capsenseThread.h"

Thread blinkThreadHandle;
Thread displayThreadHandle;
Thread temperatureThreadHandle;
Thread capsenseThreadHandle;

int main()
{

    printf("Started System\n");

    blinkThreadHandle.start(blinkThread);
    displayThreadHandle.start(displayThread);
    temperatureThreadHandle.start(temperatureThread);
    capsenseThreadHandle.start(capsenseThread);

}
</pre>
<h1>Build, Program and Test</h1>
<p>When I build, program and test I can see that by pressing the two CapSense Buttons I can raise and lower the setPoint.</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l5-capsense-thread/img_1166-2/" rel="attachment wp-att-8214"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/IMG_1166-2-1024x741.jpg" alt="" class="alignnone size-large wp-image-8214" width="1024" height="741" srcset="https://iotexpert.com/wp-content/uploads/2019/12/IMG_1166-2-1024x741.jpg 1024w, https://iotexpert.com/wp-content/uploads/2019/12/IMG_1166-2-600x434.jpg 600w, https://iotexpert.com/wp-content/uploads/2019/12/IMG_1166-2-300x217.jpg 300w, https://iotexpert.com/wp-content/uploads/2019/12/IMG_1166-2-768x556.jpg 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/mouser-psoc-6-wifi-bt-mbed-l5-capsense-thread/feed/</wfw:commentRss>
			<slash:comments>8</slash:comments>
		
		
			</item>
		<item>
		<title>Mouser PSoC 6 WiFi/BT MBED: L7 The CY8CKIT-062-WiFi-BT</title>
		<link>https://iotexpert.com/mouser-psoc-6-wifi-bt-mbed-l7-the-cy8ckit-062-wifi-bt/</link>
					<comments>https://iotexpert.com/mouser-psoc-6-wifi-bt-mbed-l7-the-cy8ckit-062-wifi-bt/#respond</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Mon, 09 Dec 2019 17:03:33 +0000</pubDate>
				<category><![CDATA[Mouser PSoC 6 WiFi/BT MBED]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=8003</guid>

					<description><![CDATA[Summary This weekend as I was getting ready for the workshop, my son, Nicholas, was making fun of me for my user interface.  To fix this I decided to show how you could move platforms.  Specifically to the CY8CKIT-062-WiFi-BT which you can buy from mouser.com You could literally just change the target and things would [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><span><h1>IoT Design with Cypress PSoC® 6 MCUs and Wi-Fi/Bluetooth using Arm® Mbed<img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2122.png" alt="™" class="wp-smiley" style="height: 1em; max-height: 1em;" /></h1>
<p><div class="table-responsive"><table  style="width:95%; "  class="easy-table easy-table-default " border="1">
<thead>
<tr><th >#</th>
<th >Lesson</th>
<th >GitHub Project</th>
</tr>
</thead>
<tbody>
<tr><td >0</td>
<td ><a href="https://iotexpert.com/2019/12/08/mouser-psoc-6-wifi-bt-mbed-l0-introduction/" target="_blank" rel="noopener">Introduction</a></td>
<td ></td>
</tr>

<tr><td >1</td>
<td ><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l1-developer-resources/" target="_blank" rel="noopener">Developer Resources</a></td>
<td ></td>
</tr>

<tr><td >2</td>
<td ><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l2-the-blinking-thread/" target="_blank" rel="noopener">Your First Project &amp; The Blinking Thread</a></td>
<td >https://github.com/iotexpert/mouser-mbed-02.git</td>
</tr>

<tr><td >3</td>
<td ><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l3-display-thread/" target="_blank" rel="noopener">Display Thread</a></td>
<td >https://github.com/iotexpert/mouser-mbed-03.git</td>
</tr>

<tr><td >4</td>
<td ><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l4-temperature-thread/" target="_blank" rel="noopener">Temperature Thread</a></td>
<td >https://github.com/iotexpert/mouser-mbed-04.git</td>
</tr>

<tr><td >5</td>
<td ><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l5-capsense-thread/" target="_blank" rel="noopener">CapSense Thread</a></td>
<td >https://github.com/iotexpert/mouser-mbed-05.git</td>
</tr>

<tr><td >6</td>
<td ><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l6-wifi-ntp-thread/" target="_blank" rel="noopener">WiFi &amp; NTP Thread</a></td>
<td >https://github.com/iotexpert/mouser-mbed-06.git</td>
</tr>

<tr><td >7</td>
<td ><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l7-the-cy8ckit-062-wifi-bt/" target="_blank" rel="noopener">The CY8CKIT-062-WiFi-BT</a></td>
<td >https://github.com/iotexpert/mouser-mbed-07.git </td>
</tr>

<tr><td >8</td>
<td ><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l8-amazon-aws-mqtt-thread-part1/" target="_blank" rel="noopener">Amazon AWS MQTT Thread - Part1</a></td>
<td >https://github.com/iotexpert/mouser-mbed-08.git</td>
</tr>

<tr><td >9</td>
<td ><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l9-amazon-aws-mqtt-thread-part2/" target="_blank" rel="noopener">Amazon AWS MQTT Thread - Part2</a></td>
<td >https://github.com/iotexpert/mouser-mbed-09.git</td>
</tr>
</tbody></table></div></p>
<p>You can “mbed import https://github.com/iotexpert/mouser-mbed-09.git“ to make a copy of the project in your workspace.</p>
<p>The final architecture of the thermostat looks like this.</p>
<p><a href="https://iotexpert.com/?attachment_id=8027" rel="attachment wp-att-8027"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/overallarch-1024x651.png" alt="" width="1024" height="651" class="alignnone size-large wp-image-8027" srcset="https://iotexpert.com/wp-content/uploads/2019/12/overallarch-1024x651.png 1024w, https://iotexpert.com/wp-content/uploads/2019/12/overallarch-600x381.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/overallarch-300x191.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/overallarch-768x488.png 768w, https://iotexpert.com/wp-content/uploads/2019/12/overallarch.png 1662w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>&nbsp;</p></span></p>
<h1>Summary</h1>
<p>This weekend as I was getting ready for the workshop, my son, Nicholas, was making fun of me for my user interface.  To fix this I decided to show how you could move platforms.  Specifically to the <a href="https://www.cypress.com/documentation/development-kitsboards/psoc-6-wifi-bt-pioneer-kit-cy8ckit-062-wifi-bt" target="_blank" rel="noopener noreferrer">CY8CKIT-062-WiFi-BT </a>which you can buy from <a href="https://www.mouser.com/new/cypress-semiconductor/cypress-psoc6-wifi-bt-pioneer-kit/" target="_blank" rel="noopener noreferrer">mouser.com</a></p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l7-the-cy8ckit-062-wifi-bt/cy8ckit-062_family_pic_700x600/" rel="attachment wp-att-8189"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/CY8CKIT-062_Family_Pic_700x600.jpg" alt="" width="450" height="385" class="alignnone size-full wp-image-8189" srcset="https://iotexpert.com/wp-content/uploads/2019/12/CY8CKIT-062_Family_Pic_700x600.jpg 450w, https://iotexpert.com/wp-content/uploads/2019/12/CY8CKIT-062_Family_Pic_700x600-300x257.jpg 300w" sizes="(max-width: 450px) 100vw, 450px" /></a></p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l7-the-cy8ckit-062-wifi-bt/lesson7-3/" rel="attachment wp-att-8104"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/lesson7-2-1024x651.png" alt="" width="1024" height="651" class="alignnone size-large wp-image-8104" srcset="https://iotexpert.com/wp-content/uploads/2019/12/lesson7-2-1024x651.png 1024w, https://iotexpert.com/wp-content/uploads/2019/12/lesson7-2-600x381.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/lesson7-2-300x191.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/lesson7-2-768x488.png 768w, https://iotexpert.com/wp-content/uploads/2019/12/lesson7-2.png 1662w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>You could literally just change the target and things would work&#8230; almost.  The one exception is that this development kit doesn&#8217;t have a thermistor.  So, I soldered a different kind of temperature sensor, called a TMP36, onto the kit which means I have to make a few small code updates to the temperatureThread.</p>
<p>In addition to changing to the TMP36 I will also use the TFT as a display for the system, so I need to modify the displayThread.</p>
<p>One of the major points that I wanted to make with this lesson is that if you design your code correctly, a change like this isn’t very hard.</p>
<p>To implement this I will:</p>
<ol>
<li>Provide a TMP36 Temperature Sensor Tutorial</li>
<li>Modify the CY8CKIT-028-TFT</li>
<li>Import Lesson06</li>
<li>Update temperatureThread.cpp</li>
<li>Build, Program and Test</li>
<li>Add Segger emWin Library</li>
<li>Add IoTExpert CY8CKIT-028-TFT configuration</li>
<li>Update mbed_app.json to include driver</li>
<li>Update displayThread.cpp</li>
<li>Build, Program and Test</li>
</ol>
<h1>TMP36 Temperature Sensor</h1>
<p>The TMP36 is a single wire temperature sensor that gives you a voltage that is directly proportional to the temperature.  Specifically, it is 10mV per degree C.</p>
<p>The Analog Devices TMP36 that I am using I bought <a href="https://www.mouser.com/ProductDetail/Analog-Devices/TMP36GT9Z?qs=sGAEpiMZZMv9Q1JI0Mo%2FtZYNPIqRJ81F" target="_blank" rel="noopener noreferrer">here</a> from mouser.com.  It looks like a little 3-wire transistor looking thing.</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l7-the-cy8ckit-062-wifi-bt/adi_to_92_3/" rel="attachment wp-att-8184"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/ADI_TO_92_3.jpg" alt="" width="300" height="300" class="alignnone size-full wp-image-8184" srcset="https://iotexpert.com/wp-content/uploads/2019/12/ADI_TO_92_3.jpg 300w, https://iotexpert.com/wp-content/uploads/2019/12/ADI_TO_92_3-100x100.jpg 100w, https://iotexpert.com/wp-content/uploads/2019/12/ADI_TO_92_3-150x150.jpg 150w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Here is a snapshot from the data sheet of the temperature voltage response</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l7-the-cy8ckit-062-wifi-bt/screen-shot-2019-12-08-at-2-11-24-pm/" rel="attachment wp-att-8185"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.11.24-PM-1024x712.png" alt="" width="1024" height="712" class="alignnone size-large wp-image-8185" srcset="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.11.24-PM-1024x712.png 1024w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.11.24-PM-600x417.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.11.24-PM-300x209.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.11.24-PM-768x534.png 768w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.11.24-PM.png 1472w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>Yes I know that I am a digital guy, but for just three wires, signal, ground and power, it sure looks like there is a lot going on in the package.</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l7-the-cy8ckit-062-wifi-bt/screen-shot-2019-12-08-at-2-13-25-pm/" rel="attachment wp-att-8186"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.13.25-PM-1024x821.png" alt="" width="1024" height="821" class="alignnone size-large wp-image-8186" srcset="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.13.25-PM-1024x821.png 1024w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.13.25-PM-600x481.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.13.25-PM-300x241.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.13.25-PM-768x616.png 768w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.13.25-PM.png 1454w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>The <a href="https://www.mouser.com/datasheet/2/609/TMP35_36_37-1504323.pdf" target="_blank" rel="noopener noreferrer">data sheet</a> (which is actually really interesting)  gives you the parameters to make the linear equation for temperature</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l7-the-cy8ckit-062-wifi-bt/screen-shot-2019-12-08-at-2-15-12-pm/" rel="attachment wp-att-8187"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.15.12-PM-1024x354.png" alt="" width="1024" height="354" class="alignnone size-large wp-image-8187" srcset="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.15.12-PM-1024x354.png 1024w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.15.12-PM-600x208.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.15.12-PM-300x104.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.15.12-PM-768x266.png 768w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-08-at-2.15.12-PM.png 1376w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>To calculate the temperature you just calculate:</p>
<p>T = (degree C/10mV) * V &#8211; 50 degree C</p>
<p>Here is the function to read the temperature:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">static AnalogIn tmp36(A5);
static void readTemp()
{
    float volts;
    
    volts = tmp36.read() * 2.4;
    float temperatureC = 1/.01 * volts - 50.0;
    temperatureF = (temperatureC * 9.0/5.0) + 32;
}</pre>
<h1>Modify the CY8CKIT-028-TFT</h1>
<p>Start this process by attaching the TMP36.  On the back of the CY8CKIT-028-TFT I solder three wires onto the temperature sensor.  One to 3.3v, one to ground and one to A5.  Here is the picture of my ugly ugly solder job.  Yes, that is duct-tape holding it onto the back of the shield.  And yes that is a prototype sticker.</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l7-the-cy8ckit-062-wifi-bt/img_1159/" rel="attachment wp-att-8115"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/IMG_1159-1024x846.jpg" alt="" width="1024" height="846" class="alignnone size-large wp-image-8115" srcset="https://iotexpert.com/wp-content/uploads/2019/12/IMG_1159-1024x846.jpg 1024w, https://iotexpert.com/wp-content/uploads/2019/12/IMG_1159-600x496.jpg 600w, https://iotexpert.com/wp-content/uploads/2019/12/IMG_1159-300x248.jpg 300w, https://iotexpert.com/wp-content/uploads/2019/12/IMG_1159-768x634.jpg 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>I also remove the PDM microphone which is also attached to A5 (I’m not sure that it would cause a problem&#8230; but better safe than sorry)</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l7-the-cy8ckit-062-wifi-bt/img_1150/" rel="attachment wp-att-8191"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/IMG_1150-768x1024.jpg" alt="" width="768" height="1024" class="alignnone size-large wp-image-8191" srcset="https://iotexpert.com/wp-content/uploads/2019/12/IMG_1150-768x1024.jpg 768w, https://iotexpert.com/wp-content/uploads/2019/12/IMG_1150-600x800.jpg 600w, https://iotexpert.com/wp-content/uploads/2019/12/IMG_1150-225x300.jpg 225w, https://iotexpert.com/wp-content/uploads/2019/12/IMG_1150-scaled.jpg 1920w" sizes="(max-width: 768px) 100vw, 768px" /></a></p>
<h1>Import Lesson06</h1>
<p>Now, fix the code.  As always, start by importing the previous lesson.</p>
<p>https://github.com/iotexpert/mouser-mbed-06.git</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l7-the-cy8ckit-062-wifi-bt/screen-shot-2019-12-07-at-7-53-58-pm/" rel="attachment wp-att-8117"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.53.58-PM.png" alt="" width="890" height="612" class="alignnone size-full wp-image-8117" srcset="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.53.58-PM.png 890w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.53.58-PM-600x413.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.53.58-PM-300x206.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.53.58-PM-768x528.png 768w" sizes="(max-width: 890px) 100vw, 890px" /></a></p>
<h1>Update temperatureThread.cpp</h1>
<p>In order to switch between the thermistor and the TMP36 I create two #defines based on the target that is currently active.  This is cool because the Mbed OS creates these #defines automatically.</p>
<pre class="start-line:5 EnlighterJSRAW" data-enlighter-language="c"">#ifdef TARGET_CY8CPROTO_062_4343W
#define THERMISTOR
#endif

#ifdef TARGET_CY8CKIT_062_WIFI_BT
#define TMP36
#endif</pre>
<p>There are a bunch of ways that could have been done, including with the Mbed configuration system which you control with the mbed_app.json.  But this was expedient.</p>
<p>Next, I create the code to measure the temperature using the AnalogIn object. Notice that it is connected to the Arduino A5 pin. Also notice that when the TMP36 is active I have a different readTemp function so I don’t need to change anything else to get the temperature.</p>
<pre class="start-line:125 EnlighterJSRAW" data-enlighter-language="c"">#ifdef TMP36
static AnalogIn tmp36(A5);
static void readTemp()
{
    float volts;
    
    volts = tmp36.read() * 2.4;
    float temperatureC = 1/.01 * volts - 50.0; // from the data sheet
    temperatureF = (temperatureC * 9.0/5.0) + 32; // conver to F
}
#endif
</pre>
<p>Then I update the thermistor readTemp function with the THERMISTOR #define</p>
<pre class="start-line:103 EnlighterJSRAW" data-enlighter-language="c"">#ifdef THERMISTOR
static DigitalOut thermVDD(P10_3,1);
static DigitalOut thermGND(P10_0,0);
static AnalogIn thermOut(P10_1);

static void readTemp()
{
    float refVoltage = thermOut.read() * 2.4; // Range of ADC 0-&gt;2*Vref
    float refCurrent = refVoltage  / 10000.0; // 10k Reference Resistor
    float thermVoltage = 3.3 - refVoltage;    // Assume supply voltage is 3.3v
    float thermResistance = thermVoltage / refCurrent; 
    float logrT = (float32_t)log((float64_t)thermResistance);

    /* Calculate temperature from the resistance of thermistor using Steinhart-Hart Equation */
    float stEqn = (float32_t)((0.0009032679) + ((0.000248772) * logrT) + 
                             ((2.041094E-07) * pow((float64)logrT, (float32)3)));

    float temperatureC = (float32_t)(((1.0 / stEqn) - 273.15)  + 0.5);
    temperatureF = (temperatureC * 9.0/5.0) + 32;
}
#endif</pre>
<p>Here is the whole file:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">#include "mbed.h"
#include "temperatureThread.h"
#include "displayThread.h"


#ifdef TARGET_CY8CPROTO_062_4343W
#define THERMISTOR
#endif

#ifdef TARGET_CY8CKIT_062_WIFI_BT
#define TMP36
#endif


static float temperatureF;
static float setPoint = 75.0;

static void readTemp();

typedef enum {
    CMD_setPointDelta,
    CMD_setPoint,

} command_t;


typedef struct {
    command_t cmd;
    float    value;   /* AD result of measured voltage */
} msg_t;


static Queue&lt;msg_t, 32&gt; queue;
static MemoryPool&lt;msg_t, 16&gt; mpool;

void tempSendDeltaSetpointF(float delta)
{
    msg_t *message = mpool.alloc();
    if(message)
    {
        message-&gt;cmd = CMD_setPointDelta;
        message-&gt;value = delta;
        queue.put(message);
    }
}

void tempSendUpdateCurrentSetPointF(float setPoint)
{
    msg_t *message = mpool.alloc();
    if(message)
    {
        message-&gt;cmd = CMD_setPoint;
        message-&gt;value = setPoint;
        queue.put(message);
    }
}

void temperatureThread()
{

    char buffer[128];
    displaySendUpdateTemp(temperatureF);
    displaySendUpdateSetPoint(setPoint);
    
    while(1)
    {
        osEvent evt = queue.get(200);
        if (evt.status == osEventMessage) {
            msg_t *message = (msg_t*)evt.value.p;
            switch(message-&gt;cmd)
            {
                case CMD_setPointDelta:
                    setPoint += message-&gt;value;
                    displaySendUpdateSetPoint(setPoint);
                break;
                case CMD_setPoint:
                    setPoint = message-&gt;value;
                    displaySendUpdateSetPoint(setPoint);
                break;

            }
            mpool.free(message);

        }
        else
        {
            readTemp();

            // Control the HVAC system with +- 0.5 degree of Hystersis
            if(temperatureF &lt; setPoint - 0.5)
                displaySendUpdateMode(-1.0);
            else if (temperatureF &gt; setPoint + 0.5)
                displaySendUpdateMode(1.0);
            else
                displaySendUpdateMode(0.0);

            displaySendUpdateTemp(temperatureF);
        
        }
    }

}


#ifdef THERMISTOR
static DigitalOut thermVDD(P10_3,1);
static DigitalOut thermGND(P10_0,0);
static AnalogIn thermOut(P10_1);

static void readTemp()
{
    float refVoltage = thermOut.read() * 2.4; // Range of ADC 0-&gt;2*Vref
    float refCurrent = refVoltage  / 10000.0; // 10k Reference Resistor
    float thermVoltage = 3.3 - refVoltage;    // Assume supply voltage is 3.3v
    float thermResistance = thermVoltage / refCurrent; 
    float logrT = (float32_t)log((float64_t)thermResistance);

    /* Calculate temperature from the resistance of thermistor using Steinhart-Hart Equation */
    float stEqn = (float32_t)((0.0009032679) + ((0.000248772) * logrT) + 
                             ((2.041094E-07) * pow((float64)logrT, (float32)3)));

    float temperatureC = (float32_t)(((1.0 / stEqn) - 273.15)  + 0.5);
    temperatureF = (temperatureC * 9.0/5.0) + 32;
}
#endif

#ifdef TMP36
static AnalogIn tmp36(A5);
static void readTemp()
{
    float volts;
    
    volts = tmp36.read() * 2.4;
    float temperatureC = 1/.01 * volts - 50.0;
    temperatureF = (temperatureC * 9.0/5.0) + 32;
}
#endif
</pre>
<h1>Build, Program and Test</h1>
<p>With that little update I can now build, program and test. Notice that my display is still working&#8230; and I can change the temperature by holding the TMP36 between my fingers.</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l7-the-cy8ckit-062-wifi-bt/screen-shot-2019-12-07-at-7-34-40-pm/" rel="attachment wp-att-8106"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.34.40-PM-1024x136.png" alt="" width="1024" height="136" class="alignnone size-large wp-image-8106" srcset="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.34.40-PM-1024x136.png 1024w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.34.40-PM-600x80.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.34.40-PM-300x40.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.34.40-PM-768x102.png 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Add Segger emWin Library</h1>
<p>But, I haven’t really addressed Nicholas’ complaint, the GUI.  To do this I am going to use the Segger emWin library to control the TFT display.  Go to the library manager and add the library.</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l7-the-cy8ckit-062-wifi-bt/screen-shot-2019-12-07-at-7-36-22-pm/" rel="attachment wp-att-8107"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.36.22-PM-1024x242.png" alt="" width="1024" height="242" class="alignnone size-large wp-image-8107" srcset="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.36.22-PM-1024x242.png 1024w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.36.22-PM-600x142.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.36.22-PM-300x71.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.36.22-PM-768x182.png 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>https://github.com/cypresssemiconductorco/emwin.git</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l7-the-cy8ckit-062-wifi-bt/screen-shot-2019-12-07-at-7-37-51-pm/" rel="attachment wp-att-8109"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.37.51-PM.png" alt="" width="788" height="560" class="alignnone size-large wp-image-8109" srcset="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.37.51-PM.png 788w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.37.51-PM-600x426.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.37.51-PM-300x213.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.37.51-PM-768x546.png 768w" sizes="(max-width: 788px) 100vw, 788px" /></a></p>
<p>Use the master branch:</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l7-the-cy8ckit-062-wifi-bt/screen-shot-2019-12-07-at-7-38-00-pm/" rel="attachment wp-att-8110"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.38.00-PM.png" alt="" width="784" height="392" class="alignnone size-large wp-image-8110" srcset="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.38.00-PM.png 784w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.38.00-PM-600x300.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.38.00-PM-300x150.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.38.00-PM-768x384.png 768w" sizes="(max-width: 784px) 100vw, 784px" /></a></p>
<h1>Add IoTExpert CY8CKIT-028-TFT configuration</h1>
<p>The emWin library doesn&#8217;t know anything about how the TFT is attached.  So, I created a driver library which you can use for all of the Cypress development kits.</p>
<p>https://github.com/iotexpert/mbed-os-emwin-st7789v.git</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l7-the-cy8ckit-062-wifi-bt/screen-shot-2019-12-07-at-7-39-00-pm/" rel="attachment wp-att-8111"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.39.00-PM.png" alt="" width="802" height="564" class="alignnone size-large wp-image-8111" srcset="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.39.00-PM.png 802w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.39.00-PM-600x422.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.39.00-PM-300x211.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.39.00-PM-768x540.png 768w" sizes="(max-width: 802px) 100vw, 802px" /></a></p>
<p>Use the master branch:</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l7-the-cy8ckit-062-wifi-bt/screen-shot-2019-12-07-at-7-39-09-pm/" rel="attachment wp-att-8112"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.39.09-PM.png" alt="" width="798" height="390" class="alignnone size-large wp-image-8112" srcset="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.39.09-PM.png 798w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.39.09-PM-600x293.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.39.09-PM-300x147.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.39.09-PM-768x375.png 768w" sizes="(max-width: 798px) 100vw, 798px" /></a></p>
<p>Now your libraries should look like this.</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l7-the-cy8ckit-062-wifi-bt/screen-shot-2019-12-07-at-7-39-25-pm/" rel="attachment wp-att-8113"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.39.25-PM-1024x353.png" alt="" width="1024" height="353" class="alignnone size-large wp-image-8113" srcset="https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.39.25-PM-1024x353.png 1024w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.39.25-PM-600x207.png 600w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.39.25-PM-300x103.png 300w, https://iotexpert.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-07-at-7.39.25-PM-768x265.png 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>Update mbed_app.json to include emWin Driver</h1>
<p>In order to use this driver you need to tell Mbed OS to add the correct archive file.  To do this update mbed_app.json</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">{
    "target_overrides": {
        "*": {
            "target.components_add": ["EMWIN_OSNTS"],
            "platform.stdio-baud-rate": 115200
        }
    }
}
</pre>
<h1>Update displayThread.cpp</h1>
<p>Finally update the displayThread.  Start by adding the include for the Segger library.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#ifdef TARGET_CY8CKIT_062_WIFI_BT
#include "GUI.h"
#endif
</pre>
<p>Then update the displayAtXY function to also print on the display.  By doing it here I don’t have to change anything else in the code because this is the only function that draws on the screen.</p>
<pre class="start-line:71 EnlighterJSRAW" data-enlighter-language="c"">static void displayAtXY(int x, int y,char *buffer)
{
    #ifdef TARGET_CY8CKIT_062_WIFI_BT
    GUI_SetTextAlign(GUI_TA_LEFT);
    GUI_DispStringAt(buffer,(x-1)*8,(y-1)*16); // 8x16 font
    #endif
    // row column
    printf("\033[%d;%dH%s",y,x,buffer);
    fflush(stdout);
}</pre>
<p>Update the main thread to initialize the display when the thread starts.</p>
<pre class="start-line:85 EnlighterJSRAW" data-enlighter-language="c"">void displayThread()
{
    char buffer[128];

    printf("\033[2J\033[H"); // Clear Screen and go Home
    printf("\033[?25l"); // Turn the cursor off
    fflush(stdout);

    #ifdef TARGET_CY8CKIT_062_WIFI_BT
        GUI_Init();
        GUI_SetColor(GUI_WHITE);
        GUI_SetBkColor(GUI_BLACK);
        GUI_SetFont(GUI_FONT_8X16_1);
    #endif</pre>
<h1>Build, Program and Test</h1>
<p>When you program, now you should have a TFT display.  Happy Nicholas?</p>
<p><a href="https://iotexpert.com/2019/12/09/mouser-psoc-6-wifi-bt-mbed-l7-the-cy8ckit-062-wifi-bt/img_1156/" rel="attachment wp-att-8116"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/12/IMG_1156-1024x591.jpg" alt="" width="1024" height="591" class="alignnone size-large wp-image-8116" srcset="https://iotexpert.com/wp-content/uploads/2019/12/IMG_1156-1024x591.jpg 1024w, https://iotexpert.com/wp-content/uploads/2019/12/IMG_1156-600x346.jpg 600w, https://iotexpert.com/wp-content/uploads/2019/12/IMG_1156-300x173.jpg 300w, https://iotexpert.com/wp-content/uploads/2019/12/IMG_1156-768x443.jpg 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/mouser-psoc-6-wifi-bt-mbed-l7-the-cy8ckit-062-wifi-bt/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>MBED OS &#038; CY8CKIT_062S2_43012 &#038; Segger emWin &#038; NTP</title>
		<link>https://iotexpert.com/mbed-os-cy8ckit_062s2_43012-segger-emwin-ntp/</link>
					<comments>https://iotexpert.com/mbed-os-cy8ckit_062s2_43012-segger-emwin-ntp/#comments</comments>
		
		<dc:creator><![CDATA[Alan Hawse]]></dc:creator>
		<pubDate>Mon, 23 Sep 2019 12:00:31 +0000</pubDate>
				<category><![CDATA[CY8CKIT-062S2-43012]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[MBED OS]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=7890</guid>

					<description><![CDATA[Summary Have you ever wondered about the nature of time?  Given the demographics of my readers I am quite sure that all of you have pondered this topic.  In this article I will solve one of the great mysteries of human kind.  What time is it?  Well that may be a little bit over dramatic [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Summary</h1>
<p>Have you ever wondered about the nature of time?  Given the demographics of my readers I am quite sure that all of you have pondered this topic.  In this article I will solve one of the great mysteries of human kind.  What time is it?  Well that may be a little bit over dramatic <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" />  Actually what I will show you is how to use the CY8CKIT-062S2-43012 development kit to get time from the internet using the Network Time Protocol (NTP), update the PSoC 6 RTC and display it on the CY8CKIT-028-TFT using MBED OS.</p>
<p>Unfortunately I will not show you a beautiful way to convert UTC to Eastern Time because I don&#8217;t actually know what that way would be which is very frustrating.  Every way that I know requires me to drag a lot of crap into my PSoC6 which I don&#8217;t really want to do.</p>
<p><a href="https://iotexpert.com/?attachment_id=7895" rel="attachment wp-att-7895"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/IMG_0811-1024x736.jpg" alt="" width="1024" height="736" class="alignnone size-large wp-image-7895" srcset="https://iotexpert.com/wp-content/uploads/2019/08/IMG_0811-1024x736.jpg 1024w, https://iotexpert.com/wp-content/uploads/2019/08/IMG_0811-600x431.jpg 600w, https://iotexpert.com/wp-content/uploads/2019/08/IMG_0811-300x216.jpg 300w, https://iotexpert.com/wp-content/uploads/2019/08/IMG_0811-768x552.jpg 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<p>For this article I will discuss:</p>
<ol>
<li>MBED OS Project Setup</li>
<li>MBED OS Event Queues</li>
<li>The RTOS Architecture</li>
<li>The Display Function(s)</li>
<li>The NTP Thread</li>
<li>The WiFI/Main Thread</li>
<li>The Whole Program</li>
</ol>
<h1>Project Setup: Create and Configure Project + Add the Libraries</h1>
<p>You should start this project by creating a project by running &#8220;mbed new NTPDemo&#8221; or by using mbed studio to create a new project.  To run this project requires at least mbed-os-5.13.3.  You have two basic choices to get the latest mbed.  For some reason which I don&#8217;t totally understand when you run mbed new it gives you a slightly older version of mbed-os.  To get a new version you can either &#8220;cd mbed-os ; mbed update mbed-os-5.13.3&#8221; or to get the latest &#8220;cd mbed-os ; mbed update master&#8221;.</p>
<p>In this project I use three libraries, emWin, the IoT Expert ST7789V library (for the display) and the ntp-client library.  To add them run</p>
<ul>
<li>mbed add <a href="https://github.com/cypresssemiconductorco/emwin.git">https://github.com/cypresssemiconductorco/emwin</a></li>
<li><span>mbed add <a href="https://github.com/ARMmbed/ntp-client.git">https://github.com/ARMmbed/ntp-client/</a></span></li>
<li>mbed add <a href="https://github.com/iotexpert/mbed-os-emwin-st7789v" target="_blank" rel="noopener noreferrer">https://github.com/iotexpert/mbed-os-emwin-st7789v</a></li>
</ul>
<p>The emWin library requires that you tell emWin about which version of the library .a to link with.  You can do this by adding this to the mbed_app.json</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">{
    "target_overrides": {
        "*": {
            "target.components_add": ["EMWIN_OSNTS"]
        }
    }
}
</pre>
<h1>MBED OS Event Queues</h1>
<p>For this project I will use one of the cool RTOS mechanisms that is built into MBED OS,  the &#8220;<a href="https://os.mbed.com/docs/mbed-os/v5.13/apis/eventqueue.html" target="_blank" rel="noopener noreferrer">EventQueue</a>&#8220;.   There is a nice tutorial in the MBED OS <a href="https://os.mbed.com/docs/mbed-os/v5.13/tutorials/the-eventqueue-api.html">documentation</a>.  An EventQueue is a tool for running a function &#8220;later&#8221; and in a different thread context.  What does that mean?  It means that there is a thread that sits around waiting until you tell it to run a function.  You tell it to run the function by pushing a function pointer into it&#8217;s EventQueue.  In other words, an EventQueue is a thread that waits for functions to be pushed into queue.  When the function is pushed into the queue it runs it.</p>
<p>How is this helpful?  There are a several of reasons.</p>
<ul>
<li>If you are in an ISR it allows you to defer execution of something to the main program.</li>
<li>It can be used to serialize access to some resource &#8211; in my case the display.</li>
<li>It allows you to schedule some event to happen regularly</li>
</ul>
<p>When MBED OS starts it automatically creates two of these event queues one of the queue threads runs at &#8220;osPriorityNormal&#8221; and can be accessed via mbed_event_queue();  The other event queue runs at &#8220;osPriorityHigh&#8221; and can be accesed by <span>mbed_highprio_event_queue();  For some reason (which I don&#8217;t understand) these queues are documented on a separate <a href="https://os.mbed.com/docs/mbed-os/v5.13/mbed-os-api-doxy/mbed__shared__queues_8h_source.html" target="_blank" rel="noopener noreferrer">page</a>.</span></p>
<h1>The RTOS Architecture</h1>
<p>Here is a picture of the architecture of my program.</p>
<p>&nbsp;</p>
<p><a href="https://iotexpert.com/?attachment_id=7914" rel="attachment wp-att-7914"><img loading="lazy" decoding="async" src="https://iotexpert.com/wp-content/uploads/2019/08/rtosarch-4-1024x445.png" alt="" width="1024" height="445" class="alignnone size-large wp-image-7914" srcset="https://iotexpert.com/wp-content/uploads/2019/08/rtosarch-4-1024x445.png 1024w, https://iotexpert.com/wp-content/uploads/2019/08/rtosarch-4-600x261.png 600w, https://iotexpert.com/wp-content/uploads/2019/08/rtosarch-4-300x130.png 300w, https://iotexpert.com/wp-content/uploads/2019/08/rtosarch-4-768x334.png 768w, https://iotexpert.com/wp-content/uploads/2019/08/rtosarch-4.png 1089w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></p>
<h1>The Main Thread</h1>
<p>The main function which is also the main thread, which then becomes the WiFi Thread</p>
<ol>
<li>Initializes the GUI</li>
<li>Starts up the Display Event Queue</li>
<li>Turns on the WiFi and attaches a callback (to notify the program of WiFI Status Changes)</li>
<li>Try&#8217;s to connect to the WiFi Network</li>
<li>If it fails, it updates the display and try&#8217;s again after 2 seconds</li>
<li>Once it is connected it starts up the NTP Server Thread</li>
<li>And then waits for the WiFi semaphore to be set&#8230; which only happens if WiFi gets disconnected at which point it goes back to the start of the WiFI connection and try again.</li>
</ol>
<pre class="start-line:133 EnlighterJSRAW" data-enlighter-language="c" ">int main()
{
    int wifiConnectionAttempts;
    int ret;

    GUI_Init();
    displayQueue = mbed_event_queue();
    displayQueue-&gt;call_every(1000, &amp;updateDisplayTime);

    wifi = WiFiInterface::get_default_instance();
    wifi-&gt;attach(&amp;wifiStatusCallback);

   while(1)
   {
       wifiConnectionAttempts = 1;
        do {

            ret = wifi-&gt;connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
            displayQueue-&gt;call(updateDisplayWiFiConnectAttempts,wifiConnectionAttempts);

            if (ret != 0) {
                wifiConnectionAttempts += 1;
                wait(2.0); // If for some reason it doesnt work wait 2s and try again
            }
        } while(ret !=0);

        // If the NTPThread is not running... then start it up
        if(netTimeThreadHandle.get_state() == Thread::Deleted)
            netTimeThreadHandle.start(NTPTimeThread);
 
        WiFiSemaphore.acquire();
   }</pre>
<h1>Display Event Queue</h1>
<p>Display EventQueue is used to run functions which update the display.  By using an EventQueue it ensure that the Display updates happen serially and there are no display resource conflicts.  The four functions are</p>
<ol>
<li>updateDisplayWiFiStatus</li>
<li>updateDisplayWifiConnectAttempts</li>
<li>updateDisplayNTPCount</li>
<li>updateDisplayTime</li>
</ol>
<p>I wanted a function which could display the state of the WiFi connection on the screen.  This is a string of text which is generated in the connection status function.  In order to send the message, the connection status function will &#8220;malloc&#8221; which then requires the updateDisplayWiFiStatus function to free the memory associated with the message.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">#define DISP_LEFTMARGIN 10
#define DISP_TOPMARGIN 4
#define DISP_LINESPACE 2
// updateDisplayWiFiStatus
// Used to display the wifi status
void updateDisplayWiFiStatus(char *status)
{
    GUI_SetFont(GUI_FONT_16_1);
    GUI_DispStringAt(status,DISP_LEFTMARGIN, DISP_TOPMARGIN); 
    free(status);  
}
</pre>
<p>When I started working on this program I had a bug in my connections so I added the ability to tell how many WiFI connections attempts had happened.  I also wondered how many times there might be a disconnect if I ran this program a long time.  The answer is I ran it for two days and it didn&#8217;t disconnect a single time.  This function simply takes a number from the caller and displays it on the screen.  Notice that I use snprintf to make sure that I don&#8217;t overrun the buffer (which I doubt could happen because I made it 128 bytes).</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">// updateDisplayWiFiConnectAttempts
// This function displays the number of attempted connections
void updateDisplayWiFiConnectAttempts(int count)
{
    char buffer[128];
    snprintf(buffer,sizeof(buffer),"WiFi Connect Attempts = %d",count); 
    GUI_SetFont(GUI_FONT_16_1);
    GUI_DispStringAt(buffer,DISP_LEFTMARGIN, DISP_TOPMARGIN + (GUI_GetFontSizeY()+DISP_LINESPACE) ); 
}
</pre>
<p>I was curious how many times the NTP connection would happen.  So I added the ability to display a count.  Notice that I use a static variable to keep track of the number of times this function is called rather than pushing the number as an argument.  Perhaps this is a design flaw?</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">// updateDisplayNTPCount
// updates the display with the number of time the NTP Server has been called
void updateDisplayNTPCount(void)
{
    static int count=0;
    char buffer[128];
    count = count + 1;
    snprintf(buffer,sizeof(buffer),"NTP Updates = %d\n",count);
    GUI_SetFont(GUI_FONT_16_1);
    GUI_DispStringHCenterAt(buffer,LCD_GetXSize()/2,LCD_GetYSize() - GUI_GetFontSizeY()); // near the bottom
}
</pre>
<p>The main display function is the seconds which is displayed in the middle of the screen.  I get the time from the RTC in the PSoC and is set by the NTP Server.  Notice my rather serious hack to handle the Eastern time difference to UTC&#8230; which unfortunately only works in the Summer.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">// updateDisplayTime
// This function updates the time on the screen
void updateDisplayTime()
{
  time_t rawtime;
  struct tm * timeinfo;
  char buffer [128];

  time (&amp;rawtime);
  rawtime = rawtime - (4*60*60); // UTC - 4hours ... serious hack which only works in summer

  timeinfo = localtime (&amp;rawtime);
  strftime (buffer,sizeof(buffer),"%r",timeinfo);
  GUI_SetFont(GUI_FONT_32B_1);
  GUI_DispStringHCenterAt(buffer,LCD_GetXSize()/2,LCD_GetYSize()/2 - GUI_GetFontSizeY()/2);
}</pre>
<h1>NTP Time Thread</h1>
<p>The <a href="https://en.wikipedia.org/wiki/Network_Time_Protocol" target="_blank" rel="noopener noreferrer">Network Time Protocol</a> was invented in 1981 by <a href="https://www.eecis.udel.edu/~mills/" target="_blank" rel="noopener noreferrer">Dr. David Mills</a> for use in getting Internet connected computers to have the right time.  Since then it has been expanded a bunch of times to include Cellular, GPS and other networks.  The scheme includes methods for dealing with propogation delay etc.  However, for our purposes we will just ask one of the NIST computers, what time is it?</p>
<p>The way it works is that you setup a structure with 48 bytes in it.  Then you open a UDP connection to an NTP server (which NIST runs for you) then it will fill out the same structure with some time data and send it back to you.  Here is the packet:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">typedef struct
{

  uint8_t li_vn_mode;      // Eight bits. li, vn, and mode.
                           // li.   Two bits.   Leap indicator.
                           // vn.   Three bits. Version number of the protocol.
                           // mode. Three bits. Client will pick mode 3 for client.

  uint8_t stratum;         // Eight bits. Stratum level of the local clock.
  uint8_t poll;            // Eight bits. Maximum interval between successive messages.
  uint8_t precision;       // Eight bits. Precision of the local clock.

  uint32_t rootDelay;      // 32 bits. Total round trip delay time.
  uint32_t rootDispersion; // 32 bits. Max error aloud from primary clock source.
  uint32_t refId;          // 32 bits. Reference clock identifier.

  uint32_t refTm_s;        // 32 bits. Reference time-stamp seconds.
  uint32_t refTm_f;        // 32 bits. Reference time-stamp fraction of a second.

  uint32_t origTm_s;       // 32 bits. Originate time-stamp seconds.
  uint32_t origTm_f;       // 32 bits. Originate time-stamp fraction of a second.

  uint32_t rxTm_s;         // 32 bits. Received time-stamp seconds.
  uint32_t rxTm_f;         // 32 bits. Received time-stamp fraction of a second.

  uint32_t txTm_s;         // 32 bits and the most important field the client cares about. Transmit time-stamp seconds.
  uint32_t txTm_f;         // 32 bits. Transmit time-stamp fraction of a second.

} __PACKED ntp_packet_t;              // Total: 384 bits or 48 bytes.</pre>
<p>The code to send the packet is really simple.  The only trick is that when you send data on the network you almost always use big endian, so you need to use the function nthol to convert.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">void NTPClient::set_server(char* server, int port) {
    nist_server_address = server;
    nist_server_port = port;
}

time_t NTPClient::get_timestamp(int timeout) {
    const time_t TIME1970 = (time_t)2208988800UL;
    int ntp_send_values[12] = {0};
    int ntp_recv_values[12] = {0};

    SocketAddress nist;

    if (iface) {
        int ret_gethostbyname = iface-&gt;gethostbyname(nist_server_address, &amp;nist);

        if (ret_gethostbyname &lt; 0) {
            // Network error on DNS lookup
            return ret_gethostbyname;
        }

        nist.set_port(nist_server_port);

        memset(ntp_send_values, 0x00, sizeof(ntp_send_values));
        ntp_send_values[0] = '\x1b';

        memset(ntp_recv_values, 0x00, sizeof(ntp_recv_values));

        UDPSocket sock;
        sock.open(iface);
        sock.set_timeout(timeout);

        sock.sendto(nist, (void*)ntp_send_values, sizeof(ntp_send_values));

        SocketAddress source;
        const int n = sock.recvfrom(&amp;source, (void*)ntp_recv_values, sizeof(ntp_recv_values));

        if (n &gt; 10) {
            return ntohl(ntp_recv_values[10]) - TIME1970;
</pre>
<p>The times in the structure are represented with two 32-bit numbers</p>
<ul>
<li># of seconds since 1/1/1900 (notice this is not 1970)</li>
<li># of fractional seconds in 1/2^32 chunks (that ain&#8217;t a whole lotta time)</li>
</ul>
<p>The four numbers are</p>
<ul>
<li>Reference Time &#8211; when you last sent a packet</li>
<li>Origin Time &#8211; when you sent the packet (from your clock)</li>
<li>Receive Time &#8211; when the NTP server received your packet</li>
<li>Transmit Time &#8211; when your NTP server sent the packet back to you</li>
</ul>
<p>You know when you send the packet &#8211; called T1.  You know when you received the packet &#8211; called T4.  You know when the other side received your packet &#8211; called T2 and you know when the other side sent the packet called T3.  With this information you can calculate the network delay, stability of the clocks etc.  However, the simplest thing to do is to take the transit time, which is in UTC, and set your clock assuming 0 delay.</p>
<p>In MBEDOS to set the RTC clock in the PSoC you call the function with the number of seconds since 1/1/1970.  Don&#8217;t forget that the time that comes back from NTP is in seconds since 1/1/1900.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">                set_time(timestamp);
</pre>
<p>Given that the PSoC 6 RTC counts in seconds you can just ignore the partial seconds.</p>
<h1>WiFi Semaphore</h1>
<p>At the top of main I registered to WiFi that I want a callback when the state of the WiFi changes.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">    wifi-&gt;attach(&amp;wifiStatusCallback);
</pre>
<p>This function does two things.</p>
<ul>
<li>Updates the screen as the state goes from unconnected to connected</li>
<li>Unlocks a semaphore to tell the main thread to reconnect.</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="c" ">// wifiStatusCallback
// Changes the display when the wifi status is changed
void wifiStatusCallback(nsapi_event_t status, intptr_t param)
{
    const int buffSize=40;
    char *statusText;
    statusText = (char *)malloc(buffSize);

    switch(param) {
        case NSAPI_STATUS_LOCAL_UP:
            snprintf(statusText,buffSize,"WiFi IP = %s",wifi-&gt;get_ip_address());
            break;
        case NSAPI_STATUS_GLOBAL_UP:
            snprintf(statusText,buffSize,"WiFi IP = %s",wifi-&gt;get_ip_address());
            break;
        case NSAPI_STATUS_DISCONNECTED:
            WiFiSemaphore.release();
            snprintf(statusText,buffSize,"WiFi Disconnected");
            break;
        case NSAPI_STATUS_CONNECTING:
            snprintf(statusText,buffSize,"WiFi Connecting");
            break;
        default:
            snprintf(statusText,buffSize,"Not Supported");
            break;
    }
    displayQueue-&gt;call(updateDisplayWiFiStatus,statusText);
}</pre>
<h1>The Whole Program</h1>
<p>Here is the whole program.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include "mbed.h"
#include "GUI.h"
#include "mbed_events.h"
#include "ntp-client/NTPClient.h"

Thread netTimeThreadHandle;

WiFiInterface *wifi;
EventQueue *displayQueue;
Semaphore WiFiSemaphore;

/******************************************************************************************
*
* Display Functions
*
********************************************************************************************/

#define DISP_LEFTMARGIN 10
#define DISP_TOPMARGIN 4
#define DISP_LINESPACE 2
// updateDisplayWiFiStatus
// Used to display the wifi status
void updateDisplayWiFiStatus(char *status)
{
    GUI_SetFont(GUI_FONT_16_1);
    GUI_DispStringAt(status,DISP_LEFTMARGIN, DISP_TOPMARGIN); 
    free(status);  
}

// updateDisplayWiFiConnectAttempts
// This function displays the number of attempted connections
void updateDisplayWiFiConnectAttempts(int count)
{
    char buffer[128];
    snprintf(buffer,sizeof(buffer),"WiFi Connect Attempts = %d",count); 
    GUI_SetFont(GUI_FONT_16_1);
    GUI_DispStringAt(buffer,DISP_LEFTMARGIN, DISP_TOPMARGIN + (GUI_GetFontSizeY()+DISP_LINESPACE) ); 
}

// updateDisplayNTPCount
// updates the display with the number of time the NTP Server has been called
void updateDisplayNTPCount(void)
{
    static int count=0;
    char buffer[128];
    count = count + 1;
    snprintf(buffer,sizeof(buffer),"NTP Updates = %d\n",count);
    GUI_SetFont(GUI_FONT_16_1);
    GUI_DispStringHCenterAt(buffer,LCD_GetXSize()/2,LCD_GetYSize() - GUI_GetFontSizeY()); // near the bottom
}

// updateDisplayTime
// This function updates the time on the screen
void updateDisplayTime()
{
  time_t rawtime;
  struct tm * timeinfo;
  char buffer [128];

  time (&amp;rawtime);
  rawtime = rawtime - (4*60*60); // UTC - 4hours ... serious hack which only works in summer

  timeinfo = localtime (&amp;rawtime);
  strftime (buffer,sizeof(buffer),"%r",timeinfo);
  GUI_SetFont(GUI_FONT_32B_1);
  GUI_DispStringHCenterAt(buffer,LCD_GetXSize()/2,LCD_GetYSize()/2 - GUI_GetFontSizeY()/2);
}

/******************************************************************************************
* NTPTimeThread
* This thread calls the NTP Timeserver to get the UTC time
* It then updates the time in the RTC
* And it updates the display by adding an event to the display queue
********************************************************************************************/
void NTPTimeThread()
{
    NTPClient ntpclient(wifi);

    while(1)
    {
        if(wifi-&gt;get_connection_status() == NSAPI_STATUS_GLOBAL_UP)
        {
            time_t timestamp = ntpclient.get_timestamp();
            if (timestamp &lt; 0) {
                // probably need to do something different here
            } 
            else 
            {
                set_time(timestamp);
                displayQueue-&gt;call(updateDisplayNTPCount);
            }
        }
        wait(60.0*5); // Goto the NTP server every 5 minutes
    }
}

/******************************************************************************************
*
* Main &amp; WiFi Thread
*
********************************************************************************************/

// wifiStatusCallback
// Changes the display when the wifi status is changed
void wifiStatusCallback(nsapi_event_t status, intptr_t param)
{
    const int buffSize=40;
    char *statusText;
    statusText = (char *)malloc(buffSize);

    switch(param) {
        case NSAPI_STATUS_LOCAL_UP:
            snprintf(statusText,buffSize,"WiFi IP = %s",wifi-&gt;get_ip_address());
            break;
        case NSAPI_STATUS_GLOBAL_UP:
            snprintf(statusText,buffSize,"WiFi IP = %s",wifi-&gt;get_ip_address());
            break;
        case NSAPI_STATUS_DISCONNECTED:
            WiFiSemaphore.release();
            snprintf(statusText,buffSize,"WiFi Disconnected");
            break;
        case NSAPI_STATUS_CONNECTING:
            snprintf(statusText,buffSize,"WiFi Connecting");
            break;
        default:
            snprintf(statusText,buffSize,"Not Supported");
            break;
    }
    displayQueue-&gt;call(updateDisplayWiFiStatus,statusText);
}


int main()
{
    int wifiConnectionAttempts;
    int ret;

    GUI_Init();
    displayQueue = mbed_event_queue();
    displayQueue-&gt;call_every(1000, &amp;updateDisplayTime);

    wifi = WiFiInterface::get_default_instance();
    wifi-&gt;attach(&amp;wifiStatusCallback);

   while(1)
   {
       wifiConnectionAttempts = 1;
        do {

            ret = wifi-&gt;connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
            displayQueue-&gt;call(updateDisplayWiFiConnectAttempts,wifiConnectionAttempts);

            if (ret != 0) {
                wifiConnectionAttempts += 1;
                wait(2.0); // If for some reason it doesnt work wait 2s and try again
            }
        } while(ret !=0);

        // If the NTPThread is not running... then start it up
        if(netTimeThreadHandle.get_state() == Thread::Deleted)
            netTimeThreadHandle.start(NTPTimeThread);
 
        WiFiSemaphore.acquire();
   }
}
</pre>
<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/mbed-os-cy8ckit_062s2_43012-segger-emwin-ntp/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
