Skip to content

Commit

Permalink
Merge pull request #107 from Ross-Robotics/feature/cmake_support
Browse files Browse the repository at this point in the history
CMake Support [breaking change Ctor API]
  • Loading branch information
aentinger authored Feb 5, 2025
2 parents 2ac84a7 + ee42941 commit c909f40
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 27 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/smoke-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Smoke Test

on:
push:
pull_request:
workflow_dispatch:
repository_dispatch:

permissions:
contents: read

jobs:
smoke-test:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install CMake
uses: lukka/get-cmake@latest
with:
cmakeVersion: "~3.28.0"

- name: Configure the build
run: mkdir build && cmake -B build/ ./

- name: Build library
run: cmake --build build/
30 changes: 30 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
##########################################################################
cmake_minimum_required(VERSION 3.28)
##########################################################################
project("libmcp2515")
##########################################################################
if(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_C_COMPILER_VERSION VERSION_LESS 11)
message(WARNING "GNU C compiler version ${CMAKE_C_COMPILER_VERSION} is too old and is unsupported. Version 11+ is required.")
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 11)
message(WARNING "GNU C++ compiler version ${CMAKE_CXX_COMPILER_VERSION} is too old and is unsupported. Version 11+ is required.")
endif()
##########################################################################
set(CMAKE_VERBOSE_MAKEFILE OFF)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
##########################################################################
add_library(${PROJECT_NAME} STATIC
"src/ArduinoMCP2515.cpp"
"src/MCP2515/MCP2515_Config.cpp"
"src/MCP2515/MCP2515_Const.cpp"
"src/MCP2515/MCP2515_Control.cpp"
"src/MCP2515/MCP2515_Io.cpp"
)
##########################################################################
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Wpedantic -Werror)
##########################################################################
target_include_directories(${PROJECT_NAME} PUBLIC
"src"
)
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20)
##########################################################################
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
[![Check keywords.txt](https://github.com/107-systems/107-Arduino-MCP2515/actions/workflows/check-keywords-txt.yml/badge.svg)](https://github.com/107-systems/107-Arduino-MCP2515/actions/workflows/check-keywords-txt.yml)
[![General Formatting Checks](https://github.com/107-systems/107-Arduino-MCP2515/workflows/General%20Formatting%20Checks/badge.svg)](https://github.com/107-systems/107-Arduino-MCP2515/actions?workflow=General+Formatting+Checks)
[![Spell Check](https://github.com/107-systems/107-Arduino-MCP2515/workflows/Spell%20Check/badge.svg)](https://github.com/107-systems/107-Arduino-MCP2515/actions?workflow=Spell+Check)
[![Build Status](https://github.com/107-systems/107-Arduino-MCP2515/workflows/Smoke%20Test/badge.svg)](https://github.com/107-systems/107-Arduino-MCP2515/actions?workflow=Smoke+Test)

<p align="center">
<a href="https://github.com/107-systems/l3xz"><img src="https://raw.githubusercontent.com/107-systems/.github/main/logo/l3xz-logo-memento-mori-github.png" width="30%"></a>
Expand Down
1 change: 1 addition & 0 deletions examples/MCP2515-CAN-Sniffer/MCP2515-CAN-Sniffer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ ArduinoMCP2515 mcp2515([]() { digitalWrite(MKRCAN_MCP2515_CS_PIN, LOW); },
[]() { digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH); },
[](uint8_t const d) { return SPI.transfer(d); },
micros,
millis,
onReceiveBufferFull,
nullptr,
[](MCP2515::EFLG const err_flag) { Serial.print("MCP2515::OnError, error code = "); Serial.println(MCP2515::toStr(err_flag)); },
Expand Down
1 change: 1 addition & 0 deletions examples/MCP2515-Filter/MCP2515-Filter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ ArduinoMCP2515 mcp2515([]() { digitalWrite(MKRCAN_MCP2515_CS_PIN, LOW); },
[]() { digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH); },
[](uint8_t const d) { return SPI.transfer(d); },
micros,
millis,
onReceiveBufferFull,
nullptr);

Expand Down
1 change: 1 addition & 0 deletions examples/MCP2515-Loopback/MCP2515-Loopback.ino
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ ArduinoMCP2515 mcp2515([]() { digitalWrite(MKRCAN_MCP2515_CS_PIN, LOW); },
[]() { digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH); },
[](uint8_t const d) { return SPI.transfer(d); },
micros,
millis,
onReceiveBufferFull,
nullptr);

Expand Down
5 changes: 3 additions & 2 deletions src/ArduinoMCP2515.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ ArduinoMCP2515::ArduinoMCP2515(SpiSelectFunc select,
SpiDeselectFunc deselect,
SpiTransferFunc transfer,
MicroSecondFunc micros,
MilliSecondFunc millis,
OnReceiveBufferFullFunc on_rx_buf_full,
OnTransmitBufferEmptyFunc on_tx_buf_empty,
OnCanErrorFunc on_error,
OnCanWarningFunc on_warning)
: _io{select, deselect, transfer}
, _cfg{_io}
: _io{select, deselect, transfer, micros}
, _cfg{_io, millis}
, _ctrl{_io}
, _micros{micros}
, _on_rx_buf_full{on_rx_buf_full}
Expand Down
16 changes: 4 additions & 12 deletions src/ArduinoMCP2515.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "MCP2515/MCP2515_Io.h"
#include "MCP2515/MCP2515_Config.h"
#include "MCP2515/MCP2515_Control.h"
#include "MCP2515/MCP2515_Types.h"

#undef min
#undef max
Expand Down Expand Up @@ -82,17 +83,6 @@ enum class CanBitRate : size_t
BR_1000kBPS_12MHZ
};

typedef std::function<unsigned long()> MicroSecondFunc;
#if LIBCANARD
typedef std::function<void(CanardFrame const & frame)> OnReceiveBufferFullFunc;
#else
typedef std::function<void(uint32_t const, uint32_t const, uint8_t const *, uint8_t const)> OnReceiveBufferFullFunc;
#endif
class ArduinoMCP2515;
typedef std::function<void(ArduinoMCP2515 *)> OnTransmitBufferEmptyFunc;
typedef std::function<void(MCP2515::EFLG const)> OnCanErrorFunc;
typedef std::function<void(MCP2515::EFLG const)> OnCanWarningFunc;

/**************************************************************************************
* CLASS DECLARATION
**************************************************************************************/
Expand All @@ -106,6 +96,7 @@ class ArduinoMCP2515
MCP2515::SpiDeselectFunc deselect,
MCP2515::SpiTransferFunc transfer,
MicroSecondFunc micros,
MilliSecondFunc millis,
OnReceiveBufferFullFunc on_rx_buf_full,
OnTransmitBufferEmptyFunc on_tx_buf_empty,
OnCanErrorFunc on_error,
Expand All @@ -115,9 +106,10 @@ class ArduinoMCP2515
MCP2515::SpiDeselectFunc deselect,
MCP2515::SpiTransferFunc transfer,
MicroSecondFunc micros,
MilliSecondFunc millis,
OnReceiveBufferFullFunc on_rx_buf_full,
OnTransmitBufferEmptyFunc on_tx_buf_empty)
: ArduinoMCP2515{select, deselect, transfer, micros, on_rx_buf_full, on_tx_buf_empty, nullptr, nullptr}
: ArduinoMCP2515{select, deselect, transfer, micros, millis, on_rx_buf_full, on_tx_buf_empty, nullptr, nullptr}
{ }


Expand Down
8 changes: 3 additions & 5 deletions src/MCP2515/MCP2515_Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

#include "MCP2515_Config.h"

#include <Arduino.h>

/**************************************************************************************
* NAMESPACE
**************************************************************************************/
Expand All @@ -24,10 +22,10 @@ namespace MCP2515
* CTOR/DTOR
**************************************************************************************/

MCP2515_Config::MCP2515_Config(MCP2515_Io & io)
MCP2515_Config::MCP2515_Config(MCP2515_Io & io, MilliSecondFunc millis)
: _io{io}
, _millis{millis}
{

}

/**************************************************************************************
Expand All @@ -40,7 +38,7 @@ bool MCP2515_Config::setMode(Mode const mode)

_io.modifyRegister(Register::CANCTRL, CANCTRL_REQOP_MASK, mode_val);

for(unsigned long const start = millis(); (millis() - start) < 10; )
for(unsigned long const start = _millis(); (_millis()- start) < 10; )
{
uint8_t const canstat_op_mode = (_io.readRegister(Register::CANSTAT) & CANSTAT_OP_MASK);
if(canstat_op_mode == mode_val) {
Expand Down
4 changes: 3 additions & 1 deletion src/MCP2515/MCP2515_Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
**************************************************************************************/

#include "MCP2515_Io.h"
#include "MCP2515_Types.h"

/**************************************************************************************
* NAMESPACE
Expand Down Expand Up @@ -92,7 +93,7 @@ static CanBitRateConfig constexpr BitRate_1000kBPS_12MHz = {0x00, 0x88, 0x01};
class MCP2515_Config
{
public:
MCP2515_Config(MCP2515_Io & io);
MCP2515_Config(MCP2515_Io & io, MilliSecondFunc millis);


bool setMode (Mode const mode);
Expand All @@ -117,6 +118,7 @@ class MCP2515_Config

private:
MCP2515_Io & _io;
MilliSecondFunc _millis;

void setFilterId (Register const rxf_n_sidh, uint32_t const id);
void setFilterMask(Register const rxm_n_sidh, uint32_t const mask);
Expand Down
3 changes: 1 addition & 2 deletions src/MCP2515/MCP2515_Control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

#include "MCP2515_Control.h"

#include <Arduino.h>

#undef min
#undef max
#include <algorithm>
#include <cstring>

/**************************************************************************************
* NAMESPACE
Expand Down
12 changes: 8 additions & 4 deletions src/MCP2515/MCP2515_Io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

#include "MCP2515_Io.h"

#include <Arduino.h>

#include <assert.h>

/**************************************************************************************
Expand Down Expand Up @@ -50,10 +48,11 @@ static Instruction const TABLE_READ_RX_BUFFER[] =
* CTOR/DTOR
**************************************************************************************/

MCP2515_Io::MCP2515_Io(SpiSelectFunc select, SpiDeselectFunc deselect, SpiTransferFunc transfer)
MCP2515_Io::MCP2515_Io(SpiSelectFunc select, SpiDeselectFunc deselect, SpiTransferFunc transfer, MicroSecondFunc micros)
: _select{select}
, _deselect{deselect}
, _transfer{transfer}
, _micros{micros}
{

}
Expand All @@ -70,7 +69,12 @@ void MCP2515_Io::reset()
_transfer(instruction);
_deselect();

delay(10);
/* Delay 10 ms. */
auto const start_time = _micros();
while ((_micros() - start_time) < (10*1000UL))
{
asm volatile("nop");
}
}

uint8_t MCP2515_Io::status()
Expand Down
4 changes: 3 additions & 1 deletion src/MCP2515/MCP2515_Io.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <functional>

#include "MCP2515_Const.h"
#include "MCP2515_Types.h"

/**************************************************************************************
* NAMESPACE
Expand Down Expand Up @@ -85,7 +86,7 @@ class MCP2515_Io

public:

MCP2515_Io(SpiSelectFunc select, SpiDeselectFunc deselect, SpiTransferFunc transfer);
MCP2515_Io(SpiSelectFunc select, SpiDeselectFunc deselect, SpiTransferFunc transfer, MicroSecondFunc micros);


static uint8_t constexpr TX_BUF_SIZE = 5 + 8;
Expand All @@ -110,6 +111,7 @@ class MCP2515_Io
SpiSelectFunc _select;
SpiDeselectFunc _deselect;
SpiTransferFunc _transfer;
MicroSecondFunc _micros;

};

Expand Down
36 changes: 36 additions & 0 deletions src/MCP2515/MCP2515_Types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* This software is distributed under the terms of the MIT License.
* Copyright (c) 2020 LXRobotics.
* Author: Alexander Entinger <[email protected]>
* Contributors: https://github.com/107-systems/107-Arduino-MCP2515/graphs/contributors.
*/

#ifndef ARDUINO_MCP2515_TYPES_H_
#define ARDUINO_MCP2515_TYPES_H_

/**************************************************************************************
* INCLUDE
**************************************************************************************/

#include <functional>
#include <cstdint>

#include "MCP2515/MCP2515_Const.h"

/**************************************************************************************
* TYPEDEF
**************************************************************************************/

typedef std::function<unsigned long()> MicroSecondFunc;
typedef std::function<unsigned long()> MilliSecondFunc;
#if LIBCANARD
typedef std::function<void(CanardFrame const & frame)> OnReceiveBufferFullFunc;
#else
typedef std::function<void(uint32_t const, uint32_t const, uint8_t const *, uint8_t const)> OnReceiveBufferFullFunc;
#endif
class ArduinoMCP2515;
typedef std::function<void(ArduinoMCP2515 *)> OnTransmitBufferEmptyFunc;
typedef std::function<void(MCP2515::EFLG const)> OnCanErrorFunc;
typedef std::function<void(MCP2515::EFLG const)> OnCanWarningFunc;

#endif /* ARDUINO_MCP2515_TYPES_H_ */

0 comments on commit c909f40

Please sign in to comment.