[En-Nut-Discussion] Ethernut II RS485 Communication
Zheng Anding
adzheng at yahoo.com
Thu May 13 21:44:40 CEST 2004
Hi Dusan,
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.
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:
0. Follow the hardware manual to configure the UART0 as RS232 and UART1 as RS485...;
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;
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;
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;
#if 1
/* Define half duplex for RS485 */
#define UART1_HDX_PORT PORTD
#define UART1_HDX_DDR DDRD
#define UART1_HDX_BIT 5
#endif
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;
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.
// More other includes files .....
#include <dev/usartavr.h>
int got;
FILE *uart0, *uart1;
u_long baud = 9600, flag = USART_MF_HALFDUPLEX;
NutRegisterDevice(&devUsartAvr0, 0, 0);
NutRegisterDevice(&devUsartAvr1, 0, 0);
uart0 = fopen("uart0", "r+b");
uart1 = fopen("uart1", "r+b");
_ioctl(_fileno(uart0), UART_SETSPEED, &baud);
_ioctl(_fileno(uart1), UART_SETSPEED, &baud);
_ioctl(_fileno(uart1), UART_SETFLOWCONTROL, &flag);
for (;;)
{
got = fread(RecBuf, 1, 32, uart1);
fprintf(uart0, "Receive %d bytes...\n", got);
NutSleep(1000);
fwrite(WriteBuf, 1, 11, uart1);
fprintf(uart0, "Write 11 bytes...\n");
NutSleep(1000);
}
6. Rebuild All of your project and you should be able to use RS485 now.
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...
Let me know if I mistake anything or any questions about it. Thanks.
Regards,
Alex
Dusan Ferbas <dferbas at dfsoft.cz> wrote:
Hi Alex,
you need to modify SW. First is to enable direction pin toggling (see 1).
Then properly initialize usart - use new driver and set USART_MF_HALFDUPLEX
mode (see 3 below).
You can live also with original Nut sources if you will set speed after
changing flow control mode. See example below.
You can omit setting speed if you use Damian Slee's bug correction. Look
for it in discussion archive 17th February, 16th April (BugFix: Usart half
duplex mode... )
Anyway: CVS maintainers what about to incorporate Damian's fixes ?
----
1) nut/include/cfg/modem.h
For RS485 you do not need RTS/CTS so only following has to be enabled.
Whole block is by default "#if 0". Do not enable (allow define) for
..._RTS/CTS_.... This adds more code and also processing within USART driver.
#if 1
/* Define half duplex for RS485 */
#define UART0_HDX_PORT PORTD
#define UART0_HDX_DDR DDRD
#define UART0_HDX_BIT 4
#endif
2) recompile Nut/OS libraries with 'make install'
3) example of initialization: call as InitUart0(9600);
FILE *InitUart0(u_long baud)
{
FILE *uart;
//u_long cooked_mode = 0; /* disable
replacing \n with \r\n */
NutRegisterDevice(&devUsartAvr0, 0, 0);
uart = fopen("uart0", "r+b"); /* select binary
mode */
u_long flags = USART_MF_HALFDUPLEX;
_ioctl(_fileno(uart0), UART_SETFLOWCONTROL, &flags); /* due to
bug in Nut/OS 3.5.0 do this BEFORE SETSPEED */
_ioctl(_fileno(uart), UART_SETSPEED, &baud);
//following is set automatically when binary mode is selected
//_ioctl(_fileno(uart), UART_SETCOOKEDMODE, &cooked_mode);
return uart;
}
>But I cann't get anything from my slave device so far. My questions are:
>
>1. Is there anything special for the hardware setting? Did I setup my
>board correctly?
>2. I knew the 485 on Ethernut is half duplex and have to use PD5 / PD4 to
>control the data direction, but based on Nut/OS, do I still need to do
>this? Is there any existing RS-485 UART driver for Nut/OS now?
>3. If the above answer is NO, do I have to wirte ISR for Mega128 to send
>and receive package?
>
>Thanks for any kindly help.
>
>Sincerely,
>Alex
Dusan Ferbas
www.dfsoft.cz
_______________________________________________
En-Nut-Discussion mailing list
En-Nut-Discussion at egnite.de
http://www.egnite.de/mailman/listinfo.cgi/en-nut-discussion
---------------------------------
Do you Yahoo!?
Yahoo! Movies - Buy advance tickets for 'Shrek 2'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.egnite.de/pipermail/en-nut-discussion/attachments/20040513/5206c2b8/attachment.html>
More information about the En-Nut-Discussion
mailing list