R-Car/AWS IoT Greengrass

Introduction
This page describes how to setup the Ubuntu environment to use the AWS IoT Greengrass with R-Car and run it.

This page contains information abot running AWS IoT Greengrass on Ubuntu environment on:
 * Renesas R-Car S4 Reference Board/Spider
 * Renesas R-Car-S4
 * AWS IoT Greengrass

Start Ubuntu on R-car S4
In subsequent chapters, the tags will be included in the title as follows.
 * Work on the target board (R-Car S4) is described as .
 * Work on the host PC is described as .
 * 1)  Install software.
 * 2)  Make directory.
 * 3)  Change directory.
 * 4)  Copy make-rootfs.sh.
 * 5)  Give authority.
 * 6)  Execute.
 * 7)  Setup TFTP server and NFS server.
 * For more details, please refer to section "Setup TFTP server" and "Setup nfs-kernel-server" in "R-Car S4_StartupGuide_x_x_x.pdf"
 * 1)  Set environment value and boot NFS by U-Boot.
 * 2)  Copy Ubuntu-20.04.5-rootfs-image-rcar-spider.ext4 from current directory to /work/export/.
 * 3)  Flash Ubuntu-20.04.5-rootfs-image-rcar-spider.ext4.
 * 4)  Synchronize.
 * 5)  Reboot S4.
 * Power off and Power on SW1.
 * 1)  Boot eMMC.
 * 1)  Copy Ubuntu-20.04.5-rootfs-image-rcar-spider.ext4 from current directory to /work/export/.
 * 2) <Terminal> Flash Ubuntu-20.04.5-rootfs-image-rcar-spider.ext4.
 * 3) <Terminal> Synchronize.
 * 4) <Terminal> Reboot S4.
 * Power off and Power on SW1.
 * 1) <Terminal> Boot eMMC.
 * 1) <Terminal> Reboot S4.
 * Power off and Power on SW1.
 * 1) <Terminal> Boot eMMC.
 * 1) <Terminal> Boot eMMC.
 * 1) <Terminal> Boot eMMC.

Naming Conventions
In this document, tags will be included in the title as follows: The used is Asia Pacific(Tokyo) “ap-northeast-1". This can be replaced with your region as appropriate.
 * Work on the target board (R-Car S4) is described as <Terminal>.
 * Work on the AWS Cloud is described as <Web>.
 * Work on the host PC is described as <PC>.

Revision History (Version, Date, Description of change)
0.1	26-Dec-2022	Initial document version

Overview
This solution provides a complete development environment with hardware and software for automotive gateway applications to support the new E/E architecture.
 * Reduces the board size and BoM costs through MCU core integration into R-Car SoC, and use a single board to control both MCU domain and application SoC domain, which previously required separately.
 * The reference board consists of a CPU board with core SoC, power management IC (PMIC) and memory, and an interface board, enabling support for a variety of networks.
 * Supports 16 channels of CAN FD (can be used as 16 channels of LIN and 8 channels of SENT by multi-function), 2 channels of FlexRay, 2 channels of PCIe V4.0 x2 lanes, and 3 channels of 5G-USXGMII for Ethernet.
 * The core system is realized by installing the R-Car S4, LPDDR4x-3200 memory and HyperFlash™ memory on the CPU board, which contributes to shortening the time to market by simplifying the design.
 * The combination of pre-regulators and PMICs developed for R-Car S4 can provide various supply voltages to meet functional safety requirements up to ASIL D in accordance with ISO 26262.

About AWS IoT Greengrass
To learn more about AWS IoT Greengrass, see how it works and what's new.

DataSheet
Refer to https://elinux.org/R-Car/Boards/Spider.

Standard Kit Contents
https://www.renesas.com/us/en/products/automotive-products/automotive-system-chips-socs/rtp8a779f0askb0sp2s-r-car-s4-reference-boardspider

User Provided items
Host PC(Ubuntu 20.04 or 22.04)

3rd Party purchasable items
None

Additional Hardware References
https://www.renesas.com/us/en/products/automotive-products/automotive-system-chips-socs/rtp8a779f0askb0sp2s-r-car-s4-reference-boardspider

<Terminal>Set up your hardware
To set up your board, follow instructions in the section Quick Start How To at https://elinux.org/R-Car/Boards/Spider. This document also has annotated diagrams of the board showing key components.

<Web>Setup your AWS account and Permissions
Refer to the online AWS documentation at Set up your AWS Account. Follow the steps outlined in the sections below to create your account and a user and get started:
 * Sign up for an AWS account
 * Create a user and grant permissions
 * Open the AWS IoT console

Pay special attention to the Notes.

<Terminal>Install Java
Verify the Java version

<Terminal>Install AWS IoT Greengrass
For additional information, refer to the AWS documentation on Automatic resource provisioning.

Download the AWS IoT Greengrass Core software
You can download the greengrass core software as follows:

Install the AWS IoT Greengrass Core software
Unzip the AWS IoT Greengrass Core software to a folder on your device. If required, replace GreengrassInstaller with the folder that you want to use

<Terminal>Verify the version of the AWS IoT Greengrass Core software:

You will see the Greengrass version displayed - similar to: AWS Greengrass v2.7.0

<Terminal>Provide your credentials
Run the following commands to provide the credentials to the AWS IoT Greengrass Core software.

<Terminal>Run the installer
Run the installer as shown below. Modify the values as per your region, install directory and thing name.

Use the --provision true option to have the installer set up the "thing" and required policies for you. If you prefer to configure Greengrass manually, see the online guide. If all goes well, you will see the following output on the device console: The local development tools (specified by the --deploy-dev-tools option) take some time to deploy.

Wait for a while for the operation to complete, and then run the following commands.

<Terminal> Confirm version and component list. <Terminal> Confirm greengrass.service status. <Terminal> If greengrass.service doesn’t start, you need to start it.

Create a Hello World component
In Greengrass v2, components can be created on the edge device and uploaded to the cloud, or vice versa.

Create the component on your edge device

 * 1) Attach policy.
 * 2) <Web> Select "IAM" from AWS services.
 * 3) <Web> Select "Policies" from the side menu.
 * 4) <Web> Select "Create Policy".
 * 5) <Web> Select "IoT" for Service, "Publish" for Actions, and "All Resources" for Resources.
 * 6) <Web> Click "Next: Tags" and "Next: Review", then enter any "Name" in the Review policy, and click "Create Policy" button.
 * In this procedure, the name shall be "R-CarS4Device-IoT-Publish".
 * NOTE The examples in this document are intended only for dev environments. All devices in your production fleet must have credentials with privileges that authorize only intended actions on specific resources. The specific permission policies can vary for your use case. Identify the permission policies that best meet your business and security requirements.  For more information, refer to Example policies and Security Best practices.
 * 1) <Web> Return to the IAM screen and select "Roles" from the side menu.
 * 2) <Web> Enter "GreengrassV2TokenExchangeRole" in the search and select the role with the same name.
 * 3) <Web> Click "Attach Policies", search for the policy you just created(Account name), and click "Attach Policy".
 * 4) Prepare the execution environment for Lambda functions.
 * Build an environment to run the sample program on a terminal.
 * <Terminal> Execute the following commands.
 * 1) The endpoints can be found below.
 * 2) <Web> Navigate to the AWS IoT console.
 * 3) <Web> Select "Settings" from the side menu.
 * 4) <Web> The endpoint can be found under Device Data endpoint.  It should look similar to this: xxxxxxxxxxxxxxx-ats.iot.ap-northeast-1.amazonaws.com, take notes.
 * 5) Create a Lambda Function.
 * Create a Lambda function to deploy to the terminal.
 * <PC> Run the following command to create a template for a Lambda function.
 * 1) <PC> Copy and paste the following code into the lambda_function.py that you created.
 * Rewrite the endpoint_url to xxxxxxxxxxxxxxx-ats.iot.ap-northeast-1.amazonaws.com that you wrote down.
 * 1) Zip the packages.
 * <PC> Zip the lambda_function.py and boto3 packages you have created.
 * The zip file is copied to a PC that has access to the AWS cloud for uploading to the cloud.
 * 1) Zip the packages.
 * <PC> Zip the lambda_function.py and boto3 packages you have created.
 * The zip file is copied to a PC that has access to the AWS cloud for uploading to the cloud.
 * The zip file is copied to a PC that has access to the AWS cloud for uploading to the cloud.


 * 1) Register the Lambda function.
 * Register the created Lambda function to the AWS cloud.


 * 1) <Web> Select "Lambda" from AWS services.
 * 2) <Web> Select "Functions" from the side menu.
 * 3) <Web> Select "Create function".
 * 4) <Web> Check the "Author from scratch" box.
 * 5) <Web> Give an arbitrary name to the "Function name". In this procedure, "DummyTemperatureSensor" is used.
 * 6) <Web> Select "Python 3.8" as the Runtime.
 * 7) <Web> Select "arm64" as the Architecture.
 * 8) <Web> Other items keep the default values and click "Create Function".
 * 9) <Web> When the screen changes, Move Configuration tab and Timeout change 1 min 0 sec.
 * 10) <Web> Move Code tab, select "Upload from" → ".zip file", and upload → click "Save".
 * 11) <Web> Select "Actions" → "Publish new version" in the upper right corner and click “Publish”.

Deploy the Lambda function
Deploy the registered Lambda function to the terminal.
 * 1) <Web> Select "IoT Core" from AWS services.
 * 2) <Web> Select "Greengrass devices" → "Components" from the side menu.
 * 3) <Web> Click "Create component".
 * 4) <Web> Check the "Import Lambda function" box and select the function that you created in "Lambda function", enter "topic/sensor/get" in the Topic field of the Event sources, select "AWS IoT Core MQTT" as the Type of the Event sources, enter "60" in the Timeout field of the Event sources.
 * 5) <Web> Set the Memory size of Container parameters to "64 MB".
 * 6) <Web> Other items keep the default values and click "Create component".
 * 7) <Web> When the screen changes, click "Deploy" in the upper right corner.
 * 8) <Web> Select “Add create”, and click your deployment and "Next".
 * 9) <Web> Other items keep the default values and click "Next" several times, and click "Deploy" at the last confirmation screen.

Operation check
Confirm that the function you created will be deployed on the terminal side. Also, check the log on the terminal side to see if messages are being received by AWS IoT → terminal. Check the operation of R-Car SK. Confirm that the Lambda function has been deployed. <Terminal> Execute the following command to confirm. When you execute the command the following log will be output, and you can see that the deployed function (DummyTemperatureSensor) is displayed in the list.
 * 1) Check the log.
 * <Terminal> Run the following command to check the log.
 * Verify that the following log is output.
 * Verify that the following log is output.

Verify that messages are received in the AWS cloud

 * 1) <Web> Select "IoT Core" from AWS services.
 * 2) <Web> Select "MQTT Test Client" from the side menu.
 * 3) <Web> Enter "topic/sensor/temperature" in the Topic filter and click "Subscribe".
 * 4) <Web> Enter "topic/sensor/get" in the Topic name of Publish to a topic and click "Publish.
 * Note The default message payload will be garbled on the terminal side, so change it to any alphanumeric text if necessary.
 * 1) <Terminal> The message received will be displayed in the log as follows.
 * 2) <Web> A message is received in the subscription.
 * 1) <Web> A message is received in the subscription.

How to connect serial console
Use a microUSB cable to connect the PC to R-Car S4 Evaluation board(Spider) board. CN21 must be used on Spider side. It is routed to HSCIF0 in the R-Car S4 via a FT232 interface converter chip. On Linux, FT232 driver is included with kernel versions >=2.6.12. Windows driver and sources can be found on FTDI Chip website. Serial settings are below

1843200 8N1. Any standard terminal emulator program can be used. For example, on Linux you can use picocom replace DEVICE with the proper tty device name, for example /dev/ttyUSB0. Running dmesg | tail can help locating proper device. After the successful connection, picocom should display: Use Ctrl+A, Ctrl+Q to exit picocom.

Power On
Use DC 12 V power supply. There are sequences for turning on and off the power supply to the Spider board set. For the Spider board set, be sure to obey the notes below. Be sure to confirm that the Power Switch (SW11) is off before plugging the AC adapter into the power source. It is prohibited to plug the AC adapter into a power source while the Power Switch (SW11) is on. Be sure to turn off the Power Switch (SW11) before unplugging the AC adapter from the power source. It is prohibited to unplug the AC adapter from the power source while the Power Switch (SW11) is on.
 * When power is turned on
 * When power is shut off

Boot up Ubuntu
After setting up the firmware using the standard procedure below, Ubuntu will boot on Power On. https://elinux.org/R-Car/AWS_IoT_Greengrass#Start_Ubuntu_on_R-car_S4 SW11 to switch the board on. Please refer to Section 12.1Boot up Ubuntu log.

Login
The following default accounts are set

This user can become the root user with sudo, so please change the password setting as a security measure. And you can install the software development and debug tools with "apt install" command.

Check LAN cable
If you are not able to connect to the Internet, please check the following points.

Is the LAN IP address, etc. set correctly?
Is the IP address correct? Run "ifconfig" and Check inet of using adapter. Is the router address correct? Run "ip route" and check. Can you resolve a DNS request? Check "nslookup www.renesas.com". If you can't resolve a DNS request, check "cat /etc/resolv.conf". If nameserver doesn’t write, you add nameserver.

Check the serial console
If there is no output on the USB serial console, please check the following points.

Is it plugged into the correct location?
Use a USB microB cable to connect to CN21 indicated by the red frame.

Are the settings correct?
Serial settings are below

Is the port correct?
Please confirm the USB-Serial device name (ex. /dev/ttyUSBxx). Running dmesg | tail can help locating proper device.

Is the power supply turned on properly?
Are the LEDs illuminated? If DC12V was supplied by AC Adapter to this board, LED 1 and 2 are illuminated. When the power switch(SW11) is turned on, LEDs 13 through 16 light up. From LED 13 to 16 are located in the upper left corner of the CPU board.

Does U-Boot start up?
If U-Boot start up, you can rewrite Ubuntu root filesystem to eMMC. R-Car/AWS IoT Greengrass - eLinux.org.

If U-Boot prompt doesn’t display, you need to check the DIP-SW or to rewrite the firmware. Please refer to section "How to flash ICUMX Loader and U-Boot" in R-Car S4_StartupGuide. This PDF file is provided with a Spider board.

Boot up Ubuntu log
Please refer to https://elinux.org/R-Car/AWS_IoT_Greengrass#Start_Ubuntu_on_R-car_S4 for details.

NFS boot (first boot only)
At first, the spider board doesn't have a root file system. You can follow the steps below to write Root file system to eMMC. After power on S4 board, when "Hit any key to stop autoboot" in U-Boot boot log is output, hit any key to stop autoboot. First, set environmental variables and NFS boot. Second, login "root", and run "dd if=./Ubuntu-20.04.5-rootfs-image-rcar-spider.ext4 of=/dev/mmcblk0 bs=1M status=progress". After dd command finish, run "sync" and "poweroff".

eMMC boot ( usual, without NFS)
After Root file system is written to eMMC, it will boot without an NFS server. After power on S4 board, when "Hit any key to stop autoboot" in U-Boot boot log is output, hit any key to stop autoboot. First, set environmental variables and eMMC boot. Second, login "rcar".