This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Code runs on IDE's but not on nRF52 board

Hi. I have the following code which runs perfectly on several C IDEs ( including Eclipse, Dev-C++, Code:Blocks and on the Linux Terminal using gcc as well ). However, when I put it on the board ( using either Keil or Eclipse ), the board does not give back any results. The Expected results and the results being obtained are shown below:

Code:

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>

#include "app_uart.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "nrf_gpio.h"
#include "nrf.h"
#include "bsp.h"
#include "boards.h"

const uint8_t leds_list[LEDS_NUMBER] = LEDS_LIST;
float InitialArrayValue=-999;
char DispNumStr [15];

//=======================================================================


//================ UART INITIALIZATION SETTINGS ======================
#define MAX_TEST_DATA_BYTES (15U)            /**< max number of test bytes to be used for tx and rx. */
#define UART_TX_BUF_SIZE 256                 /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 8                   /**< UART RX buffer size. */


uint32_t err_code;
uint8_t len;

void uart_error_handle(app_uart_evt_t * p_event)
{
	if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR){
		APP_ERROR_HANDLER(p_event->data.error_communication);
	}
	else if (p_event->evt_type == APP_UART_FIFO_ERROR){
		APP_ERROR_HANDLER(p_event->data.error_code);
	}
}

void SendString(const uint8_t * str)
{
  len = strlen((char *) str);
  for (uint8_t i = 0; i < len; i++)
    {err_code = app_uart_put(str[i]);
    APP_ERROR_CHECK(err_code);}
  nrf_delay_us(10000);
}
//========================================================================================


//================== STRUCTS AND FUNCTIONS FOR MAIN CODE ===============================
typedef struct ArrayStruct{
  float * array;
  int used;
  int size;
} Array;

// FREE ARRAY
void freeArray(Array *a) {
  free(a->array);
  a->array = NULL;
  a->used = 0;
  a->size = 0;
}

// INITIALIZE NULL ARRAY
void InitNullArray(Array * ArrayPtr){
  ArrayPtr->array=NULL;
  freeArray(ArrayPtr);
  ArrayPtr->array = (float *)malloc(1 * sizeof(float));
  ArrayPtr->used = 0;
  ArrayPtr->size = 1;
  ArrayPtr->array[0] = InitialArrayValue;
}

// INSERT ELEMENT INTO ARRAY
void insertArray(Array *a, float element) {
  if (a->used+1 == a->size) {
    a->size *= 2;
    a->array = (float *)realloc(a->array, a->size * sizeof(float));
  }
  a->array[(a->used)+1] = element;
  a->used=a->used+1;
}

// SHOW CONTENTS OF ARRAY
void ShowArray(Array * GivenArray){


	int iTemp;

	for(iTemp=1;iTemp<=GivenArray->used;iTemp++){
		SendString((uint8_t *) "Array Index ");
		sprintf(DispNumStr,"%d",iTemp);
		SendString((uint8_t *) DispNumStr);
		SendString((uint8_t *) " has value ");
		sprintf(DispNumStr,"%0.3f",GivenArray->array[iTemp]);
		SendString((uint8_t *) DispNumStr);
		SendString((uint8_t *) " \n\r");
	}

	SendString((uint8_t *) "SIZE:  ");
	sprintf(DispNumStr,"%d",GivenArray->size);
	SendString((uint8_t *) DispNumStr);
	SendString((uint8_t *) "   LENGTH: ");
	sprintf(DispNumStr,"%d",GivenArray->used);
	SendString((uint8_t *) DispNumStr);
	SendString((uint8_t *) " \n\n\r");
}
//=========================================================================







int main(void)
{

//================== UART INITIALIZATION CODE =====================
LEDS_CONFIGURE(LEDS_MASK);
LEDS_OFF(LEDS_MASK);

const app_uart_comm_params_t comm_params ={RX_PIN_NUMBER,TX_PIN_NUMBER,RTS_PIN_NUMBER,CTS_PIN_NUMBER,APP_UART_FLOW_CONTROL_ENABLED,false,UART_BAUDRATE_BAUDRATE_Baud9600};

APP_UART_FIFO_INIT(&comm_params,UART_RX_BUF_SIZE,UART_TX_BUF_SIZE,uart_error_handle,APP_IRQ_PRIORITY_LOW,err_code);
APP_ERROR_CHECK(err_code);

//=================================================================


// =============== MY TESTING CODE OF MAIN PROGRAM ====================
SendString((uint8_t *) "Starting Program... \n\r");

	Array A1; InitNullArray(&A1);
	Array B1; InitNullArray(&B1);

	int i;
	for(i=0;i<=10;i++) {
	insertArray(&A1,i);
	}

	for(i=11;i<=20;i++) {
	insertArray(&B1,i);
	}

	ShowArray(&A1);
	ShowArray(&B1);
SendString((uint8_t *) "Program Complete. Going into infinite while loop...\n\r");
// =============== MY TESTING CODE OF MAIN PROGRAM ====================

//=============== LEDS PATTERN CHANGING FUNCTION======================

	LEDS_CONFIGURE(LEDS_MASK);
    while (true)
    {
        for (int i = 0; i < LEDS_NUMBER; i++)
        {
            LEDS_INVERT(1 << leds_list[i]);
            nrf_delay_ms(500);
        }
    }
//====================================================================

    return 0;
}

The Expected Results are:

Starting Program...
Array Index 1 has value 0
Array Index 2 has value 1
Array Index 3 has value 2
Array Index 4 has value 3
Array Index 5 has value 4
Array Index 6 has value 5
Array Index 7 has value 6
Array Index 8 has value 7
Array Index 9 has value 8
Array Index 10 has value 9
Array Index 11 has value 10
SIZE:  16   LENGTH: 11

Array Index 1 has value 11
Array Index 2 has value 12
Array Index 3 has value 13
Array Index 4 has value 14
Array Index 5 has value 15
Array Index 6 has value 16
Array Index 7 has value 17
Array Index 8 has value 18
Array Index 9 has value 19
Array Index 10 has value 20
SIZE:  16   LENGTH: 10

Program Complete. Going into infinite while loop...

Results that are coming via UART from the nRF52 board are:

Starting Program...
Array Index 1 has value
Array Index 2 has value
Array Index 3 has value
Array Index 4 has value
Array Index 5 has value
Array Index 6 has value
Array Index 7 has value
Array Index 8 has value
Array Index 9 has value
Array Index 10 has value
Array Index 11 has value
SIZE:  16   LENGTH: 11

Array Index 1 has value
Array Index 2 has value
Array Index 3 has value
Array Index 4 has value
Array Index 5 has value
Array Index 6 has value
Array Index 7 has value
Array Index 8 has value
Array Index 9 has value
Array Index 10 has value
SIZE:  16   LENGTH: 10

Program Complete. Going into infinite while loop...

This shows that the the board is failing to either store the values or is not able to read them back..... Please help.

Related