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

📄 mc_signal.c

📁 simulink real-time workshop for dragon12 development board from
💻 C
📖 第 1 页 / 共 3 页
字号:
/******************************************************************************/
/*                                                                            */
/* Auxilliary functions, signalling of error codes                            */
/*                                                                            */
/* Author: F. Wornle (FW-mn-yr)                                               */
/* Latest change: fw-02-05                                                    */
/*                                                                            */
/******************************************************************************/

/* microcontroller specific headers */
#include <mc9s12dp256.h>		  /* port definitions etc. */
#include <stddef.h>               /* NULL */

/* System macros (EXT_MODE, etc.) -- generated by 'gen_cpp_req_defines_h.tlc' */
#include "cpp_req_defines.h"
#include "tmwtypes.h"
#include "pll.h"                  /* _BUSCLOCK */
#include "freePortComms.h"        /* MAX_FREECOM_CHANNELS, etc. */
#include "mc_signal.h"            /* RDRF_mask, BAUD_xxx, etc. */
#include "buffer.h"               /* buffer macros - freePort comms */

/* radio communication modules */
#include "radioComms.h"			  /* global RF communication admin variables */
#include "radioClient.h"		  /* RF client */
#include "radioServer.h"		  /* RF server */


#ifdef LCDUSE4ERRORS
#include "lcd.h"
#endif


/* SCI interface (used by FreePortComs, ExtMode, etc. */
/* SCI interface (used by FreePortComs, ExtMode, etc. */
/* SCI interface (used by FreePortComs, ExtMode, etc. */


/* FreePort communication ring buffer (reception) -- this buffer can be static */
static struct BufferTyp	*FreePortComBufPtrSCI0 = NULL;
static struct BufferTyp	*FreePortComBufPtrSCI1 = NULL;
static int16_T          currentFPchannelSCI0 = -1; 
static int16_T          currentFPchannelSCI1 = -1; 


#if (HAS_RFCOMMS > 0)
static int16_T          currentRFchannel = -1; 
#endif





/* ==============================================================================
 * Function: SCI[0/1]_RX_ isr (FreePortCom) =====================================
 ============================================================================== */

#pragma CODE_SEG __NEAR_SEG NON_BANKED /* Interrupt section for this module. Placement will be in NON_BANKED area. */


/* setup SCI0_RX_isr for FreePort communications ('2') */
#if SCI0_COMMS == 2

/* FreePort communication ISR */
__interrupt void SCI0_RX_isr(void) {

char_T inVal;

  /* determine cause of interrupt */
  if((SCI0SR1 & RDRF_mask) != 0) {
    
    /* Receive Data Register Full -> fetch character and store */
  	inVal = SCI0DRL;    						/* get value from register SCI0DRL */
	  InBuffer(inVal, FreePortComBufPtrSCI0);		/* write character to the FreePort (ring) buffer */

  }  
  
} /* SCI0_RX_isr */

#endif /* SCI0_COMMS == 2 */


/* setup SCI0_RX_isr for FreePort communications ('2') */
#if SCI1_COMMS == 2

/* FreePort communication ISR */
__interrupt void SCI1_RX_isr(void) {

char_T inVal;

  /* determine cause of interrupt */
  if((SCI1SR1 & RDRF_mask) != 0) {
    
    /* Receive Data Register Full -> fetch character and store */
  	inVal = SCI1DRL;    						/* get value from register SCI1DRL */
	  InBuffer(inVal, FreePortComBufPtrSCI1);		/* write character to the FreePort (ring) buffer */

  }  

} /* SCI1_RX_isr */

#endif /* SCI1_COMMS == 2 */


#pragma CODE_SEG DEFAULT 

/* ============================================================================ */



/*
 * Initialize SCI0 ========================================================
 */
void SCI0_Init(uint_T baudrate) {

  /* check if bus frequency has been boosted to 24 MHz (fw-07-04) */
  #if _BUSCLOCK == 24

  /* 24 MHz bus frequency (PLL is used, SYNR = 2, REFDV = 0 -> factor 6)
     Baud rate generator: SCI1BDL/H = (24e6/16)/baudrate = 1.5e6/baudrate */
	switch(baudrate) {

	case BAUD_300:
	    SCI0BDH=19;
  	  SCI0BDL=136;
	  break;
	case BAUD_600:
	    SCI0BDH=9;
  	  SCI0BDL=196;
	  break;
	case BAUD_1200:
	    SCI0BDH=4;
  	  SCI0BDL=226;
	  break;
	case BAUD_2400:
	    SCI0BDH=2;
  	  SCI0BDL=113;
	  break;
	case BAUD_4800:
      SCI0BDH=1;
      SCI0BDL=56;
	  break;
	case BAUD_9600:
      SCI0BDH=0;
      SCI0BDL=156;
	  break;
	case BAUD_19200:
      SCI0BDH=0;
      SCI0BDL=78;
	  break;
	case BAUD_38400:
      SCI0BDH=0;
      SCI0BDL=39;
	  break;
	case BAUD_57600:
      SCI0BDH=0;
      SCI0BDL=26;
	  break;
	case BAUD_115200:
      SCI0BDH=0;
      SCI0BDL=13;
	  break;
	}
  
  #else /* _BUSCLOCK */
  
  /* 4 MHz bus frequency (PLL not used, SYNR = REFDV = 0 -> factor 2)
     Baud rate generator: SCI1BDL/H = (4e6/16)/baudrate = 250000/baudrate */
	switch(baudrate) {

	case BAUD_300:
      SCI0BDH=3;
      SCI0BDL=64;
	  break;
	case BAUD_600:
      SCI0BDH=1;
      SCI0BDL=160;
	  break;
	case BAUD_1200:
      SCI0BDH=0;
      SCI0BDL=208;
	  break;
	case BAUD_2400:
      SCI0BDH=0;
      SCI0BDL=104;
	  break;
	case BAUD_4800:
      SCI0BDH=0;
      SCI0BDL=52;
	  break;
	case BAUD_9600:
      SCI0BDH=0;
      SCI0BDL=26;
	  break;
	case BAUD_19200:
      SCI0BDH=0;
      SCI0BDL=13;
	  break;
	}

  #endif /* _BUSCLOCK */

  SCI0CR1 = 0;
/* bit value meaning
    7   0    LOOPS, no looping, normal
    6   0    WOMS, normal high/low outputs
    5   0    RSRC, not appliable with LOOPS=0
    4   0    M, 1 start, 8 data, 1 stop
    3   0    WAKE, wake by idle (not applicable)
    2   0    ILT, short idle time (not applicable)
    1   0    PE, no parity
    0   0    PT, parity type (not applicable with PE=0) */ 

  SCI0CR2 = 0x2C; 
/* bit value meaning
    7   0    TIE, no transmit interrupts on TDRE
    6   0    TCIE, no transmit interrupts on TC
    5   1    RIE, receive interrupts on RDRF
    4   0    ILIE, no interrupts on idle
    3   1    TE, enable transmitter
    2   1    RE, enable receiver
    1   0    RWU, no receiver wakeup
    0   0    SBK, no send break */ 

	//asm cli				/* enable all interrupts */

} /* SCI0_Init */

  
/*
 * Initialize SCI1 ========================================================
 */
void SCI1_Init(uint_T baudrate) {

  /* check if bus frequency has been boosted to 24 MHz (fw-07-04) */
  #if _BUSCLOCK == 24

  /* 24 MHz bus frequency (PLL is used, SYNR = 2, REFDV = 0 -> factor 6)
     Baud rate generator: SCI1BDL/H = (24e6/16)/baudrate = 1.5e6/baudrate */
	switch(baudrate) {

	case BAUD_300:
	    SCI1BDH=19;
  	  SCI1BDL=136;
	  break;
	case BAUD_600:
	    SCI1BDH=9;
  	  SCI1BDL=196;
	  break;
	case BAUD_1200:
	    SCI1BDH=4;
  	  SCI1BDL=226;
	  break;
	case BAUD_2400:
	    SCI1BDH=2;
  	  SCI1BDL=113;
	  break;
	case BAUD_4800:
      SCI1BDH=1;
      SCI1BDL=56;
	  break;
	case BAUD_9600:
      SCI1BDH=0;
      SCI1BDL=156;
	  break;
	case BAUD_19200:
      SCI1BDH=0;
      SCI1BDL=78;
	  break;
	case BAUD_38400:
      SCI1BDH=0;
      SCI1BDL=39;
	  break;
	case BAUD_57600:
      SCI1BDH=0;
      SCI1BDL=26;
	  break;
	case BAUD_115200:
      SCI1BDH=0;
      SCI1BDL=13;
	  break;
	}
  
  #else /* _BUSCLOCK */
  
  /* 4 MHz bus frequency (PLL not used, SYNR = REFDV = 0 -> factor 2)
     Baud rate generator: SCI1BDL/H = (4e6/16)/baudrate = 250000/baudrate */
	switch(baudrate) {

	case BAUD_300:
      SCI1BDH=3;
      SCI1BDL=64;
	  break;
	case BAUD_600:
      SCI1BDH=1;
      SCI1BDL=160;
	  break;
	case BAUD_1200:
      SCI1BDH=0;
      SCI1BDL=208;
	  break;
	case BAUD_2400:
      SCI1BDH=0;
      SCI1BDL=104;
	  break;
	case BAUD_4800:
      SCI1BDH=0;
      SCI1BDL=52;
	  break;
	case BAUD_9600:
      SCI1BDH=0;
      SCI1BDL=26;
	  break;
	case BAUD_19200:
      SCI1BDH=0;
      SCI1BDL=13;
	  break;
	}

  #endif /* _BUSCLOCK */

  SCI1CR1 = 0;
/* bit value meaning
    7   0    LOOPS, no looping, normal
    6   0    WOMS, normal high/low outputs
    5   0    RSRC, not appliable with LOOPS=0
    4   0    M, 1 start, 8 data, 1 stop
    3   0    WAKE, wake by idle (not applicable)
    2   0    ILT, short idle time (not applicable)
    1   0    PE, no parity
    0   0    PT, parity type (not applicable with PE=0) */ 

  SCI1CR2 = 0x2C; 
/* bit value meaning
    7   0    TIE, no transmit interrupts on TDRE
    6   0    TCIE, no transmit interrupts on TC
    5   1    RIE, receive interrupts on RDRF
    4   0    ILIE, no interrupts on idle
    3   1    TE, enable transmitter
    2   1    RE, enable receiver
    1   0    RWU, no receiver wakeup
    0   0    SBK, no send break */ 

	//asm cli				/* enable all interrupts */
	
} /* SCI1_Init */


/* Function: FreePort_Init =======================================================
 */
void FreePort_Init(uint16_T port, uint16_T baudrate) {

	/* initialize FreePort reception ring buffer and configure port 
	   -- make sure this hasn't been done before */
  	switch(port) {
  	
	  case 0:
    	if(FreePortComBufPtrSCI0 == NULL) {
	      InitBuffer(FreePortComBufPtrSCI0, SCI0_FREEPORT_CHANNELS*MAX_FREECOM_BUF_SIZE, char_T);
  		  SCI0_Init(baudrate);
    	}
  		break;

	  case 1:
    	if(FreePortComBufPtrSCI1 == NULL) {
	      InitBuffer(FreePortComBufPtrSCI1, SCI1_FREEPORT_CHANNELS*MAX_FREECOM_BUF_SIZE, char_T);
  		  SCI1_Init(baudrate);
    	}
  		break;

	  }
	
} /* end FreePort_Init */


/* Function: process_fpdata_SCI0 ====================================================
 * Abstract:
 *  Attempts to read a telegram from the FreePortComBuf ring buffer. If one is 
 *  found, it is copied to the appropriate buffer.
 *  This function simply returns if there are less than 4 bytes in the reception 
 *  buffer, otherwise it BLOCKS until the entire telegram has been received.
 */
void process_fpdata_SCI0(uint16_T raw_data) {

char_T    myBuf[4];
uint_T    i;

static myUsrBuf  *admin;        /* static, coz they may get used in subsequent calls to this function */
static uint16_T  size;          /* static, coz they may get used in subsequent calls to this function */
static uint8_T   *buf;          /* static, coz they may get used in subsequent calls to this function */


	/* check if we're dealing with formatted data or not (raw_data == 1) */
	if(raw_data) {

  		currentFPchannelSCI0 = 0;   /* using channel '0' for raw data transmissions */
	  	admin            = freecomTelBuf[currentFPchannelSCI0];
	  	size             = (uint16_T)admin->buf_size;
  		buf              = admin->buf;

⌨️ 快捷键说明

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