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

📄 sm1.c

📁 SPI主机模式进行串行通信
💻 C
📖 第 1 页 / 共 2 页
字号:
    return ERR_TXFULL;                 /* If no then return error */
  }
  if (EnUser) {                        /* Is device enabled? */
    setReg(SPI0_DTR,Chr);              /* If yes, send character */
  }
  else {
    BufferWrite = Chr;                 /* If no, save character */
    SerFlag |= FULL_TX;                /* ...and set flag */
  }
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  SM1_CharsInRxBuf (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.
**         (deprecated method)
**         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  :
**         NAME            - DESCRIPTION
**       * Chr             - A pointer to number of characters in
**                           the input buffer
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte SM1_CharsInRxBuf(word *Chr)
{
  *Chr = (getRegBit(SPI0_SCR,SPRF)?(word)1:(word)0); /* Return number of chars in receive buffer */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  SM1_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 SM1_GetCharsInRxBuf(void)

**      This method is implemented as a macro. See header module. **
*/

/*
** ===================================================================
**     Method      :  SM1_CharsInTxBuf (bean SynchroMaster)
**
**     Description :
**         Returns the number of characters in the output buffer.
**         (deprecated method)
**         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  :
**         NAME            - DESCRIPTION
**       * Chr             - A pointer to the number of characters
**                           in the output buffer
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte SM1_CharsInTxBuf(word *Chr)
{
  *Chr = (EnUser) ? (getRegBit(SPI0_SCR,SPTE)?(word)0:(word)1) : ((SerFlag & FULL_TX)?(word)1:(word)0); /* Return number of chars in the transmit buffer */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  SM1_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 SM1_GetCharsInTxBuf(void)
{
  return ((EnUser) ? (getRegBit(SPI0_SCR,SPTE)?(word)0:(word)1) : ((SerFlag & FULL_TX)?(word)1:(word)0)); /* Return number of chars in the transmit buffer */
}

/*
** ===================================================================
**     Method      :  SM1_SetShiftClockPolarity (bean SynchroMaster)
**
**     Description :
**         Sets the shift clock polarity at runtime. Output data
**         will be shifted on the selected edge polarity.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Edge            - Edge polarity.
**                           0-falling edge
**                           1-rising edge
**     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
**                           ERR_RANGE - Parameter out of range
** ===================================================================
*/
byte SM1_SetShiftClockPolarity(byte Edge)
{
  if (!EnUser)
    return ERR_DISABLED;
  if (Edge > 1)
    return ERR_RANGE;
  EnUser=FALSE;
  HWEnDi();                            /* Disable device */
  if ((getRegBit(SPI0_SCR,CPOL) && (!Edge)) || (!getRegBit(SPI0_SCR,CPOL) && (Edge))) /* 'CPOL' logical XOR 'Edge' */
    setRegBit(SPI0_SCR,CPHA);          /* Set phase bit */
  else
    clrRegBit(SPI0_SCR,CPHA);          /* Clear phase bit */
  EnUser=TRUE;
  HWEnDi();                            /* Enable device */
  return ERR_OK;
}

/*
** ===================================================================
**     Method      :  SM1_SetIdleClockPolarity (bean SynchroMaster)
**
**     Description :
**         Sets the idle clock polarity at runtime. If the
**         communication does not run, the clock signal will have
**         required level.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Level           - Idle clock polarity:
**                           0-low
**                           1-high
**     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
**                           ERR_RANGE - Parameter out of range
** ===================================================================
*/
byte SM1_SetIdleClockPolarity(byte Level)
{
  if (Level > 1)
    return ERR_RANGE;
  if ((getRegBit(SPI0_SCR,CPOL) && (!Level)) || (!getRegBit(SPI0_SCR,CPOL) && (Level))) {
    if (Level)
      setRegBits(GPIO_E_DR,16);        /* Level '1' will be on clk pin when device disabled */
    else
      clrRegBits(GPIO_E_DR,16);        /* Level '0' will be on clk pin when device disabled */
    EnUser=FALSE;
    HWEnDi();                          /* Disable device */
    setReg(SPI0_SCR,getReg(SPI0_SCR) ^ (SPI0_SCR_CPOL_MASK|SPI0_SCR_CPHA_MASK)); /* Invert polarity and phase bit */
    EnUser=TRUE;
    HWEnDi();                          /* Enable device */
  }
  return ERR_OK;
}

/*
** ===================================================================
**     Method      :  SM1_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 SM1_Init(void)
{
  /* SPI0_SCR: SPR2=1,SPR1=1,SPR0=1,DSO=1,ERRIE=0,MODFEN=0,SPRIE=0,SPMSTR=1,CPOL=0,CPHA=1,SPE=0,SPTIE=0,SPRF=0,OVRF=0,MODF=1,SPTE=0 */
  setReg(SPI0_SCR,61762);              /* Set control register */
  /* SPI0_DSR: WOM=0,TDMAEN=0,RDMAEN=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,DS3=0,DS2=1,DS1=1,DS0=1 */
  setReg(SPI0_DSR,7);                  /* 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 SM1. */

/*
** ###################################################################
**
**     This file was created by UNIS Processor Expert 2.97 [03.74]
**     for the Freescale 56800 series of microcontrollers.
**
** ###################################################################
*/

⌨️ 快捷键说明

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