// File: /workspace/github_c_makefile_star2_O2/lupyuen[P]lora-sx1262/src/radio.c
// Function: RadioSetTxConfig
void RadioSetTxConfig( RadioModems_t modem, int8_t power, uint32_t fdev,
                        uint32_t bandwidth, uint32_t datarate,
                        uint8_t coderate, uint16_t preambleLen,
                        bool fixLen, bool crcOn, bool freqHopOn,
                        uint8_t hopPeriod, bool iqInverted, uint32_t timeout )
{
    printf("RadioSetTxConfig: modem=%d, power=%d, fdev=%d, bandwidth=%d, datarate=%d, coderate=%d, preambleLen=%d, fixLen=%d, crcOn=%d, freqHopOn=%d, hopPeriod=%d, iqInverted=%d, timeout=%d\r\n", (int) modem, (int) power, (int) fdev, (int) bandwidth, (int) datarate, (int) coderate, (int) preambleLen, (int) fixLen, (int) crcOn, (int) freqHopOn, (int) hopPeriod, (int) iqInverted, (int) timeout);
    switch( modem )
    {
        case MODEM_FSK:
            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.ModulationParams.Params.Gfsk.Fdev = fdev;

            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;

            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 );
            break;

        case MODEM_LORA:
            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;

            ///////////////////////////

            //// TODO
            printf("RadioSetTxConfig: SpreadingFactor=%d, Bandwidth=%d, CodingRate=%d, LowDatarateOptimize=%d, PreambleLength=%d, HeaderType=%d, PayloadLength=%d, CrcMode=%d, InvertIQ=%d\r\n", (int) SX126x.ModulationParams.Params.LoRa.SpreadingFactor, (int) SX126x.ModulationParams.Params.LoRa.Bandwidth, (int) SX126x.ModulationParams.Params.LoRa.CodingRate, (int) SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize, (int) SX126x.PacketParams.Params.LoRa.PreambleLength, (int) SX126x.PacketParams.Params.LoRa.HeaderType, (int) SX126x.PacketParams.Params.LoRa.PayloadLength, (int) SX126x.PacketParams.Params.LoRa.CrcMode, (int) SX126x.PacketParams.Params.LoRa.InvertIQ); //// TODO

            // SX126x.ModulationParams.Params.LoRa.SpreadingFactor;
            // SX126x.ModulationParams.Params.LoRa.Bandwidth;
            // SX126x.ModulationParams.Params.LoRa.CodingRate;
            // SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize;

            // SX126x.PacketParams.Params.LoRa.PreambleLength;
            // SX126x.PacketParams.Params.LoRa.HeaderType;
            // SX126x.PacketParams.Params.LoRa.PayloadLength;
            // SX126x.PacketParams.Params.LoRa.CrcMode;
            // SX126x.PacketParams.Params.LoRa.InvertIQ;

            ///////////////////////////

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

    // WORKAROUND - Modulation Quality with 500 kHz LoRa Bandwidth, see DS_SX1261-2_V1.2 datasheet chapter 15.1
    if( ( modem == MODEM_LORA ) && ( SX126x.ModulationParams.Params.LoRa.Bandwidth == LORA_BW_500 ) )
    {
        SX126xWriteRegister( REG_TX_MODULATION, SX126xReadRegister( REG_TX_MODULATION ) & ~( 1 << 2 ) );
    }
    else
    {
        SX126xWriteRegister( REG_TX_MODULATION, SX126xReadRegister( REG_TX_MODULATION ) | ( 1 << 2 ) );
    }
    // WORKAROUND END

    SX126xSetRfTxPower( power );
    TxTimeout = timeout;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r9d, %r14d
movl %r8d, %r9d
movl %ecx, %r8d
movl %edx, %ecx
movl %esi, %edx
movl %edi, %esi
movzwl 0x10(%rbp), %r15d
movzbl 0x30(%rbp), %eax
movq %rax, -0x38(%rbp)
movzbl 0x18(%rbp), %r12d
movzbl 0x20(%rbp), %ebx
movzbl 0x28(%rbp), %r11d
movzbl 0x38(%rbp), %r13d
leaq 0x325a(%rip), %rdi      # 0x62be
movl %edx, -0x40(%rbp)
movl %ecx, -0x3c(%rbp)
movl %r8d, -0x2c(%rbp)
movq %r9, -0x48(%rbp)
xorl %eax, %eax
movl 0x40(%rbp), %r10d
pushq %r10
movq %r13, -0x50(%rbp)
pushq %r13
pushq -0x38(%rbp)
pushq %r11
movq %rbx, -0x58(%rbp)
pushq %rbx
movq %r12, -0x60(%rbp)
pushq %r12
movq %r15, -0x38(%rbp)
pushq %r15
movq %r14, %rbx
pushq %r14
movl %esi, %r14d
callq 0x2050
addq $0x40, %rsp
cmpl $0x1, %r14d
je 0x31ab
testl %r14d, %r14d
jne 0x32bd
andl $0x0, 0x729f(%rip)      # 0xa360
leaq 0x7298(%rip), %r15      # 0xa360
movq -0x48(%rbp), %rax
movl %eax, 0x7292(%rip)      # 0xa364
movl $0xb, 0x7290(%rip)      # 0xa36c
movl -0x2c(%rbp), %edi
addl %edi, %edi
callq 0x3b24
movb %al, 0x7284(%rip)       # 0xa370
movl -0x3c(%rbp), %eax
movl %eax, 0x7273(%rip)      # 0xa368
andl $0x0, 0x7218(%rip)      # 0xa314
movq -0x38(%rbp), %rax
leal (,%rax,8), %eax
movw %ax, 0x720a(%rip)       # 0xa318
movl $0x4, 0x7204(%rip)      # 0xa31c
movb $0x18, 0x7201(%rip)     # 0xa320
andl $0x0, 0x71fe(%rip)      # 0xa324
leaq 0x71e7(%rip), %rbx      # 0xa314
movb 0x18(%rbp), %al
xorb $0x1, %al
movzbl %al, %eax
movl %eax, 0x71ed(%rip)      # 0xa328
cmpb $0x0, 0x20(%rbp)
movl $0xf2, %eax
pushq $0x1
popq %rcx
cmovnel %eax, %ecx
movl %ecx, 0x71e0(%rip)      # 0xa330
movl $0x1, 0x71da(%rip)      # 0xa334
callq 0x35ee
xorl %eax, %eax
xorl %edi, %edi
cmpl %eax, 0x71f7(%rip)      # 0xa360
setne %dil
callq 0x2c08
movq %r15, %rdi
callq 0x43eb
movq %rbx, %rdi
callq 0x449d
leaq -0x68(%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
jmp 0x32bd
pushq $0x1
popq %rax
movl %eax, 0x71ac(%rip)      # 0xa360
leaq 0x71a5(%rip), %r13      # 0xa360
movq -0x48(%rbp), %rsi
movl %esi, 0x71af(%rip)      # 0xa374
movl -0x2c(%rbp), %r9d
movl %r9d, %ecx
leaq 0x300d(%rip), %rdx      # 0x61e0
movl (%rdx,%rcx,4), %edx
movl %edx, 0x719c(%rip)      # 0xa378
movq %rbx, %rcx
movl %ecx, 0x7197(%rip)      # 0xa37c
testl %r9d, %r9d
sete %dil
leal -0xb(%rsi), %r8d
cmpl $0x2, %r8d
setb %r8b
andb %dil, %r8b
xorl %eax, %r9d
movl %esi, %edi
xorl $0xc, %edi
orl %r9d, %edi
sete %dil
orb %r8b, %dil
movb %dil, 0x716c(%rip)      # 0xa380
movl %eax, 0x70fa(%rip)      # 0xa314
leaq 0x70f3(%rip), %rbx      # 0xa314
movq -0x38(%rbp), %r8
cmpw $0xd, %r8w
pushq $0xc
popq %rax
cmovael %r8d, %eax
leal -0x5(%rsi), %r9d
cmpl $0x2, %r9d
cmovael %r8d, %eax
movw %ax, 0x70f4(%rip)       # 0xa338
movq -0x60(%rbp), %r11
movl %r11d, 0x70ed(%rip)     # 0xa33c
movzbl 0x6f39(%rip), %r10d     # 0xa190
movb %r10b, 0x70e2(%rip)     # 0xa340
movq -0x58(%rbp), %r15
movl %r15d, 0x70db(%rip)     # 0xa344
movq -0x50(%rbp), %r12
movl %r12d, 0x70d4(%rip)     # 0xa348
movzbl %dil, %r8d
movzwl %ax, %r9d
leaq 0x30ef(%rip), %rdi      # 0x6372
xorl %eax, %eax
pushq %r12
pushq %r15
pushq %r10
pushq %r11
callq 0x2050
addq $0x20, %rsp
callq 0x35ee
xorl %edi, %edi
cmpl $0x0, 0x70bc(%rip)      # 0xa360
setne %dil
callq 0x2c08
movq %r13, %rdi
callq 0x43eb
movq %rbx, %rdi
callq 0x449d
xorl $0x1, %r14d
movl 0x70b1(%rip), %eax      # 0xa378
xorl $0x6, %eax
orl %r14d, %eax
setne %bl
movl $0x889, %edi            # imm = 0x889
callq 0x4c96
andb $-0x5, %al
shlb $0x2, %bl
orb %al, %bl
movzbl %bl, %esi
movl $0x889, %edi            # imm = 0x889
callq 0x4c2e
movsbl -0x40(%rbp), %edi
callq 0x4d39
movl 0x40(%rbp), %eax
movl %eax, 0x6f40(%rip)      # 0xa240
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq