These are the examples provided for [[/users/frank26080115/libraries/LPC1700CMSIS_Lib/]] Note, the entire "program" is not compilable!

Committer:
frank26080115
Date:
Sun Mar 20 05:38:56 2011 +0000
Revision:
0:bf7b9fba3924

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
frank26080115 0:bf7b9fba3924 1 /***********************************************************************//**
frank26080115 0:bf7b9fba3924 2 * @file rs485_slave.c
frank26080115 0:bf7b9fba3924 3 * @purpose This example used to test RS485 functionality on UART1 of
frank26080115 0:bf7b9fba3924 4 * LPC1768.In this case, RS485 function on UART1 acts as SLave
frank26080115 0:bf7b9fba3924 5 * on RS485 bus.
frank26080115 0:bf7b9fba3924 6 * @version 2.0
frank26080115 0:bf7b9fba3924 7 * @date 21. May. 2010
frank26080115 0:bf7b9fba3924 8 * @author NXP MCU SW Application Team
frank26080115 0:bf7b9fba3924 9 *---------------------------------------------------------------------
frank26080115 0:bf7b9fba3924 10 * Software that is described herein is for illustrative purposes only
frank26080115 0:bf7b9fba3924 11 * which provides customers with programming information regarding the
frank26080115 0:bf7b9fba3924 12 * products. This software is supplied "AS IS" without any warranties.
frank26080115 0:bf7b9fba3924 13 * NXP Semiconductors assumes no responsibility or liability for the
frank26080115 0:bf7b9fba3924 14 * use of the software, conveys no license or title under any patent,
frank26080115 0:bf7b9fba3924 15 * copyright, or mask work right to the product. NXP Semiconductors
frank26080115 0:bf7b9fba3924 16 * reserves the right to make changes in the software without
frank26080115 0:bf7b9fba3924 17 * notification. NXP Semiconductors also make no representation or
frank26080115 0:bf7b9fba3924 18 * warranty that such application will be suitable for the specified
frank26080115 0:bf7b9fba3924 19 * use without further testing or modification.
frank26080115 0:bf7b9fba3924 20 **********************************************************************/
frank26080115 0:bf7b9fba3924 21 #include "lpc17xx_uart.h"
frank26080115 0:bf7b9fba3924 22 #include "lpc17xx_libcfg.h"
frank26080115 0:bf7b9fba3924 23 #include "lpc17xx_pinsel.h"
frank26080115 0:bf7b9fba3924 24
frank26080115 0:bf7b9fba3924 25 /* Example group ----------------------------------------------------------- */
frank26080115 0:bf7b9fba3924 26 /** @defgroup UART_RS485_Slave RS485_Slave
frank26080115 0:bf7b9fba3924 27 * @ingroup UART_Examples
frank26080115 0:bf7b9fba3924 28 * @{
frank26080115 0:bf7b9fba3924 29 */
frank26080115 0:bf7b9fba3924 30
frank26080115 0:bf7b9fba3924 31 /************************** PRIVATE DEFINITIONS *************************/
frank26080115 0:bf7b9fba3924 32 /*
frank26080115 0:bf7b9fba3924 33 * These following defines can be modified:
frank26080115 0:bf7b9fba3924 34 * - RECEIVER_ALWAYS_EN (0/1)
frank26080115 0:bf7b9fba3924 35 * - AUTO_SLVADDR_DETECT (0/1) in case RECEIVER_ALWAYS_EN is set to 0
frank26080115 0:bf7b9fba3924 36 */
frank26080115 0:bf7b9fba3924 37
frank26080115 0:bf7b9fba3924 38 /* Receiver always be enabled to receive any data frame on RS485 bus,
frank26080115 0:bf7b9fba3924 39 * regardless that frame is data frame or slave address frame (9-bit).
frank26080115 0:bf7b9fba3924 40 * - When receiving a data frame, slave will display that data frame content
frank26080115 0:bf7b9fba3924 41 * via UART0.
frank26080115 0:bf7b9fba3924 42 * - When receiving a slave address frame (9bit mode), line error interrupt
frank26080115 0:bf7b9fba3924 43 * - 0: Receiver is not always enabled, only slave address frame can trigger
frank26080115 0:bf7b9fba3924 44 * an interrupt event to allow slave handle.
frank26080115 0:bf7b9fba3924 45 * - 1: Receiver always be enabled */
frank26080115 0:bf7b9fba3924 46 #define RECEIVER_ALWAYS_EN 0
frank26080115 0:bf7b9fba3924 47
frank26080115 0:bf7b9fba3924 48
frank26080115 0:bf7b9fba3924 49 #if (RECEIVER_ALWAYS_EN == 0)
frank26080115 0:bf7b9fba3924 50 /* Enable/Disable Auto Slave Address Detection
frank26080115 0:bf7b9fba3924 51 * - In case of '0': any received data bytes will be ignored and will not
frank26080115 0:bf7b9fba3924 52 * be stored in the RXFIFO. When an address byte is detected (parity bit
frank26080115 0:bf7b9fba3924 53 * = '1') it will be placed into the RXFIFO and an Rx Data Ready Interrupt
frank26080115 0:bf7b9fba3924 54 * will be generated. The interrupt handler can then read the address byte
frank26080115 0:bf7b9fba3924 55 * and decide whether or not to enable the receiver to accept the following data.
frank26080115 0:bf7b9fba3924 56 * - In case of '1': any received byte will be discarded if it is either a
frank26080115 0:bf7b9fba3924 57 * data byte OR an address byte which fails to match the slave address configured
frank26080115 0:bf7b9fba3924 58 * when initializing RS485. When a matching address character is detected it will
frank26080115 0:bf7b9fba3924 59 * be pushed onto the RXFIFO along with the parity bit, and the receiver will
frank26080115 0:bf7b9fba3924 60 * be automatically enabled (RS485CTRL bit 1 will be cleared by hardware).
frank26080115 0:bf7b9fba3924 61 * The receiver will also generate an Rx Data Ready Interrupt */
frank26080115 0:bf7b9fba3924 62 #define AUTO_SLVADDR_DETECT 1
frank26080115 0:bf7b9fba3924 63 #define SLAVE_ADDR 'A'
frank26080115 0:bf7b9fba3924 64 #endif
frank26080115 0:bf7b9fba3924 65
frank26080115 0:bf7b9fba3924 66 /* buffer size definition */
frank26080115 0:bf7b9fba3924 67 #define UART_RING_BUFSIZE 256
frank26080115 0:bf7b9fba3924 68
frank26080115 0:bf7b9fba3924 69 /* Buf mask */
frank26080115 0:bf7b9fba3924 70 #define __BUF_MASK (UART_RING_BUFSIZE-1)
frank26080115 0:bf7b9fba3924 71 /* Check buf is full or not */
frank26080115 0:bf7b9fba3924 72 #define __BUF_IS_FULL(head, tail) ((tail&__BUF_MASK)==((head+1)&__BUF_MASK))
frank26080115 0:bf7b9fba3924 73 /* Check buf will be full in next receiving or not */
frank26080115 0:bf7b9fba3924 74 #define __BUF_WILL_FULL(head, tail) ((tail&__BUF_MASK)==((head+2)&__BUF_MASK))
frank26080115 0:bf7b9fba3924 75 /* Check buf is empty */
frank26080115 0:bf7b9fba3924 76 #define __BUF_IS_EMPTY(head, tail) ((head&__BUF_MASK)==(tail&__BUF_MASK))
frank26080115 0:bf7b9fba3924 77 /* Reset buf */
frank26080115 0:bf7b9fba3924 78 #define __BUF_RESET(bufidx) (bufidx=0)
frank26080115 0:bf7b9fba3924 79 #define __BUF_INCR(bufidx) (bufidx=(bufidx+1)&__BUF_MASK)
frank26080115 0:bf7b9fba3924 80
frank26080115 0:bf7b9fba3924 81 /************************** PRIVATE TYPES *************************/
frank26080115 0:bf7b9fba3924 82 /** @brief UART Ring buffer structure */
frank26080115 0:bf7b9fba3924 83 typedef struct
frank26080115 0:bf7b9fba3924 84 {
frank26080115 0:bf7b9fba3924 85 __IO uint32_t tx_head; /*!< UART Tx ring buffer head index */
frank26080115 0:bf7b9fba3924 86 __IO uint32_t tx_tail; /*!< UART Tx ring buffer tail index */
frank26080115 0:bf7b9fba3924 87 __IO uint32_t rx_head; /*!< UART Rx ring buffer head index */
frank26080115 0:bf7b9fba3924 88 __IO uint32_t rx_tail; /*!< UART Rx ring buffer tail index */
frank26080115 0:bf7b9fba3924 89 __IO uint8_t tx[UART_RING_BUFSIZE]; /*!< UART Tx data ring buffer */
frank26080115 0:bf7b9fba3924 90 __IO uint8_t rx[UART_RING_BUFSIZE]; /*!< UART Rx data ring buffer */
frank26080115 0:bf7b9fba3924 91 } UART_RING_BUFFER_T;
frank26080115 0:bf7b9fba3924 92
frank26080115 0:bf7b9fba3924 93 /************************** PRIVATE VARIABLES *************************/
frank26080115 0:bf7b9fba3924 94 #if (RECEIVER_ALWAYS_EN)
frank26080115 0:bf7b9fba3924 95 uint8_t menu1[] =
frank26080115 0:bf7b9fba3924 96 "Hello NXP Semiconductors \n\r" \
frank26080115 0:bf7b9fba3924 97 "RS485 demo in Slave mode \n\r" \
frank26080115 0:bf7b9fba3924 98 "Slave's Receiver always enabled \n\r";
frank26080115 0:bf7b9fba3924 99 #else
frank26080115 0:bf7b9fba3924 100 #if (AUTO_SLVADDR_DETECT == 0)
frank26080115 0:bf7b9fba3924 101 uint8_t menu1[] =
frank26080115 0:bf7b9fba3924 102 "Hello NXP Semiconductors \n\r" \
frank26080115 0:bf7b9fba3924 103 "RS485 demo in Slave mode \n\r" \
frank26080115 0:bf7b9fba3924 104 "Slave's Receiver is not always enabled - Auto Address Detection is disabled\n\r";
frank26080115 0:bf7b9fba3924 105 #else
frank26080115 0:bf7b9fba3924 106 uint8_t menu1[] =
frank26080115 0:bf7b9fba3924 107 "Hello NXP Semiconductors \n\r" \
frank26080115 0:bf7b9fba3924 108 "RS485 demo in Slave mode \n\r" \
frank26080115 0:bf7b9fba3924 109 "Slave's Receiver is not always enabled - Auto Address Detection is enabled\n\r";
frank26080115 0:bf7b9fba3924 110 #endif
frank26080115 0:bf7b9fba3924 111 #endif
frank26080115 0:bf7b9fba3924 112
frank26080115 0:bf7b9fba3924 113 uint8_t send_msg[] = "Sending... \n\r";
frank26080115 0:bf7b9fba3924 114 uint8_t recv_msg[] = "Receive: ";
frank26080115 0:bf7b9fba3924 115 uint8_t p_err_menu[] = "Parity error";
frank26080115 0:bf7b9fba3924 116 uint8_t addr_menu[] = " - Slv Addr Frm received\n\r";
frank26080115 0:bf7b9fba3924 117 uint8_t addr_acc[] = " - Slave Addr accepted\n\r";
frank26080115 0:bf7b9fba3924 118 uint8_t addr_una[] = " - Slave Addr unaccepted\n\r";
frank26080115 0:bf7b9fba3924 119 uint8_t addr_auto[] = "Slave Addr detected!\n\r";
frank26080115 0:bf7b9fba3924 120 uint8_t f_err_menu[] = "Frame error \n\r";
frank26080115 0:bf7b9fba3924 121 uint8_t nextline[] = "\n\r";
frank26080115 0:bf7b9fba3924 122
frank26080115 0:bf7b9fba3924 123 uint8_t ack_msg[] = "ACK";
frank26080115 0:bf7b9fba3924 124 uint8_t terminator = 13;
frank26080115 0:bf7b9fba3924 125
frank26080115 0:bf7b9fba3924 126 // UART Ring buffer
frank26080115 0:bf7b9fba3924 127 UART_RING_BUFFER_T rb;
frank26080115 0:bf7b9fba3924 128
frank26080115 0:bf7b9fba3924 129 /************************** PRIVATE FUNCTIONS *************************/
frank26080115 0:bf7b9fba3924 130 void UART1_IRQHandler(void);
frank26080115 0:bf7b9fba3924 131 void UART_IntReceive(void);
frank26080115 0:bf7b9fba3924 132 void UART_IntErr(uint8_t bLSErrType);
frank26080115 0:bf7b9fba3924 133
frank26080115 0:bf7b9fba3924 134 uint32_t UARTReceive(LPC_UART_TypeDef *UARTPort, uint8_t *rxbuf, uint8_t buflen);
frank26080115 0:bf7b9fba3924 135 void print_menu(void);
frank26080115 0:bf7b9fba3924 136
frank26080115 0:bf7b9fba3924 137 /*----------------- INTERRUPT SERVICE ROUTINES --------------------------*/
frank26080115 0:bf7b9fba3924 138 /*********************************************************************//**
frank26080115 0:bf7b9fba3924 139 * @brief UART1 interrupt handler sub-routine
frank26080115 0:bf7b9fba3924 140 * @param[in] None
frank26080115 0:bf7b9fba3924 141 * @return None
frank26080115 0:bf7b9fba3924 142 **********************************************************************/
frank26080115 0:bf7b9fba3924 143 void UART1_IRQHandler(void)
frank26080115 0:bf7b9fba3924 144 {
frank26080115 0:bf7b9fba3924 145 // Call Standard UART 0 interrupt handler
frank26080115 0:bf7b9fba3924 146 uint32_t intsrc, tmp, tmp1;
frank26080115 0:bf7b9fba3924 147
frank26080115 0:bf7b9fba3924 148 /* Determine the interrupt source */
frank26080115 0:bf7b9fba3924 149 intsrc = UART_GetIntId(LPC_UART0);
frank26080115 0:bf7b9fba3924 150 tmp = intsrc & UART_IIR_INTID_MASK;
frank26080115 0:bf7b9fba3924 151
frank26080115 0:bf7b9fba3924 152 // Receive Line Status
frank26080115 0:bf7b9fba3924 153 if (tmp == UART_IIR_INTID_RLS){
frank26080115 0:bf7b9fba3924 154 // Check line status
frank26080115 0:bf7b9fba3924 155 tmp1 = UART_GetLineStatus(LPC_UART0);
frank26080115 0:bf7b9fba3924 156 // Mask out the Receive Ready and Transmit Holding empty status
frank26080115 0:bf7b9fba3924 157 tmp1 &= (UART_LSR_OE | UART_LSR_PE | UART_LSR_FE \
frank26080115 0:bf7b9fba3924 158 | UART_LSR_BI | UART_LSR_RXFE);
frank26080115 0:bf7b9fba3924 159 // If any error exist
frank26080115 0:bf7b9fba3924 160 if (tmp1) {
frank26080115 0:bf7b9fba3924 161 UART_IntErr(tmp1);
frank26080115 0:bf7b9fba3924 162 }
frank26080115 0:bf7b9fba3924 163 }
frank26080115 0:bf7b9fba3924 164
frank26080115 0:bf7b9fba3924 165 // Receive Data Available or Character time-out
frank26080115 0:bf7b9fba3924 166 if ((tmp == UART_IIR_INTID_RDA) || (tmp == UART_IIR_INTID_CTI)){
frank26080115 0:bf7b9fba3924 167 UART_IntReceive();
frank26080115 0:bf7b9fba3924 168 }
frank26080115 0:bf7b9fba3924 169 }
frank26080115 0:bf7b9fba3924 170
frank26080115 0:bf7b9fba3924 171
frank26080115 0:bf7b9fba3924 172 /********************************************************************//**
frank26080115 0:bf7b9fba3924 173 * @brief UART receive function (ring buffer used)
frank26080115 0:bf7b9fba3924 174 * @param[in] None
frank26080115 0:bf7b9fba3924 175 * @return None
frank26080115 0:bf7b9fba3924 176 *********************************************************************/
frank26080115 0:bf7b9fba3924 177 void UART_IntReceive(void)
frank26080115 0:bf7b9fba3924 178 {
frank26080115 0:bf7b9fba3924 179
frank26080115 0:bf7b9fba3924 180 #if (RECEIVER_ALWAYS_EN)
frank26080115 0:bf7b9fba3924 181 uint8_t tmpc;
frank26080115 0:bf7b9fba3924 182 uint32_t rLen;
frank26080115 0:bf7b9fba3924 183
frank26080115 0:bf7b9fba3924 184 while(1){
frank26080115 0:bf7b9fba3924 185 // Call UART read function in UART driver
frank26080115 0:bf7b9fba3924 186 rLen = UART_Receive((LPC_UART_TypeDef *)LPC_UART1, &tmpc, 1, NONE_BLOCKING);
frank26080115 0:bf7b9fba3924 187 // If data received
frank26080115 0:bf7b9fba3924 188 if (rLen){
frank26080115 0:bf7b9fba3924 189 /* Check if buffer is more space
frank26080115 0:bf7b9fba3924 190 * If no more space, remaining character will be trimmed out
frank26080115 0:bf7b9fba3924 191 */
frank26080115 0:bf7b9fba3924 192 if (!__BUF_IS_FULL(rb.rx_head,rb.rx_tail)){
frank26080115 0:bf7b9fba3924 193 rb.rx[rb.rx_head] = tmpc;
frank26080115 0:bf7b9fba3924 194 __BUF_INCR(rb.rx_head);
frank26080115 0:bf7b9fba3924 195 }
frank26080115 0:bf7b9fba3924 196 }
frank26080115 0:bf7b9fba3924 197 // no more data
frank26080115 0:bf7b9fba3924 198 else {
frank26080115 0:bf7b9fba3924 199 break;
frank26080115 0:bf7b9fba3924 200 }
frank26080115 0:bf7b9fba3924 201 }
frank26080115 0:bf7b9fba3924 202 #else
frank26080115 0:bf7b9fba3924 203 #if (AUTO_SLVADDR_DETECT == 0)
frank26080115 0:bf7b9fba3924 204
frank26080115 0:bf7b9fba3924 205 uint8_t tmpc;
frank26080115 0:bf7b9fba3924 206 uint32_t rLen;
frank26080115 0:bf7b9fba3924 207
frank26080115 0:bf7b9fba3924 208 while(1){
frank26080115 0:bf7b9fba3924 209 // Call UART read function in UART driver
frank26080115 0:bf7b9fba3924 210 rLen = UART_Receive((UART_TypeDef *)UART1, &tmpc, 1, NONE_BLOCKING);
frank26080115 0:bf7b9fba3924 211 // If data received
frank26080115 0:bf7b9fba3924 212 if (rLen){
frank26080115 0:bf7b9fba3924 213 /* Check if buffer is more space
frank26080115 0:bf7b9fba3924 214 * If no more space, remaining character will be trimmed out
frank26080115 0:bf7b9fba3924 215 */
frank26080115 0:bf7b9fba3924 216 if (!__BUF_IS_FULL(rb.rx_head,rb.rx_tail)){
frank26080115 0:bf7b9fba3924 217 rb.rx[rb.rx_head] = tmpc;
frank26080115 0:bf7b9fba3924 218 __BUF_INCR(rb.rx_head);
frank26080115 0:bf7b9fba3924 219 }
frank26080115 0:bf7b9fba3924 220 }
frank26080115 0:bf7b9fba3924 221 // no more data
frank26080115 0:bf7b9fba3924 222 else {
frank26080115 0:bf7b9fba3924 223 break;
frank26080115 0:bf7b9fba3924 224 }
frank26080115 0:bf7b9fba3924 225 }
frank26080115 0:bf7b9fba3924 226 #else
frank26080115 0:bf7b9fba3924 227
frank26080115 0:bf7b9fba3924 228 uint8_t tmpc;
frank26080115 0:bf7b9fba3924 229 uint32_t rLen;
frank26080115 0:bf7b9fba3924 230
frank26080115 0:bf7b9fba3924 231 while(1){
frank26080115 0:bf7b9fba3924 232 // Call UART read function in UART driver
frank26080115 0:bf7b9fba3924 233 rLen = UART_Receive((LPC_UART_TypeDef *)LPC_UART1, &tmpc, 1, NONE_BLOCKING);
frank26080115 0:bf7b9fba3924 234 // If data received
frank26080115 0:bf7b9fba3924 235 if (rLen){
frank26080115 0:bf7b9fba3924 236 /* Check if buffer is more space
frank26080115 0:bf7b9fba3924 237 * If no more space, remaining character will be trimmed out
frank26080115 0:bf7b9fba3924 238 */
frank26080115 0:bf7b9fba3924 239 if (!__BUF_IS_FULL(rb.rx_head,rb.rx_tail)){
frank26080115 0:bf7b9fba3924 240 rb.rx[rb.rx_head] = tmpc;
frank26080115 0:bf7b9fba3924 241 __BUF_INCR(rb.rx_head);
frank26080115 0:bf7b9fba3924 242 }
frank26080115 0:bf7b9fba3924 243 }
frank26080115 0:bf7b9fba3924 244 // no more data
frank26080115 0:bf7b9fba3924 245 else {
frank26080115 0:bf7b9fba3924 246 break;
frank26080115 0:bf7b9fba3924 247 }
frank26080115 0:bf7b9fba3924 248 }
frank26080115 0:bf7b9fba3924 249 #endif
frank26080115 0:bf7b9fba3924 250 #endif
frank26080115 0:bf7b9fba3924 251 }
frank26080115 0:bf7b9fba3924 252
frank26080115 0:bf7b9fba3924 253
frank26080115 0:bf7b9fba3924 254 /*********************************************************************//**
frank26080115 0:bf7b9fba3924 255 * @brief UART Line Status Error
frank26080115 0:bf7b9fba3924 256 * @param[in] bLSErrType UART Line Status Error Type
frank26080115 0:bf7b9fba3924 257 * @return None
frank26080115 0:bf7b9fba3924 258 **********************************************************************/
frank26080115 0:bf7b9fba3924 259 void UART_IntErr(uint8_t bLSErrType)
frank26080115 0:bf7b9fba3924 260 {
frank26080115 0:bf7b9fba3924 261 uint8_t tmp;
frank26080115 0:bf7b9fba3924 262 #if (RECEIVER_ALWAYS_EN)
frank26080115 0:bf7b9fba3924 263 uint8_t tmpc;
frank26080115 0:bf7b9fba3924 264
frank26080115 0:bf7b9fba3924 265 if (bLSErrType & UART_LSR_PE){
frank26080115 0:bf7b9fba3924 266 // Parity error means the latest frame receive is slave address frame,
frank26080115 0:bf7b9fba3924 267 // Value of slave address is read and trimmed out.
frank26080115 0:bf7b9fba3924 268 UART_Send(LPC_UART0, p_err_menu, sizeof(p_err_menu), BLOCKING);
frank26080115 0:bf7b9fba3924 269 UART_Send(LPC_UART0, addr_menu, sizeof(addr_menu), BLOCKING);
frank26080115 0:bf7b9fba3924 270 UART_Receive((LPC_UART_TypeDef *)LPC_UART1, &tmpc, 1, NONE_BLOCKING);
frank26080115 0:bf7b9fba3924 271 }
frank26080115 0:bf7b9fba3924 272
frank26080115 0:bf7b9fba3924 273 if (bLSErrType & UART_LSR_FE){
frank26080115 0:bf7b9fba3924 274 UART_Send(LPC_UART0, f_err_menu, sizeof(f_err_menu), BLOCKING);
frank26080115 0:bf7b9fba3924 275 }
frank26080115 0:bf7b9fba3924 276 #else
frank26080115 0:bf7b9fba3924 277 #if (AUTO_SLVADDR_DETECT == 0)
frank26080115 0:bf7b9fba3924 278 uint8_t tmp;
frank26080115 0:bf7b9fba3924 279 // Check if this interrupt caused by parity error,
frank26080115 0:bf7b9fba3924 280 // that means the last received frame is address frame,
frank26080115 0:bf7b9fba3924 281 // if this address is matched with its own address,
frank26080115 0:bf7b9fba3924 282 // continue to receive following data frame.
frank26080115 0:bf7b9fba3924 283 if (bLSErrType & UART_LSR_PE){
frank26080115 0:bf7b9fba3924 284 UART_Receive((UART_TypeDef *)UART1, &tmp, 1, NONE_BLOCKING);
frank26080115 0:bf7b9fba3924 285 UART_Send(UART0, p_err_menu, sizeof(p_err_menu), BLOCKING);
frank26080115 0:bf7b9fba3924 286 if (tmp == SLAVE_ADDR){
frank26080115 0:bf7b9fba3924 287 UART_RS485ReceiverCmd(UART1, ENABLE);
frank26080115 0:bf7b9fba3924 288 UART_Send(UART0, addr_acc, sizeof(addr_acc), BLOCKING);
frank26080115 0:bf7b9fba3924 289 } else {
frank26080115 0:bf7b9fba3924 290 // Disable receiver
frank26080115 0:bf7b9fba3924 291 UART_RS485ReceiverCmd(UART1, DISABLE);
frank26080115 0:bf7b9fba3924 292 UART_Send(UART0, addr_una, sizeof(addr_una), BLOCKING);
frank26080115 0:bf7b9fba3924 293 }
frank26080115 0:bf7b9fba3924 294 }
frank26080115 0:bf7b9fba3924 295 #else
frank26080115 0:bf7b9fba3924 296
frank26080115 0:bf7b9fba3924 297 // Check if this interrupt caused by parity error,
frank26080115 0:bf7b9fba3924 298 // that means the last received frame is address frame,
frank26080115 0:bf7b9fba3924 299 // if this address is matched with its own address,
frank26080115 0:bf7b9fba3924 300 // continue to receive following data frame.
frank26080115 0:bf7b9fba3924 301 if (bLSErrType & UART_LSR_PE){
frank26080115 0:bf7b9fba3924 302 UART_Receive((LPC_UART_TypeDef *)LPC_UART1, &tmp, 1, NONE_BLOCKING);
frank26080115 0:bf7b9fba3924 303 UART_Send(LPC_UART0, addr_auto, sizeof(addr_auto), BLOCKING);
frank26080115 0:bf7b9fba3924 304 }
frank26080115 0:bf7b9fba3924 305 #endif
frank26080115 0:bf7b9fba3924 306 #endif
frank26080115 0:bf7b9fba3924 307 }
frank26080115 0:bf7b9fba3924 308
frank26080115 0:bf7b9fba3924 309 /*-------------------------PRIVATE FUNCTIONS------------------------------*/
frank26080115 0:bf7b9fba3924 310 /*********************************************************************//**
frank26080115 0:bf7b9fba3924 311 * @brief UART read function for interrupt mode (using ring buffers)
frank26080115 0:bf7b9fba3924 312 * @param[in] UARTPort Selected UART peripheral used to send data,
frank26080115 0:bf7b9fba3924 313 * should be UART0
frank26080115 0:bf7b9fba3924 314 * @param[out] rxbuf Pointer to Received buffer
frank26080115 0:bf7b9fba3924 315 * @param[in] buflen Length of Received buffer
frank26080115 0:bf7b9fba3924 316 * @return Number of bytes actually read from the ring buffer
frank26080115 0:bf7b9fba3924 317 **********************************************************************/
frank26080115 0:bf7b9fba3924 318 uint32_t UARTReceive(LPC_UART_TypeDef *UARTPort, uint8_t *rxbuf, uint8_t buflen)
frank26080115 0:bf7b9fba3924 319 {
frank26080115 0:bf7b9fba3924 320 uint8_t *data = (uint8_t *) rxbuf;
frank26080115 0:bf7b9fba3924 321 uint32_t bytes = 0;
frank26080115 0:bf7b9fba3924 322
frank26080115 0:bf7b9fba3924 323 /* Temporarily lock out UART receive interrupts during this
frank26080115 0:bf7b9fba3924 324 read so the UART receive interrupt won't cause problems
frank26080115 0:bf7b9fba3924 325 with the index values */
frank26080115 0:bf7b9fba3924 326 UART_IntConfig(UARTPort, UART_INTCFG_RBR, DISABLE);
frank26080115 0:bf7b9fba3924 327
frank26080115 0:bf7b9fba3924 328 /* Loop until receive buffer ring is empty or
frank26080115 0:bf7b9fba3924 329 until max_bytes expires */
frank26080115 0:bf7b9fba3924 330 while ((buflen > 0) && (!(__BUF_IS_EMPTY(rb.rx_head, rb.rx_tail))))
frank26080115 0:bf7b9fba3924 331 {
frank26080115 0:bf7b9fba3924 332 /* Read data from ring buffer into user buffer */
frank26080115 0:bf7b9fba3924 333 *data = rb.rx[rb.rx_tail];
frank26080115 0:bf7b9fba3924 334 data++;
frank26080115 0:bf7b9fba3924 335
frank26080115 0:bf7b9fba3924 336 /* Update tail pointer */
frank26080115 0:bf7b9fba3924 337 __BUF_INCR(rb.rx_tail);
frank26080115 0:bf7b9fba3924 338
frank26080115 0:bf7b9fba3924 339 /* Increment data count and decrement buffer size count */
frank26080115 0:bf7b9fba3924 340 bytes++;
frank26080115 0:bf7b9fba3924 341 buflen--;
frank26080115 0:bf7b9fba3924 342 }
frank26080115 0:bf7b9fba3924 343
frank26080115 0:bf7b9fba3924 344 /* Re-enable UART interrupts */
frank26080115 0:bf7b9fba3924 345 UART_IntConfig(UARTPort, UART_INTCFG_RBR, ENABLE);
frank26080115 0:bf7b9fba3924 346
frank26080115 0:bf7b9fba3924 347 return bytes;
frank26080115 0:bf7b9fba3924 348 }
frank26080115 0:bf7b9fba3924 349
frank26080115 0:bf7b9fba3924 350 /*********************************************************************//**
frank26080115 0:bf7b9fba3924 351 * @brief Print Welcome menu
frank26080115 0:bf7b9fba3924 352 * @param[in] none
frank26080115 0:bf7b9fba3924 353 * @return None
frank26080115 0:bf7b9fba3924 354 **********************************************************************/
frank26080115 0:bf7b9fba3924 355 void print_menu(void)
frank26080115 0:bf7b9fba3924 356 {
frank26080115 0:bf7b9fba3924 357 UART_Send(LPC_UART0, menu1, sizeof(menu1), BLOCKING);
frank26080115 0:bf7b9fba3924 358 }
frank26080115 0:bf7b9fba3924 359
frank26080115 0:bf7b9fba3924 360
frank26080115 0:bf7b9fba3924 361 /*-------------------------MAIN FUNCTION------------------------------*/
frank26080115 0:bf7b9fba3924 362 /*********************************************************************//**
frank26080115 0:bf7b9fba3924 363 * @brief c_entry: Main UART-RS485 program body
frank26080115 0:bf7b9fba3924 364 * @param[in] None
frank26080115 0:bf7b9fba3924 365 * @return int
frank26080115 0:bf7b9fba3924 366 **********************************************************************/
frank26080115 0:bf7b9fba3924 367 int c_entry(void)
frank26080115 0:bf7b9fba3924 368 {
frank26080115 0:bf7b9fba3924 369 // UART Configuration structure variable
frank26080115 0:bf7b9fba3924 370 UART_CFG_Type UARTConfigStruct;
frank26080115 0:bf7b9fba3924 371 // UART FIFO configuration Struct variable
frank26080115 0:bf7b9fba3924 372 UART_FIFO_CFG_Type UARTFIFOConfigStruct;
frank26080115 0:bf7b9fba3924 373 // Pin configuration
frank26080115 0:bf7b9fba3924 374 PINSEL_CFG_Type PinCfg;
frank26080115 0:bf7b9fba3924 375 // RS485 configuration
frank26080115 0:bf7b9fba3924 376 UART1_RS485_CTRLCFG_Type rs485cfg;
frank26080115 0:bf7b9fba3924 377 uint32_t idx, len;
frank26080115 0:bf7b9fba3924 378 uint8_t buffer[10];
frank26080115 0:bf7b9fba3924 379 uint32_t tmp;
frank26080115 0:bf7b9fba3924 380
frank26080115 0:bf7b9fba3924 381 // UART0 section ----------------------------------------------------
frank26080115 0:bf7b9fba3924 382 /*
frank26080115 0:bf7b9fba3924 383 * Initialize UART0 pin connect
frank26080115 0:bf7b9fba3924 384 */
frank26080115 0:bf7b9fba3924 385 PinCfg.Funcnum = 1;
frank26080115 0:bf7b9fba3924 386 PinCfg.OpenDrain = 0;
frank26080115 0:bf7b9fba3924 387 PinCfg.Pinmode = 0;
frank26080115 0:bf7b9fba3924 388 PinCfg.Pinnum = 2;
frank26080115 0:bf7b9fba3924 389 PinCfg.Portnum = 0;
frank26080115 0:bf7b9fba3924 390 PINSEL_ConfigPin(&PinCfg);
frank26080115 0:bf7b9fba3924 391 PinCfg.Pinnum = 3;
frank26080115 0:bf7b9fba3924 392 PINSEL_ConfigPin(&PinCfg);
frank26080115 0:bf7b9fba3924 393
frank26080115 0:bf7b9fba3924 394 /* Initialize UART Configuration parameter structure to default state:
frank26080115 0:bf7b9fba3924 395 * Baudrate = 115200 bps
frank26080115 0:bf7b9fba3924 396 * 8 data bit
frank26080115 0:bf7b9fba3924 397 * 1 Stop bit
frank26080115 0:bf7b9fba3924 398 * None parity
frank26080115 0:bf7b9fba3924 399 */
frank26080115 0:bf7b9fba3924 400 UART_ConfigStructInit(&UARTConfigStruct);
frank26080115 0:bf7b9fba3924 401 UARTConfigStruct.Baud_rate = 115200;
frank26080115 0:bf7b9fba3924 402
frank26080115 0:bf7b9fba3924 403 // Initialize UART0 peripheral with given to corresponding parameter
frank26080115 0:bf7b9fba3924 404 UART_Init(LPC_UART0, &UARTConfigStruct);
frank26080115 0:bf7b9fba3924 405
frank26080115 0:bf7b9fba3924 406 /* Initialize FIFOConfigStruct to default state:
frank26080115 0:bf7b9fba3924 407 * - FIFO_DMAMode = DISABLE
frank26080115 0:bf7b9fba3924 408 * - FIFO_Level = UART_FIFO_TRGLEV0
frank26080115 0:bf7b9fba3924 409 * - FIFO_ResetRxBuf = ENABLE
frank26080115 0:bf7b9fba3924 410 * - FIFO_ResetTxBuf = ENABLE
frank26080115 0:bf7b9fba3924 411 * - FIFO_State = ENABLE
frank26080115 0:bf7b9fba3924 412 */
frank26080115 0:bf7b9fba3924 413 UART_FIFOConfigStructInit(&UARTFIFOConfigStruct);
frank26080115 0:bf7b9fba3924 414
frank26080115 0:bf7b9fba3924 415 // Initialize FIFO for UART0 peripheral
frank26080115 0:bf7b9fba3924 416 UART_FIFOConfig(LPC_UART0, &UARTFIFOConfigStruct);
frank26080115 0:bf7b9fba3924 417
frank26080115 0:bf7b9fba3924 418 // Enable UART Transmit
frank26080115 0:bf7b9fba3924 419 UART_TxCmd(LPC_UART0, ENABLE);
frank26080115 0:bf7b9fba3924 420
frank26080115 0:bf7b9fba3924 421 // print welcome screen
frank26080115 0:bf7b9fba3924 422 print_menu();
frank26080115 0:bf7b9fba3924 423
frank26080115 0:bf7b9fba3924 424
frank26080115 0:bf7b9fba3924 425 // UART1 - RS485 section -------------------------------------------------
frank26080115 0:bf7b9fba3924 426 /*
frank26080115 0:bf7b9fba3924 427 * Initialize UART1 pin connect
frank26080115 0:bf7b9fba3924 428 */
frank26080115 0:bf7b9fba3924 429 PinCfg.Funcnum = 2;
frank26080115 0:bf7b9fba3924 430 PinCfg.OpenDrain = 0;
frank26080115 0:bf7b9fba3924 431 PinCfg.Pinmode = 0;
frank26080115 0:bf7b9fba3924 432 // TXD1 - P2.0
frank26080115 0:bf7b9fba3924 433 PinCfg.Pinnum = 0;
frank26080115 0:bf7b9fba3924 434 PinCfg.Portnum = 2;
frank26080115 0:bf7b9fba3924 435 PINSEL_ConfigPin(&PinCfg);
frank26080115 0:bf7b9fba3924 436 // RXD1 - P2.1
frank26080115 0:bf7b9fba3924 437 PinCfg.Pinnum = 1;
frank26080115 0:bf7b9fba3924 438 PINSEL_ConfigPin(&PinCfg);
frank26080115 0:bf7b9fba3924 439 // DTR1 - P2.5
frank26080115 0:bf7b9fba3924 440 PinCfg.Pinnum = 5;
frank26080115 0:bf7b9fba3924 441 PINSEL_ConfigPin(&PinCfg);
frank26080115 0:bf7b9fba3924 442
frank26080115 0:bf7b9fba3924 443
frank26080115 0:bf7b9fba3924 444 /* Initialize UART Configuration parameter structure to default state:
frank26080115 0:bf7b9fba3924 445 * Baudrate = 9600 bps
frank26080115 0:bf7b9fba3924 446 * 8 data bit
frank26080115 0:bf7b9fba3924 447 * 1 Stop bit
frank26080115 0:bf7b9fba3924 448 * Parity: None
frank26080115 0:bf7b9fba3924 449 * Note: Parity will be enabled later in UART_RS485Config() function.
frank26080115 0:bf7b9fba3924 450 */
frank26080115 0:bf7b9fba3924 451 UART_ConfigStructInit(&UARTConfigStruct);
frank26080115 0:bf7b9fba3924 452
frank26080115 0:bf7b9fba3924 453 // Initialize UART0 peripheral with given to corresponding parameter
frank26080115 0:bf7b9fba3924 454 UART_Init((LPC_UART_TypeDef *)LPC_UART1, &UARTConfigStruct);
frank26080115 0:bf7b9fba3924 455
frank26080115 0:bf7b9fba3924 456 /* Initialize FIFOConfigStruct to default state:
frank26080115 0:bf7b9fba3924 457 * - FIFO_DMAMode = DISABLE
frank26080115 0:bf7b9fba3924 458 * - FIFO_Level = UART_FIFO_TRGLEV0
frank26080115 0:bf7b9fba3924 459 * - FIFO_ResetRxBuf = ENABLE
frank26080115 0:bf7b9fba3924 460 * - FIFO_ResetTxBuf = ENABLE
frank26080115 0:bf7b9fba3924 461 * - FIFO_State = ENABLE
frank26080115 0:bf7b9fba3924 462 */
frank26080115 0:bf7b9fba3924 463 UART_FIFOConfigStructInit(&UARTFIFOConfigStruct);
frank26080115 0:bf7b9fba3924 464
frank26080115 0:bf7b9fba3924 465 // Initialize FIFO for UART0 peripheral
frank26080115 0:bf7b9fba3924 466 UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART1, &UARTFIFOConfigStruct);
frank26080115 0:bf7b9fba3924 467
frank26080115 0:bf7b9fba3924 468 // Configure RS485
frank26080115 0:bf7b9fba3924 469 /*
frank26080115 0:bf7b9fba3924 470 * - Auto Direction in Tx/Rx driving is enabled
frank26080115 0:bf7b9fba3924 471 * - Direction control pin is set to DTR1
frank26080115 0:bf7b9fba3924 472 * - Direction control pole is set to "1" that means direction pin
frank26080115 0:bf7b9fba3924 473 * will drive to high state before transmit data.
frank26080115 0:bf7b9fba3924 474 * - Multidrop mode is enable
frank26080115 0:bf7b9fba3924 475 * - Auto detect address is disabled
frank26080115 0:bf7b9fba3924 476 * - Receive state is enable
frank26080115 0:bf7b9fba3924 477 */
frank26080115 0:bf7b9fba3924 478 rs485cfg.AutoDirCtrl_State = ENABLE;
frank26080115 0:bf7b9fba3924 479 rs485cfg.DirCtrlPin = UART1_RS485_DIRCTRL_DTR;
frank26080115 0:bf7b9fba3924 480 rs485cfg.DirCtrlPol_Level = SET;
frank26080115 0:bf7b9fba3924 481 rs485cfg.DelayValue = 50;
frank26080115 0:bf7b9fba3924 482 rs485cfg.NormalMultiDropMode_State = ENABLE;
frank26080115 0:bf7b9fba3924 483 #if AUTO_SLVADDR_DETECT
frank26080115 0:bf7b9fba3924 484 rs485cfg.AutoAddrDetect_State = ENABLE;
frank26080115 0:bf7b9fba3924 485 rs485cfg.MatchAddrValue = SLAVE_ADDR;
frank26080115 0:bf7b9fba3924 486 #else
frank26080115 0:bf7b9fba3924 487 rs485cfg.AutoAddrDetect_State = DISABLE;
frank26080115 0:bf7b9fba3924 488 #endif
frank26080115 0:bf7b9fba3924 489 #if RECEIVER_ALWAYS_EN
frank26080115 0:bf7b9fba3924 490 rs485cfg.Rx_State = ENABLE;
frank26080115 0:bf7b9fba3924 491 #else
frank26080115 0:bf7b9fba3924 492 rs485cfg.Rx_State = DISABLE;
frank26080115 0:bf7b9fba3924 493 #endif
frank26080115 0:bf7b9fba3924 494 UART_RS485Config(LPC_UART1, &rs485cfg);
frank26080115 0:bf7b9fba3924 495
frank26080115 0:bf7b9fba3924 496 /* Enable UART Rx interrupt */
frank26080115 0:bf7b9fba3924 497 UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_RBR, ENABLE);
frank26080115 0:bf7b9fba3924 498 /* Enable UART line status interrupt */
frank26080115 0:bf7b9fba3924 499 UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_RLS, ENABLE);
frank26080115 0:bf7b9fba3924 500
frank26080115 0:bf7b9fba3924 501 /* preemption = 1, sub-priority = 1 */
frank26080115 0:bf7b9fba3924 502 NVIC_SetPriority(UART1_IRQn, ((0x01<<3)|0x01));
frank26080115 0:bf7b9fba3924 503 /* Enable Interrupt for UART0 channel */
frank26080115 0:bf7b9fba3924 504 NVIC_EnableIRQ(UART1_IRQn);
frank26080115 0:bf7b9fba3924 505
frank26080115 0:bf7b9fba3924 506 // Enable UART Transmit
frank26080115 0:bf7b9fba3924 507 UART_TxCmd((LPC_UART_TypeDef *)LPC_UART1, ENABLE);
frank26080115 0:bf7b9fba3924 508
frank26080115 0:bf7b9fba3924 509 // for testing...
frank26080115 0:bf7b9fba3924 510 while (1){
frank26080115 0:bf7b9fba3924 511 len = 0;
frank26080115 0:bf7b9fba3924 512 while (len == 0)
frank26080115 0:bf7b9fba3924 513 {
frank26080115 0:bf7b9fba3924 514 len = UARTReceive((LPC_UART_TypeDef *)LPC_UART1, buffer, sizeof(buffer));
frank26080115 0:bf7b9fba3924 515 }
frank26080115 0:bf7b9fba3924 516
frank26080115 0:bf7b9fba3924 517 /* Got some data */
frank26080115 0:bf7b9fba3924 518 idx = 0;
frank26080115 0:bf7b9fba3924 519 while (idx < len)
frank26080115 0:bf7b9fba3924 520 {
frank26080115 0:bf7b9fba3924 521 if (buffer[idx] == 13){
frank26080115 0:bf7b9fba3924 522 for (tmp = 0; tmp < 1000000; tmp++);
frank26080115 0:bf7b9fba3924 523 UART_RS485SendData(LPC_UART1, ack_msg, sizeof(ack_msg));
frank26080115 0:bf7b9fba3924 524 UART_Send(LPC_UART0, nextline, sizeof(nextline), BLOCKING);
frank26080115 0:bf7b9fba3924 525 UART_RS485SendData(LPC_UART1, &terminator, 1);
frank26080115 0:bf7b9fba3924 526 } else {
frank26080115 0:bf7b9fba3924 527 /* Echo it back */
frank26080115 0:bf7b9fba3924 528 UART_Send(LPC_UART0, &buffer[idx], 1, BLOCKING);
frank26080115 0:bf7b9fba3924 529 }
frank26080115 0:bf7b9fba3924 530 idx++;
frank26080115 0:bf7b9fba3924 531 }
frank26080115 0:bf7b9fba3924 532 }
frank26080115 0:bf7b9fba3924 533
frank26080115 0:bf7b9fba3924 534 return 1;
frank26080115 0:bf7b9fba3924 535 }
frank26080115 0:bf7b9fba3924 536
frank26080115 0:bf7b9fba3924 537 /* With ARM and GHS toolsets, the entry point is main() - this will
frank26080115 0:bf7b9fba3924 538 allow the linker to generate wrapper code to setup stacks, allocate
frank26080115 0:bf7b9fba3924 539 heap area, and initialize and copy code and data segments. For GNU
frank26080115 0:bf7b9fba3924 540 toolsets, the entry point is through __start() in the crt0_gnu.asm
frank26080115 0:bf7b9fba3924 541 file, and that startup code will setup stacks and data */
frank26080115 0:bf7b9fba3924 542 int main(void)
frank26080115 0:bf7b9fba3924 543 {
frank26080115 0:bf7b9fba3924 544 return c_entry();
frank26080115 0:bf7b9fba3924 545 }
frank26080115 0:bf7b9fba3924 546
frank26080115 0:bf7b9fba3924 547
frank26080115 0:bf7b9fba3924 548 #ifdef DEBUG
frank26080115 0:bf7b9fba3924 549 /*******************************************************************************
frank26080115 0:bf7b9fba3924 550 * @brief Reports the name of the source file and the source line number
frank26080115 0:bf7b9fba3924 551 * where the CHECK_PARAM error has occurred.
frank26080115 0:bf7b9fba3924 552 * @param[in] file Pointer to the source file name
frank26080115 0:bf7b9fba3924 553 * @param[in] line assert_param error line source number
frank26080115 0:bf7b9fba3924 554 * @return None
frank26080115 0:bf7b9fba3924 555 *******************************************************************************/
frank26080115 0:bf7b9fba3924 556 void check_failed(uint8_t *file, uint32_t line)
frank26080115 0:bf7b9fba3924 557 {
frank26080115 0:bf7b9fba3924 558 /* User can add his own implementation to report the file name and line number,
frank26080115 0:bf7b9fba3924 559 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
frank26080115 0:bf7b9fba3924 560
frank26080115 0:bf7b9fba3924 561 /* Infinite loop */
frank26080115 0:bf7b9fba3924 562 while(1);
frank26080115 0:bf7b9fba3924 563 }
frank26080115 0:bf7b9fba3924 564 #endif
frank26080115 0:bf7b9fba3924 565
frank26080115 0:bf7b9fba3924 566 /*
frank26080115 0:bf7b9fba3924 567 * @}
frank26080115 0:bf7b9fba3924 568 */