Bluetooth GATT Protocol for Bluetooth Low Energy (BLE 4.0)

GATT protocol

GATT is a protocol style of exchange data over the wireless Bluetooth connection that is introduced for BLE 4.0.

The Bluetooth connection can consist of many services. A service is like data channel or view as a data object passing to and from the Bluetooth device. Inside each service can consist of many data attributes which is also known as the characteristic. Characteristic is simply the data that is associated with a standard defined label.

GATT protocol

  • Service 1
    • Characteristic 1A
    • Characteristic 1B
    • Characteristic 1C
  • Service 2
    • Characteristic 2A
  • Service 3
    • Characteristic 3A
    • Characteristic 3B

Reference on GATT:
https://www.oreilly.com/library/view/getting-started-with/9781491900550/ch04.html

Topic:
– Handler (16 bit ID as and address for the attribute) <- is it address for the Service?
– Permission
– UUID identify the data attribute type in the data block.

Hands-on GATT protocol

One useful tool in learning the new GATT Protocol designed for Bluetooth Low Energy (BLE4.0 and above) is using the nRF Connect’s Bluetooth Low Energy apps from Nordic. You can install the nRF Connect software to have access to this app.

The following example uses nRF52840 Dongle as a sniffer.

When in the BLE apps, select the dongle device to start.

On the right side, click on the button <Start scan> to scan for nearby Bluetooth devices.

Bluetooth devices is constantly sending our advertisement broadcast so that other Bluetooth client can detect it and be able to connect to it.

In the following example, a BLE device is detected by the scan process. The device name ending with “…..-172AFE04”. Select the device and click on the <Connect> button.

Device Name: …..-172AFE04
Address: D4:ED:17:2A:FE:04
Address type: RandomStatic
Services: Battery Service
Appearance: 00-00

The column on the left represents the nRF52840-Dongle which is now a Central Bluetooth device. The right column is displaying the connected Bluetooth Peripheral (server) device name “…..-172AFE04”. The central is a Bluetooth client device which can connect to one or more peripheral devices. Each peripheral (server) can only accept one connection from the central (client) Bluetooth device. Once the peripheral is connected, they will stop their advertisement broadcast. Other Bluetooth devices will not be able to see and connect to them.

In the connected peripheral Bluetooth device with name ending with “…..-172AFE04”, along the connection line, there is a lock icon. Hover your mouse to check up the connection information. The configuration icon inside the device box have option that allows you to disconnect the Bluetooth connection of the central device to the peripheral device.

Inside the peripheral device, you will notice the following section of data. These are the GATT Services available from this server peripheral Bluetooth device.

  • Generic Access
  • Generic Attribute
  • FFE0
  • FFE5
  • Legacy DFU

Generic Access

Type UUID: 0x1800

CharacteristicValue
Device Name…..-172AFE04
Appearance00 00
Peripheral Preferred Connection Parameters (optional)20 00 30 00 00 00 90 01
Peripheral Privacy Flag (optional)

Generic Attribute

Type UUID: 0x1801

CharacteristicValue
Service Changed (optional)

GATT Service: FFE0

Type UUID: 0xFFE0

Characteristic is FFE4.
is a notify service.

Characteristic User Description
64 65 76 69 63 2D 3E 70 68 6F 6E 65
which represent a string
devic->phone

Client Characteristic Configuration
00 00

GATT Service: FFE5

Type UUID: 0xFFE5

Characteristic is FFE9.
is a writeWoResp write service.

Characteristic User Description
70 68 6F 6E 65 2D 3E 64 65 76 69 63 65
which represent a string
phone->device

Legacy DFU

GATT Service in detail (xml)

An overview of the service “generic-access” data structure.

Max data size of an attribute is 512 bytes

Reference:
https://www.bluetooth.com/specifications/gatt/services/

Generic Access Service data structure

– InformativeText

– Characteristics

– Configurations

<- Back to Bluetooth Resources Page

AT Command for NB-IoT communication

Hardware used is nRF9160 modem.

Set terminal software to the following setting.

  • Baudrate: 115200bps
  • Data bits: 8 bits
  • Stop bit: 1 bit
  • Parity: None
  • No hardware handshaking

Note that ↵ means sending of char 0x0D 0x0A (<CR><LF>)

//Test if modem is connected
🡲 AT ↵
🡰 OK ↵

//Get the modem firmware version
🡲 AT+CGMR ↵
🡰 352656100032351 ↵

//Get IMEI number
🡲 AT+CGSN ↵
🡰 352656100032351 ↵

//Sim Activation
🡲 AT+CFUN=0 ↵     //turn  off cellular functionality
🡰 OK ↵
🡲 AT+CFUN=1 ↵     //turn  on cellular functionality
🡰 OK ↵

//PDP Context Configuration (configure how the data packet be accepted by the telecom)

<- Back to Bluetooth Resources Page

Nordic Bluetooth Resources Page

Nordic nRF9160 Certification

Certification resources referred by AVNET (Nordic Product Distributor)
https://www.nordicsemi.com/Products/Low-power-cellular-IoT/nRF9160-Certifications

Digital Locks Resources

Bluetooth (Technical Overview)

Bluetooth protocol operates at 2.4GHz, same as ZigBee and WiFi, working in the same unlicensed ISM frequency band.

Each network (Piconets) consist of a coordinating Master and many connecting Slaves.

Bluetooth Profile

Bluetooth profile is like the many various protocol of bluetooth communication. Some profile are for keyboards, some for storage, some for audio. Here are a list of commonly used profiles.

  • Serial Port Profile (SPP), or sometimes known as UART
  • Human Interface Device (HID)
  • Hands-Free Profile (HFP)
  • Headset Profile (HSP)
  • Advanced Audio Distribution Profile (A2DP)
  • A/V Remote Control Profile (AVRCP)
  • Generic Attribute Profile (GATT), custom profile communication using Attribute Protocol (ATT) between the master and slave.

GATT is typically used for proprietary project that uses custom communication through the use of exchanging attribute data (Attribute Protocol, ATT).

A temperature sensor device can be acting as a server providing a service to expose the temperature reading.

A mobile smart phone is this case is a client, sending commands, requests and accepts incoming notifications/indications from the server.

The ATT attributes is made up of 4 components.

  • Attribute Handle (the address of this attribute during the connection session)
    2 bytes address (0x0001-0xFFFF)
  • Attribute Type (UUID Universally Unique Identifier)
    2 or 16 bytes
  • Attribute Value
    Variable length data.
  • Attribute Permissions
Bluetooth Attribute Protocol

Bluetooth Versions

  • Bluetooth v1.2
  • Bluetooth v2.1 + EDR (enhanced data rate)
  • Bluetooth v3.0 + HS
  • Bluetooth v4.0 (BLE, Bluetooth Low Energy)

Reference:
https://learn.sparkfun.com/tutorials/bluetooth-basics/all

Reverse Engineering (Bluetooth)

Reference:
https://www.instructables.com/id/Reverse-Engineering-Smart-Bluetooth-Low-Energy-Dev/

<- Back to Bluetooth Resources Page

Nordic Chip Programming (using nRF Connect Programmer software)

Nordic Chip can be nRF52840, nRF52832, nRF9160

Methods of Programming Nordic Chips

There are 4 ways of loading your program onto Nordic chip.

  1. Debug header (for use of programming of the full image of the chip memory *.hex). This can be done using nRF52840-DK or nRF9160-DK board as a J-LINK programmer, or using a J-LINK programmer like the j-link Base, j-link BASE Compact or J-Link EDU Mini Programmer.
  2. USB Bootloader (USB Wired DFU, programming of the non-boot loader, application memory *.hex). This can be done using the onboard USB peripheral.
  3. Bluetooth Firmware Loader (Wireless DFU, programming of the non-boot loader, application memory *.bin). This can be done when your chip is pre-flash with bootloader firmware and SoftDevice firmware.
  4. USB Mass Storage (drag and drop *.hex file into the JLINK storage drive to automatically program the chip). This can be done through the nRF52840-DK or nRF9160-DK board.

Tools Required

  • An nRF52840-DK kit board to act as the programmer for the chip
  • nRF Connect Software

Extra programmer tool like the j-link Base, j-link BASE Compact or J-Link EDU Mini Programmer may come in handy when the tool or chip fails. Besides for nRF chip, this j-link programmer can also be used for other microcontroller chip listed on this page -> click here for supported devices J-Link.

Debug Header Method

This is the most fundamental method of loading in firmware into the Nordic nRF52840 chip.

Connect up the nRF52840-DK or nRF9160-DK kit. They are used as a programmer in this example. These boards contains a programmer chip that uses Jlink driver for connection as a programmer, to upload *.hex file into the nRF52840 or nRF9160 chips.

There is typically 3 *.hex module that you need to load into the chip’s memory. Namely,

  • Bootloader
  • Soft-Device
  • Your Application

Different chip has its own memory map where the *.hex file should be loaded to. Here is the memory map for nRF52832, nRF52840.

Memory Map of nRF52832 and nRF52840

Go to the software “nRF Connect” and open the “Programmer” program.

You will come to the screenshot as follows.

Selecting Programmer Board

There is a drop-down list on the top.
When you board is connected, there should be an option in the drop down list for you to choose from. Select the programmer shown in the list.

If there is no board listed even when you got your board connected to the computer. You may also see error message in red color at the log box below. Check out the last section to see if there is a solution to resolve your problem.

Load Bootloader *.hex file

Bootloader *.hex file is the part that allows you to use USB cable to directly load the program into the chip.

It enables the chip to be able to receive it firmware through wired USB.

Click on the “Add HEX file” on the right side. Choose the bootloader that you want to use.

On the memory map, you will see green (bootloader sectors in use) on the top and bottom of the memory map.

Load SoftDevice *.hex file

SoftDevice is a bluetooth stack codes that you can choose to load into the chip.

There are many version to choose from. You can download them from S132 SoftDevice.
https://www.nordicsemi.com/Software-and-tools/Software/S132

Click “Add HEX file” again to load SoftDevice *.hex file.

The SoftDevice *.hex file is loaded in the other memory of the chip. You can see from the purple color zone in the memory map shown on the right side box.

Load Application *.hex file

Lastly, this is the *.hex file for your custom application.

Click on “Add HEX file” again and select the *.hex file of your application.

Download Code into the Chip

Click on the button “Erase & write” to start down loading the firmware containing the 3 sector of the *.hex code into the chip at one go.

nRF52840, “Hello World” Example

Let’s use a simple project “Blinky” as our hello world example to help you kick start your chip programming experience.

Problem Encounter Case Study

Problem encounter from, nRF Connect -> Programmer

Problem “Error occured when get serial numbers”

Error encounter:
Error while probing devices: Error occured when get serial numbers. Errorcode: CouldNotCallFunction (0x9) Lowlevel error: INVALID_OPERATION (fffffffe)numbers.

This error occurred if JLINK driver is not installed on the system.

Problem “Unsupported device. The detected device could not be recognized as neither JLink device nor Nordic USB device.”

This error occurred when the JLink driver is not working.

(doesn’t work) May need to reinstall SEGGER J-Link driver. Install version 6.22g. Download the J-Link Software and Documentation Pack from

https://www.segger.com/downloads/jlink/#J-LinkSoftwareAndDocumentationPack

Solution 2, can consider reinstalling “nrfprog tool” as the previous example.

Problem: The nRF Programmer got hang when connecting to the nRF9160 chip. Log message halt at “Using nrfjprog to communicate with target”

Somehow during one of the attempt to program the nRF9160 chip on Thingy91, the programmer halt.

Upon retry to connect with the chip, the log always stops at “Using nrfjprog to communicate with target”.

It doesn’t happens to the nRF52840 chip on the Thingy91. Only the nRF9160 chip is affect.

I thought it was a hardware issue. Alan from Avnet managed to recover this hang sequence by using the following command (program via command prompt)

> nrfjprog -f UNKNOWN --eraseall

Once this command is issue, the block is clear and the chip can be program normally.

<- Back to Bluetooth Resources Page

How to load Arduino bootloader into ATMEL microcontroller ATMEGA328P

Install Mini-Core

Install Mini-Core following the instructions given on their githhub page:
https://github.com/MCUdude/MiniCore#how-to-install

This installation method requires Arduino IDE version 1.6.4 or greater.

  1. Open the Arduino IDE.
  2. Open the File > Preferences menu item.
    Arduino preference menu

  3. Enter the following URL in Additional Boards Manager URLs:
    https://mcudude.github.io/MiniCore/package_MCUdude_MiniCore_index.json
    Additional Boards Manager URLs

  4. Open the Tools > Board > Boards Manager… menu item.
    Arduino board selection

  5. Wait for the platform indexes to finish downloading.
    Scroll down until you see the MiniCore entry and click on it.
    Click Install.
    Boards Manager, MiniCore by MCUdude

  6. After installation is complete close the Boards Manager window.

Programmer preparation

Plug the USBasp device into your computer.

USBasp programmer


If you get an error stating the USBasp cannot be found,

use Zadig to download the device driver.

Go to : https://zadig.akeo.ie and download the software. Once downloaded

Launch the software and go to Options>List all devices

Zadig software for programmer driver

 

In the drop-down menu, select USBasp.
Then in the Driver selection click the small arrows until you find “libusbK (v3.0.7.0)”

Zadig driver selectioin

Then click “Replace Driver”

Burning the Bootloader onto the chip

Inside of Arduino IDE go into Tools>Programmer>USBasp

Arduino select programmer USBasp

Select the board ATmega328

Select board ATmega328

and select the variant 328/328A

Select IC chip variant

 

Connect the USBasp to the Arduino programming port (6 pins header)

Connecting programmer to Arduino ICSP programming header

Go to Tools>Burn Bootloader

Burn Arduino bootloader into ATMEL ATmega328 microcontroller

You should see this message in the console.

avrdude done. Thank you.

A quick way to check if the boot-loader was correctly burned is to try loading with an empty source code.

If the loading fails, try changing the port in Tools>Port. If the loading still not works, try redoing step 3. for the start.

 

Arduino programming via USB

 

You can program the board using a USB B mini, just like you would a normal Arduino Board.

For this you need to change the port if necessary: Tools>Port

Use the Check button to compile ans the arrow to load the code onto the board.

Select Arduino USB programming port.

Set up:

The pins of the USBasp must be connected to the corresponding of the Arduino ICSP connector.

Arduino programming header port

Basic circuit for Arduino and boot-loader programming.

 

 

 

Other references

Microcontroller Firmware Programming Upload with MPLAB X IDE

Introduction

This section provides fresh user a step-by-step instruction to upload your updated firmware (a program or machine codes) onto your circuit board using a Microchip microcontroller.

Microchip logo

Things That You Need To Prepare

  1. Your Microchip microcontroller circuit board
  2. Programmer Tool (PICkit)
  3. MPLAB X IDE (Integrated Development Environment)
  4. Firmware (a hex code file *.hex or project files, containing the program of your controller)

Once you are prepared, you can proceed step-by-step to upload your firmware to your microcontroller.

Microchip Circuit Board

Most microchip circuit board design by PIC-CONTROL comes with a programming port which allows us to upload the program onto the microcontroller chip onboard.

There are standard microchip programming port also known as ICSP, which you can connect to with your programmer tool.

Programming Connector Port on your circuit board product

The programming port is usually located near the edge of your circuit board. It is a row of 6 holes grouped up with a rectangular box (2.54mm pitch header pins).

You must know how to identify the pin no.1 on this port. Pin number 1 of the footprint is indicated by a triangle arrow marking. You can also identify this pin 1 from the bevel corner on the rectangular box.

The following are some photos of how the ICSP programming port looks like on your circuit board.

ICSP programming port on PCB board     ICSP programming port on PCB board     ICSP programming port on PCB board

smd ICSP programming port on PCB board (without holes) ICSP (a special version without any holes)

You are suppose to connect the programmer tool to these ICSP port, with alignment to the pin 1.

Details of the procedure will be provided in the later section below.

Programmer Tool

You will need to get yourself a programmer tool. A programmer tools helps you to connect your computer to your microcontroller chip. It is used for uploading of the microcontroller firmware (controller program) onto the microcontroller on your circuit board.

There are a number of programmer tools which allows you to upload your firmware. In this example, we are using the official PICkit 3 or PICkit 4 programming tool from Microchip.

Other commonly known programming tools available are ICD4 (In-Circuit Debugger), ICD3, PM3 and REAL ICE.

Please take note of the pin no.1  on the programmer tool. You must identify this pin and align it to the pin 1 of the ICSP port on your circuit board.

PICkit 3 Programming Tool

PICkit 3 programmer tool

PICkit 3


PICkit 4 programmer tool

PICkit 4 (new version)

PICkit Programming Tool’s Pin-out

This is the PICkit programmer pin-out. This pin-out is also known as the ICSP programming port.

Please take note that the pin no.1 is identified by the triangular arrow head.

PICkit 3 programmer pin out

MPLAB X IDE (Integrated Development Environment for Microchip Microcontroller)

The last item that you need is the software which allows you to develop and upload your program to your microcontroller chip.

MPLAB X IDE is a free microchip microcontroller development software. It is a complete platform for your firmware coding development work.

What you should do now is to download the following,

  1. MPLAB X IDE software (Windows version).
  2. MPLAB XC Compiler.

Download & Install the MPLAB X IDE Software MPLAB X IDE

You can click on this shortcut link to download the latest MPLAB X IDE software (Windows version). If this links cannot work, please follow the steps here to download from the Microchip official website.
https://www.microchip.com/mplab/mplab-x-ide

  1. Click here to go to the Microchip website for MPLAB X IDE software.
  2. Scroll down to the bottom of the page and look for the “Downloads” tab to click on.
  3. Go to the respective version (Windows, Linux or Mac) and download the software.
  4. As of this point in time the software is of version “MPLAB® X IDE v5.05

Install this software to your computer.

Download & Install the MPLAB X Compiler MPLAB XC Compiler

The compiler converts the programming source code into a machine code before you can upload the program into a microcontroller chip. Different microcontroller would requires its respective compiler. It is recommended to install all the compiler as follows.
http://www.microchip.com/mplab/compilers

Install these MPLAB XC8 and MPLAB XC16 compilers to your computer.

Step-by-Step Procedure to Upload Firmware to Microcontroller Chip

Now that you have your

  • Circuit Board
  • Programmer Tool
  • MPLAB X

ready…

We can start to upload your project or *.hex file..

 

There are two modes of uploading firmware to the microcontroller.

  1. Method 1: Upload hex.* file (machine code) using MPLAB X IPE software.
  2. Method 2: Upload project (source code) using MPLAB X IDE software.

Method 1: Firmware upload using IPE Software

This method is usually used for the production. Programmer operators will be provided with a *.hex file for them to upload the firmware to the microcontroller chip.

The advantage of this method is that it is simple, and it do not expose your actual project source code.

 

Step 1: Launch the program MPLAB X IPE software.

IPE software launch up screen shot

Step 2: Under the section “Device:“, select the microcontroller part number that you want to load your firmware to. Refer to your schematic for the exact microcontroller part number.

Click on the button “Apply“.

IPE selecting microcontroller device

Step 3: Plug in your programmer tool to your computer.

You should notice that the software update the “Tool:” section once your tool is detected.

IPE programmer tool detected

Click on the button “Connect” to start connecting to your programmer.

You should see the following message printed out…

*****************************************************

Connecting to MPLAB PICkit 3…

Currently loaded firmware on PICkit 3
Firmware Suite Version…..01.51.08 *
Firmware type…………..Enhanced Midrange

IPE programmer tool successfully connected

Step 4: Under the section “Hex File:“, load your *.hex file for your production. This is the machine code file for your circuit board. This is the only file required for the production.

For a start, this file is located in your project folder. For example…
Your project directory -> “yourProject.X\dist\default\production\yourProject.X.production.hex

Step 5: Connect your programmer tool to the ICSP port on your circuit board. Make sure that the pin 1 on your connector is aligned correctly. Then power up your board.

Step 6: Click on the button “Program” to start the uploading of your firmware to the microcontroller chip on your circuit board.

IPE program button

Yes, you are done. Congratulation.
Your board is successfully loaded with the firmware.

Method 2: Project source code upload using IDE Software

This method is usually used during the development. Engineer can edit the source code and upload the firmware changes onto the microcontroller chip directly.

It is recommended to maintain your firmware version while you are doing editing and improvement work. Keeping the original functioning version is a must. You can always fall back on the original copy if you mess up the project source codes.

Step 1: Launch the program MPLAB X IDE software.

MPLAB X startup screen

Step 2: Open up your project files. Go to File > Open Project…

Select your project files. It is the project folder which ends with *.X. For example “yourProject.X”

MPLAB X select project file

The project will loads up.

MPLAB X upload firmware to microcontroller IC chip

Step 3: Plug in your programmer tool to your computer.

Step 4: Connect your programmer tool to the ICSP port on your circuit board.
Make sure that the pin 1 on your connector is aligned correctly. Then power up your board.

Step 5: Click on the arrow down icon below the menu bar.
Select “Make and Program Device” to start the uploading of your firmware to the microcontroller chip on your circuit board.

Yes, you are done. Congratulation.
Your board is successfully loaded with the firmware.

More…

For further details, you can check up the Microchip official website Microchip logo
or YouTube YouTube logofor a more detailed instructions.

Electronic Components Store in Indonesia

This page contains references to where you can get electronic components and stuff in Indonesia.

Electronic Components Online Stores in Indonesia

Electronic Components Distributor in Indonesia

 

More electronic stuffs.

Check out more electronic component stuff that
you can shop in Singapore

and more electronics in Malaysia.

Electronic Circuit Design Companies in Malaysia

Are you looking for electronic circuit design company to develop your electronic product prototype in Malaysia?

Check out this list for your electronic specialise near your place in Malaysia.

Electronic Circuit Design & Prototyping

Electronic Manufacturing Service (Mass Production, Box Build)

More Electronic Services and Stuffs in Malaysia

Wondering where you can purchase electronic components, parts and accessories in Malaysia?
Click here to check out the electronic component marketplace in Malaysia.

Looking for More Options?

Malaysia is a big country. Searching for engineering services can be quite tedious.

Still cannot find…
Check out these electronic design companies in Singapore for more options.