cancel
Showing results for 
Search instead for 
Did you mean: 

Undefinded reference error at while(1)

JustSomeGuy
Associate III

Hey all,

I've been losing my mind over this error message that generates right on the while(1) line. it says "undefined reference to `SX1276' ", but SX1276 is defined in the PV section. Before, I defined it right after int main(void) {, but still got the same error. Any idea what's going on here?

Here is the main file:

 

 

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "radio.h"
#include "stdint.h"
#include "stdbool.h"
#include "stdio.h"
#include "JS_SX12xx.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
RTC_HandleTypeDef hrtc;

SPI_HandleTypeDef hspi1;

UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */
extern SX12xxRegisters SX1276;
uint16_t message[4] = {0xDE, 0xAD, 0xBE, 0xEF};
uint8_t FifoAddrPtr = 0;
uint32_t resetFIFOCommand = 0x0D00; //0x0D = address for FifoAddrPtr;

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_RTC_Init(void);
static void MX_SPI1_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */
static void SX1276_Init(SX12xxRegisters *workingSX1276);
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
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_RTC_Init();
  MX_SPI1_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  SX1276_Init(&SX1276);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	  //SX1276PowerUp();
	  //SX1276TXInit();
	  //writeDataFIFO(message);

  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_6;
  RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_3;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses 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_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_RTC;
  PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief RTC Initialization Function
  *  None
  * @retval None
  */
static void MX_RTC_Init(void)
{

  /* USER CODE BEGIN RTC_Init 0 */

  /* USER CODE END RTC_Init 0 */

  RTC_TimeTypeDef sTime = {0};
  RTC_DateTypeDef sDate = {0};
  RTC_AlarmTypeDef sAlarm = {0};

  /* USER CODE BEGIN RTC_Init 1 */

  /* USER CODE END RTC_Init 1 */

  /** Initialize RTC Only
  */
  hrtc.Instance = RTC;
  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  hrtc.Init.AsynchPrediv = 31;
  hrtc.Init.SynchPrediv = 1023;
  hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
  hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  if (HAL_RTC_Init(&hrtc) != HAL_OK)
  {
    Error_Handler();
  }

  /* USER CODE BEGIN Check_RTC_BKUP */

  /* USER CODE END Check_RTC_BKUP */

  /** Initialize RTC and set the Time and Date
  */
  sTime.Hours = 0;
  sTime.Minutes = 0;
  sTime.Seconds = 0;
  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }
  sDate.WeekDay = RTC_WEEKDAY_MONDAY;
  sDate.Month = RTC_MONTH_JANUARY;
  sDate.Date = 1;
  sDate.Year = 0;

  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }

  /** Enable the Alarm A
  */
  sAlarm.AlarmTime.Hours = 0;
  sAlarm.AlarmTime.Minutes = 0;
  sAlarm.AlarmTime.Seconds = 0;
  sAlarm.AlarmTime.SubSeconds = 0;
  sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
  sAlarm.AlarmMask = RTC_ALARMMASK_NONE;
  sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE;
  sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
  sAlarm.AlarmDateWeekDay = 1;
  sAlarm.Alarm = RTC_ALARM_A;
  if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN RTC_Init 2 */

  /* USER CODE END RTC_Init 2 */

}

/**
  * @brief SPI1 Initialization Function
  *  None
  * @retval None
  */
static void MX_SPI1_Init(void)
{

  /* USER CODE BEGIN SPI1_Init 0 */

  /* USER CODE END SPI1_Init 0 */

  /* USER CODE BEGIN SPI1_Init 1 */

  /* USER CODE END SPI1_Init 1 */
  /* SPI1 parameter configuration*/
  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_32;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi1.Init.CRCPolynomial = 7;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SPI1_Init 2 */

  /* USER CODE END SPI1_Init 2 */

}

/**
  * @brief USART2 Initialization Function
  *  None
  * @retval None
  */
static void MX_USART2_UART_Init(void)
{

  /* USER CODE BEGIN USART2_Init 0 */

  /* USER CODE END USART2_Init 0 */

  /* USER CODE BEGIN USART2_Init 1 */

  /* USER CODE END USART2_Init 1 */
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 115200;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART2_Init 2 */

  /* USER CODE END USART2_Init 2 */

}

/**
  * @brief GPIO Initialization Function
  *  None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, PA15_RESERVED_Pin|PA12_RESERVED_Pin|PA1_RESERVED_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOC, PC1_RESERVED_Pin|PC0_RESERVED_Pin|PC2_RESERVED_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pins : PA15_RESERVED_Pin PA12_RESERVED_Pin PA1_RESERVED_Pin */
  GPIO_InitStruct.Pin = PA15_RESERVED_Pin|PA12_RESERVED_Pin|PA1_RESERVED_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pins : PB4_RESERVED_Pin PB1_RESERVED_Pin PB0_RESERVED_Pin */
  GPIO_InitStruct.Pin = PB4_RESERVED_Pin|PB1_RESERVED_Pin|PB0_RESERVED_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /*Configure GPIO pin : PC13_RESERVED_Pin */
  GPIO_InitStruct.Pin = PC13_RESERVED_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(PC13_RESERVED_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pins : PC1_RESERVED_Pin PC0_RESERVED_Pin PC2_RESERVED_Pin */
  GPIO_InitStruct.Pin = PC1_RESERVED_Pin|PC0_RESERVED_Pin|PC2_RESERVED_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

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

  HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);

/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */
void writeDataFIFO(uint16_t *toSend)
{
	/*Steps to writing to FIFO:
	 * 1.  Set FifoPtrAddr to FifoTxPtrBase.
	 * 2.  Write PayloadLength bytes to the FIFO (RegFifo)
	 */
	int i = 0;
	uint16_t buffer[((sizeof(toSend)*8)+8)]; //add +8 to the message for the FIFO memory address
	buffer[0] = 0x00; //the address of the FIFO buffer

	//copy data to the buffer
	for(i = 1; i < sizeof(buffer); i++)
	{
		buffer[i] = toSend[i];
	}

	//reset the address pointer
	resetFIFOCommand = 0x0D00; //prepare the 2 LSB to be bitmasked with FifoTxBaseAddr
	resetFIFOCommand |= FifoTxBaseAddr;

		//@todo unsigned char will chop of MSB of FIFOcommand
	HAL_SPI_Transmit(&hspi1, (unsigned char *)resetFIFOCommand, sizeof((unsigned char *)resetFIFOCommand), HAL_MAX_DELAY);

	HAL_SPI_Transmit(&hspi1, buffer, sizeof(buffer), HAL_MAX_DELAY);
}

void SX1276_Init(SX12xxRegisters *SX12xx)
{
	//declare addresses
	SX12xx->Fifo.Addr = 0x00;
	SX12xx->OpMode.Addr = 0x01;
	SX12xx->carrierFrequency.AddrFrMsb = 0x06;
	SX12xx->carrierFrequency.AddrFrMid = 0x07;
	SX12xx->carrierFrequency.AddrFrLsb = 0x08;
	SX12xx->PaConfig.Addr = 0x09;
	SX12xx->PaRamp.Addr = 0x0A;
	SX12xx->Ocp.Addr = 0x0B;
	SX12xx->Lna.Addr = 0x0C;
	SX12xx->FifoAddrPtr.Addr = 0x0D;
	SX12xx->FifoTxBaseAddress.Addr = 0x0E;
	SX12xx->FifoRxBaseAddress.Addr = 0x0F;
	SX12xx->FifoRxCurrentAddress.Addr = 0x10;
	SX12xx->IrqFlagsMask.Addr = 0x11;
	SX12xx->IrgFlags.Addr = 0x12;
	SX12xx->RxNbBytes.Addr = 0x13;
	SX12xx->RxHeaderCntValueMsb.Addr = 0x14;
	SX12xx->RxHeaderCntValueLsb.Addr = 0x15;
	SX12xx->RxPacketCntValueMsb.Addr = 0x16;
	SX12xx->RxPacketCntValueLsb.Addr = 0x17;
	SX12xx->ModemStat.Addr = 0x18;
	SX12xx->PktSnrValue.Addr = 0x19;
	SX12xx->PktRssiValue.Addr=0x1A;
	SX12xx->RssiValue.Addr=0x1B;
	SX12xx->HopChannel.Addr=0x1C;
	SX12xx->ModemConfig1.Addr=0x1D;
	SX12xx->ModemConfig2.Addr=0x1E;
	SX12xx->SymbTimeoutLsb.Addr=0x1F;
	SX12xx->PreambleMsb.Addr=0x20;
	SX12xx->PreambleLsb.Addr=0x21;
	SX12xx->PayloadLength.Addr=0x22;
	SX12xx->MaxPayloadLength.Addr=0x23;
	SX12xx->HopPeriod.Addr=0x24;
	SX12xx->FifoRxByteAddr.Addr=0x25;
	SX12xx->ModemConfig3.Addr=0x26;
	SX12xx->PpmCorrection.Addr=0X027;
	SX12xx->Fei.AddrMsb=0x28;
	SX12xx->Fei.AddrMid=0x29;
	SX12xx->Fei.AddrLsb=0x2A;
	SX12xx->RssiWideBand.Addr=0x2C;
	SX12xx->IfFreq2.Addr=0x2F;
	SX12xx->IfFreq1.Addr=0x30;
	SX12xx->DetectOptimize.Addr=0x31;
	SX12xx->InvertIQ.Addr=0x33;
	SX12xx->HighBWOptimize1.Addr=0x36;
	SX12xx->DetectionThreshold.Addr=0x37;
	SX12xx->SyncWord.Addr=0x39;
	SX12xx->HighBWOptimize2.Addr=0x3A;
	SX12xx->InvertIQ2.Addr=0x3B;

	//assign initial values
	SX12xx->OpMode.Reg.LongRangeMode = 1;
	SX12xx->OpMode.Reg.AccessSharedReg = 0;
	SX12xx->OpMode.Reg.LowFrequencyModeOn = 0;
	SX12xx->OpMode.Reg.Mode = 0; //initial mode is sleep mode

	//@todo functionalize this
	SX12xx->carrierFrequency.desiredFrequency = carrierFr;
	SX12xx->carrierFrequency.step = 0;//@todo not 0
	SX12xx->carrierFrequency.frequencyOverStep = SX12xx->carrierFrequency.desiredFrequency / SX12xx->carrierFrequency.step;
	SX12xx->carrierFrequency.RegFrLsb = SX12xx->carrierFrequency.frequencyOverStep & 0xFF;
	SX12xx->carrierFrequency.RegFrMid = (SX12xx->carrierFrequency.frequencyOverStep >>  & 0xFF;
	SX12xx->carrierFrequency.RegFrMsb = (SX12xx->carrierFrequency.frequencyOverStep >> 16) & 0xFF;

}
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  *   file: pointer to the source file name
  *   line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

In the C language, when you have a variable declared in one source file (let's say main.c) and you want to use it in another source file (let's say module.c), you typically use the extern keyword to declare that variable in the other file.

// File: main.c

#include "main.h"

int var;

 

// File: module.c

#include "module.h"

extern int var;

 

Other another way is include an external declaration of var in main.h using the extern keyword like this:

// File: main.c

#include "main.h"

int var;

 

// File: main.h

extern int var;

 

// File: module.c

#include "module.h"

#include "mian.h"

 

 

This last option is useful when severval modules use same external var.

 

 

 

View solution in original post

9 REPLIES 9
JustSomeGuy
Associate III

Here is the custom header file I wrote. posting it here because the combined files exceeded the 20,000 character limit

 

/*
 * JS_Sx12xx.h
 *
 *  Created on: Apr 30, 2024
 *      Author: JonathanSera
 */

#ifndef INC_JS_SX12XX_H_
#define INC_JS_SX12XX_H_
#endif /* INC_JS_SX12XX_H_ */

typedef struct tagTypeInstructionAndAddress
{
	uint8_t Reg;
	uint8_t Addr;
}TypeInstructionAndAddress;

typedef struct tagTypeRegOpModeBits
{
	//LSB first
	uint8_t Mode: 3;
	uint8_t LowFrequencyModeOn: 1;
	uint8_t reserved: 2;
	uint8_t AccessSharedReg: 1;
	uint8_t LongRangeMode: 1;
} TypeRegOpModeBits;

typedef struct tagTypeRegPaConfigBits
{
	uint8_t OutputPower: 4;
	uint8_t MaxPower: 3;
	uint8_t PaSelect: 1;
}TypeRegPaConfigBits;

typedef struct tagTypeRegPaConfig
{
	TypeRegPaConfigBits Reg;
	uint8_t Addr;
}TypeRegPaConfig;

typedef struct tagTypeRegOpModeInstrAndAddr
{
	TypeRegOpModeBits Reg;
	uint8_t Addr;
}TypeRegOpModeInstrAndAddr;

typedef struct tagTypeRegFr
{
	float step; //resolution of the frequency. at an Fclock of 32 MHz, this is 61.035 Hz
	uint64_t desiredFrequency;
	uint32_t frequencyOverStep;
	uint8_t RegFrLsb;
	uint8_t RegFrMid;
	uint8_t RegFrMsb;
	uint8_t AddrFrLsb;
	uint8_t AddrFrMid;
	uint8_t AddrFrMsb;
} TypeRegFr;

typedef struct tagTypeRegPaRampBits
{
	uint8_t PaRamp: 4;
	uint8_t reserved: 1;
	uint8_t unused: 3;
} TypeRegPaRampBits;

typedef struct tagTypeRegPaRampInstrAndAddr
{
	TypeRegPaRampBits Reg;
	uint8_t Addr;
} TypeRegPaRampInstrAndAddr;

typedef struct tagTypeRegOcpInstrAndAddr
{
	uint8_t OcpTrim: 5;
	uint8_t OcpOn: 1;
	uint8_t unused: 2;
} TypeRegOcpBits;

typedef struct
{
	TypeRegOcpBits Reg;
	uint8_t Addr;
} TypeRegOcpInstrAndAddr;

typedef struct tagTypeRegLnaBits
{
	uint8_t LnaBoostHf: 2;
	uint8_t reserved: 1;
	uint8_t LnaBoostLf: 2;
	uint8_t LnaGain: 3;
} TypeRegLnaBits;

typedef struct tagTypeRegLnaInstrAndAddr
{
	TypeRegLnaBits Reg;
	uint8_t Addr;
} TypeRegLnaInstrAndAddr;

typedef struct tagTypeRegIrqFlagsBits
{
	uint8_t CadDetectedMask: 1;
	uint8_t FhssChangeChannelMask: 1;
	uint8_t CadDoneMask: 1;
	uint8_t TxDoneMask: 1;
	uint8_t ValidHeaderMask: 1;
	uint8_t PayloadCrcErrorMask: 1;
	uint8_t RxDoneMask: 1;
	uint8_t RxTimeoutMask: 1;
} TypeRegIrqFlagsBits;

typedef struct tagTypeRegIrqFlagsInstructionAndAddress
{
	TypeRegIrqFlagsBits RegIrqFlags;
	uint8_t Addr;
} TypeRegIrqFlagsInstructionAndAddress;

typedef struct tagTypeRegModemStatBits
{
	uint8_t ModemStatus: 5;
	uint8_t RxCodingRate: 3;
} TypeRegModemStatBits;

typedef struct tagTypeRegModemStat
{
	TypeRegModemStatBits Reg;
	uint8_t Addr;
} TypeRegModemStat;

typedef struct tagTypeRegHopChannelBits
{
	uint8_t FhssPresentChannel: 6;
	uint8_t CrcOnPayload: 1;
	uint8_t PllTimeout: 1;
} TypeRegHopChannelBits;

typedef struct tagTypeRegHopChannel
{
	TypeRegHopChannelBits Reg;
	uint8_t Addr;
} TypeRegHopChannel;

typedef struct tagTypeRegModemConfig1Bits
{
	uint8_t ImplicitHeaderModeOn: 1;
	uint8_t CodingRate: 3;
	uint8_t Bw: 4;
} TypeRegModemConfig1Bits;

typedef struct tagTypeRegModemConfig1
{
	TypeRegModemConfig1Bits Reg;
	uint8_t Addr;
} TypeRegModemConfig1;

typedef struct tagTypeRegModemConfig2Bits
{
	uint8_t SymbTimeout: 2;
	uint8_t RxPayloadCrcOn: 1;
	uint8_t TxContinuousMode: 1;
	uint8_t SpreadingFactor: 4;
} TypeRegModemConfig2Bits;

typedef struct tagTypeRegModemConfig2
{
	TypeRegModemConfig1Bits Reg;
	uint8_t Addr;
} TypeRegModemConfig2;

typedef struct tagTypeRegModemConfig3Bits
{
	uint8_t reserved: 2;
	uint8_t agcAutoOn: 1;
	uint8_t LowDataRateOptimize: 1;
	uint8_t unused: 4;
} TypeRegModemConfig3Bits;

typedef struct tagTypeRegModemConfig3
{
	TypeRegModemConfig1Bits Reg;
	uint8_t Addr;
} TypeRegModemConfig3;

typedef struct tagTypeRegFei
{
	uint8_t RegLsb;
	uint8_t RegMid;
	uint8_t RegMsb;
	uint8_t AddrLsb;
	uint8_t AddrMid;
	uint8_t AddrMsb;
} TypeRegFei;

typedef struct tagTypeRegDetectOptimizeBits
{
	uint8_t DetectionOptimize: 3;
	uint8_t reserved: 4;
	uint8_t AutomaticIFOn: 1;
} TypeRegDetectOptimizeBits;

typedef struct tagTypeRegDetectOptimize
{
	TypeRegDetectOptimizeBits Reg;
	uint8_t Addr;
} TypeRegDetectOptimize;

typedef struct tagTypeRegInvertIQBits
{
	uint8_t InvertIQ_TX: 1;
	uint8_t reserved1: 5;
	uint8_t InvertIQ_RX: 1;
	uint8_t reserved2: 1;
} TypeRegInvertIQBits;

typedef struct tagTypeRegInvertIQ
{
	TypeRegInvertIQBits Reg;
	uint8_t Addr;
} TypeRegInvertIQ;

typedef struct tagSX12xxRegisters
{
	//Addr = Address
	TypeInstructionAndAddress Fifo;
	TypeRegOpModeInstrAndAddr OpMode;
	TypeRegFr carrierFrequency;
	TypeRegPaConfig PaConfig;
	TypeRegPaRampInstrAndAddr PaRamp;
	TypeRegOcpInstrAndAddr Ocp;
	TypeRegLnaInstrAndAddr Lna;
	TypeInstructionAndAddress FifoAddrPtr;
	TypeInstructionAndAddress FifoTxBaseAddress;
	TypeInstructionAndAddress FifoRxBaseAddress;
	TypeInstructionAndAddress FifoRxCurrentAddress;
	TypeRegIrqFlagsInstructionAndAddress IrqFlagsMask;
	TypeRegIrqFlagsInstructionAndAddress IrgFlags;
	TypeInstructionAndAddress RxNbBytes;
	TypeInstructionAndAddress RxHeaderCntValueMsb;
	TypeInstructionAndAddress RxHeaderCntValueLsb;
	TypeInstructionAndAddress RxPacketCntValueMsb;
	TypeInstructionAndAddress RxPacketCntValueLsb;
	TypeRegModemStat ModemStat;
	TypeInstructionAndAddress PktSnrValue;
	TypeInstructionAndAddress PktRssiValue;
	TypeInstructionAndAddress RssiValue;
	TypeRegHopChannel HopChannel;
	TypeRegModemConfig1 ModemConfig1;
	TypeRegModemConfig2 ModemConfig2;
	TypeRegModemConfig3 ModemConfig3;
	TypeInstructionAndAddress SymbTimeoutLsb;
	TypeInstructionAndAddress PreambleLsb;
	TypeInstructionAndAddress PreambleMsb;
	TypeInstructionAndAddress PayloadLength;
	TypeInstructionAndAddress MaxPayloadLength;
	TypeInstructionAndAddress HopPeriod;
	TypeInstructionAndAddress FifoRxByteAddr;
	TypeInstructionAndAddress PpmCorrection;
	TypeInstructionAndAddress RssiWideBand;
	TypeInstructionAndAddress IfFreq2;
	TypeInstructionAndAddress IfFreq1;
	TypeRegDetectOptimize DetectOptimize;
	TypeRegInvertIQ InvertIQ;
	TypeInstructionAndAddress HighBWOptimize1;
	TypeInstructionAndAddress DetectionThreshold;
	TypeInstructionAndAddress SyncWord;
	TypeInstructionAndAddress HighBWOptimize2;
	TypeInstructionAndAddress InvertIQ2;
	TypeRegFei Fei;
}SX12xxRegisters;
Andrew Neil
Evangelist II

@JustSomeGuy wrote:

but SX1276 is defined in the PV section. 


No, it's not defined - it's just declared as 'extern':

 

/* USER CODE BEGIN PV */
extern SX12xxRegisters SX1276;

 

So you need a matching definition somewhere.

 

https://c-faq.com/decl/decldef.html

https://community.st.com/t5/stm32cubeide-mcus/adding-new-c-source-files-to-my-project-and-navigating-through/m-p/657455/highlight/true#M25847

 

Oooh, I see.

So if the header file is included, do I not need to use extern? I just removed the 'extern' from the declaration and it returned no error.


@JustSomeGuy wrote:

So if the header file is included, do I not need to use extern?


Err, no - don't do that!

 


@JustSomeGuy wrote:

I just removed the 'extern' from the declaration and it returned no error.


By removing the 'extern', you now have a definition in the header;  therefore, every single source file which #includes that header will get a definition - so you will end up with "multiple definition" errors.

See this post:

https://community.st.com/t5/stm32cubeide-mcus/adding-new-c-source-files-to-my-project-and-navigating-through/m-p/657455/highlight/true#M25847

TL;DR: you need exactly one definition within the Project; usually in a .c file. You then have an 'extern' declaration in a header (.h file), which can be #included by any/all other .c files which need to "see" that symbol.

 

This is standard C stuff - nothing specific to ST or STM32.


This is standard C stuff - nothing specific to ST or STM32.


Well I thought it was something to do with STM32cubeIDE but either way, thanks for clearing that up. It's not something we learned in college.

In the C language, when you have a variable declared in one source file (let's say main.c) and you want to use it in another source file (let's say module.c), you typically use the extern keyword to declare that variable in the other file.

// File: main.c

#include "main.h"

int var;

 

// File: module.c

#include "module.h"

extern int var;

 

Other another way is include an external declaration of var in main.h using the extern keyword like this:

// File: main.c

#include "main.h"

int var;

 

// File: main.h

extern int var;

 

// File: module.c

#include "module.h"

#include "mian.h"

 

 

This last option is useful when severval modules use same external var.

 

 

 

Awesome, typing out what a solution would look like instead of just saying 'you need this' is the best way to answer questions. Thank you for that!

👍