EmbDev.net

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


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


Rate this post
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
1
/*
2
 * main.c
3
 *
4
 *  Created on: 25.05.2011
5
 *      Author: moldov
6
 */
7
8
#include <avr/io.h>              // Most basic include files
9
#include <avr/interrupt.h>       // Add the necessary ones here
10
11
//flash char string_1[]="Prog Start";
12
//const char left[] PROGMEM ="no support\0x0ALLno support\0x0D\0x0A";
13
14
15
char command_indicator;
16
17
#define TXB8 0
18
#define RXB8 1
19
#define UPE  2
20
#define OVR  3
21
#define FE   4
22
#define UDRE 5
23
#define RXC  7
24
25
#define FRAMING_ERROR (1<<FE)
26
#define PARITY_ERROR (1<<UPE)
27
#define DATA_OVERRUN (1<<OVR)
28
#define DATA_REGISTER_EMPTY (1<<UDRE)
29
//#define RX_COMPLETE (1<<RXC)
30
31
32
//TCCR1A
33
#define WGM11 1
34
#define WGM10 0
35
#define COM1A1 7
36
#define COM1A0 6
37
#define COM1B1 5
38
#define COM1B0 4
39
//TCCR1B
40
#define WGM13 4
41
#define WGM12 3
42
#define CS0 0
43
#define CS1 1
44
#define CS2 2
45
46
47
void USART_Transmit( unsigned char data );
48
void USART_Init( unsigned int baud );
49
unsigned char USART_Receive( void );
50
51
ISR(USART_RXC_vect) {
52
  char status, data;
53
  status=UCSRA;
54
  data=UDR;
55
56
  if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0) {
57
58
  if (command_indicator == '$') {
59
      switch (data ) {
60
      case 'C':
61
        PORTB |= 0x03;
62
        PORTD |= 0x30;
63
        USART_Transmit('C');
64
      break;
65
66
      case 'V':
67
        PORTB &= ~0x03;
68
        PORTD &= ~0x30;
69
        USART_Transmit('V');
70
      break;
71
72
      case 'Z':
73
        PORTB |= (1 << 3);
74
        USART_Transmit('Z');
75
      break;
76
77
      case 'X':
78
        PORTB &= ~(1 << 3);
79
        USART_Transmit('X');
80
      break;
81
82
      case 'F':
83
        PORTB |= (1 << 0);
84
        PORTB |= (1 << 1);
85
        USART_Transmit('F');
86
         USART_Transmit(13);
87
            USART_Transmit(10);
88
89
      break;
90
91
      case 'B':
92
        PORTB &= ~(1 << 0);
93
        PORTB &= ~(1 << 1);
94
        USART_Transmit('B');
95
      break;
96
97
98
      case 'L':
99
        USART_Transmit('L');
100
      break;
101
102
      case 'R':
103
        USART_Transmit('R');
104
      break;
105
106
      case '0':
107
        USART_Transmit('0');
108
        OCR1AL = 0x00; // 0x00FF это число 256
109
        OCR1BL = 0x00; // 0x00FF это число 256
110
      break;
111
112
      case '1':
113
        USART_Transmit('1');
114
        OCR1AL = 0x0F; // 0x00FF это число 256
115
        OCR1BL = 0x0F; // 0x00FF это число 256
116
      break;
117
118
      case '2':
119
        USART_Transmit('2');
120
        OCR1AL = 0x3F; // 0x00FF это число 256
121
        OCR1BL = 0x3F; // 0x00FF это число 256
122
      break;
123
124
      case '3':
125
        USART_Transmit('3');
126
        OCR1AL = 0x6F; // 0x00FF это число 256
127
        OCR1BL = 0x6F; // 0x00FF это число 256
128
      break;
129
130
      case '4':
131
        USART_Transmit('4');
132
        OCR1AL = 0x9F; // 0x00FF это число 256
133
        OCR1BL = 0x9F; // 0x00FF это число 256
134
      break;
135
136
      case  '5':
137
        USART_Transmit('5');
138
        OCR1AL = 0xCF; // 0x00FF это число 256
139
        OCR1BL = 0xCF; // 0x00FF это число 256
140
      break;
141
      }
142
      command_indicator = 0x00;
143
144
145
    }
146
    if(data =='$') {//If we send command "$" shows that
147
    command_indicator = '$';
148
      //PORTB &= ~(1 << 3);
149
    }
150
  }
151
152
}
153
void USART_Transmit( unsigned char data ) {
154
  /* Wait for empty transmit buffer */
155
  while ( !( UCSRA & (1<<UDRE)) ); /* Put data into buffer, sends the data */
156
  UDR = data;
157
}
158
void USART_Init( unsigned int baud ) {
159
  /* Set baud rate */
160
  UBRRH = (unsigned char)(baud>>8);
161
  UBRRL = (unsigned char)baud;
162
163
  /* Enable receiver and transmitter */
164
  UCSRB = (1<<RXEN)|(1<<TXEN);
165
  UCSRC = (1<<URSEL)|(0<<USBS)|(3<<UCSZ0);  // Set frame format: 8data, 1stop bit
166
  UCSRB |= (1 << RXCIE); // Enable the USART Recieve Complete interrupt (USART_RXC)
167
  sei();// Enable the Global Interrupt Enable flag so that interrupts can be processed
168
}
169
unsigned char USART_Receive( void ) {
170
  /* Wait for data to be received */
171
  while ( !(UCSRA & (1<<RXC)) ); /* Get and return received data from buffer */
172
  return UDR;
173
}
174
// ***********************************************************
175
// Main program
176
//
177
int main(void) {
178
179
  //DDRD=0xFF;
180
  DDRD = 0x30; //4-5-й пин порта D настроим как выход
181
  DDRB = 0xFF; //
182
  // Настройка TIMER1 для генерации ШИМ
183
184
185
  TCCR1A = 0x00; //stop Timer
186
187
  TCNT1H = 0xFF; // 11111111
188
  TCNT1L = 0x00; // 00000000
189
190
  /* регистр OCR1A состоит из двух 8-ми битных регистров OCR1AH и OCR1AL запись в них нужно проводить в указанной последовательности! */
191
  OCR1AH = 0x00;
192
  OCR1AL = 0x00;
193
  OCR1BH = 0x00;
194
  OCR1BL = 0x00;
195
  ICR1H  = 0x00;
196
  ICR1L  = 0x00;
197
198
  //Got from OR Project
199
  TCCR1A = 0xA1;
200
  TCCR1B = 0x0A;
201
202
  //Скорость USART 115200 при кварцевом генераторе 7.3MHz USART_Init (0x03);
203
  USART_Init (0x03);
204
  USART_Transmit('O');//Передаем при включении
205
  USART_Transmit('k');//сообщение "Ok!", что свидетельствует
206
  USART_Transmit('!');//о правильно работе программы
207
  USART_Transmit(0x0d);//переход в начало строки
208
  USART_Transmit(0x0a);//переход на новую строку
209
  while(1) {}             // Infinite loop; define here the
210
211
}

And code for ARM linux
1
#include <fcntl.h>    /* open() and O_XXX flags */
2
//#include <stdio.h>
3
#include <unistd.h>
4
//#include <sys/stat.h>  /* S_IXXX flags */
5
//#include <unistd.h>  /* close() */
6
7
int main (int argc, char ** argv)
8
{
9
  const char * name = "/home/kiss.txt";
10
  int mode = 0777;
11
  int flags = O_WRONLY | O_CREAT | O_EXCL;
12
  
13
  int fd = open (name , flags, mode);
14
  printf ("File descriptor is: %d", fd);
15
  
16
17
  int tty_fd = open ("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY );
18
  if (tty_fd == -1) {
19
    perror ("open_port:Unable to open /dev/ttyS0 - ");
20
  }
21
  else {
22
        unsigned char c='D';
23
24
    fcntl (tty_fd, F_SETFL,0);
25
26
        while (1) {
27
      if (read(tty_fd,&c,1)>0)        {
28
                write(STDOUT_FILENO,&c,1);
29
                write(fd,&c,1);
30
31
            }              // if new data is available on the serial port, print it out
32
            if (read(STDIN_FILENO,&c,1)>0)  {
33
                write(tty_fd,&c,1);
34
                write(fd,&c,1);
35
            }
36
    }
37
    
38
    close (tty_fd);
39
  }
40
    
41
    close (fd);
42
}

Could some one help me with this

von Martin T. (mthomas) (Moderator)


Rate this post
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.

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.