Porting code from AVR to STM32 ?

Status
Not open for further replies.

bianchi77

Advanced Member level 4
Joined
Jun 11, 2009
Messages
1,313
Helped
21
Reputation
44
Reaction score
20
Trophy points
1,318
Location
California
Activity points
9,442
Guys,

I want to port from AVR to STM32CubeMx...
Please correct me if I'm wrong
Code:
STM32 :
/* Exchange a byte */
static
BYTE xchg_spi (     /* Returns received data */
    BYTE dat[1]     /* Data to be sent */
)
{
    while(HAL_SPI_GetState(&hspi2) != HAL_SPI_STATE_READY);
    HAL_SPI_Receive(&hspi2,dat,1,100);
    return dat[1];
}
 
AVR :
/* Exchange a byte */
static
BYTE xchg_spi (     /* Returns received data */
    BYTE dat        /* Data to be sent */
)
{
    SPDR = dat;
    loop_until_bit_is_set(SPSR, SPIF);
    return SPDR;
}

Thanks
 

I want to port from AVR to STM32CubeMx...
Please correct me if I'm wrong

The compiler can do this task much better than us, showing the errors and their exact location.
 

In the STM32 version of your code:
- why define 'dat' as a single element byte array; just define it as a byte variable (as is done in the AVR version)
- as 'dat' is a single element array, the ONLY valid index into that array is 'dat[0]' - check your return statement array indexing!!!!!
- if you are trying to perform an exchange (as the AVR does where it sends the 'dat' parameter value and passes back the received value from the SPI exchange) then you should use the HAL_SPI_TransmitReceive function and you really need to write the logically equivalent function and not try to blindly do a line-by-line translation.
- the parameter passing semantics are different: The AVR version passes the 'dat' parameter by value but the STM32 version passes it by reference.
Unfortunately, those are logical/programming errors and the compiler will probably not see those as errors.
Susan
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…