コード例 #1
0
ファイル: cc1101.cpp プロジェクト: Kreyl/nute
void cc1101_t::Init() {
    // ==== GPIO ====
    PinSetupOut      (GPIOA, CC_CS,   omPushPull, pudNone);
    PinSetupAlterFunc(GPIOA, CC_SCK,  omPushPull, pudNone, AF5);
    PinSetupAlterFunc(GPIOA, CC_MISO, omPushPull, pudNone, AF5);
    PinSetupAlterFunc(GPIOA, CC_MOSI, omPushPull, pudNone, AF5);
    PinSetupIn       (GPIOA, CC_GDO0, pudNone);
    PinSetupIn       (GPIOA, CC_GDO2, pudNone);
    CsHi();

    // ==== SPI ====    MSB first, master, SCK idle low, Baudrate=f/2
    rccEnableSPI1(FALSE);
    // NoCRC, FullDuplex, 8bit, MSB, Baudrate, Master, ClkLowIdle(CPOL=0),
    // FirstEdge(CPHA=0), NSS software controlled and is 1
    CC_SPI->CR1 = SPI_CR1_SSM | SPI_CR1_SSI | SPI_CR1_MSTR;
    CC_SPI->CR2 = 0;
    CC_SPI->I2SCFGR &= ~((uint16_t)SPI_I2SCFGR_I2SMOD);
    CC_SPI->CR1 |= SPI_CR1_SPE; // Enable SPI

    // ==== Init CC ====
    CReset();
    FlushRxFIFO();
    RfConfig();

    // ==== IRQ ====
    rccEnableAPB2(RCC_APB2ENR_SYSCFGEN, FALSE); // Enable sys cfg controller
    SYSCFG->EXTICR[1] &= 0xFFFFFFF0;    // EXTI4 is connected to PortA
    // Configure EXTI line
    EXTI->IMR  |=  GPIO0_IRQ_MASK;      // Interrupt mode enabled
    EXTI->EMR  &= ~GPIO0_IRQ_MASK;      // Event mode disabled
    EXTI->RTSR &= ~GPIO0_IRQ_MASK;      // Rising trigger disabled
    EXTI->FTSR |=  GPIO0_IRQ_MASK;      // Falling trigger enabled
    EXTI->PR    =  GPIO0_IRQ_MASK;      // Clean irq flag
    nvicEnableVector(EXTI4_IRQn, CORTEX_PRIORITY_MASK(STM32_EXT_EXTI4_IRQ_PRIORITY));
}
コード例 #2
0
ファイル: cc1101.cpp プロジェクト: Kreyl/PythonsHead
uint8_t cc1101_t::Init() {
    // ==== GPIO ====
    PinSetupOut      (CC_GPIO, CC_CS,   omPushPull);
    PinSetupAlterFunc(CC_GPIO, CC_SCK,  omPushPull, pudNone, CC_SPI_AF);
    PinSetupAlterFunc(CC_GPIO, CC_MISO, omPushPull, pudNone, CC_SPI_AF);
    PinSetupAlterFunc(CC_GPIO, CC_MOSI, omPushPull, pudNone, CC_SPI_AF);
    IGdo0.Init(ttFalling);
    //PinSetupAnalog   (CC_GPIO, CC_GDO2);    // GDO2 not used
    CsHi();
    // ==== SPI ====
    // MSB first, master, ClkLowIdle, FirstEdge, Baudrate no more than 6.5MHz
    ISpi.Setup(boMSB, cpolIdleLow, cphaFirstEdge, sbFdiv16);
    ISpi.Enable();
    // ==== Init CC ====
    if(Reset() != OK) {
        ISpi.Disable();
        Uart.Printf("\rCC Rst Fail");
        return FAILURE;
    }
    // Check if success
    WriteRegister(CC_PKTLEN, 7);
    uint8_t Rpl = ReadRegister(CC_PKTLEN);
    if(Rpl != 7) {
        ISpi.Disable();
        Uart.Printf("\rCC R/W Fail; rpl=%u", Rpl);
        return FAILURE;
    }
    // Proceed with init
    FlushRxFIFO();
    RfConfig();
    IGdo0.EnableIrq(IRQ_PRIO_HIGH);
    return OK;
}
コード例 #3
0
ファイル: cc1101.cpp プロジェクト: Kreyl/nute
void CC_t::Init(void) {
    // ******** Hardware init section *******
    // ==== GPIO init ====
    klGpioSetupByMsk(CC_GPIO, CC_CS, GPIO_Mode_Out_PP);             // Configure CC_CS as Push-Pull output
    klGpioSetupByMsk(CC_GPIO, CC_SCLK | CC_MOSI, GPIO_Mode_AF_PP);  // Configure MOSI & SCK as Alternate Function Push Pull
    klGpioSetupByMsk(CC_GPIO, CC_MISO, GPIO_Mode_IN_FLOATING);      // Configure MISO as Input Floating
    CS_Hi();

    // ==== SPI init ====    MSB first, master, SCK idle low
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
    SPI_InitTypeDef SPI_InitStructure;
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode      = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize  = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL      = SPI_CPOL_Low;
    SPI_InitStructure.SPI_CPHA      = SPI_CPHA_1Edge;
    SPI_InitStructure.SPI_NSS       = SPI_NSS_Soft;
    SPI_InitStructure.SPI_FirstBit  = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
    SPI_Init(SPI1, &SPI_InitStructure);
    SPI_Cmd(SPI1, ENABLE);
    // ******* Firmware init section *******
    Reset();
    FlushRxFIFO();
    RfConfig();

    // ==== IRQ ====
    IrqPin.Init(CC_GPIO, CC_GDO0_N, GPIO_Mode_IPD);
    IrqPin.IrqSetup(EXTI_Trigger_Falling);
    IrqPin.IrqEnable();
}
コード例 #4
0
ファイル: main.cpp プロジェクト: Kreyl/nute
void CC_t::Task(void) {
    if (IsShutdown) return;
    // Do with CC what needed
    GetState();
    switch (State) {
        case CC_STB_RX_OVF:
            klPrintf("RX ovf\r");
            FlushRxFIFO();
            break;
        case CC_STB_TX_UNDF:
            klPrintf("TX undf\r");
            FlushTxFIFO();
            break;

        case CC_STB_IDLE:
            if (SearchState == IsWaiting) {
                EnterRX();
            }
            else {
                //klPrintf("TX\r");
                // Prepare packet to send
                TX_Pkt.To = 207;
                WriteTX();
                EnterTX();
                //klPrintf("TX\r");
            }
            break;

        default: // Just get out in other cases
            //klPrintf("Other: %X\r", State);
            break;
    } //Switch
}
コード例 #5
0
ファイル: dwhcidevice.cpp プロジェクト: aniket-deole/circle
boolean CDWHCIDevice::InitHost (void)
{
	// Restart the PHY clock
	CDWHCIRegister Power (ARM_USB_POWER, 0);
	Power.Write ();

	CDWHCIRegister HostConfig (DWHCI_HOST_CFG);
	HostConfig.Read ();
	HostConfig.And (~DWHCI_HOST_CFG_FSLS_PCLK_SEL__MASK);

	CDWHCIRegister HWConfig2 (DWHCI_CORE_HW_CFG2);
	CDWHCIRegister USBConfig (DWHCI_CORE_USB_CFG);
	if (   DWHCI_CORE_HW_CFG2_HS_PHY_TYPE (HWConfig2.Read ()) == DWHCI_CORE_HW_CFG2_HS_PHY_TYPE_ULPI
	    && DWHCI_CORE_HW_CFG2_FS_PHY_TYPE (HWConfig2.Get ()) == DWHCI_CORE_HW_CFG2_FS_PHY_TYPE_DEDICATED
	    && (USBConfig.Read () & DWHCI_CORE_USB_CFG_ULPI_FSLS))
	{
		HostConfig.Or (DWHCI_HOST_CFG_FSLS_PCLK_SEL_48_MHZ);
	}
	else
	{
		HostConfig.Or (DWHCI_HOST_CFG_FSLS_PCLK_SEL_30_60_MHZ);
	}

	HostConfig.Write ();

#ifdef DWC_CFG_DYNAMIC_FIFO
	CDWHCIRegister RxFIFOSize (DWHCI_CORE_RX_FIFO_SIZ, DWC_CFG_HOST_RX_FIFO_SIZE);
	RxFIFOSize.Write ();
	
	CDWHCIRegister NonPeriodicTxFIFOSize (DWHCI_CORE_NPER_TX_FIFO_SIZ, 0);
	NonPeriodicTxFIFOSize.Or (DWC_CFG_HOST_RX_FIFO_SIZE);
	NonPeriodicTxFIFOSize.Or (DWC_CFG_HOST_NPER_TX_FIFO_SIZE << 16);
	NonPeriodicTxFIFOSize.Write ();
	
	CDWHCIRegister HostPeriodicTxFIFOSize (DWHCI_CORE_HOST_PER_TX_FIFO_SIZ, 0);
	HostPeriodicTxFIFOSize.Or (DWC_CFG_HOST_RX_FIFO_SIZE + DWC_CFG_HOST_NPER_TX_FIFO_SIZE);
	HostPeriodicTxFIFOSize.Or (DWC_CFG_HOST_PER_TX_FIFO_SIZE << 16);
	HostPeriodicTxFIFOSize.Write ();
#endif

	FlushTxFIFO (0x10);	 	// Flush all TX FIFOs
	FlushRxFIFO ();

	CDWHCIRegister HostPort (DWHCI_HOST_PORT);
	HostPort.Read ();
	HostPort.And (~DWHCI_HOST_PORT_DEFAULT_MASK);
	if (!(HostPort.Get () & DWHCI_HOST_PORT_POWER))
	{
		HostPort.Or (DWHCI_HOST_PORT_POWER);
		HostPort.Write ();
	}
	
	EnableHostInterrupts ();

	return TRUE;
}
コード例 #6
0
ファイル: cc1101.cpp プロジェクト: Kreyl/nute
void CC_t::IRQHandler() {
    if (Aim == caTx) {
        Aim = caIdle;
        TxEndHandler();
    }
    else { // Was receiving
        if (ReadRX((uint8_t*)&PktRx)) NewPktHandler();
        FlushRxFIFO();
        // Do not reenter RX here to allow safely enter TX right after RX
    }
}
コード例 #7
0
ファイル: cc1101.cpp プロジェクト: Kreyl/nute
// ========================== Implementation ===================================
void CC_t::Task(void) {
    // Do with CC what needed
    GetState();
    switch (State){
        case CC_STB_RX_OVF:
            FlushRxFIFO();
            klPrintf("RX_OVF");
            break;
        case CC_STB_TX_UNDF:
            FlushTxFIFO();
            klPrintf("\rTX_OVF");
            break;

        case CC_STB_IDLE:
            //Uart.PrintString("\rIDLE");
            //EnterRX();
            if (Delay.Elapsed(&Timer, 100)) {
                klPrintf("TX\r");
                // Prepare packet to send
                TX_Pkt.PktID++;
                WriteTX();
                //CC.EnterTXAndWaitToComplete();
                EnterTX();
            }
            break;

        case CC_STB_RX:
            //Uart.PrintString("\rRX");
//            if (GDO0_IsHi()) GDO0_WasHi = true;
//            // Check if GDO0 has fallen
//            else if (GDO0_WasHi) {
//                //UART_PrintString("\rIRQ\r");
//                GDO0_WasHi = false;
//                FifoSize = ReadRegister(CC_RXBYTES); // Get number of bytes in FIFO
//                if (FifoSize != 0) {
//                    ReadRX(RX_PktArray, (CC_PKT_LEN+2));    // Read two extra bytes of RSSI & LQI
//                    EVENT_NewPacket();
//                } // if size>0
//            } // if falling edge
            break;

        case CC_STB_TX:
            //UART_PrintString("\rTX");
            break;

        default: // Just get out in other cases
            //Uart.PrintString("\rOther: ");
            //Uart.PrintUint(CC.State);
            break;
    }//Switch
}
コード例 #8
0
ファイル: cc1101.cpp プロジェクト: Kreyl/PythonsHead
// Enter RX mode and wait reception for Timeout_st.
uint8_t cc1101_t::Receive_st(systime_t Timeout_st, void *Ptr, int8_t *PRssi) {
    FlushRxFIFO();
    chSysLock();
    EnterRX();
    msg_t Rslt = chThdSuspendTimeoutS(&ThdRef, Timeout_st);    // Wait IRQ
    chSysUnlock();  // Will be here when IRQ will fire, or timeout occur - with appropriate message

    if(Rslt == MSG_TIMEOUT) {   // Nothing received, timeout occured
        EnterIdle();            // Get out of RX mode
        return TIMEOUT;
    }
    else return ReadFIFO(Ptr, PRssi);
    return OK;
}
コード例 #9
0
ファイル: cc1101.cpp プロジェクト: Kreyl/nute
// Enter RX mode and wait reception for Timeout_ms.
uint8_t cc1101_t::ReceiveSync(uint32_t Timeout_ms, void *Ptr, int8_t *PRssi) {
    FlushRxFIFO();
    chSysLock();
    PWaitingThread = chThdSelf();
    EnterRX();
    msg_t Rslt = chSchGoSleepTimeoutS(THD_STATE_SUSPENDED, MS2ST(Timeout_ms));
    chSysUnlock();  // Will be here when IRQ will fire, or timeout occur - with appropriate message

    if(Rslt == RDY_TIMEOUT) {   // Nothing received, timeout occured
        EnterIdle();            // Get out of RX mode
        return TIMEOUT;
    }
    else return ReadFIFO(Ptr, PRssi);
}
コード例 #10
0
ファイル: cc1101.cpp プロジェクト: Kreyl/nute
/*
 * Enter RX mode and wait reception for Timeout_ms.
 */
uint8_t cc1101_t::ReceiveSync(uint32_t Timeout_ms, rPkt_t *pPkt) {
    FlushRxFIFO();
    EnterRX();  // After that, some time will be wasted to recalibrate
    chSysLock();
    PWaitingThread = chThdSelf();
    msg_t Rslt = chSchGoSleepTimeoutS(THD_STATE_SUSPENDED, MS2ST(Timeout_ms));
    chSysUnlock();  // Will be here when IRQ will fire, or timeout occur - with appropriate message

    if(Rslt == RDY_TIMEOUT) {   // Nothing received, timeout occured
        EnterIdle();            // Get out of RX mode
        return TIMEOUT;
    }
    // IRQ occured: something received, or CRC error
    else return ReadFIFO(pPkt);
}
コード例 #11
0
ファイル: main.cpp プロジェクト: Kreyl/nute
void CC_t::IRQHandler() {
    if (SearchState == IsWaiting) {
        // Will be here if packet received successfully or in case of wrong address
        if (ReadRX()) { // Proceed if read was successful
            // Check address
            if(RX_Pkt.To == ID) {   // This packet is ours
                //klPrintf("From: %u; RSSI: %u\r", RX_Pkt.From, RX_Pkt.RSSI);
                SearchState = IsReplying;
                PktCounter=0;
            }
        } // if read
        FlushRxFIFO();
    }
    else {  // Packet transmitted
        if(++PktCounter == PKTS_TO_REPLY) SearchState = IsWaiting;
    }
}
コード例 #12
0
ファイル: cc1101.cpp プロジェクト: Kreyl/nute
void CC_t::Init(void) {
    // ******** Hardware init section *******
    // SPI clock init
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
    // ==== GPIO init ====
    klGpio::SetupByMsk(GPIOB, CC_CS, GPIO_Mode_Out_PP);             // Configure CC_CS as Push-Pull output
    klGpio::SetupByMsk(GPIOB, CC_SCLK | CC_MOSI, GPIO_Mode_AF_PP);  // Configure MOSI & SCK as Alternate Function Push Pull
    klGpio::SetupByMsk(GPIOB, CC_MISO, GPIO_Mode_IN_FLOATING);      // Configure MISO as Input Floating
    klGpio::SetupByMsk(GPIOB, CC_GDO0, GPIO_Mode_IPU);              // Configure CC_GDO as Input Pull-up
    this->CS_Hi();
    // ==== IRQ ====
#ifndef STM32F10X_LD_VL
    // Enable SYSCFG clock
    //RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
    // Connect EXTI0 Line to PA3 pin
    //SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource3);
#else
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource3);
#endif
    // Configure EXTI3 line
//    EXTI_InitTypeDef   EXTI_InitStructure;
//    EXTI_InitStructure.EXTI_Line = EXTI_Line3;
//    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
//    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
//    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
//    EXTI_Init(&EXTI_InitStructure);
    // ==== SPI init ====    MSB first, master, SCK idle low

    SPI_InitTypeDef SPI_InitStructure;
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode      = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize  = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL      = SPI_CPOL_Low;
    SPI_InitStructure.SPI_CPHA      = SPI_CPHA_1Edge;
    SPI_InitStructure.SPI_NSS       = SPI_NSS_Soft;
    SPI_InitStructure.SPI_FirstBit  = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
    SPI_Init(SPI2, &SPI_InitStructure);
    SPI_Cmd(SPI2, ENABLE);
    // ******* Firmware init section *******
    Reset();
    FlushRxFIFO();
    RfConfig();
}
コード例 #13
0
ファイル: cc1101.cpp プロジェクト: Kreyl/nute
uint8_t cc1101_t::ReadFifo(uint8_t *PBuf, uint8_t Length) {
    uint8_t b, Cnt=0;
    Cnt = ReadRegister(CC_RXBYTES);
    b = ReadRegister(CC_PKTSTATUS);
    //Uart.Printf("Sz: %X; st: %X\r", Cnt, b);

    if(b & 0x80) {
        CsLo();                                             // Start transmission
        //BusyWait();                                       // Wait for chip to become ready
        ReadWriteByte(CC_FIFO|CC_READ_FLAG|CC_BURST_FLAG);  // Address with read & burst flags
        for (uint8_t i=0; i<Length; i++) {                  // Read bytes
            b = ReadWriteByte(0);
            *PBuf++ = b;
            //Uart.Printf(" %X", b);
        }
        CsHi();    // End transmission
    }
    else Cnt = 0;   // Signal that nothing was read
    FlushRxFIFO();
    return 1;
}
コード例 #14
0
ファイル: cc1101.cpp プロジェクト: Kreyl/nute
void cc1101_t::Init() {
    // ==== GPIO ====
    PinSetupOut      (CC_GPIO, CC_CS,   omPushPull, pudNone);
    PinSetupAlterFunc(CC_GPIO, CC_SCK,  omPushPull, pudNone, AF5);
    PinSetupAlterFunc(CC_GPIO, CC_MISO, omPushPull, pudNone, AF5);
    PinSetupAlterFunc(CC_GPIO, CC_MOSI, omPushPull, pudNone, AF5);
    PinSetupIn       (CC_GPIO, CC_GDO0, pudNone);
    PinSetupIn       (CC_GPIO, CC_GDO2, pudNone);
    CsHi();
    // ==== SPI ====    MSB first, master, ClkLowIdle, FirstEdge, Baudrate=f/2
    ISpi.Setup(CC_SPI, boMSB, cpolIdleLow, cphaFirstEdge, sbFdiv2);
    ISpi.Enable();
    // ==== Init CC ====
    CReset();
    FlushRxFIFO();
    RfConfig();
    PWaitingThread = nullptr;
    // ==== IRQ ====
    IGdo0.Setup(CC_GPIO, CC_GDO0, ttFalling);
    IGdo0.EnableIrq(IRQ_PRIO_HIGH);
}
コード例 #15
0
ファイル: cc1101.cpp プロジェクト: Kreyl/nute
// ========================== Implementation ===================================
void CC_t::Task(void) {
//    static uint32_t Tmr;
//    if(!Delay.Elapsed(&Tmr, 99)) return;
    GetState();
    switch (IState) {
        case CC_STB_RX_OVF:
            FlushRxFIFO();
            Uart.Printf("RX ovf\r");
            break;
        case CC_STB_TX_UNDF:
            FlushTxFIFO();
            Uart.Printf("TX ovf\r");
            break;
        case CC_STB_IDLE:   // Reenter RX if packet was jammed
            if (Aim == caRx) EnterRX();
            break;
        default: // Just get out in other cases
            break;
    } //Switch
    //if ((IState != CC_STB_IDLE) or (IState != CC_STB_RX)) klPrintf("State: %X\r", IState);
}
コード例 #16
0
ファイル: main.cpp プロジェクト: Kreyl/nute
void CC_t::Task(void) {
    // Proceed with state processing
    GetState();
    switch (State) {
        case CC_STB_RX_OVF:
            Uart.Printf("RX ovf\r");
            FlushRxFIFO();
            break;
        case CC_STB_TX_UNDF:
            Uart.Printf("TX undf\r");
            FlushTxFIFO();
            break;

        case CC_STB_IDLE:
            if (SearchState == IsCalling) { // Call alien
                WriteTX();
                //klPrintf("TX: %u\r", TX_Pkt.To);
                EnterTX();
            }
            else {  // Is waiting
                if (Delay.Elapsed(&Timer, RX_WAIT_TIME)) SearchState = IsCalling;
                else EnterRX();
            }
            break;

        case CC_STB_RX:
            if (Delay.Elapsed(&Timer, RX_WAIT_TIME)) {
                SearchState = IsCalling;
                EnterIdle();
                // Note that all was interrogated
                if((TX_Pkt.To == MAX_ADDRESS) and (PktCounter == TRY_COUNT-1)) Signal.AllThemCalled = true;
            }
            break;

        default: // Just get out in other cases
            //klPrintf("Other: %X\r", State);
            break;
    } //Switch
}
コード例 #17
0
ファイル: cc1101.cpp プロジェクト: Kreyl/nute
void cc1101_t::Receive(void) {
    //while(IState != CC_STB_IDLE) EnterIdle();
    //Aim = caRx;
    PinSet(GPIOB, 0);

    EnterRX();  // After that, some time will be wasted to recalibrate
//    while(!GDO2IsHi());
    while(!GDO0IsHi());     // Wait until sync word is sent
    while(GDO0IsHi());      // Wait until transmission completed

    //PinClear(GPIOB, 0);
    FlushRxFIFO();
    //Uart.Printf("1\r");
//    uint8_t b;
////    bool result=false;
////    b = ReadRegister(CC_RXBYTES);
////    Uart.Printf("Sz: %X  ", b);
//    // Get pkt status
//    b = ReadRegister(CC_PKTSTATUS);
////    Uart.Printf("St: %X  ", b);
//    if(b & 0x80) {  // CRC OK
////    if(b) {  // FIFO not empty
//        CsLo();                                            // Start transmission
//        //BusyWait();                                         // Wait for chip to become ready
//        ReadWriteByte(CC_FIFO|CC_READ_FLAG|CC_BURST_FLAG);  // Address with read & burst flags
//        for (uint8_t i=0; i<12+2; i++) {            // Read bytes
//            b = ReadWriteByte(0);
//            //*PArr++ = b;
//      //      Uart.Printf(" %X", b);
//        }
//        CsHi();    // End transmission
////        result = true;
//    }
//    Uart.Printf("\r");
    //FlushRxFIFO();
    //Uart.Printf("2\r");
}
コード例 #18
0
ファイル: main.cpp プロジェクト: Kreyl/nute
void CC_t::IRQHandler() {
    if (SearchState == IsCalling) { // Packet transmitted, enter RX
        if(++PktCounter == TRY_COUNT) {     // Several packets sent
            PktCounter = 0;
            // Increase packet address
            TX_Pkt.To++;
            if (TX_Pkt.To > MAX_ADDRESS) TX_Pkt.To = MIN_ADDRESS;
            // Switch to waiting state
            SearchState = IsWaiting;
            Delay.Reset(&Timer);
        }
    }
    else { // Will be here if packet received successfully or in case of wrong address
        if (ReadRX()) {
            //Uart.Printf("%u\r", RX_Pkt.To);
            // Check address
            if(RX_Pkt.To == CC_ADDRESS) {   // This packet is ours
                //klPrintf("From: %u; RSSI: %u\r", RX_Pkt.From, RX_Pkt.RSSI);
                Signal.Remember(RX_Pkt.From, RX_Pkt.RSSI);
            }
            FlushRxFIFO();
        } // if size>0
    } // if is waiting
}
コード例 #19
0
ファイル: cc1101.cpp プロジェクト: Kreyl/nute
void cc1101_t::Init() {
    // ==== GPIO ====
    PinSetupOut(GPIOA, CC_CS,   omPushPull);
    PinSetupAlterFuncOutput(GPIOA, CC_SCK,  omPushPull);
    PinSetupAlterFuncOutput(GPIOA, CC_MISO, omPushPull);
    PinSetupAlterFuncOutput(GPIOA, CC_MOSI, omPushPull);
    PinSetupIn(GPIOA, CC_GDO0, pudNone);
    PinSetupIn(GPIOA, CC_GDO2, pudNone);
    CsHi();

    // ==== SPI ====    MSB first, master, ClkLowIdle, FirstEdge, Baudrate=f/2
    SpiSetup(CC_SPI, boMSB, cpolIdleLow, cphaFirstEdge, sbFdiv2);
    SpiEnable(CC_SPI);

    // ==== Init CC ====
    CReset();
    FlushRxFIFO();
    RfConfig();
    chEvtInit(&IEvtSrcRx);
    chEvtInit(&IEvtSrcTx);
    State = ccIdle;

    // ==== IRQ ====
    RCC->APB2ENR |= RCC_APB2ENR_AFIOEN; // Enable AFIO clock
    uint8_t tmp = CC_GDO0 / 4;          // Indx of EXTICR register
    uint8_t Shift = (CC_GDO0 & 0x03) * 4;
    AFIO->EXTICR[tmp] &= ~((uint32_t)0b1111 << Shift);    // Clear bits and leave clear to select GPIOA
    tmp = 1<<CC_GDO0;   // IRQ mask
    // Configure EXTI line
    EXTI->IMR  |=  tmp;      // Interrupt mode enabled
    EXTI->EMR  &= ~tmp;      // Event mode disabled
    EXTI->RTSR &= ~tmp;      // Rising trigger disabled
    EXTI->FTSR |=  tmp;      // Falling trigger enabled
    EXTI->PR    =  tmp;      // Clean irq flag
    nvicEnableVector(EXTI4_IRQn, CORTEX_PRIORITY_MASK(IRQ_PRIO_MEDIUM));
}
コード例 #20
0
ファイル: cc1101.cpp プロジェクト: Kreyl/nute
void cc1101_t::ReceiveAsync() {
    PWaitingThread = NULL;
    State = ccReceiving;
    FlushRxFIFO();
    EnterRX();
}
コード例 #21
0
   /* success.                                                          */
int BTPSAPI HCITR_COMOpen(HCI_COMMDriverInformation_t *COMMDriverInformation, HCITR_COMDataCallback_t COMDataCallback, unsigned long CallbackParameter)
{
   int ret_val;

   /* First, make sure that the port is not already open and make sure  */
   /* that valid COMM Driver Information was specified.                 */
   if((!HCITransportOpen) && (COMMDriverInformation) && (COMDataCallback))
   {
      /* Initialize the return value for success.                       */
      ret_val               = TRANSPORT_ID;

      /* Note the COM Callback information.                             */
      _COMDataCallback      = COMDataCallback;
      _COMCallbackParameter = CallbackParameter;

      /* Initialize the UART Context Structure.                         */
      BTPS_MemInitialize(&UartContext, 0, sizeof(UartContext_t));

      UartContext.Base         = HCI_UART_BASE;
      UartContext.IntBase      = HCI_UART_INT;
      UartContext.ID           = 1;
      UartContext.FlowInfo     = UART_CONTEXT_FLAG_FLOW_CONTROL_ENABLED;
      UartContext.XOnLimit     = DEFAULT_XON_LIMIT;
      UartContext.XOffLimit    = DEFAULT_XOFF_LIMIT;
      UartContext.RxBufferSize = DEFAULT_INPUT_BUFFER_SIZE;
      UartContext.RxBytesFree  = DEFAULT_INPUT_BUFFER_SIZE;
      UartContext.TxBufferSize = DEFAULT_OUTPUT_BUFFER_SIZE;
      UartContext.TxBytesFree  = DEFAULT_OUTPUT_BUFFER_SIZE;

      /* Flag that the Rx Thread should not delete itself.              */
      RxThreadDeleted          = FALSE;

      /* Check to see if this is the first time that the port has been  */
      /* opened.                                                        */
      if(!Handle)
      {
         /* Configure the UART module and the GPIO pins used by the     */
         /* UART.                                                       */
         MAP_SysCtlPeripheralEnable(HCI_UART_GPIO_PERIPH);
         MAP_SysCtlPeripheralEnable(HCI_UART_RTS_GPIO_PERIPH);
         MAP_SysCtlPeripheralEnable(HCI_UART_CTS_GPIO_PERIPH);
         MAP_SysCtlPeripheralEnable(HCI_UART_PERIPH);

         MAP_GPIOPinConfigure(HCI_PIN_CONFIGURE_UART_RX);
         MAP_GPIOPinConfigure(HCI_PIN_CONFIGURE_UART_TX);
         MAP_GPIOPinConfigure(HCI_PIN_CONFIGURE_UART_RTS);
         MAP_GPIOPinConfigure(HCI_PIN_CONFIGURE_UART_CTS);

         MAP_GPIOPinTypeUART(HCI_UART_GPIO_BASE, HCI_UART_PIN_RX | HCI_UART_PIN_TX);
         MAP_GPIOPinTypeUART(HCI_UART_RTS_GPIO_BASE, HCI_UART_PIN_RTS);
         MAP_GPIOPinTypeUART(HCI_UART_CTS_GPIO_BASE, HCI_UART_PIN_CTS);

         UARTFlowControlSet(UartContext.Base, UART_FLOWCONTROL_RX | UART_FLOWCONTROL_TX);

         /* Create an Event that will be used to signal that data has   */
         /* arrived.                                                    */
         RxDataEvent = BTPS_CreateEvent(FALSE);
         if(RxDataEvent)
         {
            /* Create a thread that will process the received data.     */
            Handle = BTPS_CreateThread(RxThread, 1600, NULL);
            if(!Handle)
            {
               BTPS_CloseEvent(RxDataEvent);
               ret_val = HCITR_ERROR_UNABLE_TO_OPEN_TRANSPORT;
            }
         }
         else
            ret_val = HCITR_ERROR_UNABLE_TO_OPEN_TRANSPORT;
      }

      /* If there was no error, then continue to setup the port.        */
      if(ret_val != HCITR_ERROR_UNABLE_TO_OPEN_TRANSPORT)
      {
         /* Configure UART Baud Rate and Interrupts.                    */
         MAP_UARTConfigSetExpClk(UartContext.Base, MAP_SysCtlClockGet(), COMMDriverInformation->BaudRate, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE));

         /* SafeRTOS requires RTOS-aware int handlers to be priority    */
         /* value 5 or greater                                          */
         MAP_IntPrioritySet(UartContext.IntBase, 6 << 5);

         MAP_IntEnable(UartContext.IntBase);
         MAP_UARTIntEnable(UartContext.Base, UART_INT_RX | UART_INT_RT);
         UartContext.Flags |= UART_CONTEXT_FLAG_RX_FLOW_ENABLED;

         /* Clear any data that is in the Buffer.                       */
         FlushRxFIFO(UartContext.Base);

         /* Bring the Bluetooth Device out of Reset.                    */
         MAP_GPIOPinWrite(HCI_RESET_BASE, HCI_RESET_PIN, HCI_RESET_PIN);

         /* Check to see if we need to delay after opening the COM Port.*/
         if(COMMDriverInformation->InitializationDelay)
            BTPS_Delay(COMMDriverInformation->InitializationDelay);

         /* Flag that the HCI Transport is open.                        */
         HCITransportOpen = 1;
      }
   }
   else
      ret_val = HCITR_ERROR_UNABLE_TO_OPEN_TRANSPORT;

   return(ret_val);
}
コード例 #22
0
ファイル: cc1101.cpp プロジェクト: Kreyl/nute
void CC_t::IRQHandler() {
    if (ReadRX()) {
        // Do what needed
        FlushRxFIFO();
    } // if size>0
}
コード例 #23
0
   /* negative return value to signify an error.                        */
int BTPSAPI HCITR_COMOpen(HCI_COMMDriverInformation_t *COMMDriverInformation, HCITR_COMDataCallback_t COMDataCallback, unsigned long CallbackParameter)
{
   int ret_val;
   volatile char dummy = 0;

   /* First, make sure that the port is not already open and make sure  */
   /* that valid COMM Driver Information was specified.                 */
   if((!HCITransportOpen) && (COMMDriverInformation) && (COMDataCallback))
   {
      /* Initialize the return value for success.                       */
      ret_val               = TRANSPORT_ID;

      /* Note the COM Callback information.                             */
      _COMDataCallback      = COMDataCallback;
      _COMCallbackParameter = CallbackParameter;

      /* Try to Open the port for Reading/Writing.                      */
      BTPS_MemInitialize(&UartContext, 0, sizeof(UartContext_t));

      UartContext.UartBase     = BT_UART_MODULE_BASE;
      UartContext.ID           = 1;
      UartContext.RxBufferSize = DEFAULT_INPUT_BUFFER_SIZE;
      UartContext.RxBytesFree  = DEFAULT_INPUT_BUFFER_SIZE;
      UartContext.XOffLimit    = XOFF_LIMIT;
      UartContext.XOnLimit     = XON_LIMIT;
      UartContext.TxBufferSize = DEFAULT_OUTPUT_BUFFER_SIZE;
      UartContext.TxBytesFree  = DEFAULT_OUTPUT_BUFFER_SIZE;
      UartContext.SuspendState = hssNormal;

      /* Check to see if this is the first time that the port has been  */
      /* opened.                                                        */
      if(!HCITransportOpen)
      {
         /* Configure the Bluetooth Slow Clock.                         */
         BT_CONFIG_SLOW_CLOCK();

         /* Configure the TXD and RXD pins as UART peripheral pins.     */
         BT_CONFIG_UART_PINS();

         /* configures the RTS and CTS lines.                           */
         BT_CONFIG_RTS_PIN();
         BT_CONFIG_CTS_PIN();

         /* disable Flow through Local RTS line.                        */
         FLOW_OFF();

         /* configure the Device Reset line                             */
         BT_CONFIG_RESET();

         /* Set the Baud rate up.                                       */
         HAL_CommConfigure(UartContext.UartBase, BLUETOOTH_STARTUP_BAUD_RATE, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE));

         /* Disable Tx Flow, later we will check RTS and see if we      */
         /* should enable it, but enable our receive flow.              */
         DISABLE_INTERRUPTS();
         UartContext.Flags &= (~UART_CONTEXT_FLAG_TX_FLOW_ENABLED);
         UartContext.Flags |= UART_CONTEXT_FLAG_FLOW_ENABLED;
         ENABLE_INTERRUPTS();

         /* Bring the Bluetooth Device out of Reset.                    */
         BT_DEVICE_RESET();
         BTPS_Delay(10);
         BT_DEVICE_UNRESET();

         /* Bring CTS Line Low to Indicate that we are ready to receive.*/
         FLOW_ON();

         /* Check to see if we need to enable Tx Flow.                  */
         if(BT_CTS_READ())
         {
            /* CTS is High so we cannot send data at this time. We will */
            /* configure the CTS Interrupt to be Negative Edge Active.  */
            DISABLE_INTERRUPTS();
            UartContext.Flags &= (~UART_CONTEXT_FLAG_TX_FLOW_ENABLED);
            BT_CTS_INT_NEG_EDGE();
            ENABLE_INTERRUPTS();
         }
         else
         {
            /* CTS is low and ergo we may send data to the controller.  */
            /* The CTS interrupt will be set to fire on the Positive    */
            /* Edge.                                                    */
            DISABLE_INTERRUPTS();
            UartContext.Flags |= (UART_CONTEXT_FLAG_TX_FLOW_ENABLED);
            BT_CTS_INT_POS_EDGE();
            ENABLE_INTERRUPTS();
         }

         /* Clear any data that is in the Buffer.                       */
         FlushRxFIFO(UartContext.UartBase);

         /* Enable Receive interrupt.                                   */
         UARTIntEnableReceive(UartContext.UartBase);

         /* Disable Transmit Interrupt.                                  */
         UARTIntDisableTransmit(UartContext.UartBase);

         DISABLE_INTERRUPTS();

         /* Flag that the UART Tx Buffer will need to be primed.        */
         UartContext.Flags &= (~UART_CONTEXT_FLAG_TX_PRIMED);

         /* Enable the transmit functionality.                          */
         UartContext.Flags |= UART_CONTEXT_FLAG_TRANSMIT_ENABLED;

         ENABLE_INTERRUPTS();

         /* Check to see if we need to delay after opening the COM Port.*/
         if(COMMDriverInformation->InitializationDelay)
            BTPS_Delay(COMMDriverInformation->InitializationDelay);

         /* Flag that the HCI Transport is open.                        */
         HCITransportOpen = 1;
      }
   }
   else
      ret_val = HCITR_ERROR_UNABLE_TO_OPEN_TRANSPORT;

   return(ret_val);
}