nvt_rs485

Dependents:   modbus-over-rs485-sample NTOUEE-mbed-modbus-RTU NuMaker-mbed-modbus-sample NuMaker_NuWicam_Lite ... more

Committer:
wclin
Date:
Fri Oct 06 02:23:24 2017 +0000
Revision:
3:4c9d65e9275c
Parent:
2:750bbd296b36
Support NUMAKER_PFM_M487

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wclin 0:22be4d425fdb 1 #include "nvt_rs485.h"
wclin 0:22be4d425fdb 2 #include "PeripheralPins.h"
wclin 0:22be4d425fdb 3
wclin 1:783999122629 4 NvtRS485::NvtRS485(PinName tx, PinName rx, PinName dir)
wclin 1:783999122629 5 : Serial(tx, rx)
wclin 0:22be4d425fdb 6 {
wclin 0:22be4d425fdb 7 this->set_rs485_mode(dir);
wclin 0:22be4d425fdb 8 }
wclin 0:22be4d425fdb 9
wclin 0:22be4d425fdb 10 NvtRS485::~NvtRS485()
wclin 0:22be4d425fdb 11 {
wclin 0:22be4d425fdb 12
wclin 0:22be4d425fdb 13 }
wclin 0:22be4d425fdb 14
wclin 0:22be4d425fdb 15 int NvtRS485::set_rs485_mode(PinName dir)
wclin 0:22be4d425fdb 16 {
wclin 0:22be4d425fdb 17 int ret=0;
wclin 1:783999122629 18 serial_t *obj = &_serial; // Get pointer from grandfather-SerialBase.
wclin 1:783999122629 19
wclin 1:783999122629 20 lock();
wclin 1:783999122629 21
wclin 1:783999122629 22 //Implement RS485-AUD mode for various platform of Nuvoton
wclin 3:4c9d65e9275c 23 #if defined(TARGET_NUMAKER_PFM_NUC472) || defined(TARGET_NUMAKER_PFM_M453) || defined(TARGET_NUMAKER_PFM_M487)
wclin 1:783999122629 24 /*
wclin 1:783999122629 25 Program Sequence example:
wclin 1:783999122629 26 1. Program FUNCSEL in UART_FUNCSEL to select RS-485 function.
wclin 1:783999122629 27 2. Program the RXOFF bit (UART_FIFO[8]) to determine enable or disable RS-485 receiver
wclin 1:783999122629 28 3. Program the RS-485_NMM or RS-485_AAD mode.
wclin 1:783999122629 29 4. If the RS-485_AAD mode is selected, the ADDRMV (UART_ALTCTL[31:24]) is programmed for auto address match value.
wclin 1:783999122629 30 5. Determine auto direction control by programming RS-485_AUD.
wclin 1:783999122629 31 */
wclin 1:783999122629 32
wclin 0:22be4d425fdb 33 UART_T *uart_base = (UART_T *) NU_MODBASE(obj->serial.uart);
wclin 0:22be4d425fdb 34
wclin 0:22be4d425fdb 35 // First, disable flow control completely.
wclin 0:22be4d425fdb 36 uart_base->INTEN &= ~(UART_INTEN_ATORTSEN_Msk | UART_INTEN_ATOCTSEN_Msk);
wclin 0:22be4d425fdb 37
wclin 1:783999122629 38 // Check if RTS pin matches.
wclin 1:783999122629 39 uint32_t uart_rts = pinmap_peripheral(dir, PinMap_UART_RTS);
wclin 1:783999122629 40 MBED_ASSERT(uart_rts == obj->serial.uart);
wclin 0:22be4d425fdb 41
wclin 1:783999122629 42 // Enable the pin for RTS function
wclin 1:783999122629 43 pinmap_pinout(dir, PinMap_UART_RTS);
wclin 0:22be4d425fdb 44
wclin 1:783999122629 45 uart_base->FUNCSEL = (uart_base->FUNCSEL & ~UART_FUNCSEL_FUNCSEL_Msk) | 0x3;
wclin 1:783999122629 46 uart_base->ALTCTL = (uart_base->ALTCTL & ~UART_ALTCTL_RS485AUD_Msk) | UART_ALTCTL_RS485AUD_Msk;
wclin 1:783999122629 47 uart_base->MODEM = (uart_base->MODEM & ~UART_MODEM_RTSACTLV_Msk);
wclin 1:783999122629 48 uart_base->FIFO = (uart_base->FIFO & ~UART_FIFO_RTSTRGLV_Msk) | (0x2 << UART_FIFO_RTSTRGLV_Pos);
wclin 1:783999122629 49
wclin 1:783999122629 50 #else
wclin 1:783999122629 51 #error "Unknown platform."
wclin 1:783999122629 52 #endif
wclin 0:22be4d425fdb 53
wclin 1:783999122629 54 unlock();
wclin 0:22be4d425fdb 55 return ret;
wclin 0:22be4d425fdb 56 }