char CanbusClass::message_rx(unsigned char *buffer) { tCAN message; if (mcp2515_check_message()) { // Lese die Nachricht aus dem Puffern des MCP2515 if (mcp2515_get_message(&message)) { // print_can_message(&message); // PRINT("\n"); buffer[0] = message.data[0]; buffer[1] = message.data[1]; buffer[2] = message.data[2]; buffer[3] = message.data[3]; buffer[4] = message.data[4]; buffer[5] = message.data[5]; buffer[6] = message.data[6]; buffer[7] = message.data[7]; // buffer[] = message[]; // buffer[] = message[]; // buffer[] = message[]; // buffer[] = message[]; } else { // PRINT("Kann die Nachricht nicht auslesen\n\n"); } } }
char CanbusClass::message_rx(unsigned char *buffer) { tCAN message; if (mcp2515_check_message()) { // Read the message from the buffer of MCP2515 if (mcp2515_get_message(&message)) { // print_can_message(&message); // PRINT("\n"); buffer[0] = message.data[0]; buffer[1] = message.data[1]; buffer[2] = message.data[2]; buffer[3] = message.data[3]; buffer[4] = message.data[4]; buffer[5] = message.data[5]; buffer[6] = message.data[6]; buffer[7] = message.data[7]; // buffer[] = message[]; // buffer[] = message[]; // buffer[] = message[]; // buffer[] = message[]; } else { // PRINT("Error" Message could not be read!\n\n"); } } }
/** * Removes all message from the receive buffer */ void clear_receive_buffer(void) { tExtendedCAN reply; while (mcp2515_check_message()) mcp2515_get_extmessage(&reply); }
char CanbusClass::message_rx(tCAN *msg) { tCAN message; if (mcp2515_check_message()) { if (mcp2515_get_message(&message)) { *msg = message; return 1; } } return 0; }
/** * Recieve CAN Messages */ void can_receive_handler(void) { tExtendedCAN reply; if (mcp2515_check_message()) { mcp2515_get_extmessage(&reply); //char buf[10]; //sprintf(buf,"id:%i",reply.id[3]); //uart1_puts(buf); // soft-CAN_ID-filter if (reply.id[3] == SENSORBOARD_ID) { parse_data(&reply); } } }
int CanbusClass::read_msg( int &a, int &b, int &c, int &d) { tCAN message; int timeout; while(timeout < 4000) { timeout++; if (mcp2515_check_message()) { mcp2515_get_message(&message); int tps = (message.data[1]*256 + message.data[0]); a = (message.data[1]*256 + message.data[0]); b = (message.data[3]*256 + message.data[2]); c = (message.data[5]*256 + message.data[4]); d = (message.data[7]*256 + message.data[6]); return 1; } else { } } return 0; }
char CanbusClass::ecu_req(unsigned char pid, char *buffer) { tCAN message; float engine_data; int timeout = 0; char message_ok = 0; // Prepair message message.id = PID_REQUEST; message.header.rtr = 0; message.header.length = 8; message.data[0] = 0x02; message.data[1] = 0x01; message.data[2] = pid; message.data[3] = 0x00; message.data[4] = 0x00; message.data[5] = 0x00; message.data[6] = 0x00; message.data[7] = 0x00; mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0); if (mcp2515_send_message(&message)) { } if (mcp2515_check_message()) { if (mcp2515_get_message(&message)) { switch(message.data[2]) { /* Details from http://en.wikipedia.org/wiki/OBD-II_PIDs */ case ENGINE_LOAD: // Throttle Position engine_data = (message.data[3]*100)/255; sprintf(buffer,"%d %% ",(int) engine_data); break; case ENGINE_RPM: // ((A*256)+B)/4 [RPM] engine_data = ((message.data[3]*256) + message.data[4])/4; sprintf(buffer,"%d rpm ",(int) engine_data); break; case ENGINE_COOLANT_TEMP: // A-40 [degree C] engine_data = message.data[3] - 40; sprintf(buffer,"%d degC",(int) engine_data); break; case VEHICLE_SPEED: // A [km] engine_data = message.data[3]; sprintf(buffer,"%d km ",(int) engine_data); break; case MAF_SENSOR: // ((256*A)+B) / 100 [g/s] engine_data = ((message.data[3]*256) + message.data[4])/100; sprintf(buffer,"%d g/s",(int) engine_data); break; case O2_VOLTAGE: // A * 0.005 (B-128) * 100/128 (if B==0xFF, sensor is not used in trim calc) engine_data = message.data[3]*0.005; sprintf(buffer,"%d V",(int) engine_data); break; case THROTTLE: // Throttle Position engine_data = (message.data[3]*100)/255; sprintf(buffer,"%d %% ",(int) engine_data); break; case INTAKE_AIR_TEMP: // A-40 [degree C] engine_data = message.data[3] - 40; sprintf(buffer,"%d degC",(int) engine_data); break; case FUEL_TAKE_LEVEL: // Fuel Tank Level engine_data = (message.data[3]*100)/255; sprintf(buffer,"%d %% ",(int) engine_data); break; case OBD_STANDARD: // OBD_STD sprintf(buffer,"%d ",(int) message.data[3]); break; } } } }
char CanbusClass::ecu_req(unsigned char pid, char *buffer) { tCAN message; float engine_data; int timeout = 0; char message_ok = 0; // Prepair message message.id = PID_REQUEST; message.header.rtr = 0; message.header.length = 8; message.data[0] = 0x02; message.data[1] = 0x01; message.data[2] = pid; message.data[3] = 0x00; message.data[4] = 0x00; message.data[5] = 0x00; message.data[6] = 0x00; message.data[7] = 0x00; mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0); // SET(LED2_HIGH); if (mcp2515_send_message(&message)) { } while(timeout < 4000) { timeout++; if (mcp2515_check_message()) { if (mcp2515_get_message(&message)) { if((message.id == PID_REPLY) && (message.data[2] == pid)) // Check message is the reply and its the right PID { switch(message.data[2]) { /* Details from http://en.wikipedia.org/wiki/OBD-II_PIDs */ case ENGINE_RPM: // ((A*256)+B)/4 [RPM] engine_data = ((message.data[3]*256) + message.data[4])/4; sprintf(buffer,"%d rpm ",(int) engine_data); break; case ENGINE_COOLANT_TEMP: // A-40 [degree C] engine_data = message.data[3] - 40; sprintf(buffer,"%d degC",(int) engine_data); break; case VEHICLE_SPEED: // A [km] engine_data = message.data[3]; sprintf(buffer,"%d km ",(int) engine_data); break; case MAF_SENSOR: // ((256*A)+B) / 100 [g/s] engine_data = ((message.data[3]*256) + message.data[4])/100; sprintf(buffer,"%d g/s",(int) engine_data); break; case O2_VOLTAGE: // A * 0.005 (B-128) * 100/128 (if B==0xFF, sensor is not used in trim calc) engine_data = message.data[3]*0.005; sprintf(buffer,"%d v",(int) engine_data); case THROTTLE: // Throttle Position engine_data = (message.data[3]*100)/255; sprintf(buffer,"%d %% ",(int) engine_data); break; } message_ok = 1; } } } if(message_ok == 1) return 1; } return 0; }
int main(void) { // Initialisiere die UART Schnittstelle uart_init(UART_BAUD_SELECT(9600UL, F_CPU)); // Aktiviere Interrupts sei(); // Umleiten der Standardausgabe => ab jetzt koennen wir printf() verwenden stdout = &mystdout; // Versuche den MCP2515 zu initilaisieren if (!mcp2515_init()) { PRINT("Fehler: kann den MCP2515 nicht ansprechen!\n"); for (;;); } else { PRINT("MCP2515 is aktiv\n\n"); } PRINT("Erzeuge Nachricht\n"); tCAN message; // einige Testwerte message.id = 0x123; message.header.rtr = 0; message.header.length = 2; message.data[0] = 0xab; message.data[1] = 0xcd; print_can_message(&message); PRINT("\nwechsle zum Loopback-Modus\n\n"); mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), (1<<REQOP1)); // Sende eine Nachricht if (mcp2515_send_message(&message)) { PRINT("Nachricht wurde in die Puffer geschrieben\n"); } else { PRINT("Fehler: konnte die Nachricht nicht senden\n"); } // warte ein bisschen _delay_ms(10); if (mcp2515_check_message()) { PRINT("Nachricht empfangen!\n"); // read the message from the buffers if (mcp2515_get_message(&message)) { print_can_message(&message); PRINT("\n"); } else { PRINT("Fehler: konnte die Nachricht nicht auslesen\n\n"); } } else { PRINT("Fehler: keine Nachricht empfangen\n\n"); } PRINT("zurueck zum normalen Modus\n\n"); mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0); // wir sind ab hier wieder mit dem CAN Bus verbunden PRINT("Versuche die Nachricht per CAN zu verschicken\n"); // Versuche nochmal die Nachricht zu verschicken, diesmal per CAN if (mcp2515_send_message(&message)) { PRINT("Nachricht wurde in die Puffer geschrieben\n\n"); } else { PRINT("Fehler: konnte die Nachricht nicht senden\n\n"); } PRINT("Warte auf den Empfang von Nachrichten\n\n"); while (1) { // warten bis wir eine Nachricht empfangen if (mcp2515_check_message()) { PRINT("Nachricht empfangen!\n"); // Lese die Nachricht aus dem Puffern des MCP2515 if (mcp2515_get_message(&message)) { print_can_message(&message); PRINT("\n"); } else { PRINT("Kann die Nachricht nicht auslesen\n\n"); } } } return 0; }