2015년 12월 25일 금요일

stm32 SPI 예제

#include "stm32f10x.h" //헤더

typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus; //상태 enum
#define SPI2_DR_Address  0x4000380C //spi2 주소
#define BufferSize       32 //버퍼 사이즈
SPI_InitTypeDef  SPI_InitStructure; //spi 구조체
DMA_InitTypeDef  DMA_InitStructure; //DMA 구조체
GPIO_InitTypeDef GPIO_InitStructure; //GPIO 설정 구조체
uint8_t SPI1_Buffer_Tx[BufferSize] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
                                 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12,
                                 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B,
                                 0x1C, 0x1D, 0x1E, 0x1F, 0x20}; //spi1 전송 TX 버퍼
uint8_t SPI2_Buffer_Rx[BufferSize]; //수신 버퍼
uint8_t TxIdx = 0; //아이디
volatile TestStatus TransferStatus = FAILED; //상태 (하드웨어 변경 가능)
void RCC_Configuration(void); //클럭 설정
void GPIO_Configuration(void);//gpio 설정
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength); //두개 버퍼 비교후 true false
                
 int main(void)
{
  RCC_Configuration();//클럭 설정
  GPIO_Configuration();//GPIO설정
  /* DMA1 channel4 configuration ---------------------------------------------*/
  DMA_DeInit(DMA1_Channel4);
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPI2_DR_Address;//실제 SPI 칩의 주소 어떤 정보를 받을지 설정
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPI2_Buffer_Rx;// 실제 저장될 메모리 주소
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//?
  DMA_InitStructure.DMA_BufferSize = BufferSize;//변수에 저장할 메모리 크기 설정
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//미사용
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//메모리 주소를 증가시키면서 다음메모리 정보 사용
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//바이트
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//변수에 저장할 데이타크기 바이트로 설정
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//보통모드
  DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;//우선순위
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//?
  DMA_Init(DMA1_Channel4, &DMA_InitStructure);//저장
  /* SPI1 configuration ------------------------------------------------------*/
  SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx; //단방향 양방향 결정가능
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //마스터모드
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8비트 모드
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;//CPOL 는 리셋 극성 변경 가능
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;//CPHA 는 셋 마스터 클럭의 rising,falling edge 할건지  결정 가능
  SPI_InitStructure.SPI_NSS = SPI_NSS_Hard; //nss 하드웨어 소프트웨어 셜정가능
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; //18MHZ
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //MSB first
  SPI_InitStructure.SPI_CRCPolynomial = 7;//?
  SPI_Init(SPI1, &SPI_InitStructure);//설정완료
  /* SPI2 configuration ------------------------------------------------------*/
  SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Rx; //단방향
  SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;//슬레이브모드
  SPI_Init(SPI2, &SPI_InitStructure);//설정완료

  SPI_SSOutputCmd(SPI1, ENABLE);//아웃풋을 받을거라 설정
  SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, ENABLE); //Tx 도 설정 이 가능하지만 spi2의 RX 사용으로 설정함
  SPI_Cmd(SPI2, ENABLE);//슬레이브 사용
  SPI_Cmd(SPI1, ENABLE);//마스터 사용
  DMA_Cmd(DMA1_Channel4, ENABLE);//DMA 채널 14 사용

  while (TxIdx < BufferSize) //TX갯수가 버퍼 사이즈보다 작으면 반복
  {
   
    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); //마스터의 spi i2s 상태가 리셋이면 계속 spt1 버퍼에 데이터를 저장한다.
  
    SPI_I2S_SendData(SPI1, SPI1_Buffer_Tx[TxIdx++]);
  }//데이터를 보내기 위해 spi1의 버퍼 데이터를 DMA에 저장

  while (!DMA_GetFlagStatus(DMA1_FLAG_TC4));//데이터가 다 보내질 때 가지 기다림
 
  TransferStatus = Buffercmp(SPI2_Buffer_Rx, SPI1_Buffer_Tx, BufferSize); //송신된 버퍼와 수신된 버퍼를 비교한다.
 while (TransferStatus==PASSED)
  {

  GPIOC->BSRR=GPIO_Pin_9;

 }
 
}               
                
void RCC_Configuration(void)
{
 
  SystemInit();//시스템 이닛

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //DMA1 클럭을 사용한다.
 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_SPI1, ENABLE);//72mhz A B SPI1 클럭을 사용
 
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);//36mhz SPI2 클럭을 사용
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

}
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  /* Configure SPI1 pins: NSS, SCK, MISO and MOSI ----------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50mHZ
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //대체 펑션 모드로 설정
  GPIO_Init(GPIOA, &GPIO_InitStructure);//SPI1->A 설정
  /* Configure SPI2 pins: NSS, SCK, MISO and MOSI ----------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  GPIO_Init(GPIOB, &GPIO_InitStructure);//SPI2-> B설정 위와 동일

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

}
                
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)//두개 버퍼 비교후 true false
{
  while (BufferLength--)
  {
    if (*pBuffer1 != *pBuffer2)
    {
      return FAILED;
    }
    pBuffer1++;
    pBuffer2++;
  }
  return PASSED;
}
#ifdef  USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)
{
  while (1)
  {}
}
#endif

2015년 12월 22일 화요일

stm32 RCC 설명

http://lifeseed.tistory.com/62
http://toggleswitch.tistory.com/13

STM32 DMA_ADC 예제

#include "stm32f10x.h"
#include "stm32f10x_adc.h"
#include "stm32f10x_dma.h"
#define ADC1_DR_Address ((uint32_t)0x4001244C)
double x=0, y=0;
GPIO_InitTypeDef GP_C;
ADC_InitTypeDef AD_C; //ADC
DMA_InitTypeDef DM_K; //DMA
 __IO uint32_t ADC_DualConvertedValueTab[2]; //전역변수


void GPIO_Configuration(void);
void ADC_Configuration(void);
int main(void)
{
 GPIO_Configuration();
 ADC_Configuration();

 while(1)
 {
  ADC_ResetCalibration(ADC1);
  while(ADC_GetResetCalibrationStatus(ADC1));
  ADC_StartCalibration(ADC1);
  while(ADC_GetCalibrationStatus(ADC1));
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);//ADC!을 사용하겠다고 알리고 ADC 내부 회로를 초기화 하는 캘리브레이션
  x=ADC_DualConvertedValueTab[0]*3.3/4095;
  if(x>2)
  {
   GPIOC->BSRR=GPIO_Pin_8;
  }
  else
  {
   GPIOC->BRR=GPIO_Pin_8;
  }
  y=ADC_DualConvertedValueTab[1]*3.3/4095;
  if(y>2)
  {
   GPIOC->BSRR=GPIO_Pin_9;
  }
  else
  {
   GPIOC->BRR=GPIO_Pin_9;
  }
 }


}
void ADC_Configuration(void)
{
 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//DMA 사용
 RCC_ADCCLKConfig(RCC_PCLK2_Div6); //clock for ADC (max 14MHz, 72/6=12MHz)
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //enable ADC clock


 DMA_DeInit(DMA1_Channel1);//DMA 설정시작

  DM_K.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;//변수를 통해 ADC의 정보를 받을지 설정
  DM_K.DMA_MemoryBaseAddr = (uint32_t)ADC_DualConvertedValueTab; //저장할 주소 (버퍼)
  DM_K.DMA_DIR = DMA_DIR_PeripheralSRC;
  DM_K.DMA_BufferSize = 2;
  DM_K.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DM_K.DMA_MemoryInc = DMA_MemoryInc_Enable;//여기를 Enable해야 메모리 주소르 증가시키면서 다음메모리에 정보 씀
  DM_K.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; // 32bit
  DM_K.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; // 32bit
  DM_K.DMA_Mode = DMA_Mode_Circular; //정해진 크기의 메모리에 데이터를 쓰면 처음으로 돌아와서 씀
  DM_K.DMA_Priority = DMA_Priority_High;
  DM_K.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel1, &DM_K);//설정 완료
  // Enable DMA1 Channel1
  DMA_Cmd(DMA1_Channel1, ENABLE);//DMA 명령전달
 AD_C.ADC_Mode=ADC_Mode_Independent;
 AD_C.ADC_ScanConvMode=ENABLE;
  AD_C.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
 AD_C.ADC_DataAlign=ADC_DataAlign_Right;
 AD_C.ADC_NbrOfChannel=2;
 ADC_Init(ADC1,&AD_C);

 ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_55Cycles5); //PC1 as Input
  ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 2, ADC_SampleTime_55Cycles5); //PC2 as Input

  ADC_DMACmd(ADC1, ENABLE);

 ADC_Cmd(ADC1, ENABLE);

 ADC_ResetCalibration(ADC1);
  while(ADC_GetResetCalibrationStatus(ADC1));
  ADC_StartCalibration(ADC1);
  while(ADC_GetCalibrationStatus(ADC1));
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);//ADC!을 사용하겠다고 알리고 ADC 내부 회로를 초기화 하는 캘리브레이션

};
void GPIO_Configuration(void)
{
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
 GPIO_StructInit(&GP_C);
 GP_C.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9;
 GP_C.GPIO_Mode=GPIO_Mode_Out_PP;  //GPIO를 아웃풋으로 한다.
 GP_C.GPIO_Speed=GPIO_Speed_2MHz;
 GPIO_Init(GPIOC,&GP_C);


 GP_C.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_2;//데이터 시트안에 있는 ADC 핀에설정해야함.
 GP_C.GPIO_Mode=GPIO_Mode_AIN; // GPIO아날로그 인풋으로 설정
 GPIO_Init(GPIOC,&GP_C);
};

  #ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* Infinite loop */
  while (1)
  {
  }
}
#endif

2015년 12월 11일 금요일

stm32 I2C UART 예제

#include "stm32f10x.h"//헤더
#include "stm32f10x_i2c.h" //헤더
#include "stm32f10x_usart.h"
typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;
#define I2C1_SLAVE_ADDRESS7 0x30//I2c1주소
#define I2C2_SLAVE1_ADDRESS7   0x30//I2c2주소 주소가 있어야 통신이 가능
#define BufferSize 4
#define ClockSpeed 200000
I2C_InitTypeDef I2C_InitStructure; //i2c 설정 구조체
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
uint8_t I2C1_Buffer1_Tx[BufferSize]={1,2,3,4}; //i2c1 버퍼
uint8_t I2C2_Buffer1_Rx[BufferSize];
uint8_t Tx_Idx=0,Rx_Idx=0;
uint8_t countx=0;
char cbuffer[4]={0,};
volatile TestStatus TransferStatus1=FAILED; //하드웨어 값을 폴링할 때 사용가능
void RCC_Configuration(void); //클럭
void GPIO_Configuration(void);//GPIO 설정
void I2C_Configuration(void);
void USART_Configuration(void);
void SerialPutChar(uint8_t c);
void Serial_PutString(uint8_t *s);
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength); //리턴값이 스테이터스인 함수 버퍼 비교

int main(void)
{
 RCC_Configuration(); //클럭 활성화
  GPIO_Configuration();//gp 활성화
 USART_Configuration();
 I2C_Cmd(I2C1, ENABLE); // i2c1 활성화
  I2C_Cmd(I2C2, ENABLE); // i2c2 활성화
 I2C_Configuration();
 
 /*----- First transmission Phase -----*/
  /* Send I2C1 START condition */
  I2C_GenerateSTART(I2C1, ENABLE);//마스터에서 시작
  /* Test on I2C1 EV5 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); //i2c1 레지스터 비우고 이벤트 발생
  /* Send I2C2 slave Address for write */
  I2C_Send7bitAddress(I2C1, I2C2_SLAVE1_ADDRESS7, I2C_Direction_Transmitter);//1에서 2로 보낸다는 7비트 주소 입력
  /* Test on I2C2 EV1 and clear it */
  while(!I2C_CheckEvent(I2C2, I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED));  //2에서 레지스터 비우고 주소 이벤트 체크
  /* Test on I2C1 EV6 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); //1에서 보내기 준비
  /* Send data */
  while (Rx_Idx < BufferSize)
  {
    /* Send I2C1 data */
    I2C_SendData(I2C1, I2C1_Buffer1_Tx[Tx_Idx++]); //1에서 버퍼에있는 내용을 보냄
    /* Test on I2C2 EV2 and clear it */
    while(!I2C_CheckEvent(I2C2, I2C_EVENT_SLAVE_BYTE_RECEIVED));  //2에서 리시브 이벤트
    /* Store received data on I2C2 */
    I2C2_Buffer1_Rx[Rx_Idx++] = I2C_ReceiveData(I2C2); //받은 데이터를 버퍼에 적제
  cbuffer[countx]=I2C2_Buffer1_Rx[countx];
  countx++;
    /* Test on I2C1 EV8 and clear it */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //1에서 보내기 준비
 
  }

  I2C_GenerateSTOP(I2C1, ENABLE); //1에서 스탑
  /* Test on I2C2 EV4 and clear it */
  while(!I2C_CheckEvent(I2C2, I2C_EVENT_SLAVE_STOP_DETECTED));  //2번도 스탑
  /* Clear I2C2 STOPF flag: read operation to I2C_SR1 followed by a
     write operation to I2C_CR1 */
  (void)(I2C_GetFlagStatus(I2C2, I2C_FLAG_STOPF));//플래그 지정
  I2C_Cmd(I2C2, ENABLE);    //2번을 활성화
  /* Check the corectness of written data */
  TransferStatus1 = Buffercmp(I2C1_Buffer1_Tx, I2C2_Buffer1_Rx, BufferSize); //올바로 갔는지 비교
  /* TransferStatus1 = PASSED, if the transmitted and received data
     are equal */
  /* TransferStatus1 = FAILED, if the transmitted and received data
     are different */
 while (1)               
  {
  SerialPutChar(cbuffer[0]);
 SerialPutChar(cbuffer[1]);
 SerialPutChar(cbuffer[2]);
 SerialPutChar(cbuffer[3]);
  }
}
void USART_Configuration(void)
{

 RCC_APB2PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE); //pwr 클럭 생성
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//usart 클럭 생성

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;    /*UART1_TX*/
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;    /*UART1_RX*/
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure); 

 USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
  NVIC_Init( &NVIC_InitStructure );

 USART_Init(USART1, &USART_InitStructure);   
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  USART_Cmd(USART1, ENABLE);
}
void I2C_Configuration(void)
{
   /* I2C1 configuration ------------------------------------------------------*/
  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
  I2C_InitStructure.I2C_OwnAddress1 = I2C1_SLAVE_ADDRESS7;
  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  I2C_InitStructure.I2C_ClockSpeed = ClockSpeed;
  I2C_Init(I2C1, &I2C_InitStructure);//마스터
  /* I2C2 configuration ------------------------------------------------------*/
  I2C_InitStructure.I2C_OwnAddress1 = I2C2_SLAVE1_ADDRESS7;
  I2C_Init(I2C2, &I2C_InitStructure);//술레이브
}
void RCC_Configuration(void) //클럭 설정
{
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//지피 아이오 B 단자의 클럭 활성화을 설정
  /* I2C1 and I2C2 Periph clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1 | RCC_APB1Periph_I2C2, ENABLE);// i2c1 i2c2 클럭 활성화을 설정
}
void GPIO_Configuration(void)//지피아이오 설정함수
{
 GPIO_InitTypeDef GPIO_InitStructure; //지피아이오를 설정한다 I2C1, I2C2를 핀 6,7 | 10,11에 배정한다, 마스터
 /* Configure I2C1 pins: SCL(시리얼 클럭:마스터 단반향) and SDA(시리얼 데이터 양방향) ----------------------------------------*/
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7;//6TX 7RX
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//클럭스피드
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;//alternate fuction 모드로 설정해서 i2c할때 사용한다.
  GPIO_Init(GPIOB, &GPIO_InitStructure);//설정된 값을 이니트
  /* Configure I2C2 pins: SCL and SDA ----------------------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;//이전에 설정한 값을 그대로 따르고 핀만 바꾼다. 슬레이브
  GPIO_Init(GPIOB, &GPIO_InitStructure); //10TX 11 RX

}
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
{
  while(BufferLength--)
  {
    if(*pBuffer1 != *pBuffer2)
    {
      return FAILED;
    }
    pBuffer1++;
    pBuffer2++;
  }
  return PASSED; 
}
void SerialPutChar(uint8_t c)
{
 USART_SendData(USART1,c);
 while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
}
void Serial_PutString(uint8_t *s)
{
 while(*s !='\0')
 {
  SerialPutChar(*s);
  s++;
 }
}
#ifdef  USE_FULL_ASSERT //어썰트 페일이 일어날때 함수
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* Infinite loop */
  while (1)
  {
  }
}
#endif

2015년 12월 10일 목요일

stm32 USART 예제

#include "stm32f10x.h"
#include "stm32f10x_usart.h"
void SerialPutChar(uint8_t c);
void Serial_PutString(uint8_t *s);
int main()
{
 USART_InitTypeDef USART_InitStructure;
 GPIO_InitTypeDef GPIO_InitStructure;
 NVIC_InitTypeDef NVIC_InitStructure;
 GPIO_InitTypeDef PORTC;

 RCC_APB2PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE); //pwr 클럭 생성
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//usart 클럭 생성

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE);

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;    /*UART1_TX*/
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;    /*UART1_RX*/
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure); 
 PORTC.GPIO_Pin = GPIO_Pin_1;
 PORTC.GPIO_Speed = GPIO_Speed_10MHz;
 PORTC.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 GPIO_Init(GPIOC, &PORTC);

 USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
  NVIC_Init( &NVIC_InitStructure );

 USART_Init(USART1, &USART_InitStructure);   
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  USART_Cmd(USART1, ENABLE);
0

 while(1)
 {
  //Serial_PutString("1");
  if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_1 )!=1)
  {
   Serial_PutString("\r\n Hello!\n");
  }

 }
}
void SerialPutChar(uint8_t c)
{
 USART_SendData(USART1,c);
 while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
}
void Serial_PutString(uint8_t *s)
{
 while(*s !='\0')
 {
  SerialPutChar(*s);
  s++;
 }
}

stm32 input 타이머 예제

#include "stm32f10x.h"
volatile unsigned int Timer2_Counter=0;
void init_port()
{
 GPIO_InitTypeDef PORTA;
 GPIO_InitTypeDef PORTB; //GPIO 구조체
 GPIO_InitTypeDef PORTC;
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE); // 클럭수를 배당
 PORTA.GPIO_Pin=GPIO_Pin_0; //핀
 PORTA.GPIO_Mode=GPIO_Mode_AF_PP;// 모드
 PORTA.GPIO_Speed=GPIO_Speed_10MHz;//속도
 GPIO_Init(GPIOA,&PORTA); //GPIOA를 PORTA로 초기화 한다.
 PORTB.GPIO_Pin=GPIO_Pin_0; //핀
 PORTB.GPIO_Mode=GPIO_Mode_AF_PP;// 모드
 PORTB.GPIO_Speed=GPIO_Speed_10MHz;//속도
 GPIO_Init(GPIOB,&PORTB); //GPIOA를 PORTA로 초기화 한다.

 PORTC.GPIO_Pin = GPIO_Pin_0;
 PORTC.GPIO_Speed = GPIO_Speed_10MHz;
 PORTC.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 GPIO_Init(GPIOC, &PORTC);

}
void TIM2_IRQHandler(void) //핸들러 함수에서  TIM_GetITStatus(TIM2,TIM_IT_Update)를 통해서 인터럽트 플래그가  set 되어있는지 확인하고 내가 만든 변수 증가한다.
{
  if(TIM_GetITStatus(TIM2,TIM_IT_Update) != RESET)
    {
        TIM_ClearITPendingBit(TIM2, TIM_IT_Update); // Clear the interrupt flag
        Timer2_Counter++; //카운트를 증가시킨다.
        GPIOB->BRR = GPIO_Pin_0;  // PB0 OFF
    }

    if(TIM_GetITStatus(TIM2,TIM_IT_CC1) != RESET)
    {
        TIM_ClearITPendingBit(TIM2,TIM_IT_CC1);//인터럽트 플래그를 지운다.
        GPIOB->BSRR = GPIO_Pin_0;  // PB0 ON
    }
}
void init_Timer2()
{
   NVIC_InitTypeDef NVIC_InitStructure;// 타이머 인터럽트 컨드롤
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//타이머

    TIM_OCInitTypeDef OutputChannel;   
    
    /* TIM2 Clock Enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//클럭 할당
    
    /* Enable TIM2 Global Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);//NVIC 초기화
    
    /* TIM2 Initialize */  
    TIM_TimeBaseStructure.TIM_Period=100-1; // 100kHz// 주기
    TIM_TimeBaseStructure.TIM_Prescaler=24-1; //
    TIM_TimeBaseStructure.TIM_ClockDivision=0;
    TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);//타임 초기화
    
    /* TIM2 PWM Initialize */
    OutputChannel.TIM_OCMode = TIM_OCMode_PWM1;
    OutputChannel.TIM_OutputState=TIM_OutputState_Enable;
    OutputChannel.TIM_OutputNState=TIM_OutputNState_Enable;
    OutputChannel.TIM_Pulse=50-1; // 50% duty ratio
    OutputChannel.TIM_OCPolarity=TIM_OCPolarity_Low;
    OutputChannel.TIM_OCNPolarity=TIM_OCNPolarity_High;
    OutputChannel.TIM_OCIdleState=TIM_OCIdleState_Set;
    OutputChannel.TIM_OCNIdleState=TIM_OCIdleState_Reset;

    TIM_OC1Init(TIM2,&OutputChannel);//오실리스코프 초기화
    
    /* TIM2 Enale */
    TIM_Cmd(TIM2,ENABLE); //타이머가 가득차면 인터럽트 발생
    TIM_ITConfig(TIM2,TIM_IT_Update | TIM_IT_CC1 ,ENABLE); // interrupt enable
}
void make_pwn(u16 val)
{

    TIM_OCInitTypeDef OutputChannel; //오실리스코프 구조체 할당
    
    OutputChannel.TIM_OCMode = TIM_OCMode_PWM1;
    OutputChannel.TIM_OutputState=TIM_OutputState_Enable;
    OutputChannel.TIM_OutputNState=TIM_OutputNState_Enable;
    OutputChannel.TIM_Pulse=val;// 펄스값이 들어옴 val
    OutputChannel.TIM_OCPolarity=TIM_OCPolarity_Low;
    OutputChannel.TIM_OCNPolarity=TIM_OCNPolarity_High;
    OutputChannel.TIM_OCIdleState=TIM_OCIdleState_Set;
    OutputChannel.TIM_OCNIdleState=TIM_OCIdleState_Reset;
   
  TIM_OC1Init(TIM2,&OutputChannel);// 구조체 할당
}
void delay(unsigned int del) //del 100이므로
{
 Timer2_Counter=0; //전역 변수 초기화
 while(Timer2_Counter<del); //100까지 반복 아마 타이머로인해 카운터가 수시로 증가됨.
}
int main()
{
 u16 i;
 SystemInit(); //시스템 초기화
 init_port(); //출력 포트 초기화
 init_Timer2(); //타이머 초기화 아마 출력포트에 영향
 /*
   if( (!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0 )==1) )
  {
   GPIO_WriteBit(GPIOA,0x0001,1);
   delay(1000);
   GPIO_WriteBit(GPIOA,0x0001,0);
  }else
  {}*/
 
    for(i=0;i<100;i++)
   {
   TIM2->CCR1=i; //튜티비는 TIM_CCR / TIM_ARR 으로 제어 ccr 증가
   delay(100); //딜레이
   }
   for(i=98;i>0;i--)
   {
   TIM2->CCR1=i;
   delay(100);
  
  
   }
  while(1)
  {
   if( (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0 )==0.) )
   {
   for(i=0;i<100;i++)
   {
   TIM2->CCR1=i; //튜티비는 TIM_CCR / TIM_ARR 으로 제어 ccr 증가
   delay(100); //딜레이
   }
   for(i=98;i>0;i--)
   {
   TIM2->CCR1=i;
   delay(100);
  
  
  
   }
   }
  }
 


}

stm32 타이머 예제

#include "stm32f10x.h"
volatile unsigned int Timer2_Counter=0;
void init_port()
{
 GPIO_InitTypeDef PORTA;
 GPIO_InitTypeDef PORTB; //GPIO 구조체
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); // 클럭수를 배당
 PORTA.GPIO_Pin=GPIO_Pin_0; //핀
 PORTA.GPIO_Mode=GPIO_Mode_AF_PP;// 모드
 PORTA.GPIO_Speed=GPIO_Speed_10MHz;//속도
 GPIO_Init(GPIOA,&PORTA); //GPIOA를 PORTA로 초기화 한다.
 PORTB.GPIO_Pin=GPIO_Pin_0; //핀
 PORTB.GPIO_Mode=GPIO_Mode_AF_PP;// 모드
 PORTB.GPIO_Speed=GPIO_Speed_10MHz;//속도
 GPIO_Init(GPIOB,&PORTB); //GPIOA를 PORTA로 초기화 한다.
}
void TIM2_IRQHandler(void) //핸들러 함수에서  TIM_GetITStatus(TIM2,TIM_IT_Update)를 통해서 인터럽트 플래그가  set 되어있는지 확인하고 내가 만든 변수 증가한다.
{
  if(TIM_GetITStatus(TIM2,TIM_IT_Update) != RESET)
    {
        TIM_ClearITPendingBit(TIM2, TIM_IT_Update); // Clear the interrupt flag
        Timer2_Counter++; //카운트를 증가시킨다.
        GPIOB->BRR = GPIO_Pin_0;  // PB0 OFF
    }

    if(TIM_GetITStatus(TIM2,TIM_IT_CC1) != RESET)
    {
        TIM_ClearITPendingBit(TIM2,TIM_IT_CC1);//인터럽트 플래그를 지운다.
        GPIOB->BSRR = GPIO_Pin_0;  // PB0 ON
    }
}
void init_Timer2()
{
   NVIC_InitTypeDef NVIC_InitStructure;// 타이머 인터럽트 컨드롤
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//타이머

    TIM_OCInitTypeDef OutputChannel;   
    
    /* TIM2 Clock Enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//클럭 할당
    
    /* Enable TIM2 Global Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);//NVIC 초기화
    
    /* TIM2 Initialize */  
    TIM_TimeBaseStructure.TIM_Period=100-1; // 100kHz// 주기
    TIM_TimeBaseStructure.TIM_Prescaler=24-1; //
    TIM_TimeBaseStructure.TIM_ClockDivision=0;
    TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);//타임 초기화
    
    /* TIM2 PWM Initialize */
    OutputChannel.TIM_OCMode = TIM_OCMode_PWM1;
    OutputChannel.TIM_OutputState=TIM_OutputState_Enable;
    OutputChannel.TIM_OutputNState=TIM_OutputNState_Enable;
    OutputChannel.TIM_Pulse=50-1; // 50% duty ratio
    OutputChannel.TIM_OCPolarity=TIM_OCPolarity_Low;
    OutputChannel.TIM_OCNPolarity=TIM_OCNPolarity_High;
    OutputChannel.TIM_OCIdleState=TIM_OCIdleState_Set;
    OutputChannel.TIM_OCNIdleState=TIM_OCIdleState_Reset;
 
    TIM_OC1Init(TIM2,&OutputChannel);//오실리스코프 초기화
    
    /* TIM2 Enale */
    TIM_Cmd(TIM2,ENABLE); //타이머가 가득차면 인터럽트 발생
    TIM_ITConfig(TIM2,TIM_IT_Update | TIM_IT_CC1 ,ENABLE); // interrupt enable
}
void make_pwn(u16 val)
{

    TIM_OCInitTypeDef OutputChannel; //오실리스코프 구조체 할당
    
    OutputChannel.TIM_OCMode = TIM_OCMode_PWM1;
    OutputChannel.TIM_OutputState=TIM_OutputState_Enable;
    OutputChannel.TIM_OutputNState=TIM_OutputNState_Enable;
    OutputChannel.TIM_Pulse=val;// 펄스값이 들어옴 val
    OutputChannel.TIM_OCPolarity=TIM_OCPolarity_Low;
    OutputChannel.TIM_OCNPolarity=TIM_OCNPolarity_High;
    OutputChannel.TIM_OCIdleState=TIM_OCIdleState_Set;
    OutputChannel.TIM_OCNIdleState=TIM_OCIdleState_Reset;
   
  TIM_OC1Init(TIM2,&OutputChannel);// 구조체 할당
}
void delay(unsigned int del) //del 100이므로
{
 Timer2_Counter=0; //전역 변수 초기화
 while(Timer2_Counter<del); //100까지 반복 아마 타이머로인해 카운터가 수시로 증가됨.
}
int main()
{
 u16 i;
 SystemInit(); //시스템 초기화
 init_port(); //출력 포트 초기화
 init_Timer2(); //타이머 초기화 아마 출력포트에 영향
 while(1)
 {
  for(i=0;i<100;i++)
  {
   TIM2->CCR1=i; //튜티비는 TIM_CCR / TIM_ARR 으로 제어 ccr 증가
   delay(100); //딜레이
  }
  for(i=98;i>0;i--)
  {
   TIM2->CCR1=i;
   delay(100);
  }
 }
}

2015년 12월 2일 수요일

임베디드 보드 스타터

http://embejide.tistory.com/81

리눅스 명령어

[디렉토리 이동]
pwd :현재 디렉토리 위치 보기
ls  :디렉토리 파일 보기
cd test :test 디렉토리로 이동
cd /home/test : test 디렉토리로 이동
cd .. :상위 디렉토리로 이동


[디렉토리 생성 삭제]


mkdir (디렉토리):디렉토리 생성
mkdir -p 디렉토리/파일명
rmdir (디렉토리):디렉토리 삭제


[파일 삭제]


rm a/test.txt 디렉토리 모든파일 삭제
rm -rf 파일명


[파일 생성]


cat > 파일명
vi 파일명  -> :wq 빠져나오기
nano (파일.c)


[파일 이동 복사]
mv (파일) ./(이동 경로) :현재 디렉토리안의 파일을 이동경로 파일로 이동
mv 이름1 이름2 :파일 이름1을 파일 이름2로 변경
cp 파일 ./이동경로 :현재 디렉토리안의 파일을 이동경로 파일로 복사
cp -rf * dir1/ :현재 디렉토리안의 파일을 모두 복사




[권한]


sudo
sudo su




[gcc 컴파일]


gcc -o (실행파일) 실행파일.c  :컴파일
nano test.c :파일 수정
./실행파일 :실행파일 실행


[마운트]
mount -t ext3 /dev/hda2 /mnt 장치 마운트
unmount /dev/fd0 언마운트
/automount  etc/auto.misc 오토마운트
ps: 실행중인 프로세스 출력


[설치]
$ sudo apt-get install build-essential (설치)