// File: /workspace/github_c_makefile_star2_O2/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 0x37d0(%rip), %rdi      # 0x6577
callq 0x2280
movb %bl, 0x7496(%rip)       # 0xa248
testb %bl, %bl
jne 0x2dbc
movzwl 0x10(%rbp), %ebx
jmp 0x2dbe
xorl %ebx, %ebx
movl $0xff, %eax
testb %r13b, %r13b
je 0x2dcb
movl 0x20(%rbp), %eax
movb %al, 0x73bf(%rip)       # 0xa190
cmpl $0x1, %r12d
je 0x2ee6
testl %r12d, %r12d
jne 0x3002
xorl %edi, %edi
callq 0x3ff1
andl $0x0, 0x756e(%rip)      # 0xa360
movq -0x38(%rbp), %r12
movl %r12d, 0x7567(%rip)     # 0xa364
movl $0xb, 0x7565(%rip)      # 0xa36c
addl %r15d, %r15d
movl %r15d, %edi
callq 0x3b24
movb %al, 0x7558(%rip)       # 0xa370
andl $0x0, 0x74f5(%rip)      # 0xa314
leaq 0x74ee(%rip), %r15      # 0xa314
shll $0x3, %r14d
movw %r14w, 0x74e6(%rip)     # 0xa318
movl $0x4, 0x74e0(%rip)      # 0xa31c
movb $0x18, 0x74dd(%rip)     # 0xa320
andl $0x0, 0x74da(%rip)      # 0xa324
xorb $0x1, %r13b
movzbl %r13b, %eax
movl %eax, 0x74d0(%rip)      # 0xa328
movb 0x7332(%rip), %al       # 0xa190
movb %al, 0x74c8(%rip)       # 0xa32c
cmpb $0x0, 0x28(%rbp)
movl $0xf2, %eax
pushq $0x1
popq %rcx
cmovnel %eax, %ecx
movl %ecx, 0x74b7(%rip)      # 0xa330
movl $0x1, 0x74b1(%rip)      # 0xa334
callq 0x35ee
xorl %edi, %edi
xorl %eax, %eax
cmpl %eax, 0x74ce(%rip)      # 0xa360
setne %dil
callq 0x2c08
leaq 0x74be(%rip), %rdi      # 0xa360
callq 0x43eb
movq %r15, %rdi
callq 0x449d
leaq -0x40(%rbp), %rdi
movw $0x94c1, (%rdi)         # imm = 0x94C1
movb $-0x3f, 0x2(%rdi)
andl $0x0, 0x3(%rdi)
movb $0x0, 0x7(%rdi)
callq 0x3d19
movl $0x1ff, %edi            # imm = 0x1FF
callq 0x3d9d
movzwl %bx, %eax
imull $0x1f40, %eax, %eax     # imm = 0x1F40
xorl %edx, %edx
divl %r12d
jmp 0x2ffc
movzbl %r13b, %r13d
xorl %edi, %edi
callq 0x3ff1
pushq $0x1
popq %r12
movl %r12d, 0x7464(%rip)     # 0xa360
movq -0x38(%rbp), %rdx
movl %edx, 0x746e(%rip)      # 0xa374
movl %r15d, %eax
leaq 0x32d0(%rip), %rcx      # 0x61e0
movl (%rcx,%rax,4), %eax
movl %eax, 0x745f(%rip)      # 0xa378
movzbl -0x2c(%rbp), %eax
movl %eax, 0x7459(%rip)      # 0xa37c
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, 0x7436(%rip)       # 0xa380
movl %r12d, 0x73c3(%rip)     # 0xa314
addl $-0x5, %edx
cmpw $0xd, %r14w
pushq $0xc
popq %rax
cmovael %r14d, %eax
leaq 0x73ad(%rip), %r15      # 0xa314
cmpl $0x2, %edx
cmovael %r14d, %eax
movw %ax, 0x73c3(%rip)       # 0xa338
movl %r13d, 0x73c0(%rip)     # 0xa33c
movb 0x720e(%rip), %al       # 0xa190
movb %al, 0x73b8(%rip)       # 0xa340
movzbl 0x28(%rbp), %eax
movl %eax, 0x73b2(%rip)      # 0xa344
movzbl 0x40(%rbp), %eax
movl %eax, 0x73ac(%rip)      # 0xa348
callq 0x35ee
xorl %edi, %edi
cmpl $0x0, 0x73b6(%rip)      # 0xa360
setne %dil
callq 0x2c08
leaq 0x73a6(%rip), %rdi      # 0xa360
callq 0x43eb
movq %r15, %rdi
callq 0x449d
movzbl %bl, %edi
callq 0x4013
cmpl %r12d, 0x7372(%rip)     # 0xa348
setne %bl
movl $0x736, %edi            # imm = 0x736
callq 0x4c96
andb $-0x5, %al
shlb $0x2, %bl
orb %al, %bl
movzbl %bl, %esi
movl $0x736, %edi            # imm = 0x736
callq 0x4c2e
movl $0xffff, %eax           # imm = 0xFFFF
movl %eax, 0x7242(%rip)      # 0xa244
leaq 0x3580(%rip), %rdi      # 0x6589
callq 0x2280
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq