新手上路
- 积分
- 33
- 金钱
- 33
- 注册时间
- 2016-7-22
- 在线时间
- 9 小时
|
链接 http://blog.chinaunix.net/uid-12664992-id-300272.html #ifndef __STM_PIO_H
#define __STM_PIO_H
#include "cox_pio.h"
//extern COX_PIO_PI pi_pio;
#endif
/***************************************************************************//**
* @file : stm32_pio.c
* @brief : STM32F1xx CoX PIO Peripheral Interface // 注意看这版本 这是我在网上找的 而我用的是战舰 3.5
* @version : V1.0 // 本人菜鸟对电机了解不深 这段代码看不懂
* @date : 28 Feb. 2011 //求大师能说一下 这中间的配置过程
* @author : CooCox
******************************************************************************/
#include "stm32_pio.h"
#include "stm32f10x.h"
/***************************************************************************//**
* @brief Get pointer to GPIO peripheral due to GPIO port
* @param[in] portNum : Port Number value, should be in range from 0 to 6.
* @return Pointer to GPIO peripheral
*******************************************************************************/
static GPIO_TypeDef* STM32_GetGPIO(uint8_t port)
{
GPIO_TypeDef *pGPIO = COX_NULL;
switch(port)
{
case 0: pGPIO = GPIOA; break;
case 1: pGPIO = GPIOB; break;
case 2: pGPIO = GPIOC; break;
case 3: pGPIO = GPIOD; break;
case 4: pGPIO = GPIOE; break;
case 5: pGPIO = GPIOF; break;
case 6: pGPIO = GPIOG; break;
default: break;
}
return pGPIO;
}
/***************************************************************************//**
* @brief Initializes the PIO peripheral
* @param[in] pio : The specified peripheral
* @return Result, may be :
* -COX_ERROR : Error occurred, parameter is not supported
* -COX_SUCCESS : Previous argument of the specified option
*******************************************************************************/
static COX_Status STM32_PIO_Init (COX_PIO_Dev pio)
{
GPIO_TypeDef* pGPIO = COX_NULL;
uint8_t port, pin;
port = (pio >> 8) & 0xFF;
pin = (pio >> 0) & 0xFF;
pGPIO = STM32_GetGPIO(port);
if(port >6 || port<0 || pin<0 || pin > 15)
return COX_ERROR;
/* Enable GPIO and AFIO clocks */
switch(port)
{
case 0: RCC->APB2ENR |= (RCC_APB2ENR_IOPAEN | RCC_APB2ENR_AFIOEN); break;
case 1: RCC->APB2ENR |= (RCC_APB2ENR_IOPBEN | RCC_APB2ENR_AFIOEN); break;
case 2: RCC->APB2ENR |= (RCC_APB2ENR_IOPCEN | RCC_APB2ENR_AFIOEN); break;
case 3: RCC->APB2ENR |= (RCC_APB2ENR_IOPDEN | RCC_APB2ENR_AFIOEN); break;
case 4: RCC->APB2ENR |= (RCC_APB2ENR_IOPEEN | RCC_APB2ENR_AFIOEN); break;
default: break;
}
return COX_SUCCESS;
}
/***************************************************************************//**
* @brief Set direction (Input or Output)
* @param[in] pio : The specified PIO peripheral
* @param[in] dir : Direction, should be
* -0: Input
* -1: Output
* @return Result, may be :
* -COX_ERROR : Error occurred, parameter is not supported
* -COX_SUCCESS : Previous argument of the specified option
*******************************************************************************/
static COX_Status STM32_PIO_SetDir(COX_PIO_Dev pio, uint8_t dir)
{
GPIO_TypeDef* pGPIO = COX_NULL;
uint8_t port, pin;
port = (pio >> 8) & 0xFF;
pin = (pio >> 0) & 0xFF;
pGPIO = STM32_GetGPIO(port);
/* Direction is input:GPIO_Mode_IN_FLOATING */
if(dir == 0){
if(pin>7)
{
/* Configure the eight high port pins */
pin = pin-8;
/* MODE[1:0]=00 */
pGPIO -> CRH &= ~(0x3 << (pin*4));
/* CNF[1:0] =01 */
pGPIO -> CRH &= ~(0x8 << (pin*4));
pGPIO -> CRH |= (0x4 <<(pin*4));
}
else
{
/* Configure the eight low port pins */
pGPIO -> CRL &= ~(0x3 << (pin*4));
pGPIO -> CRL &= ~(0x8 << (pin*4));
pGPIO -> CRL |= (0x4 << (pin*4));
}
}
/* Direction is output:GPIO_Mode_Out_PP */
else {
if(pin>7)
{
pin = pin-8;
/* MODE[1:0]=11 */
pGPIO -> CRH |= (0x3 <<(pin*4));
/* CNF[1:0] =00 */
pGPIO -> CRH &= ~(0xc <<(pin*4));
}
else
{
pGPIO -> CRL |= (0x3 << (pin*4));
pGPIO -> CRL &= ~(0xc<<(pin*4));
}
}
return COX_SUCCESS;
}
|
|