cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F446 Serial: Union of two structures for transforming data not working

Ekthelionn
Associate

Hi...I am pretty new in stm32 world and this is my problem:

Context of the problem

I am transmitting data from a computer to the STM32F446ET through the USB port.

The string the PC sends is:

cadena = ["1","0","0","0","3","0","0","0","0","1","7","2","8","C","0","0","0","0","0","0","0","0","0","0"]

it is a simple string of 24 characters representing hex values.

The microcontroller receives the string and storage it in a union of structures as follows:

union datos

{

uint8_t imun[24]; //This is the vector where the mcu receives the chain.

struct

{

uint8_t hatsof; // start/stop

uint8_t dirx; //direction in x

uint8_t diry; //direction in y

uint8_t dirz; //direction in z

uint32_t prsc; //prescaler value

uint32_t arrd; //Auto-reload register value

uint32_t x; //distance in x

uint32_t y; //distance in y

uint32_t z; //distance in z

};

struct

{

uint8_t hatsofa; //Start/stop byte

uint8_t dirxa; //x direction byte

uint8_t dirya; //y direction byte

uint8_t dirza; //z direction byte

uint8_t prsca; //prescaler byte 1

uint8_t prscb; //prescaler byte 2

uint8_t prscc; //prescaler byte 3

uint8_t prscd; //prescaler byte 4

uint8_t arrda; //auto-reload byte 1

uint8_t arrdb; //auto-reload byte 2

uint8_t arrdc; //auto-reload byte 3

uint8_t arrdd; //auto-reload byte 4

uint8_t xa; //x distance byte 1

uint8_t xb; //x distance byte 2

uint8_t xc; //x distance byte 3

uint8_t xd; //x distance byte 4

uint8_t ya; //y distance byte 1

uint8_t yb; //y distance byte 2

uint8_t yc; //y distance byte 3

uint8_t yd; //y distance byte 4

uint8_t za; //z distance byte 1

uint8_t zb; //z distance byte 2

uint8_t zc; //z distance byte 3

uint8_t zd; //z distance byte 4

};

}u1;

Then I need to transform everything from hex string to hex values. I do it through this function:

void transforma2(void)

{

int i = 0;

// Here, each ascii value is changed to a numerical value:

for (i=0;i<24;i++)

{

if (u1.imun[i]<58)

u1.imun[i] = u1.imun[i] - 48;

else

u1.imun[i] = u1.imun[i] - 55;

}

// then each byte is used to be transformed into a numerical value:

u1.prsc = u1.prsca + u1.prscb*16 + u1.prscc*256 + u1.prscd*4096; //Prescaler value

u1.arrd = u1.arrda + u1.arrdb*16 + u1.arrdc*256 + u1.arrdd*4096; //Auto-reload value

u1.x = u1.xa + u1.xb*16 + u1.xc*256 + u1.xd*4096; // x value

u1.y = u1.ya + u1.yb*16 + u1.yc*256 + u1.yd*4096; // y value

u1.z = u1.za + u1.zb*16 + u1.zc*256 + u1.zd*4096; // z value

}

The problem:

The serial string received is right, the union contains the information according to what is expected and the function transform the information but just the 2 first times the function is called, the 3rd time everything gets messed up...

Can anyone help me?? Maybe I am not considering something....why could it work fine the first 2 times but and not anymore??

Note: the 3rd time and so on, when it is debugged, works fine...but running does not work... I do not understand.

2 REPLIES 2

> STM32F446 Serial:

> I am transmitting data from a computer to the STM32F446ET through the USB port.

So how do you transfer the data: through serial (UART) or through USB?

> everything gets messed up...

How exactly?

JW

In this way:

The hex string contains the following information:

hatsof = 1

dirx = 0

diry = 0

dirz = 0

prescaler = 3

auto-reload = 10000

x= 200

y= 0

z= 0

all this variables are in the first structure in the union. The first and second time I call the transforma2() function, the transformation from hex string to this values works. When I do it for the 3rd time, the information appears like this:

hatsof = 0

dirx = 0

diry = 0

dirz = 0

prescaler = 0

auto-reload = 10000

x= 200

y= 0

z= 0

0693W000000UtnrQAC.png

The problem is really weird because if I debug the code step by step, it works just fine and there is no problem at all...but when I run it, the values got wrong...

Here it is the capture of the 4th time I call the function:

0693W000000UtmpQAC.png

what could be wrong?? I something about the architecture or about the registers I am configuring wrong???