Shahariar
Published © GPL3+

Setting Up GPIO, PWM, I2C for K82 Freedom Board in KDS

A helping hand for the novices like me to set up Output on other available pins of K82 Freedom

BeginnerProtip1 hour4,309
Setting Up GPIO, PWM, I2C for K82 Freedom Board in KDS

Things used in this project

Story

Read more

Schematics

Output on Other Pins

Most Important I2C APIs (incomplete)

Some I2C APIs

Code

main c

C/C++
/*

Setting Output on PTB16
 */


#include <stdio.h>
#include "main.h"
#include "board.h"
#include "pin_mux.h"
#include "fsl_gpio.h"
#include "clock_config.h"



#define High 1
#define Low 0
/*
Setting Up GPIO in Pin_Mux.c
Inside BOARD_InitPins() from pin_mux
First, Enable- Port Clock which will start up the port for setting
Second, Set- Port, Port Pin Number, MUX Function - GOIO/UART/I2C

In Main C

Third, Set the Pin As Input/Output with config Struct
and call BOARD_InitPins() from pin_mux

Forth, Use Config Struct to Initialize the Pin
Five, Skatte Nee Shiro


*/

int main(void)
{

	gpio_pin_config_t pin_config =
	{
kGPIO_DigitalOutput,1,
	};

  BOARD_InitPins(); 
  BOARD_BootClockRUN();
  BOARD_InitDebugConsole();

  GPIO_PinInit(GPIOB, 16U, &pin_config);

  for (;;)
  {
	  GPIO_WritePinOutput (GPIOB, 16U, High);
	  GPIO_WritePinOutput (GPIOB, 16U, Low);


  }


}

PWM Main C

C/C++
#include "fsl_debug_console.h"
#include "board.h"
#include "fsl_ftm.h"

#include "pin_mux.h"
#include "clock_config.h"


/* Get source clock for FTM driver */
#define FTM_SOURCE_CLOCK CLOCK_GetFreq(kCLOCK_BusClk)

void delay(void);

void delay(void)
{
    volatile uint32_t i = 0U;
    for (i = 0U; i < 200000U; ++i)
    {
        __asm("NOP"); /* delay */
    }
}


int main(void)
{
    bool brightnessUp = true; /* Indicate LEDs are brighter or dimmer */
    uint8_t updatedDutycycle = 0U;

    // holds ftm configuration info
    ftm_config_t ftmInfo;

    // sets parameters for pwm signal such as Channel No, Duty etc... 3 for 3 channels
    ftm_chnl_pwm_signal_param_t ftmParam[3];

    /* Configure ftm params with frequency 24kHZ */
    ftmParam[0].chnlNumber = (ftm_chnl_t)4U;
    ftmParam[0].level = kFTM_LowTrue;
    ftmParam[0].dutyCyclePercent = 0U;
    ftmParam[0].firstEdgeDelayPercent = 0U;

    ftmParam[1].chnlNumber = (ftm_chnl_t)5U;
    ftmParam[1].level = kFTM_LowTrue;
    ftmParam[1].dutyCyclePercent = 0U;
    ftmParam[1].firstEdgeDelayPercent = 0U;

    ftmParam[2].chnlNumber = (ftm_chnl_t)6U;
    ftmParam[2].level = kFTM_LowTrue;
    ftmParam[2].dutyCyclePercent = 0U;
    ftmParam[2].firstEdgeDelayPercent = 0U;

    /* Board pin, clock, debug console init */
    BOARD_InitPins();
    BOARD_BootClockRUN();
    BOARD_InitDebugConsole();

    /* Print a note to terminal */
    PRINTF("\r\nFTM example to output PWM on 3 channels\r\n");
    PRINTF("\r\nYou will see a change in LED brightness if an LED is connected to the FTM pin");
    PRINTF("\r\nIf no LED is connected to the FTM pin, then probe the signal using an oscilloscope");

    

    // loads default FTM config into ftminfo variable
    FTM_GetDefaultConfig(&ftmInfo);

    // Initialize FTM module-3 with config settings, which is associated with port c pin 8,9,10
    FTM_Init(FTM3, &ftmInfo);


    // Here 3U means 3 Channels are active at a time @
    // Parameters are FTM Module No, FTM Parameters, No of Channels, PWM Type, Frequency, Clock Source
    FTM_SetupPwm(FTM3, ftmParam, 3U, kFTM_EdgeAlignedPwm, 24000U, FTM_SOURCE_CLOCK);
    FTM_StartTimer(FTM3, kFTM_SystemClock);

    while (1)
    {
    	uint8_t x=0u;
        /* Delay to see the change of LEDs brightness */
        delay();

        if (brightnessUp)
        {
            /* Increase duty cycle until it reach limited value */
            if (++updatedDutycycle == 100U)
            {
                brightnessUp = false;
            }
        }
        else
        {
            /* Decrease duty cycle until it reach limited value */
            if (--updatedDutycycle == 0U)
            {
                brightnessUp = true;
            }
        }
        /* Start PWM mode with updated duty cycle */
        // FTM3 is the PWM module 4U,5U,6U are channels
        // FTM_module_name, n_th_channel_no, pwm_typee, duty_cycle are the parameters

        FTM_UpdatePwmDutycycle(FTM3, (ftm_chnl_t)4U, kFTM_EdgeAlignedPwm,
                              90);
        FTM_UpdatePwmDutycycle(FTM3, (ftm_chnl_t)5U, kFTM_EdgeAlignedPwm,
                             50);
        FTM_UpdatePwmDutycycle(FTM3, (ftm_chnl_t)6U, kFTM_EdgeAlignedPwm,
        		updatedDutycycle);

        /* Software trigger to update registers */
        //FTM_module_name, trigger_is_OK

        FTM_SetSoftwareTrigger(FTM3, true);
    }
}

pin mux of pwm

C/C++
#include "fsl_port.h"
#include "pin_mux.h"
#include "fsl_common.h"



void BOARD_InitPins(void)
{
    /* Initialize LPUART4 pins below */
    /* Ungate the port clock */
    CLOCK_EnableClock(kCLOCK_PortC);
    /* Affects PORTC_PCR 14,15 register */
    // On Pin 14,15  kPORT_MuxAlt3 Mux with PWM

    PORT_SetPinMux(PORTC, 14U, kPORT_MuxAlt3);
    PORT_SetPinMux(PORTC, 15U, kPORT_MuxAlt3);

    /* Initialize muxing of FTM3 with pins below  which is "Flex Timer Module the Third" 
    /* Affects PORTC_PCR 8,9,10 register */
    // On Pin 8,9,10  kPORT_MuxAlt3 Mux with PWM
    PORT_SetPinMux(PORTC, 8U, kPORT_MuxAlt3);
    PORT_SetPinMux(PORTC, 9U, kPORT_MuxAlt3);
    PORT_SetPinMux(PORTC, 10U, kPORT_MuxAlt3);
}

Credits

Shahariar

Shahariar

71 projects • 260 followers
"What Kills a 'Great life' is a 'Good Life', which is Living a Life Inside While Loop"

Comments