1
0
Fork 0

Add heapMemoryUsed info from CO_new()

This commit is contained in:
Janez 2020-03-06 11:33:25 +01:00
parent 02a734a832
commit a98492ecaf
3 changed files with 43 additions and 11 deletions

View file

@ -35,7 +35,6 @@
extern const CO_OD_entry_t CO_OD[CO_OD_NoOfElements]; /* Object Dictionary */ extern const CO_OD_entry_t CO_OD[CO_OD_NoOfElements]; /* Object Dictionary */
static CO_t COO; /* Pointers to CANopen objects */ static CO_t COO; /* Pointers to CANopen objects */
CO_t *CO = NULL; /* Pointer to COO */ CO_t *CO = NULL; /* Pointer to COO */
static uint32_t CO_memoryUsed = 0; /* informative */
static CO_CANrx_t *CO_CANmodule_rxArray0; static CO_CANrx_t *CO_CANmodule_rxArray0;
static CO_CANtx_t *CO_CANmodule_txArray0; static CO_CANtx_t *CO_CANmodule_txArray0;
@ -109,9 +108,10 @@ static uint32_t CO_traceBufferSize[CO_NO_TRACE];
/******************************************************************************/ /******************************************************************************/
CO_ReturnError_t CO_new(void) { CO_ReturnError_t CO_new(uint32_t *heapMemoryUsed) {
int16_t i; int16_t i;
uint16_t errCnt = 0; uint16_t errCnt = 0;
uint32_t CO_memoryUsed = 0;
/* If CANopen was initialized before, return. */ /* If CANopen was initialized before, return. */
if (CO != NULL) { if (CO != NULL) {
@ -134,7 +134,6 @@ CO_ReturnError_t CO_new(void) {
/* globals */ /* globals */
CO = &COO; CO = &COO;
CO_memoryUsed = 0;
/* CANmodule */ /* CANmodule */
CO->CANmodule[0] = (CO_CANmodule_t *)calloc(1, sizeof(CO_CANmodule_t)); CO->CANmodule[0] = (CO_CANmodule_t *)calloc(1, sizeof(CO_CANmodule_t));
@ -259,6 +258,10 @@ CO_ReturnError_t CO_new(void) {
} }
#endif #endif
if(heapMemoryUsed != NULL) {
*heapMemoryUsed = CO_memoryUsed;
}
return (errCnt == 0) ? CO_ERROR_NO : CO_ERROR_OUT_OF_MEMORY; return (errCnt == 0) ? CO_ERROR_NO : CO_ERROR_OUT_OF_MEMORY;
} }
@ -346,7 +349,6 @@ void CO_delete(void *CANptr) {
/* globals */ /* globals */
CO = NULL; CO = NULL;
CO_memoryUsed = 0;
} }

View file

@ -216,10 +216,17 @@ extern CO_t *CO;
* *
* Function must be called the first time, after program starts. * Function must be called the first time, after program starts.
* *
* @remark
* With some microcontrollers it is necessary to specify Heap size within
* linker configuration.
*
* @param [out] heapMemoryUsed Information about heap memory used. Ignored if
* NULL.
*
* @return #CO_ReturnError_t: CO_ERROR_NO, CO_ERROR_ILLEGAL_ARGUMENT, * @return #CO_ReturnError_t: CO_ERROR_NO, CO_ERROR_ILLEGAL_ARGUMENT,
* CO_ERROR_OUT_OF_MEMORY * CO_ERROR_OUT_OF_MEMORY
*/ */
CO_ReturnError_t CO_new(void); CO_ReturnError_t CO_new(uint32_t *heapMemoryUsed);
/** /**

View file

@ -25,12 +25,17 @@
*/ */
#include <stdio.h>
#include "CANopen.h" #include "CANopen.h"
#define TMR_TASK_INTERVAL (1000) /* Interval of tmrTask thread in microseconds */ #define TMR_TASK_INTERVAL (1000) /* Interval of tmrTask thread in microseconds */
#define INCREMENT_1MS(var) (var++) /* Increment 1ms variable in tmrTask */ #define INCREMENT_1MS(var) (var++) /* Increment 1ms variable in tmrTask */
#define log_printf(macropar_message, ...) \
printf(macropar_message, ##__VA_ARGS__)
/* Global variables and objects */ /* Global variables and objects */
volatile uint16_t CO_timer1ms = 0U; /* variable increments each millisecond */ volatile uint16_t CO_timer1ms = 0U; /* variable increments each millisecond */
@ -40,14 +45,19 @@
int main (void){ int main (void){
CO_ReturnError_t err; CO_ReturnError_t err;
CO_NMT_reset_cmd_t reset = CO_RESET_NOT; CO_NMT_reset_cmd_t reset = CO_RESET_NOT;
uint32_t heapMemoryUsed;
/* Configure microcontroller. */ /* Configure microcontroller. */
/* Allocate memory */ /* Allocate memory */
err = CO_new(); err = CO_new(&heapMemoryUsed);
if (err != CO_ERROR_NO) { if (err != CO_ERROR_NO) {
while(1); log_printf("Error: Can't allocate memory\n");
return 0;
}
else {
log_printf("Allocated %d bytes for CANopen objects\n", heapMemoryUsed);
} }
/* initialize EEPROM */ /* initialize EEPROM */
@ -56,20 +66,27 @@ int main (void){
/* increase variable each startup. Variable is stored in EEPROM. */ /* increase variable each startup. Variable is stored in EEPROM. */
OD_powerOnCounter++; OD_powerOnCounter++;
log_printf("CANopenNode - Reset application, count = %d\n", OD_powerOnCounter);
while(reset != CO_RESET_APP){ while(reset != CO_RESET_APP){
/* CANopen communication reset - initialize CANopen objects *******************/ /* CANopen communication reset - initialize CANopen objects *******************/
uint16_t timer1msPrevious; uint16_t timer1msPrevious;
log_printf("CANopenNode - Reset communication\n");
/* disable CAN and CAN interrupts */ /* disable CAN and CAN interrupts */
/* initialize CANopen */ /* initialize CANopen */
err = CO_CANinit(NULL /* CAN module address */, 125 /* bit rate */); err = CO_CANinit(NULL /* CAN module address */, 125 /* bit rate */);
if (err == CO_ERROR_NO) { if (err != CO_ERROR_NO) {
err = CO_CANopenInit(10 /* NodeID */); log_printf("Error: CAN initialization failed: %d\n", err);
return 0;
} }
if(err != CO_ERROR_NO){ err = CO_CANopenInit(10 /* NodeID */);
while(1); if(err != CO_ERROR_NO) {
log_printf("Error: CANopen initialization failed: %d\n", err);
return 0;
/* CO_errorReport(CO->em, CO_EM_MEMORY_ALLOCATION_ERROR, CO_EMC_SOFTWARE_INTERNAL, err); */ /* CO_errorReport(CO->em, CO_EM_MEMORY_ALLOCATION_ERROR, CO_EMC_SOFTWARE_INTERNAL, err); */
} }
@ -85,6 +102,9 @@ int main (void){
reset = CO_RESET_NOT; reset = CO_RESET_NOT;
timer1msPrevious = CO_timer1ms; timer1msPrevious = CO_timer1ms;
log_printf("CANopenNode - Running...\n");
fflush(stdout);
while(reset == CO_RESET_NOT){ while(reset == CO_RESET_NOT){
/* loop for normal program execution ******************************************/ /* loop for normal program execution ******************************************/
uint16_t timer1msCopy, timer1msDiff; uint16_t timer1msCopy, timer1msDiff;
@ -100,6 +120,8 @@ int main (void){
/* Nonblocking application code may go here. */ /* Nonblocking application code may go here. */
/* Process EEPROM */ /* Process EEPROM */
/* optional sleep for short time */
} }
} }
@ -111,6 +133,7 @@ int main (void){
/* delete objects from memory */ /* delete objects from memory */
CO_delete((void*) 0/* CAN module address */); CO_delete((void*) 0/* CAN module address */);
log_printf("CANopenNode finished\n");
/* reset */ /* reset */
return 0; return 0;