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

📄 bl78k0_kx2_uart.c

📁 NEC upd78f0300的自编程序样例
💻 C
字号:
//==============================================================================
// PROJECT      = Bootloader
// MODULE       = bl78k0_kx2_uart.c
// SHORT DESC.  = UART driver routines
// DEVICE       = 78K0/Kx2
// VERSION      = 1.0
// DATE         = 05.12.2006
// LAST CHANGE  =
// =============================================================================
// By:          NEC Electronics (Europe) GmbH
//              Arcadiastrasse 10
//              D-40472 Duesseldorf
// =============================================================================





// Include files
//*********************

#include "io78f0547_80.h"
#include <intrinsics.h>
#include "bl78k0_kx2_user.h"
#include "bl78k0_kx2_uart.h"




//*****************************************************************************
// Function:    ifaceInit
// Parameter:   None
// Return:      void
// Description: This function set baudrate for the uart which is defined
//              in defines.h. Also this function set the uart for
//              8 Bit data, 1 stop bit, no parity
//*****************************************************************************

void ifaceInit(void)
{

  PM1_bit.no4=1;                          //input mode for RxD
  PM1_bit.no3=0;                          //output mode for TxD
  P1_bit.no3=1;                           //set TxD output to high level


  #if CLOCK_FREQUENCY == 20
      #if BAUDRATE == 115200
        CKSR6 = 0x00;                       // peripheral clock isn't divided
        BRGC6 = 87;                         // set baudrate
      #elif BAUDRATE  == 57600
        CKSR6 = 0x00;                       // peripheral clock isn't divided
        BRGC6 = 174;                         // set baudrate
      #else
        #error ERROR: Choosen baud rate is not supported!
      #endif


  #elif CLOCK_FREQUENCY == 12

      #if BAUDRATE   == 115200
        CKSR6 = 0x00;                       // peripheral clock isn't divided
        BRGC6 = 52;                         // set baudrate
      #elif BAUDRATE  == 57600
        CKSR6 = 0x00;                       // peripheral clock isn't divided
        BRGC6 = 104;                         // set baudrate
      #else
        #error ERROR: Choosen baud rate is not supported!
      #endif
  #else
    #error ERROR: Frequency not supported! Change const. CLOCK_FREQUENCY!
  #endif


  POWER6 = 1;                             //enable internal clock operation
  ASIM6 |= 0x05;                          //no parity
                                          //character lenght of data = 8-bits
                                          //number of stop bits = 1

  ASIM6 |= 0xE0;                          //enable transmission
                                          //enable reception
}



//*****************************************************************************
// Function:    readyToRx
// Parameter:   None
// Return:      void
// Description: This function transmit the Xon message(11 hex) to the host.
//              Host can send bytes after received Xon message.
//*****************************************************************************
void readyToRx(void)
{
    TXB6 = XON;                           // write Xon byte in transmit buffer
}


//*****************************************************************************
// Function:    readyToRxEnabled
// Parameter:   None
// Return:      u08
//              0: interface don't completed the activation for ready to receive
//              1: completed
// Description: this function is essential, because there can be other methods(not Xon/Xoff)
//              for other interfaces to tell the transmitter(host) that the interface
//              is ready for receive. TX IRQ flag for is reset.
//*****************************************************************************

u08 readyToRxEnabled(void)
{
  if(STIF6)                               // is transmitted(irq)?
  {
    STIF6=0;                              // reset irq
    return 1;                             // transmitted, interface is ready
  }
  return 0;                               // interface isn't ready
}




//*****************************************************************************
// Function:    stopRx
// Parameter:   void
// Return:      void
// Description: This function transmit the Xoff message(13 hex) to the host.
//              Host stop send bytes after received Xoff message.
//*****************************************************************************
void stopRx(void)
{
    TXB6 = XOFF;                          // write Xoff byte in transmit buffer
}

//*****************************************************************************
// Function:    stopRxEnabled
// Parameter:   None
// Return:      u08
//              0: interface don't completed the activation for ready to receive
//              1: completed
// Description: this function is essential, because there can be other methods
//              (not Xon/Xoff) for other interfaces to tell the transmitter that
//               the interface is ready for receive. RX IRQ is reset.
//*****************************************************************************

u08 stopRxEnabled(void)
{
  if(STIF6)                               // is transmitted(irq)?
  {
    STIF6=0;                              // reset irq
    return 1;                             // transmitted, interface is ready
  }
  return 0;                               // interface isn't ready
}



//*****************************************************************************
// Function:    sendByte
// Parameter:   u08 byte -> byte to send
// Return:      void
// Description: This function write the transmit-byte in the tx-buffer
// Important:   Before you use this function check the tx-state with the function
//              txStatus() whether the tx-modul is already in use
//*****************************************************************************
void sendByte(u08 txData)
{
  TXB6 = txData;                          // write into UART transmit buffer
}



//*****************************************************************************
// Function:    txStatus
// Parameter:   void
// Return:      u08
//                    0: tx ready -> request bit is cleared
//                    1: tx still send
// Description: This function return the state of the tx module whether
//              transmission is completed or not
//*****************************************************************************
u08 txStatus(void)
{
  if(STIF6)                               // is tx irq?
  {
    STIF6=0;                              // clear tx irq
    return 0;                             // return byte send
  }
  return 1;                               // still send
}





//*****************************************************************************
// Function:    byteReceived
// Parameter:   void
// Return:      u08
//              1: byte received
//              0: nothing received
// Description: This function verfy the rx irq, whether one byte is received
//*****************************************************************************
__callt u08 byteReceived(void)
{
  if(SRIF6)                               // is rx irq?
  {
    SRIF6=0;                              // clear rx irq
    return 1;                             // return byte received
  }
  return 0;                               // nothing received
}



//*****************************************************************************
// Function:    getRxByte
// Parameter:   void
// Return:      u08
//              1: ok, none error
//              0: error on receive
// Description: This function return the error status of the receiver
//*****************************************************************************
__callt u08 rxError(void)
{
  if( ASIS6 != 0 )                        // is ASIS6 reg. clear?
  {
   return 0;                              // error
  }
  return 1;                               // no error
}

//*****************************************************************************
// Function:    getRxByte
// Parameter:   void
// Return:      u08 -> received byte on UART6
// Description: This function return the received byte.
//*****************************************************************************
__callt u08 getRxByte(void)
{
  u08 receive_byte = 0;
  receive_byte = RXB6;                    // load UART receive buffer
  SRIF6=0;                                // reset interrupt flag
  return receive_byte;                    // return received byte
}


//*****************************************************************************
// Function:    resetRxErrorFlags
// Parameter:   none
// Return:      void
// Description: Clear all receive bits
//*****************************************************************************
void resetRxErrorFlags(void)
{
    // to clear the error flags, the register ASIS6 must be read and then the RXB6
    // register
    u08 temp;
    temp = ASIS6;             // read ASIS6 register to clear all bits
    temp = RXB6;              // read rx buffer, this prevents an overrun error
}




void resetInterface(void)
{
  resetRxErrorFlags();

}



⌨️ 快捷键说明

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