int from_sys_clock ( int utc ) { struct timeval tv = { 0, 0 }; struct timezone tz = { 0, 0 }; if ( gettimeofday ( &tv, &tz )) bb_perror_msg_and_die ( "gettimeofday() failed" ); write_rtc ( tv. tv_sec, utc ); return 0; }
//Interrupt receptor void interupt_handler(int time) { int ipc_status; message msg; //int irq_set=timer_subscribe_int(); int error; //ligar RTC unsigned long regB; counter = 0; regB = read_rtc(RTC_REG_B); regB = regB ^ RTC_UIE; write_rtc(RTC_REG_B,regB); while(counter<(time*60) && check_number_of_tries()==0) { /* Get a request message. */ error = driver_receive(ANY, &msg, &ipc_status); if (error != 0 ) { printf("driver_receive failed with: %d",error); continue; } if (is_ipc_notify(ipc_status)) { /* received notification */ switch (_ENDPOINT_P(msg.m_source)) { case HARDWARE: /* hardware interrupt notification */ /*if ((msg.NOTIFY_ARG & BIT(0)) && (counter%60==0)) //subscribed interruptTimer { //timer_int_handler(counter); //same function as in lab3 RTC_handler_date(); counter++; } else*/ if(msg.NOTIFY_ARG & BIT(1)) { keyboard_handler(); //different function from the original } break; default: break; /* no other notifications expected: do nothing */ } } else { /* received a standard message, not a notification */ /* no standard messages expected: do nothing */ } counter++; } return; }
void reset_rtc(void) { // fputs("# invalid date, resetting clock",rs232); write_rtc(0x00,0x00); /* enable oscillator and set seconds to zero */ write_rtc(0x06,0x0a); /* 2010 */ write_rtc(0x05,0x01); /* January */ write_rtc(0x04,0x01); /* 1st */ write_rtc(0x02,0x00); /* 00 */ write_rtc(0x01,0x00); /* 00 */ }
void set_nist_time(){ int dst, health; struct tm t; unsigned long longsec; char response[100]; int bytes_read,total_bytes; tcp_Socket sock; longword nist_server; printf("Trying to set time from NIST server...\n"); //get the time if (!(nist_server = resolve(NIST_SERVER))) { printf(" ! Could not resolve time host\n"); exit( 3 ); } if( !tcp_open(&sock,0,nist_server,NIST_PORT,NULL)){ printf(" ! Unable to connect to time server\n"); exit( 3 ); } while (!sock_established(&sock) && sock_bytesready(&sock)==-1){ tcp_tick(NULL); } sock_mode(&sock, TCP_MODE_ASCII); total_bytes=0; do{ bytes_read=sock_fastread(&sock,response+total_bytes,sizeof(response)-1-total_bytes); total_bytes+=bytes_read; } while(tcp_tick(&sock) && (total_bytes < sizeof(response)-2)); //parse it t.tm_year = 100 + 10*(response[7]-'0') + (response[8]-'0'); t.tm_mon = 10*(response[10]-'0') + (response[11]-'0'); t.tm_mday = 10*(response[13]-'0') + (response[14]-'0'); t.tm_hour = 10*(response[16]-'0') + (response[17]-'0'); t.tm_min = 10*(response[19]-'0') + (response[20]-'0'); t.tm_sec = 10*(response[22]-'0') + (response[23]-'0'); dst = 10*(response[25]-'0') + (response[26]-'0'); health = response[28]-'0'; longsec = mktime(&t); longsec += 3600ul * NIST_TIMEZONE; // adjust for timezone if (dst != 0) longsec += 3600ul; // DST is in effect if (health < 2) write_rtc(longsec); printf(" Time set to : "); print_time(read_rtc()); }
// if arg=NULL, time stamp will sync to system clock int SYSTEM_SetTime(struct timeval* pstVal) { int nRet = 0; unsigned long long u64Pts = 0; struct timeval stVal = {0}; if(pstVal){ stVal = *pstVal; nRet = settimeofday(pstVal, NULL); assert(0 == nRet); write_rtc(pstVal->tv_sec); sleep(2); }else{ gettimeofday(&stVal, NULL); } u64Pts = stVal.tv_sec; u64Pts *= 1000000; u64Pts += stVal.tv_usec; DVR_ASSERT(HI_MPI_SYS_InitPtsBase(u64Pts)); return 0; }
/* Check if MAGIC is valid and print the Multiboot information structure pointed by ADDR. */ void entry (unsigned long magic, unsigned long addr) { multiboot_info_t *mbi; /* Clear the screen. */ clear(); paging_init(); /* Am I booted by a Multiboot-compliant boot loader? */ if (magic != MULTIBOOT_BOOTLOADER_MAGIC) { printf ("Invalid magic number: 0x%#x\n", (unsigned) magic); return; } /* Set MBI to the address of the Multiboot information structure. */ mbi = (multiboot_info_t *) addr; /* Print out the flags. */ printf ("flags = 0x%#x\n", (unsigned) mbi->flags); /* Are mem_* valid? */ if (CHECK_FLAG (mbi->flags, 0)) printf ("mem_lower = %uKB, mem_upper = %uKB\n", (unsigned) mbi->mem_lower, (unsigned) mbi->mem_upper); /* Is boot_device valid? */ if (CHECK_FLAG (mbi->flags, 1)) printf ("boot_device = 0x%#x\n", (unsigned) mbi->boot_device); /* Is the command line passed? */ if (CHECK_FLAG (mbi->flags, 2)) printf ("cmdline = %s\n", (char *) mbi->cmdline); if (CHECK_FLAG (mbi->flags, 3)) { int mod_count = 0; int i; module_t* mod = (module_t*)mbi->mods_addr; while(mod_count < mbi->mods_count) { printf("Module %d loaded at address: 0x%#x\n", mod_count, (unsigned int)mod->mod_start); printf("Module %d ends at address: 0x%#x\n", mod_count, (unsigned int)mod->mod_end); printf("First few bytes of module:\n"); for(i = 0; i<16; i++) { printf("0x%x ", *((char*)(mod->mod_start+i))); } printf("\n"); mod_count++; } } /* Bits 4 and 5 are mutually exclusive! */ if (CHECK_FLAG (mbi->flags, 4) && CHECK_FLAG (mbi->flags, 5)) { printf ("Both bits 4 and 5 are set.\n"); return; } /* Is the section header table of ELF valid? */ if (CHECK_FLAG (mbi->flags, 5)) { elf_section_header_table_t *elf_sec = &(mbi->elf_sec); printf ("elf_sec: num = %u, size = 0x%#x," " addr = 0x%#x, shndx = 0x%#x\n", (unsigned) elf_sec->num, (unsigned) elf_sec->size, (unsigned) elf_sec->addr, (unsigned) elf_sec->shndx); } /* Are mmap_* valid? */ if (CHECK_FLAG (mbi->flags, 6)) { memory_map_t *mmap; printf ("mmap_addr = 0x%#x, mmap_length = 0x%x\n", (unsigned) mbi->mmap_addr, (unsigned) mbi->mmap_length); for (mmap = (memory_map_t *) mbi->mmap_addr; (unsigned long) mmap < mbi->mmap_addr + mbi->mmap_length; mmap = (memory_map_t *) ((unsigned long) mmap + mmap->size + sizeof (mmap->size))) printf (" size = 0x%x, base_addr = 0x%#x%#x\n" " type = 0x%x, length = 0x%#x%#x\n", (unsigned) mmap->size, (unsigned) mmap->base_addr_high, (unsigned) mmap->base_addr_low, (unsigned) mmap->type, (unsigned) mmap->length_high, (unsigned) mmap->length_low); } /* Construct an LDT entry in the GDT */ { seg_desc_t the_ldt_desc; the_ldt_desc.granularity = 0; the_ldt_desc.opsize = 1; the_ldt_desc.reserved = 0; the_ldt_desc.avail = 0; the_ldt_desc.present = 1; the_ldt_desc.dpl = 0x0; the_ldt_desc.sys = 0; the_ldt_desc.type = 0x2; SET_LDT_PARAMS(the_ldt_desc, &ldt, ldt_size); ldt_desc_ptr = the_ldt_desc; lldt(KERNEL_LDT); } /* Construct a TSS entry in the GDT */ { seg_desc_t the_tss_desc; the_tss_desc.granularity = 0; the_tss_desc.opsize = 0; the_tss_desc.reserved = 0; the_tss_desc.avail = 0; the_tss_desc.seg_lim_19_16 = TSS_SIZE & 0x000F0000; the_tss_desc.present = 1; the_tss_desc.dpl = 0x0; the_tss_desc.sys = 0; the_tss_desc.type = 0x9; the_tss_desc.seg_lim_15_00 = TSS_SIZE & 0x0000FFFF; SET_TSS_PARAMS(the_tss_desc, &tss, tss_size); tss_desc_ptr = the_tss_desc; tss.ldt_segment_selector = KERNEL_LDT; tss.ss0 = KERNEL_DS; tss.esp0 = 0x800000; ltr(KERNEL_TSS); } idt_desc_t interrupt; interrupt.seg_selector = KERNEL_CS; interrupt.dpl = 0; interrupt.size = 1; interrupt.reserved0 = 0; interrupt.reserved1 = 1; interrupt.reserved2 = 1; interrupt.reserved3 = 0; interrupt.reserved4 = 0; interrupt.present = 1; idt_desc_t divide_error_desc = interrupt; SET_IDT_ENTRY(divide_error_desc, divide_error); idt[0] = divide_error_desc; idt_desc_t reserved_desc = interrupt; SET_IDT_ENTRY(reserved_desc, reserved); idt[1] = reserved_desc; idt_desc_t nmi_interrupt_desc = interrupt; SET_IDT_ENTRY(nmi_interrupt_desc, nmi_interrupt); idt[2] = nmi_interrupt_desc; idt_desc_t breakpoint_desc = interrupt; SET_IDT_ENTRY(breakpoint_desc, breakpoint); idt[3] = breakpoint_desc; idt_desc_t overflow_desc = interrupt; SET_IDT_ENTRY(overflow_desc, overflow); idt[4] = overflow_desc; idt_desc_t bound_range_exceeded_desc = interrupt; SET_IDT_ENTRY(bound_range_exceeded_desc, bound_range_exceeded); idt[5] = bound_range_exceeded_desc; idt_desc_t invalid_opcode_desc = interrupt; SET_IDT_ENTRY(invalid_opcode_desc, invalid_opcode); idt[6] = invalid_opcode_desc; idt_desc_t device_not_available_desc = interrupt; SET_IDT_ENTRY(device_not_available_desc, device_not_available); idt[7] = device_not_available_desc; idt_desc_t double_fault_desc = interrupt; SET_IDT_ENTRY(double_fault_desc, double_fault); idt[8] = double_fault_desc; idt_desc_t coprocessor_segment_overrun_desc = interrupt; SET_IDT_ENTRY(coprocessor_segment_overrun_desc, coprocessor_segment_overrun); idt[9] = divide_error_desc; idt_desc_t invalid_tss_desc = interrupt; SET_IDT_ENTRY(invalid_tss_desc, invalid_tss); idt[10] = invalid_tss_desc; idt_desc_t segment_not_present_desc = interrupt; segment_not_present_desc.present = 0; SET_IDT_ENTRY(segment_not_present_desc, segment_not_present); idt[11] = segment_not_present_desc; idt_desc_t stack_segment_fault_desc = interrupt; SET_IDT_ENTRY(stack_segment_fault_desc, stack_segment_fault); idt[12] = stack_segment_fault_desc; idt_desc_t general_protection_desc = interrupt; SET_IDT_ENTRY(general_protection_desc, general_protection); idt[13] = general_protection_desc; idt_desc_t page_fault_desc = interrupt; SET_IDT_ENTRY(page_fault_desc, page_fault); idt[14] = page_fault_desc; idt_desc_t math_fault_desc = interrupt; SET_IDT_ENTRY(math_fault_desc, math_fault); idt[16] = math_fault_desc; idt_desc_t alignment_check_desc = interrupt; SET_IDT_ENTRY(alignment_check_desc, alignment_check); idt[17] = alignment_check_desc; idt_desc_t machine_check_desc = interrupt; SET_IDT_ENTRY(machine_check_desc, machine_check); idt[18] = machine_check_desc; idt_desc_t simd_desc = interrupt; SET_IDT_ENTRY(simd_desc, simd_floating_exception); idt[19] = simd_desc; idt_desc_t keyboard_desc = interrupt; SET_IDT_ENTRY(keyboard_desc, &keyboard_wrapper); idt[0x21] = keyboard_desc; idt_desc_t rtc_desc = interrupt; SET_IDT_ENTRY(rtc_desc, &rtc_wrapper); idt[0x28] = rtc_desc; // load idt lidt(idt_desc_ptr); /* Init the PIC */ i8259_init(); /* Initialize devices, memory, filesystem, enable device interrupts on the * PIC, any other initialization stuff... */ // initialize rtc rtc_init(); // enable irq1 for keyboard enable_irq(1); /* Enable interrupts */ /* Do not enable the following until after you have set up your * IDT correctly otherwise QEMU will triple fault and simple close * without showing you any output */ printf("Enabling Interrupts\n"); sti(); int freq = 0x0010; write_rtc((char *) (&freq)); // while(1) { // printf("h"); // read_rtc(); // } // int x = 3 / 0; // int * x = 0x12345000; // int y; // y = *x; /* Execute the first program (`shell') ... */ /* Spin (nicely, so we don't chew up cycles) */ asm volatile(".1: hlt; jmp .1;"); }
/*----------------------------------------------------------------------------*/ void ap_handle_hotkey(u8 key) { u8 res; switch (key) { case MSG_POWER_DOWN: if(sys_pwr_flag==0){ sys_pwr_flag =1; sys_mute_flag =1; dac_mute_control(sys_mute_flag,1); //调节音量时,自动UNMUTE work_mode = IDLE_MODE; put_msg_lifo(MSG_CHANGE_WORK_MODE); } else{ sys_mute_flag =0; //dac_mute_control(sys_mute_flag,1); //调节音量时,自动UNMUTE sys_pwr_flag =0; //work_mode = IDLE_MODE; } break; case MSG_MUTE_UNMUTE: sys_mute_flag=~sys_mute_flag; //dac_mute_toggle(); dac_mute_control(sys_mute_flag,1); //调节音量时,自动UNMUTE break; #if 1 case MSG_MUSIC_PREV_FILE: //选择上一个文件进行播放 case MSG_MUSIC_FR: //启动快退 if ((disp_scenario == DISP_RTC_SCEN)&&(rtc_setting_flag!=0)) { rtc_set_cnt=30; curr_time_minus(); write_rtc(&curr_time); //write_next_alm_sec(); disp_port(MENU_RTC); } break; case MSG_MUSIC_NEXT_FILE: //选择下一个文件进行播放 case MSG_MUSIC_FF: //启动快退 if ((disp_scenario == DISP_RTC_SCEN)&&(rtc_setting_flag!=0)) { rtc_set_cnt=30; curr_time_plus(); write_rtc(&curr_time); //write_next_alm_sec(); disp_port(MENU_RTC); } break; case MSG_NEXT_WORKMODE: //set_brightness_all_on(); if(work_mode==FM_RADIO_MODE)break; if(rtc_setting_flag!=0){ rtc_coordinate++; rtc_set_cnt=30; if(rtc_coordinate>4){ rtc_coordinate = 3; rtc_setting_flag = 0x00; } break; } if(disp_scenario == DISP_NORMAL){ input_number_en = 0; //允许数字输入功能 disp_scenario = DISP_RTC_SCEN; rtc_disp_hdlr(); } else{ if(device_online==0)break; input_number_en = 1; //允许数字输入功能 disp_scenario = DISP_NORMAL; disp_port(MENU_MUSIC_MAIN); } break; case MSG_TIME_SETTING: //set_brightness_all_on(); if(disp_scenario == DISP_RTC_SCEN){ rtc_set_cnt=30; rtc_setting_flag = 0x01; rtc_coordinate = 3; } break; case MSG_USB_DISK_OUT: case MSG_SDMMC_OUT: if((RECODE_WORKING == encode_status)||(RECODE_PAUSE == encode_status)) { #ifdef AUTO_PLAY_RADIO_REC_FILE auto_play_radio_rec=0; #endif rec_device_out = 1; api_stop_encode(); if(work_mode == FM_RADIO_MODE){ main_menu = MENU_FM_MAIN;// disp_port(MENU_FM_MAIN); break; } } #ifndef LCD_BACK_LIGHT_DUMMY set_brightness_all_on(); #endif if((!device_check()) && (REC_MIC_MODE == work_mode)) { put_msg_lifo(MSG_MUSIC_NEW_DEVICE_IN); break; } if(MUSIC_MODE != work_mode) break; //put_msg_lifo(MSG_DEVICE_REMOVED); disk_remove_deal_for_music(); break; #endif #if USB_DEVICE_ENABLE case MSG_USB_PC_IN: break_encode(); set_brightness_all_on(); device_check(); work_mode = USB_DEVICE_MODE; put_msg_lifo(MSG_CHANGE_WORK_MODE); break; case MSG_USB_PC_OUT: set_brightness_all_on(); break; #endif case MSG_SDMMC_IN : #ifndef LCD_BACK_LIGHT_DUMMY set_brightness_all_on(); #endif device_check(); if((RECODE_WORKING == encode_status)||(RECODE_PAUSE == encode_status)) { break; } break_encode(); disp_scenario = DISP_NORMAL; #if USB_DEVICE_ENABLE if (work_mode == USB_DEVICE_MODE) break; #endif given_device = read_info(MEM_ACTIVE_DEV); if(given_device != DEVICE_SDMMC0_REC) // given_device = DEVICE_SDMMC0_REC; //else given_device = DEVICE_SDMMC0; given_file_method = PLAY_BREAK_POINT; put_msg_lifo(MSG_MUSIC_NEW_DEVICE_IN); break; case MSG_USB_DISK_IN : #ifndef LCD_BACK_LIGHT_DUMMY set_brightness_all_on(); #endif device_check(); if((RECODE_WORKING == encode_status)||(RECODE_PAUSE == encode_status)) { break; } break_encode(); disp_scenario = DISP_NORMAL; given_device = read_info(MEM_ACTIVE_DEV); if(given_device != DEVICE_UDISK_REC) // given_device = DEVICE_UDISK_REC; //else given_device = DEVICE_UDISK; given_file_method = PLAY_BREAK_POINT; put_msg_lifo(MSG_MUSIC_NEW_DEVICE_IN); break; #if 0 case MSG_NEXT_WORKMODE: #if 0//def K820_LHD_820_REC_V001 if(MUSIC_MODE != work_mode) break; if(device_check()>0){ given_device = DEVICE_AUTO_NEXT; given_file_method = PLAY_BREAK_POINT; put_msg_lifo(MSG_MUSIC_SELECT_NEW_DEVICE); backup_music_point(); } else{ //flashled(3); work_mode =IDLE_MODE; put_msg_lifo(MSG_CHANGE_WORK_MODE); } break; #endif break_encode(); work_mode++; if (work_mode > MAX_WORK_MODE) work_mode = MUSIC_MODE; put_msg_lifo(MSG_CHANGE_WORK_MODE); break; #endif #if 1 case MSG_VOL_UP: if(vol_change_en==0) break; dac_mute_control(0,1); //调节音量时,自动UNMUTE main_vol_set(0, CHANGE_VOL_INC); //write_info(MEM_VOL, main_vol_set(0, CHANGE_VOL_INC)); disp_port(MENU_MAIN_VOL); break; case MSG_VOL_DOWN: if(vol_change_en==0) break; dac_mute_control(0,1); //调节音量时,自动UNMUTE main_vol_set(0, CHANGE_VOL_DEC); //write_info(MEM_VOL, main_vol_set(0, CHANGE_VOL_DEC)); disp_port(MENU_MAIN_VOL); break; #endif case MSG_0: case MSG_1: case MSG_2: case MSG_3: case MSG_4: case MSG_5: case MSG_6: case MSG_7: case MSG_8: case MSG_9: if (!input_number_en) break; if((input_number)>6553){ input_number = 0x0000; } if (input_number > 9999) input_number = 0; input_number = input_number * 10 + key; disp_port(MENU_INPUT_NUMBER); break; //2 REC FSM BEGIN case MSG_REC_KEY: //录音开始和结束 if(work_mode ==IDLE_MODE) break; if(RECODE_PLAY >= encode_status) { put_msg_lifo(MSG_REC_FIND); #ifdef AUTO_PLAY_RADIO_REC_FILE if(work_mode == FM_RADIO_MODE){ auto_play_radio_rec=1; } #endif } else { #ifdef AUTO_PLAY_RADIO_REC_FILE //auto_play_radio_rec=0; #endif put_msg_lifo(MSG_REC_STOP); } break; case MSG_MUSIC_PP: //录音暂停和继续录音 //case MSG_REC_PP: //录音暂停和继续录音 if(RECODE_WORKING == encode_status) { put_msg_lifo(MSG_REC_PAUSE); } else if(RECODE_PAUSE == encode_status) { put_msg_lifo(MSG_REC_CONTINUE); } break; case MSG_ENCODE_END: //设备写err 或 设备满 if(rec_device_out) //录音时活动设备拔出,在设备拔出那里处理,在此不做处理 { rec_device_out =0; break; } api_stop_encode(); //停止录音 put_msg_lifo(MSG_REC_PLAY); break; case MSG_ENCODE_FAT_FULL: //建文件时 api_stop_encode(); //停止录音 if((!device_check())&& (REC_MIC_MODE == work_mode)) { put_msg_lifo(MSG_MUSIC_NEW_DEVICE_IN); break; } // break_encode(); disp_port(main_menu); break; ////////////////录音涉及的各种状态 case MSG_REC_FIND: // if(RECODE_PLAY >= encode_status) // { // break; // } if(get_device_online_status()==0){ disp_port(MENU_NODEVICE); break; } encode_status = RECODE_INIT; if(MUSIC_MODE == work_mode) { encode_device = device_active; //设置录音存储设备 work_mode = REC_MIC_MODE; put_msg_lifo(MSG_CHANGE_WORK_MODE); break; } else if(REC_MIC_MODE == work_mode){ given_device = encode_device; } else //if((MUSIC_MODE != work_mode)) { if (given_device == NO_DEVICE) { given_device = read_info(MEM_ACTIVE_DEV); } } // if( ((given_device & (~VIRTUAL_DEVICE)) != DEVICE_SDMMC0) && ((given_device & (~VIRTUAL_DEVICE)) != DEVICE_UDISK)) { //given_device = DEVICE_SDMMC0; } SYSTEM_CLK_DIV2(); put_msg_lifo(MSG_MUSIC_SELECT_NEW_DEVICE); break; case MSG_REC_START: //开始录音 rec_device_out = 0; rec_sys_set(0); //0:24M 1:48M init_rec_name(); device_active |= VIRTUAL_DEVICE; encode_device = device_active; //设置录音存储设备 write_file_info(0); SYSTEM_CLK_DIV2(); set_rec_channel(encode_channel); //设置录音通道 set_rec_vol(encode_vol); //设置录音音量 if(REC_MIC == encode_channel) { set_rec_track(TRACK_LEFT); } else { set_rec_track(TRACK_ALL); } /**/ CLKGAT |= MP3CLK;// | SPIURCLK; CLKCON0 |= DACCLK; if(DEVICE_SDMMC0 == (device_active & (~VIRTUAL_DEVICE))) { CLKGAT |= USBCLK; } else if(DEVICE_UDISK == (device_active & (~VIRTUAL_DEVICE))) { CLKGAT |= SDCLK; } if(1 != start_encode(IS_MP3)) //开始录音 { put_msg_lifo(MSG_ENCODE_FAT_FULL); break; } /* if(REC_MIC_MODE == work_mode) { while(!((cmd_ctl_rec(REC_NO_OPT,0)) & FRIST_ADC_PACKET)) //采数据前的处理 { } delay_10ms(50);//抛弃半秒的数据 } cmd_ctl_rec(ENABLE_REC_DATA,ENABLE_REC_BIT); */ encode_status = RECODE_WORKING; #if FM_MODULE // if(FM_RADIO_MODE != work_mode) #endif { main_menu = MENU_RECWORKING;// } disp_port(main_menu); break; case MSG_REC_STOP: //停止录音 api_stop_encode(); //停止录音 if(work_mode==REC_MIC_MODE){ put_msg_lifo(MSG_REC_PLAY); } #ifdef REC_PLAY_KEY_BREAK_POINT rec_pley_bp_flag=0; #endif break; case MSG_REC_PAUSE: //暂停录音 encode_status = RECODE_PAUSE; #if FM_MODULE if(FM_RADIO_MODE == work_mode) disp_port(MENU_FM_MAIN); else #endif { //main_menu = MENU_REC_PAUSE; disp_port(MENU_RECWORKING); } pause_encode(); break; case MSG_REC_CONTINUE: //暂停录音之后继续录音 encode_status = RECODE_WORKING; #if FM_MODULE if(FM_RADIO_MODE == work_mode) disp_port(MENU_FM_MAIN); else #endif { main_menu = MENU_RECWORKING;// disp_port(MENU_RECWORKING); } continue_encode(); break; case MSG_REC_PLAY: //播放最后的录音文件 if(work_mode ==IDLE_MODE) break; #ifdef REC_PLAY_KEY_BREAK_POINT rec_pley_bp_flag=~rec_pley_bp_flag; if(rec_pley_bp_flag){ last_play_index = given_file_number; } else{ given_file_number=last_play_index; put_msg_lifo(MSG_MUSIC_PLAY_NEW_FILE); break; } #endif encode_status = RECODE_PLAY; given_device = encode_device & (~VIRTUAL_DEVICE); if( (given_device != DEVICE_SDMMC0) && (given_device != DEVICE_UDISK)) { given_device = DEVICE_SDMMC0; } given_device |= VIRTUAL_DEVICE; if((MUSIC_MODE != work_mode)) { put_msg_lifo(MSG_MUSIC_NEW_DEVICE_IN); } else { put_msg_lifo(MSG_MUSIC_SELECT_NEW_DEVICE); } break; case MSG_MUSIC_SELECT_NEW_DEVICE: //重新选择设备 res = find_device(given_device); if ((res == DEV_INIT_ERR) || (res == NO_DEFINE_DEV)) //指定的设备不在线,或初始化失败 { given_device = DEVICE_AUTO_NEXT; //自动选择下一个设备 put_msg_lifo(MSG_MUSIC_SELECT_NEW_DEVICE); break; } else if ((res == NO_EFFECTIVE_DEV) || (res == NO_DEV_ONLINE)) //无可播放的设备 { if(RECODE_STOP != encode_status) { encode_status = RECODE_STOP; if(REC_MIC_MODE == work_mode) { put_msg_lifo(MSG_NEXT_WORKMODE); } } else { //put_msg_lifo(MSG_NEXT_WORKMODE); if(disp_scenario == DISP_NORMAL){ #ifdef PLAY_STATUS_LED_FUNC set_play_status_led_spark(PLED_ON); #endif disp_scenario = DISP_RTC_SCEN; rtc_disp_hdlr(); } } break; } else { if(RECODE_PLAY < encode_status) { put_msg_lifo(MSG_REC_START); } else { if(RECODE_PLAY == encode_status) //4去播刚录好的文件 { encode_status = RECODE_STOP; #if VIRTUAL_ENABLE #if 1//defined(K820_LHD_820_REC_V001) given_file_number = logic_fileTotal; #else given_file_number = encode_filenum; #endif #else given_file_number = logic_fileTotal; //given_file_number = encode_filenum + encode_fristfile -1; #endif put_msg_lifo(MSG_MUSIC_PLAY_NEW_FILE); } else { put_msg_lifo(MSG_MUSIC_SELECT_NEW_FILE); //找到可用设备 } } } break; //2 REC FSM END } }
void rtc_write_int32(int8 addr, int32 val) { write_rtc(addr+0,make8(val,3)); // MSB at address+0 write_rtc(addr+1,make8(val,2)); write_rtc(addr+2,make8(val,1)); write_rtc(addr+3,make8(val,0)); // LSB at address+3 }
void rtc_write_float(int8 addr, union_int32_float u) { write_rtc(addr+0,make8(u.l,3)); // MSB at address+0 write_rtc(addr+1,make8(u.l,2)); write_rtc(addr+2,make8(u.l,1)); write_rtc(addr+3,make8(u.l,0)); // LSB at address+3 }
void init_rtc(void) { write_rtc(0x07, 0x10); /* enable SQWE with one second pulse */ }
void rtc_write_int16(int8 addr, int16 val) { write_rtc(addr+0,make8(val,1)); write_rtc(addr+1,make8(val,0)); }
void write_rtcv(Byte add, Byte value) { rtc_valid(); write_rtc(add, value); }
void main() { int status; char buffer[2048]; longword ip; int i, dst, health; struct tm t; unsigned long longsec; // open connection to NIST server sock_init(); // Wait for the interface to come up while (ifpending(IF_DEFAULT) == IF_COMING_UP) { tcp_tick(NULL); } ip=resolve(NIST_SERVER_IP); tcp_open(&s, 0, ip, NIST_PORT, NULL); sock_wait_established(&s, NIST_TIMEOUT, NULL, &status); sock_mode(&s, TCP_MODE_ASCII); // receive and process data -- the server will close the connection, // which will cause execution to continue at sock_err below. while (tcp_tick(&s)) { sock_wait_input(&s, NIST_TIMEOUT, NULL, &status); sock_gets(&s, buffer, 48); } sock_err: if (status == -1) { printf("\nConnection timed out, exiting.\n"); exit(1); } if (status != 1) { printf("\nUnknown sock_err (%d), exiting.\n", status); exit(1); } sock_close(&s); // Dynamic C doesn't have a sscanf function, so we do // it this way instead... t.tm_year = 100 + 10*(buffer[6]-'0') + (buffer[7]-'0'); t.tm_mon = 10*(buffer[9]-'0') + (buffer[10]-'0'); t.tm_mday = 10*(buffer[12]-'0') + (buffer[13]-'0'); t.tm_hour = 10*(buffer[15]-'0') + (buffer[16]-'0'); t.tm_min = 10*(buffer[18]-'0') + (buffer[19]-'0'); t.tm_sec = 10*(buffer[21]-'0') + (buffer[22]-'0'); dst = 10*(buffer[24]-'0') + (buffer[25]-'0'); health = buffer[27]-'0'; // convert from tm_struct to seconds since Jan 1, 1980 // (much easier to adjust for DST and timezone this way) longsec = mktime(&t); longsec += 3600ul * TIMEZONE; // adjust for timezone dst = (dst >= 1 && dst <= 50 ); if (dst) longsec += 3600ul; // DST is in effect // convert back to tm struct for display to stdio mktm(&t, longsec); printf("Current time: %02d:%02d:%02d %02d/%02d/%04d\n", t.tm_hour, t.tm_min, t.tm_sec, t.tm_mon, t.tm_mday, 1900 + t.tm_year); if (dst) printf("Daylight Saving Time is in effect.\n"); switch (health) { case 0: printf("Server is healthy.\n"); break; case 1: printf("Server may be off by up to 5 seconds.\n"); break; case 2: printf("Server is off by more than 5 seconds; not setting RTC.\n"); break; default: printf("Server failure has occured; try another server (not setting RTC).\n"); break; } // finally, set the RTC if the results seems good if (health < 2) write_rtc(longsec); }
int main(int argc, char **argv) { int lp; unsigned char val; int year,month,day,hour,minute,second; time_t epoch_time; struct tm time_requested; struct timeval time_setformat; /* Check that the program was called correctly */ if ( argc > 2 ) { printf("Too many arguments specified.\nRun as:\nrtc-pi\nor\nrtc-pi CCYYMMDDHHMMSS\n"); exit (-1); } /* Set up gpi pointer for direct register access */ setup_io(); if ( argc == 2 ) { /* If the number of arguments are two, that means the user enter a date & time. */ /* Read that value and write it to the RTC chip */ sscanf(argv[1],"%4d%2d%2d%2d%2d%2d",&year,&month,&day,&hour,&minute,&second); /* Validate that the input date and time is basically sensible */ if ( (year < 2000) || (year > 2099) || (month < 1) || (month > 12) || (day < 1) || (day>31) || (hour < 0) || (hour > 23) || (minute < 0) || (minute > 59) || (second < 0) || (second > 59) ) { printf("Incorrect date and time specified.\nRun as:\nrtc-pi\nor\nrtc-pi CCYYMMDDHHMMSS\n"); exit (-1); } /* Got valid input - now write it to the RTC */ /* The RTC expects the values to be written in packed BCD format */ write_rtc(SEC_WRITE, ( (second/10) << 4) | ( second % 10) ); write_rtc(MIN_WRITE, ( (minute/10) << 4) | ( minute % 10) ); write_rtc(HOUR_WRITE, ( (hour/10) << 4) | ( hour % 10) ); write_rtc(DATE_WRITE, ( (day/10) << 4) | ( day % 10) ); write_rtc(MONTH_WRITE, ( (month/10) << 4) | ( month % 10) ); write_rtc(YEAR_WRITE, ( ((year-2000)/10) << 4) | (year % 10) ); /* Finally convert to it to EPOCH time, ie the number of seconds since January 1st 1970, and set the system time */ time_requested.tm_sec = second; time_requested.tm_min = minute; time_requested.tm_hour = hour; time_requested.tm_mday = day; time_requested.tm_mon = month-1; time_requested.tm_year = year-1900; time_requested.tm_wday = 0; /* not used */ time_requested.tm_yday = 0; /* not used */ time_requested.tm_isdst = -1; /* determine daylight saving time from the system */ epoch_time = mktime(&time_requested); /* Now set the clock to this time */ time_setformat.tv_sec = epoch_time; time_setformat.tv_usec = 0; lp = settimeofday(&time_setformat,NULL); /* Check that the change was successful */ if ( lp < 0 ) { printf("Unable to change the system time. Did you run the program as an administrator?\n"); printf("The operation returned the error message \"%s\"\n", strerror( errno ) ); exit (-1); } } else { /* The program was called without a date specified; therefore read the date and time from */ /* the RTC chip and set the system time to this */ second = read_rtc(SEC_READ); minute = read_rtc(MIN_READ); hour = read_rtc(HOUR_READ); day = read_rtc(DATE_READ); month = read_rtc(MONTH_READ); year = read_rtc(YEAR_READ); /* Finally convert to it to EPOCH time, ie the number of seconds since January 1st 1970, and set the system time */ /* Bearing in mind that the format of the time values in the RTC is packed BCD, hence the conversions */ time_requested.tm_sec = (((second & 0x70) >> 4) * 10) + (second & 0x0F); time_requested.tm_min = (((minute & 0x70) >> 4) * 10) + (minute & 0x0F); time_requested.tm_hour = (((hour & 0x30) >> 4) * 10) + (hour & 0x0F); time_requested.tm_mday = (((day & 0x30) >> 4) * 10) + (day & 0x0F); time_requested.tm_mon = (((month & 0x10) >> 4) * 10) + (month & 0x0F) - 1; time_requested.tm_year = (((year & 0xF0) >> 4) * 10) + (year & 0x0F) + 2000 - 1900; time_requested.tm_wday = 0; /* not used */ time_requested.tm_yday = 0; /* not used */ time_requested.tm_isdst = -1; /* determine daylight saving time from the system */ epoch_time = mktime(&time_requested); /* Now set the clock to this time */ time_setformat.tv_sec = epoch_time; time_setformat.tv_usec = 0; lp = settimeofday(&time_setformat,NULL); /* Check that the change was successful */ if ( lp < 0 ) { printf("Unable to change the system time. Did you run the program as an administrator?\n"); printf("The operation returned the error message \"%s\"\n", strerror( errno ) ); exit (-1); } } return 0; }
//------------------------------------------------------------------------ // Set Date and Time //------------------------------------------------------------------------ void SetDateTime( void ) { int wKey; int col, row; char buffer[256]; fieldupdate dateTime; // Setup for FAST key repeat after holding down key for 12 ticks keyConfig ( 6,'E',0, 12, 1, 1, 1 ); keyConfig ( 2,'D',0, 12, 1, 1, 1 ); keyConfig ( 5,'+',0, 12, 1, 1, 1 ); keyConfig ( 1,'U',0, 12, 1, 1, 1 ); keyConfig ( 4,'-',0, 12, 1, 1, 1 ); date_prompt("Select \n4 digit year: ", &col, &row); dateTime.data = 2001; while(1) { sprintf(buffer, "%04d", dateTime.data); TextGotoXY(&textWindow, col, row); TextPrintf(&textWindow, "%s", buffer); while((wKey = ProcessKeyField(NUMBER, &dateTime)) == 0); if(dateTime.data < 1900 || dateTime.data > 2047) { dateTime.data = 2001; } if(wKey == 'E') { if( dateTime.data >= 1900 && dateTime.data < 2048) { CurTime.tm_year = dateTime.data - 1900; // offset from 1900 break; } } } date_prompt("Enter month: ", &col, &row); dateTime.data = 1; while(1) { sprintf(buffer, "%02d", dateTime.data); TextGotoXY(&textWindow, col, row); TextPrintf(&textWindow, "%s", buffer); while((wKey = ProcessKeyField(NUMBER, &dateTime)) == 0); if(wKey == 'E') { if( dateTime.data >= 1 && dateTime.data < 13 ) { CurTime.tm_mon = month2tm_mon(dateTime.data); break; } } if(dateTime.data < 1 || dateTime.data > 12) { dateTime.data = (dateTime.data < 1) ? 12 : 1; } } date_prompt("Enter \nday of month: ", &col, &row); dateTime.data = 1; while(1) { sprintf(buffer, "%02d", dateTime.data); TextGotoXY(&textWindow, col, row); TextPrintf(&textWindow, "%s", buffer); while((wKey = ProcessKeyField(NUMBER, &dateTime))== 0); if(wKey == 'E') { if( dateTime.data >= 1 && dateTime.data < 32) { CurTime.tm_mday = dateTime.data; break; } } if(dateTime.data < 1 || dateTime.data > 31) { dateTime.data = (dateTime.data < 1) ? 31 : 1; } } date_prompt("Enter \nhour (24hr): ", &col, &row); dateTime.data = 0; while(1) { sprintf(buffer, "%02d", dateTime.data); TextGotoXY(&textWindow, col, row); TextPrintf(&textWindow, "%s", buffer); while((wKey = ProcessKeyField(NUMBER, &dateTime)) == 0); if(wKey == 'E') { if(dateTime.data >= 0 && dateTime.data < 24) { CurTime.tm_hour = dateTime.data; break; } } if(dateTime.data < 0 || dateTime.data > 23) { dateTime.data = (dateTime.data < 0) ? 23 : 0; } } date_prompt("Enter minute: ", &col, &row); dateTime.data = 0; while(1) { sprintf(buffer, "%02d", dateTime.data); TextGotoXY(&textWindow, col, row); TextPrintf(&textWindow, "%s", buffer); while((wKey = ProcessKeyField(NUMBER, &dateTime)) == 0); if(wKey == 'E') { if( dateTime.data >= 0 && dateTime.data < 60) { CurTime.tm_min = dateTime.data; break; } if(wKey == 'E') { break; } } if(dateTime.data < 0 || dateTime.data > 59) { dateTime.data = (dateTime.data < 0) ? 59 : 0; } } CurTime.tm_sec = 0; ulTime = mktime ( &CurTime ); // get seconds from 1/1/1980 write_rtc ( ulTime ); // set the real time clock keypadDef(); glBlankScreen(); while(1) { // Get current Date/Time FormatDateTime(); // convert to text // Display Date and Time glBuffLock(); TextGotoXY(&textWindow, 0, 0); TextPrintf(&textWindow, "%s\n", szTime); // Display user exit message TextGotoXY(&textWindow, 0, 3); TextPrintf(&textWindow, "Press Key to EXIT "); glBuffUnlock(); keyProcess (); if((wKey = keyGet()) != 0) { glBlankScreen(); break; } } }
/*----------------------------------------------------------------------------*/ void deal_rtc(void) { u8 key; u8 rtc_set_cnt; close_alarm(); while (1) { key = app_get_msg(); #if 0 if (alm_on_flag) { key = alm_msg_convert(key); } #endif switch (key) { case MSG_NEXT_WORKMODE: case MSG_REC_KEY: case MSG_REC_PLAY: break; case MSG_CHANGE_WORK_MODE: return; case MSG_MUSIC_NEW_DEVICE_IN: //有新设备接入 break; case MSG_ALM_SOURCE: //if(rtc_mode_setting == ALARM_SETTING) { #if 1 if(alarm.sw==0)break; if(rtc_mode_setting != ALARM_SETTING){ alarm.bSource =0; alm_coordinate=3; rtc_mode_setting = ALARM_SETTING; disp_port(MENU_ALM_SET); } else{ rtc_mode_setting = RTC_DISPLAY; disp_port(MENU_RTC); break; } #if 0 if(alm_coordinate==3){ rtc_mode_setting = RTC_DISPLAY; disp_port(MENU_RTC); break; } else{ alarm.bSource =0; alm_coordinate=3;; } #endif #else alarm.bSource++; if(alarm.bSource>3){ alarm.bSource =0; } #endif } rtc_set_cnt=0; break; case MSG_ALM_SETUP: if(rtc_mode_setting == ALARM_SETTING){ alm_coordinate++; if(alm_coordinate>2){ rtc_mode_setting = RTC_DISPLAY; } } else{ snooze_init(); alm_coordinate = 1; alarm.sw=1; alarm.bSource =0; rtc_mode_setting = ALARM_SETTING; write_next_alm_sec(); save_alarm_info(); } rtc_set_cnt=0; disp_port(MENU_RTC); break; case MSG_RTC_SETTING: if (rtc_mode_setting == RTC_SETTING) { rtc_mode_setting = RTC_DISPLAY; } else{ rtc_coordinate = 3; rtc_mode_setting = RTC_SETTING; } rtc_set_cnt=0; disp_port(MENU_RTC); break; case MSG_RTC_NEXT: if (rtc_mode_setting == RTC_SETTING) { rtc_coordinate++; if (rtc_coordinate > 4) { rtc_mode_setting = RTC_DISPLAY; } rtc_set_cnt=0; disp_port(MENU_RTC); } break; #if 0 case MSG_RTC_PREV: if (rtc_mode_setting == RTC_SETTING) { rtc_coordinate--; if (rtc_coordinate == 255) { rtc_coordinate = 5; } rtc_set_cnt=0; disp_port(MENU_RTC); } if (rtc_mode_setting == ALARM_SETTING) { alm_coordinate--; if (alm_coordinate == 255) { //if(alarm[0].sw) //{ // alm_coordinate = 9; //} //else //{ alm_coordinate = 2; //} } disp_port(MENU_ALM_SET); } break; #endif case MSG_RTC_PLUS: case MSG_RTC_PLUS_HOLD: if (rtc_mode_setting == RTC_SETTING) { rtc_set_cnt=0; curr_time_plus(); write_rtc(&curr_time); write_next_alm_sec(); disp_port(MENU_RTC); } if (rtc_mode_setting == ALARM_SETTING) { rtc_set_cnt=0; alm_time_plus(); save_alarm_info(); write_next_alm_sec(); if(alm_coordinate==3){ disp_port(MENU_ALM_SET); } else{ disp_port(MENU_RTC); } } break; case MSG_RTC_MINUS: case MSG_RTC_MINUS_HOLD: if (rtc_mode_setting == RTC_SETTING) { rtc_set_cnt=0; curr_time_minus(); write_rtc(&curr_time); write_next_alm_sec(); disp_port(MENU_RTC); } if (rtc_mode_setting == ALARM_SETTING) { rtc_set_cnt=0; alm_time_minus(); save_alarm_info(); write_next_alm_sec(); if(alm_coordinate==3){ disp_port(MENU_ALM_SET); } else{ disp_port(MENU_RTC); } } break; case MSG_HALF_SECOND: snooze_hdlr(); if (main_menu_conter < SUB_MENU_TIME*4) { main_menu_conter++; } else if (cur_menu != main_menu) { rtc_mode_setting = RTC_DISPLAY; read_rtc(&curr_time); chk_date_err(); disp_port(MENU_RTC); } if ((rtc_mode_setting == RTC_SETTING)||(rtc_mode_setting == ALARM_SETTING)) rtc_set_cnt++; if ((rtc_set_cnt==8)&&((rtc_mode_setting == RTC_SETTING)||(rtc_mode_setting == ALARM_SETTING))) { rtc_set_cnt=0; rtc_mode_setting = RTC_DISPLAY; read_rtc(&curr_time); chk_date_err(); disp_port(MENU_RTC); } if (cur_menu == MENU_RTC) { read_rtc(&curr_time); chk_date_err(); disp_port(MENU_RTC); } break; case MSG_ALM_ON: write_next_alm_sec(); alm_on_flag = 1; alrm_vol_bakup=sys_main_vol; sys_main_vol = 10; alm_recover_vol_enable=1; if(alarm.bSource== 0){ work_mode = MUSIC_MODE; } else if(alarm.bSource== 1){ work_mode = MUSIC_MODE; } else if(alarm.bSource== 2){ work_mode = FM_RADIO_MODE; } else{ work_mode = AUX_MODE; } disp_port(MENU_RTC); return; break; default: ap_handle_hotkey(key); break; } } }