Electronics
DIT Adafruit FeatherM0 RS232 Library
The source code can be found on GitHub.
This library provides:
- The
RS232_configure()
function, that configures SERCOM0 of the MCU to use the pins that are connected to Feather's JP1.2 (labelled TX) and JP1.3 (labelled RX). - The
RS232_enable()
function, that enables the USART and theon_received
andon_transmitted
callback functions (if assigned).
See the 02_RS232
tutorial for a demo on how to use the library.
Adafruit_FeatherM0_RS232.h
Adafruit_FeatherM0_RS232.c
RS232_cofigure()
In lines 43..65 we find the function RS232_configure()
that configures SERCOM0
of an Atmel ATSAMD21G18A MCU for RS232 communication.
- Line 47 creates a variable of the
usart_config
type - This variable is initialized with the default USART configuration in line 49
- Line 51 sets the desired baud rate
- Line 52 defines that the RX line of the USART should be routed to pad 3 and the TX line to pad 2
- The USART pads 0 and 1 are not used (lines 53 and 54)
All this pad and pinmux voodoo comes from the fact, that modern MCUs have more function-blocks than they have pins available to connect them all to the outside world.
Since usually not all the function-blocks are needed for every application the MCU designers work around that by connecting the pins to more than one MCU function-block using pin-multiplexing (pinumx). During the initialization of the MCU the application designer can connect the function-blocks in- and outputs (pads) to the pins they want them use to communicate to the outside world.
- Line 55 connects USART0 pad2 to the PA10 pin of the MCU
- Line 56 connects USART0 pad3 to the PA11 pin of the MCU
- Line 59 tries to initialize the USART until the initialization was successful
RS232_configure_callback()
In lines 70..81 we find the function RS232_configure_callback()
that assigns a callback function to an USART
callback event and enables the callback if a function is assigned.
Callback functions are called by the ASF USART module, for example:
- When the receive buffer is filled with characters received (
USART_CALLBACK_BUFFER_RECEIVED
) - When the content of a buffer was transferred (
USART_CALLBACK_BUFFER_TRANSMITTED
)
There are a couple of other events you can attach a callback function to (see usart.h: enum usart_callback
),
depending on you application's needs.
- Line 75 checks if a
callback_func
- Line 76 registers
callback_func
with thecallback_type
of the USART - Line 77 enables the
callback_type
for the USART - Line 79 disables the
callback_type
for the USART
- if
- otherwise
RS232_enable()
In lines 86..90 we find the function RS232_enable()
that enables the USART.
There is nothing fancy here. We just call the usart library function. This function only exists to have a complete RS232 API.