Skip to content

Commit

Permalink
Version 2.0.0 for Arduino ESP32 3.0.0-alpha3
Browse files Browse the repository at this point in the history
  • Loading branch information
pierremolinaro committed Jan 10, 2024
1 parent 417c07a commit 8c3c4d1
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 98 deletions.
2 changes: 1 addition & 1 deletion examples/CheckBitRate/CheckBitRate.ino
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void setup() {
delay (100) ;

attachInterrupt (settings.mTxPin, handleEdgeInterrupt, FALLING) ;
timer = timerBegin (0, 2, true) ;
timer = timerBegin (40'000'000) ;
// 0 = first timer
// 2 is prescaler so 80 MHz divided by 2 = 40 MHz signal
// true - counts up
Expand Down
74 changes: 0 additions & 74 deletions examples/ESP32CANRegisterTest/ESP32CANRegisterTest.ino

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
//----------------------------------------------------------------------------------------

#include <ACAN_ESP32.h>
#include <esp_chip_info.h>
#include <esp_flash.h>
#include <core_version.h> // For ARDUINO_ESP32_RELEASE

//----------------------------------------------------------------------------------------
Expand All @@ -46,7 +48,9 @@ void setup() {
Serial.print ("ESP32 SDK: ") ;
Serial.println (ESP.getSdkVersion ()) ;
Serial.print ("ESP32 Flash: ") ;
Serial.print (spi_flash_get_chip_size () / (1024 * 1024)) ;
uint32_t size_flash_chip ;
esp_flash_get_size (NULL, &size_flash_chip) ;
Serial.print (size_flash_chip / (1024 * 1024)) ;
Serial.print (" MB ") ;
Serial.println (((chip_info.features & CHIP_FEATURE_EMB_FLASH) != 0) ? "(embeded)" : "(external)") ;
Serial.print ("APB CLOCK: ") ;
Expand Down
8 changes: 6 additions & 2 deletions examples/LoopBackDemo/LoopBackDemo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
//----------------------------------------------------------------------------------------

#include <ACAN_ESP32.h>
#include <esp_chip_info.h>
#include <esp_flash.h>
#include <core_version.h> // For ARDUINO_ESP32_RELEASE

//----------------------------------------------------------------------------------------
Expand All @@ -23,7 +25,7 @@ static const uint32_t DESIRED_BIT_RATE = 1000UL * 1000UL ; // 1 Mb/s
// SETUP
//----------------------------------------------------------------------------------------

void setup() {
void setup () {
//--- Switch on builtin led
pinMode (LED_BUILTIN, OUTPUT) ;
digitalWrite (LED_BUILTIN, HIGH) ;
Expand All @@ -40,7 +42,9 @@ void setup() {
Serial.print ("ESP32 SDK: ") ;
Serial.println (ESP.getSdkVersion ()) ;
Serial.print ("ESP32 Flash: ") ;
Serial.print (spi_flash_get_chip_size () / (1024 * 1024)) ;
uint32_t size_flash_chip ;
esp_flash_get_size (NULL, &size_flash_chip) ;
Serial.print (size_flash_chip / (1024 * 1024)) ;
Serial.print (" MB ") ;
Serial.println (((chip_info.features & CHIP_FEATURE_EMB_FLASH) != 0) ? "(embeded)" : "(external)") ;
Serial.print ("APB CLOCK: ") ;
Expand Down
Binary file modified extras/acan-esp32.pdf
Binary file not shown.
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=ACAN_ESP32
version=1.1.2
version=2.0.0
author=Mohamed Irfanulla, Pierre Molinaro
maintainer=Pierre Molinaro <[email protected]>
sentence=An ESP32 CAN Driver.
paragraph=An ACAN Style library for ESP32, ESP32C3 and ESP32S3 CAN Peripheral. Compatible with ACAN2515, ACAN2515Tiny, ACAN2517, ACAN2517FD libraries. Default configuration enables reception of all frames. Reception filters can be easily defined. Note releases before 1.0.3 do not compile on ESP32 Arduino 2.x.x.
paragraph=An ACAN Style library for ESP32, ESP32C3 and ESP32S3 CAN Peripheral. Compatible with ACAN2515, ACAN2515Tiny, ACAN2517, ACAN2517FD libraries. Default configuration enables reception of all frames. Reception filters can be easily defined. Note: for ESP32 Arduino 3.x.x use ACAN_ESP32 2.x.x, for ESP32 Arduino 2.x.x use ACAN_ESP32 1.0.3 to 1.1.2, for ESP32 Arduino 1.x.x use ACAN_ESP32 < 1.0.3.
category=Communication
url=https://github.com/pierremolinaro/acan-esp32
architectures=*
7 changes: 4 additions & 3 deletions src/ACAN_ESP32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
//----------------------------------------------------------------------------------------

#include <ACAN_ESP32.h>
#include <core_version.h>

#include <esp_private/periph_ctrl.h>

//----------------------------------------------------------------------------------------
// ESP32 Critical Section
Expand Down Expand Up @@ -116,7 +117,7 @@ inline void ACAN_ESP32::setBitTimingSettings (const ACAN_ESP32_Settings & inSett
void ACAN_ESP32::setAcceptanceFilter (const ACAN_ESP32_Filter & inFilter) {
//--- Write the Code and Mask Registers with Acceptance Filter Settings
if (inFilter.mAMFSingle) {
TWAI_MODE_REG |= TWAI_RX_FILTER_MODE ;
TWAI_MODE_REG = TWAI_MODE_REG | TWAI_RX_FILTER_MODE ;
}
mAcceptedFrameFormat = inFilter.mFormat ;

Expand Down Expand Up @@ -247,7 +248,7 @@ bool ACAN_ESP32::recoverFromBusOff (void) const {
const bool inResetMode = (TWAI_MODE_REG & TWAI_RESET_MODE) != 0 ;
const bool recover = isBusOff && inResetMode ;
if (recover) {
TWAI_MODE_REG &= ~ TWAI_RESET_MODE ;
TWAI_MODE_REG = TWAI_MODE_REG & ~ TWAI_RESET_MODE ;
}
return recover ;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ACAN_ESP32_CANRegisters.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//----------------------------------------------------------------------------------------

#include <stdint.h>
#include <driver/periph_ctrl.h>
//#include <driver/periph_ctrl.h>

#include <freertos/FreeRTOS.h>
#include <freertos/queue.h>
Expand Down
19 changes: 10 additions & 9 deletions src/ACAN_ESP32_Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ mDesiredBitRate (inDesiredBitRate) {
uint32_t bestBRP = MAX_BRP ; // Setting for slowest bit rate
uint32_t bestTQCount = MAX_TQ ; // Setting for slowest bit rate
uint32_t smallestError = UINT32_MAX ;

uint32_t BRP = CAN_CLOCK / (inDesiredBitRate * TQCount) ; // BRP: min(2) max(128)
const uint32_t CANClock = CAN_CLOCK () ;
uint32_t BRP = CANClock / (inDesiredBitRate * TQCount) ; // BRP: min(2) max(128)
//--- Loop for finding best BRP and best TQCount
while ((TQCount >= MIN_TQ) && (BRP <= MAX_BRP)) {
//--- Compute error using BRP (caution: BRP should be > 0)
if (BRP >= MIN_BRP) {
const uint32_t error = CAN_CLOCK - (inDesiredBitRate * TQCount * BRP) ; // error is always >= 0
const uint32_t error = CANClock - (inDesiredBitRate * TQCount * BRP) ; // error is always >= 0
if (error < smallestError) {
smallestError = error ;
bestBRP = BRP ;
Expand All @@ -30,7 +30,7 @@ mDesiredBitRate (inDesiredBitRate) {
}
//--- Compute error using BRP+1 (caution: BRP+1 should be <= 128)
if (BRP < MAX_BRP) {
const uint32_t error = (inDesiredBitRate * TQCount * (BRP + 1)) - CAN_CLOCK ; // error is always >= 0
const uint32_t error = (inDesiredBitRate * TQCount * (BRP + 1)) - CANClock ; // error is always >= 0
if (error < smallestError) {
smallestError = error ;
bestBRP = BRP + 1 ;
Expand All @@ -39,7 +39,7 @@ mDesiredBitRate (inDesiredBitRate) {
}
//--- Continue with next value of TQCount
TQCount -= 1 ;
BRP = CAN_CLOCK / (inDesiredBitRate * TQCount) ;
BRP = CANClock / (inDesiredBitRate * TQCount) ;
}
//--- Set the BRP
mBitRatePrescaler = uint8_t (bestBRP) ;
Expand All @@ -55,7 +55,7 @@ mDesiredBitRate (inDesiredBitRate) {
mTripleSampling = (inDesiredBitRate <= 125000) && (mTimeSegment2 > 2) ;
//--- Final check of the configuration
const uint32_t W = bestTQCount * mDesiredBitRate * mBitRatePrescaler ;
const uint64_t diff = (CAN_CLOCK > W) ? (CAN_CLOCK - W) : (W - CAN_CLOCK) ;
const uint64_t diff = (CANClock > W) ? (CANClock - W) : (W - CANClock) ;
const uint64_t ppm = uint64_t (1000UL * 1000UL) ;
mBitRateClosedToDesiredRate = (diff * ppm) <= (uint64_t (W) * inTolerancePPM) ;
}
Expand All @@ -64,22 +64,23 @@ mDesiredBitRate (inDesiredBitRate) {

uint32_t ACAN_ESP32_Settings::actualBitRate (void) const {
const uint32_t TQCount = SYNC_SEGMENT + mTimeSegment1 + mTimeSegment2 ;
return CAN_CLOCK / mBitRatePrescaler / TQCount ;
return CAN_CLOCK () / mBitRatePrescaler / TQCount ;
}

//----------------------------------------------------------------------------------------

bool ACAN_ESP32_Settings::exactBitRate (void) const {
const uint32_t TQCount = SYNC_SEGMENT + mTimeSegment1 + mTimeSegment2 ;
return CAN_CLOCK == (mDesiredBitRate * mBitRatePrescaler * TQCount) ;
return CAN_CLOCK () == (mDesiredBitRate * mBitRatePrescaler * TQCount) ;
}

//----------------------------------------------------------------------------------------

uint32_t ACAN_ESP32_Settings::ppmFromDesiredBitRate(void) const {
const uint32_t TQCount = SYNC_SEGMENT + mTimeSegment1 + mTimeSegment2 ;
const uint32_t W = TQCount * mDesiredBitRate * mBitRatePrescaler ;
const uint64_t diff = (CAN_CLOCK > W) ? (CAN_CLOCK - W) : (W - CAN_CLOCK) ;
const uint32_t CANClock = CAN_CLOCK () ;
const uint64_t diff = (CANClock > W) ? (CANClock - W) : (W - CANClock) ;
const uint64_t ppm = (uint64_t)(1000UL * 1000UL) ; // UL suffix is required for Arduino Uno
return uint32_t ((diff * ppm) / W) ;
}
Expand Down
15 changes: 10 additions & 5 deletions src/ACAN_ESP32_Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@

#include <stdint.h>

//--- For getting gpio_num_t type declaration (added in release 1.0.3)
//--- For getting getApbFrequency function declaration
#ifdef ARDUINO
#include <driver/adc.h>
#include <Arduino.h>
// #include <esp32-hal-cpu.h>
#endif

//----------------------------------------------------------------------------------------
// CAN CLOCK
//----------------------------------------------------------------------------------------

#ifdef ARDUINO
static const uint32_t CAN_CLOCK = APB_CLK_FREQ / 2 ; // APB_CLK_FREQ: 80 MHz APB CLOCK
inline uint32_t CAN_CLOCK (void) { return getApbFrequency () / 2 ; }// APB_CLK_FREQ: 80 MHz APB CLOCK
#else
static const uint32_t CAN_CLOCK = 40 * 1000 * 1000 ; // 40 MHz
inline uint32_t CAN_CLOCK (void) { return 40 * 1000 * 1000 ; } // 40 MHz
#endif

//----------------------------------------------------------------------------------------
Expand All @@ -34,7 +35,11 @@ class ACAN_ESP32_Settings {
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//--- CAN driver operating modes
public: typedef enum : uint8_t {
public: typedef enum
#ifdef ARDUINO
: uint8_t
#endif
{
NormalMode,
ListenOnlyMode,
LoopBackMode
Expand Down

0 comments on commit 8c3c4d1

Please sign in to comment.