Archive

Posts Tagged ‘KSDK’

KSDK USB CDC PRINTF

23/12/2015 18 comments

Previously I created a blog post on Printf to UART using Processor Expert, this is a great way to output debug info at runtime to a console using the OpenSDA USB debugger port as found for example on the FRDM K64F board. What if you don’t want to use this USB port or can’t because you don’t have one, for instance on a custom board perhaps. Well if your board provides a USB port that connects direct to the mpu’s USB pins this blog will help you configure all the Processor Expert components and code needed to output to that type of USB port. In this blog I will again use the FRDM K64F board but it has a second USB port that is directly connected to the USB pins of the of the MK64FN1M0VLL12 MCU. I am using KSDK 1.3.0 and Eclipse Neon but I will also try provide notes on how to do this in KSDK 1.2.0, if you want to know how to set up Eclipse you can follow the directions on my blog Toolchain: KSDK 1.2.0 with Eclipse 4.4 (Luna) and GNU ARM Plugin, although that blog covers an older version of Eclipse and KSDK, the steps work perfectly still as long as you download all the latest pieces of software mentioned in that blog.

PLEASE NOTE: I am not an expert with USB on Kinetis devices so I cannot provide any help on any other USB related projects such as Mass storage devices or HID devices. Please do not post comments/questions asking how to perform other types of USB projects.

 

FRDM_K64F_USB

 

  1. Ok to start, create a new Kinetis project.CreateProject_Name
  2. Select the board type. In my case I select the FRDM-K64F board.CreateProject_Board
  3. Select KSDK_1.3.0, Processor Expert and standalone.CreateProject_SDK
  4. Select “GNU C Compiler” and click Finish.CreateProject_Compiler
  5. Switch to Processor Expert perspective and add the component “fsl_usb_framework”. Note this will also automatically add the referenced components “Init_FMC” and “fsl_debug_console”.
    Note: In KSDK 1.2.0 the “Init_FMC” is not automatically added for you but you still add it manually then afterwards.PE_fsl_usb_framework
  6. If you get prompted on how you want to add the “Init_FMC” component, select “New component [KSDK 1.3.0/Init_FMC_VAR1]…”PE_fsl_usb_framework_sharedComp
  7. On the Components window select “usbFmw1:usb_framework” and set the properties as below. Its important that the USB clock is set to 48 MHz. Its also very important that MPU is disabled as the MPU protects the Bus from USB read/writes. Make sure the Mode is set to “DEVICE” then Enable “USBCFG_DEV_CDC” under the Device class driver section.
    Note: In KSDK 1.2.0 the option to disable the MPU is not available on this component, you will need to manually add the component “Init_MPU” and disable it from there.fsl_usb_framework_Properties
  8. Now select the component “FMC:Init_FMC”, this should be already set correctly for us but just note that the important bit here is that the access protection on Master 4 which protects USB memory access areas must be set to “Read only”.
    Note: in KSDK 1.2.0 this is not set for you, you must manually set Master 4 to Read only.Init_FMC_Properties
  9. Next select the “DbgCs1:fsl_debug_console” component, make sure the “Baud rate” is set to 115,200. It’s not important what Pins the Receiver and Transmitter are set to as this will get overridden in code when we initialise this component manually. VERY IMPORTANT, make sure the “Auto initialization” is disabled, as said, we will manually do this in code.fsl_debug_console_Properties
  10. Ok one last configuration then, select the “Cpu:MK64FN1MOVLL12” component (or whatever MPU you are using). Switch to the “Build Options” tab and make sure you set a Heap size because usb stack uses malloc which uses a function called _sbrk(). This function checks if there is heap memory available to allocate to the malloc. The FRDM K64F board has enough memory available for me to set the Heap size to its max on this screen of 0xFFFF, Also make sure you have at least 0x400 allocated to the Stack.
    Note: KSDK 1.2.0 has a bug and does not implement a _sbrk() function, you will need to create your own _sbrk() function to get this to work. See the end of this Blog on how to do this.Cpu_HeapSize
  11. Now generate code from Processor Expert.Components
  12. On the Project Explorer window, expand the folder \SDK\usb\usb_core\device\sources. Create a folder under the sources folder called “classes”.Import the following 3 folders into the classes folder from the path “C:\Freescale\KSDK_1.3.0\usb\usb_core\device\sources\classes”
    • cdc
    • common
    • include

    import_usb_classes

     

  13. We need to add these new folders into the includes. Open up the project properties and add the following 4 folders under “C/C++ Build >> Settings >> Cross ARM C Compiler >> Includes”.Project_Settings_includesOn the Project Explorer window again, Open up the file “\SDK\platform\utilities\fsl_debug_console.c”.  Note around line 49 the following #if block:
#if (defined(USB_INSTANCE_COUNT) &&  (defined(BOARD_USE_VIRTUALCOM)))
  #include "usb_device_config.h"
  #include "usb.h"
  #include "usb_device_stack_interface.h"
  #include "usb_descriptor.h"
  #include "virtual_com.h"
#endif

based on this section of code I can see that the code actually supports using USB and the use of the “VIRTUALCOM”. What is this “VIRTUALCOM”, well if you were to look at the code in the example KSDK project “C:\Freescale\KSDK_1.3.0\examples\twrk64f120m\demo_apps\usb\device\cdc\virtual_com\bm\kds” which is a project for demonstrating how to use USB CDC communication but without the use of the” fsl_debug” component or using Processor expert at all, then you will see that the “VIRTUALCOM” is the main piece of code written for sending and receiving messages over the USB CDC. Great, that means then that we can enable the “fsl_debug_console” to make use of it. So looking at this section of code in “fsl_debug_console.c”, it looks like all we need do is define a symbol for “BOARD_USE_VIRTUALCOM”.

  1. Open up the Properties of the project and under “C/C++ Build >> Settings >> Cross ARM C Compiler >> Preprocessor” add the symbol “BOARD_USE_VIRTUALCOM”. make sure to include the double quotes around the symbol.Project_Settings_Preprocessor
  2. Ok but now the code will complain that it can’t find the included files “usb_descriptor.h” and “virtual_com.h”, so where can this be found. Well actually you can find these files in the same directory where the “fsl_debug_console” files get imported from. So to fix this perform the following imports:import_virtual_com_headersimport_virtual_com_code

     

  3. One last thing to do is make sure we initialise the Debug Console component telling it to use the USB CDC. If you remember we intentionally disabled auto initialising the “fsl_debug_console” in processor expert. This was done for the purpose of providing us the ability to change the device used by the console. So at the start of your main function just after the call to “PE_low_level_init()”, make the call “DbgConsole_Init(BOARD_DEBUG_UART_INSTANCE, DEBUG_UART_BAUD, kDebugConsoleUSBCDC);”.

 

/* ###################################################################
**     Filename    : main.c
**     Project     : K64F_USB_CDC
**     Processor   : MK64FN1M0VLL12
**     Version     : Driver 01.01
**     Compiler    : GNU C Compiler
**     Date/Time   : 2015-12-12, 23:04, # CodeGen: 0
**     Abstract    :
**         Main module.
**         This module contains user's application code.
**     Settings    :
**     Contents    :
**         No public methods
**
** ###################################################################*/
/*!
** @file main.c
** @version 01.01
** @brief
**         Main module.
**         This module contains user's application code.
*/
/*!
**  @addtogroup main_module main module documentation
**  @{
*/
/* MODULE main */

/* Including needed modules to compile this module/procedure */
#include "Cpu.h"
#include "Events.h"
#include "clockMan1.h"
#include "pin_init.h"
#include "osa1.h"
#include "usbFmw1.h"
#include "DbgCs1.h"
#if CPU_INIT_CONFIG
  #include "Init_Config.h"
#endif
/* User includes (#include below this line is not maintained by Processor Expert) */

/*lint -save  -e970 Disable MISRA rule (6.3) checking. */
int main(void)
/*lint -restore Enable MISRA rule (6.3) checking. */
{
  /* Write your local variable definition here */

  /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
  PE_low_level_init();
  /*** End of Processor Expert internal initialization.                    ***/

  DbgConsole_Init(BOARD_DEBUG_UART_INSTANCE, DEBUG_UART_BAUD, kDebugConsoleUSBCDC);

  PRINTF("Hello CDC!");

  /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
  #ifdef PEX_RTOS_START
    PEX_RTOS_START();                  /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
  #endif
  /*** End of RTOS startup code.  ***/
  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
  for(;;){}
  /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

/* END main */
/*!
** @}
*/
/*
** ###################################################################
**
**     This file was created by Processor Expert 10.5 [05.21]
**     for the Freescale Kinetis series of microcontrollers.
**
** ###################################################################
*/

 

  1. If you try to compile the code now you will get an error saying the Symbol ‘usb_device_board_init’ could not be resolved within the file “virtual_com.c”. If you navigate to the line where this error is caused all that need to be done is to set the value to NULL instead:

 

cdc_config.board_init_callback.callback = NULL;

 

That’s it then, after that you can make “PRINTF”, “SCANF” or “GETCHAR” calls in your code and it will output to the OTG usb port on your board. Please note, you must capitalise the calls to the functions “PRINTF”, “SCANF” or “GETCHAR” as these are specialised versions of the functions that reside inside the “fsl_debug_console.c” file.

 

You can get a copy of this project on my Github repo at https://github.com/wesleyhunter/centaurian/tree/master/Examples/Eclipse/FRDM-K64F/K64F_USB_CDC

 

Good luck, hope this works well for you all.

 

KSDK 1.2.0 _sbrk() function

 

To implement a _sbrk() function simply add the following block of code to your project:

 

void *_sbrk ( uint32_t incr )
{
	extern char   end __asm ("end");
	extern char   heap_limit __asm ("__HeapLimit");
	static char * heap_end;
	char *        prev_heap_end;

	if (heap_end == NULL)
		heap_end = & end;

	prev_heap_end = heap_end;

	heap_end += incr;

  return (void *) prev_heap_end;
}
Advertisements

FRDM K64F Data Logger using FatFs with KSDK 1.2.0

15/07/2015 38 comments

This blog is a guide on how to setup using the FatFs library included with the Kinetis SDK 1.2.0 using mostly the Processor expert within Eclipse Luna. FatFs is a generic FAT file system module for small embedded systems written by Chan. I prefer to use Processor Expert when possible as this will often generate code that is smaller in size than using the KSDK library files direct. I experienced many gotcha’s and complication setting this up and wanted to share how I finally managed to get it to work.

  1. Prerequisites
  2. Create New Project
  3. Open Processor Expert
  4. Add SD card component
  5. Add Init_Port component
  6. Add GPIO component
  7. Configure the clock
  8. Import FatFs
  9. Add Card detection code
  10. FAT file appender function
  11. Disable the MPU

Prerequisites

Before you begin please make sure your development environment is setup and you can already compile and debug code using KSDK 1.2.0 on Eclipse Luna. If not please refer to my blog: Toolchain: KSDK 1.2.0 with Eclipse 4.4 (Luna) and GNU ARM Plugin

 

Create New Project

  1. Within Eclipse, on the top menu point your mouse over “File” menu item, this should expand out with an option to select “Kinetis Project”.

    NewKinetisProject

  2. Name your project, I tend to name my project starting with the type of development board or processor followed by an underscore and then purpose of the project, this allows me to create projects of the same purpose for other boards/processors. Set location of project, I use the default location under the workspace area. Click Next.

    NameProject

  3. Select the development board used, and click Next.

    DevelopmentBoard

  4. Select “KSDK 1.2.0”. Set location to KSDK path, I use an absolute path but you can select Environment variable if you have set up a path under Windows operating system environment variables with a name called “KSDK_PATH” that points to the location of the KSDK folder. The location of my KSDK is in the default location where KSDK would install to.
    Ensure “Processor Expert” checkbox is checked. The perspective radio button you select does not matter nor does the Project mode for this project. Click the Next button.

    SDKandPESelect

  5. Select “GNU C Compiler” and Click the “Finish” button.

    SelectGNUCompiler

    N.B. Wait a while for Processor Expert to finish generating the initial source for the project. Keep an eye on the progress when it reaches 100% by looking at the bottom right hand corner of Eclipse.

    GeneratingCode

Open Processor Expert

Before you proceed make sure you can build the project now without errors. If not then make sure you have followed the instructions and understood how to setup your Eclipse environment as specified in the blog Toolchain: KSDK 1.2.0 with Eclipse 4.4 (Luna) and GNU ARM Plugin, especially the “Setting Eclipse workspace preferences” section.

  1. Open the Processor Expert perspective, this should be a button on the top right hand side of Eclipse.

    OpenPEPerspective

    If it’s not there, try clicking on the “Open Perspective” button and then selecting “Processor Expert” in the form that appears.

    OpenPerspective

    AddPEPerspective

Add SD Card component

  1. Click on the “Components Library” tab, search for the component “fsl_sdcard” and double click on it.

    component_fsl_sdcard

  2. Select the “memoryCard1:fsl_sdcard” in the Components window.

    select_memoryCard1

  3. Click on the “Component Inspector” tab. Then select the Initialization tab. Untick the “Auto initialization” check box. This is very important as the FatFs component will perform this step for us instead. Actually even if you don’t use FatFs you would perform this step manually every time you wanted to open a file on the sd card.

    sdCard_disable_initialise

  4. Now back on the Components window, expand the “memoryCard1:fsl_sdcard” component and select the “fsl_sdhc1:fsl_sdhc” component.

    select_fsl_sdhc1

  5. Click on the “Component Inspector” tab. Then select the “Configurations” tab, then select “ADMA2” for the transfer mode. Ensure “GPIO” is selected for the Card detection type. The FRDM K64F board’s SD card reader is wired with a switch connected to PTE6 to detect when a card is inserted. The SD card reader itself can also detect card insertion but it is easier to setup and use the GPIO pin.

    Don’t worry that the Bus clock is set to 400 kHz, this ultimately gets geared up to 50 MHz when the fsl_sdcard component is initialised. You won’t see this code in this example but basically when the FatFs component opens a file it performs a mounting check on the SD card and within that check it initialises the fsl_sdcard where it under goes a gearing up of different speeds on the SD card, first at 100 kHz, then 400 kHz, then 25 MHz and finally 50 MHz.

    set_ADMA2

  6. Now switch to the “Pins” tab. Refer to the FRDM-K64F Freedom Module User’s Guide at section 7 “Secure digital card” and map the pins as specified. 

    fsl_sdhc_pins

  7. Now switch to the “Initialization” tab. We need to ensure we generate a handler for interrupt service routine as this gets used by the fsl_sdcard initialisation. We cannot add a handler unless we first enable “Auto initialisation” then tick the check box “Install interrupts”. There is no particular need to “Auto initialise” other than to be able to install the interrupt. I expect future versions of the KSDK will probably separate these 2 properties. Luckily there is no harm caused enabling the “Auto initialization” as the initialisation code of the fsl_sdcard routine first shuts down the fsl_sdhc and then re-initialises it.

    fsl_sdhc_initialization

Add Init_Port component

  1. Click on the “Components Library” tab, search for the component “Init_Port” and double click on it.

    component_Init_PORT

  2. Select the “PORTA:Init_PORT” in the Components window.

    select_Init_Port

  3. Click on the “Component Inspector” tab. Change Device to “PORTE”, Enable Clock gate, Initialize Pin 0, set Mux control to “SDHC01_D1”.

    For Pins 0 to 5 refer to section “3.9.7.2 SD bus pullup/pulldown constraints” within the K64 reference manual K64P144M120SF5RM, this section explains that the Pull resistor setting needs to be enabled and the Pull selection needs to “Pull Up”.

    I couldn’t find where its documented but I discovered that the SD card host cannot run at 50 MHz unless the slew rate is set to “Fast” and the Drive strength set to “High”.

    Init_PORT_Pin0_settings 

Add GPIO component

  1. Click on the “Components Library” tab, search for the component “fsl_gpio” and double click on it. 

    component_fsl_gpio

  2. Select the “gpio1:fsl_gpio” in the Components window. 

    select_gpio1

  3. Click on the “Component Inspector” tab. Select the Input pins tab. Under the Input configuration 0 section change the Pin to “SD_CARD_DETECT”  which is also known as the PTE6 pin. We need to detect when the SD card is inserted so we will need an interrupt on this pin, select “Interrupt on either edge” under the Interrupt/DMA section. Refer to the FRDM-K64F Freedom Module User’s Guide at section 7 “Secure digital card”. In this section it states that the pin shorts with VDD when the card is inserted. Thus we must ensure the Pin is pulled down. Tick the Pull enable check box and set the Pull select direction to “Down”. Untick the Passive filter and Digital filter.

    fsl_gpio_Input

  4. Select the “Output pins” tab. Untick the Output pins check box. We do not need to configure any output pins as GPIO.

    fsl_gpio_Output

  5. Select the “Initialization” tab, Ensure “Auto initialization” and “Input pins” check box is ticked. Untick the “Output pins” check box. Now notice here under the Interrupts section that the interrupt for PORTE is disabled. See the next step to enable it.

    fsl_gpio_Init1

  6. To enable the PORTE interrupt, select the “Events” tab and then select “generate code” next to PORTE IRQ handler. If you switch back to the Properties tab you should see the PORTE interrupt is now enabled.

    fsl_gpio_Events 

Configure the clock

In order to get the maximum speed of 50 MHz on the SD card, we need to ensure the host system clock is running faster than this. For this reason I recommend configuring the system clock to run at 100 MHz.

  1. Select the “clockMan1:fsl_clock_manager” in the Components window.

    select_clockMan1

  2. Click on the “Component Inspector” tab. Under it select Initialization tab. Select “clockMan1_InitConfig5” in the drop down field “Init clock configuration”.

    fsl_clock_manager_Init

  3. If you don’t have the default clock configurations then make sure you configure a clock source with the details below.

    fsl_clock_manager_Sources
    fsl_clock_manager_InternalRef
    fsl_clock_manager_ExternalRef
    fsl_clock_manager_MCG
    fsl_clock_manager_SystemClocks

    With this configuration you can see the Core clock will run at 100 MHz.

Import FatFs

  1. At this point we have done all we can within Processor Expert. Make sure you save all on the project at this point then on the Components window, click on the Generate code button.

    GenerateCode

  2. Build the project and make sure there are no errors.
  3. Right click on the project in the project explorer and Add a New Folder. Name the folder “Middleware”.

    NewFolder

    NewFolder_Middleware

  4. Right click on the Middleware folder within the Project Explorer window. Select Import.

    Import

  5. Under the General folder select “File System” and click Next.

    Import_FileSystem

  6. Click the “Browse…” button and navigate to where you have installed KSDK 1.2.0. Under the KSDK folder navigate into the “middleware folder, then the filesystem folder and then select the fatfs folder and click the OK button.

    Import_Browse

  7. Now don’t import the whole FatFs directory, this will not work. Instead select the files as follows:

    Import_FatFs1

    Import_FatFs2

    Import_FatFs3

  8. Now we need to add all the header files from this library to the project. Right click on the project in Project Explorer and select properties. Then expand the C/C++ Build folder in the left part of the window and select “Settings”. On the right side select “Includes” under the “Cross ARM C Compiler” section. Now click on the Add button and add the directories "${ProjDirPath}/Middleware/fatfs" and "${ProjDirPath}/Middleware/fatfs/fsl_sd_disk"

    Includes_FatFs1

    Includes_FatFs2 

  9. The FatFs library needs to be told how you are detecting the SD card has been inserted, it also wants to know if you are using USB or SD card for storage. You need to add Preprocessor symbols to resolve this. On the Project properties window again under “C/C++ Build”-> “Settings”->”Cross ARM C Compiler” but this time select “Preprocessor”. Click the Add button and add the symbol “CD_USING_GPIO” and also “SD_DISK_ENABLE=1”

    Preprocessor_CD_USING_GPIO

    Preprocessor_SD_DISK_ENABLE

Add Card detection code

Look for the file Events.h under the sources folder in your project. Notice this file contains a interrupt handler for Port E. We need to use this for detecting when the SD card is inserted. In this file Add 2 more functions that we will need, check_card_inserted() and wait_for_card().

/* ###################################################################
**     Filename    : Events.h
**     Project     : K64F_DataLogger
**     Processor   : MK64FN1M0VLL12
**     Component   : Events
**     Version     : Driver 01.00
**     Compiler    : GNU C Compiler
**     Date/Time   : 2015-06-26, 18:12, # CodeGen: 0
**     Abstract    :
**         This is user's event module.
**         Put your event handler code here.
**     Settings    :
**     Contents    :
**         No public methods
**
** ###################################################################*/
/*!
** @file Events.h
** @version 01.00
** @brief
**         This is user's event module.
**         Put your event handler code here.
*/         
/*!
**  @addtogroup Events_module Events module documentation
**  @{
*/         

#ifndef __Events_H
#define __Events_H
/* MODULE Events */

#include "fsl_device_registers.h"
#include "clockMan1.h"
#include "pin_init.h"
#include "osa1.h"
#include "memoryCard1.h"
#include "fsl_sdhc1.h"
#include "gpio1.h"

#ifdef __cplusplus
extern "C" {
#endif 


void PORTE_IRQHandler(void);

void fsl_sdhc1_OnCardDetect0(bool inserted);

void fsl_sdhc1_OnCardInterrupt0(void);

void fsl_sdhc1_OnCardBlockGap0(void);

void check_card_inserted(void);
void wait_for_card(void);

/* END Events */

#ifdef __cplusplus
}  /* extern "C" */
#endif 

#endif 
/* ifndef __Events_H*/
/*!
** @}
*/
/*
** ###################################################################
**
**     This file was created by Processor Expert 10.5 [05.21]
**     for the Freescale Kinetis series of microcontrollers.
**
** ###################################################################
*/

 

Now update the Events.c file to look like the following:

/* ###################################################################
 **     Filename    : Events.c
 **     Project     : K64F_DataLogger
 **     Processor   : MK64FN1M0VLL12
 **     Component   : Events
 **     Version     : Driver 01.00
 **     Compiler    : GNU C Compiler
 **     Date/Time   : 2015-06-26, 18:12, # CodeGen: 0
 **     Abstract    :
 **         This is user's event module.
 **         Put your event handler code here.
 **     Settings    :
 **     Contents    :
 **         No public methods
 **
 ** ###################################################################*/
/*!
 ** @file Events.c
 ** @version 01.00
 ** @brief
 **         This is user's event module.
 **         Put your event handler code here.
 */
/*!
 **  @addtogroup Events_module Events module documentation
 **  @{
 */
/* MODULE Events */

#include "Cpu.h"
#include "Events.h"

#ifdef __cplusplus
extern "C" {
#endif 


/* User includes (#include below this line is not maintained by Processor Expert) */

static volatile bool cardInserted = false; /* Flag to indicate a card has been inserted */

void PORTE_IRQHandler(void)
{
	if(PORT_HAL_GetPortIntFlag(PORTE_BASE_PTR) == (1<<SD_CARD_DETECT))
	{
		check_card_inserted();
	}

	PORT_HAL_ClearPortIntFlag(PORTE_BASE_PTR);
}

void fsl_sdhc1_OnCardDetect0(bool inserted)
{
	/* Write your code here ... */
}

void fsl_sdhc1_OnCardInterrupt0(void)
{
	/* Write your code here ... */
}

void fsl_sdhc1_OnCardBlockGap0(void)
{
	/* Write your code here ... */
}

void check_card_inserted(void)
{
	uint32_t state = GPIO_DRV_ReadPinInput(SD_CARD_DETECT);
	uint32_t matchState = -1;

	// Debounce input
	do
	{
		for (int i = 0; i < 0x1FFFFF; i++)
		{
			__asm("nop");
		}
		matchState = state;
		state = GPIO_DRV_ReadPinInput(SD_CARD_DETECT);
	}
	while (state != matchState);

	// Set card state
	cardInserted = (state == 1);

	fsl_sdhc1_OnCardDetect0(cardInserted);
}

void wait_for_card(void)
{
	while ((!cardInserted));
}

/* END Events */

#ifdef __cplusplus
}  /* extern "C" */
#endif 

/*!
 ** @}
 */
/*
 ** ###################################################################
 **
 **     This file was created by Processor Expert 10.5 [05.21]
 **     for the Freescale Kinetis series of microcontrollers.
 **
 ** ###################################################################
 */

Here we are using the Port E interrupt handler function called PORTE_IRQHandler to check if an interrupt happened on the SD card detect pin (Port E Pin 6). If so then we check the pin to see if the card was inserted or removed by using the function check_card_inserted. after checking the card the handler must ensure the interrupt flag is reset to handle further interrupts.

The check_card_inserted function reads the state on SD card detect pin, it then performs a process of repeatedly reading the state of this pin after a short delayed period until previous reads and current reads match. Once they match then it should mean that the pin has stopped bouncing and the final state value is the true current state. We then store this state as a boolean type in the variable cardInserted. Later in the main.c file we will call into the function wait_for_card where we read and wait for the value on the cardInserted to equal true at which point we know the card is inserted and can perform operations on the card.

 

FAT file appender function

Update the main.c file to look like the following:

/* ###################################################################
 **     Filename    : main.c
 **     Project     : K64F_DataLogger
 **     Processor   : MK64FN1M0VLL12
 **     Version     : Driver 01.01
 **     Compiler    : GNU C Compiler
 **     Date/Time   : 2015-06-26, 18:12, # CodeGen: 0
 **     Abstract    :
 **         Main module.
 **         This module contains user's application code.
 **     Settings    :
 **     Contents    :
 **         No public methods
 **
 ** ###################################################################*/
/*!
 ** @file main.c
 ** @version 01.01
 ** @brief
 **         Main module.
 **         This module contains user's application code.
 */
/*!
 **  @addtogroup main_module main module documentation
 **  @{
 */
/* MODULE main */


/* Including needed modules to compile this module/procedure */
#include "Cpu.h"
#include "Events.h"
#include "clockMan1.h"
#include "pin_init.h"
#include "osa1.h"
#include "memoryCard1.h"
#include "fsl_sdhc1.h"
#include "gpio1.h"
#if CPU_INIT_CONFIG
#include "Init_Config.h"
#endif
/* User includes (#include below this line is not maintained by Processor Expert) */

#include <string.h>
#include "ff.h"

#define		SD_DRIVE_NUMBER		1

void append(char *fileNameAndPath, char *text)
{
	FATFS fileSystem;

	// mount SD card
	FRESULT fResult = f_mount(SD_DRIVE_NUMBER, &fileSystem);

	if (fResult == FR_OK)
	{
		fResult = f_chdrive(SD_DRIVE_NUMBER);

		if (fResult == FR_OK)
		{
			FIL file;
			static char pathWithDriveLetter[255];
			sprintf(pathWithDriveLetter, "%u:%s", SD_DRIVE_NUMBER, fileNameAndPath);

			/* Open a text file */
			fResult = f_open(&file, pathWithDriveLetter, FA_OPEN_ALWAYS | FA_WRITE);
			if (fResult == FR_OK)
			{
				/* Move to end of the file to append data */
				fResult = f_lseek(&file, file.fsize);

				/* Write to the file */
				uint32_t bytesWritten;
				fResult = f_write(&file, text, strlen(text), &bytesWritten);
			}
			/* Close the file */
			fResult = f_close(&file);
		}

		// unmount SD card
		fResult = f_mount(SD_DRIVE_NUMBER, NULL);
	}
}

/*lint -save  -e970 Disable MISRA rule (6.3) checking. */
int main(void)
/*lint -restore Enable MISRA rule (6.3) checking. */
{
	/* Write your local variable definition here */

	/*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
	PE_low_level_init();
	/*** End of Processor Expert internal initialization.                    ***/

	// Check card is already inserted and don't wait for interrupt
	check_card_inserted();

	// If card is not inserted then we must wait till it is inserted
	wait_for_card();

	append("/data.log", "test data");

	/*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
  #ifdef PEX_RTOS_START
    PEX_RTOS_START();                  /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
  #endif
  /*** End of RTOS startup code.  ***/
  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
  for(;;){}
  /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

/* END main */
/*!
 ** @}
 */
/*
 ** ###################################################################
 **
 **     This file was created by Processor Expert 10.5 [05.21]
 **     for the Freescale Kinetis series of microcontrollers.
 **
 ** ###################################################################
 */

 

Here in the main function we make use of the check_card_inserted and wait_for_card function we created in the Events.c file. Once the SD card is inserted we call a new function added to this main.c file called append. The append function’s responsibility is to mount the SD card, open a file at a given path on the SD card, if the file does not exist it will create it, then it will move to the end of the file and append some given text to the file. It will then close the file and unmount the SD card so that the append function can repeatedly be called.

 

Disable the MPU

If you place a breakpoint on line 68 in the main.c file just after where it calls the f_open function then compile, run the application and observer the value of the fResult, then you will see it failed to open the file and returned a value of FR_NOT_READY. The reason this happens is because the Memory Protection Unit (MPU) is enabled by default on the K64 controller and is preventing bus master accesses to the SDHC memory areas. Configuring the MPU areas is a blog for another day but it is safe to simply disable the MPU.

  1. Open the Processor Expert perspective again. Click on the “Components Library” tab, search for the component “Init_MPU” and double click on it.  

    component_Init_MPU

  2. Select the “MPU:Init_MPU” in the Components window.  

    select_Init_MPU

  3. Click on the “Component Inspector” tab. Enable the clock gate and untick the "MPU module" checkbox. That will disable the MPU.

    Init_MPU_disable

  4. click on the Generate code button.

    GenerateCode 

  5. Build the project and make sure there are no errors.

You should now be able to run the application successfully. Once you have run it, eject the SD card and insert it into your computer. You should now see a file on the SD card called "data.log". If you open the file it should contain one or more lines of text saying "test data".

 

Well done if you managed to follow along and get it all working, I spent many days working this one out so I hope this blog helps others. As a bonus you may want to try figure out how to add a real time clock to your project and use it within the FatFs library. If you look at what the last modified date is of data.log file, then you will notice it always equals 1 Jan 2010 no matter how many times you run the application or when you run the application. This is because the kinetis FatFs library hard codes it to this value. To fix it you can add a RTC component to your project and then search for the function "get_fattime" within diskio.c file and replace the logic there to make use of your RTC component.

FRDM K64F Printf to UART using Processor Expert

05/06/2015 4 comments

If you are using the OpenSDAv2 USB for debugging with Segger J-Link on the FRDM K64F development board within Eclipse then you may want to route Printf functions to the Segger virtual com port. This blog will cover how to set this up on Windows using Processor Expert on KSDK 1.2.0 and Eclipse Luna.

UARTPort

  1. Prerequisites
  2. Create New Project
  3. Configure Processor Expert
  4. Hello World Test Code
  5. View output using COM port terminal application
  6. Clock Configuration

Prerequisites

Before you begin please make sure your development environment is setup and you can already compile and debug code using KSDK 1.2.0 on Eclipse Luna. If not please refer to my blog: Toolchain: KSDK 1.2.0 with Eclipse 4.4 (Luna) and GNU ARM Plugin

Create New Project

  1. Within Eclipse, on the top menu point your mouse over “File” menu item, this should expand out with an option to select “Kinetis Project”.NewKinetisProject
  2. Name your project, I tend to name my project starting with the type of development board or processor followed by an underscore and then purpose of the project, this allows me to create projects of the same purpose for other boards/processors. Set location of project, I use the default location under the workspace area. Click Next.NameProject
  3. Select the development board used, and click Next.DevelopmentBoard
  4. Select “KSDK 1.2.0”.
    Set location to KSDK path, I use an absolute path but you can select Environment variable if you have set up a path under Windows operating system environment variables with a name called “KSDK_PATH” that points to the location of the KSDK folder. The location of my KSDK is in the default location where KSDK would install to.
    Ensure “Processor Expert” checkbox is checked. The perspective radio button you select does not matter nor does the Project mode for this project.
    Click the Next button.SDKandPESelect
  5. Select “GNU C Compiler” and Click the “Finish” button.SelectGNUCompilerN.B. Wait a while for Processor Expert to finish generating the initial source for the project. Keep an eye on the progress when it reaches 100% by looking at the bottom right hand corner of Eclipse.GeneratingCode

Configure Processor Expert

Before you proceed make sure you can build the project now without errors. If not then make sure you have followed the instructions and understood how to setup your Eclipse environment as specified in the blog Toolchain: KSDK 1.2.0 with Eclipse 4.4 (Luna) and GNU ARM Plugin, especially the “Setting Eclipse workspace preferences” section.

  1. Open the Processor Expert perspective, this should be a button on the top right hand side of Eclipse.OpenPEPerspectiveIf it’s not there, try clicking on the “Open Perspective” button and then selecting “Processor Expert” in the form that appears.OpenPerspective

    AddPEPerspective

  2. Click on the “Components Library” tab, search for the component “fsl_debug_console” and double click on it.addcomponent_debugConsole
  3. Select the “DbgCs1:fsl_debug_console” in the Components windowComponent_DbgCs1
  4. Click on the “Component Inspector” tab. Then click on the three ellipses “…” button next to the Baud rate.Component_DbgCs1_BaudRate_ElipsesButton
  5. Double click on the baud rate “115200” on the right hand form and click the “OK” button.SetBaudRateNote: Note that “Clock cfg. 4” is Enabled above. We will review these settings further down in “Clock Configuration”.
  6. On the Pin Settings tab, the FRDM K64F board uses Port B 16 (PTB16) for RxD and Port B 17 (PTB17) for TxD. Make sure these are set correctly.  You can refer to the FRDM-K64F Freedom Module User’s Guide at section “11 Serial port” for these pin settings.PinSetting
  7. Make sure all component settings are saved and click on the “Generate Processor Expert Code” button within the Components window.GenerateCode

Hello World Test Code

Make sure your project can still build without errors at this point. Then open the “main.c” file, search for the text that says “/* Write your code here */” and “/* For example: for(;;) { } */” and just below it write the following code:

PRINTF(“Hello World”);

PrintF

Note: PRINTF should intentionally be uppercase. This will use a printf function supplied with the KSDK.
That’s all there is to it Smile
Build your project again, it should compile without Errors.

View output using COM port terminal application

To be able to view the output of the PRINTF statement you will need a serial port terminal application. There are several good ones available for Windows but one I like to use which has other useful features available is an application called “PuTTY”. You can download the latest copy at: http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

PuttyDownload

You are going to need the COM Port used by your device. You can find out what yours is by opening the Windows “Device Manager” and expanding the “Ports (COM & LPT)” section. Look for the device named “JLink CDC UART Port” and in brackets you can see the COM port used.

UARTPort

After downloading the PuTTY file, simply run the downloaded file. Select the “Serial” radio button, in the “Serial line” text box type the COM port used by your JLink CDC UART Port, in my case its “COM3”.

Putty

One set, click the Open button. You should see a blank terminal window.

PuttyTerminal

Now go back to Eclipse and run the application, you should now see the following in the PuTTY terminal window:

PuttyHello

Clock Configuration

Congratulations if all is working well for you now. I wanted to point out what the clock configuration is that gets used. This is helpful to know if for some reason you do not have or are using the default clock settings.

If you refer to section “52.4.3 Baud rate generation” within the K64 reference manual K64P144M120SF5RM, we can see the Baud rate is a calculation of the following:

UART baud rate = UART module clock / (16 × (SBR[12:0] + BRFD))

  • UART module clock: is one of 5 clock configuration sources we can choose from set on the clock manager.
  • SBR: is a module clock divisor we use that can be a number between 1 and 8191.
  • BRFD: is a fractional fine-adjust counter that adds fractional delays to the baud rate clock to allow fine trimming of the baud rate to match the system baud rate. Basically any fraction by 32 bits (n/32).

Lets take a look at the clock manager to see what the settings are in this example. Go into the Processor Expert perspective. Select the “clockMan1:fsl_clock_manager” component in the Components window.

Component_ClockMan1

In the Component Inspector window, select the “Clock settings tab” then the “Clock sources” tab. Take note of the clock frequency  under the System oscillator 0 section.

ClockSources_SystemOscillator

Now click on the “Clock configuration” tab then select the “MCG settings” tab. Now select the “clockMan1_InitConfig4” configuration which is the one we used for the “DbgCs1:fsl_debug_console” configuration. Note we are using System oscillator 0 here and the MCG mode is “PEE” which uses PLL settings, so looking at the PLL settings we can see 48 MHz is entered as the desired output clock.

MCGSettings

Now select the tab “System clocks” tab. If we refer to section “5.7.10 UART clocking” within the K64 reference manual K64P144M120SF5RM, we can see that UART0 uses the core/system clock which should be set to 48 MHz.

SystemClocks

Ok, so coming back to the baud rate formula:

UART baud rate = UART module clock / (16 × (SBR[12:0] + BRFD))

    115,200 Hz = 48 MHz (or 48,000,000 Hz) / (16 × (SBR[12:0] + BRFD))

16 × (SBR[12:0] + BRFD) = 48,000,000 Hz / 115,200 Hz

16 × (SBR[12:0] + BRFD) = 416.6666666666667

(SBR[12:0] + BRFD) = 416.6666666666667 / 16

(SBR[12:0] + BRFD) = 26.04166666666667

So therefore SBR can equal 26

BRFD can be calculated as 0.04166666666667 * 32 = 1.333333333333333 but must be rounded to a whole number thus = 1.

 

Ok so maybe that’s a lot of detail on the calculation but I hope this helps when you are trying to use different clock settings.

Toolchain: KSDK 1.2.0 with Eclipse 4.4 (Luna) and GNU ARM Plugin

22/05/2015 9 comments

I’m excited to see the recent release of version 1.2.0 of the Software Development Kit for Kinetis MCUs. This now means we can use Eclipse Luna with it. If you are like me who recently started developing on ARM Kinetis devices then you might be pulling out your hair trying to find information on how to get any of it to work, including the basics like how to install the Kinetis SDK on a IDE that won’t break the bank like Eclipse. I prefer to use Eclipse over Kinetis Design Studio because I want to develop applications for other MCU providers like STMicroelectronics or Texus Instruments. I started with KSDK 1.1 which only worked on Eclipse Kepler which I found out the hard way. Its important to note that this post may only work on Eclipse Luna and not future versions. The only post at the time I could find that explained how to set all this up was using KSDK 1.0 (Constructing a Classroom IDE with Eclipse for ARM), its a good post that was vital to understand but it needs an update and that’s what I’m attempting to do here.

Overview:

  1. Install Java Runtime Environment (JRE)
  2. Install Eclipse 4.4 (Luna)
  3. Install Kinetis Processor Expert Software
  4. Install Kinetis SDK 1.2.0
  5. Install GCC for ARM Embedded Processors
  6. Build tools (make & rm) for Windows
  7. Install GNU ARM Eclipse Plugins
  8. Setting Eclipse workspace preferences
  9. Other recommended tools

Install Java Runtime Environment (JRE)

Eclipse is a Java based application and requires that you either have Java Runtime Environment installed (JRE) or Java Development Kit installed (JDK). If you already have one of these installed then you can skip to the next section. This blog assumes you are not doing any Java development so the following steps cover installing a version of JRE that can be used with the version of Eclipse installed.

Step 1. Download the JRE from: http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html, download the exe version instead of the tar.gz

Step 2. Run the downloaded exe.

Step 3. A welcome screen is displayed, click the Install button.

Step 4. It should install and once complete it should display a message saying “You have successfully installed Java”. Click the close button.

 

Install Eclipse 4.4 (Luna)

Step 1. Download Eclipse (Luna) from: http://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/lunasr2

Step 2. Extract the downloaded zip file. You can extract Eclipse to any folder location and it will still work but I prefer to extract it to “C:\Program Files” where most other applications reside. This applies for both x64 and x86 versions of Windows. After the extract Program files should now contain a folder called eclipse and the path to eclipse.exe should now be at “C:\Program Files\eclipse\eclipse.exe”.

Step 3. At this point I like to create a shortcut to the exe. Open up file explorer, navigate to the path “C:\Program Files\eclipse\”, right mouse click on eclipse.exe and select “Pin to start” or “Pin to taskbar” or both.

Step 4. If you created a shortcut, then click on the shortcut to run Eclipse, otherwise open file explorer, navigate to “C:\Program Files\eclipse\” and run the eclipse.exe.

Step 5. Set a workspace area where all your projects will be saved and click OK button.

 

Install Kinetis Processor Expert Software

KSDK 1.2.0 on Eclipse Luna requires PEx-KINETIS-v3.0-PLUGIN-ECLIPSE_4.4 (REV 3.0.0) is installed.

Step 1. Download PEx-KINETIS-v3.0-PLUGIN-ECLIPSE_4.4 (REV 3.0.0) from: https://www.freescale.com/webapp/Download?colCode=PEx-KINETIS-PLUGIN-ECLIPSE_4-4&appType=license&location=null&fpsp=1&WT_TYPE=Embedded%20Components&WT_VENDOR=FREESCALE&WT_FILE_FORMAT=zip&WT_ASSET=Downloads&fileExt=.zip&Parent_nodeId=1310405628018705856814&Parent_pageType=product

You may have to sign into the freescale site first or register for an account on the freescale site.

You may be asked to accept terms and conditions on the download page, click on the accept button to begin the download.

Step 2. Unzip the downloaded file “PEx_for_Kinetis_3.0.0_Install_into_Eclipse_4.4_Unzip_me.zip” to any location.

Step 3. Open Eclipse, in the top menu select “Help” and then “Install New Software…”

Eclipse_InstallNewSoftware

Step 4. Click the “Add” button next to the “Work with:” drop down box.

Eclipse_InstallNewSoftware_Add

Step 5. Click the “Archive…” button

Eclipse_AddRepository

Step 6. Navigate to the folder where you unzipped the file “PEx_for_Kinetis_3.0.0_Install_into_Eclipse_4.4_Unzip_me.zip” to. Select the file “freescale_updater.zip” and click on the “Open” button.

Eclipse_AddRepository_Archive_FreescaleUpdater

Step 7. Fill in the name field, it can be anything but I use “PE Freescale updater”. Click the “OK” button.

Eclipse_AddRepository_FreescaleUpdater

Step 8. Back on the Install Form, check the checkbox next to “Eclipse Freescale Solutions Category”. Click Next.

Eclipse_InstallNewSoftware_FreescaleSolutionsCategory

Step 9. Click Next again.

Eclipse_InstallNewSoftware_FreescaleSolutionsCategory_Review

Step 10. Check the radio button “I accept the terms of the licence agreements”. Click Finish.

Eclipse_InstallNewSoftware_FreescaleSolutionsCategory_Accept

Step 11. It may then ask you if you trust a certificate. Click on the “Select All” button and then click the “OK”.

Eclipse_InstallNewSoftware_FreescaleSolutionsCategory_Certificate

Step 12. When the installation finishes, it will ask you to restart Eclipse. Click on the “Yes” button.

 

Step 13. Wait for eclipse to restart, if it does not restart for any reason, make sure it has closed, then reopen it. Then in the top menu select “Help” and then “Install New Software…” again.

Step 14. Click the “Add” button next to the “Work with:” drop down box again.

Step 15. Click the “Archive…” button again

 

Step 16. Navigate to the folder where you unzipped the file “PEx_for_Kinetis_3.0.0_Install_into_Eclipse_4.4_Unzip_me.zip” to again. This time select the file “PEx_for_Kinetis_3.0.0.zip” and click on the “Open” button.

Eclipse_AddRepository_Archive_PEx

Step 17. Fill in the name field, it can be anything but I use “PEx for Kinetis 3.0.0”. Click the “OK” button.

Eclipse_AddRepository_PEx

Step 18. Back on the Install Form, check the checkbox next to “Processor Expert for Kinetis 3.0.0”. Click Next.

Eclipse_InstallNewSoftware_PEx

Step 19. Click Next again.

Eclipse_InstallNewSoftware_PEx_Review

Step 20. Check the radio button “I accept the terms of the licence agreements”. Click Finish.

Eclipse_InstallNewSoftware_PEx_Accept

Step 21. When the installation finishes, it will ask you to restart Eclipse. Click on the “Yes” button.

 

Install Kinetis SDK 1.2.0

Step 1. Download PEx-KINETIS-v3.0-PLUGIN-ECLIPSE_4.4 (REV 3.0.0) from: https://freescaleesd.flexnetoperations.com/337170/397/8806397/Kinetis%20SDK%201.2.0%20Mainline%20-%20Windows.exe?ftpRequestID=3493503477&server=freescaleesd.flexnetoperations.com&dtm=DTM20150514175928NDgxNTUxODcz&authparam=1431651568_352fe3f0cafc24ce5ea2906ae405bc9b&ext=.exe

You may be asked to accept terms and conditions on the download page, click on the accept button to begin the download.

Step 2. Run the downloaded file “Kinetis SDK 1.2.0 Mainline – Windows.exe”

Step 3. Click the “Yes” button on the do you want to install the KSDK 1.2.0 form.

KSDK_WantToInstall

Step 4. I’d leave the installation path as default and click on the “OK” button. N.B. Take note of the path, you will need to know this path for workspace and project settings which I explain further down in this blog.

KSDK_Path

Step 5. This will now extract files to the folder path you provided. Once installation is complete, click on the “OK” button.

KSDK_Complete

Step 6. Now we need to install the SDK into Eclipse, Open up Eclipse, in the top menu select “Help” and then “Install New Software…”

 

Step 7. Click the “Add” button next to the “Work with:” drop down box.

 

Step 8. Click the “Archive…” button

 

Step 9. Navigate to the folder where you installed the KSDK to. If you used the default path then this would be “C:\Freescale\KSDK_1.2.0\”. Go into the “Tools” folder and then into the “eclipse_update” folder. The end path you should have navigated may look something like “C:\Freescale\KSDK_1.2.0\tools\eclipse_update”. Select the file “KSDK_1.2.0_Eclipse_Update.zip” and click on the “Open” button.

Eclipse_AddRepository_Archive_KSDK

Step 10. Fill in the name field, it can be anything but I use “KSDK 1.2.0”. Click the “OK” button.

Eclipse_AddRepository_KSDK

Step 11. Back on the Install Form, check the checkbox next to “KSDK 1.2.0 Eclipse Update”. Click Next.

Eclipse_InstallNewSoftware_KSDK

Step 12. Click Next again.

Eclipse_InstallNewSoftware_KSDK_Review

Step 13. Check the radio button “I accept the terms of the licence agreements”. Click Finish.

Eclipse_InstallNewSoftware_KSDK_Accept

Step 14. When the installation finishes, it will ask you to restart Eclipse. Click on the “Yes” button.

Once it has restarted you should see Processor Expert update its list of supported processors.

At this point you are able to create Kinetis projects in Eclipse but you will need a cross platform compiler to be able to compile your code for your MCU. The next 3 sections will show you how to set this up.

 

Install GCC for ARM Embedded Processors

Step 1. In your web browser navigate to the url: https://launchpad.net/gcc-arm-embedded. Download the file with the “.exe” extension.

GCCARM_Download

Step 2. Run the downloaded file and click the “OK” button.

GCCARM_Language

Step 3. Click the “Yes” button to continue.

GCCARM_Confirm

Step 4. Click the “Next” button.

GCCARM_Welcome

Step 4. Accept the licence agreement and click the “Next” button.

GCCARM_Accept

Step 5. The next screen is where the path to the ARM GCC can be installed to. You can install this to any folder you prefer but I leave this as default. N.B. Take note of the path where you are installing to. You will need this to set preferences within Eclipse.

GCCARM_Path

Step 6. Click the “Next” button.

GCCARM_Review

Step 7. Once installation is complete and you get the Installation Wizard Complete form, make sure the checkbox “Add path to environment variable” is NOT checked. You can uncheck the other checkboxes as well and click the “Finish” button.

GCCARM_Complete

 

Build tools (make & rm) for Windows

The following package installs additional tools for building needed on Windows “make.exe” and “rm.exe”. On Linux and OS X these tools are part of the standard distribution of the operating system. The package is supplied by the “GNU ARM Eclipse” project which provides some great tools for ARM development using Eclipse.

Step 1. Download the latest version of the file gnuarmeclipse-build-tools-win32-2.*-*-setup.exe at http://sourceforge.net/projects/gnuarmeclipse/files/Build%20Tools/

GNUARMEclipsePlugins_Download

Step 2. Run the downloaded file and click the “OK” button.

GNUARMEclipsePlugins_Language

Step 3. Click the “Next” button.

GNUARMEclipsePlugins_Welcome

Step 4. Click on the “I Agree” button.

GNUARMEclipsePlugins_Accept

Step 5. Click the “Next” button.

GNUARMEclipsePlugins_Choose

Step 6. The next screen is where the path to the build tools can be installed to. You can install this to any folder you prefer but I leave this as default. N.B. Take note of the path where you are installing to. You will need this to set preferences within Eclipse. Click on the “Install” button.

GNUARMEclipsePlugins_Path

Step 7. Click the “Finish” button.

GNUARMEclipsePlugins_Complete

 

Install GNU ARM Eclipse Plugins

Now we need to add a plugin into Eclipse to make the tools installed available in Eclipse. We are going to add a plugin also supplied by the “GNU ARM Eclipse” project.

Step 1. Open Eclipse if not already open. In the top menu select “Help” and then “Install New Software…”

Step 2. Click the “Add” button next to the “Work with:” drop down box.

Step 3. This time do NOT click the archive button, simply fill in the name field with “GNU ARM Eclipse Plug-ins”, in the value field fill in “http://gnuarmeclipse.sourceforge.net/updates” and click the “OK” button.

Eclipse_AddRepository_GNUPlugins

Step 4. Back on the Install Form, check the checkbox next to “GNU ARM C/C++ Cross Development Tools”. Click Next.

Eclipse_InstallNewSoftware_GNUPlugins

Step 9. Click Next again.

Eclipse_InstallNewSoftware_GNUPlugins_Review

Step 10. Check the radio button “I accept the terms of the licence agreements”. Click Finish.

Eclipse_InstallNewSoftware_GNUPlugins_Accept

Step 11. It may then ask you if you trust a certificate. Click on the “OK” button.

Eclipse_InstallNewSoftware_GNUPlugins_Certificate

Step 12. When the installation finishes, it will ask you to restart Eclipse. Click on the “Yes” button.

 

Setting Eclipse workspace preferences

At this point we will set Eclipse preferences to map to all the tools installed. Open up Eclipse if its not open already.

1. Set Global Tools Paths: On top menu within Eclipse, click “Window”, then select “Preferences”. In Preferences form on the left hand menu expand “C/C++”, then “Build”, then “Global Tools Paths”. Make sure the field “Build tools folder” is set to point to the bin folder under where you installed Build tools (make & rm) for Windows. If you used the default then this would be “C:/Program Files (x86)/GNU ARM Eclipse/Build Tools/2.4-201503242026/bin”. Make sure the field “Default toolchain” is set to “GNU Tools for ARM Embedded Processors”. Make sure the field “Toolchain folder” is set to the bin folder under where you installed GCC for ARM Embedded Processors. If you kept the default then this would be “C:\Program Files (x86)\GNU Tools ARM Embedded\4.9 2015q1\bin”. Note, you could try clicking on the “Restore Defaults” button which may set it to these values for you.

Eclipse_Preferences_GlobalToolsPaths

This gets used on project settings under build settings, Toolchains:

Eclipse_Project_Properties_ToolChains

2. Set Linked Resource KSDK_LOC: In the preferences form on the left hand side menu expand “General”, then “Workspace”, then select “Linked Resources”. Add a new variable with the name “KSDK_LOC” and the value pointing to the folder where you installed Kinetis SDK 1.2.0. If you kept the default location then this value would be “C:\Freescale\KSDK_1.2.0”.

Eclipse_Preferences_KSDKLOC

Eclipse_Preferences_KSDKLOC_Add

This can be used when adding new files to a project that should link to the Kinetis SDK.

Eclipse_AddLinkedFile_KSDK_LOC

Once complete its a good idea to export your settings so you can import them later again if you need to rebuild your installation of Eclipse and KSDK. To do so you can follow the steps in the blog: “Copy my workspace settings“.

Other recommended tools

You should now be able to code and compile Kinetis projects using KSDK 1.2.0  on Eclipse. Congratulations but most kinetis development boards now days use OpenSDAv2 and so you likely need to setup Segger J-Link to be able to upload your project onto your controller and debug. I will not go into details here how to set all that up but the basics are:

1. Follow details on https://www.segger.com/opensda.html. Make sure you have installed both the J-Link software package and read and perform the correct install of the Firmware noted in the last section of the site under “Firmware images”.

2. Follow the instruction on http://gnuarmeclipse.livius.net/blog/jlink-debugging/. Make sure you have performed the “J-Link Install” part of this site and followed the details under the section “J-Link plug-in usage”.

That’s it. If you managed to follow all these steps correctly then you should be up and running now creating some amazing projects. Good luck, hope this blog helped you out.