본문 바로가기
Raspberry Pi (Linux, ubuntu)

raspberry pi 4, MAX14830, RS485 and RS232

by 소나무기운 2021. 3. 20.
반응형

2021-08-10 485통신 설정 예제 추가

2021-08-09 회로 추가, dts파일 추가

2021-03-20 처음작성

 

ㅁ 개요.

별도의 PCB를 제작하여 라즈베리파이 4와 연결하여 시리얼 포트를 확장한다.

MAX14830을 이용하여 2개의 RS-232, 2개의 485를 추가 구성한다.

 

하드웨어는 추가하여야 하며,  SPI 혹은 I2C를 이용하여 4개의 시리얼 포트를 확정한다.

확장된 시리얼포트는  RS-232, RS-485, RS-422의 형태로 사용할 수 있다.

 

라즈베리파이에는 기본적으로 MAX14830의 드라이버 코드가 내장되어 있으며

이를 활성화하여 드라이버를 등록하고 커널을 빌드하여 적용하도록 한다.

 

라즈베리파이 버젼 : Raspberry Pi 4 Model B Rev 1.4

                           Linux raspberrypi 5.10.23-v7l+ #1 SMP Thu Mar 18 23:34:12 KST 2021 armv7l GNU/Linux

 

ㅁ 작업 순서

1. H/W 준비

MAX14830을 추가한 제품을 준비한다.

[회로도 핀맵]

라즈베리파이 4 핀맵
MAX14830 핀맵

[uname -a 명령을 통한 버전 확인 화면 추가 화면]

[install device tree compiler using]

apt-get install device-tree-compiler

DTS파일 생성

/dts-v1/;
/plugin/;

/ {
        compatible = "brcm,bcm2708";


        fragment@0 {
                target = <&spidev0>;
                __overlay__ {
                        status = "disabled";
                };
        };

        fragment@1 {
                target = <&spidev1>;
                __overlay__ {
                        status = "disabled";
                };
        };
};

spidev-off.dts

// Overlay for the Maxim max14830
/dts-v1/;
/plugin/;

/ {
        compatible = "brcm,bcm2708";

        fragment@0 {
                target = <&spi0>;
                __overlay__ {
                        /* needed to avoid dtc warning */
                        #address-cells = <1>;
                        #size-cells = <0>;

                        status = "okay";


                        max14830_1: max14830_1@0 {
                                compatible = "maxim,max14830";
                                reg = <0>; /* CE0 */
                                clocks = <0xe1>;
                                clock-names = "xtal";
                                spi-max-frequency = <12000000>;
                                status = "okay";
                                pinctrl-names = "default";
                                pinctrl-0 = <&max14830_pins>;
                                interrupt-parent = <&gpio>;
                                interrupts = <17 0x2>; /* falling edge */
                        };
                };
        };

        fragment@1 {
                target = <&gpio>;
                __overlay__ {
                        max14830_pins: max_14830_pins {
                                brcm,pins = <17>;
                                brcm,function = <0>; /* in */
                                brcm,pull = <0>; /* none */
                        };
                };
        };

        __overrides__ {
                int_pin = <&max14830_1>, "interrupts:0",
                          <&max14830_pins>, "brcm,pins:0";
                speed   = <&max14830_1>, "spi-max-frequency:0";
        };
};

max14830.dts

// Overlay for the Maxim max14830 clock
/dts-v1/;
/plugin/;

/ {
        compatible = "brcm,bcm2708";


        fragment@1 {
                target = <&clocks>;
                __overlay__ {
                        xtal36: xtal36@7 {
                                reg = <0x7>;
                                compatible = "fixed-clock";
                                #clock-cells = <0>;
                                clock-frequency = <3686400>;
                                clock-output-names = "xtal36";
                        };
                };
        };
};

max14830-clock.dts

 

 

clock 값 변경 적용<0xE1>

 

재부팅

/opt/vc/bin/dtoverlay max14830

ttyMAX0-3  확인

 

부팅시 동작하도록 추가.

 

2. Driver 추가 Kernel Build

3. MAX14830 관련 설정, interrupt pin 설정

4. C를 이용한 RS485 동작 시험

[시리얼 터미털 프로그램 화면 캡쳐]
[485 통신 설정 예제]

int RS485Setting(void)
{
    char buf[80];
    struct serial_rs485 rs485conf;
    struct termios options;

    int enable = 1;
    fd = open(g_tDevCfg.byComName, O_RDWR);

    if (ioctl(fd, TIOCGRS485, &rs485conf) < 0)
    {
        fprintf(stderr, "Error reading ioctl port (%d): %s\n", errno, strerror(errno));
        return 1;
    }

    printf("Port currently RS485 mode is %s\n", (rs485conf.flags & SER_RS485_ENABLED) ? "set" : "NOT set");

    if (enable)
    {
        printf("RS485 mode will be SET\n");
        rs485conf.flags |= SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND;
    }
    else
    {
        printf("RS485 mode will be UNSET\n");
        rs485conf.flags &= ~SER_RS485_ENABLED;
    }

    if (ioctl(fd, TIOCSRS485, &rs485conf) < 0)
    {
        fprintf(stderr, "Error sending ioctl port (%d): %s\n", errno, strerror(errno));
        return 1;
    }

    tcgetattr(fd, &options);
    options.c_cflag = RS485GetBuadrate() | CS8 | CLOCAL | CREAD; //<Set baud rate
    options.c_iflag = IGNPAR;
    options.c_oflag = 0;
    options.c_lflag = 0;
    tcflush(fd, TCIFLUSH);

    tcsetattr(fd, TCSANOW, &options);

    if (ioctl(fd, TIOCGRS485, &rs485conf) < 0)
    {
        fprintf(stderr, "Error reading ioctl port (%d): %s\n", errno, strerror(errno));
    }

    return 0;
}

 

ㅁ 참고 문헌

http://akheh.blogspot.com/2016/06/using-max14830-with-raspberry-pi-3.html

 

Learner's blog

Using MAX14830 with Raspberry Pi 3 MAX14830 is SPI/I2C to quad UART converter IC. This IC works with Linux, tested on Raspberry Pi 3. To ...

akheh.blogspot.com

https://www.raspberrypi.org/forums/viewtopic.php?t=187632 

 

How to write device tree for multiple MAX14830 con to SPI - Raspberry Pi Forums

As per my uderstaning, the MAX driver will pick the IRQ line from the device tree, is it correct ? Almost - the SPI framework is supposed to read the "interrupts" property and make it available as the "irq" field of the spi_device structure passed to the p

www.raspberrypi.org

 

반응형

'Raspberry Pi (Linux, ubuntu)' 카테고리의 다른 글

Raspberry pi 복제하기 2가지 방법  (2) 2021.04.22
socket 통신  (0) 2021.03.31
Install VSCode on Raspberry Pi 4  (0) 2021.03.19
Using C++ on Linux in VS Code  (0) 2021.03.14
VSCode On the Raspberry pi 4  (0) 2021.03.14

댓글