cancel
Showing results for 
Search instead for 
Did you mean: 

Using HAL_UART_Transmit outside main.c cause hardfault

taoybb
Associate II

Hello, I got a problem on 'HAL_UART_Transmit' function. I wrote a simple line to create and define unsigned char of 10 then send it through 'HAL_UART_Transmit' on different .c file.

unsigned char A[10] = { ...... 10 value ......};
HAL_UART_Transmit(&huart1, &A[0], 10, 500);

And it goes IRQ HardFault after it ran through these line. But if I move whole thing to 'main.c' without any modification it will work pretty fine. I already include neccessary file. What did I do wrong?

MCU : STM32F103C8T6

IDE : 1.13.1

 

7 REPLIES 7
TDK
Guru

Probably huart1 is not initialized correctly. Show the contents of that structure at the time of the call.

Nothing else wrong with the code that you've shown.

If you feel a post has answered your question, please click "Accept as Solution".

Ok, but look at what's actually faulting..

Is the address in scope? Is the structure initialized? Present enough code to replicate.

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

Hello, thank you for reply.

Here is main.c

 

#include "main.h"
#include "dma.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
#include "includeall.h"

StrSystemData SystemData = {0};
TypedefArray100 CentralCardData = {0};
UART_BUFFER Uart1 = {0};

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_DMA_Init();
  MX_TIM2_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
  SetpUARTDMA();
  HAL_TIM_Base_Start_IT(&htim2);
  GetSNNumber(&SystemData);
  PowerLEDUpdate();
  LoginLEDUpdate();
  DigitalSensorSetup(10, false);
  unsigned char RDY[5] = {0x52, 0x44, 0x59, 0x0D, 0x0A};
  HAL_UART_Transmit(&huart1, &RDY[0], 5, 100);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1){
	// ----------------------------------------------------------------
	// ---------- Routine Function----------
	DigitalSensorProcess();
	SystemLED(&SystemData);
	BuzzerProcess();
	// ---------- Check Sensor ----------
	if(DigitalSensorRead() == true){
		SystemData.Mode = MODE_DVR;
	}

	// ---------- UART Process ----------
	if(UART1RXReady() == true){
		UART1Process(&SystemData);
		LoginLEDUpdate();
		//uint8_t VConfigOut = GetVehicleType();
		//uint16_t CFG03SizeOut = 0;
		//unsigned char CFG03Out[20] = {0};
		//PackProtocol(PSC_GROUP03, PSC_G03_CFG, &VConfigOut, 1, &CFG03Out[0], &CFG03SizeOut);
		//HAL_UART_Transmit(&huart1, &CFG03Out[0], 10, 100);
	}

	// END ----------
	// ----------------------------------------------------------------
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

 

Here is separated file

 

extern UART_HandleTypeDef huart1;
extern UART_BUFFER Uart1;

// ------------------------- UART RX -------------------------
bool UART1RXReady(void){
	return Uart1.Ready;
}

void UART1Process(StrSystemData *SysDataIn){
			
			// ### ... Other Process ... ###
	
			if((CRCcal[0]+CRCcal[1]) == 0x00){ // CRC Check ---------
				CMDA = Uart1.RxDataBuffer[offCnt+HEADERSIZE];
				CMDB = Uart1.RxDataBuffer[offCnt+HEADERSIZE+1];
				// ---------- Start Sorting ----------
				if(CMDA == PSC_GROUP03){ // Group '0x03'
					if(CMDB == PSC_G03_CFG){
						// Configuration Command
						uint8_t VConfigIn = Uart1.RxDataBuffer[offCnt+HEADERSIZE+2];
						SaveVehicleType(VConfigIn);
						// -------------------
						uint8_t VConfigOut = GetVehicleType();
						uint16_t CFG03SizeOut = 0;
						unsigned char CFG03Out[20];
						PackProtocol(PSC_GROUP03, PSC_G03_CFG, &VConfigOut, 1, &CFG03Out[0], &CFG03SizeOut);
						HAL_UART_Transmit(&huart1, &CFG03Out[0], 10, 100);
						offCnt = UARTBUFFERSIZE;
					}
				// -----------------------------------
				}

				// ### ... Other Process ... ###

				// --------- End Sorting ----------
			}
		}	// -----------------------------------------------------------------------------
	}
	Uart1.Ready = false; // Clear UART Status
}

 

All configuration was generated by integrated ST code generator. On main.c there is similar line of code was commented, that was for test and it worked fine but the one in UART process are the one that creating hardfault

 

 

 

 

TDK
Guru

And the contents of huart1 at the time of the call?

If you feel a post has answered your question, please click "Accept as Solution".
taoybb
Associate II

Hi TDK, after I tried everything and found out that if I turn 'Optimization Option' from 0 to 1 then it worked. But btw, may you give me a clue which one I should look in 'huart1' ? I would like to know what is really a root cause of this.

You're looking to see that the contents are properly initialized and valid at the time of the call.

If you feel a post has answered your question, please click "Accept as Solution".
taoybb
Associate II

Here is unsigned array to print out thought UART and size to print.

arrayOut1.pnghuart1a.png

And here is huart1 data

huart1b.png