Example #1
0
File: main.c Project: spa-pal/fat42
/* -------------------------------------------------------------------------- */
void main(void) {
	u8 i;
	u16 tout;
	
	GPIOH->ODR&=~ALL_LEDs; 						// LEDs - as push-pull outputs, all off
	GPIOH->DDR|= ALL_LEDs;
	GPIOH->CR1|= ALL_LEDs;
	
	init_CAN();
	CAN->DGR|= CAN_DGR_LBKM;					// set CAN in loop back mode
	
	leds= 0;											// utility variables
	id_offset= -1;
	
	while (1) {
		
		CAN->PSR= 0;								// send my message
		memcpy(&CAN->Page.TxMailbox.MDLCR, &MY_MESS[0], MY_MESS_DLC + 5);
		id_offset= ++id_offset & 0x0F;
		CAN->Page.TxMailbox.MIDR2+= (id_offset<<2);
		CAN->Page.TxMailbox.MCSR|= CAN_MCSR_TXRQ;		// transmit request
		tout= 50000;
		while((CAN->TSR & CAN_TSR_TXOK0) == 0  &&  --tout > 0);	// wait for transmition OK
		if(tout) {
			set_Tx_LEDs();
			CAN->TSR|= CAN_TSR_RQCP0;
		}
		else
			if(CAN->Page.TxMailbox.MCSR & CAN_MCSR_TERR)
				CAN->Page.TxMailbox.MCSR|= CAN_MCSR_ABRQ;

		tout= 50000;
		while((CAN->RFR & CAN_RFR_FMP01) == 0 &&  --tout > 0);		// wait for any CAN receive message
		
		if(tout) {
			CAN->PSR= 7;									// page 7 - read messsage
			while (CAN->RFR & CAN_RFR_FMP01) {				// make up all received messages
				memcpy(&mess[0], &CAN->Page.RxFIFO.MFMI, 14); // compare the message content
				for(i=5; i<MY_MESS_DLC+5; ++i)
					if(mess[i+1]!=MY_MESS[i]) { tout= 0; break;	};
				if(tout)
					set_Rx_LEDs();
				CAN->RFR|= CAN_RFR_RFOM;				// release received message
				while(CAN->RFR & CAN_RFR_RFOM);		// wait until the current message is released
			};
		};		
			
		
		delay_loop((u32)( 5000));
		switch_LEDs_off();
		delay_loop((u32)(15000));
	};
}
Example #2
0
void initialisation_CAN(void){

	/*IO settings*/
   	//Inputs (RPINxxbits.registre)
  	_C1RXR = 98;    //RP98 Pin 34
     	
   	//Outputs (RPORxxbits.registre)
  	_RP99R = 0b001110;  //RP99 Pin 33 

	init_CAN(CAN_NORMAL, 8, 2, 3, 7);

}
Example #3
0
void initialisation_CAN(void){

	/*IO settings*/
   	//Inputs (RPINxxbits.registre)
  	_C1RXR = 66;    //RP66 Pin 34

   	//Outputs (RPORxxbits.registre)
  	_RP65R = 0b001110;  //RP65 Pin 33

	init_CAN(CAN_NORMAL,8,4,1,5);

        setup_can_rx();
        setup_can_tx();

        /*Timeout*/
        Init_Timer5(10.0f);
        /*Envoi périodique*/
        Init_Timer4(15.0f);
}