FRDM KL25Z Solution

VERY VERY URGENT! KINDLY SOLVE THIS FOR FRDM_KL25Z BOARD. PLEASE WRITE THE CORRECT ANSWER FOR THIS AS IT’S VERY IMPORTANT

Using existing drivers (the drivers are listed below), create a program that flashes the RGBLED in one of two patterns:

  • A flashing pattern with a period of 1Hz and an on time of n*10%, where 0<n<10
  • A pattern that pulses the LED with a 0.5s on time and a delay of 0.5*n seconds between pulses, where 0<n<10

Note that in both cases, the parameter n can range only from 1 through 9 (inclusive).

A momentary press of the button cycles the parameter n through its range. A longer press (approximately 1.5 s) changes the pattern. Use a single color for the LED.

Use FSM’s for the patterns and to distinguish button presses.

  1. Red LED driver

redled.c

#include <redled.h>

#include <MKL25Z4.h>

#define RED_LED_LOC 18

void configure_red_led()

{

SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK;

PORTB->PCR[RED_LED_LOC] =

PORT_PCR_MUX(1) |

PORT_PCR_IRQC(0) |

PORT_PCR_ISF(1);

PTB->PDDR |= (1<<RED_LED_LOC);

}

void turn_on_red_led()

{

PTB->PCOR |= (1<<RED_LED_LOC);

}

void turn_off_red_led()

{

PTB->PSOR |= (1<<RED_LED_LOC);

}

void toggle_red_led()

{

PTB->PTOR |= (1<<RED_LED_LOC);

}

redled.h

#ifndef REDLED_H

#define REDLED_H

void configure_red_led();

void turn_on_red_led();

void turn_off_red_led();

void toggle_red_led();

#endif

  1. Switch driver

sw.c

#include <MKL25Z4.h>

#include <stdbool.h>

#include “sw1.h”

#define SW1_LOCATION 20

void configure_sw1()

{

SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK;

PORTB->PCR[SW1_LOCATION] =

PORT_PCR_PE(1) |

PORT_PCR_PS(1) |

PORT_PCR_PFE(0) |

PORT_PCR_DSE(0) |

PORT_PCR_SRE(0) |

PORT_PCR_MUX(1) |

PORT_PCR_IRQC(0) |

PORT_PCR_ISF(1);

PTA->PDDR &= ~(1<<SW1_LOCATION);

}

_Bool sw1_is_pressed()

{

return !(PTA->PDIR & (1<<SW1_LOCATION));

}

_Bool sw1_is_not_pressed()

{

return !sw1_is_pressed();

}

sw.h

#ifndef SW1_H

#define SW1_H

_Bool sw1_is_pressed();

_Bool sw1_is_not_pressed();

void configure_sw1();

#endif

  1. Cop driver

cop.c

#include <MKL25Z4.h>

#include “copwdt.h”

void configure_copwdt()

{

SIM->COPC= SIM_COPC_COPT(3) |

SIM_COPC_COPCLKS(1) |

SIM_COPC_COPW(0) ;

}

void feed_the_watchdog()

{

SIM->SRVCOP = 0x55;

SIM->SRVCOP = 0xAA;

}

cop.h

#ifndef COPWDT_H

#define COPWDT_H

void configure_copwdt();

void feed_the_watchdog();

#endif

  1. Systick driver

systick.c

include <systick.h>

#include <MKL25Z4.h>

#include <stdbool.h>

static volatile _Bool systick_interrupt_has_occurred=0;

#define SYSTICK_PERIOD (1000)

#define SYSTICK_TOP (SYS_CLOCK / SYSTICK_PERIOD)

void configure_systick()

{

SysTick->LOAD = SYSTICK_TOP;

SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |

SysTick_CTRL_TICKINT_Msk |

SysTick_CTRL_ENABLE_Msk;

}

_Bool systick_has_fired()

{

_Bool retval = systick_interrupt_has_occurred;

systick_interrupt_has_occurred=0;

return retval;

}

void SysTick_Handler()

{

systick_interrupt_has_occurred=1;

}

systick.h

#ifndef SYSTICK_H

#define SYSTICK_H

#include<stdbool.h>

void configure_systick();

_Bool systick_has_fired();

#endif