2.2.1 Getting Started on CAN Protocol (CAN-FD) SAMA5D29 Curiosity Development Board
Description
This application transmits and receives CAN FD messages from MCAN0 to MCAN1 or vice versa. To run this application on SAMA5D29 Curiosity Development Board, two mikroBUS ATA6563 click boards (MCAN0 and MCAN1) are required. MCAN0 and MCAN1 acts as different nodes on the CAN bus. User can send and receive CAN-FD messages from/to respective CAN node through UART console on the PC.
User input | Functions |
---|---|
Press 0 on the UART console | CAN-FD standard message transmit From MCAN0 to MCAN1 |
Press 1 on the UART console | CAN-FD standard message transmit From MCAN0 to MCAN1 |
Press 2 on the UART console | CAN-FD Extended message transmit From MCAN0 to MCAN1 |
Press 3 on the UART console | CAN-FD Extended message transmit From MCAN0 to MCAN1 |
Press 4 on the UART console | CAN-FD standard message transmit From MCAN1 to MCAN0 |
Press 5 on the UART console | CAN-FD standard message transmit From MCAN1 to MCAN0 |
Press 6 on the UART console | CAN-FD Extended message transmit From MCAN1 to MCAN0 |
Press 7 on the UART console | CAN-FD Extended message transmit From MCAN1 to MCAN0 |
Press M or m on the UART
console | Display menu |
Key Highlights of SAMA5D29 Curiosity Development Board
- External Non-Volatile Memories like QSPI, SD, and micro-SD card interfaces.
- Additional sensors can be interfaced using click boards through an on-board mikroBUS connector.
- Two mechanical programmable buttons.
- One User Input Switch and one RGB LED.
- UART, USB, CAN, Ethernet, Audio and JTAG.
- LCD and ISC camera.
- Raspberry pi connectors.
Modules/Technology Used
- UART0
- MCAN0
- MCAN1
Hardware Used
Hardware Modifications to Enable MCAN Communications
- Enable MCAN0:
- Enable MCAN1:
- Step1: To disable MBUS_2_UART_TX and MBUS_2_UART_RX, remove the resistors from R192 and R193.
- Step2: To enable MCAN1 communication, add 0R resistors across R188 and R189.
- Step3: To disconnect LCD_D22 line, remove the R132 resistor.
- Step4: To enable MCAN1 communication, add 22R resistor to R134.
- Step5: To disconnect LCD_D23 line, remove the R136 resistor.
- Step6: To enable MCAN1 communication, add 22R resistor to R138.
Note: Follow the above steps to enable the MCAN1 communication. For more details, refer the Hardware Modification to enable MCAN0 communication section.
Modified SAMA5D29 Curiosity Development Board
Software/Tools Used
This project has been verified to work with the following versions of software tools:
Refer Project Manifest present in harmony-manifest-success.yml present in harmony-manifest-success.yml under the project folder firmware/src/config/sam_a5d29_cu to know the MPLAB® X IDE, MCC, libraries version.
Hardware Setup
- Power up the board by connecting the powered USB type-C cable to the USB port J1 on the SAMA5D29 curiosity development board.
- Connect USB-TTL cable to J28 (UART DEBUG).
- Remove/Open the jumper from J10 to disable QSPI boot and J14 to disable ETHERNET.
- Connect external JTAG
debugger at J27 (JTAG connector).
- Plug the ATA6563
mikroBUS 1 click board to J19 (mikroBUS 1) of the SAMA5D29 Curiosity Development Board and ATA6563 mikroBUS 2 click board to J22 (mikroBUS
2) of the SAMA5D29 Curiosity Development Board as per the
Pin connections shown below.
- MCAN0 (PC10 and PC11)
lines are routed to mikroBUS 1 connector (J19 Connector)
mikroBUS 1 interface (MCAN0) ATA6563 click Board CANTX0 (PC10) RX (PIN 14) CANRX0 (PC11) TX (PIN 13) VDD_3V3 3V3 (PIN 7) VDD_5V 5V (PIN 10) GND GND (PIN 9) - MCAN1 (PC26 and PC27)
lines are routed to mikroBUS 2 connector (J22 Connector)
mikroBUS 2 interface (MCAN1) ATA6563 click Board CANTX1 (PC26) RX (PIN 14) CANRX1 (PC27) TX (PIN 13) VDD_3V3 3V3 (PIN 7) VDD_5V 5V (PIN 10) GND GND (PIN 9)
- MCAN0 (PC10 and PC11)
lines are routed to mikroBUS 1 connector (J19 Connector)
- Connect the jumper wires from the ATA6563 mikroBUS click 1 (CANL and CANH)
to the ATA6563 mikroBUS click 2 (CANL and CANH).
Developing a CAN-FD demo application
- Open MPLAB® X IDE from the main menu.
- Create a New Project by clicking
the New Project icon
or by selecting File -> New Project.
- In the New Project window, under
Projectschoose Application Project(s). and click Next.
- In the Select Device
dialog window, fill in or select the information for below:
- Family: Fill configuration name as 32-bit MCUs (PIC32C/SAM).
- Device : From drop down list select SAMA2D29.
- In the Select Compiler
window, for Compiler Toolchains select XC32 COMPILER, and Click
Next.
- Enter Project Location ,
Project Folder and Project Name. Click Finish
- This creates an empty project and
set this project as main project. If there are other projects open in the
project explorer window, set this project as main project by right
clicking on the project, choose Set as Main Project.
- After the project is created, MCC
will be automatically launched. (To launch MCC manually, from main menu, click
on Tools -> Embedded -> MPLAB® Code Configurator or
click MCC button in the MPLAB® X IDE tool bar.) It will launch Content
Manger Wizard. Then select MPLAB® Harmony.
- In addition to the required
packages(csp, dev_packs), download the optional packages bsp, core and then
click Finish. Content download will take some time. Wait till all the
contents are downloaded.
- A project graph will be
displayed. From Device Resource add Board Support Packages for
SAM A5D29 Curiosity BSP to Project Graph.
- From Device Resource,
Peripherals -> UART , add UART0 to the Project
Graph. This helps to print the debug messages (console serial
communication messages) via comm port.
- In the project graph, right click
on UART in UART0 and then add consumers STDIO.
- From the project graph window,
click the System and in the Configuration Options -> Clock
(PMC) -> Generic Clock Menu -> Generic Clock for
Peripheral UART0. Enable the Generic Clock for UART0. Select the
MCK_CLK clock.
- From the project graph window,
click the UART0 and in the Configuration Options -> Select
Clock Source - MCK and Baud Rate - 115200.
- From Device Resource,
Peripherals -> MCAN , add MCAN0 to the Project
Graph.
- From the project graph window,
click the System and in the Configuration Options -> Clock
(PMC) -> Generic Clock Menu -> Generic Clock for
Peripheral MCAN0. Enable the Generic Clock for MCAN0. Select the
UPLL_CLK clock and set Generic Clock Division Ratio to 12.
- From Device Resource,
Peripherals -> MCAN , add MCAN1 to the Project
Graph.
- From the project graph window,
click the System and in the Configuration Options -> Clock
(PMC) -> Generic Clock Menu -> Generic Clock for
Peripheral MCAN1. Enable the Generic Clock for MCAN1. Select the
UPLL_CLK clock and set Generic Clock Division Ratio to 12.
- From the project graph window,
click the MCAN0 and in the Configuration Options -> select
MCAN Operation Mode to CAN-FD, Enable the Interrupt
Mode, Normal Bit Timing, Data Bit Timing, Use RX FIFO
0, Use RX FIFO 1, Use Dedicated Rx Buffer, Use TX
FIFO, Standard Filter, Extended Filter and Enable the
Timestamp Enable.
- From the project graph window,
click the MCAN1 and in the Configuration Options -> select
MCAN Operation Mode to CAN-FD, Enable the Interrupt
Mode, Normal Bit Timing, Data Bit Timing, Use RX FIFO
0, Use RX FIFO 1, Use Dedicated Rx Buffer, Use TX
FIFO, Standard Filter, Extended Filter and Enable the
Timestamp Enable.
- From the project graph
window, click the plugins and select the Pin Configuration. Pin
Setting window will open.
- Ensure all the pins are configured as below:
Pin ID Function PA7 LED_AH PA8 LED_AH PA9 LED_AH PA17 SWITCH_AL PB26 UART0_URXD0 PC27 UART0_UTXD1 PC10 MCAN0_CANTX0 PC11 MCAN0_CANRX0 PC26 MCAN1_CANTX1 PC27 MCAN1_CANRX1 - From the project graph
window, click the plugins and select the AIC Configuration. AIC
window will open. Ensure MCAN0 and MCAN1 interrupt handlers are enabled.
- Save all and then click
Generate Code. This will generate code for all the Device Resource
that have been added in the project graph.
- Up to this point in the project
creation process, the user used MPLAB Code Configurator(MCC) to configure and
generate code to initialize the device (SAMA5D29) and initialize MACN0 and
MCAN1.
Refer reference_apps_sam_mpu\apps\sama5d29_curiosity_dev_board\sama5d29_curiosity_can_fd\firmware\src\config\sam_a5d29_cu\initialization.c to find all the device resource added in the project graph got initialized. All that is left is for the user to write the application code in the app.c file. Documentation for each of the peripheral libraries or diver libraries can be accessed as follows:
- Peripheral libraries APIs can be accessed as a HTML file (*.html) from the Harmony 3 Framework path. (/framework_path/csp/docs/index.html)
- BSP libraries APIs can be found in bsp.h
- MCAN APIs can be accessed as a HTML file (*.html) from the Harmony 3 Framework path. (/framework_path/csp/docs/GUID-FE4048B2-547B-4753-A6AD-63A3D937288F.html)
- APIs used for this application are as follows:
- static void display_menu(void)
- static void print_message(uint8_t numberOfMessage, MCAN_RX_BUFFER *rxBuf, uint8_t rxBufLen, uint8_t rxFifoBuf,uint8_t mcanID)
- void MCAN0_MessageRAMConfigSet(uint8_t *msgRAMConfigBaseAddress)
- void MCAN0_TxFifoCallbackRegister(MCAN_TX_FIFO_CALLBACK callback, uintptr_t contextHandle)
- void MCAN0_RxFifoCallbackRegister(MCAN_RX_FIFO_NUM rxFifoNum, MCAN_RX_FIFO_CALLBACK callback, uintptr_t contextHandle)
- void MCAN0_RxBuffersCallbackRegister(MCAN_TXRX_BUFFERS_CALLBACK callback, uintptr_t contextHandle)
- bool MCAN0_MessageTransmitFifo(uint8_t numberOfMessage, MCAN_TX_BUFFER *txBuffer)
- bool MCAN0_MessageReceiveFifo(MCAN_RX_FIFO_NUM rxFifoNum, uint8_t numberOfMessage, MCAN_RX_BUFFER *rxBuffer)
- bool MCAN0_MessageReceive(uint8_t bufferNumber, MCAN_RX_BUFFER *rxBuffer)
- LED_GREEN_On().
- LED_GREEN_Off().
- LED_RED_On().
- LED_RED_Off().
- Right click on the project and
click Properties. For Connected Hardware Tool -> select
connected hardware debugger used, for Compiler Toolchain -> select
XC32 and click Apply.
- From J-32/J-Link, Option
categories choose Communication and for JTAG Method,
select 4-wire JTAG.
- Select Bootstrap for
Categories and select the Use Bootstrap checkbox. For bootstrap
file -> select the at91bootstrap elf file
(
${ProjectDir}/at91bootstrap.elf
) from reference_apps_sam_mpu\apps\sama5d29_curiosity_dev_board\sama5d29_curiosity_can_fd\firmware\sama5d29_cu.X. Click on Apply and OK. - By default, MPLAB X only produces
ELF and Hex format output files. To generate application
output in binary format, a post build step needs to be added to the project
properties. To do this project properties, building, click the check box
Execute this line after build, then enter the below
command.
${MP_CC_DIR}/xc32-objcopy -O binary ${DISTDIR}/${PROJECTNAME}.${IMAGE_TYPE}.elf ${DISTDIR}/harmony.bin
- Clean and build the project. The user should see a message on the output console that the project was successfully built. This completes the development of the MCAN getting started application.
- The harmony.bin binaries will be available at ./firmware/mcan_fd_operation_interrupt_timestamp_sam_a5d29_cu.X/dist/default/production.
Programming/Debugging Application Project on MPLAB® X IDE
- Open the project (sama5d29_curiosity_dev_board/mcan/firmware/mcan_fd_operation_interrupt_timestamp_sam_a5d29_cu.X) in MPLAB® X IDE.
- Right click on the project and
click Properties. For Connected Hardware Tool -> select
connected external hardware debugger, for Compiler Toolchain -> select
XC32 and click Apply.
- From J-32/J-Link , in Option
categories choose Communication and for JTAG Method,
select 4-wire JTAG.
- Select Bootstrap for
Categories and select the Use Bootstrap checkbox. For bootstrap
file, select the at91bootstrap elf file from
reference_apps_sam_mpu\apps\sama5d29_curiosity_dev_board\sama5d29_curiosity_can_fd\firmware\sama5d29_cu.X.
Click on Apply and OK.
- Clean and build the project. The user should see a message on the output console that the project was successfully built.
- Debug the code by clicking on the Debug button in MPLAB® X IDE tool bar.
- Run the application by clicking Run button in MPLAB® X IDE tool bar.
- Open the Terminal application (Ex.: Tera term) on the computer.
- Connect to the DEBUG/J-link Virtual COM port and configure the serial settings
as follows:
- Baud Rate : 115200
- Data : 8 Bits
- Parity : None
- Stop : 1 Bit
- Flow Control : None
- In the console associated with MCAN0 and MCAN1, press "0" to transmit a CAN-FD standard message From MCAN0 to MCAN1.
- Transmitted message description and status will be displayed in the console window.
- Received message will be displayed in the console window.
- Follow the sequence below to send and receive different messages:
- Press '1' on the UART console to transmit a CAN-FD standard message From MCAN0 to MCAN1.
- Press '2' on the UART console to transmit a CAN-FD extended message From MCAN0 to MCAN1.
- Press '3' on the UART console to transmit a CAN-FD extended message From MCAN0 to MCAN1.
- Press '4' on the UART console to transmit a CAN-FD standard message From MCAN1 to MCAN0.
- Press '5' on the UART console to transmit a CAN-FD standard message From MCAN1 to MCAN0.
- Press '6' on the UART console to transmit a CAN-FD extended message From MCAN1 to MCAN0.
- Press '7' on the UART console to transmit a CAN-FD extended message From MCAN1 to MCAN0.
If the steps are executed in this sequence, the final output in the consoles will be as below:
Running the Pre-built Harmony Application from macro-SD Card
The pre-built application bin file can be programmed by following the below steps:
Steps to program the bin file on macro-SD card:
- Take a micro-SD Card formatted with FAT32 file system.
- Copy the boot.bin and harmony.bin files from reference_apps_sam_mpu\apps\sama5d29_curiosity_dev_board\sama5d29_curiosity_can_fd\hex to the micro-SD card using the PC.
- Insert the macro-SD card to J6 on the SAMA5D29 Curiosity development board.
- Perform a reset by pressing
RESET button on the SAMA5D29 Curiosity Development Board
Comments
- This application demo builds and
works by following the instructions above in Running the Demo section. If
the user needs to enhance/customize this application demo, should use the MPLAB
Harmony v3 Software framework. Refer links below to setup and build the
applications using MPLAB Harmony.
- How to Setup MPLAB Harmony v3 Software Development Framework
- How to Build an Application by Adding a New PLIB, Driver, or Middleware to an Existing MPLAB Harmony v3 Project
- SAMA5D2 Series Boot Process
- MPLAB® Harmony v3 is also configurable through MPLAB® Code Configurator (MCC)- Refer to the below links for specific instructions to use MPLAB® Harmony v3 with MCC.