I have a piece of code that runs on an embedded system. Its job is to convert some ASCII characters into proprietary data. The data is stored in a multi-dimensional array and what appears to be the problem, although I am unable to confirm this with the hardware debugger, is that the bit variable is staying at value 2. This code works the first two times it is run, but on the third run it breaks and returns starts sending wrong data through the UART interface. I though maybe someone else analyzing this might be able to see what I'm missing. This is C99 which I am not too familiar with. Bleow is the entire function, but I think the problem is with the for statement?? Any help would be greatly appreciated!
void simple_uart_putstring(uint8_t *str, uint16_t length)
{
//send data bits
uint_fast8_t index = 0;
uint8_t ch = str[index++];
uint_fast8_t bitCount = 0;
int index2 = 0;
int bit = 0;
if (length > 1)
{
while (length >= index)
{
if (bitCount < 2)
{
if (length < 10)
{
//send sync bits
simple_uart_put(254);
simple_uart_put(223);
bitCount = 2;
} else {
//send sync bits and add scrolling
simple_uart_put(254);
simple_uart_put(222);
bitCount = 2;
}
}
//send each bit for each letter in the string
for (uint_fast8_t i = 0; i < 5; i++)
{
index2 = (int)ch;
bit = (int)i;
simple_uart_put(matrix[index2 - 32][bit]);
bitCount++;
}
ch = str[index++];
}
//the main controller is expecting 150 bits total to continue to send bit until 150
while (bitCount <= 150)
{
simple_uart_put(0);
bitCount++;
if(bitCount >= 150)
{
bitCount = 0;
break;
}
}
bitCount = 0;
}
}
And here is a sample of the array:
const uint8_t matrix[59][5] =
{
{ 0, 0, 0, 0, 0}, //space
{ 0, 125, 0, 0, 0}, //!
{ 0, 112, 0, 112, 0}, //"
{127, 127, 127, 127, 127 }, //#
{ 18, 42, 107, 36, 0}, //$
{ 50, 52, 22, 38, 0}, //%
{ 38, 89, 57, 6, 9}, //&
{ 64, 48, 0, 0, 0}, //'
{ 0, 0, 62, 65, 0}, //(
{ 0, 65, 62, 0, 0}, //)
{ 20, 8, 62, 8, 20}, //*
{ 0, 8, 28, 8, 0}, //+
{ 1, 6, 0, 0, 0}, //,
{ 0, 8, 8, 8, 0}, //-
{ 3, 3, 0, 0, 0}, //.
{ 2, 4, 8, 16, 32}, //
{ 62, 69, 73, 62, 0}, //0
{ 1, 33, 127, 1, 0}, //1
{ 35, 67, 69, 49, 0}, //2
{ 34, 73, 73, 54, 0}, //3
{ 12, 20, 36, 127, 0}, //4
{ 114, 81, 81, 78, 0}, //5
{ 30, 41, 73, 6, 0}, //6
{ 64, 71, 72, 112, 0}, //7
{ 54, 73, 73, 54, 0}, //8
{ 48, 73, 74, 60, 0}, //9
{ 0, 54, 54, 0, 0}, //:
{ 0, 1, 54, 0, 0}, //;
{ 0, 8, 20, 34, 0}, //<
{ 0, 20, 20, 20, 0}, //=
{ 0, 34, 20, 8, 0}, //>
{ 32, 64, 69, 72, 48}, //?
{ 62, 65, 93, 93, 112}, //@
{ 63, 72, 72, 63, 0 }, //a
{ 127, 73, 73, 54, 0 }, //b
{ 62, 65, 65, 34, 0}, //c
{ 127, 65, 34, 28, 0 }, //d
{ 127, 73, 73, 65, 0}, //e
{ 127, 72, 72, 64, 0}, //f
{ 62, 65, 73, 47, 0}, //g
{ 127, 8, 8, 127, 0}, //h
{ 0, 65, 127, 65, 0},//i
{ 6, 65, 126, 64, 0}, //j
{ 127, 8, 20, 99, 0}, //k
{ 127, 1, 1, 1, 0}, //l
{ 127, 32, 24, 32, 127}, //m
{ 127, 16, 8, 127, 0}, //n
{ 62, 65, 65, 62, 0}, //o
{ 127, 72, 72, 48, 0}, //p
{ 60, 70, 66, 61, 0}, //q
{ 127, 76, 74, 49, 0}, //r
{ 50, 73, 73, 38, 0}, //s
{ 0, 64, 127, 64, 0}, //t
{ 126, 1, 1, 126, 0},
{ 127, 1, 2, 124, 0},
{ 126, 1, 6, 1, 126},
{ 99, 28, 28, 99, 0},
{ 112, 8, 8, 127, 0},
{ 71, 73, 81, 97, 0}};
And the uart send method:
void simple_uart_put(uint8_t cr)
{
NRF_UART0->TXD = (uint8_t)cr;
while (NRF_UART0->EVENTS_TXDRDY!=1)
{
// Wait for TXD data to be sent
}
NRF_UART0->EVENTS_TXDRDY=0;
}
An example of this working would be if the input string is "AB" and the length = 2; It should send the following bytes via UART:
{254, 223, 63, 72, 72, 63, 0, 127, 73, 73, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, etc....}
The first two bytes are sync bytes, the next five come from the array matrix[33][0 to 4] because ASCII 'A' = 65 and 65-32 = 33. Then the next five come from ASCII 'B' = 66 and 66 - 32 = 34 so they are sent from matrix[34][0 to 4]. Then the next n = 150 - bitNumber are sent as 0 because the main controller is expecting 150 bytes always.