void checkCBS(unsigned int *voltageArray, unsigned char *currentModule, unsigned char numModules)
{
//*
	unsigned char action = BALANCEOFF;
	unsigned char dataReceived[8];	//maximum length that can be recieved
	unsigned char lengthReceived, flagsReceived;
	unsigned long addressReceived = 0;
	unsigned int my_CAN_id = ((unsigned int) *currentModule) << 2;
	unsigned int lowestVoltage = 50000;
	unsigned char lowestModule = *currentModule;
	unsigned char i;
	//determine the lowest module
	for(i=0; i<numModules;++i)
	{
		if(voltageArray[i]<lowestVoltage)
		{
			lowestVoltage = voltageArray[i];
			lowestModule = i;
		}
	}
	//turn off CBS power to the previously low module if it's actually charging
	//check to see if we have received a comfirm message from the slave
	if(*currentModule != MODULE_ID_NULL)
	{
		while(addressReceived != (MASK_CBS | my_CAN_id))
		{
			//keep sending the message until something responds
			while(!ECANSendMessage(MASK_BPS_MASTER | MASK_CBS | my_CAN_id, &action, 1, ECAN_TX_STD_FRAME | ECAN_TX_PRIORITY_0 | ECAN_TX_NO_RTR_FRAME));
			//read message
			while(!ECANReceiveMessage(&addressReceived, &dataReceived, &lengthReceived, &flagsReceived));
		}
	}
	//turn on CBS power to the new low module if low module is low enough
	if(lowestVoltage < (CUTOFF_VOLTAGE_HIGH - 3000))
	{
		my_CAN_id = ((unsigned int)lowestModule) << 2;
		addressReceived = 0;
		action = BALANCEON;
		//check to see if we have received a comfirm message from the slave
		while(addressReceived != (MASK_CBS | my_CAN_id))
		{
			//keep sending the message until something responds
			while(!ECANSendMessage(MASK_BPS_MASTER | MASK_CBS | my_CAN_id, &action, 1, ECAN_TX_STD_FRAME | ECAN_TX_PRIORITY_0 | ECAN_TX_NO_RTR_FRAME));
			//read message
			while(!ECANReceiveMessage(&addressReceived, &dataReceived, &lengthReceived, &flagsReceived));
		}
		*currentModule = lowestModule;
	}
	else
	{
		*currentModule = MODULE_ID_NULL;
	}
	//print out the result
	printf("CBS=%d\n\r",*currentModule);
	while(!ECANSendMessage((MASK_BPS_MASTER|MASK_BPS_READING|MASK_CBS), currentModule, 1, ECAN_TX_STD_FRAME | ECAN_TX_PRIORITY_0 | ECAN_TX_NO_RTR_FRAME));	
	//*/
	return;
}
Exemple #2
0
 /** Résultat :
 * 0 s'il n'y a aucune donnée reçue sur le bus CAN
 * 1 si des données ont été correctement reçues
 * 2 si c est nul ou param est nul
 */
int can_receive(Command *c, BYTE *param)
{
	unsigned long id;
	BYTE data_recu[8] = {0, 0, 0, 0, 0, 0, 0, 0};
	BYTE dataLen;
	ECAN_RX_MSG_FLAGS flags;
	int res = 0;

	if (c != NULL && param != NULL)
	{
		BOOL didReceiveData = ECANReceiveMessage(&id, data_recu, &dataLen, &flags);

		if (didReceiveData)
		{
			// FIXME: peut etre verifier les bits de la variable 'flags'
			*c = data_recu[0];
			*param = data_recu[1];
			res = 1;
		}
	}
	else
	{
		res = 2;
	}

	return res;
}
Exemple #3
0
void high_isr(void)
#else
#error "Invalid compiler selection for implemented ISR routines"
#endif

{
    struct CANRxMsg CANRxNewMessage;

      /* This code stub shows general interrupt handling.  Note that these
      conditional statements are not handled within 3 seperate if blocks.
      Do not use a seperate if block for each interrupt flag to avoid run
      time errors. */

    if(PIR5bits.RXB0IF==1 | PIR5bits.RXB1IF==1)          // new CAN message received in buffer0 or buffer1
        {

            ECANReceiveMessage(&CANRxNewMessage.id, CANRxNewMessage.data_RX, &CANRxNewMessage.dataLen, &CANRxNewMessage.flags);
            PutCANRxFifo(CANRxNewMessage);

            PIR5bits.RXB0IF=0;      // clear interrupt flags
            PIR5bits.RXB1IF=0;
        }

   


}
void high_isr(void) {

  // Check for timer0 rollover indicating a millisecond has passed
  if (INTCONbits.TMR0IF) {
    INTCONbits.TMR0IF = 0;
    TMR0L = 0x84; // Adjusted from TMR0_RELOAD experimentally
    millis++;
  }

  // Check for timer1 rollover
  if (PIR1bits.TMR1IF) {
    PIR1bits.TMR1IF = 0;
    TMR1H = TMR1H_RELOAD;
    TMR1L = TMR1L_RELOAD;
    seconds++;
  }

	// Check for received CAN message
	if (PIR5bits.RXB1IF) {
		PIR5bits.RXB1IF = 0; // Reset the flag

		// Get data from receive buffer
		ECANReceiveMessage(&id, data, &dataLen, &flags);

    switch (id) {

#if FRONT
      case WHEEL_ID + 1:
        // Process radio_sw CAN message
        radio_sw = data[BUTTON_BITS_BYTE] & RADIO_BTN_MASK;
        break;
#endif
    }
  }
}
Exemple #5
0
void main()
{
	unsigned long id;
	BYTE cpt_RB4;
	BYTE cpt_RB5;
    BYTE data_envoi;
    BYTE data_recu[8];
    BYTE dataLen;
    int i;
    
    ECAN_RX_MSG_FLAGS flags;
	init();
	
	cpt_RB4 = 0;
	cpt_RB5 = 0;
	data_envoi = 0;
	
	for (i = 0; i < 8;i++)
		data_recu[i] = 0xFF;
	
	printf(NL NL "début programme" NL);
    ECANInitialize();
    ECANSetBaudRate(2, 4, 8, 8, 8);
    
	while(1)
	{
		if(TRUE == ECANReceiveMessage(&id, data_recu, &dataLen, &flags))
		{
			PIC1_ONLY printf("ECAN: reçu %u octet(s) : %u" NL, dataLen, data_recu[0]);
			led_afficher_int(data_recu[0]);
		}
		
		
		if(bouton_RB4_pressed())
		{
			cpt_RB4 = cpt_RB4 + 1;
			
			data_envoi = (BYTE)cpt_RB4;
			while (!ECANSendMessage(0, &data_envoi, 1, 0));
			PIC1_ONLY printf("compteur de RB4 : %u" NL, cpt_RB4);
			
			led_afficher_int(cpt_RB4);
		}
			
		if(bouton_RB5_pressed())
		{
			cpt_RB5 = cpt_RB5 + 1;
			
			data_envoi = (BYTE)cpt_RB5;
			while (!ECANSendMessage(0, &data_envoi, 1, 0));
			PIC1_ONLY printf("compteur de RB5 : %u" NL, cpt_RB5);
			
			led_afficher_int(cpt_RB5);
		}
	}
}
//check for new CAN messages and if new messages are found then react accordingly
void checkMessages()
{
	char messageReceived = 0;
	unsigned char dataReceived[8];	//maximum length that can be recieved
	unsigned char lengthReceived, flagsReceived;
	unsigned long addressReceived;
	messageReceived = ECANReceiveMessage(&addressReceived, &dataReceived, &lengthReceived, &flagsReceived);
	//while there are still messages in the buffer
	while(messageReceived == 1)
	{
		//if I get here then I have recieved a message
		//Now parse the identifier and decide what I need to do	
		if(addressReceived == SHUTDOWN){
			failure(addressReceived, 0xFF, 0x00, 0x00);
		}		
		//check for any more messages
		messageReceived = ECANReceiveMessage(&addressReceived, &dataReceived, &lengthReceived, &flagsReceived);
	}	
	return;
}
void high_isr(void) {

	// check for timer0 rollover indicating a millisecond has passed
	if (INTCONbits.TMR0IF) {
		INTCONbits.TMR0IF = FALSE;
		WriteTimer0(0x85);		// load timer rgisters (0xFF (max val) - 0x7D (125) = 0x82)
		millis++;
	}
	// check for recieved CAN message
	if(PIR5bits.RXB1IF) {
		PIR5bits.RXB1IF = FALSE; // reset the flag
		// get data from recieve buffer
		ECANReceiveMessage(&id, data, &dataLen, &flags);
		bufferData();	// put data in an array
	}

	return;
}
Exemple #8
0
void main(void)
{
    unsigned long id;
    BYTE data[4];
    BYTE dataLen;
    ECAN_RX_MSG_FLAGS flags;


    ECANInitialize();

    // This function is for illustration purpose only.
    // It shows how an application would perform run-time initialization.
    // Normally, ECANInitialize() would initialize  ECAN module
    // as per ECAN.def options.  You would do run-time initialization
    // only if the ECAN optoins need to be changed at run-time.
    //RunTimeInitialization();

    while( !ECANSendMessage(0x123, data, 0, ECAN_TX_STD_FRAME) );


    do
    {
        while( !ECANReceiveMessage(&id, data, &dataLen, &flags) )
        {
            // Pushed down RB4 will fill-up FIFO/buffers.
            while( PORTB_RB4 == 0 );
        }


        id++;
        while( !ECANSendMessage(id, data, dataLen, flags) );

        // Delay so that when we are transmitting contents of FIFO all at once,
        // CANKing can receive it correctly without overflowing its buffer.
        id = 0xafff;
        while( id-- );
    } while(1);
}
void main (void)
{
	unsigned long int i;
	unsigned int voltage;
	unsigned char temp;
	unsigned int received_CAN_ID;
	signed long  int current;
	//can receiving variables
	char messageReceived = 0;
	char dataReceived[8]={0,0,0,0,0,0,0,0};	//maximum length that can be recieved
	char lengthReceived = 0, flagsReceived = 0;
	unsigned long addressReceived = 0;
	
	//set all of the unused pins to output and have them output 0;
	TRISA = 0x00;
  	TRISB = 0x08;
	TRISC = 0x00;
	TRISD = 0x00;
	TRISE = 0x00;
	LATA = 0x00;
	LATB = 0x00;
	LATC = 0x00;
	LATD = 0x00;
	LATE = 0x00;
	
	//open the serial port
	openSerialPort();
	//setup the can port
	ECANInitialize();																							    // defined ECANPoll.c
  	CIOCONbits.ENDRHI =1;  

	while(1)
	{
		messageReceived = 0;
		//LATCbits.LATC3 = ~LATCbits.LATC3;
		messageReceived = ECANReceiveMessage(&addressReceived, &dataReceived, &lengthReceived, &flagsReceived);

		while(messageReceived == 1)
		{
			messageReceived = 0;
			//if it has the reading bit set

			//printf("CAN_ID = 0x%.3lx\r\n", addressReceived);
			
			if(((MASK_BPS_READING | MASK_BPS_SLAVE) | addressReceived) == (MASK_BPS_READING | MASK_BPS_SLAVE))
			{
				//just behave as if it is a slave and print out the information received
				received_CAN_ID = (addressReceived & MASK_BPS_SLAVE) >> 2;
				//memcpy(&voltage, dataReceived, 2);	//requires string.h
				memcpy_reduced(&voltage, dataReceived);
				temp = dataReceived[2];
				printf("V[%.2d]=%u\n\r", received_CAN_ID, voltage);
				printf("T[%.2d]=%.2d\n\r", received_CAN_ID, temp);
			}
			else if(((MASK_BPS_MASTER | MASK_BPS_READING) == addressReceived)) //if current reading
			{
				if(lengthReceived == 4)
				{
					int i; int d;
					unsigned char *test ;
					signed long * current_u;
				//	signed int *stupid;
					test = (void*) dataReceived;
				//	memcpy_reduced(&current, dataReceived);

					current_u = (void*)dataReceived;
				//for(i = 0; i < 4; i++) printf("[%d]  --  %x  --- %x\n\r",i,test[i], dataReceived[i]);
				printf("BC=%ld\n\r",*current_u);
				}
			}
			else if(((MASK_BPS_MASTER | MASK_BPS_READING | MASK_ENERGY) == addressReceived)) //if energy reading
			{
				if(lengthReceived == 4)
				{
					long *energy = (void*)dataReceived;
					printf("E=%ld\n\r",*energy);
				}
			}
			else if(((MASK_BPS_MASTER | MASK_BPS_READING | MASK_CBS) == addressReceived))	//if shutdown message or cbs reading
			{
				
				if(lengthReceived == 4)
				{
					printf("Shutting Car Down\n\r");
					memcpy_reduced(&voltage, &(dataReceived[1]));
					temp = dataReceived[3];
					printf("Addr = %.2x\n\r", dataReceived[0]);
					printf("Volt = %u\n\r", voltage);
					printf("temp = %.2d\n\r", temp);
				}
				if(lengthReceived == 1)
				{
					unsigned char * module;
					module = (void*) dataReceived;
					printf("CBS=%d\n\r",*module);

				}
			}
			
			//check for any more messages
			//for(i=0; i<1000; ++i);
			messageReceived = ECANReceiveMessage(&addressReceived, &dataReceived, &lengthReceived, &flagsReceived);
		}
void readSlaves(unsigned int *voltageArray, unsigned char *tempArray, unsigned char numModules)
{
	int i = 0, j = 0;
	char messageReceived = 0;
	unsigned char dataReceived[8];	//maximum length that can be recieved
	unsigned char lengthReceived, flagsReceived;
	unsigned long addressReceived = 0;
	unsigned long sendAddress;
	unsigned int receiveTimeoutCounter = 0;
	unsigned int slaveTimeoutCounter = 0;
	unsigned int newVoltage = 0;
	unsigned char newTemp = 0;
	
	for(i=0; i<numModules; ++i)
	{
        unsigned int timeOutCounterJohn = 1000; //how long we wait for a message
		slaveTimeoutCounter = 0;                //how many other messages we get before fail;
		sendAddress = (i << 2) | MASK_BPS_READING;
		//printf("i:%d\r\nadd:%lb\r\n", i, sendAddress);
		while(addressReceived != sendAddress)
		{
			receiveTimeoutCounter = 0;
			//ensure that the 0 transmit buffer is empty
			/*
			while((TXB0CON & 0b10000000) == 0)
			{
				for(j=0; j<100; ++j);
				arrayrelay=~arrayrelay;
				for(j=0; j<100; ++j);
				arrayrelay=~arrayrelay;
			}//*/
			//send the message until it is acknowledged by something
			timeOutCounterJohn = 1000;
			while(!ECANSendMessage((MASK_BPS_MASTER | sendAddress), NULL, 0, ECAN_TX_STD_FRAME | ECAN_TX_PRIORITY_0 | ECAN_TX_NO_RTR_FRAME)  && timeOutCounterJohn) 
			{
				timeOutCounterJohn --;
                for(j=0; j<50; ++j);
				//led1=~led1;
				for(j=0; j<50; ++j);
				//led1=~led1;
			}
			
			//check for a reply from the master acknowleging the message
			while( (!ECANReceiveMessage(&addressReceived, &dataReceived, &lengthReceived, &flagsReceived)) && receiveTimeoutCounter<=RECEIVETIMEOUT  && timeOutCounterJohn) 
			{
                 timeOutCounterJohn--;
            	//	++receiveTimeoutCounter;
				for(j=0; j<100; ++j);
				//led1=~led1;

			//	printf("\tRXcnt: %u\r\n", receiveTimeoutCounter);//need this here for timing
			}
			//printf("RXadd:%lb\r\n", addressReceived);
			
			//*
			++slaveTimeoutCounter;
			if(slaveTimeoutCounter >= SLAVETIMEOUT)
				{printf("Slave Timeout Error\r\n");failure(BPSERR, i, 0x00, 0x00);}//*/
		}
		//check and ensure that three byte has been recieved
		if(lengthReceived == 3)
		{
			//retrieve and store the values for voltage and temperature
			memcpy_reduced(&newVoltage, dataReceived);
			newTemp = dataReceived[2];
			
			//store the new values into their arrays
			voltageArray[i] = newVoltage;
			tempArray[i] = newTemp;
			//print out the values
			GLOBALINTERRUPTS = INTERRUPTDISABLE;
			printf("V[%.2d]=%u\n\r",i, newVoltage);
			printf("T[%.2d]=%.2d\n\r",i, newTemp);
			GLOBALINTERRUPTS = INTERRUPTENABLE;
			//if it is under voltage over voltage or over temperature then shut the car off
			if((newVoltage < CUTOFF_VOLTAGE_LOW) || 
				(newVoltage > CUTOFF_VOLTAGE_HIGH) ||
				(newTemp > CUTOFF_TEMP_HIGH))
			{
				failure(BPSERR, i, newVoltage, newTemp);
			}
		}
		else {printf("Message Corruption Error\r\n");failure(BPSERR, i, 0x00, 0x00);}
	
		//if there was an interrupt, check the current now.
		if(glob_interrupt && switch5 == SWITCHOFF) //switch5 disables current reading
		{
			glob_current=checkcurrent();
			printf("BC=%ld\n\r",glob_current);
			while(!ECANSendMessage((MASK_BPS_MASTER|MASK_BPS_READING), &glob_current, 4, ECAN_TX_STD_FRAME | ECAN_TX_PRIORITY_0 | ECAN_TX_NO_RTR_FRAME));
			glob_interrupt = 0;
		}
	}
	return;
}