cancel
Showing results for 
Search instead for 
Did you mean: 

STM8S105 direct register access, pointers...

alexis2
Associate II
Posted on November 25, 2015 at 15:14

Hello everybody,  I'm working on the STM8 discovery board to implement some application which controls the STM8's GPIOs from an SPI master.

The code relies on SPI interrupts. I would like the master sends the address of the targeted register (15 bits) and a 16th one (MSB) which indicates the access mode (Read or Write).

I can't solve an issue about pointers. The interrupt code is the following :

&sharpdefine ACCESS_BIT 7

&sharpdefine READ_INPUT_ID 0x50

char cpt, temp, add;

uint8_t mode_add_MSB, add_LSB;

bool access; // '1' = W, '0' = R

INTERRUPT_HANDLER(SPI_IRQHandler, 10)

{    

    if (SPI_GetITStatus(SPI_IT_RXNE)!=RESET)

    {

        if(cpt==0) // Reception of the access mode bit and the 7 MSB address bits

        {

            while(SPI_GetFlagStatus(SPI_FLAG_RXNE)!=SET);

            mode_add_MSB = SPI->DR;

            access = mode_add_MSB>>ACCESS_BIT; // Insulation of the access bit

            mode_add_MSB&=~(1<<ACCESS_BIT); // Ignorance of the access bit in the MSB address bits

            cpt++;

            SPI->DR=cpt; // debug

        }

        if(cpt==1) // Reception of the LSB address bits

        {

            while(SPI_GetFlagStatus(SPI_FLAG_RXNE)!=SET);

            add_LSB = SPI->DR;

            cpt++;

            add=((mode_add_MSB<<8)|add_LSB)&add_LSB; // Concatenation of the 15 bit register address

            if(access==READ_MODE)

            {

                temp = (uint8_t *) add;   // error line 249 : I would like to affect in temp the content of the memory case which address is add.

                SPI->DR=temp;

            }

        }

    }

}

Can anybody can help me please ? Let me know if it's not clear

Thank you

 

The interrupt code is not complete yet but I have yet a problem with the targetting of the register. I would like to send, from the master, the address of the IDR register for the port targetted and read it. I got the following error code with Cosmic

&sharperror cpstm8 ..\..\src\stm8s_it.c:249(23+3) incompatible pointer assignment

    

#stm8-pointer-language-c-address
2 REPLIES 2
luca239955_stm1_st
Senior II
Posted on November 25, 2015 at 17:05

Hello,

you seem to have some confusion about 8 bit types, 16 bit types and pointers.

In your code, all variables are 8 bit, and that is probably not what you want, because you try to store a 16 bit value (the address) into at least onr of them (add)..

Coming to the specific error you mention, you write

                temp = (uint8_t *) add;

The compiler understands the following: take add (a char variable), cast it to a pointer to char (so it is now an address pointing to a char), and assign it to temp (another char variable). That makes char = pointer, which is forbidden in C.

If you want to store into temp the value at the address pointed by add, you can write

                temp = *(uint8_t *) add;

that will compile but I'm not sure it will do what you expect, since the address you get is always lower that 0xFF.

The standard way to access a char variable in the whole STM8 memory space is

char * address;

char data;

address = (char *)0x200;   // or any other value, fixed or coming from a calculation

data = *address;

Hope it helps.

alexis2
Associate II
Posted on November 27, 2015 at 10:49

Hi luca, thanks for your answer.

It helps yes, because I didn't know that we have to cast a value before affect it into a pointer. I was trying all writable syntax, but not the good one ^^ The concept of pointer is not easy for me, but it seems very usefull.

Now it works.

Thank you :D