int test_read_write(struct harness_t *harness_p) { struct canif_frame_t frame; struct canif_frame_t frames[1]; struct mcp2515_driver_t mcp2515; BTASSERT(mcp2515_init(&mcp2515, &spi_device[0], &pin_d10_dev, &exti_device[0], MCP2515_MODE_LOOPBACK, MCP2515_SPEED_1000KBPS, NULL, frames, membersof(frames)) == 0); BTASSERT(mcp2515_start(&mcp2515) == 0); /* Write a frame to the device. */ memset(&frame, 0, sizeof(frame)); frame.id = 57; frame.data[0] = 9; BTASSERT(mcp2515_write(&mcp2515, &frame) == 0); /* Read a frame from the device. */ memset(&frame, 0, sizeof(frame)); BTASSERT(mcp2515_read(&mcp2515, &frame) == 0); /* Verify frame contents. */ BTASSERT(frame.id == 57); BTASSERT(frame.data[0] == 9); BTASSERT(mcp2515_stop(&mcp2515) == 0); return (0); }
/** * Initialize the can controller */ void can_init(void) { mcp2515_init(); mcp2515_static_filter(can_filter); mcp2515_bit_modify(CANCTRL, (1<<REQOP2) | (1<<REQOP1) | (1<<REQOP0), 0); }
void can_init(void) { if (!mcp2515_init()) { uart_puts("Fehler: kann MCP2515 nicht ansprechen!\n\n"); } else { uart_puts("MCP2515 is aktiv\n\n"); } }
int main(void) { board_init(); led_init(); spi_init(); blink_init(); _delay_ms(10); #if (F_CPU == 16000000UL) mcp2515_init(0); #elif (F_CPU == 8000000UL) mcp2515_init(1); #else #error unsupported F_CPU value #endif buffer_reset(); usb_init(); ep0_init(); user_get_descriptor = strings_get_descr; /* move interrupt vectors to 0 */ MCUCR = 1 << IVCE; MCUCR = 0; sei(); hello(); while (1) { cli(); mcp2515_update_status(); sei(); buffer_tx_process(); buffer_rx_process(); //sleep_mode(); } }
int main (void) { /* * SPI-Modul initialisieren */ spi_init(); /* * Funkmodul initialisieren */ rfm12_init(); rfm12_rx_on(); /* * CAN-Bus initialisieren */ mcp2515_init(); /* Interrupts initialisieren */ interrupt_init(); /* * Hauptprogramm */ while (1) { if (rfm_new_data) { rfm_new_data = 0; cli(); struct rfm_message tmp_rfm_message; if (rfm_copy_data(&tmp_rfm_message)) { struct can_message tmp_can_message; tmp_can_message.typ = tmp_rfm_message.typ; tmp_can_message.rtr = tmp_rfm_message.rtr; tmp_can_message.id = tmp_rfm_message.id; tmp_can_message.length = tmp_rfm_message.length; for (uint8_t i=0; i<8; i++) { tmp_can_message.data[i] = tmp_rfm_message.data[i]; } can_send_message(&tmp_can_message); } sei(); } if (rfm_crc_error != rfm_crc_error_last) { struct can_message tmp_can_message; tmp_can_message.typ = Einzelmeldung; tmp_can_message.id = Funkkoppler_CRC_Error; tmp_can_message.length = Laenge_Einzelmeldung; tmp_can_message.data[0] = rfm_crc_error; can_send_message(&tmp_can_message); rfm_crc_error_last = rfm_crc_error; } set_sleep_mode(SLEEP_MODE_STANDBY); sleep_mode(); } }
char CanbusClass::init(unsigned char speed) { return mcp2515_init(speed); }
/** * Main function. Entry point for USBtin application. * Handles initialization and the the main processing loop. */ void main(void) { // initialize MCP2515 (reset and clock setup) mcp2515_init(); // switch (back) to external clock (if fail-safe-monitor switched to internal) OSCCON = 0x30; // disable all analog pin functions, set led pin to output ANSEL = 0; ANSELH = 0; TRISBbits.TRISB5 = 0; hardware_setLED(0); // initialize modules clock_init(); usb_init(); char line[LINE_MAXLEN]; unsigned char linepos = 0; unsigned short lastclock = 0; while (1) { // do module processing usb_process(); clock_process(); // receive characters from UART and collect the data until end of line is indicated if (usb_chReceived()) { unsigned char ch = usb_getch(); if (ch == CR) { line[linepos] = 0; parseLine(line); linepos = 0; } else if (ch != LR) { line[linepos] = ch; if (linepos < LINE_MAXLEN - 1) linepos++; } } // handles interrupt requests of MCP2515 controller: receive message and print it out. if ((state != STATE_CONFIG) && (hardware_getMCP2515Int())) { canmsg_t canmsg; mcp2515_receive_message(&canmsg); char type; unsigned char idlen; unsigned short timestamp = clock_getMS(); if (canmsg.flags.rtr) type = 'r'; else type = 't'; if (canmsg.flags.extended) { type -= 'a' - 'A'; idlen = 8; } else { idlen = 3; } usb_putch(type); sendHex(canmsg.id, idlen); sendHex(canmsg.length, 1); if (!canmsg.flags.rtr) { unsigned char i; for (i = 0; i < canmsg.length; i++) { sendHex(canmsg.data[i], 2); } } if (timestamping) { sendHex(timestamp, 4); } usb_putch(CR); } // led signaling hardware_setLED(state != STATE_CONFIG); // jump into bootloader, if jumper is closed if (hardware_getBLSwitch()) { UCON = 0; _delay(1000); RESET(); } } }
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; }