cancel
Showing results for 
Search instead for 
Did you mean: 

Trying to Generate a sbus

Safana_Ismail
Associate

[moderation:] Please see How to write your question to maximize your chances to find a solution, How to insert code [/moderation]

I am trying to generate a sbus signal for radio transmission in drones. imhave tried this code. in oscilloscope, uart output is showing, but qgc is not detecting sbus. i do pass a 16 channel array to convert into sbus. the project is as below:- 

/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2025 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 <stdio.h>
#include <string.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 ---------------------------------------------------------*/
TIM_HandleTypeDef htim3;

UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */
uint16_t channels[16] = { 1000, 900,  1200, 900,  1500, 1500, 1500, 1500,
                          1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500 };

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_TIM3_Init(void);

/* USER CODE BEGIN 0 */

void SBUS_GenerateFrame(uint16_t channels[16], uint8_t sbus_data[25])
{
  // memset(sbus_data, 0, 25);

  uint32_t ch[16];
  for (int i = 0; i < 16; i++) {
    ch[i] = channels[i];
    // & 0x07FF; // 11-bit channel data
  }
  sbus_data[0] = 0x0F;
  sbus_data[1] = (uint8_t)(ch[0] & 0xFF);
  sbus_data[2] = (uint8_t)((ch[0] >> ?? | (ch[1] << 3));
  sbus_data[3] = (uint8_t)((ch[1] >> 5) | (ch[2] << 6));
  sbus_data[4] = (uint8_t)(ch[2] >> 2);
  sbus_data[5] = (uint8_t)((ch[2] >> 10) | (ch[3] << 1));
  sbus_data[6] = (uint8_t)((ch[3] >> 7) | (ch[4] << 4));
  sbus_data[7] = (uint8_t)((ch[4] >> 4) | (ch[5] << 7));
  sbus_data[8] = (uint8_t)(ch[5] >> 1);
  sbus_data[9] = (uint8_t)((ch[5] >> 9) | (ch[6] << 2));
  sbus_data[10] = (uint8_t)((ch[6] >> 6) | (ch[7] << 5));
  sbus_data[11] = (uint8_t)(ch[7] >> 3);
  sbus_data[12] = (uint8_t)(ch[8] & 0xFF);
  sbus_data[13] = (uint8_t)((ch[8] >> ?? | (ch[9] << 3));
  sbus_data[14] = (uint8_t)((ch[9] >> 5) | (ch[10] << 6));
  sbus_data[15] = (uint8_t)(ch[10] >> 2);
  sbus_data[16] = (uint8_t)((ch[10] >> 10) | (ch[11] << 1));
  sbus_data[17] = (uint8_t)((ch[11] >> 7) | (ch[12] << 4));
  sbus_data[18] = (uint8_t)((ch[12] >> 4) | (ch[13] << 7));
  sbus_data[19] = (uint8_t)(ch[13] >> 1);
  sbus_data[20] = (uint8_t)((ch[13] >> 9) | (ch[14] << 2));
  sbus_data[21] = (uint8_t)((ch[14] >> 6) | (ch[15] << 5));
  sbus_data[22] = (uint8_t)(ch[15] >> 3);
  sbus_data[23] = 0x00; // flags
  sbus_data[24] = 0x00; // end byte
// printf("sbus_data= ");
// for(int i=0;i<16;i++)
// printf(" %d ",sbus_data[i] );
// printf("\r\n");
}

void SBUS_Transmit(UART_HandleTypeDef* huart, uint16_t channels[16])
{
  uint8_t sbus_data[25];
  SBUS_GenerateFrame(channels, sbus_data);
  HAL_UART_Transmit(huart, sbus_data, 25, HAL_MAX_DELAY);
}

/* USER CODE END 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();

  /* 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_USART2_UART_Init();
  MX_TIM3_Init();
  /* USER CODE BEGIN 2 */

  HAL_TIM_Base_Start_IT(&htim3);
  /* USER CODE END 2 */

  void SystemClock_Config(void)
  {
    RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };
    RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };

    /** Initializes the RCC Oscillators according to the specified parameters
     * in the RCC_OscInitTypeDef structure.
     */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
    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_HSI;
    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_0) != HAL_OK) {
      Error_Handler();
    }
  }

  /**
   * @brief TIM3 Initialization Function
   * @PAram None
   * @retval None
   */
  static void MX_TIM3_Init(void)
  {

    /* USER CODE BEGIN TIM3_Init 0 */

    /* USER CODE END TIM3_Init 0 */

    TIM_ClockConfigTypeDef sClockSourceConfig = { 0 };
    TIM_MasterConfigTypeDef sMasterConfig = { 0 };

    /* USER CODE BEGIN TIM3_Init 1 */

    /* USER CODE END TIM3_Init 1 */
    htim3.Instance = TIM3;
    htim3.Init.Prescaler = 0;
    htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
    htim3.Init.Period = 139;
    htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
    if (HAL_TIM_Base_Init(&htim3) != HAL_OK) {
      Error_Handler();
    }
    sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
    if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK) {
      Error_Handler();
    }
    sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
    sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
    if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) !=
        HAL_OK) {
      Error_Handler();
    }
    /* USER CODE BEGIN TIM3_Init 2 */

    /* USER CODE END TIM3_Init 2 */
  }

  /**
   * @brief USART1 Initialization Function
   * @PAram None
   * @retval None
   */
  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 = 100000;
    huart1.Init.WordLength = UART_WORDLENGTH_8B;
    huart1.Init.StopBits = UART_STOPBITS_2;
    huart1.Init.Parity = UART_PARITY_EVEN;
    huart1.Init.Mode = UART_MODE_TX_RX;
    huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart1.Init.OverSampling = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&huart1) != HAL_OK) {
      Error_Handler();
    }
    /* USER CODE BEGIN USART1_Init 2 */

    /* USER CODE END USART1_Init 2 */
  }

  /**
   * @brief USART2 Initialization Function
   * @PAram 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 = 100000;
    huart2.Init.WordLength = UART_WORDLENGTH_8B;
    huart2.Init.StopBits = UART_STOPBITS_2;
    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;
    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
   * @PAram 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_GPIOC_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();

    /*Configure GPIO pin Output Level */
    HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);

    /*Configure GPIO pin : PC13 */
    GPIO_InitStruct.Pin = GPIO_PIN_13;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    /* USER CODE BEGIN MX_GPIO_Init_2 */

    /* USER CODE END MX_GPIO_Init_2 */
  }

  /* USER CODE BEGIN 4 */
  void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef * htim)
  {
    if (htim->Instance == TIM3) {
      // Debug LED blink
      SBUS_Transmit(&huart2, channels); // Send SBUS frame every 14ms
    }
  }
  /* 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) {
    }

#endif /* USE_FULL_ASSERT */

 

0 REPLIES 0