📄 ledspi.c
字号:
/** ###################################################################
** THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
** Filename : LEDSPI.C
** Project : PMSM
** Processor : 56F8013VFAE
** Beantype : SynchroMaster
** Version : Bean 02.240, Driver 01.18, CPU db: 2.87.089
** Compiler : Metrowerks DSP C Compiler
** Date/Time : 2008-1-24, 下午 01:45
** Abstract :
** This bean "SynchroMaster" implements MASTER part of synchronous
** serial master-slave communication.
** Settings :
** Synchro type : MASTER
**
** Serial channel : SPI
**
** Protocol
** Init baud rate : 0_5us
** Clock edge : falling
** Width : 16 bits
** Empty character : 0
** Empty char. on input : RECEIVED
**
** Registers
** Input buffer : SPI_DRR [61634]
** Output buffer : SPI_DTR [61635]
** Control register : SPI_SCR [61632]
** Mode register : SPI_DSR [61633]
** Baud setting reg. : SPI_SCR [61632]
**
**
**
** Used pins :
** ----------------------------------------------------------
** Function | On package | Name
** ----------------------------------------------------------
** Output | 16 | GPIOB3_MOSI_T3
** Clock | 21 | GPIOB0_SCLK_SCL
** Select slave | 2 | GPIOB1_SS_B_SDA
** ----------------------------------------------------------
**
** Contents :
** Enable - byte LEDSPI_Enable(void);
** RecvChar - byte LEDSPI_RecvChar(LEDSPI_TComData *Chr);
** SendChar - byte LEDSPI_SendChar(LEDSPI_TComData Chr);
** GetCharsInRxBuf - word LEDSPI_GetCharsInRxBuf(void);
** GetCharsInTxBuf - word LEDSPI_GetCharsInTxBuf(void);
**
** (c) Copyright UNIS, spol. s r.o. 1997-2006
** UNIS, spol. s r.o.
** Jundrovska 33
** 624 00 Brno
** Czech Republic
** http : www.processorexpert.com
** mail : info@processorexpert.com
** ###################################################################*/
/* MODULE LEDSPI. */
#include "LEDSPI.h"
#define OVERRUN_ERR 1 /* Overrun error flag bit */
#define CHAR_IN_RX 8 /* Char is in RX buffer */
#define FULL_TX 16 /* Full transmit buffer */
#define RUNINT_FROM_TX 32 /* Interrupt is in progress */
#define FULL_RX 64 /* Full receive buffer */
#define FAULT_ERR 128 /* Fault error flag bit */
static bool EnUser; /* Enable/Disable SPI */
static byte SerFlag; /* Flags for serial communication */
/* Bits: 0 - OverRun error */
/* 1 - Unused */
/* 2 - Unused */
/* 3 - Char in RX buffer */
/* 4 - Full TX buffer */
/* 5 - Running int from TX */
/* 6 - Full RX buffer */
/* 7 - Fault mode error */
static LEDSPI_TComData BufferWrite; /* Output char SPI commmunication */
/*
** ===================================================================
** Method : HWEnDi (bean SynchroMaster)
**
** Description :
** Enables or disables the peripheral(s) associated with the bean.
** The method is called automatically as a part of the Enable and
** Disable methods and several internal methods.
** This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
static void HWEnDi(void)
{
if (EnUser) { /* Enable device? */
setRegBit(SPI_SCR,MODF); /* Clear MODF flag */
setRegBit(SPI_SCR,SPE); /* Enable device */
setRegBits(GPIO_B_PER,1); /* Switch pin to peripheral */
if (SerFlag & FULL_TX) { /* Is any char in transmit buffer? */
setReg(SPI_DTR,BufferWrite); /* Store char to transmitter register */
SerFlag &= ~FULL_TX; /* Zeroize FULL_TX flag */
}
}
else {
clrRegBits(GPIO_B_PER,1); /* Switch pin to GPIO */
clrRegBit(SPI_SCR,SPE); /* Disable device */
}
}
/*
** ===================================================================
** Method : LEDSPI_Enable (bean SynchroMaster)
**
** Description :
** Enable the bean - it starts send and receive functions.
** Events may be generated ("DisableEvent"/"EnableEvent").
** Parameters : None
** Returns :
** --- - Error code, possible codes:
** ERR_OK - OK
** ERR_SPEED - This device does not work in
** the active speed mode
** ===================================================================
*/
byte LEDSPI_Enable(void)
{
if (!EnUser) { /* Is the device disabled by user? */
EnUser = TRUE; /* If yes then set the flag "device enabled" */
HWEnDi(); /* Enable the device */
}
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : LEDSPI_RecvChar (bean SynchroMaster)
**
** Description :
** If any data is received, this method returns one
** character, otherwise it returns an error code (it does
** not wait for data).
** DMA mode:
** If DMA controller is available on selected CPU and
** receiver is configured to use DMA controller then this
** method only sets the selected DMA channel. Status of the
** DMA transfer can then be checked using method
** GetCharsInRxBuf. See typical usage for details about
** communication using DMA.
** Parameters :
** NAME - DESCRIPTION
** * Chr - A pointer to the received character
** Returns :
** --- - Error code, possible codes:
** ERR_OK - OK
** ERR_SPEED - This device does not work in
** the active speed mode
** ERR_RXEMPTY - No data in receiver
** ERR_OVERRUN - Overrun error was detected
** from the last char or block received
** ERR_FAULT - Fault error was detected
** from the last char or block received.
** This error may not be supported on some
** CPUs (see generated code).
** ===================================================================
*/
byte LEDSPI_RecvChar(LEDSPI_TComData *Chr)
{
register word Status;
Status = getReg(SPI_SCR); /* Read the device error register */
if (Status & SPI_SCR_MODF_MASK) { /* Is mode flag error set? */
setRegBit(SPI_SCR,MODF); /* Clear Fault mode error flag */
EnUser = FALSE; /* Set flag to FALSE, because device is disabled by HW */
return ERR_FAULT; /* If yes then return error */
}
if (!(Status & SPI_SCR_SPRF_MASK)) /* Is not received char? */
return ERR_RXEMPTY; /* If yes then error is returned */
*Chr = (LEDSPI_TComData)getReg(SPI_DRR); /* Read data from receiver */
if (Status & SPI_SCR_OVRF_MASK) /* Is the overrun error flag set? */
return ERR_OVERRUN; /* If yes then error is returned */
return ERR_OK;
}
/*
** ===================================================================
** Method : LEDSPI_SendChar (bean SynchroMaster)
**
** Description :
** Sends one character to the channel.
** DMA mode:
** If DMA controller is available on the selected CPU and
** the transmitter is configured to use DMA controller then
** this method only sets the selected DMA channel. The
** status of the DMA transfer can then be checked using
** GetCharsInTxBuf method. See the typical usage for details
** about communication using DMA.
** Parameters :
** NAME - DESCRIPTION
** Chr - Character to send
** Returns :
** --- - Error code, possible codes:
** ERR_OK - OK
** ERR_SPEED - This device does not work in
** the active speed mode
** ERR_DISABLED - Device is disabled (only
** if output DMA is supported and enabled)
** ERR_TXFULL - Transmitter is full
** ===================================================================
*/
byte LEDSPI_SendChar(LEDSPI_TComData Chr)
{
if ((!getRegBit(SPI_SCR,SPTE))||(SerFlag&FULL_TX)) { /* Is last character send? */
return ERR_TXFULL; /* If no then return error */
}
if (EnUser) { /* Is device enabled? */
setReg(SPI_DTR,Chr); /* If yes, send character */
}
else {
BufferWrite = Chr; /* If no, save character */
SerFlag |= FULL_TX; /* ...and set flag */
}
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : LEDSPI_GetCharsInRxBuf (bean SynchroMaster)
**
** Description :
** Returns the number of characters in the input buffer.
** Note: If the Interrupt service is disabled, and the
** Ignore empty character is set to yes, and a character has
** been received, then this method returns 1 although it was
** an empty character.
** DMA mode:
** If DMA controller is available on the selected CPU and
** the receiver is configured to use DMA controller then
** this method returns the number of characters in the
** receive buffer.
** Parameters : None
** Returns :
** --- - Number of characters in the input
** buffer.
** ===================================================================
*/
/*
word LEDSPI_GetCharsInRxBuf(void)
** This method is implemented as a macro. See header module. **
*/
/*
** ===================================================================
** Method : LEDSPI_GetCharsInTxBuf (bean SynchroMaster)
**
** Description :
** Returns the number of characters in the output buffer.
** DMA mode:
** If DMA controller is available on the selected CPU and
** the transmitter is configured to use DMA controller then
** this method returns the number of characters in the
** transmit buffer.
** Parameters : None
** Returns :
** --- - Number of characters in the output
** buffer.
** ===================================================================
*/
word LEDSPI_GetCharsInTxBuf(void)
{
return ((EnUser) ? (getRegBit(SPI_SCR,SPTE)?(word)0:(word)1) : ((SerFlag & FULL_TX)?(word)1:(word)0)); /* Return number of chars in the transmit buffer */
}
/*
** ===================================================================
** Method : LEDSPI_Init (bean SynchroMaster)
**
** Description :
** Initializes the associated peripheral(s) and the beans
** internal variables. The method is called automatically as a
** part of the application initialization code.
** This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
void LEDSPI_Init(void)
{
/* SPI_SCR: SPR2=0,SPR1=1,SPR0=1,DSO=0,ERRIE=0,MODFEN=1,SPRIE=0,SPMSTR=1,CPOL=0,CPHA=0,SPE=0,SPTIE=0,SPRF=0,OVRF=0,MODF=1,SPTE=0 */
setReg(SPI_SCR,25858); /* Set control register */
/* SPI_DSR: WOM=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,DS3=1,DS2=1,DS1=1,DS0=1 */
setReg(SPI_DSR,15); /* Set data size and control register */
SerFlag = 0; /* Reset all flags */
EnUser = TRUE; /* Enable device */
HWEnDi(); /* Enable/disable device according to the status flags */
}
/* END LEDSPI. */
/*
** ###################################################################
**
** This file was created by UNIS Processor Expert 2.98 [03.79]
** for the Freescale 56800 series of microcontrollers.
**
** ###################################################################
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -