changeset 18:afdd22502c2a

Add i2c test code
author Daniel O'Connor <darius@dons.net.au>
date Wed, 14 Nov 2012 12:30:58 +1030
parents b62eaa3131b1
children d078651f5353
files BSDmakefile hw.c
diffstat 2 files changed, 82 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/BSDmakefile	Wed Nov 14 12:30:29 2012 +1030
+++ b/BSDmakefile	Wed Nov 14 12:30:58 2012 +1030
@@ -15,7 +15,7 @@
 		system_stm32f10x.c \
 		touch.c
 
-STM32LIBS=	usart gpio rcc rtc pwr bkp fsmc spi tim
+STM32LIBS=	usart gpio rcc rtc pwr bkp fsmc spi tim i2c
 
 .for f in ${STM32LIBS}
 SRCS+=		stm32f10x_${f}.c
--- a/hw.c	Wed Nov 14 12:30:29 2012 +1030
+++ b/hw.c	Wed Nov 14 12:30:58 2012 +1030
@@ -5,6 +5,8 @@
 #include "1wire.h"
 #include "lcd.h"
 
+#define I2C_TIMEOUT 10000
+
 static void	hw_port_cfg(void);
 
 /* Wait for cnt microseconds */
@@ -26,16 +28,22 @@
 
 static void
 hw_port_cfg(void) {
-    GPIO_InitTypeDef			GPIO_InitStructure;
-    USART_InitTypeDef USART_InitStructure;
-    SPI_InitTypeDef			SPI_InitStructure;
     FSMC_NORSRAMInitTypeDef		FSMC_NORSRAMInitStructure;
     FSMC_NORSRAMTimingInitTypeDef	p;
+    GPIO_InitTypeDef			GPIO_InitStructure;
+    I2C_InitTypeDef			I2C_InitStructure;
+    SPI_InitTypeDef			SPI_InitStructure;
     TIM_TimeBaseInitTypeDef		TIM_TimeBaseStructure;
-    
-    /* RTC stuff */
-    /* Enable PWR and BKP clocks */
-    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
+    USART_InitTypeDef 			USART_InitStructure;
+
+    /* Enable clocks */
+    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP | RCC_APB1Periph_TIM4 | RCC_APB1Periph_I2C1,
+			   ENABLE);
+
+    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOB | 
+			   RCC_APB2Periph_GPIOE | RCC_APB2Periph_SPI1, ENABLE);
+
+    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
 
     /* Allow access to BKP Domain */
     PWR_BackupAccessCmd(ENABLE);
@@ -74,9 +82,6 @@
     /* Wait until last write operation on RTC registers has finished */
     RTC_WaitForLastTask();
 
-    /* Clock setup */
-    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
-
     /* Port configuration */
     /* Configure USART1 TX (PA.09) as alternate function push-pull */
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
@@ -89,9 +94,6 @@
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
     GPIO_Init(GPIOA, &GPIO_InitStructure);
 
-    /* Enable GPIOB clock */
-    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
-
     /* Configure PB5 as output push-pull for LED */
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
@@ -121,16 +123,6 @@
     USART_Cmd(USART1, ENABLE);
 
     /* Enable FSMC clock */
-    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
-
-    /* Enable alternate function IO clock */
-    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
-
-    /* Enable GPIOD clock */
-    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
-
-    /* Enable GPIOD clock */
-    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
 
     /* Configures LCD Control lines (FSMC Pins) in alternate function Push-Pull mode.
      *
@@ -153,8 +145,6 @@
     GPIO_Init(GPIOE, &GPIO_InitStructure);
 
     /* Configure backlight control (PD13/FSMC_A18 remapped to TIM4_CH2) */
-    /* Enable TIM4 clock */
-    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
 
     /* Enable timer function
      * Note source clock is SYSCLK / 2 = 36MHz
@@ -234,9 +224,6 @@
      * The flash chip is selected with PA4.
      */
 
-    /* Enable SPI1 clock */
-    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
-
     /* Configure MOSI, MISO and SCLK as alternate function PP */
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
@@ -275,8 +262,6 @@
     SPI_Cmd(SPI1, ENABLE);
 
     /* Configure TIM6 for interval timing */
-    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
-
     /* Reset TIM6 */
     TIM_DeInit(TIM6);
 
@@ -291,7 +276,9 @@
     TIM_SelectOnePulseMode(TIM6, TIM_OPMode_Single);
     TIM_UpdateDisableConfig(TIM6, DISABLE);
 
-    /* Setup GPIO for 1-wire */
+    /* Setup GPIO for delay test & 1-wire
+     * PE2 -> pin 3 on header
+     * PE3 -> pin 4 on header */
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
@@ -310,5 +297,67 @@
 	GPIO_ResetBits(GPIOE, GPIO_Pin_2);
 	_usleep16(100);
     }
-#endif    
+#endif
+
+    /* Setup I2C bus */
+      
+    /* Configure SCL/SDA pins */
+    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
+    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
+    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
+    GPIO_Init(GPIOB, &GPIO_InitStructure);
+      
+    /* Reset I2C1 */
+    I2C_DeInit(I2C1);
+
+    /* Setup I2C1 */
+    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
+    I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
+    I2C_InitStructure.I2C_OwnAddress1 = 0x00;
+    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
+    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
+    I2C_InitStructure.I2C_ClockSpeed = 100000;
+    I2C_Init(I2C1, &I2C_InitStructure);
+
+    /* Start I2C controller */
+    I2C_Cmd(I2C1, ENABLE);
+
+#if 0
+    uint32_t I2C_TimeOut = I2C_TIMEOUT;
+
+    /* Clear the I2C1 AF flag */
+    I2C_ClearFlag(I2C1, I2C_FLAG_AF);
+
+    /* Enable I2C1 acknowledgement if it is already disabled by other function */
+    I2C_AcknowledgeConfig(I2C1, ENABLE);
+
+    /*---------------------------- Transmission Phase ---------------------------*/
+
+    /* Send I2C1 START condition */
+    I2C_GenerateSTART(I2C1, ENABLE);
+
+    /*!< Test on I2C1 EV5 and clear it */
+    while ((!I2C_GetFlagStatus(I2C1, I2C_FLAG_SB)) && I2C_TimeOut) {
+	I2C_TimeOut--;
+    }
+
+    if (I2C_TimeOut == 0) {
+	return ERROR;
+    }
+  
+    I2C_TimeOut = I2C_TIMEOUT;
+    /* Send STLM75 slave address for write */
+    I2C_Send7bitAddress(I2C1, LM75_ADDR, I2C_Direction_Transmitter);
+
+    while ((!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) && I2C_TimeOut) {
+	I2C_TimeOut--;
+    }
+
+    if ((I2C_GetFlagStatus(LM75_I2C, I2C_FLAG_AF) != 0x00) || (I2C_TimeOut == 0)) {
+	return ERROR;
+    } else {
+	return SUCCESS;
+    }
+
+#endif
 }