These are the examples provided for [[/users/frank26080115/libraries/LPC1700CMSIS_Lib/]] Note, the entire "program" is not compilable!
uart_fullmodem_test.c
00001 /***********************************************************************//** 00002 * @file uart_fullmodem_test.c 00003 * @purpose This example describes how to use UART1 full-modem function 00004 * @version 2.0 00005 * @date 21. May. 2010 00006 * @author NXP MCU SW Application Team 00007 *--------------------------------------------------------------------- 00008 * Software that is described herein is for illustrative purposes only 00009 * which provides customers with programming information regarding the 00010 * products. This software is supplied "AS IS" without any warranties. 00011 * NXP Semiconductors assumes no responsibility or liability for the 00012 * use of the software, conveys no license or title under any patent, 00013 * copyright, or mask work right to the product. NXP Semiconductors 00014 * reserves the right to make changes in the software without 00015 * notification. NXP Semiconductors also make no representation or 00016 * warranty that such application will be suitable for the specified 00017 * use without further testing or modification. 00018 **********************************************************************/ 00019 #include "lpc17xx_uart.h" 00020 #include "lpc17xx_libcfg.h" 00021 #include "lpc17xx_pinsel.h" 00022 00023 /* Example group ----------------------------------------------------------- */ 00024 /** @defgroup UART_UART1_FullModem UART1_FullModem 00025 * @ingroup UART_Examples 00026 * @{ 00027 */ 00028 00029 00030 /************************** PRIVATE DEFINITIONS *************************/ 00031 #define MCB_LPC_1768 00032 //#define IAR_LPC_1768 00033 00034 // buffer size definition 00035 #define UART_RING_BUFSIZE 256 00036 /* Auto RTS and Auto CTS definition: 00037 * - 1: Enable Auto RTS and CTS function 00038 * - 0: Disable this function, in this case, handle manually 00039 * modem functionality */ 00040 #define AUTO_RTS_CTS_USE 0 00041 00042 /* Buf mask */ 00043 #define __BUF_MASK (UART_RING_BUFSIZE-1) 00044 /* Check buf is full or not */ 00045 #define __BUF_IS_FULL(head, tail) ((tail&__BUF_MASK)==((head+1)&__BUF_MASK)) 00046 /* Check buf will be full in next receiving or not */ 00047 #define __BUF_WILL_FULL(head, tail) ((tail&__BUF_MASK)==((head+2)&__BUF_MASK)) 00048 /* Check buf is empty */ 00049 #define __BUF_IS_EMPTY(head, tail) ((head&__BUF_MASK)==(tail&__BUF_MASK)) 00050 /* Reset buf */ 00051 #define __BUF_RESET(bufidx) (bufidx=0) 00052 #define __BUF_INCR(bufidx) (bufidx=(bufidx+1)&__BUF_MASK) 00053 00054 00055 /************************** PRIVATE TYPES *************************/ 00056 /** @brief UART Ring buffer structure */ 00057 typedef struct 00058 { 00059 __IO uint32_t tx_head; /*!< UART Tx ring buffer head index */ 00060 __IO uint32_t tx_tail; /*!< UART Tx ring buffer tail index */ 00061 __IO uint32_t rx_head; /*!< UART Rx ring buffer head index */ 00062 __IO uint32_t rx_tail; /*!< UART Rx ring buffer tail index */ 00063 __IO uint8_t tx[UART_RING_BUFSIZE]; /*!< UART Tx data ring buffer */ 00064 __IO uint8_t rx[UART_RING_BUFSIZE]; /*!< UART Rx data ring buffer */ 00065 } UART_RING_BUFFER_T; 00066 00067 00068 /************************** PRIVATE VARIABLES *************************/ 00069 uint8_t menu1[] = "Hello NXP Semiconductors \n\r"; 00070 uint8_t menu2[] = 00071 "UART1 Full Modem \n\r\t " 00072 "MCU LPC17xx - ARM Cortex-M3 \n\r\t " 00073 "UART1 - 9600bps \n\r"; 00074 uint8_t menu3[] = "UART demo terminated!\n"; 00075 00076 // UART Ring buffer 00077 UART_RING_BUFFER_T rb; 00078 00079 // RTS State 00080 __IO int32_t RTS_State; 00081 00082 // Current Tx Interrupt enable state 00083 __IO FlagStatus TxIntStat; 00084 00085 00086 /************************** PRIVATE FUNCTIONS *************************/ 00087 /* Interrupt service routines */ 00088 void UART1_IRQHandler(void); 00089 void UART1_IntTransmit(void); 00090 void UART1_IntReceive(void); 00091 void UART1_IntErr(uint8_t bLSErrType); 00092 00093 uint32_t UARTReceive(LPC_UART_TypeDef *UARTPort, uint8_t *rxbuf, uint8_t buflen); 00094 uint32_t UARTSend(LPC_UART_TypeDef *UARTPort, uint8_t *txbuf, uint8_t buflen); 00095 void print_menu(void); 00096 00097 00098 /*----------------- INTERRUPT SERVICE ROUTINES --------------------------*/ 00099 /*********************************************************************//** 00100 * @brief UART1 interrupt handler sub-routine 00101 * @param[in] None 00102 * @return None 00103 **********************************************************************/ 00104 void UART1_IRQHandler(void) 00105 { 00106 uint8_t modemsts; 00107 uint32_t intsrc, tmp, tmp1; 00108 00109 /* Determine the interrupt source */ 00110 intsrc = UART_GetIntId((LPC_UART_TypeDef *)LPC_UART1); 00111 tmp = intsrc & UART_IIR_INTID_MASK; 00112 00113 /* 00114 * In case of using UART1 with full modem, 00115 * interrupt ID = 0 that means modem status interrupt has been detected 00116 */ 00117 00118 if (tmp == 0){ 00119 // Check Modem status 00120 modemsts = UART_FullModemGetStatus(LPC_UART1); 00121 #if (AUTO_RTS_CTS_USE == 0) 00122 // Check CTS status change flag 00123 if (modemsts & UART1_MODEM_STAT_DELTA_CTS) { 00124 // if CTS status is active, continue to send data 00125 if (modemsts & UART1_MODEM_STAT_CTS) { 00126 // Re-Enable Tx 00127 UART_TxCmd((LPC_UART_TypeDef *)LPC_UART1, ENABLE); 00128 } 00129 // Otherwise, Stop current transmission immediately 00130 else{ 00131 // Disable Tx 00132 UART_TxCmd((LPC_UART_TypeDef *)LPC_UART1, DISABLE); 00133 } 00134 } 00135 #endif 00136 } 00137 00138 // Receive Line Status 00139 if (tmp == UART_IIR_INTID_RLS){ 00140 // Check line status 00141 tmp1 = UART_GetLineStatus((LPC_UART_TypeDef *)LPC_UART1); 00142 // Mask out the Receive Ready and Transmit Holding empty status 00143 tmp1 &= (UART_LSR_OE | UART_LSR_PE | UART_LSR_FE \ 00144 | UART_LSR_BI | UART_LSR_RXFE); 00145 // If any error exist 00146 if (tmp1) { 00147 UART1_IntErr(tmp1); 00148 } 00149 } 00150 00151 // Receive Data Available or Character time-out 00152 if ((tmp == UART_IIR_INTID_RDA) || (tmp == UART_IIR_INTID_CTI)){ 00153 UART1_IntReceive(); 00154 } 00155 00156 // Transmit Holding Empty 00157 if (tmp == UART_IIR_INTID_THRE){ 00158 UART1_IntTransmit(); 00159 } 00160 } 00161 00162 /********************************************************************//** 00163 * @brief UART1 receive function (ring buffer used) 00164 * @param[in] None 00165 * @return None 00166 *********************************************************************/ 00167 void UART1_IntReceive(void) 00168 { 00169 uint8_t tmpc; 00170 uint32_t rLen; 00171 00172 while (1){ 00173 // Call UART read function in UART driver 00174 rLen = UART_Receive((LPC_UART_TypeDef *)LPC_UART1, &tmpc, 1, NONE_BLOCKING); 00175 // If data received 00176 if (rLen){ 00177 00178 /* If buffer will be full and RTS is driven manually, 00179 * RTS pin should be forced into INACTIVE state 00180 */ 00181 #if (AUTO_RTS_CTS_USE == 0) 00182 if (__BUF_WILL_FULL(rb.rx_head, rb.rx_tail)) 00183 { 00184 if (RTS_State == ACTIVE) 00185 { 00186 // Disable request to send through RTS line 00187 UART_FullModemForcePinState(LPC_UART1, UART1_MODEM_PIN_RTS, \ 00188 INACTIVE); 00189 RTS_State = INACTIVE; 00190 } 00191 } 00192 #endif 00193 00194 /* Check if buffer is more space 00195 * If no more space, remaining character will be trimmed out 00196 */ 00197 if (!__BUF_IS_FULL(rb.rx_head,rb.rx_tail)){ 00198 rb.rx[rb.rx_head] = tmpc; 00199 __BUF_INCR(rb.rx_head); 00200 } 00201 } 00202 // no more data 00203 else { 00204 break; 00205 } 00206 } 00207 } 00208 00209 00210 /********************************************************************//** 00211 * @brief UART1 transmit function (ring buffer used) 00212 * @param[in] None 00213 * @return None 00214 *********************************************************************/ 00215 void UART1_IntTransmit(void) 00216 { 00217 // Disable THRE interrupt 00218 UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_THRE, DISABLE); 00219 00220 /* Wait for FIFO buffer empty, transfer UART_TX_FIFO_SIZE bytes 00221 * of data or break whenever ring buffers are empty */ 00222 /* Wait until THR empty */ 00223 while (UART_CheckBusy((LPC_UART_TypeDef *)LPC_UART1) == SET); 00224 00225 while (!__BUF_IS_EMPTY(rb.tx_head,rb.tx_tail)) 00226 { 00227 /* Move a piece of data into the transmit FIFO */ 00228 if (UART_Send((LPC_UART_TypeDef *)LPC_UART1, (uint8_t *)&rb.tx[rb.tx_tail], \ 00229 1, NONE_BLOCKING)){ 00230 /* Update transmit ring FIFO tail pointer */ 00231 __BUF_INCR(rb.tx_tail); 00232 } else { 00233 break; 00234 } 00235 } 00236 00237 /* If there is no more data to send, disable the transmit 00238 interrupt - else enable it or keep it enabled */ 00239 if (__BUF_IS_EMPTY(rb.tx_head, rb.tx_tail)) { 00240 UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_THRE, DISABLE); 00241 // Reset Tx Interrupt state 00242 TxIntStat = RESET; 00243 } 00244 else{ 00245 // Set Tx Interrupt state 00246 TxIntStat = SET; 00247 UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_THRE, ENABLE); 00248 } 00249 } 00250 00251 00252 /*********************************************************************//** 00253 * @brief UART Line Status Error 00254 * @param[in] bLSErrType UART Line Status Error Type 00255 * @return None 00256 **********************************************************************/ 00257 void UART1_IntErr(uint8_t bLSErrType) 00258 { 00259 uint8_t test; 00260 // Loop forever 00261 while (1){ 00262 // For testing purpose 00263 test = bLSErrType; 00264 } 00265 } 00266 00267 /*-------------------------PRIVATE FUNCTIONS------------------------------*/ 00268 /*********************************************************************//** 00269 * @brief UART transmit function for interrupt mode (using ring buffers) 00270 * @param[in] UARTPort Selected UART peripheral used to send data, 00271 * should be UART1. 00272 * @param[out] txbuf Pointer to Transmit buffer 00273 * @param[in] buflen Length of Transmit buffer 00274 * @return Number of bytes actually sent to the ring buffer 00275 **********************************************************************/ 00276 uint32_t UARTSend(LPC_UART_TypeDef *UARTPort, uint8_t *txbuf, uint8_t buflen) 00277 { 00278 uint8_t *data = (uint8_t *) txbuf; 00279 uint32_t bytes = 0; 00280 00281 00282 /* Temporarily lock out UART transmit interrupts during this 00283 read so the UART transmit interrupt won't cause problems 00284 with the index values */ 00285 UART_IntConfig(UARTPort, UART_INTCFG_THRE, DISABLE); 00286 00287 /* Loop until transmit run buffer is full or until n_bytes 00288 expires */ 00289 while ((buflen > 0) && (!__BUF_IS_FULL(rb.tx_head, rb.tx_tail))) 00290 { 00291 /* Write data from buffer into ring buffer */ 00292 rb.tx[rb.tx_head] = *data; 00293 data++; 00294 00295 /* Increment head pointer */ 00296 __BUF_INCR(rb.tx_head); 00297 00298 /* Increment data count and decrement buffer size count */ 00299 bytes++; 00300 buflen--; 00301 } 00302 00303 /* 00304 * Check if current Tx interrupt enable is reset, 00305 * that means the Tx interrupt must be re-enabled 00306 * due to call UART_IntTransmit() function to trigger 00307 * this interrupt type 00308 */ 00309 if (TxIntStat == RESET) { 00310 UART1_IntTransmit(); 00311 } 00312 /* 00313 * Otherwise, re-enables Tx Interrupt 00314 */ 00315 else { 00316 UART_IntConfig(UARTPort, UART_INTCFG_THRE, ENABLE); 00317 } 00318 00319 return bytes; 00320 } 00321 00322 /*********************************************************************//** 00323 * @brief UART read function for interrupt mode (using ring buffers) 00324 * @param[in] UARTPort Selected UART peripheral used to send data, 00325 * should be UART1. 00326 * @param[out] rxbuf Pointer to Received buffer 00327 * @param[in] buflen Length of Received buffer 00328 * @return Number of bytes actually read from the ring buffer 00329 **********************************************************************/ 00330 uint32_t UARTReceive(LPC_UART_TypeDef *UARTPort, uint8_t *rxbuf, uint8_t buflen) 00331 { 00332 uint8_t *data = (uint8_t *) rxbuf; 00333 uint32_t bytes = 0; 00334 00335 /* Temporarily lock out UART receive interrupts during this 00336 read so the UART receive interrupt won't cause problems 00337 with the index values */ 00338 UART_IntConfig(UARTPort, UART_INTCFG_RBR, DISABLE); 00339 00340 /* Loop until receive buffer ring is empty or 00341 until max_bytes expires */ 00342 while ((buflen > 0) && (!(__BUF_IS_EMPTY(rb.rx_head, rb.rx_tail)))) 00343 { 00344 /* Read data from ring buffer into user buffer */ 00345 *data = rb.rx[rb.rx_tail]; 00346 data++; 00347 00348 /* Update tail pointer */ 00349 __BUF_INCR(rb.rx_tail); 00350 00351 /* Increment data count and decrement buffer size count */ 00352 bytes++; 00353 buflen--; 00354 00355 #if (AUTO_RTS_CTS_USE == 0) 00356 /* In case of driving RTS manually, this pin should be 00357 * release into ACTIVE state if buffer is free 00358 */ 00359 if (RTS_State == INACTIVE) 00360 { 00361 if (!__BUF_WILL_FULL(rb.rx_head, rb.rx_tail)) 00362 { 00363 // Disable request to send through RTS line 00364 UART_FullModemForcePinState(LPC_UART1, UART1_MODEM_PIN_RTS, \ 00365 ACTIVE); 00366 RTS_State = ACTIVE; 00367 } 00368 } 00369 #endif 00370 } 00371 00372 /* Re-enable UART interrupts */ 00373 UART_IntConfig(UARTPort, UART_INTCFG_RBR, ENABLE); 00374 00375 return bytes; 00376 } 00377 00378 /*********************************************************************//** 00379 * @brief Print Welcome Screen Menu subroutine 00380 * @param None 00381 * @return None 00382 **********************************************************************/ 00383 void print_menu(void) 00384 { 00385 uint32_t tmp, tmp2; 00386 uint8_t *pDat; 00387 00388 tmp = sizeof(menu1); 00389 tmp2 = 0; 00390 pDat = (uint8_t *)&menu1[0]; 00391 while(tmp) { 00392 tmp2 = UARTSend((LPC_UART_TypeDef *)LPC_UART1, pDat, tmp); 00393 pDat += tmp2; 00394 tmp -= tmp2; 00395 } 00396 00397 tmp = sizeof(menu2); 00398 tmp2 = 0; 00399 pDat = (uint8_t *)&menu2[0]; 00400 while(tmp) { 00401 tmp2 = UARTSend((LPC_UART_TypeDef *)LPC_UART1, pDat, tmp); 00402 pDat += tmp2; 00403 tmp -= tmp2; 00404 } 00405 } 00406 00407 /*-------------------------MAIN FUNCTION------------------------------*/ 00408 /*********************************************************************//** 00409 * @brief c_entry: Main UART-FULLMODEM program body 00410 * @param[in] None 00411 * @return int 00412 **********************************************************************/ 00413 int c_entry(void) 00414 { 00415 // UART Configuration structure variable 00416 UART_CFG_Type UARTConfigStruct; 00417 // UART FIFO configuration Struct variable 00418 UART_FIFO_CFG_Type UARTFIFOConfigStruct; 00419 // Pin configuration for UART1 00420 PINSEL_CFG_Type PinCfg; 00421 uint32_t idx, len; 00422 __IO FlagStatus exitflag; 00423 uint8_t buffer[10]; 00424 00425 /* 00426 * Initialize UART1 pin connect 00427 * If using MCB1700 eval board, assign pin P2.0 - P2.7 00428 * If using IAR 1768 KS board, assign pin P0.7 - P0.15 00429 */ 00430 #ifdef MCB_LPC_1768 00431 PinCfg.Funcnum = 2; 00432 PinCfg.OpenDrain = 0; 00433 PinCfg.Pinmode = 0; 00434 PinCfg.Portnum = 2; 00435 for (idx = 0; idx <= 7; idx++){ 00436 PinCfg.Pinnum = idx; 00437 PINSEL_ConfigPin(&PinCfg); 00438 } 00439 #elif defined(IAR_LPC_1768) 00440 PinCfg.Funcnum = 1; 00441 PinCfg.OpenDrain = 0; 00442 PinCfg.Pinmode = 0; 00443 PinCfg.Portnum = 0; 00444 for (idx = 15; idx <= 22; idx++){ 00445 PinCfg.Pinnum = idx; 00446 PINSEL_ConfigPin(&PinCfg); 00447 } 00448 #endif 00449 00450 /* Initialize UART Configuration parameter structure to default state: 00451 * Baudrate = 9600bps 00452 * 8 data bit 00453 * 1 Stop bit 00454 * None parity 00455 */ 00456 UART_ConfigStructInit(&UARTConfigStruct); 00457 00458 // Initialize UART1 peripheral with given to corresponding parameter 00459 UART_Init((LPC_UART_TypeDef *)LPC_UART1, &UARTConfigStruct); 00460 00461 /* Initialize FIFOConfigStruct to default state: 00462 * - FIFO_DMAMode = DISABLE 00463 * - FIFO_Level = UART_FIFO_TRGLEV0 00464 * - FIFO_ResetRxBuf = ENABLE 00465 * - FIFO_ResetTxBuf = ENABLE 00466 * - FIFO_State = ENABLE 00467 */ 00468 UART_FIFOConfigStructInit(&UARTFIFOConfigStruct); 00469 00470 // Initialize FIFO for UART1 peripheral 00471 UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART1, &UARTFIFOConfigStruct); 00472 00473 #if (AUTO_RTS_CTS_USE==0) 00474 /* 00475 * Determine current state of CTS pin to enable Tx 00476 * activity 00477 */ 00478 if (UART_FullModemGetStatus(LPC_UART1) & UART1_MODEM_STAT_CTS) { 00479 // Enable UART Transmit 00480 UART_TxCmd((LPC_UART_TypeDef *)LPC_UART1, ENABLE); 00481 } 00482 #else 00483 // Enable UART Transmit 00484 UART_TxCmd((LPC_UART_TypeDef *)LPC_UART1, ENABLE); 00485 #endif 00486 00487 // Reset ring buf head and tail idx 00488 __BUF_RESET(rb.rx_head); 00489 __BUF_RESET(rb.rx_tail); 00490 __BUF_RESET(rb.tx_head); 00491 __BUF_RESET(rb.tx_tail); 00492 00493 #if AUTO_RTS_CTS_USE 00494 UART_FullModemConfigMode(LPC_UART1, UART1_MODEM_MODE_AUTO_RTS, ENABLE); 00495 UART_FullModemConfigMode(LPC_UART1, UART1_MODEM_MODE_AUTO_CTS, ENABLE); 00496 #else 00497 // Enable Modem status interrupt 00498 UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART1_INTCFG_MS, ENABLE); 00499 // Enable CTS1 signal transition interrupt 00500 UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART1_INTCFG_CTS, ENABLE); 00501 // Force RTS pin state to ACTIVE 00502 UART_FullModemForcePinState(LPC_UART1, UART1_MODEM_PIN_RTS, ACTIVE); 00503 //RESET RTS State flag 00504 RTS_State = ACTIVE; 00505 #endif 00506 00507 00508 /* Enable UART Rx interrupt */ 00509 UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_RBR, ENABLE); 00510 /* Enable UART line status interrupt */ 00511 UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_RLS, ENABLE); 00512 00513 /* 00514 * Do not enable transmit interrupt here, since it is handled by 00515 * UART_Send() function, just to reset Tx Interrupt state for the 00516 * first time 00517 */ 00518 TxIntStat = RESET; 00519 00520 /* preemption = 1, sub-priority = 1 */ 00521 NVIC_SetPriority(UART1_IRQn, ((0x01<<3)|0x01)); 00522 /* Enable Interrupt for UART1 channel */ 00523 NVIC_EnableIRQ(UART1_IRQn); 00524 00525 // print welcome screen 00526 print_menu(); 00527 00528 // reset exit flag 00529 exitflag = RESET; 00530 00531 /* Read some data from the buffer */ 00532 while (exitflag == RESET) 00533 { 00534 len = 0; 00535 while (len == 0) 00536 { 00537 len = UARTReceive((LPC_UART_TypeDef *)LPC_UART1, buffer, sizeof(buffer)); 00538 } 00539 00540 /* Got some data */ 00541 idx = 0; 00542 while (idx < len) 00543 { 00544 if (buffer[idx] == 27) 00545 { 00546 /* ESC key, set exit flag */ 00547 UARTSend((LPC_UART_TypeDef *)LPC_UART1, menu3, sizeof(menu3)); 00548 exitflag = SET; 00549 } 00550 else if (buffer[idx] == 'r') 00551 { 00552 print_menu(); 00553 } 00554 else 00555 { 00556 /* Echo it back */ 00557 UARTSend((LPC_UART_TypeDef *)LPC_UART1, &buffer[idx], 1); 00558 } 00559 idx++; 00560 } 00561 } 00562 00563 // wait for current transmission complete - THR must be empty 00564 while (UART_CheckBusy((LPC_UART_TypeDef *)LPC_UART1) == SET); 00565 00566 // DeInitialize UART1 peripheral 00567 UART_DeInit((LPC_UART_TypeDef *)LPC_UART1); 00568 00569 /* Loop forever */ 00570 while(1); 00571 return 1; 00572 } 00573 00574 /* With ARM and GHS toolsets, the entry point is main() - this will 00575 allow the linker to generate wrapper code to setup stacks, allocate 00576 heap area, and initialize and copy code and data segments. For GNU 00577 toolsets, the entry point is through __start() in the crt0_gnu.asm 00578 file, and that startup code will setup stacks and data */ 00579 int main(void) 00580 { 00581 return c_entry(); 00582 } 00583 00584 00585 #ifdef DEBUG 00586 /******************************************************************************* 00587 * @brief Reports the name of the source file and the source line number 00588 * where the CHECK_PARAM error has occurred. 00589 * @param[in] file Pointer to the source file name 00590 * @param[in] line assert_param error line source number 00591 * @return None 00592 *******************************************************************************/ 00593 void check_failed(uint8_t *file, uint32_t line) 00594 { 00595 /* User can add his own implementation to report the file name and line number, 00596 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00597 00598 /* Infinite loop */ 00599 while(1); 00600 } 00601 #endif 00602 00603 /* 00604 * @} 00605 */
Generated on Tue Jul 12 2022 17:28:09 by 1.7.2