gpio_lm3s.c
Go to the documentation of this file.00001
00038 #include <cfg/compiler.h>
00039 #include <cfg/debug.h>
00040 #include <io/lm3s.h>
00041 #include "gpio_lm3s.h"
00042
00043
00044 INLINE int lm3s_gpioPinConfigMode(uint32_t port, uint8_t pins, uint32_t mode)
00045 {
00046 if (mode == GPIO_DIR_MODE_IN)
00047 {
00048 HWREG(port + GPIO_O_DIR) &= ~pins;
00049 HWREG(port + GPIO_O_AFSEL) &= ~pins;
00050 }
00051 else if (mode == GPIO_DIR_MODE_OUT)
00052 {
00053 HWREG(port + GPIO_O_DIR) |= pins;
00054 HWREG(port + GPIO_O_AFSEL) &= ~pins;
00055 }
00056 else if (mode == GPIO_DIR_MODE_HW)
00057 {
00058 HWREG(port + GPIO_O_DIR) &= ~pins;
00059 HWREG(port + GPIO_O_AFSEL) |= pins;
00060 }
00061 else
00062 {
00063 ASSERT(0);
00064 return -1;
00065 }
00066 return 0;
00067 }
00068
00069
00070 INLINE int
00071 lm3s_gpioPinConfigStrength(uint32_t port, uint8_t pins, uint32_t strength)
00072 {
00073 if (strength == GPIO_STRENGTH_2MA)
00074 {
00075 HWREG(port + GPIO_O_DR2R) |= pins;
00076 HWREG(port + GPIO_O_DR4R) &= ~pins;
00077 HWREG(port + GPIO_O_DR8R) &= ~pins;
00078 HWREG(port + GPIO_O_SLR) &= ~pins;
00079 }
00080 else if (strength == GPIO_STRENGTH_4MA)
00081 {
00082 HWREG(port + GPIO_O_DR2R) &= ~pins;
00083 HWREG(port + GPIO_O_DR4R) |= pins;
00084 HWREG(port + GPIO_O_DR8R) &= ~pins;
00085 HWREG(port + GPIO_O_SLR) &= ~pins;
00086 }
00087 else if (strength == GPIO_STRENGTH_8MA)
00088 {
00089 HWREG(port + GPIO_O_DR2R) &= ~pins;
00090 HWREG(port + GPIO_O_DR4R) &= ~pins;
00091 HWREG(port + GPIO_O_DR8R) |= pins;
00092 HWREG(port + GPIO_O_SLR) &= ~pins;
00093 }
00094 else if (strength == GPIO_STRENGTH_8MA_SC)
00095 {
00096 HWREG(port + GPIO_O_DR2R) &= ~pins;
00097 HWREG(port + GPIO_O_DR4R) &= ~pins;
00098 HWREG(port + GPIO_O_DR8R) |= pins;
00099 HWREG(port + GPIO_O_SLR) |= pins;
00100 }
00101 else
00102 {
00103 ASSERT(0);
00104 return -1;
00105 }
00106 return 0;
00107 }
00108
00109
00110 INLINE int lm3s_gpioPinConfigType(uint32_t port, uint8_t pins, uint32_t type)
00111 {
00112 if (type == GPIO_PIN_TYPE_STD)
00113 {
00114 HWREG(port + GPIO_O_ODR) &= ~pins;
00115 HWREG(port + GPIO_O_PUR) &= ~pins;
00116 HWREG(port + GPIO_O_PDR) &= ~pins;
00117 HWREG(port + GPIO_O_DEN) |= pins;
00118 HWREG(port + GPIO_O_AMSEL) &= ~pins;
00119 }
00120 else if (type == GPIO_PIN_TYPE_STD_WPU)
00121 {
00122 HWREG(port + GPIO_O_ODR) &= ~pins;
00123 HWREG(port + GPIO_O_PUR) |= pins;
00124 HWREG(port + GPIO_O_PDR) &= ~pins;
00125 HWREG(port + GPIO_O_DEN) |= pins;
00126 HWREG(port + GPIO_O_AMSEL) &= ~pins;
00127 }
00128 else if (type == GPIO_PIN_TYPE_STD_WPD)
00129 {
00130 HWREG(port + GPIO_O_ODR) &= ~pins;
00131 HWREG(port + GPIO_O_PUR) &= ~pins;
00132 HWREG(port + GPIO_O_PDR) |= pins;
00133 HWREG(port + GPIO_O_DEN) |= pins;
00134 HWREG(port + GPIO_O_AMSEL) &= ~pins;
00135 }
00136 else if (type == GPIO_PIN_TYPE_OD)
00137 {
00138 HWREG(port + GPIO_O_ODR) |= pins;
00139 HWREG(port + GPIO_O_PUR) &= ~pins;
00140 HWREG(port + GPIO_O_PDR) &= ~pins;
00141 HWREG(port + GPIO_O_DEN) |= pins;
00142 HWREG(port + GPIO_O_AMSEL) &= ~pins;
00143 }
00144 else if (type == GPIO_PIN_TYPE_OD_WPU)
00145 {
00146 HWREG(port + GPIO_O_ODR) |= pins;
00147 HWREG(port + GPIO_O_PUR) |= pins;
00148 HWREG(port + GPIO_O_PDR) &= ~pins;
00149 HWREG(port + GPIO_O_DEN) |= pins;
00150 HWREG(port + GPIO_O_AMSEL) &= ~pins;
00151 }
00152 else if (type == GPIO_PIN_TYPE_OD_WPD)
00153 {
00154 HWREG(port + GPIO_O_ODR) |= pins;
00155 HWREG(port + GPIO_O_PUR) &= pins;
00156 HWREG(port + GPIO_O_PDR) |= pins;
00157 HWREG(port + GPIO_O_DEN) |= pins;
00158 HWREG(port + GPIO_O_AMSEL) &= ~pins;
00159 }
00160 else if (type == GPIO_PIN_TYPE_ANALOG)
00161 {
00162 HWREG(port + GPIO_O_ODR) &= ~pins;
00163 HWREG(port + GPIO_O_PUR) &= ~pins;
00164 HWREG(port + GPIO_O_PDR) &= ~pins;
00165 HWREG(port + GPIO_O_DEN) &= ~pins;
00166 HWREG(port + GPIO_O_AMSEL) |= pins;
00167 }
00168 else
00169 {
00170 ASSERT(0);
00171 return -1;
00172 }
00173 return 0;
00174 }
00175
00187 int lm3s_gpioPinConfig(uint32_t port, uint8_t pins,
00188 uint32_t mode, uint32_t strength, uint32_t type)
00189 {
00190 int ret;
00191
00192 ret = lm3s_gpioPinConfigMode(port, pins, mode);
00193 if (UNLIKELY(ret < 0))
00194 return ret;
00195 ret = lm3s_gpioPinConfigStrength(port, pins, strength);
00196 if (UNLIKELY(ret < 0))
00197 return ret;
00198 ret = lm3s_gpioPinConfigType(port, pins, type);
00199 if (UNLIKELY(ret < 0))
00200 return ret;
00201 return 0;
00202 }