NXP's driver library for LPC17xx, ported to mbed's online compiler. Not tested! I had to fix a lot of warings and found a couple of pretty obvious bugs, so the chances are there are more. Original: http://ics.nxp.com/support/documents/microcontrollers/zip/lpc17xx.cmsis.driver.library.zip
lpc17xx_gpio.c
00001 /** 00002 * @file : lpc17xx_gpio.c 00003 * @brief : Contains all functions support for GPIO firmware library on LPC17xx 00004 * @version : 1.0 00005 * @date : 11. Jun. 2009 00006 * @author : HieuNguyen 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 00020 /* Peripheral group ----------------------------------------------------------- */ 00021 /** @addtogroup GPIO 00022 * @{ 00023 */ 00024 00025 /* Includes ------------------------------------------------------------------- */ 00026 #include "lpc17xx_gpio.h" 00027 00028 /* If this source file built with example, the LPC17xx FW library configuration 00029 * file in each example directory ("lpc17xx_libcfg.h") must be included, 00030 * otherwise the default FW library configuration file must be included instead 00031 */ 00032 #ifdef __BUILD_WITH_EXAMPLE__ 00033 #include "lpc17xx_libcfg.h" 00034 #else 00035 #include "lpc17xx_libcfg_default.h" 00036 #endif /* __BUILD_WITH_EXAMPLE__ */ 00037 00038 00039 #ifdef _GPIO 00040 00041 /* Private Functions ---------------------------------------------------------- */ 00042 /** @addtogroup GPIO_Private_Functions 00043 * @{ 00044 */ 00045 /*********************************************************************//** 00046 * @brief Get pointer to GPIO peripheral due to GPIO port 00047 * @param[in] portNum Port Number value, should be in range from 0 to 4. 00048 * @return Pointer to GPIO peripheral 00049 **********************************************************************/ 00050 static LPC_GPIO_TypeDef *GPIO_GetPointer(uint8_t portNum) 00051 { 00052 LPC_GPIO_TypeDef *pGPIO = NULL; 00053 00054 switch (portNum) { 00055 case 0: 00056 pGPIO = LPC_GPIO0; 00057 break; 00058 case 1: 00059 pGPIO = LPC_GPIO1; 00060 break; 00061 case 2: 00062 pGPIO = LPC_GPIO2; 00063 break; 00064 case 3: 00065 pGPIO = LPC_GPIO3; 00066 break; 00067 case 4: 00068 pGPIO = LPC_GPIO4; 00069 break; 00070 default: 00071 break; 00072 } 00073 00074 return pGPIO; 00075 } 00076 00077 /*********************************************************************//** 00078 * @brief Get pointer to FIO peripheral in halfword accessible style 00079 * due to FIO port 00080 * @param[in] portNum Port Number value, should be in range from 0 to 4. 00081 * @return Pointer to FIO peripheral 00082 **********************************************************************/ 00083 static GPIO_HalfWord_TypeDef *FIO_HalfWordGetPointer(uint8_t portNum) 00084 { 00085 GPIO_HalfWord_TypeDef *pFIO = NULL; 00086 00087 switch (portNum) { 00088 case 0: 00089 pFIO = GPIO0_HalfWord; 00090 break; 00091 case 1: 00092 pFIO = GPIO1_HalfWord; 00093 break; 00094 case 2: 00095 pFIO = GPIO2_HalfWord; 00096 break; 00097 case 3: 00098 pFIO = GPIO3_HalfWord; 00099 break; 00100 case 4: 00101 pFIO = GPIO4_HalfWord; 00102 break; 00103 default: 00104 break; 00105 } 00106 00107 return pFIO; 00108 } 00109 00110 /*********************************************************************//** 00111 * @brief Get pointer to FIO peripheral in byte accessible style 00112 * due to FIO port 00113 * @param[in] portNum Port Number value, should be in range from 0 to 4. 00114 * @return Pointer to FIO peripheral 00115 **********************************************************************/ 00116 static GPIO_Byte_TypeDef *FIO_ByteGetPointer(uint8_t portNum) 00117 { 00118 GPIO_Byte_TypeDef *pFIO = NULL; 00119 00120 switch (portNum) { 00121 case 0: 00122 pFIO = GPIO0_Byte; 00123 break; 00124 case 1: 00125 pFIO = GPIO1_Byte; 00126 break; 00127 case 2: 00128 pFIO = GPIO2_Byte; 00129 break; 00130 case 3: 00131 pFIO = GPIO3_Byte; 00132 break; 00133 case 4: 00134 pFIO = GPIO4_Byte; 00135 break; 00136 default: 00137 break; 00138 } 00139 00140 return pFIO; 00141 } 00142 00143 /** 00144 * @} 00145 */ 00146 00147 00148 /* Public Functions ----------------------------------------------------------- */ 00149 /** @addtogroup GPIO_Public_Functions 00150 * @{ 00151 */ 00152 00153 00154 /* GPIO ------------------------------------------------------------------------------ */ 00155 00156 /*********************************************************************//** 00157 * @brief Set Direction for GPIO port. 00158 * @param[in] portNum Port Number value, should be in range from 0 to 4 00159 * @param[in] bitValue Value that contains all bits to set direction, 00160 * in range from 0 to 0xFFFFFFFF. 00161 * example: value 0x5 to set direction for bit 0 and bit 1. 00162 * @param[in] dir Direction value, should be: 00163 * - 0: Input. 00164 * - 1: Output. 00165 * @return None 00166 * 00167 * Note: All remaining bits that are not activated in bitValue (value '0') 00168 * will not be effected by this function. 00169 **********************************************************************/ 00170 void GPIO_SetDir(uint8_t portNum, uint32_t bitValue, uint8_t dir) 00171 { 00172 LPC_GPIO_TypeDef *pGPIO = GPIO_GetPointer(portNum); 00173 00174 if (pGPIO != NULL) { 00175 // Enable Output 00176 if (dir) { 00177 pGPIO->FIODIR |= bitValue; 00178 } 00179 // Enable Input 00180 else { 00181 pGPIO->FIODIR &= ~bitValue; 00182 } 00183 } 00184 } 00185 00186 00187 /*********************************************************************//** 00188 * @brief Set Value for bits that have output direction on GPIO port. 00189 * @param[in] portNum Port number value, should be in range from 0 to 4 00190 * @param[in] bitValue Value that contains all bits on GPIO to set, 00191 * in range from 0 to 0xFFFFFFFF. 00192 * example: value 0x5 to set bit 0 and bit 1. 00193 * @return None 00194 * 00195 * Note: 00196 * - For all bits that has been set as input direction, this function will 00197 * not effect. 00198 * - For all remaining bits that are not activated in bitValue (value '0') 00199 * will not be effected by this function. 00200 **********************************************************************/ 00201 void GPIO_SetValue(uint8_t portNum, uint32_t bitValue) 00202 { 00203 LPC_GPIO_TypeDef *pGPIO = GPIO_GetPointer(portNum); 00204 00205 if (pGPIO != NULL) { 00206 pGPIO->FIOSET = bitValue; 00207 } 00208 } 00209 00210 /*********************************************************************//** 00211 * @brief Clear Value for bits that have output direction on GPIO port. 00212 * @param[in] portNum Port number value, should be in range from 0 to 4 00213 * @param[in] bitValue Value that contains all bits on GPIO to clear, 00214 * in range from 0 to 0xFFFFFFFF. 00215 * example: value 0x5 to clear bit 0 and bit 1. 00216 * @return None 00217 * 00218 * Note: 00219 * - For all bits that has been set as input direction, this function will 00220 * not effect. 00221 * - For all remaining bits that are not activated in bitValue (value '0') 00222 * will not be effected by this function. 00223 **********************************************************************/ 00224 void GPIO_ClearValue(uint8_t portNum, uint32_t bitValue) 00225 { 00226 LPC_GPIO_TypeDef *pGPIO = GPIO_GetPointer(portNum); 00227 00228 if (pGPIO != NULL) { 00229 pGPIO->FIOCLR = bitValue; 00230 } 00231 } 00232 00233 /*********************************************************************//** 00234 * @brief Read Current state on port pin that have input direction of GPIO 00235 * @param[in] portNum Port number to read value, in range from 0 to 4 00236 * @return Current value of GPIO port. 00237 * 00238 * Note: Return value contain state of each port pin (bit) on that GPIO regardless 00239 * its direction is input or output. 00240 **********************************************************************/ 00241 uint32_t GPIO_ReadValue(uint8_t portNum) 00242 { 00243 LPC_GPIO_TypeDef *pGPIO = GPIO_GetPointer(portNum); 00244 00245 if (pGPIO != NULL) { 00246 return pGPIO->FIOPIN; 00247 } 00248 00249 return (0); 00250 } 00251 00252 /* FIO word accessible ----------------------------------------------------------------- */ 00253 /* Stub function for FIO (word-accessible) style */ 00254 00255 /** 00256 * @brief The same with GPIO_SetDir() 00257 */ 00258 void FIO_SetDir(uint8_t portNum, uint32_t bitValue, uint8_t dir) 00259 { 00260 GPIO_SetDir(portNum, bitValue, dir); 00261 } 00262 00263 /** 00264 * @brief The same with GPIO_SetValue() 00265 */ 00266 void FIO_SetValue(uint8_t portNum, uint32_t bitValue) 00267 { 00268 GPIO_SetValue(portNum, bitValue); 00269 } 00270 00271 /** 00272 * @brief The same with GPIO_ClearValue() 00273 */ 00274 void FIO_ClearValue(uint8_t portNum, uint32_t bitValue) 00275 { 00276 GPIO_ClearValue(portNum, bitValue); 00277 } 00278 00279 /** 00280 * @brief The same with GPIO_ReadValue() 00281 */ 00282 uint32_t FIO_ReadValue(uint8_t portNum) 00283 { 00284 return (GPIO_ReadValue(portNum)); 00285 } 00286 00287 00288 /*********************************************************************//** 00289 * @brief Set mask value for bits in FIO port 00290 * @param[in] portNum Port number, in range from 0 to 4 00291 * @param[in] bitValue Value that contains all bits in to set, 00292 * in range from 0 to 0xFFFFFFFF. 00293 * @param[in] maskValue Mask value contains state value for each bit: 00294 * - 0: not mask. 00295 * - 1: mask. 00296 * @return None 00297 * 00298 * Note: 00299 * - All remaining bits that are not activated in bitValue (value '0') 00300 * will not be effected by this function. 00301 * - After executing this function, in mask register, value '0' on each bit 00302 * enables an access to the corresponding physical pin via a read or write access, 00303 * while value '1' on bit (masked) that corresponding pin will not be changed 00304 * with write access and if read, will not be reflected in the updated pin. 00305 **********************************************************************/ 00306 void FIO_SetMask(uint8_t portNum, uint32_t bitValue, uint8_t maskValue) 00307 { 00308 LPC_GPIO_TypeDef *pFIO = GPIO_GetPointer(portNum); 00309 if(pFIO != NULL) { 00310 // Mask 00311 if (maskValue){ 00312 pFIO->FIOMASK |= bitValue; 00313 } 00314 // Un-mask 00315 else { 00316 pFIO->FIOMASK &= ~bitValue; 00317 } 00318 } 00319 } 00320 00321 00322 /* FIO halfword accessible ------------------------------------------------------------- */ 00323 00324 /*********************************************************************//** 00325 * @brief Set direction for FIO port in halfword accessible style 00326 * @param[in] portNum Port number, in range from 0 to 4 00327 * @param[in] halfwordNum HalfWord part number, should be 0 (lower) or 1(upper) 00328 * @param[in] bitValue Value that contains all bits in to set direction, 00329 * in range from 0 to 0xFFFF. 00330 * @param[in] dir Direction value, should be: 00331 * - 0: Input. 00332 * - 1: Output. 00333 * @return None 00334 * 00335 * Note: All remaining bits that are not activated in bitValue (value '0') 00336 * will not be effected by this function. 00337 **********************************************************************/ 00338 void FIO_HalfWordSetDir(uint8_t portNum, uint8_t halfwordNum, uint16_t bitValue, uint8_t dir) 00339 { 00340 GPIO_HalfWord_TypeDef *pFIO = FIO_HalfWordGetPointer(portNum); 00341 if(pFIO != NULL) { 00342 // Output direction 00343 if (dir) { 00344 // Upper 00345 if(halfwordNum) { 00346 pFIO->FIODIRU |= bitValue; 00347 } 00348 // lower 00349 else { 00350 pFIO->FIODIRL |= bitValue; 00351 } 00352 } 00353 // Input direction 00354 else { 00355 // Upper 00356 if(halfwordNum) { 00357 pFIO->FIODIRU &= ~bitValue; 00358 } 00359 // lower 00360 else { 00361 pFIO->FIODIRL &= ~bitValue; 00362 } 00363 } 00364 } 00365 } 00366 00367 00368 /*********************************************************************//** 00369 * @brief Set mask value for bits in FIO port in halfword accessible style 00370 * @param[in] portNum Port number, in range from 0 to 4 00371 * @param[in] halfwordNum HalfWord part number, should be 0 (lower) or 1(upper) 00372 * @param[in] bitValue Value that contains all bits in to set, 00373 * in range from 0 to 0xFFFF. 00374 * @param[in] maskValue Mask value contains state value for each bit: 00375 * - 0: not mask. 00376 * - 1: mask. 00377 * @return None 00378 * 00379 * Note: 00380 * - All remaining bits that are not activated in bitValue (value '0') 00381 * will not be effected by this function. 00382 * - After executing this function, in mask register, value '0' on each bit 00383 * enables an access to the corresponding physical pin via a read or write access, 00384 * while value '1' on bit (masked) that corresponding pin will not be changed 00385 * with write access and if read, will not be reflected in the updated pin. 00386 **********************************************************************/ 00387 void FIO_HalfWordSetMask(uint8_t portNum, uint8_t halfwordNum, uint16_t bitValue, uint8_t maskValue) 00388 { 00389 GPIO_HalfWord_TypeDef *pFIO = FIO_HalfWordGetPointer(portNum); 00390 if(pFIO != NULL) { 00391 // Mask 00392 if (maskValue){ 00393 // Upper 00394 if(halfwordNum) { 00395 pFIO->FIOMASKU |= bitValue; 00396 } 00397 // lower 00398 else { 00399 pFIO->FIOMASKL |= bitValue; 00400 } 00401 } 00402 // Un-mask 00403 else { 00404 // Upper 00405 if(halfwordNum) { 00406 pFIO->FIOMASKU &= ~bitValue; 00407 } 00408 // lower 00409 else { 00410 pFIO->FIOMASKL &= ~bitValue; 00411 } 00412 } 00413 } 00414 } 00415 00416 00417 /*********************************************************************//** 00418 * @brief Set bits for FIO port in halfword accessible style 00419 * @param[in] portNum Port number, in range from 0 to 4 00420 * @param[in] halfwordNum HalfWord part number, should be 0 (lower) or 1(upper) 00421 * @param[in] bitValue Value that contains all bits in to set, 00422 * in range from 0 to 0xFFFF. 00423 * @return None 00424 * 00425 * Note: 00426 * - For all bits that has been set as input direction, this function will 00427 * not effect. 00428 * - For all remaining bits that are not activated in bitValue (value '0') 00429 * will not be effected by this function. 00430 **********************************************************************/ 00431 void FIO_HalfWordSetValue(uint8_t portNum, uint8_t halfwordNum, uint16_t bitValue) 00432 { 00433 GPIO_HalfWord_TypeDef *pFIO = FIO_HalfWordGetPointer(portNum); 00434 if(pFIO != NULL) { 00435 // Upper 00436 if(halfwordNum) { 00437 pFIO->FIOSETU = bitValue; 00438 } 00439 // lower 00440 else { 00441 pFIO->FIOSETL = bitValue; 00442 } 00443 } 00444 } 00445 00446 00447 /*********************************************************************//** 00448 * @brief Clear bits for FIO port in halfword accessible style 00449 * @param[in] portNum Port number, in range from 0 to 4 00450 * @param[in] halfwordNum HalfWord part number, should be 0 (lower) or 1(upper) 00451 * @param[in] bitValue Value that contains all bits in to clear, 00452 * in range from 0 to 0xFFFF. 00453 * @return None 00454 * 00455 * Note: 00456 * - For all bits that has been set as input direction, this function will 00457 * not effect. 00458 * - For all remaining bits that are not activated in bitValue (value '0') 00459 * will not be effected by this function. 00460 **********************************************************************/ 00461 void FIO_HalfWordClearValue(uint8_t portNum, uint8_t halfwordNum, uint16_t bitValue) 00462 { 00463 GPIO_HalfWord_TypeDef *pFIO = FIO_HalfWordGetPointer(portNum); 00464 if(pFIO != NULL) { 00465 // Upper 00466 if(halfwordNum) { 00467 pFIO->FIOCLRU = bitValue; 00468 } 00469 // lower 00470 else { 00471 pFIO->FIOCLRL = bitValue; 00472 } 00473 } 00474 } 00475 00476 00477 /*********************************************************************//** 00478 * @brief Read Current state on port pin that have input direction of GPIO 00479 * in halfword accessible style. 00480 * @param[in] portNum Port number, in range from 0 to 4 00481 * @param[in] halfwordNum HalfWord part number, should be 0 (lower) or 1(upper) 00482 * @return Current value of FIO port pin of specified halfword. 00483 * Note: Return value contain state of each port pin (bit) on that FIO regardless 00484 * its direction is input or output. 00485 **********************************************************************/ 00486 uint16_t FIO_HalfWordReadValue(uint8_t portNum, uint8_t halfwordNum) 00487 { 00488 GPIO_HalfWord_TypeDef *pFIO = FIO_HalfWordGetPointer(portNum); 00489 if(pFIO != NULL) { 00490 // Upper 00491 if(halfwordNum) { 00492 return (pFIO->FIOPINU); 00493 } 00494 // lower 00495 else { 00496 return (pFIO->FIOPINL); 00497 } 00498 } 00499 return (0); 00500 } 00501 00502 00503 /* FIO Byte accessible ------------------------------------------------------------ */ 00504 00505 /*********************************************************************//** 00506 * @brief Set direction for FIO port in byte accessible style 00507 * @param[in] portNum Port number, in range from 0 to 4 00508 * @param[in] byteNum Byte part number, should be in range from 0 to 3 00509 * @param[in] bitValue Value that contains all bits in to set direction, 00510 * in range from 0 to 0xFF. 00511 * @param[in] dir Direction value, should be: 00512 * - 0: Input. 00513 * - 1: Output. 00514 * @return None 00515 * 00516 * Note: All remaining bits that are not activated in bitValue (value '0') 00517 * will not be effected by this function. 00518 **********************************************************************/ 00519 void FIO_ByteSetDir(uint8_t portNum, uint8_t byteNum, uint8_t bitValue, uint8_t dir) 00520 { 00521 GPIO_Byte_TypeDef *pFIO = FIO_ByteGetPointer(portNum); 00522 if(pFIO != NULL) { 00523 // Output direction 00524 if (dir) { 00525 if (byteNum <= 3) { 00526 pFIO->FIODIR[byteNum] |= bitValue; 00527 } 00528 } 00529 // Input direction 00530 else { 00531 if (byteNum <= 3) { 00532 pFIO->FIODIR[byteNum] &= ~bitValue; 00533 } 00534 } 00535 } 00536 } 00537 00538 /*********************************************************************//** 00539 * @brief Set mask value for bits in FIO port in byte accessible style 00540 * @param[in] portNum Port number, in range from 0 to 4 00541 * @param[in] byteNum Byte part number, should be in range from 0 to 3 00542 * @param[in] bitValue Value that contains all bits in to set mask, 00543 * in range from 0 to 0xFF. 00544 * @param[in] maskValue Mask value contains state value for each bit: 00545 * - 0: not mask. 00546 * - 1: mask. 00547 * @return None 00548 * 00549 * Note: 00550 * - All remaining bits that are not activated in bitValue (value '0') 00551 * will not be effected by this function. 00552 * - After executing this function, in mask register, value '0' on each bit 00553 * enables an access to the corresponding physical pin via a read or write access, 00554 * while value '1' on bit (masked) that corresponding pin will not be changed 00555 * with write access and if read, will not be reflected in the updated pin. 00556 **********************************************************************/ 00557 void FIO_ByteSetMask(uint8_t portNum, uint8_t byteNum, uint8_t bitValue, uint8_t maskValue) 00558 { 00559 GPIO_Byte_TypeDef *pFIO = FIO_ByteGetPointer(portNum); 00560 if(pFIO != NULL) { 00561 // Mask 00562 if (maskValue) { 00563 if (byteNum <= 3) { 00564 pFIO->FIOMASK[byteNum] |= bitValue; 00565 } 00566 } 00567 // Un-mask 00568 else { 00569 if (byteNum <= 3) { 00570 pFIO->FIOMASK[byteNum] &= ~bitValue; 00571 } 00572 } 00573 } 00574 } 00575 00576 00577 /*********************************************************************//** 00578 * @brief Set bits for FIO port in byte accessible style 00579 * @param[in] portNum Port number, in range from 0 to 4 00580 * @param[in] byteNum Byte part number, should be in range from 0 to 3 00581 * @param[in] bitValue Value that contains all bits in to set, 00582 * in range from 0 to 0xFF. 00583 * @return None 00584 * 00585 * Note: 00586 * - For all bits that has been set as input direction, this function will 00587 * not effect. 00588 * - For all remaining bits that are not activated in bitValue (value '0') 00589 * will not be effected by this function. 00590 **********************************************************************/ 00591 void FIO_ByteSetValue(uint8_t portNum, uint8_t byteNum, uint8_t bitValue) 00592 { 00593 GPIO_Byte_TypeDef *pFIO = FIO_ByteGetPointer(portNum); 00594 if (pFIO != NULL) { 00595 if (byteNum <= 3){ 00596 pFIO->FIOSET[byteNum] = bitValue; 00597 } 00598 } 00599 } 00600 00601 00602 /*********************************************************************//** 00603 * @brief Clear bits for FIO port in byte accessible style 00604 * @param[in] portNum Port number, in range from 0 to 4 00605 * @param[in] byteNum Byte part number, should be in range from 0 to 3 00606 * @param[in] bitValue Value that contains all bits in to clear, 00607 * in range from 0 to 0xFF. 00608 * @return None 00609 * 00610 * Note: 00611 * - For all bits that has been set as input direction, this function will 00612 * not effect. 00613 * - For all remaining bits that are not activated in bitValue (value '0') 00614 * will not be effected by this function. 00615 **********************************************************************/ 00616 void FIO_ByteClearValue(uint8_t portNum, uint8_t byteNum, uint8_t bitValue) 00617 { 00618 GPIO_Byte_TypeDef *pFIO = FIO_ByteGetPointer(portNum); 00619 if (pFIO != NULL) { 00620 if (byteNum <= 3){ 00621 pFIO->FIOCLR[byteNum] = bitValue; 00622 } 00623 } 00624 } 00625 00626 00627 /*********************************************************************//** 00628 * @brief Read Current state on port pin that have input direction of GPIO 00629 * in byte accessible style. 00630 * @param[in] portNum Port number, in range from 0 to 4 00631 * @param[in] byteNum Byte part number, should be in range from 0 to 3 00632 * @return Current value of FIO port pin of specified byte part. 00633 * Note: Return value contain state of each port pin (bit) on that FIO regardless 00634 * its direction is input or output. 00635 **********************************************************************/ 00636 uint8_t FIO_ByteReadValue(uint8_t portNum, uint8_t byteNum) 00637 { 00638 GPIO_Byte_TypeDef *pFIO = FIO_ByteGetPointer(portNum); 00639 if (pFIO != NULL) { 00640 if (byteNum <= 3){ 00641 return (pFIO->FIOPIN[byteNum]); 00642 } 00643 } 00644 return (0); 00645 } 00646 00647 /** 00648 * @} 00649 */ 00650 00651 #endif /* _GPIO */ 00652 00653 /** 00654 * @} 00655 */ 00656 00657 /* --------------------------------- End Of File ------------------------------ */
Generated on Tue Jul 12 2022 17:06:02 by 1.7.2