Ejemplo n.º 1
0
void hal_sys_init(){
    SYSTEMConfigPerformance(SYS_CLK);
    INTSetVectorPriority(INT_VECTOR_UART(UART3),3);
    INTSetVectorPriority(INT_VECTOR_UART(UART5), 3);
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
    INTDisableInterrupts();
}
Ejemplo n.º 2
0
/*******************************************************************************
 * FUNCTION: vUART2FlushRxBuffer
 *
 * PARAMETERS:
 * ~ void
 *
 * RETURN:
 * ~ void
 *
 * DESCRIPTIONS:
 * Flush all the data in the Rx buffer.
 *
 *******************************************************************************/
void vUART2FlushRxBuffer(void)
{
    unsigned char ucReceivedData;

    unsigned int iStatus = INTDisableInterrupts();
    
    prv_xRx.uiDataCount = 0;
    prv_xRx.uiReadPt = 0;
    prv_xRx.uiWritePt = 0;
    
    // Discard all data available in the UART receive buffer.
    while (UARTReceivedDataIsAvailable(UART2)) {
        // Read the received data.
        ucReceivedData = UARTGetDataByte(UART2);
    }

    // Clear the overrun flag.
    if (UARTGetLineStatus(UART2) & UART_OVERRUN_ERROR) {
        U2STAbits.OERR = 0;
    }

    // Clear the semaphore.
    xSemaphoreTake(xBluetoothRxSemaphore, 0);

    INTRestoreInterrupts(iStatus);
}
Ejemplo n.º 3
0
/*******************************************************************************
 * FUNCTION: uiUART2Read
 *
 * PARAMETERS:
 * ~ *pucBuffer	- Buffer to store the received data.
 * ~ uiLength	- Number of bytes to read.
 *
 * RETURN:
 * ~ Number of bytes read.
 *
 * DESCRIPTIONS:
 * Read the data from UART.
 *
 *******************************************************************************/
unsigned int uiUART2Read(unsigned char *pucBuffer, unsigned char uiLength)
{
    unsigned int uiActualLength = 0;

    while (uiLength-- > 0) {
        // Make sure there is data available in the buffer.
        if (uiUART2GetRxDataCount() > 0) {
            // Copy the data to the buffer.
            *pucBuffer++ = prv_xRx.pucBuffer[prv_xRx.uiReadPt];

            // Increase the number of data read.
            uiActualLength++;

            // Increase the read pointer and decrease the data count.
            prv_vIncPointer(&prv_xRx.uiReadPt, prv_xRx.uiBufferSize);

            unsigned int iStatus = INTDisableInterrupts();
            prv_xRx.uiDataCount--;
            INTRestoreInterrupts(iStatus);
        }	
        else {
            break;
        }
    }

    return uiActualLength;
}
Ejemplo n.º 4
0
void Servo::writeMicroseconds(int value)
{
  // calculate and store the values for the given channel
  byte channel = this->servoIndex;
  if( (channel >= 0) && (channel < MAX_SERVOS) )   // ensure channel is valid
  {  
    if( value < SERVO_MIN() )          // ensure pulse width is valid
      value = SERVO_MIN();
    else if( value > SERVO_MAX() )
      value = SERVO_MAX();   
    
  	value = value - TRIM_DURATION;
    value = usToTicks(value);  // convert to ticks after compensating for interrupt overhead 

   

    unsigned int status;
    status = INTDisableInterrupts();
    servos[channel].ticks = value;

    INTRestoreInterrupts(status);
  } 
     
    
}
Ejemplo n.º 5
0
/*******************************************************************************
 * FUNCTION: uiUART2Write
 *
 * PARAMETERS:
 * ~ *pucBuffer	- Buffer for the data to send.
 * ~ uiLength	- Number of bytes to send.
 *
 * RETURN:
 * ~ Number of bytes sent.
 *
 * DESCRIPTIONS:
 * Transmit data via UART.
 *
 *******************************************************************************/
unsigned int uiUART2Write(const unsigned char *pucBuffer, unsigned char uiLength)
{
    unsigned char ucActualLength = 0;

    while (uiLength-- > 0) {
        // Make sure there is empty space in the buffer.
        if (uiUART2GetTxSpace() > 0) {

            // Copy the data to the buffer.
            prv_xTx.pucBuffer[prv_xTx.uiWritePt] = *pucBuffer++;

            // Increase the number of transmitted data.
            ucActualLength++;

            // Increase the write pointer and data count.
            prv_vIncPointer(&prv_xTx.uiWritePt, prv_xTx.uiBufferSize);

            unsigned int iStatus = INTDisableInterrupts();
            prv_xTx.uiDataCount++;
            INTRestoreInterrupts(iStatus);
        }

        else {
            break;
        }
    }

    // Enable the Tx interrupt if there is new data.
    if (ucActualLength > 0) {
        INTEnable(INT_U2TX, INT_ENABLED);
    }

    return ucActualLength;
}
Ejemplo n.º 6
0
/*******************************************************************************
 * FUNCTION: uiUART2GetTxSpace
 *
 * PARAMETERS:
 * ~ void
 *
 * RETURN:
 * ~ Remaining space in TX buffer.
 *
 * DESCRIPTIONS:
 * Read the remaining empty space in the TX buffer.
 *
 *******************************************************************************/
volatile unsigned int uiUART2GetTxSpace(void)
{
    unsigned int iStatus = INTDisableInterrupts();
    unsigned int uiTxSpace = prv_xTx.uiBufferSize - prv_xTx.uiDataCount;
    INTRestoreInterrupts(iStatus);

    return uiTxSpace;
}
Ejemplo n.º 7
0
/*******************************************************************************
 * FUNCTION: uiUART2GetRxDataCount
 *
 * PARAMETERS:
 * ~ void
 *
 * RETURN:
 * ~ Number of bytes available.
 *
 * DESCRIPTIONS:
 * Read the number of bytes available in the Rx buffer.
 *
 *******************************************************************************/
volatile unsigned int uiUART2GetRxDataCount(void)
{
    unsigned int iStatus = INTDisableInterrupts();
    unsigned int uiDataCount = prv_xRx.uiDataCount;
    INTRestoreInterrupts(iStatus);

    return uiDataCount;
}
Ejemplo n.º 8
0
/********************************************************************
* Function:   JumpToApp()
*
* Precondition: 
*
* Input:    None.
*
* Output:   
*
* Side Effects: No return from here.
*
* Overview:   Jumps to application.
*
*     
* Note:     None.
********************************************************************/
void JumpToApp(void)
{ 
  void (*fptr)(void);

  INTDisableInterrupts();

  fptr = (void (*)(void))USER_APP_RESET_ADDRESS;
  fptr();
} 
Ejemplo n.º 9
0
inline BOOL SPIFUBAR(void)
{
    BOOL returnValue;
    unsigned int intEnabled;
    intEnabled=INTDisableInterrupts();
    returnValue=FALSE;
    INTRestoreInterrupts(intEnabled);
    return returnValue;
}
Ejemplo n.º 10
0
void __ISR(_TIMER_1_VECTOR, ipl7) _Timer1Handler(void)
{
	HWORD	dtcMtr;
	
	IFS0CLR = ( 1 << 4 );
	
	INTDisableInterrupts();
	if ( ! fMtrLeft ) {
		// Left motor feedback has timed out.
		
		INTEnableInterrupts();
		
		dtcMtr = OC1R + 20;
		if ( dtcMtrMax >= dtcMtr ) {
			OC1RS=  dtcMtr;
		}
		else {
			OC1RS = dtcMtrMax;
		}
	}
	else {
		fMtrLeft = fFalse;
		INTEnableInterrupts();
	}
	
	INTDisableInterrupts();
	if ( ! fMtrRight ) {
		// Right motor feedback has timed out.
		
		INTEnableInterrupts();
		
		dtcMtr = OC4R + 20;
		if ( dtcMtrMax >= dtcMtr ) {
			OC4RS = dtcMtr;
		}
		else {
			OC4RS = dtcMtrMax;
		}
	}
	else {
		fMtrRight = fFalse;
		INTEnableInterrupts();
	}
}
Ejemplo n.º 11
0
BOOL SPIDataReady(void)
{
    BOOL returnValue;
    unsigned int intTemp;
    intTemp = INTDisableInterrupts();
    returnValue=SPI.status.RXDataReady;
    SPI.status.RXDataReady=FALSE;
    INTRestoreInterrupts(intTemp);
    return returnValue;
}
Ejemplo n.º 12
0
/*********************************************************************
 * Function:  		DWORD get_fattime(void)
 *
 * PreCondition:    
 *
 * Input:           None
 *
 * Output:          Time
 *
 * Side Effects:    
 *
 * Overview:        when writing fatfs requires a time stamp
 *					in this exmaple we are going to use a counter
 *					If the starter kit has the 32kHz crystal
 *					installed then the RTCC could be used instead
 *
 * Note:           
 ********************************************************************/
DWORD get_fattime(void)
{
	DWORD tmr;

	INTDisableInterrupts();
	tmr = GetCurrentDateTimeAsDWORD();
	INTEnableInterrupts();

	return tmr;
}
Ejemplo n.º 13
0
void setup_spi() {
    // TODO move it out of here
    INTDisableInterrupts();
    mCNOpen(CN_ON | CN_IDLE_CON, CN1_ENABLE, CN1_PULLUP_ENABLE);
    mPORTCRead();
    mCNSetIntPriority(6); // same as below
    mCNClearIntFlag();
    mCNIntEnable(1);
    INTEnableInterrupts();
}
Ejemplo n.º 14
0
void MyCyclone_Write(unsigned int theAddress, unsigned int theData)
{
    unsigned int intStatus;

    intStatus = INTDisableInterrupts();
    mPORTEClearBits(CS_FPGA);
    MySPI_PutC(theAddress | 0x8000); // Bit 7 = R/W = 1
    MySPI_PutC(theData);
    mPORTESetBits(CS_FPGA);
    INTRestoreInterrupts(intStatus);
}
Ejemplo n.º 15
0
void DelayMs(WORD delay) {
    unsigned int int_status;
    while (delay--) {
        int_status = INTDisableInterrupts();
        OpenCoreTimer(500000 / 2000);
        INTRestoreInterrupts(int_status);
        mCTClearIntFlag();
        while (!mCTGetIntFlag());
    }
    mCTClearIntFlag();
}
Ejemplo n.º 16
0
/********************************************************************
* Function: 	NVMemOperation()
*
* Precondition: 
*
* Input: 		NV operation
*
* Output:		NV eror
*
* Side Effects:	This function must generate MIPS32 code only and 
				hence the attribute (nomips16)
*
* Overview:     Performs reuested operation.
*
*			
* Note:		 	None.
********************************************************************/
UINT __attribute__((nomips16)) NVMemOperation(UINT nvmop)
{
    int	int_status;
    int	susp;

    // Disable DMA & Disable Interrupts
	#ifdef _DMAC
	int_status = INTDisableInterrupts();
	susp = DmaSuspend();
	#else
	int_status = INTDisableInterrupts(); 
	#endif	// _DMAC

    // Enable Flash Write/Erase Operations
    NVMCON = NVMCON_WREN | nvmop;
    // Data sheet prescribes 6us delay for LVD to become stable.
    // To be on the safer side, we shall set 7us delay.
    delay_us(7);

    NVMKEY 		= 0xAA996655;
    NVMKEY 		= 0x556699AA;
    NVMCONSET 	= NVMCON_WR;

    // Wait for WR bit to clear
    while(NVMCON & NVMCON_WR);
    
    // Disable Flash Write/Erase operations
    NVMCONCLR = NVMCON_WREN;  


	// Enable DMA & Enable Interrupts
	#ifdef _DMAC
	DmaResume(susp);
	INTRestoreInterrupts(int_status);
	#else
	INTRestoreInterrupts(int_status);
	#endif // _DMAC

	// Return Error Status
    return(NVMemIsError());
}
Ejemplo n.º 17
0
BOOL SPIGet(uint8_t *tempSPIRX)
{
    BOOL returnValue;
    unsigned int index;
    unsigned int intTemp;
    intTemp = INTDisableInterrupts();
    for(index=0;index<sizeof(SPI.RXData);index++)
    {
        tempSPIRX[index]=SPI.RXData[index];
    }
    INTRestoreInterrupts(intTemp);
    return returnValue;
}
Ejemplo n.º 18
0
void Setup_initializeBoard(void)
{
    INTDisableInterrupts();
    Setup_ports();
    setup_ADC();

    INTEnableSystemMultiVectoredInt();
    
    Setup_timers();
    RcRx_setupInputCaptures();
    MotorCtrl_setupOutputCompares();
    Setup_configInterrupts();
}
Ejemplo n.º 19
0
unsigned int MyCyclone_Read(unsigned int theAddress)
{
    unsigned int theData;
    unsigned int intStatus;

    intStatus = INTDisableInterrupts();
    mPORTEClearBits(CS_FPGA);
    MySPI_PutC(theAddress & 0x7FFF);   // Bit 7 = R/W = 0
    theData = MySPI_GetC();
    mPORTESetBits(CS_FPGA);
    INTRestoreInterrupts(intStatus);
    return(theData);
}
Ejemplo n.º 20
0
//===============================================================
// Name     : StateClose
// Purpose  : Close all peripherals and put device in sleep mode
//===============================================================
void StateClose(void)
{

  INTDisableInterrupts();   // Disable all interrupts of the system.

//  Wdt.Disable();
  
  LED_ALL_OFF();

  I2c.Close(I2C4);

  // DRIVE B
  //==========================================================
  if (USE_DRIVE_B == 1)
  {
    Pwm.Close(PWM_2);
    Pwm.Close(PWM_3);

    InputCapture.Close(IC2);
    InputCapture.Close(IC4);
  }
  //==========================================================

  // DRIVE A
  //==========================================================
  if (USE_DRIVE_A == 1)
  {
    Pwm.Close(PWM_4);
    Pwm.Close(PWM_5);

    InputCapture.Close(IC1);
    InputCapture.Close(IC3);
  }
  //==========================================================

  Spi.Close(SPI4);

//  Can.Close(CAN1);

  Uart.Close(UART6);

  Timer.Close(TIMER_1);
  Timer.Close(TIMER_2);
  Timer.Close(TIMER_3);
  Timer.Close(TIMER_5);

//  OSCCONSET = 0x10;         // Sleep mode

}
void DelayMs(WORD time)
{
    while(time--)
    {
        unsigned int    int_status;

        int_status = INTDisableInterrupts();
        OpenCoreTimer(GetSystemClock() / 2000); // core timer is at 1/2 system clock
        INTRestoreInterrupts(int_status);

        mCTClearIntFlag();

        while(!mCTGetIntFlag());
    }

    mCTClearIntFlag();
}
Ejemplo n.º 22
0
void setupTimer3(int freq) {

    INTDisableInterrupts(); //disable interrupts while settting up

    T3CON = 0b000 << 4; // set prescaler to 1:1
    T3CONCLR = 0x2; // set input to PBCLK (the default; all defaults fine)
    PR3 = (SYS_FREQ / freq) - 1; // set the period in period register 3
    TMR3 = 0; // reset the Timer3 count
    T3CONSET = 1 << 15; // turn Timer3 on


    IPC3bits.T3IP = 5; // set timer interrupt priority
    IPC3bits.T3IS = 0; // set subpriority
    IFS0bits.T3IF = 0; // clear interrupt flag


    INTEnableSystemMultiVectoredInt(); // enable interrupts at CPU
    INTEnableInterrupts();
}
Ejemplo n.º 23
0
int main(void) {
  CFGCONbits.JTAGEN = 0; // turn off JTAG, get back those pins for IO use

  SYSTEMConfigPerformance(SYS_FREQ);
  INTDisableInterrupts();
  
  PIC32MX250_setup_pins();
  
  //Setup SPI1
  SPI1CON = 0;              // turn off the spi module and reset it
  SPI1BUF;                  // clear the rx buffer by reading from it
  SPI1BRG = 0x3;            // baud rate to 5MHz [SPI1BRG = (40000000/(2*desired))-1]
  SPI1STATbits.SPIROV = 0;  // clear the overflow bit
  SPI1CONbits.CKE = 1;      // data changes when clock goes from active to inactive
                            //    (high to low since CKP is 0)
  SPI1CONbits.MSTEN = 1;    // master operation
  SPI1CONbits.ON = 1;       // turn on spi
  
  TRISBbits.TRISB7=0;   //set pins as outputs--0=output
  TRISAbits.TRISA4=0;
  TRISAbits.TRISA3=0;
  TRISAbits.TRISA2=1; //make RA2 an input to trigger ADC collecting

  //Initialize pin values
  LATAbits.LATA3=1; //make this pin be 3.3v
  LATAbits.LATA4=1;
  LATBbits.LATB7=0;  //make this pin go low
  
  
  unsigned int sample=0;
  while(1){
      if (PORTAbits.RA2){
          //if pin 9 goes high, read from the adc
          sample=adc_grab(1);
      }
  }
  return 0;
}
Ejemplo n.º 24
0
//===============================================================
// Name     : StateInit
// Purpose  : Initialization of the system.
//===============================================================
void StateInit(void)
{
  INTDisableInterrupts();   // Disable all interrupts of the system.

  INIT_PORTS;
//  INIT_WDT;
  INIT_TIMER;
#ifdef USE_POTENTIOMETER
  INIT_ADC;
#else
  INIT_INPUT_CAPTURE;
#endif
  INIT_UART;
  INIT_SPI;
  INIT_PWM;
  INIT_I2C;
  INIT_CAN;
  INIT_SKADI;
  START_INTERRUPTS;

  // Send ID to backplane by CAN protocol
  SEND_ID_TO_BACKPLANE;

  Timer.DelayMs(10);
  
  // Send the mode of operation to the steering wheel
  SEND_MODE_TO_STEERING_WHEEL;

  // Get last known position of the mast
  ReadMastPosFromEeprom();
  if (AbsFloat(mastAngle.currentValue) > 360)  // Error
  {
    mastAngle.previousValue = 0;
    mastAngle.currentValue  = 0;
  }
#ifdef USE_POTENTIOMETER
  if (potValues.lastAverage > ADC_TOTAL_BITS)
  {
    potValues.lastAverage = ADC_TOTAL_BITS << 1;
  }
  if (potValues.zeroInBits > ADC_TOTAL_BITS)
  {
    potValues.zeroInBits = ADC_TOTAL_BITS << 1;
  }
  if (potValues.potStepValue > POT_TO_MOTOR_RATIO)
  {
    potValues.potStepValue = POT_TO_MOTOR_RATIO << 1;
  }
  if (potValues.lastBits > (ADC_BITS_PER_REVOLUTION - 1))
  {
    potValues.lastBits = 0;
  }
  PotAddFirstSample(&potValues);
//  potValues.potSamples.lineBuffer.buffer[potValues.potSamples.maxBufSize] = potValues.lastAverage;
//  potValues.potStepSamples.lineBuffer.buffer[potValues.potStepSamples.maxBufSize] = potValues.potStepValue;
#endif

  // Init registers for the drive
  InitDriver();
  
#ifdef USE_POTENTIOMETER
  potValues.angle = &mastAngle;
  potValues.speed = &mastSpeed;
#endif
}
Ejemplo n.º 25
0
/***	UpdateMotorControl
**
**	Synopsis:
**		PutChUart1(ch)
**
**	Parameters:
**		posX - x position of the right joystick
**		posY - y position of the right joystick
**
**	Return Values:
**		none
**
**	Errors:
**		none
**
**	Description:
**		This routine will update the motor controller based on the
**		current X and Y position of the right joystick of the controller.
*/
void UpdateMotorControl( HWORD posX, HWORD posY )
{
	static	HWORD	tusMtrLeftDcSav		= dtcMtrStopped;	// stopped
	static	HWORD	tusMtrRightDcSav	= dtcMtrStopped;	// stopped
	static	BYTE	dirMtrLeftSav		= dirMtrLeftFwd;
	static	BYTE	dirMtrRightSav		= dirMtrRightFwd;
			HWORD	tusMtrLeftFbNew;
			HWORD	tusMtrRightFbNew;
	
	// Determine motor direction and speed based on joystick position.
	if ( posYMax < posY ) {
		if ( posXMax < posX ) {
			// Go forward and left.
			MtrCtrlFwdLeft();
		}
		else if ( posXMin > posX ) {
			// Go backward and left.
			MtrCtrlBwdLeft();
		}
		else {
			// Go left.
			MtrCtrlLeft();
		}
	}
	else if ( posYMin > posY ) {
		if ( posXMax < posX ) {
			// Go forward and right.
			MtrCtrlFwdRight();
		}
		else if ( posXMin > posX ) {
			// Go backward and right.
			MtrCtrlBwdRight();
		}
		else {
			// Go right.
			MtrCtrlRight();
		}
	}
	else {
		if ( posXMax < posX ) {
			// Go forward.
			MtrCtrlFwd();
		}
		else if ( posXMin > posX ) {
			// Go backward.
			MtrCtrlBwd();
		}
		else {
			MtrCtrlStop();
			// Stop.
		}
	}
	
	// Update left motor speed if necessary.
	if ( tusMtrLeftDcSav != dtcMtrLeft ) {
		
		// 1. disable left motor feedback.
		INTDisableInterrupts();
		fMtrLeftFb	= fFalse;
		INTEnableInterrupts();
		
		if ( dtcMtrStopped != dtcMtrLeft ) {
			// 3. update left motor feedback
			tusMtrLeftFb = tusMtrLeftFbNew;
			
			// 4. re-enable left motor feedback.
			INTDisableInterrupts();
			fMtrLeft	= fFalse;
			fMtrLeftFb	= fTrue;
			INTEnableInterrupts();
			
			// 5. enable feedback watchdog
			T1CONSET = ( 1 << 15 );
		}
		else {
			
			// 2. update left motor duty cycle.
			OC1RS = dtcMtrLeft;
			
			// Disable feedback watchdog.
			T1CONCLR 	= ( 1 << 15 );
			TMR1		= 0;
			IFS0CLR		= ( 1 << 4 );
		}
		
		tusMtrLeftDcSav = dtcMtrLeft;
	}
	
	// Update right motor speed if necessary.
	if ( tusMtrRightDcSav != dtcMtrRight ) {
		
		// 1. disable right motor feedback.
		INTDisableInterrupts();
		fMtrRightFb	= fFalse;
		INTEnableInterrupts();
		
		if ( dtcMtrStopped != dtcMtrRight ) {
			// 3. update right motor feedback.
			tusMtrRightFb = tusMtrRightFbNew;
			
			// 4. re-enable right motor feedback.
			INTDisableInterrupts();
			fMtrRight	= fFalse;
			fMtrRightFb	= fTrue;
			INTEnableInterrupts();
			
			// 5. enable feedback watchdog
			T1CONSET = ( 1 << 15 );
		}
		else {
			
			// 2. update right motor duty cycle.
			OC4RS = dtcMtrRight;
		}
		
		tusMtrRightDcSav = dtcMtrRight;
	}
	
	// Update left motor direction if necessary.
	if ( dirMtrLeftSav != dirMtrLeft ) {
		
		OC1CONCLR	= ( 1 << 15 );	// Disable output compare module 1
									// to release I/O pin control of enable
									// signal.
									
		trisMtrLeftEnClr	= ( 1 << bnMtrLeftEn );	// Set enable pin as a digital output.
		prtMtrLeftEnClr		= ( 1 << bnMtrLeftEn ); // Drive enable pin low.
									
		if ( dirMtrLeftFwd	== dirMtrLeft ) {
			prtMtrLeftDirSet = ( 1 << bnMtrLeftDir );
		}
		else {
			prtMtrLeftDirClr = ( 1 << bnMtrLeftDir );
		}
									
		OC1CONSET	= ( 1 << 15 );	// Re-enable output compare module 1.
		dirMtrLeftSav = dirMtrLeft;	// Save the current direction of the
									// left motor.
	}
	
	// Update right motor direction if necessary.
	if ( dirMtrRightSav != dirMtrRight ) {
		OC4CONCLR	= ( 1 << 15 );	// Disable output compare module 4
									// to release I/O pin control of enable
									// signal.
									
		trisMtrRightEnClr	= ( 1 << bnMtrRightEn ); // Set enable pin as a digital output.
		prtMtrRightEnClr	= ( 1 << bnMtrRightEn ); // Drive enable pin low.
		
		if ( dirMtrRightFwd == dirMtrRight ) {
			prtMtrRightDirClr = ( 1 << bnMtrRightDir );
		}
		else {
			prtMtrRightDirSet = ( 1 << bnMtrRightDir );
		}
									
		OC4CONSET	= ( 1 << 15 );	// Re-enable output compare module 4.
		
		dirMtrRightSav = dirMtrRight;	// Save the current direciton of the
										// right motor.
	}
}
Ejemplo n.º 26
0
void  BSP_IntDisAll (void)
{
    INTDisableInterrupts();
}
Ejemplo n.º 27
0
void ClearExternalIO(void) {
	int i;

//	GS I2C Start
	i2c_disable();
	i2c_slave_disable();
//	GS I2C End

	if (SerialConsole != 1) SerialClose(1);
	if (SerialConsole != 2) SerialClose(2);
	if (SerialConsole != 3) SerialClose(3);
	if (SerialConsole != 4) SerialClose(4);

	// stop the sound
	SoundPlay = 0;
	CloseTimer2();
#ifdef OLIMEX
	CloseOC1();
#else
	CloseOC2();
#endif
	inttbl[NBRPINS + 2].intp = NULL;					// disable the tick interrupt
	for (i = 1; i < NBRPINS + 1; i++) {
		inttbl[i].intp = NULL;						// disable all interrupts
#ifdef OLIMEX
    #ifdef  OLIMEX_DUINOMITE_EMEGA
		if (ExtCurrentConfig[i] != EXT_CONSOLE_RESERVED && i != 35 ) {	// don't reset the serial console
    #else
		if (ExtCurrentConfig[i] != EXT_CONSOLE_RESERVED && i != 21 ) {	// don't reset the serial console
    #endif
#else
		if (ExtCurrentConfig[i] != EXT_CONSOLE_RESERVED) {              // don't reset the serial console
#endif
			ExtCfg(i, EXT_NOT_CONFIG);                              // all set to unconfigured
			ExtSet(i, 0);						// all outputs (when set) default to low
		}
	}
        InterruptReturn = NULL;
}


/****************************************************************************************************************************
Initialise the I/O pins
*****************************************************************************************************************************/
void initExtIO(void) {
	int i;

	for (i = 1; i < NBRPINS + 1; i++) {
		ExtCfg(i, EXT_NOT_CONFIG);                                      // all set to unconfigured
		ExtSet(i, 0);							// all outputs (when set) default to low
	}
#ifndef OLIMEX
 	CNCONbits.ON = 1;       						// turn on Change Notification module
 	CNPUEbits.CNPUE1 = 1;							// turn on the pullup for pin C13 also called CN1
#endif
	ExtCurrentConfig[0] = EXT_DIG_IN;                                       // and show that we can read from it
	P_LED_TRIS = P_OUTPUT; 							// make the LED pin an output
	ExtSet(0, 0);								// and turn it off
#ifdef OLIMEX
    #ifdef OLIMEX_DUINOMITE_EMEGA
	ExtCurrentConfig[35] = EXT_ANA_IN;
    #else
	ExtCurrentConfig[21] = EXT_ANA_IN;
    #endif
#endif

	// setup the analog (ADC) function
	AD1CON1 = 0x00E0;       						// automatic conversion after sampling
 	AD1CSSL = 0;       							// no scanning required
	AD1CON2 = 0;       							// use MUXA, use AVdd   &   AVss as Vref+/-
	AD1CON3 = 0x203; //0x1F3F;  							// Tsamp = 32 x Tad;
	AD1CON1bits.ADON = 1; 							// turn on the ADC

}


/****************************************************************************************************************************
Configure an I/O pin
Returns true if all is OK
*****************************************************************************************************************************/
int ExtCfg(int pin, int cfg) {
	int tris, ana, oc;

	if (pin < 0 || pin > NBRPINS) return false;						// initial sanity check

	// make sure that interrupts are disabled in case we are changing from an interrupt input
#ifdef OLIMEX
//	if (pin == 5) ConfigINT2(EXT_INT_PRI_2 | RISING_EDGE_INT | EXT_INT_DISABLE);
//	if (pin == 6) ConfigINT3(EXT_INT_PRI_2 | RISING_EDGE_INT | EXT_INT_DISABLE);
        #ifdef  OLIMEX_DUINOMITE_EMEGA
        #else
                if (pin == 7) ConfigINT4(EXT_INT_PRI_2 | RISING_EDGE_INT | EXT_INT_DISABLE);
        #endif
#else
	if (pin == 11) ConfigINT1(EXT_INT_PRI_2 | RISING_EDGE_INT | EXT_INT_DISABLE);
	if (pin == 12) ConfigINT2(EXT_INT_PRI_2 | RISING_EDGE_INT | EXT_INT_DISABLE);
	if (pin == 13) ConfigINT3(EXT_INT_PRI_2 | RISING_EDGE_INT | EXT_INT_DISABLE);
	if (pin == 14) ConfigINT4(EXT_INT_PRI_2 | RISING_EDGE_INT | EXT_INT_DISABLE);
#endif

	inttbl[pin].intp = NULL;		// also disable a software interrupt on this pin

	switch (cfg) {
		case EXT_NOT_CONFIG:
//	#ifdef OLIMEX
//			if (pin == 5) {P_E5_TRIS = 1; P_E5_OC = 1;}
//			if (pin == 6) {P_E6_TRIS = 1; P_E6_OC = 1;}
//			if (pin == 11) {P_E11_TRIS = 1; P_E11_OC = 1;}
//			if (pin == 12) {P_E12_TRIS = 1; P_E12_OC = 1;}
//	#endif
			tris = 1; ana = 1; oc = 1;
			break;

		case EXT_ANA_IN:
//	#ifdef OLIMEX
			if (pin > 6 && pin < 19) return false;
//			if (pin == 5) {P_E5_TRIS = 1; P_E5_OC = 1;}
//			if (pin == 6) {P_E6_TRIS = 1; P_E6_OC = 1;}
//	#else
//			if (pin > 10) return false;
//	#endif
			tris = 1; ana = 0; oc = 1;
			break;

		case EXT_FREQ_IN:											// same as counting, so fall through
		case EXT_PER_IN:											// same as counting, so fall through
		case EXT_CNT_IN:
//	#ifdef OLIMEX
//	#else
//			if (pin == 11) {
//				INT1Count = INT1Value = 0;
//				ConfigINT1(EXT_INT_PRI_2 | RISING_EDGE_INT | EXT_INT_ENABLE);
//				tris = 1; ana = 1; oc = 1;
//				break;
//			}
//	#endif
//	#ifdef OLIMEX
//			if (pin == 5) {
//				P_E5_TRIS = 1;
//				P_E5_OC = 1;
//	#else
//			if (pin == 12) {
//	#endif
//				INT2Count = INT2Value = 0;
//				ConfigINT2(EXT_INT_PRI_2 | RISING_EDGE_INT | EXT_INT_ENABLE);
//				tris = 1; ana = 1; oc = 1;
//				break;
//			}
//	#ifdef OLIMEX
//			if (pin == 6) {
//				P_E6_TRIS = 1;
//				P_E6_OC = 1;
//	#else
//			if (pin == 13) {
//	#endif
//				INT3Count = INT3Value = 0;
//				ConfigINT3(EXT_INT_PRI_2 | RISING_EDGE_INT | EXT_INT_ENABLE);
//				tris = 1; ana = 1; oc = 1;
//				break;
//			}
#ifdef  OLIMEX_DUINOMITE_EMEGA
                    break;
#else
	#ifdef OLIMEX
			if (pin == 7) {
	#else
			if (pin == 14) {
	#endif
				INT4Count = INT4Value = 0;
				ConfigINT4(EXT_INT_PRI_2 | RISING_EDGE_INT | EXT_INT_ENABLE);
				tris = 1; ana = 1; oc = 1;
				break;
			}
			return false;							// not an interrupt enabled pin
#endif

		case EXT_INT_LO:											// same as digital input, so fall through
		case EXT_INT_HI:											// same as digital input, so fall through
		case EXT_DIG_IN:
	#ifdef OLIMEX
//                        if (pin == 5) {P_E5_TRIS = 1; P_E5_OC = 1;}
//			if (pin == 6) {P_E6_TRIS = 1; P_E6_OC = 1;}
//			if (pin == 11) {P_E11_TRIS = 1; P_E11_OC = 1;}
//			if (pin == 12) {P_E12_TRIS = 1; P_E12_OC = 1;}
	#endif
			tris = 1; ana = 1; oc = 1;
			break;

		case EXT_DIG_OUT:
	#ifdef OLIMEX
//			if (pin == 11) {P_E11_TRIS = 1; P_E11_OC = 1;} //return false;
//			if (pin == 5) {P_E5_TRIS = 1; P_E5_OC = 1;}
//			if (pin == 6) {P_E6_TRIS = 1; P_E6_OC = 1;}
//			if (pin == 12) {P_E12_TRIS = 1; P_E12_OC = 1;}
	#endif
			tris = 0; ana = 1; oc = 0;
			break;

		case EXT_OC_OUT:
	#ifdef OLIMEX
//			if (pin == 11) {P_E11_TRIS = 1; P_E11_OC = 1;} //return false;
//			if (pin == 5) {P_E5_TRIS = 1; P_E5_OC = 1;}
//			if (pin == 6) {P_E6_TRIS = 1; P_E6_OC = 1;}
//			if (pin == 12) {P_E12_TRIS = 1; P_E12_OC = 1;}
	#else
			if (pin < 11) return false;
	#endif
			tris = 0; ana = 1; oc = 1;
			break;

		case EXT_COM_RESERVED:
		case EXT_CONSOLE_RESERVED:
			ExtCurrentConfig[pin] = cfg;		// don't do anything except set the config type
		#ifdef OLIMEX
//			if (pin == 5) {P_E5_TRIS = 1; P_E5_OC = 1; P_E5_ANALOG = 1;}
//			if (pin == 6) {P_E6_TRIS = 1; P_E6_OC = 1; P_E6_ANALOG = 1;}
//			if (pin == 11) {P_E11_TRIS = 1; P_E11_OC = 1;}
//			if (pin == 12) {P_E12_TRIS = 1; P_E12_OC = 1;}
		#endif
			return true;

		default:
			return false;
	}

	ExtCurrentConfig[pin] = cfg;

	// set the TRIS and analog characteristics
	switch (pin) {
		case 1:  P_E1_TRIS = tris;   P_E1_OC = oc;   P_E1_ANALOG = ana;		break;
		case 2:  P_E2_TRIS = tris;   P_E2_OC = oc;   P_E2_ANALOG = ana;		break;
		case 3:  P_E3_TRIS = tris;   P_E3_OC = oc;   P_E3_ANALOG = ana;		break;
		case 4:  P_E4_TRIS = tris;   P_E4_OC = oc;   P_E4_ANALOG = ana;		break;
		case 5:  P_E5_TRIS = tris;   P_E5_OC = oc;   P_E5_ANALOG = ana;		break;
		case 6:  P_E6_TRIS = tris;   P_E6_OC = oc;   P_E6_ANALOG = ana;		break;
	#ifdef OLIMEX
            #ifdef  OLIMEX_DUINOMITE_EMEGA
		case 7:  P_E7_TRIS = tris;   P_E7_OC = oc;   P_E7_ANALOG = ana;		break;
		case 8:  P_E8_TRIS = tris;   P_E8_OC = oc;   break;
		case 9:  P_E9_TRIS = tris;   P_E9_OC = oc;   break;
		case 10: P_E10_TRIS = tris;  P_E10_OC = oc;  break;
            #else
		case 7:  P_E7_TRIS = tris;   P_E7_OC = oc;	              break;
 		case 8:  if (!S.SDEnable) { P_E8_TRIS = tris; P_E8_OC = oc;}  break;
		case 9:  if (!S.SDEnable) { P_E9_TRIS = tris; P_E9_OC = oc;}  break;
		case 10: if (!S.SDEnable) { P_E10_TRIS = tris; P_E10_OC = oc;} break;
            #endif
	#else
		case 7:  P_E7_TRIS = tris;   P_E7_OC = oc;   P_E7_ANALOG = ana;		break;
		case 8:  P_E8_TRIS = tris;   P_E8_OC = oc;   P_E8_ANALOG = ana;		break;
		case 9:  P_E9_TRIS = tris;   P_E9_OC = oc;   P_E9_ANALOG = ana;		break;
		case 10: P_E10_TRIS = tris;  P_E10_OC = oc;  P_E10_ANALOG = ana;	break;
	#endif
		case 11: P_E11_TRIS = tris;  P_E11_OC = oc;			break;
		case 12: P_E12_TRIS = tris;  P_E12_OC = oc;			break;
		case 13: P_E13_TRIS = tris;  P_E13_OC = oc;			break;
		case 14: P_E14_TRIS = tris;  P_E14_OC = oc;			break;
		case 15: P_E15_TRIS = tris;  P_E15_OC = oc;			break;
		case 16: P_E16_TRIS = tris;  P_E16_OC = oc;			break;
		case 17: P_E17_TRIS = tris;  P_E17_OC = oc;			break;
		case 18: P_E18_TRIS = tris;  P_E18_OC = oc;			break;
	#ifdef OLIMEX
            // SPP +
            #ifdef	OLIMEX_DUINOMITE_EMEGA		// edit for DuinoMite eMega
		case 19: P_E19_TRIS = tris;  P_E19_OC = oc;			break;
		case 20: P_E20_TRIS = tris;  P_E20_OC = oc;                     break;
                case 21: P_E21_TRIS = tris;  P_E21_OC = oc;			break;
		case 22: P_E22_TRIS = tris;  P_E22_OC = oc;			break;
		case 23: P_E23_TRIS = tris;  P_E23_OC = oc;			break;
		case 24: P_E24_TRIS = tris;  P_E24_OC = oc;			break;
		case 25: P_E25_TRIS = tris;  P_E25_OC = oc;			break;
		case 26: P_E26_TRIS = tris;  P_E26_OC = oc;			break;
		case 27: P_E27_TRIS = tris;  P_E27_OC = oc;			break;
		case 28: P_E28_TRIS = tris;  P_E28_OC = oc;			break;
		case 29: P_E29_TRIS = tris;  P_E29_OC = oc;			break;
		case 30: P_E30_TRIS = tris;  P_E30_OC = oc;			break;
		case 31: P_E31_TRIS = tris;  P_E31_OC = oc; P_E31_ANALOG = ana;	break;
		case 32: P_E32_TRIS = tris;  P_E32_OC = oc; P_E31_ANALOG = ana;	break;
		case 33: P_E33_TRIS = tris;  P_E33_OC = oc;			break;
		case 34: P_E34_TRIS = tris;  P_E34_OC = oc;			break;
		case 35: P_E35_TRIS = tris;  P_E35_OC = oc; P_E31_ANALOG = ana; break;
		case 36: P_E36_TRIS = tris;  P_E36_OC = oc;			break;
		case 37: P_E37_TRIS = tris;  P_E37_OC = oc;			break;
		case 38: P_E38_TRIS = tris;  P_E38_OC = oc;			break;
		case 39: P_E39_TRIS = tris;  P_E39_OC = oc;			break;
            #else	// original by Geoff Graham for DuinoMite Mega
		case 19: //if (!S.VideoMode) {
                    P_E19_TRIS = tris; P_E19_OC = oc; P_E19_ANALOG = ana;//}
                break;
		case 20: if (!S.VideoMode || P_VGA_COMP) {P_E20_TRIS = tris; P_E20_OC = oc; P_E20_ANALOG = ana;} break;
		case 21: P_E21_TRIS = tris;  P_E21_OC = oc;  P_E21_ANALOG = ana;		break;
                case 22: P_E22_TRIS = tris;  P_E22_OC = oc; break;
                case 23: P_E23_TRIS = tris;  P_E23_OC = oc; break;
            #endif
            // SPP -
	#else
		case 19: P_E19_TRIS = tris;  P_E19_OC = oc;			break;
		case 20: P_E20_TRIS = tris;  P_E20_OC = oc;			break;
	#endif
	#ifdef UBW32
                case 21: P_E21_TRIS = tris;  P_E21_OC = oc;			break;
		case 22: P_E22_TRIS = tris;  P_E22_OC = oc;			break;
		case 23: P_E23_TRIS = tris;  P_E23_OC = oc;			break;
		case 24: P_E24_TRIS = tris;  P_E24_OC = oc;			break;
		case 25: P_E25_TRIS = tris;  P_E25_OC = oc;			break;
		case 26: P_E26_TRIS = tris;  P_E26_OC = oc;			break;
		case 27: P_E27_TRIS = tris;  P_E27_OC = oc;			break;
		case 28: P_E28_TRIS = tris;  P_E28_OC = oc;			break;
		case 29: P_E29_TRIS = tris;  P_E29_OC = oc;			break;
		case 30: P_E30_TRIS = tris;  P_E30_OC = oc;			break;
		case 31: P_E31_TRIS = tris;  P_E31_OC = oc;			break;
		case 32: P_E32_TRIS = tris;  P_E32_OC = oc;			break;
		case 33: P_E33_TRIS = tris;  P_E33_OC = oc;			break;
		case 34: P_E34_TRIS = tris;  P_E34_OC = oc;			break;
		case 35: P_E35_TRIS = tris;  P_E35_OC = oc;			break;
		case 36: P_E36_TRIS = tris;  P_E36_OC = oc;			break;
		case 37: P_E37_TRIS = tris;  P_E37_OC = oc;			break;
		case 38: P_E38_TRIS = tris;  P_E38_OC = oc;			break;
		case 39: P_E39_TRIS = tris;  P_E39_OC = oc;			break;
		case 40: P_E40_TRIS = tris;  P_E40_OC = oc;			break;
		case 41: P_E41_TRIS = tris;  P_E41_OC = oc;			break;
		case 42: P_E42_TRIS = tris;  P_E42_OC = oc;			break;
		case 43: P_E43_TRIS = tris;  P_E43_OC = oc;			break;
		case 44: P_E44_TRIS = tris;  P_E44_OC = oc;			break;
		case 45: P_E45_TRIS = tris;  P_E45_OC = oc;			break;
		case 46: P_E46_TRIS = tris;  P_E46_OC = oc;			break;
		case 47: P_E47_TRIS = tris;  P_E47_OC = oc;			break;
		case 48: P_E48_TRIS = tris;  P_E48_OC = oc;			break;
		case 49: P_E49_TRIS = tris;  P_E49_OC = oc;			break;
		case 50: P_E50_TRIS = tris;  P_E50_OC = oc;			break;
	#endif
	}

	if (cfg == EXT_NOT_CONFIG) ExtSet(pin, 0);						// set the default output to low
	return true;
}


/****************************************************************************************************************************
Set the output of a digital I/O pin
Returns true if all is OK
*****************************************************************************************************************************/
int ExtSet(int pin, int val){
	val = (val != 0);							// non zero is on
	INTDisableInterrupts();				// setting an output bit is NOT atomic and a bit set operation
																// in an interrupt could result in this set corrupting the output
	switch (pin) {
	#ifdef UBW32
		case 0:  P_LED_OUT = !val;  break;		// this is the LED - the UBW32 wired them upside down !!
	#else
		case 0:  P_LED_OUT = val;  break;			// this is the LED
	#endif
		case 1:  P_E1_OUT = val;   break;
		case 2:  P_E2_OUT = val;   break;
		case 3:  P_E3_OUT = val;   break;
		case 4:	 P_E4_OUT = val;   break;
		case 5:	 P_E5_OUT = val;   break;
		case 6:	 P_E6_OUT = val;   break;
		case 7:	 P_E7_OUT = val;   break;
	#ifdef OLIMEX
            #ifdef  OLIMEX_DUINOMITE_EMEGA
		case 8:	 P_E8_OUT = val;   break;
		case 9:	 P_E9_OUT = val;   break;
		case 10: P_E10_OUT = val;  break;
            #else
		case 8:	 if (!S.SDEnable) P_E8_OUT = val;   break;
		case 9:	 if (!S.SDEnable) P_E9_OUT = val;   break;
		case 10: if (!S.SDEnable) P_E10_OUT = val;  break;
            #endif
	#else
		case 8:	 P_E8_OUT = val;   break;
		case 9:	 P_E9_OUT = val;   break;
		case 10: P_E10_OUT = val;  break;
	#endif
		case 11: P_E11_OUT = val;  break;
		case 12: P_E12_OUT = val;  break;
		case 13: P_E13_OUT = val;  break;
		case 14: P_E14_OUT = val;  break;
		case 15: P_E15_OUT = val;  break;
		case 16: P_E16_OUT = val;  break;
		case 17: P_E17_OUT = val;  break;
		case 18: P_E18_OUT = val;  break;
	#ifdef OLIMEX
            #ifdef  OLIMEX_DUINOMITE_EMEGA
                case 19: P_E19_OUT = val; break;
                case 20: P_E20_OUT = val; break;
            #else
		case 19: P_E19_OUT = val;  break;
		case 20: if (!S.VideoMode || P_VGA_COMP) P_E20_OUT = val;  break;
                case 22: P_E22_OUT = val ; break;
                case 23: P_E23_OUT = val ; break;
            #endif
        #else
		case 19: P_E19_OUT = val;  break;
		case 20: P_E20_OUT = val;  break;
	#endif
        #if defined UBW32 || defined OLIMEX_DUINOMITE_EMEGA
		case 21: P_E21_OUT = val;	break;
		case 22: P_E22_OUT = val;	break;
		case 23: P_E23_OUT = val;	break;
		case 24: P_E24_OUT = val;	break;
		case 25: P_E25_OUT = val;	break;
		case 26: P_E26_OUT = val;	break;
		case 27: P_E27_OUT = val;	break;
		case 28: P_E28_OUT = val;	break;
		case 29: P_E29_OUT = val;	break;
		case 30: P_E30_OUT = val;	break;
		case 31: P_E31_OUT = val;	break;
		case 32: P_E32_OUT = val;	break;
		case 33: P_E33_OUT = val;	break;
		case 34: P_E34_OUT = val;	break;
		case 35: P_E35_OUT = val;	break;
		case 36: P_E36_OUT = val;	break;
		case 37: P_E37_OUT = val;	break;
		case 38: P_E38_OUT = val;	break;
		case 39: P_E39_OUT = val;	break;
        #endif
        #ifdef UBW32
		case 40: P_E40_OUT = val;	break;
		case 41: P_E41_OUT = val;	break;
		case 42: P_E42_OUT = val;	break;
		case 43: P_E43_OUT = val;	break;
		case 44: P_E44_OUT = val;	break;
		case 45: P_E45_OUT = val;	break;
		case 46: P_E46_OUT = val;	break;
		case 47: P_E47_OUT = val;	break;
		case 48: P_E48_OUT = val;	break;
		case 49: P_E49_OUT = val;	break;
		case 50: P_E50_OUT = val;	break;
	#endif
		default:
			INTEnableInterrupts();
			return false;
	}
	INTEnableInterrupts();
	return true;
}