📄 linsci.c
字号:
/**************** (c) 2004 STMicroelectronics **********************
PROJECT : ST7MC demokit
COMPILER : ST7 METROWERKS C (HIWARE) / COSMIC
MODULE : LinSCI.c
LIBRARY VERSION : 1.0.2
CREATION DATE : 04.2005
AUTHOR : Jiang Jian-guo / Microcontroller Competence center / ST Shang Hai
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
DESCRIPTION : Lin/SCI control routines
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
******************************************************************************
THE SOFTWARE INCLUDED IN THIS FILE IS FOR GUIDANCE ONLY. ST MICROELECTRONICS
SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES
WITH RESPECT TO ANY CLAIMS ARISING FROM USE OF THIS SOFTWARE.
******************************************************************************
******************************************************************************/
#include "lib.h"
#include "ST7MC_hr.h"
#include "LinSCI.h"
#include "mtc.h"
#include "Misc.h"
#include "it_ST7MC.h"
#include "MTC_Settings_Sensorless.h"
static volatile u8 Tx_Index; // SCI transmitter index ( 0 -> LIN_TX_BUFFER_SIZE)
static volatile u8 Rx_Index; // SCI receiver index ( 0 -> LIN_TX_BUFFER_SIZE)
static volatile u8 SCI_Status;
#define RX_Complete 0x80 // Bit7: SCI reveive completed
volatile u8 Lin_Tx_Buffer[LIN_TX_BUFFER_SIZE];
volatile u8 Lin_Rx_Buffer[LIN_TX_BUFFER_SIZE];
volatile u8 * PtrToSCIBuffTx;
volatile u8 * PtrToSCIBuffRx;
volatile u8 MotorStatus_Backup = 0;
void SCI_Config(void)
{
Resert_SCIRX_Watchdog(); // 6 second RX watchdog Refresh
SCIBRR = TX_RX_SCIBRR_2400_Bauds; // 2.4K bauds TX RX
SCISR; // Clear all the flags in Status register
SCIDR;
SCIDR = 0xff;
SCICR1 = 0x00; // SCI on,8 bit length,disable parity
SCICR2 = SCI_RIE + SCI_RE + SCI_TE; // Receive/transmit enable,Receive interrupt enable
Rx_Index = 0; // Initializes variables
Tx_Index = 0;
SCI_Status = 0;
PtrToSCIBuffRx = Lin_Rx_Buffer;
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : SCI_Send_Data
INPUT : *PtrtoBuffer,NbOfBytes
OUTPUT : None
DESCRIPTION : Transmits data from the user buffer and
starts transmission in interrupt driven
COMMENTS : None
-----------------------------------------------------------------------------*/
void SCI_Send_Data(void)
{
if (Timer_IsSCITXDelayElapsed() == TRUE)
{
u8 i;
// prepare for the TX data for the next transmission
Lin_Tx_Buffer[0] = Slave_Addr; // master address
Lin_Tx_Buffer[1] = Master_Addr; // slave address
Lin_Tx_Buffer[2] = SCI_COM; // command
Lin_Tx_Buffer[3] = Data_Length - 1; // data length - 1
Lin_Tx_Buffer[4] = (u8)(Freq_Motor/(10 * Pole_Pair_Num)); // actual frequency
Lin_Tx_Buffer[6] = (u8)(Freq_Motor/(10 * Pole_Pair_Num));
/* Error code: Bit7 -- Emergency Fault
Bit6 -- Start-up Failure
Bit4 -- Stall Fault */
//Lin_Tx_Buffer[5] = (u8)(GetMotorStatus() & FAULT_MSK);
//Lin_Tx_Buffer[7] = (u8)(GetMotorStatus() & FAULT_MSK);
Lin_Tx_Buffer[5] = (u8)(MotorStatus_Backup & FAULT_MSK);
Lin_Tx_Buffer[7] = (u8)(MotorStatus_Backup & FAULT_MSK);
Lin_Tx_Buffer[8] = 0x00; // checksum
for (i=0;i<8;i++)
{
Lin_Tx_Buffer[8] += Lin_Tx_Buffer[i];
}
Lin_Tx_Buffer[8] = (u8)(0 - Lin_Tx_Buffer[8]);
PtrToSCIBuffTx = Lin_Tx_Buffer; // copies the user data to global variables
// PtrToSCIBuffTx = Lin_Rx_Buffer; // only for SCI debug
Tx_Index = LIN_TX_BUFFER_SIZE;
SCISR; // clear TDRE & TC bit
SCIDR = *PtrToSCIBuffTx; /* First byte is transmitted to generate the
interrupt*/
SCICR2 |= SCI_TIE; //Enable TDRE Interrupt
}
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : SCI_IsTransmitCompleted
INPUT : None
OUTPUT : TRUE or FALSE
DESCRIPTION : Checks if the transmission is completed or not
COMMENTS : Must be called before SCI_Send_Data()
-----------------------------------------------------------------------------*/
BOOL SCI_IsTransmitCompleted(void)
{
if ((Tx_Index == 0) && (SCISR & SCI_TC)) return (TRUE);
else return (FALSE);
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : SCI_IsReceptionCompleted
INPUT : None
OUTPUT : TRUE if all the bytes are received correctly
DESCRIPTION : process the datas if all the bytes are received correctly
COMMENTS :
-----------------------------------------------------------------------------*/
BOOL SCI_IsReceptionCompleted(void)
{
if (SCI_Status & RX_Complete)
{
u8 temp,i;
SCI_Status &= (u8)(~RX_Complete);
temp = Lin_Rx_Buffer[0];
for (i=1;i<9;i++)
{
temp += Lin_Rx_Buffer[i];
}
if ((temp == 0x00) && (Lin_Rx_Buffer[4] == Lin_Rx_Buffer[6])
&& (Lin_Rx_Buffer[5] == Lin_Rx_Buffer[7]))
{
Timer_SetSCITXDelayPeriod(10); // 100ms delay before transmission begin
Set_TargetSpeed(Lin_Rx_Buffer[4]);
Set_SpdVarPeriod(Lin_Rx_Buffer[5]);
return(TRUE);
}
else return(FALSE);
}
else return(FALSE);
}
#ifdef __HIWARE__ /* test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS /* additional registers will be saved */
#else
#ifdef __CSMC__ /* test for Cosmic Compiler */
@interrupt /* Cosmic interrupt handling */
#endif
#endif
/*-----------------------------------------------------------------------------
ROUTINE NAME : SCI_IT_Routine
INPUT : None
OUTPUT : None
DESCRIPTION : Carries out communiction in Interrupt driven mode.
COMMENTS : This function must be called inside the SCI interrupt service
routine for Interrupt driven communication
-----------------------------------------------------------------------------*/
void SCI_IT_Routine (void)
{
// Reception Interrupt
if (SCISR & SCI_RDRF)
{
if (SCISR & SCI_ErrValue) // Error(OR/NF/FE) happened
{
SCIDR;
Rx_Index = 0; // Resert the index for receive again
PtrToSCIBuffRx = Lin_Rx_Buffer;
}
else
{
*PtrToSCIBuffRx = SCIDR;
Rx_Index++;
switch (Rx_Index)
{
case 1:
if (*PtrToSCIBuffRx != 0x00) goto Error_Handler;
else PtrToSCIBuffRx++;
break;
case 2:
if (*PtrToSCIBuffRx != 0x01) goto Error_Handler;
else PtrToSCIBuffRx++;
break;
case 3:
if (*PtrToSCIBuffRx != 0x10) goto Error_Handler;
else PtrToSCIBuffRx++;
break;
case 4:
case 5:
case 6:
case 7:
case 8:
PtrToSCIBuffRx++;
break;
case 9:
Rx_Index = 0; // prepare for next receive
PtrToSCIBuffRx = Lin_Rx_Buffer;
SCI_Status |= RX_Complete;
break;
default:
Error_Handler: Rx_Index = 0; // Resert the index for receive again
PtrToSCIBuffRx = Lin_Rx_Buffer;
break;
}
}
}
/* Transmission Interrupt*/
else if (SCISR & SCI_TDRE) // Checks if TDRE Flag is set
{
if (Tx_Index > 1) // Checks if all the bytes are transmitted
{
Tx_Index--;
PtrToSCIBuffTx++; /*Moves the pointer to next location*/
SCIDR = *PtrToSCIBuffTx; /*Transmits a single byte*/
}
else if(Tx_Index == 1)
{
Tx_Index--;
SCICR2 &= (u8)(~SCI_TIE); /*Disable IT if all bytes
is transmitted*/
}
}
}
/*** (c) 2004 STMicroelectronics ****************** END OF FILE ***/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -