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

📄 freescale

📁 Freescale 系列单片机常用模块与综合系统设计
💻
📖 第 1 页 / 共 2 页
字号:
/** ###################################################################
**     THIS COMPONENT MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**     Filename  : Master_SPI.C
**     Project   : Power_Monitor
**     Processor : MC9S08JM60CLHE
**     Component : SynchroMaster
**     Version   : Component 02.322, Driver 01.26, CPU db: 3.00.046
**     Compiler  : CodeWarrior HCS08 C Compiler
**     Date/Time : 2010-1-23, 16:39
**     Abstract  :
**         This bean "SynchroMaster" implements MASTER part of synchronous
**         serial master-slave communication.
**     Settings  :
**         Synchro type                : MASTER
**
**         Serial channel              : SPI1
**
**         Protocol
**             Init baud rate          : 4MHz
**             Clock edge              : rising
**             Width                   : 8 bits
**             Empty character         : 0
**             Empty char. on input    : RECEIVED
**
**         Registers
**             Input buffer            : SPI1DL    [$0055]
**             Output buffer           : SPI1DL    [$0055]
**             Control register        : SPI1C1    [$0050]
**             Mode register           : SPI1C2    [$0051]
**             Baud setting reg.       : SPI1BR    [$0052]
**
**             Priority                : 
**
**             Priority                : 
**
**         Used pins                   :
**         ----------------------------------------------------------
**              Function    | On package |    Name
**         ----------------------------------------------------------
**               Input      |     17     |  PTE4_MISO1
**               Output     |     18     |  PTE5_MOSI1
**               Clock      |     19     |  PTE6_SPSCK1
**         ----------------------------------------------------------
**
**     Contents  :
**         RecvChar        - byte Master_SPI_RecvChar(Master_SPI_TComData *Chr);
**         SendChar        - byte Master_SPI_SendChar(Master_SPI_TComData Chr);
**         RecvBlock       - byte Master_SPI_RecvBlock(Master_SPI_TComData *Ptr, word Size, word *Rcv);
**         SendBlock       - byte Master_SPI_SendBlock(Master_SPI_TComData *Ptr, word Size, word *Snd);
**         ClearRxBuf      - byte Master_SPI_ClearRxBuf(void);
**         ClearTxBuf      - byte Master_SPI_ClearTxBuf(void);
**         GetCharsInRxBuf - word Master_SPI_GetCharsInRxBuf(void);
**         GetCharsInTxBuf - word Master_SPI_GetCharsInTxBuf(void);
**         GetError        - byte Master_SPI_GetError(Master_SPI_TError *Err);
**
**     Copyright : 1997 - 2009 Freescale Semiconductor, Inc. All Rights Reserved.
**     
**     http      : www.freescale.com
**     mail      : support@freescale.com
** ###################################################################*/


/* MODULE Master_SPI. */

#include "Master_SPI.h"
#include "LCD_Data.h"
#include "LCD_E.h"
#include "LCD_DI.h"
#include "LCD_RW.h"
#include "LCD_CS1.h"
#include "LCD_CS2.h"
#include "Events.h"


/* Internal method prototypes */

#define OVERRUN_ERR      0x01          /* Overrun error flag bit   */
#define CHAR_IN_RX       0x08          /* Char is in RX buffer     */
#define FULL_TX          0x10          /* Full transmit buffer     */
#define RUNINT_FROM_TX   0x20          /* Interrupt is in progress */
#define FULL_RX          0x40          /* Full receive buffer      */

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 byte ErrFlag;                   /* Error flags mirror of SerFlag */
byte Master_SPI_InpLen;                /* Length of the input buffer content */
static byte InpIndxR;                  /* Index for reading from input buffer */
static byte InpIndxW;                  /* Index for writing to input buffer */
static Master_SPI_TComData InpBuffer[Master_SPI_INP_BUF_SIZE]; /* Input buffer SPI commmunication */
byte Master_SPI_OutLen;                /* Length of the output buffer content */
static byte OutIndxR;                  /* Index for reading from output buffer */
static byte OutIndxW;                  /* Index for writing to output buffer */
static Master_SPI_TComData OutBuffer[Master_SPI_OUT_BUF_SIZE]; /* Output buffer for SPI commmunication */

/*
** ===================================================================
**     Method      :  Master_SPI_RecvChar (component SynchroMaster)
**
**     Description :
**         If any data is received, this method returns one character,
**         otherwise it returns an error code (it does not wait for
**         data).
**     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. In
**                           polling mode, this error code is returned
**                           only when the hardware supports detection
**                           of the overrun error. 
**                           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 Master_SPI_RecvChar(Master_SPI_TComData *Chr)
{
  byte FlagTmp;

  if (Master_SPI_InpLen > 0) {         /* Is number of received chars greater than 0? */
    EnterCritical();                   /* Save the PS register */
    Master_SPI_InpLen--;               /* Decrease number of received chars */
    *Chr = InpBuffer[InpIndxR];        /* Read the char */
    InpIndxR = (byte)((InpIndxR+1) & (Master_SPI_INP_BUF_SIZE-1)); /* Update index */
    FlagTmp = SerFlag;                 /* Safe the flags */
    SerFlag &= ~(OVERRUN_ERR | FULL_RX); /* Clear flag "char in RX buffer" */
    ExitCritical();                    /* Restore the PS register */
  }
  else {
    return ERR_RXEMPTY;                /* Receiver is empty */
  }
  if ((FlagTmp & (OVERRUN_ERR | FULL_RX)) != 0) { /* Has the overrun occured? */
    return ERR_OVERRUN;                /* If yes then return error */
  } else {
    return ERR_OK;
  }
}

/*
** ===================================================================
**     Method      :  Master_SPI_SendChar (component SynchroMaster)
**
**     Description :
**         Sends one character to the channel.
**     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 Master_SPI_SendChar(Master_SPI_TComData Chr)
{
  EnterCritical();                     /* Save the PS register */
  if (Master_SPI_OutLen == Master_SPI_OUT_BUF_SIZE) { /* Is number of chars in buffer the same as the size of transmit buffer? */
    ExitCritical();                    /* Restore the PS register */
    return ERR_TXFULL;                 /* If yes then error */
  }
  Master_SPI_OutLen++;                 /* Increase number of bytes in the transmit buffer */
  OutBuffer[OutIndxW] = Chr;           /* Store char to buffer */
  OutIndxW = (byte)((OutIndxW+1) & (Master_SPI_OUT_BUF_SIZE-1)); /* Update index */
  if(!(SerFlag & RUNINT_FROM_TX)) {    /* Transmition running ? */
    (void)SPI1S;                       /* Read the status register */
    SPI1DL = OutBuffer[OutIndxR];      /* Store char to transmitter register */
    SerFlag |= RUNINT_FROM_TX;         /* Set RUNINT_FROM_TX flag */
  }
  ExitCritical();                      /* Restore the PS register */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  Master_SPI_RecvBlock (component SynchroMaster)
**
**     Description :
**         If any data received, this method returns the block of the
**         data and its length (and incidental error), otherwise it
**         returns error code (it does not wait for data).
**         If less than requested number of characters is received only
**         the available data is copied from the receive buffer to the
**         user specified destination and the ERR_EXEMPTY value is
**         returned.
**         This method is available only if non-zero length of input
**         buffer is defined.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Ptr             - A pointer to the block of received data
**         Size            - The size of the block
**       * Rcv             - Pointer to a variable where an actual
**                           number of copied characters is stored
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_RXEMPTY - It was not possible to read
**                           requested number of bytes from the buffer
**                           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 Master_SPI_RecvBlock(Master_SPI_TComData *Ptr,word Size,word *Rcv)
{
  word count;                          /* Number of received chars */
  byte Result = ERR_OK;                /* Most serious error */
  byte ResultTmp;                      /* Last error */

  count = 0;
  while (count < Size) {
    ResultTmp = Master_SPI_RecvChar(Ptr++);
    if (ResultTmp>Result) {            /* Is last error most serious than through error state? */
      Result = ResultTmp;              /* If yes then prepare error value to return */
    }
    if ((ResultTmp != ERR_OK)&&(ResultTmp != ERR_OVERRUN)) { /* Receiving given number of chars */
     *Rcv = count;                     /* Return number of received chars */
      return Result;                   /* Return most serious error */
    }
    count++;
  }
  *Rcv = count;                        /* Return number of received chars */
  return Result;                       /* Return most serious error */
}

/*
** ===================================================================
**     Method      :  Master_SPI_SendBlock (component SynchroMaster)
**
**     Description :
**         Send a block of characters to the channel. This method is
**         only available if a non-zero length of output buffer is
**         defined.
**     Parameters  :

⌨️ 快捷键说明

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