SCU_LPC18xx.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* --------------------------------------------------------------------------
  2. * Copyright (c) 2013-2016 ARM Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * $Date: 02. March 2016
  19. * $Revision: V1.1
  20. *
  21. * Project: SCU Driver for NXP LPC18xx
  22. * -------------------------------------------------------------------------- */
  23. /* History:
  24. * Version 1.1
  25. * - Corrected SCU_SFSCLKx(clk_pin) and SCU_ENAIOx(n) macros
  26. * Version 1.0
  27. * - Initial release
  28. */
  29. #include "LPC18xx.h"
  30. #include "SCU_LPC18xx.h"
  31. #define PORT_OFFSET ( 0x80 )
  32. #define PIN_OFFSET ( 0x04 )
  33. #define SCU_SFSPx(port, pin) (*((volatile uint32_t *) ((LPC_SCU_BASE + PORT_OFFSET * port + PIN_OFFSET * pin))))
  34. #define SCU_SFSCLKx(clk_pin) (*((volatile uint32_t *) (&(LPC_SCU->SFSCLK_0) + clk_pin)))
  35. #define SCU_ENAIOx(n) (*((volatile uint32_t *) (&(LPC_SCU->ENAIO0) + n)))
  36. /**
  37. \fn int32_t SCU_PinConfiguare (uint8_t port, uint8_t pin, uint32_t pin_cfg)
  38. \brief Set pin function and electrical characteristics
  39. \param[in] port Port number (0..15)
  40. \param[in] pin Pin number (0..31)
  41. \param[in] pin_cfg pin_cfg configuration bit mask
  42. - \b 0: function succeeded
  43. - \b -1: function failed
  44. */
  45. int32_t SCU_PinConfigure (uint8_t port, uint8_t pin, uint32_t pin_cfg) {
  46. if ((port > 15) || (pin > 31)) return -1;
  47. SCU_SFSPx(port, pin) = pin_cfg;
  48. return 0;
  49. }
  50. /**
  51. \fn int32_t SCU_CLK_PinConfigure (uint8_t clk_pin, uint32_t pin_cfg)
  52. \brief Set pin function and electrical characteristics for CLK pins
  53. \param[in] clk_pin Clock pin number should be 0..3
  54. \param[in] pin_cfg pin_cfg
  55. - \b 0: function succeeded
  56. - \b -1: function failed
  57. */
  58. int32_t SCU_CLK_PinConfigure (uint8_t pin_clk, uint32_t pin_cfg) {
  59. if (pin_clk > 3) return -1;
  60. SCU_SFSCLKx(pin_clk) = pin_cfg;
  61. return 0;
  62. }
  63. /**
  64. \fn int32_t SCU_USB1_PinConfigure (uint32_t USB1_pin_cfg)
  65. \brief Pin configuration for USB1 USB_DP/USBDM pins
  66. \param[in] USB1_pin_cfg USB1_pin_cfg configuration bit mask
  67. - \b 0: function succeeded
  68. - \b -1: function failed
  69. */
  70. int32_t SCU_USB1_PinConfigure (uint32_t USB1_pin_cfg) {
  71. LPC_SCU->SFSUSB = USB1_pin_cfg;
  72. return 0;
  73. }
  74. /**
  75. \fn int32_t SCU_I2C_PinConfigure (uint32_t I2C_mode)
  76. \brief Set I2C pin configuration
  77. \param[in] I2C_mode: SCU_I2C_PIN_MODE_DISABLED
  78. SCU_I2C_PIN_MODE_STANDARD_FAST
  79. SCU_I2C_PIN_MODE_FAST_PLUS
  80. - \b 0: function succeeded
  81. - \b -1: function failed
  82. */
  83. int32_t SCU_I2C_PinConfigure (uint32_t I2C_mode) {
  84. switch (I2C_mode) {
  85. case SCU_I2C_PIN_MODE_DISABLED: break;
  86. case SCU_I2C_PIN_MODE_STANDARD_FAST: break;
  87. case SCU_I2C_PIN_MODE_FAST_PLUS: break;
  88. default: return -1;
  89. }
  90. LPC_SCU->SFSI2C0 = I2C_mode;
  91. return 0;
  92. }
  93. /**
  94. \fn int32_t SCU_ADC_ChannelPinConfigure (uint8_t ADC_num, uint8_t channel, uint32_t cmd)
  95. \brief ADC Channel configuration
  96. \param[in] ADC_num: 0 = ADC0, 1 = ADC1
  97. \param[in] channel: channel number 0..7
  98. \param[in] cmd: 1 - enabled, 0 - disabled
  99. - \b 0: function succeeded
  100. - \b -1: function failed
  101. */
  102. int32_t SCU_ADC_ChannelPinConfigure (uint8_t ADC_num, uint8_t channel, uint32_t cmd) {
  103. if ((ADC_num > 1) || (channel > 7) || (cmd > 1)) return -1;
  104. cmd ? (SCU_ENAIOx(ADC_num) |= (1 << channel)) : (SCU_ENAIOx(ADC_num) &= ~(1 << channel));
  105. return 0;
  106. }
  107. /**
  108. \fn int32_t SCU_DAC_PinConfigure (uint32_t cmd)
  109. \brief Analog function on P4_4
  110. \param[in] cmd: 1 - enabled, 0 - disabled
  111. - \b 0: function succeeded
  112. - \b -1: function failed
  113. */
  114. int32_t SCU_DAC_PinConfigure (uint32_t cmd) {
  115. if (cmd > 1) return -1;
  116. cmd ? (LPC_SCU->ENAIO2 |= SCU_ENAIO2_DAC) : (LPC_SCU->ENAIO2 &= ~SCU_ENAIO2_DAC);
  117. return 0;
  118. }
  119. /**
  120. \fn int32_t SCU_PinInterruptSourceSelect (uint8_t pin_int, uint8_t port, uint8_t pin)
  121. \brief Select interrupt source pin
  122. \param[in] pin_int: pin interrupt 0..7
  123. \param[in] port: GPIO port number 0..7
  124. \param[in] pin: GPIO pin number 0..31
  125. \returns
  126. - \b 0: function succeeded
  127. - \b -1: function failed
  128. */
  129. int32_t SCU_PinInterruptSourceSelect (uint8_t pin_int, uint8_t port, uint8_t pin) {
  130. if ((port > 7) || (pin > 31) || (pin_int > 7)) return -1;
  131. if (pin_int < 4) {
  132. LPC_SCU->PINTSEL0 &= ~(0xFF << (8 * pin_int));
  133. LPC_SCU->PINTSEL0 |= ((pin | (port << 5)) << (8 * pin_int));
  134. } else {
  135. pin_int -= 4;
  136. LPC_SCU->PINTSEL1 &= ~(0xFF << (8 * pin_int));
  137. LPC_SCU->PINTSEL1 |= ((pin | (port << 5)) << (8 * pin_int));
  138. }
  139. return 0;
  140. }