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.

MSP430F5638: MSP low-power microcontroller forum

Part Number: MSP430F5638
Other Parts Discussed in Thread: MSP430F6638

I've designed and built a custom SBC using 1 msp430f5638 which is driving a 480x272 graphical LCD. All is working pretty well, but the LCD redraws the entire screen slowly. By default the MCU is using DCO at about 1 MHz. The SBC has external crystals for XT1 (32.768 kHz), and XT2 (20 MHz).

Switching the MCU from DCO to XT2 and the LCD just gets scrambled. There are two delays in the LCD init routine which I haven't experimented with yet. Wanted to get the MCU running with XT2 divided down to about 1 MHz and see if that works, and then try speeding it up with a smaller divisor. I got somewhat strange results from this.

Code to set UCSCTL5 dividers

// slow down XT2, divide both MCLK and SMCLK by divisor
if(XT2_xtal_on)
{
//UCSCTL5 = 0x0044; // divide both by 16 NOT WORKING, stalls out, locks up LCD
//UCSCTL5 = 0x0033; // divide both by 8 this works, semi-fast lcd init, medium SMCLK on scope
//UCSCTL5 = 0x0022; // divide both by 4 THIS WORKS!!! fast lcd init, fast SMCLK on scope
//UCSCTL5 = 0x0011; // divide both by 2 THIS runs but slower than /4 ???, LCD extremely slow, slow SMCLK on scope
//UCSCTL5 = 0x0000; // divide both by 1, identical to default, should lock up lcd, lcd goes whacko

// just do MCLK, but not available on MCU pin, so must do both to see it
UCSCTL5 = 0x0022; // divide MCLK by 4 THIS WORKS!!! fast lcd init, fast SMCLK on scope
}

This specific test program is putting the MCU into LPM3 and waking up from the RTC every 2 seconds, so can't accurately measure the SMCLK period and frequency on the scope. But qualitatively, why are the 2, 4, 8 divisors not behaving linearly?

/2 is very slow @ 10 Mhz

/4 is real fast @ 5 MHz

/8 is medium speed  @ 2.5 Mhz

/16 seems to be dividing DCO, not XT2, extremely slow clock on scope, @ 16 MHz

Why is it doing this? Maybe I need to look at making the two LCD delays smaller for the 10 MHz speed?

Ted

setup-430-clocks-00.c
/*********************************************************************
*                                                                    *
*                        Ted Fryberger, PE                           *
*                          DeepSoft, LLC                             *
*                     6259 Deep River Canyon                         *
*                       Columbia, MD 21045                           *
*                         443-917-2902                               *
*                      www.DeepSoftInc.com                           *
*                      tkf@deepsoftinc.com                           *
*                                                                    *
**********************************************************************
*                                                                    *
*                      setup-430-clocks-xx.c                         *
*                                                                    *
*                                                                    *
*         Copyright 1987 - 2021 by Ted Fryberger &                   *
*         DeepSoft, LLC; DeepSoft, Inc; & TKF Systems                *
*                        All Rights Reserved                         *
*                                                                    *
*  This code is proprietary and confidential to Ted Fryberger &      *
*  DeepSoft, LLC. Possession or duplication of this code is          *
*  prohibited without the written consent of Ted Fryberger &         *
*  DeepSoft, LLC. All included functions and modules are also        *
*  proprietary and confidential to DeepSoft, LLC.                    *
*                                                                    *
*  Engineer: Ted Fryberger        Current last revision: 1/06/2021   *
*                                 Prior last revision:               *
*                                                                    *
*********************************************************************/

/************************** MSP430 Clocks ******************************/
// XT1 = 32.768 KHz xtal
// XT2 = 20 MHz xtal, max is 32 MHz
// DCO = MCLK = SMCLK ~ 1.045 MHz
// MCLK     master clock, high frequency xtal, or DCO
// SMCLK    subsystem master clock
// DCO      digitally controlled oscillator = MCLK
// ACLK     auxiliary clock 32.768 KHz, external xtal required, used for precise timing RTC

/************************** MSP430 Clock Settings ***********************/
// ACLK = XT1 = 32.768 KHz xtal for RTC_B
// MCLK = XT2 = 20 MHz xtal, max is 32 MHz
// SMCLK = XT2 = 20 MHz xtal
// DCO = MCLK = SMCLK ~ 1.045 MHz
// want to be able to switch MCU-1 from XT2 to DCO as needed to save power
// RTC_B sets system timing, requires XT1

/************************** DC2 Test Results ****************************/
// no dividers used for these tests, UCS default settings unless set in code below
// ACLK = XT1 @ XT1 TP
// 0.6 div x 50 us/div = 30 us = 33,333 kHz
// 0.4 div x 1v/div = 0.40 volts

// SMCLK = MCLK = XT2 @ P3.4 S2 Pin 3
// 0.25 div x 0.2 us/div = 0.05 us = 20 MHz
// 0.4 div x 5v/div = 2.0 volts

// SMCLK = MCLK = XT2 @ XT2 TP
// 0.22 div x 0.2 us/div = 0.044 us = 22.73 MHz
// 0.22 div x 0.5v/div = 0.11 volts

// STP3005DH power supply
// 3.34 v @ 162mA with LCD
// 3.34v @ 0mA without LCD

// with SMCLK = MCLK = DCO
// nothing out of S2 P3.4
// must disable XT2 pins at a minimum to use DCO
// SMCLK = MCLK = DCO @ P3.4 S2 Pin 3
// 1.9 div x .5 us/div = 0.95 us = 2.105 MHz
// 0.6 div x 5v/div = 3.0 v
// STP3005DH power supply
// 3.34 v @ 163mA with LCD

/************************** Test Conclusions ******************************/
// XT1 has to be enabled and used all the time for RTC
// must be able to switch between XT2 and DCO dynamically in the program
// use global var to set either one, include any required XT2 settings
// or
// may be better off using separate functions to turn either XT2 or DCO on

#include <msp430f5638.h>

// constants to put ACLK and SMCLK out to port pins
#define ACLK_OUT    1
#define SMCLK_OUT   1

// relevant global var
// 0 = DCO
// 1 = XT2
extern unsigned int XT2_xtal_on;

// initial setup of 430 clocks
// ACLK = XT1
// MCLK = XT2 or DCO
// SMCLK = XT2 or DCO
// need another function to dynamically change this after startup
void setup_430_clocks(void)
{
    // put clock signals onto out ports
#if(ACLK_OUT)
    P1DIR |= BIT0;              // ACLK out to P1.0
    P1SEL |= BIT0;
#endif

#if(SMCLK_OUT)
    P3DIR |= BIT4;              // SMCLK out to P3.4, S2 pin 3
    P3SEL |= BIT4;
#endif

    // unlock XT1 pins
    // battery backup registers, UG Table 3-2, p. 130
    // BAKCTL Register bits
    // 15-4 reserved read as 0
    // 3    BAKDIS  disable backup supply switching, reset to 0 after complete power cycle
    //          0   backup supply switching enabled
    //          1   backup supply switching disabled, backup subsystem always powered from Vcc
    // 2    BAKADC  battery backup supply to ADC
    //          0   Vbat measurement disabled
    //          1   Vbat measurement enabled
    // 1    BAKSW   manual switch to battery backup supply
    //          0   switching is automatic
    //          1   switch to backup battery
    // 0    LOCKBAK lock battery backup subsystem, only write as 0 after RTC configured
    //              SVSH must be active when LOCKBAK is cleared
    //              LOCKBAK is always set to 1 by hardware after core has powered down from DVCC or LPMx.5
    //          0   backup subsystem not locked
    //          1   backup subsystem locked
    // all go to 0 on Reset

    // BAKCTL Register
    //     1111 11
    // msb 5432 1098 7654 3210 lsb
    //     0000 0000 0000 1100 = 0x000C
    // if locked, unlock battery backup system

    // where are these set by default?

    // Unlock XT1 pins for operation
    while(BAKCTL & LOCKBAK)                     // both must eval to 1 for AND to be true
        BAKCTL &= ~(LOCKBAK);                   // clear bit low to unlock

    // Port select XT2 pins, disable for DCO
    if(XT2_xtal_on)
    {
        P7SEL |= BIT2 + BIT3;
    }

    // UCSCTL5
    // sets any divisors on ACLK, ACLK/n, MCLK, SMCLK
    //    15 Reserved
    //    14-12 DIVPA ACLK source divider, available at external pin, 1,2,4,8,16,32
    //        000b = fACLK/1
    //        001b = fACLK/2
    //        010b = fACLK/4
    //        011b = fACLK/8
    //        100b = fACLK/16
    //        101b = fACLK/32
    //        110b = Reserved for future use. Defaults to fACLK/32.
    //        111b = Reserved for future use. Defaults to fACLK/32.
    //    11 Reserved
    //    10-8 DIVA ACLK source divider
    //        000b = fACLK/1
    //        001b = fACLK/2
    //        010b = fACLK/4
    //        011b = fACLK/8
    //        100b = fACLK/16
    //        101b = fACLK/32
    //        110b = Reserved for future use. Defaults to fACLK/32.
    //        111b = Reserved for future use. Defaults to fACLK/32.
    //    7 Reserved
    //    6-4 DIVS SMCLK source divider
    //        000b = fSMCLK/1
    //        001b = fSMCLK/2
    //        010b = fSMCLK/4
    //        011b = fSMCLK/8
    //        100b = fSMCLK/16
    //        101b = fSMCLK/32
    //        110b = Reserved for future use. Defaults to fSMCLK/32.
    //        111b = Reserved for future use. Defaults to fSMCLK/32.
    //    3 Reserved
    //    2-0 DIVM MCLK source divider
    //        000b = fMCLK/1
    //        001b = fMCLK/2
    //        010b = fMCLK/4
    //        011b = fMCLK/8
    //        100b = fMCLK/16
    //        101b = fMCLK/32
    //        110b = Reserved for future use. Defaults to fMCLK/32.
    //        111b = Reserved for future use. Defaults to fMCLK/32.

    // currently LCD is running at DCO frequencies of 1-2 MHz, but not XT2 of 20 MHz
    // probably need to adjust delays in LCD_init_3()
    // use a divider to set XT2 down to DCO and try that
    //     1111 11
    // msb 5432 1098 7654 3210 lsb
    //     0000 0000 0100 0100 = 0x0044 divide both by 16
    //     0000 0000 0011 0011 = 0x0033 divide both by 8
    //     0000 0000 0010 0010 = 0x0022 divide both by 4
    //     0000 0000 0001 0001 = 0x0011 divide both by 2
    //     0000 0000 0000 0000 = 0x0000 divide both by 1, same as default of course

    // slow down XT2, divide both MCLK and SMCLK by divisor
    if(XT2_xtal_on)
    {
        //UCSCTL5 = 0x0044;       // divide both by 16 NOT WORKING, stalls out, locks up LCD
        //UCSCTL5 = 0x0033;       // divide both by 8 this works, semi-fast lcd init, medium SMCLK on scope
        //UCSCTL5 = 0x0022;       // divide both by 4 THIS WORKS!!! fast lcd init, fast SMCLK on scope
        //UCSCTL5 = 0x0011;       // divide both by 2 THIS runs but slower than /4 ???, LCD extremely slow, slow SMCLK on scope
        //UCSCTL5 = 0x0000;       // divide both by 1, identical to default, should lock up lcd, lcd goes whacko

        // just do MCLK, but not available on MCU pin, so must do both to see it
        UCSCTL5 = 0x0022;       // divide MCLK by 4 THIS WORKS!!! fast lcd init, fast SMCLK on scope
    }

    // above is slowing down 1 MHz / 16 = 62.5 khz
    // NOT slowing down 20 MHz to 1.25 MHz??????

    // UCSCTL6
    // 15-14 XT2DRIVE XT2 oscillator drive current, high initially, can be lowered, based on speed
    //      0   00b lowest current, 4-8 MHz
    //      1   01b increased current, 8-16 MHz
    //      2   10b increased current, 16-24 MHz, use this for 20 MHz xtal
    //      3   11b max current, 24-32 Mhz
    // 13 reserved
    // 12 XT2BYPASS
    //      0 external crystal
    //      1 external clock signal
    // 11-9 reserved
    //  8 XT2OFF
    //      0 on
    //      1 off
    // 7-6 XT1DRIVE XT1 oscillator drive current, high initially, can be lowered, based on speed
    //      00b lowest current, 4-8 MHz
    //      01b increased current, 8-16 MHz
    //      10b increased current, 16-24 MHz
    //      11b max current, 24-32 Mhz
    //  5 XTS XT1 mode select
    //      0 LF mode, XCAP bits define capacitance at XIN and XOUT pins
    //      1 HF mode, XCAP bits not used
    //  4 XTBYPASS
    //      0 XT1 sourced from external crystal
    //      1 XT1 sourced from external clock signal
    // 3-2 XCAPx oscillator capacitor selection, 3 bits select internal cap for LF crystal in LF mode, see DS
    //      00b 0   2 pf internal caps, DS p. 53 for XT1
    //      01b 1   5.5 pf
    //      10b 2   8.5 pf
    //      11b 3   12 pf
    //  1 SMCLKOFF
    //      0 SMCLK on
    //      1 SMCLK off
    //  0 XT1OFF
    //      0 XT1 on
    //      1 XT1 off, if not used as a source, or FLL reference

    //     1111 11
    // msb 5432 1098 7654 3210 lsb
    //     0000 0000 0000 0000 = 0x0000

    // Set XT2 On or not
    if(XT2_xtal_on)
    {
        UCSCTL6 &= ~XT2OFF;
    }

    // Always set XT1 On
    UCSCTL6 &= ~XT1OFF;

    // Set XT1 internal load cap
    // external 12 pf on PCB, assume internal parallel, will add internal cap
    // internal 2 pf, parasitic bond and package capacitance, requires external caps, DS p. 53, footnote 6
    UCSCTL6 |= XCAP_0;

    // UCSCTL7
    // 15-4 Reserved
    // 3  XT2OFFG XT2 oscillator fault flag
    //      0 no fault condition after last reset
    //      1 XT2 fault after last reset
    // 2  XT1HFOFFG XT1 oscillator fault flag in HF mode
    //      0 no fault condition after last reset
    //      1 XT1 HF fault after last reset
    // 1  XT1LFOFFG XT1 oscillator fault flag in LF mode
    //      0 no fault condition after last reset
    //      1 XT1 LF fault after last reset
    // 0  DCOFFG DCO fault flag
    //      0 no fault condition after last reset
    //      1 DCO fault after last reset

    // SFR Special Function Register
    // SFRIE1  interrupt enable
    //    SFRIE1_L
    //    SFRIE1_H
    // SFRIFG1 interrupt flag
    //    SFRIFG1_L   (IFG1)
    //    SFRIFG1_H   (IFG2)
    // SFRRPCR reset pin control
    //    SFRRPCR_L
    //    SFRRPCR_H

    // SFRIE1 Register
    // UG, p. 84, Table 1-12
    // 15-8 Reserved
    // 7    JMBOUTIE    JTAG mailbox output interrupt enable flag
    //      0 interrupts disabled
    //      1 interrupts enabled
    // 6    JMBINIE    JTAG mailbox input interrupt enable flag
    //      0 interrupts disabled
    //      1 interrupts enabled
    // 5    ACCVIE    Flash controller access violation interrupt enable flag
    //      0 interrupts disabled
    //      1 interrupts enabled
    // 4    NMIIE    NMI pin interrupt enable flag
    //      0 interrupts disabled
    //      1 interrupts enabled
    // 3    VMAIE    Vacant memory access interrupt enable flag
    //      0 interrupts disabled
    //      1 interrupts enabled
    // 2    Reserved
    // 1    OFIE    Oscillator fault interrupt enable flag
    //      0 interrupts disabled
    //      1 interrupts enabled
    // 0    WDTIE    Watchdog timer interrupt enable flag
    //      0 interrupts disabled
    //      1 interrupts enabled

    // SFRIFG1 Register
    // UG, p. 85, Table 1-13
    // 7  JMBOUTIFG   JTAG mailbox output interrupt flag
    //      0 no interrupt pending
    //      1 interrupt pending
    // 6  JMBINIFG    JTAG mailbox input interrupt flag
    //      0 no interrupt pending
    //      1 interrupt pending
    // 5  Reserved
    // 4  NMIIFG      NMI pin interrupt
    //      0 no interrupt pending
    //      1 interrupt pending
    // 3  VMAIFG  vacant memory access interrupt flag
    //      0 no interrupt pending
    //      1 interrupt pending
    // 2  Reserved
    // 1  OFIFG       oscillator fault interrupt flag
    //      0 no interrupt pending
    //      1 interrupt pending
    // 0  WDTIFG    watchdog timer interrupt flag
    //      0 no interrupt pending
    //      1 interrupt pending

    // SFRRPCR Register
    // UG, p. 87, table 1-14
    //15-4  Reserved
    // 3    SYSRSTRE    reset pin resistor enable
    //      0 pullup / pulldown resistor at RST/NMI is disabled
    //      1 pullup / pulldown resistor at RST/NMI is enabled
    // 2    SYSRSTUP    reset resistor pin pullup / pulldown
    //      0 pulldown resistor selected
    //      1 pullup resistor selected
    // 1    SYSNMIIES   NMI edge select
    //      when SYSNMI = 1, modifying this bit can cause an NMI
    //      clear SYSNMI = 0 to avoid this NMI
    //      0 NMI on rising edge
    //      1 NMI on falling edge
    // 0    SYSNMI      NMI select, picks function for RST/NMI pin
    //      0 reset function
    //      1 NMI function

    // Loop until XT1, XT2, DCO oscillator fault flags are cleared
    do
    {
        UCSCTL7 &= ~(XT2OFFG | XT1LFOFFG | DCOFFG); // Clear XT2, XT1 LF, DCO oscillator fault flags
        SFRIFG1 &= ~OFIFG;                          // Clear overall oscillator fault flag
    }while (SFRIFG1 & OFIFG);                       // Test: are oscillator fault flags clear

    // set XT2 drive current for 20 MHz clock, disable for DCO
    if(XT2_xtal_on)
    {
        UCSCTL6 &= ~XT2DRIVE_2;                         // Set XT2 Drive according to xtal frequency = 20 MHz
    }

    // UCSCTL4
    // 15-11 Reserved
    // 10-8 SELA selects ACLK source
    //    000b = XT1CLK
    //    001b = VLOCLK
    //    010b = REFOCLK
    //    011b = DCOCLK
    //    100b = DCOCLKDIV
    //    101b = XT2CLK when available, otherwise DCOCLKDIV
    //    110b = Reserved for future use. Defaults to XT2CLK when available, otherwise DCOCLKDIV.
    //    111b = Reserved for future use. Defaults to XT2CLK when available, otherwise DCOCLKDIV.
    // 7 Reserved
    // 6-4 SELS selects SMCLK source
    //    000b = XT1CLK
    //    001b = VLOCLK
    //    010b = REFOCLK
    //    011b = DCOCLK
    //    100b = DCOCLKDIV
    //    101b = XT2CLK when available, otherwise DCOCLKDIV
    //    110b = Reserved, Defaults to XT2CLK when available, otherwise DCOCLKDIV.
    //    111b = Reserved, Defaults to XT2CLK when available, otherwise DCOCLKDIV.
    // 3 Reserved
    // 2-0 SELM selects MCLK source
    //    000b = XT1CLK
    //    001b = VLOCLK
    //    010b = REFOCLK
    //    011b = DCOCLK
    //    100b = DCOCLKDIV
    //    101b = XT2CLK when available, otherwise DCOCLKDIV
    //    110b = Reserved, Defaults to XT2CLK when available, otherwise DCOCLKDIV.
    //    111b = Reserved, Defaults to XT2CLK when available, otherwise DCOCLKDIV.

    // below from 5638.h
    //    ACLK source - uses double underscore
    //    SELA__XT1CLK = SELA_0 = 0x0000
    //    SELA__DCOCLK = SELA_3 = 0x0300
    //    SELA__XT2CLK = SELA_5 = 0x0500
    //
    //    SMCLK source
    //    SELS__XT1CLK = SELS_0 = 0x0000
    //    SELS__DCOCLK = SELS_3 = 0x0030
    //    SELS__XT2CLK = SELS_5 = 0x0050
    //
    //    MCLK source
    //    SELM__XT1CLK = SELM_0 = 0x0000
    //    SELM__DCOCLK = SELM_3 = 0x0003
    //    SELM__XT2CLK = SELM_5 = 0x0005

    //     1111 11
    // msb 5432 1098 7654 3210 lsb
    //     0000 0000 0101 0101 = 0x0055

    // set all 3 clock sources for XT1, XT2, DCO
    // XT1 always needed
    if(XT2_xtal_on)
    {
        // use XT2 source for MCLK and SMCLK
        UCSCTL4 |= SELA__XT1CLK | SELS__XT2CLK | SELM__XT2CLK;     // Select ACLK = XT1, SMCLK = MCLK = XT2 sources
    }
    else
    {
        // use DCO source for MCLK and SMCLK
        UCSCTL4 |= SELA__XT1CLK | SELS__DCOCLK | SELM__DCOCLK;     // Select ACLK = XT1, SMCLK = MCLK = DCO sources
    }
}

  • I'm not quite clear on your symptom, but there may be something about your slowing MCLK down to 1MHz/16=65kHz, though it doesn't stay there very long.

    Can you measure SMCLK (P3.4) on a scope? 

  • Because the RTC, LPM3, and 2 second interrupt is complicating understanding the process of running from a divided down XT2 issue, I'm going to create a simpler program that runs continuously and see what I get from that when I try to run from XT2 vs. the DCO.

  • Not running the RTC, and no ISR's, just setup the ports, then setup the clocks, MCLK = SMCLK with a range of equal divisors of XT2, and end with an endless loop, SMCLK out to P3.4 to scope

    XT2 = 20 MHz

    /1 = default divisor, LCD displays junk, no SMCLK out on P3.4, fixed at logic high

    /2  SMCLK = 526 kHz, should be 10 MHz, LCD displays correct data for /2 and all below

    /4  SMCLK = 5 MHz

    /8  SMCLK = 2.5 MHz

    /16  SMCLK = 66.66 kHz, looks like DCO/16, should be 1.25 MHz

    /32  SMCLK = 588 kHz

    So /4, and /8, and /32 all display the correct SMCLK values on P3.4

    The /1 clock may be too fast for the LCD delay values, I'll try making those larger. But why is /16 putting out an SMCLK that looks like DCO/16, not XT2/16? And why is /2 so slow?

  • What I see in the code you posted is you (1)  slow MCLK (running from the DCO) way down (2)  turn on XT2, and wait for it to start (3) assign SMCLK to XT2. During (2) you're running very slow, though crystal startup consumes wall-clock time, not CPU time.

    One thing I didn't notice before is that you set XT2DRIVE=1, which may not be strong enough to drive 20MHz. [Ref User Guide (SLAU208Q) Table 5-9.] I wonder if your crystal is failing over to the DCO. What happens if you remove that line (leave XT2DRIVE=3)?

  • Tried different values for XT2DRIVE with the following results

    // SMCLK @ P3.4
    // /4 XT2DRIVE_0 SMCLK = 5 MHz
    // /4 XT2DRIVE_1 SMCLK = 5 MHz
    // /4 XT2DRIVE_2 SMCLK = 5 MHz
    // /4 XT2DRIVE_3 SMCLK = 270.270 kHz
    // /16 XT2DRIVE_3 SMCLK = 66.66 kHz

    It doesn't seem to like XT2DRIVE_3. 

    Also discovered that if I comment out the UCSCTL5 initialization below, I get 20 MHz at P3.4

    if(XT2_xtal_on)
    {

    // comment this out and get 20 MHz at P3.4

    // 0x0000 should be equivalent to default, but LCD black, P3.4 fixed high, no clock

    UCSCTL5 = 0x0022;
    }

    The MCU is running at XT2 = 20 MHz so that is working, I need to look at the two software delays in the LCD init routine and see if that is the LCD problem. 

    I'm setting up XT1, XT2, and DCO per TI's examples: msp430f665x_ucs_07, msp430f66xx_ucs_06, and msp430f66xx_ucs08. Is there a better sequence to use?

  • Sorry, I don't know the answer. The only F5 I have (Launchpad) has only a 4MHz crystal, so I'm not sure I can replicate any of this.

    A quick experiment might be to set OFIE after the crystal settles, to see if any of these cases results from the oscillator spontaneously failing.

  • Ok, thanks for your help

  • Hi Ted,

    I am a little lost in the thread. It seems that XT2 can't output the divided clock as expected? Is it right.

    Can you share a smallest example. I will try it after I finish my business travel.

    Eason

  • Hi Eason,

    There is a link to my code to setup the 430 clocks in my initial post, next to my name. Lots of comments to ignore, next time I will post a much shorter version stripping out most of the comments.

    The problem is getting the XT2 divisor working for /2 - it results in a slow frequency, should be 10 MHz.

    /4 works and creates 5 MHz, /8 also works creating 2.5 MHz, XT2 with no divisor creates 20 MHz

    Ted

  • Eason,

    I've uploaded a shorter version of the original code, same code, but stripped out hundreds of lines of comments to make it easier to see the code. I like to dissect the UG register tables and insert them into the code  for reference while I'm creating the code for a specific MCU peripheral.

    Ted

    setup-430-clocks-00-short.c
    /************************** MSP430 Clocks ******************************/
    // XT1 = 32.768 KHz xtal
    // XT2 = 20 MHz xtal, max is 32 MHz
    // DCO = MCLK = SMCLK ~1.045 MHz
    // MCLK     master clock, high frequency xtal, or DCO
    // SMCLK    subsystem master clock
    // DCO      digitally controlled oscillator = MCLK
    // ACLK     auxiliary clock 32.768 KHz, external xtal required, used for precise timing RTC
    
    /************************** MSP430 Clock Settings ***********************/
    // ACLK = XT1 = 32.768 KHz xtal for RTC_B
    // MCLK = XT2 = 20 MHz xtal, max is 32 MHz
    // SMCLK = XT2 = 20 MHz xtal
    // DCO = MCLK = SMCLK ~ 1.045 MHz
    
    /************************** DC2 Test Results ****************************/
    // no dividers used for these tests (default divider = 1), UCS default settings unless set in code below
    // ACLK = XT1 @ XT1 TP
    // 0.6 div x 50 us/div = 30 us = 33,333 kHz
    // 0.4 div x 1v/div = 0.40 volts
    
    // SMCLK = MCLK = XT2 @ P3.4 S2 Pin 3
    // no divisor, LCD runs but gets scrambled, but see 20 MHz on P3.4
    // 0.25 div x 0.2 us/div = 0.05 us = 20 MHz
    // 0.4 div x 5v/div = 2.0 volts
    // use 0x0000 for both divisors, LCD blank, P3.4 fixed high
    
    // SMCLK = MCLK = XT2 @ XT2 TP
    // 0.22 div x 0.2 us/div = 0.044 us = 22.73 MHz
    // 0.22 div x 0.5v/div = 0.11 volts
    
    // with SMCLK = MCLK = DCO
    // nothing out of S2 P3.4
    // must disable XT2 pins at a minimum to use DCO
    // SMCLK = MCLK = DCO @ P3.4 S2 Pin 3
    // 1.9 div x .5 us/div = 0.95 us = 2.105 MHz
    // 0.6 div x 5v/div = 3.0 v
    
    /************************** Test XT2DRIVE_x Effects ***********************/
    // SMCLK @ P3.4
    // /4 XT2DRIVE_0    SMCLK = 5 MHz
    // /4 XT2DRIVE_1    SMCLK = 5 MHz
    // /4 XT2DRIVE_2    SMCLK = 5 MHz
    // /4 XT2DRIVE_3    SMCLK = 270.270 kHz
    // /16  XT2DRIVE_3  SMCLK = 66.66 kHz
    
    #include <msp430f5638.h>
    
    // constants to put ACLK and SMCLK out to port pins
    #define ACLK_OUT    1
    #define SMCLK_OUT   1
    
    // relevant global var
    // 0 = DCO
    // 1 = XT2
    extern unsigned int XT2_xtal_on;
    
    // initial setup of 430 clocks
    // ACLK = XT1
    // MCLK = XT2 or DCO
    // SMCLK = XT2 or DCO
    void setup_430_clocks(void)
    {
        // put clock signals onto out ports
    #if(ACLK_OUT)
        P1DIR |= BIT0;              // ACLK out to P1.0
        P1SEL |= BIT0;
    #endif
    
    #if(SMCLK_OUT)
        P3DIR |= BIT4;              // SMCLK out to P3.4, S2 pin 3
        P3SEL |= BIT4;
    #endif
    
        // Unlock XT1 pins for operation
        while(BAKCTL & LOCKBAK)                     // both must eval to 1 for AND to be true
            BAKCTL &= ~(LOCKBAK);                   // clear bit low to unlock
    
        // Port select XT2 pins, disable for DCO
        if(XT2_xtal_on)
        {
            P7SEL |= BIT2 + BIT3;
        }
    
        // slow down XT2, divide both MCLK and SMCLK by equal divisors
        if(XT2_xtal_on)
        {
            //UCSCTL5 = 0x0055;     // divide both by 32, f = 588 kHz
            //UCSCTL5 = 0x0044;     // divide both by 16, f = 66.66 kHz, looks like divides DCO/16 not XT2
            //UCSCTL5 = 0x0033;     // divide both by 8,  f = 2.5 MHz, this works, semi-fast lcd init
            //UCSCTL5 = 0x0022;     // divide both by 4,  f = 5 MHz, THIS WORKS!!! fast lcd init
            //UCSCTL5 = 0x0011;     // divide both by 2,  f = 526 kHz, runs slower than /4
            //UCSCTL5 = 0x0000;     // divide both by 1,  locks up lcd, lcd goes whacko, P3.4 = fixed logic high
    
            // divide both MCLK and SMCLK
            // comment this out to get 20 MHz at P3.4
            // 0x0000 should be equivalent to default, but LCD black, P3.4 fixed high, no clock
            UCSCTL5 = 0x0022;
        }
    
        // Set XT2 On
        if(XT2_xtal_on)
        {
            UCSCTL6 &= ~XT2OFF;
        }
    
        // Always set XT1 On
        UCSCTL6 &= ~XT1OFF;
    
        // Set XT1 internal load cap
        // external 12 pf on PCB, assume internal parallel, will add internal cap
        // internal 2 pf, parasitic bond and package capacitance, requires external caps, DS p. 53, footnote 6
        UCSCTL6 |= XCAP_0;
    
        // Loop until XT1, XT2, DCO oscillator fault flags are cleared
        do
        {
            UCSCTL7 &= ~(XT2OFFG | XT1LFOFFG | DCOFFG); // Clear XT2, XT1 LF, DCO oscillator fault flags
            SFRIFG1 &= ~OFIFG;                          // Clear overall oscillator fault flag
        }while (SFRIFG1 & OFIFG);                       // Test: are oscillator fault flags clear
    
        // set XT2 drive current for 20 MHz clock, disable for DCO
        // 0, 1, 2 work fine, 3 is extremely slow, should be for fastest MCU clock speed
        if(XT2_xtal_on)
        {
            UCSCTL6 &= ~XT2DRIVE_2;                         // Set XT2 Drive according to xtal frequency = 20 MHz
        }
    
        // set all 3 clock sources for XT1, XT2, DCO
        // XT1 always needed
        if(XT2_xtal_on)
        {
            // use XT2 source for MCLK and SMCLK
            // try both defines below, should be identical - they are, /2 still is real slow
            UCSCTL4 |= SELA__XT1CLK | SELS__XT2CLK | SELM__XT2CLK;     // Select ACLK = XT1, SMCLK = MCLK = XT2 sources
            //UCSCTL4 |= SELA__XT1CLK | SELS_5 | SELM_5;     // Select ACLK = XT1, SMCLK = MCLK = XT2 sources
        }
        else
        {
            // use DCO source for MCLK and SMCLK
            UCSCTL4 |= SELA__XT1CLK | SELS__DCOCLK | SELM__DCOCLK;     // Select ACLK = XT1, SMCLK = MCLK = DCO sources
        }
    }
    

  • I will recreate the problem when I finish by business travel. Please wait for a few days.

  • Hi Ted,

    I test your code on MSP430F6638. We only have a 4MHz oscillator.

    After I test, I have meet the problem you met using 20MHz oscillator.

    See from your test result:

    // /4 XT2DRIVE_3 SMCLK = 270.270 kHz
    // /16 XT2DRIVE_3 SMCLK = 66.66 kHz

    /2  SMCLK = 526 kHz

    /16  SMCLK = 66.66 kHz

    expect:

    /32  SMCLK = 588 kHz

    They all seems divided from 1MHz DCO clock.

    My advice is that can you test the oscillator frequency directly, when the output SMCLK doesn't meet the expectation. If you have a low capacitance probe, it will be great.

    Eason 

  • Hi Eason,

    Thanks for looking into this. But it sounds like you are confirming what I found.

    If I put the scope directly on the crystals, I see low voltage and the correct frequencies of 32.768 kHz for XT1, and 20 MHz for XT2.

    The problem is not getting the correct divided down frequency for all divisors, some work ok, others look like they are dividing down DCO of 1 Mhz.

    Divide XT2\2 gives  a low frequency around 500 kHz which is not correct, should be = 10.0 MHz

    Divide XT2\4 gives 5.0 MHz which is correct

    Divide XT2\8 gives 2.5 MHz which is correct

    Divide XT2\16 gives 66 kHz, which is not correct, should be 1.25 MHz

    So the question remains, why is it doing this?

  • Hi Ted,

    Let's focus on one problem: Divide XT2\2 gives  a low frequency around 500 kHz

    Now we are sure the XT2 works fine:

    As we can output XT2CLK from ACLK, MCLK:

    Can you make some try:

    1. Divide XT2\2 for SMCLK, Divide XT2\2 for MCLK to see the output frequency

    2. Divide XT2\2 for SMCLK, Divide XT2\4 for MCLK to see the output frequency

    3. Divide XT2\2 for SMCLK, see XT2OFFG is cleared in debug mode

    4. Check if this problem only happens on one device.

    Eason

  • Hi Eason,

    Thanks for your help. Got a new handheld scope and the MCU divided XT2 clock and code is working fine from XT2/1 down to XT2/32 at both X1 and X10 probe settings. So I tried the code on my lab bench scope again and discovered one of the probes at 10x is not working at all. At 1x and using the other probe, the divided frequencies are all correct. So that problem is fixed. Also discovered it I set UCSCTL5 = 0x0001 both MCU and LCD run fine at 20 MHz, but with 0x0000 the MCU runs fine at 20 MHz, but the LCD gets scrambled. So that problem fixed too.

    Ted

**Attention** This is a public forum