// File: /workspace/github_c_makefile_star2_O0/lupyuen[P]lora-sx1262/src/radio.c
// Function: RadioSetRxConfig
void RadioSetRxConfig( RadioModems_t modem, uint32_t bandwidth,
                         uint32_t datarate, uint8_t coderate,
                         uint32_t bandwidthAfc, uint16_t preambleLen,
                         uint16_t symbTimeout, bool fixLen,
                         uint8_t payloadLen,
                         bool crcOn, bool freqHopOn, uint8_t hopPeriod,
                         bool iqInverted, bool rxContinuous )
{
    printf("RadioSetRxConfig\r\n");
    RxContinuous = rxContinuous;
    if( rxContinuous == true )
    {
        symbTimeout = 0;
    }
    if( fixLen == true )
    {
        MaxPayloadLength = payloadLen;
    }
    else
    {
        MaxPayloadLength = 0xFF;
    }

    switch( modem )
    {
        case MODEM_FSK:
            SX126xSetStopRxTimerOnPreambleDetect( false );
            SX126x.ModulationParams.PacketType = PACKET_TYPE_GFSK;

            SX126x.ModulationParams.Params.Gfsk.BitRate = datarate;
            SX126x.ModulationParams.Params.Gfsk.ModulationShaping = MOD_SHAPING_G_BT_1;
            SX126x.ModulationParams.Params.Gfsk.Bandwidth = RadioGetFskBandwidthRegValue( bandwidth << 1 ); // SX126x badwidth is double sided

            SX126x.PacketParams.PacketType = PACKET_TYPE_GFSK;
            SX126x.PacketParams.Params.Gfsk.PreambleLength = ( preambleLen << 3 ); // convert byte into bit
            SX126x.PacketParams.Params.Gfsk.PreambleMinDetect = RADIO_PREAMBLE_DETECTOR_08_BITS;
            SX126x.PacketParams.Params.Gfsk.SyncWordLength = 3 << 3; // convert byte into bit
            SX126x.PacketParams.Params.Gfsk.AddrComp = RADIO_ADDRESSCOMP_FILT_OFF;
            SX126x.PacketParams.Params.Gfsk.HeaderType = ( fixLen == true ) ? RADIO_PACKET_FIXED_LENGTH : RADIO_PACKET_VARIABLE_LENGTH;
            SX126x.PacketParams.Params.Gfsk.PayloadLength = MaxPayloadLength;
            if( crcOn == true )
            {
                SX126x.PacketParams.Params.Gfsk.CrcLength = RADIO_CRC_2_BYTES_CCIT;
            }
            else
            {
                SX126x.PacketParams.Params.Gfsk.CrcLength = RADIO_CRC_OFF;
            }
            SX126x.PacketParams.Params.Gfsk.DcFree = RADIO_DC_FREEWHITENING;

            RadioStandby( );
            RadioSetModem( ( SX126x.ModulationParams.PacketType == PACKET_TYPE_GFSK ) ? MODEM_FSK : MODEM_LORA );
            SX126xSetModulationParams( &SX126x.ModulationParams );
            SX126xSetPacketParams( &SX126x.PacketParams );
            SX126xSetSyncWord( ( uint8_t[] ){ 0xC1, 0x94, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x00 } );
            SX126xSetWhiteningSeed( 0x01FF );

            RxTimeout = ( uint32_t )symbTimeout * 8000UL / datarate;
            break;

        case MODEM_LORA:
            SX126xSetStopRxTimerOnPreambleDetect( false );
            SX126x.ModulationParams.PacketType = PACKET_TYPE_LORA;
            SX126x.ModulationParams.Params.LoRa.SpreadingFactor = ( RadioLoRaSpreadingFactors_t )datarate;
            SX126x.ModulationParams.Params.LoRa.Bandwidth = Bandwidths[bandwidth];
            SX126x.ModulationParams.Params.LoRa.CodingRate = ( RadioLoRaCodingRates_t )coderate;

            if( ( ( bandwidth == 0 ) && ( ( datarate == 11 ) || ( datarate == 12 ) ) ) ||
            ( ( bandwidth == 1 ) && ( datarate == 12 ) ) )
            {
                SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize = 0x01;
            }
            else
            {
                SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize = 0x00;
            }

            SX126x.PacketParams.PacketType = PACKET_TYPE_LORA;

            if( ( SX126x.ModulationParams.Params.LoRa.SpreadingFactor == LORA_SF5 ) ||
                ( SX126x.ModulationParams.Params.LoRa.SpreadingFactor == LORA_SF6 ) )
            {
                if( preambleLen < 12 )
                {
                    SX126x.PacketParams.Params.LoRa.PreambleLength = 12;
                }
                else
                {
                    SX126x.PacketParams.Params.LoRa.PreambleLength = preambleLen;
                }
            }
            else
            {
                SX126x.PacketParams.Params.LoRa.PreambleLength = preambleLen;
            }

            SX126x.PacketParams.Params.LoRa.HeaderType = ( RadioLoRaPacketLengthsMode_t )fixLen;

            SX126x.PacketParams.Params.LoRa.PayloadLength = MaxPayloadLength;
            SX126x.PacketParams.Params.LoRa.CrcMode = ( RadioLoRaCrcModes_t )crcOn;
            SX126x.PacketParams.Params.LoRa.InvertIQ = ( RadioLoRaIQModes_t )iqInverted;

            RadioStandby( );
            RadioSetModem( ( SX126x.ModulationParams.PacketType == PACKET_TYPE_GFSK ) ? MODEM_FSK : MODEM_LORA );
            SX126xSetModulationParams( &SX126x.ModulationParams );
            SX126xSetPacketParams( &SX126x.PacketParams );
            SX126xSetLoRaSymbNumTimeout( symbTimeout );

            // WORKAROUND - Optimizing the Inverted IQ Operation, see DS_SX1261-2_V1.2 datasheet chapter 15.4
            if( SX126x.PacketParams.Params.LoRa.InvertIQ == LORA_IQ_INVERTED )
            {
                SX126xWriteRegister( REG_IQ_POLARITY, SX126xReadRegister( REG_IQ_POLARITY ) & ~( 1 << 2 ) );
            }
            else
            {
                SX126xWriteRegister( REG_IQ_POLARITY, SX126xReadRegister( REG_IQ_POLARITY ) | ( 1 << 2 ) );
            }
            // WORKAROUND END

            // Timeout Max, Timeout handled directly in SetRx function
            RxTimeout = 0xFFFF;

            break;
    }
    printf("RadioSetRxConfig done\r\n");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x28, %rsp
movl %r9d, %eax
movl %r8d, %r9d
movl %edx, %r11d
movl %esi, %ebx
movl %edi, %r14d
movw %ax, %r8w
movb %cl, %r10b
movb 0x48(%rbp), %al
movb 0x40(%rbp), %cl
movb 0x38(%rbp), %dl
movb 0x30(%rbp), %dl
movb 0x28(%rbp), %sil
movb 0x20(%rbp), %dil
movb 0x18(%rbp), %dil
movw 0x10(%rbp), %r15w
movl %r14d, -0x1c(%rbp)
movl %ebx, -0x20(%rbp)
movl %r11d, -0x24(%rbp)
movb %r10b, -0x25(%rbp)
movl %r9d, -0x2c(%rbp)
movw %r8w, -0x2e(%rbp)
andb $0x1, %dil
movb %dil, -0x2f(%rbp)
andb $0x1, %sil
movb %sil, -0x30(%rbp)
andb $0x1, %dl
movb %dl, -0x31(%rbp)
andb $0x1, %cl
movb %cl, -0x32(%rbp)
andb $0x1, %al
movb %al, -0x33(%rbp)
leaq 0x53d9(%rip), %rdi      # 0x92e8
movb $0x0, %al
callq 0x2050
movb -0x33(%rbp), %al
andb $0x1, %al
movb %al, 0xa347(%rip)       # 0xe268
movb -0x33(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x3f34
movw $0x0, 0x10(%rbp)
movb -0x2f(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x3f4c
movb 0x20(%rbp), %al
movb %al, 0xa266(%rip)       # 0xe1b0
jmp 0x3f53
movb $-0x1, 0xa25d(%rip)     # 0xe1b0
movl -0x1c(%rbp), %eax
movl %eax, -0x40(%rbp)
testl %eax, %eax
je 0x3f70
jmp 0x3f5f
movl -0x40(%rbp), %eax
subl $0x1, %eax
je 0x40bb
jmp 0x4238
xorl %edi, %edi
callq 0x5b60
movl $0x0, 0xa3ff(%rip)      # 0xe380
movl -0x24(%rbp), %eax
movl %eax, 0xa3fa(%rip)      # 0xe384
movl $0xb, 0xa3f8(%rip)      # 0xe38c
movl -0x20(%rbp), %edi
shll %edi
callq 0x5150
movb %al, 0xa3ec(%rip)       # 0xe390
movl $0x0, 0xa386(%rip)      # 0xe334
movzwl -0x2e(%rbp), %eax
shll $0x3, %eax
movw %ax, 0xa37c(%rip)       # 0xe338
movl $0x4, 0xa376(%rip)      # 0xe33c
movb $0x18, 0xa373(%rip)     # 0xe340
movl $0x0, 0xa36d(%rip)      # 0xe344
movb -0x2f(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
movl $0x1, %eax
xorl %ecx, %ecx
cmpl $0x1, %edx
cmovel %ecx, %eax
movl %eax, 0xa356(%rip)      # 0xe348
movb 0xa1b8(%rip), %al       # 0xe1b0
movb %al, 0xa34e(%rip)       # 0xe34c
movb -0x30(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x4017
movl $0xf2, 0xa33b(%rip)     # 0xe350
jmp 0x4021
movl $0x1, 0xa32f(%rip)      # 0xe350
movl $0x1, 0xa329(%rip)      # 0xe354
callq 0x4900
movl 0xa34a(%rip), %ecx      # 0xe380
movl $0x1, %edi
xorl %eax, %eax
cmpl $0x0, %ecx
cmovel %eax, %edi
callq 0x3c60
leaq 0xa2dd(%rip), %rdi      # 0xe32c
addq $0x54, %rdi
callq 0x61f0
leaq 0xa2cd(%rip), %rdi      # 0xe32c
addq $0x8, %rdi
callq 0x6350
movb $-0x3f, -0x3b(%rbp)
movb $-0x6c, -0x3a(%rbp)
movb $-0x3f, -0x39(%rbp)
movb $0x0, -0x38(%rbp)
movb $0x0, -0x37(%rbp)
movb $0x0, -0x36(%rbp)
movb $0x0, -0x35(%rbp)
movb $0x0, -0x34(%rbp)
leaq -0x3b(%rbp), %rdi
callq 0x5700
movl $0x1ff, %edi            # imm = 0x1FF
callq 0x57e0
movzwl 0x10(%rbp), %eax
movl %eax, %eax
imulq $0x1f40, %rax, %rax     # imm = 0x1F40
movl -0x24(%rbp), %ecx
xorl %edx, %edx
divq %rcx
movl %eax, 0xa1ae(%rip)      # 0xe264
jmp 0x4238
xorl %edi, %edi
callq 0x5b60
movl $0x1, 0xa2b4(%rip)      # 0xe380
movl -0x24(%rbp), %eax
movl %eax, 0xa2bf(%rip)      # 0xe394
movl -0x20(%rbp), %eax
movl %eax, %ecx
leaq 0x50ff(%rip), %rax      # 0x91e0
movl (%rax,%rcx,4), %eax
movl %eax, 0xa2ae(%rip)      # 0xe398
movzbl -0x25(%rbp), %eax
movl %eax, 0xa2a8(%rip)      # 0xe39c
cmpl $0x0, -0x20(%rbp)
jne 0x4106
cmpl $0xb, -0x24(%rbp)
je 0x4112
cmpl $0xc, -0x24(%rbp)
je 0x4112
cmpl $0x1, -0x20(%rbp)
jne 0x411b
cmpl $0xc, -0x24(%rbp)
jne 0x411b
movb $0x1, 0xa287(%rip)      # 0xe3a0
jmp 0x4122
movb $0x0, 0xa27e(%rip)      # 0xe3a0
movl $0x1, 0xa208(%rip)      # 0xe334
cmpl $0x5, 0xa261(%rip)      # 0xe394
je 0x413e
cmpl $0x6, 0xa258(%rip)      # 0xe394
jne 0x415f
movzwl -0x2e(%rbp), %eax
cmpl $0xc, %eax
jge 0x4152
movw $0xc, 0xa208(%rip)      # 0xe358
jmp 0x415d
movw -0x2e(%rbp), %ax
movw %ax, 0xa1fb(%rip)       # 0xe358
jmp 0x416a
movw -0x2e(%rbp), %ax
movw %ax, 0xa1ee(%rip)       # 0xe358
movb -0x2f(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0xa1e4(%rip)      # 0xe35c
movb 0xa032(%rip), %al       # 0xe1b0
movb %al, 0xa1dc(%rip)       # 0xe360
movb -0x30(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0xa1d2(%rip)      # 0xe364
movb -0x32(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0xa1c8(%rip)      # 0xe368
callq 0x4900
movl 0xa1d5(%rip), %ecx      # 0xe380
movl $0x1, %edi
xorl %eax, %eax
cmpl $0x0, %ecx
cmovel %eax, %edi
callq 0x3c60
leaq 0xa168(%rip), %rdi      # 0xe32c
addq $0x54, %rdi
callq 0x61f0
leaq 0xa158(%rip), %rdi      # 0xe32c
addq $0x8, %rdi
callq 0x6350
movw 0x10(%rbp), %ax
movzbl %al, %edi
callq 0x5b90
cmpl $0x1, 0xa178(%rip)      # 0xe368
jne 0x4211
movl $0x736, %edi            # imm = 0x736
callq 0x7210
movzbl %al, %eax
andl $-0x5, %eax
movl $0x736, %edi            # imm = 0x736
movzbl %al, %esi
callq 0x70f0
jmp 0x422e
movl $0x736, %edi            # imm = 0x736
callq 0x7210
movzbl %al, %eax
orl $0x4, %eax
movl $0x736, %edi            # imm = 0x736
movzbl %al, %esi
callq 0x70f0
movl $0xffff, 0xa02c(%rip)   # imm = 0xFFFF
leaq 0x50bc(%rip), %rdi      # 0x92fb
movb $0x0, %al
callq 0x2050
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)