Nuvoton / nvt_rs485

Dependents:   NuMaker-mbed-modbus-sample

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nvt_rs485.cpp Source File

nvt_rs485.cpp

00001 #include "nvt_rs485.h"
00002 #include "PeripheralPins.h"
00003 #if MBED_MAJOR_VERSION >= 6
00004 NvtRS485::NvtRS485(PinName tx, PinName rx, PinName dir)
00005     : MyUnbufferedSerial(tx, rx)
00006 #else
00007 NvtRS485::NvtRS485(PinName tx, PinName rx, PinName dir)
00008     : Serial(tx, rx)
00009 #endif
00010 {
00011     this->set_rs485_mode(dir);
00012 }
00013 
00014 NvtRS485::~NvtRS485()
00015 {
00016     
00017 }
00018 
00019 int NvtRS485::set_rs485_mode(PinName dir)
00020 {
00021     int ret=0;
00022     serial_t *obj = &_serial;  // Get pointer from grandfather-SerialBase.
00023 
00024     lock();
00025     
00026     //Implement RS485-AUD mode for various platform of Nuvoton
00027 #if defined(TARGET_NUMAKER_PFM_NUC472) || defined(TARGET_NUMAKER_PFM_M453) || defined(TARGET_NUMAKER_PFM_M487) || defined(TARGET_NUMAKER_IOT_M467) || defined(TARGET_NUMAKER_IOT_M487)
00028     /*
00029     Program Sequence example:
00030      1. Program FUNCSEL in UART_FUNCSEL to select RS-485 function.
00031      2. Program the RXOFF bit (UART_FIFO[8]) to determine enable or disable RS-485 receiver
00032      3. Program the RS-485_NMM or RS-485_AAD mode.
00033      4. If the RS-485_AAD mode is selected, the ADDRMV (UART_ALTCTL[31:24]) is programmed for auto address match value.
00034      5. Determine auto direction control by programming RS-485_AUD.
00035     */
00036 
00037     UART_T *uart_base = (UART_T *) NU_MODBASE(obj->serial.uart);
00038 
00039     // First, disable flow control completely.
00040     uart_base->INTEN &= ~(UART_INTEN_ATORTSEN_Msk | UART_INTEN_ATOCTSEN_Msk);
00041 
00042     // Check if RTS pin matches.
00043     uint32_t uart_rts = pinmap_peripheral(dir, PinMap_UART_RTS);
00044     MBED_ASSERT(uart_rts == obj->serial.uart);
00045 
00046     // Enable the pin for RTS function
00047     pinmap_pinout(dir, PinMap_UART_RTS);
00048 
00049     uart_base->FUNCSEL = (uart_base->FUNCSEL & ~UART_FUNCSEL_FUNCSEL_Msk) | 0x3;
00050     uart_base->ALTCTL  = (uart_base->ALTCTL & ~UART_ALTCTL_RS485AUD_Msk) | UART_ALTCTL_RS485AUD_Msk;
00051     uart_base->MODEM = (uart_base->MODEM & ~UART_MODEM_RTSACTLV_Msk);
00052     uart_base->FIFO = (uart_base->FIFO & ~UART_FIFO_RTSTRGLV_Msk) | (0x2 << UART_FIFO_RTSTRGLV_Pos);
00053  
00054 #else
00055     #error "Unknown platform."
00056 #endif
00057 
00058     unlock();    
00059     return ret;
00060 }