// File: /workspace/github_c_makefile_star2_O3/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 0x38a8(%rip), %rdi      # 0x6577
callq 0x2260
movb %bl, 0x755e(%rip)       # 0xa238
testb %bl, %bl
jne 0x2ce4
movzwl 0x10(%rbp), %ebx
jmp 0x2ce6
xorl %ebx, %ebx
movl $0xff, %eax
testb %r13b, %r13b
je 0x2cf3
movl 0x20(%rbp), %eax
movb %al, 0x7487(%rip)       # 0xa180
cmpl $0x1, %r12d
je 0x2e22
testl %r12d, %r12d
jne 0x2f50
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x400a
movl %r12d, 0x7633(%rip)     # 0xa350
movl %ebx, -0x2c(%rbp)
movq -0x38(%rbp), %rbx
movl %ebx, 0x762a(%rip)      # 0xa354
movl $0xb, 0x7628(%rip)      # 0xa35c
addl %r15d, %r15d
movl %r15d, %edi
callq 0x3ab5
movb %al, 0x761b(%rip)       # 0xa360
movl %r12d, 0x75b8(%rip)     # 0xa304
leaq 0x75b1(%rip), %r15      # 0xa304
shll $0x3, %r14d
movw %r14w, 0x75a9(%rip)     # 0xa308
movl $0x4, 0x75a3(%rip)      # 0xa30c
movb $0x18, 0x75a0(%rip)     # 0xa310
movl %r12d, 0x759d(%rip)     # 0xa314
xorb $0x1, %r13b
movzbl %r13b, %eax
movl %eax, 0x7593(%rip)      # 0xa318
movb 0x73f5(%rip), %al       # 0xa180
movb %al, 0x758b(%rip)       # 0xa31c
cmpb $0x0, 0x28(%rbp)
movl $0xf2, %eax
movl $0x1, %ecx
cmovnel %eax, %ecx
movl %ecx, 0x7578(%rip)      # 0xa320
movl $0x1, 0x7572(%rip)      # 0xa324
leaq 0x37f3(%rip), %rdi      # 0x65ac
callq 0x2260
xorl %edi, %edi
callq 0x3b7d
xorl %edi, %edi
cmpl %r12d, 0x7582(%rip)     # 0xa350
setne %dil
callq 0x2af8
leaq 0x7572(%rip), %rdi      # 0xa350
callq 0x443c
movq %r15, %rdi
callq 0x4526
leaq -0x40(%rbp), %rdi
movw $0x94c1, (%rdi)         # imm = 0x94C1
movb $-0x3f, 0x2(%rdi)
movl %r12d, 0x3(%rdi)
movb $0x0, 0x7(%rdi)
callq 0x3ce4
movl $0x1ff, %edi            # imm = 0x1FF
callq 0x3d6e
movzwl -0x2c(%rbp), %eax
imull $0x1f40, %eax, %eax     # imm = 0x1F40
xorl %edx, %edx
divl %ebx
jmp 0x2f4a
movzbl %r13b, %r13d
xorl %edi, %edi
callq 0x400a
movl $0x1, %r12d
movl %r12d, 0x7516(%rip)     # 0xa350
movq -0x38(%rbp), %rdx
movl %edx, 0x7520(%rip)      # 0xa364
movl %r15d, %eax
leaq 0x3392(%rip), %rcx      # 0x61e0
movl (%rcx,%rax,4), %eax
movl %eax, 0x7511(%rip)      # 0xa368
movzbl -0x2c(%rbp), %eax
movl %eax, 0x750b(%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, 0x74e8(%rip)       # 0xa370
movl %r12d, 0x7475(%rip)     # 0xa304
addl $-0x5, %edx
cmpw $0xd, %r14w
movl $0xc, %eax
cmovael %r14d, %eax
leaq 0x745d(%rip), %r15      # 0xa304
cmpl $0x2, %edx
cmovael %r14d, %eax
movw %ax, 0x7473(%rip)       # 0xa328
movl %r13d, 0x7470(%rip)     # 0xa32c
movb 0x72be(%rip), %al       # 0xa180
movb %al, 0x7468(%rip)       # 0xa330
movzbl 0x28(%rbp), %eax
movl %eax, 0x7462(%rip)      # 0xa334
movzbl 0x40(%rbp), %eax
movl %eax, 0x745c(%rip)      # 0xa338
leaq 0x36c9(%rip), %rdi      # 0x65ac
callq 0x2260
xorl %edi, %edi
callq 0x3b7d
xorl %edi, %edi
cmpl $0x0, 0x7458(%rip)      # 0xa350
setne %dil
callq 0x2af8
leaq 0x7448(%rip), %rdi      # 0xa350
callq 0x443c
movq %r15, %rdi
callq 0x4526
movzbl %bl, %edi
callq 0x402e
cmpl %r12d, 0x7414(%rip)     # 0xa338
setne %bl
movl $0x736, %edi            # imm = 0x736
callq 0x4e50
andb $-0x5, %al
shlb $0x2, %bl
orb %al, %bl
movzbl %bl, %esi
movl $0x736, %edi            # imm = 0x736
callq 0x4dd3
movl $0xffff, %eax           # imm = 0xFFFF
movl %eax, 0x72e4(%rip)      # 0xa234
leaq 0x3632(%rip), %rdi      # 0x6589
callq 0x2260
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq