打印

MSP430USB例程的语法疑问

[复制链接]
808|10
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
Rospiers|  楼主 | 2018-8-20 12:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
MSP430提供的USB官方例程里有一句语法看不懂?求指教!!!里面时钟具体是怎么配置的?我只看见一句话USBHAL_initClocks(8000000);   // Config clocks. MCLK=SMCLK=FLL=8MHz; ACLK=REFO=32kHz,这句话如何理解???
具体程序例程见下面:
/* --COPYRIGHT--,BSD
* Copyright (c) 2014, Texas Instruments Incorporated
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* *  Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
*
* *  Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
*
* *  Neither the name of Texas Instruments Incorporated nor the names of
*    its contributors may be used to endorse or promote products derived
*    from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* --/COPYRIGHT--*/
/*
* ======== main.c ========
* "Simple Send"
*
* This example shows a very simplified way of sending.  This might be used in
* an application where the main functionality doesn't change much if a USB
* host is present or not.  It simply calls USBCDC_sendDataInBackground(); if no
* host is present, it simply carries on.
*
* Simply build, run, and attach to a USB host.  Run a terminal application
* and open the COM port associated with this device.  (In Windows, check the
* Device Manager.)  The time should be displayed, every second.
*
* Note that this code is identical to example H9, except for one line of
* code:  the call to USBCDC_sendDataInBackground() instead of
* USBHID_sendDataInBackground().  This reflects the symmetry between writing
* MSP430 code for CDC vs. HID-Datapipe.
*
*+----------------------------------------------------------------------------+
* Please refer to the MSP430 USB API Stack Programmer's Guide, located
* in the root directory of this installation for more details.
* ---------------------------------------------------------------------------*/

#include <string.h>
#include "driverlib.h"

#include "USB_config/descriptors.h"
#include "USB_API/USB_Common/device.h"
#include "USB_API/USB_Common/usb.h"                     //USB-specific functions
#include "USB_API/USB_CDC_API/UsbCdc.h"
#include "USB_app/usbConstructs.h"

/*
* NOTE: Modify hal.h to select a specific evaluation board and customize for
* your own board.
*/
#include "hal.h"

// Function declarations
void convertTimeBinToASCII(uint8_t* timeStr);
void initRTC(void);

// Application globals
volatile uint8_t hour = 4, min = 30, sec = 00;  // Real-time clock (RTC) values.  4:30:00
volatile uint8_t bSendTimeToHost = FALSE;       // RTC-->main():  "send the time over USB"
uint8_t timeStr[9];                    // Stores the time as an ASCII string

/*
* ======== main ========
*/
void main(void)
{
    WDT_A_hold(WDT_A_BASE); //Stop watchdog timer

    // Minimum Vcore setting required for the USB API is PMM_CORE_LEVEL_2
    PMM_setVCore(PMM_CORE_LEVEL_2);
    USBHAL_initPorts();           // Config GPIOS for low-power (output low)
    USBHAL_initClocks(8000000);   // Config clocks. MCLK=SMCLK=FLL=8MHz; ACLK=REFO=32kHz
    USB_setup(TRUE,TRUE);  // Init USB & events; if a host is present, connect
    initRTC();             // Start the real-time clock

    __enable_interrupt();  // Enable interrupts globally

    while (1)
    {
        // Enter LPM0, which keeps the DCO/FLL active but shuts off the
        // CPU.  For USB, you can't go below LPM0!
        __bis_SR_register(LPM0_bits + GIE);

        // If USB is present, sent the time to the host.  Flag is set every sec
        if (bSendTimeToHost)
        {
            bSendTimeToHost = FALSE;
            convertTimeBinToASCII(timeStr);

            // This function begins the USB send operation, and immediately
            // returns, while the sending happens in the background.
            // Send timeStr, 9 bytes, to intf #0 (which is enumerated as a
            // COM port).  1000 retries.  (Retries will be attempted if the
            // previous send hasn't completed yet).  If the bus isn't present,
            // it simply returns and does nothing.
            if (USBCDC_sendDataInBackground(timeStr, 9, CDC0_INTFNUM, 1000))
            {
              _NOP();  // If it fails, it'll end up here.  Could happen if
                       // the cable was detached after the connectionState()
            }           // check, or if somehow the retries failed
        }
    }  //while(1)
}  //main()


// Starts a real-time clock on TimerA_0.  Earlier we assigned ACLK to be driven
// by the REFO, at 32768Hz.  So below we set the timer to count up to 32768 and
// roll over; and generate an interrupt when it rolls over.
void initRTC(void)
{
    TA0CCR0 = 32768;
    TA0CTL = TASSEL_1+MC_1+TACLR; // ACLK, count to CCR0 then roll, clear TAR
    TA0CCTL0 = CCIE;              // Gen int at rollover (TIMER0_A0 vector)
}


// Timer0 A0 interrupt service routine.  Generated when TimerA_0 (real-time
// clock) rolls over from 32768 to 0, every second.
#if defined(__TI_COMPILER_VERSION__) || (__IAR_SYSTEMS_ICC__)
#pragma vector=TIMER0_A0_VECTOR
__interrupt void TIMER0_A0_ISR (void)
#elif defined(__GNUC__) && (__MSP430__)
void __attribute__ ((interrupt(TIMER0_A0_VECTOR))) TIMER0_A0_ISR (void)
#else
#error Compiler not found!
#endif
{
    if (sec++ == 60)
    {
        sec = 0;
        if (min++ == 60)
        {
            min = 0;
            if (hour++ == 24)
            {
                hour = 0;
            }
        }
    }

    bSendTimeToHost = TRUE;                 // Time to update
    __bic_SR_register_on_exit(LPM3_bits);   // Exit LPM
}


// Convert a number 'bin' of value 0-99 into its ASCII equivalent.  Assumes
// str is a two-byte array.
void convertTwoDigBinToASCII(uint8_t bin, uint8_t* str)
{
    str[0] = '0';
    if (bin >= 10)
    {
        str[0] = (bin / 10) + 48;
    }
    str[1] = (bin % 10) + 48;
}


// Convert the binary globals hour/min/sec into a string, of format "hr:mn:sc"
// Assumes str is an nine-byte string.
void convertTimeBinToASCII(uint8_t* str)
{
    uint8_t hourStr[2], minStr[2], secStr[2];

    convertTwoDigBinToASCII(hour, hourStr);
    convertTwoDigBinToASCII(min, minStr);
    convertTwoDigBinToASCII(sec, secStr);

    str[0] = hourStr[0];
    str[1] = hourStr[1];
    str[2] = ':';
    str[3] = minStr[0];
    str[4] = minStr[1];
    str[5] = ':';
    str[6] = secStr[0];
    str[7] = secStr[1];
    str[8] = '\n';
}


/*
* ======== UNMI_ISR ========
*/
#if defined(__TI_COMPILER_VERSION__) || (__IAR_SYSTEMS_ICC__)
#pragma vector = UNMI_VECTOR
__interrupt void UNMI_ISR (void)
#elif defined(__GNUC__) && (__MSP430__)
void __attribute__ ((interrupt(UNMI_VECTOR))) UNMI_ISR (void)
#else
#error Compiler not found!
#endif
{
        switch (__even_in_range(SYSUNIV, SYSUNIV_BUSIFG )) {
        case SYSUNIV_NONE:
                __no_operation();
                break;
        case SYSUNIV_NMIIFG:
                __no_operation();
                break;
        case SYSUNIV_OFIFG:

                UCS_clearFaultFlag(UCS_XT2OFFG);
                UCS_clearFaultFlag(UCS_DCOFFG);
                SFR_clearInterrupt(SFR_OSCILLATOR_FAULT_INTERRUPT);
                break;
        case SYSUNIV_ACCVIFG:
                __no_operation();
                break;
        case SYSUNIV_BUSIFG:
                // If the CPU accesses USB memory while the USB module is
                // suspended, a "bus error" can occur.  This generates an NMI.  If
                // USB is automatically disconnecting in your software, set a
                // breakpoint here and see if execution hits it.  See the
                // Programmer's Guide for more information.
                SYSBERRIV = 0;  // Clear bus error flag
                USB_disable();  // Disable
        }
}

//Released_Version_5_00_01

相关下载

相关帖子

沙发
Lewisnx| | 2018-8-20 12:20 | 只看该作者
虽然程序没有看懂,但是调试结果出来了,MCLK=SMCLK=DCOCLKDIV=8MHz;,FLL Reference select is REFLCLK. ACLK=REFO=32kHz

使用特权

评论回复
板凳
dirtwillfly| | 2018-8-20 15:47 | 只看该作者
USBHAL_initClocks(8000000); 是初始化usb模块的时钟

使用特权

评论回复
地板
CCompton| | 2018-8-20 16:21 | 只看该作者
USB通讯时,单片机需要外界高频晶振4MHZ,TONGGUO PPL锁相环为USB通信模块配置精确的48MHZ的参考时钟

使用特权

评论回复
5
Richardd| | 2018-8-20 16:33 | 只看该作者
USB通信模式模块结构框图中可以看出提供48MHZ PLL 是有XT1和XT2控制的。我理解的意思是MSP430自带的USB模块通讯是需要外部晶振的。并且要通过寄存器设置,使得能够达到48MHZ PLL

使用特权

评论回复
6
Mattheww| | 2018-8-20 16:47 | 只看该作者
请问usb例程哪里可以找到

使用特权

评论回复
7
firstblood| | 2018-8-22 23:18 | 只看该作者
这个例程还是蛮不错的,赞一个的

使用特权

评论回复
8
angerbird| | 2018-8-22 23:19 | 只看该作者
这时钟配置的不是通过分频倍频实现的么?

使用特权

评论回复
9
smilingangel| | 2018-8-23 22:11 | 只看该作者
这USB例程的分享还是值得点赞的

使用特权

评论回复
10
userwjp| | 2019-4-3 11:11 | 只看该作者
能不能请教下这个例程是哪里找来的啊。我在TI的网站上找了好久都没有找到这个例程。
感谢。

使用特权

评论回复
11
comparison| | 2019-4-3 15:35 | 只看该作者
没怎么用过MSP430的USB,一起学习下。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

116

主题

377

帖子

0

粉丝