<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>LPCXpresso IDEのトピックRe: Two LPC1765 devices communicating through RS-485?</title>
    <link>https://community.nxp.com/t5/LPCXpresso-IDE/Two-LPC1765-devices-communicating-through-RS-485/m-p/588675#M28249</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by Wolfie on Wed Sep 19 05:25:44 MST 2012&lt;/STRONG&gt;&lt;BR /&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD bgcolor="#cacaca"&gt; &lt;PRE&gt;/****************************************************************************
 *&amp;nbsp;&amp;nbsp; $Id:: rs485.c 6097 2011-01-07 04:31:25Z nxp12832&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $
 *&amp;nbsp;&amp;nbsp; Project: NXP LPC17xx RS485 example
 *
 *&amp;nbsp;&amp;nbsp; Description:
 *&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; This file contains RS485 code example which include RS485
 *&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; initialization, RS485 interrupt handler, and APIs for RS485 access.
 *
 ****************************************************************************
 * Software that is described herein is for illustrative purposes only
 * which provides customers with programming information regarding the
 * products. This software is supplied "AS IS" without any warranties.
 * NXP Semiconductors assumes no responsibility or liability for the
 * use of the software, conveys no license or title under any patent,
 * copyright, or mask work right to the product. NXP Semiconductors
 * reserves the right to make changes in the software without
 * notification. NXP Semiconductors also make no representation or
 * warranty that such application will be suitable for the specified
 * use without further testing or modification.
****************************************************************************/




// ===============================================================================================================================
// RS485 pins
// ===============================================================================================================================
//
//&amp;nbsp; RS485_TXDP2[0]/TXD1transmitter output for UART1
//&amp;nbsp; RS485_RXDP2[1]/RXD1receiver input for UART1
//&amp;nbsp; RS485_OFP2[5]/DTR1/PWM1[6]/TRACEDATA[0] data terminal ready for UART1 (RS-485 output enable signal)
//&amp;nbsp; RS485_MASTER_PWRP2[6]/RI1ring indicator input for UART1
//&amp;nbsp; RS485_MASTER_MFASP2[7]/RTS1request to send output for UART1 (RS-485 output enable signal)
//&amp;nbsp; RS485 SLAVE DETECTP2[8]/TXD2general purpose
//
// ===============================================================================================================================



#include "LPC17xx.h"
#include "rs485.h"

volatile uint32_t UARTStatus;
volatile uint8_t&amp;nbsp; UARTTxEmpty = 1;
volatile uint8_t&amp;nbsp; UARTBuffer[BUFSIZE];
volatile uint32_t UARTCount = 0;
volatile uint32_t garbageCount = 0;

extern uint32_t test_485;

/*****************************************************************************
** Function name:UART1_IRQHandler
**
** Descriptions:UART interrupt handler
**
** parameters:None
** Returned value:None
**
*****************************************************************************/
void UART1_IRQHandler(void)
{
&amp;nbsp; test_485++;//DEBUG

&amp;nbsp; volatile uint8_t IIRValue, LSRValue;
&amp;nbsp; volatile uint8_t Dummy = Dummy;

&amp;nbsp; IIRValue = LPC_UART1-&amp;gt;IIR;

&amp;nbsp; IIRValue &amp;gt;&amp;gt;= 1;/* skip pending bit in IIR */
&amp;nbsp; IIRValue &amp;amp;= 0x07;/* check bit 1~3, interrupt identification */
&amp;nbsp; if (IIRValue == IIR_RLS)/* Receive Line Status */
&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; LSRValue = LPC_UART1-&amp;gt;LSR;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Receive Line Status */
if ( ((LSRValue &amp;amp; (LSR_PE|LSR_RDR)) == (LSR_PE|LSR_RDR))
&amp;amp;&amp;amp; (LPC_UART1-&amp;gt;RS485CTRL &amp;amp; RS485_NMMEN) )
{
&amp;nbsp; Dummy = LPC_UART1-&amp;gt;RBR;
&amp;nbsp; /* if address match, enable RX, otherwise, disable RX. */
&amp;nbsp; if ( Dummy == LPC_UART1-&amp;gt;ADRMATCH )
&amp;nbsp; {
LPC_UART1-&amp;gt;RS485CTRL &amp;amp;= ~RS485_RXDIS;/* Enable RX */
&amp;nbsp; }
&amp;nbsp; else
&amp;nbsp; {
LPC_UART1-&amp;gt;RS485CTRL |= RS485_RXDIS;/* Disable RX */
while ( LPC_UART1-&amp;gt;LSR &amp;amp; LSR_RDR ){
&amp;nbsp; Dummy = LPC_UART1-&amp;gt;RBR;/* Clear data from RX FIFO */
&amp;nbsp; garbageCount++;
}
return;
&amp;nbsp; }
}
&amp;nbsp;&amp;nbsp;&amp;nbsp; else if (LSRValue &amp;amp; (LSR_OE | LSR_PE | LSR_FE | LSR_RXFE | LSR_BI))
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* There are errors or break interrupt */
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Read LSR will clear the interrupt */
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; UARTStatus = LSRValue;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dummy = LPC_UART1-&amp;gt;RBR;/* Dummy read on RX to clear
interrupt, then bail out */
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; else if (LSRValue &amp;amp; LSR_RDR)/* Receive Data Ready */
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* If no error on RLS, normal ready, save into the data buffer. */
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Note: read RBR will clear the interrupt */


&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; UARTBuffer[UARTCount++] = LPC_UART1-&amp;gt;RBR;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (UARTCount == BUFSIZE)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; UARTCount = 0;/* buffer overflow */
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp; }
&amp;nbsp; else if (IIRValue == IIR_RDA)/* Receive Data Available */
&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Receive Data Available */


&amp;nbsp;&amp;nbsp;&amp;nbsp; UARTBuffer[UARTCount++] = LPC_UART1-&amp;gt;RBR;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (UARTCount == BUFSIZE)
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; UARTCount = 0;/* buffer overflow */
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp; }
&amp;nbsp; else if (IIRValue == IIR_CTI)/* Character timeout indicator */
&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Character Time-out indicator */
&amp;nbsp;&amp;nbsp;&amp;nbsp; UARTStatus |= 0x100;/* Bit 9 as the CTI error */
&amp;nbsp; }
&amp;nbsp; else if (IIRValue == IIR_THRE)/* THRE, transmit holding register empty */
&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; /* THRE interrupt */
&amp;nbsp;&amp;nbsp;&amp;nbsp; LSRValue = LPC_UART1-&amp;gt;LSR;/* Check status in the LSR to see if
valid data in U0THR or not */
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (LSRValue &amp;amp; LSR_THRE)
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; UARTTxEmpty = 1;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; else
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; UARTTxEmpty = 0;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp; }
&amp;nbsp; return;
}

/*****************************************************************************
** Function name:RS485Init
**
** Descriptions:Initialize UART1 port, setup pin select,
**clock, parity, stop bits, FIFO, etc.
**
** parameters:UART baudrate
** Returned value:None
**
*****************************************************************************/
void RS485Init(uint32_t baudrate)
{

&amp;nbsp; uint32_t Fdiv;
&amp;nbsp; uint32_t regVal = regVal;
&amp;nbsp; uint32_t pclkdiv, pclk;

&amp;nbsp; UARTCount = 0;

&amp;nbsp; NVIC_DisableIRQ(UART1_IRQn);

&amp;nbsp; LPC_PINCON-&amp;gt;PINSEL4 &amp;amp;= ~0x0000000F;
&amp;nbsp; LPC_PINCON-&amp;gt;PINSEL4 |= 0x0000000A;/* Enable RxD1 P2.1, TxD1 P2.0 */

&amp;nbsp; /* By default, the PCLKSELx value is zero, thus, the PCLK for
&amp;nbsp; all the peripherals is 1/4 of the SystemFrequency. */
&amp;nbsp; /* Bit 8,9 are for UART1 */
&amp;nbsp; pclkdiv = (LPC_SC-&amp;gt;PCLKSEL0 &amp;gt;&amp;gt; 8) &amp;amp; 0x03;
&amp;nbsp; switch ( pclkdiv )
&amp;nbsp; {
case 0x00:
default:
&amp;nbsp; pclk = SystemFrequency/4;
break;
case 0x01:
&amp;nbsp; pclk = SystemFrequency;
break;
case 0x02:
&amp;nbsp; pclk = SystemFrequency/2;
break;
case 0x03:
&amp;nbsp; pclk = SystemFrequency/8;
break;
&amp;nbsp; }

&amp;nbsp; LPC_UART1-&amp;gt;LCR = 0x83;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* 8 bits, no Parity, 1 Stop bit */
&amp;nbsp; Fdiv = ( pclk / 16 ) / baudrate ;/* baud rate */
&amp;nbsp; LPC_UART1-&amp;gt;DLM = Fdiv / 256;
&amp;nbsp; LPC_UART1-&amp;gt;DLL = Fdiv % 256;
&amp;nbsp; LPC_UART1-&amp;gt;LCR = 0x03;/* DLAB = 0 */
&amp;nbsp; LPC_UART1-&amp;gt;FCR = 0x07;/* Enable and reset TX and RX FIFO. */

&amp;nbsp; /* Read to clear the line status. */
&amp;nbsp; regVal = LPC_UART1-&amp;gt;LSR;
&amp;nbsp; /* Ensure a clean start, no data in either TX or RX FIFO. */
&amp;nbsp; while ( LPC_UART1-&amp;gt;LSR &amp;amp; (LSR_THRE|LSR_TEMT) != (LSR_THRE|LSR_TEMT) );
&amp;nbsp; while ( LPC_UART1-&amp;gt;LSR &amp;amp; LSR_RDR )
&amp;nbsp; {
regVal = LPC_UART1-&amp;gt;RBR;/* Dump data from RX FIFO */
&amp;nbsp; }

&amp;nbsp; LPC_UART1-&amp;gt;LCR |= ((0x3&amp;lt;&amp;lt;4)|(0x1&amp;lt;&amp;lt;3));/* forced "0" sticky parity, parity enable */
&amp;nbsp; LPC_UART1-&amp;gt;ADRMATCH = RS485_SLAVE_ADR;/* UARTA 485 address, 0xC0 */

#if RS485_NMM
&amp;nbsp; LPC_UART1-&amp;gt;RS485CTRL = RS485_NMMEN;/* NMM mode */
#else
&amp;nbsp; LPC_UART1-&amp;gt;RS485CTRL = RS485_NMMEN|RS485_AADEN|RS485_RXDIS;/* AAD mode */
#endif



&amp;nbsp; // Below is the direction control setting.
#if DIR_CTRL
&amp;nbsp; // Either DTR or RTS can be used as direction pin out.
#if 1
&amp;nbsp; LPC_PINCON-&amp;gt;PINSEL1 &amp;amp;= ~(0x03&amp;lt;&amp;lt;8);
&amp;nbsp; LPC_PINCON-&amp;gt;PINSEL1 |= (0x01&amp;lt;&amp;lt;8);// DTR1 as direction control
&amp;nbsp; LPC_UART1-&amp;gt;RS485CTRL |= RS485_DCTRL;
#else
&amp;nbsp; LPC_PINCON-&amp;gt;PINSEL1 &amp;amp;= ~(0x03&amp;lt;&amp;lt;12);
&amp;nbsp; LPC_PINCON-&amp;gt;PINSEL1 |= (0x01&amp;lt;&amp;lt;12);// RTS1 as direction control
&amp;nbsp; LPC_UART1-&amp;gt;RS485CTRL |= (RS485_DCTRL|RS485_SEL);
#endif
#endif




&amp;nbsp; // Enable UART1 Interrupt
&amp;nbsp; NVIC_EnableIRQ(UART1_IRQn);
&amp;nbsp; LPC_UART1-&amp;gt;IER = IER_RBR | IER_THRE | IER_RLS;/* Enable UART interrupt */
&amp;nbsp; return;
}

/*****************************************************************************
** Function name:RS485Send
**
** Descriptions:Send a block of data to the UART 0 port based
**on the data length
**
** parameters:buffer pointer, and data length
** Returned value:None
**
*****************************************************************************/
void RS485Send(uint8_t *BufferPtr, uint32_t Length)
{
&amp;nbsp; uint32_t i;

&amp;nbsp; for ( i = 0; i &amp;lt; Length; i++ )
&amp;nbsp; {

/* THRE status, contain valid data */
&amp;nbsp;&amp;nbsp;&amp;nbsp; while ( !(UARTTxEmpty &amp;amp; 0x01) );
&amp;nbsp;&amp;nbsp;&amp;nbsp; LPC_UART1-&amp;gt;THR = *BufferPtr;
&amp;nbsp;&amp;nbsp;&amp;nbsp; UARTTxEmpty = 0;/* not empty in the THR until it shifts out */
/* At this point, bit 5 of LCR is always 1 for RS485 test */
if ( i == 0 )
&amp;nbsp; LPC_UART1-&amp;gt;LCR &amp;amp;= ~(0x1&amp;lt;&amp;lt;4);/* Sticky parity to 1 */
else
&amp;nbsp; LPC_UART1-&amp;gt;LCR |= (0x1&amp;lt;&amp;lt;4);/* Sticky parity to 0 */
LPC_UART1-&amp;gt;LCR |= (0x1&amp;lt;&amp;lt;3);
BufferPtr++;
&amp;nbsp; }
&amp;nbsp; return;
}

/******************************************************************************
**&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Of File
******************************************************************************/
&lt;/PRE&gt; &lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 15 Jun 2016 21:28:34 GMT</pubDate>
    <dc:creator>lpcware</dc:creator>
    <dc:date>2016-06-15T21:28:34Z</dc:date>
    <item>
      <title>Two LPC1765 devices communicating through RS-485?</title>
      <link>https://community.nxp.com/t5/LPCXpresso-IDE/Two-LPC1765-devices-communicating-through-RS-485/m-p/588674#M28248</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by Wolfie on Wed Sep 19 01:12:29 MST 2012&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm trying to implement a simple communication between two LPC1765 devices using RS-485. I'm using this example added to my menu system (it's a basic example taken from nxp website):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://http://www.codeforge.com/read/155431/rs485.c__html"&gt;http://www.codeforge.com/read/155431/rs485.c__html&lt;/A&gt;&lt;BR /&gt;&lt;A href="http://http://www.codeforge.com/read/155431/rs485.h__html"&gt;http://www.codeforge.com/read/155431/rs485.h__html&lt;/A&gt;&lt;BR /&gt;&lt;A href="http://http://www.codeforge.com/read/155431/rs485test.c__html"&gt;http://www.codeforge.com/read/155431/rs485test.c__html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So, both devices are initialized using RS485Init(). When I send data using RS485Send using a slave address as the first byte, the result is:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;(1) if the devices are physically disconnected, transmitter receives its own communication (the contents of UARTBuffer are updated on transmitter only)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;(2) if the devices are physically connected, nothing happens -- transmitted buffer is lost somewhere (UARTBuffer doesn't change on either device)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I see that this RS485 example uses UART1 interrupt, is this correct? UART1 seems to be located on pins that are not used by any RS485 circuits.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now hardware should be properly connected so I'll just list things for reference. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;There is a SN65HVD1781D driver on RS485_TXD/RXD which has RS485_A+, RS485_B- and +3.3V. On the LPC side RS485_TXD/RXD (P2[0] and P2[1]), RS485_OF (P2[5]), RS485_MASTER_PWR (P2[6]) and RS485_MASTER_MFAS (p2[7])). The latter two are connected to SOT23-5 LP2985AIM5-3.3 and has RS485_UP and +3.3V.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;There is a RS485 slave detect circuit (p2[8]) connected to RS485_DOWN.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Each device is connected with four contacts and they are RS485_UP, RS485_A+, RS485_B- and RS485_DOWN. There are two 4-pin connectors on a device to connect devices in a sequence, where the first device would, if everything works, assume the status of a master over all other devices.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Jun 2016 21:28:33 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPCXpresso-IDE/Two-LPC1765-devices-communicating-through-RS-485/m-p/588674#M28248</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-15T21:28:33Z</dc:date>
    </item>
    <item>
      <title>Re: Two LPC1765 devices communicating through RS-485?</title>
      <link>https://community.nxp.com/t5/LPCXpresso-IDE/Two-LPC1765-devices-communicating-through-RS-485/m-p/588675#M28249</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by Wolfie on Wed Sep 19 05:25:44 MST 2012&lt;/STRONG&gt;&lt;BR /&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD bgcolor="#cacaca"&gt; &lt;PRE&gt;/****************************************************************************
 *&amp;nbsp;&amp;nbsp; $Id:: rs485.c 6097 2011-01-07 04:31:25Z nxp12832&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $
 *&amp;nbsp;&amp;nbsp; Project: NXP LPC17xx RS485 example
 *
 *&amp;nbsp;&amp;nbsp; Description:
 *&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; This file contains RS485 code example which include RS485
 *&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; initialization, RS485 interrupt handler, and APIs for RS485 access.
 *
 ****************************************************************************
 * Software that is described herein is for illustrative purposes only
 * which provides customers with programming information regarding the
 * products. This software is supplied "AS IS" without any warranties.
 * NXP Semiconductors assumes no responsibility or liability for the
 * use of the software, conveys no license or title under any patent,
 * copyright, or mask work right to the product. NXP Semiconductors
 * reserves the right to make changes in the software without
 * notification. NXP Semiconductors also make no representation or
 * warranty that such application will be suitable for the specified
 * use without further testing or modification.
****************************************************************************/




// ===============================================================================================================================
// RS485 pins
// ===============================================================================================================================
//
//&amp;nbsp; RS485_TXDP2[0]/TXD1transmitter output for UART1
//&amp;nbsp; RS485_RXDP2[1]/RXD1receiver input for UART1
//&amp;nbsp; RS485_OFP2[5]/DTR1/PWM1[6]/TRACEDATA[0] data terminal ready for UART1 (RS-485 output enable signal)
//&amp;nbsp; RS485_MASTER_PWRP2[6]/RI1ring indicator input for UART1
//&amp;nbsp; RS485_MASTER_MFASP2[7]/RTS1request to send output for UART1 (RS-485 output enable signal)
//&amp;nbsp; RS485 SLAVE DETECTP2[8]/TXD2general purpose
//
// ===============================================================================================================================



#include "LPC17xx.h"
#include "rs485.h"

volatile uint32_t UARTStatus;
volatile uint8_t&amp;nbsp; UARTTxEmpty = 1;
volatile uint8_t&amp;nbsp; UARTBuffer[BUFSIZE];
volatile uint32_t UARTCount = 0;
volatile uint32_t garbageCount = 0;

extern uint32_t test_485;

/*****************************************************************************
** Function name:UART1_IRQHandler
**
** Descriptions:UART interrupt handler
**
** parameters:None
** Returned value:None
**
*****************************************************************************/
void UART1_IRQHandler(void)
{
&amp;nbsp; test_485++;//DEBUG

&amp;nbsp; volatile uint8_t IIRValue, LSRValue;
&amp;nbsp; volatile uint8_t Dummy = Dummy;

&amp;nbsp; IIRValue = LPC_UART1-&amp;gt;IIR;

&amp;nbsp; IIRValue &amp;gt;&amp;gt;= 1;/* skip pending bit in IIR */
&amp;nbsp; IIRValue &amp;amp;= 0x07;/* check bit 1~3, interrupt identification */
&amp;nbsp; if (IIRValue == IIR_RLS)/* Receive Line Status */
&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; LSRValue = LPC_UART1-&amp;gt;LSR;
&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Receive Line Status */
if ( ((LSRValue &amp;amp; (LSR_PE|LSR_RDR)) == (LSR_PE|LSR_RDR))
&amp;amp;&amp;amp; (LPC_UART1-&amp;gt;RS485CTRL &amp;amp; RS485_NMMEN) )
{
&amp;nbsp; Dummy = LPC_UART1-&amp;gt;RBR;
&amp;nbsp; /* if address match, enable RX, otherwise, disable RX. */
&amp;nbsp; if ( Dummy == LPC_UART1-&amp;gt;ADRMATCH )
&amp;nbsp; {
LPC_UART1-&amp;gt;RS485CTRL &amp;amp;= ~RS485_RXDIS;/* Enable RX */
&amp;nbsp; }
&amp;nbsp; else
&amp;nbsp; {
LPC_UART1-&amp;gt;RS485CTRL |= RS485_RXDIS;/* Disable RX */
while ( LPC_UART1-&amp;gt;LSR &amp;amp; LSR_RDR ){
&amp;nbsp; Dummy = LPC_UART1-&amp;gt;RBR;/* Clear data from RX FIFO */
&amp;nbsp; garbageCount++;
}
return;
&amp;nbsp; }
}
&amp;nbsp;&amp;nbsp;&amp;nbsp; else if (LSRValue &amp;amp; (LSR_OE | LSR_PE | LSR_FE | LSR_RXFE | LSR_BI))
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* There are errors or break interrupt */
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Read LSR will clear the interrupt */
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; UARTStatus = LSRValue;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dummy = LPC_UART1-&amp;gt;RBR;/* Dummy read on RX to clear
interrupt, then bail out */
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; else if (LSRValue &amp;amp; LSR_RDR)/* Receive Data Ready */
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* If no error on RLS, normal ready, save into the data buffer. */
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Note: read RBR will clear the interrupt */


&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; UARTBuffer[UARTCount++] = LPC_UART1-&amp;gt;RBR;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (UARTCount == BUFSIZE)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; UARTCount = 0;/* buffer overflow */
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp; }
&amp;nbsp; else if (IIRValue == IIR_RDA)/* Receive Data Available */
&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Receive Data Available */


&amp;nbsp;&amp;nbsp;&amp;nbsp; UARTBuffer[UARTCount++] = LPC_UART1-&amp;gt;RBR;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (UARTCount == BUFSIZE)
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; UARTCount = 0;/* buffer overflow */
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp; }
&amp;nbsp; else if (IIRValue == IIR_CTI)/* Character timeout indicator */
&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Character Time-out indicator */
&amp;nbsp;&amp;nbsp;&amp;nbsp; UARTStatus |= 0x100;/* Bit 9 as the CTI error */
&amp;nbsp; }
&amp;nbsp; else if (IIRValue == IIR_THRE)/* THRE, transmit holding register empty */
&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; /* THRE interrupt */
&amp;nbsp;&amp;nbsp;&amp;nbsp; LSRValue = LPC_UART1-&amp;gt;LSR;/* Check status in the LSR to see if
valid data in U0THR or not */
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (LSRValue &amp;amp; LSR_THRE)
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; UARTTxEmpty = 1;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; else
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; UARTTxEmpty = 0;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp; }
&amp;nbsp; return;
}

/*****************************************************************************
** Function name:RS485Init
**
** Descriptions:Initialize UART1 port, setup pin select,
**clock, parity, stop bits, FIFO, etc.
**
** parameters:UART baudrate
** Returned value:None
**
*****************************************************************************/
void RS485Init(uint32_t baudrate)
{

&amp;nbsp; uint32_t Fdiv;
&amp;nbsp; uint32_t regVal = regVal;
&amp;nbsp; uint32_t pclkdiv, pclk;

&amp;nbsp; UARTCount = 0;

&amp;nbsp; NVIC_DisableIRQ(UART1_IRQn);

&amp;nbsp; LPC_PINCON-&amp;gt;PINSEL4 &amp;amp;= ~0x0000000F;
&amp;nbsp; LPC_PINCON-&amp;gt;PINSEL4 |= 0x0000000A;/* Enable RxD1 P2.1, TxD1 P2.0 */

&amp;nbsp; /* By default, the PCLKSELx value is zero, thus, the PCLK for
&amp;nbsp; all the peripherals is 1/4 of the SystemFrequency. */
&amp;nbsp; /* Bit 8,9 are for UART1 */
&amp;nbsp; pclkdiv = (LPC_SC-&amp;gt;PCLKSEL0 &amp;gt;&amp;gt; 8) &amp;amp; 0x03;
&amp;nbsp; switch ( pclkdiv )
&amp;nbsp; {
case 0x00:
default:
&amp;nbsp; pclk = SystemFrequency/4;
break;
case 0x01:
&amp;nbsp; pclk = SystemFrequency;
break;
case 0x02:
&amp;nbsp; pclk = SystemFrequency/2;
break;
case 0x03:
&amp;nbsp; pclk = SystemFrequency/8;
break;
&amp;nbsp; }

&amp;nbsp; LPC_UART1-&amp;gt;LCR = 0x83;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* 8 bits, no Parity, 1 Stop bit */
&amp;nbsp; Fdiv = ( pclk / 16 ) / baudrate ;/* baud rate */
&amp;nbsp; LPC_UART1-&amp;gt;DLM = Fdiv / 256;
&amp;nbsp; LPC_UART1-&amp;gt;DLL = Fdiv % 256;
&amp;nbsp; LPC_UART1-&amp;gt;LCR = 0x03;/* DLAB = 0 */
&amp;nbsp; LPC_UART1-&amp;gt;FCR = 0x07;/* Enable and reset TX and RX FIFO. */

&amp;nbsp; /* Read to clear the line status. */
&amp;nbsp; regVal = LPC_UART1-&amp;gt;LSR;
&amp;nbsp; /* Ensure a clean start, no data in either TX or RX FIFO. */
&amp;nbsp; while ( LPC_UART1-&amp;gt;LSR &amp;amp; (LSR_THRE|LSR_TEMT) != (LSR_THRE|LSR_TEMT) );
&amp;nbsp; while ( LPC_UART1-&amp;gt;LSR &amp;amp; LSR_RDR )
&amp;nbsp; {
regVal = LPC_UART1-&amp;gt;RBR;/* Dump data from RX FIFO */
&amp;nbsp; }

&amp;nbsp; LPC_UART1-&amp;gt;LCR |= ((0x3&amp;lt;&amp;lt;4)|(0x1&amp;lt;&amp;lt;3));/* forced "0" sticky parity, parity enable */
&amp;nbsp; LPC_UART1-&amp;gt;ADRMATCH = RS485_SLAVE_ADR;/* UARTA 485 address, 0xC0 */

#if RS485_NMM
&amp;nbsp; LPC_UART1-&amp;gt;RS485CTRL = RS485_NMMEN;/* NMM mode */
#else
&amp;nbsp; LPC_UART1-&amp;gt;RS485CTRL = RS485_NMMEN|RS485_AADEN|RS485_RXDIS;/* AAD mode */
#endif



&amp;nbsp; // Below is the direction control setting.
#if DIR_CTRL
&amp;nbsp; // Either DTR or RTS can be used as direction pin out.
#if 1
&amp;nbsp; LPC_PINCON-&amp;gt;PINSEL1 &amp;amp;= ~(0x03&amp;lt;&amp;lt;8);
&amp;nbsp; LPC_PINCON-&amp;gt;PINSEL1 |= (0x01&amp;lt;&amp;lt;8);// DTR1 as direction control
&amp;nbsp; LPC_UART1-&amp;gt;RS485CTRL |= RS485_DCTRL;
#else
&amp;nbsp; LPC_PINCON-&amp;gt;PINSEL1 &amp;amp;= ~(0x03&amp;lt;&amp;lt;12);
&amp;nbsp; LPC_PINCON-&amp;gt;PINSEL1 |= (0x01&amp;lt;&amp;lt;12);// RTS1 as direction control
&amp;nbsp; LPC_UART1-&amp;gt;RS485CTRL |= (RS485_DCTRL|RS485_SEL);
#endif
#endif




&amp;nbsp; // Enable UART1 Interrupt
&amp;nbsp; NVIC_EnableIRQ(UART1_IRQn);
&amp;nbsp; LPC_UART1-&amp;gt;IER = IER_RBR | IER_THRE | IER_RLS;/* Enable UART interrupt */
&amp;nbsp; return;
}

/*****************************************************************************
** Function name:RS485Send
**
** Descriptions:Send a block of data to the UART 0 port based
**on the data length
**
** parameters:buffer pointer, and data length
** Returned value:None
**
*****************************************************************************/
void RS485Send(uint8_t *BufferPtr, uint32_t Length)
{
&amp;nbsp; uint32_t i;

&amp;nbsp; for ( i = 0; i &amp;lt; Length; i++ )
&amp;nbsp; {

/* THRE status, contain valid data */
&amp;nbsp;&amp;nbsp;&amp;nbsp; while ( !(UARTTxEmpty &amp;amp; 0x01) );
&amp;nbsp;&amp;nbsp;&amp;nbsp; LPC_UART1-&amp;gt;THR = *BufferPtr;
&amp;nbsp;&amp;nbsp;&amp;nbsp; UARTTxEmpty = 0;/* not empty in the THR until it shifts out */
/* At this point, bit 5 of LCR is always 1 for RS485 test */
if ( i == 0 )
&amp;nbsp; LPC_UART1-&amp;gt;LCR &amp;amp;= ~(0x1&amp;lt;&amp;lt;4);/* Sticky parity to 1 */
else
&amp;nbsp; LPC_UART1-&amp;gt;LCR |= (0x1&amp;lt;&amp;lt;4);/* Sticky parity to 0 */
LPC_UART1-&amp;gt;LCR |= (0x1&amp;lt;&amp;lt;3);
BufferPtr++;
&amp;nbsp; }
&amp;nbsp; return;
}

/******************************************************************************
**&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Of File
******************************************************************************/
&lt;/PRE&gt; &lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Jun 2016 21:28:34 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPCXpresso-IDE/Two-LPC1765-devices-communicating-through-RS-485/m-p/588675#M28249</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-15T21:28:34Z</dc:date>
    </item>
    <item>
      <title>Re: Two LPC1765 devices communicating through RS-485?</title>
      <link>https://community.nxp.com/t5/LPCXpresso-IDE/Two-LPC1765-devices-communicating-through-RS-485/m-p/588676#M28250</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by Wolfie on Fri Sep 21 06:16:14 MST 2012&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;I got RS485 to work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Using LPC17xx user manual at &lt;/SPAN&gt;&lt;A href="http://" rel="nofollow noopener noreferrer" target="_blank"&gt;http://www.nxp.com/documents/user_manual/UM10360.pdf&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In rs485.c in function RS485Init, direction control bits were wrong with PINSEL1 instead of PINSEL4; according to manual page 110, this should be correct (well, you can write 0x800 in many ways, but this is just a quick fix):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD bgcolor="#cacaca"&gt; &lt;PRE&gt;if (DIR_CTRL == 1){
&amp;nbsp; LPC_PINCON-&amp;gt;PINSEL4 |= 0x800;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp; LPC_UART1-&amp;gt;RS485CTRL |= (RS485_DCTRL|RS485_SEL|RS485_OINV);
}else{
&amp;nbsp; LPC_PINCON-&amp;gt;PINSEL4 |= 0x8000;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp; LPC_UART1-&amp;gt;RS485CTRL |= (RS485_DCTRL|RS485_SEL|RS485_OINV);
} &lt;/PRE&gt; &lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;BR /&gt;&lt;SPAN&gt;The second device has received my buffer.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Jun 2016 21:28:35 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPCXpresso-IDE/Two-LPC1765-devices-communicating-through-RS-485/m-p/588676#M28250</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-15T21:28:35Z</dc:date>
    </item>
  </channel>
</rss>

