Forum Rule: Always post complete source code & details to reproduce any issue!
Page 1 of 3 1 2 3 LastLast
Results 1 to 25 of 60

Thread: CAN Bus Library for Teensy 3.1

  1. #1

    Teensy as a CAN bus controller

    Moderator Edit: I've merged the many CAN bus threads and made this topic stick to the top of the Suggestions forum.

    Current CAN Bus Status (June 2014)
    : Teachop's FlexCAN library works on Teensy 3.1.

    Please use this thread to discuss CAN bus for Teensy 3.1.


    -------------------------------


    Is it possible to use a teensy 3.0 for a CAN bus controller? A few other classmates and myself are trying to develop a supervisory computer to monitor all onboard sensors. We are thinking of using CAN for a few reasons; error detection, message priority, increased immunity to noise, etc.
    If teensy doesnt support CAN, are there any other suggestions? Perhaps I can convince some people to use I2C instead.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,927
    Moderator Edit: This old message was written before Teensy 3.1 was released, in January 2014.

    ------------------------------------------

    Currently none of the Teensy boards supports CAN bus.

    This CAN bus shield library will probably work on Teensy 3.0. It uses the SPI library to talk to the MCP2515 chip. I just checked the code and it compiles ok (on Linux, there's a lowercase filename that needs to be edited, but a trivial thing to fix, and it doesn't matter on Mac and Windows). Of course, you'll need that shield with those 2 chips.

    I do have a quick question though. If I were to look into trying that shield, or just those 2 chips somehow, what CAN devices would I use for testing?
    Last edited by PaulStoffregen; 11-16-2013 at 12:29 PM.

  3. #3
    Thanks for the link! Just to clarify, the shield allows communication between an Arduino and the 2515 controller with SPI interface and the 2551 transceiver? Please bare with me as I am very new to CAN. For each CAN device (sensors, actuators, etc.) there is a host processor that will send out (or receive) messages to the CAN transceiver. The transceiver manipulates the message so that the CAN controller can understand it. Then does the controller put the received message on the CAN bus so it is available to be used by the host processor (i.e. the Arduino/Teensy)? The CAN controller also has the ability to decide which message/node has priority.

    As for the test CAN devices, would two photoreceptors attached to analog pins work? So the message being sent would be based on the value being read at the pins? I dont think I understand this

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,927
    Let me rephrase that question...

    Suppose you magically had a Teensy with MCP2515 and tranceiver chip and 9 pin connector. What would you plug into that 9 pin connector? What other CAN stuff would you have communicate with Teensy?

  5. #5
    Ok now I understand. There are going to be quite a few inputs and outputs but here are a few: throttle sensor, brake sensor, traction control system, BMS error, Emergency kill switch, ready indicator, battery temperature sensor. Those are the more important ones of a long list. Pretty much the supervisory computer has to indicate whether any of the systems fail and if they do the battery has to be disconnected immediately.

  6. #6
    Paul,

    Would my next step be to purchase the MCP 2515 and 2551 along with the shield? Then setup a basic network to see if the teensy supports CAN? I am really excited to work on this project

  7. #7

  8. #8
    Thanks Paul. I will look into that but I think I may just go with another controller with CAN built in

  9. #9
    Hi Paul,

    I just noticed the 3.1 is CAN enabled. Ive done my research and understand more or less how a CAN network operates. With the CAN controller integrated into teensy, all I would need is a transceiver like the MCP2551/2561 to take the differential signals from the CAN bus and transmit a 1 (recessive) or 0 (dominant) to the CAN controller to then be processed by the teensy. In my case, we have these nodes that are putting data on the CAN bus to be processed by the supervisory computer. I assume the data being put on the bus from the nodes contains message ID, message length, message, etc. otherwise we would have now way of telling which node is trying to "speak".

    Anyways, what I am trying to get at is what does the CAN controller do? Does it just make it easier for the teensy to do its thing? I have read it has built in error handling functions.

    Also, is the CAN controller hardware that needs to be programmed to handle these errors? Is that what a CAN library would do?

    Many thanks!

  10. #10
    Additionally,

    What would have to be done to make a CAN library?

  11. #11
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,927
    Quote Originally Posted by chadder_all View Post
    What would have to be done to make a CAN library?
    Only programming, merely pressing buttons on a keyboard in the right order...

  12. #12
    Senior Member
    Join Date
    Jan 2013
    Posts
    966
    ROTFLMAO. If it would just be that easy ......

  13. #13
    say you want to put a message on the CAN bus, does the controller automatically frame the message or do you have to write functions/libraries to do so?

  14. #14
    The driver chip just handles the electrical driving on the bus.
    The controller handles sending a frame, which consists of lead bits, header, control-bits, data-length, data, checksum, and follow bits; and also does bit-stuffing and error detection and correction.
    CAN was invented by Bosch for cars and trucks, and there is a lot of info on the net, e.g. http://en.wikipedia.org/wiki/CAN_bus and the original spec at http://www.bosch-semiconductors.de/m...r/can2spec.pdf
    CAN controllers differ quite a lot. The MCP2515 is quite basic. The built in CAN controller in the Teensy 3.1 is more sophisticated, and has a number of 'mail-boxes', each of which can be used to receive particular messages, or to send messages. The messages can be filtered on the header field. and matched messages can cause automatic replies.
    BTW, you have to have two CAN nodes on a bus to get it to work, otherwise the CAN controller will hang.

  15. #15

    CAN support /library

    Hi All -

    If there is no CAN support for the FLEXCAN in Teensy 3.1, then I will take a stab at a very basic library.

    I am thinking:
    Can_setup( id, rate)
    Where id is channel number, rate is an enum(125000,...),
    And returns 0=fail, 1=success
    Can_send( type, header, length, &data)
    Where type is enum(standard, extended), header is 11 or 29-bit, length is number of data bytes,
    And returns 0=fail, 1=success
    Can_receive( type, header, length, &data)
    Where type is enum(standard, extended), header is 11 or 29-bit, length is number of data bytes,
    And returns 0=fail, 1=success

    Filtering and other stuff on request.

    David

  16. #16
    Junior Member
    Join Date
    Feb 2014
    Location
    Oakland, CA
    Posts
    2

    CAN Bus Library?

    It looks like Teensy 3.1 includes support for CAN bus though it doesn't look like there's any library or other documentation that supports it. Any idea on when there's be support for the CAN bus?

    Thanks!

    - Peter

  17. #17
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,828
    Quote Originally Posted by pkropf View Post
    It looks like Teensy 3.1 includes support for CAN bus though it doesn't look like there's any library or other documentation that supports it. Any idea on when there's be support for the CAN bus?

    Thanks!

    - Peter
    Paul posted a few days ago that a CAN library for T3 is in the TODO list - a few weeks out.

  18. #18
    Junior Member
    Join Date
    Feb 2014
    Location
    Oakland, CA
    Posts
    2
    Quote Originally Posted by stevech View Post
    Paul posted a few days ago that a CAN library for T3 is in the TODO list - a few weeks out.
    Excellent, thanks for the info!

    BTW, I had done a search for CAN bus but the forum engine returned nothing. Seems a bit strange to me.

  19. #19
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,828
    there is Arduino (AVR) code for the CAN bus... in the model railroad world. I found some with Google's help.

  20. #20
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,927
    I merged the many CAN bus threads.

    Please use this threads to discuss CAN bus. If you see people start new threads on CAN bus, please direct them to post and subscribe on this thread.

    Having it all in 1 thread will really help in a few weeks when I start actually working on the CAN library.

  21. #21
    Senior Member
    Join Date
    Jan 2014
    Posts
    105
    Quote Originally Posted by dpharris View Post
    If there is no CAN support for the FLEXCAN in Teensy 3.1, then I will take a stab at a very basic library.
    In the Arduino Due CAN API thread Paul provided a link to mbed's CAN API which looks very nice, clean and simple (and also very Arduino-like).

    http://mbed.org/handbook/CAN#api

    Maybe that is something you could aim for?

  22. #22
    There is an up-and-running Teensy3.1 CAN driver here. It will undoubtedly need to gain more features, but if interested please go ahead and take a look:
    https://github.com/teachop/FlexCAN_Library

  23. #23
    Teachop's code looks like a good start :-). Thanks for coalescing the various CAN threads.

  24. #24
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Hello Everyone,

    I am newbie to CAN Bus, I have done some google on it, but i want some clarification.

    1.If am having CAN controller inbuilt on my Microcontroller(say STM32), it is having CAN_H and CAN_L. Can i connect these CAN_H and CAN_L lines to other CAN device to form a CAN network. Is CAN tranceiver is needed? or can i connect One CAN device CAN_H to other CAN device CAN_H and same for CAN_L also.

    2. If i want to send some message to CAN bus, how and where can i send, since there is two lines CAN_H and CAN_L. and through which pin will receive data from the BUS. is there any transmitter and receiver Pin as in UART

    3. What is the difference between CAN and CANopen. Normally CAN data rate is max 1Mbps, is CANopen also has same datarate

  25. #25
    Quote Originally Posted by allen View Post
    I am newbie to CAN Bus, I have done some google on it, but i want some clarification
    1. Yes you need a CAN transceiver, it won't work to just connect between parts CAN-TX and RX directly. Also include termination (resistors).
    2. CAN is different from a UART. The UART has TX and RX that can operate at the same time, called full duplex. The CAN uses a single pair of wires in differential mode for noise immunity (some sort of ground reference is still needed too). The CAN bus can transfer information both ways but can only talk in one direction at a time, called half duplex.
    3. CANopen is an additional protocol layer on top of the CAN bus. It can run at different rates, and 1Mbps is one of them.

    Edit: Fixed TX/Rx oops thanks pawelsky for pointing it out!
    Last edited by teachop; 03-19-2014 at 09:05 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •