// File: /workspace/github_c_makefile_star2_O0/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 $0x78, %rsp
movl %r9d, %eax
movl %ecx, %r9d
movl %edx, %r10d
movl %edi, %ebx
movb %al, %dil
movb %sil, %r11b
movl 0x40(%rbp), %eax
movb 0x38(%rbp), %al
movb 0x30(%rbp), %cl
movb 0x28(%rbp), %cl
movb 0x20(%rbp), %dl
movb 0x18(%rbp), %sil
movw 0x10(%rbp), %r14w
movl %ebx, -0x2c(%rbp)
movb %r11b, -0x2d(%rbp)
movl %r10d, -0x34(%rbp)
movl %r9d, -0x38(%rbp)
movl %r8d, -0x3c(%rbp)
movb %dil, -0x3d(%rbp)
andb $0x1, %sil
movb %sil, -0x3e(%rbp)
andb $0x1, %dl
movb %dl, -0x3f(%rbp)
andb $0x1, %cl
movb %cl, -0x40(%rbp)
andb $0x1, %al
movb %al, -0x41(%rbp)
movl -0x2c(%rbp), %esi
movsbl -0x2d(%rbp), %edx
movl -0x34(%rbp), %ecx
movl -0x38(%rbp), %r8d
movl -0x3c(%rbp), %r9d
movzbl -0x3d(%rbp), %edi
movzwl 0x10(%rbp), %r10d
movzbl -0x3e(%rbp), %r11d
andl $0x1, %r11d
movzbl -0x3f(%rbp), %ebx
andl $0x1, %ebx
movzbl -0x40(%rbp), %r14d
andl $0x1, %r14d
movzbl 0x30(%rbp), %r15d
movzbl -0x41(%rbp), %r12d
andl $0x1, %r12d
movl 0x40(%rbp), %r13d
movq %rsp, %rax
movq %rax, -0x58(%rbp)
movl %r13d, 0x38(%rax)
movl %r12d, 0x30(%rax)
movl %r15d, 0x28(%rax)
movl %r14d, 0x20(%rax)
movl %ebx, 0x18(%rax)
movl %r11d, 0x10(%rax)
movl %r10d, 0x8(%rax)
movl %edi, (%rax)
leaq 0x4fd8(%rip), %rdi      # 0x9313
xorl %eax, %eax
callq 0x2050
movl -0x2c(%rbp), %eax
movl %eax, -0x50(%rbp)
testl %eax, %eax
je 0x435f
jmp 0x434e
movl -0x50(%rbp), %eax
subl $0x1, %eax
je 0x4485
jmp 0x45fc
movl $0x0, 0xa017(%rip)      # 0xe380
movl -0x3c(%rbp), %eax
movl %eax, 0xa012(%rip)      # 0xe384
movl $0xb, 0xa010(%rip)      # 0xe38c
movl -0x38(%rbp), %edi
shll %edi
callq 0x5150
movb %al, 0xa004(%rip)       # 0xe390
movl -0x34(%rbp), %eax
movl %eax, 0x9ff3(%rip)      # 0xe388
movl $0x0, 0x9f95(%rip)      # 0xe334
movzwl 0x10(%rbp), %eax
shll $0x3, %eax
movw %ax, 0x9f8b(%rip)       # 0xe338
movl $0x4, 0x9f85(%rip)      # 0xe33c
movb $0x18, 0x9f82(%rip)     # 0xe340
movl $0x0, 0x9f7c(%rip)      # 0xe344
movb -0x3e(%rbp), %al
andb $0x1, %al
movzbl %al, %edx
movl $0x1, %eax
xorl %ecx, %ecx
cmpl $0x1, %edx
cmovel %ecx, %eax
movl %eax, 0x9f65(%rip)      # 0xe348
movb -0x3f(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x43fc
movl $0xf2, 0x9f56(%rip)     # 0xe350
jmp 0x4406
movl $0x1, 0x9f4a(%rip)      # 0xe350
movl $0x1, 0x9f44(%rip)      # 0xe354
callq 0x4900
movl 0x9f65(%rip), %ecx      # 0xe380
movl $0x1, %edi
xorl %eax, %eax
cmpl $0x0, %ecx
cmovel %eax, %edi
callq 0x3c60
leaq 0x9ef8(%rip), %rdi      # 0xe32c
addq $0x54, %rdi
callq 0x61f0
leaq 0x9ee8(%rip), %rdi      # 0xe32c
addq $0x8, %rdi
callq 0x6350
movb $-0x3f, -0x49(%rbp)
movb $-0x6c, -0x48(%rbp)
movb $-0x3f, -0x47(%rbp)
movb $0x0, -0x46(%rbp)
movb $0x0, -0x45(%rbp)
movb $0x0, -0x44(%rbp)
movb $0x0, -0x43(%rbp)
movb $0x0, -0x42(%rbp)
leaq -0x49(%rbp), %rdi
callq 0x5700
movl $0x1ff, %edi            # imm = 0x1FF
callq 0x57e0
jmp 0x45fc
movl $0x1, 0x9ef1(%rip)      # 0xe380
movl -0x3c(%rbp), %eax
movl %eax, 0x9efc(%rip)      # 0xe394
movl -0x38(%rbp), %eax
movl %eax, %ecx
leaq 0x4d3c(%rip), %rax      # 0x91e0
movl (%rax,%rcx,4), %eax
movl %eax, 0x9eeb(%rip)      # 0xe398
movzbl -0x3d(%rbp), %eax
movl %eax, 0x9ee5(%rip)      # 0xe39c
cmpl $0x0, -0x38(%rbp)
jne 0x44c9
cmpl $0xb, -0x3c(%rbp)
je 0x44d5
cmpl $0xc, -0x3c(%rbp)
je 0x44d5
cmpl $0x1, -0x38(%rbp)
jne 0x44de
cmpl $0xc, -0x3c(%rbp)
jne 0x44de
movb $0x1, 0x9ec4(%rip)      # 0xe3a0
jmp 0x44e5
movb $0x0, 0x9ebb(%rip)      # 0xe3a0
movl $0x1, 0x9e45(%rip)      # 0xe334
cmpl $0x5, 0x9e9e(%rip)      # 0xe394
je 0x4501
cmpl $0x6, 0x9e95(%rip)      # 0xe394
jne 0x4522
movzwl 0x10(%rbp), %eax
cmpl $0xc, %eax
jge 0x4515
movw $0xc, 0x9e45(%rip)      # 0xe358
jmp 0x4520
movw 0x10(%rbp), %ax
movw %ax, 0x9e38(%rip)       # 0xe358
jmp 0x452d
movw 0x10(%rbp), %ax
movw %ax, 0x9e2b(%rip)       # 0xe358
movb -0x3e(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x9e21(%rip)      # 0xe35c
movb 0x9c6f(%rip), %al       # 0xe1b0
movb %al, 0x9e19(%rip)       # 0xe360
movb -0x3f(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x9e0f(%rip)      # 0xe364
movb -0x41(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x9e05(%rip)      # 0xe368
movl 0x9e2b(%rip), %esi      # 0xe394
movl 0x9e29(%rip), %edx      # 0xe398
movl 0x9e27(%rip), %ecx      # 0xe39c
movzbl 0x9e23(%rip), %r8d      # 0xe3a0
movzwl 0x9dd3(%rip), %r9d      # 0xe358
movl 0x9dd1(%rip), %ebx      # 0xe35c
movzbl 0x9dcd(%rip), %r11d     # 0xe360
movl 0x9dca(%rip), %r10d     # 0xe364
movl 0x9dc8(%rip), %eax      # 0xe368
leaq 0x4e20(%rip), %rdi      # 0x93c7
movl %ebx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movl %eax, 0x18(%rsp)
movb $0x0, %al
callq 0x2050
callq 0x4900
movl 0x9db6(%rip), %ecx      # 0xe380
movl $0x1, %edi
xorl %eax, %eax
cmpl $0x0, %ecx
cmovel %eax, %edi
callq 0x3c60
leaq 0x9d49(%rip), %rdi      # 0xe32c
addq $0x54, %rdi
callq 0x61f0
leaq 0x9d39(%rip), %rdi      # 0xe32c
addq $0x8, %rdi
callq 0x6350
cmpl $0x1, -0x2c(%rbp)
jne 0x462a
cmpl $0x6, 0x9d8f(%rip)      # 0xe398
jne 0x462a
movl $0x889, %edi            # imm = 0x889
callq 0x7210
movzbl %al, %eax
andl $-0x5, %eax
movl $0x889, %edi            # imm = 0x889
movzbl %al, %esi
callq 0x70f0
jmp 0x4647
movl $0x889, %edi            # imm = 0x889
callq 0x7210
movzbl %al, %eax
orl $0x4, %eax
movl $0x889, %edi            # imm = 0x889
movzbl %al, %esi
callq 0x70f0
movsbl -0x2d(%rbp), %edi
callq 0x73f0
movl 0x40(%rbp), %eax
movl %eax, 0x9c07(%rip)      # 0xe260
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax,%rax)