6

I am writing code in C for STM32 with gcc compiler, I am tried suggestions. I am calling fuction from i2c.c via i2c.h definiton. May be I did mistake there. Thanks for comments.

(i2c.h)

#define Chip_TxBurst(wREG, wCNT, pbDATA)   \
    I2C_TxBurst((0xC0) | (((wREG) >> 7) & 0x1E), (wREG), (wCNT), (pbDATA))

(i2c.c)

void I2C_TxBurst (
uint16_t  bSLA,          /* I2C slave address */
uint16_t  bREG,          /* I2C sub-address */
uint16_t  bCNT,          /* The number of data which will be transmitted */
uint8_t  *pbDATA        /* Point to the first DATA item */
)
{
    uint8_t bIdx;

    for (bIdx = 0; bIdx < bCNT; bIdx++)
    {
        I2C_TxData(pbDATA[bIdx]);
    } /* for */

    } /* I2C_TxBurst */

(osd.c)

void OSD0_TEXT( uint8_t     *TEXT,
                    uint8_t     FONT_SIZE_X,
                    ......
                    )
{
    Chip_TxBurst(0x400, FONT_SIZE_X, TEXT);
}

(main)

main{
OSD0_TEXT("STAY FOLISH",11);
}
# #

(old question code)

void OSD0_TEXT (    uint8_t     *TEXT)
{
.....
TxBurst(0x400, strlen(TEXT), TEXT);
.....
}
main{
OSD0_TEXT("STAY FOLISH");
}
1
  • 2
    You pass char[N] to a function(OS0_TEXT) that expects uint8_t and you pass uint8_t to a function(strlen) that expects const char*.
    – P.P
    Apr 15, 2015 at 6:32

2 Answers 2

7

Strictly speaking, uint8_t and char may not always be compatible, since char has implementation-defined signedness and could be a signed type on some compilers.

In practice, casting to/from uint8_t to/from char will always work when char is used for storing ASCII characters, since there are no symbol tables with negative indices.

Side-notes:

  • You should always strive for const correctness, especially when creating pointers to string literals.

  • The size of string literals could be computed at compile-time, to save a little bit of time.

  • main() returns void on any bare metal ARM system I have ever seen. It does not return int, because that wouldn't make any sense (return to who?).

So the proper fix for your code is:

void OSD0_TEXT (const uint8_t* TEXT, size_t size)
{
  ...
  TxBurst(0x400, size, TEXT);
  ...
}

void main (void)
{
  static const char STR[] = "STAY FOOLISH"; // static to ensure it wont end up in RAM
  const size_t STR_LEN = sizeof(STR) - 1;

  OSD0_TEXT((const uint8_t*)STR, STR_LEN);
}
0
0
void OSD0_TEXT (    const char *TEXT)
{
.....
TxBurst(0x400, strlen(TEXT), TEXT);
.....
}
main{
OSD0_TEXT("STAY FOLISH");
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.