/**************************************************************************
* Copyright (c) 2014 Wasion Group;
* All Rights Reserved
***************************************************************************
* THIS SOFTWARE IS PROVIDED BY WASION "AS IS" AND ANY EXPRESSED 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 WASION OR ITS 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.
***************************************************************************//*!
* @file meter_comunication.c
* @author W16555
* @version 1.0
* @date AUG-28-2014
* @brief comunication module of the MMK34 multi-channel 3-phase electricity meter.
* @notes
******************************************************************************/
/* ------------------------------------- includes -------------------------------------- */
#include "CM.h"
#include "..\EEP\EEP.h"
#include "..\SI\SI.h"
/* -------------------------------------- defines --------------------------------------- */
//#define CESHI 1
/* -------------------------------- function declaration -------------------------------- */
void uart_init(UINT8 lowpowermode, UINT16 len,UINT8 comid);
void spi_init(UINT8 lowpowermode, UINT16 len);
void F_Back_Check();
void F_repeat_back();
Frame645 COM_645;
UINT8 DeviceAddr1[6];
UINT8 DataLen;
UINT64 Reset_CM = 0;//载波复位标志
SINT32 ABCabc[100];
SINT32 ABCabc1[100];
int sinowell = 0;
/* ---------------------------------- global variables ---------------------------------- */
static UINT8 COM_RX_Buff[80]; /* Tx/Rx buffer */
static UINT8 COM_TX_Buff[200]; /* Tx/Rx buffer */
static UINT8 COM_RX_Buff_1[80];
UINT8 COM_Flag_1;
volatile UINT8 COM_Buf_Tmp_1;
volatile UINT8 COM_RX_Cnt_1 = 0;
//volatile UINT8 COM_State_1 = 0;
UINT8 COM_TX_Cnt_1 = 0;
UINT8 Send_645addr_start = 0;
UINT8 Read_645_addr = 0;
UINT16 Uint16_Temp_Val;
UINT8 Temp_positive_flag;
UINT8 CM_address[18] = {0x68,0x11,0x11,0x11,0x11,0x11,0x11,0x68,0x93,0x06,0x44,0x44,0x44,0x44,0x44,0x44,0x67,0x16};
//UINT8 Read_addr_success = 0;
volatile UINT8 COM_RX_Flag_1;
volatile UINT8 COM_RX_Flag, COM_Buf_Tmp; /*" 串口中断接收标志 "*/
UINT8 *COM_TX_Ptr;
UINT8 dat_code, cmd_code;
static UINT16 COM_Flag; /*" SPI标志, 用于指示打包写数据的复位项, YP20151015 "*/
volatile UINT16 COM_TX_Cnt, COM_TX_Len, COM_RX_Cnt, DMA_RX_Len; // SPI transmit length
volatile UINT8 COM_State; // communication inferface state
volatile UINT8 COM_TXEnd_Flag; /*" 发送数据完毕标志 "*/
volatile SINT32 *Ptr32;
volatile UINT32 *EnergyPtrbufa, *EnergyPtrbufb; // energy intermediate buffer pointer
volatile UINT32 *EnergyPtrcal; // energy calculate buffer pointer
volatile UINT32 *EnergyPtrtran; // energy transmit buffer pointer
volatile SINT64 *EnergyPtradd; /*" 累计电量数据包缓存区指针 "*/
volatile UINT8 CRC_Pass_Flag = RESET;
volatile UINT16 CRC_UnPass_Times = 0;
UINT8 ModbusRead(UINT8 *dat);
UINT8 ModbusRead_CM(UINT8 *dat);
UINT16 CRC16_MODBUS(UINT8 *startaddr, UINT16 len);
void Read645();
void Read645_CM();
void Write645();
void Write645_CM();
void LIB_MemCpy(UINT8 *src, UINT8 *dest, UINT16 len);
void LIB_LongToBcd (UINT8 *srcData, UINT8 *destData);
UINT8 LIB_CharToBcd (UINT8 srcData);
void LIB_ShortToBcd (UINT8 *srcData, UINT8 * destData);
UINT8 LIB_BcdToChar (UINT8 srcData);
UINT16 LIB_BcdToShort(UINT8 *srcData);
void V_Calibration();
void I_Calibration();
void P_Calibration();
void P_offsetL_Calibration();
#ifdef CESHI
volatile UINT8 ceshi, ceshi1;
static UINT16 volatile COM_RX_Buff1[500], COM_TX_Buff1[500]; /* Tx/Rx buffer */
#endif
volatile UINT8 Change_modbus_Fig = 0; /* modbus协议和校表协议切换标志 */
const UINT16 crc_tb[16]= {
0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,
};
volatile UINT8 FunctionNumber;
volatile UINT16 ByteCount;
volatile UINT16 BeginRegister;
volatile UINT8 WriteByteL; /*" 写命令的数据长度 "*/
volatile UINT16 crc_tx;
UINT8 DeviceAddr;
MODBUS_BUFF modbus_buff;
unsigned int Modbus_CRC(unsigned char len , unsigned int CRC, unsigned cnt, unsigned char buff[]);
void Data_Copy(unsigned char *Dest, unsigned char *Src, unsigned char Len)
{
while (Len>0 )
{
*Dest = *Src;
Dest++;
Src++;
Len--;
}
}
void Data_Copy_BIGEDIAN( unsigned char *Dest, unsigned char *Src, unsigned char Len )
{
while (Len>0 )
{
*(Dest+Len-1) = *Src;
//Dest++;
Src++;
Len--;
}
}
/* ----------------------------------- functions ------------------------------------ */
/* ---------------------------------------------------
% fun_name: spi_init()
% fun_prag: lowpowermode -- system run mode
% fun_return: none
% fun_desp: SPI1 initialization, transmit by IRQ, receive by DMA
-------------------------------------------------- */
void spi_init(UINT8 lowpowermode, UINT16 len)
{
// initial SPI in SYSRUN_NORMAL、SYSRUN_LPMEA and SYSRUN_LPTIM mode
if(lowpowermode != SYSRUN_LOWPOWER)
{
DMA_RX_Len = len; // receive 5 half-words as a default
CRC_UnPass_Times = 0;
SPI1_BR = 0x00;
SPI1_C2 = 0x00;
SPI1_C1 = 0x04;
SPI1_ML = 0x00;
SPI1_MH = 0x00;
dat_code = SPI1_S;
dat_code = SPI1_DL;
cmd_code = SPI1_DH;
SPI1_CI = 0x00;
SPI1_C3 = 0x00;
COM_State = COM_IDLE;
/* Install DMA2 callback and initialize DMA2 channel */
DMA_InstallCallback (DMA2, PRI_LVL_3, dma_callback);
DMA_Init (DMA2,DMA_CH_HWTRG_P2M_CS_CONFIG(DMA2_REQ_SPI1_RX,DMA_SIZE16BIT,
&SPI1_DL,COM_RX_Buff,DMA_RX_Len*2));
/* SPI1 initialization */
/*" 不需要上拉电阻 "*/
PORT_Init (PORTI, PORT_MODULE_ALT4_MODE, SPI1_MOSI | SPI1_MISO);
/*" 需要上拉电阻 "*/
// PORT_Init (PORTI, PORT_MODULE_ALT5_MODE, SPI1_MOSI | SPI1_MISO);
PORT_Init (PORTF, PORT_MODULE_ALT2_MODE, SPI1_SCK | SPI1_SS);
SPI_Init (SPI1, SPI_MODULE_DIV64_16B_RXDMA_TXIRQ_CONFIG,NULL, PRI_LVL_3, spi1_callback);
}
else
{
;
}
}
/* ---------------------------------------------------
% fun_name: uart_init()
% fun_prag: none
% fun_return: none
% fun_desp: communication interface initialization, transmit by IRQ, receive by DMA
-------------------------------------------------- */
void uart_init(UINT8 lowpowermode, UINT16 len,UINT8 comid)
{
if(comid == _COM_ID_485)
{
if(lowpowermode != SYSRUN_LOWPOWER)
{
DMA_RX_Len = len<<1; // receive 10 bytes as a default
COM_RX_Cnt = 0;
//COM_RX_Cnt_1 = 0;
COM_RX_Flag = 0;
//COM_RX_Flag_1 = 0;
CRC_UnPass_Times = 0;
#if METER_TYPE == METER_FTU
/* UART1 init 9600bd :system clock -- 48MHz, defined in S
评论0