0

To continue of this question...

I figured out the main problem and now I have an odd problem. at first, let me to put the codes.

"Main.c":

/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_hal.h"

/* Private variables ---------------------------------------------------------*/
SPI_HandleTypeDef hspi1;

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);

int main(void)
{
    uint8_t aTxBuffer[] = "Rohalamin";

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* System interrupt init*/
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_SPI1_Init();

  /* USER CODE BEGIN 2 */
    HAL_GPIO_WritePin( GPIOA , GPIO_PIN_4 , GPIO_PIN_SET );
  /* USER CODE END 2 */

  /* USER CODE BEGIN 3 */
  /* Infinite loop */
  while (1)
  {
        HAL_GPIO_WritePin( GPIOA , GPIO_PIN_4 , GPIO_PIN_RESET );
        HAL_SPI_Transmit_IT( &hspi1 , (uint8_t*)aTxBuffer , 10 );
        HAL_GPIO_WritePin( GPIOA , GPIO_PIN_4 , GPIO_PIN_SET );

  }
  /* USER CODE END 3 */

}

/** System Clock Configuration
*/
void SystemClock_Config(void)
{

  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_OscInitTypeDef RCC_OscInitStruct;

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
  RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);

  __SYSCFG_CLK_ENABLE();

}

/* SPI1 init function */
void MX_SPI1_Init(void)
{

    HAL_SPI_MspInit(&hspi1);

  hspi1.Instance = SPI1;
  hspi1.Init.Mode = SPI_MODE_MASTER;
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
  HAL_SPI_Init(&hspi1);

}

/** Configure pins as 
        * Analog 
        * Input 
        * Output
        * EVENT_OUT
        * EXTI
*/
void MX_GPIO_Init(void)
{

  GPIO_InitTypeDef GPIO_InitStruct;

  /* GPIO Ports Clock Enable */
  __GPIOF_CLK_ENABLE();
  __GPIOA_CLK_ENABLE();

  /*Configure GPIO pin : PA0 */
  GPIO_InitStruct.Pin = GPIO_PIN_0;
  GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /*Configure GPIO pin : PA4 */
  GPIO_InitStruct.Pin = GPIO_PIN_4;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI0_1_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI0_1_IRQn);

}

"stm32f0xx_it.c":

/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_hal.h"
#include "stm32f0xx.h"
#include "stm32f0xx_it.h"

/* External variables --------------------------------------------------------*/

extern SPI_HandleTypeDef hspi1;

uint8_t aTxBuffer[] = "R";

/******************************************************************************/
/*            Cortex-M4 Processor Interruption and Exception Handlers         */ 
/******************************************************************************/

/**
* @brief This function handles System tick timer.
*/
void SysTick_Handler(void)
{
  HAL_IncTick();
  HAL_SYSTICK_IRQHandler();
}

/**
* @brief This function handles SPI1 global interrupt.
*/
void SPI1_IRQHandler(void)
{
  HAL_NVIC_ClearPendingIRQ(SPI1_IRQn);
  HAL_SPI_IRQHandler(&hspi1);
}

/**
* @brief This function handles EXTI Line 0 and Line 1 interrupts.
*/
void EXTI0_1_IRQHandler(void)
{
  HAL_NVIC_ClearPendingIRQ(EXTI0_1_IRQn);
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
    HAL_SPI_Transmit_IT( &hspi1 , (uint8_t*)aTxBuffer , 2 );
}

"stm32f0xx_hal_msp.c":

/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_hal.h"

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{

  GPIO_InitTypeDef GPIO_InitStruct;
  if(hspi->Instance==SPI1)
  {
    /* Peripheral clock enable */
    __SPI1_CLK_ENABLE();

    /**SPI1 GPIO Configuration    
    PA4     ------> SPI1_NSS
    PA5     ------> SPI1_SCK
    PA6     ------> SPI1_MISO
    PA7     ------> SPI1_MOSI 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* Peripheral interrupt init*/
    HAL_NVIC_SetPriority(SPI1_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(SPI1_IRQn);
  }

}

void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
{

  if(hspi->Instance==SPI1)
  {
    /* Peripheral clock disable */
    __SPI1_CLK_DISABLE();

    /**SPI1 GPIO Configuration    
    PA4     ------> SPI1_NSS
    PA5     ------> SPI1_SCK
    PA6     ------> SPI1_MISO
    PA7     ------> SPI1_MOSI 
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);

    /* Peripheral interrupt Deinit*/
    HAL_NVIC_DisableIRQ(SPI1_IRQn);
  }

}

and look at my saleae result:

figure1

I seperated each 8bit. if you consider those 10 bytes are "Rohalamin"+0 but there are two problem.

1- Why cannot the saleae identify the "Rohalamin"+0?

2- There are so much pulses on the Enable line. Why? What are they? are they noise?

Roh
  • 4,598
  • 6
  • 41
  • 86
  • Did your linked question get answered? If this question relates a lot to that why create a new question? – Andy aka Jul 24 '14 at 18:03
  • @Andyaka Good question, as I said because I could to figure out the big problem (the big problem was that I couldn't see any correct clock or data) but it doesn't work still. now it works but not correctly as you can see. then because I see the new problems, I created a new question. – Roh Jul 24 '14 at 18:11
  • Are you trying to manually control the `nSS` pin? I assume this won't work, since SPI driver is implemented on the hardware level. – Ashton H. Jul 24 '14 at 18:34
  • I'll post everything else as an answer, since it's a lot of text to fit into comments – Ashton H. Jul 24 '14 at 18:37
  • @AshtonHearts Yes. No, I have changed it to software controle. look this part of my codes in "Main.c" -> "hspi1.Init.NSS = SPI_NSS_SOFT;" – Roh Jul 24 '14 at 18:37
  • @AshtonHearts Ok. – Roh Jul 24 '14 at 18:38

1 Answers1

2

There are a few things in your code that I'm concerned about.

First of all, you are using external interrupts for SPI, while transmitting manually. It requires an external interrupt source to trigger the interrupt (a button for example). In any case, external interrupt lines should never be left unconnected. I would suggest disabling interrupts for now, and enabling them later if needed.

Second - you've redefined aTxBuffer[] in your main routine that was previously defined in stm32f0xx_it.c

Third - you are controlling nSS pin in software. At the beginning of the transmission the pin should be pulled LOW. You've done that correctly. However, pulling the nSS pin HIGH in software will not work the way you've done it. You have to set an interrupt on the RXNE flag and pull nSS HIGH inside the interrupt. This way nSS will be HIGH as soon as SPI transmission ends.

Here is an example code with Standard Library (note that you might have to adjust it for your microcontroller):

// Enable interrupts on RXNE
SPI_I2S_ITConfig(SPI1,SPI_IT_RXNE,ENABLE);

...

// SPI1 interrupt handler
void SPI1_IRQHandler(void)
{
    if (SPI_GetITStatus(SPI1,SPI_IT_RXNE))
    {
        uint16_t temp = SPI_ReceiveData(SPI1); // Read SPI data and clear RXNE
        GPIOA->BSRRL = GPIO_Pin_4; // Set nSS pin (PA4) HIGH
    }
}

And finally - it would be helpful to have a schematic of what you're working with.

Ashton H.
  • 801
  • 7
  • 19