Code Monkey home page Code Monkey logo

azure-sphere-azure-rtos-threadx-real-time-ultrasonic-rover-1's Introduction

Secure IoT with Azure RTOS ThreadX, Azure Sphere, and Azure IoT

Better together, learn how to build a secure IoT solution with Azure RTOS ThreadX and Azure Sphere.


What you will learn

You will learn how to integrate a Real-time Azure RTOS ThreadX application responsible for running a timing-sensitive ultrasonic distance sensor with the security and cloud connectivity of Azure Sphere.


#JulyOT

This is part of the #JulyOT IoT Tech Community series, a collection of blog posts, hands-on-labs, and videos designed to demonstrate and teach developers how to build projects with Azure Internet of Things (IoT) services. Please also follow #JulyOT on Twitter.


Source code and learning resources

Source code: Azure Sphere seeing eyed rover Real-time Azure RTOS ThreadX sensors and Azure IoT.

Learning resources: Azure Sphere Developer Learning Path.


Learn more about Azure Sphere

Azure Sphere is a comprehensive IoT security solution โ€“ including hardware, OS, and cloud components โ€“ to actively protect your devices, your business, and your customers.

Azure Sphere is made up of three interrelated components:

  1. Azure Sphere-certified MCUs
  2. Azure Sphere OS
  3. Azure Sphere Security Service

Azure Sphere end-to-end


Azure Sphere Architecture

The Azure Sphere is built on the Mediatec MT3620. This crossover MCU consists of 5 cores. There is a dedicated communications core, a dedicated Security Subsystem core, and three user application cores.

The three applications cores are as follows:

  • 1 x ARM Cortex A7 core running Embedded Linux (built with Yokto), exposing a set of POSIX APIs. Developers can build and deploy a High-level application to this core. This core is also responsible for the TrustZone Security Monitor, threat detection reporting, and OS and Application life cycle management.
  • 2 x ARM Cortex M4Fs. Developers can build and deploy Real-time applications to these cores. Real-time applications can be built against the bare metal or built using Real-time frameworks such as Azure RTOS ThreadX and Azure RTOS.

With Visual Studio (free community edition or better) or Visual Studio Code, you can develop and debug applications running on all three cores. For example, you can simultaneously debug an app running on the A7 core and a M4 core Azure RTOS ThreadX app.

Azure Sphere architecture


Application architecture

The application running on the Azure Sphere consists of two parts.

Real-time Azure RTOS ThreadX Application

  • The Real-time Azure RTOS ThreadX application running on one of the M4 cores that is responsible for running the timing-sensitive HC-SR04 ultrasonic distance sensor.
  • Distance is measured every 20 milliseconds so the rover can decide the best route.
  • The sensor requires precise microsecond timing to trigger the distance measurement process, so it is a perfect candidate for running on the Real-time core as a Azure RTOS ThreadX thread.
  • Every 5 seconds a Azure RTOS ThreadX thread sends distance telemetry to the Azure Sphere A7 High-level application.

Azure IoT High-level Application

  • The application running on the Azure Sphere A7 High-level application core is responsible for less timing-sensitive tasks such as establishing WiFi/Network connectivity, negotiating security and connecting with Azure IoT Central, updating the device twin and send telemetry messages.

Extending

  • I am thinking about extending this solution with a local TinyML module for smarter navigation.

Parts list


Azure IoT Central

Azure IoT Central provides an easy way to connect, monitor, and manage your Internet of Things (IoT) assets at scale.

I created a free trial of Azure IoT Central and in no time I had the rover distance sensor charted and available for deeper analysis. By the way, you can continue to connect two devices for free to IoT Central after the trial period expires.

Extend and integrate Azure IoT Central applications with other cloud services

Azure IoT Central is also extensible using rules and workflows. For more information, review Use workflows to integrate your Azure IoT Central application with other cloud services


How to build the solution

  1. Set up your Azure Sphere development environment.
  2. Review Integrate Azure RTOS ThreadX Real-time room sensors with Azure IoT.
  3. Learn how to connect and Azure Sphere to Azure IoT Central or Azure IoT Hub.
  4. The IoT Central Device Template Capabilities Model JSON file for this solution is included in the iot_central directory of this repo.

How to install the real time tool chain on Linux

  1. Download the GNU Arm Embedded Toolchain

  2. Install the downloaded package. I install in the /opt directory.

    sudo tar -xjvf gcc-arm-none-eabi-9-2020-q2-update-x86_64-linux.tar.bz2 -C /opt
  3. Update your path. Open ~/.bashrc and at the end add.

    export PATH=$PATH:/opt/gcc-arm-none-eabi-9-2020-q2-update/bin

How to update to the latest ThreadX bits

How to determine current ThreadX version

  1. Open tx_api.h.

    /**************************************************************************/
    /*                                                                        */
    /*  APPLICATION INTERFACE DEFINITION                       RELEASE        */
    /*                                                                        */
    /*    tx_api.h                                            PORTABLE C      */
    /*                                                           6.0.1        */

Download latest bits

Clone the ThreadX GitHub repo

git clone https://github.com/azure-rtos/threadx.git
  1. Update the Cortex M4 bits
  2. Update the ThreadX common bits

Customise for Azure Sphere

  1. Open tx_initialize_low_level.S

  2. Change SYSTEM_CLOCK = 200000000

  3. Comment out @ .global RAM_segment_used_end and the related code, as there is no such symbol in the .ld file. It's likely these areas are already commented out.

    @    .global     __RAM_segment_used_end__

    and

    @
    @    /* Set base of available memory to end of non-initialised RAM area.  */
    @     
    @    LDR     r0, =_tx_initialize_unused_memory       @ Build address of unused memory pointer
    @    LDR     r1, =__RAM_segment_used_end__           @ Build first free address   
    @    ADD     r1, r1, #4                              @ 
    @    STR     r1, [r0]                                @ Setup first unused memory pointer
    @

How to update to the latest MediaTec bits

MediaTek MT3620 M4 Driver & Real-Time Application Sample Code

MediaTec regularly update the MT3620 M4 drivers and samples.

Enabling the System Tick Handler

Open the MT3620_M4_BSP/scr/nvic.c file

Search for

void SystmTick_Handler(void)
{
	sys_tick_in_ms++;
	#ifdef OSAI_FREERTOS
	extern void SysTick_Handler(void);
	SysTick_Handler();
	#endif
}

Comment out the ifdef/endif directives to enable the system tick handler for Azure ThreadX.

void SystmTick_Handler(void)
{
	sys_tick_in_ms++;
	//#ifdef OSAI_FREERTOS
	extern void SysTick_Handler(void);
	SysTick_Handler();
	//#endif
}

Tips and tricks

Milliseconds to ticks

/* Convert from millisecond to ThreadX Tick value. */
#define MS_TO_TICK(ms)  ((ms) * (TX_TIMER_TICKS_PER_SECOND) / 1000)

Ticks per millisecond

1 tick = 10ms. It is configurable.


Have fun and stay safe and be sure to follow us on #JulyOT.

azure-sphere-azure-rtos-threadx-real-time-ultrasonic-rover-1's People

Contributors

gloveboxes avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.