BeRTOS
gpio_stm32.c
Go to the documentation of this file.
00001 
00038 #include "gpio_stm32.h"
00039 
00040 #include <cfg/compiler.h>
00041 #include <cfg/debug.h>
00042 
00043 #include <io/stm32.h>
00044 
00045 
00056 int stm32_gpioPinConfig(struct stm32_gpio *base,
00057             uint16_t pins, uint8_t mode, uint8_t speed)
00058 {
00059     uint32_t reg_mode = mode & 0x0f;
00060     int i;
00061 
00062     if (mode & 0x10)
00063         reg_mode |= speed;
00064 
00065     if (pins & 0xff)
00066     {
00067         uint32_t reg = base->CRL;
00068 
00069         for (i = 0; i < 8; i++)
00070         {
00071             uint32_t pos = 1 << i;
00072 
00073             if (pins & pos)
00074             {
00075                 pos = i << 2;
00076                 reg &= ~(0x0f << pos);
00077                 reg |= reg_mode << pos;
00078 
00079                 if (mode == GPIO_MODE_IPD)
00080                     base->BRR = 0x01 << i;
00081                 if (mode == GPIO_MODE_IPU)
00082                     base->BSRR = 0x01 << i;
00083             }
00084         }
00085         base->CRL = reg;
00086     }
00087     if (pins > 0xff)
00088     {
00089         uint32_t reg = base->CRH;
00090 
00091         for (i = 0; i < 8; i++)
00092         {
00093             uint32_t pos = 1 << (i + 8);
00094 
00095             if (pins & pos)
00096             {
00097                 pos = i << 2;
00098                 reg &= ~(0x0f << pos);
00099                 reg |= reg_mode << pos;
00100 
00101                 if (mode == GPIO_MODE_IPD)
00102                     base->BRR = 0x01 << (i + 8);
00103                 if (mode == GPIO_MODE_IPU)
00104                     base->BSRR = 0x01 << (i + 8);
00105             }
00106         }
00107         base->CRH = reg;
00108     }
00109     return 0;
00110 }