2.5.1 Blink LED Application Getting Executed from QSPI Flash on SAM9X60 Evaluation Kit

Download

Description

This application demonstrates how to run and debug an application executed from QSPI flash on SAM9X60 Evaluation Kit. Blue LED turn ON when user input key(sw1) is pressed.

Key Highlights of SAM9X60 Evaluation Kit

  • LCD Interface.
  • External Non-Volatile Memories like NAND, SD, and MicroSD card interfaces.
  • Additional sensors can be interfaced using click boards through an on-board mikroBUS connector.
  • Two mechanical programmable buttons.
  • One User Input Switch(sw1) and one RGB LED.
  • UART, USB and CAN Interfaces.
  • Raspberry pi connectors.

Modules/Technology Used

Peripheral Modules:
  • BSP
  • DBGU

Hardware Used

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 under the project folder firmware/src/config/default to know the MPLAB® X IDE, MCC, libraries version.

Hardware Setup

  • Power up the board by connecting 5V/2A power adapter to J1 or connect the USB Micro-B port(J7) to the Host PC using a micro-USB cable.
  • Connect the USB Micro-B port (J22- J-Link-CDC) on board to the computer using a micro-USB cable (to print debug messages on the serial console).
  • Default jumper settings should be as follows:
  • Press the Start button(nSTART switch) on the board.

Developing an Application Executed from QSPI Flash Memory

  1. Open MPLAB® X IDE from the main menu.
  2. Create a New Project by clicking the New Project icon or by selecting File -> New Project.
  3. In the New Project window, under Projectschoose Application Project(s). and click Next.
  4. In the Select Device dialog window, fill in or select the information for below:
    1. Family: Fill configuration name as 32-bit MCUs (PIC32C/SAM).
    2. Device : From drop down list select SAM9X60.
  5. In the Select Compiler window, for Compiler Toolchains select XC32 COMPILER, and Click Next.
  6. Enter Project Location , Project Folder and Project Name. Click Finish
  7. 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.
  8. In addition to the required packages, download the optional packages bsp, csp, core, dev_packs and then click Finish. Content download will take some time. Wait till all the contents are downloaded.
  9. A project graph will be displayed. From Device Resource add Board Support Packages for SAM9X60 Evaluation kit BSP to Project Graph.
  10. From Device Resource, Peripherals -> add DBGU to the Project Graph and ensure the configuration options of DBGU as shown below. This helps to print the debug messages (console serial communication messages) via comm port.
  11. Ensure all the pins are configured as below:

    PA9 and PA10 are for console serial debugging. PD18 should be configured for user input switch. PB11, PB12, PB13 are for LEDs red, green, and blue respectively.

    Configure the pins PB19, PB20,PB21, PB22, PB23, PB24 for QSPI as mentioned in the below image.
  12. Save all. Then, click Generate Code. This will generate code for all the Device Resource that have been added in the project graph.
  13. 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 (SAM9X60) .

    Refer initialization.c from the project_folder/src/config directory 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 main.c file. Documentation for each of the peripheral libraries or bsp libraries can be accessed as follows:

    1. Peripheral libraries APIs can be accessed as a HTML file (*.html) from the Harmony 3 Framework path. (//csp/docs/index.html).
    2. BSP libraries APIs can be found in bsp.h.
  14. APIs used for this application are as follows:
    • bool DBGU_Write( void* buffer, const size_t size );
    • bool DBGU_WriteIsBusy( void );
    • LED_BLUE_On();
    • LED_BLUE_Off);
    • SW1_Get();
  15. Sample code can be found in main.c file from the project_folder/src directory.
  16. 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.
  17. From J-32/J-Link, Option categories choose Communication and for JTAG Method, select 4-wire JTAG.
  18. Select Bootstrap for Categories and select the Use Bootstrap checkbox. For bootstrap file -> select the at91bootstrap elf file from the project directory. Click on Apply and OK.
  19. 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, go to 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

Linker File Modifications to Execute an Application from QSPI Flash (Offset 0x40000)

In this example, it has demonstrated an application getting executed from QSPI flash offset 0x40000.

Note: To change the QSPI offset from where application will get executed, at91bootstrap kconfig must be configured and built.

The following linker file(ddram.ld) modifications are required to enable application getting executed from QSPI:

  • Define only two memory regions:
  • Assign text section to rom(qspi) and data section to relocate section(so, it will be copied from qspi memory to sram during runtime).
  • Remove cache aligned sections and assign _ramcode_lma and _ramdata_lma to ram as shown below.
  • Sample linker file can be found in the project directory/src/config.
  • Its a good practice to remove unused input section while building output binary. To do this, go to project properties, xc32-ld config , check Remove Unused Sections as shown below.
  • 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 application.
  • The harmony.bin file can be found in the /project_directory/dist/default/production/harmony.bin.

SAM-BA Installation and Setup

  • Install SAM-BA tool on the Windows host PC:
    1. To Download the latest version of SAM-BA tool for Windows click this link.
    2. Download the ZIP file and unzip it into a working directory of the choice.
    3. Add the SAM-BA directory path to the environment variables.
      • To do this from the PC --> Open the Start Search, type in env.
      • Choose Edit the system environment variables --> Click Environment Variables --> System Variables --> add SAM-BA directory path to path variables.
    4. Once the SAM-BA Host program has been installed, the execution of the application is from the Windows command prompt.
  • Setup to flash using SAM-BA:
    1. Open Disable boot Jumper(J13) on SAM9X60 Evaluation Kit.
    2. Ensure there is no SD memory card inserted and there is no application on any of the external NVMs.
    3. Press Start button.
    4. Push and hold the BOOT_DIS button. (This disables booting from the onboard memories - NAND and NOR(QSPI) flash Memory)
    5. Reset the board by pressing and then releasing the reset nRST button.
    6. Release the BOOT_DIS button.

Configure First Stage Bootloader to Boot from QSPI Flash

Use the below SAM-BA commands to configure first stage bootloader to boot from QSPI. Click here to learn about boot process and first stage boot configuration.

Note: It is not a mandatory step.
  • sam-ba -p j-link -b sam9x60-ek -a bootconfig -c writecfg:bscr:EMULATION_ENABLED // Enable Emulation
  • sam-ba -p j-link -b sam9x60-ek -a bootconfig -c readcfg:bscr // Read bscr and verify emulation is enabled
  • sam-ba -p j-link -b sam9x60-ek -a bootconfig -c resetemul // Emulation SRAM Reset
  • sam-ba -p j-link -b sam9x60-ek -a bootconfig -c writecfg:bcp-emul:DBGU,QSPI0_IOSET1 // Enable debug, QSPI0 as external NVM
  • sam-ba -p j-link -b sam9x60-ek -a bootconfig -c readcfg:bcp-emul // Read bcp_emul and verify whether QSPI is set as external NVM
  • Reset the board by pressing reset button.
Note: Boot configuration using Emulation SRAM has to be set every power cycle.

User can also write boot configuration packet(BCP) to OTPC(One Time Programmable Memory Controller). Refer SAM-BA installation directory on how to write BCP to OTPC

Debugging Application Project on MPLAB® X IDE

  1. Open the project (sam9x60_ek_blink_led_qspi_xip/firmware/sam9x60_ek.X) in MPLAB® X IDE.
  2. In the project properties, ensure SAM9X60 is selected as the Device and for Connected Hardware Tool, select connected J-Link debugger with the board to program/debug the application.
  3. Build the code. The harmony.bin file can be found in the /project_directory/dist/default/production/harmony.bin.
  4. Before debugging ensure that harmony.bin file in the project directory is flashed in the QSPI offset.
    Note: Refer SAM-BA Installation and Setup section for initial device setup and tool installation procedure.
    Use the below sam-ba commands to erase QSPI flash and program the application:
    • sam-ba -p j-link -b sam9x60-ek -a qspiflash -c erase //To erase entire QSPI flash
    • sam-ba -p j-link -b sam9x60-ek -a qspiflash -c erase:0x40000:0x20000 //Erase only 0x20000 bytes of data from the QSPI offset 0x40000
    • sam-ba -p j-link -b sam9x60-ek -a qspiflash -c write:harmony.bin:0x40000 //Program the application in the QSPI offset 0x40000
      Note: Do not flash boot.bin to QSPI memory to debug the application on MPLAB® X IDE.
  5. Debug the code by clicking on the Debug button in MPLAB® X IDE tool bar and observe that the application is getting executed from QSPI flash memory.
  6. Run the application by clicking the Run button in MPLAB® X IDE tool bar. A terminal emulation program running on the Host PC communicates with the SAM9X60 Evaluation kit J-Link CDC. Ensure the terminal emulation program (Eg: PUTTY) is configured to the COM port and settings are:
    • Speed: 115200
    • Data: 8
    • Parity: None
    • Stop Bits: 1
  7. The user will get the debug message on the serial console as This application is getting executed from QSPI-offset 0x40000. Press the user button(sw1) and observe LED getting turned ON.

Running the Pre-built Harmony Application from QSPI Flash Memory

The boot.bin and harmony.bin files can be found from the project directory/hex folder. The pre-built application bin file and boot bin file can be programmed to QSPI flash using SAM-BA .
Note: Refer SAM-BA Installation and Setup section for initial device setup and tool installation procedure.

SAM-BA commands to program the boot and application file to QSPI flash:

  • sam-ba -p j-link -b sam9x60-ek -a qspiflash -c erase //To erase entire QSPI flash
  • sam-ba -p j-link -b sam9x60-ek -a qspiflash -c writeboot:boot.bin //To program bootstrap bin file
  • sam-ba -p j-link -b sam9x60-ek -a qspiflash -c write:harmony.bin:0x40000 //Program the application in the QSPI offset 0x40000
    Note: Flash both boot.bin and harmony.bin to QSPI memory to execute the application from QSPI upon reset.

Steps to execute the harmony application from QSPI flash:

  • Press the reset button.
  • The user will get the debug message on the serial console as This application is getting executed from QSPI-offset 0x40000. Press the user button(sw1) and observe LED getting turned ON.

Comments

This application demo builds and works out of box 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.