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

📄 freescale

📁 Freescale 系列单片机常用模块与综合系统设计
💻
📖 第 1 页 / 共 2 页
字号:
**         NAME            - DESCRIPTION
**       * Ptr             - Pointer to the block of data to send
**         Size            - Size of the block
**       * Snd             - Pointer to number of data that are sent
**                           (moved to buffer)
**     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 - It was not possible to send
**                           requested number of bytes
** ===================================================================
*/
byte Master_SPI_SendBlock(Master_SPI_TComData * Ptr,word Size,word *Snd)
{
  word count;                          /* Number of bytes to send*/
  byte Result = ERR_OK;                /* Last error */

  for (count = 0; count < Size; count++) {
    Result = Master_SPI_SendChar(*Ptr++); /* Send one character */
    if (Result != ERR_OK) {            /* Sending given number of chars */
      break;                           /* Break data block sending */
    }
  }
  *Snd = count;                        /* Return number of sended chars */
  return Result;                       /* Return error code */
}

/*
** ===================================================================
**     Method      :  Master_SPI_ClearRxBuf (component SynchroMaster)
**
**     Description :
**         Clears the receive buffer. This method is available only if
**         a non-zero length of input buffer is defined.
**     Parameters  : None
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte Master_SPI_ClearRxBuf(void)
{
  EnterCritical();                     /* Save the PS register */
  Master_SPI_InpLen = 0;               /* Set number of chars in the transmit buffer to 0 */
  InpIndxR = InpIndxW = 0;             /* Reset indices */
  SerFlag &= ~(OVERRUN_ERR | FULL_RX); /* Clear flags */
  ExitCritical();                      /* Restore the PS register */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  Master_SPI_ClearTxBuf (component SynchroMaster)
**
**     Description :
**         Clears the transmit buffer. This method is only available if
**         a non-zero length of output buffer is defined.
**     Parameters  : None
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte Master_SPI_ClearTxBuf(void)
{
  EnterCritical();                     /* Save the PS register */
  Master_SPI_OutLen = 0;               /* Set number of chars in the receive buffer to 0 */
  OutIndxR = OutIndxW = 0;             /* Reset indices */
  ExitCritical();                      /* Restore the PS register */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  Master_SPI_GetCharsInRxBuf (component 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.
**     Parameters  : None
**     Returns     :
**         ---             - Number of characters in the input buffer.
** ===================================================================
*/
/*
word Master_SPI_GetCharsInRxBuf(void)

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

/*
** ===================================================================
**     Method      :  Master_SPI_GetCharsInTxBuf (component SynchroMaster)
**
**     Description :
**         Returns the number of characters in the output buffer.
**     Parameters  : None
**     Returns     :
**         ---             - Number of characters in the output buffer.
** ===================================================================
*/
/*
word Master_SPI_GetCharsInTxBuf(void)

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

/*
** ===================================================================
**     Method      :  Master_SPI_GetError (component SynchroMaster)
**
**     Description :
**         Returns a set of errors on the channel (errors that cannot
**         be returned in given methods). The errors accumulate in a
**         set; after calling [GetError] this set is returned and
**         cleared.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Err             - A pointer to the returned set of errors
**     Returns     :
**         ---             - Error code (if GetError did not succeed),
**                           possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte Master_SPI_GetError(Master_SPI_TError *Err)
{
  EnterCritical();                     /* Save the PS register */
  Err->err = 0;
  Err->errName.OverRun = ((ErrFlag & OVERRUN_ERR) != 0); /* Overrun error */
  Err->errName.RxBufOvf = ((ErrFlag & FULL_RX) != 0); /* Buffer overflow */
  ErrFlag = 0x00;                      /* Reset error flags */
  ExitCritical();                      /* Restore the PS register */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  Master_SPI_Interrupt (component SynchroMaster)
**
**     Description :
**         The method services the error interrupt of the selected 
**         peripheral(s) and eventually invokes the bean's event(s).
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
#define ON_ERROR    0x01
#define ON_FULL_RX  0x02
#define ON_RX_CHAR  0x04
#define ON_FREE_TX  0x08
#define ON_TX_CHAR  0x10
ISR(Master_SPI_Interrupt)
{
  Master_SPI_TComData Data = 0;        /* Temporary variable for data */
  byte Flags = 0;                      /* Temporary variable for flags */
  byte Status;                         /* Temporary variable for flags */

  Status = SPI1S;                      /* Read the device error register */
  Data = SPI1DL;                       /* Read data from receiver */
  if(Master_SPI_InpLen < Master_SPI_INP_BUF_SIZE) { /* Is number of bytes in the receive buffer lower than size of buffer? */
    Master_SPI_InpLen++;               /* Increase number of chars in the receive buffer */
    InpBuffer[InpIndxW] = Data;        /* Save received char to the receive buffer */
    InpIndxW = (byte)((InpIndxW+1) & (Master_SPI_INP_BUF_SIZE-1)); /* Update index */
    Flags |= ON_RX_CHAR;               /* If yes then set the OnRxChar flag */
    if(Master_SPI_InpLen == Master_SPI_INP_BUF_SIZE) { /* Is number of bytes in the receive buffer equal to the size of buffer? */
      Flags |= ON_FULL_RX;             /* Set flag "OnFullRxBuf" */
    }
  }
  else {
    SerFlag |= FULL_RX;                /* Set flag "full RX buffer" */
    Flags |= ON_ERROR;                 /* Set the OnError flag */
    ErrFlag |= SerFlag;                /* Update error flag mirror */
  }
  Master_SPI_OutLen--;                 /* Decrease number of chars in the transmit buffer */
  OutIndxR = (byte)((OutIndxR+1) & (Master_SPI_OUT_BUF_SIZE-1)); /* Update index */
  if (Master_SPI_OutLen) {             /* Is number of bytes in the transmit buffer greather then 0? */
    SPI1DL = OutBuffer[OutIndxR];      /* Store char to transmitter register */
  }
  else {
    SerFlag &= ~(RUNINT_FROM_TX|FULL_TX); /* Clear "running int from TX" and "full TX buff" flags */
    Flags |= ON_FREE_TX;               /* Set the OnFreeTXBuf flag */
  }
  if(Flags & ON_ERROR) {               /* Is the error flag set? */
    Master_SPI_OnError();              /* If yes then invoke user event */
  }
  else {
    if(Flags & ON_RX_CHAR) {           /* Is OnRxChar flag set? */
      Master_SPI_OnRxChar();           /* If yes then invoke user event */
    }
    if(Flags & ON_FULL_RX) {           /* Is OnTxChar flag set? */
      Master_SPI_OnFullRxBuf();        /* If yes then invoke user event */
    }
  }
  Master_SPI_OnTxChar();               /* If yes then invoke user event */
  if (Flags & ON_FREE_TX) {            /* Is flag "OnFreeTxBuf" set? */
    Master_SPI_OnFreeTxBuf();          /* If yes then invoke user event */
  }
}

/*
** ===================================================================
**     Method      :  Master_SPI_Init (component SynchroMaster)
**
**     Description :
**         Initializes the associated peripheral(s) and the bean 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 Master_SPI_Init(void)
{
  SerFlag = 0;                         /* Reset all flags */
  ErrFlag = 0;                         /* Reset all flags in mirror */
  Master_SPI_InpLen = 0;               /* No char in the receive buffer */
  InpIndxW = InpIndxR = 0;             /* Set the indices to the start of the receive buffer */
  Master_SPI_OutLen = 0;               /* No char in the transmit buffer */
  OutIndxW = OutIndxR = 0;             /* Set the indices to the start of the transmit buffer */
  #pragma MESSAGE DISABLE C4002        /* Disable warning C4002 "Result not used" */
  (void)SPI1S;                         /* Read the status register */
  (void)SPI1DL;                        /* Read the device register */
  /* SPI1BR: ??=0,SPPR2=0,SPPR1=1,SPPR0=0,??=0,SPR2=0,SPR1=0,SPR0=1 */
  setReg8(SPI1BR, 0x21);               /* Set the baud rate register */ 
  /* SPI1C2: SPMIE=0,SPIMODE=0,??=0,MODFEN=0,BIDIROE=0,??=0,SPISWAI=0,SPC0=0 */
  setReg8(SPI1C2, 0x00);               /* Configure the SPI port - control register 2 */ 
  /* SPI1C1: SPIE=1,SPE=0,SPTIE=0,MSTR=1,CPOL=0,CPHA=1,SSOE=0,LSBFE=1 */
  setReg8(SPI1C1, 0x95);               /* Configure the SPI port - control register 1 */ 
  SPI1C1_SPE = 1;                      /* Enable device */
}


/* END Master_SPI. */

/*
** ###################################################################
**
**     This file was created by Processor Expert 3.07 [04.34]
**     for the Freescale HCS08 series of microcontrollers.
**
** ###################################################################
*/

⌨️ 快捷键说明

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