Each LPC1768 is capable of controlling 2 CAN bus lines. Linking multiple chips together via SPI allows for more bus lines to be monitored simultaneously. Slave unit.
Revision 0:a46303f3277c, committed 2014-10-31
- Comitter:
- ggudgel
- Date:
- Fri Oct 31 22:14:03 2014 +0000
- Commit message:
- First working revision. Test functionality of one mbed system controlling another through SPI, while utilizing CAN bus lines.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CAN_thread.cpp Fri Oct 31 22:14:03 2014 +0000 @@ -0,0 +1,31 @@ +#include "mbed.h" +#include "rtos.h" +#include "CAN_thread.h" + +DigitalOut led1(LED1); +DigitalOut led2(LED2); +CAN can1(p9, p10); +CAN can2(p30, p29); +char counter1 = 0; + +void sendBUS1(void const *args) { + if(can1.write(CANMessage(1337, &counter1, 1))) { + counter1++; + } + led1 = !led1; +} + +void CAN_thread (void const *args) { + can1.frequency(125000); + can2.frequency(125000); + + RtosTimer CAN1send(sendBUS1, osTimerPeriodic); + CAN1send.start(1000); + CANMessage msg; + while(1) { + if(can2.read(msg)) { + led2 = !led2; + } + Thread::wait(10); + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CAN_thread.h Fri Oct 31 22:14:03 2014 +0000 @@ -0,0 +1,3 @@ +#include "mbed.h" + +void CAN_thread (void const *args); \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Oct 31 22:14:03 2014 +0000 @@ -0,0 +1,16 @@ +#include "mbed.h" +#include "rtos.h" +#include "spiSlaveProtocol.h" +#include "CAN_thread.h" + +int main() { + Thread thread(CAN_thread); + + + Serial* pc = new Serial(USBTX, USBRX); // tx, rx + + spiSlaveProtocol* device = new spiSlaveProtocol(pc); + while (1) { + device->run(); + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Fri Oct 31 22:14:03 2014 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/mbed_official/code/mbed-rtos/#a3452b867ec3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Oct 31 22:14:03 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/031413cf7a89 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/spiSlaveProtocol.cpp Fri Oct 31 22:14:03 2014 +0000 @@ -0,0 +1,32 @@ +#include "spiSlaveProtocol.h" + +spiSlaveProtocol::spiSlaveProtocol(Serial* PC) { + spiLine = new SPISlave(p5, p6, p7, p8); + spiLine->format(8,3); // Setup: bit data, high steady state clock, 2nd edge capture + spiLine->frequency(1000000); // 1MHz + + pc = PC; // tx, rx + pc->printf("======================================================\r\n"); + + timout = new Timer(); + + state = 0; + commandBytes = 0; +} + + void spiSlaveProtocol::run() { + if (spiLine->receive()) { + int valueFromMaster = spiLine->read(); + + if (valueFromMaster == 0x42) { + spiLine->reply(0xAA); // Prime SPI with next reply + //pc->printf("Life rxvd, next line will reply with 0xAA\n\r"); + } + + else if (valueFromMaster == 0x84) { + spiLine->reply(0xAB); + spiLine->reply(0xAC); + //pc->printf("Life multiplyer, next line will reply with 2 x 0xBB\n\r"); + } + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/spiSlaveProtocol.h Fri Oct 31 22:14:03 2014 +0000 @@ -0,0 +1,19 @@ +#ifndef _spiSlave_included_ +#define _spiSlave_included_ +#include "mbed.h" + +class spiSlaveProtocol +{ + public: + spiSlaveProtocol(Serial*); + void run(void); + + private: + SPISlave* spiLine; + int state; + int commandBytes; + Serial* pc; + Timer* timout; + +}; +#endif \ No newline at end of file