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

📄 mcohw_mm.c

📁 MicoCANOpen很好的学习源代码
💻 C
字号:
/**************************************************************************
MODULE:    MCOHWMM
CONTAINS:  MicroCANopen hardware driver implementation for
           PEAK System Tehnik MicroMod Module
           www.peak-system.com
           Compiled with Fujitsu Softune Workbench
COPYRIGHT: Embedded Systems Academy, Inc. 2002 - 2007
           All rights reserved. www.microcanopen.com
           This software was written in accordance to the guidelines at
           www.esacademy.com/software/softwarestyleguide.pdf
DISCLAIM:  Read and understand our disclaimer before using this code!
           www.esacademy.com/disclaim.htm
LICENSE:   THIS IS THE EDUCATIONAL VERSION OF MICROCANOPEN
           See file license_educational.txt or
           www.microcanopen.com/license_educational.txt
           A commercial MicroCANopen license is available at
           www.CANopenStore.com
VERSION:   3.30, ESA 30-JAN-07
           $LastChangedDate: 2007-01-30 20:50:41 -0800 (Tue, 30 Jan 2007) $
           $LastChangedRevision: 235 $
***************************************************************************/ 

#include "inc\can.h"
#include "inc\timer.h"
#include "inc\compiler.h"
#include "inc\mb90495.h"
#include "inc\vectors.h"
#include "inc\hardware.h"
#include "inc\adi.h"

#include "..\mco\mco.h"

// global timer tick, counts 1ms
UNSIGNED16 volatile gTimCnt = 0;

// CAN-ID SW Receive Filter
// Maximum number of filters
#define MAX_CAN_FILTERS 8

// Current number of filters
UNSIGNED8 gNrOfFilters = 0;

// Array with filters
UNSIGNED16 gCANFilterList[MAX_CAN_FILTERS];


/**************************************************************************
DOES:    Gets the next received CAN message and places it in
         a receive buffer                                         
RETURNS: 0 if no message received, 1 if message received and      
         copied to the buffer                                     
**************************************************************************/

UNSIGNED8 MCOHW_PullMessage (
  CAN_MSG *pReceiveBuf  // pointer to a single message sized buffer
                        // to hold the received message
  )
{
UNSIGNED8 i; // loop vaiable for data copy

    if(CAN_Read() != CAN_ERR_OK)
    { // Nothing received
        return 0;
    }

    // Copy received message to destination
    pReceiveBuf->ID = CAN_BUFF_ID;
    pReceiveBuf->LEN = CAN_BUFF_LEN;

    for (i = 0; i < CAN_BUFF_LEN; i++)
    {
        pReceiveBuf->BUF[i] = CAN_BUFF_DATA[i];
    }
    
    return 1;
}


/**************************************************************************
DOES:    Transmits a CAN message                                  
RETURNS: 0 if the message could not be transmitted, 1 if the      
         message was transmitted                                  
**************************************************************************/
UNSIGNED8 MCOHW_PushMessage (
  CAN_MSG *pTransmitBuf  // pointer to buffer containing CAN
                         // message to transmit
  )
{
UNSIGNED8 i; // Loop counter to copy data

    // Copy message into transmit buffer
    CAN_BUFF_FORMAT = 0;
    CAN_BUFF_ID = pTransmitBuf->ID;
    CAN_BUFF_LEN = pTransmitBuf->LEN;

    for (i = 0; i < CAN_BUFF_LEN; i++)
    {
        CAN_BUFF_DATA[i] = pTransmitBuf->BUF[i];
    }
    
    CAN_Write();
    
    return 1;
}

/**************************************************************************
DOES:    Gets the value of the current 1 millisecond system timer 
RETURNS: The current timer tick                                   
**************************************************************************/
UNSIGNED16 MCOHW_GetTime (
  void
  )
{
  return gTimCnt;
}


/**************************************************************************
DOES:    Checks if a moment in time has passed (a timestamp has expired)
RETURNS: 0 if timestamp has not yet expired, 1 if the             
         timestamp has expired                                    
**************************************************************************/
UNSIGNED8 MCOHW_IsTimeExpired
  (
  UNSIGNED16 timestamp  // timestamp to check for expiration
  )
{
UNSIGNED16 time_now;

  // get current time
  time_now = gTimCnt;

  // Adjust time_now so that timer expires 1ms in advance
  // This is a fix to improve overall timing behavior
  time_now++;

  if (time_now > timestamp)
  {
    if ((time_now - timestamp) < 0x8000)
      return 1;
    else
      return 0;
  }
  else
  {
    if ((timestamp - time_now) > 0x8000)
      return 1;
    else
      return 0;
  }
}

static UNSIGNED8 ntog;

/**************************************************************************
DOES:    Timer interrupt service routine                          
         Increments the global millisecond counter tick           
         This needs to be called once every millisecond  
RETURNS: nothing                                                     
**************************************************************************/
void Timer_2000Hz()
{
    
    ntog++;
    if(ntog&1) // only continue every other call   
    {
      gTimCnt++;
      AD_1000Hz();
    }
}


/**************************************************************************
DOES:    Initializes the CAN interface.                           
CAUTION: Does not initialize filters - nothing will be received   
         unless screeners are set using set_screener_std          
RETURNS: 0 for failed init, 1 for successful init                 
**************************************************************************/
UNSIGNED8 MCOHW_Init (
  UNSIGNED16 BaudRate  // desired baudrate in kbps
  )
{
UNSIGNED16 btr;

    gNrOfFilters = 0;
    
    switch (BaudRate)
    {
    case 1000:
        btr = CAN_BAUD_1M;
        break; 
    case 500:
        btr = CAN_BAUD_500K;
        break; 
    case 250:
        btr = CAN_BAUD_250K;
        break; 
    case 125:
        btr = CAN_BAUD_125K;
        break; 
    case 50:
        btr = CAN_BAUD_50K;
        break; 
    default:
        btr = 0;
        break;
    }
    
    if (btr == 0)
    {
        return 0;
    }
    
    CAN_Init(btr);
    
    return 1;
}


/**************************************************************************
DOES:    This function implements the initialization of a CAN ID hardware
         filter as supported by CAN controllers.
**************************************************************************/
UNSIGNED8 MCOHW_SetCANFilter (
  UNSIGNED16 CANID     // CAN-ID to be received by filter 
  )
{
  if (gNrOfFilters >= MAX_CAN_FILTERS)
  {
    return FALSE;
  }
  gCANFilterList[gNrOfFilters] = CANID;
  gNrOfFilters++;
  return TRUE;
}


/**************************************************************************
DOES:    Call-back function for reset application.
         Waits until watchdog causes a reset.
RETURNS: nothing
**************************************************************************/
void CCOHW_Reset_Application (
  void
  )
{
  __DI();

  while(1)
  { // Wait for Watchdog to hit
  }
}


/**************************************************************************
DOES:    Call-Back function from CAN receive interrupt.
         As this function is called from a high-priority level, execution
         time must be kept to a minimum
RETURNS: 1, if this CAN message was already handled and does not need
         to be copied into the regular receive queue
         0, if this CAN message was NOT yet handled and will be processed
         by a call to CAN_Read() later
**************************************************************************/
int CANRxISRCallBack(CANI_qentrytyp* pCANRx)
{
UNSIGNED8 b;
UNSIGNED16 id;

  // Check if the received message is in FilterList
  id = (UNSIGNED16) pCANRx->ID;
  if (pCANRx->FORMAT & 0x01)
  { // This is a remote frame
    // Ignore all remote frames
    return 1;
  }
  for (b = 0 ; b < gNrOfFilters ; b++)
  {
    if (id == gCANFilterList[b])
    { // ID match
      return 0; // Message needs to be processed!
    }
  }
  return 1; // Ignore this message, as it is not in FilterList
}

⌨️ 快捷键说明

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