This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2530 PWM setup

Other Parts Discussed in Thread: Z-STACK, CC2530, CC2590

HI Guys!

I working on to finish my dimmer device, as I told here before, I using Z-Stack Home 1.2.2a.44539 and SampleLight project as a reference. 

I already have my device connected in network and sending and responding all those messages necessary to working well, including ZCL messages, as a dimmer device.

I studing Mr Yikai post here (https://sunmaysky.blogspot.com/2014/11/how-to-output-pwm-from-cc2530.html ) to see how to start timer and other parameters necessary to use PWM definitions and implement it in my dimmer device. 

I would like to use P1.1 as a PWM port. To set it I used 2nd part code from YK´s post:

at: hal_board_cfg.h

PERCFG |= BV(6); // Select Timer 1 Alternative 2 location
P2DIR = (P2DIR & ~0xC0) | 0x80; // Give priority to Timer 1
P1SEL |= BV(1); // Set P1_1 to peripheral
T1CC0L = 0x3A; // PWM signal period
T1CC0H = 0x01;
T1CC1L = 0x9D; // PWM duty cycle
T1CC1H = 0x00;
T1CCTL1 = 0x1c;
T1CTL |= (BV(2)|0x03);

and I comment in at compile options HAL_PWM to starting to use PWM definitons in all those zstack files.  

The next step, in my mind, is to set up the PWM definitions in my app file (zlc_myproject.c), and need to use some functions to set up duty cycle variations over epecific port (in my case is at P1.1). As I said that using zcl_samplelight as a reference, and in this file I´m seeing a few PWM definitions structures, as I describing bellow:

#if (defined HAL_BOARD_ZLIGHT) || (defined HAL_PWM)
HalTimer1Init( 0 );
halTimer1SetChannelDuty( WHITE_LED, 0 );
halTimer1SetChannelDuty( RED_LED, 0 );
halTimer1SetChannelDuty( BLUE_LED, 0 );
halTimer1SetChannelDuty( GREEN_LED, 0 );

void zclTTa0003_UpdateLampLevel( uint8 level )

{
uint16 gammaCorrectedLevel;

// gamma correct the level
gammaCorrectedLevel = (uint16) ( pow( ( (float)level / LEVEL_MAX ), (float)GAMMA_VALUE ) * (float)LEVEL_MAX);

halTimer1SetChannelDuty(WHITE_LED, (uint16)(((uint32)gammaCorrectedLevel*PWM_FULL_DUTY_CYCLE)/LEVEL_MAX) );
}
#endif

+

* @fn zclTTa0003_AdjustLightLevel
*
* @brief Called each 10th of a second while state machine running
*
* @param none
*
* @return none
*/
static void zclTTa0003_AdjustLightLevel( void )

.

.

.

I trying understanding:

How to I link, in app definition, hal_board_cfg.h with espcific port (my case P1.1)? 

I seeing WHITE_LED, RED_LED, GREEN_LED and BLUE_LED, but I can´t find in hal_board_cfg.h any correlation code line. 

Each app code function is responsible for to increase or decrease duty cycle, even using WHITE_LED, RED_LED and etc...

I believe that I must not use WHITE_LED and each others LEDs or need to associate one with P1.1 port. 

Somebody can give a way to understanding it to finish my project?

BR

Alex

  • They are defined in the following lines in hal_board_cfg.h

    #if defined (PWM_ALT2)

    #define GREEN_LED HAL_T1_CH2
    #define RED_LED HAL_T1_CH1
    #define BLUE_LED HAL_T1_CH4 //Not connected on SmartRF05
    #define WHITE_LED HAL_T1_CH4
    #define ENABLE_LAMP P1SEL |= ( 0x1 | 0x2 );

    #define DISABLE_LAMP P1SEL &= ~( 0x1 | 0x2 );\
    P1 &= ~( 0x1 | 0x2 ); /* P0.3:6 */
    #else
    #define GREEN_LED HAL_T1_CH3
    #define RED_LED HAL_T1_CH1
    #define BLUE_LED HAL_T1_CH2
    #define WHITE_LED HAL_T1_CH4
    #define ENABLE_LAMP P0SEL |= ( 0x08 | 0x10 | 0x20 | 0x40); /* P0.3:6 */\

    #define DISABLE_LAMP P0SEL &= ~( 0x08 | 0x10 | 0x20 | 0x40); /* P0.3:6 */\
    P0 &= ~( 0x08 | 0x10 | 0x20 | 0x40); /* P0.3:6 */
    #endif
  • Hi Mr YK,

    thanks a lot for your assistance!!!

    I´m traveling this week, at the end of this week I´ll let you know about your suggestion.

    BR

    Alex

  • Hi YK,

    I did the changes and included another things, mainly declaration variable, according the compiler (IAR) requested me, but I have a doubts in declaring how I declared. I got some errors. As you can see in BOLD:

    1) because the dependencies, I included in zcl_TTa0003.c

    /* HAL */

    ..

    #include "hal_timer.h" 

    #if (defined HAL_BOARD_ZLIGHT) || (defined HAL_PWM)
    #define GREEN_LED HAL_T1_CH2
    #define RED_LED HAL_T1_CH1
    #define BLUE_LED HAL_T1_CH4 //Not connected on SmartRF05
    #define WHITE_LED HAL_T1_CH4
    HalTimer1Init( 0 );
    ...

    zclTTa0003_OnOff = LIGHT_ON;
    #if (defined HAL_BOARD_ZLIGHT) || (defined HAL_PWM)
    #define ENABLE_LAMP P1SEL |= ( 0x1 | 0x2 );
    ENABLE_LAMP;

    ...

    zclTTa0003_OnOff = LIGHT_OFF;
    #if (defined HAL_BOARD_ZLIGHT) || (defined HAL_PWM)
    #define DISABLE_LAMP P1SEL &= ~( 0x1 | 0x2 );\
    P1 &= ~( 0x1 | 0x2 ); /* P0.3:6 */
    DISABLE_LAMP;

    But IAR log expose for me the follow error:

    My PWM code in hal_board_cfg.h:

    #if defined (PWM_ALT2)
    #define INIT_PWM_PINS() \
    { \
    /* Drive LED1 from timer 1 ch2 and LED2 from timer 1 ch1 */ \
    /* unfortunitly we can not connect any other leds to the timer PWM */ \
    /* on the SmartRF05EB/BB */ \
    // PERCFG |= 0x40; /* set timer 1 to use alt configuration*/ \
    // P1SEL |= ( 0x1 | 0x2 ); \
    // P1DIR |= ( 0x1 | 0x2 );

    /*6.5K PWM with 50% duty cycle to P1.1 with CC2530 Timer 1 */

    PERCFG |= BV(6); // Select Timer 1 Alternative 2 location
    P2DIR = (P2DIR & ~0xC0) | 0x80; // Give priority to Timer 1
    P1SEL |= BV(1); // Set P1_1 to peripheral
    T1CC0L = 0x3A; // PWM signal period
    T1CC0H = 0x01;
    T1CC1L = 0x9D; // PWM duty cycle
    T1CC1H = 0x00;
    T1CCTL1 = 0x1c;
    T1CTL |= (BV(2)|0x03); // divide with 128 and to do i up-down mode
    }
    #else
    #define INIT_PWM_PINS() \
    { \
    PERCFG &= ~(0x40); /* bit 6 = 0 */ \
    P0SEL |= ( 0x08 | 0x10 | 0x20 | 0x40); /* P0.3:6 */ \
    P0DIR |= ( 0x08 | 0x10 | 0x20 | 0x40); /* P0.3:6 */ \
    }

    #endif

    /* ----------- Debounce ---------- */
    #define HAL_DEBOUNCE(expr) { int i; for (i=0; i<500; i++) { if (!(expr)) i = 0; } }

    /* ----------- Push Buttons ---------- */
    #define HAL_PUSH_BUTTON1() (PUSH1_POLARITY (PUSH1_SBIT))
    #define HAL_PUSH_BUTTON2() (0)
    #define HAL_PUSH_BUTTON3() (0)
    #define HAL_PUSH_BUTTON4() (0)
    #define HAL_PUSH_BUTTON5() (0)
    #define HAL_PUSH_BUTTON6() (0)

    /* ----------- LED's ---------- */
    #if defined (PWM_ALT2)

    #define GREEN_LED HAL_T1_CH2
    #define RED_LED HAL_T1_CH1
    #define BLUE_LED HAL_T1_CH4 //Not connected on SmartRF05
    #define WHITE_LED HAL_T1_CH4
    #define ENABLE_LAMP P1SEL |= ( 0x1 | 0x2 );

    #define DISABLE_LAMP P1SEL &= ~( 0x1 | 0x2 );\
    P1 &= ~( 0x1 | 0x2 ); /* P0.3:6 */
    #else
    #define GREEN_LED HAL_T1_CH3
    #define RED_LED HAL_T1_CH1
    #define BLUE_LED HAL_T1_CH2
    #define WHITE_LED HAL_T1_CH4

    #if defined (WHITE_LED_ONLY)
    #define ENABLE_LAMP P0SEL |= ( 0x40); /* P0.6 */
    #else
    //#define ENABLE_LAMP P0SEL |= ( 0x08 | 0x10 | 0x20 | 0x40); /* P0.3:6 */
    #define ENABLE_LAMP P0SEL |= ( 0x08 | 0x10 | 0x20 | 0x40); /* P0.3:6 */\
    #endif

    #define DISABLE_LAMP P0SEL &= ~( 0x08 | 0x10 | 0x20 | 0x40); /* P0.3:6 */\
    P0 &= ~( 0x08 | 0x10 | 0x20 | 0x40); /* P0.3:6 */

    #endif

  • You have to include hal_timer.h in your C file.
  • Hi Mr YK,

    Yes, I already inserted, in my c file, as you can see bellow:

  • You also need to add hal_timer.c into your project.
  • Hi Mr YK,

    firstly I would like to say thanks a lot for your tireless help 7 days a week....

    Sorry, the problem still persist! I made some tests, and I suspect the is declaring those variables in my c file:

    GREEN_LED, RED_LED, BLUE_LED, WHITE_LED,  ENABLE_LAMP, DISABLE_LAMP. Mainly in last two variables. I´ll describe my tests bellow:

    a) The 1st four variables, I declaring in my c file as:

    #define GREEN_LED HAL_T1_CH2
    #define RED_LED HAL_T1_CH1
    #define BLUE_LED HAL_T1_CH4 //Not connected on SmartRF05
    #define WHITE_LED HAL_T1_CH4

    b) The two last variables, I declaring in my c file as: 

    Error[e46]: as you see bellow at the end.

    #define ENABLE_LAMP   P1SEL |= ( 0x1 | 0x2 );

    #define DISABLE_LAMP P1SEL &= ~( 0x1 | 0x2 );\
    P1 &= ~( 0x1 | 0x2 ); /* P0.3:6 */

    But all those variables (a and b) above already declared in hal_board_cfg.h. 

    When I comment out b variables declaring, the compiler running without Error [e46], but present the error [Pe020] 

    When I comment out #define ENABLE_LAMP and #define DISABLE_LAMP

    Error[Pe020]: identifier "ENABLE_LAMP" is undefined C:\Texas Instruments\Z-Stack Home 1.2.2a.44539\Projects\zstack\HomeAutomation\TEHA0003\Source\zcl_TTa0003.c 1369

     

    When all the variables where declared, as I said in (a) and (b) items.

    Error[e46]: Undefined external "HalTimer1Init::?relay" referred in zcl_TTa0003 ( C:\Texas Instruments\Z-Stack Home 1.2.2a.44539\Projects\zstack\HomeAutomation\

    Error[e46]: Undefined external "halTimer1SetChannelDuty::?relay" referred in zcl_TTa0003 ( C:\Texas Instruments\Z-Stack Home 1.2.2a.44539\Projects\zstack\

    HomeAutomation\TEHA0003\CC2530DB\RouterEB\Obj\zcl_TTa0003.r51 )

    Error while running Linker

  • Since all those variables (a and b) above already declared in hal_board_cfg.h, I suppose you only need to include hal_board_cfg.h in your c file and don't have to add those defines again in your C file.

  • Hi Mr YK,


    I've done this before, but I always see the Pe020 error. I didn´t see anything else to fix it. Would you can advise me, please?

     As you can see bellow:


     

    Compiler options and path:

    My C file Includes:

  • Where do you define all those variables (a and b) in hal_board_cfg.h? There are different board defines in hal_board_cfg.h so you have to make sure you put them in right place.
  • I declared at hal_board_cfg.h in the HAL_PA_LNA and HALPA_LNA_CC2590, as you can see below:

    #elif defined (HAL_PA_LNA) || defined (HAL_PA_LNA_CC2590)

    #define HAL_BOARD_INIT() \
    { \
    uint16 i; \
    \
    SLEEPCMD &= ~OSC_PD; /* turn on 16MHz RC and 32MHz XOSC */ \
    while (!(SLEEPSTA & XOSC_STB)); /* wait for 32MHz XOSC stable */ \
    asm("NOP"); /* chip bug workaround */ \
    for (i=0; i<504; i++) asm("NOP"); /* Require 63us delay for all revs */ \
    CLKCONCMD = (CLKCONCMD_32MHZ | OSC_32KHZ); /* Select 32MHz XOSC and the source for 32K clock */ \
    while (CLKCONSTA != (CLKCONCMD_32MHZ | OSC_32KHZ)); /* Wait for the change to be effective */ \
    SLEEPCMD |= OSC_PD; /* turn off 16MHz RC */ \
    \
    /* Turn on cache prefetch mode */ \
    PREFETCH_ENABLE(); \
    \
    INIT_PWM_PINS(); \
    \
    /* Set PA/LNA HGM control P0_7 */ \
    P0DIR |= BV(7); \
    \
    /* setup RF frontend if necessary */ \
    HAL_BOARD_RF_FRONTEND_SETUP(); \
    }

    #endif

    #if defined (PWM_ALT2)
    #define INIT_PWM_PINS() \
    { \
    /* Drive LED1 from timer 1 ch2 and LED2 from timer 1 ch1 */ \
    /* unfortunitly we can not connect any other leds to the timer PWM */ \
    /* on the SmartRF05EB/BB */ \
    // PERCFG |= 0x40; /* set timer 1 to use alt configuration*/ \
    // P1SEL |= ( 0x1 | 0x2 ); \
    // P1DIR |= ( 0x1 | 0x2 );

    /*6.5K PWM with 50% duty cycle to P1.1 with CC2530 Timer 1 */ reference from Mr YK site

    PERCFG |= BV(6); // Select Timer 1 Alternative 2 location
    P2DIR = (P2DIR & ~0xC0) | 0x80; // Give priority to Timer 1
    P1SEL |= BV(1); // Set P1_1 to peripheral
    T1CC0L = 0x3A; // PWM signal period
    T1CC0H = 0x01;
    T1CC1L = 0x9D; // PWM duty cycle
    T1CC1H = 0x00;
    T1CCTL1 = 0x1c;
    T1CTL |= (BV(2)|0x03); // divide with 128 and to do i up-down mode
    }
    #else
    #define INIT_PWM_PINS() \
    { \
    PERCFG &= ~(0x40); /* bit 6 = 0 */ \
    P0SEL |= ( 0x08 | 0x10 | 0x20 | 0x40); /* P0.3:6 */ \
    P0DIR |= ( 0x08 | 0x10 | 0x20 | 0x40); /* P0.3:6 */ \
    }

    #endif

    /* ----------- Debounce ---------- */
    #define HAL_DEBOUNCE(expr) { int i; for (i=0; i<500; i++) { if (!(expr)) i = 0; } }

    /* ----------- Push Buttons ---------- */
    #define HAL_PUSH_BUTTON1() (PUSH1_POLARITY (PUSH1_SBIT))
    #define HAL_PUSH_BUTTON2() (0)
    #define HAL_PUSH_BUTTON3() (0)
    #define HAL_PUSH_BUTTON4() (0)
    #define HAL_PUSH_BUTTON5() (0)
    #define HAL_PUSH_BUTTON6() (0)

    /* ----------- LED's ---------- */
    #if defined (PWM_ALT2)

    #define GREEN_LED HAL_T1_CH2
    #define RED_LED HAL_T1_CH1
    #define BLUE_LED HAL_T1_CH4 //Not connected on SmartRF05
    #define WHITE_LED HAL_T1_CH4
    #define ENABLE_LAMP P1SEL |= ( 0x1 | 0x2 );

    #define DISABLE_LAMP P1SEL &= ~( 0x1 | 0x2 );\
    P1 &= ~( 0x1 | 0x2 ); /* P0.3:6 */
    #else
    #define GREEN_LED HAL_T1_CH3
    #define RED_LED HAL_T1_CH1
    #define BLUE_LED HAL_T1_CH2
    #define WHITE_LED HAL_T1_CH4

    #if defined (WHITE_LED_ONLY)
    #define ENABLE_LAMP P0SEL |= ( 0x40); /* P0.6 */
    #else
    #define ENABLE_LAMP P0SEL |= ( 0x08 | 0x10 | 0x20 | 0x40); /* P0.3:6 */
    #endif

    #define DISABLE_LAMP P0SEL &= ~( 0x08 | 0x10 | 0x20 | 0x40); /* P0.3:6 */\
    P0 &= ~( 0x08 | 0x10 | 0x20 | 0x40); /* P0.3:6 */

    #endif

  • I would suggest you to put the following defines in the beginning of hal_board_cfg.h and out of all "#if defined ..."

    #define GREEN_LED HAL_T1_CH3
    #define RED_LED HAL_T1_CH1
    #define BLUE_LED HAL_T1_CH2
    #define WHITE_LED HAL_T1_CH4

    #if defined (WHITE_LED_ONLY)
    #define ENABLE_LAMP P0SEL |= ( 0x40); /* P0.6 */
    #else
    #define ENABLE_LAMP P0SEL |= ( 0x08 | 0x10 | 0x20 | 0x40); /* P0.3:6 */
    #endif

    #define DISABLE_LAMP P0SEL &= ~( 0x08 | 0x10 | 0x20 | 0x40); /* P0.3:6 */\
    P0 &= ~( 0x08 | 0x10 | 0x20 | 0x40); /* P0.3:6 */
  • Hi Mr Yk,

    I almost got fix the errors. I had made change in an incorrect hal_board file. At any moment I changed the target hal_board file. Sorry for that Mr YK!!!!

    I included at beginning of the hal_boarg_cfg file, at the CC2530EB Target, those code lines:

    /* ------------------------------------------------------------------------------------------------
    * CC2530 PWM Definitions
    * ------------------------------------------------------------------------------------------------
    */
    #define GREEN_LED HAL_T1_CH3
    #define RED_LED HAL_T1_CH1
    #define BLUE_LED HAL_T1_CH2
    #define WHITE_LED HAL_T1_CH4
    #if defined (WHITE_LED_ONLY)

    #define ENABLE_LAMP P0SEL |= ( 0x40); /* P0.6 */
    #else

    #define ENABLE_LAMP P0SEL |= ( 0x08 | 0x10 | 0x20 | 0x40); /* P0.3:6 */
    #endif

    #define DISABLE_LAMP P0SEL &= ~( 0x08 | 0x10 | 0x20 | 0x40); /* P0.3:6 */\
    P0 &= ~( 0x08 | 0x10 | 0x20 | 0x40); /* P0.3:6 */

    But during compiler process I got some errors:

    Error[e46]: Undefined external "HalTimer1Init::?relay" referred in zcl_TTa0003 ( C:\Texas Instruments\Z-Stack Home 1.2.2a.44539\Projects\zstack\HomeAutomation\
    Error[e46]: Undefined external "halTimer1SetChannelDuty::?relay" referred in zcl_TTa0003 ( C:\Texas Instruments\Z-Stack Home 1.2.2a.44539\Projects\zstack\

    The above errors have a correlation with those code lines in my C file:

    #if (defined HAL_BOARD_ZLIGHT) || (defined HAL_PWM)
    HalTimer1Init( 0 );
    halTimer1SetChannelDuty( WHITE_LED, 0 );
    halTimer1SetChannelDuty( RED_LED, 0 );
    halTimer1SetChannelDuty( BLUE_LED, 0 );
    halTimer1SetChannelDuty( GREEN_LED, 0 );

     I can take out above code lines? Or it will damage my PWM functions?

    BR

    Alex

  • If you don't use those lines, how do you implement PWM functions?
  • Hi Mr YK,

    I had success to compiler out after deleted the part of code lines as I said you before. 

    I response to your last question,"how do you implement PWM functions?":

    I didn´t implement it yet in the correctly "CC2530EB/hal_board_cfg.h" file. I tried to do it bellow the macros but I didn´t have a success.

    In the wrong  file that i used (CC2530PMP4712/hal_board_cfg.h) has a part of code that have a PWM lines (at INIT_PWM_PINS), but in a correctly target file is not have),. 

    Would you can advise me how and where i need to include the PWM setting correctly in CC2530EB/hal_board_cfg.h file?

    #if defined (PWM_ALT2)

    #define INIT_PWM_PINS() \
    { \
    /* Drive LED1 from timer 1 ch2 and LED2 from timer 1 ch1 */ \
    /* unfortunitly we can not connect any other leds to the timer PWM */ \
    /* on the SmartRF05EB/BB */ \

    /*6.5K PWM with 50% duty cycle to P1.1 with CC2530 Timer 1 */

    PERCFG |= BV(6); // Select Timer 1 Alternative 2 location
    P2DIR = (P2DIR & ~0xC0) | 0x80; // Give priority to Timer 1
    P1SEL |= BV(1); // Set P1_1 to peripheral
    T1CC0L = 0x3A; // PWM signal period
    T1CC0H = 0x01;
    T1CC1L = 0x9D; // PWM duty cycle
    T1CC1H = 0x00;
    T1CCTL1 = 0x1c;
    T1CTL |= (BV(2)|0x03); // divide with 128 and to do i up-down mode
    }

  • The example in sunmaysky.blogspot.com/.../how-to-output-pwm-from-cc2530.html uses very fundamental registers to operate PWM. I would suggest you to use PWM APIs in hal_timer.c/hal_timer.h of Z-Stack Lighting to implement PWM LED control.
  • Thanks a lot Mr Yk.

    Do you have example to call those API functions?
  • You can refer to SampleApp in Z-Stack Lighting.
  • Hi Mr YK,

    thanks a lot, I´ll working on SampleAPP do study PWM API. 

    I´ll closing this issue.

    BR

    Alex