책을 읽다보니 후에 GPIO_Init설명이 나왔다... 책을 순서대로 학습한다면 지금부터 쓰는거였다.
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
{
uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;
uint32_t tmpreg = 0x00, pinmask = 0x00;
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
/*---------------------------- GPIO Mode Configuration -----------------------*/
currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);
if ((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00)
{
/* Check the parameters */
assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
/* Output mode */
currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;
}
1줄 : input mode, output mode 판별. 0x0F를 이용하여 하위 4비트의 값만 추출
2줄 : mode가 0x00이 아니면 output모드
typedef enum
{ GPIO_Mode_AIN = 0x0,
GPIO_Mode_IN_FLOATING = 0x04,
GPIO_Mode_IPD = 0x28,
GPIO_Mode_IPU = 0x48,
GPIO_Mode_Out_OD = 0x14,
GPIO_Mode_Out_PP = 0x10,
GPIO_Mode_AF_OD = 0x1C,
GPIO_Mode_AF_PP = 0x18
}GPIOMode_TypeDef;
/*---------------------------- GPIO CRL Configuration ------------------------*/
/* Configure the eight low port pins */
if (((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x00FF)) != 0x00) //CRL인지 확인
{
tmpreg = GPIOx->CRL;
for (pinpos = 0x00; pinpos < 0x08; pinpos++) //어느위치 설정값인지 확인(앞의 학습에서 했음)
{
pos = ((uint32_t)0x01) << pinpos;
/* Get the port pins position */
currentpin = (GPIO_InitStruct->GPIO_Pin) & pos; //맞는 핀 위치 확인
if (currentpin == pos)
{
pos = pinpos << 2; //CRL은 4비트 단위로 값 담당. pinpos*4로 위치값 구함(2만큼 왼쪽으로 shift)
/* Clear the corresponding low control register bits */
pinmask = ((uint32_t)0x0F) << pos;
tmpreg &= ~pinmask; //원하는 부분만 0으로 만든값을 tmpreg에 and함. 원하는 부분만 0으로 변함
/* Write the mode configuration in the corresponding bits */
tmpreg |= (currentmode << pos); //현재 설정값을 원하는 위치에 shift시켜 or시켜 적용
/* Reset the corresponding ODR bit */
//CRL,CRH가 아닌 BRR.BSRR레지스터 설정
//input mode에서 pull-down, pull-up으로 설정할 경우, 그 GPIO에 값이 설정되어야한 정상적으로 설정 가능.
//즉, 해당 작업을 밖에서 설정하는것이 아니라, GPIO초기화 과정에서 한번에 처리
//Pull-down : Reset(0), Pull-up : Set(1)
if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
{
GPIOx->BRR = (((uint32_t)0x01) << pinpos);
}
else
{
/* Set the corresponding ODR bit */
if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
{
GPIOx->BSRR = (((uint32_t)0x01) << pinpos);
}
}
}
}
GPIOx->CRL = tmpreg;
}
/*---------------------------- GPIO CRH Configuration ------------------------*/
/* Configure the eight high port pins */
if (GPIO_InitStruct->GPIO_Pin > 0x00FF)
{
tmpreg = GPIOx->CRH;
for (pinpos = 0x00; pinpos < 0x08; pinpos++)
{
pos = (((uint32_t)0x01) << (pinpos + 0x08));
/* Get the port pins position */
currentpin = ((GPIO_InitStruct->GPIO_Pin) & pos);
if (currentpin == pos)
{
pos = pinpos << 2;
/* Clear the corresponding high control register bits */
pinmask = ((uint32_t)0x0F) << pos;
tmpreg &= ~pinmask;
/* Write the mode configuration in the corresponding bits */
tmpreg |= (currentmode << pos);
/* Reset the corresponding ODR bit */
if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
{
GPIOx->BRR = (((uint32_t)0x01) << (pinpos + 0x08));
}
/* Set the corresponding ODR bit */
if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
{
GPIOx->BSRR = (((uint32_t)0x01) << (pinpos + 0x08));
}
}
}
GPIOx->CRH = tmpreg;
}
}
'중도연재종료 > CORTEX M3' 카테고리의 다른 글
UART_ Hello_Wolrd 찍기_2 (0) | 2013.06.08 |
---|---|
UART_ Hello World 찍기_1 (0) | 2013.06.08 |
Key가 눌린것 알아채기 (0) | 2013.05.24 |
고급스러운 코딩1. (0) | 2013.05.24 |
LED 끄기[GPIO Set Reset 레지스터] (0) | 2013.05.24 |
P213
참고. STM32_Reference_manual. p140의 table 17, 18
직접 만드는것도 연습이 되나, 되도록 있는 함수를 이용하도록한다. 새로 만든 함수와 기존 함수를 병행하면 헷갈리기 쉽기때문에 본래 있는 함수에 익숙하도록 하자.
P216의 GPIO_Port_Init의 대체함수로 GPIO_Configuration을 이용하자.
아래와 같다.
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_0+GPIO_Pin_1+GPIO_Pin_4);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
위의 함수들은 본래 주어진다.
BitAction함수도 본래 주어진다.
GPIO_ReadInputDataBit도 주어진다.
GPIO_TypeDef->IDR
:_IO uint32_t IDR
:0~15번 비트까지 0번 핀에서 15번 핀까지의 입력 데이터의 값을 가지고 있다.
GPIO_Pin은 사용하는 핀에 대하여 1이 들어가 있고<핀정보> IDR에 이를 AND함으로써 데이터값이 1이 들어와 있는 핀에 대해서는 1을 반납한다. 물론 IDR에는 복수핀에 대한 데이터 정보로 되어있고 GPIO_Pin은 단일도 복수도 가능하다. 단일일 경우 해당핀만 조건을 만족하면 되고, 복수이면 여러핀이 조건을 만족해야 한다. Bit_RESET, SET은 각각 0,1이다. 32비트로 처리한다. 반납값 bitstatus는 8비트로 0,1표현
ex)
IDR : 0b1011110111110001
PIN : 0b1001001100101100
& : 0b1001000100100000
rese: 0b0000000000000000
테스트 파일. LED3개를 이용하여 깜빡임. 스위치 하나.
Key_Test.zip'중도연재종료 > CORTEX M3' 카테고리의 다른 글
UART_ Hello World 찍기_1 (0) | 2013.06.08 |
---|---|
GPIO_Init (0) | 2013.05.25 |
고급스러운 코딩1. (0) | 2013.05.24 |
LED 끄기[GPIO Set Reset 레지스터] (0) | 2013.05.24 |
LED회로도를 통한 기본적인 이해 (0) | 2013.05.22 |