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