Ejemplo n.º 1
0
int main(void)
{
	clock_init();
	serial_init(9600);
	nrf_init();

	cio_print("nRF2401 v0.1, TestClient\n\r");

	nrf_configure_esb_rx();
	nrf_dump_regs(&nrf_reg_def);

	static nrf_payload   p;

	int s;

	// set payload size according to the payload size configured for the RX channel
	p.size = 1;

	while (1) {
		s = nrf_receive_blocking(&p);

		cio_printf("Received payload: %x; bytes received: %u\n\r", p.data[0], s);
	}

	return 0;
}
Ejemplo n.º 2
0
void slaveMain() {
    //slave
    int i;
    char offset;
    char status;

    nrf_init();
    delay();

    nrf_rxmode();
    delay();

//    while(1) {
//        LED_GREEN = nrf_recieve(&tx_buf, &rx_buf);
//    }

    nrf_setTxAddr(0);
    nrf_setRxAddr(0, 0);

    rx_buf[0] = 0;
    while(rx_buf[0] != 0x42) {
        nrf_recieve(&tx_buf, &rx_buf);
    }
    
    nrf_setTxAddr(rx_buf[1]);
    nrf_setRxAddr(0, rx_buf[1]);
    while(1) {
        LED_GREEN = nrf_recieve(&tx_buf, &rx_buf);
    }
}
Ejemplo n.º 3
0
int main (void)
{
    sysclk_init();
	pmic_init();
    port_init();
	tc_init();
	wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_32CLK);
	wdt_enable();
	usart_init();
	spi_init();
	
	char str[200];
	uint8_t count ;
	count = sprintf(str,"RESET");
	for (uint8_t i=0;i<count;i++)
	usart_putchar(&USARTE0,str[i]);
	nrf_init(Address);
	sei();
	

	for (uint8_t i=0;i<Max_Robot;i++)
	{
		Robot_D_tmp[2][i].RID=12;
	}

	while (1)
	{
		
	}
}
Ejemplo n.º 4
0
void receiverMain() {
    int i;
    char offset;
    char status;

    //doCycle();
    //doOscillate();

    nrf_init();
    delay();

    nrf_rxmode();
    delay();

    Delay10KTCYx(100);

    offset = 0;
    STATUS_LED = 0;
    while(1) {
        //STATUS_LED =
        nrf_receive(&rx_buf);

        updateBuffer();
        updateLEDs();
    }
}
Ejemplo n.º 5
0
void Init_NRF()
{
    //*********************************************************************
    printf("\n\n\n***********无线模块NRF24L01+测试************");
    while(!nrf_init())                  //初始化NRF24L01+ ,等待初始化成功为止
    {
        printf("\n  NRF与MCU连接失败,请重新检查接线。\n");
    }
      //*********************************************************************  
    set_vector_handler(PORTE_VECTORn ,PORTE_IRQHandler);    			//设置 PORTE 的中断服务函数为 PORTE_VECTORn
    enable_irq(PORTE_IRQn);
}
Ejemplo n.º 6
0
int main(void)
{
	int i;

	clock_init();
	gpio_init();
	serial_init(9600);
	serirq_init();
	nrf_init();
	nrf_configure_sb();

	prx.size 	= PL_SIZE;
	ptx.size 	= PL_SIZE;

	while (1) {
        ptx.data[0] = 0;

        while(!serial_rb_empty(&srx) && ptx.data[0] < (PL_SIZE - 1)) {
			ptx.data[ptx.data[0] + 1] = serial_rb_read(&srx);
			ptx.data[0]++;
        }

        if(ptx.data[0] > 0) {
#ifdef MSP430
			P1OUT |= RXTX_LED;
#else
            gpio_set(GPIOC, TX_LED);
#endif
            // switch to PTX for sending out data ...
            nrf_set_mode_ptx();

            nrf_send_blocking(&ptx);

            nrf_set_mode_prx();
        }

        if(nrf_receive(&prx) != 0 && prx.data[0] > 0) {
            if(!serial_rb_full(&stx)) {
                for(i = 0; i < prx.data[0]; i++) serial_rb_write(&stx, prx.data[i + 1]);
#ifdef MSP430
				P1OUT |= RXTX_LED;
				IE2 |= UCA0TXIE;
#else
             	gpio_set(GPIOC, RX_LED);
               	USART_CR1(USART1) |= USART_CR1_TXEIE;
#endif
            }
    	}
	}

	return 0;
}
NrfErrorType_e NrfInit()
{
	uint8 i = NrfInitRetryTime;
	while (!nrf_init())                  //初始化NRF24L01+ ,等待初始化成功为止
	{
		if (i-- == 0)
		{
			ASSERT(true);
			return Nrf_NotOnLine;
		}
	}
	nrf_msg_init();//初始化消息
	return Nrf_AllGreen;
}
Ejemplo n.º 8
0
Archivo: rf.cpp Proyecto: HclX/MSP430
void Rf_Init()
{
    nrf_init();

    // disable TX_DS and MAX_RT interrupt
    nrf_reg_write(REG_CONFIG, MASK_TX_DS | MASK_MAX_RT | PWR_UP);

    // clear any existing RX/TX payload
    nrf_flush_rx();
    nrf_flush_tx();

    // clear any pending RX/TX interrupt flags
    nrf_reg_write(REG_STATUS, RX_DR| TX_DS | MAX_RT);

    // disable auto-acknowledge feature
    nrf_reg_write(REG_EN_AA, 0);

    // enable only P0 RX channel
    nrf_reg_write(REG_EN_RXADDR, 0x01);

#ifdef __NRF24L01P
    // 250kbps, max RF power
    nrf_reg_write(REG_RF_SETUP, 0x27);
#else
    // 1Mbps, max RF power, LNA gain
    nrf_reg_write(REG_RF_SETUP, 0x07);
#endif

    // RF channel
    nrf_reg_write(REG_RF_CH, g_sysConfig.channel);

    // make sure 5 bytes address length
    nrf_reg_write(REG_SETUP_AW, sizeof(g_sysConfig.rxAddr) - 2);

    // disable retry
    nrf_reg_write(REG_SETUP_RETR, 0);

    // fixed packet size
    nrf_set_rx_payload_length(sizeof(PACKET));

    // initialize rx_addr to be whatever in the system configuration
    nrf_set_rx_addr(g_sysConfig.rxAddr, sizeof(g_sysConfig.rxAddr));

    // standby mode
    nrf_standby();

    // dump registers
    nrf_test_dump_reg();
}
Ejemplo n.º 9
0
void main() {
    char buffer[NRF_PAYLOAD_LEN];

    _nrf_sleep(1000);

    if (!nrf_init()) {
        P0 = _nrf_get_reg(STATUS);
        return;
    }

    while (TRUE) {
        nrf_recv(ADDR_TEST_SLAVE, buffer, -1);
        nrf_send(ADDR_TEST_MASTER, buffer);
    }
}
Ejemplo n.º 10
0
Archivo: main.c Proyecto: jwcxz/ACRIS
int main(void) {
    dbg_init();
    dbg_set(0xA);

    uart_rb_init();
    uart_printf_init();

    nrf_init(rxbuf);
    nrf_set_channel(115);
    nrf_set_power(NRF_CFG_RF_GAIN_M12);
    nrf_enable_pipe(0, tx_addr);

    sei();

    while (1) {
        transmitter_loop();
    }
}
Ejemplo n.º 11
0
Archivo: funk.c Proyecto: Bediko/r0ket
void f_init(void){
    nrf_init();

    struct NRF_CFG config = {
        .channel= BEACON_CHANNEL,
        .txmac= BEACON_MAC,
        .nrmacs=1,
        .mac0=  BEACON_MAC,
        .maclen ="\x10",
    };

    nrf_config_set(&config);
    lcdPrintln("Done.");
};

void f_status(void){
    int dx=0;
    int dy=8;
    uint8_t buf[4];

    buf[0]=C_R_REGISTER | R_CONFIG;
    buf[1]=0;
    buf[2]=0;
    buf[3]=0;
    dx=DoString(0,dy,"S:"); 
    dx=DoCharX(dx,dy,buf[0]);
    dx=DoCharX(dx,dy,buf[1]);
    dx=DoCharX(dx,dy,buf[2]);
    dx=DoCharX(dx,dy,buf[3]);
    dy+=8;
    nrf_cmd_rw_long(buf,2);
    dx=DoString(0,dy,"R:");
    dx=DoCharX(dx,dy,buf[0]);
    dx=DoCharX(dx,dy,buf[1]);
    dx=DoCharX(dx,dy,buf[2]);
    dx=DoCharX(dx,dy,buf[3]);
    dy+=8;

    int status=nrf_cmd_status(C_NOP);
    dx=DoString(0,dy,"St:"); DoCharX(dx,dy,status);dy+=8;
};
Ejemplo n.º 12
0
//========== main =====================================================================
void main(void) {
    nrf_init();

    TRIS_CE = output;
    TRIS_CSN = output;
    TRIS_IRQ = input;
    TRIS_SCK = output;
    TRIS_MISO = input;
    TRIS_MOSI = output;

    OSCCONbits.IRCF = 0b111; //sets internal osc to 111=16mhz, 110=8mhz
    OSCCONbits.SCS = 0b00;
    OSCTUNEbits.PLLEN = 0b1; //1=pllx4 enabled

    //set up timer
    T0CONbits.TMR0ON = 1; //enable timer 0
    T0CONbits.T0CS = 0; //select clock (0=internal,1=t0pin)
    T0CONbits.PSA = 1; //disable's prescaler (1=disable, 0=enable)
    T0CONbits.T08BIT = 1; //set mode (1=8bit mode, 0=16bit mode)
    T0CONbits.T0SE = 1; //edge select (1=falling edge, 0=rising edge)
    T0CONbits.T0PS = 0b000; //configure prescaler 000=1:2

    //Set up timer0 interrupts
    INTCONbits.TMR0IE = 1;
    INTCONbits.TMR0IF = 0;
    INTCONbits.PEIE = 1;

    //Misc port config
    TRIS_LED = output;

    initRX();
    Delay10KTCYx(20);

    while(1) {
        nrf_Recieve(&rx_buf);
        LED = rx_buf[0];
        Delay10KTCYx(20);
    }

}
Ejemplo n.º 13
0
int main(void)
{
	clock_init();
	serial_init(9600);
	exti_init();
	nrf_init();
	cio_print("nRF2401 v0.1 - TestClient ESBPL-exti\n\r");

	nrf_configure_esbpl_rx();

	// setup payload
	prx.size = 1;		// receive payload size 1Byte
	ptx.size = 1;		// send payload size 1Byte
	ptx.data[0] = 0;	// set payload to 0
	
	// Nothing to do here since ISR does all the work
	while (1) {
		__asm__("nop");
	}

	return 0;
}
Ejemplo n.º 14
0
void main_bridge(void)
{
    GLOBAL(daytrig)=10;
    GLOBAL(lcdbacklight)=10;
    GLOBAL(privacy) = 3;
    char input[64];
    char led1=0;
    char led2=0;

    usbCDCInit();
    delayms(500);
    nrf_init();
    nrf_config_set(&config);
    
    nrf_rcv_pkt_start(R_CONFIG_EN_CRC);
    while(1){
        int l, i, status;
        CDC_OutBufAvailChar (&l);
        if(l>0){
            gpioSetValue (RB_LED0, led1);led1=1-led1;
            CDC_RdOutBuf (input, &l);
            for(i=0; i<l; i++){
                uint8_t cmd = serialmsg_put(input[i]);
                if( cmd != SERIAL_NONE ){
                    switch( cmd ){
                        case '1':
                            // can we loose packets here?
                            nrf_rcv_pkt_end();
                            status=snd_pkt_no_crc(serialmsg_len, serialmsg_message);
                            //status=nrf_snd_pkt_crc(serialmsg_len, serialmsg_message);
                            nrf_rcv_pkt_start(R_CONFIG_EN_CRC);
                        break;
                        case '3':
                            memcpy(config.txmac, serialmsg_message, 5);
                            nrf_write_long(C_W_REGISTER|R_TX_ADDR,5,config.txmac);
                        break;
                        case '4':
                            memcpy(config.mac0, serialmsg_message, 5);
                            nrf_write_long(C_W_REGISTER|R_RX_ADDR_P0,5,config.mac0);
                            nrf_write_reg(R_EN_RXADDR,1);
                        break;
                        case '5':
                            config.channel=serialmsg_message[0];
                            nrf_set_channel(config.channel);
                            nrf_cmd(C_FLUSH_RX);
                        break;
                        case '6':
                            config.maclen[0]=serialmsg_message[0];
                            nrf_write_reg(R_RX_PW_P0,config.maclen[0]);
                        break;
                        case '7':
                            puts("\\7");
                            char s[sizeof(uint32_t)+1];
                            *((uint32_t*)s) =GetUUID32();
                            s[sizeof(uint32_t)]=0;
                            puts(s);
                            puts("\\0");
                        break;
                        case '8': /* set mac width */
                            nrf_write_reg(R_SETUP_AW,serialmsg_message[0]);
                        break;
                        case '9': // Dis/Enable CRC
                            nrf_write_reg(R_CONFIG, R_CONFIG_PRIM_RX|R_CONFIG_PWR_UP|
                                    ((serialmsg_message[0]&1)?R_CONFIG_EN_CRC :0)|
                                    ((serialmsg_message[0]&2)?R_CONFIG_CRCO :0)
                                    
                                    );
                            /* maybe add enhanced shockburst stuff here */
                            nrf_cmd(C_FLUSH_RX);
                            nrf_write_reg(R_STATUS,0);
                        break;

                    };
                    puts("\\2\\0");
                }
            }
        }
        int len;
        uint8_t buf[32];
        len=nrf_rcv_pkt_poll(sizeof(buf),buf);
        if( len > 0 ){
            gpioSetValue (RB_LED2, led2);led2=1-led2;
            puts("\\1");
            dump_encoded(len, buf);
            puts("\\0");
        }
    }
}
Ejemplo n.º 15
0
int main()
{  
  u8 data[ 4 ];
  int res;

  leds_init();
  btn_init();
  ind_init();
  uart_init();
  sei();
  ledvm_init();
  i2cee_init();
  pgm_init();
  //stdout = &mystdout;
  nrf_init();
  nrf_set_own_addr( lights_addr );
  nrf_set_mode( NRF_MODE_RX );

  while( 1 )
  {
    // Something to execute?
    if( pgm_crt != -1 )
    {
      res = ledvm_run();
      if( res != LEDVM_ERR_OK )
      {
        if( res == LEDVM_ERR_FINISHED ) // program finished, look for the next one
        {
          if( pgm_cycle )
          {
            res = pgm_get_next_index( pgm_crt, 1 );
            if( res != -1 ) // if found use the other program, otherwise don't change it
              pgm_crt = res;
          }
          ledvm_init();
        }
        else
        {
          ledvm_ll_setrgb( 0, 0, 0 );
          pgm_crt = -1;
        }
      }
    }
      
    // New data via radio?
    if( nrf_get_packet( data, 4, NULL ) == 4 )
    {
      // Got data, interpret it
      if( data[ 0 ] == CMD_START_WRITE )
      {
        pgm_recv_on = 1;
        pgm_recv_slot = data[ 3 ];
        pgm_recv_addr = 0;
        pgm_recv_buf_idx = 0;
        if( pgm_recv_slot == pgm_crt )
          pgm_crt = -1;
      }
      else if( data[ 0 ] == CMD_END_WRITE )
      {
        pgm_recv_on = 0;
        pgm_recv_to_flash();
        pgm_set_program_state( pgm_recv_slot, 1 );
        if( pgm_crt == -1 )
          pgm_crt = pgm_get_first_index();
      }
      else if( data[ 0 ] == CMD_RUN )
      {
        if( pgm_mask & ( 1 << data[ 3 ] ) )
        {
          pgm_crt = data[ 3 ];
          ledvm_init();
        }
      }
      else if( data[ 0 ] == CMD_RAW )
      {
        pgm_crt = -1;
        ledvm_ll_setrgb( data[ 1 ], data[ 2 ], data[ 3 ] );
      }
      else if( data[ 0 ] == CMD_DEL ) 
      {
        pgm_set_program_state( data[ 3 ], 0 );
        if( pgm_crt == data[ 3 ] )
        {
          pgm_crt = pgm_get_first_index();
          ledvm_init();
        }
      }
      else if( pgm_recv_on ) // instruction
      {
        memcpy( pgm_recv_buffer + pgm_recv_buf_idx, data, 4 );
        pgm_recv_buf_idx += 4;
        if( pgm_recv_buf_idx == PGM_EE_PAGE_SIZE )
          pgm_recv_to_flash();
      }
    }

    // Handle buttons
    if( pgm_crt != -1 )
    {
      btn_handler();
      if( btn_is_pressed( BTN_NEXT_PIN ) )
      {
        res = pgm_get_next_index( pgm_crt, 1 );
        if( res != -1 )
          pgm_crt = res;
        ledvm_init();
      }
      else if( btn_is_pressed( BTN_PREV_PIN ) )
      {
        res = pgm_get_next_index( pgm_crt, -1 );
        if( res != -1 )
          pgm_crt = res;
        ledvm_init();
      }
      else if( btn_is_pressed( BTN_REPEAT_PIN ) )
        ind_set( 1 - pgm_cycle );
    }
  }  
}
Ejemplo n.º 16
0
int main(void)
{
    uint8_t buffer[32] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
    /* Unlock the protected registers */
    UNLOCKREG();

    /* Enable the 12MHz oscillator oscillation */
    DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);

    /* Waiting for 12MHz Xtal stable */
    DrvSYS_Delay(5000);

    /* HCLK clock source. 0: external 12MHz. */
    DrvSYS_SelectHCLKSource(0);

    LOCKREG();

    DrvSYS_SetClockDivider(E_SYS_HCLK_DIV, 0); /* HCLK clock frequency = HCLK clock source / (HCLK_N + 1) */

    nrf_init();

    nrf_detect();

    //nrf_rx_mode_no_aa(addr,5,16,40);
    //nrf_rx_mode(addr,5,16,40);
    nrf_rx_mode_dual(addr,5,40);

    nrf_test_reg();

    DrvGPIO_Open(E_GPA, 2, E_IO_OUTPUT);
    DrvGPIO_Open(E_GPA, 3, E_IO_OUTPUT);
    DrvGPIO_Open(E_GPA, 4, E_IO_OUTPUT);
    DrvGPIO_Open(E_GPA, 5, E_IO_OUTPUT);
    {
        uint8_t status = nrf_read_reg(NRF_STATUS);
        nrf_write_reg(NRF_WRITE_REG|NRF_STATUS,status); // clear IRQ flags
        nrf_write_reg(NRF_FLUSH_RX, 0xff);
        nrf_write_reg(NRF_FLUSH_TX, 0xff);
    }

    while(1) {
        uint8_t buffer[32];
        if(tx_done) {
            static uint8_t yy = 0;
            yy++;
            if(yy&1) {
                DrvGPIO_SetBit(E_GPA,2);
            } else {
                DrvGPIO_ClrBit(E_GPA,2);
            }
            if(tx_done == 1) {
            } else {
            }
            buffer[0] = tx_done;
            tx_done = 0;
        }
        if(rx_done) {
            static uint8_t xx = 0;
            rx_done = 0;
            xx++;
            if(xx & 1)
                DrvGPIO_SetBit(E_GPA,5);
            else
                DrvGPIO_ClrBit(E_GPA,5);

            //nrf_ack_packet(0,buffer, (xx&15) + 1);
            nrf_ack_packet(0,rx_buffer, rx_len);
        }
    }

    while(1) {
        static uint8_t cnt = 0;
        if(cnt&1) {
            DrvGPIO_SetBit(E_GPA,2);
        } else {
            DrvGPIO_ClrBit(E_GPA,2);
        }
        DrvSYS_Delay(50000*2);
        cnt++;
        //nrf_tx_packet(buffer, 16);
        //buffer[0]++;

        if(nrf_rx_packet(buffer,16) == NRF_RX_OK)
        {
            static uint8_t xx = 0;
            xx++;
            if(xx & 1)
                DrvGPIO_SetBit(E_GPA,5);
            else
                DrvGPIO_ClrBit(E_GPA,5);
        }
    }
    return 0;
}
Ejemplo n.º 17
0
void testInit(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    uint8_t i;

    struct __gpio_config_t {
    GPIO_TypeDef *gpio;
    uint16_t pin;
    GPIOMode_TypeDef mode;
    }
    gpio_cfg[] = {
        {LED0_GPIO, LED0_PIN, GPIO_Mode_Out_PP},        // PB3 (LED)
        {LED1_GPIO, LED1_PIN, GPIO_Mode_Out_PP},        // PB4 (LED)
    };

    uint8_t gpio_count = sizeof(gpio_cfg) / sizeof(gpio_cfg[0]);

    // Turn on clocks for stuff we use
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
    RCC_ClearFlag();

    // Make all GPIO in by default to save power and reduce noise
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    // Turn off JTAG port 'cause we're using the GPIO for leds
    GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);

    // Configure gpio
    for (i = 0; i < gpio_count; i++) {
        GPIO_InitStructure.GPIO_Pin = gpio_cfg[i].pin;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = gpio_cfg[i].mode;

        GPIO_Init(gpio_cfg[i].gpio, &GPIO_InitStructure);
    }

    // Init cycle counter
    cycleCounterInit();

    // SysTick
    SysTick_Config(SystemCoreClock / 1000);

    LED0_OFF;
    LED1_OFF;

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);     // 2 bits for pre-emption priority, 2 bits for subpriority
    
    checkFirstTime(true,true);
    initMixer();
    
    pwmOutputConfig.motorPwmRate = 10*1000;
    pwmOutputConfig.noEsc = true;
    pwmOutputInit(&pwmOutputConfig);
    
    
    i2cInit(SENSOR_I2C);
    initGyro();
    initAccel();
    initMag();
    nrf_init();
    nrf_detect();
}
Ejemplo n.º 18
0
void masterMain() {
    //master
    short i;
    short l;
    short nextSlot;
    int clients[MAX_CLIENTS];
    int clientInfo[MAX_CLIENTS];

    nrf_init();
    delay();

    nrf_txmode();
    delay();
    
    nextSlot = 0;
    for (i=0; i<MAX_CLIENTS; i++) {
        clients[i] = 0;
    }

    sendLiteralBytes("\nLoop Start\n");
    LED_RED = 0;
    LED_GREEN = 0;
//    while(1) {
//        LED_RED = !LED_RED;
//        LED_GREEN = nrf_send(&tx_buf,&rx_buf);
//        delay();
//    }
    while(1) {
        LED_RED = !LED_RED;
        delay();
        sendIntArray(&clients,MAX_CLIENTS);
        sendLiteralBytes("\n");
        
        nrf_setTxAddr(0); //master channel
        nrf_setRxAddr(0,0); //master channel
        tx_buf[0] = 0x42;
        tx_buf[1] = nextSlot+1;
        //sendLiteralBytes("Attempt Send\n");
        //displayStatus(nrf_getStatus());
        if (nrf_send(&tx_buf,&rx_buf)) {
            sendLiteralBytes("\nClient Connected!\n");

            clients[nextSlot] = 1;

            //find the next available slot
            nextSlot = -1;
            for (i=0; i<MAX_CLIENTS; i++) {
                if (clients[i] == 0) {
                    nextSlot = i;
                    break;
                }
            }
        }

        for (i=0; i<MAX_CLIENTS; i++) {
            if (clients[i] != 0) {
                nrf_setTxAddr(i+1); //slave channel
                nrf_setRxAddr(0,i+1); //slave channel
                if (nrf_send(&tx_buf,&rx_buf)) {
                    clients[i] = 1;
                    clientInfo[i] = 1;
                } else {
                    clients[i]++;
                    if (clients[i] > 3) {
                        sendLiteralBytes("Client Disconnected!\n");
                        clients[i] = 0;
                        clientInfo[i] = 0;
                    }
                    for (l=0; l<MAX_CLIENTS; l++) {
                        if (clients[l] == 0) {
                            nextSlot = l;
                            break;
                        }
                    }
                }
            }
        }
    }
}
Ejemplo n.º 19
0
void init_beacon(void){
    nrf_init();
    openbeaconSetup();
}
Ejemplo n.º 20
0
int main(void)
{
	// USB BEGIN //
	SetupHardware();
	/* Create a regular character stream for the interface so that it can be used with the stdio.h functions */
	CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream);
	
	TCCR0B = (1<<CS02)|(1<<CS00); // Set up timer to handle USB stuff.
	TIMSK0 = (1<<TOIE0);
	
	sei();
	// USB END //
	
	
	while(rxByte != 0x20)
	{
		fputs(".", &USBSerialStream);
		delay_ms(100);
	}
	rxByte = 0;
	nrf_init();

	delay_ms(500);
	
	nrf_spillRegisters();
	
	uint16_t number = 0;
	
	fputs("Last transmission: ",&USBSerialStream);
	itoa(number, buffer, 10);
	fputs(buffer,&USBSerialStream);
			
	
	for (;;)
	{	
		/*while(!rxByte)
		{
			fputs(".", &USBSerialStream);
			delay_ms(100);
		}*/
		number++;
		nrf_tx((uint16_t)number);
		
		fputs("\rLast transmission: ",&USBSerialStream);
		itoa(number, buffer, 10);
		fputs(buffer,&USBSerialStream);
		
		//rxByte = 0;
		
		GET_STATUS;
		if(nrf_last_status & (1<<MAX_RT))
		{
			fputs("\r\nPacket transmission failed. Clearing MAX_RT...\r\n",&USBSerialStream);
			delay_us(1);
			SS_DOWN;
			SPI_WRITE(W_REGISTER | NRG_STATUS);
			SPI_WRITE(1<<MAX_RT);
			SS_UP;
		}
		else
		{
			
		}
		
		delay_ms(500);
		
		//nrf_spillRegisters();
	}
}
Ejemplo n.º 21
0
////////////////////////////////////////////////////////////////////////////////
////                                                                        ////
////                            Sender Code                                 ////
////                                                                        ////
////////////////////////////////////////////////////////////////////////////////
void senderMain() {
    int fixweirdbehavior;
    unsigned char status;
    short i;
    char mode;
    short offset;

    //OpenADC(ADC_FOSC_64 & ADC_RIGHT_JUST & ADC_20_TAD, ADC_CH0 & ADC_INT_OFF, 0b0000);

    nrf_init();
    delay();

    nrf_txmode();
    delay();

    PIE1bits.RC1IE = SET; //enables peripheral interrupts for serial

    INTCONbits.TMR0IE = 0;
    INTCONbits.TMR0IF = 0;
    INTCONbits.PEIE = 1;
    INTCONbits.GIE = 1;

    LED_RED = 0;
    LED_GREEN = 0;
//    mode = 0;
//    while(1) {
//        //while (!PIR1bits.RC1IF);
//        //LED_RED = 1;
//        //readSerialIntoBuffer(led_buffer);
//        //LED_RED = 0;
//        //delay();
//
//        mode = !mode;
//        led_buffer[3] = mode ? 25 : 0;
//        led_buffer[4] = mode ? 25 : 0;
//        led_buffer[5] = mode ? 25 : 0;
//        sendStrip();
//        //STATUS_LED = 0;
//    }

    mode = 0;

    offset = 0;
    //LED_GREEN = 0;
    while(1) {
        //LED_RED++;

        offset ++;
        if (offset >= 125) offset = 0;

        //value = readPotentiometer();
        //value = value >> 4;
        //value = value >> 1;
        //if (value > 124) value = 124;

        //offset = value;
        //if (value > offset) offset++;
        //if (value < offset) offset--;

//        if (mode) {
//            clearStrip(0,0,0);
//            setLED(offset,10,10,10);
        //} else {
        for (i = 0; i<125; i++) {
            led_buffer[3*i] =   10*((i+offset) % 3 == 0);
            led_buffer[3*i+1] = 10*((i+offset) % 3 == 1);
            led_buffer[3*i+2] = 10*((i+offset) % 3 == 2);
        }
        //åloadPayload
        //writeSource(offset);
        //}

        //Delay10KTCYx(50);

        sendStrip();

        //sendStrip();
    }

    //CloseADC();
}
Ejemplo n.º 22
0
int main(void)
{
    /* 2 bit for pre-emption priority, 2 bits for subpriority */
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    
    setup_systick();
    enable_tick_count();
    setup_io_leds();
    setup_io_usb();
    
    init_sensor_config();
    
    GYRO_INIT();
    ACC_INIT();
    MAG_INIT();
    nrf_init();
    nrf_detect();
    nrf_rx_mode_dual(nrf_addr, 5, 40);
    {
        uint8_t status = nrf_read_reg(NRF_STATUS);
        nrf_write_reg(NRF_FLUSH_RX, 0xff);
        nrf_write_reg(NRF_FLUSH_TX, 0xff);
        nrf_write_reg(NRF_WRITE_REG|NRF_STATUS,status); // clear IRQ flags
    }
    
    pwm_input_init();
    
    USB_Init();
    
    acc_scale_factor = calc_acc_scale(200);
    compute_gyro_runtime_bias(sensors.gyro_rt_bias, 1000);
    
    // wait usb ready
    //while ((bDeviceState != CONFIGURED)&&(USBConnectTimeOut != 0))
    //{}
    current_mode = DT_ATT;
    
    // endless loop
    while(1)
    {
        uint8_t buf[64];
        if(frame_100Hz){
            frame_100Hz = 0;
            buf[0] = 0;
            if(current_mode == DT_RCDATA){
                prepare_rc_data(buf);
                usb_send_data(buf,64);
            }else if(current_mode == DT_SENSOR){
                buf[0] = DT_SENSOR;
                buf[1] = 9;
                read_raw_gyro((int16_t*)(buf+2));
                read_raw_acc((int16_t*)(buf+8));
                read_raw_mag((int16_t*)(buf+14));
                usb_send_data(buf,64);
            }
            if(buf[0]){
                usb_send_data(buf,64);
            }
        }
        
        if(sensor_data_ready){
            sensor_data_ready = 0;
            if(sensors.sumTime_us){
                update_AHRS();
                if(current_mode == DT_ATT){
                    buf[0] = DT_ATT;
                    buf[1] = 3;
                    sensors.height = 0.0;
                    memcpy(buf+2,sensors.attitude,sizeof(sensors.attitude) + 4);
                    usb_send_data(buf,64);
                }
                LED4_TOGGLE;
                LED5_TOGGLE;
                LED10_TOGGLE;
            }
            // process sensor data
        }
        if(frame_200Hz){
            frame_200Hz = 0;
            // if L3GD20 already contains gyro data, rising edge will not occur
            if( (current_mode == DT_ATT) && (gyro_hungry>1) ){
                if(L3GD20_INT2){
                    int16_t gyro[3];
                    read_raw_gyro(gyro);
                }
            }
            if(gyro_hungry < 10)
                gyro_hungry++;
        }
        if(frame_1Hz){
            frame_1Hz = 0;
            LED3_TOGGLE;
        }
    }
}
Ejemplo n.º 23
0
//
//  Main program loop.
//
int main(void)
{
	unsigned char memflags;
	unsigned char action;
	unsigned char stage;
	unsigned char timeidx;
	unsigned char i;

	time = 0;
	
	__disable_interrupt();
	InitialiseSystemClock();
	InitialiseUSART();
	InitialiseTIM2();
	spi_init();
	nrf_init();
	network_init(RF_ROLE_TX);
	__enable_interrupt();

	delay_ms(1000);
	
#ifdef DUMP
	nrf_setrx();
#endif
#ifdef CITY
	cities_init();
	timeidx = 0;
	memflags = 0;
	my_info.type = TYPE_CITY;
	my_info.team = NO_TEAM;
	my_info.id = MY_ID;
	my_info.units = CITY_UNITS;
	my_info.mode = MODE_DEFENSE;
	my_info.level = 0;

	pk_out.node_from = my_info.id;
	pk_out.type = my_info.type;
	for (i = 0;i<10;i++){
		pk_out.teams[i] = 0xFF;
		pk_out.times[i] = 0xFF;	
	}
#endif

#ifdef SOURCE
	my_info.type = TYPE_SOURCE;
	my_info.team = NO_TEAM;
	my_info.id = MY_ID;	

	pk_out.node_from = my_info.id;
	pk_out.type = my_info.type;
#endif

	// Configure pins
	PB_DDR = (1<<5);
	PB_CR1 = (1<<5);
//	PB_ODR ^= (1<<5);
	// Loop
	do {
	//	PB_ODR ^= (1<<5);

#ifdef DUMP
		nrf_listen();
		delay_ms(10);
		nrf_nolisten();
		if (nrf_receive(data,32)){
			nrf_listen();
			puts("Data:");	
			for (i=0;i<32;i++){
				putchex(data[i]);	
				putc(' ');
			}
			puts("\n");	
		}
#endif
#ifdef SOURCE
		pk_out.team = my_info.team;
		pk_out.units = SOURCE_UNITS;
		pk_out.action = ACTION_SOURCE;
		pk_out.node_to = 0;
		if (memflags & FL_SEC){
			PB_ODR ^= (1<<5);
			network_send(&pk_out,0);
		}
		delay_ms(100);
	
#endif
#ifdef CITY
		pk_out.team = my_info.team;
		if (memflags & FL_SEC){
			puts("Team: ");
			putchex(my_info.team);
			puts(" level: ");
			putchex(my_info.level);
			puts(" units: ");
			putchex(my_info.units);
			puts(" uplevel: ");
			putchex(my_info.uplevel);
			putchex(timeidx);
			putc('\n');
			cities_broadcast();		
			if (time%(10-CITY_MAXLEVEL) == 0){
				if (my_info.units < CITY_MAXUNITS){ 
					my_info.units++;
				} else if (my_info.uplevel < CITY_LEVEL_UP){
					my_info.uplevel++;
				} else {
					my_info.uplevel = 0;
					if (my_info.level < CITY_MAXLEVEL){
						my_info.level++;	
					}	
				}
			}
			if (time%60 == 0){
				pk_out.teams[timeidx]=my_info.team;
				pk_out.times[timeidx]=time/60;	
				timeidx++;
				timeidx %= 10;
			}
		} 
		network_arcv_stop();
		delay_us(100);
                while (network_arcv(&pk_in)){
			if (my_info.units==0){
				break;	
			}
			PB_ODR ^= (1<<5);
                        if (pk_in.action == ACTION_BROADCAST){
				if ((pk_in.team == my_info.team)&&(my_info.units >=1)){
					transfer_start(pk_in.node_from);	
				}
                        } else {
                                if (pk_in.node_to == my_info.id){
					cities_city_process_action();
					puts("Processing\n");
                                }
                        }
                }
 

		delay_us(100);
		network_arcv_start();	
		delay_ms(100);


#endif


			

	memflags = flags;
	flags = 0;		
	} while(1);
}
Ejemplo n.º 24
0
void main_bridge(void)
{
    GLOBAL(daytrig)=10;
    GLOBAL(lcdbacklight)=10;
    GLOBAL(privacy) = 3;
    char input[64];
    char led1=0;
    char led2=0;

    usbCDCInit();
    delayms(500);
    nrf_init();
    nrf_config_set(&config);
    
    nrf_rcv_pkt_start();
    while(1){
        int l, i, status;
        CDC_OutBufAvailChar (&l);
        if(l>0){
            gpioSetValue (RB_LED0, led1);led1=1-led1;
            CDC_RdOutBuf (input, &l);
            for(i=0; i<l; i++){
                uint8_t cmd = serialmsg_put(input[i]);
                if( cmd != SERIAL_NONE ){
                    switch( cmd ){
                        case '1':
                            // can we loose packets here?
                            nrf_rcv_pkt_end();
                            status=snd_pkt_no_crc(serialmsg_len, serialmsg_message);
                            //status=nrf_snd_pkt_crc(serialmsg_len, serialmsg_message);
                            nrf_rcv_pkt_start();
                        break;
                        case '3':
                            memcpy(config.txmac, serialmsg_message, 5);
                            nrf_config_set(&config);
                        break;
                        case '4':
                            memcpy(config.mac0, serialmsg_message, 5);
                            nrf_config_set(&config);
                        break;
                        case '5':
                            config.channel=serialmsg_message[0];
                            nrf_config_set(&config);
                        break;
                        case '6':
                            config.maclen[0]=serialmsg_message[0];
                            nrf_config_set(&config);
                        break;
                        case '7':
                            puts("\\7");
                            char s[sizeof(uint32_t)+1];
                            *((uint32_t*)s) =GetUUID32();
                            s[sizeof(uint32_t)]=0;
                            puts(s);
                            puts("\\0");
                        break;
                    };
                    puts("\\2\\0");
                }
            }
        }
        int len;
        uint8_t buf[32];
        len=nrf_rcv_pkt_poll(sizeof(buf),buf);
        if( len > 0 ){
            gpioSetValue (RB_LED2, led2);led2=1-led2;
            puts("\\1");
            dump_encoded(len, buf);
            puts("\\0");
        }
    }
}
Ejemplo n.º 25
0
void nrf_check_reset(void){
    if(nrf_cmd_status(C_NOP) & R_STATUS_MAX_RT){
        _nrfresets++;
        nrf_init();
    };
};