📄 usb.c
字号:
/** ###################################################################
** THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
** Filename : USB.C
** Project : RTOSDemo
** Processor : MC9S08GT60CFD
** Beantype : AsynchroSerial
** Version : Bean 02.333, Driver 01.12, CPU db: 2.87.074
** Compiler : Metrowerks HCS08 C Compiler
** Date/Time : 3/10/2006, 1:34 PM
** Abstract :
** This bean "AsynchroSerial" implements an asynchronous serial
** communication. The bean supports different settings of
** parity, word width, stop-bit and communication speed,
** user can select interrupt or polling handler.
** Communication speed can be changed also in runtime.
** The bean requires one on-chip asynchronous serial channel.
** Settings :
** Serial channel : SCI2
**
** Protocol
** Init baud rate : 57600baud
** Width : 8 bits
** Stop bits : 1
** Parity : none
** Breaks : Disabled
**
** Registers
** Input buffer : SCI2D [0027]
** Output buffer : SCI2D [0027]
** Control register : SCI2C1 [0022]
** Mode register : SCI2C2 [0023]
** Baud setting reg. : SCI2BD [0020]
** Special register : SCI2S1 [0024]
**
** Input interrupt
** Vector name : Vsci2rx
**
** Output interrupt
** Vector name : Vsci2tx
**
** Used pins:
** ----------------------------------------------------------
** Function | On package | Name
** ----------------------------------------------------------
** Input | 3 | PTC1_RxD2
** Output | 2 | PTC0_TxD2
** RTS | 42 | PTA7_KBI1P7
** ----------------------------------------------------------
**
** Note: RTS pin is NOT supported by hardware.
** It is handled by software.
**
**
** Contents :
** RecvChar - byte USB_RecvChar(USB_TComData *Chr);
** SendChar - byte USB_SendChar(USB_TComData Chr);
** GetCharsInRxBuf - word USB_GetCharsInRxBuf(void);
** GetCharsInTxBuf - word USB_GetCharsInTxBuf(void);
**
** (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 USB. */
#pragma MESSAGE DISABLE C4002 /* WARNING C4002: Result not used is ignored */
#include "USB.h"
#include "Events.h"
#define OVERRUN_ERR 0x01 /* Overrun error flag bit */
#define COMMON_ERR 0x02 /* Common error of RX */
#define CHAR_IN_RX 0x04 /* Char is in RX buffer */
#define FULL_TX 0x08 /* Full transmit buffer */
static byte SerFlag; /* Flags for serial communication */
/* Bit 0 - Overrun error */
/* Bit 1 - Common error of RX */
/* Bit 2 - Char in RX buffer */
/* Bit 3 - Full TX buffer */
static bool EnMode; /* Enable/Disable SCI in speed mode */
static USB_TComData BufferRead; /* Input char for SCI commmunication */
/*
** ===================================================================
** Method : HWEnDi (bean AsynchroSerial)
**
** 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(EnMode) { /* Enable device? */
SCI2BDH = 0x00; /* Set high divisor register (enable device) */
SCI2BDL = 0x16; /* Set low divisor register (enable device) */
/* SCI2C3: ORIE=1,NEIE=1,FEIE=1,PEIE=1 */
SCI2C3 |= 0x0F; /* Enable error interrupts */
SCI2C2 |= ( SCI2C2_TE_MASK | SCI2C2_RE_MASK | SCI2C2_RIE_MASK); /* Enable transmitter, Enable receiver, Enable receiver interrupt */
if(!(SerFlag & CHAR_IN_RX)) /* Is any char in the receive buffer? */
/* PTAD: PTAD7=0 */
PTAD &= ~0x80; /* Set RTS to the low level */
}
else {
/* PTAD: PTAD7=1 */
PTAD |= 0x80; /* Set RTS to the high level */
/* SCI2C3: ORIE=0,NEIE=0,FEIE=0,PEIE=0 */
SCI2C3 &= ~0x0F; /* Disable error interrupts */
SCI2C2 &= ( (~SCI2C2_RE_MASK) & (~SCI2C2_TE_MASK) & (~SCI2C2_TIE_MASK) & (~SCI2C2_RIE_MASK)); /* Disable receiver, Disable transmitter, Disable transmit interrupt, Disable receiver interrupt */
SCI2BDH = 0x00; /* Set high divisor register to zero (disable device) */
SCI2BDL = 0x00; /* Set low divisor register to zero (disable device) */
}
}
/*
** ===================================================================
** Method : USB_RecvChar (bean AsynchroSerial)
**
** Description :
** If any data is received, this method returns one
** character, otherwise it returns an error code (it does
** not wait for data). This method is enabled only if the
** receiver property is enabled.
** DMA mode:
** If DMA controller is available on the selected CPU and
** the receiver is configured to use DMA controller then
** this method only sets the selected DMA channel. Then the
** status of the DMA transfer can be checked using
** GetCharsInRxBuf method. See an example of a typical usage
** for details about the communication using DMA.
** Parameters :
** NAME - DESCRIPTION
** * Chr - Pointer to a 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_BREAK - Break character is detected(only when <Interrupt service> property
** is disabled)
** ERR_COMMON - common error occurred (the
** GetError method can be used for error
** specification)
** DMA mode:
** If DMA controller is available on the
** selected CPU and the receiver is
** configured to use DMA controller then
** only ERR_OK, ERR_RXEMPTY, and ERR_SPEED
** error code can be returned from this
** method.
** ===================================================================
*/
byte USB_RecvChar(USB_TComData *Chr)
{
byte Result = ERR_OK; /* Return error code */
if(!EnMode) /* Is the device disabled in the actual speed CPU mode? */
return ERR_SPEED; /* If yes then error */
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; /* Received char */
/* PTAD: PTAD7=0 */
PTAD &= ~0x80; /* Set RTS to the low level */
Result = (byte)((SerFlag & (OVERRUN_ERR|COMMON_ERR))?ERR_COMMON:ERR_OK);
SerFlag &= ~(OVERRUN_ERR|COMMON_ERR|CHAR_IN_RX); /* Clear all errors in the status variable */
ExitCritical(); /* Restore the PS register */
return Result; /* Return error code */
}
/*
** ===================================================================
** Method : USB_SendChar (bean AsynchroSerial)
**
** Description :
** Sends one character to the channel. If the bean is
** temporarily disabled (Disable method) SendChar method
** only stores data into an output buffer. In case of a zero
** output buffer size, only one character can be stored.
** Enabling the bean (Enable method) starts the transmission
** of the stored data. This method is available only if the
** transmitter property is enabled.
** 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 selected DMA channel. Then the
** status of the DMA transfer can be checked using
** GetCharsInTxBuf method. See an example of a 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_TXFULL - Transmitter is full
** ===================================================================
*/
byte USB_SendChar(USB_TComData Chr)
{
if(!EnMode) /* Is the device disabled in the actual speed CPU mode? */
return ERR_SPEED; /* If yes then error */
if(SerFlag & FULL_TX) /* Is any char is in TX buffer */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -