S3A7 with Smart Chef Demo and Cloud-Driven GPIO – Quick Start Guide

Updated June 28th, 2017

Overview

This tutorial will show how to connect GPIO signals on the S3A7 IoT Enabler Kit to the Renesas IoT Sandbox, by modifying the Smart Chef demo and using the M1 Synergy Cloud Driver. The Cloud Driver allows applications to specify I/O details once, then drive the I/O through python code in the Sandbox.

The following methods will be shown:

  • Interrupt GPIO input
  • Cloud-driven GPIO input
  • Cloud-driven GPIO output

GPIO signals on PMOD D (J10) will be used, but the same concepts apply in general. The M1 Synergy Cloud Driver GPIO features are currently only supported on the S3A7 IoT Enabler Kit for the following pins:

  • PMOD D (J10) pin 1
  • PMOD D (J10) pin 7 – interrupt pin
  • PMOD D (J10) pin 8
  • PMOD D (J10) pin 9
  • PMOD D (J10) pin 10

Other PMOD D pins and other PMOD ports are in-use already by the application and peripherals. When an interrupt is received on the interrupt input pin, an event will be sent to the Renesas IoT Sandbox. Other GPIO inputs and GPIO outputs can be controlled by commands sent from the Renesas IoT Sandbox.

Here is what you need to get started:
  • S3A7 IoT Fast Prototyping Kit
  • USB Cable (to power the board)
  • Renesas e2 studio software Version: 5.4.0.018
  • SmartChef zip file
  • To build a simple LED toggling circuit, you will need a breadboard, switch, LED light, resistor and a few wires
PREREQUISITES:

 

1. Import SmartChef Project

Download the SmartChef project zip file.

Now go to e2 studio. Click File -> Import…

Choose “Rename & Import Existing C/C++ Projects into Workspace”, then click “Next”:

Enter a project name. In the “Import from:” section  choose “Select archive file” and navigate to the project zip fie. Click on the “SmartChef” project that appears in the list, then click finish.

 

2. Open Synergy Configuration (configuration.xml)

Double click on the file “configuration.xml” within your project. Switch to ‘Synergy Configuration’ perspective:

synconfig

 

Alternatively, go to ‘Open Perspective’ and select ‘Synergy Configuration’ from there:

openper

 

3. Add External IRQ

Switch to Sensor Thread:

sensorthread

 

Add External IRQ Driver by clicking on the button with paper and a plus sign beneath “Generate Project Content”:

externalirq

4. Set External IRQ Properties

These settings need to be changed in the box in the bottom right-hand corner.

  • Name -> g_external_irq2
  • Channel -> 9
  • Trigger -> Falling
  • Digital Filtering -> Enabled
  • Callback -> gpio_irq_callback
  • Interrupt Priority -> 10

5. Configure Pin P304 (IRQ9)

In Pins, configure port P304:

  • Pull up -> input pull-up

6. Generate Project Content

Save, then click Generate Project Content:

projcontent

 

 

7. Switch to C/C++ Perspective

cc

 

 

8. Edit sensor_thread.c

Navigate to the file sensor_thread_entry.c in the project explorer under ‘src’. Add the highlighted sections in GREEN to the current content.

...

void m1_message_callback(int type, char * topic, char * payload, int length);
void sensor_thread_entry(void);
void gpio_irq_callback(external_irq_callback_args_t * p_args);

...
    m1_initialize_comms(&g_sf_comms0);

   // open PMOD A as I2C (known devices)
#ifdef I2C_OPEN
    err = g_sf_i2c_device0.p_api->open(g_sf_i2c_device0.p_ctrl, g_sf_i2c_device0.p_cfg);
    APP_ERR_TRAP(err);
    m1_initialize_i2c(&g_sf_i2c_device0);
#ifndef I2C_MULTI_THREAD
    err = g_sf_i2c_device1.p_api->open(g_sf_i2c_device1.p_ctrl, g_sf_i2c_device1.p_cfg);
    APP_ERR_TRAP(err);
    m1_initialize_i2c(&g_sf_i2c_device1);
    err = g_sf_i2c_device2.p_api->open(g_sf_i2c_device2.p_ctrl, g_sf_i2c_device2.p_cfg);
    APP_ERR_TRAP(err);
    m1_initialize_i2c(&g_sf_i2c_device2);
    err = g_sf_i2c_device3.p_api->open(g_sf_i2c_device3.p_ctrl, g_sf_i2c_device3.p_cfg);
    APP_ERR_TRAP(err);
    m1_initialize_i2c(&g_sf_i2c_device3);
#endif
#endif
    m1_initialize_ioport(&g_ioport);
    err = g_external_irq2.p_api->open(g_external_irq2.p_ctrl, g_external_irq2.p_cfg);
    APP_ERR_TRAP(err);

At the end of the file, add:

void gpio_irq_callback(external_irq_callback_args_t * p_args) {
    char buf[20];
    sprintf(buf, "{\"interrupt\":%lu}", p_args->channel);
    m1_publish_event(buf, NULL);
}

9. Build

build

CHECKPOINT:

You have successfully updated the SmartChefDemo, compiled and built the project.

Now, you are ready to flash the board with your new build, attach the demo circuit, and connect to Renesas IoT Sandbox and visualize your data.

 

10. Build the Demo Circuit

Now you are ready to create a simple circuit to showcase LED toggling as well as button switching the LED light on and off.

For convenience, we use a breadboard to build the following simple circuit. To power the circuit board connected to S3-A7 kit, you can use the the micro USB and plug it to your computer USB port.

We used:

  • S3-A7 board with attached PMOD D connector
  • Switch
  • LED light
  • 100 Ω resistor
  • wires

The following is the circuit schematic.

  • X1 -> PMOD D connector on S3-A7 board
  • P1 -> PMOD D (J10) pin 7
  • P2 -> PMOD D (J10) pin 1
  • S1 -> Switch
  • R2 – 100 Ω resistor
  • L1 – LED light
  • GND -> PMOD D (J10) pin 5

circuit

 

Now, you are ready to flash the board with your new build, attach the demo circuit, and connect to Renesas IoT Sandbox and visualize your data.

NOTE: Physical connection of wires between PMOD D and Breadboard circuit is shown below:

Pin 1 = P2 connection to resistor (in above schematic) = YELLOW wire

Pin 5 = GND connection (in above schematic) = BROWN wire

Pin 7 = P1 connection to switch (in above schematic) = ORANGE wire

 

 

11. Program the Board, Connect Circuit, and Test Button

At this point, make sure your board has power by plugging Micro USB to J1 and the board is provisioned.  Now, you need to update the board’s image with the previously generated build.

To flash the board, connect the J-Link debugger to your Windows or Mac computer and connect the debugger to the S3A7 Fast Prototyping board.  Right click on your project, then select: “Debug As -> Renesas GDB Hardware Debugging”. Select JLink, then select S3a7 as the board. It may be listed with a prefix.

debugas

 

If any errors occur, try to resolve them and try to reprogram the board again. Click on the run button to progress through the initial breakpoints, keeping the j-link plugged.

Note: You can find the new image SREC file in the Debug folder of your project.

Test the button on your circuit. This will trigger an interrupt on IRQ9 and will cause the board to send an event to the cloud with tag “interrupt”.

If you haven’t done so, open your Renesas IoT Sandbox account.  You will need to select the new tag and build a workflow to toggle the LED light.

12. Select raw Tag interrupt

Config -> Data Stream -> click on Edit raw Tags

Activate interrupt tag, then click Save Data Stream

 

 

13. Build the LED Toggling Workflow

Create LED Toggling workflow as depicted below:

 

In Custom tag, set user to ‘device’; then click Save and Activate

In Base Python, replace the original code with following code; then click Save and Activate:

Base Python Node:

'''

Last Updated: Dec 13, 2016

Author: Medium One

'''

import MQTT

import Store

import Analytics

import DateConversion

import Filter

import datetime

def read_pin(pin, tag):

    send_mqtt_msg(u'0;4;{};{};'.format(pin, tag));

def write_pin(pin, val):

    send_mqtt_msg(u'0;3;{};{};'.format(pin, val));

def send_mqtt_msg(msg):

    mqtt_msg = msg

    if not isinstance(msg, basestring):

        mqtt_msg = ''.join(map(unichr, msg))

    MQTT.publish_event_to_client('s3a7', mqtt_msg, 'latin1')

# debounce

in1 = IONode.get_input('in1')

if IONode.is_trigger('in1'):

    last_2_interrupts = Analytics.events(stream_name='raw', filters=Filter.numeric_tag('raw.interrupt'), limit=2, sort=('observed_at', 'DESC'))

    if len(last_2_interrupts) > 1:

        if DateConversion.to_py_datetime(in1['observed_at']) - DateConversion.to_py_datetime(last_2_interrupts[1]['observed_at']) < datetime.timedelta(seconds=1):

            escape()

try:

    prev_state = int(Store.get('prev_state'))

except (ValueError, TypeError):

    prev_state = 0

# toggle LED state

next_state = 0 if prev_state else 1

write_pin(0, next_state)

Store.set_data('prev_state', str(next_state))

The following values of the pin parameter in read_pin() and write_pin() map to the physical pins as follows:

    • 0 -> PMOD D (J10) physical pin 1
    • 4 -> PMOD D (J10) physical pin 7 – interrupt pin
    • 5 -> PMOD D (J10) physical pin 8
    • 6 -> PMOD D (J10) physical pin 9
    • 7 -> PMOD D (J10) physical pin 10

14. Monitoring Switch and LED Light Toggling

In Dashboard, check the Real Log file to confirm that the board is connected.

Once you have connected the breadboard circuitry, you should see the LED light toggle (switching on and off) every 1 minute.

Additionally, if you press the switch, the LED light will toggle from its current position (on -> off, off -> on).

NOTE:  Since many switches vary in style and characteristics, be sure to consider adding debouncing circuitry if needed.

Now you can connect your own circuit to S3A7 board and to the cloud.

What’s Next?

Congrats, You have just completed setting up the Smart Chef IoT Kit! The rest is up to your imagination!