Ryan TC
Ryan TC

Reputation: 45

Transmitting over UART on STM32

Hello i am using an STM32 as the host mcu for a SI4362 radio. the radio uses SPI communication to initialize. although i am stepping through meaning i believe to be receiving correct CTS from the radio it is not working. i would like to transmit everything i send and receive from the SPI out to a UART terminal for debug.

Here is a sample of what i am sending:

   uint8_t RF_POWER_UP[7] = { 0x02, 0x01, 0x01, 0x01, 0xC9, 0xC3, 0x80};

I would like to send the bytes over SPI as designated in the datasheet, and then promptly send the output over UART for debugging more easily something like this:

    uart_buf_len = sprintf(uart_buf, "\r\nSPI Test\r\n");
    HAL_UART_Transmit(&huart1, (uint8_t*) uart_buf, uart_buf_len, 100)

    HAL_GPIO_WritePin(SDN_GPIO_Port, SDN_Pin, GPIO_PIN_RESET);
    HAL_GPIO_WritePin(SS_GPIO_Port, SS_Pin, GPIO_PIN_RESET);
    HAL_SPI_Transmit(&hspi1, RF_POWER_UP, 7, 50);

    //convert RF_POWER_UP to something the UART can transmit in plain text 

    HAL_UART_Transmit(&huart1, RF_POWER_UP, 7, 50);
    do {
        HAL_SPI_Receive(&hspi1, (uint8_t*) spi_buf, 1, 100);
        
    //convert spi buf to something the UART can transmit in plain text 

        HAL_UART_Transmit(&huart1, (uint8_t*) spi_buf, 1, 100);
    } while (*spi_buf != 0xFF);
    HAL_GPIO_WritePin(SS_GPIO_Port, SS_Pin, GPIO_PIN_SET);

so i feel that writing a function might make the most sense. i found a helpful post using memcopy, but it converted the bytes into the equivalent ascii value basically returning the equal result. i am now actively searching the correct and appropriate return value for the needed function as well as propper code inside. i just wanted to make the edits to the post in the meantime as requested.

thank you

Upvotes: 0

Views: 1213

Answers (1)

Ryan TC
Ryan TC

Reputation: 45

I found some helpful tools on this stack overflow that gave me what i needed. i dont quite understand whats going on with the shifting. if someone would like to bring clarity i would appreciate that.

void btox(char *xp, const char *bb, int n) {
    const char xx[] = "0123456789ABCDEF";
    while (--n >= 0)
        xp[n] = xx[(bb[n >> 1] >> ((1 - (n & 1)) << 2)) & 0xF];
}

from what i gather i am calling the program with a pointer to the first address of an empty string identified in main using xp and likewise the byte aray using bb. also passing the size of the array of bytes as sizeof would only pass the size of the address.

xx[] is the array containing defining characters to be used for the byte to string conversion. so running through a while loop until n-1 >= to zero and i start to get very lost very fast.

it is working but i don't fully understand the code.

Upvotes: 0

Related Questions