It’s Relax Time: LED PWM

In the previous posting, we haven’t use DAVE CE application. Now, let’s start use it!!
We will make an application, still base on LED, but we will control the LED with PWM (Pulse Width Modulation) signal. Then we can control the brightness of the LED.

Just create new project and choose DAVE CE Project.

ADDING PWMSP001 APPLICATION

DAVE CE application, allows us to use component based program. The component actually is the library provided by DAVE IDE. When we want to create PWM application, we can use PWMSP001,  the component to generate PWM signal base on CCU4 (Capture/Compare Unit 4). CCU4 consists of 4 16-bit timer. And can be concatenated to generate 32, 48 or 64 bit timer. To use this component, also others component, can be select from App Selection View. Double click PWMSP001 to add to our project. We will use 2 PWM, so do it twice..:).

When we add the second PWMSP001, DAVE will show App Sharability dialog, because PWMSP001 use CCU4, so DAVE will ask to create new instance for CCU4 or use the same CCU4 (share/Existing Apps). Just select use Existing Apps, by click  app/ccuglobal/0.

We will have 2 PWMSP001 (PWMSP001/0 and PWMSP001/1) application on App Dependency Treeview with the same CCU4 (CCU4GLOBAL/0). If we select new instance, PWMSP001/1 will use new CCU4, CCU4GLOBAL/1.

UI EDITOR

Let start to initialize 2 PWM applications. Right click on the application and click UIEditor.

UIEditor also can be display by double click the application from S/W App Connectivity View.

The configure the both PWM application as below:

  • Counting Mode: Edge-Aligned Mode
  • Check Start during initialization
  • CCU4 resolution : 10uSec (10000 nsec)
  • PWM freq: 200 Hz
  • Duty Cycle: 0 % (First time LED off)
  • Check Period Match (check Enable at initialization)

The CCU4GLOBAL use default setting (with PLL, frequency  120 MHz).

MANUAL PIN ASSIGNMENT

PWM application need to output the signal to one of MCU pin. Then we need to assign a pin for our PWM applicaion, 2 pin for 2 PWM application. As we know that Relax Kit has 2 LEDs, then we can assign these LEDs for our PWM applicaion. Right click from our application then select Manual Pin Assignment.

Select pin_directouput from Resource, and from Port-Pin/Pin Number select P1.0 and P1.1 for each application. Then click Solve And Save. OK, we’ve done the setting for our application. If we want use, example, interrupt we must add interrupt controller application  (NVIC), and we must connect it to the PWM application using Signal Connection. For now we will not use interrupt.

Next step is to generate the code from application setting, click SOLVER and Generate Code. DAVE will generate source code base on application setting. Every time we change the setting, Solver and Generate Code must be done.

Off course that the generated code by DAVE still need to edit. Just like when we make a PC program by visual programming language, like Delphi. We just do to set windows appearance by set the visual properties from the component we used. When we run the program, there is nothing happen if we click a button without a code in the click event handler.

PWMPS001 FUNCTIONS

DAVE has a good documentation from its help. Just click Help->Help Content. When help come out select Dave Apps then click PWMSP001.

 

The API Documentation, will show the functions for PWMSP001. The functions will control the PWMSP001 application: start or stop PWM. set PWM frequency, change duty cycle, etc. What we’ve done before was to configure the PWMSP001 application in design time. And the code that we will make is to configure/control it in run time.

PWM PROGRAM

We will make a program to adjust the LED brightness by PWM signal. After power ON, both LED will be off, duty cycle 0%, then PWMSP001/0 duty cycle will be increased to 100% with 25 mS interval. Interval time come from delay function using System Tick Timer. After 100%, PWMSP001/1 will be configure in the same way. Next is to decrease the duty cycle back to 0%.

And here is the program:

/*
* Main.c
*
* Created on: Dec 16, 2012
* Author: Kang Usman
*/

#include <DAVE3.h> //Declarations from DAVE3 Code Generation (includes SFR declaration)

uint32_t ticks = 0UL;

void SysTick_Handler(void);
void delayms(uint32_t thedelay);

int main(void)
{
  status_t status; //declaration of return variable for DAVE3 APIs
  uint32_t PWMerrorcount = 0; // accumulation of number of not successful executed API for the PWMSP001 App
  uint32_t PWMerrorcount2 = 0; // accumulation of number of not successful executed API for the PWMSP001 App
  int i;

  DAVE_Init(); // Initialization of DAVE Apps
  SysTick_Config(SystemCoreClock / 1000UL);

  status = PWMSP001_Start(&PWMSP001_Handle0); // start of the PWMSP001 App, instance 0
  if (status != DAVEApp_SUCCESS) PWMerrorcount++; // if start was not successful PWM error counter incremented
  status = PWMSP001_Start(&PWMSP001_Handle1); // start of the PWMSP001 App, instance 0
  if (status != DAVEApp_SUCCESS) PWMerrorcount2++; // if start was not successful PWM error counter incremented

  while(1)
  {
    for(i=0;i<101;i+=5)
     {
       status = PWMSP001_SetDutyCycle(&PWMSP001_Handle0, i); //change brightness of LED
       if (status != DAVEApp_SUCCESS) PWMerrorcount++; //if set of new duty cycle was not successful
       delayms(25);
     }

   for(i=0;i<101;i+=5)     {       status = PWMSP001_SetDutyCycle(&PWMSP001_Handle1, i); //change brightness of LED       if (status != DAVEApp_SUCCESS) PWMerrorcount2++; //if set of new duty cycle was not successful       delayms(25);     }    for(i=100;i>=0;i-=5)
    {
      status = PWMSP001_SetDutyCycle(&PWMSP001_Handle0, i); //change brightness of LED
      if (status != DAVEApp_SUCCESS) PWMerrorcount++; //if set of new duty cycle was not successful
      delayms(25);
    }

   for(i=100;i>=0;i-=10)
    {
      status = PWMSP001_SetDutyCycle(&PWMSP001_Handle1, i); //change brightness of LED
      if (status != DAVEApp_SUCCESS) PWMerrorcount2++; //if set of new duty cycle was not successful
      delayms(25);
    }

  }
 return 0;
}

void SysTick_Handler(void)
{
  ticks++;
}

void delayms(uint32_t thedelay)
{
  uint32_t currenttick;

  currenttick= ticks;
  while(ticks-currenttick<thedelay);
}

And here is the video…

 

It’s Relax Time: Blinky LED

OK, let’s start to play with the Relax Kit. It’s Relax Time… 🙂

Let’s start with the simplest thing…just blink the LED, just say hello to the world. Relax Kit has 2 LED connected to P1.0 and P1.1. Here is the schematic:

With this connection, we need to give logic HIGH the pin if want to turn on the LED, and logic LOW if want to turn off the LED.

PORT CONFIGURATION

Before we can use the port, we have to do some configuration to the related registers. Not like standard C51 family, that no need to set a register before read or write to the port. But, off course, we can not set mode for C51 port, like open drain, push pull etc. Only has internal pull up.

There are 8 registers to configure XMC4500 port:

  • Port Input/Output Control Register (IOCR)
  • Pad Drive Mode Register (PDR)
  • Pin Function Decision Control Register (PDISC)
  • Port Output Register (OUT)
  • Port Output Modification Register (OMR)
  • Port Input Register (IN)
  • Port Pin Power Save Register (PPS)
  • Port Pin Hardware Select Register (HWSEL)

IOCR will set a port as standard input/output or alternative function. As input port, it can the port can be set as direct input or inverted input, with or without internal pull up/down. For output function, we can set output mode as push pull or open drain. The port also can be configured as alternative function in output mode, like PWM outputm clock output etc.

There are 4 registers of  IOCR:

  • IOCR0: controls pin [3:0]
  • IOCR4: controls pin [7:4]
  • IOCR8: controls pin [11:8]
  • IOCR12: controls pin [15:12]

XMC4500 has 8 ports: P0, P1, P2, P3, P4, P5, P6, P14, and P15. Each ports can have until 16 pin (bit), depends on the XMC4500 package. Detail in the XMC manual.

IOCR0 structure (others are the same)

PC0, for pin 0, PC1 for pin 1 and so on, rw can be read or write, r read only bit and bit with 0 value should write 0.

And the input/output configuration:

PDR is used to select output drive strength and the slew rate.

There are 2 PDR registers: PDR0 (for bit 0-7) and PDR1 (for bit 8-15). PDR0 is used for P0-P6, and  PDR1 only use for P0-P3 and P5, the port that has more than 8 pin. P14 dan P15, that use for analog analog, did not have PDR. PDRx register structure:

The primary use for PDSIC register is to disable/enable the digital pad structure in shared analog and digital ports, see the dedicated description of the Pn_PDISC (n=14-15) register of the analog ports. PDSIC structure:

OUT register determines the value of a GPIO pin when it is selected by Pn_IOCRx as output. Writing a 0 to a Pn_OUT.Px (x = 0-15) bit position delivers a low level at the corresponding output pin. A high level is output when the  corresponding bit is written with a 1. Note that the bits of Pn_OUT.Px can be individually set/reset by writing appropriate values into the port output modification register Pn_OMR, avoiding readmodify-write operations on the Pn_OUT, which might affect other pins of the port. The Pn_OUT is also used to store/drive a defined value for the input in Deep Sleep mode. For details on this see the Port Pin Power Save Register. That is also the only use of the Pn_OUT register in the analog and digital input ports P14 and P15.

OMR register contains control bits that make it possible to individually set, reset, or toggle the logic state of a single port line by manipulating the output register.

To select for each pin, PSx bit and PRx bit can be programmed as below:

PPS registers is used for power save or sleep mode, detail information can be read from XMC4500 manual, also for HWSEL register. 🙂

BLINKY LED

OK, let’s start to make a program. In this program we will just to blink both of LED on the Relax Kit board, when P1.0 turned ON, P1.1 will turned OFF and vice versa.

We will set P1.0 and P1.1 as output with push pull mode. PC0 in IOCR0 will has value 10000B. In C we can write as:

  PORT1->IOCR0 = 0x80UL << 0; //P1.0 output, push pull   
  PORT1->IOCR0|= 0x80UL << 8; //P1.1 output, push pull   

PDR register is set to select strong driver, PD0 and PD1 will set to 2.

  PORT1->PDR0 = 0x02UL << 0; //P1.0, pad driver strong   
  PORT1->PDR0 |= 0x02UL << 8; //P1.1, pad driver strong   

To turn ON and turn OFF the LED, we will not use OUT reguster, but we will use OMR register, because we will control per pin. Set PRx=0 and PSx=1 will turn on the LED, and PRx=1 and PSx=1 will turn off the LED.

LED ON:

PORT1->OMR = (1UL<<0)|((1UL<<0)<<16); // P1.0 ON
PORT1->OMR = (1UL<<1)|((1UL<<1)<<16); // P1.1 ON

LED OFF:

PORT1->OMR = (1UL<<0)<<16; // P1.0 OFF
PORT1->OMR |=(1UL<<1)<<16; //P1.1 OFF

Then, make a new project with DAVE IDE, and select Empty Main Project:

And write this in main.c and build the project.

#include  //SFR declarations of the selected device
void thedelay (unsigned long delay)
{
  while (delay–)
  {
    __NOP();
  }
}

int main(void)
{
  PORT1->IOCR0 = 0x80UL << 0; //P1.0 output, push pull   
  PORT1->IOCR0|= 0x80UL << 8; //P1.1 output, push pull   
  PORT1->PDR0 = 0x02UL << 0; //P1.0, pad driver strong   
  PORT1->PDR0 |= 0x02UL << 8; //P1.1, pad driver strong   
  
  while(1)    
    {
      PORT1->OMR = (1UL<<0)|((1UL<<0)<<16); // P1.0 ON
      PORT1->OMR |=(1UL<<1)<<16; //P1.1 OFF
      thedelay(4000000);
      PORT1->OMR = (1UL<<0)<<16; // P1.0 OFF
      PORT1->OMR = (1UL<<1)|((1UL<<1)<<16); // P1.1 ON
      thedelay(4000000);
   }
  return 0;
}

DOWNLOAD AND DEBUGGING

Connect the Relax Kit with the PC/Laptop. If we installed J-Link driver when installing DAVE 3, PC/Laptop will detect it. Just click Debug button/menu.

If BlinkyLED didn’t appear in the debugger, just compile and build our project before start download or debugging. If present, just click BlinkyLED. Select Infineon XMC Relax Kit as target. Then click Debug button. DAVE will download the program into flash memory of XMC4500. After that will show debug window:

From Debug window, click Resume(F8) to run the program. We also can run the program step by step  (Step Into), step Over, or step return, reset the target and inspect the value of declared variables on the program, register value, and the program in assembly.

Check out the video:

XMC4500 Development Tool: DAVE™ 3 – Auto Code Generation and Free Tools

DAVE™ 3 is an Integrated Development Tool (IDE) made by Infineon, Eclipse based with code editor, debugger,loader and use free ARM-GCC C Compiler. DAVE™ has special feature Code Engine (CE), an automatic code generator, designed to support Componen Based Programming (CBP) for embedded system. Same as Code Wizard in AVR Code Vision. With CBP, maybe we no need to read every page the XM4500 reference manual (1615 pages :). But, off course, we still need to understand the basic principal of all XMC4500 features. But we no need to remember all bit or register name.

DAVE™ INSTALLATION

DAVE™ is a free tool from Infineon, it can be download from here. Current version is 3.1.4 Beta Realease. Just unzip download file and run DAVE-3.1.4_Master_Setup.exe. Follow installation instruction. Just click Next. 🙂

daveinstall1

SEGGER-JLinkARM_V456 is integrated debugger with DAVE™ 3, used for program download and debugging. When in our PC have another IDE that support jLink plug in, will be an option whether to install it in IDE or not.

jlink

I already try in Keil to download program to Relax Kit. For AVR studio, need another debugger tool that support AVR.

When installation finish, if we run, DAVE™ 3 will ask to select Workspace folder, where the projects will be saved. Just go to desired folder by click Browse button. Select Use this as the default… so DAVE™ 3 will not ask again v.

workspace

DAVE™ 3 APPLICATION AND LIBRARY INSTALLATION

To use DAVE CE features,we need to install the needed libraries for create DAVE CE application. Make sure we connected to internet. From Help menu, click Install App Library.

installlibrary

Will show up Library Manager Wizard.

daveappslibrary

Select Dave Project Library Manager to download program samnples or Dave Apps Library Manager to download the DAVE CE library. Just follow the next instruction. If download failed, try to change connection setting. Just click Window>Preference. From General>Network Connection. Select Active Provider>Direct.

preferences

If still fail, we can install manually. Download .

Program Samples

DAVE Apps

Enjoy it…

XMC4500 Development Tool: The Relax Lite Kit

Relax Lite Kit, is an evaluation board from Infineon as development tool to learn XMC4500 microcontroller, an ARM Cortex M4 processor core. The Relax Lite Kit is downgrade version of The Relax Kit with reduce some feature like Ethernet, Micro SD, RTC crystal, Quad-SPI flash memory.

relax-lite-kit

MAIN FEATURES

  • XMC4500 Microcontroller (ARM® Cortex™-M4F based)
  • Detachable on-board Debugger
  • Power over USB
  • 2 x User Button and 2 x User LED
  • Reset Button
  • Power Regulator from 5 V to 3.3V
  • 4 x SPI-Master, 3x I2C, 3 x I2S, 3 x UART, 2 x CAN, 17 x ADC (12 bit), 2 x DAC, 31x PMW mapped on 2 Pin Headers 2 x 20
  • USB-OTG (Micro USB Plug)

Relax Kit version that not include in Lite version:

  • Ethernet PHY and RJ45 jack
  • Real Time Clock Crystal
  • 32 Mbit Quad-SPI Flash Memory
  • microSD Card Slot

 POWER SUPPLY

Relax Kit must be supplied by an external power supply (5V) connected to any of the Micro UBS plugs (X3, X100). There are 2 micro USB plugs, micro USB plug for debugger board and micro USB OTG. Both V-USB use protection diode, that ensure safe operation in case power is provided through both USB plugs at the same time.

powersupply

 

Standard USB only can draw 500 mA current, so it need an external power supply if the Relax Kit connected with other circuit that need current higher than 500 mA. Out of the box with the pre-programmed web server application and the on-board debugger in operation the XMC4500 Relax Kit-V1 typically draws about 250 mA. The XMC4500 Relax Lite Kit-V1 without the web server capabilities draws about 200 mA. The external power supply can be connected to to one of the 5 Volt power pins (VDD5) on the pin headers X1 or X2. If the board is powered via a USB plug, it’s not recommended to apply an additional 5 Volt power supply because no protection diode. So if the circuit need current more than 500 mA just use external power supply to pin header X1 or X2.

PIN HEADER CONNECTOR

The pin headers X1 and X2 can be used to extend the evaluation board or to perform measurements on the XMC4500. Figure below shows the available GPIOs/signals at the pin headers. The pinning table is also printed onto the bottom side of the PCB.

pinheader

The XMC4500 provides a flexible mapping of functions to different pins. Figure below shows an example how the communication peripheral functions UART, I2C, SPI, CAN and I2S can be mapped to XMC4500’s GPIOs.
GPIOs with the same colour code belong to the same group of physical pins and cannot be choosen twice. For instance UART-3 has got a pin overlap with I2C-Master1 and therefore this combination cannot work in parallel.
Please also avoid pheripheral combinations which are using the same USIC channel. For example I2C-Master2 and UART-3 utilizing USIC 0 Channel 0 (U0C0), therefore this combination does not work in parallel.

pinmapping

Detail explanation just refer to The Relax Kit manual.