lpcware

LPCOpen v2.13 example project periph_sct will not work as written - Fixed code included

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by larryvc on Sun Dec 07 19:00:35 MST 2014
Firstly I want to say that I think the SCT is an awesome peripheral.  I intend to use it to replace processor intensive software bit-banged pattern generators in a few of my devices.  But I do have some concerns with the quality of some of the example programs that are provided in LPCOpen.

Example program periph_sct in LPCOpen v2.13 for the LPC824 will not work as written because there is setup code missing.  The SCT interrupt will never happen without first enabling a state for event 0, starting (unhalting) the SCT counter, and setting event 0 as the limit for the counter.

This is the working code, sct.c, with changes indicated in red:


/*
* @brief State Configurable Timer (SCT) example
*
* @note
* Copyright(C) NXP Semiconductors, 2012
* All rights reserved.
*
* @par
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* LPC products.  This software is supplied "AS IS" without any warranties of
* any kind, and NXP Semiconductors and its licensor disclaim any and
* all warranties, express or implied, including all implied warranties of
* merchantability, fitness for a particular purpose and non-infringement of
* intellectual property rights.  NXP Semiconductors assumes no responsibility
* or liability for the use of the software, conveys no license or rights under any
* patent, copyright, mask work right, or any other intellectual property rights in
* or to any products. NXP Semiconductors reserves the right to make changes
* in the software without notification. NXP Semiconductors also makes no
* representation or warranty that such application will be suitable for the
* specified use without further testing or modification.
*
* @par
* Permission to use, copy, modify, and distribute this software and its
* documentation is hereby granted, under NXP Semiconductors' and its
* licensor's relevant copyrights in the software, without fee, provided that it
* is used in conjunction with NXP Semiconductors microcontrollers.  This
* copyright, permission, and disclaimer notice must appear in all copies of
* this code.
*/

#include "board.h"

/*****************************************************************************
* Private types/enumerations/variables
****************************************************************************/

#define TICKRATE_HZ (10)/* 10 ticks per second */

static volatile uint32_t ticks;

/*****************************************************************************
* Public types/enumerations/variables
****************************************************************************/

/*****************************************************************************
* Private functions
****************************************************************************/

/*****************************************************************************
* Public functions
****************************************************************************/

/**
* @briefHandle interrupt from SysTick timer
* @returnNothing
*/
void SysTick_Handler(void)
{
++ticks;
[color=#f00]//      Board_LED_Toggle(1);  // Get rid of this as it messes up the SCT_IRQHandler display[/color]
[/color]}

/**
* @briefHandle interrupt from State Configurable Timer
* @returnNothing
*/
void SCT_IRQHandler(void)
{
if (ticks % 2) {
Board_LED_Toggle(2);
}
else {
Board_LED_Toggle(0);
}

[color=#f00]/* Clear the SCT Event 0 Interrupt */[/color]
Chip_SCT_ClearEventFlag(LPC_SCT, SCT_EVT_0);
}

/**
* @briefApplication main program
* @returnNothing (This function will not return)
*/
int main(void)
{
[color=#f00]uint8_t result;

[/color]/* Generic Initialization */

SystemCoreClockUpdate();
Board_Init();

[color=#f00]/* Enable SysTick Timer */  // Check that we are passing a valid value, endless loop if not
result = SysTick_Config(SystemCoreClock / TICKRATE_HZ);
while (result == 1);  // error - reload value not possible
[/color]
/* Custom Initialization */

[color=#f00]/* Initialize the SCT clock and reset the SCT */
//Chip_SCT_Init(LPC_SCT);  // Get rid of this call, use the inline functions within the call
Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SCT);
Chip_SYSCTL_PeriphReset(RESET_SCT);
[/color]
/* Configure the SCT counter as a unified (32 bit) counter using the bus clock */
Chip_SCT_Config(LPC_SCT, SCT_CONFIG_32BIT_COUNTER | SCT_CONFIG_CLKMODE_BUSCLK);

[color=#f00]/* The match/capture REGMODE defaults to match mode */
/* No REGMODE changes are needed for this program   */
[/color]
/* Set the match count for match register 0 */
Chip_SCT_SetMatchCount(LPC_SCT, SCT_MATCH_0, SystemCoreClock / TICKRATE_HZ);

[color=#f00]/* Set the match reload value for match reload register 0*/[/color]
Chip_SCT_SetMatchReload(LPC_SCT, SCT_MATCH_0, SystemCoreClock / TICKRATE_HZ);

[color=#f00]/* Event 0 only happens on a match condition */
LPC_SCT->EVENT[0].CTRL = (1 << 12);

/* Event 0 only happens in state 0 */
LPC_SCT->EVENT[0].STATE = 0x00000001;

/* Event 0 is used as the counter limit */
LPC_SCT->LIMIT = 0x00000001;

/* Enable flag to request an interrupt for Event 0 */
[/color]Chip_SCT_EnableEventInt(LPC_SCT, SCT_EVT_0);

[color=#f00]/* Enable the interrupt for the SCT */
[/color]NVIC_EnableIRQ(SCT_IRQn);

[color=#f00]/* Start the SCT counter by clearing Halt_L in the SCT control register */
Chip_SCT_ClearControl(LPC_SCT, SCT_CTRL_HALT_L);

[/color]while (1) {
[color=#f00]//              Board_LED_Toggle(0);  // Get rid of this as it messes up the SCT_IRQHandler display
[/color]__WFI();
}

return 0;
}

Original Attachment has been moved to: sct_2.c.zip

Outcomes