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 uart_fullmodem_test.c
frank26080115 0:bf7b9fba3924 3 * @purpose This example describes how to use UART1 full-modem function
frank26080115 0:bf7b9fba3924 4 * @version 2.0
frank26080115 0:bf7b9fba3924 5 * @date 21. May. 2010
frank26080115 0:bf7b9fba3924 6 * @author NXP MCU SW Application Team
frank26080115 0:bf7b9fba3924 7 *---------------------------------------------------------------------
frank26080115 0:bf7b9fba3924 8 * Software that is described herein is for illustrative purposes only
frank26080115 0:bf7b9fba3924 9 * which provides customers with programming information regarding the
frank26080115 0:bf7b9fba3924 10 * products. This software is supplied "AS IS" without any warranties.
frank26080115 0:bf7b9fba3924 11 * NXP Semiconductors assumes no responsibility or liability for the
frank26080115 0:bf7b9fba3924 12 * use of the software, conveys no license or title under any patent,
frank26080115 0:bf7b9fba3924 13 * copyright, or mask work right to the product. NXP Semiconductors
frank26080115 0:bf7b9fba3924 14 * reserves the right to make changes in the software without
frank26080115 0:bf7b9fba3924 15 * notification. NXP Semiconductors also make no representation or
frank26080115 0:bf7b9fba3924 16 * warranty that such application will be suitable for the specified
frank26080115 0:bf7b9fba3924 17 * use without further testing or modification.
frank26080115 0:bf7b9fba3924 18 **********************************************************************/
frank26080115 0:bf7b9fba3924 19 #include "lpc17xx_uart.h"
frank26080115 0:bf7b9fba3924 20 #include "lpc17xx_libcfg.h"
frank26080115 0:bf7b9fba3924 21 #include "lpc17xx_pinsel.h"
frank26080115 0:bf7b9fba3924 22
frank26080115 0:bf7b9fba3924 23 /* Example group ----------------------------------------------------------- */
frank26080115 0:bf7b9fba3924 24 /** @defgroup UART_UART1_FullModem UART1_FullModem
frank26080115 0:bf7b9fba3924 25 * @ingroup UART_Examples
frank26080115 0:bf7b9fba3924 26 * @{
frank26080115 0:bf7b9fba3924 27 */
frank26080115 0:bf7b9fba3924 28
frank26080115 0:bf7b9fba3924 29
frank26080115 0:bf7b9fba3924 30 /************************** PRIVATE DEFINITIONS *************************/
frank26080115 0:bf7b9fba3924 31 #define MCB_LPC_1768
frank26080115 0:bf7b9fba3924 32 //#define IAR_LPC_1768
frank26080115 0:bf7b9fba3924 33
frank26080115 0:bf7b9fba3924 34 // buffer size definition
frank26080115 0:bf7b9fba3924 35 #define UART_RING_BUFSIZE 256
frank26080115 0:bf7b9fba3924 36 /* Auto RTS and Auto CTS definition:
frank26080115 0:bf7b9fba3924 37 * - 1: Enable Auto RTS and CTS function
frank26080115 0:bf7b9fba3924 38 * - 0: Disable this function, in this case, handle manually
frank26080115 0:bf7b9fba3924 39 * modem functionality */
frank26080115 0:bf7b9fba3924 40 #define AUTO_RTS_CTS_USE 0
frank26080115 0:bf7b9fba3924 41
frank26080115 0:bf7b9fba3924 42 /* Buf mask */
frank26080115 0:bf7b9fba3924 43 #define __BUF_MASK (UART_RING_BUFSIZE-1)
frank26080115 0:bf7b9fba3924 44 /* Check buf is full or not */
frank26080115 0:bf7b9fba3924 45 #define __BUF_IS_FULL(head, tail) ((tail&__BUF_MASK)==((head+1)&__BUF_MASK))
frank26080115 0:bf7b9fba3924 46 /* Check buf will be full in next receiving or not */
frank26080115 0:bf7b9fba3924 47 #define __BUF_WILL_FULL(head, tail) ((tail&__BUF_MASK)==((head+2)&__BUF_MASK))
frank26080115 0:bf7b9fba3924 48 /* Check buf is empty */
frank26080115 0:bf7b9fba3924 49 #define __BUF_IS_EMPTY(head, tail) ((head&__BUF_MASK)==(tail&__BUF_MASK))
frank26080115 0:bf7b9fba3924 50 /* Reset buf */
frank26080115 0:bf7b9fba3924 51 #define __BUF_RESET(bufidx) (bufidx=0)
frank26080115 0:bf7b9fba3924 52 #define __BUF_INCR(bufidx) (bufidx=(bufidx+1)&__BUF_MASK)
frank26080115 0:bf7b9fba3924 53
frank26080115 0:bf7b9fba3924 54
frank26080115 0:bf7b9fba3924 55 /************************** PRIVATE TYPES *************************/
frank26080115 0:bf7b9fba3924 56 /** @brief UART Ring buffer structure */
frank26080115 0:bf7b9fba3924 57 typedef struct
frank26080115 0:bf7b9fba3924 58 {
frank26080115 0:bf7b9fba3924 59 __IO uint32_t tx_head; /*!< UART Tx ring buffer head index */
frank26080115 0:bf7b9fba3924 60 __IO uint32_t tx_tail; /*!< UART Tx ring buffer tail index */
frank26080115 0:bf7b9fba3924 61 __IO uint32_t rx_head; /*!< UART Rx ring buffer head index */
frank26080115 0:bf7b9fba3924 62 __IO uint32_t rx_tail; /*!< UART Rx ring buffer tail index */
frank26080115 0:bf7b9fba3924 63 __IO uint8_t tx[UART_RING_BUFSIZE]; /*!< UART Tx data ring buffer */
frank26080115 0:bf7b9fba3924 64 __IO uint8_t rx[UART_RING_BUFSIZE]; /*!< UART Rx data ring buffer */
frank26080115 0:bf7b9fba3924 65 } UART_RING_BUFFER_T;
frank26080115 0:bf7b9fba3924 66
frank26080115 0:bf7b9fba3924 67
frank26080115 0:bf7b9fba3924 68 /************************** PRIVATE VARIABLES *************************/
frank26080115 0:bf7b9fba3924 69 uint8_t menu1[] = "Hello NXP Semiconductors \n\r";
frank26080115 0:bf7b9fba3924 70 uint8_t menu2[] =
frank26080115 0:bf7b9fba3924 71 "UART1 Full Modem \n\r\t "
frank26080115 0:bf7b9fba3924 72 "MCU LPC17xx - ARM Cortex-M3 \n\r\t "
frank26080115 0:bf7b9fba3924 73 "UART1 - 9600bps \n\r";
frank26080115 0:bf7b9fba3924 74 uint8_t menu3[] = "UART demo terminated!\n";
frank26080115 0:bf7b9fba3924 75
frank26080115 0:bf7b9fba3924 76 // UART Ring buffer
frank26080115 0:bf7b9fba3924 77 UART_RING_BUFFER_T rb;
frank26080115 0:bf7b9fba3924 78
frank26080115 0:bf7b9fba3924 79 // RTS State
frank26080115 0:bf7b9fba3924 80 __IO int32_t RTS_State;
frank26080115 0:bf7b9fba3924 81
frank26080115 0:bf7b9fba3924 82 // Current Tx Interrupt enable state
frank26080115 0:bf7b9fba3924 83 __IO FlagStatus TxIntStat;
frank26080115 0:bf7b9fba3924 84
frank26080115 0:bf7b9fba3924 85
frank26080115 0:bf7b9fba3924 86 /************************** PRIVATE FUNCTIONS *************************/
frank26080115 0:bf7b9fba3924 87 /* Interrupt service routines */
frank26080115 0:bf7b9fba3924 88 void UART1_IRQHandler(void);
frank26080115 0:bf7b9fba3924 89 void UART1_IntTransmit(void);
frank26080115 0:bf7b9fba3924 90 void UART1_IntReceive(void);
frank26080115 0:bf7b9fba3924 91 void UART1_IntErr(uint8_t bLSErrType);
frank26080115 0:bf7b9fba3924 92
frank26080115 0:bf7b9fba3924 93 uint32_t UARTReceive(LPC_UART_TypeDef *UARTPort, uint8_t *rxbuf, uint8_t buflen);
frank26080115 0:bf7b9fba3924 94 uint32_t UARTSend(LPC_UART_TypeDef *UARTPort, uint8_t *txbuf, uint8_t buflen);
frank26080115 0:bf7b9fba3924 95 void print_menu(void);
frank26080115 0:bf7b9fba3924 96
frank26080115 0:bf7b9fba3924 97
frank26080115 0:bf7b9fba3924 98 /*----------------- INTERRUPT SERVICE ROUTINES --------------------------*/
frank26080115 0:bf7b9fba3924 99 /*********************************************************************//**
frank26080115 0:bf7b9fba3924 100 * @brief UART1 interrupt handler sub-routine
frank26080115 0:bf7b9fba3924 101 * @param[in] None
frank26080115 0:bf7b9fba3924 102 * @return None
frank26080115 0:bf7b9fba3924 103 **********************************************************************/
frank26080115 0:bf7b9fba3924 104 void UART1_IRQHandler(void)
frank26080115 0:bf7b9fba3924 105 {
frank26080115 0:bf7b9fba3924 106 uint8_t modemsts;
frank26080115 0:bf7b9fba3924 107 uint32_t intsrc, tmp, tmp1;
frank26080115 0:bf7b9fba3924 108
frank26080115 0:bf7b9fba3924 109 /* Determine the interrupt source */
frank26080115 0:bf7b9fba3924 110 intsrc = UART_GetIntId((LPC_UART_TypeDef *)LPC_UART1);
frank26080115 0:bf7b9fba3924 111 tmp = intsrc & UART_IIR_INTID_MASK;
frank26080115 0:bf7b9fba3924 112
frank26080115 0:bf7b9fba3924 113 /*
frank26080115 0:bf7b9fba3924 114 * In case of using UART1 with full modem,
frank26080115 0:bf7b9fba3924 115 * interrupt ID = 0 that means modem status interrupt has been detected
frank26080115 0:bf7b9fba3924 116 */
frank26080115 0:bf7b9fba3924 117
frank26080115 0:bf7b9fba3924 118 if (tmp == 0){
frank26080115 0:bf7b9fba3924 119 // Check Modem status
frank26080115 0:bf7b9fba3924 120 modemsts = UART_FullModemGetStatus(LPC_UART1);
frank26080115 0:bf7b9fba3924 121 #if (AUTO_RTS_CTS_USE == 0)
frank26080115 0:bf7b9fba3924 122 // Check CTS status change flag
frank26080115 0:bf7b9fba3924 123 if (modemsts & UART1_MODEM_STAT_DELTA_CTS) {
frank26080115 0:bf7b9fba3924 124 // if CTS status is active, continue to send data
frank26080115 0:bf7b9fba3924 125 if (modemsts & UART1_MODEM_STAT_CTS) {
frank26080115 0:bf7b9fba3924 126 // Re-Enable Tx
frank26080115 0:bf7b9fba3924 127 UART_TxCmd((LPC_UART_TypeDef *)LPC_UART1, ENABLE);
frank26080115 0:bf7b9fba3924 128 }
frank26080115 0:bf7b9fba3924 129 // Otherwise, Stop current transmission immediately
frank26080115 0:bf7b9fba3924 130 else{
frank26080115 0:bf7b9fba3924 131 // Disable Tx
frank26080115 0:bf7b9fba3924 132 UART_TxCmd((LPC_UART_TypeDef *)LPC_UART1, DISABLE);
frank26080115 0:bf7b9fba3924 133 }
frank26080115 0:bf7b9fba3924 134 }
frank26080115 0:bf7b9fba3924 135 #endif
frank26080115 0:bf7b9fba3924 136 }
frank26080115 0:bf7b9fba3924 137
frank26080115 0:bf7b9fba3924 138 // Receive Line Status
frank26080115 0:bf7b9fba3924 139 if (tmp == UART_IIR_INTID_RLS){
frank26080115 0:bf7b9fba3924 140 // Check line status
frank26080115 0:bf7b9fba3924 141 tmp1 = UART_GetLineStatus((LPC_UART_TypeDef *)LPC_UART1);
frank26080115 0:bf7b9fba3924 142 // Mask out the Receive Ready and Transmit Holding empty status
frank26080115 0:bf7b9fba3924 143 tmp1 &= (UART_LSR_OE | UART_LSR_PE | UART_LSR_FE \
frank26080115 0:bf7b9fba3924 144 | UART_LSR_BI | UART_LSR_RXFE);
frank26080115 0:bf7b9fba3924 145 // If any error exist
frank26080115 0:bf7b9fba3924 146 if (tmp1) {
frank26080115 0:bf7b9fba3924 147 UART1_IntErr(tmp1);
frank26080115 0:bf7b9fba3924 148 }
frank26080115 0:bf7b9fba3924 149 }
frank26080115 0:bf7b9fba3924 150
frank26080115 0:bf7b9fba3924 151 // Receive Data Available or Character time-out
frank26080115 0:bf7b9fba3924 152 if ((tmp == UART_IIR_INTID_RDA) || (tmp == UART_IIR_INTID_CTI)){
frank26080115 0:bf7b9fba3924 153 UART1_IntReceive();
frank26080115 0:bf7b9fba3924 154 }
frank26080115 0:bf7b9fba3924 155
frank26080115 0:bf7b9fba3924 156 // Transmit Holding Empty
frank26080115 0:bf7b9fba3924 157 if (tmp == UART_IIR_INTID_THRE){
frank26080115 0:bf7b9fba3924 158 UART1_IntTransmit();
frank26080115 0:bf7b9fba3924 159 }
frank26080115 0:bf7b9fba3924 160 }
frank26080115 0:bf7b9fba3924 161
frank26080115 0:bf7b9fba3924 162 /********************************************************************//**
frank26080115 0:bf7b9fba3924 163 * @brief UART1 receive function (ring buffer used)
frank26080115 0:bf7b9fba3924 164 * @param[in] None
frank26080115 0:bf7b9fba3924 165 * @return None
frank26080115 0:bf7b9fba3924 166 *********************************************************************/
frank26080115 0:bf7b9fba3924 167 void UART1_IntReceive(void)
frank26080115 0:bf7b9fba3924 168 {
frank26080115 0:bf7b9fba3924 169 uint8_t tmpc;
frank26080115 0:bf7b9fba3924 170 uint32_t rLen;
frank26080115 0:bf7b9fba3924 171
frank26080115 0:bf7b9fba3924 172 while (1){
frank26080115 0:bf7b9fba3924 173 // Call UART read function in UART driver
frank26080115 0:bf7b9fba3924 174 rLen = UART_Receive((LPC_UART_TypeDef *)LPC_UART1, &tmpc, 1, NONE_BLOCKING);
frank26080115 0:bf7b9fba3924 175 // If data received
frank26080115 0:bf7b9fba3924 176 if (rLen){
frank26080115 0:bf7b9fba3924 177
frank26080115 0:bf7b9fba3924 178 /* If buffer will be full and RTS is driven manually,
frank26080115 0:bf7b9fba3924 179 * RTS pin should be forced into INACTIVE state
frank26080115 0:bf7b9fba3924 180 */
frank26080115 0:bf7b9fba3924 181 #if (AUTO_RTS_CTS_USE == 0)
frank26080115 0:bf7b9fba3924 182 if (__BUF_WILL_FULL(rb.rx_head, rb.rx_tail))
frank26080115 0:bf7b9fba3924 183 {
frank26080115 0:bf7b9fba3924 184 if (RTS_State == ACTIVE)
frank26080115 0:bf7b9fba3924 185 {
frank26080115 0:bf7b9fba3924 186 // Disable request to send through RTS line
frank26080115 0:bf7b9fba3924 187 UART_FullModemForcePinState(LPC_UART1, UART1_MODEM_PIN_RTS, \
frank26080115 0:bf7b9fba3924 188 INACTIVE);
frank26080115 0:bf7b9fba3924 189 RTS_State = INACTIVE;
frank26080115 0:bf7b9fba3924 190 }
frank26080115 0:bf7b9fba3924 191 }
frank26080115 0:bf7b9fba3924 192 #endif
frank26080115 0:bf7b9fba3924 193
frank26080115 0:bf7b9fba3924 194 /* Check if buffer is more space
frank26080115 0:bf7b9fba3924 195 * If no more space, remaining character will be trimmed out
frank26080115 0:bf7b9fba3924 196 */
frank26080115 0:bf7b9fba3924 197 if (!__BUF_IS_FULL(rb.rx_head,rb.rx_tail)){
frank26080115 0:bf7b9fba3924 198 rb.rx[rb.rx_head] = tmpc;
frank26080115 0:bf7b9fba3924 199 __BUF_INCR(rb.rx_head);
frank26080115 0:bf7b9fba3924 200 }
frank26080115 0:bf7b9fba3924 201 }
frank26080115 0:bf7b9fba3924 202 // no more data
frank26080115 0:bf7b9fba3924 203 else {
frank26080115 0:bf7b9fba3924 204 break;
frank26080115 0:bf7b9fba3924 205 }
frank26080115 0:bf7b9fba3924 206 }
frank26080115 0:bf7b9fba3924 207 }
frank26080115 0:bf7b9fba3924 208
frank26080115 0:bf7b9fba3924 209
frank26080115 0:bf7b9fba3924 210 /********************************************************************//**
frank26080115 0:bf7b9fba3924 211 * @brief UART1 transmit function (ring buffer used)
frank26080115 0:bf7b9fba3924 212 * @param[in] None
frank26080115 0:bf7b9fba3924 213 * @return None
frank26080115 0:bf7b9fba3924 214 *********************************************************************/
frank26080115 0:bf7b9fba3924 215 void UART1_IntTransmit(void)
frank26080115 0:bf7b9fba3924 216 {
frank26080115 0:bf7b9fba3924 217 // Disable THRE interrupt
frank26080115 0:bf7b9fba3924 218 UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_THRE, DISABLE);
frank26080115 0:bf7b9fba3924 219
frank26080115 0:bf7b9fba3924 220 /* Wait for FIFO buffer empty, transfer UART_TX_FIFO_SIZE bytes
frank26080115 0:bf7b9fba3924 221 * of data or break whenever ring buffers are empty */
frank26080115 0:bf7b9fba3924 222 /* Wait until THR empty */
frank26080115 0:bf7b9fba3924 223 while (UART_CheckBusy((LPC_UART_TypeDef *)LPC_UART1) == SET);
frank26080115 0:bf7b9fba3924 224
frank26080115 0:bf7b9fba3924 225 while (!__BUF_IS_EMPTY(rb.tx_head,rb.tx_tail))
frank26080115 0:bf7b9fba3924 226 {
frank26080115 0:bf7b9fba3924 227 /* Move a piece of data into the transmit FIFO */
frank26080115 0:bf7b9fba3924 228 if (UART_Send((LPC_UART_TypeDef *)LPC_UART1, (uint8_t *)&rb.tx[rb.tx_tail], \
frank26080115 0:bf7b9fba3924 229 1, NONE_BLOCKING)){
frank26080115 0:bf7b9fba3924 230 /* Update transmit ring FIFO tail pointer */
frank26080115 0:bf7b9fba3924 231 __BUF_INCR(rb.tx_tail);
frank26080115 0:bf7b9fba3924 232 } else {
frank26080115 0:bf7b9fba3924 233 break;
frank26080115 0:bf7b9fba3924 234 }
frank26080115 0:bf7b9fba3924 235 }
frank26080115 0:bf7b9fba3924 236
frank26080115 0:bf7b9fba3924 237 /* If there is no more data to send, disable the transmit
frank26080115 0:bf7b9fba3924 238 interrupt - else enable it or keep it enabled */
frank26080115 0:bf7b9fba3924 239 if (__BUF_IS_EMPTY(rb.tx_head, rb.tx_tail)) {
frank26080115 0:bf7b9fba3924 240 UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_THRE, DISABLE);
frank26080115 0:bf7b9fba3924 241 // Reset Tx Interrupt state
frank26080115 0:bf7b9fba3924 242 TxIntStat = RESET;
frank26080115 0:bf7b9fba3924 243 }
frank26080115 0:bf7b9fba3924 244 else{
frank26080115 0:bf7b9fba3924 245 // Set Tx Interrupt state
frank26080115 0:bf7b9fba3924 246 TxIntStat = SET;
frank26080115 0:bf7b9fba3924 247 UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_THRE, ENABLE);
frank26080115 0:bf7b9fba3924 248 }
frank26080115 0:bf7b9fba3924 249 }
frank26080115 0:bf7b9fba3924 250
frank26080115 0:bf7b9fba3924 251
frank26080115 0:bf7b9fba3924 252 /*********************************************************************//**
frank26080115 0:bf7b9fba3924 253 * @brief UART Line Status Error
frank26080115 0:bf7b9fba3924 254 * @param[in] bLSErrType UART Line Status Error Type
frank26080115 0:bf7b9fba3924 255 * @return None
frank26080115 0:bf7b9fba3924 256 **********************************************************************/
frank26080115 0:bf7b9fba3924 257 void UART1_IntErr(uint8_t bLSErrType)
frank26080115 0:bf7b9fba3924 258 {
frank26080115 0:bf7b9fba3924 259 uint8_t test;
frank26080115 0:bf7b9fba3924 260 // Loop forever
frank26080115 0:bf7b9fba3924 261 while (1){
frank26080115 0:bf7b9fba3924 262 // For testing purpose
frank26080115 0:bf7b9fba3924 263 test = bLSErrType;
frank26080115 0:bf7b9fba3924 264 }
frank26080115 0:bf7b9fba3924 265 }
frank26080115 0:bf7b9fba3924 266
frank26080115 0:bf7b9fba3924 267 /*-------------------------PRIVATE FUNCTIONS------------------------------*/
frank26080115 0:bf7b9fba3924 268 /*********************************************************************//**
frank26080115 0:bf7b9fba3924 269 * @brief UART transmit function for interrupt mode (using ring buffers)
frank26080115 0:bf7b9fba3924 270 * @param[in] UARTPort Selected UART peripheral used to send data,
frank26080115 0:bf7b9fba3924 271 * should be UART1.
frank26080115 0:bf7b9fba3924 272 * @param[out] txbuf Pointer to Transmit buffer
frank26080115 0:bf7b9fba3924 273 * @param[in] buflen Length of Transmit buffer
frank26080115 0:bf7b9fba3924 274 * @return Number of bytes actually sent to the ring buffer
frank26080115 0:bf7b9fba3924 275 **********************************************************************/
frank26080115 0:bf7b9fba3924 276 uint32_t UARTSend(LPC_UART_TypeDef *UARTPort, uint8_t *txbuf, uint8_t buflen)
frank26080115 0:bf7b9fba3924 277 {
frank26080115 0:bf7b9fba3924 278 uint8_t *data = (uint8_t *) txbuf;
frank26080115 0:bf7b9fba3924 279 uint32_t bytes = 0;
frank26080115 0:bf7b9fba3924 280
frank26080115 0:bf7b9fba3924 281
frank26080115 0:bf7b9fba3924 282 /* Temporarily lock out UART transmit interrupts during this
frank26080115 0:bf7b9fba3924 283 read so the UART transmit interrupt won't cause problems
frank26080115 0:bf7b9fba3924 284 with the index values */
frank26080115 0:bf7b9fba3924 285 UART_IntConfig(UARTPort, UART_INTCFG_THRE, DISABLE);
frank26080115 0:bf7b9fba3924 286
frank26080115 0:bf7b9fba3924 287 /* Loop until transmit run buffer is full or until n_bytes
frank26080115 0:bf7b9fba3924 288 expires */
frank26080115 0:bf7b9fba3924 289 while ((buflen > 0) && (!__BUF_IS_FULL(rb.tx_head, rb.tx_tail)))
frank26080115 0:bf7b9fba3924 290 {
frank26080115 0:bf7b9fba3924 291 /* Write data from buffer into ring buffer */
frank26080115 0:bf7b9fba3924 292 rb.tx[rb.tx_head] = *data;
frank26080115 0:bf7b9fba3924 293 data++;
frank26080115 0:bf7b9fba3924 294
frank26080115 0:bf7b9fba3924 295 /* Increment head pointer */
frank26080115 0:bf7b9fba3924 296 __BUF_INCR(rb.tx_head);
frank26080115 0:bf7b9fba3924 297
frank26080115 0:bf7b9fba3924 298 /* Increment data count and decrement buffer size count */
frank26080115 0:bf7b9fba3924 299 bytes++;
frank26080115 0:bf7b9fba3924 300 buflen--;
frank26080115 0:bf7b9fba3924 301 }
frank26080115 0:bf7b9fba3924 302
frank26080115 0:bf7b9fba3924 303 /*
frank26080115 0:bf7b9fba3924 304 * Check if current Tx interrupt enable is reset,
frank26080115 0:bf7b9fba3924 305 * that means the Tx interrupt must be re-enabled
frank26080115 0:bf7b9fba3924 306 * due to call UART_IntTransmit() function to trigger
frank26080115 0:bf7b9fba3924 307 * this interrupt type
frank26080115 0:bf7b9fba3924 308 */
frank26080115 0:bf7b9fba3924 309 if (TxIntStat == RESET) {
frank26080115 0:bf7b9fba3924 310 UART1_IntTransmit();
frank26080115 0:bf7b9fba3924 311 }
frank26080115 0:bf7b9fba3924 312 /*
frank26080115 0:bf7b9fba3924 313 * Otherwise, re-enables Tx Interrupt
frank26080115 0:bf7b9fba3924 314 */
frank26080115 0:bf7b9fba3924 315 else {
frank26080115 0:bf7b9fba3924 316 UART_IntConfig(UARTPort, UART_INTCFG_THRE, ENABLE);
frank26080115 0:bf7b9fba3924 317 }
frank26080115 0:bf7b9fba3924 318
frank26080115 0:bf7b9fba3924 319 return bytes;
frank26080115 0:bf7b9fba3924 320 }
frank26080115 0:bf7b9fba3924 321
frank26080115 0:bf7b9fba3924 322 /*********************************************************************//**
frank26080115 0:bf7b9fba3924 323 * @brief UART read function for interrupt mode (using ring buffers)
frank26080115 0:bf7b9fba3924 324 * @param[in] UARTPort Selected UART peripheral used to send data,
frank26080115 0:bf7b9fba3924 325 * should be UART1.
frank26080115 0:bf7b9fba3924 326 * @param[out] rxbuf Pointer to Received buffer
frank26080115 0:bf7b9fba3924 327 * @param[in] buflen Length of Received buffer
frank26080115 0:bf7b9fba3924 328 * @return Number of bytes actually read from the ring buffer
frank26080115 0:bf7b9fba3924 329 **********************************************************************/
frank26080115 0:bf7b9fba3924 330 uint32_t UARTReceive(LPC_UART_TypeDef *UARTPort, uint8_t *rxbuf, uint8_t buflen)
frank26080115 0:bf7b9fba3924 331 {
frank26080115 0:bf7b9fba3924 332 uint8_t *data = (uint8_t *) rxbuf;
frank26080115 0:bf7b9fba3924 333 uint32_t bytes = 0;
frank26080115 0:bf7b9fba3924 334
frank26080115 0:bf7b9fba3924 335 /* Temporarily lock out UART receive interrupts during this
frank26080115 0:bf7b9fba3924 336 read so the UART receive interrupt won't cause problems
frank26080115 0:bf7b9fba3924 337 with the index values */
frank26080115 0:bf7b9fba3924 338 UART_IntConfig(UARTPort, UART_INTCFG_RBR, DISABLE);
frank26080115 0:bf7b9fba3924 339
frank26080115 0:bf7b9fba3924 340 /* Loop until receive buffer ring is empty or
frank26080115 0:bf7b9fba3924 341 until max_bytes expires */
frank26080115 0:bf7b9fba3924 342 while ((buflen > 0) && (!(__BUF_IS_EMPTY(rb.rx_head, rb.rx_tail))))
frank26080115 0:bf7b9fba3924 343 {
frank26080115 0:bf7b9fba3924 344 /* Read data from ring buffer into user buffer */
frank26080115 0:bf7b9fba3924 345 *data = rb.rx[rb.rx_tail];
frank26080115 0:bf7b9fba3924 346 data++;
frank26080115 0:bf7b9fba3924 347
frank26080115 0:bf7b9fba3924 348 /* Update tail pointer */
frank26080115 0:bf7b9fba3924 349 __BUF_INCR(rb.rx_tail);
frank26080115 0:bf7b9fba3924 350
frank26080115 0:bf7b9fba3924 351 /* Increment data count and decrement buffer size count */
frank26080115 0:bf7b9fba3924 352 bytes++;
frank26080115 0:bf7b9fba3924 353 buflen--;
frank26080115 0:bf7b9fba3924 354
frank26080115 0:bf7b9fba3924 355 #if (AUTO_RTS_CTS_USE == 0)
frank26080115 0:bf7b9fba3924 356 /* In case of driving RTS manually, this pin should be
frank26080115 0:bf7b9fba3924 357 * release into ACTIVE state if buffer is free
frank26080115 0:bf7b9fba3924 358 */
frank26080115 0:bf7b9fba3924 359 if (RTS_State == INACTIVE)
frank26080115 0:bf7b9fba3924 360 {
frank26080115 0:bf7b9fba3924 361 if (!__BUF_WILL_FULL(rb.rx_head, rb.rx_tail))
frank26080115 0:bf7b9fba3924 362 {
frank26080115 0:bf7b9fba3924 363 // Disable request to send through RTS line
frank26080115 0:bf7b9fba3924 364 UART_FullModemForcePinState(LPC_UART1, UART1_MODEM_PIN_RTS, \
frank26080115 0:bf7b9fba3924 365 ACTIVE);
frank26080115 0:bf7b9fba3924 366 RTS_State = ACTIVE;
frank26080115 0:bf7b9fba3924 367 }
frank26080115 0:bf7b9fba3924 368 }
frank26080115 0:bf7b9fba3924 369 #endif
frank26080115 0:bf7b9fba3924 370 }
frank26080115 0:bf7b9fba3924 371
frank26080115 0:bf7b9fba3924 372 /* Re-enable UART interrupts */
frank26080115 0:bf7b9fba3924 373 UART_IntConfig(UARTPort, UART_INTCFG_RBR, ENABLE);
frank26080115 0:bf7b9fba3924 374
frank26080115 0:bf7b9fba3924 375 return bytes;
frank26080115 0:bf7b9fba3924 376 }
frank26080115 0:bf7b9fba3924 377
frank26080115 0:bf7b9fba3924 378 /*********************************************************************//**
frank26080115 0:bf7b9fba3924 379 * @brief Print Welcome Screen Menu subroutine
frank26080115 0:bf7b9fba3924 380 * @param None
frank26080115 0:bf7b9fba3924 381 * @return None
frank26080115 0:bf7b9fba3924 382 **********************************************************************/
frank26080115 0:bf7b9fba3924 383 void print_menu(void)
frank26080115 0:bf7b9fba3924 384 {
frank26080115 0:bf7b9fba3924 385 uint32_t tmp, tmp2;
frank26080115 0:bf7b9fba3924 386 uint8_t *pDat;
frank26080115 0:bf7b9fba3924 387
frank26080115 0:bf7b9fba3924 388 tmp = sizeof(menu1);
frank26080115 0:bf7b9fba3924 389 tmp2 = 0;
frank26080115 0:bf7b9fba3924 390 pDat = (uint8_t *)&menu1[0];
frank26080115 0:bf7b9fba3924 391 while(tmp) {
frank26080115 0:bf7b9fba3924 392 tmp2 = UARTSend((LPC_UART_TypeDef *)LPC_UART1, pDat, tmp);
frank26080115 0:bf7b9fba3924 393 pDat += tmp2;
frank26080115 0:bf7b9fba3924 394 tmp -= tmp2;
frank26080115 0:bf7b9fba3924 395 }
frank26080115 0:bf7b9fba3924 396
frank26080115 0:bf7b9fba3924 397 tmp = sizeof(menu2);
frank26080115 0:bf7b9fba3924 398 tmp2 = 0;
frank26080115 0:bf7b9fba3924 399 pDat = (uint8_t *)&menu2[0];
frank26080115 0:bf7b9fba3924 400 while(tmp) {
frank26080115 0:bf7b9fba3924 401 tmp2 = UARTSend((LPC_UART_TypeDef *)LPC_UART1, pDat, tmp);
frank26080115 0:bf7b9fba3924 402 pDat += tmp2;
frank26080115 0:bf7b9fba3924 403 tmp -= tmp2;
frank26080115 0:bf7b9fba3924 404 }
frank26080115 0:bf7b9fba3924 405 }
frank26080115 0:bf7b9fba3924 406
frank26080115 0:bf7b9fba3924 407 /*-------------------------MAIN FUNCTION------------------------------*/
frank26080115 0:bf7b9fba3924 408 /*********************************************************************//**
frank26080115 0:bf7b9fba3924 409 * @brief c_entry: Main UART-FULLMODEM program body
frank26080115 0:bf7b9fba3924 410 * @param[in] None
frank26080115 0:bf7b9fba3924 411 * @return int
frank26080115 0:bf7b9fba3924 412 **********************************************************************/
frank26080115 0:bf7b9fba3924 413 int c_entry(void)
frank26080115 0:bf7b9fba3924 414 {
frank26080115 0:bf7b9fba3924 415 // UART Configuration structure variable
frank26080115 0:bf7b9fba3924 416 UART_CFG_Type UARTConfigStruct;
frank26080115 0:bf7b9fba3924 417 // UART FIFO configuration Struct variable
frank26080115 0:bf7b9fba3924 418 UART_FIFO_CFG_Type UARTFIFOConfigStruct;
frank26080115 0:bf7b9fba3924 419 // Pin configuration for UART1
frank26080115 0:bf7b9fba3924 420 PINSEL_CFG_Type PinCfg;
frank26080115 0:bf7b9fba3924 421 uint32_t idx, len;
frank26080115 0:bf7b9fba3924 422 __IO FlagStatus exitflag;
frank26080115 0:bf7b9fba3924 423 uint8_t buffer[10];
frank26080115 0:bf7b9fba3924 424
frank26080115 0:bf7b9fba3924 425 /*
frank26080115 0:bf7b9fba3924 426 * Initialize UART1 pin connect
frank26080115 0:bf7b9fba3924 427 * If using MCB1700 eval board, assign pin P2.0 - P2.7
frank26080115 0:bf7b9fba3924 428 * If using IAR 1768 KS board, assign pin P0.7 - P0.15
frank26080115 0:bf7b9fba3924 429 */
frank26080115 0:bf7b9fba3924 430 #ifdef MCB_LPC_1768
frank26080115 0:bf7b9fba3924 431 PinCfg.Funcnum = 2;
frank26080115 0:bf7b9fba3924 432 PinCfg.OpenDrain = 0;
frank26080115 0:bf7b9fba3924 433 PinCfg.Pinmode = 0;
frank26080115 0:bf7b9fba3924 434 PinCfg.Portnum = 2;
frank26080115 0:bf7b9fba3924 435 for (idx = 0; idx <= 7; idx++){
frank26080115 0:bf7b9fba3924 436 PinCfg.Pinnum = idx;
frank26080115 0:bf7b9fba3924 437 PINSEL_ConfigPin(&PinCfg);
frank26080115 0:bf7b9fba3924 438 }
frank26080115 0:bf7b9fba3924 439 #elif defined(IAR_LPC_1768)
frank26080115 0:bf7b9fba3924 440 PinCfg.Funcnum = 1;
frank26080115 0:bf7b9fba3924 441 PinCfg.OpenDrain = 0;
frank26080115 0:bf7b9fba3924 442 PinCfg.Pinmode = 0;
frank26080115 0:bf7b9fba3924 443 PinCfg.Portnum = 0;
frank26080115 0:bf7b9fba3924 444 for (idx = 15; idx <= 22; idx++){
frank26080115 0:bf7b9fba3924 445 PinCfg.Pinnum = idx;
frank26080115 0:bf7b9fba3924 446 PINSEL_ConfigPin(&PinCfg);
frank26080115 0:bf7b9fba3924 447 }
frank26080115 0:bf7b9fba3924 448 #endif
frank26080115 0:bf7b9fba3924 449
frank26080115 0:bf7b9fba3924 450 /* Initialize UART Configuration parameter structure to default state:
frank26080115 0:bf7b9fba3924 451 * Baudrate = 9600bps
frank26080115 0:bf7b9fba3924 452 * 8 data bit
frank26080115 0:bf7b9fba3924 453 * 1 Stop bit
frank26080115 0:bf7b9fba3924 454 * None parity
frank26080115 0:bf7b9fba3924 455 */
frank26080115 0:bf7b9fba3924 456 UART_ConfigStructInit(&UARTConfigStruct);
frank26080115 0:bf7b9fba3924 457
frank26080115 0:bf7b9fba3924 458 // Initialize UART1 peripheral with given to corresponding parameter
frank26080115 0:bf7b9fba3924 459 UART_Init((LPC_UART_TypeDef *)LPC_UART1, &UARTConfigStruct);
frank26080115 0:bf7b9fba3924 460
frank26080115 0:bf7b9fba3924 461 /* Initialize FIFOConfigStruct to default state:
frank26080115 0:bf7b9fba3924 462 * - FIFO_DMAMode = DISABLE
frank26080115 0:bf7b9fba3924 463 * - FIFO_Level = UART_FIFO_TRGLEV0
frank26080115 0:bf7b9fba3924 464 * - FIFO_ResetRxBuf = ENABLE
frank26080115 0:bf7b9fba3924 465 * - FIFO_ResetTxBuf = ENABLE
frank26080115 0:bf7b9fba3924 466 * - FIFO_State = ENABLE
frank26080115 0:bf7b9fba3924 467 */
frank26080115 0:bf7b9fba3924 468 UART_FIFOConfigStructInit(&UARTFIFOConfigStruct);
frank26080115 0:bf7b9fba3924 469
frank26080115 0:bf7b9fba3924 470 // Initialize FIFO for UART1 peripheral
frank26080115 0:bf7b9fba3924 471 UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART1, &UARTFIFOConfigStruct);
frank26080115 0:bf7b9fba3924 472
frank26080115 0:bf7b9fba3924 473 #if (AUTO_RTS_CTS_USE==0)
frank26080115 0:bf7b9fba3924 474 /*
frank26080115 0:bf7b9fba3924 475 * Determine current state of CTS pin to enable Tx
frank26080115 0:bf7b9fba3924 476 * activity
frank26080115 0:bf7b9fba3924 477 */
frank26080115 0:bf7b9fba3924 478 if (UART_FullModemGetStatus(LPC_UART1) & UART1_MODEM_STAT_CTS) {
frank26080115 0:bf7b9fba3924 479 // Enable UART Transmit
frank26080115 0:bf7b9fba3924 480 UART_TxCmd((LPC_UART_TypeDef *)LPC_UART1, ENABLE);
frank26080115 0:bf7b9fba3924 481 }
frank26080115 0:bf7b9fba3924 482 #else
frank26080115 0:bf7b9fba3924 483 // Enable UART Transmit
frank26080115 0:bf7b9fba3924 484 UART_TxCmd((LPC_UART_TypeDef *)LPC_UART1, ENABLE);
frank26080115 0:bf7b9fba3924 485 #endif
frank26080115 0:bf7b9fba3924 486
frank26080115 0:bf7b9fba3924 487 // Reset ring buf head and tail idx
frank26080115 0:bf7b9fba3924 488 __BUF_RESET(rb.rx_head);
frank26080115 0:bf7b9fba3924 489 __BUF_RESET(rb.rx_tail);
frank26080115 0:bf7b9fba3924 490 __BUF_RESET(rb.tx_head);
frank26080115 0:bf7b9fba3924 491 __BUF_RESET(rb.tx_tail);
frank26080115 0:bf7b9fba3924 492
frank26080115 0:bf7b9fba3924 493 #if AUTO_RTS_CTS_USE
frank26080115 0:bf7b9fba3924 494 UART_FullModemConfigMode(LPC_UART1, UART1_MODEM_MODE_AUTO_RTS, ENABLE);
frank26080115 0:bf7b9fba3924 495 UART_FullModemConfigMode(LPC_UART1, UART1_MODEM_MODE_AUTO_CTS, ENABLE);
frank26080115 0:bf7b9fba3924 496 #else
frank26080115 0:bf7b9fba3924 497 // Enable Modem status interrupt
frank26080115 0:bf7b9fba3924 498 UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART1_INTCFG_MS, ENABLE);
frank26080115 0:bf7b9fba3924 499 // Enable CTS1 signal transition interrupt
frank26080115 0:bf7b9fba3924 500 UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART1_INTCFG_CTS, ENABLE);
frank26080115 0:bf7b9fba3924 501 // Force RTS pin state to ACTIVE
frank26080115 0:bf7b9fba3924 502 UART_FullModemForcePinState(LPC_UART1, UART1_MODEM_PIN_RTS, ACTIVE);
frank26080115 0:bf7b9fba3924 503 //RESET RTS State flag
frank26080115 0:bf7b9fba3924 504 RTS_State = ACTIVE;
frank26080115 0:bf7b9fba3924 505 #endif
frank26080115 0:bf7b9fba3924 506
frank26080115 0:bf7b9fba3924 507
frank26080115 0:bf7b9fba3924 508 /* Enable UART Rx interrupt */
frank26080115 0:bf7b9fba3924 509 UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_RBR, ENABLE);
frank26080115 0:bf7b9fba3924 510 /* Enable UART line status interrupt */
frank26080115 0:bf7b9fba3924 511 UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_RLS, ENABLE);
frank26080115 0:bf7b9fba3924 512
frank26080115 0:bf7b9fba3924 513 /*
frank26080115 0:bf7b9fba3924 514 * Do not enable transmit interrupt here, since it is handled by
frank26080115 0:bf7b9fba3924 515 * UART_Send() function, just to reset Tx Interrupt state for the
frank26080115 0:bf7b9fba3924 516 * first time
frank26080115 0:bf7b9fba3924 517 */
frank26080115 0:bf7b9fba3924 518 TxIntStat = RESET;
frank26080115 0:bf7b9fba3924 519
frank26080115 0:bf7b9fba3924 520 /* preemption = 1, sub-priority = 1 */
frank26080115 0:bf7b9fba3924 521 NVIC_SetPriority(UART1_IRQn, ((0x01<<3)|0x01));
frank26080115 0:bf7b9fba3924 522 /* Enable Interrupt for UART1 channel */
frank26080115 0:bf7b9fba3924 523 NVIC_EnableIRQ(UART1_IRQn);
frank26080115 0:bf7b9fba3924 524
frank26080115 0:bf7b9fba3924 525 // print welcome screen
frank26080115 0:bf7b9fba3924 526 print_menu();
frank26080115 0:bf7b9fba3924 527
frank26080115 0:bf7b9fba3924 528 // reset exit flag
frank26080115 0:bf7b9fba3924 529 exitflag = RESET;
frank26080115 0:bf7b9fba3924 530
frank26080115 0:bf7b9fba3924 531 /* Read some data from the buffer */
frank26080115 0:bf7b9fba3924 532 while (exitflag == RESET)
frank26080115 0:bf7b9fba3924 533 {
frank26080115 0:bf7b9fba3924 534 len = 0;
frank26080115 0:bf7b9fba3924 535 while (len == 0)
frank26080115 0:bf7b9fba3924 536 {
frank26080115 0:bf7b9fba3924 537 len = UARTReceive((LPC_UART_TypeDef *)LPC_UART1, buffer, sizeof(buffer));
frank26080115 0:bf7b9fba3924 538 }
frank26080115 0:bf7b9fba3924 539
frank26080115 0:bf7b9fba3924 540 /* Got some data */
frank26080115 0:bf7b9fba3924 541 idx = 0;
frank26080115 0:bf7b9fba3924 542 while (idx < len)
frank26080115 0:bf7b9fba3924 543 {
frank26080115 0:bf7b9fba3924 544 if (buffer[idx] == 27)
frank26080115 0:bf7b9fba3924 545 {
frank26080115 0:bf7b9fba3924 546 /* ESC key, set exit flag */
frank26080115 0:bf7b9fba3924 547 UARTSend((LPC_UART_TypeDef *)LPC_UART1, menu3, sizeof(menu3));
frank26080115 0:bf7b9fba3924 548 exitflag = SET;
frank26080115 0:bf7b9fba3924 549 }
frank26080115 0:bf7b9fba3924 550 else if (buffer[idx] == 'r')
frank26080115 0:bf7b9fba3924 551 {
frank26080115 0:bf7b9fba3924 552 print_menu();
frank26080115 0:bf7b9fba3924 553 }
frank26080115 0:bf7b9fba3924 554 else
frank26080115 0:bf7b9fba3924 555 {
frank26080115 0:bf7b9fba3924 556 /* Echo it back */
frank26080115 0:bf7b9fba3924 557 UARTSend((LPC_UART_TypeDef *)LPC_UART1, &buffer[idx], 1);
frank26080115 0:bf7b9fba3924 558 }
frank26080115 0:bf7b9fba3924 559 idx++;
frank26080115 0:bf7b9fba3924 560 }
frank26080115 0:bf7b9fba3924 561 }
frank26080115 0:bf7b9fba3924 562
frank26080115 0:bf7b9fba3924 563 // wait for current transmission complete - THR must be empty
frank26080115 0:bf7b9fba3924 564 while (UART_CheckBusy((LPC_UART_TypeDef *)LPC_UART1) == SET);
frank26080115 0:bf7b9fba3924 565
frank26080115 0:bf7b9fba3924 566 // DeInitialize UART1 peripheral
frank26080115 0:bf7b9fba3924 567 UART_DeInit((LPC_UART_TypeDef *)LPC_UART1);
frank26080115 0:bf7b9fba3924 568
frank26080115 0:bf7b9fba3924 569 /* Loop forever */
frank26080115 0:bf7b9fba3924 570 while(1);
frank26080115 0:bf7b9fba3924 571 return 1;
frank26080115 0:bf7b9fba3924 572 }
frank26080115 0:bf7b9fba3924 573
frank26080115 0:bf7b9fba3924 574 /* With ARM and GHS toolsets, the entry point is main() - this will
frank26080115 0:bf7b9fba3924 575 allow the linker to generate wrapper code to setup stacks, allocate
frank26080115 0:bf7b9fba3924 576 heap area, and initialize and copy code and data segments. For GNU
frank26080115 0:bf7b9fba3924 577 toolsets, the entry point is through __start() in the crt0_gnu.asm
frank26080115 0:bf7b9fba3924 578 file, and that startup code will setup stacks and data */
frank26080115 0:bf7b9fba3924 579 int main(void)
frank26080115 0:bf7b9fba3924 580 {
frank26080115 0:bf7b9fba3924 581 return c_entry();
frank26080115 0:bf7b9fba3924 582 }
frank26080115 0:bf7b9fba3924 583
frank26080115 0:bf7b9fba3924 584
frank26080115 0:bf7b9fba3924 585 #ifdef DEBUG
frank26080115 0:bf7b9fba3924 586 /*******************************************************************************
frank26080115 0:bf7b9fba3924 587 * @brief Reports the name of the source file and the source line number
frank26080115 0:bf7b9fba3924 588 * where the CHECK_PARAM error has occurred.
frank26080115 0:bf7b9fba3924 589 * @param[in] file Pointer to the source file name
frank26080115 0:bf7b9fba3924 590 * @param[in] line assert_param error line source number
frank26080115 0:bf7b9fba3924 591 * @return None
frank26080115 0:bf7b9fba3924 592 *******************************************************************************/
frank26080115 0:bf7b9fba3924 593 void check_failed(uint8_t *file, uint32_t line)
frank26080115 0:bf7b9fba3924 594 {
frank26080115 0:bf7b9fba3924 595 /* User can add his own implementation to report the file name and line number,
frank26080115 0:bf7b9fba3924 596 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
frank26080115 0:bf7b9fba3924 597
frank26080115 0:bf7b9fba3924 598 /* Infinite loop */
frank26080115 0:bf7b9fba3924 599 while(1);
frank26080115 0:bf7b9fba3924 600 }
frank26080115 0:bf7b9fba3924 601 #endif
frank26080115 0:bf7b9fba3924 602
frank26080115 0:bf7b9fba3924 603 /*
frank26080115 0:bf7b9fba3924 604 * @}
frank26080115 0:bf7b9fba3924 605 */