Electronics

Adafruit FeatherM0 ASF Tutorials

The source code for the tutorials can be found on GitHub.

2017-07-04 00:58

02_RS232 - Serial Communication

The second project not only introduces the ASF SERCOM module, it also provides us with the basics for printing debug messages on a terminal or terminal emulator (like PuTTY on Windows).


Schematic Source: Adafruit, License: Attribution-ShareAlike Creative Commons

The schematic of Adafruit's FeatherM0_WiFi shows two pins on the ATSAMD21G18_QFN being labelled TXD (PB22, pin 37) and RXD (port PB23, pin 26).

If you look on the PCB there are two pins on JP1 (the longer header) also labelled TX (pin 2) and RX (pin 3).

Don't let them confuse you! The pins on the chip are not connected to the pins on this or any other header!

The program will:

Adding a new Atmel Software Framework (ASF) Skeleton Project

In project 01_Blink we created a new Atmel Software Framework (ASF) project and the new Adafruit_FeatherM0_ASF_Tutorials Solution.

Now add a new ASF Skeleton Project for the 02_RS232 tutorial to this existing solution.

See my Step-By-Step tutorial if you are not sure how to do this.

Choosing a startup project

To make sure AtmelStudio always compiles and runs the 02_RS232 project we make it the StartUp Project.

In the Solution 'Adafruit_FeatherM0_ASF_Tutorials' Property Pages dialog select

Adding the project specific Atmel Software Framework (ASF) code

The next step is to import the ASF modules needed for the specific project.

You should see two lists:

In Selected Modules we already see:

This is the standard framework needed for any ASF application.

For the LED to blink we need to control the PORT/Pin the LED is connected to.

So in Available Modules find:

And Add>> the module to the project.

Also in Available Modules find:

And Add>> this module to the project.

Now press Apply. This will add all the necessary ASF code to our project.

Using the DIT Adafruit Feather Library

For this project we need to:

See my Step-By-Step tutorial if you are unsure how to do this.

Writing the application code

In the Solution Explorer find the generated main.c.

All we need to import to get the whole ASF magic working is the asf.h, that was generated by the ASF wizard. The entry should already be there. The wizard took care of that for us (line 54).

usart_on_received()

The lines 73..76 implement the callback function that we want the ASF USART module to call whenever the receive buffer is filled.

We use the function call in lines 75 to write back whatever character was received to the sender.

usart_on_transmitted()

The lines 87..90 implement the callback function that we want the ASF USART module to call whenever a buffer was transmitted.

We use the function call in lines 89 to toggle the on/off state of the LED in that case.

main()

The main function in lines 101..133:

  1. Initializes the board (line 109) and enables interrupts globally (line 110)
    If you forget to enable the interrupts the callback functions will not work.
  2. We use the DIT Adafruit Feather Library functions to:
    • Configure the FeatherM0 LED pin (LED_configure(), line 116)
    • Configure the FeatherM0 RS232 SERCOM (RS232_configure(), line 118)
    • Configure and enable the usart_on_received() callback that we want to be called by the usart, whenever the receive buffer is filled (RS232_configure_callback(), line 119)
    • Configure and enable the usart_on_transmitted() callback that we want to be called by the usart, whenever it is finished transmitting a buffer (RS232_configure_callback(), line 120)
    • Enable the FeatherM0 RS232 SERCOM (RS232_enable(), line 121)
  3. In line 127 we define the welcome message
  4. In line 128 we send the welcome message out
  5. Finally we enter an infinite loop (lines 1130..132) and read any characters that might have been sent to us (line 131)

Remember: