Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add STM32F3DISCOVERY example #73

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CANopenNode
Submodule CANopenNode updated 67 files
+191 −13 .clang-format
+2 −2 .github/FUNDING.yml
+293 −365 301/CO_Emergency.c
+281 −466 301/CO_Emergency.h
+157 −243 301/CO_HBconsumer.c
+104 −189 301/CO_HBconsumer.h
+103 −164 301/CO_NMT_Heartbeat.c
+111 −184 301/CO_NMT_Heartbeat.h
+390 −0 301/CO_Node_Guarding.c
+244 −0 301/CO_Node_Guarding.h
+154 −142 301/CO_ODinterface.c
+315 −458 301/CO_ODinterface.h
+532 −627 301/CO_PDO.c
+178 −284 301/CO_PDO.h
+976 −1,035 301/CO_SDOclient.c
+171 −302 301/CO_SDOclient.h
+863 −958 301/CO_SDOserver.c
+339 −475 301/CO_SDOserver.h
+151 −199 301/CO_SYNC.c
+87 −144 301/CO_SYNC.h
+70 −103 301/CO_TIME.c
+55 −103 301/CO_TIME.h
+88 −76 301/CO_config.h
+322 −443 301/CO_driver.h
+735 −626 301/CO_fifo.c
+163 −222 301/CO_fifo.h
+35 −58 301/crc16-ccitt.c
+15 −27 301/crc16-ccitt.h
+93 −71 303/CO_LEDs.c
+45 −63 303/CO_LEDs.h
+76 −74 304/CO_GFC.c
+34 −49 304/CO_GFC.h
+814 −663 304/CO_SRDO.c
+176 −204 304/CO_SRDO.h
+103 −122 305/CO_LSS.h
+327 −479 305/CO_LSSmaster.c
+134 −223 305/CO_LSSmaster.h
+264 −336 305/CO_LSSslave.c
+103 −146 305/CO_LSSslave.h
+1,056 −1,127 309/CO_gateway_ascii.c
+162 −292 309/CO_gateway_ascii.h
+844 −850 CANopen.c
+257 −345 CANopen.h
+481 −238 Doxyfile
+140 −0 MISRA.md
+22 −22 README.md
+0 −113 codingStyle
+5 −0 doc/CHANGELOG.md
+26 −9 doc/deviceSupport.md
+16 −16 doc/objectDictionary.md
+88 −142 example/CO_driver_blank.c
+30 −35 example/CO_driver_target.h
+91 −111 example/CO_storageBlank.c
+51 −63 example/CO_storageBlank.h
+14 −14 example/DS301_profile.eds
+14 −14 example/DS301_profile.md
+30 −14 example/DS301_profile.xpd
+129 −129 example/OD.c
+67 −67 example/OD.h
+63 −99 example/main_blank.c
+12 −13 extra/CO_trace.c
+7 −16 extra/CO_trace.h
+113 −133 storage/CO_eeprom.h
+165 −171 storage/CO_storage.c
+142 −169 storage/CO_storage.h
+222 −268 storage/CO_storageEeprom.c
+109 −133 storage/CO_storageEeprom.h
24 changes: 19 additions & 5 deletions CANopenNode_STM32/CO_app_STM32.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
#include "main.h"
#include <stdio.h>

#ifdef USE_CO_STORAGE_FLASH
#include "CO_storageFlash.h"
#else
#include "CO_storageBlank.h"
#endif
#include "OD.h"

CANopenNodeSTM32*
Expand All @@ -54,6 +58,7 @@ CO_t* CO = NULL; /* CANopen object */
// Global variables
uint32_t time_old, time_current;
CO_ReturnError_t err;
uint32_t storageInitError = 0;

/* This function will basically setup the CANopen node */
int
Expand All @@ -68,9 +73,13 @@ canopen_app_init(CANopenNodeSTM32* _canopenNodeSTM32) {
.len = sizeof(OD_PERSIST_COMM),
.subIndexOD = 2,
.attr = CO_storage_cmd | CO_storage_restore,
#ifdef USE_CO_STORAGE_FLASH
.reservedSpace = SIZE_OF_PAGE,
.offset = 0}};
#else
.addrNV = NULL}};
#endif
uint8_t storageEntriesCount = sizeof(storageEntries) / sizeof(storageEntries[0]);
uint32_t storageInitError = 0;
#endif

/* Allocate memory */
Expand All @@ -90,18 +99,23 @@ canopen_app_init(CANopenNodeSTM32* _canopenNodeSTM32) {
log_printf("Error: Can't allocate memory\n");
return 1;
} else {
log_printf("Allocated %u bytes for CANopen objects\n", heapMemoryUsed);
log_printf("Allocated %lu bytes for CANopen objects\n", heapMemoryUsed);
}

canopenNodeSTM32->canOpenStack = CO;

#if (CO_CONFIG_STORAGE) & CO_CONFIG_STORAGE_ENABLE
#ifdef USE_CO_STORAGE_FLASH
err = CO_storageFlash_init(&storage, CO->CANmodule, OD_ENTRY_H1010_storeParameters, OD_ENTRY_H1011_restoreDefaultParameters, storageEntries, storageEntriesCount, &storageInitError);
#else

err = CO_storageBlank_init(&storage, CO->CANmodule, OD_ENTRY_H1010_storeParameters,
OD_ENTRY_H1011_restoreDefaultParameters, storageEntries, storageEntriesCount,
&storageInitError);
#endif

if (err != CO_ERROR_NO && err != CO_ERROR_DATA_CORRUPT) {
log_printf("Error: Storage %d\n", storageInitError);
log_printf("Error: Storage %lu\n", storageInitError);
return 2;
}
#endif
Expand Down Expand Up @@ -155,7 +169,7 @@ canopen_app_resetCommunication() {
canopenNodeSTM32->activeNodeID, &errInfo);
if (err != CO_ERROR_NO && err != CO_ERROR_NODE_ID_UNCONFIGURED_LSS) {
if (err == CO_ERROR_OD_PARAMETERS) {
log_printf("Error: Object Dictionary entry 0x%X\n", errInfo);
log_printf("Error: Object Dictionary entry 0x%lX\n", errInfo);
} else {
log_printf("Error: CANopen initialization failed: %d\n", err);
}
Expand All @@ -165,7 +179,7 @@ canopen_app_resetCommunication() {
err = CO_CANopenInitPDO(CO, CO->em, OD, canopenNodeSTM32->activeNodeID, &errInfo);
if (err != CO_ERROR_NO) {
if (err == CO_ERROR_OD_PARAMETERS) {
log_printf("Error: Object Dictionary entry 0x%X\n", errInfo);
log_printf("Error: Object Dictionary entry 0x%lX\n", errInfo);
} else {
log_printf("Error: PDO initialization failed: %d\n", err);
}
Expand Down
63 changes: 63 additions & 0 deletions CANopenNode_STM32/CO_driver_target.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
#include <stddef.h>
#include <stdint.h>

#ifdef USE_FREERTOS_LOCKING
#include "FreeRTOS.h"
#include "task.h"
#endif

// Determining the CANOpen Driver

#if defined(FDCAN) || defined(FDCAN1) || defined(FDCAN2) || defined(FDCAN3)
Expand All @@ -45,7 +50,13 @@
#error This STM32 Do not support CAN or FDCAN
#endif

#ifdef USE_CO_STORAGE_FLASH
#define CO_CONFIG_STORAGE CO_CONFIG_STORAGE_ENABLE
#define CO_CONFIG_CRC16 CO_CONFIG_CRC16_ENABLE
#else
#undef CO_CONFIG_STORAGE_ENABLE // We don't need Storage option, implement based on your use case and remove this line from here
#endif


#ifdef CO_DRIVER_CUSTOM
#include "CO_driver_custom.h"
Expand Down Expand Up @@ -134,9 +145,60 @@ typedef struct {
uint8_t attr;
/* Additional variables (target specific) */
void* addrNV;
#ifdef USE_CO_STORAGE_FLASH
uint16_t offset;
uint16_t reservedSpace;
uint16_t crc;
#endif
} CO_storage_entry_t;

/* (un)lock critical section in CO_CANsend() */
#ifdef USE_FREERTOS_LOCKING

// NOTE: valid for STM32F3XX. Not tested on other MCUs
// This works as long as you don't have interrupts pre-empting each other, which call the CO_LOCK_* macros below!
#define CO_VECTACTIVE_MASK (0xFFUL)
#define CO_IS_IRQ() ((portNVIC_INT_CTRL_REG & CO_VECTACTIVE_MASK) != 0)

#define CO_ENTER_CRITICAL_SECTION() \
do \
{ \
if (CO_IS_IRQ()) \
{ \
taskENTER_CRITICAL_FROM_ISR(); \
} \
else \
{ \
taskENTER_CRITICAL(); \
} \
} while (0)

#define CO_EXIT_CRITICAL_SECTION() \
do \
{ \
if (CO_IS_IRQ()) \
{ \
taskEXIT_CRITICAL_FROM_ISR(0); \
} \
else \
{ \
taskEXIT_CRITICAL(); \
} \
} while (0)

/* (un)lock critical section in CO_CANsend() */
#define CO_LOCK_CAN_SEND(CAN_MODULE) CO_ENTER_CRITICAL_SECTION()
#define CO_UNLOCK_CAN_SEND(CAN_MODULE) CO_EXIT_CRITICAL_SECTION()

/* (un)lock critical section when accessing Object Dictionary */
#define CO_LOCK_OD(CAN_MODULE) CO_ENTER_CRITICAL_SECTION()
#define CO_UNLOCK_OD(CAN_MODULE) CO_EXIT_CRITICAL_SECTION()

/* (un)lock critical section in CO_errorReport() or CO_errorReset() */
#define CO_LOCK_EMCY(CAN_MODULE) CO_ENTER_CRITICAL_SECTION()
#define CO_UNLOCK_EMCY(CAN_MODULE) CO_EXIT_CRITICAL_SECTION()

#else
// Why disabling the whole Interrupt
#define CO_LOCK_CAN_SEND(CAN_MODULE) \
do { \
Expand All @@ -160,6 +222,7 @@ typedef struct {
__disable_irq(); \
} while (0)
#define CO_UNLOCK_OD(CAN_MODULE) __set_PRIMASK((CAN_MODULE)->primask_od)
#endif

/* Synchronization between CAN receive and message processing threads. */
#define CO_MemoryBarrier()
Expand Down
Loading