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

📄 sm1.c

📁 SPI主机模式进行串行通信
💻 C
📖 第 1 页 / 共 2 页
字号:
/** ###################################################################
**     THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**     Filename  : SM1.C
**     Project   : spi_master
**     Processor : 56F8346
**     Beantype  : SynchroMaster
**     Version   : Bean 02.213, Driver 01.15, CPU db: 2.87.097
**     Compiler  : Metrowerks DSP C Compiler
**     Date/Time : 2009-1-16, 8:43
**     Abstract  :
**         This bean "SynchroMaster" implements MASTER part of synchronous
**         serial master-slave communication.
**     Settings  :
**         Synchro type                : MASTER
**
**         Serial channel              : SPI0
**
**         Protocol
**             Init baud rate          : 4_267us
**             Clock edge              : rising
**             Width                   : 8 bits
**             Empty character         : 0
**             Empty char. on input    : RECEIVED
**
**         Registers
**             Input buffer            : SPI0_DRR  [62114]
**             Output buffer           : SPI0_DTR  [62115]
**             Control register        : SPI0_SCR  [62112]
**             Mode register           : SPI0_DSR  [62113]
**             Baud setting reg.       : SPI0_SCR  [62112]
**
**
**
**         Used pins                   :
**         ----------------------------------------------------------
**              Function    | On package |    Name
**         ----------------------------------------------------------
**               Input      |     131    |  GPIOE6_MISO0
**               Output     |     132    |  GPIOE5_MOSI0
**               Clock      |     130    |  GPIOE4_SCLK0
**         ----------------------------------------------------------
**
**     Contents  :
**         Enable                - byte SM1_Enable(void);
**         Disable               - byte SM1_Disable(void);
**         RecvChar              - byte SM1_RecvChar(SM1_TComData *Chr);
**         SendChar              - byte SM1_SendChar(SM1_TComData Chr);
**         CharsInRxBuf          - byte SM1_CharsInRxBuf(word *Chr);
**         GetCharsInRxBuf       - word SM1_GetCharsInRxBuf(void);
**         CharsInTxBuf          - byte SM1_CharsInTxBuf(word *Chr);
**         GetCharsInTxBuf       - word SM1_GetCharsInTxBuf(void);
**         SetShiftClockPolarity - byte SM1_SetShiftClockPolarity(byte Edge);
**         SetIdleClockPolarity  - byte SM1_SetIdleClockPolarity(byte Level);
**
**     (c) Copyright UNIS, spol. s r.o. 1997-2005
**     UNIS, spol. s r.o.
**     Jundrovska 33
**     624 00 Brno
**     Czech Republic
**     http      : www.processorexpert.com
**     mail      : info@processorexpert.com
** ###################################################################*/

/* MODULE SM1. */

#include "SM1.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      */

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 - Unused */
static SM1_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(SPI0_SCR,SPE);           /* Enable device */
    setRegBits(GPIO_E_PER,16);         /* Switch pin to peripheral */
    if (SerFlag & FULL_TX) {           /* Is any char in transmit buffer? */
      setReg(SPI0_DTR,BufferWrite);    /* Store char to transmitter register */
      SerFlag &= ~FULL_TX;             /* Zeroize FULL_TX flag */
    }
  }
  else {
    clrRegBits(GPIO_E_PER,16);         /* Switch pin to GPIO */
    clrRegBit(SPI0_SCR,SPE);           /* Disable device */
  }
}

/*
** ===================================================================
**     Method      :  SM1_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 SM1_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      :  SM1_Disable (bean SynchroMaster)
**
**     Description :
**         Disable the bean - it stops the send and receive
**         functions. No events will be generated.
**     Parameters  : None
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte SM1_Disable(void)
{
  if (EnUser) {                        /* Is the device enabled by user? */
    EnUser = FALSE;                    /* If yes then set the flag "device disabled" */
    HWEnDi();                          /* Disable the device */
  }
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  SM1_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 SM1_RecvChar(SM1_TComData *Chr)
{
  register word Status;                                          

  Status = getReg(SPI0_SCR);           /* Read the device error register */
  if (!(Status & SPI0_SCR_SPRF_MASK))  /* Is not received char? */
    return ERR_RXEMPTY;                /* If yes then error is returned */
  *Chr = (SM1_TComData)getReg(SPI0_DRR); /* Read data from receiver */
  if (Status & SPI0_SCR_OVRF_MASK)     /* Is the overrun error flag set? */
    return ERR_OVERRUN;                /* If yes then error is returned  */
  return ERR_OK;
}

/*
** ===================================================================
**     Method      :  SM1_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 SM1_SendChar(SM1_TComData Chr)
{
  if ((!getRegBit(SPI0_SCR,SPTE))||(SerFlag&FULL_TX)) { /* Is last character send? */

⌨️ 快捷键说明

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