<DIV>Hi Dusan,</DIV>
<DIV> </DIV>
<DIV>Thanks for your reply and kindly help. Actually I've figured out the solution for Ethernut II RS-485 with the great help of Mr.Damian. Here I would like to give my credit to Mr.Damian. I couldn't finish it without his help.</DIV>
<DIV> </DIV>
<DIV>It's a little bit difference from yours since I use Ethernut II and use UART0 as RS232 and UART1 as RS485. I would like to contribute out since it looks like so far I havn't seen any similar post. Hope this will help some beginners like me to get into the board faster and save some time for more other codes. The procedure are:</DIV>
<DIV> </DIV>
<DIV>0. Follow the hardware manual to configure the UART0 as RS232 and UART1 as RS485...;</DIV>
<DIV> </DIV>
<DIV>1. Follow the hardware manual and decide which pin you want to use as a toggle pin. Here I use PD5 which is set by shorten Pin1&2, 3&4 of JP2 on board;</DIV>
<DIV> </DIV>
<DIV>2. Take a look at board and find the correct screw pin for 485A & 485B. Here I found a mistake on hardware manual that the position of RS485 A/B is different from which is drawn in manual. Actually it's swapped. On board you should read D(DC) - S(Shield) - A(RS485A) and B(RS485B). Hook up A(RS485A) to High(+) and B(RS485B) to Low(-) of your own 485 device or RS232/485 converter;</DIV>
<DIV> </DIV>
<DIV>3. Modify nut/include/cfg/modem.h. Since I want to use UART0 as RS232 and UART1 as RS485. So what I did is comment out all the "define UART1***" under "#if 1"except the following three definitions. Then change the UART0 to UART1. And change the UART1_HDX_BIT value to 5 since I decide to use PD5 as a toggle pin;</DIV>
<DIV> </DIV>
<DIV>#if 1<BR>/* Define half duplex for RS485 */<BR>#define UART1_HDX_PORT PORTD<BR>#define UART1_HDX_DDR DDRD<BR>#define UART1_HDX_BIT 5<BR>#endif</DIV>
<DIV> </DIV>
<DIV>4. Since I use ICCAVR, I use nutconf.exe under \ethernut\nut to rebuild the Nut/OS libs. After running this utility, all the new built libs will be automatically copied into ICCAVR's library directory. You dont need to do anything to copy by yourself;</DIV>
<DIV> </DIV>
<DIV>5. Here are a piece of my initialization code which is working fine on Ethernut II board. I'm using latest Nut/OS 3.4.2.</DIV>
<DIV> </DIV>
<DIV>// More other includes files .....</DIV>
<DIV>#include <dev/usartavr.h></DIV>
<DIV> </DIV>
<DIV>int got;<BR>FILE *uart0, *uart1;<BR>u_long baud = 9600, flag = USART_MF_HALFDUPLEX;<BR></DIV>
<DIV>NutRegisterDevice(&devUsartAvr0, 0, 0);<BR>NutRegisterDevice(&devUsartAvr1, 0, 0);<BR>    <BR>uart0 = fopen("uart0", "r+b");<BR>uart1 = fopen("uart1", "r+b");<BR> <BR>_ioctl(_fileno(uart0), UART_SETSPEED, &baud);<BR>_ioctl(_fileno(uart1), UART_SETSPEED, &baud);<BR>_ioctl(_fileno(uart1), UART_SETFLOWCONTROL, &flag);<BR>    <BR>for (;;)<BR>{<BR>   got = fread(RecBuf, 1, 32, uart1);<BR>   fprintf(uart0, "Receive %d bytes...\n", got);<BR>   </DIV>
<DIV>   NutSleep(1000);</DIV>
<DIV><BR>   fwrite(WriteBuf, 1, 11, uart1); </DIV>
<DIV>   fprintf(uart0, "Write 11 bytes...\n");</DIV>
<DIV><BR>   NutSleep(1000);<BR>}</DIV>
<DIV> </DIV>
<DIV>6. Rebuild All of your project and you should be able to use RS485 now.</DIV>
<DIV> </DIV>
<DIV>BTW, you've to get the latest usartavr.c which I got from Mr.Damian and I'm sure you can download it from somewhere else...</DIV>
<DIV> </DIV>
<DIV>Let me know if I mistake anything or any questions about it. Thanks.</DIV>
<DIV> </DIV>
<DIV>Regards,</DIV>
<DIV>Alex</DIV>
<DIV><BR><B><I>Dusan Ferbas <dferbas@dfsoft.cz></I></B> wrote:</DIV>
<BLOCKQUOTE class=replbq style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #1010ff 2px solid">Hi Alex,<BR><BR>you need to modify SW. First is to enable direction pin toggling (see 1). <BR>Then properly initialize usart - use new driver and set USART_MF_HALFDUPLEX <BR>mode (see 3 below).<BR><BR>You can live also with original Nut sources if you will set speed after <BR>changing flow control mode. See example below.<BR><BR>You can omit setting speed if you use Damian Slee's bug correction. Look <BR>for it in discussion archive 17th February, 16th April (BugFix: Usart half <BR>duplex mode... )<BR><BR>Anyway: CVS maintainers what about to incorporate Damian's fixes ?<BR><BR>----<BR>1) nut/include/cfg/modem.h<BR><BR>For RS485 you do not need RTS/CTS so only following has to be enabled. <BR>Whole block is by default "#if 0". Do not enable (allow define) for <BR>..._RTS/CTS_.... This adds more code and also processing within USART driver.<BR><BR>#if 1<BR>/* Define half duplex for
 RS485 */<BR>#define UART0_HDX_PORT PORTD<BR>#define UART0_HDX_DDR DDRD<BR>#define UART0_HDX_BIT 4<BR>#endif<BR><BR>2) recompile Nut/OS libraries with 'make install'<BR><BR>3) example of initialization: call as InitUart0(9600);<BR><BR>FILE *InitUart0(u_long baud)<BR>{<BR>FILE *uart;<BR><BR>//u_long cooked_mode = 0; /* disable <BR>replacing \n with \r\n */<BR><BR>NutRegisterDevice(&devUsartAvr0, 0, 0);<BR>uart = fopen("uart0", "r+b"); /* select binary <BR>mode */<BR><BR>u_long flags = USART_MF_HALFDUPLEX;<BR>_ioctl(_fileno(uart0), UART_SETFLOWCONTROL, &flags); /* due to <BR>bug in Nut/OS 3.5.0 do this BEFORE SETSPEED */<BR>_ioctl(_fileno(uart), UART_SETSPEED, &baud);<BR>//following is set automatically when binary mode is selected<BR>//_ioctl(_fileno(uart), UART_SETCOOKEDMODE, &cooked_mode);<BR><BR>return uart;<BR>}<BR><BR>>But I cann't get anything from my slave device so far. My questions are:<BR>><BR>>1. Is there anything special for the hardware setting?
 Did I setup my <BR>>board correctly?<BR>>2. I knew the 485 on Ethernut is half duplex and have to use PD5 / PD4 to <BR>>control the data direction, but based on Nut/OS, do I still need to do <BR>>this? Is there any existing RS-485 UART driver for Nut/OS now?<BR>>3. If the above answer is NO, do I have to wirte ISR for Mega128 to send <BR>>and receive package?<BR>><BR>>Thanks for any kindly help.<BR>><BR>>Sincerely,<BR>>Alex<BR><BR><BR>Dusan Ferbas<BR>www.dfsoft.cz <BR><BR>_______________________________________________<BR>En-Nut-Discussion mailing list<BR>En-Nut-Discussion@egnite.de<BR>http://www.egnite.de/mailman/listinfo.cgi/en-nut-discussion</BLOCKQUOTE><p>
                <hr size=1><font face=arial size=-1>Do you Yahoo!?<br>Yahoo! Movies - <a href="http://movies.yahoo.com/showtimes/movie?mid=1808405861">Buy advance tickets for 'Shrek 2' </a>