Using Atmel START with the SAM D21 MCU ADC and I²C

 Objective

This project shows you how to:

  • Use Atmel START to add and configure an Analog-to-Digital Converter (ADC) and I²C driver for your project.
  • Export your project for the Atmel Studio 7 IDE.
  • Use Advanced Software Framework 4 (ASF4) example code to:
    • Measure the analog output of a light sensor (using a SAM D21 ADC)
    • Read the value from a temperature sensor over an I²C bus (using a SAM D21 SERCOM configured for I²C)
  • Program the SAM D21 Xplained Pro Evaluation Kit to verify your code is working.

 Materials

Hardware Tools

Tool About Purchase
board-50px.png
SAM D21 Xplained Pro
Evaluation Kit
board-50px.png
I/O1 Xplained Pro
Extension Kit

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
swtool-28px.png
Atmel® Studio
Integrated Development Environment
Atmel® START (ASF4)
Integrated Software Framework
Web Based

Optional Lab Manual

A hardcopy of this tutorial is available here:
Using Atmel START with the SAM D21 MCU >

Procedure:

Step 1: Create a new Atmel START project
Step 2: Add and configure an ADC driver
Step 3: Add and configure the temperature sensor middleware and its associated I²C driver
Step 4: Add a Delay driver (sensor measurements every second)
Step 5: Save and export the application for the Studio 7 IDE
Step 6: Import the Atmel START project into the Studio 7 IDE
Step 7: Modify ADC example code to read light sensor data
Step 8: Program the board
Step 9: Verify light sensor measurements
Step 10: Modify I²C example code to read temp sensor data and verify measurements

Step 1: Create a new Atmel START project

1

Open a web browser, go to http://start.atmel.com, and select CREATE NEW PROJECT.

create.png

2

Select a specific board for the project.

  • Click on ‘Show only boards’ from the "RESULTS" section then select the ‘SAM D21 Xplained Pro’ board
  • Click CREATE NEW PROJECT to complete the project creation.
boards.png

The project is created in Atmel START and you now have access to the DASHBOARD view. Scroll to the bottom of this window to verify the ATSAMD21J18A is the selected device and explore its capabilities.

dashboard.png

Step 2: Add and configure an ADC driver

1

The I/O1 Xplained Pro uses a TEMT6000 light sensor. The sensor data can be read by an ADC pin on any Xplained Pro MCU board. Determine which pin from the SAM D21 Xplained Pro interfaces with it.

light_sensor.png
ext2_adc.png

Notice SAM D21 pin number PA10 (function AIN[18]) is connected to the light sensor.

2

Add the ADC driver:

  • Atmel START displays the 'DASHBOARD' tab (left side) by default. Click the Add software component button.
add_software_component.png
  • Type 'ADC' in the 'Filter' field, look for the ADC driver and add it. You can also directly look for it in the 'Drivers' list.
add_adc.png

3

Configure the ADC driver:

  • Click on the 'ADC_0' component block to start its configuration.
adc_config.png
  • Configure ADC Component Settings:
    • Driver:HAL:Driver:ADC_Async
adc_component.png
  • Configure ADC Signals:
    • AIN/18 - PA10: Enabled
adc_io.png
  • Configure ADC Basic Configuration:
    • Positive Mux Input Selection: ADC AIN18 pin
    • Negative Mux Input Selection: Internal ground
adc_basic.png

The ADC driver is now configured.

Step 3: Add and configure the temperature sensor middleware and its associated I²C driver

The I/O1 Xplained Pro extension board uses a Microchip AT30TSE758 temperature sensor chip with an 8 kb serial EEPROM inside. The sensor includes programmable high and low-temperature alarms, user selectable temperature resolution up to 12 bits and an I²C/SMBus™ compatible serial interface. The SAM D21 reads the temperature using its serial communication interface (SERCOM) peripheral configured for I²C.

1

Determine which pins from the SAM D21 Xplained Pro interface with the temperature sensor.

temp_pins.png
ext2_i2c.png

Notice SAM D21 pin numbers PA08 and PA09 (function SERCOM2 I²C SDA and SCL) are connected to the temperature sensor.

2

Add the Temperature Sensor Middleware.

  • Select 'DASHBOARD' and click ‘Add software component’.
  • Expand Middleware > Sensors, find the Temperature Sensor middleware and click on it.
  • Click Add component(s).
add_temp_sensor.png

Notice that adding the temperature sensor middleware automatically adds an I²C driver which is required to interface to the sensor.

temp_i2c.png

The Temperature Sensor middleware is now added to the application.

If you click on the Temperature Sensor component block, you will see the dependencies of that middleware.

3

Configure the I²C driver.

  • Click on the 'I2C_INSTANCE' component block to start its configuration.
  • Configure the I²C Component Settings:
    • Driver: HAL:Driver:I2C_Master_Sync
    • Mode: I²C Master Standard/Fast-mode
    • Instance: SERCOM2
  • Configure I²C Signals:
    • SCL: PA09
    • SDA: PA08
  • Configure I²C Basic Configuration:
    • I²C Bus Clock speed (Hz): 100 kHz
i2c_driver_config.png

The I²C driver is now configured.

Step 4: Add a Delay driver (sensor measurements every second)

This driver provides functions which allow you to add delays in µs or ms using the Cortex®-M0+ SysTick timer.

  • Add the Delay driver:
    • Select DASHBOARD and click ‘Add software component’.
    • Expand ‘Drivers’, find the Delay driver and click on it.
    • Click Add component(s).
delay.png

The Delay driver is now added to the application.

Step 5: Save and export the project for the Studio 7 IDE

You have now created and configured your Atmel START based project. It’s a good idea to save this configuration so you can make changes to it sometime in the future. Atmel START allows you to restore any project using its configuration file (*.atstart file format).

1

Save your Atmel START project.

  • Select SAVE CONFIGURATION.
  • Click on DOWNLOAD CONFIGURATION.
  • Provide a filename and location and then click Save.
save_config.png

2

Export the project for the Studio 7 IDE.

  • Select EXPORT PROJECT then DOWNLOAD PACK.
  • Provide a filename and location, then click Save.
export.png

Your Atmel START project has been exported for the Studio 7 IDE in a *.atzip file format (standard zip format automatically recognized by Studio 7).

Step 6: Import the Atmel START project into the Studio 7 IDE

1

Open Studio 7 and select:

  • File > Import > Atmel Start Project
import.png

2

Browse to the project you exported from START (*.atzip) and click OK. Your project is now created.

import1.png

Note a default project name and location have been selected for you. Feel free to change these.

Atmel START projects come with some useful examples to help you get started on the different peripherals you’re looking to use. You can find these in the driver_examples.c file in the examples folder.

solution_explorer.png

Open the main.c file. You will see the only function called in the current project is the atmel_start_init() function:

main1.png

The atmel_start_init() function calls the system_init() and temperature_sensors_init() functions.
The system_init() function calls the following functions:

  • init_mcu(): initialize oscillators, clocks, flash wait states…
  • ADC_0_init()
  • I2C_INSTANCE_init()
  • delay_driver_init()

Want to see what these functions do?
Right-click on the function name and select 'Go to Implementation' to see the function definition (C file) and description (header file).
For more details, see the ASF4 API Reference Manual.

Open the driver_init.c file to see these and other initialization functions generated in response to the selections you made in Atmel START. You may also want to check out the configuration header files (found in the Config folder).

Step 7: Modify ADC example code to read light sensor data

As mentioned above, the examples in the driver_examples.c file will be used to help you get started.

1

In step 2, you configured the ADC driver as 'HAL:Driver:ADC_Async'. This means that the ADC takes a measurement and then generates an interrupt. The ADC interrupt then calls a callback function that will have to be implemented.

  • Open driver_examples.c (in the examples folder) and copy the following functions:
    • convert_cb_ADC_0()
    • ADC_0_example()
  • Paste them above the main() function in the main.c file.
  • Rename the ADC_0_example() function as ADC_light_init().
  • Rename the convert_cb_ADC_0() function as convert_cb_ADC().
  • Update convert_cb_ADC() callback call in ADC_light_init().

Your code should look like this:

code4.png

2

Initialize the ADC by calling this function after atmel_start_init():

  • ADC_light_init();

3

Start an ADC conversion every second.
First, use the Delay driver you added to implement a one second delay.

  • Add this delay function inside the while(1) loop in main().
    • delay_ms(1000);

Now you have to call a function to start the conversion. The ADC_light_init() function uses this start conversion function, but it doesn't need to be in there.

  • Cut the following function from ADC_light_init() and paste it after the delay(1000) function:
    • adc_async_start_conversion(&ADC_0)
code19.png

Your main.c should look like this:

code20.png

4

Add code for ADC callback function.

  • Add the following variable declaration after the include startement in main.c. This is used to indicate if the conversion is completed or not.
    • volatile bool conversion_done = false;
code21.png
  • Update the ADC callback to indicate the conversion is done:
    • conversion_done = true;
code9.png
  • Wait for conversion done in the while loop:
    • while(!conversion_done);
    • conversion_done = false;
code10.png

5

Declare a two-byte buffer to store the 12-bit ADC value from the light sensor.

  • Add the following code to the variable declarations at the top of main.c:
    • uint8_t ADC_buffer[2];

6

Read the light sensor data.

  • Call the following function in the while loop, once data is converted to read the 12-bit value.
    • adc_async_read_channel(&ADC_0, 0, ADC_buffer, 2);

Note: The ADC driver functions can be found in the hal_adc_async.c file (hal/src folder).

Your main.c file should look like this:

code22.png

Step 8: Program the board

1

Connect the SAM D21 Xplained Pro Evaluation Kit to your computer.

  • Power-up your SAM D21 Xplained Pro board using DEBUG USB Connector.
  • Please be patient as the driver installs (it may take a minute or so).
edbg.png

2

Select the embedded (on-board) debugger found on the SAM D21 XPRO board as the Debugger/Programmer for the project.

  • Click on Project > Properties.
proj_prop.png
  • Select Tool > EDBG … as debugger/programmer.
tool.png

3

Compile the project and program the board.

  • Compile the project by clicking on the 'Build Solution' icon or by typing ‘F7’, and verify that it builds successfully.
build.png
  • Program the application by clicking on the 'Start Without Debugging' icon.
start_without_debug.png

Note: If the firmware on the evaluation board is out of date, a Firmware Upgrade window will appear asking you if you want to upgrade the firmware. Select Upgrade and allow the process to complete.

Your application is now running on the evaluation board.

Step 9: Verify light sensor measurements

  • Break the application by clicking the 'Break All' icon:
break.png
  • In the main.c file, right click on the 'ADC_buffer' variable then 'Add Watch':
  • The Watch 1 window will appear with your selected variable. Right-click on the Watch window value to select 'Hexadecimal Display'.
watch1.png
  • Click on the 'Continue' icon to execute the application.
continue.png

If you hide the light sensor (i.e., put your finger on it) then click the 'Break All' icon to break the application, you can verify the buffer value should approach 0x0FFF.

watch2.png

Congratulations!
The SAM D21 ADC is measuring the output of the light sensor!

Step 10: Modify I²C example code to read temp sensor data and verify measurements

1

Add your application code to retrieve the Temperature Sensor data over I²C.

  • Call temperature_sensors_init() after ADC_light_init() as shown below:
code23.png

Note: The temperature sensor initialization source code can be found in the temperature_sensor_main.c file.

A global variable will be used to get temperature data.

  • Add temperature global variable:
    • float temperature;
code24.png
  • Get the temperature using the at30tse75x_read() function:
    • temperature = at30tse75x_read(TEMPERATURE_SENSOR_0);
code16.png

The I²C Temperature Sensor implementation is now completed.

2

Use a Watch window to view the temperature sensor value.

  • Stop the debugger.
stop.png
  • Compile the project by clicking the 'Build Solution' icon or by typing ‘F7’, and verify it builds successfully.
  • Launch the debugger by clicking the 'Start Debugging' icon then break the application by clicking the 'Break All' icon.
  • Right-click on the temperature variable then 'Add Watch' (Unselect Hexadecimal Display).
  • Put a breakpoint after the at30tse_read call and execute the application to get the temperature value:
watch3.png

Place your warm finger on the temperature sensor and click the debugger’s 'Continue' icon to verify the temperature rises as expected.

Congratulations!
The SAM D21 SERCOM (configured for I²C) is receiving data from the temperature sensor over the I²C bus!

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.