Forum: ARM programming with GCC/GNU tools Atmega and ARM UART connection. The odd behavior.

von Igor G. (Company: Schlumberger) (moldov)

Rate this post
0 useful
not useful
So, it's kind a complex problem and not related to AVR only, but I think 
that problem is in Atmega.

- I have FOSCAM WI-FI camera based on ARM NUC745 which runes under 
uClinux. There is UART port on that camera which is connected with my 
Atmega board. In the same time that UART on ARM board used for console 
output and input.

- I can send the commands through web interface provided by the camera 
and this commands goes through ARM board to Atmega board through UART.

-I've written a program for Atmega to read the data which comes to UART 
from ARM. And all the data seems to be received by Atmega. Regarding to 
the data received, Atmega  sent data from Atmega to ARM board through 
UART. In VMLAB I can see that data is coming out in the oscilloscope. 
But ARM board doesn't receive that data. When I disconnect Atmega board 
from ARM board and connect ARM board to the terminal from my iMac all 
the data which has been sent by Atmega quickly appears in the terminal 
and program which I've written for ARM board receives all the data.

It's pretty strange behavior. Seems that Terminal connection and Atmega 
serial connections are different and I cannot realize why:(((

Here is the code for Atmega

 * main.c
 *  Created on: 25.05.2011
 *      Author: moldov

#include <avr/io.h>              // Most basic include files
#include <avr/interrupt.h>       // Add the necessary ones here

//flash char string_1[]="Prog Start";
//const char left[] PROGMEM ="no support\0x0ALLno support\0x0D\0x0A";

char command_indicator;

#define TXB8 0
#define RXB8 1
#define UPE  2
#define OVR  3
#define FE   4
#define UDRE 5
#define RXC  7

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
//#define RX_COMPLETE (1<<RXC)

#define WGM11 1
#define WGM10 0
#define COM1A1 7
#define COM1A0 6
#define COM1B1 5
#define COM1B0 4
#define WGM13 4
#define WGM12 3
#define CS0 0
#define CS1 1
#define CS2 2

void USART_Transmit( unsigned char data );
void USART_Init( unsigned int baud );
unsigned char USART_Receive( void );

  char status, data;

  if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0) {

  if (command_indicator == '$') {
      switch (data ) {
      case 'C':
        PORTB |= 0x03;
        PORTD |= 0x30;

      case 'V':
        PORTB &= ~0x03;
        PORTD &= ~0x30;

      case 'Z':
        PORTB |= (1 << 3);

      case 'X':
        PORTB &= ~(1 << 3);

      case 'F':
        PORTB |= (1 << 0);
        PORTB |= (1 << 1);


      case 'B':
        PORTB &= ~(1 << 0);
        PORTB &= ~(1 << 1);

      case 'L':

      case 'R':

      case '0':
        OCR1AL = 0x00; // 0x00FF это число 256
        OCR1BL = 0x00; // 0x00FF это число 256

      case '1':
        OCR1AL = 0x0F; // 0x00FF это число 256
        OCR1BL = 0x0F; // 0x00FF это число 256

      case '2':
        OCR1AL = 0x3F; // 0x00FF это число 256
        OCR1BL = 0x3F; // 0x00FF это число 256

      case '3':
        OCR1AL = 0x6F; // 0x00FF это число 256
        OCR1BL = 0x6F; // 0x00FF это число 256

      case '4':
        OCR1AL = 0x9F; // 0x00FF это число 256
        OCR1BL = 0x9F; // 0x00FF это число 256

      case  '5':
        OCR1AL = 0xCF; // 0x00FF это число 256
        OCR1BL = 0xCF; // 0x00FF это число 256
      command_indicator = 0x00;

    if(data =='$') {//If we send command "$" shows that
    command_indicator = '$';
      //PORTB &= ~(1 << 3);

void USART_Transmit( unsigned char data ) {
  /* Wait for empty transmit buffer */
  while ( !( UCSRA & (1<<UDRE)) ); /* Put data into buffer, sends the data */
  UDR = data;
void USART_Init( unsigned int baud ) {
  /* Set baud rate */
  UBRRH = (unsigned char)(baud>>8);
  UBRRL = (unsigned char)baud;

  /* Enable receiver and transmitter */
  UCSRB = (1<<RXEN)|(1<<TXEN);
  UCSRC = (1<<URSEL)|(0<<USBS)|(3<<UCSZ0);  // Set frame format: 8data, 1stop bit
  UCSRB |= (1 << RXCIE); // Enable the USART Recieve Complete interrupt (USART_RXC)
  sei();// Enable the Global Interrupt Enable flag so that interrupts can be processed
unsigned char USART_Receive( void ) {
  /* Wait for data to be received */
  while ( !(UCSRA & (1<<RXC)) ); /* Get and return received data from buffer */
  return UDR;
// ***********************************************************
// Main program
int main(void) {

  DDRD = 0x30; //4-5-й пин порта D настроим как выход
  DDRB = 0xFF; //
  // Настройка TIMER1 для генерации ШИМ

  TCCR1A = 0x00; //stop Timer

  TCNT1H = 0xFF; // 11111111
  TCNT1L = 0x00; // 00000000

  /* регистр OCR1A состоит из двух 8-ми битных регистров OCR1AH и OCR1AL запись в них нужно проводить в указанной последовательности! */
  OCR1AH = 0x00;
  OCR1AL = 0x00;
  OCR1BH = 0x00;
  OCR1BL = 0x00;
  ICR1H  = 0x00;
  ICR1L  = 0x00;

  //Got from OR Project
  TCCR1A = 0xA1;
  TCCR1B = 0x0A;

  //Скорость USART 115200 при кварцевом генераторе 7.3MHz USART_Init (0x03);
  USART_Init (0x03);
  USART_Transmit('O');//Передаем при включении
  USART_Transmit('k');//сообщение "Ok!", что свидетельствует
  USART_Transmit('!');//о правильно работе программы
  USART_Transmit(0x0d);//переход в начало строки
  USART_Transmit(0x0a);//переход на новую строку
  while(1) {}             // Infinite loop; define here the


And code for ARM linux
#include <fcntl.h>    /* open() and O_XXX flags */
//#include <stdio.h>
#include <unistd.h>
//#include <sys/stat.h>  /* S_IXXX flags */
//#include <unistd.h>  /* close() */

int main (int argc, char ** argv)
  const char * name = "/home/kiss.txt";
  int mode = 0777;
  int flags = O_WRONLY | O_CREAT | O_EXCL;
  int fd = open (name , flags, mode);
  printf ("File descriptor is: %d", fd);

  int tty_fd = open ("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY );
  if (tty_fd == -1) {
    perror ("open_port:Unable to open /dev/ttyS0 - ");
  else {
        unsigned char c='D';

    fcntl (tty_fd, F_SETFL,0);

        while (1) {
      if (read(tty_fd,&c,1)>0)        {

            }              // if new data is available on the serial port, print it out
            if (read(STDIN_FILENO,&c,1)>0)  {
    close (tty_fd);
    close (fd);

Could some one help me with this

von Martin T. (mthomas) (Moderator)

Rate this post
0 useful
not useful
This might be caused by flow-control. The USB-serial cable used to 
conncet camera to Mac provides RTS and CTS-lines. Maybe the camera just 
waits for a clear to send on the interface but your board with the AVR 
on it does not connect this line and so the module keeps the data to be 
sent in the output buffer. Try to disable flow-control on the module for 
a test. If data gets lost because one off the devices processes incoming 
data too slow you need to implement flow-control.


Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]

Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.