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

Not able to see log output in COM port for SPI.

Hello,

I am using NRF51 DK for developing an application. I tried running the default SPI example in the SDK 12.3 which uses the spi and spis example. The location of those examples are SDK ->examples -> peripheral -> spi & spis. When I run these examples in two different boards I can see the led blinking showing that the spi transfer is completed but when I connect my COM port to Putty software I am not seeing any output in the log. I want to connect nRF51822 boards using SPI but I cannot figure out if they are working or not without the log. If you can please let me know how do I make the log work for this specific example.

Thank You.

Parents
  • By default, the logger module is not enabled in the examples. You need to set the following define in sdk_config.h:

    #define NRF_LOG_ENABLED 1

    Hein said:
    Not sure about the 51, but in the 52 you can not run the SPI0 and UART0 at the same time, are they both configured for instance 0 ?

    This is not correct, UART does not share instance with other serial devices. Only SPI/SPIM/SPIS/TWI/TWIM/TWIS share base address.

  • Unless you are using the SD Card/FatFS library, I would recommend that you control SS pin manually, rather than modifying the driver as suggested in the link.

    You cannot transmitt data from more than one device at a time on a single bus, then you need to setup multiple SPI instances. nRF51 support two SPI master instances. 

    The simples solution would be to configure the transmitter as a SPI master device and the 4 others as SPI slave. You can then set SS pin manually for each device and transmitt/receive from that device. Then move to the next device.

  • So for changing the SS pin manually do I not do the changes in the nrf_drv_spi.h and nrf_drv_spi.c file and what other changes do I have to do for it to work. Also, when 4 slaves are transmitting how do I manage the timing because they cannot transmit simultaneously right. Is there a way to know whether the other slave is currently using the SPI pin or not?

    Thank You.

  • Yes, no changes to the nrf_drv_spi files. You simply set and clear the SS pin in use with GPIO functions nrf_gpio_pin_set/clear(). Note that you have to setup the SS pins using nrf_gpio_cfg_output() first.

    The master is always the one initiating a transfer on the SPI bus. The slaves can not start transmitting by itself. You will have full control over which slave device is transferring in the master application.

  • Hello,

    I am really sorry for the trouble, I am a beginner, if you could please help me with a gist of code changes that I have to do in the SDK 12.3 spi and spis example that would be great.

    Thank You.

  • You should not have to modify the SPIS example, the slaves does not care which device it is interfaced by.

    I have attached main.c file that show how you can interface 4 different slaves that is connected to the same SPI instace for SPI master example: 

    /**
     * Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
     * 
     * All rights reserved.
     * 
     * Redistribution and use in source and binary forms, with or without modification,
     * are permitted provided that the following conditions are met:
     * 
     * 1. Redistributions of source code must retain the above copyright notice, this
     *    list of conditions and the following disclaimer.
     * 
     * 2. Redistributions in binary form, except as embedded into a Nordic
     *    Semiconductor ASA integrated circuit in a product or a software update for
     *    such product, must reproduce the above copyright notice, this list of
     *    conditions and the following disclaimer in the documentation and/or other
     *    materials provided with the distribution.
     * 
     * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
     *    contributors may be used to endorse or promote products derived from this
     *    software without specific prior written permission.
     * 
     * 4. This software, with or without modification, must only be used with a
     *    Nordic Semiconductor ASA integrated circuit.
     * 
     * 5. Any software provided in binary form under this license must not be reverse
     *    engineered, decompiled, modified and/or disassembled.
     * 
     * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
     * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
     * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
     * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     * 
     */
    
    #include "nrf_drv_spi.h"
    #include "app_util_platform.h"
    #include "nrf_gpio.h"
    #include "nrf_delay.h"
    #include "boards.h"
    #include "app_error.h"
    #include <string.h>
    #define NRF_LOG_MODULE_NAME "APP"
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    
    #define SPI_INSTANCE  0 /**< SPI instance index. */
    static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);  /**< SPI instance. */
    static volatile bool spi_xfer_done;  /**< Flag used to indicate that SPI instance completed the transfer. */
    
    #define NUMBER_OF_SLAVES 4
    uint8_t ss_pin_numbers[NUMBER_OF_SLAVES]	= {12, 13, 14, 15};
    
    #define TEST_STRING "Nordic"
    static uint8_t       m_tx_buf[] = TEST_STRING;           /**< TX buffer. */
    static uint8_t       m_rx_buf[sizeof(TEST_STRING) + 1];    /**< RX buffer. */
    static const uint8_t m_length = sizeof(m_tx_buf);        /**< Transfer length. */
    
    /**
     * @brief SPI user event handler.
     * @param event
     */
    void spi_event_handler(nrf_drv_spi_evt_t const * p_event)
    {
        spi_xfer_done = true;
        NRF_LOG_INFO("Transfer completed.\r\n");
        if (m_rx_buf[0] != 0)
        {
            NRF_LOG_INFO(" Received: \r\n");
            NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
        }
    		
    }
    
    int main(void)
    {
        bsp_board_leds_init();
    
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    
        NRF_LOG_INFO("SPI example\r\n");
    
        nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
        spi_config.ss_pin   = NRF_DRV_SPI_PIN_NOT_USED;
        spi_config.miso_pin = SPI_MISO_PIN;
        spi_config.mosi_pin = SPI_MOSI_PIN;
        spi_config.sck_pin  = SPI_SCK_PIN;
        APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler));
    
    		// Configuring GPIOs as output for all SPI SS pins
    		for(uint8_t i = 0; i < NUMBER_OF_SLAVES; i++)
    		{
    				nrf_gpio_cfg_output(ss_pin_numbers[i]);
    		}
    		
        while (1)
        {
            // Reset rx buffer and transfer done flag
    				for(uint8_t i = 0; i < NUMBER_OF_SLAVES; i++)
    				{
    						memset(m_rx_buf, 0, m_length);
    						spi_xfer_done = false;
    
    						nrf_gpio_pin_clear(ss_pin_numbers[i]); // Assert SS pin for current slave to initiate transfer
    					
    						APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
    
    						while (!spi_xfer_done)
    						{
    								__WFE();
    						}
    						nrf_gpio_pin_set(ss_pin_numbers[i]); // De-assert SS pin for current slave to complete transfer
    				}
            
    
            NRF_LOG_FLUSH();
    
            bsp_board_led_invert(BSP_BOARD_LED_0);
            nrf_delay_ms(200);
        }
    }
    

    The slave SS pins should be connected to GPIO P0.12-P0.15.

Reply
  • You should not have to modify the SPIS example, the slaves does not care which device it is interfaced by.

    I have attached main.c file that show how you can interface 4 different slaves that is connected to the same SPI instace for SPI master example: 

    /**
     * Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
     * 
     * All rights reserved.
     * 
     * Redistribution and use in source and binary forms, with or without modification,
     * are permitted provided that the following conditions are met:
     * 
     * 1. Redistributions of source code must retain the above copyright notice, this
     *    list of conditions and the following disclaimer.
     * 
     * 2. Redistributions in binary form, except as embedded into a Nordic
     *    Semiconductor ASA integrated circuit in a product or a software update for
     *    such product, must reproduce the above copyright notice, this list of
     *    conditions and the following disclaimer in the documentation and/or other
     *    materials provided with the distribution.
     * 
     * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
     *    contributors may be used to endorse or promote products derived from this
     *    software without specific prior written permission.
     * 
     * 4. This software, with or without modification, must only be used with a
     *    Nordic Semiconductor ASA integrated circuit.
     * 
     * 5. Any software provided in binary form under this license must not be reverse
     *    engineered, decompiled, modified and/or disassembled.
     * 
     * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
     * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
     * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
     * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     * 
     */
    
    #include "nrf_drv_spi.h"
    #include "app_util_platform.h"
    #include "nrf_gpio.h"
    #include "nrf_delay.h"
    #include "boards.h"
    #include "app_error.h"
    #include <string.h>
    #define NRF_LOG_MODULE_NAME "APP"
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    
    #define SPI_INSTANCE  0 /**< SPI instance index. */
    static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);  /**< SPI instance. */
    static volatile bool spi_xfer_done;  /**< Flag used to indicate that SPI instance completed the transfer. */
    
    #define NUMBER_OF_SLAVES 4
    uint8_t ss_pin_numbers[NUMBER_OF_SLAVES]	= {12, 13, 14, 15};
    
    #define TEST_STRING "Nordic"
    static uint8_t       m_tx_buf[] = TEST_STRING;           /**< TX buffer. */
    static uint8_t       m_rx_buf[sizeof(TEST_STRING) + 1];    /**< RX buffer. */
    static const uint8_t m_length = sizeof(m_tx_buf);        /**< Transfer length. */
    
    /**
     * @brief SPI user event handler.
     * @param event
     */
    void spi_event_handler(nrf_drv_spi_evt_t const * p_event)
    {
        spi_xfer_done = true;
        NRF_LOG_INFO("Transfer completed.\r\n");
        if (m_rx_buf[0] != 0)
        {
            NRF_LOG_INFO(" Received: \r\n");
            NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
        }
    		
    }
    
    int main(void)
    {
        bsp_board_leds_init();
    
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    
        NRF_LOG_INFO("SPI example\r\n");
    
        nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
        spi_config.ss_pin   = NRF_DRV_SPI_PIN_NOT_USED;
        spi_config.miso_pin = SPI_MISO_PIN;
        spi_config.mosi_pin = SPI_MOSI_PIN;
        spi_config.sck_pin  = SPI_SCK_PIN;
        APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler));
    
    		// Configuring GPIOs as output for all SPI SS pins
    		for(uint8_t i = 0; i < NUMBER_OF_SLAVES; i++)
    		{
    				nrf_gpio_cfg_output(ss_pin_numbers[i]);
    		}
    		
        while (1)
        {
            // Reset rx buffer and transfer done flag
    				for(uint8_t i = 0; i < NUMBER_OF_SLAVES; i++)
    				{
    						memset(m_rx_buf, 0, m_length);
    						spi_xfer_done = false;
    
    						nrf_gpio_pin_clear(ss_pin_numbers[i]); // Assert SS pin for current slave to initiate transfer
    					
    						APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
    
    						while (!spi_xfer_done)
    						{
    								__WFE();
    						}
    						nrf_gpio_pin_set(ss_pin_numbers[i]); // De-assert SS pin for current slave to complete transfer
    				}
            
    
            NRF_LOG_FLUSH();
    
            bsp_board_led_invert(BSP_BOARD_LED_0);
            nrf_delay_ms(200);
        }
    }
    

    The slave SS pins should be connected to GPIO P0.12-P0.15.

Children
No Data
Related