Skip to main content
Graduate
October 16, 2023
Question

Need Help to understand this code

  • October 16, 2023
  • 10 replies
  • 5102 views

this is code from an Udemy tutorial but i couldn't get the part of 9 bit data transfer with no parity bit can anyone help me to understand that please

 

Screenshot 2023-10-12 195942.png

    This topic has been closed for replies.

    10 replies

    Super User
    October 16, 2023

    What part is tripping you up?

    With 9 bit no parity, it uses (reads and advances) 2 bytes of the buffer per character.

    AsrafulAuthor
    Graduate
    October 16, 2023

    can we talk in google meet then i will be able to explain my problem better it will be great help

     

    AsrafulAuthor
    Graduate
    October 16, 2023

    suppose my data is uint8_t data[5]={0xFF, 0xFF, 0xFF, 0xFF, 0xFF} now the len of my data is 5 so it will be pass as len. Now my loop will iterate 5 times if i am not wrong but the problem is when we cast it into uint16_t *pdata it will take first two item as one now then it will be mask as 9 bits and pTxBuffer will be increment twice and point to next 16 bit. now my confusion is withing 3 iteration my data array will be completed and if we take 0xFFFF and mask it 
    then the data become 0x1FF how they can be same

    Super User
    October 16, 2023

    > suppose my data is uint8_t data[5]={0xFF, 0xFF, 0xFF, 0xFF, 0xFF}

    This data is not 9-bit data.

    9-bit data would be stored as an array of uint16_t values.

    AsrafulAuthor
    Graduate
    October 16, 2023

    but what is saw in my pc if i cast 8bit array into 16 bit then it became from uint8_t data[5]={0xFF, 0xFF, 0xFF, 0xFF, 0xFF} to {0xFFFF, 0xFFFF, 0x00FF}

     

    Super User
    October 16, 2023

    True but irrelevant. The issue comes from you using a uint8_t array of length 5 to pass 5 parameters of 2 bytes each. Clearly 5 != 10 so, as you mentioned in your previous post, the function will read outside of the bounds of the array.

    So fix the problem: declare the array as uint16_t.

    Graduate II
    October 16, 2023

    Then find a tutor or teacher that you're paying. Perhaps find someone who can cover pointers, and data representation in memory

    Do you want to send as 9-bit? If so that data has to come from somewhere.

    You can't randomly cast a structure, and change it's content. You're just telling the compiler to process the data in memory a different way. The bytes in memory don't change, the size of the structure in bytes doesn't change.

    Where the peripheral is adding the parity bit, it takes 8-bits from you and adds it into the 9-bit shift register. If you want to send 9-bits of actual data, it needs to read at least that much from memory, it reads 16-bits as that's the next size up sufficient to do that.

    You could send an array of uint16_t

    uint16_t data[5]={0x1FF, 0x1FF, 0x1FF, 0x1FF, 0x1FF}

    AsrafulAuthor
    Graduate
    October 16, 2023

    Here is the problem I couldn't find someone thats why i am here 

    AsrafulAuthor
    Graduate
    October 16, 2023

    is there any discord channel or anything like that where i can talk with someone to clear this problem 

    Super User
    October 16, 2023

    >is there any discord channel or anything like that where i can talk with someone to clear this problem 

    Here you can find a friendly soul: https://www.fiverr.com/categories/programming-tech/electronics-engineering/embedded-systems-iot

    For general programming techniques, learning C -

    https://www.fiverr.com/categories/programming-tech/software-development/help-consultation

     

    Graduate II
    October 16, 2023

    ST uses casting as it's C, and they only want to implement one universal function.

    Most people aren't using 9-bit mode, and those using parity its somewhat invisible.

    9-bit mode is most often used where you're trying to differentiate data from a command/control channel. ie the additional bit flags as to whether the byte should be processed as data, or handled specially as a command. Sometimes used on 8051 in multi-processor mode, a special byte in the stream indicates the MCU you wish to address, whilst the other bytes you want the MCU to consume into it's buffers. ie a very simplistic networking protocol, where there's a constant stream of data, and you want ONE to cherry pick specific blocks of bytes.

    If you're not using 9-bit data forms, perhaps stop getting hung up on this, and come back to it when the need or reasoning becomes more apparent. Perhaps look at more examples of pointers and casting, and why they might be useful.

    Graduate II
    October 16, 2023

    You dont show config your handle and asked code is very clean commented. 

    Explorer II
    November 28, 2023

    I think I am running into something similar to this.  

    First time for me in MCP and 9bit mode.  Blowing my mind.

    I just picked up STM32 CubeIDE... been reading so many different links, articles, demos, I think it has caused me to forget some basics.  ha

     

    I want to send a 9 bit byte.    I'm in 9 bit mode..

    uint8_t tx_buff[]={270,66,67,68,69,70,71,72,73,74}; //0x10E BCDEFGHIJ in ASCII code

    In case you missed it, the first element is larger than 8 bits...

    The send -

    HAL_UART_Transmit_IT(&huart1, tx_buff, 10);

     

    HAL_UART_Transmit_IT(&huart1, tx_buff, 10);

     

     

    I get a compiler warning, of course, saying it doesn't like my 270.

     

    Duh.   OK,  let's make it uint16_t

     

    uint16_t tx_buff[]={270,66,67,68,69,70,71,72,73,74}; //0x10E BCDEFGHIJ in ASCII code

     

     

     

    New warning -

     

     

    Description	Resource	Path	Location	Type
    passing argument 2 of 'HAL_UART_Transmit_IT' from incompatible pointer type [-Wincompatible-pointer-types]	main.c	/Olimex_Eval_STM32-P152_Try2/Core/Src	line 118	C/C++ Problem

     

     

     

    How do I send a 9bit .. byte?   I actually haven't test it yet.  Was hoping to find a nice working example somewhere of MCP Master and Slave.

    :)

     

    Explorer II
    November 28, 2023

    I look up the function HAL_UART_Transmit_IT...

     

    It says -

    /**
     * @brief Sends an amount of data in non blocking mode.
     * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
     * the sent data is handled as a set of u16. In this case, Size must indicate the number
     * of u16 provided through pData.
     * @PAram huart Pointer to a UART_HandleTypeDef structure that contains
     * the configuration information for the specified UART module.
     * @PAram pData Pointer to data buffer (u8 or u16 data elements).
     * @PAram Size Amount of data elements (u8 or u16) to be sent
     * @retval HAL status
     */
    HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size)

    pData is still uint8_t  ............. 

     

    ../Drivers/STM32L1xx_HAL_Driver/Inc/stm32l1xx_hal_uart.h:747:82: note: expected 'const uint8_t *' {aka 'const unsigned char *'} but argument is of type 'uint16_t *' {aka 'short unsigned int *'}

    const uint16_t tx_buff[]={270,66,67,68,69,70,71,72,73,74}; //0x10E BCDEFGHIJ in ASCII code

    Adding const didn't help.

    hehe

     

    Explorer II
    November 28, 2023