Beispiel #1
0
uint8_t TW88_Init(void)
{
  //i2c init
  GPIO_InitTypeDef GPIO_InitStructure;
  I2C_InitTypeDef I2C_InitStructure;
  
  //init now the TW88 chip on I2C 1
	
	//I2C init ...on PB6 And PB7
	
	/* Configure the I2C clock source. The clock is derived from the SYSCLK */
  RCC_I2CCLKConfig(RCC_I2C1CLK_HSI);													

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB , ENABLE);
	

	

	
	//init our serial port on UART2
//	SerialPort_Init(115200,1);
	
	
//	printf("\n\r ***** COEN490/ELEC490 Capstone Project Team 14 Welcome! ***** \n\r");
	
	
	I2C_DeInit(I2C1);
	I2C_StructInit(&I2C_InitStructure);
	I2C_InitStructure.I2C_Mode=I2C_Mode_I2C;
	I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
	I2C_InitStructure.I2C_DigitalFilter = 0x00;
	I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
	I2C_InitStructure.I2C_OwnAddress1 = 0x00;
	I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	//I2C_InitStructure.I2C_Timing = 0x50330309; //0xB0420F13;    ///0x00902025; // 0xc062121f
	I2C_InitStructure.I2C_Timing = 0x00310309; 
	I2C_Init(I2C1,&I2C_InitStructure);
	I2C_Cmd(I2C1,ENABLE);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_4);  // SCL
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_4);	// SDA


  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;


  GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_6);	
  GPIO_Init(GPIOB, &GPIO_InitStructure);		

	GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_7);
	GPIO_Init(GPIOB, &GPIO_InitStructure);	

	
	delay(500);	
  
  
  
  
  
  
  
	//first thing to do would be to make sure that we are on first page
	while (TW88Write(0xff,0x00)==0) {
			//now we detected an error ... we shall resend ?
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
    delay(50);
	}
  
  while (TW88Write(0x06,0x80)==0) { // force reset
      I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
  }
  
	while (TW88Write(0x1C, 0x0F)==0) { // 0F
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	
	while (TW88Write(0x02,0x40)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	
	while (TW88Write(0x04,0x00)==0) { //00
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x06,0x03)==0) { // 03
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x07,0x02)==0) { // 02
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x08,0x00)==0) { // 00
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x09,0xF0)==0) { // F0
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
			
	}
	while (TW88Write(0x0A,0x19)==0) { // 19
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x0B,0xD0)==0) { // D0
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x0C,0xDC)==0) { // DC
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x0D,0x15)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x10,0x40)==0) { // brightness 00
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	while (TW88Write(0x11,0x80)==0) { // contrast 50
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	while (TW88Write(0x12,0x71)==0) { //sharpness 71
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x13,0x9E)==0) { //chroma U 9E
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x14,0x9E)==0) {  //chroma V 9E
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x15,0x00)==0) {  //hue 
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	while (TW88Write(0x17,0x33)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	while (TW88Write(0x18,0x44)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x1C,0x0F)==0) { // Standard Selection 0x08=NTSC 0x0F=all
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x1D,0x7F)==0) { // 7F
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	
	while (TW88Write(0x1E,0x08)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x1F,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x20,0x50)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	
		while (TW88Write(0x21,0x42)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x22,0xF0)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	
	while (TW88Write(0x23,0xD8)==0) { // D8
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	
	while (TW88Write(0x24,0xBC)==0) { // BC
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x25,0xB8)==0) { // B8
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x26,0x44)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x27,0x2a)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x28,0x00)==0) { // 00
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x29,0x15)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x2A,0x78)==0) { // 78
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x2B,0x44)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x2C,0x30)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x2D,0x14)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x2E,0xA5)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
				
		while (TW88Write(0x2F,0x00)==0) { // E0
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x33,0x85)==0) { // 85
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	while (TW88Write(0x34,0x1A)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x35,0x40)==0) { // 00 CLAMPING CONTROL ENABLE
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0x38,0x80)==0) { // 8E
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x40,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x41,0x20)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x42,0x04)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x43,0x22)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x44,0x08)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0x45,0x54)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0x46,0x20)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0x47,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0x48,0xCF)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0x49,0x20)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	
	while (TW88Write(0x4A,0x13)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	
	while (TW88Write(0x4B,0x13)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	
	while (TW88Write(0x4C,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x4D,0x30)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x4E,0x64)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x4F,0x1E)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x50,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x60,0xE0)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x61,0x80)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	
	while (TW88Write(0x62,0x7C)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x63,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	
	while (TW88Write(0x64,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x65,0x80)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x66,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x67,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x68,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x69,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x6A,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	
	while (TW88Write(0x6B,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x6C,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
		while (TW88Write(0x6D,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x6E,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	
	while (TW88Write(0x70,0x60)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x71,0x60)==0) { 
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	while (TW88Write(0x72,0x80)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x73,0x80)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	while (TW88Write(0x74,0x60)==0) { ///brightness
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	while (TW88Write(0x75,0x80)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	while (TW88Write(0x76,0x80)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x77,0x03)==0) { // 03
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x78,0x3B)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x7C,0x1C)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0x7D,0x08)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x7E,0xF6)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x7F,0x08)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x80,0x10)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x81,0x1D)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x82,0x03)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x83,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x84,0x67)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x85,0x94)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x86,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0x87,0xFF)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x88,0xCA)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0x89,0x02)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0xB0,0x40)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0xB1,0x02)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xB2,0xDB)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xB3,0x14)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xB4,0x10)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xB5,0x20)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xB6,0x33)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	
	while (TW88Write(0xBB,0x12)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	
	while (TW88Write(0xB7,0x0D)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xB8,0x04)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xB9,0x08)==0) {//back porch value
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xBA,0xE0)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	

	while (TW88Write(0xBC,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	
	while (TW88Write(0xBD,0x08)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	while (TW88Write(0xBE,0xC2)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	
	
	while (TW88Write(0xBF,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}		
	
	while (TW88Write(0xC0,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	
	while (TW88Write(0xC1,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	

	while (TW88Write(0xC2,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xC3,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xC4,0x99)==0) { // PWM setup and duty cycle default 0x40 
		// 0xBF - 50% duty cycle - 0x99 - 20% duty cycle
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xC6,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xC7,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xC8,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xC9,0xED)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xCA,0xE7)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xD2,0xFF)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xD3,0x07)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xD4,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xD5,0x3F)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0xD6,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0xD7,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xD8,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0xDA,0x3D)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0xDB,0xC3)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xDC,0xFC)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xDD,0x70)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0xDE,0x6F)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	
	while (TW88Write(0xDF,0x6F)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xE0,0x10)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xF0,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xF4,0x16)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xF6,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xF7,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xF8,0xA0)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xF9,0x01)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xFA,0x30)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xFB,0x00)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
	while (TW88Write(0xFC,0x48)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	
	while (TW88Write(0xFD,0x30)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xFE,0x51)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	
	while (TW88Write(0xFF,0x01)==0) { // 2nd page
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
	while (TW88Write(0xC0,0x01)==0) {
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}
  
	while (TW88Write(0xCC,0x00)==0) { // 00
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}

	while (TW88Write(0xFF,0x00)==0) { // go back to 1st page
			I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
	}	
  
  while (TW88Write(0x06,0x80)==0) { // force reset
      I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
  }
  delay(10);
  
    while (TW88Write(0x06,0x01)==0) { // rewrite this reg
      I2CReset(I2C1,GPIOB,GPIO_Pin_6,GPIO_Pin_7);
  }
  
		
	return 1;
}
Beispiel #2
0
void init_I2C1(void){

		/* This is a concept that has to do with the libraries provided by ST
		* to make development easier the have made up something similar to
		* classes, called TypeDefs, which actually just define the common
		* parameters that every peripheral needs to work correctly
		*
		* They make our life easier because we don't have to mess around with
		* the low level stuff of setting bits in the correct registers
		*/
		GPIO_InitTypeDef GPIO_InitStruct; // this is for the GPIO pins used as I2C1SDA and I2C1SCL
		GPIO_InitTypeDef GPIO_Output;
		I2C_InitTypeDef I2C_InitStruct; // this is for the I2C1 initilization

		/* enable APB1 peripheral clock for I2C1 */
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C3, ENABLE);//zmianaRCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

		/* enable the peripheral clock for the pins used by
		* I2C1, PB6 for I2C SCL and PB9 for I2C1_SDL
		*/
		/* GPIOB clock enable */
		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
		//RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

		/* This sequence sets up the I2C1SDA and I2C1SCL pins
		* so they work correctly with the I2C1 peripheral
		*/
		GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8; // Pins 6 (I2C1_SCL) and 9 (I2C1_SDA) are used
		GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; 			// the pins are configured as alternate function so the USART peripheral has access to them
		GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;		// this defines the IO speed and has nothing to do with the baudrate!
		GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;			// this defines the output type as open drain mode (as opposed to push pull)
		GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;			// this activates the pullup resistors on the IO pins
		GPIO_Init(GPIOA, &GPIO_InitStruct);					// now all the values are passed to the GPIO_Init() function which sets the GPIO registers


		//nowe
		//RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

		/* This sequence sets up the I2C1SDA and I2C1SCL pins
		* so they work correctly with the I2C1 peripheral
		*/
		GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9; // Pins 6 (I2C1_SCL) and 9 (I2C1_SDA) are used
		GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; 			// the pins are configured as alternate function so the USART peripheral has access to them
		GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;		// this defines the IO speed and has nothing to do with the baudrate!
		GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;			// this defines the output type as open drain mode (as opposed to push pull)
		GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;			// this activates the pullup resistors on the IO pins
		GPIO_Init(GPIOC, &GPIO_InitStruct);					// now all the values are passed to the GPIO_Init() function which sets the GPIO registers

		//nowe
		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

		/* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
		GPIO_Output.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
		GPIO_Output.GPIO_Mode = GPIO_Mode_OUT;
		GPIO_Output.GPIO_OType = GPIO_OType_PP;
		GPIO_Output.GPIO_Speed = GPIO_Speed_100MHz;
		GPIO_Output.GPIO_PuPd = GPIO_PuPd_NOPULL;
		GPIO_Init(GPIOD, &GPIO_Output);


		/* The I2C1_SCL and I2C1_SDA pins are now connected to their AF
		* so that the USART1 can take over control of the
		* pins
		*/
		GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_I2C3); //
		GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_I2C3);

		/* Configure I2C1 */
		/* I2C DeInit */
		I2C_DeInit(I2C3);

		/* Enable the I2C peripheral */
		I2C_Cmd(I2C3, ENABLE);

		/* Set the I2C structure parameters */
		I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
		I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
		I2C_InitStruct.I2C_OwnAddress1 = 0xEE;
		I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
		I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
		I2C_InitStruct.I2C_ClockSpeed = 30000;

		/* Initialize the I2C peripheral w/ selected parameters */
		I2C_Init(I2C3, &I2C_InitStruct);
}
Beispiel #3
0
//############################################################################
//Hauptprogramm
int main (void)
//############################################################################
{
	unsigned int timer,i,timer2 = 0, timerPolling;

    DDRB  = 0x00;
    PORTB = 0x00;
    for(timer = 0; timer < 1000; timer++); // verzögern
#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
	PlatinenVersion = 21;
#else
	if(PINB & 0x01)
     {
      if(PINB & 0x02) PlatinenVersion = 13;
       else           PlatinenVersion = 11;
     }
    else
     {
      if(PINB & 0x02) PlatinenVersion = 20;
       else           PlatinenVersion = 10;
     }
#endif
    DDRC  = 0x81; // SCL
    DDRC  |=0x40; // HEF4017 Reset
    PORTC = 0xff; // Pullup SDA
    DDRB  = 0x1B; // LEDs und Druckoffset
    PORTB = 0x01; // LED_Rot
    DDRD  = 0x3E; // Speaker & TXD & J3 J4 J5
	PORTD = 0x47; // LED
    HEF4017R_ON;
    MCUSR &=~(1<<WDRF);
    WDTCSR |= (1<<WDCE)|(1<<WDE);
    WDTCSR = 0;

    beeptime = 2500;
	StickGier = 0; PPM_in[K_GAS] = 0; StickRoll = 0; StickNick = 0;
    if(PlatinenVersion >= 20)  GIER_GRAD_FAKTOR = 1220; else GIER_GRAD_FAKTOR = 1291; // unterschiedlich für ME und ENC
    ROT_OFF;

    Timer_Init();
	TIMER2_Init();
	UART_Init();
    rc_sum_init();
   	ADC_Init();
	I2C_Init(1);
	SPI_MasterInit();
	Capacity_Init();
	LIBFC_Init();
	GRN_ON;
    sei();
	ParamSet_Init();


// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Check connected BL-Ctrls
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	// Check connected BL-Ctrls
	BLFlags |= BLFLAG_READ_VERSION;
	motor_read = 0;  // read the first I2C-Data
	SendMotorData();
	timer = SetDelay(500);
	while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer

    printf("\n\rFound BL-Ctrl: ");
    timer = SetDelay(4000);
	for(i=0; i < MAX_MOTORS; i++)
	{
		SendMotorData();
		while(!(BLFlags & BLFLAG_TX_COMPLETE)  && !CheckDelay(timer)); //wait for complete transfer
		if(Mixer.Motor[i][0] > 0) // wait max 4 sec for the BL-Ctrls to wake up
		{
			while(!CheckDelay(timer) && !(Motor[i].State & MOTOR_STATE_PRESENT_MASK) )
			{
				SendMotorData();
				while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer
			}
		}
		if(Motor[i].State & MOTOR_STATE_PRESENT_MASK)
		{
			printf("%d",i+1);
			FoundMotors++;
//			if(Motor[i].Version & MOTOR_STATE_NEW_PROTOCOL_MASK) printf("(new) ");
		}
	}
	for(i=0; i < MAX_MOTORS; i++)
	{
		if(!(Motor[i].State & MOTOR_STATE_PRESENT_MASK) && Mixer.Motor[i][0] > 0)
		{
			printf("\n\r\n\r!! MISSING BL-CTRL: %d !!",i+1);
			ServoActive = 2; // just in case the FC would be used as camera-stabilizer
		}
		Motor[i].State &= ~MOTOR_STATE_ERROR_MASK; // clear error counter
	}
 	printf("\n\r===================================");

    if(RequiredMotors < FoundMotors) VersionInfo.HardwareError[1] |= FC_ERROR1_MIXER;

	//if(EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG)
	{
		printf("\n\rCalibrating pressure sensor..");
		timer = SetDelay(1000);
		SucheLuftruckOffset();
		while (!CheckDelay(timer));
		printf("OK\n\r");
	}

	SetNeutral(0);

	ROT_OFF;

    beeptime = 2000;
    ExternControl.Digital[0] = 0x55;


	FlugMinuten = (unsigned int)GetParamByte(PID_FLIGHT_MINUTES) * 256 + (unsigned int)GetParamByte(PID_FLIGHT_MINUTES + 1);
	FlugMinutenGesamt = (unsigned int)GetParamByte(PID_FLIGHT_MINUTES_TOTAL) * 256 + (unsigned int)GetParamByte(PID_FLIGHT_MINUTES_TOTAL + 1);

	if((FlugMinutenGesamt == 0xFFFF) || (FlugMinuten == 0xFFFF))
	{
		FlugMinuten = 0;
		FlugMinutenGesamt = 0;
	}
    printf("\n\rFlight-time %u min  Total:%u min", FlugMinuten, FlugMinutenGesamt);

	printf("\n\rControl: ");
	if (EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) printf("HeadingHold");
	else printf("Normal (ACC-Mode)");

    LcdClear();
    I2CTimeout = 5000;
    WinkelOut.Orientation = 1;
    LipoDetection(1);

	LIBFC_ReceiverInit(EE_Parameter.Receiver);

	printf("\n\r===================================\n\r");
	//SpektrumBinding();
    timer = SetDelay(2000);
	timerPolling = SetDelay(250);

	Debug(ANSI_CLEAR "FC-Start!\n\rFlugzeit: %d min", FlugMinutenGesamt);  	// Note: this won't waste flash memory, if #DEBUG is not active
    DebugOut.Status[0] = 0x01 | 0x02;
	JetiBeep = 0;
	while (1)
	{
	
	if (JetiUpdateModeActive) while (1);
	
	if(CheckDelay(timerPolling))
	{
	  timerPolling = SetDelay(100);
	  LIBFC_Polling();
	}
	if(UpdateMotor && AdReady)      // ReglerIntervall
            {
  		    UpdateMotor=0;
            if(WinkelOut.CalcState) CalMk3Mag();
            else  MotorRegler();
			SendMotorData();
            ROT_OFF;
            if(SenderOkay)  { SenderOkay--; VersionInfo.HardwareError[1] &= ~FC_ERROR1_PPM; }
			else
			{
				TIMSK1 |= _BV(ICIE1); // enable PPM-Input
				PPM_in[0] = 0; // set RSSI to zero on data timeout
				VersionInfo.HardwareError[1] |= FC_ERROR1_PPM;
			}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//if(HoehenReglerAktiv && NaviDataOkay && SenderOkay < 160 && SenderOkay > 10 && FromNaviCtrl_Value.SerialDataOkay > 220) SenderOkay = 160;
//if(HoehenReglerAktiv && NaviDataOkay && SenderOkay < 101 && SenderOkay > 10 && FromNaviCtrl_Value.SerialDataOkay > 1) SenderOkay = 101;
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            if(!--I2CTimeout || MissingMotor)
                {
                  if(!I2CTimeout)
				   {
				    I2C_Reset();
                    I2CTimeout = 5;
					DebugOut.Analog[28]++; // I2C-Error
					VersionInfo.HardwareError[1] |= FC_ERROR1_I2C;
					DebugOut.Status[1] |= 0x02; // BL-Error-Status
  				   }
                  if((BeepMuster == 0xffff) && MotorenEin)
                   {
                    beeptime = 10000;
                    BeepMuster = 0x0080;
                   }
                }
            else
                {
                 ROT_OFF;
				 if(!beeptime)
				  {
				   VersionInfo.HardwareError[1] &= ~FC_ERROR1_I2C;
				  }
                }
          if(!UpdateMotor)
		   {
			if(CalculateServoSignals) CalculateServo();
			DatenUebertragung();
			BearbeiteRxDaten();
			if(CheckDelay(timer))
			{
				static unsigned char second;
				timer += 20; // 20 ms interval
				if(MissingMotor)
				 {
				  VersionInfo.HardwareError[1] |= FC_ERROR1_BL_MISSING;
				  DebugOut.Status[1] |= 0x02; // BL-Error-Status
				 }
				 else
				 {
				   VersionInfo.HardwareError[1] &= ~FC_ERROR1_BL_MISSING;
				   if(I2CTimeout > 6) DebugOut.Status[1] &= ~0x02; // BL-Error-Status
				 }

			    if(I2CTimeout > 6) VersionInfo.HardwareError[1] &= ~FC_ERROR1_I2C;

				if(PcZugriff) PcZugriff--;
				else
				{
					ExternControl.Config = 0;
					ExternStickNick = 0;
					ExternStickRoll = 0;
					ExternStickGier = 0;
					if(BeepMuster == 0xffff && SenderOkay == 0)
					{
						beeptime = 15000;
						BeepMuster = 0x0c00;
					}
				}
				if(NaviDataOkay > 200)
				{
					NaviDataOkay--;
					VersionInfo.HardwareError[1] &= ~FC_ERROR1_SPI_RX;
				}
				else
				{
					if(NC_Version.Compatible)
					 {
						VersionInfo.HardwareError[1] |= FC_ERROR1_SPI_RX;
                       if(BeepMuster == 0xffff && MotorenEin)
						{
							beeptime = 15000;
							BeepMuster = 0xA800;
						}
					 }
					GPS_Nick = 0;
					GPS_Roll = 0;
					//if(!beeptime)
                    FromNaviCtrl.CompassValue = -1;
                    NaviDataOkay = 0;
				}
			   if(UBat < BattLowVoltageWarning)
				{
					FC_StatusFlags |= FC_STATUS_LOWBAT;
					if(BeepMuster == 0xffff)
					{
						beeptime = 6000;
						BeepMuster = 0x0300;
					}
				}
				else if(!beeptime) FC_StatusFlags &= ~FC_STATUS_LOWBAT;

				SPI_StartTransmitPacket();
				SendSPI = 4;
				if(!MotorenEin) timer2 = 1450; // 0,5 Minuten aufrunden
				else
                if(++second == 49)
				 {
				   second = 0;
				   FlugSekunden++;
				 }
				if(++timer2 == 2930)  // eine Minute
				 {
				   timer2 = 0;
               	   FlugMinuten++;
	               FlugMinutenGesamt++;
                   SetParamByte(PID_FLIGHT_MINUTES,FlugMinuten / 256);
                   SetParamByte(PID_FLIGHT_MINUTES+1,FlugMinuten % 256);
                   SetParamByte(PID_FLIGHT_MINUTES_TOTAL,FlugMinutenGesamt / 256);
                   SetParamByte(PID_FLIGHT_MINUTES_TOTAL+1,FlugMinutenGesamt % 256);
				   timer = SetDelay(20); // falls "timer += 20;" mal nicht geht
	  		     }
			}
           LED_Update();
           Capacity_Update();
           } //else DebugOut.Analog[26]++;
          }
     if(!SendSPI) { SPI_TransmitByte(); }
    }
 return (1);
}
Beispiel #4
0
void I2C_LowLevel_Init(I2C_TypeDef* I2Cx, int ClockSpeed, int OwnAddress)
{

	GPIO_InitTypeDef  GPIO_InitStructure;
	I2C_InitTypeDef  I2C_InitStructure;

	// Enable GPIOB clocks
	//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

	// Configure I2C clock and GPIO 
	GPIO_StructInit(&GPIO_InitStructure);
	if (I2Cx == I2C1){

		/* I2C1 clock enable */
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

		/* I2C1 SDA and SCL configuration */
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
		GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
		GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
		GPIO_Init(GPIOB, &GPIO_InitStructure);

		GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);
		GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1);

		/* I2C1 Reset */
		//RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
		//RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);

	} else {

		/* I2C2 clock enable */
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);

		/* I2C1 SDA and SCL configuration */
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
		GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
		GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
		GPIO_Init(GPIOB, &GPIO_InitStructure);

		GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_I2C2);
		GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_I2C2);

		/* I2C2  Reset */
		//RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
		//RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);

	}

	/* Configure I2Cx                */
	I2C_StructInit(&I2C_InitStructure);
	I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
	I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
	I2C_InitStructure.I2C_OwnAddress1 = OwnAddress;
	I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
	I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	I2C_InitStructure.I2C_ClockSpeed = ClockSpeed;
	
	I2C_Init(I2Cx, &I2C_InitStructure);
	I2C_Cmd(I2Cx, ENABLE); 

}
Beispiel #5
0
/*********************************************************************//**
 * @brief		c_entry: Main program body
 * @param[in]	None
 * @return 		int
 **********************************************************************/
int c_entry(void)
{
	PINSEL_CFG_Type PinCfg;
	uint8_t idx,i;
	/* Initialize debug via UART0
	 * – 115200bps
	 * – 8 data bit
	 * – No parity
	 * – 1 stop bit
	 * – No flow control
	 */
	debug_frmwrk_init();

	//print menu screen
	print_menu();

	/* I2C block ------------------------------------------------------------------- */

	/*
	 * Init I2C pin connect
	 */
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Funcnum = 1;
	PinCfg.Pinnum = 27;
	PinCfg.Portnum = 0;
	PINSEL_ConfigPin(&PinCfg);//SDA0
	PinCfg.Pinnum = 28;
	PINSEL_ConfigPin(&PinCfg);//SCL0

	// Initialize I2C peripheral
	I2C_Init(I2CDEV, 100000);

	/* Configure interrupt for I2C in NVIC of ARM core */
    /* Disable I2C0 interrupt */
    NVIC_DisableIRQ(I2C0_IRQn);
    /* preemption = 1, sub-priority = 0 */
    NVIC_SetPriority(I2C0_IRQn, ((0x01<<3)|0x01));
    //enable I2C interrupt
    I2C_IntCmd(LPC_I2C0, ENABLE);

	/* Enable I2C operation */
	I2C_Cmd(I2CDEV, ENABLE);

	while(1)
	{
		idx=0;count=0;
		while(idx<2)
		{
			if(idx==0)
			{
				_DBG_("\n\rEnter monitor buffer size: ");
			}
			idx++;
			switch(_DG)
			{
				case '0': count=(count<<4)|0x00;break;
				case '1': count=(count<<4)|0x01;break;
				case '2': count=(count<<4)|0x02;break;
				case '3': count=(count<<4)|0x03;break;
				case '4': count=(count<<4)|0x04;break;
				case '5': count=(count<<4)|0x05;break;
				case '6': count=(count<<4)|0x06;break;
				case '7': count=(count<<4)|0x07;break;
				case '8': count=(count<<4)|0x08;break;
				case '9': count=(count<<4)|0x09;break;
				case 'a': count=(count<<4)|0x0A;break;
				case 'A': count=(count<<4)|0x0A;break;
				case 'b': count=(count<<4)|0x0B;break;
				case 'B': count=(count<<4)|0x0B;break;
				case 'c': count=(count<<4)|0x0C;break;
				case 'C': count=(count<<4)|0x0C;break;
				case 'd': count=(count<<4)|0x0D;break;
				case 'D': count=(count<<4)|0x0D;break;
				case 'e': count=(count<<4)|0x0E;break;
				case 'E': count=(count<<4)|0x0E;break;
				case 'f': count=(count<<4)|0x0F;break;
				case 'F': count=(count<<4)|0x0F;break;
				default: idx=0;count=0;break;
			}
			if(idx==2)
			{
				if(count>BUFFER_SIZE)
				{
					_DBG_("invalid! The size is bigger than ");_DBH(BUFFER_SIZE);
					idx=0;count=0;
				}
				else
					_DBH(count);
			}
		}
		//Configure I2C in monitor mode
		I2C_MonitorModeConfig(I2CDEV,(uint32_t)I2C_MONITOR_CFG_MATCHALL, ENABLE);
		I2C_MonitorModeCmd(I2CDEV, ENABLE);

		_DBG_("\n\rStart monitoring I2C bus...");

		while(done==FALSE); done=FALSE;
		_DBG_("done!");
		for(i=0;i<count;i++)
		{
			if((i%16)==0) _DBG_("");
			_DBH(buffer[i]);_DBC(0x20);
			buffer[i]=0;
		}

	}
	return 1;
}
Beispiel #6
0
/*********************************************************************//**
 * @brief       c_entry: Main program body
 * @param[in]   None
 * @return      int
 **********************************************************************/
int c_entry(void)
{
    I2C_M_SETUP_Type transferMCfg;
    uint32_t tempp;
    uint8_t *pdat;

    /* Initialize debug via UART0
     * – 115200bps
     * – 8 data bit
     * – No parity
     * – 1 stop bit
     * – No flow control
     */
    debug_frmwrk_init();

    print_menu();


    /* I2C block ------------------------------------------------------------------- */

    /*
     * Init I2C pin connect
     */
#if ((I2CDEV_M == 0))
    PINSEL_ConfigPin (5, 2, 5);
    PINSEL_ConfigPin (5, 3, 5);
#elif ((I2CDEV_M == 1))
    PINSEL_ConfigPin (0, 19, 3);
    PINSEL_ConfigPin (0, 20, 3);
    PINSEL_SetOpenDrainMode(0, 19, ENABLE);
    PINSEL_SetOpenDrainMode(0, 20, ENABLE);
    PINSEL_SetPinMode(0, 19, PINSEL_BASICMODE_PLAINOUT);
    PINSEL_SetPinMode(0, 20, PINSEL_BASICMODE_PLAINOUT);
#elif ((I2CDEV_M == 2)&& (_CURR_USING_OEM_BRD != LPC4088_OEM_BOARD))
    PINSEL_ConfigPin (0, 10, 2);
    PINSEL_ConfigPin (0, 11, 2);
    PINSEL_SetOpenDrainMode(0, 10, ENABLE);
    PINSEL_SetOpenDrainMode(0, 11, ENABLE);
    PINSEL_SetPinMode(0, 10, PINSEL_BASICMODE_PLAINOUT);
    PINSEL_SetPinMode(0, 11, PINSEL_BASICMODE_PLAINOUT);
#else
    #error "Please choose the correct peripheral."
#endif

    // Initialize Slave I2C peripheral
    I2C_Init((en_I2C_unitId)I2CDEV_M, 100000);

    /* Enable Slave I2C operation */
    I2C_Cmd((en_I2C_unitId)I2CDEV_M, I2C_MASTER_MODE, ENABLE);

    /* Transmit -------------------------------------------------------- */
    _DBG_("Press '1' to transmit");
    while (_DG != '1');


    /* Initialize buffer */
    Buffer_Init(1);
     _DBG_("Transmit Data: ");
    Buffer_Print((uint8_t*)Master_Buf, sizeof(Master_Buf));

    /* Start I2C slave device first */
    transferMCfg.sl_addr7bit = I2CDEV_S_ADDR;
    transferMCfg.tx_data = Master_Buf;
    transferMCfg.tx_length = sizeof(Master_Buf);
    transferMCfg.rx_data = NULL;
    transferMCfg.rx_length = 0;
    transferMCfg.retransmissions_max = 3;
    transferMCfg.tx_count = 0;
    transferMCfg.rx_count = 0;
    transferMCfg.retransmissions_count = 0;
     #if (I2CDEV_TRANSFER_POLLING == 0)
    complete = FALSE;
    I2C_MasterTransferData((en_I2C_unitId)I2CDEV_M, &transferMCfg, I2C_TRANSFER_INTERRUPT);
     while(!complete) ;
     #else
    I2C_MasterTransferData((en_I2C_unitId)I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING);
    #endif


    /* Receive -------------------------------------------------------- */
    _DBG_("Press '2' to receive");
    while(_DG != '2');

    /* Initialize buffer */
    Buffer_Init(0);

    /* Start I2C slave device first */
    transferMCfg.sl_addr7bit = I2CDEV_S_ADDR;
    transferMCfg.tx_data = NULL ;
    transferMCfg.tx_length = 0;
    transferMCfg.rx_data = Master_Buf;
    transferMCfg.rx_length = sizeof(Master_Buf);
    transferMCfg.retransmissions_max = 3;
    transferMCfg.tx_count = 0;
    transferMCfg.rx_count = 0;
    transferMCfg.retransmissions_count = 0;
     #if (I2CDEV_TRANSFER_POLLING == 0)
    complete = FALSE;
    I2C_MasterTransferData((en_I2C_unitId)I2CDEV_M, &transferMCfg, I2C_TRANSFER_INTERRUPT);
     while(!complete) ;
     #else
    I2C_MasterTransferData((en_I2C_unitId)I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING);
    #endif

    pdat = (uint8_t*)Master_Buf;
    _DBG_("Receive Data: ");
    Buffer_Print((uint8_t*)Master_Buf, sizeof(Master_Buf));

    // Verify
    for (tempp = 0; tempp < sizeof(Master_Buf); tempp++){
        if (*pdat != (tempp)){
                 _DBG_("Verify error ");
             break;
        }
    pdat++;
    }
    if (tempp == sizeof(Master_Buf)){
        _DBG_("Verify successfully");
    }

    /* Transmit and receive -------------------------------------------------------- */
    _DBG_("Press '3' to Transmit, then repeat start and receive...");
    while (_DG != '3');

    /* Initialize buffer */
    Buffer_Init(0);
    master_test[0] = 0xAA;
    master_test[1] = 0x55;

    /* Start I2C slave device first */
    transferMCfg.sl_addr7bit = I2CDEV_S_ADDR;
    transferMCfg.tx_data = master_test ;
    transferMCfg.tx_length = sizeof(master_test);
    transferMCfg.rx_data = Master_Buf;
    transferMCfg.rx_length = sizeof(Master_Buf);
    transferMCfg.tx_count = 0;
    transferMCfg.rx_count = 0;
    transferMCfg.retransmissions_count = 0;
    transferMCfg.retransmissions_max = 3;
     #if (I2CDEV_TRANSFER_POLLING == 0)
    complete = FALSE;
    I2C_MasterTransferData((en_I2C_unitId)I2CDEV_M, &transferMCfg, I2C_TRANSFER_INTERRUPT);
     while(!complete) ;
     #else
    I2C_MasterTransferData((en_I2C_unitId)I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING);
    #endif

    pdat = (uint8_t*)Master_Buf;
    _DBG_("Transmit Data: ");
    Buffer_Print((uint8_t*)master_test, sizeof(master_test));
    _DBG_("Receive Data: ");
    Buffer_Print((uint8_t*)Master_Buf, sizeof(Master_Buf));
    // Verify
    for (tempp = 0; tempp < sizeof(Master_Buf); tempp++){
        if (*pdat++ != tempp){
            _DBG_("Verify error: ");
            break;
        }
    }
    if (tempp == sizeof(Master_Buf)){
        _DBG_("Verify successfully");
    }
    I2C_DeInit((en_I2C_unitId)I2CDEV_M);
     /* Loop forever */
    while(1);
}
Beispiel #7
0
/*---------------------------------------------------------------------------*/
int main(void)
{
    uint8_t rv;
    uint8_t i = 0;
    uint8_t mcp3421_addr;
    uint8_t mcp9800_addr;
    uint8_t tmpReadout[4];

    /* Variables for cold junction moving average filter */
    int16_t movAvg_read;
    int8_t movAvg_ind = 0;
    int32_t movAvg_sum = 0;
    uint8_t movAvg_stabil = 0;
    int16_t movAvg_mem[8] = {0,0,0,0,0,0,0,0};    

    gainSetting = 0;
    timer0_counter = 0;

    initSerialNumber();
    usb_init();  
    I2C_Init();  
    timer_init();

    pinMode(B,1,OUTPUT);

    /*-----------------------------------------------------------------------*/
    /* Search for viable MCP3421 address options */
    /*-----------------------------------------------------------------------*/
    for(i=0x68;i<0x70;i++)
    {
        I2C_Start();
        rv = I2C_Write(write_address(i));
        I2C_Stop();

        if(rv == 0)
        {
            mcp3421_addr = i;
        }
    }

    /*-----------------------------------------------------------------------*/
    /* Search for viable MCP9800 address options */
    /*-----------------------------------------------------------------------*/
    I2C_Start();
    rv = I2C_Write(write_address(0x48));
    I2C_Stop();

    if(rv == 0)
    {
        mcp9800_addr = 0x48;
    }
    else
    {
        mcp9800_addr = 0x4D;
    }

    /*-----------------------------------------------------------------------*/
    /* Set MCP9800 to 12 bit resolution */
    /*-----------------------------------------------------------------------*/
    I2C_Start();
    I2C_Write(write_address(mcp9800_addr));    
    I2C_Write(0x01);
    I2C_Write((1<<7)|(1<<6)|(1<<5));
    I2C_Stop();

    /*-----------------------------------------------------------------------*/
    /* Set MCP9800 Register Pointer to Ambient Temperature */
    /*-----------------------------------------------------------------------*/
    I2C_Start();
    I2C_Write(write_address(mcp9800_addr));
    I2C_Write(0x00);
    I2C_Stop();
    
    while(1)
    {                
        /*-------------------------------------------------------------------*/
        /* MCP9800: Cold junction channel */
        /*-------------------------------------------------------------------*/
        usbPoll();
        I2C_Start();
        debug[0] = I2C_Write(read_address(mcp9800_addr));
        tmpReadout[0] = I2C_Read(ACK);
        tmpReadout[1] = I2C_Read(NO_ACK);        
        I2C_Stop();

        movAvg_read = ((int16_t)tmpReadout[0] << 8) + ((int16_t)tmpReadout[1]);                
        movAvg_sum -= movAvg_mem[movAvg_ind];
        movAvg_sum += movAvg_read;        
        movAvg_mem[movAvg_ind] = movAvg_read;
        
        if(movAvg_ind == 7)
        {
            movAvg_ind = 0;
            movAvg_stabil = 1;
        }
        else
        {
            movAvg_ind++;
        }

        if(movAvg_stabil == 1)
        {
            movAvg_read = movAvg_sum >> 3;    
        }        

        usbPoll();
        cli();                                
            coldJunctionReadout[0] = movAvg_read >> 8;            
            coldJunctionReadout[1] = movAvg_read & 0xFF;          
        sei();

        /*-------------------------------------------------------------------*/
        /* MCP3421: 3.75 SPS + 18 Bits + Initiate new conversion
        /*-------------------------------------------------------------------*/
        usbPoll();
        I2C_Start();
        I2C_Write(write_address(mcp3421_addr));
        I2C_Write((1<<7)|(1<<3)|(1<<2)|gainSetting);
        I2C_Stop();

        /*-------------------------------------------------------------------*/
        /* Small delay ...
        /*-------------------------------------------------------------------*/
        timer0_counter = 250;
        while(timer0_counter)
        {
            usbPoll();            
        }

        /*-------------------------------------------------------------------*/
        /* MCP3421
        /*-------------------------------------------------------------------*/
        usbPoll();
        I2C_Start();
        I2C_Write(read_address(mcp3421_addr));
        tmpReadout[0] = I2C_Read(ACK);
        tmpReadout[1] = I2C_Read(ACK);
        tmpReadout[2] = I2C_Read(ACK);
        tmpReadout[3] = I2C_Read(NO_ACK);
        I2C_Stop();

        usbPoll();
        cli();
            thermocoupleReadout[0] = tmpReadout[0];
            thermocoupleReadout[1] = tmpReadout[1];
            thermocoupleReadout[2] = tmpReadout[2];
            thermocoupleReadout[3] = tmpReadout[3];
        sei();
    }
Beispiel #8
0
int main()
{
	unsigned int nextState;
	unsigned int actualStateButton = 0;

	//Define a a data e hora que o sistema vai ter inicialmente
	//RTC_SetTime(00,00,00);
	ti.tm_hour = 00;
	ti.tm_min = 00;
	ti.tm_sec = 00;
	
	//RTC_SetDate(8,11,2013);
	ti.tm_mday = 8;
	ti.tm_mon = 11;
	ti.tm_year = 2013;
	
	//RTC_SetDays(5 ,365);
	ti.tm_wday = 5;
	ti.tm_yday = 365;
	
	//System init
	SYSCLK_Init(1000); /* Acertar o clock do TIMER*/
	RTC_Init(&ti); /* Iniciar o RTC com a data e hora definida inifialmente*/
	LCD_Init();
	LCD_Clear();
	I2C_Init();
	
	RADIO_Init();
	RADIO_SetFreq(89.9);
	WriteData();
	
	while(1)
	{

		actualStateButton = Button_Read();
		nextState = decodeButtons(actualStateButton);
		
		switch(nextState)
		{
			case CHANGE_HOURS:
				changeHours(&ti);
				break;
				
			case CHANGE_RADIO:
				changeRadio(&tea);
				LCD_Clear();
				break;
				
			//Tambem devia de escrever a freq do radio
			case SHOW:
				RTC_GetValue(&ti);
				LCD_Goto(4,0);
				strftime(buffer,16,"%T",&ti);
				LCD_WriteString(buffer);

				ReadData(&tea);
				float fm = RADIO_GetFreq(&tea);
				int level = RADIO_Station_Level(&tea);
				int parteDecimal = (int)(fm*10)%10;
				sprintf(buffer, "%d.%dMHz L:%d",(int)fm,parteDecimal, level);
				LCD_Goto(0,1);
				LCD_WriteString(buffer);
				break;
				
			default:
				break;
		}
	}
	return 0;
}
/**
  * @brief  Initializes the hardware resources (I2C and GPIO) used to configure 
  *         the OV9655 camera.
  * @param  None
  * @retval None
  */
void OV9655_HW_Init(void) 
{
  GPIO_InitTypeDef GPIO_InitStructure;
  I2C_InitTypeDef  I2C_InitStruct;

  /*** Configures the DCMI GPIOs to interface with the OV9655 camera module ***/
  /* Enable DCMI GPIOs clocks */
  /* Enable DCMI GPIOs clocks */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC |
                         RCC_AHB1Periph_GPIOE, ENABLE); 

  /* Enable DCMI clock */
  RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_DCMI, ENABLE);

  /* Connect DCMI pins to AF13 ************************************************/
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_DCMI);	//HSYNC
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_DCMI);	//PIXCLK

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_DCMI);	//DCMI_D5
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_DCMI);	//VSYNC

  GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_DCMI);	//DCMI_D0
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_DCMI);	//DCMI_D1

  GPIO_PinAFConfig(GPIOE, GPIO_PinSource0, GPIO_AF_DCMI);	//DCMI_D2
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource1, GPIO_AF_DCMI);	//DCMI_D3
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource4, GPIO_AF_DCMI);	//DCMI_D4
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource5, GPIO_AF_DCMI);	//DCMI_D6
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource6, GPIO_AF_DCMI);	//DCMI_D7
  
  /* DCMI GPIO configuration **************************************************/
  /* HSYNC(PA4), PIXCLK(PA6) */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;  
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* DCMI_D5(PB6), VSYNC(PB7) */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;  
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* DCMI_D0(PC6), DCMI_D1(PC7) */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;  
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  /* DCMI_D2(PE0), DCMI_D3(PE1), DCMI_D4(PE4), DCMI_D6(PE5), DCMI_D7(PE6),*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;  
  GPIO_Init(GPIOE, &GPIO_InitStructure);
  
  /****** Configures the I2C1 used for OV9655 camera module configuration *****/
 /* I2C1 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

  /* GPIOB clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); 

  /* Connect I2C1 pins to AF4 */
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);
  
  /* Configure I2C1 GPIOs */  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* Configure I2C1 */
  /* I2C DeInit */
  I2C_DeInit(I2C1);
    
  /* Enable the I2C peripheral */
  I2C_Cmd(I2C1, ENABLE);
 
  /* Set the I2C structure parameters */
  I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
  I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
  I2C_InitStruct.I2C_OwnAddress1 = 0xFE;
  I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
  I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  I2C_InitStruct.I2C_ClockSpeed = 30000;
  
  /* Initialize the I2C peripheral w/ selected parameters */
  I2C_Init(I2C1, &I2C_InitStruct);
}
Beispiel #10
0
/**
  * @brief  Initializes the hardware resources (I2C and GPIO) used to configure 
  *         the OV2640 camera.
  * @param  None
  * @retval None
  */
void OV2640_HW_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  I2C_InitTypeDef  I2C_InitStruct;

  /*** Configures the DCMI GPIOs to interface with the OV2640 camera module ***/
  /* Enable DCMI GPIOs clocks */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOH | 
                         RCC_AHB1Periph_GPIOI, ENABLE);

  /* Connect DCMI pins to AF13 */
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_DCMI);

  GPIO_PinAFConfig(GPIOH, GPIO_PinSource8, GPIO_AF_DCMI);
  GPIO_PinAFConfig(GPIOH, GPIO_PinSource9, GPIO_AF_DCMI);
  GPIO_PinAFConfig(GPIOH, GPIO_PinSource10, GPIO_AF_DCMI);
  GPIO_PinAFConfig(GPIOH, GPIO_PinSource11, GPIO_AF_DCMI);
  GPIO_PinAFConfig(GPIOH, GPIO_PinSource12, GPIO_AF_DCMI);
  GPIO_PinAFConfig(GPIOH, GPIO_PinSource14, GPIO_AF_DCMI);

  GPIO_PinAFConfig(GPIOI, GPIO_PinSource5, GPIO_AF_DCMI);
  GPIO_PinAFConfig(GPIOI, GPIO_PinSource6, GPIO_AF_DCMI);
  GPIO_PinAFConfig(GPIOI, GPIO_PinSource7, GPIO_AF_DCMI);
  GPIO_PinAFConfig(GPIOI, GPIO_PinSource4, GPIO_AF_DCMI);
  
  /* DCMI GPIO configuration */
  /* D0..D4(PH9/10/11/12/14), HSYNC(PH8) */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | 
                                GPIO_Pin_12 | GPIO_Pin_14| GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  GPIO_Init(GPIOH, &GPIO_InitStructure);

  /* D5..D7(PI4/6/7), VSYNC(PI5) */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_5;
  GPIO_Init(GPIOI, &GPIO_InitStructure);

  /* PCLK(PA6) */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /****** Configures the I2C1 used for OV2640 camera module configuration *****/
 /* I2C1 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

  /* GPIOB clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

  /* Connect I2C1 pins to AF4 */
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);
  
  /* Configure I2C1 GPIOs */  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* Configure I2C1 */
  /* I2C DeInit */ 
  I2C_DeInit(I2C1);
    
  /* Enable the I2C peripheral */
  I2C_Cmd(I2C1, ENABLE);
 
  /* Set the I2C structure parameters */
  I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
  I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
  I2C_InitStruct.I2C_OwnAddress1 = 0xFE;
  I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
  I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  I2C_InitStruct.I2C_ClockSpeed = 30000;
  
  /* Initialize the I2C peripheral w/ selected parameters */
  I2C_Init(I2C1, &I2C_InitStruct);
}
Beispiel #11
0
void main(void)
{
	#ifdef CAN_OPEN_ENABLE
		BYTE i;
	#endif
	//===================================
	//#if ((BUFFER_LEN_UART>1000)||(COUNT_UART_BUF>4))
	InitWatchDog();
	//#endif
	
	I2C_Init(0);
	I2C_Init(1);
	
	InitUART(0);
	SetupTime();
	InitReloadTimer0();
	InitServiceUart0();
	InitIrqLevels(); 
	__set_il(7);                /* allow all levels           */
	__EI();                     /* globally enable interrupts */
	//===================================
	InitProgrammVar();
	//===================================
	if(INIT_BLOK!=NULL)
		INIT_BLOK();
	//===================================
	#ifdef CAN_OPEN_ENABLE
		setState(Initialisation);
	#else
		SetupCAN(SPEED_CAN_0, SPEED_CAN_1, SPEED_CAN_2);
	#endif
	//===================================
	#ifdef EN_SR_ZN_CYKL
		add_timer(&TimerSrZnC);
	#endif
	#ifdef CASH_PDO_DATA
		add_timer(&TimerCashCan);
	#endif
	puts((BYTE *)"===============================================================================\r\n");
	puts((BYTE *)"                        Terminal Fujitsu MB96F338                              \r\n");
	puts((BYTE *)"===============================================================================\r\n$>");
				
	while(1)
	{
		//========================================
		clrwdt;					// обнуление WatchDog таймера 
		//========================================
		CyclicModules();		// цикличные операции выполняемые в каждом блоке
		//========================================
		if(DRIVER_BLOK!=NULL)	// Функционал конкретного блока
			DRIVER_BLOK();
		//========================================
		#ifdef CAN_OPEN_ENABLE
			
			receiveMsgHandler(0);
			receiveMsgHandler(1);
			receiveMsgHandler(2);
			DriverCanOpen();
			#ifdef CASH_PDO_DATA
				if(getTimer(&TimerCashCan)==0)
				{
					setTimer(&TimerCashCan, 30);	// обновляем данные в объектном словаре 1 раз в 30 мс
					for(i=0;i<30;i++) buildPDO_CASH(0x1800+i);
					
					if(program.Cnt1WareDev == 0)
						GetDataWare(0);
				}
			#endif
		#endif
		//========================================
		#ifdef TERMINAL_EN
			Terminal();
		#endif
		//========================================
		// подсчёт среднего времени цикла в мкс
		#ifdef EN_SR_ZN_CYKL
			program.CountCikl_1s++;
			if(getTimer(&TimerSrZnC)==0)
			{
				program.SrCikl_mks = 1000000/program.CountCikl_1s; //	среднее значение цикла в мкс
				program.CountCikl_1s = 0;
				setTimer(&TimerSrZnC, 1000);
			}
		#endif
		//========================================
	}
}
Beispiel #12
0
void I2C1_INIT()
{
	GPIO_InitTypeDef  GPIO_InitStructure;
    I2C_InitTypeDef  I2C_InitStructure;
	NVIC_InitTypeDef  NVIC_InitStructure;

	if( i2c1_init_flag == 0 )
	{
		/* Enable the I2C clock */
		RCC_APB1PeriphClockCmd(I2C1_CLK, ENABLE);
		/* GPIOB clock enable */
	    RCC_AHB1PeriphClockCmd(I2C1_GPIO_CLK, ENABLE);
		/* Enable the DMA1 clock */
	    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
	
		//Reset GPIO
		GPIO_InitStructure.GPIO_Pin =  I2C1_SDA_PIN | I2C1_SCL_PIN;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
		GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
		GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
		GPIO_Init(I2C1_GPIO_PORT, &GPIO_InitStructure);
	
		/* Connect PXx to I2C_SCL*/
	 	GPIO_PinAFConfig(I2C1_GPIO_PORT, I2C1_SDA_SOURCE, GPIO_AF_I2C1);
	
	    /* Connect PXx to I2C_SDA*/
	  	GPIO_PinAFConfig(I2C1_GPIO_PORT, I2C1_SCL_SOURCE, GPIO_AF_I2C1); 		
	
	    /* Enable I2C1 reset state */
	    RCC_APB1PeriphResetCmd(I2C1_CLK, ENABLE);
	    /* Release I2C1 from reset state */
	    RCC_APB1PeriphResetCmd(I2C1_CLK, DISABLE);
	
		I2C_DeInit(I2C1);
		I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
	    I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
	    I2C_InitStructure.I2C_OwnAddress1 = OwnAddress1;
	    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
	    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	    I2C_InitStructure.I2C_ClockSpeed = ClockSpeed;
	    I2C_Init(I2C1, &I2C_InitStructure);	 
	
		I2C_Cmd(I2C1, ENABLE);
	
		/* Configure and enable I2C1 event interrupt -------------------------------*/
		NVIC_InitStructure.NVIC_IRQChannel = I2C1_EV_IRQn;
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
		NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
		NVIC_Init(&NVIC_InitStructure);
		
		/* Configure and enable I2C1 DMA interrupt -------------------------------*/  
		NVIC_InitStructure.NVIC_IRQChannel = I2C1_DMA_TX_IRQn;
		NVIC_Init(&NVIC_InitStructure);
	
		NVIC_InitStructure.NVIC_IRQChannel = I2C1_DMA_RX_IRQn;
		NVIC_Init(&NVIC_InitStructure);
	
		/* Configure and enable I2C1 error interrupt -------------------------------*/  
		NVIC_InitStructure.NVIC_IRQChannel = I2C1_ER_IRQn;
		NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
		NVIC_Init(&NVIC_InitStructure);
	
		/* I2C1 TX DMA Channel configuration */
		DMA_Cmd(I2C1_DMA_CHANNEL_TX, DISABLE);
	    DMA_DeInit(I2C1_DMA_CHANNEL_TX);
		I2CDMA_InitStructure.DMA_Channel = DMA_Channel_1;
	    I2CDMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)I2C1_DR_Address;
	    I2CDMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)0;   /* This parameter will be configured durig communication */
	    I2CDMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;    /* This parameter will be configured durig communication */
	    I2CDMA_InitStructure.DMA_BufferSize = 0xFFFF;            /* This parameter will be configured durig communication */
	    I2CDMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	    I2CDMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	    I2CDMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;	
	    I2CDMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	    I2CDMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
	    I2CDMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
	    //I2CDMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
		I2CDMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
		I2CDMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
		I2CDMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
		I2CDMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
	    DMA_Init(I2C1_DMA_CHANNEL_TX, &I2CDMA_InitStructure);
	
	    /* I2C1 RX DMA Channel configuration */
		DMA_Cmd(I2C1_DMA_CHANNEL_RX, DISABLE);
	    DMA_DeInit(I2C1_DMA_CHANNEL_RX);
	    DMA_Init(I2C1_DMA_CHANNEL_RX, &I2CDMA_InitStructure); 
		
		//I2C_AcknowledgePolling(I2C1, 0x70);
	
		rt_event_init(&i2c_event, "i2c_event", RT_IPC_FLAG_FIFO );
		i2c_mux = rt_mutex_create("i2c_mux", RT_IPC_FLAG_FIFO );
		i2c1_init_flag = 1;
	}
}
void Gyro_Init(void)
{
	// Set the I2C baudrate & Enable the I2C bus
	I2C_Init(I2C_CLOCK_FREQ);	
}
Beispiel #14
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f2xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f2xx.c file
     */

  /* I2C configuration ---------------------------------------------------------*/
  I2C_Config();

  /* Initialize LEDs mounted on STM322xG-EVAL board */
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);

  /* SysTick configuration -----------------------------------------------------*/
  SysTickConfig();

  /* Clear the RxBuffer */
  Fill_Buffer(RxBuffer, RXBUFFERSIZE);

  /*************************************Master Code******************************/
#if defined (I2C_MASTER)

  /* I2C Struct Initialize */
  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  I2C_InitStructure.I2C_DutyCycle = I2C_DUTYCYCLE;
  I2C_InitStructure.I2C_OwnAddress1 = 0xA0;
  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED;

#ifndef I2C_10BITS_ADDRESS
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
#else
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_10bit;
#endif /* I2C_10BITS_ADDRESS */

  /* I2C Initialize */
  I2C_Init(I2Cx, &I2C_InitStructure);

  /* Master Transmitter --------------------------------------------------------*/

  /* Generate the Start condition */
  I2C_GenerateSTART(I2Cx, ENABLE);

#ifdef I2C_10BITS_ADDRESS
  /* Test on I2C1 EV5 and clear it */
  TimeOut = USER_TIMEOUT;
  while ((!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }
  /* Send Header to I2Cx for write or time out */
  I2C_SendData(I2Cx, HEADER_ADDRESS_Write);
  /* Test on I2Cx EV9 and clear it */
  TimeOut = USER_TIMEOUT;
  while ((!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_ADDRESS10))&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }
  /* Send I2Cx slave Address for write */
  I2C_Send7bitAddress(I2Cx, (uint8_t)SLAVE_ADDRESS, I2C_Direction_Transmitter);

 #else /* I2C_7BITS_ADDRESS */

  /* Test on I2Cx EV5 and clear it or time out */
  TimeOut = USER_TIMEOUT;
  while ((!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }
  /* Send I2Cx slave Address for write */
  I2C_Send7bitAddress(I2Cx, SLAVE_ADDRESS, I2C_Direction_Transmitter);

#endif /* I2C_10BITS_ADDRESS */

  /* Test on I2Cx EV6 and clear it or time out */
  TimeOut = USER_TIMEOUT;
  while ((!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }
  /* I2Cx DMA Enable */
  I2C_DMACmd(I2Cx, ENABLE);

  /* Enable DMA TX Channel */
  DMA_Cmd(I2Cx_DMA_STREAM_TX, ENABLE);

  /* Wait until I2Cx_DMA_STREAM_TX enabled or time out */
  TimeOut = USER_TIMEOUT;
  while ((DMA_GetCmdStatus(I2Cx_DMA_STREAM_TX)!= ENABLE)&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }

  /* Wait until DMA Transfer Complete or time out */
  TimeOut = USER_TIMEOUT;
  while ((DMA_GetFlagStatus(I2Cx_DMA_STREAM_TX,I2Cx_TX_DMA_TCFLAG) == RESET)&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }

  /* I2Cx DMA Disable */
  I2C_DMACmd(I2Cx, DISABLE);

  /* Wait until BTF Flag is set before generating STOP or time out */
  TimeOut = USER_TIMEOUT;
  while ((!I2C_GetFlagStatus(I2Cx,I2C_FLAG_BTF))&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }

  /* Send I2Cx STOP Condition */
  I2C_GenerateSTOP(I2Cx, ENABLE);

  /* Disable DMA TX Channel */
  DMA_Cmd(I2Cx_DMA_STREAM_TX, DISABLE);

  /* Wait until I2Cx_DMA_STREAM_TX disabled or time out */
  TimeOut = USER_TIMEOUT;
  while ((DMA_GetCmdStatus(I2Cx_DMA_STREAM_TX)!= DISABLE)&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }

  /* Clear any pending flag on Tx Stream  */
  DMA_ClearFlag(I2Cx_DMA_STREAM_TX, I2Cx_TX_DMA_TCFLAG | I2Cx_TX_DMA_FEIFLAG | I2Cx_TX_DMA_DMEIFLAG | \
                                       I2Cx_TX_DMA_TEIFLAG | I2Cx_TX_DMA_HTIFLAG);

  /* Master Receiver -----------------------------------------------------------*/

  /* Enable DMA NACK automatic generation */
  I2C_DMALastTransferCmd(I2Cx, ENABLE);

  /* Send I2Cx START condition */
  I2C_GenerateSTART(I2Cx, ENABLE);

#ifdef I2C_10BITS_ADDRESS
  /* Test on EV5 and clear it or time out */
  TimeOut = USER_TIMEOUT;
  while ((!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }
  /* Send Header to Slave for write */
  I2C_SendData(I2Cx, HEADER_ADDRESS_Write);

  /* Test on EV9 and clear it or time out */
  TimeOut = USER_TIMEOUT;
  while ((!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_ADDRESS10))&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }
  /* Send slave Address */
  I2C_Send7bitAddress(I2Cx, (uint8_t)SLAVE_ADDRESS, I2C_Direction_Transmitter);

  /* Test on I2Cx EV6 and clear it or time out*/
  TimeOut = USER_TIMEOUT;
  while ((!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }
  /* Repeated Start */
  I2C_GenerateSTART(I2Cx, ENABLE);

  /* Test on EV5 and clear it or time out*/
  TimeOut = USER_TIMEOUT;
  while ((!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }
  /* Send Header to Slave for Read */
  I2C_SendData(I2Cx, HEADER_ADDRESS_Read);

#else /* I2C_7BITS_ADDRESS */

  /* Test on I2Cx EV5 and clear it or time out*/
  TimeOut = USER_TIMEOUT;
  while ((!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }
  /* Send I2Cx slave Address for write */
  I2C_Send7bitAddress(I2Cx, SLAVE_ADDRESS, I2C_Direction_Receiver);

#endif /* I2C_10BITS_ADDRESS */

  /* Test on I2Cx EV6 and clear it or time out */
  TimeOut = USER_TIMEOUT;
  while ((!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }
  /* I2Cx DMA Enable */
  I2C_DMACmd(I2Cx, ENABLE);

  /* Enable DMA RX Channel */
  DMA_Cmd(I2Cx_DMA_STREAM_RX, ENABLE);

  /* Wait until I2Cx_DMA_STREAM_RX enabled or time out */
  TimeOut = USER_TIMEOUT;
  while ((DMA_GetCmdStatus(I2Cx_DMA_STREAM_RX)!= ENABLE)&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }

  /* Transfer complete or time out */
  TimeOut = USER_TIMEOUT;
  while ((DMA_GetFlagStatus(I2Cx_DMA_STREAM_RX,I2Cx_RX_DMA_TCFLAG)==RESET)&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }
  /* Send I2Cx STOP Condition */
  I2C_GenerateSTOP(I2Cx, ENABLE);

  /* Disable DMA RX Channel */
  DMA_Cmd(I2Cx_DMA_STREAM_RX, DISABLE);

  /* Wait until I2Cx_DMA_STREAM_RX disabled or time out */
  TimeOut = USER_TIMEOUT;
  while ((DMA_GetCmdStatus(I2Cx_DMA_STREAM_RX)!= DISABLE)&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }

  /* Disable I2C DMA request */
  I2C_DMACmd(I2Cx,DISABLE);

  /* Clear any pending flag on Rx Stream  */
  DMA_ClearFlag(I2Cx_DMA_STREAM_RX, I2Cx_RX_DMA_TCFLAG | I2Cx_RX_DMA_FEIFLAG | I2Cx_RX_DMA_DMEIFLAG | \
                                       I2Cx_RX_DMA_TEIFLAG | I2Cx_RX_DMA_HTIFLAG);

  if (Buffercmp(TxBuffer, RxBuffer, RXBUFFERSIZE) == PASSED)
  {
    /* LED2, LED3 and LED4 Toggle */
    STM_EVAL_LEDOn(LED2);
    STM_EVAL_LEDOn(LED3);
    STM_EVAL_LEDOn(LED4);
  }
  else
  {   /* ED2, LED3 and LED4 On */
    STM_EVAL_LEDOff(LED2);
    STM_EVAL_LEDOff(LED3);
    STM_EVAL_LEDOff(LED4);
  }

#endif /* I2C_MASTER */

/**********************************Slave Code**********************************/
#if defined (I2C_SLAVE)

  /* Initialize I2C peripheral */
  /* I2C Init */
  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  I2C_InitStructure.I2C_DutyCycle = I2C_DUTYCYCLE;
  I2C_InitStructure.I2C_OwnAddress1 = SLAVE_ADDRESS;
  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED;

#ifndef I2C_10BITS_ADDRESS
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
#else
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_10bit;
#endif /* I2C_10BITS_ADDRESS */

  I2C_Init(I2Cx, &I2C_InitStructure);

  /* Slave Receiver ------------------------------------------------------------*/

  /* Test on I2C EV1 and clear it */
  while (!I2C_CheckEvent(I2Cx, I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED))
  {}

  /* I2Cx DMA Enable */
  I2C_DMACmd(I2Cx, ENABLE);

  /* Enable DMA RX Channel */
  DMA_Cmd(I2Cx_DMA_STREAM_RX, ENABLE);

  /* Wait until I2Cx_DMA_STREAM_RX enabled or time out */
  TimeOut = USER_TIMEOUT;
  while ((DMA_GetCmdStatus(I2Cx_DMA_STREAM_RX)!= ENABLE)&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }

  /* Transfer complete or time out */
  TimeOut = USER_TIMEOUT;
  while ((DMA_GetFlagStatus(I2Cx_DMA_STREAM_RX,I2Cx_RX_DMA_TCFLAG)==RESET)&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }
  /* Test on I2Cx EV4 and clear it or time out */
  TimeOut = USER_TIMEOUT;
  while ((!I2C_CheckEvent(I2Cx, I2C_EVENT_SLAVE_STOP_DETECTED))&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }
  /* Disable DMA RX Channel */
  DMA_Cmd(I2Cx_DMA_STREAM_RX, DISABLE);

  /* Wait until I2Cx_DMA_STREAM_RX disabled or time out */
  TimeOut = USER_TIMEOUT;
  while ((DMA_GetCmdStatus(I2Cx_DMA_STREAM_RX)!= DISABLE)&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }

  /* Disable I2C DMA request */
  I2C_DMACmd(I2Cx,DISABLE);

  /* Clear any pending flag on Rx Stream  */
  DMA_ClearFlag(I2Cx_DMA_STREAM_RX, I2Cx_RX_DMA_TCFLAG | I2Cx_RX_DMA_FEIFLAG | I2Cx_RX_DMA_DMEIFLAG | \
                                       I2Cx_RX_DMA_TEIFLAG | I2Cx_RX_DMA_HTIFLAG);

/* Slave Transmitter ---------------------------------------------------------*/

  /* Test on I2C EV1 and clear it or time out*/
  while (!I2C_CheckEvent(I2Cx, I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED))
  {}
  /* I2Cx DMA Enable */
  I2C_DMACmd(I2Cx, ENABLE);

  /* Enable DMA RX Channel */
  DMA_Cmd(I2Cx_DMA_STREAM_TX, ENABLE);

  /* Wait until I2Cx_DMA_STREAM_TX enabled or time out */
  TimeOut = USER_TIMEOUT;
  while ((DMA_GetCmdStatus(I2Cx_DMA_STREAM_TX)!= ENABLE)&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }

  /* Transfer complete or time out */
  TimeOut = USER_TIMEOUT;
  while ((DMA_GetFlagStatus(I2Cx_DMA_STREAM_TX,I2Cx_TX_DMA_TCFLAG)==RESET)&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }

  /* Test on I2Cx EV3-2 and clear it or time out */
  TimeOut = USER_TIMEOUT;
  while ((!I2C_CheckEvent(I2Cx, I2C_EVENT_SLAVE_ACK_FAILURE))&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }

  /* Disable DMA TX Channel */
  DMA_Cmd(I2Cx_DMA_STREAM_TX, DISABLE);

  /* Wait until I2Cx_DMA_STREAM_TX disabled or time out */
  TimeOut = USER_TIMEOUT;
  while ((DMA_GetCmdStatus(I2Cx_DMA_STREAM_TX)!= DISABLE)&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }

  /* Disable I2C DMA request */
  I2C_DMACmd(I2Cx,DISABLE);

    /* Clear any pending flag on Tx Stream  */
  DMA_ClearFlag(I2Cx_DMA_STREAM_TX, I2Cx_TX_DMA_TCFLAG | I2Cx_TX_DMA_FEIFLAG | I2Cx_TX_DMA_DMEIFLAG | \
                                       I2Cx_TX_DMA_TEIFLAG | I2Cx_TX_DMA_HTIFLAG);

  if (Buffercmp(TxBuffer, RxBuffer, RXBUFFERSIZE) == PASSED)
  {
    /* LED2, LED3 and LED4 are On */
    STM_EVAL_LEDOn(LED2);
    STM_EVAL_LEDOn(LED3);
    STM_EVAL_LEDOn(LED4);
  }
  else
  {   /* LED2, LED3 and LED4 are Off */
    STM_EVAL_LEDOff(LED2);
    STM_EVAL_LEDOff(LED3);
    STM_EVAL_LEDOff(LED4);
  }

#endif /* I2C_SLAVE */
  while(1)
  {}
}
Beispiel #15
0
void i2cInit()
{
	NVIC_InitTypeDef nvic;
	I2C_InitTypeDef i2c;

	GPIO_InitTypeDef  GPIO_InitStructure;

	//Enable the i2c
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
	//Reset the Peripheral
	RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
	RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);

	//Enable the GPIOs for the SCL/SDA Pins
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

	//Configure and initialize the GPIOs
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	//Connect GPIO pins to peripheral
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1);
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);

	// clock out stuff to make sure slaves arent stuck
	// This will also configure GPIO as AF_OD at the end
	//i2cUnstick();

	// Init I2C peripheral
	I2C_DeInit(I2C1);
	I2C_StructInit(&i2c);

	I2C_ITConfig(I2C1, I2C_IT_EVT | I2C_IT_ERR, DISABLE);               // Enable EVT and ERR interrupts - they are enabled by the first request
	i2c.I2C_Mode = I2C_Mode_I2C;
	i2c.I2C_DutyCycle = I2C_DutyCycle_2;
	i2c.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	i2c.I2C_Ack = I2C_Ack_Enable;
	i2c.I2C_OwnAddress1 = 0;
	i2c.I2C_ClockSpeed =  100000;
	I2C_Init(I2C1, &i2c);

	I2C_Cmd(I2C1, ENABLE);

	// I2C ER Interrupt
	nvic.NVIC_IRQChannel = I2C1_ER_IRQn;
	nvic.NVIC_IRQChannelPreemptionPriority = 0;
	nvic.NVIC_IRQChannelSubPriority = 2;
	nvic.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&nvic);

	// I2C EV Interrupt
	nvic.NVIC_IRQChannel = I2C1_EV_IRQn;
	nvic.NVIC_IRQChannelPreemptionPriority = 0;
	nvic.NVIC_IRQChannelSubPriority = 2;
	NVIC_Init(&nvic);
}
Beispiel #16
0
void TM_I2C_Init(I2C_TypeDef* I2Cx, TM_I2C_PinsPack_t pinspack, uint32_t clockSpeed) {
	I2C_InitTypeDef I2C_InitStruct;
	
	/* Set default I2C GPIO	settings */
	GPIO_I2C_InitStruct.GPIO_Mode = GPIO_Mode_AF;
	GPIO_I2C_InitStruct.GPIO_OType = GPIO_OType_OD;
	GPIO_I2C_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_I2C_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
	
	if (I2Cx == I2C1) {
		/* Enable clock */
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
		
		/* Enable pins */
		TM_I2C1_InitPins(pinspack);
		
		/* Check clock */
		if (clockSpeed < TM_I2C_INT_Clocks[0]) {
			TM_I2C_INT_Clocks[0] = clockSpeed;
		}
		
		/* Set values */
		I2C_InitStruct.I2C_ClockSpeed = TM_I2C_INT_Clocks[0];
		I2C_InitStruct.I2C_AcknowledgedAddress = TM_I2C1_ACKNOWLEDGED_ADDRESS;
		I2C_InitStruct.I2C_Mode = TM_I2C1_MODE;
		I2C_InitStruct.I2C_OwnAddress1 = TM_I2C1_OWN_ADDRESS;
		I2C_InitStruct.I2C_Ack = TM_I2C1_ACK;
		I2C_InitStruct.I2C_DutyCycle = TM_I2C1_DUTY_CYCLE;
	} else if (I2Cx == I2C2) {
		/* Enable clock */
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
		/* Enable pins */
		TM_I2C2_InitPins(pinspack);
		
		/* Check clock */
		if (clockSpeed < TM_I2C_INT_Clocks[1]) {
			TM_I2C_INT_Clocks[1] = clockSpeed;
		}
		
		/* Set values */
		I2C_InitStruct.I2C_ClockSpeed = TM_I2C_INT_Clocks[1];
		I2C_InitStruct.I2C_AcknowledgedAddress = TM_I2C2_ACKNOWLEDGED_ADDRESS;
		I2C_InitStruct.I2C_Mode = TM_I2C2_MODE;
		I2C_InitStruct.I2C_OwnAddress1 = TM_I2C2_OWN_ADDRESS;
		I2C_InitStruct.I2C_Ack = TM_I2C2_ACK;
		I2C_InitStruct.I2C_DutyCycle = TM_I2C2_DUTY_CYCLE;
	} else if (I2Cx == I2C3) {
		/* Enable clock */
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C3, ENABLE);
		/* Enable pins */
		TM_I2C3_InitPins(pinspack);
		
		/* Check clock */
		if (clockSpeed < TM_I2C_INT_Clocks[2]) {
			TM_I2C_INT_Clocks[2] = clockSpeed;
		}
		
		/* Set values */
		I2C_InitStruct.I2C_ClockSpeed = TM_I2C_INT_Clocks[2];
		I2C_InitStruct.I2C_AcknowledgedAddress = TM_I2C3_ACKNOWLEDGED_ADDRESS;
		I2C_InitStruct.I2C_Mode = TM_I2C3_MODE;
		I2C_InitStruct.I2C_OwnAddress1 = TM_I2C3_OWN_ADDRESS;
		I2C_InitStruct.I2C_Ack = TM_I2C3_ACK;
		I2C_InitStruct.I2C_DutyCycle = TM_I2C3_DUTY_CYCLE;
	}
	
	/* Disable I2C first */
	I2C_Cmd(I2Cx, DISABLE);
	/* Initialize I2C */
	I2C_Init(I2Cx, &I2C_InitStruct);
	/* Enable I2C */
	I2C_Cmd(I2Cx, ENABLE);
}
/*--------------------------------------------------------------------------*/
int
main(int argc, char **argv)
{
  /*
  * Initalize hardware.
  */
  msp430_cpu_init();
  clock_init();

  uart_init(9600); /* Must come before first printf */

  /* xmem_init(); */

  PRINTF("iWatch 0.10 build at " __TIME__ " " __DATE__ "\n");
  UCSCTL8 &= ~BIT2;
  
  /*
  * Hardware initialization done!
  */

  /*
  * Initialize Contiki and our processes.
  */
  process_init();
  process_start(&etimer_process, NULL);
  
  rtimer_init();
  ctimer_init();

  energest_init();
  ENERGEST_ON(ENERGEST_TYPE_CPU);

  backlight_init();
  battery_init();
  SPI_FLASH_Init();

  if (system_testing())
  {
    clock_time_t t;

    backlight_on(200, 0);
    t = clock_seconds();
    // sleep 1
    while(clock_seconds() - t <= 3);
    printf("$$OK BACKLIGHT\n");
    t = clock_seconds();
    while(clock_seconds() - t <= 3);
    backlight_on(0, 0);

    motor_on(200, 0);
    // sleep 1s
    t = clock_seconds();
    while(clock_seconds() - t <= 3);
    printf("$$OK MOTOR\n");
    t = clock_seconds();
    while(clock_seconds() - t <= 3);
    motor_on(0, 0);

#if PRODUCT_W001
    I2C_Init();
    codec_init();
    codec_bypass(1);
    // sleep 1s
    t = clock_seconds();
    while(clock_seconds() - t <= 3);
    printf("$$OK MIC\n");
    // sleep 1s
    t = clock_seconds();
    while(clock_seconds() - t <= 3);
    codec_bypass(0);

    codec_shutdown();
#endif
  }

  int reason = CheckUpgrade();

  window_init(reason);

  button_init();
  rtc_init();
  CFSFontWrapperLoad();

  system_init(); // check system status and do factor reset if needed

  I2C_Init();

  //codec_init();
  //ant_init();
  bluetooth_init();

#ifdef PRODUCT_W004
  //bmx_init();
#else
  mpu6050_init();
#endif

  // check the button status
  if (button_snapshot() & (1 << BUTTON_UP))
  {
    clock_time_t t;
    // delay 1 second
    // button up is pressed, we will set emerging flag
    motor_on(200, CLOCK_SECOND * 2);
    t = clock_seconds();
    while(clock_seconds() - t <= 1);

    if (button_snapshot() & (1 << BUTTON_UP)) 

    system_setemerging();
    motor_on(0, 0);
  }  
  
  if (!system_retail())
  {
    bluetooth_discoverable(1);
  }

#if PRODUCT_W001
  if (system_testing())
    ant_init(MODE_HRM);
#endif
  
  system_restore();

//  protocol_init();
//  protocol_start(1);
  
  process_start(&system_process, NULL);

  /*
  * This is the scheduler loop.
  */
  msp430_dco_required = 0;

  /*
    check firmware update
    */
  if (reason == 0xff)
  {
    printf("Start Upgrade\n");
    Upgrade();
    // never return if sucessfully upgrade
  }

  watchdog_start();

  while(1) {
    int r;
    do {
      /* Reset watchdog. */
      watchdog_periodic();
      r = process_run();
    } while(r > 0);

    /*
    * Idle processing.
    */
    int s = splhigh();          /* Disable interrupts. */
    /* uart1_active is for avoiding LPM3 when still sending or receiving */
    if(process_nevents() != 0) {
      splx(s);                  /* Re-enable interrupts. */
    } else {
      static unsigned long irq_energest = 0;

      /* Re-enable interrupts and go to sleep atomically. */
      ENERGEST_OFF(ENERGEST_TYPE_CPU);
      ENERGEST_ON(ENERGEST_TYPE_LPM);
      /* We only want to measure the processing done in IRQs when we
         are asleep, so we discard the processing time done when we
         were awake. */
      energest_type_set(ENERGEST_TYPE_IRQ, irq_energest);
      watchdog_stop();

      if (shutdown_mode)
      {
        system_shutdown(1); // never return
        LPM4;
      }
      
      if (msp430_dco_required)
      {
        __low_power_mode_0();
      }
      else
      {
        __low_power_mode_3();
      }

      /* We get the current processing time for interrupts that was
         done during the LPM and store it for next time around.  */
      __disable_interrupt();
      irq_energest = energest_type_time(ENERGEST_TYPE_IRQ);
      __enable_interrupt();
      watchdog_start();
      ENERGEST_OFF(ENERGEST_TYPE_LPM);
      ENERGEST_ON(ENERGEST_TYPE_CPU);
    }
  }
}
Beispiel #18
0
void i2cInitPort(I2C_TypeDef *I2Cx)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    I2C_InitTypeDef I2C_InitStructure;

    if (I2Cx == I2C1) {
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
        RCC_AHBPeriphClockCmd(I2C1_SCL_CLK_SOURCE | I2C1_SDA_CLK_SOURCE, ENABLE);
        RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);

        //i2cUnstick(I2Cx);                                         // Clock out stuff to make sure slaves arent stuck

        GPIO_PinAFConfig(I2C1_SCL_GPIO, I2C1_SCL_PIN_SOURCE, GPIO_AF_4);
        GPIO_PinAFConfig(I2C1_SDA_GPIO, I2C1_SDA_PIN_SOURCE, GPIO_AF_4);

        GPIO_StructInit(&GPIO_InitStructure);
        I2C_StructInit(&I2C_InitStructure);

        // Init pins

        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

        GPIO_InitStructure.GPIO_Pin = I2C1_SCL_PIN;
        GPIO_Init(I2C1_SCL_GPIO, &GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Pin = I2C1_SDA_PIN;
        GPIO_Init(I2C1_SDA_GPIO, &GPIO_InitStructure);

        I2C_StructInit(&I2C_InitStructure);

        I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
        I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
        I2C_InitStructure.I2C_DigitalFilter = 0x00;
        I2C_InitStructure.I2C_OwnAddress1 = 0x00;
        I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
        I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
        I2C_InitStructure.I2C_Timing = 0x00E0257A; // 400 Khz, 72Mhz Clock, Analog Filter Delay ON, Rise 100, Fall 10.
        //I2C_InitStructure.I2C_Timing              = 0x8000050B;

        I2C_Init(I2C1, &I2C_InitStructure);

        I2C_Cmd(I2C1, ENABLE);
    }

    if (I2Cx == I2C2) {
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
        RCC_AHBPeriphClockCmd(I2C2_SCL_CLK_SOURCE | I2C2_SDA_CLK_SOURCE, ENABLE);
        RCC_I2CCLKConfig(RCC_I2C2CLK_SYSCLK);

        //i2cUnstick(I2Cx);                                         // Clock out stuff to make sure slaves arent stuck

        GPIO_PinAFConfig(I2C2_SCL_GPIO, I2C2_SCL_PIN_SOURCE, GPIO_AF_4);
        GPIO_PinAFConfig(I2C2_SDA_GPIO, I2C2_SDA_PIN_SOURCE, GPIO_AF_4);

        GPIO_StructInit(&GPIO_InitStructure);
        I2C_StructInit(&I2C_InitStructure);

        // Init pins
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

        GPIO_InitStructure.GPIO_Pin = I2C2_SCL_PIN;
        GPIO_Init(I2C2_SCL_GPIO, &GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Pin = I2C2_SDA_PIN;
        GPIO_Init(I2C2_SDA_GPIO, &GPIO_InitStructure);

        I2C_StructInit(&I2C_InitStructure);

        I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
        I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
        I2C_InitStructure.I2C_DigitalFilter = 0x00;
        I2C_InitStructure.I2C_OwnAddress1 = 0x00;
        I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
        I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
        I2C_InitStructure.I2C_Timing = 0x00E0257A; // 400 Khz, 72Mhz Clock, Analog Filter Delay ON, Rise 100, Fall 10.
        //I2C_InitStructure.I2C_Timing              = 0x8000050B;

        I2C_Init(I2C2, &I2C_InitStructure);

        I2C_Cmd(I2C2, ENABLE);
    }
}
Beispiel #19
0
/**
 * initialize MAXIM module
 */
maxim_status_t MAXIM_Init(
                                  handleMAXIM_t* maximHandle,
                          const settingsMAXIM_t* maximSettings
                        )
{
    /**
    * initialize intern structures,
    * which will be used from now on
    */

    memcpy( (void*)&self,     (void*)maximHandle,   sizeof(self) );
    memcpy( (void*)&settings, (void*)maximSettings, sizeof(settings) );

    statusI2C_t
    	status = I2C_Init( &(self.protocol), settings.address, settings.baudRate_kbps );

    if ( STATUS_I2C_SUCCESS != status )
    {
    return STATUS_MAXIM_INIT_ERROR;
    }

    else
    {
        // read revision ID
        statusI2C_t
          i2cStatus = STATUS_I2C_SUCCESS;

        genericI2cHandle_t
          i2cProtocol = self.protocol;

        /**
         * set up the registers
         */

        i2cStatus |= MAXIM_Reset();

        OSA_TimeDelay(50);

        i2cStatus |= I2C_WriteReg( &i2cProtocol, MAXIM_REG_LED_RED_PA,   0xFF );
        i2cStatus |= I2C_WriteReg( &i2cProtocol, MAXIM_REG_LED_IR_PA,    0x33 );
        i2cStatus |= I2C_WriteReg( &i2cProtocol, MAXIM_REG_LED_GREEN_PA, 0xFF );
        i2cStatus |= I2C_WriteReg( &i2cProtocol, MAXIM_REG_PROXY_PA,     0x19 );

        i2cStatus |= I2C_WriteReg( &i2cProtocol, MAXIM_REG_MULTILED_MODE_CR_12, 0x03 );
        i2cStatus |= I2C_WriteReg( &i2cProtocol, MAXIM_REG_MULTILED_MODE_CR_34, 0x00 );

        i2cStatus |= I2C_WriteReg( &i2cProtocol, MAXIM_REG_FIFO_CFG , 0x06 );
        i2cStatus |= I2C_WriteReg( &i2cProtocol, MAXIM_REG_SPO2_CFG , 0x43 );

        i2cStatus |= I2C_WriteReg( &i2cProtocol, MAXIM_REG_PROXY_INT_THR , 0x14 );

        /** clear FIFO pointers */
        i2cStatus |= I2C_WriteReg( &i2cProtocol, MAXIM_REG_FIFO_WR_PTR , 0 );
        i2cStatus |= I2C_WriteReg( &i2cProtocol, MAXIM_REG_FIFO_RD_PTR , 0 );
        i2cStatus |= I2C_WriteReg( &i2cProtocol, MAXIM_REG_FIFO_OV_PTR , 0 );

        // read only green LED valus
        maxim_bytesPerSample = MAXIM_BYTES_PER_ADC_VALUE * 1;
        maxim_bitMask        = 0x00003FFFF;

        return STATUS_MAXIM_SUCCESS;
    }
}
Beispiel #20
0
/*---------------------------------------------------------------------------------------------------------*/
int32_t main (void)
{
    uint32_t i;

    /* Init System, IP clock and multi-function I/O */
    SYS_Init();

    /* Init UART for printf */
    UART_Init();

    /*
        This sample code sets I2C bus clock to 100kHz. Then, accesses EEPROM 24LC64 with Byte Write
        and Byte Read operations, and check if the read data is equal to the programmed data.
    */

    printf("+-------------------------------------------------------+\n");
    printf("|    Mini51 I2C Driver Sample Code with EEPROM 24LC64    |\n");
    printf("+-------------------------------------------------------+\n");

    /* Init I2C to access EEPROM */
    I2C_Init();

    g_u8DeviceAddr = 0x50;

    for (i = 0; i < 0x100; i++)
    {
        g_au8TxData[0] = (uint8_t)((i & 0xFF00) >> 8);
        g_au8TxData[1] = (uint8_t)(i & 0x00FF);
        g_au8TxData[2] = (uint8_t)(g_au8TxData[1] + 3);

        g_u8DataLen = 0;
        g_u8EndFlag = 0;

        /* I2C function to write data to slave */
        s_I2CHandlerFn = (I2C_FUNC)I2C_MasterTx;

        /* I2C as master sends START signal */
        I2C->I2CON |= I2C_I2CON_STA_Msk;

        /* Wait I2C Tx Finish */
        while (g_u8EndFlag == 0);
        g_u8EndFlag = 0;

        /* I2C function to read data from slave */
        s_I2CHandlerFn = (I2C_FUNC)I2C_MasterRx;

        g_u8DataLen = 0;
        g_u8DeviceAddr = 0x50;

        /* Wait write finished */
        SysTick->LOAD = 5000 * CyclesPerUs;
        SysTick->VAL  =  (0x00);
        SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk;
        /* Waiting for down-count to zero */
        while((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == 0);

        /* I2C as master sends START signal */
        I2C->I2CON |= I2C_I2CON_STA_Msk;

        /* Wait I2C Rx Finish */
        while (g_u8EndFlag == 0);

        /* Compare data */
        if (g_u8RxData != g_au8TxData[2])
        {
            printf("I2C Byte Write/Read Failed, Data 0x%x\n", g_u8RxData);
            return -1;
        }
    }
    printf("I2C Access EEPROM Test OK\n");

    return 0;
}
Beispiel #21
0
void codec_init()
{
	GPIO_InitTypeDef PinInitStruct;
	GPIO_StructInit(&PinInitStruct);

	I2S_InitTypeDef I2S_InitType;

	I2C_InitTypeDef I2C_InitType;

	//Reset pin as GPIO
	PinInitStruct.GPIO_Pin = CODEC_RESET_PIN;
	PinInitStruct.GPIO_Mode = GPIO_Mode_OUT;
	PinInitStruct.GPIO_PuPd = GPIO_PuPd_DOWN;
	PinInitStruct.GPIO_OType = GPIO_OType_PP;
	PinInitStruct.GPIO_Speed = GPIO_Speed_50MHz;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOB, ENABLE);

	GPIO_Init(GPIOD, &PinInitStruct);

	// I2C pins
	PinInitStruct.GPIO_Mode = GPIO_Mode_AF;
	PinInitStruct.GPIO_OType = GPIO_OType_OD;
	PinInitStruct.GPIO_Pin = I2C_SCL_PIN | I2C_SDA_PIN;
	PinInitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
	PinInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &PinInitStruct);

	GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);

	//enable I2S and I2C clocks
	//RCC_I2SCLKConfig(RCC_I2S2CLKSource_PLLI2S);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1 | RCC_APB1Periph_SPI3, ENABLE);
	RCC_PLLI2SCmd(ENABLE);

	// I2S pins
	PinInitStruct.GPIO_OType = GPIO_OType_PP;
	PinInitStruct.GPIO_Pin = I2S3_SCLK_PIN | I2S3_SD_PIN | I2S3_MCLK_PIN;
	GPIO_Init(GPIOC, &PinInitStruct);

	PinInitStruct.GPIO_Pin = I2S3_WS_PIN;
	GPIO_Init(GPIOA, &PinInitStruct);

	//prepare output ports for alternate function
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_SPI3);
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_SPI3);
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_SPI3);
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_SPI3);


	//keep Codec off for now
	GPIO_ResetBits(GPIOD, CODEC_RESET_PIN);


	// configure I2S port
	SPI_I2S_DeInit(CODEC_I2S);
	I2S_InitType.I2S_AudioFreq = I2S_AudioFreq_48k;
	I2S_InitType.I2S_MCLKOutput = I2S_MCLKOutput_Enable;
	I2S_InitType.I2S_DataFormat = I2S_DataFormat_16b;
	I2S_InitType.I2S_Mode = I2S_Mode_MasterTx;
	I2S_InitType.I2S_Standard = I2S_Standard_Phillips;
	I2S_InitType.I2S_CPOL = I2S_CPOL_Low;

	I2S_Init(CODEC_I2S, &I2S_InitType);
	//I2S_Cmd(CODEC_I2S, ENABLE);


	// configure I2C port
	I2C_DeInit(CODEC_I2C);
	I2C_InitType.I2C_ClockSpeed = 100000;
	I2C_InitType.I2C_Mode = I2C_Mode_I2C;
	I2C_InitType.I2C_OwnAddress1 = CORE_I2C_ADDRESS;
	I2C_InitType.I2C_Ack = I2C_Ack_Enable;
	I2C_InitType.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	I2C_InitType.I2C_DutyCycle = I2C_DutyCycle_2;

	I2C_Cmd(CODEC_I2C, ENABLE);
	I2C_Init(CODEC_I2C, &I2C_InitType);


}
Beispiel #22
0
Datei: i2c.c Projekt: bnahill/iMU
int i2c_init(i2c_t *i2c, i2c_mode_t mode, uint32_t speed) {
    i2c_config_t const * const conf = i2c->config;
    GPIO_InitTypeDef gpio_init_s;
    I2C_InitTypeDef i2c_init_s;
    NVIC_InitTypeDef nvic_init_s;

    __disable_irq();
    if(i2c->mode != I2C_MODE_DISABLED) {
        __enable_irq();
        return 1;
    }

    GPIO_StructInit(&gpio_init_s);
    I2C_StructInit(&i2c_init_s);

    // All I2C peripherals are on APB1
    RCC_APB1PeriphClockCmd(conf->clock, ENABLE);

    // Reset
    RCC_APB1PeriphResetCmd(conf->clock, ENABLE);
    RCC_APB1PeriphResetCmd(conf->clock, DISABLE);

    ////////////////////////////////////////////////////////////////////
    // GPIO Config
    ////////////////////////////////////////////////////////////////////

    // Set alternate functions to use
    GPIO_PinAFConfig(conf->sda_gpio, conf->sda_pinsrc, conf->af);
    GPIO_PinAFConfig(conf->scl_gpio, conf->scl_pinsrc, conf->af);
    /*
    	// Mode configuration
    	gpio_init_s.GPIO_Mode = GPIO_Mode_IN;
    	gpio_init_s.GPIO_Speed = GPIO_Speed_50MHz;
    	gpio_init_s.GPIO_PuPd = GPIO_PuPd_NOPULL;
    	gpio_init_s.GPIO_OType = GPIO_OType_OD;

    	gpio_init_s.GPIO_Pin = conf->sda_pin;
    	GPIO_Init(conf->sda_gpio, &gpio_init_s);
    	gpio_init_s.GPIO_Pin = conf->scl_pin;
    	GPIO_Init(conf->scl_gpio, &gpio_init_s);
    */

    // Mode configuration
    gpio_init_s.GPIO_Mode = GPIO_Mode_AF;
    gpio_init_s.GPIO_Speed = GPIO_Speed_50MHz;
    gpio_init_s.GPIO_PuPd = GPIO_PuPd_UP;
    gpio_init_s.GPIO_OType = GPIO_OType_OD;

    // For some reason the order here matters and it really shouldn't
    gpio_init_s.GPIO_Pin = conf->scl_pin;
    GPIO_Init(conf->scl_gpio, &gpio_init_s);
    gpio_init_s.GPIO_Pin = conf->sda_pin;
    GPIO_Init(conf->sda_gpio, &gpio_init_s);

    ////////////////////////////////////////////////////////////////////
    // I2C Config
    ////////////////////////////////////////////////////////////////////

    i2c_init_s.I2C_Mode = I2C_Mode_I2C;
    i2c_init_s.I2C_DutyCycle = I2C_DutyCycle_2;
    i2c_init_s.I2C_ClockSpeed = 100000;
    i2c_init_s.I2C_OwnAddress1 = 0xA0;
    i2c_init_s.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
    i2c_init_s.I2C_Ack = I2C_Ack_Enable;

    ////////////////////////////////////////////////////////////////////
    // Interrupt Config
    ////////////////////////////////////////////////////////////////////

    nvic_init_s.NVIC_IRQChannel = conf->irq_er;
    nvic_init_s.NVIC_IRQChannelSubPriority = 2;
    nvic_init_s.NVIC_IRQChannelPreemptionPriority = 2;
    nvic_init_s.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&nvic_init_s);

    nvic_init_s.NVIC_IRQChannel = conf->irq_ev;
    nvic_init_s.NVIC_IRQChannelSubPriority = 3;
    nvic_init_s.NVIC_IRQChannelPreemptionPriority = 3;
    nvic_init_s.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&nvic_init_s);

    I2C_ITConfig(i2c->i2c, I2C_IT_ERR, ENABLE);
    I2C_ITConfig(i2c->i2c, I2C_IT_BUF, ENABLE);
    I2C_ITConfig(i2c->i2c, I2C_IT_EVT, ENABLE);

    ////////////////////////////////////////////////////////////////////
    // Finalize
    ////////////////////////////////////////////////////////////////////

    I2C_Cmd(i2c->i2c, ENABLE);

    I2C_Init(i2c->i2c, &i2c_init_s);

    i2c->mode = mode;

    __enable_irq();
    return 1;
}
Beispiel #23
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32l1xx_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32l1xx.c file
     */

  /* I2C configuration ------------------------------------------------------*/
  I2C_Config();

  /* Initialize LEDs mounted on STM32L152-EVAL board */
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);
  
/* SysTick configuration ---------------------------------------------------*/
  SysTickConfig();
  
/*************************************Master Code******************************/
#if defined (I2C_MASTER)
  /* I2C De-initialize */
  I2C_DeInit(I2Cx);

  /*!< I2C Struct Initialize */
  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  I2C_InitStructure.I2C_DutyCycle = I2C_DUTYCYCLE;
  I2C_InitStructure.I2C_OwnAddress1 = 0xA0;
  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED;

#ifndef I2C_10BITS_ADDRESS
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
#else
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_10bit;
#endif /* I2C_10BITS_ADDRESS */
  
  /*!< I2C Initialize */
  I2C_Init(I2Cx, &I2C_InitStructure);

  /* Enable Error Interrupt */
  I2C_ITConfig(I2Cx, I2C_IT_ERR , ENABLE);

  /* I2C ENABLE */
  I2C_Cmd(I2Cx, ENABLE);
  
  while (1)
  {
    NumberOfByteToReceive = RXBUFFERSIZE;
    Rx_Idx = 0x00;
    /* Clear the RxBuffer */
    Fill_Buffer(RxBuffer, RXBUFFERSIZE);
    
    /* Enable Event Interrupts */
    I2C_ITConfig(I2Cx, I2C_IT_EVT , ENABLE);
    
    /* Enable Acknowledge */
    I2C_AcknowledgeConfig(I2Cx, ENABLE);
    /* Generate the Start condition */
    I2C_GenerateSTART(I2Cx, ENABLE);
    /* Data transfer is performed in the I2C interrupt routine */
    /* Wait until end of data transfer or time out*/
    TimeOut = USER_TIMEOUT;
    while ((Rx_Idx < RXBUFFERSIZE)&&(TimeOut != 0x00))
    {}
    if(TimeOut == 0)
    {
      TimeOut_UserCallback();
    }
    
    if (Buffercmp(TxBuffer, RxBuffer, RXBUFFERSIZE) == PASSED)
    {
       /* LED2, LED3 and LED4 Toggle */
       STM_EVAL_LEDToggle(LED2);
       STM_EVAL_LEDToggle(LED3);
       STM_EVAL_LEDToggle(LED4);
    }
    else 
    { 
      /* ED2, LED3 and LED4 On */
       STM_EVAL_LEDOff(LED2);
       STM_EVAL_LEDOff(LED3);
       STM_EVAL_LEDOff(LED4);
     }
     delay(10);
  }
#endif /* I2C_MASTER */

/**********************************Slave Code**********************************/
#if defined (I2C_SLAVE)
  /*I2Cx DeInitialize*/
  I2C_DeInit(I2Cx);

  /*!< I2C Struct Initialize */
  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  I2C_InitStructure.I2C_DutyCycle = I2C_DUTYCYCLE;
  I2C_InitStructure.I2C_OwnAddress1 = SLAVE_ADDRESS;
  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED;

#ifndef I2C_10BITS_ADDRESS
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
#else
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_10bit;
#endif /* I2C_10BITS_ADDRESS */
  I2C_Init(I2Cx, &I2C_InitStructure);

  /* Enable Error Interrupt */
  I2C_ITConfig(I2Cx, I2C_IT_ERR , ENABLE);

  /* I2C ENABLE */
  I2C_Cmd(I2Cx, ENABLE);

  /* Infinite Loop */
  while (1)
  {
    Tx_Idx = 0x00;
    
    /* Enable I2C event interrupt */
    I2C_ITConfig(I2Cx, I2C_IT_EVT, ENABLE);
    
    /* Wait until end of data transfer */
    while (Tx_Idx < TXBUFFERSIZE)
    {
    } 

    /* LED2, LED3 and LED4 Toggle */
    STM_EVAL_LEDToggle(LED2);
    STM_EVAL_LEDToggle(LED3);
    STM_EVAL_LEDToggle(LED4);
  }
#endif /* I2C_SLAVE */
}
Beispiel #24
0
static void I2C_Initialize1()
{
  I2C_InitTypeDef I2C_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure; 
  NVIC_InitTypeDef NVIC_InitStructure;
  
  RCC_AHB1PeriphClockCmd(I2C1_DMA_CLK, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
  RCC_AHB1PeriphClockCmd(I2C1_GPIO_CLK, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  
  RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
  RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
  
  I2C_StructInit(&I2C_InitStructure);
  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
  I2C_InitStructure.I2C_OwnAddress1 = I2C1_OWN_ADDRESS;
  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  I2C_InitStructure.I2C_ClockSpeed = 100000;

  I2C_Cmd(I2C1, ENABLE);
  I2C_Init(I2C1, &I2C_InitStructure);
  
  /* Initialize I2C1 on PB8 and PB9 */
  
  GPIO_PinAFConfig(I2C1_SCL_PORT, I2C1_SCL_PIN_SOURCE, GPIO_AF_I2C1);
  GPIO_PinAFConfig(I2C1_SDA_PORT, I2C1_SDA_PIN_SOURCE, GPIO_AF_I2C1);
  
  GPIO_InitStructure.GPIO_Pin = I2C1_SCL_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  
  GPIO_InitStructure.GPIO_Pin = I2C1_SDA_PIN;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  
  DMA_ClearFlag(I2C1_DMA_STREAM_RX, I2C1_DMA_RX_FLAG_FEIF | I2C1_DMA_RX_FLAG_DMEIF | I2C1_DMA_RX_FLAG_TEIF |
                              I2C1_DMA_RX_FLAG_HTIF | I2C1_DMA_RX_FLAG_TCIF);
  DMA_Cmd(I2C1_DMA_STREAM_RX, DISABLE);
  DMA_DeInit(I2C1_DMA_STREAM_RX);
  DMA_StructInit(&I2C1_DMA_InitStructure);
  I2C1_DMA_InitStructure.DMA_Channel = I2C1_DMA_CHANNEL_RX;
  I2C1_DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &(I2C1->DR);
  I2C1_DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)0;    /* This parameter will be configured durig communication */;
  I2C1_DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; /* This parameter will be configured durig communication */
  I2C1_DMA_InitStructure.DMA_BufferSize = 0xFFFF;              /* This parameter will be configured durig communication */
  I2C1_DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  I2C1_DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  I2C1_DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  I2C1_DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  I2C1_DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  I2C1_DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
  I2C1_DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
  I2C1_DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
  I2C1_DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  I2C1_DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  DMA_Init(I2C1_DMA_STREAM_RX, &I2C1_DMA_InitStructure);
  
  DMA_ClearFlag(I2C1_DMA_STREAM_TX, I2C1_DMA_TX_FLAG_FEIF | I2C1_DMA_TX_FLAG_DMEIF | I2C1_DMA_TX_FLAG_TEIF |
                              I2C1_DMA_TX_FLAG_HTIF | I2C1_DMA_TX_FLAG_TCIF);
  DMA_Cmd(I2C1_DMA_STREAM_TX, DISABLE);
  
  DMA_DeInit(I2C1_DMA_STREAM_TX);
  I2C1_DMA_InitStructure.DMA_Channel = I2C1_DMA_CHANNEL_TX;
  DMA_Init(I2C1_DMA_STREAM_TX, &I2C1_DMA_InitStructure);
  
  /* Setup interrupts */
  NVIC_InitStructure.NVIC_IRQChannel = I2C1_DMA_RX_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = I2C1_DMA_PREPRIO;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = I2C1_DMA_SUBPRIO;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  NVIC_InitStructure.NVIC_IRQChannel = I2C1_DMA_TX_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = I2C1_DMA_PREPRIO;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = I2C1_DMA_SUBPRIO;
  NVIC_Init(&NVIC_InitStructure);  
  
  /* Enable TC Interrupts */
  DMA_ITConfig(I2C1_DMA_STREAM_TX, DMA_IT_TC, ENABLE);
  DMA_ITConfig(I2C1_DMA_STREAM_RX, DMA_IT_TC, ENABLE);      
  
  I2C1_DMA_RX_Semaphore = 0;
  I2C1_DMA_TX_Semaphore = 0;
}
Beispiel #25
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void main()
{

  /* I2C  clock Enable*/
  CLK_PeripheralClockConfig(CLK_Peripheral_I2C1, ENABLE);

#ifdef FAST_I2C_MODE
  /* system_clock / 1 */
  CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
#else
  /* system_clock / 2 */
  CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_2);
#endif

  /* Initialize LEDs mounted on STM8L1526-EVAL board */
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);

  /* Initialize I2C peripheral */
  I2C_Init(I2C1, I2C_SPEED, 0xA0,
           I2C_Mode_I2C, I2C_DutyCycle_2,
           I2C_Ack_Enable, I2C_AcknowledgedAddress_7bit);

  /* Enable Buffer and Event Interrupt*/
  I2C_ITConfig(I2C1, (I2C_IT_TypeDef)(I2C_IT_EVT | I2C_IT_BUF) , ENABLE);

  enableInterrupts();

  /* TXBuffer initialization */
  for (i = 0; i < BUFFERSIZE; i++)
    TxBuffer[i] = i;

  /* Send START condition */
  I2C_GenerateSTART(I2C1, ENABLE);
  while (NumOfBytes);
  while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));

  /* Add a delay to be sure that communication is finished */
  Delay(0xFFFF);

  /*****  reception phase ***/
  /*  Wait while the bus is busy */
  while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));

  /* Send START condition */
  I2C_GenerateSTART(I2C1, ENABLE);

  /* Test on EV5 and clear it */
  while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

#ifdef TEN_BITS_ADDRESS
  /* Send Header to Slave for write */
  I2C_SendData(I2C1, HEADER_ADDRESS_Write);

  /* Test on EV9 and clear it*/
  while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_ADDRESS10));

  /* Send slave Address */
  I2C_Send7bitAddress(I2C1, (uint8_t)SLAVE_ADDRESS, I2C_Direction_Transmitter);

  /* Test on EV6 and clear it */
  while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

  /* Repeated Start */
  I2C_GenerateSTART(I2C1, ENABLE);

  /* Test on EV5 and clear it */
  while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

  /* Send Header to Slave for Read */
  I2C_SendData(I2C1, HEADER_ADDRESS_Read);

  /* Test on EV6 and clear it */
  while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

#else
  /* Send slave Address for write */
  I2C_Send7bitAddress(I2C1, SLAVE_ADDRESS, I2C_Direction_Receiver);

  /* Test on EV6 and clear it */
  while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
#endif /* TEN_BITS_ADDRESS */

  /* While there is data to be read */
  while (NumByteToRead)
  {
#ifdef SAFE_PROCEDURE
    if (NumByteToRead != 3) /* Receive bytes from first byte until byte N-3 */
    {
      while ((I2C_GetLastEvent(I2C1) & 0x04) != 0x04); /* Poll on BTF */

      /* Read a byte from the Slave */
      RxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1);

      /* Point to the next location where the byte read will be saved */
      Rx_Idx++;

      /* Decrement the read bytes counter */
      NumByteToRead--;
    }

    if (NumByteToRead == 3)  /* it remains to read three data: data N-2, data N-1, Data N */
    {
      /* Data N-2 in DR and data N -1 in shift register */
      while ((I2C_GetLastEvent(I2C1) & 0x04) != 0x04); /* Poll on BTF */

      /* Clear ACK */
      I2C_AcknowledgeConfig(I2C1, DISABLE);

      /* Disable general interrupts */
      disableInterrupts();

      /* Read Data N-2 */
      RxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1);

      /* Point to the next location where the byte read will be saved */
      Rx_Idx++;

      /* Program the STOP */
      I2C_GenerateSTOP(I2C1, ENABLE);

      /* Read DataN-1 */
      RxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1);

      /* Enable General interrupts */
      enableInterrupts();

      /* Point to the next location where the byte read will be saved */
      Rx_Idx++;

      while ((I2C_GetLastEvent(I2C1) & 0x40) != 0x40); /* Poll on RxNE */

      /* Read DataN */
      RxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1);

      /* Reset the number of bytes to be read by master */
      NumByteToRead = 0;

    }
#else
    if (NumByteToRead == 1)
    {
      /* Disable Acknowledgement */
      I2C_AcknowledgeConfig(I2C1, DISABLE);

      /* Send STOP Condition */
      I2C_GenerateSTOP(I2C1, ENABLE);

      /* Poll on RxNE Flag */
      while ((I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE) == RESET));
      /* Read a byte from the Slave */
      RxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1);

      /* Point to the next location where the byte read will be saved */
      Rx_Idx++;

      /* Decrement the read bytes counter */
      NumByteToRead--;
    }

    /* Test on EV7 and clear it */
    if (I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED) )
    {
      /* Read a byte */
      RxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1);

      /* Point to the next location where the byte read will be saved */
      Rx_Idx++;

      /* Decrement the read bytes counter */
      NumByteToRead--;
    }
#endif /* SAFE_PROCEDURE */
  }

  /* check if sent and received data are not corrupted */
  TransferStatus1 = Buffercmp((uint8_t*)TxBuffer, (uint8_t*) RxBuffer, BUFFERSIZE);

  if (TransferStatus1 != FAILED)
  {
    while (1)
    {
      /* Toggle LED1*/
      STM_EVAL_LEDToggle(LED1);
      /* Insert delay */
      Delay(0x7FFF);
    }
  }
  else
  {
    while (1)
    {
      /* Toggle LED4*/
      STM_EVAL_LEDToggle(LED4);
      /* Insert delay */
      Delay(0x7FFF);
    }
  }
}
/*********************************************************************//**
 * @brief		c_entry: Main program body
 * @param[in]	None
 * @return 		int
 **********************************************************************/
int c_entry(void)
{
	uint8_t tmpchar[2] = {0, 0};
	__IO FlagStatus exitflag;
	PINSEL_CFG_Type PinCfg;
	I2C_M_SETUP_Type transferCfg;
	uint8_t SC16IS_RegStat;

#if (USEDI2CDEV == 0)
    /* Disable I2C0 interrupt */
    NVIC_DisableIRQ(I2C0_IRQn);
    /* preemption = 1, sub-priority = 1 */
    NVIC_SetPriority(I2C0_IRQn, ((0x01<<3)|0x01));
#elif (USEDI2CDEV == 2)
    /* Disable I2C2 interrupt */
    NVIC_DisableIRQ(I2C2_IRQn);
    /* preemption = 1, sub-priority = 1 */
    NVIC_SetPriority(I2C2_IRQn, ((0x01<<3)|0x01));
#endif

	/* Initialize debug via UART0
	 * – 115200bps
	 * – 8 data bit
	 * – No parity
	 * – 1 stop bit
	 * – No flow control
	 */
	debug_frmwrk_init();

	// print welcome screen
	print_menu();

	/*
	 * Init I2C pin connect
	 */
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
#if (USEDI2CDEV == 0)
	PinCfg.Funcnum = 1;
	PinCfg.Pinnum = 27;
	PinCfg.Portnum = 0;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 28;
	PINSEL_ConfigPin(&PinCfg);
#elif (USEDI2CDEV == 2)
	PinCfg.Funcnum = 2;
	PinCfg.Pinnum = 10;
	PinCfg.Portnum = 0;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 11;
	PINSEL_ConfigPin(&PinCfg);
#endif

	/* I2C block ------------------------------------------------------------------- */
	// Initialize I2C peripheral
	I2C_Init(I2CDEV, 100000);

	/* Enable I2C1 operation */
	I2C_Cmd(I2CDEV, ENABLE);

	// test
	complete = RESET;
	transferCfg.sl_addr7bit = SLVADDR;
	transferCfg.tx_data = NULL;
	transferCfg.tx_length = 0;
	transferCfg.rx_data = &SC16IS_RegStat;
	transferCfg.rx_length = 1;
	transferCfg.retransmissions_max = 2;
	I2C_MasterTransferData(I2CDEV, &transferCfg, I2C_TRANSFER_INTERRUPT);
	while (complete == RESET);

	/* Configure SC16IS750 ---------------------------------------------------------- */
	/* First, send some command to reset SC16IS740 chip via I2C bus interface */
	complete = RESET;
	transferCfg.sl_addr7bit = SLVADDR;
	transferCfg.tx_data = (uint8_t *)iocon_cfg;
	transferCfg.tx_length = sizeof(iocon_cfg);
	transferCfg.rx_data = NULL;
	transferCfg.rx_length = 0;
	transferCfg.retransmissions_max = 2;
	I2C_MasterTransferData(I2CDEV, &transferCfg, I2C_TRANSFER_INTERRUPT);
	while (complete == RESET);

	complete = RESET;
	transferCfg.sl_addr7bit = SLVADDR;
	transferCfg.tx_data = (uint8_t *)iodir_cfg;
	transferCfg.tx_length = sizeof(iodir_cfg);
	transferCfg.rx_data = NULL;
	transferCfg.rx_length = 0;
	transferCfg.retransmissions_max = 2;
	I2C_MasterTransferData(I2CDEV, &transferCfg, I2C_TRANSFER_INTERRUPT);
	while (complete == RESET);

	complete = RESET;
	transferCfg.sl_addr7bit = SLVADDR;
	transferCfg.tx_data = (uint8_t *)iostate_cfg_0;
	transferCfg.tx_length = sizeof(iostate_cfg_0);
	transferCfg.rx_data = NULL;
	transferCfg.rx_length = 0;
	transferCfg.retransmissions_max = 2;
	I2C_MasterTransferData(I2CDEV, &transferCfg, I2C_TRANSFER_INTERRUPT);
	while (complete == RESET);


	/* Validate value of SC16IS750 register ------------------------------------------ */
	/* This section will dump out value of register that set through I2C bus */
	complete = RESET;
	transferCfg.sl_addr7bit = SLVADDR;
	transferCfg.tx_data = NULL; //(uint8_t *)iocon_cfg;
	transferCfg.tx_length = 0;//1;
	transferCfg.rx_data = &SC16IS_RegStat;
	transferCfg.rx_length = 1;
	transferCfg.retransmissions_max = 2;
	I2C_MasterTransferData(I2CDEV, &transferCfg, I2C_TRANSFER_INTERRUPT);
	while (complete == RESET);

	complete = RESET;
	transferCfg.sl_addr7bit = SLVADDR;
	transferCfg.tx_data = (uint8_t *)iodir_cfg;
	transferCfg.tx_length = 1;
	transferCfg.rx_data = &SC16IS_RegStat;
	transferCfg.rx_length = 1;
	transferCfg.retransmissions_max = 2;
	I2C_MasterTransferData(I2CDEV, &transferCfg, I2C_TRANSFER_INTERRUPT);
	while (complete == RESET);

	complete = RESET;
	transferCfg.sl_addr7bit = SLVADDR;
	transferCfg.tx_data = (uint8_t *)iostate_cfg_0;
	transferCfg.tx_length = 1;
	transferCfg.rx_data = &SC16IS_RegStat;
	transferCfg.rx_length = 1;
	transferCfg.retransmissions_max = 2;
	I2C_MasterTransferData(I2CDEV, &transferCfg, I2C_TRANSFER_INTERRUPT);
	while (complete == RESET);


	// Reset exit flag
	exitflag = RESET;

    /* Read some data from the buffer */
    while (exitflag == RESET){

    	while((tmpchar[0] = _DG) == 0);

    	if (tmpchar[0] == 27){
			/* ESC key, set exit flag */
			_DBG_(menu2);
			exitflag = SET;
		}
		else if (tmpchar[0] == 'r'){
			print_menu();
		}
		else{
			if (tmpchar[0] == '1'){
				// LEDs are ON now...
				transferCfg.sl_addr7bit = SLVADDR;
				transferCfg.tx_data = (uint8_t *)iostate_cfg_0;
				transferCfg.tx_length = sizeof(iostate_cfg_0);
				transferCfg.rx_data = NULL;
				transferCfg.rx_length = 0;
				transferCfg.retransmissions_max = 2;
				if (I2C_MasterTransferData(I2CDEV, &transferCfg, I2C_TRANSFER_POLLING) \
						== ERROR){
					Error_Loop(transferCfg.status);
				}
			}
			else if (tmpchar[0] == '2')
			{
				// LEDs are OFF now...
				transferCfg.sl_addr7bit = SLVADDR;
				transferCfg.tx_data = (uint8_t *)iostate_cfg_1;
				transferCfg.tx_length = sizeof(iostate_cfg_1);
				transferCfg.rx_data = NULL;
				transferCfg.rx_length = 0;
				transferCfg.retransmissions_max = 2;
				if (I2C_MasterTransferData(I2CDEV, &transferCfg, I2C_TRANSFER_POLLING) \
						== ERROR){
					Error_Loop(transferCfg.status);
				}
			}
			/* Then Echo it back */
			_DBG_(tmpchar);
		}
    }

    // wait for current transmission complete - THR must be empty
    while (UART_CheckBusy(LPC_UART0) == SET);

    // DeInitialize UART0 peripheral
    UART_DeInit(LPC_UART0);
    I2C_DeInit(I2CDEV);

    /* Loop forever */
    while(1);
    return 1;
}
Beispiel #27
0
/**
  * @brief   Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* System Clocks Configuration */
  RCC_Configuration();
       
  /* Configure the GPIO ports */
  GPIO_Configuration();

  /* DMA1 channel5 configuration ----------------------------------------------*/
  DMA_DeInit(DMA1_Channel5);
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)I2C2_DR_Address;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)I2C2_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_MemoryDataSize_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_Channel5, &DMA_InitStructure);

  /* DMA1 channel6 configuration ----------------------------------------------*/
  DMA_DeInit(DMA1_Channel6);
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)I2C1_DR_Address;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)I2C1_Buffer_Tx;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_Init(DMA1_Channel6, &DMA_InitStructure);

  /* Enable I2C1 and I2C2 ----------------------------------------------------*/
  I2C_Cmd(I2C1, ENABLE);
  I2C_Cmd(I2C2, ENABLE);

  /* 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_SLAVE_ADDRESS7;
  I2C_Init(I2C2, &I2C_InitStructure);
  
  /*----- 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));  
  /* Send I2C2 slave Address for write */
  I2C_Send7bitAddress(I2C1, I2C2_SLAVE_ADDRESS7, I2C_Direction_Transmitter);
  /* Test on I2C2 EV1 and clear it */
  while(!I2C_CheckEvent(I2C2, I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED));  
  /* Test on I2C1 EV6 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));  

  /* Enable I2C2 DMA */
  I2C_DMACmd(I2C2, ENABLE);
  /* Enable I2C1 DMA */
  I2C_DMACmd(I2C1, ENABLE);

  /* Enable DMA1 Channel5 */
  DMA_Cmd(DMA1_Channel5, ENABLE);
  /* Enable DMA1 Channel6 */
  DMA_Cmd(DMA1_Channel6, ENABLE);

  /* DMA1 Channel5 transfer complete test */
  while(!DMA_GetFlagStatus(DMA1_FLAG_TC5));
  /* DMA1 Channel6 transfer complete test */
  while(!DMA_GetFlagStatus(DMA1_FLAG_TC6));

  /* Send I2C1 STOP Condition */
  I2C_GenerateSTOP(I2C1, ENABLE);
  /* Test on I2C2 EV4 */
  while(!I2C_CheckEvent(I2C2, I2C_EVENT_SLAVE_STOP_DETECTED)); 
  /* 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);


  /* Check if the transmitted and received data are equal */
  TransferStatus = Buffercmp(I2C1_Buffer_Tx, I2C2_Buffer_Rx, BufferSize);
  /* TransferStatus = PASSED, if the transmitted and received data 
     are the same */
  /* TransferStatus = FAILED, if the transmitted and received data 
     are different */

  while (1)
  {
  }
}
void sens9DInit(void){
	//PB10/I2C2_SCL, PB11/I2C2_SDA
	// XM Read Address 8bit 0x3B, Gyro 8bit Read 0xD7
	GPIO_InitTypeDef      GPIO_InitStructure;
	I2C_InitTypeDef  I2C_InitStructure;
	u8 statOk=!0;
	int i;



	I2C_DeInit(I2C2);
	I2C_Cmd(I2C2, DISABLE);
	I2C_SoftwareResetCmd(I2C2, ENABLE);
	I2C_SoftwareResetCmd(I2C2, DISABLE);

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2,ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);


	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	GPIOB->BSRRL=GPIO_Pin_10|GPIO_Pin_11;

	i=8;
	while( (i--) && ((GPIOB->IDR&GPIO_Pin_11)==0)){
		delay_us(200);
		GPIOB->BSRRH=GPIO_Pin_10;
		delay_us(200);
		GPIOB->BSRRL=GPIO_Pin_10;
	}

	GPIO_PinAFConfig(GPIOB,GPIO_PinSource10, GPIO_AF_I2C2);
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource11, GPIO_AF_I2C2);
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
	I2C_InitStructure.I2C_OwnAddress1 = 0x01;
	I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
	I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	I2C_InitStructure.I2C_ClockSpeed=100e3;
	I2C_InitStructure.I2C_DutyCycle=I2C_DutyCycle_16_9;
	I2C_Init(I2C2, &I2C_InitStructure);
	I2C_StretchClockCmd(I2C2,ENABLE);
	//I2C_GeneralCallCmd(I2C1,ENABLE);
	//I2C_AnalogFilterCmd(I2C2,ENABLE);
	//I2C_DigitalFilterConfig(I2C2,0x02); Not available on 407.. :(
	I2C_Cmd(I2C2, ENABLE);


	delay_us(1e3);

	//CTRL_REG1_G to CTRL_REG5_G 0x20..0x24
	//190Hz, 25Hz cut
	//0.45Hz HPass
	//Full scale 2000dps
	//HPass ΝΟΤ selected
	//					20h			21h				22h			23h			24h
	u8 GyroCTRreg[5]={0b01111111, 0b00100101,	0b00000000,	0b10100000,	0b00000000 };//24h ->0b00000000 disables LPF2 //Enable 0b00000010
	I2C_write(Gy_addr,0x20|0x80,GyroCTRreg,5);
	// HP filter bypassed
	//					1Fh			20h			21h			22h			23h			24h			25h			26h
	u8 XM_CTRreg[8]={0b00000000, 0b01111111, 0b11001000, 0b00000000, 0b00000000, 0b11110100, 0b00000000, 0b00000000 };//200Hz, 50HzBW, 4G, Magn:100Hz,2Gauss
	I2C_write(XM_addr,0x1F|0x80,XM_CTRreg,8);

}
Beispiel #29
0
int main(void)
{
	GPIO_InitTypeDef        GPIO_InitStructure;								//structure for GPIO setup
	//TouchInitStr touch_init;
	
	I2C_InitTypeDef					I2C_InitStructure; 		 	
	

  /* SysTick end of count event each 1ms */
  RCC_GetClocksFreq(&RCC_Clocks);
  SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);	
	
 
	touch_coords.processed=1;
	
	

  /* Initialize LEDs, Key Button, LCD and COM port(USART) available on
     STM32303C-EVAL board *****************************************************/
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);
	
	STM_EVAL_LEDInit(LED9);
	

	//init now the TW88 chip on I2C 1
	
	//I2C init ...on PB6 And PB7
	
	/* Configure the I2C clock source. The clock is derived from the SYSCLK */
  RCC_I2CCLKConfig(RCC_I2C1CLK_HSI);													

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB , ENABLE);
	

	
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_4);  // SCL
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_4);	// SDA


  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;																
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;							
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;	
	GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;


  GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_6);	
  GPIO_Init(GPIOB, &GPIO_InitStructure);		

	GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_7);
	GPIO_Init(GPIOB, &GPIO_InitStructure);	
	
	//init our serial port on UART2
	SerialPort_Init(115200,1);
	
	
	printf("\n\r ***** COEN490/ELEC490 Capstone Project Team 14 Welcome! ***** \n\r");
	
	
	I2C_DeInit(I2C1);
	I2C_StructInit(&I2C_InitStructure);
	I2C_InitStructure.I2C_Mode=I2C_Mode_I2C;
	I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
	I2C_InitStructure.I2C_DigitalFilter = 0x00;
	I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
	I2C_InitStructure.I2C_OwnAddress1 = 0x00;
	I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	//I2C_InitStructure.I2C_Timing = 0x50330309; //0xB0420F13;    ///0x00902025; // 0xc062121f
	I2C_InitStructure.I2C_Timing = 0x00310309; 
	I2C_Init(I2C1,&I2C_InitStructure);
	I2C_Cmd(I2C1,ENABLE);



	
	Delay(1);	
	
  //init TW88
	TW88_Init();
	
	
	//add the osd
	TW88_AddOSD_Win(0,0,0x3F,1);
	
	
	//touch_init.onTouchEvent = onTouchScreenEvent;
	AR1100Init();
	
	
	
  /* Infinite loop */
  while (1)
  {
	
		if (rx_counter>0) {
			printf("Recv: %d",rx_buffer[0]);
			memset(rx_buffer, 0, RX_BUFFER_LENGTH);
			rx_counter=0;
		}
		
		
		if (touch_coords.processed==0) {
			 touch_coords.processed=1;
			 onTouchScreenEvent(touch_coords);
		}
		
		
    /* Toggle LD1 */
    STM_EVAL_LEDToggle(LED1);

    /* Insert 50 ms delay */
    Delay(50);

    /* Toggle LD2 */
    STM_EVAL_LEDToggle(LED2);

    /* Insert 50 ms delay */
    Delay(50);

    /* Toggle LD3 */
    STM_EVAL_LEDToggle(LED3);

    /* Insert 50 ms delay */
    Delay(50);

    /* Toggle LD4 */
    //STM_EVAL_LEDToggle(LED4);

    /* Insert 50 ms delay */
    //Delay(50);
		

		
		
  }
}
static void s_hal_i2c4hal_hw_gpio_init(hal_i2c4hal_port_t port)
{
    
#if     defined(USE_STM32F1)

    static const GPIO_InitTypeDef  s_hal_i2c4hal_sclsda_altcfg  =
    {
        .GPIO_Pin       = 0,
        .GPIO_Speed     = GPIO_Speed_50MHz,
        .GPIO_Mode      = GPIO_Mode_AF_OD,
    }; 

    // 1. prepare af.
    // for i2c1 (scl, sda): no-remap if it is (PB6, PB7). GPIO_Remap_I2C1 if it is (PB8, PB9).
    // for i2c2 (scl, sda): no remap if it is (PB10, PB11).
    uint32_t afname = HAL_GPIO_AFNAME_NONE;
    uint32_t afmode = HAL_GPIO_AFMODE_NONE;
    hal_bool_t found = hal_false;

    
    hal_gpio_port_t portscl = hal_brdcfg_i2c4hal__scl[HAL_i2c4hal_port2index(port)].port;
    hal_gpio_pin_t  pinscl  = hal_brdcfg_i2c4hal__scl[HAL_i2c4hal_port2index(port)].pin;
    hal_gpio_port_t portsda = hal_brdcfg_i2c4hal__sda[HAL_i2c4hal_port2index(port)].port;
    hal_gpio_pin_t  pinsda  = hal_brdcfg_i2c4hal__sda[HAL_i2c4hal_port2index(port)].pin;    
    
    if(hal_i2c4hal_port1 == port)
    {        
        if((hal_gpio_portB == portscl) && (hal_gpio_portB == portsda) && (hal_gpio_pin6 == pinscl) && (hal_gpio_pin7 == pinsda))
        {
            afname = HAL_GPIO_AFNAME_NONE;  afmode = HAL_GPIO_AFMODE_NONE;      found = hal_true;
        }
        if((hal_gpio_portB == portscl) && (hal_gpio_portB == portsda) && (hal_gpio_pin8 == pinscl) && (hal_gpio_pin9 == pinsda))
        {
            afname = GPIO_Remap_I2C1;       afmode = ENABLE;                    found = hal_true;
        }            
    }
    else if(hal_i2c4hal_port2 == port)
    {
        if((hal_gpio_portB == portscl) && (hal_gpio_portB == portsda) && (hal_gpio_pin10 == pinscl) && (hal_gpio_pin11 == pinsda))
        {
            afname = HAL_GPIO_AFNAME_NONE;  afmode = HAL_GPIO_AFMODE_NONE;      found = hal_true;
        }  
    }
    
    if(hal_false == found)
    {
        hal_base_on_fatalerror(hal_fatalerror_incorrectparameter, "hal_i2c4hal_init(): incorrect pin mapping");
    }

    hal_gpio_altcfg_t hal_i2c4hal_scl_altcfg;
    hal_gpio_altcfg_t hal_i2c4hal_sda_altcfg;
    
    // prepare the altcfg for scl and sda pins
    memcpy(&hal_i2c4hal_scl_altcfg, &s_hal_i2c4hal_sclsda_altcfg, sizeof(hal_gpio_altcfg_t));
    memcpy(&hal_i2c4hal_sda_altcfg, &s_hal_i2c4hal_sclsda_altcfg, sizeof(hal_gpio_altcfg_t));
    hal_i2c4hal_scl_altcfg.afname = hal_i2c4hal_sda_altcfg.afname = afname;
    hal_i2c4hal_scl_altcfg.afmode = hal_i2c4hal_sda_altcfg.afmode = afmode;
    
    // configure scl and sda pins
    hal_gpio_configure(hal_brdcfg_i2c4hal__scl[HAL_i2c4hal_port2index(port)], &hal_i2c4hal_scl_altcfg);    
    hal_gpio_configure(hal_brdcfg_i2c4hal__sda[HAL_i2c4hal_port2index(port)], &hal_i2c4hal_sda_altcfg);


#elif   defined(USE_STM32F4)    

    static const GPIO_InitTypeDef  s_hal_i2c4hal_sclsda_altcfg  =
    {
        .GPIO_Pin       = GPIO_Pin_All,
        .GPIO_Mode      = GPIO_Mode_AF,
        .GPIO_Speed     = GPIO_Speed_50MHz,
        .GPIO_OType     = GPIO_OType_OD,
        .GPIO_PuPd      = GPIO_PuPd_NOPULL
    }; 
    
    
    // 1. prepare af.
    // for i2c1 (scl, sda): scl -> PB6, PB8. sda -> PB7, PB9
    // for i2c2 (scl, sda): scl -> PF1, PH4, PB10. sda -> PF0, PH5, PB11
    // for i2c3 (scl, sda): scl -> PH7, PA8. sda -> PH8, PC9, PA9

    uint32_t afname = HAL_GPIO_AFNAME_NONE;
    uint32_t afmode = HAL_GPIO_AFMODE_NONE;
    hal_bool_t foundscl = hal_false;
    hal_bool_t foundsda = hal_false;

    
    hal_gpio_port_t portscl = hal_brdcfg_i2c4hal__scl[HAL_i2c4hal_port2index(port)].port;
    hal_gpio_pin_t  pinscl  = hal_brdcfg_i2c4hal__scl[HAL_i2c4hal_port2index(port)].pin;
    hal_gpio_port_t portsda = hal_brdcfg_i2c4hal__sda[HAL_i2c4hal_port2index(port)].port;
    hal_gpio_pin_t  pinsda  = hal_brdcfg_i2c4hal__sda[HAL_i2c4hal_port2index(port)].pin;       
    
    if(hal_i2c4hal_port1 == port)
    { 
        afname = GPIO_AF_I2C1;  afmode = ENABLE;
        
        if( ((hal_gpio_portB == portscl) && (hal_gpio_pin6 == pinscl))  ||
            ((hal_gpio_portB == portscl) && (hal_gpio_pin8 == pinscl))  )
        {
            foundscl = hal_true;
        }
        
        if( ((hal_gpio_portB == portsda) && (hal_gpio_pin7 == pinsda))  ||
            ((hal_gpio_portB == portsda) && (hal_gpio_pin9 == pinsda))  )
        {
            foundsda = hal_true;
        }
    }
    else if(hal_i2c4hal_port2 == port)
    {   
        afname = GPIO_AF_I2C2;
        
        if( ((hal_gpio_portF == portscl) && (hal_gpio_pin1 == pinscl))  ||
            ((hal_gpio_portH == portscl) && (hal_gpio_pin4 == pinscl))  ||
            ((hal_gpio_portB == portscl) && (hal_gpio_pin10 == pinscl)) )
        {
            foundscl = hal_true;
        }
        
        if( ((hal_gpio_portF == portsda) && (hal_gpio_pin0 == pinsda))  ||
            ((hal_gpio_portH == portsda) && (hal_gpio_pin5 == pinsda))  ||
            ((hal_gpio_portB == portsda) && (hal_gpio_pin11 == pinsda)) )
        {
            foundsda = hal_true;
        }
    }
    else if(hal_i2c4hal_port3 == port)
    {
        afname = GPIO_AF_I2C3;
        
        if( ((hal_gpio_portH == portscl) && (hal_gpio_pin7 == pinscl))  ||
            ((hal_gpio_portA == portscl) && (hal_gpio_pin8 == pinscl))  )
        {
            foundscl = hal_true;
        }
        
        if( ((hal_gpio_portH == portsda) && (hal_gpio_pin8 == pinsda))  ||
            ((hal_gpio_portC == portsda) && (hal_gpio_pin9 == pinsda))  ||
            ((hal_gpio_portB == portsda) && (hal_gpio_pin9 == pinsda))  )
        {
            foundsda = hal_true;
        }    
    
    }
    
    if((hal_false == foundscl) || (hal_false == foundsda))
    {
        hal_base_on_fatalerror(hal_fatalerror_incorrectparameter, "hal_i2c4hal_init(): incorrect pin mapping");
    }
    
   
    hal_gpio_altcfg_t hal_i2c4hal_scl_altcfg;
    hal_gpio_altcfg_t hal_i2c4hal_sda_altcfg;
    
    // prepare the altcfg for scl and sda pins
    memcpy(&hal_i2c4hal_scl_altcfg, &s_hal_i2c4hal_sclsda_altcfg, sizeof(hal_gpio_altcfg_t));
    memcpy(&hal_i2c4hal_sda_altcfg, &s_hal_i2c4hal_sclsda_altcfg, sizeof(hal_gpio_altcfg_t));
    hal_i2c4hal_scl_altcfg.afname = hal_i2c4hal_sda_altcfg.afname = afname;
    hal_i2c4hal_scl_altcfg.afmode = hal_i2c4hal_sda_altcfg.afmode = afmode;
    
    // configure scl and sda pins
    hal_gpio_configure(hal_brdcfg_i2c4hal__scl[HAL_i2c4hal_port2index(port)], &hal_i2c4hal_scl_altcfg);    
    hal_gpio_configure(hal_brdcfg_i2c4hal__sda[HAL_i2c4hal_port2index(port)], &hal_i2c4hal_sda_altcfg);    

    
#endif
}

static void s_hal_i2c4hal_hw_enable(hal_i2c4hal_port_t port, hal_i2c4hal_speed_t speed)
{
#if     defined(USE_STM32F1) || defined(USE_STM32F4)

    I2C_TypeDef* I2Cx = (hal_i2c4hal_port1 == port) ? (I2C1) : (I2C2);
    
    I2C_InitTypeDef i2c4hal_cfg;
    memcpy(&i2c4hal_cfg, &s_hal_i2c4hal_periph_cfg, sizeof(I2C_InitTypeDef));
    // apply the clockspeed 
    i2c4hal_cfg.I2C_ClockSpeed = (uint32_t)speed * 100000;
    // i2c peripheral enable
    I2C_Cmd(I2Cx, ENABLE);
    // apply configuration
    I2C_Init(I2Cx, &i2c4hal_cfg);

#endif


}