We're on Discord! Please join our server now if you don't want to miss anything! (More info) | Join the UCC4 contest! (More info)

* WalrusIRC

You need to have 5 posts and not be part of restricted usergroups in order to use the WalrusIRC embedded shoutbox. However, you can also access our IRC channel called #CodeWalrus via EFnet.

Author Topic: TI Nspire uart  (Read 6689 times)

0 Members and 1 Guest are viewing this topic.

Offline TurtleForGaming

  • New User
  • Join Date: Jan 2018
  • Location:
  • Posts: 14
  • Post Rating Ratio: +1/-0
    • TurtleForGaming
  • Gender: Male
TI Nspire uart
« on: January 20, 2018, 01:44:33 pm »
So for around 2month now I'm developping an bluetooth interface (via UART) to the TI-Nspire. For that I use ndless and nspire-io (a bit modified to not lock the calc while waiting for message and echo remove). But for the past day I'm encoutering a little problem:  when i send a message to the calc (with or without bluetooth) I only get 32chars for exemple if I send:

Quote
%%|NUMS|10|TestA|TestB|TestC|TestD|TestE|TestF|TestG|TestH|TestI|TestJ
I will only get
Quote
%%|NUMS|10|TestA|TestB|TestC|Tes

which is a big problem for me and I don't really know why it append.

Here is my code:
Code: [Select]
#include <os.h>
#include <nspireio/uart.hpp>
#include <nspireio/console.hpp>

int key_already_pressed_ctrl = 0;
int key_already_pressed_contact = 0;
char* contact = "%%|NUMS|6|Name1|Name2|Name3|Name4|Name6|Name7";
int hasSelectedContact = 0;

// Modidification of the uart_getsn() function to remove the echo.


char* uart_getsn_mdf(char* str, int num) {
for(int i = 0; i < num-1; i++){
if(uart_ready()) {
char c = uart_getchar();
str[i] = c;
if(c == '\b'){ i -= 2; }
else if(c == '\r') { str[i] =0; return str; }
}
}
str[num] = 0;
return str;
}

char* uart_gets_mdf(char* str)
{
        return uart_getsn_mdf(str,50);
}

int numberOfCharsInArray(char* array) {
  return strlen(array);
}

int main(void)
{
//Write the program title
assert_ndless_rev(874);
nio_console csl;
nio_init(&csl,NIO_MAX_COLS,NIO_MAX_ROWS,0,0,NIO_COLOR_WHITE,NIO_COLOR_BLACK,TRUE);
nio_set_default(&csl);
nio_color(&csl,NIO_COLOR_BLACK,NIO_COLOR_WHITE);
nio_printf("               Nspire UART Chat by Samy.             ");
nio_printf("         Compiled the %s At %s        ",__DATE__,__TIME__);
nio_color(&csl,NIO_COLOR_BLACK,NIO_COLOR_LIGHTRED);
nio_printf("      Press  ESC key to exit and CTRL to send msg.    ");
nio_printf("                                                    ");
nio_color(&csl,NIO_COLOR_WHITE,NIO_COLOR_BLACK);
nio_puts("");

//Send that we are online
uart_printf("#$#CALC:ENTERING:SMS\r\n");

//Main loop
while(!isKeyPressed(KEY_NSPIRE_ESC)){
if(isKeyPressed(KEY_NSPIRE_CTRL) && !key_already_pressed_ctrl && !key_already_pressed_contact){
if(hasSelectedContact) {
nio_printf("<< ");
char output[90] = {0};
nio_getsn(output,90);
uart_printf("$#$SMS:");
int num = numberOfCharsInArray(output);
for(char* it = output; *it; ++it) {
uart_printf("%c",*it);
sleep(100);
}
uart_printf("\r\n");
} else {
nio_printf("You must select an contact !\r\n");
}
key_already_pressed_ctrl = 1;
}
if(isKeyPressed(KEY_NSPIRE_C) && !key_already_pressed_contact && !key_already_pressed_ctrl){
// START Contact select part
nio_clear(&csl);
//Title
nio_color(&csl,NIO_COLOR_BLACK,NIO_COLOR_WHITE);
nio_printf("                        Contact                      ");
nio_color(&csl,NIO_COLOR_WHITE,NIO_COLOR_BLACK);
//Search
nio_printf("Name search: ");
char output[90] = {0};
nio_getsn(output,90);
uart_printf("$#$UPDATENUM:%s\r\n",output);

nio_color(&csl,NIO_COLOR_BLACK,NIO_COLOR_WHITE);
nio_clear(&csl);
while(!uart_ready() && !isKeyPressed(KEY_NSPIRE_ESC)) {
nio_printf("                        Waiting                      ");
sleep(500);
}
nio_color(&csl,NIO_COLOR_BLACK,NIO_COLOR_WHITE);
nio_printf("                        Contact                      ");
nio_color(&csl,NIO_COLOR_WHITE,NIO_COLOR_BLACK);
nio_clear(&csl);

char input[1024] = {0};
//sleep(500);
uart_getsn_mdf(input,1024);

nio_puts(input);
nio_puts("Select your contact:");

contact = input;

        char* namestable[1024];
        char* s;
        int i=0;
        s = strtok(contact ,"|");
        while (s != NULL) {
        if(i>1) {
namestable[i] = s;
}
        if(i>2) {
        nio_printf("  %i) %s. \r\n",i-3,s);
                }
                s = strtok (NULL, "|");
                i++;
        }
        nio_printf("Change contact to (n)?");
        char selected[10] = {0};
        nio_getsn(selected,10);
        int ii = atoi(selected);
ii=ii+3;
        if(ii >= 0 && namestable[ii] != "None") {
                nio_printf("Changing send contact to %s. \r\n",namestable[ii]);
uart_printf("$#$CHANGENUM:%s\r\n",namestable[ii]);
hasSelectedContact = 1;
nio_clear(&csl);
nio_color(&csl,NIO_COLOR_BLACK,NIO_COLOR_WHITE);
// Display reponsive title
nio_printf(" Info: Now chating with: %s \r\n", namestable[ii]);
nio_printf("                                                     ");
nio_puts("");
        } else {
hasSelectedContact = 0;
nio_clear(&csl);
nio_color(&csl,NIO_COLOR_BLACK,NIO_COLOR_LIGHTRED);
nio_printf("                                                     ");
nio_printf("  Error: Error while selecting contact please retry. ");
nio_printf("          If the error persist contact samy :)       ");
nio_printf("                                                     ");
nio_color(&csl,NIO_COLOR_WHITE,NIO_COLOR_BLACK);
nio_puts("");
        }
nio_color(&csl,NIO_COLOR_WHITE,NIO_COLOR_BLACK);
        // END Contact select part
key_already_pressed_contact = 1;
}
if(!any_key_pressed()) {
key_already_pressed_ctrl = 0;
key_already_pressed_contact = 0;
}
if(uart_ready()) {
sleep(500);
char input[1024] = {0};
uart_gets_mdf(input);
contact = input;
nio_printf("%s\r\n",input);
nio_printf("%s",input);
nio_puts("");
}
}

//Send that we are offline
uart_printf("#$#CALC:LEAVING:SMS\r\n");

//Exiting
nio_puts("Offline message sended. Exiting ...");
nio_free(&csl);
return 0;
}


For those who doesn't want to understand all of my code here is the part where I get my messages:
Code: [Select]
if(uart_ready()) {
sleep(500);
char input[1024] = {0};
uart_gets_mdf(input);
nio_printf("%s",input);
nio_puts("");
}

And here are the modified function:
Code: [Select]
char* uart_getsn_mdf(char* str, int num) {
for(int i = 0; i < num-1; i++){
if(uart_ready()) {
char c = uart_getchar();
str[i] = c;
if(c == '\b'){ i -= 2; }
else if(c == '\r') { str[i] =0; return str; }
}
}
str[num] = 0;
return str;
}

char* uart_gets_mdf(char* str)
{
        return uart_getsn_mdf(str,50);
}

If somebody wanna help me I'm taking it.
PS: I'm french so sorry for mistakes


  • Calculators owned: TI Nspire
Samy

Offline Vogtinator

  • Full User
  • Join Date: Dec 2014
  • Location: Germany
  • Posts: 120
  • Post Rating Ratio: +4/-0
  • Instruction counter
    • @UCii1mkxAsrIGvjFwS80YSmg
    • /u/Vogtinator
    • Vogtinator
    • ../../../cgi-bin/acct-view.cgi?userid=87663#
Re: TI Nspire uart
« Reply #1 on: January 20, 2018, 04:17:49 pm »
This might be the FIFO running out of space - try to delay the sending or receive it in a tight loop.

Preferably, you register an interrupt handler on available input and read the whole fifo immediately.
  • Calculators owned: TI-Nspie CX CAS, Casio FX-85ES

Offline TurtleForGaming

  • New User
  • Join Date: Jan 2018
  • Location:
  • Posts: 14
  • Post Rating Ratio: +1/-0
    • TurtleForGaming
  • Gender: Male
Re: TI Nspire uart
« Reply #2 on: January 20, 2018, 05:29:39 pm »
I'm not a pro programmer just a student so what is the FIFO and how does I do that ?
  • Calculators owned: TI Nspire
Samy

Offline Vogtinator

  • Full User
  • Join Date: Dec 2014
  • Location: Germany
  • Posts: 120
  • Post Rating Ratio: +4/-0
  • Instruction counter
    • @UCii1mkxAsrIGvjFwS80YSmg
    • /u/Vogtinator
    • Vogtinator
    • ../../../cgi-bin/acct-view.cgi?userid=87663#
Re: TI Nspire uart
« Reply #3 on: January 20, 2018, 05:34:29 pm »
The FIFO is an internal buffer which the UART hardware stores data in. If data arrives faster than you read it out, it'll fill up and no new data can be processed.
Getting interrupt handling right is not trivial, it requires quite some trial-and-error. The easiest workaround is to decrease the baud rate or just make the transmitting side slower.
You could for instance add a delay of a few ms after you sent 32 B.
  • Calculators owned: TI-Nspie CX CAS, Casio FX-85ES

Offline TurtleForGaming

  • New User
  • Join Date: Jan 2018
  • Location:
  • Posts: 14
  • Post Rating Ratio: +1/-0
    • TurtleForGaming
  • Gender: Male
Re: TI Nspire uart
« Reply #4 on: January 20, 2018, 06:12:29 pm »
Can you decrease the baudrate of the Ti Nspire ?
  • Calculators owned: TI Nspire
Samy

Offline Vogtinator

  • Full User
  • Join Date: Dec 2014
  • Location: Germany
  • Posts: 120
  • Post Rating Ratio: +4/-0
  • Instruction counter
    • @UCii1mkxAsrIGvjFwS80YSmg
    • /u/Vogtinator
    • Vogtinator
    • ../../../cgi-bin/acct-view.cgi?userid=87663#
Re: TI Nspire uart
« Reply #5 on: January 20, 2018, 06:31:47 pm »
Yes, but I doubt that that would help.
If you stil want to try that, refer to the ARM pl010 manual.
  • Calculators owned: TI-Nspie CX CAS, Casio FX-85ES

Offline TurtleForGaming

  • New User
  • Join Date: Jan 2018
  • Location:
  • Posts: 14
  • Post Rating Ratio: +1/-0
    • TurtleForGaming
  • Gender: Male
Re: TI Nspire uart
« Reply #6 on: January 20, 2018, 08:05:25 pm »
Well I changed my android code to add 20mills sec each 10 char but no luck
  • Calculators owned: TI Nspire
Samy

Offline Vogtinator

  • Full User
  • Join Date: Dec 2014
  • Location: Germany
  • Posts: 120
  • Post Rating Ratio: +4/-0
  • Instruction counter
    • @UCii1mkxAsrIGvjFwS80YSmg
    • /u/Vogtinator
    • Vogtinator
    • ../../../cgi-bin/acct-view.cgi?userid=87663#
Re: TI Nspire uart
« Reply #7 on: January 20, 2018, 09:06:21 pm »
Did you try a different receiver to rule out issues on the transceiving side?

Maybe try a higher delay like 1s and send 10 chars per line.

Also, I just remembered that there's a bug in the uart_getchar function which is not fixed yet, can you try the function  from https://tiplanet.org/forum/viewtopic.php?f=20&t=20611&p=222899&hilit=nspire+uart#p222899 instead?
  • Calculators owned: TI-Nspie CX CAS, Casio FX-85ES

Offline TurtleForGaming

  • New User
  • Join Date: Jan 2018
  • Location:
  • Posts: 14
  • Post Rating Ratio: +1/-0
    • TurtleForGaming
  • Gender: Male
Re: TI Nspire uart
« Reply #8 on: January 20, 2018, 09:21:21 pm »
I have posted this bug  ;)
And I don't have an another module
  • Calculators owned: TI Nspire
Samy

Offline Vogtinator

  • Full User
  • Join Date: Dec 2014
  • Location: Germany
  • Posts: 120
  • Post Rating Ratio: +4/-0
  • Instruction counter
    • @UCii1mkxAsrIGvjFwS80YSmg
    • /u/Vogtinator
    • Vogtinator
    • ../../../cgi-bin/acct-view.cgi?userid=87663#
Re: TI Nspire uart
« Reply #9 on: January 20, 2018, 09:24:30 pm »
I have posted this bug  ;)

Right, whoops..

Quote
And I don't have an another module

Oscilloscope, pc with level shifters, a microcontroller kit?
  • Calculators owned: TI-Nspie CX CAS, Casio FX-85ES

Offline TurtleForGaming

  • New User
  • Join Date: Jan 2018
  • Location:
  • Posts: 14
  • Post Rating Ratio: +1/-0
    • TurtleForGaming
  • Gender: Male
Re: TI Nspire uart
« Reply #10 on: January 20, 2018, 09:27:14 pm »
I'have already tried that no luck
  • Calculators owned: TI Nspire
Samy

Offline Vogtinator

  • Full User
  • Join Date: Dec 2014
  • Location: Germany
  • Posts: 120
  • Post Rating Ratio: +4/-0
  • Instruction counter
    • @UCii1mkxAsrIGvjFwS80YSmg
    • /u/Vogtinator
    • Vogtinator
    • ../../../cgi-bin/acct-view.cgi?userid=87663#
Re: TI Nspire uart
« Reply #11 on: January 20, 2018, 09:40:53 pm »
Try this (untested):

Code: [Select]
char uart_getchar(void)
{
volatile unsigned *recv_buffer_reg = (unsigned*)0x90020000;
        volatile unsigned *fr = (unsigned*)0x90020018;
while(!(*fr & 0x10))
idle();
        return *recv_buffer_reg;
}

If this doesn't work, try replacing the idle call with just a semicolon for busy waiting.
« Last Edit: January 20, 2018, 09:48:46 pm by Vogtinator »
  • Calculators owned: TI-Nspie CX CAS, Casio FX-85ES

Offline TurtleForGaming

  • New User
  • Join Date: Jan 2018
  • Location:
  • Posts: 14
  • Post Rating Ratio: +1/-0
    • TurtleForGaming
  • Gender: Male
Re: TI Nspire uart
« Reply #12 on: January 20, 2018, 09:45:12 pm »
I'm getting:
Quote
error: can't convert « unsigned int* » to « volatile uint8_t* {aka volatile unsigned char*} » in the initialisation
         volatile uint8_t *fr = (unsigned*)0x90020018;
  • Calculators owned: TI Nspire
Samy

Offline Vogtinator

  • Full User
  • Join Date: Dec 2014
  • Location: Germany
  • Posts: 120
  • Post Rating Ratio: +4/-0
  • Instruction counter
    • @UCii1mkxAsrIGvjFwS80YSmg
    • /u/Vogtinator
    • Vogtinator
    • ../../../cgi-bin/acct-view.cgi?userid=87663#
Re: TI Nspire uart
« Reply #13 on: January 20, 2018, 09:49:01 pm »
As I wrote: Untested :D

Ok, fixed.
  • Calculators owned: TI-Nspie CX CAS, Casio FX-85ES

Offline TurtleForGaming

  • New User
  • Join Date: Jan 2018
  • Location:
  • Posts: 14
  • Post Rating Ratio: +1/-0
    • TurtleForGaming
  • Gender: Male
Re: TI Nspire uart
« Reply #14 on: January 20, 2018, 09:54:10 pm »
Well know my calc is blocked in a loop probably:
Code: [Select]
while(!(*fr & 0x10))
                idle();

And not displaying anything  ;D
  • Calculators owned: TI Nspire
Samy

 


You can also use the following HTML or bulletin board code to share it on your page or forum signature!


Also do not forget to check our affiliates below.
Planet Casio TI-Planet Calc.news BroniesQC BosaikNet Velocity Games