It’s Relax Time: Button Read

In the previous posting, we’ve learn how to make a program using DAVE IDE from zero. We have to understand the registers that we used in the program, without library, so we have to read the reference manual of XMC4500. I think it is good for beginner, like me. So, we didn’t depend on the library from the IDE. Or if the library didn’t match with what we want, we can make our own library. 🙂

Now, let’s start to use DAVE advance features. Just create a new project (Dave Poject), choose Easy Start Project.

Then open Main.c.

/* SFR declarations of the selected device */
#include <XMC4500.h>
#include "GPIO.h"

void SysTick_Handler(void);

typedef enum { OFF = 0, ON = 1} state_t ;
state_t led1_state = ON;
state_t led2_state = ON;

int main(void) {
/* Initialize LED1 */
 P1_1_set_mode(OUTPUT_PP_GP);
P1_1_set_driver_strength(STRONG);

/* Initialize LED2 */
P1_0_set_mode(OUTPUT_PP_GP);
P1_0_set_driver_strength(STRONG);

/* Initialize BUTTON1 */
P1_14_set_mode(INPUT);

/* Initialize BUTTON2 */
P1_15_set_mode(INPUT);

/* fSYS=fCPU at 120MHz */
/* Systick every 120000 cycles = 10ms */
if(SysTick_Config(SystemCoreClock / 100UL) == 0){

/* Loop forever */
for(;;) {
}
}
else{
asm("BKPT 255");
}
}

void SysTick_Handler(void) {
static uint32_t ticks = 0UL;
static state_t button1_state = OFF;

static state_t button2_state = OFF;
ticks++;

/* Read BUTTON1, update state if pressed */
if(P1_14_read() == 0UL){
 button1_state = ON;
}
else{
 if(button1_state == ON){
 if(led1_state == ON){
  led1_state = OFF;
}
else{
led1_state = ON;
}
}
button1_state = OFF;
}

/* Read BUTTON2, update state if pressed */
if(P1_15_read() == 0UL){
button2_state = ON;
}
else{
if(button2_state == ON){
if(led2_state == ON){
led2_state = OFF;
}
else{
led2_state = ON;
}
}
button2_state = OFF;
}

/* Toggle every 1s */
if(ticks == 100UL){
if(led1_state == ON){
/* Toggle LED1 */
P1_1_toggle();
}

if(led2_state == ON){
/* Toggle LED2 */
P1_0_toggle();
}
ticks = 0UL;
}
}

Check the program, there is no register name!. Yeah, it’s already write in “GPIO.h”. We just call the function. As we know that there are 2 LED in Relax Kit that connected to P1.0 and P1.1, and also there are 2 buttons connected to P1.14 and P1.15. To initialize P1.0 and P1.1 as output with strong driver mode we will write

/* Initialize LED1 */
P1_1_set_mode(OUTPUT_PP_GP);
P1_1_set_driver_strength(STRONG);

/* Initialize LED2 */
P1_0_set_mode(OUTPUT_PP_GP);
P1_0_set_driver_strength(STRONG);

And P1.14 and P1.15:

 /* Initialize BUTTON1 */
P1_14_set_mode(INPUT);

/* Initialize BUTTON2 */
P1_15_set_mode(INPUT);

The program also use System Tick Timer at 10 ms.

But, I will make a change. The program will be changed to blink the LED, and we can adjust the blink rate by push the button, increase and decrease. System tick timer will be set to 1 ms. LED toggle in system tick timer interrupt handle, and the main program will poll/scan the 2 buttons, if Button1 (P1.14) is pressed, the thedelay value will be add by 10, then the LED will blink 10 ms slower. And if Button2 (P1.15) is pressed, the thedelay value will minus by 10, and the LED will blink 10 ms faster.

And here is the souce code..


/* SFR declarations of the selected device */

#include <XMC4500.h>

#include "GPIO.h"

void SysTick_Handler(void);
void delayms(uint32_t delay_ms);

uint32_t thedelay;
uint32_t ticks = 0UL;
uint32_t currentticks;

int main(void) {

/* Initialize LED1 */
P1_1_set_mode(OUTPUT_PP_GP);
P1_1_set_driver_strength(STRONG);

/* Initialize LED2 */
P1_0_set_mode(OUTPUT_PP_GP);
P1_0_set_driver_strength(STRONG);

/* Initialize BUTTON1 */
P1_14_set_mode(INPUT);

/* Initialize BUTTON2 */
P1_15_set_mode(INPUT);

thedelay= 500;
ticks=0;
currentticks=ticks;

/* fSYS=fCPU at 120MHz */
/* Systick every 120000 cycles = 1ms */
SysTick_Config(SystemCoreClock / 1000UL);

while(1)
{
   if(P1_14_read()== 0UL)
    {
      delayms(100);  //Anti debounce
      while(P1_14_read()==0UL); //Wait until button released
      thedelay+=10;
      if(thedelay>1000)
         thedelay=1000;
    }

  if(P1_15_read()== 0UL)
   {
     delayms(100);
     while(P1_15_read()==0UL);
     thedelay-=10;
     if(thedelay<;10)
       thedelay=10;
   }
}
}

void delayms(uint32_t delay_ms)
{
  uint32_t currenttick;

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

void SysTick_Handler(void) {

 ticks++;
 if(ticks-currentticks>=thedelay)
 {
   P1_0_toggle();
   P1_1_toggle();
   currentticks=ticks;
 }
}

And check the video

XMC4500: An Introduction (ARM Cortex M4 from Infineon)

A few weeks ago, I met Infineon Marketing on Facebook. After a little conversation, he would like to give me an evaluation kit from Infineon,  for free. 🙂 The kit is powered by XMC4500 microcontroller, with ARM Cortex M4. Before we talk about the kit, let’s meet with XMC4500.

XMC4500 ARCHITECTURE

XMC4500 series belongs to the XMC400 family, based on ARM Cortex M4 processor core. The XMC4500 series devices are optimized for electrical motor control, power conversion, industrial connectivity and sense & control applications. Here is some main features of XMC4500, detail explanation should refer to XMC refence manual.

CPU Subsystem

CPU Core:

  • High Performance 32-bit ARM Cortex-M4 CPU
  • 16-bit and 32-bit Thumb2 instruction set
  • DSP/MAC instructions
  • System timer (SysTick) for Operating System support

Floating Point Unit

Memory Protection Unit

Nested Vectored Interrupt Controller (NVIC)

Two General Purpose DMA with up to 12 channels

Event Request Unit (ERU) for programmable processing of external and internal
service requests

Flexible CRC Engine (FCE) for multiple bit error detection

On-Chip Memories

  • 16 KB on-chip boot ROM
  • 64 KB on-chip high-speed program memory
  • 64 KB on-chip high speed data memory
  • 32 KB on-chip high-speed communication
  • 1024 KB on-chip Flash Memory with 4 KB instruction cache

Communication Peripheral

  • Ethernet MAC module capable of 10/100 Mbit/s transfer rates
  • Universal Serial Bus, USB 2.0 host, Full-Speed OTG, with integrated PHY
  • Controller Area Network interface (MultiCAN), Full-CAN/Basic-CAN with three nodes, 64 message objects, data rate up to 1 Mbit/s
  • Six Universal Serial Interface Channels (USIC), usable as UART, double-SPI, quad-SPI, IIC, IIS and LIN interfaces
  • LED and Touch-Sense Controller (LEDTS) for Human-Machine interface
  • SD and Multi-Media Card interface (SDMMC) for data storage memory cards
  • External Bus Interface Unit (EBU) enabling communication with external memories
  • and off-chip peripherals like SRAM, SDRAM, NOR, NAND and Burst Flash.

Analog Fronted Peripherals

  • Four Analog-Digital Converters (VADC) of 12-bit resolution, 8 channels each with input out-of-range comparators for over-voltage detection
  • Delta Sigma Demodulator with four channels, digital input stage for A/D signal conversion
  • Digital-Analogue Converter (DAC) with two channels of 12-bit resolution

Industrial Control Peripherals

  • Four Capture/Compare Units 4 (CCU4) for use as general purpose timers
  • Two Capture/Compare Units 8 (CCU8) for motor control and power conversion
  • Two Position Interfaces (POSIF) for hall and quadrature encoders and motor positioning
  • Window Watchdog Timer (WDT) for safety sensitive applications
  • Die Temperature Sensor (DTS)
  • Real Time Clock module with alarm support
  • System Control Unit (SCU) for system configuration and control

Input/Output Lines

  • Programmable port driver control module (PORTS)
  • Individually bit addressable
  • Tri-stated in input mode
  • Push/pull or open drain output mode
  • Boundary scan test support over JTAG interface

On Chip Debug Support

  • Full support for debug features: 8 breakpoints, CoreSight, trace
  • Various interfaces: ARM-JTAG, SWD, single wire trace

Block Diagram

The diagram below shows the functional blocks and their basic connectivity within the
XMC4500 System.

xmcblockdiagram

DEVELOPMENT TOOL

Some compiler & tool chain that support XMC4500:

For the evaluation board we can use:

And here is the evaluation kit that I received:

relaxkit1

relaxkit2

relaxkit3

The kit that I received is Lite version kit, with some features not include: Ethernet, micro SD card and qSPI Flash. But it’s not a big problem, maybe someday I’ll buy the missing component and insert to the kit. And we will use DAVE from Infineon as it’s free download from Infineon web. But we also can use Keil MDK, we have to buy it for professional use. The lite version only support 32KB flash, it’s enough to blink the LED. 🙂