HarveyBates / STM32-SDI12

Its a SDI-12 library for STM32 microcontrollers.
MIT License
3 stars 1 forks source link

STM32WB55 problem #2

Open NikolaPU opened 1 year ago

NikolaPU commented 1 year ago

Hi, first of all thank you for posting this library I'm trying to run in on STM32WB55 Nucleo board. I have Teros12 sensor and I connect it, data pin to PC1(orange) and +5V (brown wire to 5V pin), and GND to GND I set LPUART to Single wire (half duplex) 7 bits including parity, but I can not set 1200 bits/sec Baud rate In GPIO I set user label SDI12_COM and max output speed to Low, and CPIO PULL UP.

I included sdi12.c in Src folder and sdi12.h in Inc folder This is my main.c file : ` ----------------------------------- MAIN.C ------------------------------ / USER CODE BEGIN Header / /**


/ Private includes ----------------------------------------------------------/ / USER CODE BEGIN Includes /

include "sdi12.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 ---------------------------------------------------------/ UART_HandleTypeDef hlpuart1; UART_HandleTypeDef huart1;

PCD_HandleTypeDef hpcd_USB_FS;

/ USER CODE BEGIN PV /

/ USER CODE END PV /

/ Private function prototypes -----------------------------------------------/ void SystemClock_Config(void); void PeriphCommonClock_Config(void); static void MX_GPIO_Init(void); static void MX_USART1_UART_Init(void); static void MX_USB_PCD_Init(void); static void MX_LPUART1_UART_Init(void); / USER CODE BEGIN PFP /

/ USER CODE END PFP /

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

/ USER CODE END 0 /

/**

/ Configure the peripherals common clocks / PeriphCommonClock_Config();

/ USER CODE BEGIN SysInit /

/ USER CODE END SysInit /

/ Initialize all configured peripherals / MX_GPIO_Init(); MX_USART1_UART_Init(); MX_USB_PCD_Init(); MX_LPUART1_UART_Init(); / USER CODE BEGIN 2 /

SDI12_Init(&hlpuart1);

char data[64] = {0}; // Buffer to hold data returned from sensor char addr = '0'; // SDI-12 address // char devices[100] = {0};

/ USER CODE END 2 /

/ Infinite loop / / USER CODE BEGIN WHILE / while (1) { // memset(devices, 0, sizeof(devices)); // SDI12_DevicesOnBus(devices);

          SDI12_Measure_TypeDef measurement_info;
          memset(data, 0, sizeof(data));
          SDI12_StartMeasurement(addr, &measurement_info);
          HAL_Delay(measurement_info.Time * 1000);
          SDI12_SendData(addr, &measurement_info, data);
          HAL_Delay(10000);

/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

} / USER CODE END 3 / }

/**

/**

/**

}

/**

}

/**

}

/**

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

/ USER CODE BEGIN 4 /

/ USER CODE END 4 /

/**

ifdef USE_FULL_ASSERT

/**


`

in Sdi12.c file I add this for nucelo WB55

elif defined(STM32WB55XX)

define GPIO_AF8_LPUART1

and in void SDI12_Init

void SDI12_Init(UART_HandleTypeDef *hlpuart1) { sdi12.Huart = hlpuart1; sdi12.Pin = SDI12_COM_Pin; sdi12.Port = SDI12_COM_GPIO_Port; }

and in sdi12.h I replace huart to hlpuart1 like this:

void SDI12_Init(UART_HandleTypeDef *hlpuart1);

HarveyBates commented 1 year ago

Hey mate, your definately going to need a baud rate of 1200 as this is required by the SDI-12 specification so that devices can accurately send and receive messages. This is proberbly a limitation of the LPUART on the WB55.

You could check is in the clock configuration for the LPUART, see if you can bring down this value (I think it's attached to an external oscillator at 32.768 khz) as much as possible as it may allow you to select lower baud-rates. See page 43 of the WB55 data sheet, bottom right for what I am referring to.

NikolaPU commented 1 year ago

Hi m8, first of all, thanks for helping me. So I change the prescaler to 256 and now I can set 1200 baud rate to LPUART But again can not get reading. I try it on Arduino Mega and Teros is working fine.

My LPUART set up are

` static void MX_LPUART1_UART_Init(void) {

/ USER CODE BEGIN LPUART1_Init 0 /

/ USER CODE END LPUART1_Init 0 /

/ USER CODE BEGIN LPUART1_Init 1 /

/ USER CODE END LPUART1_Init 1 / hlpuart1.Instance = LPUART1; hlpuart1.Init.BaudRate = 1200; hlpuart1.Init.WordLength = UART_WORDLENGTH_8B; hlpuart1.Init.StopBits = UART_STOPBITS_1; hlpuart1.Init.Parity = UART_PARITY_EVEN; hlpuart1.Init.Mode = UART_MODE_TX_RX; hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; hlpuart1.Init.ClockPrescaler = UART_PRESCALER_DIV256; hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; if (HAL_HalfDuplex_Init(&hlpuart1) != HAL_OK) { Error_Handler(); } if (HAL_UARTEx_SetTxFifoThreshold(&hlpuart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK) { Error_Handler(); } if (HAL_UARTEx_SetRxFifoThreshold(&hlpuart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK) { Error_Handler(); } if (HAL_UARTEx_DisableFifoMode(&hlpuart1) != HAL_OK) { Error_Handler(); } / USER CODE BEGIN LPUART1_Init 2 / hlpuart1.AdvancedInit.Swap = UART_ADVFEATURE_SWAP_DISABLE; if (HAL_UART_Init(&hlpuart1) != HAL_OK) { Error_Handler(); } / USER CODE END LPUART1_Init 2 /

}

`

and this:

` / USER CODE BEGIN 2 / SDI12_Init(&hlpuart1);

char data[74] = {0}; // Buffer to hold data returned from sensor char addr = '0'; // SDI-12 address

/ USER CODE END 2 /`

maybe this is a problem In sdi12.c I add this:

elif defined(STM32WB55XX)

define GPIO_AF8_LPUART1

and change huart to hlpuart1:

void SDI12_Init(UART_HandleTypeDef *hlpuart1) { sdi12.Huart = hlpuart1; sdi12.Pin = SDI12_COM_Pin; sdi12.Port = SDI12_COM_GPIO_Port; }

and change this GPIO_InitStruct.Alternate = GPIO_AF_USART1; to this: GPIO_InitStruct.Alternate = GPIO_AF8_LPUART1;

I can send you all project files ...

NikolaPU commented 1 year ago

WOW finally it works

HarveyBates commented 1 year ago

Great! What was the change that got it going for you? Well done 👍

NikolaPU commented 1 year ago

I had to set up lpuart hlpuart1.Instance = LPUART1; hlpuart1.Init.BaudRate = 1200; hlpuart1.Init.WordLength = UART_WORDLENGTH_8B; hlpuart1.Init.StopBits = UART_STOPBITS_1; hlpuart1.Init.Parity = UART_PARITY_EVEN; hlpuart1.Init.Mode = UART_MODE_TX_RX; hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; hlpuart1.Init.ClockPrescaler = UART_PRESCALER_DIV256; hlpuart1.Init.OverSampling = UART_OVERSAMPLING_16;

hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_TXINVERT_INIT|UART_ADVFEATURE_RXINVERT_INIT |UART_ADVFEATURE_SWAP_INIT; hlpuart1.AdvancedInit.TxPinLevelInvert = UART_ADVFEATURE_TXINV_ENABLE; hlpuart1.AdvancedInit.RxPinLevelInvert = UART_ADVFEATURE_RXINV_ENABLE; hlpuart1.AdvancedInit.Swap = UART_ADVFEATURE_SWAP_ENABLE; if (HAL_UART_Init(&hlpuart1) != HAL_OK) { Error_Handler(); } / USER CODE BEGIN USART1_Init 2 /

// Keep TX as the transmit pin on startup
hlpuart1.AdvancedInit.Swap = UART_ADVFEATURE_SWAP_DISABLE;
if (HAL_UART_Init(&hlpuart1) != HAL_OK)
{
    Error_Handler();
}

and coment this // HAL_Delay(measurement_info.Time * 1000); and it is working :)

But now, I want to use Zigbee. So Zigbee uses lpuart :( Do you have some idea how to connect SDI12 Teros and use Zigbee on the stm32wb nucleo board? Maby some libraries like SoftwareSerial library for Arduino.... ?

Thanks