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

Modified peripheral UART works for RBL BLE Nano v2.0 (nRF52832, nRF5 SDK 14) but fails for RBL BLE Nano v1.0 (nRF51822, nRF5 SDK 11)

Modify peripheral UART example to use a GPIO pin as 1-wire interface to read DS18B20 temperature sensor.

This modified code works for RBL BLE Nano v2.0 (nRF52832, nRF5 SDK 14) but fails for RBL BLE Nano v1.0 (nRF51822, nRF5 SDK 11). 1-wire interface is working but

printf("%c%c\r\n", raw_temp >> 8, raw_temp & 0xff) doesn't.

The code for nRF51822 is attached for your reference. Please help to analyze why it fails. Thank you.

/** Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
 *
 * The information contained herein is property of Nordic Semiconductor ASA.
 * Terms and conditions of usage are described in detail in NORDIC
 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
 *
 * Licensees are granted free, non-transferable use of the information. NO
 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
 * the file.
 *
 */

/** @file
 * @defgroup uart_example_main main.c
 * @{
 * @ingroup uart_example
 * @brief UART Example Application main file.
 *
 * This file contains the source code for a sample application using UART.
 * 
 */

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "app_uart.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "nrf.h"
#include "bsp.h"
//DS18B20 stuff
#include "app_timer.h"
#include "nrf_drv_clock.h"
#include "nrf_drv_timer.h"
#include "string.h"

//#define ENABLE_LOOPBACK_TEST  /**< if defined, then this example will be a loopback test, which means that TX should be connected to RX to get data loopback. */

#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 1                           /**< UART RX buffer size. */
#define UART_TX_BUF_SIZE 1024                         /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 1024                           /**< UART RX buffer size. */

//DS18B20 stuff
#define DS18B20PIN 4

void ds18b20_read(void * data_ptr, unsigned int num_bytes)
{
  char  ch; //Current reading byte buffer
  char * data_buf = data_ptr;
  
  int i=0,u=0;
  for (i=0;i<num_bytes;i++)
  {
    ch=0;
    for(u=0;u<8;u++)
    {
      //Form read slot
      nrf_gpio_cfg_output(DS18B20PIN);
      nrf_gpio_pin_write(DS18B20PIN,0);
      nrf_delay_us(5);
      nrf_gpio_cfg_input(DS18B20PIN, NRF_GPIO_PIN_NOPULL);
      nrf_delay_us(5);
      if(nrf_gpio_pin_read(DS18B20PIN)>0)
      {
        ch |= 1 << u; //There is 1 on the bus
      }
      else
      {
        ch &= ~(1 << u); //There us 0 on the bus
      }

      //Apply "long" timer for make sure that timeslot is end
      nrf_delay_us(60);
    }
    data_buf[i]=ch;
  }
  nrf_gpio_cfg_input(DS18B20PIN, NRF_GPIO_PIN_NOPULL);
}

void ds18b20_write(void * data_ptr, unsigned int num_bytes)
{
  char  ch; //Current reading byte buffer
  char * data_buf = data_ptr;
  
  int i=0,u=0;
  for (i=0;i<num_bytes;i++)
  {
    ch=data_buf[i];
    for(u=0;u<8;u++)
    {
      //Form write slot
      nrf_gpio_cfg_output(DS18B20PIN);
      nrf_gpio_pin_write(DS18B20PIN,0);
      nrf_delay_us(1);
      //write 1 - pull bus to HIGH just after short timer
      if(ch&(1<<u))
      {
        nrf_gpio_cfg_input(DS18B20PIN, NRF_GPIO_PIN_NOPULL); 
      }
      //Apply "long" timer for make sure that timeslot is end
      nrf_delay_us(60);
      //Release bus, if this wasn't done before
      nrf_gpio_cfg_input(DS18B20PIN, NRF_GPIO_PIN_NOPULL);
      data_buf[i]=ch;
    }
  }
  nrf_gpio_cfg_input(DS18B20PIN, NRF_GPIO_PIN_NOPULL);
}

//Perform reset of the bus, and then wait for the presence pulse
bool ds18b20_reset_and_check(void)
{
  int res=0;
  //Form reset pulse
  
  nrf_gpio_cfg_output(DS18B20PIN);
  nrf_gpio_pin_write(DS18B20PIN,0);
  nrf_delay_us(500);
  //Release bus and wait 15-60MS
  nrf_gpio_cfg_input(DS18B20PIN, NRF_GPIO_PIN_NOPULL);
  nrf_delay_us(60);

  //Read from bus
  res=nrf_gpio_pin_read(DS18B20PIN);
  if(res==0)
  {
    nrf_delay_us(500);
    return true;
  }
  return false;
}
/**
 *@}
 **/

int16_t ds18b20_read_temp(void)
{
   char buf[16];
    //double f;
        //int i;
     int16_t raw_temp=0;

    ds18b20_reset_and_check();

//Read ROM
    buf[0]=0x33;
    ds18b20_write(&buf,1);

//Read the results
    
    ds18b20_read(&buf,8);
    memset(&buf,0,16);

    //Send convert TX cmd
    buf[0]=0x44; //Convert temp
    ds18b20_write(&buf,1);
    nrf_delay_ms(1000);//Wait for finishing of the conversion

    ds18b20_reset_and_check();
    buf[0]=0x33; //Read ROM
    ds18b20_write(&buf,1);
    //Read the results
    ds18b20_read(&buf,8);
    memset(&buf,0,16);
    buf[0]=0xBE;  //Read scratchpad
    ds18b20_write(&buf,1);
    //Read the results
    ds18b20_read(&buf,9);
    //raw_temp = (buf[1] << 4) | (buf[0] >> 4);
    raw_temp = (buf[1] << 8) | buf[0];

    //memcpy(&f,&buf,8);
    //f=(float)raw_temp / 16.0;
    return(raw_temp);
}

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);
    }
}



#ifdef ENABLE_LOOPBACK_TEST
/** @brief Function for setting the @ref ERROR_PIN high, and then enter an infinite loop.
 */
static void show_error(void)
{
    
    LEDS_ON(LEDS_MASK);
    while(true)
    {
        // Do nothing.
    }
}


/** @brief Function for testing UART loop back. 
 *  @details Transmitts one character at a time to check if the data received from the loopback is same as the transmitted data.
 *  @note  @ref TX_PIN_NUMBER must be connected to @ref RX_PIN_NUMBER)
 */
static void uart_loopback_test()
{
    uint8_t * tx_data = (uint8_t *)("\n\rLOOPBACK_TEST\n\r");
    uint8_t   rx_data;

    // Start sending one byte and see if you get the same
    for (uint32_t i = 0; i < MAX_TEST_DATA_BYTES; i++)
    {
        uint32_t err_code;
        while(app_uart_put(tx_data[i]) != NRF_SUCCESS);

        nrf_delay_ms(10);
        err_code = app_uart_get(&rx_data);

        if ((rx_data != tx_data[i]) || (err_code != NRF_SUCCESS))
        {
            show_error();
        }
    }
    return;
}


#endif


/**
 * @brief Function for main application entry.
 */
int main(void)
{
    //LEDS_CONFIGURE(LEDS_MASK);
    //LEDS_OFF(LEDS_MASK);
    //DS18B20 stuff
    uint32_t err_code;
    err_code = nrf_drv_clock_init();
    APP_ERROR_CHECK(err_code);
    int16_t raw_temp;

    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_Baud115200
      };

    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);
/*
#ifndef ENABLE_LOOPBACK_TEST
    printf("\n\rStart: \n\r");

    while (true)
    {
        uint8_t cr;
        while(app_uart_get(&cr) != NRF_SUCCESS);
        while(app_uart_put(cr) != NRF_SUCCESS);

        if (cr == 'q' || cr == 'Q')
        {
            printf(" \n\rExit!\n\r");

            while (true)
            {
                // Do nothing.
            }
        }
    }
#else

    // This part of the example is just for testing the loopback .
    while (true)
    {
        uart_loopback_test();
    }
#endif
*/
    printf("\n\rStart: \n\r");
  while(true)
  {
    raw_temp = ds18b20_read_temp();
    printf("%c%c\r\n", raw_temp >> 8, raw_temp & 0xff);
    //nrf_delay_ms(100);
    //app_uart_put(raw_temp >> 8);
    //app_uart_put(raw_temp & 0xff);
    //printf("read temp: %i.%i \r\n", raw_temp >> 4, (int)(((raw_temp & 0xf) >> 0)*6.25));
  }


}


/** @} */

  • Hi,

    To retarget the printf over UART you need to add retarget.c file to the project. See the UART example for reference code.

    regards

    Jared 

  • The retarget.c is included as you can see in Makefile. And the initial "Start: " is output correctly.

    PROJECT_NAME := uart_pca10028
    
    export OUTPUT_FILENAME
    #MAKEFILE_NAME := $(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
    MAKEFILE_NAME := $(MAKEFILE_LIST)
    MAKEFILE_DIR := $(dir $(MAKEFILE_NAME) ) 
    
    TEMPLATE_PATH = ../../../../../components/toolchain/gcc
    ifeq ($(OS),Windows_NT)
    include $(TEMPLATE_PATH)/Makefile.windows
    else
    include $(TEMPLATE_PATH)/Makefile.posix
    endif
    
    MK := mkdir
    RM := rm -rf
    
    #echo suspend
    ifeq ("$(VERBOSE)","1")
    NO_ECHO := 
    else
    NO_ECHO := @
    endif
    
    # Toolchain commands
    CC              := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-gcc'
    AS              := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-as'
    AR              := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-ar' -r
    LD              := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-ld'
    NM              := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-nm'
    OBJDUMP         := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-objdump'
    OBJCOPY         := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-objcopy'
    SIZE            := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-size'
    
    #function for removing duplicates in a list
    remduplicates = $(strip $(if $1,$(firstword $1) $(call remduplicates,$(filter-out $(firstword $1),$1))))
    
    #source common to all targets
    C_SOURCE_FILES += \
    $(abspath ../../../../../components/toolchain/system_nrf51.c) \
    $(abspath ../../main.c) \
    $(abspath ../../../../../components/libraries/util/app_error.c) \
    $(abspath ../../../../../components/libraries/util/app_error_weak.c) \
    $(abspath ../../../../../components/libraries/fifo/app_fifo.c) \
    $(abspath ../../../../../components/libraries/timer/app_timer.c) \
    $(abspath ../../../../../components/drivers_nrf/clock/nrf_drv_clock.c) \
    $(abspath ../../../../../components/drivers_nrf/timer/nrf_drv_timer.c) \
    $(abspath ../../../../../components/libraries/util/app_util_platform.c) \
    $(abspath ../../../../../components/libraries/util/nrf_assert.c) \
    $(abspath ../../../../../components/libraries/uart/retarget.c) \
    $(abspath ../../../../../components/libraries/uart/app_uart_fifo.c) \
    $(abspath ../../../../../components/drivers_nrf/delay/nrf_delay.c) \
    $(abspath ../../../../../components/drivers_nrf/common/nrf_drv_common.c) \
    $(abspath ../../../../../components/drivers_nrf/uart/nrf_drv_uart.c) \
    
    #assembly files common to all targets
    ASM_SOURCE_FILES  = $(abspath ../../../../../components/toolchain/gcc/gcc_startup_nrf51.s)
    
    #includes common to all targets
    INC_PATHS  = -I$(abspath ../../config/uart_pca10028)
    INC_PATHS += -I$(abspath ../../config)
    INC_PATHS += -I$(abspath ../../../../bsp)
    INC_PATHS += -I$(abspath ../../../../../components/drivers_nrf/nrf_soc_nosd)
    INC_PATHS += -I$(abspath ../../../../../components/device)
    INC_PATHS += -I$(abspath ../../../../../components/libraries/uart)
    INC_PATHS += -I$(abspath ../../../../../components/libraries/timer)
    INC_PATHS += -I$(abspath ../../../../../components/drivers_nrf/clock)
    INC_PATHS += -I$(abspath ../../../../../components/drivers_nrf/timer)
    INC_PATHS += -I$(abspath ../../../../../components/drivers_nrf/hal)
    INC_PATHS += -I$(abspath ../../../../../components/drivers_nrf/delay)
    INC_PATHS += -I$(abspath ../../../../../components/toolchain/CMSIS/Include)
    INC_PATHS += -I$(abspath ../..)
    INC_PATHS += -I$(abspath ../../../../../components/libraries/util)
    INC_PATHS += -I$(abspath ../../../../../components/drivers_nrf/uart)
    INC_PATHS += -I$(abspath ../../../../../components/drivers_nrf/common)
    INC_PATHS += -I$(abspath ../../../../../components/toolchain)
    INC_PATHS += -I$(abspath ../../../../../components/drivers_nrf/config)
    INC_PATHS += -I$(abspath ../../../../../components/libraries/fifo)
    INC_PATHS += -I$(abspath ../../../../../components/toolchain/gcc)
    
    OBJECT_DIRECTORY = _build
    LISTING_DIRECTORY = $(OBJECT_DIRECTORY)
    OUTPUT_BINARY_DIRECTORY = $(OBJECT_DIRECTORY)
    
    # Sorting removes duplicates
    BUILD_DIRECTORIES := $(sort $(OBJECT_DIRECTORY) $(OUTPUT_BINARY_DIRECTORY) $(LISTING_DIRECTORY) )
    
    #flags common to all targets
    CFLAGS  = -DNRF51
    CFLAGS += -DBOARD_PCA10028
    CFLAGS += -DBLE_Nano
    CFLAGS += -DBSP_DEFINES_ONLY
    CFLAGS += -mcpu=cortex-m0
    CFLAGS += -mthumb -mabi=aapcs --std=gnu99
    CFLAGS += -Wall -Werror -O3 -g3
    CFLAGS += -mfloat-abi=soft
    # keep every function in separate section. This will allow linker to dump unused functions
    CFLAGS += -ffunction-sections -fdata-sections -fno-strict-aliasing
    CFLAGS += -fno-builtin --short-enums 
    # keep every function in separate section. This will allow linker to dump unused functions
    LDFLAGS += -Xlinker -Map=$(LISTING_DIRECTORY)/$(OUTPUT_FILENAME).map
    LDFLAGS += -mthumb -mabi=aapcs -L $(TEMPLATE_PATH) -T$(LINKER_SCRIPT)
    LDFLAGS += -mcpu=cortex-m0
    # let linker to dump unused sections
    LDFLAGS += -Wl,--gc-sections
    # use newlib in nano version
    LDFLAGS += --specs=nano.specs -lc -lnosys
    
    # Assembler flags
    ASMFLAGS += -x assembler-with-cpp
    ASMFLAGS += -DNRF51
    ASMFLAGS += -DBOARD_PCA10028
    ASMFLAGS += -DBSP_DEFINES_ONLY
    
    #default target - first one defined
    default: clean nrf51422_xxac
    
    #building all targets
    all: clean
    	$(NO_ECHO)$(MAKE) -f $(MAKEFILE_NAME) -C $(MAKEFILE_DIR) -e cleanobj
    	$(NO_ECHO)$(MAKE) -f $(MAKEFILE_NAME) -C $(MAKEFILE_DIR) -e nrf51422_xxac
    
    #target for printing all targets
    help:
    	@echo following targets are available:
    	@echo 	nrf51422_xxac
    
    C_SOURCE_FILE_NAMES = $(notdir $(C_SOURCE_FILES))
    C_PATHS = $(call remduplicates, $(dir $(C_SOURCE_FILES) ) )
    C_OBJECTS = $(addprefix $(OBJECT_DIRECTORY)/, $(C_SOURCE_FILE_NAMES:.c=.o) )
    
    ASM_SOURCE_FILE_NAMES = $(notdir $(ASM_SOURCE_FILES))
    ASM_PATHS = $(call remduplicates, $(dir $(ASM_SOURCE_FILES) ))
    ASM_OBJECTS = $(addprefix $(OBJECT_DIRECTORY)/, $(ASM_SOURCE_FILE_NAMES:.s=.o) )
    
    vpath %.c $(C_PATHS)
    vpath %.s $(ASM_PATHS)
    
    OBJECTS = $(C_OBJECTS) $(ASM_OBJECTS)
    
    nrf51422_xxac: OUTPUT_FILENAME := nrf51422_xxac
    nrf51422_xxac: LINKER_SCRIPT=uart_gcc_nrf51.ld
    
    nrf51422_xxac: $(BUILD_DIRECTORIES) $(OBJECTS)
    	@echo Linking target: $(OUTPUT_FILENAME).out
    	$(NO_ECHO)$(CC) $(LDFLAGS) $(OBJECTS) $(LIBS) -lm -o $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
    	$(NO_ECHO)$(MAKE) -f $(MAKEFILE_NAME) -C $(MAKEFILE_DIR) -e finalize
    
    ## Create build directories
    $(BUILD_DIRECTORIES):
    	echo $(MAKEFILE_NAME)
    	$(MK) $@
    
    # Create objects from C SRC files
    $(OBJECT_DIRECTORY)/%.o: %.c
    	@echo Compiling file: $(notdir $<)
    	$(NO_ECHO)$(CC) $(CFLAGS) $(INC_PATHS) -c -o $@ $<
    
    # Assemble files
    $(OBJECT_DIRECTORY)/%.o: %.s
    	@echo Assembly file: $(notdir $<)
    	$(NO_ECHO)$(CC) $(ASMFLAGS) $(INC_PATHS) -c -o $@ $<
    # Link
    $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out: $(BUILD_DIRECTORIES) $(OBJECTS)
    	@echo Linking target: $(OUTPUT_FILENAME).out
    	$(NO_ECHO)$(CC) $(LDFLAGS) $(OBJECTS) $(LIBS) -lm -o $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
    ## Create binary .bin file from the .out file
    $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).bin: $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
    	@echo Preparing: $(OUTPUT_FILENAME).bin
    	$(NO_ECHO)$(OBJCOPY) -O binary $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).bin
    
    ## Create binary .hex file from the .out file
    $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex: $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
    	@echo Preparing: $(OUTPUT_FILENAME).hex
    	$(NO_ECHO)$(OBJCOPY) -O ihex $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex
    
    finalize: genbin genhex echosize
    
    genbin:
    	@echo Preparing: $(OUTPUT_FILENAME).bin
    	$(NO_ECHO)$(OBJCOPY) -O binary $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).bin
    
    ## Create binary .hex file from the .out file
    genhex: 
    	@echo Preparing: $(OUTPUT_FILENAME).hex
    	$(NO_ECHO)$(OBJCOPY) -O ihex $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex
    echosize:
    	-@echo ''
    	$(NO_ECHO)$(SIZE) $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
    	-@echo ''
    
    clean:
    	$(RM) $(BUILD_DIRECTORIES)
    
    cleanobj:
    	$(RM) $(BUILD_DIRECTORIES)/*.o
    flash: nrf51422_xxac
    	@echo Flashing: $(OUTPUT_BINARY_DIRECTORY)/$<.hex
    	nrfjprog --program $(OUTPUT_BINARY_DIRECTORY)/$<.hex -f nrf51  --chiperase
    	nrfjprog --reset -f nrf51
    
    ## Flash softdevice
    
    

  • Hi,

    Max said:
    And the initial "Start: " is output correctly.

     What happens after? Does the application assert somewhere? Does it pass the second printf?

    regards

    Jared

  • Only 1-wire transactions are asserted on DS18B20PIN. There's nothing on TX pin.

  • Where exactly does it assert? What line? How does the call stack look like when it asserts? A screenshot from debug view would be good!

Related