⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lmp_blst.c

📁 IT projecotr reference design.
💻 C
📖 第 1 页 / 共 3 页
字号:
/****************************************************************************/
/*             TEXAS INSTRUMENTS PROPRIETARY INFORMATION                    */
/*                                                                          */
/*  (c) Copyright, Texas Instruments Incorporated, 2007.                    */
/*      All Rights Reserved.                                                */
/*                                                                          */
/*  Property of Texas Instruments Incorporated. Restricted Rights -         */
/*  Use, duplication, or disclosure is subject to restrictions set          */
/*  forth in TI's program license agreement and associated documentation.   */
/****************************************************************************/


/****************************************************************************/
/* lmp_blst.c                                                               */
/*                                                                          */
/* This file contains the interface to the lamp ballast to support variable */
/* illumination lamps/ballasts for the UART-capable system.  Two types of   */
/* instructions - command and query, can be communicated with the ballast.  */
/*                                                                          */
/* Commands includes enable communication, selecting waveform index or      */
/* gain, and programming mailbox for manufacturer specific or any pass      */
/* through addresses within the ballast.                                    */
/*                                                                          */
/* Queries includes reading ballast manufacturer ID, ballast software and   */
/* hardware ID, current waveform ID, index, gain, and status, total         */
/* waveforms stored in the ballast, ballast status, mailbox status,         */
/* mailbox byte, and maximum and minimum gain.                              */
/*                                                                          */
/* Call LMP_BLST_EnableCommunication API to enable access to all other      */
/* commands and queries.                                                    */
/*                                                                          */
/* Call LMP_BLST_SetWaveform to select the waveform stored in the ballast.  */
/*                                                                          */
/* Call LMP_BLST_SetWaveformGain to set the output power level of the lamp. */
/*                                                                          */
/* LMP_BLST_OpenMailBox to open mailbox for all pass through commands.      */
/* LMP_BLST_WriteMailBox or LMP_BLST_ReadMailBox can be used multiple times */
/* for consecutive write or read. At the end, use LMP_BLST_CloseMailBox to  */
/* close the mailbox. Reference Ballast Specification for specific          */
/* command/address can be programmed through mailbox.                       */
/*                                                                          */
/* Call LAMP_UART_GetLampStatus is used to get current lamp safe mode to    */
/* indicate if any error occurred while switching sequence bin due to no    */
/* matching Ballast Manufacturer ID or waveform index found is out of       */
/* range.  When in safe mode, lamp sync will be disabled to protect the     */
/* lamp ballast.                                                            */
/*                                                                          */
/****************************************************************************/

#include "common.h"
#include "cw.h"
#include "seq.h"
#include "ddp_stat.h"
#include "int.h"
#include "lmp_blst.h"
#include "lmp.h"
#include "tmr.h"
#include "urt.h"
#include "rta_pub.h"
#include "rta_sem.h"
#include "rta_tsk.h"

#include "global.h"
#include "app_cfg.h"


#define LMP_BLST_LAMPON         BIT0
#define LMP_BLST_SEM_DELAY      500
#define LMP_BLST_UART_DELAY     500
#define LMP_BLST_INIT_DELAY     20

/* Global and External Variables */
static URTPORT LMP_BLST_PortNum = URT_PORT0;

static BOOL    LMP_BLST_SafeMode            = FALSE;
static int08   LMP_BLST_CurrERR             = 0;

       uint08  LMP_BLST_Numbertries         = 0;
static BOOL    LMP_BLST_Enabled             = FALSE;
static uint08  LMP_BLST_BallastMfgID        = 0;
static uint16  LMP_BLST_BallastID           = 0;
       uint08  LMP_BLST_CurrWaveformID      = 0;
static uint08  LMP_BLST_NumWaveforms        = 0;
static uint08  LMP_BLST_CurrWaveformIndex   = 0xFF;
static uint08  LMP_BLST_CurrWaveformGain    = 0;
       BOOL    LMP_BLST_CurrLmpLit          = FALSE;
static uint08  LMP_BLST_MaxGain             = 0;
static uint08  LMP_BLST_MinGain             = 0;

static uint08  write_buffer [5],    read_buffer [5];
static uint32  numBytesWritten = 0, numBytesRead = 0;

static uint32  LMP_BLST_Sem;


/****************************************************************************/
/* This function enables/disables the communication with UART capable       */
/* lamp ballast.  It also queries the information and saves for future      */
/* processing.                                                              */
/* param  Enable - TRUE - Enables communication.                            */
/*                 FALSE - Disables communication (further queries will     */
/*                                                  not work).              */
/*                                                                          */
/* return  PASS = Successful  <BR>                                          */
/*         FAIL = Unsuccessful  <BR>                                        */
/*         LMP_BLST_TIMEOUT = UART Timeout  <BR>                            */
/*         LMP_BLST_URTERR  = UART all other Errors  <BR>                   */
/*         LMP_BLST_BALLASTERR   = Ballast returned error                   */
/****************************************************************************/

int08   LMP_BLST_EnableCommunication (BOOL Enable)
{
    int08   i = 0;
    int08   error     = PASS;
    URTINIT urtInit;
    uint08 waveformIdx;

    LMP_BLST_PortNum = gpConfiguration->Illum.UartBallastPort;

    /*
       # Issue Enable Communication command (0x70)
       # If successful, issue the following queries to get value for local variables
         1. Ballast Manufacturer ID (0xf0)
         2. Ballast ID (0xf1)
         3. Number of Waveforms Stored (0xf6)
         4. Current Waveform Index (0xf3)
         5. Minimum Waveform Gain  (0xfa)
         6. Maximum Waveform Gain  (0xfb)
    */

    /* UART Initialization */
    urtInit.PortEnable  = ENABLE;
    urtInit.BaudRate    = gpConfiguration->Illum.UartBallastBaudrate;        /* URT_9600     */
    urtInit.DataBits    = gpConfiguration->Illum.UartBallastDatabits;        /* URT_8DBITS     */
    urtInit.StopBits    = gpConfiguration->Illum.UartBallastStopbits;        /* URT_1SBITS     */
    urtInit.Parity      = gpConfiguration->Illum.UartBallastParity;        /* URT_EVEN     */
    urtInit.FlowControl = URT_OFF;
    urtInit.RXTrigLevel = URT_RX_SEVEN_EIGHTHS_FULL;             
    urtInit.TXTrigLevel = URT_TX_ONE_EIGHTH_FULL; 
    urtInit.RXDPolarity = gpConfiguration->Illum.UartBallastRxdPolarity;  /* URT_NONINV_RXDPOL, URT_INV_RXDPOL */

    error = URT_FlushFIFOs   (LMP_BLST_PortNum);
    error = URT_SetConfig    (LMP_BLST_PortNum, &urtInit);
    error = URT_SetIntEnable (LMP_BLST_PortNum, 0x0000);
    error = URT_EnableFIFOs  (LMP_BLST_PortNum, ENABLE);
    error = URT_SetPortEnable(LMP_BLST_PortNum, ENABLE);
    
    if (error != PASS)
        return LMP_BLST_URTERR;


    if (!Enable)
    {
        /* Write "Turn Lamp Off" command (0x26) */
        write_buffer[0] = 0x26;
        error = URT_Write (LMP_BLST_PortNum, 1, &write_buffer[0], LMP_BLST_UART_DELAY, &numBytesWritten);
        RTA_TaskDelay(TMR_ConvertMSToTicks(LMP_BLST_INIT_DELAY));
        
        /* Exit UART mode */
    	write_buffer[0] = gpConfiguration->Illum.UartBallastExitUartModeCmd;
        error = URT_Write (LMP_BLST_PortNum, 1, &write_buffer[0], LMP_BLST_UART_DELAY, &numBytesWritten);

        LMP_BLST_Enabled = FALSE;
        return error;
    }
    
    URT_FlushReadFIFO(LMP_BLST_PortNum); /* Discard any spurious data */
    
    /* Write "Enable Communication" command (0x70) */
    write_buffer[0] = 0x70;
    
    for (i = 0; i < 5; i++)
    {
        error = URT_Write (LMP_BLST_PortNum, 1, &write_buffer[0], LMP_BLST_UART_DELAY, &numBytesWritten);
        RTA_TaskDelay(TMR_ConvertMSToTicks(LMP_BLST_INIT_DELAY));
        error = URT_Read  (LMP_BLST_PortNum, 2, &read_buffer[0],  LMP_BLST_UART_DELAY, &numBytesRead);

        if ((error == PASS) && (read_buffer[0] == 0x70))
        {
            LMP_BLST_Numbertries = i;
            break;
        }

        error = URT_FlushFIFOs     (LMP_BLST_PortNum);
        error = URT_ClearIntStatus (LMP_BLST_PortNum, 0x7F);
    }
    
    if (error == PASS)
    {
        if (read_buffer [0] == 0x70)
        {
            LMP_BLST_Enabled = TRUE;
            LMP_BLST_SetLampStatus (FALSE, PASS);
        }
        else
        {
            error = LMP_BLST_BALLASTERR;            
            LMP_BLST_SetLampStatus (TRUE, error);

            return error;
        }
    }
    else
    {  
        if (error == URT_TIMEOUT)
            error = LMP_BLST_TIMEOUT;
        else
            error = LMP_BLST_URTERR;
                
        LMP_BLST_SetLampStatus (TRUE, error);
            
        return error;
    }

     /* Read Ballast Manufacturer ID */
    write_buffer[0] = 0xF0;
    error = URT_Write (LMP_BLST_PortNum, 1, &write_buffer[0], LMP_BLST_UART_DELAY, &numBytesWritten);
    error = URT_Read  (LMP_BLST_PortNum, 2, &read_buffer[0],  LMP_BLST_UART_DELAY, &numBytesRead);
    if (error == PASS && read_buffer [0] == 0xF0)
        LMP_BLST_BallastMfgID = read_buffer[1];
        
    /* Read Ballast ID */
    write_buffer[0] = 0xF1;
    error = URT_Write (LMP_BLST_PortNum, 1, &write_buffer[0], LMP_BLST_UART_DELAY, &numBytesWritten);
    error = URT_Read  (LMP_BLST_PortNum, 3, &read_buffer[0],  LMP_BLST_UART_DELAY, &numBytesRead);
    if (error == PASS && read_buffer [0] == 0xF1)
    {
        LMP_BLST_BallastID = (read_buffer[1] << 8) | read_buffer[2];
    }
    read_buffer[2] = 0x00;
    
    /* Read Current Waveform Index */
    write_buffer[0] = 0xF3;
    error = URT_Write (LMP_BLST_PortNum, 1, &write_buffer[0], LMP_BLST_UART_DELAY, &numBytesWritten);
    error = URT_Read  (LMP_BLST_PortNum, 2, &read_buffer[0],  LMP_BLST_UART_DELAY, &numBytesRead);
    if (error == PASS && read_buffer [0] == 0xF3)
        LMP_BLST_CurrWaveformIndex = read_buffer[1];
    
    /* Read Current Waveform ID */
    write_buffer[0] = 0xF2;
    error = URT_Write (LMP_BLST_PortNum, 1, &write_buffer[0], LMP_BLST_UART_DELAY, &numBytesWritten);
    error = URT_Read  (LMP_BLST_PortNum, 2, &read_buffer[0],  LMP_BLST_UART_DELAY, &numBytesRead);
    if (error == PASS && read_buffer [0] == 0xF2)
        LMP_BLST_CurrWaveformID = read_buffer[1];
    
    /* Read Current Waveform Gain */
    write_buffer[0] = 0xF4;
    error = URT_Write (LMP_BLST_PortNum, 1, &write_buffer[0], LMP_BLST_UART_DELAY, &numBytesWritten);
    error = URT_Read  (LMP_BLST_PortNum, 2, &read_buffer[0],  LMP_BLST_UART_DELAY, &numBytesRead);
    if (error == PASS && read_buffer [0] == 0xF4)
        LMP_BLST_CurrWaveformGain = read_buffer[1];
    
    /* Read Number of Waveforms Stored */
    write_buffer[0] = 0xF6;
    error = URT_Write (LMP_BLST_PortNum, 1, &write_buffer[0], LMP_BLST_UART_DELAY, &numBytesWritten);
    error = URT_Read  (LMP_BLST_PortNum, 2, &read_buffer[0],  LMP_BLST_UART_DELAY, &numBytesRead);
    if (error == PASS && read_buffer [0] == 0xF6)
        LMP_BLST_NumWaveforms = read_buffer[1];

    /* Read Maximum Waveform Gain */
    write_buffer[0] = 0xFA;
    error = URT_Write (LMP_BLST_PortNum, 1, &write_buffer[0], LMP_BLST_UART_DELAY, &numBytesWritten);
    error = URT_Read  (LMP_BLST_PortNum, 2, &read_buffer[0],  LMP_BLST_UART_DELAY, &numBytesRead);
    if (error == PASS && read_buffer [0] == 0xFA)
        LMP_BLST_MinGain = read_buffer[1];

    /* Read Minimum Waveform Gain */
    write_buffer[0] = 0xFB;
    error = URT_Write (LMP_BLST_PortNum, 1, &write_buffer[0], LMP_BLST_UART_DELAY, &numBytesWritten);
    error = URT_Read  (LMP_BLST_PortNum, 2, &read_buffer[0],  LMP_BLST_UART_DELAY, &numBytesRead);
    if (error == PASS && read_buffer [0] == 0xFB)
        LMP_BLST_MaxGain = read_buffer[1];

    if(RTA_SemCreate(&LMP_BLST_Sem, "LAMP BALLAST Sem") != RTA_SUCCESS)
        return FAIL;

    /* Check the SEQ_GRP table for match Spin Rate, Seq Mode, and LMP_BLST_BallastMfgID */
    if( FAIL != SEQ_GetWaveformIndex( CW_GetMode(), 
                                             CW_GetSeqMode(),
                                             LMP_BLST_BallastMfgID,
                                             &waveformIdx ) )
    {                       
        error = LMP_BLST_SetWaveform( waveformIdx );
        if( error != PASS )
        {
            LMP_BLST_SetLampStatus (TRUE, error);
            return error;
        }
    }
    else
    {
        LMP_BLST_SetLampStatus (TRUE, LMP_BLST_NOSEQMATCH);
        return LMP_BLST_NOSEQMATCH;
    }
    
    return PASS;
}


/****************************************************************************/
/* This function returns if UART Communication is enabled.                  */
/*                                                                          */
/* return  TRUE  = UART Communication is enabled                            */
/*         FALSE = UART Communication is not enabled                        */
/****************************************************************************/

BOOL   LMP_BLST_IsCommunicationEnabled (void)
{
    return LMP_BLST_Enabled;
}


/****************************************************************************/
/* This function sets the waveform index to select a waveform programmed in */ 
/* the ballast.  NOTE that the waveform may be automatically                */
/* selected/overridden in the ballast whenever the sequence is changed.     */
/*                                                                          */
/*  param  Waveform_Index - I - Waveform Index range 0x0 to 0xF. This       */
/*                              should be less than the number of waveforms */
/*                              stored in the ballast. Refer to             */
/*                              LMP_BLST_GetNumWaveforms().                 */
/*                                                                          */
/*  return PASS = Successful                                                */
/*         FAIL = Unsuccessful                                              */
/*         LMP_BLST_INUSEERR     = Lamp UART is in use                      */
/*         LMP_BLST_TIMEOUT      = UART Timeout                             */
/*         LMP_BLST_URTERR       = UART all other Errors                    */
/*         LMP_BLST_WAVEFORM_OOR = waveform specified is out of range       */
/*         LMP_BLST_BALLASTERR   = Ballast returned error                   */
/****************************************************************************/

int08  LMP_BLST_SetWaveform (uint08 Waveform_Index)
{
    int08   error = PASS;

    /* Return if it is the same waveform index */
    if (Waveform_Index == LMP_BLST_CurrWaveformIndex)
        return PASS;

    /* Set the OOR flag if the index is out of range */
    if (Waveform_Index >= LMP_BLST_NumWaveforms)
    {
        error = LMP_BLST_SetLampStatus (TRUE, LMP_BLST_WAVEFORM_OOR);
        return LMP_BLST_WAVEFORM_OOR;
    }

    if(RTA_SemReserve(LMP_BLST_Sem, TMR_ConvertMSToTicks(LMP_BLST_SEM_DELAY)) != RTA_SUCCESS)
        return LMP_BLST_INUSEERR;

    URT_FlushReadFIFO(LMP_BLST_PortNum); /* Discard any spurious data */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -