打印

混合测试 i2c iwdg rtc exti和休眠唤醒

[复制链接]
3306|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
phz0008|  楼主 | 2011-5-30 15:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
I2c, RTC, TI, TE, ST
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "iic_soft.h"
#include "systick.h"
#include "usart1.h"
//#include "stm32f10x_Tim.h"
/* Private typedef -----------------------------------------------------------*/
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
/* Private define ------------------------------------------------------------*/
#define EEPROM_WriteAddress1    0x10
#define EEPROM_ReadAddress1     0x10
#define BufferSize1             (countof(Tx1_Buffer)-1)
#define BufferSize2             (countof(Tx2_Buffer)-1)
#define EEPROM_WriteAddress2    0xAA
#define EEPROM_ReadAddress2     0xAA
#define xx88xxAddr    0x8a
#define xx88xxChipIdAddr        0x00
#define xx88xxChipPageAddr      0xff
/* Private macro -------------------------------------------------------------*/
#define countof(a) (sizeof(a) / sizeof(*(a)))
/* Private variables ---------------------------------------------------------*/
uint8_t Tx1_Buffer[] = "STM32Library Example */";
uint8_t Tx2_Buffer[] = "Library Example */";
uint8_t Rx1_Buffer[BufferSize1], Rx2_Buffer[BufferSize2];
volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED;
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
ErrorStatus HSEStartUpStatus;
extern __IO u32 TimeCntFlag10ms;
static __IO uint32_t TimingDelay = 0;
void RCC_Configuration(void)
{
  /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration -----------------------------*/   
  /* RCC system reset(for debug purpose) */
  RCC_DeInit();
  /* Enable HSE */
  RCC_HSEConfig(RCC_HSE_ON);
  /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp();
  if(HSEStartUpStatus == SUCCESS)
  {
    /* HCLK = SYSCLK */
    RCC_HCLKConfig(RCC_SYSCLK_Div1);
  
    /* PCLK2 = HCLK */
    RCC_PCLK2Config(RCC_HCLK_Div1);
    /* PCLK1 = HCLK/2 */
    RCC_PCLK1Config(RCC_HCLK_Div2);
    /* Flash 2 wait state */
    FLASH_SetLatency(FLASH_Latency_2);
    /* Enable Prefetch Buffer */
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
    /* PLLCLK = 8MHz * 9 = 72 MHz */
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
    /* Enable PLL */
    RCC_PLLCmd(ENABLE);
    /* Wait till PLL is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }
    /* Select PLL as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
    /* Wait till PLL is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
  }
}
/*******************************************************************************
* Function Name  : SYSCLKConfig_STOP
* Description    : Configures system clock after wake-up from STOP: enable HSE, PLL
*                  and select PLL as system clock source.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SYSCLKConfig_STOP(void)
{
  /* Enable HSE */
  RCC_HSEConfig(RCC_HSE_ON);
  /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp();
  if(HSEStartUpStatus == SUCCESS)
  {
    /* Enable PLL */
    RCC_PLLCmd(ENABLE);
    /* Wait till PLL is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }
    /* Select PLL as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
    /* Wait till PLL is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
  }
}
/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures the different GPIO ports.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  /* Enable GPIOC, GPIOB and AFIO clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB |
                         RCC_APB2Periph_AFIO, ENABLE);
  /* Configure PC.06, PC.07, PC.08 and PC.09 as Output push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_4 | GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  /* Configure PB9 as input floating (EXTI Line9) */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name  : EXTI_Configuration
* Description    : Configures EXTI Line9 and Line17(RTC Alarm).
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void EXTI_Configuration(void)
{
  EXTI_InitTypeDef EXTI_InitStructure;
  /* Connect EXTI Line9 to PB.09 */
  GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9);
  /* Configure EXTI Line9 to generate an interrupt on falling edge */
  EXTI_ClearITPendingBit(EXTI_Line9);
  EXTI_InitStructure.EXTI_Line = EXTI_Line9;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);
  /* Configure EXTI Line17(RTC Alarm) to generate an interrupt on rising edge */
  EXTI_ClearITPendingBit(EXTI_Line17);
  EXTI_InitStructure.EXTI_Line = EXTI_Line17;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  EXTI_Init(&EXTI_InitStructure);   
}
/*******************************************************************************
* Function Name  : RTC_Configuration
* Description    : Configures RTC clock source and prescaler.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RTC_Configuration(void)
{
  /* RTC clock source configuration ------------------------------------------*/
  /* Allow access to BKP Domain */
  PWR_BackupAccessCmd(ENABLE);
  /* Reset Backup Domain */
  BKP_DeInit();
  
  /* Enable the LSE OSC */
  RCC_LSEConfig(RCC_LSE_ON);
  /* Wait till LSE is ready */
  while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  {
  }
  /* Select the RTC Clock Source */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  /* Enable the RTC Clock */
  RCC_RTCCLKCmd(ENABLE);
  /* RTC configuration -------------------------------------------------------*/
  /* Wait for RTC APB registers synchronisation */
  RTC_WaitForSynchro();
  /* Set the RTC time base to 1s */
  RTC_SetPrescaler(32767);  
  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
  /* Enable the RTC Alarm interrupt */
  RTC_ITConfig(RTC_IT_ALR, ENABLE);
  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
}
/*******************************************************************************
* Function Name  : NVIC_Configuration
* Description    : Configures NVIC and Vector Table base location.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  /* Set the Vector Table base location at 0x08000000 */
  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   
  /* 2 bits for Preemption Priority and 2 bits for Sub Priority */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_Init(&NVIC_InitStructure);
}
/*******************************************************************************
* Function Name  : SysTick_Configuration
* Description    : Configures the SysTick to generate an interrupt each 1 millisecond.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SysTick_Configuration(void)
{
  /* Setup SysTick Timer for 10 msec interrupts  */
  if (SysTick_Config(SystemCoreClock / 100))
  {
    /* Capture error */
    while (1);
  }
  
/* Configure the SysTick handler priority */
  NVIC_SetPriority(SysTick_IRQn, 0x0);
}
/*******************************************************************************
* Function Name  : Delay
* Description    : Inserts a delay time.
* Input          : nTime: specifies the delay time length, in milliseconds.
* Output         : None
* Return         : None
*******************************************************************************/
void Delay(__IO uint32_t nCount)
{
  TimingDelay = nCount;
  /* Enable the SysTick Counter */
  SysTick->CTRL |= SysTick_CTRL_ENABLE;
  while(TimingDelay != 0);
  /* Disable the SysTick Counter */
  SysTick->CTRL &= ~SysTick_CTRL_ENABLE;
  /* Clear the SysTick Counter */
  SysTick->VAL = (uint32_t)0x0;
}
static void TIM4_Config(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  /* TIM7 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 , ENABLE);
  
  /* Enable the TIM7 global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
  NVIC_Init(&NVIC_InitStructure);
  
  /* Configure TIM7 to generate interrupt each 50ms */
  TIM_TimeBaseStructure.TIM_Period  = 10000;
  TIM_TimeBaseStructure.TIM_Prescaler = (SystemCoreClock/1000000)-1;
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStructure.TIM_CounterMode =  TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
  /* TIM7 IT enable */
  TIM_ITConfig(TIM4, TIM_IT_Update , ENABLE);
  
  /* TIM7 enable counter */
  TIM_Cmd(TIM4, ENABLE);  
}
void Initxx88xxLED(void)
{
       u8 cPage;

       cPage = I2C_ReadByte(0x8a,0xff); // save page
      I2C_WriteByte(xx88xxAddr,0xff, 0x01); // page 1
       I2C_WriteByte(xx88xxAddr,0xb1, 0x20);
       I2C_WriteByte(xx88xxAddr,0x30, 0xD2);
       I2C_WriteByte(xx88xxAddr,0x31, 0xA8);//0xA9//0xA6); //changed for new LED circuit.
       I2C_WriteByte(xx88xxAddr,0x33, 0x67);//0x40); //changed for new LED circuit.
       I2C_WriteByte(xx88xxAddr,0x34, 0x80);
       I2C_WriteByte(xx88xxAddr,0x32, 0x84);
       I2C_WriteByte(xx88xxAddr,0x35, 0x80);
       I2C_WriteByte(xx88xxAddr,0x36, 0x00);
       I2C_WriteByte(xx88xxAddr,0xff, cPage); // restore page num.         
}
void IWDG_Configuration(void)
{
/* 写入0x5555,用于允许狗狗寄存器写入功能 */
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);

/* 狗狗时钟分频,40K/256=156HZ(6.4ms)*/
IWDG_SetPrescaler(IWDG_Prescaler_256);

/* 喂狗时间 5s/6.4MS=781 .注意不能大于0xfff*/
IWDG_SetReload(781);

/* 喂狗*/
IWDG_ReloadCounter();

/* 使能狗狗*/
IWDG_Enable();
}

int main(void)
{
  u8 temp=0;
  u8 RegValue =0;
  /* Setup STM32 system (clock, PLL and Flash configuration) */
// SystemInit();
   /* Clock configuration */
  RCC_Configuration();
  /* Enable PWR and BKP clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  /* GPIO configuration */
  GPIO_Configuration();
  /* Configure EXTI Line9 to generate an interrupt on falling edge */
  EXTI_Configuration();
  /* Configure RTC clock source and prescaler */
  RTC_Configuration();
  /* NVIC configuration */
  NVIC_Configuration();
  /* Add your application code here
     */
  USART1_Configuration(115200);
  I2C_Configuration();
  IWDG_Configuration();
  if(RCC_GetFlagStatus(RCC_FLAG_IWDGRST)!=RESET) //如果上次是看门狗引起的复位
{
GPIO_SetBits(GPIOC, GPIO_Pin_4); //4个led全亮
GPIO_SetBits(GPIOC, GPIO_Pin_5);
GPIO_SetBits(GPIOC, GPIO_Pin_6);
GPIO_SetBits(GPIOC, GPIO_Pin_7);
RCC_ClearFlag();
}

// TIM4_Config();
  /* Infinite loop */
  printf("I2C软件模拟第一次测试,写入数据:%s\r\n",Tx1_Buffer);
  //I2C_Writes();
  I2C_BufferWrite(Tx1_Buffer,EEPROM_WriteAddress1,BufferSize1);
  I2C_Reads(EEPROM_ReadAddress1,Rx1_Buffer,BufferSize1);
  TransferStatus1 = Buffercmp(Tx1_Buffer, Rx1_Buffer, BufferSize1);
  if(TransferStatus1==FAILED)
    printf("实验失败,读出数据为:%s\r\n",Rx1_Buffer);
  else
    printf("实验成功,读出数据为:%s\r\n",Rx1_Buffer);
  //I2C_Write(0x12,0xaa);
//  u8 I2C_ReadByte(u8 ChipAddr,u8 InnerAddr);//读字节
//bool I2C_WriteByte(u8 ChipAddr,u8 InnerAddr,u8 Data);//写一个字
   temp=I2C_ReadByte(xx88xxAddr,xx88xxChipIdAddr);
   if(temp==0x22)
     printf("find xx88xx!\r\n");
   else
     printf("not find xx88xx!\r\n");
   
   //I2C_WriteByte(xx88xxAddr,0xd5, 0x08); // On  Panel:1  Signal:1  Back:1
   Initxx88xxLED();
  // I2C_WriteByte(xx88xxAddr,0xd5, 0x0f); // On  Panel:1  Signal:1  Back:1
   //PAGE
   I2C_WriteByte(xx88xxAddr,0xb1, 0x80); // Enable TCON
   temp=I2C_ReadByte(xx88xxAddr,xx88xxChipPageAddr);
   printf("xx88xxChipPageAddr=:%d\r\n",temp);
   I2C_WriteByte(xx88xxAddr,xx88xxChipPageAddr,0X00);   //PAGE 0
   
   I2C_WriteByte(xx88xxAddr,0xb1, 0x80); // enable TCON  
   
    temp = I2C_ReadByte(xx88xxAddr,0xb1);
    printf("0xb1:0x%02X\r\n",temp);
   // I2C_WriteByte(xx88xxAddr,0xb1, (u8)(temp & 0x7F)); // disable TCON
  /* Turn on led connected to PC.06 */
  GPIO_SetBits(GPIOC, GPIO_Pin_4);
  /* Turn off led connected to PC.09 */
  GPIO_ResetBits(GPIOC, GPIO_Pin_7);
  while (1)
  {
    /* Wait till RTC Second event occurs */
    RTC_ClearFlag(RTC_FLAG_SEC);
    while(RTC_GetFlagStatus(RTC_FLAG_SEC) == RESET);
    /* Alarm in 3 second */
    RTC_SetAlarm(RTC_GetCounter()+ 3);
    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();
    /* Turn on led connected to PC.09 */
    GPIO_SetBits(GPIOC, GPIO_Pin_7);
    /* Turn off led connected to PC.06 */
    GPIO_ResetBits(GPIOC, GPIO_Pin_4);
     printf("马上睡觉啦\r\n");
     IWDG_ReloadCounter();
     
    /* Request to enter STOP mode with regulator ON */
    PWR_EnterSTOPMode(PWR_Regulator_ON, PWR_STOPEntry_WFI);   
// if((TimeCntFlag10ms%1000)==0)
//  {  
        /* At this stage the system has resumed from STOP mode -------------------*/
    /* Turn on led connected to PC.06 */
    GPIO_SetBits(GPIOC, GPIO_Pin_4);
    /* Configures system clock after wake-up from STOP: enable HSE, PLL and select PLL
       as system clock source (HSE and PLL are disabled in STOP mode) */
    SYSCLKConfig_STOP();
    IWDG_Configuration();
   IWDG_Enable();   //启动看门狗,自动就会使能内部的40K
    IWDG_ReloadCounter();
    /* Turn off led connected to PC.09 */
    GPIO_ResetBits(GPIOC, GPIO_Pin_7);
    printf("被闹钟吵醒啦\r\n");
if(RegValue)
{
    temp = I2C_ReadByte(xx88xxAddr,0xb1);
    printf("0xb1:0x%02X\r\n",temp);
    I2C_WriteByte(xx88xxAddr,0xb1, (u8)(temp & 0x7F)); // disable TCON
    printf("\n\txx88xx Panel control Register Shut Down The TCON!");
RegValue=0;
}
  
else
{
   I2C_WriteByte(xx88xxAddr,0xb1, 0x84); // enable TCON and FPCLK delay is 5
    printf("\n\txx88xx Panel control Register Open The TCON!");
RegValue=1;
}

//}   
  }
}
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param file: pointer to the source file name
  * @param line: assert_param error line source number
  * @retval : None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* Infinite loop */
  while (1)
  {
  }
}
#endif
/**
  * @}
  */
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
{
  while(BufferLength--)
  {
    if(*pBuffer1 != *pBuffer2)
    {
      return FAILED;
    }
   
    pBuffer1++;
    pBuffer2++;
  }
  return PASSED;  
}
/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/

结果软件模拟第一次测试,写入数据:STM32Library Example */
实验成功,读出数据为:STM32Library Example */
not find xx88xx!
xx88xxChipPageAddr=:0
0xb1:0x80
马上睡觉啦
被闹钟吵醒啦
xx88xx Panel control Register Open The TCON!马上睡觉啦
被闹钟吵醒啦
0xb1:0xFE
xx88xx Panel control Register Shut Down The TCON!马上睡觉啦
被闹钟吵醒啦
xx88xx Panel control Register Open The TCON!马上睡觉啦
被闹钟吵醒啦
0xb1:0x00
xx88xx Panel control Register Shut Down The TCON!马上睡觉啦
被闹钟吵醒啦
xx88xx Panel control Register Open The TCON!马上睡觉啦
被闹钟吵醒啦
0xb1:0x84
xx88xx Panel control Register Shut Down The TCON!马上睡觉啦
被闹钟吵醒啦
xx88xx Panel control Register Open The TCON!马上睡觉啦
被闹钟吵醒啦
0xb1:0x84
xx88xx Panel control Register Shut Down The TCON!马上睡觉啦


貌似硬件i2c确实不好用,
沙发
phz0008|  楼主 | 2011-5-30 15:44 | 只看该作者
请指教 ,除i2c外没发现stm32的其他外设不好使,使用cpal试过硬件i2c,确实不错,但是时间长后写出错的问题还是会出现。

使用特权

评论回复
板凳
donkey89| | 2011-5-30 17:10 | 只看该作者
关注一下,要用到I2C,还真麻烦啊

使用特权

评论回复
地板
phz0008|  楼主 | 2011-5-31 09:16 | 只看该作者
其实这里边测的东西不少,就那个破i2c花的时间最多,其他的倒是很好搞定,比方说,传说中的休眠唤醒和iwdg的问题,还有lsi的问题,不过就是测测,功能程序没分开,勉强看吧。

使用特权

评论回复
5
phz0008|  楼主 | 2011-6-1 09:19 | 只看该作者
void EXTI_Configuration(void)
{
  EXTI_InitTypeDef EXTI_InitStructure;
  /* Connect EXTI Line3 to PD.03 */
  GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9);
  /* Configure EXTI Line9 to generate an interrupt on falling edge */
  EXTI_ClearITPendingBit(EXTI_Line3);
  EXTI_InitStructure.EXTI_Line = EXTI_Line3;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);
  /* Configure EXTI Line17(RTC Alarm) to generate an interrupt on rising edge */
  EXTI_ClearITPendingBit(EXTI_Line17);
  EXTI_InitStructure.EXTI_Line = EXTI_Line17;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  EXTI_Init(&EXTI_InitStructure);   
}
修正一下,copy 错了

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

7

主题

124

帖子

0

粉丝