Giter VIP home page Giter VIP logo

stm32_can's Introduction

STM32_CAN library

Designed for STM32F105 CAN gateway. https://github.com/BitterAndReal/STM32_NMEA2000_CAN_gateway Might also works with F103 / F107 / F405 / F407... and other STM32 MCU's with integrated bxCAN controller.

To use this library, you will also need:

  • STM32 HAL for your MCU

Can be used with the following libraries

Example

How to use the library

#include "STM32_CAN.hpp"

// create CANbus1 object on stack
tSTM32_CAN &CANbus1 = *( new tSTM32_CAN(&hcan1, tSTM32_CAN::CAN250kbit) );

// send a frame
const unsigned char buf[8] = {
  0xFF,
  0xFF,
  0xFF,
  0xFF,
  0xFF,
  0xFF,
  0xFF,
  0xFF
};
CANbus1.CANSendFrame(0x19003838, 8, &buf);

// get a frame from the RX buffer
unsigned long id;
char len;
unsigned char buf[8];
CANbus1.CANGetFrame(&id, &len, &buf);


STM23CubeIDE setup

If you are not very experienced with STM23CubeIDE this guide can help to configure a project for your own STM32 based hardware. I recommend start with this example

  • Create a STM32 C++ project in STM23CubeIDE
  • Select your MCU which will add the correct HAL to your project.
  • We can not directly call C++ from the main.c

    Create your own application.cpp & application.hpp files with your C++ project setup() and loop() which are called from with extern "C" {...

  • In Cube MX configure external clock settings of your MCU.

    CAN bus needs an external clock source!

  • Activate the CAN bus you want to use.
  • Enable the CAN RX1 interrupt in the NVIC settings
  • Enable the CAN TX interrupt in the NVIC settings
  • If you want to use this library with the NMEA2000 library

    Copy the NMEA2000 and NMEA_STM32 library's to your "Libs" folder of your STM32Cube project or if you are using github use linked git submodules!.

    // Use the following git commands to add submodules
    cd YourProjectRepo/Libs
    git submodule add https://github.com/ttlappalainen/NMEA2000
    git submodule update --init --recursive
    git submodule add https://github.com/BitterAndReal/NMEA2000_STM32
    git submodule update --init --recursive
    // and update them with 
    git submodule foreach git pull
    
  • Add the library folders to the G++ compiler include paths:
    • Select your project in the Project Explorer.
    • go to: File > Properties > C/C++ Build > Settings > MCU G++ Compiler > Include paths
    • Add all folders inside libs to the included paths
  • If you want to use the CAN init of the library, which sets the CAN baud rate and filters, disable the STM32CubeIDE generated code.

    Navigate to “Project Manager” > “Advanced Settings” find MX_CAN#_Init” and enable “Do Not Generate Function Call”.

  • By default, the peripheral MSP initialization function HAL_CAN_MspInit (in stm32xxx_hal_msp.c) is automatically called and takes care of the configuration of the CAN_RX/TX GPIOs, enabling of the peripheral clock and enabling of the CAN interrupts

Known limitations

  • The 3 TX mailboxes are used in FIFO mode to make sure sequenced messages (like NMEA2000 fast packet) are sent in the correct order. This can result in a higher priority message waiting for up to 3 lower priority messages being sent first if they are already in the mailboxes. In the ring buffer of the library priority and order is handled correctly!
  • For dual CAN devices with 28 RX filters the filters are equaly split CAN1: 0...13 | CAN2: 14...27

Thanks for the great NMEA2000 CAN lib example Teensyx by ttlappalainen. https://github.com/ttlappalainen/NMEA2000_Teensyx


License

The MIT License

Copyright (c) 2022 Minos Eigenheer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-
1301  USA

stm32_can's People

Contributors

minoseigenheer avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.