cancel
Showing results for 
Search instead for 
Did you mean: 

Accessing peripheral with pointers

ulao
Associate III
 
4 REPLIES 4
ulao
Associate III

I guess I was thinking that was for the title...

This is more of a code related question.

So st likes to dump the auto generated code.

/* Private variables ---------------------------------------------------------*/

ADC_HandleTypeDef hadc1;

ADC_HandleTypeDef hadc2;

etc..

I want to expose them to a struct and was planning to just make pointers.

typedef struct

{

void *pADC1;

void *pADC2;

} peripheral_Pointers;

then in the main I can

peripheral_Pointers *peripheral;

peripheral->pADC1 = hadc1;

peripheral->pADC2 = hadc2;

the thinking here is that my struct now has access to them. but I guess you can not just make a generic pointer to them because they are typedef structs of ADC_HandleTypeD.

Is there some way to cast as a generic pointer?

ulao
Associate III

or do I just need this

peripheral->pADC1 = &hadc1;

peripheral->pADC2 = &hadc2;

JPeac.1
Senior

Assuming you want a void pointer in the typedef then you have to cast the struct member when you reference it:

(ADC_HandleTypeDef  *) (peripheral->pADC1) = &hadc1;

or, in the typedef if you don't require a void (better way):

typedef struct

{

ADC_HandleTypeDef* pADC1;

ADC_HandleTypeDef* pADC2;

} peripheral_Pointers;

That way you maintain type consistency with pointers.

TDK
Guru

Implicitly converting from (ADC_HandleTypeDef *) to (void *) is fine. Converting the other way requires a cast. In this case it seems like the best option is to make them the correct pointer type in the struct.

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