cancel
Showing results for 
Search instead for 
Did you mean: 

How to use USART single wire(half-duplex) for SDI-12 communication protocol with STM32G081 using Stm32cubeIDE???

Dprom
Associate II

Hi Genius,

I am using STM32G081T6 with STM32cubeIDE. I found out in pinout & configuration setting, there are several USART modes including Single Wire (half-duplex). I set baud rate to 1200, word length to 7Bits, parity even and 1 stop bit. Then connect the single pin with arduino maker nano who already tested with real SDI-12 sensor. I ping dummy msg from arduino to stm32 but doesn't get expected value. Guide me to solve this, there might be something that I missed

Thanks in advanced

9 REPLIES 9
TDK
Guru

The number of data bits includes parity, so you will need to set 8 bits if you have 7 data bits.

Single wire is nontrivial, as the two devices need to be in sync in order not to talk over each other. Show the code you're using.

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

SDI-12 doesn't seem to be very common because this question rarely came up here.

Maybe a version on Github that can be found here will help you further?

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
Dprom
Associate II
int main(void)
{
  /* USER CODE BEGIN 1 */
	uint8_t UART1_rxBuffer[255] = {0};
 
  /* 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_USART1_UART_Init();
  MX_I2C1_Init();
  MX_SPI1_Init();
  MX_USART2_UART_Init();
  MX_USART3_UART_Init();
  MX_DMA_Init();
  MX_USART4_UART_Init();
  /* USER CODE BEGIN 2 */
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  HAL_UART_Transmit(&huart1, (uint8_t*)"Write Something\r\n", 17, HAL_MAX_DELAY);
 
  while (1)
  {
	  //HAL_UART_Receive(&huart1, &UART1_rxBuffer[0], 12, 500);
	  HAL_UART_Receive(&huart2, &UART1_rxBuffer[0], 12, 500);
	  HAL_UART_Transmit(&huart1, UART1_rxBuffer, strlen((char*)UART1_rxBuffer), HAL_MAX_DELAY);
	  UART1_rxBuffer[0] = 0;
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}
static void MX_USART1_UART_Init(void)
{
 
  /* USER CODE BEGIN USART1_Init 0 */
 
  /* USER CODE END USART1_Init 0 */
 
  /* USER CODE BEGIN USART1_Init 1 */
 
  /* USER CODE END USART1_Init 1 */
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 1200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART1_Init 2 */
 
  /* USER CODE END USART1_Init 2 */
 
}
 
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 = 1200;
  huart2.Init.WordLength = UART_WORDLENGTH_7B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_EVEN;
  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_ENABLE;
  huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_HalfDuplex_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_SetTxFifoThreshold(&huart2, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_SetRxFifoThreshold(&huart2, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_DisableFifoMode(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART2_Init 2 */
 
  /* USER CODE END USART2_Init 2 */
 
}

Hi master,

This is my codes. Arduino always ping "0I!" every 1 sec.

  while(1)
  {
    String command = "";
    command += "0";
    command += "I!";
    Serial.println("Send: " + command);
    mySDI12.sendCommand(command);
    delay(1000);
  }

Hi master,

Thanks for you suggestion. Share with me if there are new update added for SDI-12 example using STMcubeIDE

Regards

> huart1.Init.WordLength = UART_WORDLENGTH_8B;

> huart1.Init.StopBits = UART_STOPBITS_1;

> huart1.Init.Parity = UART_PARITY_NONE;

> huart1.Init.Mode = UART_MODE_TX_RX;

> huart2.Init.WordLength = UART_WORDLENGTH_7B;

> huart2.Init.StopBits = UART_STOPBITS_1;

> huart2.Init.Parity = UART_PARITY_EVEN

> huart2.Init.Mode = UART_MODE_TX_RX;

The code you posted uses two UARTs, neither of which is in single wire mode. One has 8 data bits and no parity, the other has 6 data bits plus one even parity. Hard to know what the intention is here.

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

.

.

uart 1 for debug, uart 2 for SDI-12 sir.. means uart 2 is for single wire. It shows in the init function as below.

  if (HAL_HalfDuplex_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }

Regards

You're right. The word length is still incorrect, however.

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