/** * @brief Automatic test switch to LSE clock from HSI and return to HSI * @caller auto_test * @param None * @retval None */ void test_LSE(void) { /* Switch the clock to LSE */ LCD_GLASS_DisplayString("LSE"); CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); CLK_LSEConfig(CLK_LSE_ON); delay_ms(LSE_DELAY); if((CLK->ECKCR & CLK_ECKCR_LSERDY) == RESET) { LCD_GLASS_DisplayString("LSE"); delay_ms(LSE_DELAY); if((CLK->ECKCR & CLK_ECKCR_LSERDY) == RESET) { LCD_GLASS_DisplayString("LSE"); delay_ms(LSE_DELAY); if((CLK->ECKCR & CLK_ECKCR_LSERDY) == RESET) { /* Switch the clock to HSI*/ CLK_LSEConfig(CLK_LSE_OFF); CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_8); CLK_HSICmd(ENABLE); while (((CLK->ICKCR)& 0x02)!=0x02); CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI); CLK_SYSCLKSourceSwitchCmd(ENABLE); while (((CLK->SWCR)& 0x01)==0x01); while(1) { LCD_GLASS_ScrollSentence("LSE Not OK ",1,SCROLL_SPEED); //Press reset for exit KeyPressed = FALSE; } } } } /* Wait flag LSE ready */ while (!((CLK->ECKCR)& CLK_ECKCR_LSERDY)); /* Switch in LSE clock */ CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_LSE); CLK_SYSCLKSourceSwitchCmd(ENABLE); while (((CLK->SWCR)& 0x01)==0x01); LCD_GLASS_DisplayString("LSE OK"); /* Switch the clock to HSI */ CLK_LSEConfig(CLK_LSE_OFF); CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_8); CLK_HSICmd(ENABLE); while (((CLK->ICKCR)& 0x02)!=0x02); CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI); CLK_SYSCLKSourceSwitchCmd(ENABLE); while (((CLK->SWCR)& 0x01)==0x01); delay_ms(100); }
/** * @brief this function checks if a NDEF message is available * @par Parameters None * @retval none */ static void User_DisplayMessageActiveHaltMode ( uint8_t PayloadLength ) { //Switch the clock to LSE and disable HSI #ifdef USE_LSE CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_LSE); CLK_SYSCLKSourceSwitchCmd(ENABLE); while (((CLK->SWCR)& 0x01)==0x01); CLK_HSICmd(DISABLE); CLK->ECKCR &= ~0x01; #else CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_LSI); CLK_SYSCLKSourceSwitchCmd(ENABLE); while (((CLK->SWCR)& 0x01)==0x01); CLK_HSICmd(DISABLE); CLK->ECKCR &= ~0x01; #endif // disable interupt sim(); // To copy function DISPLAYRAM in RAM section DISPLAY #ifdef _COSMIC_ if (!(_fctcpy('D'))) while(1); #endif Display_Ram (); // Call in RAM // state_machine = STATE_VREF; #ifdef USE_HSI //Switch the clock to HSI CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_16); CLK_HSICmd(ENABLE); while (((CLK->ICKCR)& 0x02)!=0x02); CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI); CLK_SYSCLKSourceSwitchCmd(ENABLE); while (((CLK->SWCR)& 0x01)==0x01); #else CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_2); // Select 2MHz HSE as system clock source CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSE); // wait until the target clock source is ready while (((CLK->SWCR)& 0x01)==0x01); // wait until the target clock source is ready CLK_SYSCLKSourceSwitchCmd(ENABLE); #endif // enable interupt rim(); }
void LowPower_CLK_Config(void) { CLK_SYSCLKSourceSwitchCmd(ENABLE);//使能时钟切换 CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_LSI);//选择内部低速时钟作为时钟源 CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);//设置系统时钟分频 while (CLK_GetSYSCLKSource() != CLK_SYSCLKSource_LSI);//等待时钟稳定 CLK_HSICmd(DISABLE); //关闭原先的 CLK source CLK_HSEConfig(CLK_HSE_OFF); //关闭外部 CLK source }
/** * @brief Configure System and peripheral Clock * @param None * @retval None */ static void CLK_Config(void) { /* System Clock is HSI/1 */ CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); /* Enable ADC1 clock */ CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE); }
/** * @brief Configure System Clock * @param None * @retval None */ static void CLK_Config(void) { /* Select HSE as system clock source */ CLK_SYSCLKSourceSwitchCmd(ENABLE); CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSE); /* system clock prescaler: 1*/ CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); while (CLK_GetSYSCLKSource() != CLK_SYSCLKSource_HSE) {} }
/*********************************************************************** * @brief Configure System Clock * @param None * @retval None ************************************************************************/ static void CLK_Config(void) { // Select HSI as system clock source CLK_SYSCLKSourceSwitchCmd(ENABLE); CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI); // system clock prescaler: 1 CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); while (CLK_GetSYSCLKSource() != CLK_SYSCLKSource_HSI) {} }
/** * @brief this function checks if a NDEF message is available * @par Parameters None * @retval none */ static void User_DisplayMessage (uint8_t message[],uint8_t PayloadLength ) { //Switch the clock to LSE and disable HSI #ifdef USE_LSE CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_LSE); CLK_SYSCLKSourceSwitchCmd(ENABLE); while (((CLK->SWCR)& 0x01)==0x01); CLK_HSICmd(DISABLE); CLK->ECKCR &= ~0x01; #else CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_LSI); CLK_SYSCLKSourceSwitchCmd(ENABLE); while (((CLK->SWCR)& 0x01)==0x01); CLK_HSICmd(DISABLE); CLK->ECKCR &= ~0x01; #endif LCD_GLASS_ScrollSentenceNbCar(message,30,PayloadLength+6); #ifdef USE_HSI //Switch the clock to HSI CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_16); CLK_HSICmd(ENABLE); while (((CLK->ICKCR)& 0x02)!=0x02); CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI); CLK_SYSCLKSourceSwitchCmd(ENABLE); while (((CLK->SWCR)& 0x01)==0x01); #else CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_2); // Select 2MHz HSE as system clock source CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSE); // wait until the target clock source is ready while (((CLK->SWCR)& 0x01)==0x01); // wait until the target clock source is ready CLK_SYSCLKSourceSwitchCmd(ENABLE); #endif }
/** * @brief Function to initialize the entry in low power and wait modes * @caller test low power mode * @param None * @retval None */ void LPR_init(void) { /*Switch the clock to LSE and disable HSI*/ #ifdef USE_LSE CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_LSE); CLK_SYSCLKSourceSwitchCmd(ENABLE); while (((CLK->SWCR)& 0x01)==0x01); CLK_HSICmd(DISABLE); #else CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_LSI); CLK_SYSCLKSourceSwitchCmd(ENABLE); while (((CLK->SWCR)& 0x01)==0x01); CLK_HSICmd(DISABLE); #endif /*Configure event for WAKEUP and FUNCTION, disable the interrupts*/ sim(); /* To copy function LPR_Ram in RAM section LPRUN*/ #ifdef _COSMIC_ if (!(_fctcpy('L'))) while(1); #endif LPR_Ram(); // Call in RAM /*Switch the clock to HSI*/ CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_8); CLK_HSICmd(ENABLE); while (((CLK->ICKCR)& 0x02)!=0x02); CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI); CLK_SYSCLKSourceSwitchCmd(ENABLE); while (((CLK->SWCR)& 0x01)==0x01); }
void clock_config(void) { CLK_HSICmd(ENABLE); CLK_RTCClockConfig(CLK_RTCCLKSource_HSI,CLK_RTCCLKDiv_64); CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI); CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY)==RESET); CLK_PeripheralClockConfig(CLK_Peripheral_LCD, ENABLE); CLK_PeripheralClockConfig(CLK_Peripheral_RTC, ENABLE); CLK_PeripheralClockConfig(CLK_Peripheral_TIM4, ENABLE); CLK_PeripheralClockConfig(CLK_Peripheral_TIM2, ENABLE); CLK_PeripheralClockConfig(CLK_Peripheral_TIM3, ENABLE); CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE); };
/** * @brief Configure peripherals Clock * @param None * @retval None */ static void CLK_Config(void) { /* High speed internal clock prescaler: 1 */ CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); /* Select HSE as system clock source */ CLK_SYSCLKSourceSwitchCmd(ENABLE); CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSE); while (CLK_GetSYSCLKSource() != CLK_SYSCLKSource_HSE) {} /* Enable TIM4 CLK */ CLK_PeripheralClockConfig(CLK_Peripheral_TIM4, ENABLE); }
/** * @brief Main program. * @param None * @retval None */ void main(void) { /* High speed internal clock prescaler: 1*/ CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); /* Initialize I/Os in Output Mode for LEDs */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); /***********************SPI and MSD Card initialization******************/ while (SD_Detect() == SD_NOT_PRESENT); { /* Wait MicroSD card insertion */ } Delay(0xFFFF); /* Init the flash micro SD*/ Status = SD_Init(); /***************************Block Read/Write******************************/ /* Write block of 512 bytes on address 0 */ SD_WriteBlock(TxBuffer, 0, BUFFER_SIZE); /* Read block of 512 bytes from address 0 */ SD_ReadBlock(RxBuffer, 0, BUFFER_SIZE); /* Check data */ TransferStatus = Buffercmp(TxBuffer, RxBuffer, BUFFER_SIZE); if (TransferStatus != SUCCESS) { while (1) /* Go to infinite loop when there is mismatch in data programming*/ { STM_EVAL_LEDToggle(LED1); Delay((uint16_t)0xFFFF); } } while (1) { STM_EVAL_LEDToggle(LED1); STM_EVAL_LEDToggle(LED2); STM_EVAL_LEDToggle(LED3); STM_EVAL_LEDToggle(LED4); Delay((uint16_t)0xFFFF); } }
/** * @brief Configure peripheral clock * @param None * @retval None */ static void CLK_Config(void) { /* Select HSE as system clock source */ CLK_SYSCLKSourceSwitchCmd(ENABLE); CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSE); /*High speed external clock prescaler: 1*/ CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_2); while (CLK_GetSYSCLKSource() != CLK_SYSCLKSource_HSE) {} /* Enable SPI clock */ CLK_PeripheralClockConfig(CLK_Peripheral_SPI1, ENABLE); /* Enable DMA clock */ CLK_PeripheralClockConfig(CLK_Peripheral_DMA1, ENABLE); }
void main(void) { deInitClock(); deInitGPIO(); CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI); CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_16); initDigitalControls(); initAnalogControls(); GPIO_Init(GPIOF, GPIO_Pin_0, GPIO_Mode_In_FL_IT); EXTI_SetPinSensitivity(EXTI_Pin_0, EXTI_Trigger_Rising); enableInterrupts(); initMemory(); while (1) { halt(); } }
/** * @brief Main program. * @param None * @retval None */ void main(void) { /* I2C clock Enable*/ CLK_PeripheralClockConfig(CLK_Peripheral_I2C1, ENABLE); /* system_clock / 1 */ CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); /* Initialize LEDs mounted on STM8L1526-EVAL board */ STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); I2C_DeInit(I2C1); /* Initialize I2C peripheral */ #ifdef I2C_slave_7Bits_Address I2C_Init(I2C1, 100000, SLAVE_ADDRESS, I2C_Mode_I2C, I2C_DutyCycle_2, I2C_Ack_Enable, I2C_AcknowledgedAddress_7bit); #else I2C_Init(I2C1, 100000, SLAVE_ADDRESS, I2C_Mode_I2C, I2C_DutyCycle_2, I2C_Ack_Enable, I2C_AcknowledgedAddress_10bit); #endif /* Enable Error Interrupt*/ I2C_ITConfig(I2C1, (I2C_IT_TypeDef)(I2C_IT_ERR | I2C_IT_EVT | I2C_IT_BUF), ENABLE); /* Enable general interrupts */ enableInterrupts(); /*Main Loop */ while (1) { /* infinite loop */ } }
/** * @brief Configure USART peripheral * @param None * @retval None */ static void USART_Config(void) { /* High speed internal clock prescaler: 1*/ CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); /* EVAL COM (USARTx) configuration -----------------------------------------*/ /* USART configured as follow: - BaudRate = 9600 baud - Word Length = 8 Bits - One Stop Bit - No parity - Receive and transmit enabled */ STM_EVAL_COMInit(COM1, (uint32_t)9600, USART_WordLength_8b, USART_StopBits_1, USART_Parity_No, (USART_Mode_TypeDef)(USART_Mode_Tx | USART_Mode_Rx)); /* Set Prescaler*/ USART_SetPrescaler(EVAL_COM1, 0x1); USART_IrDAConfig(EVAL_COM1, USART_IrDAMode_Normal); USART_IrDACmd(EVAL_COM1, ENABLE); }
/** * @brief main entry point. * @par Parameters None * @retval void None * @par Required preconditions: None */ void main(void) { uint8_t PayloadLength, data_sensor, *bufMessage; /* deinit I/O ports */ DeInitClock(); DeInitGPIO(); /* Select HSI as system clock source */ #ifdef USE_HSI CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI); CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_16); #else CLK_SYSCLKSourceSwitchCmd(ENABLE); /* Select 2MHz HSE as system clock source */ CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSE); CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_4); CLK_HSICmd(DISABLE); #endif // Initializes the LCD glass LCD_GLASS_Init(); /* LED button init: GPIO set in push pull */ GPIO_Init( LED_GPIO_PORT, LED_GPIO_PIN, GPIO_Mode_Out_PP_Low_Fast); // set to 0 GPIOE->ODR &= ~LED_GPIO_PIN; /* USER button init: GPIO set in input interrupt active mode */ GPIO_Init( BUTTON_GPIO_PORT, USER_GPIO_PIN, GPIO_Mode_In_FL_IT); EXTI_SetPinSensitivity(EXTI_Pin_7, EXTI_Trigger_Falling); //* Init Bar on LCD all are OFF BAR0_OFF; BAR1_OFF; BAR2_OFF; BAR3_OFF; enableInterrupts(); //* At power on VDD diplays bufMessage = NDEFmessage; if (EEMenuState > STATE_TEMPMEAS) EEMenuState = STATE_CHECKNDEFMESSAGE; FLASH_Unlock(FLASH_MemType_Data ); state_machine = EEMenuState ; delayLFO_ms (1); if (EEInitial == 0) { User_WriteFirmwareVersion (); EEInitial =1; } while (1) { switch (state_machine) { case STATE_VREFF: // measure the voltage available at the output of the M24LR04E-R Vref_measure(); delayLFO_ms (2); //turn on led GPIO_SetBits(GPIOE, GPIO_Pin_6); break; case STATE_VBIO: //measure the output voltage of biosensor through Pin 7 Port E CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE); ADC_DeInit(ADC1); ADC_VrefintCmd(ENABLE); delay_10us(3); GPIO_DeInit(GPIOE); GPIO_Init(GPIOE,GPIO_Pin_7 ,/*GPIO_Mode_In_FL_No_IT*/GPIO_Mode_In_PU_No_IT); ADC_Cmd(ADC1, ENABLE); ADC_Init(ADC1, ADC_ConversionMode_Single,ADC_Resolution_12Bit, ADC_Prescaler_1); ADC_SamplingTimeConfig(ADC1, ADC_Group_FastChannels, ADC_SamplingTime_9Cycles); ADC_ChannelCmd(ADC1, ADC_Channel_3, ENABLE); delay_10us(3); // Important delay res = 0; res_2 = 0; i=0; for(i=8; i>0; i--) { /* start ADC convertion by software */ ADC_SoftwareStartConv(ADC1); /* wait until end-of-covertion */ while( ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == 0 ); /* read ADC convertion result */ res += ADC_GetConversionValue(ADC1); } /* de-initialize ADC*/ ADC_VrefintCmd(DISABLE); ADC_DeInit(ADC1); /* disable SchmittTrigger for ADC_Channel_24, to save power */ ADC_SchmittTriggerConfig(ADC1, ADC_Channel_3, DISABLE); CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, DISABLE); ADC_ChannelCmd(ADC1, ADC_Channel_3, DISABLE); res = res>>3; P_VREFINT_Factory = VREFINT_Factory_CONV_ADDRESS; #ifdef VREFINT_FACTORY_CONV if ((*P_VREFINT_Factory>VREFINT_Factory_CONV_MIN ) && (*P_VREFINT_Factory<VREFINT_Factory_CONV_MAX )) { /* If the value exists: Adds the hight byte to FullVREF_FACTORY */ FullVREF_FACTORY = VREFINT_Factory_CONV_MSB; FullVREF_FACTORY += *P_VREFINT_Factory; res_2 = (float)(FullVREF_FACTORY*VDD_FACTORY); res_2 /= res; } else { res_2 = (VREF/res) * ADC_CONV; // usally res>>3 } #else /* We use the theorcial value */ res_2 = (VREF/res) * ADC_CONV; #endif /* Vdd_appli in mV */ res_2*= 1000L; convert_into_char (res_2, tab); /* To add unit and decimal point */ tab[5] = 'V'; tab[4] = ' '; tab[1] |= DOT; /* To add decimal point for display in volt */ tab[0] = ' '; LCD_GLASS_DisplayStrDeci(tab); delayLFO_ms (2); //LCD_GLASS_DisplayString("V BIO"); break; case STATE_CHECKNDEFMESSAGE: // read the NDEF message from the M24LR04E-R EEPROM and display it if it is found if (User_ReadNDEFMessage (&PayloadLength) == SUCCESS) User_DisplayMessage (bufMessage,PayloadLength); // User_DisplayMessageActiveHaltMode (PayloadLength); else User_DisplayMessage(ErrorMessage,20); break; case STATE_TEMPMEAS: // read the ambiant tempserature from the STTS751 User_GetOneTemperature (&data_sensor); // display the temperature User_DisplayOneTemperature (data_sensor); delayLFO_ms (2); break; break; /* for safe: normaly never reaches */ default: LCD_GLASS_Clear(); LCD_GLASS_DisplayString("Error"); state_machine = STATE_VREFF; break; } } }
/** * @brief Main program. * @param None * @retval None */ void main() { /* TIM4 & I2C clock Enable*/ CLK_PeripheralClockConfig(CLK_Peripheral_TIM4, 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 push-buttons mounted on STM8L1526-EVAL board */ STM_EVAL_PBInit(BUTTON_RIGHT, BUTTON_MODE_EXTI); STM_EVAL_PBInit(BUTTON_LEFT, BUTTON_MODE_EXTI); STM_EVAL_PBInit(BUTTON_UP, BUTTON_MODE_EXTI); STM_EVAL_PBInit(BUTTON_DOWN, BUTTON_MODE_EXTI); STM_EVAL_PBInit(BUTTON_SEL, BUTTON_MODE_EXTI); /* Initialize TIM4 peripheral to generate an interrupt each 1ms */ TIM4_TimeBaseInit(TIM4_Prescaler_128, TIM4_PERIOD); /* Enable Update interrupt */ TIM4_ITConfig(TIM4_IT_Update, ENABLE); /* Initialize I2C peripheral */ I2C_Init(I2C1, I2C_SPEED, 0xA0, I2C_Mode_I2C, I2C_DutyCycle_2, I2C_Ack_Enable, I2C_AcknowledgedAddress_7bit); /* Set I2C IT software priority as highest */ #ifdef STM8L15X_MD ITC_SetSoftwarePriority(I2C1_IRQn, ITC_PriorityLevel_3); #elif defined (STM8L15X_HD) || defined (STM8L15X_MDP) ITC_SetSoftwarePriority(I2C1_SPI2_IRQn, ITC_PriorityLevel_3); #endif /* STM8L15X_MD */ enableInterrupts(); /* Enable TIM4 */ TIM4_Cmd(ENABLE); while (1) { switch (PressedButton) { case BUTTON_RIGHT: PressedButton = NO_BUTTON; NumOfBytes = 1; TxBuffer[NumOfBytes-1] = 0x01; ButtonPressed = 1; break; case BUTTON_LEFT: PressedButton = NO_BUTTON; NumOfBytes = 1; TxBuffer[NumOfBytes-1] = 0x02; ButtonPressed = 1; break; case BUTTON_UP: PressedButton = NO_BUTTON; NumOfBytes = 1; TxBuffer[NumOfBytes-1] = 0x03; ButtonPressed = 1; break; case BUTTON_DOWN: PressedButton = NO_BUTTON; NumOfBytes = 1; TxBuffer[NumOfBytes-1] = 0x04; ButtonPressed = 1; break; case BUTTON_SEL: PressedButton = NO_BUTTON; NumOfBytes = BUFFERSIZE; TxBuffer[0] = 0xAA; ButtonPressed = 1; for (i = 1; i < NumOfBytes; i++) { TxBuffer[i] = i; } break; default: break; } if (ButtonPressed == 1) { /* Enable Buffer and Event Interrupt*/ I2C_ITConfig(I2C1, (I2C_IT_TypeDef)(I2C_IT_EVT | I2C_IT_BUF) , ENABLE); /* Generate the Start condition */ I2C_GenerateSTART(I2C1, ENABLE); /* Data transfer is performed in the I2C interrupt routine */ /* Wait until end of data transfer */ while (NumOfBytes); while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)); /* Make sure that the STOP bit is cleared by Hardware before CR2 write access */ while ((I2C1->CR2 & I2C_CR2_STOP) == I2C_CR2_STOP); ButtonPressed = 0; } } }
/** * @brief main entry point. * @par Parameters None * @retval void None * @par Required preconditions: None */ void main(void) { uint8_t PayloadLength, data_sensor, *bufMessage; /* deinit I/O ports */ DeInitClock(); DeInitGPIO(); /* Select HSI as system clock source */ #ifdef USE_HSI CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI); CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_16); #else CLK_SYSCLKSourceSwitchCmd(ENABLE); /* Select 2MHz HSE as system clock source */ CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSE); CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_4); CLK_HSICmd(DISABLE); #endif // Initializes the LCD glass LCD_GLASS_Init(); /* LED button init: GPIO set in push pull */ GPIO_Init( LED_GPIO_PORT, LED_GPIO_PIN, GPIO_Mode_Out_PP_Low_Fast); // set to 0 GPIOE->ODR &= ~LED_GPIO_PIN; /* USER button init: GPIO set in input interrupt active mode */ GPIO_Init( BUTTON_GPIO_PORT, USER_GPIO_PIN, GPIO_Mode_In_FL_IT); EXTI_SetPinSensitivity(EXTI_Pin_7, EXTI_Trigger_Falling); //* Init Bar on LCD all are OFF BAR0_OFF; BAR1_OFF; BAR2_OFF; BAR3_OFF; enableInterrupts(); //* At power on VDD diplays bufMessage = NDEFmessage; if (EEMenuState > STATE_TEMPMEAS) EEMenuState = STATE_CHECKNDEFMESSAGE; FLASH_Unlock(FLASH_MemType_Data ); state_machine = EEMenuState ; delayLFO_ms (1); if (EEInitial == 0) { User_WriteFirmwareVersion (); EEInitial =1; } while (1) { switch (state_machine) { case STATE_VREF: // measure the voltage available at the output of the M24LR04E-R Vref_measure(); delayLFO_ms (2); break; case STATE_CHECKNDEFMESSAGE: // read the NDEF message from the M24LR04E-R EEPROM and display it if it is found if (User_ReadNDEFMessage (&PayloadLength) == SUCCESS) User_DisplayMessage (bufMessage,PayloadLength); // User_DisplayMessageActiveHaltMode (PayloadLength); else User_DisplayMessage((uint8_t*)ErrorMessage,20); break; case STATE_TEMPMEAS: // read the ambiant tempserature from the STTS751 User_GetOneTemperature (&data_sensor); // display the temperature User_DisplayOneTemperature (data_sensor); delayLFO_ms (2); break; break; /* for safe: normaly never reaches */ default: LCD_GLASS_Clear(); LCD_GLASS_DisplayString("Error"); state_machine = STATE_VREF; break; } } }
/** * @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 Main program. * @param None * @retval None */ void main(void) { uint32_t add, startadd, stopadd = 0; uint8_t newval = 0xAA; uint8_t i = 0; #ifdef _COSMIC_ /* Call the _fctcpy() function with the first segment character as parameter "_fctcpy('F');" for a manual copy of the declared moveable code segment (FLASH_CODE) in RAM before execution*/ _fctcpy('F'); #endif /*_COSMIC_*/ #ifdef _RAISONANCE_ /* Call the standard C library: memcpy() or fmemcpy() functions available through the <string.h> to copy the inram function to the RAM destination address */ MEMCPY(FLASH_EraseBlock, (void PointerAttr*)&__address__FLASH_EraseBlock, (int)&__size__FLASH_EraseBlock); MEMCPY(FLASH_ProgramBlock, (void PointerAttr*)&__address__FLASH_ProgramBlock, (int)&__size__FLASH_ProgramBlock); #endif /*_RAISONANCE_*/ /* Initialize I/Os in Output Mode */ STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); /* High speed internal clock prescaler */ CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); /* Define flash programming Time*/ FLASH_SetProgrammingTime(FLASH_ProgramTime_Standard); FLASH_Unlock(FLASH_MemType_Program); /* Wait until Flash Program area unlocked flag is set*/ while (FLASH_GetFlagStatus(FLASH_FLAG_PUL) == RESET) {} /* Unlock flash data eeprom memory */ FLASH_Unlock(FLASH_MemType_Data); /* Wait until Data EEPROM area unlocked flag is set*/ while (FLASH_GetFlagStatus(FLASH_FLAG_DUL) == RESET) {} /* Fill the buffer in RAM */ for (i = 0; i < FLASH_BLOCK_SIZE; i++) { GBuffer[i] = newval; } /* This function is executed from RAM */ FLASH_ProgramBlock(BLOCK_OPERATION, FLASH_MemType_Data, FLASH_ProgramMode_Standard, GBuffer); /* Wait until End of high voltage flag is set*/ while (FLASH_GetFlagStatus(FLASH_FLAG_HVOFF) == RESET) {} /* Check the programmed block */ startadd = FLASH_DATA_EEPROM_START_PHYSICAL_ADDRESS + ((uint16_t)BLOCK_OPERATION * (uint16_t)FLASH_BLOCK_SIZE); stopadd = startadd + (uint16_t)FLASH_BLOCK_SIZE; for (add = startadd; add < stopadd; add++) { if (FLASH_ReadByte(add) != newval) { /* Error */ OperationStatus = FAILED; /* OperationStatus = PASSED, if the data written/read to/from Flash program memory is correct */ /* OperationStatus = FAILED, if the data written/read to/from Flash program memory is corrupted */ while (1) { STM_EVAL_LEDToggle(LED1); /*FAIL: write error */ Delay(0xFFFF); } } } /* Erase block 0 and verify it */ /* This function is executed from RAM */ FLASH_EraseBlock(BLOCK_OPERATION, FLASH_MemType_Data); /* Wait until End of high voltage flag is set*/ while (FLASH_GetFlagStatus(FLASH_FLAG_HVOFF) == RESET) {} for (add = startadd; add < stopadd; add++) { if (FLASH_ReadByte(add) != 0x00) { /* Error */ OperationStatus = FAILED; /* OperationStatus = PASSED, if the data written/read to/from Flash program memory is correct */ /* OperationStatus = FAILED, if the data written/read to/from Flash program memory is corrupted */ while (1) { STM_EVAL_LEDToggle(LED2); /* FAIL: Erase error */ Delay(0xFFFF); } } } /* Pass */ OperationStatus = PASSED; /* OperationStatus = PASSED, if the data written/read to/from Flash program memory is correct */ /* OperationStatus = FAILED, if the data written/read to/from Flash program memory is corrupted */ while (1) { STM_EVAL_LEDToggle(LED3); /* PASS: without errors*/ Delay(0xFFFF); } }
void clk_config(void) { CLK_DeInit(); CLK_HSICmd(ENABLE); CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); }