Thursday, October 17, 2019

How to create micro second delay on CC2652R

The following codes show you how to create micro second delay on CC2652R

1. Use the following functions to create one micro second delay function.

#pragma FUNC_ALWAYS_INLINE (CpuDelayhundredNanoSec)
#pragma FUNCTION_OPTIONS(CpuDelayhundredNanoSec, "--opt_level=0")
static void CpuDelayhundredsNanoSec(void);
static void CpuDelayhundredsNanoSec(void) {
    asm(" NOP");
    asm(" NOP");
    asm(" NOP");
    asm(" NOP");
    asm(" NOP");
}
#pragma FUNC_ALWAYS_INLINE (CpuDelayMicroSec)
#pragma FUNCTION_OPTIONS(CpuDelayMicroSec, "--opt_level=0")
static void CpuDelayMicroSec(void);
static void CpuDelayMicroSec(void) {
    CpuDelayhundredsNanoSec();
    CpuDelayhundredsNanoSec();
    CpuDelayhundredsNanoSec();
}

2. Use the following codes in CC26x2 SDK ble5stack project_zero to test it.
    2.1. Add #include in project_zero.c
    2.2. Revise the following red codes in ProjectZero_handleButtonPress of project_zero.c to do toggle red led for 3 micro seconds when button 1 is pressed.

static void ProjectZero_handleButtonPress(pzButtonState_t *pState)
{
    Log_info2("%s %s",
              (uintptr_t)(pState->pinId ==
                          CONFIG_PIN_BTN1 ? "Button 0" : "Button 1"),
              (uintptr_t)(pState->state ?
                          ANSI_COLOR(FG_GREEN)"pressed"ANSI_COLOR(ATTR_RESET) :
                          ANSI_COLOR(FG_YELLOW)"released"ANSI_COLOR(ATTR_RESET)
                         ));

    // Update the service with the new value.
    // Will automatically send notification/indication if enabled.
    switch(pState->pinId)
    {
    case CONFIG_PIN_BTN1:
#if 0
        ButtonService_SetParameter(BS_BUTTON0_ID,
                                   sizeof(pState->state),
                                   &pState->state);
#else
        {
            ICall_CSState key;
            uint32_t c;
            key=ICall_enterCriticalSection();
            PIN_setOutputValue(ledPinHandle, CONFIG_PIN_RLED, 1);
            CpuDelayMicroSec();
            CpuDelayMicroSec();
            CpuDelayMicroSec();
            PIN_setOutputValue(ledPinHandle, CONFIG_PIN_RLED, 0);
            ICall_leaveCriticalSection(key);
        }

#endif
        break;
    case CONFIG_PIN_BTN2:
        ButtonService_SetParameter(BS_BUTTON1_ID,
                                   sizeof(pState->state),
                                   &pState->state);
        break;
    }
}

No comments:

Post a Comment