// generate one random number uint32_t get_random_number() { /* Wait until one RNG number is ready */ while(RNG_GetFlagStatus(RNG_FLAG_DRDY)== RESET); /* Get a 32bit Random number */ uint32_t r = RNG_GetRandomNumber(); return r; }
uint32_t GetRandomInteger(void) { if (!RNGInitialized) { //ERROR return 0; } while (RNG_GetFlagStatus(RNG_FLAG_DRDY) == RESET); return RNG_GetRandomNumber(); }
/* randomNumber(void) is a funtion that returns a random that corresponds to a time period between 1 and 2 second */ int randomNumber(void){ rand = RNG_GetRandomNumber()/120000; //call to the random number generator/1200000 to get teh random number //the following is a test to see if the number returned is between 6000 and 12000 //if not call randomNumber if(rand > 6000 && rand < 12000){ return rand; }else{ randomNumber(); } //the else clause is what causes for the delay. }
uint32_t SRAM_Test(void) { uint32_t adr = 0; uint32_t error = 0; uint16_t *prnd; uint16_t *pmem; uint16_t temp; prnd = malloc(512); RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_RNG, ENABLE); RNG_Cmd(ENABLE); for(adr = 0; adr < 512; adr++) { while(RNG_GetFlagStatus(RNG_FLAG_DRDY) == RESET); *(prnd++) = (uint16_t)RNG_GetRandomNumber(); } RNG_Cmd(DISABLE); RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_RNG,DISABLE); prnd -= 512; pmem = (uint16_t*)sram_bank3; temp = *prnd; for(adr = 0; adr < 524288; adr++) { if((adr & 0x3ff) == 0x3ff) temp = *(prnd++); temp = (temp + 1024) * 5; *(pmem++) = temp; } for(int test_num = 0; test_num < 3; test_num++) { prnd -= 512; pmem = (uint16_t*)sram_bank3; temp = *prnd; for(adr = 0; adr < 524288; adr++) { if((adr & 0x3ff) == 0x3ff) temp = *(prnd++); temp = (temp + 1024) * 5; if(temp != *(pmem++)) error++; } } free(prnd); return error; }
void vTaskTest( void* pvParam ) { u8 t, i; Uart_putstr( "vTaskTest start" ) ; RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_RNG,ENABLE); RNG_Cmd(ENABLE); while( 1 ) { xQueueSend( xqh_Tpframe, (void*)NULL, 0 ) ; while(RNG_GetFlagStatus(RNG_FLAG_DRDY) == RESET); t = RNG_GetRandomNumber(); for( i = 0; i < t; i++ ) vTaskDelay( 100/portTICK_RATE_MS ) ; } }
void rng_irq_handler(void) { portBASE_TYPE shouldYield = pdFALSE; uint32_t word = 0; if(RNG_GetFlagStatus(RNG_FLAG_DRDY) == SET) { word = RNG_GetRandomNumber(); if(pdTRUE != xQueueSendFromISR(rng_buffer, &word, &shouldYield)) { RNG_ITConfig(DISABLE); } } if(pdTRUE == shouldYield) { taskYIELD(); } }
/** * @brief Main program * @param None * @retval None */ int main(void) { uint32_t random32bit = 0; uint32_t counter = 0; /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s/startup_stm32f429_439xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ /* Display init (LCD or/and USART)*/ Display_Init(); /* Key Button configuration */ STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_GPIO); /* RNG configuration */ RNG_Config(); while (1) { /* Wait until Key button is pressed */ while(STM_EVAL_PBGetState(BUTTON_KEY) != RESET) { } /* Loop while Key button is maintained pressed */ while(STM_EVAL_PBGetState(BUTTON_KEY) == RESET) { } for(counter = 0; counter < 8; counter++) { /* Wait until one RNG number is ready */ while(RNG_GetFlagStatus(RNG_FLAG_DRDY)== RESET) { } /* Get a 32bit Random number */ random32bit = RNG_GetRandomNumber(); /* Display the Random number value on the LCD or/and USART */ Display(random32bit, counter+1); } } }
//give a random number from 0 to 9 uint8_t Nomber_RNG() { uint32_t Rand=RNG_GetRandomNumber(); uint8_t d = 0x0B; uint8_t i; for (i = 9; i > 0; i--) { if((Rand<((OTREZOK)*(i)))&(Rand>((OTREZOK)*(i-1)))) { d--; break; } d--; } return d; }
long random(long from, long to) { return from + (RNG_GetRandomNumber() % (to - from)); }
/** * @Function u32 RNG_Get_RandomNum(void); * @Description Get_RandomNum * @Return u32 得到随机数 */ u32 RNG_Get_RandomNum(void) { while(RNG_GetFlagStatus(RNG_FLAG_DRDY)==RESET); //等待随机数就绪 return RNG_GetRandomNumber(); }
//------------------------------------------------------------- __inline uint32_t GetRngValue() { return RNG_GetRandomNumber(); }
/* This function runs before the OS start. */ void NetworkConnecting() { int i, p, test, test1; int sendcounter = 0; u32 *pu32 = NULL; test1 = Timer2Counter; test = Timer2Counter; sPacketbuf[DES_NUM] = SELFADDRESS; sPacketbuf[SRC_NUM] = SELFADDRESS; CC2500_SeleChannel(0); CC2500_SetRecvAddr(SELFADDRESS); CC2500_SetRxd(); CC2500_SendPacket(sPacketbuf,PACKET_LEN); CC2500_SetRxd(); sPacketbuf[DES_NUM]=0x00; NbrList[0]=NbrCount; // while(RESET==RNG_GetFlagStatus(RNG_FLAG_DRDY)); // Delay_100us(RNG_GetRandomNumber() & 0x000000f); pu32 = (u32*) &sPacketbuf[1]; while (0 == NodeFlag[SELFADDRESS]) { if (ISIDLE) { NbrList[0] = NbrCount; sPacketbuf[PRC_NUM] = DISCOVERY_MESSAGE; arm_copy_q7(NbrList, sPacketbuf+LEN_NUM, NbrList[0]+1); pu32[CRC_LENGTH - 2] = Timer2Counter;//Add the timestamp. CRC_ResetDR(); pu32[CRC_LENGTH - 1] = CRC_CalcBlockCRC(pu32, CRC_LENGTH - 1); p = Bernoulli(1.0/(MAXNUM-NbrCount+1)); //Delay_100us(1); if (ISIDLE) { if (0 == DelayFlag) { if (p/*Bernoulli(1.0/(MAXNUM-NbrCount+1))*/) { CC2500_SendPacket(sPacketbuf, PACKET_LEN); CC2500_SetRxd(); DelayFlag=1; } } else { Preemptive_Delay_100us(25, &DelayFlag); DelayFlag=0; } } } } test1 = Timer2Counter - test1; while(MAXNUM > NbrCount) { if (0 != DiscoveryHelp) { if (ISIDLE) { Delay_100us(1); if (ISIDLE) { if (Bernoulli(1.0/(MAXNUM-NbrCount+1))) { NbrList[0] = NbrCount; sPacketbuf[PRC_NUM] = DISCOVERY_MESSAGE; arm_copy_q7(NbrList, sPacketbuf+LEN_NUM, NbrList[0]+1); pu32[CRC_LENGTH - 2] = Timer2Counter;//Add the timestamp. CRC_ResetDR(); pu32[CRC_LENGTH - 1] = CRC_CalcBlockCRC(pu32, CRC_LENGTH - 1); CC2500_SendPacket(sPacketbuf, PACKET_LEN); CC2500_SetRxd(); } } } DiscoveryHelp = 0; } } test = Timer2Counter - test; while(RESET==RNG_GetFlagStatus(RNG_FLAG_DRDY)); Delay_100us(RNG_GetRandomNumber() & 0x000000f); pu32[CRC_LENGTH - 2] = Timer2Counter;//Add the timestamp. sendcounter = 3; while (0 != sendcounter) { if (ISIDLE) { Delay_100us(1); if (ISIDLE) { NbrList[0] = NbrCount; sPacketbuf[PRC_NUM] = DISCOVERY_ACCOMPLISHED; arm_copy_q7(NbrList, sPacketbuf+LEN_NUM, NbrList[0]+1); CRC_ResetDR(); pu32[CRC_LENGTH - 1] = CRC_CalcBlockCRC(pu32, CRC_LENGTH - 1); CC2500_SendPacket(sPacketbuf, PACKET_LEN); CC2500_SetRxd(); sendcounter--; } } Delay_100us(10); } printf("NeighbourList:\r\n"); for (i=0;i<MAXNUM;i++) { printf("%#x\r\n",NbrList[1+i]); } printf("t1 = %d\r\n", test1); printf("t = %d\r\n", test); }
uint32_t rng_get(void) { while(RNG_GetFlagStatus(RNG_FLAG_DRDY)== RESET); return RNG_GetRandomNumber(); }
/** * @brief Start recording * @param None * @retval None */ static void RECORDER_Start (void) { GL_PageControls_TypeDef* item; uint8_t time[6]; RTC_TimeTypeDef RTC_TimeStructure; RTC_DateTypeDef RTC_DateStructure; /*Create file name */ if ( RTC_Error == 0) { RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure); RTC_GetDate(RTC_Format_BIN, &RTC_DateStructure); sprintf((char *)RecFileName, "Rec_%02d%02d%d%02d%02d%02d.wav", RTC_DateStructure.RTC_Date, RTC_DateStructure.RTC_Month, RTC_DateStructure.RTC_Year + 2000, RTC_TimeStructure.RTC_Hours, RTC_TimeStructure.RTC_Minutes, RTC_TimeStructure.RTC_Seconds); } else { /* Wait until one RNG number is ready */ while(RNG_GetFlagStatus(RNG_FLAG_DRDY)== RESET) { } RecoveryRecorderCounter = RNG_GetRandomNumber() & 0x7FFFFFFF; sprintf((char *)RecFileName, "Rec_%014d.wav", (int)RecoveryRecorderCounter); } RECORDER_GetDefaultDirectory(); strcat ((char *)REC_DefaultPath,"/"); strcat((char *)REC_DefaultPath, (char *)RecFileName); RefreshLabel(RecordPage , REC_FILNAME_ID, (uint8_t *)" "); if (AudioRecorder_Open(REC_DefaultPath)) { memset (RecFileName, 0, sizeof(RecFileName)); RefreshLabel(RecordPage , REC_FILNAME_ID, (uint8_t *)"Error Open File !"); return; } else { RefreshLabel(RecordPage , REC_FILNAME_ID, RecFileName); } /* Graphical update */ DestroyPageControl (RecordPage, REC_STRT_ID); RefreshPageControl(RecordPage, REC_STRT_ID); DestroyPageControl (RecordPage, REC_PLAY_ID); RefreshPageControl(RecordPage, REC_PLAY_ID); DestroyPageControl (RecordPage, REC_PAUSE_ID); RefreshPageControl(RecordPage, REC_PAUSE_ID); item = NewIcon (REC_STOP_ID, rec_stop_icon, 40, 40, RECORDER_Stop); AddPageControlObj(170,135,item, RecordPage); RefreshPageControl(RecordPage, REC_STOP_ID); item = NewIcon (REC_PAUSE_ID, rec_pause_icon, 40, 40, RECORDER_Pause); AddPageControlObj(110,135,item, RecordPage); RefreshPageControl(RecordPage, REC_PAUSE_ID); item = NewIcon (REC_CANCEL_ID, rec_cancel_icon, 40, 40, RECORDER_Cancel); AddPageControlObj(50,135,item, RecordPage); RefreshPageControl(RecordPage, REC_CANCEL_ID); sprintf((char *)time, "%02d:%02d", 0, 0); RefreshLabel(RecordPage, REC_TIME_ID, time); REC_GL_State = REC_GL_RECORDING; Recorder_UsedStorage = REC_DefaultPath[0]; AudioRecorder_Start(); }
/** * @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_stm32f4xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ //initiate user button //PB_Config(); STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI); //initiate LEDs and turn them on LED_Config(); /* ----------------------------------------------------------------------- TIM3 Configuration: Output Compare Timing Mode: In this example TIM3 input clock (TIM3CLK) is set to 2 * APB1 clock (PCLK1), since APB1 prescaler is different from 1. TIM3CLK = 2 * PCLK1 PCLK1 = HCLK / 4 => TIM3CLK = HCLK / 2 = SystemCoreClock /2 To get TIM3 counter clock at 50 MHz, the prescaler is computed as follows: Prescaler = (TIM3CLK / TIM3 counter clock) - 1 Prescaler = ((SystemCoreClock /2) /0.5 MHz) - 1 CC1 update rate = TIM3 counter clock / CCR1_Val = 10.0 Hz ==> Toggling frequency = 5 Hz Note: SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file. Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate() function to update SystemCoreClock variable value. Otherwise, any configuration based on this variable will be incorrect. ----------------------------------------------------------------------- */ //=======================Configure and init Timer====================== /* Compute the prescaler value */ PrescalerValue = (uint16_t) ((SystemCoreClock / 2) / 500000) - 1; //configures clock speed at 500 KHz. Both Tim2 and Tim3 use the same prescsaler and therefore run at the same speed. /* TIM Configuration */ TIM3_Config(); TIM2_Config(); // configure the output compare TIM3_OCConfig(); TIM2_OCConfig(); /* TIM Interrupts enable */ TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE); TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE); /* TIM3 enable counter */ TIM_Cmd(TIM3, ENABLE); TIM_Cmd(TIM2, ENABLE); //======================================configure and init LCD ====================== /* LCD initiatization */ LCD_Init(); /* LCD Layer initiatization */ LCD_LayerInit(); /* Enable the LTDC */ LTDC_Cmd(ENABLE); /* Set LCD foreground layer */ LCD_SetLayer(LCD_FOREGROUND_LAYER); //================EEPROM init==================================== /* Unlock the Flash Program Erase controller */ FLASH_Unlock(); /* EEPROM Init */ EE_Init(); //============ Set up for random number generation============== RNG_Config(); Ext_PushButton_Interrupt(); //configures external push button //with the default font, LCD can display 12 lines of chars, they are LINE(0), LINE(1)...LINE(11) //with the default font, LCD can display 15 columns, they are COLUMN(0)....COLUMN(14) LCD_Clear(LCD_COLOR_WHITE); //change the background colour of LCD //Display a string in one line, on the first line (line=0) LCD_DisplayString(0, 2, (uint8_t *) "Best: "); //the line will not wrap while (1){ if (UBPressed==1) { //press user button if (pause==1){ //pause mode randnum = ((RNG_GetRandomNumber()%2000)+1000); //generates a random number between 1000 and 3000 Pause_Random(randnum); //see below function to see how the pause is implemented } else { //measure time mode TIM_ITConfig(TIM2, TIM_IT_CC1, DISABLE); //turns off timer 2 TIM_Cmd(TIM2, DISABLE); time = timer; //gets user's time if (initial == 1) { //sets initial best_time to first time best_time = time; initial = 0; LCD_DisplayInt((uint16_t) 0, (uint16_t) 7, best_time); } LCD_DisplayString(2, 1, (uint8_t *) "Time: "); //print time LCD_DisplayString(2, 7, (uint8_t *) " "); //clears line LCD_DisplayInt((uint16_t) 2, (uint16_t) 7, time); //displays user's time LCD_DisplayString(2, 11, (uint8_t *) "ms"); //print ms if (time > 10 && time < best_time) { //set new best time best_time = time; LCD_DisplayString(0, 7, (uint8_t *) " "); //clears line LCD_DisplayInt((uint16_t) 0, (uint16_t) 7, best_time); } TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE); //turns on timer 2 TIM_Cmd(TIM2, ENABLE); pause = 1; //this makes it so that you can use the user button to repeat the cycle in case you don't have an external push button } UBPressed=0; } } }
/** * @brief Save the image frame into a file * @param Storage path * @param file_name * @retval Status 0 = Sucess, !0 :error */ static uint8_t Save_Bmp_To_File (uint8_t *path , uint8_t *file_name) { RTC_TimeTypeDef RTC_TimeStructure; RTC_DateTypeDef RTC_DateStructure; uint32_t i = 0 ,j = 0 , Index; FIL file; for ( i = 0 ; i < IMAGE_COLUMN_SIZE; i++) { for ( j= 0 ; j < 2 * IMAGE_LINE_SIZE; j++) { ImageBuffer.SrcData[j + (2 * IMAGE_LINE_SIZE * i)] = ImageBuffer.RawData[j + (2 * IMAGE_LINE_SIZE * ( IMAGE_COLUMN_SIZE- 1 - i))]; } } /* Convert RGB16 image to RGB24 */ RGB16toRGB24(ImageBuffer.DestData, ImageBuffer.SrcData, 1); /* Update file name */ strcpy((char *)ImageBuffer.ImageName , (char *)path), strcat ((char *)ImageBuffer.ImageName, "/"); if ( RTC_Error == 0) { RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure); RTC_GetDate(RTC_Format_BIN, &RTC_DateStructure); sprintf((char *)file_name, "Camera_%02d%02d%d%02d%02d%02d.bmp", RTC_DateStructure.RTC_Date, RTC_DateStructure.RTC_Month, RTC_DateStructure.RTC_Year + 2000, RTC_TimeStructure.RTC_Hours, RTC_TimeStructure.RTC_Minutes, RTC_TimeStructure.RTC_Seconds); } else { /* Wait until one RNG number is ready */ while(RNG_GetFlagStatus(RNG_FLAG_DRDY)== RESET) { } RecoveryImageCounter = (uint32_t )(RNG_GetRandomNumber() & 0x7FFFFFFF) ; sprintf((char *)file_name, "Camera_%014d.bmp", (int)RecoveryImageCounter); } strcat ((char *)ImageBuffer.ImageName, (char *)file_name); /* Can not create file */ if (f_open(&file, (char *)ImageBuffer.ImageName, FA_CREATE_NEW | FA_WRITE) != FR_OK) { return 0; } /* Write the received data into the file */ if (f_write(&file, (char*)ImageBuffer.ImageHeader, RGB_HEADER_SIZE, (UINT*)&Index)) { f_close(&file); return 0; } if (f_write(&file, (char*)ImageBuffer.DestData, MAX_IMAGE_SIZE, (UINT*)&Index)) { f_close(&file); return 0; } /* Close file */ f_close(&file); return 1 ; }