Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Kamal_CAN ReadFromSerial446 USNA-UMBC-KF-02_v3-noise USNA-UMBC-KF-01
Diff: main.cpp
- Revision:
- 1:dbc44582f2f8
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Sun Jun 13 09:52:03 2010 +0000
@@ -0,0 +1,189 @@
+//#define PROTOPLATFORM
+#define MBEDPLATFORM
+// Flip an LED every time we see the rising edge interrupt on a pin
+#include "mbed.h"
+#include "CAN3.h"
+#include "mcp2515.h"
+#include "mcp2515_can.h"
+#include <Includes/Platform.h>
+
+
+
+DigitalOut led_can(LED1);
+DigitalOut led_serial(LED2);
+DigitalOut led_analog(LED3);
+DigitalOut led_status(LED4);
+Serial console(CONSOLETX, CONSOLERX); // tx, rx
+//Serial console(USBTX, USBRX); // tx, rx
+
+
+SPI spi1(CAN_SPI_MOSI, CAN_SPI_MISO, CAN_SPI_SCK);
+//SPI spi1(p5,p6,p7);
+CAN3 can1(spi1, CAN1_CS, CAN1_NT);
+CAN3 can2(spi1, CAN2_CS, CAN2_NT);
+CAN3 can3(spi1, CAN3_CS, CAN3_NT);
+CAN3 can4(spi1, CAN4_CS, CAN4_NT);
+
+//CAN3 can1(spi1, p19, p11);
+//CAN3 can2(spi1, p20, p12);
+//CAN3 can5(spi1, p21, p23);
+//CAN can5(p30, p29);
+
+//CAN can5(CAN5_L, CAN5_H);
+
+
+Ticker mbed_can_ticker;
+Ticker mcp2515_s1_can_ticker;
+Ticker mcp2515_s2_can_ticker;
+Ticker mcp2515_s3_can_ticker;
+Ticker mcp2515_s4_can_ticker;
+
+char testbytes[] = { 0xA1, 0xA2, 0xAA, 0xAA, 0x00, 0x00, 0x00, 0xF0 };
+
+void showMessage(CAN3 can) {
+ CANMessage msg;
+
+
+ while ( can.read(&msg) == CAN_OK ) {
+ console.printf(" \n \r CAN[]: rx -> id 0x%02x dlc %d ",msg.id,msg.len);
+ for (int i=0;i<msg.len;i++) {
+ console.printf("%02x", msg.data[i]);
+ console.printf(" ");
+ led_serial=!led_serial;
+ }
+
+ }
+}
+
+void send() {
+ static char counter = 0;
+ //can5.write(CANMessage(0x200, &counter, 1));
+ counter++;
+
+}
+
+void sendSPICAN1() {
+
+
+ CANMessage test(0x100, testbytes, 8);
+ can1.write(&test);
+
+}
+
+void sendSPICAN2() {
+
+
+ CANMessage test(0x200, testbytes, 8);
+ can2.write(&test);
+
+}
+
+
+void sendSPICAN3() {
+
+
+ CANMessage test(0x300, testbytes, 8);
+ // can5.write(test);
+
+}
+
+
+void sendSPICAN4() {
+
+
+ CANMessage test(0x400, testbytes, 8);
+ //can4.write(&test);
+
+}
+
+
+///////
+
+
+void getCANMessages() {
+
+showMessage(can1);
+showMessage(can2);
+//showMessage(can3);
+//showMessage(can4);
+
+}
+
+int main() {
+
+
+
+led_can=1;
+led_status=1;
+
+ console.baud(57600);
+ console.printf("\nStarting\n\n");
+
+ // freopen("/console", "w", stdout);
+ // spi1.format(8,0);
+ // spi1.frequency(5000000); // anything above 5000000 5MHZ works
+ //spi1.format(16,0);
+ spi1.frequency(10000000);
+
+// can5.frequency(125000);//(500000); 50KBPS 1250000
+
+ console.printf("\n\r\rCAN1 \n\r");
+ // can1.frequency(50000);
+ can1.frequency(50000);
+
+ console.printf("\n\r\rCAN2 \n\r");
+ // can2.frequency(5000);
+can1.frequency(125000);
+
+ console.printf("\n\r\rCAN3 \n\r");
+ can1.frequency(250000);
+
+ console.printf("\n\r\rCAN4 \n\r");
+ can1.frequency(500000);
+
+
+ wait(.1);
+ led_can=0;
+ led_serial=0;
+ led_analog=0;
+ led_status=1;
+
+
+ //can3._spi.frequency(5000000); // anything above 5000000 5MHZ works
+
+ can1.fall(&getCANMessages);
+ can2.fall(&getCANMessages);
+ //can5.fall(&canmsg2);
+
+
+ printf("\n\r\n\r\n\r\n\r\n\r");
+ wait(.1);
+ mbed_can_ticker.attach(&send, 3);
+ mcp2515_s1_can_ticker.attach(&sendSPICAN1, 2);
+ mcp2515_s2_can_ticker.attach(&sendSPICAN2, 3);
+ mcp2515_s3_can_ticker.attach(&sendSPICAN3, 2);
+ mcp2515_s4_can_ticker.attach(&sendSPICAN4, 5);
+ //mcp2515_s3_can_ticker.attach(&sendSPICAN5, 1);
+ console.printf("here");
+ while (1) {
+
+ console.printf("here2");
+ wait(1);
+
+ // canmsg1();
+ /*
+ //MODE_NORMAL
+ if ( ( can3.can_checkReceive() == CAN_MSGAVAIL ) ) {
+ can3.process_CANMessages();
+ }
+ //MODE_NORMAL
+ if ( ( can4.can_checkReceive() == CAN_MSGAVAIL ) ) {
+ can4.process_CANMessages();
+ }
+ */
+ //can3.dumpExtendedStatus();
+
+ }
+
+
+}
\ No newline at end of file