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

📄 sm1.c

📁 用PE生成的SPI驱动,是一个很好的学习使用PE开发的范例
💻 C
字号:
/** ###################################################################
**     THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**     Filename  : SM1.C
**     Project   : DP256B_PE_SPI
**     Processor : MC9S12DP256BCPV
**     Beantype  : SynchroMaster
**     Version   : Bean 02.251, Driver 01.14, CPU db: 2.87.419
**     Compiler  : CodeWarrior HC12 C Compiler
**     Date/Time : 2007-4-29, 14:20
**     Abstract  :
**         This bean "SynchroMaster" implements MASTER part of synchronous
**         serial master-slave communication.
**     Settings  :
**         Synchro type                : MASTER
**
**         Serial channel              : SPI0
**
**         Protocol
**             Init baud rate          : 100kHz
**             Clock edge              : rising
**             Width                   : 8 bits (always)
**             Empty character         : 0
**             Empty char. on input    : RECEIVED
**
**         Registers
**             Input buffer            : SPI0DR    [221]
**             Output buffer           : SPI0DR    [221]
**             Control register        : SPI0CR1   [216]
**             Mode register           : SPI0CR2   [217]
**             Baud setting reg.       : SPI0BR    [218]
**
**         Input interrupt
**             Vector name             : Vspi0
**             Priority                : 1
**
**         Output interrupt
**             Vector name             : Vspi0
**             Priority                : 1
**
**         Used pins                   :
**         ----------------------------------------------------------
**              Function    | On package |    Name
**         ----------------------------------------------------------
**               Input      |     93     |  PS4_MISO0
**               Output     |     94     |  PS5_MOSI0
**               Clock      |     95     |  PS6_SCK0
**         ----------------------------------------------------------
**
**     Contents  :
**         RecvChar        - byte SM1_RecvChar(SM1_TComData *Chr);
**         SendChar        - byte SM1_SendChar(SM1_TComData Chr);
**         GetCharsInRxBuf - word SM1_GetCharsInRxBuf(void);
**         GetCharsInTxBuf - word SM1_GetCharsInTxBuf(void);
**         GetError        - byte SM1_GetError(SM1_TError *Err);
**
**     (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 SM1. */

#include "SM1.h"
#include "Events.h"

/* Internal method prototypes */

#pragma DATA_SEG SM1_DATA
#pragma CODE_SEG SM1_CODE

#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 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 */
static SM1_TComData BufferRead;        /* Input char SPI commmunication */

/*
** ===================================================================
**     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).
**     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 SM1_RecvChar(SM1_TComData *Chr)
{
  byte FlagTmp;

  if (!(SerFlag & CHAR_IN_RX)) {       /* Is any char in RX buffer? */
    return ERR_RXEMPTY;                /* If no then error */
  }
  EnterCritical();                     /* Save the PS register */
  *Chr = BufferRead;                   /* Read the char */
  FlagTmp = SerFlag;                   /* Safe the flags */
  SerFlag &= ~(OVERRUN_ERR | CHAR_IN_RX | FULL_RX); /* Clear flag "char in RX buffer" */
  ExitCritical();                      /* Restore the PS register */
  if ((FlagTmp & OVERRUN_ERR) != 0) {  /* Is the overrun occured? */
    return ERR_OVERRUN;                /* If yes then return error */
  }
  else {
    return ERR_OK;
  }
}

/*
** ===================================================================
**     Method      :  SM1_SendChar (bean 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 SM1_SendChar(SM1_TComData Chr)
{
  if (SerFlag & FULL_TX) {             /* Is any char in the TX buffer? */
    return ERR_TXFULL;                 /* If yes then error */
  }
  EnterCritical();                     /* Save the PS register */
  SPI0DR = Chr;                        /* Store char to transmitter register */
  SerFlag |= FULL_TX;                  /* Set the flag "full TX buffer" */
  ExitCritical();                      /* Restore the PS register */
  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.
**     Parameters  : None
**     Returns     :
**         ---             - Number of characters in the input
**                           buffer.
** ===================================================================
*/
word SM1_GetCharsInRxBuf(void)
{
  return (SerFlag >> 3) & 1;           /* Return number of chars in receive buffer */
}
/*
** ===================================================================
**     Method      :  SM1_GetCharsInTxBuf (bean SynchroMaster)
**
**     Description :
**         Returns the number of characters in the output buffer.
**     Parameters  : None
**     Returns     :
**         ---             - Number of characters in the output
**                           buffer.
** ===================================================================
*/
word SM1_GetCharsInTxBuf(void)
{
  return (SerFlag >> 4) & 1;           /* Return number of chars in the transmit buffer */
}
/*
** ===================================================================
**     Method      :  SM1_GetError (bean 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 SM1_GetError(SM1_TError *Err)
{
  EnterCritical();                     /* Save the PS register */
  Err->err = 0;
  Err->errName.OverRun = ((ErrFlag & OVERRUN_ERR) != 0); /* Overrun error */
  ErrFlag = 0;                         /* Reset error flags */
  ExitCritical();                      /* Restore the PS register */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  SM1_Interrupt (bean SynchroMaster)
**
**     Description :
**         The method services the interrupt of the selected peripheral(s)
**         and eventually invokes event(s) of the bean.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
#define ON_ERROR    1
#define ON_FULL_RX  2
#define ON_RX_CHAR  4
#define ON_FREE_TX  8
#define ON_TX_CHAR  16
#pragma CODE_SEG __NEAR_SEG NON_BANKED
ISR(SM1_Interrupt)
{
  SM1_TComData Data;                   /* Temporary variable for data */
  byte Flags = 0;                      /* Temporary variable for flags */
  byte Status;                         /* Temporary variable for flags */

  Status = SPI0SR;                     /* Read the device error register */
  Data = SPI0DR;                       /* Read data from receiver */
  if (SerFlag & CHAR_IN_RX) {          /* Is the overrun error flag set? */
    SerFlag |= OVERRUN_ERR;            /* If yes then set the OnError flag */
    ErrFlag |= OVERRUN_ERR;            /* Set the error flag for the GetError method */
    Flags |= ON_ERROR;                 /* Set the OnError flag */
  }
  SerFlag |= CHAR_IN_RX;               /* Set flag "char in RX buffer" */
  BufferRead = Data;                   /* Read data from receiver */
  SerFlag &= ~FULL_TX;                 /* Reset flag "full TX buffer" */
  if(Flags & ON_ERROR) {               /* Is any error flag set? */
    __DI();                            /* Disable maskable interrupts */
    SM1_OnError();                     /* If yes then invoke user event */
  }
  else {
    __DI();                            /* Disable maskable interrupts */
    SM1_OnRxChar();                    /* If yes then invoke user event */
  }
  __DI();                              /* Disable maskable interrupts */
  SM1_OnTxChar();                      /* If yes then invoke user event */
}

#pragma CODE_SEG SM1_CODE
/*
** ===================================================================
**     Method      :  SM1_Init (bean 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 SM1_Init(void)
{
  /* SPI0CR1: SPIE=0,SPE=0,SPTIE=0,MSTR=0,CPOL=0,CPHA=1,SSOE=0,LSBFE=0 */
  SPI0CR1 = 4;                         /* Reset the device register */
  #pragma MESSAGE DISABLE C4002        /* Disable warning C4002 "Result not used" */
  (void)SPI0SR;                        /* Read the status register */
  (void)SPI0DR;                        /* Read the device register */
  /* SPI0BR: ??=0,SPPR2=1,SPPR1=0,SPPR0=0,??=0,SPR2=0,SPR1=1,SPR0=1 */
  SPI0BR = 67;                         /* Set the baud rate register */
  /* SPI0CR2: ??=0,??=0,??=0,MODFEN=0,BIDIROE=0,??=0,SPISWAI=0,SPC0=0 */
  SPI0CR2 = 0;                         /* Set control register 2 */
  /* SPI0CR1: SPIE=1,SPE=1,SPTIE=0,MSTR=1,CPOL=0,CPHA=1,SSOE=0,LSBFE=1 */
  SPI0CR1 = 213;                       /* Set control register 1 */
  SerFlag = 0;                         /* Reset all flags */
  ErrFlag = 0;                         /* Reset all flags in mirror */
}


/* END SM1. */

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

⌨️ 快捷键说明

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