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

Replace HAL function (+SPI)

Hello

I am currently porting nRF52833 using SPI communication with other MCU (AD5940).

AD5940 provides an example of porting with stm32, which we are referring to.

Currently, no function is found to replace the Clock Enable used by stm32.

Can I get some help? Related photos are as follows.

----------------------------The code in the example.

  

   

 I'm sorry I brought you a question about another MCU..!

Best regards.

  • You need to separate-out what's specific to STM32 from what is independent of the microcontroller used.

    There's no point in looking at the STM32-specific code - RCCs and such - that's not relevant to nRF.

    You can look at Nordic's examples for the nRF52-specific details of getting its SPI hardware working.

    not as an image!

  • nRF52833 using SPI communication with other MCU (AD5940)

    Which one is the master, and which is the slave?

  • I'm sorry. Nordic is master, the other MCU is slave.

     The following is the current status of my code except for stm specific code like RCC.

    /**
     * Copyright (c) 2015 - 2020, 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 "AD5940.h"
    #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>
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    #include <nrf52840.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 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. */
    
    
    uint32_t MCUPlatformInit(void *pCfg);
    
    int main(void)
    {
        bsp_board_init(BSP_INIT_LEDS);
    	  void AD5940_Main(void);
        MCUPlatformInit(0);
    	  AD5940_MCUResourceInit(0);
        AD5940_Main();
    	
        NRF_LOG_INFO("SPI example started.");
    
        while (1)
        {
            // Reset rx buffer and transfer done flag
            memset(m_rx_buf, 0, m_length);
            spi_xfer_done = false;
    
            APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
    
            while (!spi_xfer_done)
            {
                __WFE();
            }
    
            NRF_LOG_FLUSH();
    
            bsp_board_led_invert(BSP_BOARD_LED_0);
            nrf_delay_ms(200);
        }
    
    }
    
    
    uint32_t MCUPlatformInit(void *pCfg)
    {
    }
    -- main.c

    #include "ad5940.h"
    #include <nrf52840.h>
    
    
    #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>
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.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 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. */
    
    
        
    
    #define AD5940SPI                          NRF_SPIM0
    #define AD5940_MISO_PIN                    SPI_MISO_PIN
    
    #define AD5940_MOSI_PIN                    SPI_MOSI_PIN
    
    #define AD5940_CS_PIN                      SPI_SS_PIN
    
    #define AD5940_RST_PIN                     SPI_RESET_PIN
    
    #define AD5940_GP0INT_PIN                  SPI_RX_PIN
    
    #define AD5940_GP0INT_IRQn                 SPI_IRQ_PRIORITY
    
    
    
    void spi_event_handler(nrf_drv_spi_evt_t const * p_event,
                           void *                    p_context)
    {
        spi_xfer_done = true;
        NRF_LOG_INFO("Transfer completed.");
        if (m_rx_buf[0] != 0)
        {
            NRF_LOG_INFO(" Received:");
            NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
        }
    }
    /*
    #define AD5940_CLK_ENABLE()                __HAL_RCC_SPI1_CLK_ENABLE()
    #define AD5940_SCK_GPIO_CLK_ENABLE()       spi_config.sck_pin
    #define AD5940_MISO_GPIO_CLK_ENABLE()      spi_config.miso_pin
    #define AD5940_MOSI_GPIO_CLK_ENABLE()      spi_config.mosi_pin
    #define AD5940_CS_GPIO_CLK_ENABLE()        spi_config.ss_pin
    #define AD5940_RST_GPIO_CLK_ENABLE()       __HAL_RCC_GPIOB_CLK_ENABLE()
    #define AD5940_GP0INT_GPIO_CLK_ENABLE()    __HAL_RCC_GPIOA_CLK_ENABLE()
    
    #define AD5940SPI_FORCE_RESET()               __HAL_RCC_SPI1_FORCE_RESET()
    #define AD5940SPI_RELEASE_RESET()             __HAL_RCC_SPI1_RELEASE_RESET()
    */
    
    
    
    
    
    
    #define SYSTICK_MAXCOUNT ((1L<<24)-1) /* we use Systick to complete function Delay10uS(). This value only applies to ADICUP3029 board. */
    #define SYSTICK_CLKFREQ   26000000L   /* Systick clock frequency in Hz. This only appies to ADICUP3029 board */
    volatile static uint32_t ucInterrupted = 0;       /* Flag to indicate interrupt occurred */
    
    /**
    	@brief Using SPI to transmit N bytes and return the received bytes. This function targets to 
             provide a more efficient way to transmit/receive data.
    	@param pSendBuffer :{0 - 0xFFFFFFFF}
          - Pointer to the data to be sent.
    	@param pRecvBuff :{0 - 0xFFFFFFFF}
          - Pointer to the buffer used to store received data.
    	@param length :{0 - 0xFFFFFFFF}
          - Data length in SendBuffer.
    	@return None.
    **/
    void AD5940_ReadWriteNBytes(unsigned char *pSendBuffer,unsigned char *pRecvBuff,unsigned long length)
    {               
    	              nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length);
    }
    
    void AD5940_CsClr(void)
    {
    	    nrf_gpio_cfg_output(AD5940_CS_PIN);
          nrf_gpio_pin_clear(AD5940_CS_PIN);
    }
    
    void AD5940_CsSet(void)
    {
    	    nrf_gpio_cfg_output(AD5940_CS_PIN);
          nrf_gpio_pin_set(AD5940_CS_PIN);
    }
    
    void AD5940_RstSet(void)
    {     
    	    nrf_gpio_cfg_output(AD5940_RST_PIN);
          nrf_gpio_pin_set(AD5940_RST_PIN);
    }
    
    void AD5940_RstClr(void)
    {  
    	    nrf_gpio_cfg_output(AD5940_RST_PIN);
          nrf_gpio_pin_clear(AD5940_RST_PIN);
    }
    
    void AD5940_Delay10us(uint32_t time)
    {
      if(time==0)return;
      if(time*10<SYSTICK_MAXCOUNT/(SYSTICK_CLKFREQ/1000000)){
        SysTick->LOAD = time*10*(SYSTICK_CLKFREQ/1000000);
        SysTick->CTRL = (1 << 2) | (1<<0);    /* Enable SysTick Timer, using core clock */
        while(!((SysTick->CTRL)&(1<<16)));    /* Wait until count to zero */
        SysTick->CTRL = 0;                    /* Disable SysTick Timer */
      }
      else {
        AD5940_Delay10us(time/2);
        AD5940_Delay10us(time/2 + (time&1));
      }
    }
    
    uint32_t AD5940_GetMCUIntFlag(void)
    {
       return ucInterrupted;
    }
    
    uint32_t AD5940_ClrMCUIntFlag(void)
    {
      
       ucInterrupted = 0;
       return 1;
    }
    
    /* Functions that used to initialize MCU platform */
    
    uint32_t AD5940_MCUResourceInit(void *pCfg)
    {
    	
      nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
                    spi_config.ss_pin   = SPI_SS_PIN;
                    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, NULL));
    
    	            
    
      return 0;
    }
    
    /* MCU related external line interrupt service routine */
    //
    void Ext_Int0_Handler()
    {
    //	NRF_SPIS0
    //  pADI_XINT0->CLR = BITM_XINT_CLR_IRQ0;
    //  ucInterrupted = 1;
     /* This example just set the flag and deal with interrupt in AD5940Main function. It's your choice to choose how to process interrupt. */
       nrfx_spim_0_irq_handler();
    
    }
    
    ---porting.c

    The following is stm 32 code.

    
    */
    #include "stdio.h"
    #include "AD5940.h"
    #include "stm32f4xx_hal.h"
    
    /* Functions that used to initialize MCU platform */
    uint32_t MCUPlatformInit(void *pCfg);
    
    int main(void)
    {
      void AD5940_Main(void);
      MCUPlatformInit(0);
      AD5940_MCUResourceInit(0);    /* Initialize resources that AD5940 use, like SPI/GPIO/Interrupt. */
    
      printf("Hello AD5940-Build Time:%s\n",__TIME__);
      AD5940_Main();
    }
    
    #define DEBUG_UART                         USART2
    #define DEBUG_UART_IRQN                    USART2_IRQn
    #define DEBUGUART_CLK_ENABLE()             __HAL_RCC_USART2_CLK_ENABLE()
    #define DEBUGUART_GPIO_CLK_ENABLE()        __HAL_RCC_GPIOA_CLK_ENABLE()
    
    /* Definition for AD5940 Pins */
    #define DEBUGUART_TX_PIN                   GPIO_PIN_2
    #define DEBUGUART_TX_GPIO_PORT             GPIOA
    #define DEBUGUART_TX_AF                    GPIO_AF7_USART2
    
    #define DEBUGUART_RX_PIN                   GPIO_PIN_3
    #define DEBUGUART_RX_GPIO_PORT             GPIOA
    #define DEBUGUART_RX_AF                    GPIO_AF7_USART2
    
    UART_HandleTypeDef UartHandle;
    
    void Error_Handler(void){
      while(1);
    }
    /**
      * @brief SPI MSP Initialization 
      *        This function configures the hardware resources used in this example: 
      *           - Peripheral's clock enable
      *           - Peripheral's GPIO Configuration  
      * @param husart: SPI handle pointer
      * @retval None
      */
    void HAL_UART_MspInit(UART_HandleTypeDef *husart)
    {
      GPIO_InitTypeDef  GPIO_InitStruct;
    
      if(husart->Instance == DEBUG_UART)
      {     
        /*##-1- Enable peripherals and GPIO Clocks #################################*/
        /* Enable GPIO TX/RX clock */
        DEBUGUART_GPIO_CLK_ENABLE();
        /* Enable UART clock */
        DEBUGUART_CLK_ENABLE();
        
        /*##-2- Configure peripheral GPIO ##########################################*/  
        /* UART TX GPIO pin configuration  */
        GPIO_InitStruct.Pin       = DEBUGUART_TX_PIN;
        GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull      = GPIO_PULLUP;
        GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_VERY_HIGH;
        GPIO_InitStruct.Alternate = DEBUGUART_TX_AF;
        HAL_GPIO_Init(DEBUGUART_TX_GPIO_PORT, &GPIO_InitStruct);
    
        /* UART RX GPIO pin configuration  */
        GPIO_InitStruct.Pin = DEBUGUART_RX_PIN;
        GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Alternate = DEBUGUART_RX_AF;
        HAL_GPIO_Init(DEBUGUART_RX_GPIO_PORT, &GPIO_InitStruct);
      }
    }
    
    /**
      * @brief System Clock Configuration
      * @retval None
      */
    void SystemClock_Config(void)
    {
      RCC_OscInitTypeDef RCC_OscInitStruct = {0};
      RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
    
      /** Configure the main internal regulator output voltage 
      */
      __HAL_RCC_PWR_CLK_ENABLE();
      __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
      /** Initializes the CPU, AHB and APB busses clocks 
      */
      RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
      RCC_OscInitStruct.HSIState = RCC_HSI_ON;
      RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
      RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
      RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
      RCC_OscInitStruct.PLL.PLLM = 8;
      RCC_OscInitStruct.PLL.PLLN = 100;
      RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
      RCC_OscInitStruct.PLL.PLLQ = 4;
      if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
      {
        Error_Handler();
      }
      /** Initializes the CPU, AHB and APB busses clocks 
      */
      RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                                  |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
      RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
      RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
      RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
      RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
    
      if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
      {
        Error_Handler();
      }
    }
    
    uint32_t MCUPlatformInit(void *pCfg)
    {
      HAL_Init();
      SystemClock_Config();
      HAL_Init();
      /* Init UART */  
      UartHandle.Instance        = DEBUG_UART;
    
      UartHandle.Init.BaudRate   = 230400;
      UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
      UartHandle.Init.StopBits   = UART_STOPBITS_1;
      UartHandle.Init.Parity     = UART_PARITY_NONE;
      UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
      UartHandle.Init.Mode       = UART_MODE_TX_RX;
      UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
      if(HAL_UART_Init(&UartHandle) != HAL_OK)
      {
        /* Initialization Error */
        return 0;
      }
      __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_RXNE);
      HAL_NVIC_EnableIRQ(DEBUG_UART_IRQN);
    	return 1;
    }
    
    void USART2_IRQHandler(void)
    {
      //void UARTCmd_Process(char);
      volatile char c;
      if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE))
      {
        c = USART2->DR;
        //UARTCmd_Process(c);
      }
    }
    
    #include "stdio.h"
    #ifdef __ICCARM__
    int putchar(int c)
    #else
    int fputc(int c, FILE *f)
    #endif
    {
      uint8_t t = c;
      HAL_UART_Transmit(&UartHandle, &t, 1, 1000); 
      return c;
    }
    
    /**
      * @brief  This function handles SysTick Handler.
      * @param  None
      * @retval None
      */
    void SysTick_Handler(void)
    {
      HAL_IncTick();
    }
    --- main.c

    #include "ad5940.h"
    #include "stdio.h"
    #include "stm32f4xx_hal.h"
    
    /* Definition for STM32 SPI clock resources */
    #define AD5940SPI                          SPI1
    #define AD5940_CLK_ENABLE()                __HAL_RCC_SPI1_CLK_ENABLE()
    #define AD5940_SCK_GPIO_CLK_ENABLE()       __HAL_RCC_GPIOA_CLK_ENABLE()
    #define AD5940_MISO_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOA_CLK_ENABLE()
    #define AD5940_MOSI_GPIO_CLK_ENABLE()      __HAL_RCC_GPIOA_CLK_ENABLE()
    #define AD5940_CS_GPIO_CLK_ENABLE()        __HAL_RCC_GPIOB_CLK_ENABLE()
    #define AD5940_RST_GPIO_CLK_ENABLE()       __HAL_RCC_GPIOB_CLK_ENABLE()
    #define AD5940_GP0INT_GPIO_CLK_ENABLE()    __HAL_RCC_GPIOA_CLK_ENABLE()
    
    #define AD5940SPI_FORCE_RESET()               __HAL_RCC_SPI1_FORCE_RESET()
    #define AD5940SPI_RELEASE_RESET()             __HAL_RCC_SPI1_RELEASE_RESET()
    
    /* Definition for AD5940 Pins */
    #define AD5940_SCK_PIN                     GPIO_PIN_5
    #define AD5940_SCK_GPIO_PORT               GPIOA
    #define AD5940_SCK_AF                      GPIO_AF5_SPI1
    #define AD5940_MISO_PIN                    GPIO_PIN_6
    #define AD5940_MISO_GPIO_PORT              GPIOA
    #define AD5940_MISO_AF                     GPIO_AF5_SPI1
    #define AD5940_MOSI_PIN                    GPIO_PIN_7
    #define AD5940_MOSI_GPIO_PORT              GPIOA
    #define AD5940_MOSI_AF                     GPIO_AF5_SPI1
    
    #define AD5940_CS_PIN                      GPIO_PIN_6
    #define AD5940_CS_GPIO_PORT                GPIOB
    
    #define AD5940_RST_PIN                     GPIO_PIN_0   //A3
    #define AD5940_RST_GPIO_PORT               GPIOB
    
    #define AD5940_GP0INT_PIN                  GPIO_PIN_10   //A3
    #define AD5940_GP0INT_GPIO_PORT            GPIOA
    #define AD5940_GP0INT_IRQn                 EXTI15_10_IRQn
    
    SPI_HandleTypeDef  SpiHandle;
    
    #define SYSTICK_MAXCOUNT ((1L<<24)-1) /* we use Systick to complete function Delay10uS(). This value only applies to NUCLEOF411 board. */
    #define SYSTICK_CLKFREQ   100000000L  /* Systick clock frequency in Hz. This only appies to NUCLEOF411 board */
    volatile static uint8_t ucInterrupted = 0;       /* Flag to indicate interrupt occurred */
    
    /**
    	@brief Using SPI to transmit N bytes and return the received bytes. This function targets to 
                         provide a more efficent way to transmit/receive data.
    	@param pSendBuffer :{0 - 0xFFFFFFFF}
          - Pointer to the data to be sent.
    	@param pRecvBuff :{0 - 0xFFFFFFFF}
          - Pointer to the buffer used to store received data.
    	@param length :{0 - 0xFFFFFFFF}
          - Data length in SendBuffer.
    	@return None.
    **/
    void AD5940_ReadWriteNBytes(unsigned char *pSendBuffer,unsigned char *pRecvBuff,unsigned long length)
    {
      HAL_SPI_TransmitReceive(&SpiHandle, pSendBuffer, pRecvBuff, length, (uint32_t)-1);
    }
    
    void AD5940_CsClr(void)
    {
      HAL_GPIO_WritePin(AD5940_CS_GPIO_PORT, AD5940_CS_PIN, GPIO_PIN_RESET);
    }
    
    void AD5940_CsSet(void)
    {
      HAL_GPIO_WritePin(AD5940_CS_GPIO_PORT, AD5940_CS_PIN, GPIO_PIN_SET);
    }
    
    void AD5940_RstSet(void)
    {
      HAL_GPIO_WritePin(AD5940_RST_GPIO_PORT, AD5940_RST_PIN, GPIO_PIN_SET);
    }
    
    void AD5940_RstClr(void)
    {
      HAL_GPIO_WritePin(AD5940_RST_GPIO_PORT, AD5940_RST_PIN, GPIO_PIN_RESET);
    }
    
    void AD5940_Delay10us(uint32_t time)
    {
      time/=100;
      if(time == 0) time =1;
      HAL_Delay(time);
    }
    
    uint32_t AD5940_GetMCUIntFlag(void)
    {
    	return ucInterrupted;
    }
    
    uint32_t AD5940_ClrMCUIntFlag(void)
    {
    	ucInterrupted = 0;
    	return 1;
    }
    
    uint32_t AD5940_MCUResourceInit(void *pCfg)
    {
      GPIO_InitTypeDef  GPIO_InitStruct;
      
      /* Step1, initialize SPI peripheral and its GPIOs for CS/RST */
      AD5940_SCK_GPIO_CLK_ENABLE();
      AD5940_MISO_GPIO_CLK_ENABLE();
      AD5940_MOSI_GPIO_CLK_ENABLE();
      AD5940_CS_GPIO_CLK_ENABLE();
      AD5940_RST_GPIO_CLK_ENABLE();
      /* Enable SPI clock */
      AD5940_CLK_ENABLE(); 
      
      GPIO_InitStruct.Pin       = AD5940_SCK_PIN;
      GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP; //toggle mode?
      GPIO_InitStruct.Pull      = GPIO_PULLDOWN;
      GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_VERY_HIGH;
      GPIO_InitStruct.Alternate = AD5940_SCK_AF;
      HAL_GPIO_Init(AD5940_SCK_GPIO_PORT, &GPIO_InitStruct);
      
      /* SPI MISO GPIO pin configuration  */
      GPIO_InitStruct.Pin = AD5940_MISO_PIN;
      GPIO_InitStruct.Alternate = AD5940_MISO_AF;
      HAL_GPIO_Init(AD5940_MISO_GPIO_PORT, &GPIO_InitStruct);
      
      /* SPI MOSI GPIO pin configuration  */
      GPIO_InitStruct.Pin = AD5940_MOSI_PIN;
      GPIO_InitStruct.Alternate = AD5940_MOSI_AF;
      HAL_GPIO_Init(AD5940_MOSI_GPIO_PORT, &GPIO_InitStruct);
      /* SPI CS GPIO pin configuration  */
      GPIO_InitStruct.Pin = AD5940_CS_PIN;
      GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
      HAL_GPIO_Init(AD5940_CS_GPIO_PORT, &GPIO_InitStruct);
      
      /* SPI RST GPIO pin configuration  */
      GPIO_InitStruct.Pin = AD5940_RST_PIN;
      HAL_GPIO_Init(AD5940_RST_GPIO_PORT, &GPIO_InitStruct);
      
      AD5940_CsSet();
      AD5940_RstSet();
      
      /* Set the SPI parameters */
      SpiHandle.Instance               = AD5940SPI;
      SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; //SPI clock should be < AD5940_SystemClock
      SpiHandle.Init.Direction         = SPI_DIRECTION_2LINES;
      SpiHandle.Init.CLKPhase          = SPI_PHASE_1EDGE;
      SpiHandle.Init.CLKPolarity       = SPI_POLARITY_LOW;
      SpiHandle.Init.DataSize          = SPI_DATASIZE_8BIT;
      SpiHandle.Init.FirstBit          = SPI_FIRSTBIT_MSB;
      SpiHandle.Init.TIMode            = SPI_TIMODE_DISABLE;
      SpiHandle.Init.CRCCalculation    = SPI_CRCCALCULATION_DISABLE;
      SpiHandle.Init.CRCPolynomial     = 7;
      SpiHandle.Init.NSS               = SPI_NSS_SOFT;
      SpiHandle.Init.Mode = SPI_MODE_MASTER;
      HAL_SPI_Init(&SpiHandle);
      
      /* Step 2: Configure external interrupot line */
      AD5940_GP0INT_GPIO_CLK_ENABLE();
      GPIO_InitStruct.Pin       = AD5940_GP0INT_PIN;
      GPIO_InitStruct.Mode      = GPIO_MODE_IT_FALLING;
      GPIO_InitStruct.Pull      = GPIO_PULLUP;
      GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_VERY_HIGH;
      GPIO_InitStruct.Alternate = 0;
      HAL_GPIO_Init(AD5940_GP0INT_GPIO_PORT, &GPIO_InitStruct);
      
      /* Enable and set EXTI Line0 Interrupt to the lowest priority */
      HAL_NVIC_EnableIRQ(AD5940_GP0INT_IRQn);
    //  HAL_NVIC_SetPriority(AD5940_GP0INT_IRQn, 0, 0);
      return 0;
    }
    
    /* MCU related external line interrupt service routine */
    void EXTI15_10_IRQHandler()
    {
      ucInterrupted = 1;
      __HAL_GPIO_EXTI_CLEAR_IT(AD5940_GP0INT_PIN);
    }
    
    
    --- porting.c

    The reason why I try to modify the part of STM32 is that I'm not a skilled person and it is the same core 4.

    I'm trying to mix Nordic's SPI example with STM example.

    I think I got the pin number right now, so I'm going to modify the GPIO clock and irq handler.

    Thank you for your reply.

    Best regards.

  • Nordic is master

    So look at the SPI Master example in the SDK to see how to use the nRF as an SPI Master.

  • Hi.
    I'm trying to understand as much as I can by looking at the SPI master example.

    I'm converting the Stm32 example codes to the nrf format.

    Then I got a question, so can you answer it?

    This picture is a system architecture of stm32. This picture is trying to use USART1 and the corresponding APB2 will be given a clark signal.  The code for this role is "RCC->APB2ENR |= RCC_APB2Periph_USART1;"

    Is there a code that plays this role in nrf?

    Best regards.

Related