SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By NetElemental
#36452
Hey,
I have a AVR-P40-USB-8535 prototyping board and ubuntu linux. I've finally decided to try and get this whole rs232 thing working because it seems like it could greatly simplify debugging. The only problem is, I can't actually get it to work. I can compile for and program the uC just fine, but whenever I use gtkterm to try and connect, all I get is
Code: Select all
Control signals read: Inappropriate ioctl for device
The command for gtkterm I'm using is
Code: Select all
sudo gtkterm -p /dev/bus/usb/001/010
The code on the uC is
Code: Select all
/*
    5-10-07
    Copyright Spark Fun Electronics© 2007
    Nathan Seidle
    nathan at sparkfun.com
    
    Example basic printf input/output
*/

#include <stdio.h>
#include <avr/io.h>

#define FOSC 16000000
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1

#define sbi(var, mask)   ((var) |= (uint8_t)(1 << mask))
#define cbi(var, mask)   ((var) &= (uint8_t)~(1 << mask))

#define STATUS_LED 0

//Define functions
//======================
void ioinit(void);      // initializes IO
static int uart_putchar(char c, FILE *stream);
uint8_t uart_getchar(void);

static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);

void delay_ms(uint16_t x); // general purpose delay
//======================

int main (void)
{
	uint8_t x = 0;
	
    ioinit(); //Setup IO pins and defaults

    while(1)
    {
		x++;
	
		printf("Test it! x = %d\r\n", x);
		
		sbi(PORTB, STATUS_LED);
		delay_ms(500);

		cbi(PORTB, STATUS_LED);
		delay_ms(500);
    }
   
    return(0);
}

void ioinit (void)
{
    //1 = output, 0 = input
    DDRB = 0xFF  ^ (1 << 6);//0b11101111; //PB4 = MISO 
    DDRC = 0xFF;//0b11111111; //
    DDRD = 0xFF  ^ (1 << 0);//0b11111110; //PORTD (RX on PD0)

    //USART Baud rate: 9600
    UBRRH = MYUBRR >> 8;
    UBRRL = MYUBRR;
    UCSRB = (1<<RXEN)|(1<<TXEN);
    UCSRC = (1<<URSEL)|(3<<UCSZ0);
    
    stdout = &mystdout; //Required for printf init
}

static int uart_putchar(char c, FILE *stream)
{
    if (c == '\n') uart_putchar('\r', stream);
  
    loop_until_bit_is_set(UCSRA, UDRE);
    UDR = c;
    
    return 0;
}

uint8_t uart_getchar(void)
{
    while( !(UCSRA & (1<<RXC)) );
    return(UDR);
}

//General short delays
void delay_ms(uint16_t x)
{
  uint8_t y, z;
  for ( ; x > 0 ; x--){
    for ( y = 0 ; y < 80 ; y++){
      for ( z = 0 ; z < 40 ; z++){
        asm volatile ("nop");
      }
    }
  }
}
Any help?
By inventore123
#36462
What USB device are you using? Maybe the FT232 breakout?
I usually open a terminal and type "tail -f /var/log/messages" before connecting the USB device to the pc, then among other informations I see which file is associated with the FT232, usually /dev/ttyUSB0, then type Ctrl-C to quit.
I do not use gkterm to see debugging data, I prefer to use "sudo stty 19200 -F /dev/ttyUSB0" where 19200 is the baudrate (see "man stty" for other useful options), then just "sudo cat /dev/ttyUSB0" and the debug data is printed to the treminal itself. Again, Ctrl-C to quit.
By NetElemental
#36482
Awesome - that looks much better than what I was doing before. I get this from tail:
Code: Select all
Oct 14 10:37:25 ubuntu kernel: [193674.802125] usb 1-3: new full speed USB device using ohci_hcd and address 12
Oct 14 10:37:26 ubuntu kernel: [193675.779618] usb 1-3: configuration #1 chosen from 1 choice
Oct 14 10:37:26 ubuntu kernel: [193675.781067] ftdi_sio 1-3:1.0: FTDI USB Serial Device converter detected
Oct 14 10:37:26 ubuntu kernel: [193675.781075] drivers/usb/serial/ftdi_sio.c: Detected FT232BM
Oct 14 10:37:26 ubuntu kernel: [193675.781234] usb 1-3: FTDI USB Serial Device converter now attached to ttyUSB0
Oct 14 10:37:29 ubuntu kernel: [193677.442007] usb 1-3: usbfs: interface 0 claimed by ftdi_sio while 'brltty' sets config #1
Oct 14 10:37:29 ubuntu kernel: [193677.443844] ftdi_sio 1-3:1.0: device disconnected
Oct 14 10:37:29 ubuntu kernel: [193676.585160] ftdi_sio ttyUSB0: FTDI USB Serial Device converter now disconnected from ttyUSB0
Oct 14 10:37:46 ubuntu kernel: [193686.244058] usb 1-3: USB disconnect, address 12
It says that the device disconnects before I get a chance to do anything(3 seconds between when I connect and when it disconnects, and then another 17 before it says the usb device is disconnected). Could this be a problem with the code, or do you have any more ideas?

BTW, yes I am using the FT232 converter on this board: http://www.sparkfun.com/commerce/produc ... ucts_id=32
By inventore123
#36484
So you plug the USB cable, it says it is connected, but after 3 seconds it disconnects with no apparent reason, is it right?

Usually the FT232 works well with linux... did you install any driver to get it working like this?

Anyway try "lsusb" (if it is not a recognized command try "/sbin/lsusb") it should list all usb devices connected, see if it is listed among these
By NetElemental
#36491
Thanks for all the help - I finally got it to connect. While I couldn't use your method of reading messages, I managed to get gtkterm to read them. I'll figure out why later - I have no preference which to use, so for me it's just whichever works. The problem was apparently ubuntu specific, and I just had to remove the brltty packages.

Now my problem is that all I receive is giberish, something which is probably more difficult to solve than it not connecting. Any thoughts? In any case, thanks for all the help.
By inventore123
#36542
Is gkterm's baudrate the same as the ATmega?
What routines are you using in the micro to send data through the serial port?
By NetElemental
#36775
I found the problem, if anybody runs into this in the future - while my code seemed to be set up at 9600 bps, when I set my terminal to receive at 1200 bps I got the correct output. I'm not sure what's up with that, but whatever, I'll go for it. If anybody does know, however, I would be very interested.

Thanks for all the great help!
By mrmeval
#38893
You'll need to dump BRLTTY, remove the package from your distribution.

http://www.ladyada.net/learn/arduino/lesson0-lin.html
If you see something like this

[ 1900.712000] ftdi_sio 2-10:1.0: FTDI USB Serial Device converter detected
[ 1900.712000] drivers/usb/serial/ftdi_sio.c: Detected FT232BM
[ 1900.712000] usb 2-10: FTDI USB Serial Device converter now attached to ttyUSB0
[ 1901.868000] usb 2-10: usbfs: interface 0 claimed by ftdi_sio while 'brltty' sets config #1
[ 1901.872000] ftdi_sio ttyUSB0: FTDI USB Serial Device converter now disconnected from ttyUSB0
[ 1901.872000] ftdi_sio 2-10:1.0: device disconnected

That means you have not uninstalled brltty and you should try again.
http://wiki.laptop.org/go/Serial_adapters
Conflict with Braille TTY (brltty) software
Ubuntu distros come with the brltty package installed. This package conflicts with using the device as a plain serial port. Upon insertion the brltty driver takes over the device and disconnects the /dev/ttyUSBn device. The solution to this is to remove or disable the brltty package. On Ubuntu you can do this with

sudo apt-get remove brltty
By Jasica Smith
#47484
It is very helpful material that u`ve provided.
By pablo
#62606
NetElemental wrote:I found the problem, if anybody runs into this in the future - while my code seemed to be set up at 9600 bps, when I set my terminal to receive at 1200 bps I got the correct output. I'm not sure what's up with that, but whatever, I'll go for it. If anybody does know, however, I would be very interested.

Thanks for all the great help!
I was pretty confused with this as well. I tried a few values of baudrate and I also didn't know what was the clock freq of my AVR (Im a noob). I read some of the data sheets (the big 300+ page manual) and it started to make more sense.

So after much trying, I found out that you can use the default multiplier (16) up to 4800 bauds. If you jump to 9600, there's too much errors or something (the data specs also show this), and it shows garbage. If you want more than 4800 bauds in *asynchronous* mode, you can set the U2X bit in the USCRA register to 1, and then the multiplier is 8 and you can get higher frequencies. I havent tried more than 9600 though.

This is more or less what I have now:
Code: Select all
/* This is the default clock speed (for the uc and the UART) for the AVR Atmega32 */
#define OSCSPEED	1000000		/* in Hz */
	/* U2X
	 * Writing this bit to one will reduce the divisor of the baud
	 * rate divider from 16 to 8 effectively doubling the transfer
	 * rate for *asynchronous* communication.
	 */
    UCSRA = UCSRA | 0b00000010;
    unsigned int BaudRate = OSCSPEED / (8 * Baud) - 1;		
    //set BaudRate into registers
    UBRRH = (unsigned char) (BaudRate>>8);
    UBRRL = (unsigned char) BaudRate;
By Aravind.P.A
#72095
:D Thank You very much for this post this was very helpful for . Thank You.
I was not able to send or receive data but once I changed the baud rate it was fine .
By signal7
#72436
One thing to check. 1200 baud is 1/8th of 9600 baud, so I'd guess that clock divider on your chip is set at the default value of 8. You should be able to set it to 1 at the start of your program so that the core runs at full speed.