Index: output/eeprom.dat
===================================================================
--- /YC1308AC/trunk/output/eeprom.dat	(revision 379)
+++ /YC1308AC/trunk/output/eeprom.dat	(working copy)
@@ -2,36 +2,496 @@
 02
 aa
 55
-72
-00
+3e
+02
 c2
 84
 00
-05
+07
+c0
+01
+80
+0d
 c0
 45
 80
-07
+7a
+c0
+52
+00
+40
 c0
 4f
 80
-13
+86
 c0
 57
 00
-15
+88
 20
 20
 13
 7c
 c0
+19
+80
+4e
+c0
+1a
+00
+54
+c0
+1a
+80
+6f
+c0
+43
+80
+1a
+c0
 58
 80
-17
+8a
 20
 20
 13
 7c
+20
+40
+70
+6c
+20
+40
+75
+97
+20
+40
+6e
+a7
+20
+40
+1b
+93
+20
+40
+08
+3a
+20
+40
+31
+08
+20
+40
+08
+92
+20
+40
+0b
+08
+20
+40
+0b
+7b
+20
+40
+08
+23
+20
+40
+21
+d3
+20
+40
+14
+dd
+20
+20
+08
+13
+da
+20
+00
+00
+68
+4a
+47
+84
+1c
+42
+24
+00
+1a
+42
+7e
+00
+98
+46
+7c
+00
+24
+41
+78
+4c
+1a
+42
+7e
+00
+18
+40
+84
+a0
+18
+40
+84
+a0
+98
+46
+7c
+00
+24
+21
+00
+2d
+1a
+20
+a2
+01
+1a
+21
+7e
+0f
+24
+3a
+00
+20
+da
+20
+00
+00
+20
+40
+00
+33
+1c
+42
+7e
+00
+67
+e2
+47
+84
+20
+60
+00
+00
+1a
+22
+7e
+00
+20
+7a
+00
+00
+18
+40
+85
+60
+18
+40
+85
+60
+79
+3f
+84
+1c
+60
+4a
+47
+84
+67
+e0
+83
+0d
+6f
+e0
+83
+0d
+20
+7a
+00
+00
+1f
+e0
+ff
+ff
+67
+e0
+83
+0d
+20
+40
+77
+c5
+20
+40
+31
+e3
+20
+40
+31
+a4
+20
+40
+31
+aa
+20
+40
+5c
+dc
+6f
+e1
+44
+6f
+20
+40
+7e
+05
+20
+20
+00
+34
+20
+40
+14
+65
+6f
+e1
+c1
+74
+20
+7a
+00
+00
+6f
+e0
+c0
+99
+20
+7a
+00
+00
+6f
+e0
+c7
+64
+20
+3a
+00
+49
+6f
+e0
+c6
+b9
+20
+7a
+00
+00
+6f
+e0
+c6
+ba
+24
+7a
+00
+00
+da
+60
+0a
+b9
+20
+40
+0a
+70
+20
+20
+14
+ec
+da
+20
+09
+9e
+d8
+40
+00
+0c
+58
+00
+00
+30
+20
+40
+5e
+63
+20
+40
+60
+d5
+20
+20
+60
+c0
+6a
+50
+8a
+12
+70
+8a
+12
+3d
+70
+8a
+11
+aa
+70
+8a
+0a
+0c
+6f
+f0
+8a
+09
+79
+20
+7e
+07
+67
+f0
+8a
+09
+70
+8a
+0b
+04
+20
+00
+00
+3c
+70
+8a
+16
+00
+70
+8a
+17
+06
+20
+40
+61
+1a
+67
+f0
+8a
+97
+70
+80
+dc
+03
+20
+00
+00
+3c
+6f
+f0
+80
+53
+79
+20
+7e
+07
+67
+f0
+80
+53
+20
+00
+03
+e8
+6f
+f1
+01
+50
+67
+e1
+44
+a2
+6f
+f0
+80
+53
+79
+3f
+fe
+07
+67
+f0
+80
+53
+70
+8a
+97
+00
+62
+50
+8a
+12
+20
+60
+00
+00
+6f
+e0
+c4
+a0
+c0
+00
+61
+2b
+c0
+00
+80
+74
+c0
+01
+61
+37
+20
+60
+00
+00
+58
+00
+00
+64
+67
+e1
+44
+9e
+68
+49
+44
+98
+6f
+e1
+44
+92
+da
+60
+01
+2c
+20
+20
+61
+3c
 70
 8a
 03
@@ -116,15 +576,15 @@
 20
 17
 6b
-7d
-d3
+f6
+42
 aa
 55
 40
 00
 00
 40
-00
+08
 00
 00
 00
@@ -144,7 +604,7 @@
 08
 00
 80
-00
+10
 40
 00
 00
@@ -162,6 +622,7 @@
 00
 00
 00
+38
 00
 00
 00
@@ -171,8 +632,7 @@
 00
 00
 00
-00
-00
+80
 00
 00
 00
@@ -1534,11 +1994,5 @@
 40
 ef
 41
-55
-aa
-aa
-55
-5e
-fc
-9d
-57
+18
+6b
Index: output/program.lis
===================================================================
--- /YC1308AC/trunk/output/program.lis	(revision 379)
+++ /YC1308AC/trunk/output/program.lis	(working copy)
@@ -1,43 +1,186 @@
               include "bt_format "
               org 0x0000
-0000 c2840005 bbit1 8 ,pf_patch_ext 
-0001 c0458007 beq patch11_3 ,p_initialize_radio_cont 
-0002 c04f8013 beq patch13_7 ,p_init_param 
-0003 c0570015 beq patch15_6 ,p_app_init 
-0004 2020137c branch loop 
+0000 c2840007 bbit1 8 ,pf_patch_ext 
+0001 c001800d beq patch00_3 ,p_main_loop 
+0002 c045807a beq patch11_3 ,p_initialize_radio_cont 
+0003 c0520040 beq patch14_4 ,p_lpm_dispatch 
+0004 c04f8086 beq patch13_7 ,p_init_param 
+0005 c0570088 beq patch15_6 ,p_app_init 
+0006 2020137c branch loop 
 
 pf_patch_ext:
-0005 c0588017 beq patch36_1 ,p_le_receive_on_attempt 
-0006 2020137c branch loop 
+0007 c019804e beq patch26_3 ,p_adc_init_data 
+0008 c01a0054 beq patch26_4 ,p_enable_adc 
+0009 c01a806f beq patch26_5 ,p_vdd_calculate_by_mode 
+000a c043801a beq patch30_7 ,p_ui_timer_check 
+000b c058808a beq patch36_1 ,p_le_receive_on_attempt 
+000c 2020137c branch loop 
+
+p_main_loop:
+000d 2040706c call sp_calc_sequence 
+000e 20407597 call sp_calc_sequence_256 
+000f 20406ea7 call sp_calc_sequence_256_check 
+0010 20401b93 call le_advertising_dispatch + 1 
+0011 2040083a call idle_dispatch 
+0012 20403108 call app_process_idle + 1 
+0013 20400892 call inquiry_dispatch + 1 
+0014 20400b08 call inquiry_scan_dispatch + 1 
+0015 20400b7b call page_scan_dispatch + 1 
+0016 20400823 call connection_dispatch + 1 
+0017 204021d3 call g24_dispatch + 2 
+0018 204014dd call lpm_dispatch 
+0019 20200813 branch main_loop 
+
+p_ui_timer_check:
+001a da200000 arg 0 ,rega 
+001b 684a4784 fetcht 4 ,mem_ui_timer_last_btclk 
+001c 1c422400 copy clkn_bt ,regb 
+001d 1a427e00 copy regb ,pdata 
+001e 98467c00 isub temp ,null 
+001f 2441784c ncall ui_timer_timeout ,positive 
+
+p_ui_timer_check_loop:
+0020 1a427e00 copy regb ,pdata 
+0021 184084a0 increase 160 ,temp 
+0022 184084a0 increase 160 ,temp 
+0023 98467c00 isub temp ,null 
+0024 2421002d nbranch p_ui_timer_check_end ,positive 
+0025 1a20a201 increase 1 ,rega 
+0026 1a217e0f and rega ,0x0f ,pdata 
+0027 243a0020 nbranch p_ui_timer_check_loop ,blank 
+0028 da200000 arg 0 ,rega 
+0029 20400033 call p_ui_timer_check_send_evt 
+
+p_ui_timer_init:
+002a 1c427e00 copy clkn_bt ,pdata 
+002b 67e24784 store 4 ,mem_ui_timer_last_btclk 
+002c 20600000 rtn 
+
+p_ui_timer_check_end:
+002d 1a227e00 copy rega ,pdata 
+002e 207a0000 rtn blank 
+002f 18408560 increase -160 ,temp 
+0030 18408560 increase -160 ,temp 
+0031 793f841c set0 28 ,temp 
+0032 604a4784 storet 4 ,mem_ui_timer_last_btclk 
+
+p_ui_timer_check_send_evt:
+
+p_app_evt_timer:
+0033 67e0830d store 1 ,mem_app_evt_timer_count 
+
+p_app_evt_100ms_loop:
+0034 6fe0830d fetch 1 ,mem_app_evt_timer_count 
+0035 207a0000 rtn blank 
+0036 1fe0ffff increase -1 ,pdata 
+0037 67e0830d store 1 ,mem_app_evt_timer_count 
+0038 204077c5 call ui_button_polling 
+0039 204031e3 call app_lpm_wake_auto_lock_timer 
+003a 204031a4 call app_unsniff_delay_timer 
+003b 204031aa call app_discovery_timer 
+003c 20405cdc call flash_write_spi_sm_timer 
+003d 6fe1446f fetch 2 ,mem_cb_event_timer 
+003e 20407e05 call callback_func 
+003f 20200034 branch p_app_evt_100ms_loop 
+
+p_lpm_dispatch:
+0040 20401465 call lpo_calibration 
+0041 6fe1c174 fetch 3 ,mem_clks_per_lpo 
+0042 207a0000 rtn blank 
+0043 6fe0c099 fetch 1 ,mem_lpm_mode 
+0044 207a0000 rtn blank 
+0045 6fe0c764 fetch 1 ,mem_ssp_enable 
+0046 203a0049 branch p_lpm_dispatch_next ,blank 
+0047 6fe0c6b9 fetch 1 ,mem_sp_local_key_invalid 
+0048 207a0000 rtn blank 
+
+p_lpm_dispatch_next:
+0049 6fe0c6ba fetch 1 ,mem_sc_calc 
+004a 247a0000 nrtn blank 
+004b da600ab9 arg context_check_a_wack ,regc 
+004c 20400a70 call context_search 
+004d 202014ec branch lpm_dispatch_next + 5 
+
+p_adc_init_data:
+004e da20099e arg mem_temp_block2 ,rega 
+004f d840000c arg 12 ,temp 
+0050 58000030 setarg efuse_offset_adc_param 
+0051 20405e63 call efuse_read_data 
+0052 204060d5 call adc_init_wait_count 
+0053 202060c0 branch adc_init_cal_data 
+
+p_enable_adc:
+0054 6a508a12 fetchr regb ,1 ,core_rf_ldo_cfg6 
+0055 708a123d jam 0x3d ,core_rf_ldo_cfg6 
+0056 708a11aa jam 0xaa ,core_rf_ldo_cfg5 
+0057 708a0a0c jam 0x0c ,core_rf_ldo_en0 
+0058 6ff08a09 fetch 1 ,core_clkpll_cfg6 
+0059 79207e07 set1 7 ,pdata 
+005a 67f08a09 store 1 ,core_clkpll_cfg6 
+005b 708a0b04 jam 0x04 ,core_rf_ldo_en1 
+005c 2000003c nop 60 
+005d 708a1600 jam 0 ,core_syn_en 
+005e 708a1706 jam 6 ,core_rx_en0 
+005f 2040611a call read_adc_mode 
+0060 67f08a97 store 1 ,core_gpadc_ctrl 
+0061 7080dc03 jam 0x03 ,core_sum_ctrl 
+0062 2000003c nop 60 
+0063 6ff08053 fetch 1 ,core_sum_en 
+0064 79207e07 set1 7 ,pdata 
+0065 67f08053 store 1 ,core_sum_en 
+0066 200003e8 nop 1000 
+0067 6ff10150 fetch 2 ,core_adc_sum 
+0068 67e144a2 store 2 ,mem_adc_current_value 
+0069 6ff08053 fetch 1 ,core_sum_en 
+006a 793ffe07 set0 7 ,pdata 
+006b 67f08053 store 1 ,core_sum_en 
+006c 708a9700 jam 0 ,core_gpadc_ctrl 
+006d 62508a12 storer regb ,1 ,core_rf_ldo_cfg6 
+006e 20600000 rtn 
+
+p_vdd_calculate_by_mode:
+006f 6fe0c4a0 fetch 1 ,mem_adc_config_flag 
+0070 c000612b beq adc_config_vinlpm ,vdd_calculate_vinlpm 
+0071 c0008074 beq adc_config_hvin ,p_vdd_calculate_hvin 
+0072 c0016137 beq adc_config_gpio ,vdd_calculate_io 
+0073 20600000 rtn 
+
+p_vdd_calculate_hvin:
+0074 58000064 setarg 100 
+0075 67e1449e store 2 ,mem_reference_voltage 
+0076 68494498 fetcht 2 ,mem_5v_adc_hvin_data 
+0077 6fe14492 fetch 2 ,mem_3v_adc_hvin_data 
+0078 da60012c arg 300 ,regc 
+0079 2020613c branch vdd_calculate 
 
 p_initialize_radio_cont:
-0007 708a0305 jam 0x05 ,core_clkpll_cfg0 
-0008 708a7a94 jam 0x94 ,core_rx_iqadc_cfg 
-0009 20401277 call initialize_rc_cal 
-000a 20401271 call initialize_rf_ldo_cfg1 
-000b 2040126a call initialize_syn_afc_cfg1 
-000c 708a8be6 jam 0xe6 ,core_syn_aac_cfg1 
-000d 708a8390 jam 0x90 ,core_syn_pfd_lkd_cfg 
-000e 708a0e0b jam 0x0b ,core_rf_ldo_cfg2 
-000f 708a0f05 jam 0x05 ,core_rf_ldo_cfg3 
-0010 70896876 jam 0x76 ,core_agc_wb_sat_th 
-0011 70896976 jam 0x76 ,core_agc_nb_sat_th 
-0012 2020125d branch initialize_radio_cont + 8 
+007a 708a0305 jam 0x05 ,core_clkpll_cfg0 
+007b 708a7a94 jam 0x94 ,core_rx_iqadc_cfg 
+007c 20401277 call initialize_rc_cal 
+007d 20401271 call initialize_rf_ldo_cfg1 
+007e 2040126a call initialize_syn_afc_cfg1 
+007f 708a8be6 jam 0xe6 ,core_syn_aac_cfg1 
+0080 708a8390 jam 0x90 ,core_syn_pfd_lkd_cfg 
+0081 708a0e0b jam 0x0b ,core_rf_ldo_cfg2 
+0082 708a0f05 jam 0x05 ,core_rf_ldo_cfg3 
+0083 70896876 jam 0x76 ,core_agc_wb_sat_th 
+0084 70896976 jam 0x76 ,core_agc_nb_sat_th 
+0085 2020125d branch initialize_radio_cont + 8 
 
 p_init_param:
-0013 70008000 jam 0 ,mem_lmo_tid2 
-0014 202013b7 branch init_param + 51 
+0086 70008000 jam 0 ,mem_lmo_tid2 
+0087 202013b7 branch init_param + 51 
 
 p_app_init:
-0015 20406df3 call enable_authrom 
-0016 202030dd branch app_init + 1 
+0088 20406df3 call enable_authrom 
+0089 202030dd branch app_init + 1 
 
 p_le_receive_on_attempt:
-0017 78287c00 enable swfine 
-0018 d8404ea9 arg param_t_ifs_150us + param_20ms_init + -13 ,temp 
-0019 6fe0c1f0 fetch 1 ,mem_system_clk 
-001a 984fb600 imul32 temp ,stop_watch 
-001b 2020176b branch le_receive_on_attempt + 6 
+008a 78287c00 enable swfine 
+008b d8404ea9 arg param_t_ifs_150us + param_20ms_init + -13 ,temp 
+008c 6fe0c1f0 fetch 1 ,mem_system_clk 
+008d 984fb600 imul32 temp ,stop_watch 
+008e 2020176b branch le_receive_on_attempt + 6 
               org 0x800
 
 start:
Index: program/patch.prog
===================================================================
--- /YC1308AC/trunk/program/patch.prog	(revision 379)
+++ /YC1308AC/trunk/program/patch.prog	(working copy)
@@ -1,14 +1,163 @@
 
 	bbit1 8,pf_patch_ext
+	beq patch00_3,p_main_loop
 	beq patch11_3,p_initialize_radio_cont
+	beq patch14_4,p_lpm_dispatch 
 	beq patch13_7,p_init_param
 	beq patch15_6,p_app_init
 	branch loop
 
 pf_patch_ext:
+	beq patch26_3,p_adc_init_data
+	beq patch26_4,p_enable_adc
+	beq patch26_5,p_vdd_calculate_by_mode
+	beq patch30_7,p_ui_timer_check
 	beq patch36_1,p_le_receive_on_attempt
 	branch loop
 
+p_main_loop:
+ifdef SIMPLE_PAIRING
+	call sp_calc_sequence
+endif
+ifdef SECURE_CONNECTION
+	call sp_calc_sequence_256
+endif
+ifdef SIMPLE_PAIRING
+ifdef SECURE_CONNECTION
+	call sp_calc_sequence_256_check
+endif
+endif
+	call le_advertising_dispatch+1
+	call idle_dispatch
+	call app_process_idle+1
+	call inquiry_dispatch+1
+	call inquiry_scan_dispatch+1
+	call page_scan_dispatch+1
+	call connection_dispatch+1
+	call g24_dispatch+2
+	call lpm_dispatch
+	branch main_loop
+
+p_ui_timer_check:
+	arg 0,rega
+	fetcht 4,mem_ui_timer_last_btclk
+	copy clkn_bt,regb
+	copy regb,pdata
+	isub temp,null
+	ncall ui_timer_timeout,positive
+p_ui_timer_check_loop:
+	copy regb,pdata
+	increase 160,temp    //320btclk = 100ms
+	increase 160,temp
+	isub temp,null
+	nbranch p_ui_timer_check_end,positive
+	increase 1,rega   //increase 1 every 100ms count
+	and rega,0x0f,pdata
+	nbranch p_ui_timer_check_loop,blank
+	arg 0,rega  //reset timer if it's longer than 1.5s between 2 checks
+	call p_ui_timer_check_send_evt
+p_ui_timer_init:
+	copy clkn_bt,pdata
+	store 4,mem_ui_timer_last_btclk
+	rtn
+p_ui_timer_check_end:
+	copy rega,pdata
+	rtn blank
+	increase -160,temp    //320btclk = 100ms
+	increase -160,temp
+	set0 28,temp
+	storet 4,mem_ui_timer_last_btclk
+p_ui_timer_check_send_evt:
+//	branch p_app_evt_timer
+
+p_app_evt_timer:
+	store 1,mem_app_evt_timer_count
+p_app_evt_100ms_loop:
+	fetch 1,mem_app_evt_timer_count
+	rtn blank
+	increase -1,pdata
+	store 1,mem_app_evt_timer_count
+	call ui_button_polling
+	call app_lpm_wake_auto_lock_timer
+	call app_unsniff_delay_timer
+	call app_discovery_timer
+	call flash_write_spi_sm_timer
+ifdef SPI2	
+	call flash_write_spi2_sm_timer
+endif	
+	fetch 2,mem_cb_event_timer
+	call callback_func
+	branch p_app_evt_100ms_loop
+
+p_lpm_dispatch:
+	call lpo_calibration
+	fetch 3,mem_clks_per_lpo
+	rtn blank
+	fetch 1,mem_lpm_mode
+	rtn blank
+	fetch 1,mem_ssp_enable
+	branch p_lpm_dispatch_next,blank
+	fetch 1,mem_sp_local_key_invalid	// simple pairing is not ready
+	rtn blank
+p_lpm_dispatch_next:
+	fetch 1,mem_sc_calc
+	nrtn blank
+	arg context_check_a_wack,regc
+	call context_search
+	branch lpm_dispatch_next+5
+
+p_adc_init_data:
+	arg mem_temp_block2,rega
+	arg 12,temp
+	setarg EFUSE_OFFSET_ADC_PARAM
+	call efuse_read_data
+	call adc_init_wait_count
+	branch adc_init_cal_data
+
+p_enable_adc:
+	fetchr regb,1,core_rf_ldo_cfg6
+	jam 0x3d,core_rf_ldo_cfg6
+	jam 0xaa,core_rf_ldo_cfg5
+	jam 0x0c,core_rf_ldo_en0
+	fetch 1,core_clkpll_cfg6
+	set1 7,pdata
+	store 1,core_clkpll_cfg6
+	jam 0x04,core_rf_ldo_en1
+	nop 60
+	jam 0,core_syn_en
+	jam 6,core_rx_en0
+	call read_adc_mode
+	store 1,core_gpadc_ctrl
+//	jam 0x07,core_gpadc_ctrl
+	jam 0x03,core_sum_ctrl
+	nop 60
+	fetch 1,core_sum_en
+	set1 7,pdata
+	store 1,core_sum_en
+	nop 1000
+	fetch 2,core_adc_sum
+	store 2,mem_adc_current_value
+	fetch 1,core_sum_en
+	set0 7,pdata
+	store 1,core_sum_en
+	jam 0,core_gpadc_ctrl
+	storer regb,1,core_rf_ldo_cfg6
+	rtn
+p_vdd_calculate_by_mode:
+	fetch 1,mem_adc_config_flag
+	beq ADC_CONFIG_VINLPM,vdd_calculate_vinlpm
+	beq ADC_CONFIG_HVIN,p_vdd_calculate_hvin
+	beq ADC_CONFIG_GPIO,vdd_calculate_io
+	rtn
+
+p_vdd_calculate_hvin:
+	setarg 100
+	store 2,mem_reference_voltage //diff
+	fetcht 2,mem_5v_adc_hvin_data
+	fetch 2,mem_3v_adc_hvin_data
+	arg 300,regc
+	branch vdd_calculate
+
 /********************rx fail patch************************/
 p_initialize_radio_cont:
 	jam 0x05,core_clkpll_cfg0
