Author Topic: STM32 GPIO Struct Syntax Help (using HAL)  (Read 18205 times)

0 Members and 1 Guest are viewing this topic.

Offline noname4meTopic starter

  • Regular Contributor
  • *
  • Posts: 93
STM32 GPIO Struct Syntax Help (using HAL)
« on: April 25, 2019, 09:36:19 pm »
Hi All,

So I've discovered structs.

I've used them everywhere I can because I find it makes my code easier to manage so, for example, if I have a Bluetooth module, I'll set up a struct to set up the hardware to talk to it, and all associated tx/rx buffers come under that one "mega" struct.

I've tried to do the same to a stepper motor, for which I have 4 GPIO pins interfacing the driver module.

I would like to set up a struct so that I can define a port and pin in my mega struct, and use this to interface with my hardware.

See below:
Code: [Select]
typedef struct MOTORS {

gpios_t  pins;

int32_t count;

int32_t setPoint;

adc_t analogs;

position_set_t position;
uint8_t first_on;

step_t step;

int32_t lastmovedvalue;

} motors_t;


and the GPIO part:

Code: [Select]
typedef struct gpios {

uint16_t pin_one;
GPIO_TypeDef  * port_pin_one;
uint16_t pin_two;
GPIO_TypeDef  * port_pin_two;
uint16_t pin_three;
GPIO_TypeDef  * port_pin_three;
uint16_t pin_four;
GPIO_TypeDef  * port_pin_four;
uint16_t end_one;
GPIO_TypeDef  * port_end_one;
uint16_t end_two;
GPIO_TypeDef  * port_end_two;

} gpios_t;



I then declare motor1 and try to set up the GPIO and it just does not want to work.


If I set up the GPIO in its own struct, not under the motor struct, then I can get to the correct memory location, but I would like to be able to access it under the motor struct to keep things tidy and so that I know which pins are attached to which motor.

Is there a way that I can do this?

I've checked the compiled memory locations, but they don't attach to say, GPIOA or GPIOB, but some other random location.

I've lost track of all the error messages, but if they will help I will go over them.

I'm sure this is something to do with pointers and memory, but I don't know how to set the GPIO to read from GPIOB under this struct format (a struct within a struct).

Any ideas on what I can try?


 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12505
  • Country: us
    • Personal site
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #1 on: April 25, 2019, 09:45:04 pm »
GPIO_TypeDef define actual hardware layout. You can't just arbitrarily put them anywhere. They are only useful when used  as pointers to specific locations on I/O memory.

You can assign the location of the actual hardware registers to those pointers, but I don't think it will be any more convenient, it will just be confusing to your and whoever has to read your code.

« Last Edit: April 25, 2019, 09:48:41 pm by ataradov »
Alex
 

Offline noname4meTopic starter

  • Regular Contributor
  • *
  • Posts: 93
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #2 on: April 25, 2019, 09:50:36 pm »
Please consider me a "beginner".

I don't understand why I can declare the gpios_t struct in the main.c file, and attach for eg GPIOA by just doing:

Code: [Select]
gpios_t * newone;

newone.port_pin_one = GPIOA;


and it works, but if I try and do it under the struct the memory location is something wildly out?
 

Offline noname4meTopic starter

  • Regular Contributor
  • *
  • Posts: 93
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #3 on: April 25, 2019, 09:53:55 pm »
The reason why I want to do it this way, is so that I can use the motors * with the HAL library functions (HAL_GPIO_Write), which takes the port and pin as arguments.


If I know that I can just put a motor.port_pin_one and motor.pin_one as arguments to that, then I can reuse that function for all instances of motor_t

That's what I'm trying to achieve..
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12505
  • Country: us
    • Personal site
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #4 on: April 25, 2019, 09:54:23 pm »
In this code you are declaring a pointer  to gpios_t called newone. So the correct version of this code would be

Code: [Select]
gpios_t newone;
newone.port_pin_one = GPIOA;

Other than this, it should work as expected.

I don't understand your description of a non-working case. Can you  show the code?
Alex
 

Offline noname4meTopic starter

  • Regular Contributor
  • *
  • Posts: 93
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #5 on: April 25, 2019, 10:02:44 pm »
whoops, yes you are right, that's how I wrote it in the code.

Is it possible to declare motor_t motor1, and then have the GPIO_TypeDef  * port_pin_one delared at the same time?

WHen I try and configure it by doing:

motor1.pins->port_pin_one = GPIOA;

I get a memory location that does not correspond to GPIOA

If I try &GPIOA, then it complains that its not an urnary lvalue or something, and I've got really muddled up trying to figure out what's going on
 

Offline noname4meTopic starter

  • Regular Contributor
  • *
  • Posts: 93
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #6 on: April 25, 2019, 10:05:16 pm »
This is currently in the main.c, and seems to be the closest I've got to something working (the memory location is at least in the 0x4...blah region, as opposed to 0x8..blah region)

Code: [Select]
void channel1_setup()
{
channel_one.pins.port_pin_one = GPIOB;//m1_s1_GPIO_Port;
channel_one.pins.pin_one = m1_s1_Pin;

// channel_one.pins.port_pin_two = m1_s2_GPIO_Port;
// channel_one.pins.pin_two = m1_s2_Pin;
//
// channel_one.pins.port_pin_three = m1_s3_GPIO_Port;
// channel_one.pins.pin_three = m1_s3_Pin;
//
// channel_one.pins.port_pin_four = m1_s4_GPIO_Port;
// channel_one.pins.pin_four = m1_s4_Pin;
//
// channel_one.pins.port_end_one = m1_end1_GPIO_Port;
// channel_one.pins.end_one = m1_end1_Pin;
//
// channel_one.pins.port_end_two = m1_end2_GPIO_Port;
// channel_one.pins.end_two = m1_end2_Pin;

// channel_one.analogs.Instance = &hadc1;
// channel_one.analogs.channel = ADC_CHANNEL_1;
//
// channel_one.analogs.result = adcValueArray[0];
// channel_one.first_on = 1;
//
// channel_one.lastmovedvalue = 0;


}


Main snippet:


Code: [Select]
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_ADC1_Init();
  MX_TIM3_Init();
  MX_TIM1_Init();
  /* USER CODE BEGIN 2 */


  StartADCs();

  HAL_Delay(100);
  channel1_setup();
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12505
  • Country: us
    • Personal site
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #7 on: April 25, 2019, 10:15:07 pm »
WHen I try and configure it by doing:
motor1.pins->port_pin_one = GPIOA;
Again, based on your structures it should be "motor1.pins.port_pin_one = GPIOA; ". And this should work.

I still don't get what you are trying to do overall.
Alex
 

Offline noname4meTopic starter

  • Regular Contributor
  • *
  • Posts: 93
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #8 on: April 25, 2019, 10:20:46 pm »
The memory location that is pointed to by using the Atollic Expression watch window for channel_one.pins.port_pin_one is 0x46187dfb

The actual location of GPIOB is 0x40010c00 (the base address).

The watch windows is also not able to read what is in the registers of port_pin_one, which I would expect to be the contents of GPIOB.
 

Offline noname4meTopic starter

  • Regular Contributor
  • *
  • Posts: 93
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #9 on: April 25, 2019, 10:22:59 pm »
This is the idea that I wanted to implement for different instances of a stepper motor:

Code: [Select]
//void turn(motors_t motor, step_t step)
//{
// switch (step)
// {
// case one:
// HAL_GPIO_WritePin(motor.pins.port_pin_one, motor.pins.pin_one,SET);
// HAL_GPIO_WritePin(motor.pins.port_pin_two, motor.pins.pin_two,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_three, motor.pins.pin_three,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_four, motor.pins.pin_four,RESET);
// HAL_Delay(MOTORDELAY);
// break;
// case two:
// HAL_GPIO_WritePin(motor.pins.port_pin_one, motor.pins.pin_one,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_two, motor.pins.pin_two,SET);
// HAL_GPIO_WritePin(motor.pins.port_pin_three, motor.pins.pin_three,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_four, motor.pins.pin_four,RESET);
// HAL_Delay(MOTORDELAY);
// break;
// case three:
// HAL_GPIO_WritePin(motor.pins.port_pin_one, motor.pins.pin_one,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_two, motor.pins.pin_two,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_three, motor.pins.pin_three,SET);
// HAL_GPIO_WritePin(motor.pins.port_pin_four, motor.pins.pin_four,RESET);
// HAL_Delay(MOTORDELAY);
// break;
// case four:
// HAL_GPIO_WritePin(motor.pins.port_pin_one, motor.pins.pin_one,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_two, motor.pins.pin_two,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_three, motor.pins.pin_three,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_four, motor.pins.pin_four,SET);
// HAL_Delay(MOTORDELAY);
// break;
// }
//
//}

Please forgive the comment stuff
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12505
  • Country: us
    • Personal site
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #10 on: April 25, 2019, 11:08:43 pm »
Post your full code for the main.c.
Alex
 

Offline noname4meTopic starter

  • Regular Contributor
  • *
  • Posts: 93
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #11 on: April 25, 2019, 11:16:38 pm »
Code: [Select]
/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "dma.h"
#include "tim.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "motors.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
extern uint32_t adcValueArray[3];
/* 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 ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

motors_t channel_one;
motors_t channel_two;
motors_t channel_three;

GPIO_TypeDef * testGPIO;

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void channel1_setup()
{
channel_one.pins.port_pin_one = GPIOB;//m1_s1_GPIO_Port;
channel_one.pins.pin_one = m1_s1_Pin;

// channel_one.pins.port_pin_two = m1_s2_GPIO_Port;
// channel_one.pins.pin_two = m1_s2_Pin;
//
// channel_one.pins.port_pin_three = m1_s3_GPIO_Port;
// channel_one.pins.pin_three = m1_s3_Pin;
//
// channel_one.pins.port_pin_four = m1_s4_GPIO_Port;
// channel_one.pins.pin_four = m1_s4_Pin;
//
// channel_one.pins.port_end_one = m1_end1_GPIO_Port;
// channel_one.pins.end_one = m1_end1_Pin;
//
// channel_one.pins.port_end_two = m1_end2_GPIO_Port;
// channel_one.pins.end_two = m1_end2_Pin;

// channel_one.analogs.Instance = &hadc1;
// channel_one.analogs.channel = ADC_CHANNEL_1;
//
// channel_one.analogs.result = adcValueArray[0];
// channel_one.first_on = 1;
//
// channel_one.lastmovedvalue = 0;


}
/* 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_DMA_Init();
  MX_ADC1_Init();
  MX_TIM3_Init();
  MX_TIM1_Init();
  /* USER CODE BEGIN 2 */


  StartADCs();

  HAL_Delay(100);
  channel1_setup();
 // channel2_setup(channel_two);
//  channel3_setup(channel_three);



//  collect_position(&channel_one);
//  collect_position(&channel_two);
//  collect_position(&channel_three);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
//   //Take readings from ADC and average them
//   //Check for difference between ADC and current point (on first run make the current position equal to the motor position)
//   collect_position(&channel_one);
//   collect_position(&channel_two);
//   collect_position(&channel_three);


  //when do I turn - when a change is detected
  //how to detect a change?  Look at ADC value[0] and average value, if difference is more than 20, then turn.  if Higher then turn left, if lower then turn right
//    HAL_Delay(100);
//
//    has_turn(&channel_one);
////    has_turn(&channel_two);
//    has_turn(&channel_three);


   HAL_Delay(100);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* 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};

  /** Initializes the CPU, AHB and APB busses clocks
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses 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_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* 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 */

  /* 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.
  * @param  file: pointer to the source file name
  * @param  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,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
 

Offline noname4meTopic starter

  • Regular Contributor
  • *
  • Posts: 93
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #12 on: April 25, 2019, 11:17:39 pm »
motors.h

Code: [Select]
/*
 * motors.h
 *
 *  Created on: 21 Apr 2019
 *      Author: Windows 7
 */

#ifndef MOTORS_H_
#define MOTORS_H_

#include "main.h"
#include "adc.h"
#include "tim.h"
#include "gpio.h"

#include "core_cm3.h"
#include "system_stm32f1xx.h"


typedef enum POTS {
pot_1 = 0,
pot_2,
pot_3
} pots;


typedef enum STEP {

one = 0,
two = 1,
three = 2,
four = 3

} step_t;

typedef struct AVERAGES {

uint32_t previousResults[12];
uint32_t currentAverage;

}position_set_t;

typedef struct ANALO {

ADC_TypeDef * Instance;
volatile uint32_t result;
volatile uint32_t averaged;
uint32_t channel;

position_set_t position;

} adc_t;

typedef struct gpios {

uint16_t pin_one;
GPIO_TypeDef  * port_pin_one;
uint16_t pin_two;
GPIO_TypeDef  * port_pin_two;
uint16_t pin_three;
GPIO_TypeDef  * port_pin_three;
uint16_t pin_four;
GPIO_TypeDef  * port_pin_four;
uint16_t end_one;
GPIO_TypeDef  * port_end_one;
uint16_t end_two;
GPIO_TypeDef  * port_end_two;

} gpios_t;


typedef struct MOTORS {

gpios_t  pins;

int32_t count;

int32_t setPoint;

adc_t analogs;

position_set_t position;
uint8_t first_on;

step_t step;

int32_t lastmovedvalue;

} motors_t;

void StartADCs();
//void channel1_setup(motors_t pMotor);
//void channel2_setup(motors_t  pMotor);
//void channel3_setup(motors_t  pMotor);
void collect_position(motors_t * motor);
void turn(motors_t * motor, step_t step);
void has_turn(motors_t * motor);

#endif /* MOTORS_H_ */
 

Offline noname4meTopic starter

  • Regular Contributor
  • *
  • Posts: 93
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #13 on: April 25, 2019, 11:18:29 pm »
motors.c

Code: [Select]
/*
 * motors.c
 *
 *  Created on: 21 Apr 2019
 *      Author: Windows 7
 */


#include "main.h"
#include "adc.h"
#include "tim.h"
#include "gpio.h"
#include "motors.h"
#include "core_cm3.h"
#include "system_stm32f1xx.h"

#define MOTORDELAY 5
#define TURN_TRIGGER 20


extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim3;

uint32_t adcValueArray[3];

//void turn(motors_t motor, step_t step)
//{
// switch (step)
// {
// case one:
// HAL_GPIO_WritePin(motor.pins.port_pin_one, motor.pins.pin_one,SET);
// HAL_GPIO_WritePin(motor.pins.port_pin_two, motor.pins.pin_two,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_three, motor.pins.pin_three,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_four, motor.pins.pin_four,RESET);
// HAL_Delay(MOTORDELAY);
// break;
// case two:
// HAL_GPIO_WritePin(motor.pins.port_pin_one, motor.pins.pin_one,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_two, motor.pins.pin_two,SET);
// HAL_GPIO_WritePin(motor.pins.port_pin_three, motor.pins.pin_three,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_four, motor.pins.pin_four,RESET);
// HAL_Delay(MOTORDELAY);
// break;
// case three:
// HAL_GPIO_WritePin(motor.pins.port_pin_one, motor.pins.pin_one,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_two, motor.pins.pin_two,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_three, motor.pins.pin_three,SET);
// HAL_GPIO_WritePin(motor.pins.port_pin_four, motor.pins.pin_four,RESET);
// HAL_Delay(MOTORDELAY);
// break;
// case four:
// HAL_GPIO_WritePin(motor.pins.port_pin_one, motor.pins.pin_one,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_two, motor.pins.pin_two,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_three, motor.pins.pin_three,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_four, motor.pins.pin_four,SET);
// HAL_Delay(MOTORDELAY);
// break;
// }
//
//}

void StartADCs()
{
  HAL_ADC_Start_DMA(&hadc1, (uint32_t *)adcValueArray, 3);
  HAL_TIM_Base_Start(&htim3);
}


//
//void channel2_setup(motors_t pMotor)
//{
// pMotor.pins.port_pin_one = m2_s1_GPIO_Port;
// pMotor.pins.pin_one = m2_s1_Pin;
//
// pMotor.pins.port_pin_two = m2_s2_GPIO_Port;
// pMotor.pins.pin_two = m2_s2_Pin;
//
// pMotor.pins.port_pin_three = m2_s3_GPIO_Port;
// pMotor.pins.pin_three = m2_s3_Pin;
//
// pMotor.pins.port_pin_four = m2_s4_GPIO_Port;
// pMotor.pins.pin_four = m2_s4_Pin;
//
// pMotor.pins.port_end_one = m2_end1_GPIO_Port;
// pMotor.pins.end_one = m2_end1_Pin;
//
// pMotor.pins.port_end_two = m2_end2_GPIO_Port;
// pMotor.pins.end_two = m2_end2_Pin;
//
// pMotor.analogs.Instance = &hadc1;
// pMotor.analogs.channel = ADC_CHANNEL_1;
//
// pMotor.analogs.result = adcValueArray[1];
// pMotor.first_on = 1;
//
// pMotor.lastmovedvalue = 0;
//}
//
//void channel3_setup(motors_t pMotor)
//{
// pMotor.pins.port_pin_one = *m3_s1_GPIO_Port;
// pMotor.pins.pin_one = m3_s1_Pin;
//
// pMotor.pins.port_pin_two = m3_s2_GPIO_Port;
// pMotor.pins.pin_two = m3_s2_Pin;
//
// pMotor.pins.port_pin_three = m3_s3_GPIO_Port;
// pMotor.pins.pin_three = m3_s3_Pin;
//
// pMotor.pins.port_pin_four = m3_s4_GPIO_Port;
// pMotor.pins.pin_four = m3_s4_Pin;
//
// pMotor.pins.port_end_one = m3_end1_GPIO_Port;
// pMotor.pins.end_one = m3_end1_Pin;
// pMotor.pins.port_end_two = m3_end2_GPIO_Port;
// pMotor.pins.end_two = m3_end2_Pin;
//
// pMotor.analogs.Instance = &hadc1;
// pMotor.analogs.channel = ADC_CHANNEL_2;
//
// pMotor.analogs.result = adcValueArray[2];
// pMotor.first_on = 1;
// pMotor.lastmovedvalue = 0;
//}
//
//
//void collect_position(motors_t * pMotor)
//{
// static uint8_t i = 0;
// uint32_t total;
// //pMotor->analogs.result = adcValueArray[1]; //temporray
// pMotor->analogs.position.previousResults[i] =  pMotor->analogs.result;//adcValueArray[1];//
//
// i++;
//
// if (pMotor->first_on > 0)
// {
// for (int j = 0; j <12; j++)
// {
// pMotor->analogs.position.previousResults[j] = pMotor->analogs.result;
//
// }
// pMotor->first_on = 0;
// }
//
// if (i >= 12)
// {
//
// i = 0;
// }
//
// for (int k = 0; k < 12; k++)
// {
// total = pMotor->analogs.position.previousResults[k]; //pMotor->analogs.position.previousResults[k];
// }
//
// pMotor->analogs.averaged = total/11;
//}
//
//
//
//void has_turn(motors_t * motor)
//{
//
// int32_t test = motor->lastmovedvalue - (motor->analogs.averaged);
//
// if (test >= TURN_TRIGGER )
// {
//
// for (int z = test; z < 0; z--)
// {
//
// turn(motor,motor->step);
//
// motor->step--;
//
// if (motor->step > 3)
// {
// motor->step = 3;
// }
//
// }
// }
//
// if (test <= TURN_TRIGGER)
// {
// for (int z = 0; z > test; z++)
// {
// turn(motor, motor->step);
//
// motor->step++;
//
// if (motor->step > 3)
// {
// motor->step = 0;
// }
// }
// }
//
// motor->lastmovedvalue = motor->analogs.averaged;
//
//}
//


 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12505
  • Country: us
    • Personal site
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #14 on: April 25, 2019, 11:30:00 pm »
I see nothing particularly wrong in the posted part.

Try to create a simple variable of type "GPIO_TypeDef  *" in a simple project and see if the assignment works better.

And please clean up your code before posting. Why do we need the motors.c if the entire file is commented out?
Alex
 

Offline noname4meTopic starter

  • Regular Contributor
  • *
  • Posts: 93
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #15 on: April 25, 2019, 11:35:21 pm »
Apologies: I'm in the middle of troubleshooting it, normally it would be all enabled.

I've disabled all the code apart from the assignment of GPIOB to the channel_one.pins.port_pin_one.

Like I said, it appears to work fine when I define a variable "gpios_t test1" and assign GPIOB to test1.port_pin_one

It just does not work for the struct within a struct, which makes me think that I am not defining it properly/initialising it properly
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12505
  • Country: us
    • Personal site
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #16 on: April 25, 2019, 11:40:59 pm »
Are you sure it is not a debugger issue?

What if you assign "motor1.pins.port_pin_one = (GPIO_TypeDef  *)0x12345678;"? Do you see the correct value?
Alex
 

Offline noname4meTopic starter

  • Regular Contributor
  • *
  • Posts: 93
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #17 on: April 25, 2019, 11:53:29 pm »
Are you sure it is not a debugger issue?

What if you assign "motor1.pins.port_pin_one = (GPIO_TypeDef  *)0x12345678;"? Do you see the correct value?
Really appreciate the help...

Do you mind if I tried this tomorrow... it's gone past midnight and I woke up at 4 AM this morning :/


Sent from my SM-N960F using Tapatalk

 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12505
  • Country: us
    • Personal site
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #18 on: April 25, 2019, 11:59:37 pm »
Sure, I'm not in a hurry :)
Alex
 

Offline noname4meTopic starter

  • Regular Contributor
  • *
  • Posts: 93
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #19 on: April 26, 2019, 06:26:27 pm »
Hi ataradov,

I did what you suggested and it appears that the memory location does not change (not to GPIOB location, nor to 0x12345678)

I was reading something about 'anonymous struct' and was wondering if that has something to do with it.

I'm not sure I get what an anonymous struct is?

I also tried changing the optimisation to none, just in case the compiler was optimising something
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12505
  • Country: us
    • Personal site
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #20 on: April 26, 2019, 06:31:45 pm »
Anonymous struct is something like this:
Code: [Select]
struct named_structure
{
    struct  // anonymous structure
    {
        int value1;
        int value2;
    };
};
This is not a problem here.

The problem here is that debugging tools have issues. If you see something that makes no sense - don't automatically trust the tool, verify it some other way.

I have no idea how to solve this, since I don't use debuggers myself (for that exact reason).
Alex
 

Offline noname4meTopic starter

  • Regular Contributor
  • *
  • Posts: 93
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #21 on: April 26, 2019, 06:33:06 pm »
Ok, so I have a further clue:

If I declare motor_t channel_one as a global variable, it appears not to work.

If I declare it within my main() function, it DOES work.

Is it possible to explain to me why?  I'm sure I've done something really simple and silly
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12505
  • Country: us
    • Personal site
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #22 on: April 26, 2019, 06:35:18 pm »
The debugging information for those two cases would be different. So again, I'm pretty sure this is just a tools issue.

Try to assign GPIOB and toggle a pin using that structure.
Alex
 
The following users thanked this post: noname4me

Offline noname4meTopic starter

  • Regular Contributor
  • *
  • Posts: 93
Re: STM32 GPIO Struct Syntax Help (using HAL)
« Reply #23 on: April 26, 2019, 06:51:16 pm »
You are an absolute star!

Thanks - I have to also share that the reason why this took so long was that I had connected only 1 motor up, to what I thought was channel_one, but actually to PA4 to PA7 (bluepill board)

The motor was not responding to anything (since there was no signal on those pins) - I just put a scope on PB12 to PB15 and the signal is there.

Really appreciate the help - and sorry for the merry-go-round...
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->