📄 dali_lib.c
字号:
//**************************************************************************
//! @file dali_lib.c
//!
//! Copyright (c) 2004
//!
//! Use of this program is subject to Atmel's End User License Agreement.
//! Please read file license.txt for copyright notice.
//!
//! @brief This file contains DALI lib routines available for DEVICE TYPE 0.
//!
//! This template file can be parsed by Doxygen for automatic documentation
//! generation.
//! Put here the functional description of this file within the software
//! architecture of your program.
//!
//! @version 1.1 (pwm3-ac-ctrl-motor-0_0_3)
//!
//! @todo
//! @bug
//**************************************************************************
//_____ I N C L U D E S ____________________________________________________
#include "config.h"
#include "lib_mcu\dali\dali_code.h"
#include "lib_mcu\dali\dali_lib.h"
extern volatile U16 fosc;
//--------------------------------------------------------------------------
// @fn dali_slave_init
//!
//! DALI SLAVE peripheral initialization. Reset and initialize the peripheral
//! and enable it.
//!
//! @warning none
//!
//! @param none
//!
//! @return Status
//! ==TRUE: function failed
//! ==FALSE: function performed
//!
//--------------------------------------------------------------------------
Bool dali_slave_init(void)
{
return Dali_slave_init(fosc);
}
//--------------------------------------------------------------------------
// @fn dali_slave_disable
//!
//! DALI SLAVE peripheral disable. Disable and clean out the receive buffer.
//!
//! @warning none
//!
//! @param none
//!
//! @return none
//!
//--------------------------------------------------------------------------
void dali_slave_disable(void)
{
Dali_slave_disable();
}
//--------------------------------------------------------------------------
// @fn dali_slave_enable
//!
//! DALI SLAVE peripheral enable. Enable without initializatio.
//!
//! @warning Previous 'dali_slave_init()' must be done before
//!
//! @param none
//!
//! @return none
//!
//--------------------------------------------------------------------------
void dali_slave_enable(void)
{
Dali_slave_enable(fosc);
}
//--------------------------------------------------------------------------
// @fn dali_slave_cmd
//!
//! This function takes a DALI SLAVE descriptor, analyses the action to do:
//! transmit, receive or abort (or nothing).
//! This function returns a status DALI_CMD_ACCEPTED or DALI_CMD_REFUSED)
//! if some problem occurs, the application must be retry later.
//!
//! @param st_dali_slave_mess_t* - pointer on DALI SLAVE message descriptor
//! structure to select the action to do.
//!
//! @return DALI_CMD_ACCEPTED - command is accepted
//! DALI_CMD_REFUSED - command is refused
//!
//--------------------------------------------------------------------------
U8 dali_slave_cmd (st_dali_slave_mess_t* st_mess)
{
switch (st_mess->cmd)
{
case CMD_NONE:
return DALI_CMD_ACCEPTED;
break;
case CMD_RX_CMD:
st_mess->handle = 0;
//- 'st_mess->cmd' must be initialized by the main application
// program with one of the commands proposed in "dali_lib.h".
//- 'st_mess->short_add' must be initialized by the main application
// program with the a 6-bit number 'SHORT_ADDRESS' as
// described in the table of variables of DALI standard.
//- 'st_mess->group' must be initialized by the main application
// program with the concatenation of 'GROUP 8-15' and
// 'GROUP 0-7' as described in the table of variables of
// DALI standard.
st_mess->g_code = NO_DALI_COMMAND_CODE;
st_mess->p_cc = 0; // Clear "Previous Command Code"
dali_slave_enable();
return DALI_CMD_ACCEPTED;
break;
case CMD_ABORT:
dali_slave_disable();
st_mess->handle = 0;
st_mess->g_code = NO_DALI_COMMAND_CODE;
st_mess->p_cc = 0;
return DALI_CMD_ACCEPTED;
break;
case CMD_TX_ANSWER:
st_mess->handle = 0;
st_mess->g_code = NO_DALI_COMMAND_CODE;
st_mess->p_cc = 0;
//- 'st_mess->dtr' must be initialized by the main application
dali_slave_enable();
Dali_slave_put(st_mess->dtr);
return DALI_CMD_ACCEPTED;
break;
default:
return DALI_CMD_REFUSED;
break;
}
}
//--------------------------------------------------------------------------
// @fn dali_slave_get_status
//!
//! This function allows to return if the command has been performed or not.
//! In an reception case, all the DALI message is stored in the structure.
//! This function also updates the DALI descriptor status (DALI_TX_COMPLETED,
//! DALI_RX_CODE_xyz, DALI_RX_2ND_CODE_NEEDED_xyz, DALI_RX_QUERY_xyz,
//! DALI_RX_COM_ERROR, DALI_RX_NO_ADD_TYPE, DALI_RX_ADD_OUT_RANGE or
//! DALI_RX_CODE_ERROR).
//!
//! @param st_dali_slave_mess_t* pointer on DALI SLAVE descriptor structure.
//!
//! @return DALI_STATUS_COMPLETED - Rx or Tx is completed
//! DALI_STATUS_NOT_COMPLETED - Rx or Tx is not completed
//! DALI_STATUS_ERROR - Error in configuration or in the
//! DALI communication
//!
//--------------------------------------------------------------------------
U8 dali_slave_get_status (st_dali_slave_mess_t* st_mess)
{
Bool direct_cmd;
Bool special_cmd;
U8 code_type;
U8 add_type;
U8 rec_add;
U8 rec_data;
U16 rec_dali;
//----- 'Handle' incrementation - Used to date DALI communication
st_mess->handle += 1; // Time_count increment
//----- CMD_TX_ANSWER ?
if (st_mess->cmd == CMD_TX_ANSWER)
{
st_mess->p_cc = 0;
//- Get status after CMD_TX_ANSWER
if (Dali_slave_tx_done()==TRUE)
{
st_mess->status = DALI_TX_COMPLETED;
dali_slave_disable();
return DALI_STATUS_COMPLETED;
}
else return DALI_STATUS_NOT_COMPLETED;
}
//----- Get DALI communication
if (Dali_slave_rx_ready())
{
rec_dali = Dali_slave_get(); // MSB='address byte', LSB='data byte'
//- Communication error control
if ((DALI_SLAVE_RX_ERROR & DALI_RX_COM_ERROR_MASK) != DALI_RX_NO_COM_ERROR)
{
st_mess->p_cc = 0;
st_mess->dtr = DALI_SLAVE_RX_ERROR; // For debugging if needed
st_mess->status = DALI_RX_COM_ERROR;
Dali_slave_disable();
return DALI_STATUS_ERROR;
}
// rec_dali = Dali_slave_get(); // MSB='address byte', LSB='data byte'
}
else return DALI_STATUS_NOT_COMPLETED;
//----- Direct address_test
direct_cmd = FALSE; // Default
if ((rec_dali & 0x0100) == 0) direct_cmd = TRUE;
//----- Short_Group_Broadcast_Address_Test
add_type = NO_ADD_TYPE; // Default
special_cmd = FALSE; // Default
rec_add = (rec_dali >> 9);
// Care with 'rec_add': 7-bit variable (rec_add = (rec_dali >> 9))
if ((rec_add & 0x40) == 0) add_type = SHORT_ADD_TYPE;
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -