///////////////////////////////////////////////////////////////////////////////
//
// IAR C/C++ Compiler V2.10.4.157 for STM8                10/Aug/2020  13:50:18
// Copyright 2010-2015 IAR Systems AB.
//
//    Source file  =  F:\2-Lora\Code\stm8l151K6T6-WLAN\USER\motor.c
//    Command line =  
//        F:\2-Lora\Code\stm8l151K6T6-WLAN\USER\motor.c -e -Ol --no_cse
//        --no_unroll --no_inline --no_code_motion --no_tbaa --no_cross_call
//        --debug --code_model small --data_model medium -o
//        F:\2-Lora\Code\stm8l151K6T6-WLAN\Debug\Obj\ --dlib_config
//        E:\IAR\stm8\LIB\dlstm8smf.h -lB
//        F:\2-Lora\Code\stm8l151K6T6-WLAN\Debug\List\ -I
//        F:\2-Lora\Code\stm8l151K6T6-WLAN\FWLIB\inc\ -I
//        F:\2-Lora\Code\stm8l151K6T6-WLAN\FWLIB\src\ -I
//        F:\2-Lora\Code\stm8l151K6T6-WLAN\USER\ -I
//        F:\2-Lora\Code\stm8l151K6T6-WLAN\BSP\ -I
//        F:\2-Lora\Code\stm8l151K6T6-WLAN\radio\ -I
//        F:\2-Lora\Code\stm8l151K6T6-WLAN\SX1278\ --vregs 16
//    List file    =  F:\2-Lora\Code\stm8l151K6T6-WLAN\Debug\List\motor.s
//
///////////////////////////////////////////////////////////////////////////////

        EXTERN ?b0
        EXTERN ?b1
        EXTERN ?b10
        EXTERN ?b11
        EXTERN ?b2
        EXTERN ?b8
        EXTERN ?b9
        EXTERN ?epilogue_l2
        EXTERN ?fcastf32u16_l0_x
        EXTERN ?fcmpge32_c_l0_dc32
        EXTERN ?fdiv32_l0_l0_dc32
        EXTERN ?fmul32_l0_l0_dc32
        EXTERN ?mov_l0_l2
        EXTERN ?mov_l2_l0
        EXTERN ?push_l2
        EXTERN ?w4
        EXTERN ADC_ChannelCmd
        EXTERN ADC_ClearFlag
        EXTERN ADC_Cmd
        EXTERN ADC_GetConversionValue
        EXTERN ADC_GetFlagStatus
        EXTERN ADC_Init
        EXTERN ADC_SamplingTimeConfig
        EXTERN ADC_SoftwareStartConv
        EXTERN CLK_PeripheralClockConfig
        EXTERN GPIO_Init
        EXTERN GPIO_ReadInputDataBit
        EXTERN GPIO_WriteBit
        EXTERN QMC5883LStr
        EXTERN bsp_beep_two
        EXTERN mag_time_count

        PUBLIC CarAction
        PUBLIC LockCarState
        PUBLIC MotorADGet
        PUBLIC MotorStr
        PUBLIC motor_cmd
        PUBLIC motor_contro_act
        PUBLIC motor_init

// F:\2-Lora\Code\stm8l151K6T6-WLAN\USER\motor.c
//    1 #include "motor.h"
//    2 
//    3 

        SECTION `.near.bss`:DATA:REORDER:NOROOT(0)
//    4 MOTORSTR MotorStr;
MotorStr:
        DS8 3

        SECTION `.near.bss`:DATA:REORDER:NOROOT(0)
//    5 MOTOR_STATE_STR LockCarState;
LockCarState:
        DS8 6

        SECTION `.near.bss`:DATA:REORDER:NOROOT(0)
//    6 motor_state motor_cmd = motor_stop;
motor_cmd:
        DS8 1

        SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
        CODE
//    7 void motor_init(void)
//    8 {
//    9 	GPIO_Init(MOTOR_GPIO,MOTOR_FALL_PIN,GPIO_Mode_Out_PP_High_Fast);
motor_init:
        MOV       S:?b0, #0xf0
        LD        A, #0x4
        LDW       X, #0x500f
        CALL      L:GPIO_Init
//   10 	GPIO_Init(MOTOR_GPIO,MOTOR_RISE_PIN,GPIO_Mode_Out_PP_High_Fast);
        MOV       S:?b0, #0xf0
        LD        A, #0x8
        LDW       X, #0x500f
        CALL      L:GPIO_Init
//   11 	GPIO_Init(LIMIT_GPIO,LIMIT_PIN,GPIO_Mode_In_FL_No_IT);
        CLR       S:?b0
        LD        A, #0x1
        LDW       X, #0x5005
        CALL      L:GPIO_Init
//   12 	CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE);
        MOV       S:?b0, #0x1
        LD        A, #0x10
        CALL      L:CLK_PeripheralClockConfig
//   13 	ADC_Init(ADC1,ADC_ConversionMode_Single,ADC_Resolution_12Bit,ADC_Prescaler_1);
        CLR       S:?b1
        CLR       S:?b0
        CLR       A
        LDW       X, #0x5340
        CALL      L:ADC_Init
//   14 	ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_384Cycles);
        MOV       S:?b0, #0x7
        CLR       A
        LDW       X, #0x5340
        CALL      L:ADC_SamplingTimeConfig
//   15 	ADC_Cmd(ADC1,ENABLE);
        LD        A, #0x1
        LDW       X, #0x5340
        JP        L:ADC_Cmd
//   16 }
//   17 

        SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
        CODE
//   18 uint16_t MotorADGet(void)
//   19 {
//   20         ADC_ChannelCmd(ADC1,ADC_Channel_22,DISABLE);
MotorADGet:
        CLR       A
        LDW       Y, #0x140
        LDW       X, #0x5340
        CALL      L:ADC_ChannelCmd
//   21 	ADC_ChannelCmd(ADC1,ADC_Channel_2,ENABLE);
        LD        A, #0x1
        LDW       Y, #0x304
        LDW       X, #0x5340
        CALL      L:ADC_ChannelCmd
//   22 	ADC_SoftwareStartConv(ADC1);
        LDW       X, #0x5340
        CALL      L:ADC_SoftwareStartConv
//   23 	while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==RESET);
??MotorADGet_0:
        LD        A, #0x1
        LDW       X, #0x5340
        CALL      L:ADC_GetFlagStatus
        CP        A, #0x0
        JREQ      L:??MotorADGet_0
//   24 	return ADC_GetConversionValue(ADC1);
        LDW       X, #0x5340
        JP        L:ADC_GetConversionValue
//   25 	//LockCarState.
//   26 }

        SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
        CODE
//   27 static void motor_act(motor_state cmd)
//   28 {
//   29 	switch(cmd)
motor_act:
        TNZ       A
        JREQ      L:??motor_act_0
        DEC       A
        JREQ      L:??motor_act_1
        DEC       A
        JREQ      L:??motor_act_2
        RET
//   30 	{
//   31 	case motor_stop:
//   32 	{
//   33 		GPIO_WriteBit(MOTOR_GPIO,MOTOR_FALL_PIN,SET);
??motor_act_0:
        MOV       S:?b0, #0x1
        LD        A, #0x4
        LDW       X, #0x500f
        CALL      L:GPIO_WriteBit
//   34 		GPIO_WriteBit(MOTOR_GPIO,MOTOR_RISE_PIN,SET);
        MOV       S:?b0, #0x1
        LD        A, #0x8
        LDW       X, #0x500f
        JP        L:GPIO_WriteBit
//   35 		break;
//   36 	}
//   37 	case motor_rise:
//   38 	{
//   39 		GPIO_WriteBit(MOTOR_GPIO,MOTOR_FALL_PIN,RESET);
??motor_act_1:
        CLR       S:?b0
        LD        A, #0x4
        LDW       X, #0x500f
        CALL      L:GPIO_WriteBit
//   40 		GPIO_WriteBit(MOTOR_GPIO,MOTOR_RISE_PIN,SET);
        MOV       S:?b0, #0x1
        LD        A, #0x8
        LDW       X, #0x500f
        JP        L:GPIO_WriteBit
//   41 		break;
//   42 	}
//   43 	case motor_fall:
//   44 	{
//   45 		GPIO_WriteBit(MOTOR_GPIO,MOTOR_FALL_PIN,SET);
??motor_act_2:
        MOV       S:?b0, #0x1
        LD        A, #0x4
        LDW       X, #0x500f
        CALL      L:GPIO_WriteBit
//   46 		GPIO_WriteBit(MOTOR_GPIO,MOTOR_RISE_PIN,RESET);
        CLR       S:?b0
        LD        A, #0x8
        LDW       X, #0x500f
        JP        L:GPIO_WriteBit
//   47 		break;
//   48 	}
//   49 	default:
//   50 	{
//   51 		break;
//   52 	}
//   53 	}
//   54 }

        SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
        CODE
//   55 void motor_contro_act(motor_state *cmd)
//   56 {
motor_contro_act:
        CALL      L:?push_l2
        LDW       S:?w4, X
//   57 	float df;
//   58 	static uint8_t OverCurrentCount = 0;
//   59 	static uint8_t MotorTurnCount = 0;
//   60 	static bool MotorActionFlag = FALSE;
//   61 	static motor_state motor_cur_state = motor_stop;
//   62 	static motor_state motor_next_state = motor_stop;
//   63 	if(!MOTOR_TEST)return;
//   64 	if(MotorActionFlag==FALSE)
        LD        A, L:??MotorActionFlag
        CP        A, #0x0
        JRNE      L:??motor_contro_act_0
//   65 	{
//   66 		MotorActionFlag = TRUE;
        MOV       L:??MotorActionFlag, #0x1
//   67 		motor_next_state = *cmd;
        LD        A, [S:?w4.w]
        LD        L:??motor_next_state, A
//   68 	}
//   69 	motor_cur_state = motor_next_state;
??motor_contro_act_0:
        LD        A, L:??motor_next_state
        LD        L:??motor_cur_state, A
//   70 	switch(motor_cur_state)
        LD        A, L:??motor_cur_state
        TNZ       A
        JRNE      ??lb_0
        JP        L:??motor_contro_act_1
??lb_0:
        DEC       A
        JREQ      L:??motor_contro_act_2
        DEC       A
        JREQ      L:??motor_contro_act_3
        DEC       A
        JREQ      L:??motor_contro_act_4
        SUB       A, #0x2
        JRNE      ??lb_1
        JP        L:??motor_contro_act_5
??lb_1:
        DEC       A
        JREQ      L:??motor_contro_act_6
        DEC       A
//   71 	{
//   72 	case motor_idle:
//   73 	{
//   74                 
//   75 		break;
        JP        L:?epilogue_l2
//   76 	}
//   77 	case motor_rise:
//   78 	{
//   79                 motor_act(motor_stop);
??motor_contro_act_2:
        CLR       A
        CALL      L:motor_act
//   80                 if(MotorStr.delay_ms500)break;
        LD        A, L:MotorStr + 2
        CP        A, #0x0
        JREQ      ??lb_2
        JP        L:??motor_contro_act_7
//   81 		motor_act(motor_cur_state);
??lb_2:
        LD        A, L:??motor_cur_state
        CALL      L:motor_act
//   82 		motor_next_state = motor_delay_ms200;
        MOV       L:??motor_next_state, #0x3
//   83 		break;
        JP        L:?epilogue_l2
//   84 	}
//   85 	case motor_fall:
//   86 	{
//   87                 motor_act(motor_stop);
??motor_contro_act_3:
        CLR       A
        CALL      L:motor_act
//   88                 if(MotorStr.delay_ms500)break;
        LD        A, L:MotorStr + 2
        CP        A, #0x0
        JREQ      ??lb_3
        JP        L:??motor_contro_act_7
//   89 		motor_act(motor_cur_state);
??lb_3:
        LD        A, L:??motor_cur_state
        CALL      L:motor_act
//   90 		motor_next_state = motor_delay_ms200;
        MOV       L:??motor_next_state, #0x3
//   91 		break;
        JP        L:?epilogue_l2
//   92 	}
//   93 	case motor_delay_ms200:
//   94 	{
//   95 		MotorStr.motor_delay = MOTOR_DELAY;
??motor_contro_act_4:
        MOV       L:MotorStr, #0x8
//   96 		MotorStr.delay_ms200 = MOTOR_DELAY_MS200;
        MOV       L:MotorStr + 1, #0x14
//   97 		motor_next_state = motor_busy;
        MOV       L:??motor_next_state, #0x6
//   98 		break;
        JP        L:?epilogue_l2
//   99 	}
//  100 	case motor_busy:
//  101 	{
//  102 		if(MotorStr.delay_ms200)break;
??motor_contro_act_6:
        LD        A, L:MotorStr + 1
        CP        A, #0x0
        JREQ      ??lb_4
        JP        L:??motor_contro_act_7
//  103 		df = MotorADGet()/4096.0*3.3/0.1;
??lb_4:
        CALL      L:MotorADGet
        CALL      L:?fcastf32u16_l0_x
        CALL      L:?fdiv32_l0_l0_dc32
        DATA
        DC32      0x45800000
        CODE
        CALL      L:?fmul32_l0_l0_dc32
        DATA
        DC32      0x40533333
        CODE
        CALL      L:?fdiv32_l0_l0_dc32
        DATA
        DC32      0x3dcccccd
        CODE
        CALL      L:?mov_l2_l0
//  104 		ADC_ClearFlag(ADC1,ADC_FLAG_EOC);
        LD        A, #0x1
        LDW       X, #0x5340
        CALL      L:ADC_ClearFlag
//  105 		ADC_ChannelCmd(ADC1,ADC_Channel_2,DISABLE);
        CLR       A
        LDW       Y, #0x304
        LDW       X, #0x5340
        CALL      L:ADC_ChannelCmd
//  106 		if(df>1.2)
        CALL      L:?mov_l0_l2
        CALL      L:?fcmpge32_c_l0_dc32
        DATA
        DC32      0x3f99999b
        CODE
        JRC       L:??motor_contro_act_8
//  107 			OverCurrentCount++;
        LD        A, L:??OverCurrentCount
        ADD       A, #0x1
        LD        L:??OverCurrentCount, A
        JRA       L:??motor_contro_act_9
//  108 		else
//  109 			OverCurrentCount = 0;
??motor_contro_act_8:
        MOV       L:??OverCurrentCount, #0x0
//  110 		if((OverCurrentCount>=10)||MotorStr.motor_delay==0)
??motor_contro_act_9:
        LD        A, L:??OverCurrentCount
        CP        A, #0xa
        JRNC      L:??motor_contro_act_10
        LD        A, L:MotorStr
        CP        A, #0x0
        JREQ      ??lb_5
        JP        L:??motor_contro_act_7
//  111 		{
//  112 			OverCurrentCount = 0;
??lb_5:
??motor_contro_act_10:
        MOV       L:??OverCurrentCount, #0x0
//  113 			motor_next_state = motor_turn;
        MOV       L:??motor_next_state, #0x5
//  114 		}
//  115 		break;
        JP        L:?epilogue_l2
//  116 	}
//  117 	case motor_turn:
//  118 	{
//  119 		if(*cmd == motor_rise)
??motor_contro_act_5:
        LD        A, [S:?w4.w]
        CP        A, #0x1
        JRNE      L:??motor_contro_act_11
//  120 		{
//  121 			if(LIMIT_READ)
        LD        A, #0x1
        LDW       X, #0x5005
        CALL      L:GPIO_ReadInputDataBit
        CP        A, #0x0
        JREQ      L:??motor_contro_act_12
//  122 			{
//  123 				motor_next_state = motor_stop;
        MOV       L:??motor_next_state, #0x0
//  124 				MotorStr.motor_delay = 0;
        MOV       L:MotorStr, #0x0
//  125 				MotorTurnCount = 0;
        MOV       L:??MotorTurnCount, #0x0
//  126 				bsp_beep_two();
        CALL      L:bsp_beep_two
//  127 				//beep_start(1,3,2);
//  128 				LockCarState.MotorActResults = RISE_OK;
        MOV       L:LockCarState + 5, #0x1
        JP        L:?epilogue_l2
//  129 			}
//  130 			else
//  131 			{
//  132 				//if(!MOTOR_TEST)printf("cnt= %d\n",MotorTurnCount);
//  133 				MotorTurnCount++;
??motor_contro_act_12:
        LD        A, L:??MotorTurnCount
        ADD       A, #0x1
        LD        L:??MotorTurnCount, A
//  134 				if(MotorTurnCount%2)
        LD        A, L:??MotorTurnCount
        MOV       S:?b0, #0x2
        CLRW      X
        LD        XL, A
        LD        A, S:?b0
        DIV       X, A
        CP        A, #0x0
        JREQ      L:??motor_contro_act_13
//  135 				{
//  136 					motor_next_state = motor_fall;
        MOV       L:??motor_next_state, #0x2
//  137                                         MotorStr.delay_ms500 = MOTOR_DELAY_MS500;
        MOV       L:MotorStr + 2, #0x32
        JRA       L:??motor_contro_act_14
//  138 				}
//  139 				else
//  140 				{
//  141 					motor_next_state = motor_rise;
??motor_contro_act_13:
        MOV       L:??motor_next_state, #0x1
//  142                                         MotorStr.delay_ms500 = MOTOR_DELAY_MS500;
        MOV       L:MotorStr + 2, #0x32
//  143 				}
//  144 				if(MotorTurnCount>=6)
??motor_contro_act_14:
        LD        A, L:??MotorTurnCount
        CP        A, #0x6
        JRC       L:??motor_contro_act_7
//  145 				{
//  146 					MotorTurnCount = 0;
        MOV       L:??MotorTurnCount, #0x0
//  147 					motor_next_state = motor_stop;
        MOV       L:??motor_next_state, #0x0
//  148 					LockCarState.MotorActResults = RISE_ERROR;
        MOV       L:LockCarState + 5, #0x3
        JP        L:?epilogue_l2
//  149 				}
//  150 			}
//  151 		}
//  152 		else if(*cmd == motor_fall)
??motor_contro_act_11:
        LD        A, [S:?w4.w]
        CP        A, #0x2
        JRNE      L:??motor_contro_act_7
//  153 		{
//  154 			LockCarState.MotorActResults = FALL_ERROR;
        MOV       L:LockCarState + 5, #0x4
//  155 			if(LIMIT_READ==0)
        LD        A, #0x1
        LDW       X, #0x5005
        CALL      L:GPIO_ReadInputDataBit
        CP        A, #0x0
        JRNE      L:??motor_contro_act_15
//  156 			{
//  157 				bsp_beep_two();//beep_start(1,3,2);
        CALL      L:bsp_beep_two
//  158 				LockCarState.MotorActResults = FALL_OK;
        MOV       L:LockCarState + 5, #0x2
//  159                                 QMC5883LStr.DataOkFlag=0;
        MOV       L:QMC5883LStr, #0x0
//  160 			}
//  161 			motor_next_state = motor_stop;
??motor_contro_act_15:
        MOV       L:??motor_next_state, #0x0
//  162 		}
//  163 		break;
        JP        L:?epilogue_l2
//  164 	}
//  165 	case motor_stop:
//  166 	{
//  167                 ADC_Cmd(ADC1,DISABLE);
??motor_contro_act_1:
        CLR       A
        LDW       X, #0x5340
        CALL      L:ADC_Cmd
//  168 		OverCurrentCount = 0;
        MOV       L:??OverCurrentCount, #0x0
//  169 		MotorTurnCount = 0;
        MOV       L:??MotorTurnCount, #0x0
//  170 		MotorActionFlag = FALSE;
        MOV       L:??MotorActionFlag, #0x0
//  171 		*cmd = motor_stop;
        CLR       A
        LD        [S:?w4.w], A
//  172 		motor_act(motor_cur_state);
        LD        A, L:??motor_cur_state
        CALL      L:motor_act
//  173 		motor_next_state = motor_idle;
        MOV       L:??motor_next_state, #0x7
//  174                 GPIO_Init(MOTOR_GPIO,MOTOR_FALL_PIN,GPIO_Mode_In_FL_No_IT);
        CLR       S:?b0
        LD        A, #0x4
        LDW       X, #0x500f
        CALL      L:GPIO_Init
//  175                 GPIO_Init(MOTOR_GPIO,MOTOR_RISE_PIN,GPIO_Mode_In_FL_No_IT);
        CLR       S:?b0
        LD        A, #0x8
        LDW       X, #0x500f
        CALL      L:GPIO_Init
//  176 		break;
//  177 	}
//  178 	default:
//  179 		break;
//  180 	}
//  181 }
??motor_contro_act_7:
        JP        L:?epilogue_l2

        SECTION `.near.bss`:DATA:REORDER:NOROOT(0)
??OverCurrentCount:
        DS8 1

        SECTION `.near.bss`:DATA:REORDER:NOROOT(0)
??MotorTurnCount:
        DS8 1

        SECTION `.near.bss`:DATA:REORDER:NOROOT(0)
??MotorActionFlag:
        DS8 1

        SECTION `.near.bss`:DATA:REORDER:NOROOT(0)
??motor_cur_state:
        DS8 1

        SECTION `.near.bss`:DATA:REORDER:NOROOT(0)
??motor_next_state:
        DS8 1
//  182 

        SECTION `.near_func.text`:CODE:REORDER:NOROOT(0)
        CODE
//  183 void CarAction(bool ReportFlag,uint8_t RepotCnt,enum CMD_SOURCE CmdSource,enum MOTOR_CMD MotorCmd)
//  184 {
CarAction:
        CALL      L:?push_l2
        LD        S:?b11, A
        MOV       S:?b10, S:?b0
        MOV       S:?b9, S:?b1
        MOV       S:?b8, S:?b2
//  185         motor_init();
        CALL      L:motor_init
//  186 	LockCarState.HostReportFlag = ReportFlag;
        LD        A, S:?b11
        LD        L:LockCarState, A
//  187 	LockCarState.HostReportCnt = RepotCnt;
        LD        A, S:?b10
        LD        L:LockCarState + 1, A
//  188 	LockCarState.MotorCmdSource = CmdSource;
        LD        A, S:?b9
        LD        L:LockCarState + 3, A
//  189 	LockCarState.MotorCmd = MotorCmd;
        LD        A, S:?b8
        LD        L:LockCarState + 4, A
//  190         mag_time_count = 0;
        MOV       L:mag_time_count, #0x0
//  191 }
        JP        L:?epilogue_l2

        SECTION VREGS:DATA:REORDER:NOROOT(0)

        END
//  192 
// 
//  15 bytes in section .near.bss
// 686 bytes in section .near_func.text
// 
// 686 bytes of CODE memory
//  15 bytes of DATA memory
//
//Errors: none
//Warnings: 1
