简体   繁体   English

如何访问另一个结构内的结构字段指针并正确编写?

[英]How to access struct fields pointers inside another struct and write correctly?

I have the following structs (from a library I'm using) with some fields and I would like to assign with OR operation a new value. 我有以下带有某些字段的结构(来自我正在使用的库),我想用OR操作分配一个新值。 But I'm debugging and I can't see how anything is writing there in stm32l4xx_hal_tim.c file: 但是我正在调试,在stm32l4xx_hal_tim.c文件中看不到任何内容:

 typedef struct
 {
   TIM_TypeDef              *Instance;     /*!< Register base address  */
   TIM_Base_InitTypeDef     Init;          /*!< TIM Time Base required parameters */
   HAL_TIM_ActiveChannel    Channel;       /*!< Active channel                    */
   DMA_HandleTypeDef        *hdma[7];      /*!< DMA Handlers array This array is accessed by a @ref DMA_Handle_index */
   HAL_LockTypeDef          Lock;          /*!< Locking object                    */
   __IO HAL_TIM_StateTypeDef   State;         /*!< TIM operation state               */
 }TIM_HandleTypeDef;


typedef struct
{
  __IO uint32_t CR1;         /*!< TIM control register 1,                   Address offset: 0x00 */
  __IO uint32_t CR2;         /*!< TIM control register 2,                   Address offset: 0x04 */
  __IO uint32_t SMCR;        /*!< TIM slave mode control register,          Address offset: 0x08 */
  __IO uint32_t DIER;        /*!< TIM DMA/interrupt enable register,        Address offset: 0x0C */
  __IO uint32_t SR;          /*!< TIM status register,                      Address offset: 0x10 */
  __IO uint32_t EGR;         /*!< TIM event generation register,            Address offset: 0x14 */
  __IO uint32_t CCMR1;       /*!< TIM capture/compare mode register 1,      Address offset: 0x18 */
  __IO uint32_t CCMR2;       /*!< TIM capture/compare mode register 2,      Address offset: 0x1C */
  __IO uint32_t CCER;        /*!< TIM capture/compare enable register,      Address offset: 0x20 */
  __IO uint32_t CNT;         /*!< TIM counter register,                     
 } TIM_TypeDef;

I have a part of code where I defined: TIM_HandleTypeDef TIMER_Struct; 我定义了一部分代码:TIM_HandleTypeDef TIMER_Struct;

And I would like to access the field "CR1" of TIM_TypeDef struct that is "*Instance" field of TIM_HandleTypeDef. 我想访问TIM_TypeDef结构的字段“ CR1”,即TIM_HandleTypeDef的“ * Instance”字段。 So I have done it by this way in a function DRV_TIMER_init(): 因此,我在函数DRV_TIMER_init()中通过这种方式完成了此操作:

  #include "main.h"
  #include "stm32l4xx_hal_tim.h"

  uint32_t uwPrescalerValue = 0;
  TIM_HandleTypeDef TIMER_Struct;
  void DRV_TIMER_init(void);

 int main(void)
 {  

   DRV_TIMER_init();
   while(1)
   {

   }

 }

 //where uint32_t SystemCoreClock = 4000000; in other system source file.
 void DRV_TIMER_init(void)
 {
  uwPrescalerValue = (uint32_t)(SystemCoreClock / 1000000) - 1;
  TIMER_Struct.Init.Period            = 100 - 1;
  TIMER_Struct.Init.Prescaler         = uwPrescalerValue;
  TIMER_Struct.Init.ClockDivision     = 0; // these accesses work

  TIMER_Struct.Instance -> CR1 |= 0x01 << 3; // this no works

 }

Even if I write directly: 即使我直接写:

 TIMER_Struct.Instance -> CR1 = 0xFFFFFFFF; 

It stills without having effect. 它仍然没有效果。

I think It could be a fact that I'm not controlling appropiately the pointer access or similar. 我认为这可能是事实,我没有适当地控制指针访问或类似操作。 But I don't see how can I access and modify the content of the commented field. 但是我看不到如何访问和修改注释字段的内容。 Since I can see (debug mode) how the rest of struct fields updates are writen correctly. 既然我可以看到(调试模式)其余结构域更新是如何正确编写的。

Any correction suggested here? 这里有建议更正吗?

TIMER_Struct.Instance -> CR1 = 0xFFFFFFFF; 

I try different ways to get it with no success. 我尝试不同的方法来获得成功。 I need new ideas. 我需要新主意。

I think you forgot to define TIMER_Struct.Instance 我认为您忘记定义TIMER_Struct.Instance

TIM_HandleTypeDef TIMER_Struct;
TIMER_Struct.Instance = TIM1;
//Now you can access TIMER_Struct.Instance
TIMER_Struct.Instance->CR1 = (uint32_t)0xFFFFFFFF;

But I prefer to use CMSIS for writing to registers. 但是我更喜欢使用CMSIS写入寄存器。 No need for the HAL. 无需HAL。 With CMSIS writing to a register could look like: 使用CMSIS写入寄存器的过程可能类似于:

TIM1->CR1=(uint32_t)0xFFFFFFFF
TIM_TypeDef              *Instance;

is just a pointer which is not pointing to anywhere so you can't dereference it 只是一个指针,它没有指向任何地方,所以您不能取消引用它

you have defined somewhere in the lib a macro like this: 您已经在lib中的某个地方定义了这样的宏:

#define TIM    (TIM_TypeDef*)0xDEADBEEF;

this is how you map the registers to the memory 这是将寄存器映射到存储器的方式

try modifying your code like this: 尝试像这样修改代码:

TIMER_Struct.Instance = (TIM_Typedef*)0xDEADBEEF;

or just 要不就

TIMER_Struct.Instance = TIM;

than

 TIMER_Struct.Instance -> CR1 |= 0x01 << 3;

should work 应该管用

I have found the solution. 我找到了解决方案。 First you must enable the peripheral clock. 首先,您必须启用外设时钟。 If not, there won't be any effect over timer registers, because the pointer structs fields were pointing directly to the hardware registers allocation. 否则,对定时器寄存器不会有任何影响,因为指针结构字段直接指向硬件寄存器分配。 This was the cause why I can't see any update when watching my struct var at debugging. 这就是为什么在调试时观看struct var时看不到任何更新的原因。

I was enabling clk (with hal_xxx_init()) after writing to the allocation. 写入分配后,我启用了clk(使用hal_xxx_init())。 That was my mistake. 那是我的错

Here in this part of code is the correction: 在这部分代码中的是更正:

//where uint32_t SystemCoreClock = 4000000; in other system source file.
void DRV_TIMER_init(void)
{
  uwPrescalerValue = (uint32_t)(SystemCoreClock / 1000000) - 1;
  TIMER_Struct.Init.Period            = 100 - 1;
  TIMER_Struct.Init.Prescaler         = uwPrescalerValue;
  TIMER_Struct.Init.ClockDivision     = 0; // these accesses work

  if (HAL_TIM_Base_Init(&TIMER_Struct) != HAL_OK)
  {
     /* Initialization Error */
     TIM_Error_Handler();
  }

  TIMER_Struct.Instance -> CR1 |= 0x01 << 3; // this works now

}

If you try to put the last sentence before the: 如果您尝试将最后一句放在:

HAL_TIM_Base_Init(&TIMER_Struct); 

no 没有

TIMER_Struct.Instance -> CR1 = xxx; //write mode register acces

will have effect. 会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM