cancel
Showing results for 
Search instead for 
Did you mean: 

Hey everyone, for an IoT project I'm using an NUCLEO-L011K4. I need to use SPI to communicate with a RFM95 LoRa chip. For this I'm using Keil uvision, CubeMX and the HAL SPI library. I however can't manage to make it work. Can anyone help?

BKas.1
Associate

I think the issue lies within SPI not working correctly. I'm using the following function to transmit data using SPI.

void RFM_Write(unsigned char RFM_Address, unsigned char RFM_Data) 
{
	uint8_t spiData[2];
	spiData[0] = RFM_Address | 0x80;
	spiData[1] = RFM_Data;
  //Set NSS pin Low to start communication
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
 
  //Send Address with MSB 1 to make it a writ command and send data
  HAL_SPI_Transmit(&hspi1, &spiData[0], 1,10);
  HAL_SPI_Transmit(&hspi1, &spiData[1], 1,10);
 
  //Set NSS pin High to end communication
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
}

I've generated all the initiating code using CubeMX, so unless CubeMX isn't functioning correctly, all the setting up should be correct. I've checked by connecting the MOSI to an oscilloscope if data is being sent, and it doesn't seem to be the case. I've also checked if the clock or slave select is working, but they also don't seem to function properly.

I decided to try just setting an output pin high and low with a 100 ms delay to see if it gave me any results, which it didn't unless I am using the debugger in Keil. I used the following code.

int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
  
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
	
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_ADC_Init();
  MX_I2C1_Init();
  MX_SPI1_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
//  /* USER CODE END 2 */
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
		HAL_Delay(100);
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
		HAL_Delay(100);
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

Whenever the pin was supposed to be set or reset, it only made a single peak for about 1 ms, either being up when set or down when reset, and then went back to being neutral.

Does anyone know what might be going wrong that causes this to happen?

1 REPLY 1

Sorry not much of a CubeMX fan. Suggest you review the clock and GPIO pin initialization, check the peripheral settings via the debugger.

Check the SysTick clocking, or timing source behind HAL_Delay()

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..