// File: /workspace/github_c_makefile_star2_O1/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 %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r9d, %r14d
movl %ecx, -0x2c(%rbp)
movq %rdx, -0x38(%rbp)
movl %esi, %r15d
movl %edi, %r12d
movb 0x18(%rbp), %r13b
movb 0x48(%rbp), %bl
leaq 0x38a4(%rip), %rdi      # 0x6577
callq 0x2260
movb %bl, 0x755a(%rip)       # 0xa238
testb %bl, %bl
jne 0x2ce8
movzwl 0x10(%rbp), %ebx
jmp 0x2cea
xorl %ebx, %ebx
movl $0xff, %eax
testb %r13b, %r13b
je 0x2cf7
movl 0x20(%rbp), %eax
movb %al, 0x7483(%rip)       # 0xa180
cmpl $0x1, %r12d
je 0x2e26
testl %r12d, %r12d
jne 0x2f54
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x3fb7
movl %r12d, 0x762f(%rip)     # 0xa350
movl %ebx, -0x2c(%rbp)
movq -0x38(%rbp), %rbx
movl %ebx, 0x7626(%rip)      # 0xa354
movl $0xb, 0x7624(%rip)      # 0xa35c
addl %r15d, %r15d
movl %r15d, %edi
callq 0x3ab5
movb %al, 0x7617(%rip)       # 0xa360
movl %r12d, 0x75b4(%rip)     # 0xa304
leaq 0x75ad(%rip), %r15      # 0xa304
shll $0x3, %r14d
movw %r14w, 0x75a5(%rip)     # 0xa308
movl $0x4, 0x759f(%rip)      # 0xa30c
movb $0x18, 0x759c(%rip)     # 0xa310
movl %r12d, 0x7599(%rip)     # 0xa314
xorb $0x1, %r13b
movzbl %r13b, %eax
movl %eax, 0x758f(%rip)      # 0xa318
movb 0x73f1(%rip), %al       # 0xa180
movb %al, 0x7587(%rip)       # 0xa31c
cmpb $0x0, 0x28(%rbp)
movl $0xf2, %eax
movl $0x1, %ecx
cmovnel %eax, %ecx
movl %ecx, 0x7574(%rip)      # 0xa320
movl $0x1, 0x756e(%rip)      # 0xa324
leaq 0x37ef(%rip), %rdi      # 0x65ac
callq 0x2260
xorl %edi, %edi
callq 0x3b57
xorl %edi, %edi
cmpl %r12d, 0x757e(%rip)     # 0xa350
setne %dil
callq 0x2afc
leaq 0x756e(%rip), %rdi      # 0xa350
callq 0x43e9
movq %r15, %rdi
callq 0x44d1
leaq -0x40(%rbp), %rdi
movw $0x94c1, (%rdi)         # imm = 0x94C1
movb $-0x3f, 0x2(%rdi)
movl %r12d, 0x3(%rdi)
movb $0x0, 0x7(%rdi)
callq 0x3cbe
movl $0x1ff, %edi            # imm = 0x1FF
callq 0x3d48
movzwl -0x2c(%rbp), %eax
imull $0x1f40, %eax, %eax     # imm = 0x1F40
xorl %edx, %edx
divl %ebx
jmp 0x2f4e
movzbl %r13b, %r13d
xorl %edi, %edi
callq 0x3fb7
movl $0x1, %r12d
movl %r12d, 0x7512(%rip)     # 0xa350
movq -0x38(%rbp), %rdx
movl %edx, 0x751c(%rip)      # 0xa364
movl %r15d, %eax
leaq 0x338e(%rip), %rcx      # 0x61e0
movl (%rcx,%rax,4), %eax
movl %eax, 0x750d(%rip)      # 0xa368
movzbl -0x2c(%rbp), %eax
movl %eax, 0x7507(%rip)      # 0xa36c
testl %r15d, %r15d
sete %al
leal -0xb(%rdx), %ecx
cmpl $0x2, %ecx
setb %cl
andb %al, %cl
xorl %r12d, %r15d
movl %edx, %eax
xorl $0xc, %eax
orl %r15d, %eax
sete %al
orb %cl, %al
movb %al, 0x74e4(%rip)       # 0xa370
movl %r12d, 0x7471(%rip)     # 0xa304
addl $-0x5, %edx
cmpw $0xd, %r14w
movl $0xc, %eax
cmovael %r14d, %eax
leaq 0x7459(%rip), %r15      # 0xa304
cmpl $0x2, %edx
cmovael %r14d, %eax
movw %ax, 0x746f(%rip)       # 0xa328
movl %r13d, 0x746c(%rip)     # 0xa32c
movb 0x72ba(%rip), %al       # 0xa180
movb %al, 0x7464(%rip)       # 0xa330
movzbl 0x28(%rbp), %eax
movl %eax, 0x745e(%rip)      # 0xa334
movzbl 0x40(%rbp), %eax
movl %eax, 0x7458(%rip)      # 0xa338
leaq 0x36c5(%rip), %rdi      # 0x65ac
callq 0x2260
xorl %edi, %edi
callq 0x3b57
xorl %edi, %edi
cmpl $0x0, 0x7454(%rip)      # 0xa350
setne %dil
callq 0x2afc
leaq 0x7444(%rip), %rdi      # 0xa350
callq 0x43e9
movq %r15, %rdi
callq 0x44d1
movzbl %bl, %edi
callq 0x3fdb
cmpl %r12d, 0x7410(%rip)     # 0xa338
setne %bl
movl $0x736, %edi            # imm = 0x736
callq 0x4dfc
andb $-0x5, %al
shlb $0x2, %bl
orb %al, %bl
movzbl %bl, %esi
movl $0x736, %edi            # imm = 0x736
callq 0x4d78
movl $0xffff, %eax           # imm = 0xFFFF
movl %eax, 0x72e0(%rip)      # 0xa234
leaq 0x362e(%rip), %rdi      # 0x6589
callq 0x2260
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq