|
bladox.com Turbo SIM Toolkit Adapter Forums
|
View previous topic :: View next topic |
Author |
Message |
bugra.hasbek
Joined: 14 Jul 2009 Posts: 26
|
Posted: Fri Sep 18, 2009 11:00 am Post subject: input methods for turbo lite 2 |
|
|
Hi,
How can i provide input to my turbo programs from my computer?
I have a turbo lite 2 and a turbo programmer 2. Afaik tp 2 can only be used for reading debug messages from tl2. Is it possible to send input to tl 2 from tp 2?
tl 2 has opto-isolated and p1-13 ports. Can i use them for providing input from my computer?
Can i use usb functions such as usb_get_char(), usb_put_char() for sending input to my turbo programs?
Thanks |
|
Back to top |
|
|
bugra.hasbek
Joined: 14 Jul 2009 Posts: 26
|
Posted: Wed Sep 23, 2009 9:40 am Post subject: |
|
|
There have been some progress on my work. I found a similar thread here http://www.bladox.com/forum/viewtopic.php?t=345
As i understand, i should have 3 applications: tl 2 module, tp 2 module and pc client. I should send data from pc client to tp2 module, then pass this data to tl 2 module.
I managed to write a tp 2 module and pc client. I read input from pc and pass it to tp 2 module succesfully. I still dont know how to send data from tp 2 module to tl 2. i will try to use uart_get_char and uart_put_char.
Any help is appreciated.. |
|
Back to top |
|
|
pz Guest
|
Posted: Fri Sep 25, 2009 5:08 am Post subject: |
|
|
I would recommend to use Turbo Motion 2 for this. It's enhanced Turbo Lite 2 and has RS232. So then connection to PC is simple (has be done by many people). You can also connect other device, e.g. GPS and so. As an example look at autoalarm src.
As for TL2 reading something from TP2 - it would not easy and we have never tried it. In theory possible but would require some work. |
|
Back to top |
|
|
bugra.hasbek
Joined: 14 Jul 2009 Posts: 26
|
Posted: Fri Sep 25, 2009 8:08 am Post subject: |
|
|
Thanks for the reply pz
pz wrote: | I would recommend to use Turbo Motion 2 for this. It's enhanced Turbo Lite 2 and has RS232. So then connection to PC is simple (has be done by many people). You can also connect other device, e.g. GPS and so. As an example look at autoalarm src.
|
I am willing to recommend purchase of Turbo Motion 2 to my boss but i couldn't quite understand how to communicate with pc client (or tp 2) from tm 2. Correct me if i am wrong, I checked autoalarm source code but there were no part where tm 2 communicated with pc or tp 2. I would appreciate if you can show me a base skeleton of communication between turbo motion 2 and a pc client (or tp 2).
pz wrote: |
As for TL2 reading something from TP2 - it would not easy and we have never tried it. In theory possible but would require some work. |
Let's assume we choosed to use tl 2, which functions would you recommend to use to communicate with tp 2, theorically? |
|
Back to top |
|
|
bugra.hasbek
Joined: 14 Jul 2009 Posts: 26
|
Posted: Wed Sep 30, 2009 3:31 pm Post subject: |
|
|
I made some research on tp 2 - tl 2 communication. As i understand it, there are 2 possible ways of communicating:
1- spi
2- i2c
I choosed using spi and concentrate my research on spi communication. Correct me if i am wrong:
my master program ( tl 2 module) should:
1-) set itself master
2-) select slave (couldnt find how to do it )
3-) send sck (serial clock) (couldnt find how to do it )
4-) write data to spdr
5-) wait until data is transmitted (checking spif)
6-) read spdr
and repeat step 4-6
my slave program (tp 2 module) should:
1-) set itself slave
2-) wait until data is transmitted (checking spif)
3-) read spdr
4-) write spdr
and repeat 2-4
Is my understanding correct so far?
i wrote 2 programs using df.c as a template. my master program executes all these steps even if tl 2 is not connected to tp 2 via 50 pin
on the other hand my slave program always waits for spif to be set.
i would be grateful if you can show me where i am doing it wrong and also help with selecting slave and sending sck
master program (run on turbo lite 2)
Code: |
#include <config.h>
#include <turbo/turbo.h>
#include <avr/sfr_defs.h>
#include <stdlib.h>
#include <avr/io.h>
u8 PROGMEM m10 [] = "finished";
void action_menu (void *data)
{
init();
}
void init()
{
u8 dummy;
/// ENABLE SPI, SET AS MASTER
outb (SPCR, 0x50);
/// Disable double SPI clock
cbi (SPSR, SPI2X);
/// change direction of ss to output
sbi(DDRB, DD0);
delayMs (10);
/// SCK pin will be output
cbi(PORTB, PORT1);
sbi(DDRB, DD1);
/// MOSI pin wil be output
sbi(PORTB, PORT2);
sbi(DDRB, DD2);
/// MISO pin will be input
cbi(DDRB, DD3);
/// send data
outb(SPDR, 0X22);
/// wait until char is transmitted
loop_until_bit_is_set (SPSR, SPIF);
/// read dummy
dummy = inb(SPDR);
display_text_raw (m10, Q_DISPLAY_TEXT_USER_CLEAR);
}
void turbo_handler (u8 action, void *data)
{
switch (action)
{
case ACTION_APP_INIT:
break;
case ACTION_INSERT_MENU:
insert_menu ("spi comm");
break;
case ACTION_MENU_SELECTION:
stk_thread (action_menu, data);
}
}
|
slave program (runs on turbo programmer 2)
Code: |
#include <config.h>
#include <tprog/tprog.h>
#include <avr/sfr_defs.h>
#include <avr/io.h>
#include <stdlib.h>
#define MY_ACTION_SET_SLAVE 0X80
void turbo_handler (u8 action, USB_Data * data)
{
u8 answer[10];
switch (action)
{
case MY_ACTION_SET_SLAVE:
/// ENABLE SPI, SET AS SLAVE
outb (SPCR, 0x40);
/// Disable double SPI clock
cbi (SPSR, SPI2X);
/// change direction of ss to input
cbi(DDRB, DD0);
delayMs (10);
/// SCK pin will be input
sbi(PORTB, PORT1);
cbi(DDRB, DD1);
/// MOSI pin will be input
cbi(PORTB, PORT2);
cbi(DDRB, DD2);
/// MISO pin will be output
sbi(DDRB, DD3);
/// wait until char is received
loop_until_bit_is_set (SPSR, SPIF);
/// read char
answer[0] = inb(SPDR);
/// send dummy
outb(SPDR, 0X00);
usb_send(NO_ERROR, 1, answer);
default:
break;
}
}
|
|
|
Back to top |
|
|
pz Guest
|
Posted: Mon Oct 05, 2009 10:29 am Post subject: |
|
|
bugra.hasbek wrote: | Thanks for the reply pz
pz wrote: | I would recommend to use Turbo Motion 2 for this. It's enhanced Turbo Lite 2 and has RS232. So then connection to PC is simple (has be done by many people). You can also connect other device, e.g. GPS and so. As an example look at autoalarm src.
|
I am willing to recommend purchase of Turbo Motion 2 to my boss but i couldn't quite understand how to communicate with pc client (or tp 2) from tm 2. Correct me if i am wrong, I checked autoalarm source code but there were no part where tm 2 communicated with pc or tp 2. I would appreciate if you can show me a base skeleton of communication between turbo motion 2 and a pc client (or tp 2).
|
TM2 has RS232 so you would use serial connection to PC. And you also need TP2 and so.
In source of autoalarm look for GPS related code. TM2 has one more mcu which is not placed for TL2 - you can see there is one chip missing on the PCB. This chip does the RS232 and communicates with main m128 via i2c.
As for code - drop a line to info@ and we'll send a piece of code to see.
Quote: |
pz wrote: |
As for TL2 reading something from TP2 - it would not easy and we have never tried it. In theory possible but would require some work. |
Let's assume we choosed to use tl 2, which functions would you recommend to use to communicate with tp 2, theorically? |
As you write in another email I2C or SPI. But both would require more work then TM2. |
|
Back to top |
|
|
pz Guest
|
Posted: Mon Oct 05, 2009 10:33 am Post subject: |
|
|
bugra.hasbek wrote: | I made some research on tp 2 - tl 2 communication. As i understand it, there are 2 possible ways of communicating:
1- spi
2- i2c
I choosed using spi and concentrate my research on spi communication. Correct me if i am wrong:
my master program ( tl 2 module) should:
1-) set itself master
2-) select slave (couldnt find how to do it )
3-) send sck (serial clock) (couldnt find how to do it )
4-) write data to spdr
5-) wait until data is transmitted (checking spif)
6-) read spdr
and repeat step 4-6
my slave program (tp 2 module) should:
1-) set itself slave
2-) wait until data is transmitted (checking spif)
3-) read spdr
4-) write spdr
and repeat 2-4
Is my understanding correct so far?
i wrote 2 programs using df.c as a template. my master program executes all these steps even if tl 2 is not connected to tp 2 via 50 pin
on the other hand my slave program always waits for spif to be set.
i would be grateful if you can show me where i am doing it wrong and also help with selecting slave and sending sck
master program (run on turbo lite 2)
Code: |
#include <config.h>
#include <turbo/turbo.h>
#include <avr/sfr_defs.h>
#include <stdlib.h>
#include <avr/io.h>
u8 PROGMEM m10 [] = "finished";
void action_menu (void *data)
{
init();
}
void init()
{
u8 dummy;
/// ENABLE SPI, SET AS MASTER
outb (SPCR, 0x50);
/// Disable double SPI clock
cbi (SPSR, SPI2X);
/// change direction of ss to output
sbi(DDRB, DD0);
delayMs (10);
/// SCK pin will be output
cbi(PORTB, PORT1);
sbi(DDRB, DD1);
/// MOSI pin wil be output
sbi(PORTB, PORT2);
sbi(DDRB, DD2);
/// MISO pin will be input
cbi(DDRB, DD3);
/// send data
outb(SPDR, 0X22);
/// wait until char is transmitted
loop_until_bit_is_set (SPSR, SPIF);
/// read dummy
dummy = inb(SPDR);
display_text_raw (m10, Q_DISPLAY_TEXT_USER_CLEAR);
}
void turbo_handler (u8 action, void *data)
{
switch (action)
{
case ACTION_APP_INIT:
break;
case ACTION_INSERT_MENU:
insert_menu ("spi comm");
break;
case ACTION_MENU_SELECTION:
stk_thread (action_menu, data);
}
}
|
slave program (runs on turbo programmer 2)
Code: |
#include <config.h>
#include <tprog/tprog.h>
#include <avr/sfr_defs.h>
#include <avr/io.h>
#include <stdlib.h>
#define MY_ACTION_SET_SLAVE 0X80
void turbo_handler (u8 action, USB_Data * data)
{
u8 answer[10];
switch (action)
{
case MY_ACTION_SET_SLAVE:
/// ENABLE SPI, SET AS SLAVE
outb (SPCR, 0x40);
/// Disable double SPI clock
cbi (SPSR, SPI2X);
/// change direction of ss to input
cbi(DDRB, DD0);
delayMs (10);
/// SCK pin will be input
sbi(PORTB, PORT1);
cbi(DDRB, DD1);
/// MOSI pin will be input
cbi(PORTB, PORT2);
cbi(DDRB, DD2);
/// MISO pin will be output
sbi(DDRB, DD3);
/// wait until char is received
loop_until_bit_is_set (SPSR, SPIF);
/// read char
answer[0] = inb(SPDR);
/// send dummy
outb(SPDR, 0X00);
usb_send(NO_ERROR, 1, answer);
default:
break;
}
}
|
|
Yes, you are on right track but as I wrote it's not that easy. As for your questions look at atmega128 datasheet from www.atmel.com
Also google for "avr lib" or similar, there are such libraries.
For I2C there is thermometer example. |
|
Back to top |
|
|
bugra.hasbek
Joined: 14 Jul 2009 Posts: 26
|
Posted: Wed Oct 07, 2009 7:20 am Post subject: |
|
|
thanks for the input pz. I managed to send input from pc to tl2 and vice versa
I am posting my code in case someone needs it in the future. It is not perfect but gets the job done At the least, it can be used as reference by the absolute novices
By the way I am open to suggestions about my programs
TL 2 program: (make sure that you dont define debug in config.h, it messes with spi)
Code: |
#include <config.h>
#include <turbo/turbo.h>
#include <avr/io.h>
#undef DEBUG
#define WRITE 0xf0
#define READY 0xe0
#define R_U_READY 0xe1
#define READLEN 0xe2
#define READDATA 0xe3
#define READDATA1 0xe4
const u8 BUFFER_LIMIT = 100;
u8 GetChar(u8 command1, u8 command2)
{
u8 c;
/// wait for ready state
outb(SPDR, R_U_READY);
delayMs(20);
c = inb(SPDR);
while(c != READY)
{
outb(SPDR,R_U_READY);
delayMs(20);
c = inb(SPDR);
}
/// send command
/// wait for state change from ready
outb(SPDR, command1);
delayMs(20);
c = inb(SPDR);
while(c == READY) /// transformation is not done yet!
{
outb(SPDR,command2);
delayMs(20);
c = inb(SPDR);
}
return c;
}
void init()
{
/// disable spi interrupts
/// enable spi
/// master
outb (SPCR, 0x53);
/// Disable double SPI clock
cbi (SPSR, SPI2X);
delayMs (10);
/// change direction of ss to output
sbi(DDRB, DD0);
delayMs (10);
/// SCK pin will be output
cbi(PORTB, PORT1);
sbi(DDRB, DD1);
/// MOSI pin wil be output
sbi(PORTB, PORT2);
sbi(DDRB, DD2);
/// MISO pin will be input
cbi(DDRB, DD3);
}
void WriteToSlave(u8 a_count, u8 * a_buff)
{
u8 c, message[5];
/// send write request
outb(SPDR, WRITE);
loop_until_bit_is_set (SPSR, SPIF);
delayMs(50);
c = inb(SPDR);
/// send message length
outb(SPDR, a_count);
loop_until_bit_is_set (SPSR, SPIF);
delayMs(50);
c = inb(SPDR);
/// send message
for(u8 i=0;i < a_count; i++)
{
outb(SPDR, a_buff[i]);
loop_until_bit_is_set (SPSR, SPIF);
delayMs(50);
c = inb(SPDR);
}
return;
}
u8 ReadFromSlave(u8 * a_buff)
{
u8 c,len;
/// clear buffer
for(u8 i = 0 ; i < BUFFER_LIMIT ;i++)
a_buff[i] = '\0';
len = GetChar(READLEN, READLEN);
if(len == 0)
{
display_text_raw("NO INPUT AVAILABLE", Q_DISPLAY_TEXT_USER_CLEAR);
return 0x01;
}
for(u8 i = 0 ; i < len ; i++)
{
a_buff[i] = GetChar(READDATA, READDATA1);
}
a_buff[c] = '\0';
return 0x00;
}
void action_menu (void *data)
{
/// get data from pc
u8 l_imsi[BUFFER_LIMIT];
while(ReadFromSlave(l_imsi) != 0x00)
delayMs(10);
display_text_raw(l_imsi, Q_DISPLAY_TEXT_USER_CLEAR);
/// send data to pc
WriteToSlave(12, "Hello PC. This is TL 2");
/// get data again from pc
u8 l_sres_kc[BUFFER_LIMIT];
while(ReadFromSlave(l_sres_kc) != 0x00)
delayMs(10);
display_text_raw(l_sres_kc, Q_DISPLAY_TEXT_USER_CLEAR);
}
void turbo_handler (u8 action, void *data)
{
switch (action)
{
case ACTION_APP_INIT:
init();
break;
case ACTION_INSERT_MENU:
insert_menu ("spi test");
break;
case ACTION_MENU_SELECTION:
stk_thread (action_menu, data);
}
}
|
TP 2 module:
Code: |
#include "config.h"
#include <tprog/tprog.h>
#include <avr/sfr_defs.h>
#include <avr/io.h>
#include <stdlib.h>
#define MY_SPI_INIT_ACTION 0x80
#define MY_SPI_SEND_ACTION 0x81
#define WRITE 0xf0
#define READY 0xe0
#define R_U_READY 0xe1
#define READLEN 0xe2
#define READDATA 0xe3
#define READDATA1 0xe4
struct persistent
{
u8 m_length;
u8 m_index;
u8 m_data[32];
};
void turbo_handler (u8 action, USB_Data * data)
{
u8 c, i;
u8 l_index, l_length, l_buffer[128];
struct persistent *gp_w, *gp_r;
switch (action)
{
case MY_SPI_INIT_ACTION:
reg_action (ACTION_SIG_SPI);
init_turboadapter_dbg_port();
usb_send(NO_ERROR, 0, NULL);
break;
case MY_SPI_SEND_ACTION:
gp_r = app_data();
if(!gp_r)
efree(gp_r);
/// write string to persistent data
gp_w = emalloc(sizeof(struct persistent));
for(i = 0; i < data->len; i++)
wb(&gp_w->m_data[i], data->buf[i]);
wb(&gp_w->m_length, data->len);
wb(&gp_w->m_index, 0);
reg_app_data(gp_w);
usb_send(NO_ERROR, 0, NULL);
break;
case ACTION_SIG_SPI:
i = inb(SPDR);
gp_r = app_data();
switch(i)
{
case R_U_READY:
outb(SPDR, READY);
break;
case READLEN:
if(!gp_r)
{
outb(SPDR, 0);
delayMs(50);
}
else
{
l_length = rw(&gp_r->m_length);
outb(SPDR, l_length);
}
break;
case READDATA:
if(!gp_r)
{
outb(SPDR, '\0');
break;
}
l_length = rw(&gp_r->m_length);
l_index = rw(&gp_r->m_index);
c = rw(&gp_r->m_data[l_index]);
if(l_index >= l_length)
{
outb(SPDR, '\0');
}
/// this is the last character of input
else if(l_index == l_length - 1)
{
outb(SPDR, c);
/// reset persistent data
ww(&gp_r->m_index, 0);
ww(&gp_r->m_length, 0);
reg_app_data(gp_r);
}
else
{
outb(SPDR, c);
/// update persistent data
ww(&gp_r->m_index, l_index+1);
reg_app_data(gp_r);
}
break;
case READDATA1:
if(!gp_r)
{
outb(SPDR, '\0');
break;
}
l_length = rw(&gp_r->m_length);
l_index = rw(&gp_r->m_index) -1;
c = rw(&gp_r->m_data[l_index]);
outb(SPDR, c);
break;
default:
usb_put_char_nb(i);
outb(SPDR, READY);
break;
}
break;
default:
break;
}
}
void init_turboadapter_dbg_port ()
{
//PROG_DEBUG_SSn_ENABLE pin set it to log.1
//(in DEBUG MODE SSn is connected)
ext_sbi (EXT_PROG_DEBUG_SSn_ENABLE_PORT,
EXT_PROG_DEBUG_SSn_ENABLE_PORT_PIN);
//PROG_DEBUG_MOSI_ENABLE pin set it to log.1
//(in DEBUG MODE MOSI is connected)
ext_sbi (EXT_PROG_DEBUG_MOSI_ENABLE_PORT,
EXT_PROG_DEBUG_MOSI_ENABLE_PORT_PIN);
//PROG_DEBUG_MISO_ENABLE pin set it to log.1
//(in DEBUG MODE MOSI is connected)
ext_sbi (EXT_PROG_DEBUG_MISO_ENABLE_PORT,
EXT_PROG_DEBUG_MISO_ENABLE_PORT_PIN);
//PROG_DEBUG_SCK_ENABLE pin set it to log.1
//(in DEBUG mode SCK is connected)
ext_sbi (EXT_PROG_DEBUG_SCK_ENABLE_PORT,
EXT_PROG_DEBUG_SCK_ENABLE_PORT_PIN);
// enable SPI, set SPI SLAVE mode
outb (SPCR, 0xC3);
}
|
PC client:
Code: |
#include "config.h"
#include <stdio.h>
#include <tfslib.h>
#include <tproglib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/timeb.h>
#include <string.h>
#define MY_SPI_INIT_ACTION 0x80
#define MY_SPI_SEND_ACTION 0x81
#define WRITE 0xf0
#define READY 0xe0
#define R_U_READY 0xe1
#define READLEN 0xe2
#define READDATA 0xe3
#define READDATA1 0xe4
void SendString(u8 a_length, u8 * a_buff)
{
u8 incoming[100], incoming_length;
usb_send (MY_SPI_SEND_ACTION, a_length, a_buff, &incoming_length, incoming);
}
void HandleWriteRequest()
{
/// read message length
u8 length;
length = usb_get_char();
/// read message
u8 buff[length+1];
for(int i = 0 ; i < length; i++)
buff[i] = usb_get_char();
buff[length] = '\0';
printf("Received Message: %s\n", buff);
}
int main (int argc, char **argv)
{
u8 answer[2048];
u16 answerLen;
u8 *dev = USBDEVICE;
u8 *app_name = "imsi_reader_prog.trb";
u8 l_buffer[128];
open_device (dev);
if (select_app (app_name) != NO_ERROR)
{
close_device ();
fprintf (stderr, "Cannot open application %s\n", app_name);
exit (-1);
}
/// initialize turbo programmer 2 for spi communication with turbo lite 2
usb_send (MY_SPI_INIT_ACTION, 127, l_buffer, &answerLen, answer);
/// get data (which will be send to tl 2) from user
printf("Please enter data to send:\n");
scanf("%s", answer);
SendString(strlen(answer), answer);
while(1)
{
if(usb_chars() != 0)
{
u8 c = usb_get_char();
switch(c)
{
case WRITE:
printf("Received Write Request\n");
HandleWriteRequest();
printf("Please enter data to send\n");
scanf("%s", answer);
SendString(strlen(answer), answer);
break;
default:
printf("unhandled char: %x\n", c);
break;
}
}
}
close_device ();
}
|
|
|
Back to top |
|
|
pz Guest
|
Posted: Wed Oct 07, 2009 7:26 am Post subject: |
|
|
Brilliant! |
|
Back to top |
|
|
vir
Joined: 27 Aug 2012 Posts: 5
|
Posted: Tue Nov 06, 2012 4:36 pm Post subject: |
|
|
Hello!!
I am looking to this very good idea for comunicating the pc with turbolite. But I have some doubts.
In turbo programmer, when defining EXT_PROG_DEBUG_SCK_ENABLE_PORT_PIN, EXT_PROG_DEBUG_MISO_ENABLE_PORT_PIN, etc, is refering to Atmega PBx i/o pins? Specifically PB1, PB2 and PB3 ports or other?
Regards and thank you very much. |
|
Back to top |
|
|
sfogel
Joined: 13 May 2010 Posts: 4
|
Posted: Fri Nov 09, 2012 6:36 pm Post subject: |
|
|
Which libraries do I need to include to complie the TP 2 part? I am getting errors at link time. |
|
Back to top |
|
|
sfogel
Joined: 13 May 2010 Posts: 4
|
Posted: Fri Nov 09, 2012 6:55 pm Post subject: |
|
|
sfogel wrote: | Which libraries do I need to include to complie the TP 2 part? I am getting errors at link time. |
In fact I am getting errors of undefined symbol for us_put_char_nb an a couple others in avr-objdump
Any clue? |
|
Back to top |
|
|
pz
Joined: 12 Mar 2004 Posts: 1161
|
Posted: Tue Nov 13, 2012 8:24 am Post subject: |
|
|
sfogel wrote: | sfogel wrote: | Which libraries do I need to include to complie the TP 2 part? I am getting errors at link time. |
In fact I am getting errors of undefined symbol for us_put_char_nb an a couple others in avr-objdump
Any clue? |
I guess you mean usb_put_char_nb. It's used in turboadapter/module-src/turboadapter.c and turboadapter/module-src/btldr.c so look at turboadapter/module-src/Makefile |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|