Example #1
0
/**
 * main() function
 * @return 0. int return type required by ANSI/ISO standard.
 */
int main(void)
{
  /* Start 16 MHz crystal oscillator */
  NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
  NRF_CLOCK->TASKS_HFCLKSTART = 1;

  /* Wait for the external oscillator to start up */
  while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) 
  {
  }
  
  // Set Port 1 as output
  nrf_gpio_range_cfg_output(8, 15);

  // Set radio configuration parameters
  radio_configure();

  nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, 0x55);

  while(true)
  {
    // Set payload pointer
    NRF_RADIO->PACKETPTR = (uint32_t)packet;

    NRF_RADIO->EVENTS_READY = 0U;

    // Enable radio and wait for ready
    NRF_RADIO->TASKS_RXEN = 1U;

    while(NRF_RADIO->EVENTS_READY == 0U)
    {
    }

    NRF_RADIO->EVENTS_END = 0U;

    // Start listening and wait for address received event
    NRF_RADIO->TASKS_START = 1U;

    // Wait for end of packet
    while(NRF_RADIO->EVENTS_END == 0U)
    {
    }

    // Write received data to port 1 on CRC match
    if (NRF_RADIO->CRCSTATUS == 1U)
    {
      nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, packet[0]);
    }

    NRF_RADIO->EVENTS_DISABLED = 0U;

    // Disable radio
    NRF_RADIO->TASKS_DISABLE = 1U;

    while(NRF_RADIO->EVENTS_DISABLED == 0U)
    {
    }
  }

}
Example #2
0
void init(void)
{
  /* Start 16 MHz crystal oscillator */
  NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
  NRF_CLOCK->TASKS_HFCLKSTART = 1;

  /* Wait for the external oscillator to start up */
  while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) 
  {
  }

  // Set radio configuration parameters
  radio_configure();
  
  simple_uart_config(RTS_PIN_NUMBER, TX_PIN_NUMBER, CTS_PIN_NUMBER, RX_PIN_NUMBER, HWFC);

  // Set payload pointer
  //NRF_RADIO->PACKETPTR = (uint32_t)scan_rsp; //packet;  


  // We will only announce on channel 37 for now
  int channel = 37;
  NRF_RADIO->DATAWHITEIV = channel & 0x3F;
  NRF_RADIO->FREQUENCY = ch2freq(channel);

}
Example #3
0
/**
 * @brief Function for application main entry.
 */
int main(void)
{
    unsigned role = role_get();
    unsigned group = role >> ROLE_GR_SHIFT;
    rtc_initialize(rtc_dummy_handler);
    uart_init();
    stat_init(group);
 
    radio_configure(
            &g_report_packet, sizeof(g_report_packet),
            group ? GR1_CH : GR0_CH
        );

    receiver_on(on_packet_received);
    receive_start();

    while (true)
    {
        __WFI();
        if (g_stat_request) {
            g_stat_request = 0;
            stat_dump();
        }
    }
}
Example #4
0
/**
 * @brief Function for application main entry.
 * @return 0. int return type required by ANSI/ISO standard.
 */
int main(void)
{
    uint32_t err_code = NRF_SUCCESS;

    clock_initialization();
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, NULL);

    err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);
    err_code = bsp_init(BSP_INIT_LED, APP_TIMER_TICKS(100, APP_TIMER_PRESCALER), NULL);
    APP_ERROR_CHECK(err_code);

    // Set radio configuration parameters
    radio_configure();
    NRF_RADIO->PACKETPTR = (uint32_t)&packet;

    err_code = bsp_indication_set(BSP_INDICATE_USER_STATE_OFF);
    NRF_LOG_INFO("Wait for first packet\r\n");
    APP_ERROR_CHECK(err_code);
    NRF_LOG_FLUSH();    

    while (true)
    {
        uint32_t received = read_packet();

        err_code = bsp_indication_set(BSP_INDICATE_RCV_OK);
        NRF_LOG_INFO("Packet was received\r\n");
        APP_ERROR_CHECK(err_code);

        NRF_LOG_INFO("The contents of the package is %u\r\n", (unsigned int)received);
        NRF_LOG_FLUSH();
    }
}
Example #5
0
/*
==============================================
Function: initialize_all(void)

	Initialize oscillator, radio, bluetooth,
	twi and vibration	

==============================================
*/
static void initialize_all()
{
	char buf[30];
	// Start 16 MHz crystal oscillator.
	NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
	NRF_CLOCK->TASKS_HFCLKSTART    = 1;
	
        // oscillator
	while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) {
		// busy wait until the oscilator is up and running
	}
	
	simple_uart_config(0, 23, 0, 22, 0);
	simple_uart_putstring("INIT\n");
	
	// initiliaze radio
    radio_configure(); 
    simple_uart_putstring("Configured radio\n");

	// initialize bluetooth
    start_ble(MUG_LIST); 
	simple_uart_putstring("BLUETOOTH STARTED\n");
		
	// initialize twi
	twi_master_init();
	simple_uart_putstring("TWI master init\n");

	init_vibration();
	simple_uart_putstring("Vibration init\n");


}
Example #6
0
/**
 * @brief Function for application main entry.
 */
int main(void)
{
    // Start 16 MHz crystal oscillator.
    NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
    NRF_CLOCK->TASKS_HFCLKSTART    = 1;

    // Wait for the external oscillator to start up.
    while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) 
    {
        // Do nothing.
    }

    // Set Port 0 as input.
    nrf_gpio_range_cfg_input(BUTTON_START, BUTTON_STOP, BUTTON_PULL);

    // Set Port 1 as output.
    nrf_gpio_range_cfg_output(LED_START, LED_STOP);

    // Set radio configuration parameters.
    radio_configure();

    // Set payload pointer.
    NRF_RADIO->PACKETPTR = (uint32_t)packet;

    while (true)
    {
        // Read Data to send, button signals are default high, and low when pressed.
        packet[0]               = ~(nrf_gpio_port_read(NRF_GPIO_PORT_SELECT_PORT0));  // Write GPIO to payload byte 0.
        NRF_RADIO->EVENTS_READY = 0U;
        NRF_RADIO->TASKS_TXEN   = 1; // Enable radio and wait for ready.

        while (NRF_RADIO->EVENTS_READY == 0U)
        {
            // Do nothing.
        }

        // Start transmission.
        NRF_RADIO->TASKS_START = 1U;
        NRF_RADIO->EVENTS_END  = 0U;
    
        while (NRF_RADIO->EVENTS_END == 0U) // Wait for end of the transmission packet.
        {
            // Do nothing.
        }

        // Write sent data to port 1.
        nrf_gpio_port_write(NRF_GPIO_PORT_SELECT_PORT1, packet[0]);

        NRF_RADIO->EVENTS_DISABLED = 0U;
        NRF_RADIO->TASKS_DISABLE   = 1U; // Disable the radio.

        while (NRF_RADIO->EVENTS_DISABLED == 0U)
        {
            // Do nothing.
        }
    }
}
Example #7
0
File: tdm.c Project: gfornax/SiK
/// build the timing table
static void 
tdm_build_timing_table(void)
{
	__pdata uint8_t j;
	__pdata uint16_t rate;
	bool golay_saved = feature_golay;
	feature_golay = false;

	for (rate=2; rate<256; rate=(rate*3)/2) {
		__pdata uint32_t latency_sum=0, per_byte_sum=0;
		uint8_t size = MAX_PACKET_LENGTH;
		radio_configure(rate);
		for (j=0; j<50; j++) {
			__pdata uint16_t time_0, time_max, t1, t2;
			radio_set_channel(1);
			radio_receiver_on();
			if (serial_read_available() > 0) {
				feature_golay = golay_saved;
				return;
			}
			t1 = timer2_tick();
			if (!radio_transmit(0, pbuf, 0xFFFF)) {
				break;
			}
			t2 = timer2_tick();
			radio_receiver_on();

			time_0 = t2-t1;

			radio_set_channel(2);
			t1 = timer2_tick();
			if (!radio_transmit(size, pbuf, 0xFFFF)) {
				if (size == 0) {
					break;
				}
				size /= 4;
				j--;
				continue;
			}

			t2 = timer2_tick();
			radio_receiver_on();

			time_max = t2-t1;
			latency_sum += time_0;
			per_byte_sum += ((size/2) + (time_max - time_0))/size;
		}
		if (j > 0) {
			printf("{ %u, %u, %u },\n",
			       (unsigned)(radio_air_rate()),
			       (unsigned)(latency_sum/j),
			       (unsigned)(per_byte_sum/j));
		}
	}
	feature_golay = golay_saved;
}
int reconfig(){
	int tx;
	int ptx;

	simple_uart_putstring((uint8_t *)"Digte uma nova potencia\n");
	ptx = simple_uart_get();

	radio_configure(1, (uint32_t)ptx);
	NRF_RADIO->PACKETPTR = (uint32_t)packet;
	
}
Example #9
0
	void transceiver_setup(void){
		
		// Atualiza a área de memória apontada pelos ponteiros.
		reconf_info_tag();

		// Set radio configuration parameters
		radio_configure(&taxa, &potencia);
		

		// Set payload pointer
		NRF_RADIO->PACKETPTR = (uint32_t)packet;  
	}
Example #10
0
File: main.c Project: IOIOI/nRF51
/**
 * @brief Function for application main entry.
 * @return 0. int return type required by ANSI/ISO standard.
 */
int main(void)
{
    uint32_t err_code = NRF_SUCCESS;
    
    clock_initialization();
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, NULL);
        
    const app_uart_comm_params_t comm_params =  
    {
        RX_PIN_NUMBER, 
        TX_PIN_NUMBER, 
        RTS_PIN_NUMBER, 
        CTS_PIN_NUMBER, 
        APP_UART_FLOW_CONTROL_ENABLED, 
        false, 
        UART_BAUDRATE_BAUDRATE_Baud115200
    };   
    
    APP_UART_FIFO_INIT(&comm_params, 
                       UART_RX_BUF_SIZE, 
                       UART_TX_BUF_SIZE, 
                       uart_error_handle, 
                       APP_IRQ_PRIORITY_LOW,
                       err_code);
        
    err_code = bsp_init(BSP_INIT_LED | BSP_INIT_BUTTONS,
                        APP_TIMER_TICKS(100, APP_TIMER_PRESCALER),
                        bsp_evt_handler);
    APP_ERROR_CHECK(err_code);

    // Set radio configuration parameters
    radio_configure();

    // Set payload pointer
    NRF_RADIO->PACKETPTR = (uint32_t)&packet;

    err_code = bsp_indication_text_set(BSP_INDICATE_USER_STATE_OFF, "Press Any Button\n\r");
    APP_ERROR_CHECK(err_code);

    while (true)
    {
        if(packet != 0)
        {
            send_packet();
            printf("The contents of the package was %u\n\r", (unsigned int)packet);
            packet = 0;
        }
        __WFE();
    }
}
Example #11
0
void init(void)
{
  /* Start 16 MHz crystal oscillator */
  NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
  NRF_CLOCK->TASKS_HFCLKSTART = 1;

  /* Wait for the external oscillator to start up */
  while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) 
  {
  }

  nrf_gpio_range_cfg_output(LED_START, LED_STOP);

  // Set radio configuration parameters
  radio_configure();
}
Example #12
0
File: main.c Project: JulianYG/WNR
/**
 * @brief Function for application main entry.
 * @return 0. int return type required by ANSI/ISO standard.
 */
int main(void)
{
    uint32_t err_code = NRF_SUCCESS;

    clock_initialization();
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, NULL);

    const app_uart_comm_params_t comm_params =
    {
        RX_PIN_NUMBER,
        TX_PIN_NUMBER,
        RTS_PIN_NUMBER,
        CTS_PIN_NUMBER,
        APP_UART_FLOW_CONTROL_ENABLED,
        false,
        UART_BAUDRATE_BAUDRATE_Baud38400
    };
    APP_UART_FIFO_INIT(&comm_params,
                       UART_RX_BUF_SIZE,
                       UART_TX_BUF_SIZE,
                       uart_error_handle,
                       APP_IRQ_PRIORITY_LOW,
                       err_code);
    APP_ERROR_CHECK(err_code);
    err_code = bsp_init(BSP_INIT_LED, APP_TIMER_TICKS(100, APP_TIMER_PRESCALER), NULL);
    APP_ERROR_CHECK(err_code);

    // Set radio configuration parameters
    radio_configure();
    NRF_RADIO->PACKETPTR = (uint32_t)&packet;

    err_code = bsp_indication_text_set(BSP_INDICATE_USER_STATE_OFF, "Wait for first packet\n\r");
    APP_ERROR_CHECK(err_code);

    while (true)
    {
        uint32_t received = read_packet();

        err_code = bsp_indication_text_set(BSP_INDICATE_RCV_OK, "Packet was received\n\r");
        APP_ERROR_CHECK(err_code);

        printf("The contents of the package is %u\n\r", (unsigned int)received);
    }
}
void init(void)
{
    /* Start 16 MHz crystal oscillator */
    NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
    NRF_CLOCK->TASKS_HFCLKSTART = 1;

    /* Wait for the external oscillator to start up */
    while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
    {
    }

    // Set radio configuration parameters
    radio_configure();

    // Set payload pointer
    NRF_RADIO->PACKETPTR = (uint32_t)packet;

    simple_uart_config(RTS_PIN_NUMBER, TX_PIN_NUMBER, CTS_PIN_NUMBER, RX_PIN_NUMBER, HWFC);
}
Example #14
0
void init(void)
{
  /* Start 16 MHz crystal oscillator */
  NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
  NRF_CLOCK->TASKS_HFCLKSTART = 1;

  /* Wait for the external oscillator to start up */
  while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) 
  {
  }

  // Set radio configuration parameters
  radio_configure();
  
  // Set payload pointer
  NRF_RADIO->PACKETPTR = (uint32_t)packet;
  
  nrf_gpio_range_cfg_input(BUTTON_START, BUTTON_STOP, NRF_GPIO_PIN_PULLUP);
}
void init()
{
  /* Start 16 MHz crystal oscillator */
  NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
  NRF_CLOCK->TASKS_HFCLKSTART = 1;

  /* Wait for the external oscillator to start up */
  while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
  {
  }

  // Set radio configuration parameters
  

  simple_uart_config(RTS_PIN_NUMBER, TX_PIN_NUMBER, CTS_PIN_NUMBER, RX_PIN_NUMBER, HWFC);

  // Set payload pointer
	
	radio_configure(1, 0);  
}
Example #16
0
/**
 * @brief Function for application main entry.
 */
int main(void)
{
    rtc_initialize(rtc_dummy_handler);
    radio_configure(&g_pkt, 0, PROTOCOL_CHANNEL);
    uart_init();
    hf_osc_start();

#ifdef USE_DISPLAY
    displ_init();
    show_startup_screen();
#endif

    receiver_on_(0);
    receive_start();

    while (true)
    {
        if (radio_tx_end()) {
            on_packet_received();
            receive_start();
        }
    }
}
Example #17
0
File: main.c Project: HefnySco/SiK
static void
radio_init(void)
{
	__pdata uint32_t freq_min, freq_max;
	__pdata uint32_t channel_spacing;
	__pdata uint8_t txpower;

	// Do generic PHY initialisation
	if (!radio_initialise()) {
		panic("radio_initialise failed");
	}

	switch (g_board_frequency) {
	case FREQ_433:
		freq_min = 433050000UL;
		freq_max = 434790000UL;
		txpower = 10;
		num_fh_channels = 10;
		break;
	case FREQ_470:
		freq_min = 470000000UL;
		freq_max = 471000000UL;
		txpower = 10;
		num_fh_channels = 10;
		break;
	case FREQ_868:
		freq_min = 868000000UL;
		freq_max = 869000000UL;
		txpower = 10;
		num_fh_channels = 10;
		break;
	case FREQ_915:
		freq_min = 915000000UL;
		freq_max = 928000000UL;
		txpower = 20;
		num_fh_channels = MAX_FREQ_CHANNELS;
		break;
	default:
		freq_min = 0;
		freq_max = 0;
		txpower = 0;
		panic("bad board frequency %d", g_board_frequency);
		break;
	}

	if (param_get(PARAM_NUM_CHANNELS) != 0) {
		num_fh_channels = param_get(PARAM_NUM_CHANNELS);
	}
	if (param_get(PARAM_MIN_FREQ) != 0) {
		freq_min        = param_get(PARAM_MIN_FREQ) * 1000UL;
	}
	if (param_get(PARAM_MAX_FREQ) != 0) {
		freq_max        = param_get(PARAM_MAX_FREQ) * 1000UL;
	}
	if (param_get(PARAM_TXPOWER) != 0) {
		txpower = param_get(PARAM_TXPOWER);
	}

	// constrain power and channels
	txpower = constrain(txpower, BOARD_MINTXPOWER, BOARD_MAXTXPOWER);
	num_fh_channels = constrain(num_fh_channels, 1, MAX_FREQ_CHANNELS);

	// double check ranges the board can do
	switch (g_board_frequency) {
	case FREQ_433:
		freq_min = constrain(freq_min, 414000000UL, 460000000UL);
		freq_max = constrain(freq_max, 414000000UL, 460000000UL);
		break;
	case FREQ_470:
		freq_min = constrain(freq_min, 450000000UL, 490000000UL);
		freq_max = constrain(freq_max, 450000000UL, 490000000UL);
		break;
	case FREQ_868:
		freq_min = constrain(freq_min, 849000000UL, 889000000UL);
		freq_max = constrain(freq_max, 849000000UL, 889000000UL);
		break;
	case FREQ_915:
		freq_min = constrain(freq_min, 868000000UL, 935000000UL);
		freq_max = constrain(freq_max, 868000000UL, 935000000UL);
		break;
	default:
		panic("bad board frequency %d", g_board_frequency);
		break;
	}

	if (freq_max == freq_min) {
		freq_max = freq_min + 1000000UL;
	}

	// get the duty cycle we will use
	duty_cycle = param_get(PARAM_DUTY_CYCLE);
	duty_cycle = constrain(duty_cycle, 0, 100);
	param_set(PARAM_DUTY_CYCLE, duty_cycle);

	// get the LBT threshold we will use
	lbt_rssi = param_get(PARAM_LBT_RSSI);
	if (lbt_rssi != 0) {
		// limit to the RSSI valid range
		lbt_rssi = constrain(lbt_rssi, 25, 220);
	}
	param_set(PARAM_LBT_RSSI, lbt_rssi);

	// sanity checks
	param_set(PARAM_MIN_FREQ, freq_min/1000);
	param_set(PARAM_MAX_FREQ, freq_max/1000);
	param_set(PARAM_NUM_CHANNELS, num_fh_channels);

	channel_spacing = (freq_max - freq_min) / (num_fh_channels+2);

	// add half of the channel spacing, to ensure that we are well
	// away from the edges of the allowed range
	freq_min += channel_spacing/2;

	// add another offset based on network ID. This means that
	// with different network IDs we will have much lower
	// interference
	srand(param_get(PARAM_NETID));
	if (num_fh_channels > 5) {
		freq_min += ((unsigned long)(rand()*625)) % channel_spacing;
	}
	debug("freq low=%lu high=%lu spacing=%lu\n", 
	       freq_min, freq_min+(num_fh_channels*channel_spacing), 
	       channel_spacing);

	// set the frequency and channel spacing
	// change base freq based on netid
	radio_set_frequency(freq_min);

	// set channel spacing
	radio_set_channel_spacing(channel_spacing);

	// start on a channel chosen by network ID
	radio_set_channel(param_get(PARAM_NETID) % num_fh_channels);

	// And intilise the radio with them.
	if (!radio_configure(param_get(PARAM_AIR_SPEED)) &&
	    !radio_configure(param_get(PARAM_AIR_SPEED)) &&
	    !radio_configure(param_get(PARAM_AIR_SPEED))) {
		panic("radio_configure failed");
	}

	// report the real air data rate in parameters
	param_set(PARAM_AIR_SPEED, radio_air_rate());

	// setup network ID
	radio_set_network_id(param_get(PARAM_NETID));

	// setup transmit power
	radio_set_transmit_power(txpower);
	
	// report the real transmit power in settings
	param_set(PARAM_TXPOWER, radio_get_transmit_power());

#ifdef USE_RTC
	// initialise real time clock
	rtc_init();
#endif

	// initialise frequency hopping system
	fhop_init(param_get(PARAM_NETID));

	// initialise TDM system
	tdm_init();
}
Example #18
0
int main(){
	
    initialize_all();
    char buf[30];
	uint8_t uart_data;
	
	// Print Device ID
	uint64_t this_device_id = (((uint64_t) NRF_FICR->DEVICEID[1] << 32) | ((uint64_t) NRF_FICR->DEVICEID[0]));
	sprintf((char*)buf, "ID: %llx\n",  this_device_id);
	simple_uart_putstring(buf);

	uint8_t availability;

	bool radio_executed = false;

	// main application loop
	while (1) {

         //All the mugs that will be invited have been set
		 if (is_connected()) {
			// Deal with radios
		    sd_softdevice_disable();
			radio_configure();
           // simple_uart_putstring("Configured radio\n");
			// simple_uart_putstring("Disabled soft device\n");

			// END - Deal with radios

			bool disovery_complete = false;
			bool all_final_state = true;
			int8_t current_mug = 0;

		 	while (!disovery_complete){
		 		uint64_t device_id = 0;

		 		// vibration_update();
		 		if (receive_packet(20)){
		 		    device_id = packet[1];
		 		    if (this_device_id == device_id){
		 		        if (packet[0] == 0xcfcf){  // Master init sequence
		 		            simple_uart_putstring("Got init!\n");
		 		            packet[0] = 0xaf;  // Send ACK to Master
		 		            send_packet(1);    // ... 3 times ...

		 		            simple_uart_putstring("[a]ccept or [r]eject:\n");
		 		            uart_data = simple_uart_get();
		 		            switch (uart_data) {
		 		              case 'a':
		 		                availability = 0xaa;
		 		                simple_uart_putstring("Accepted\n");
		 		                break;
		 		              case 'r':
		 		                availability = 0xff;
		 		                simple_uart_putstring("Rejected\n");
		 		                break;
		 		            }
		 		            uart_data = simple_uart_get();
		 		        } else if (packet[0] == 0xabab) {  // Master poll availability
		 		            simple_uart_putstring("Sent availability status!\n");
		 		            packet[0] = availability;  // Send ACK to Master
		 		            send_packet(1);
		 		        } else if (packet[0] == 0xdede) {  // Master kettle boiling
		 		            simple_uart_putstring("Got invitation!\n");
		 		            packet[0] = 0xaf;  // Send ACK to Master
		 		            send_packet(1);    // ... 3 times ...
		 		            for (uint8_t i=0; i<100; i++){
		 		                nrf_gpio_pin_toggle(0);
		 		                nrf_delay_ms(200);
		 		            }
		 		        } else {
		 		            sprintf((char*)buf, "Brocken command: %llx\n", packet[0]);
		 		            simple_uart_putstring(buf);
		 		        }
		 		    } else {
		 		        simple_uart_putstring("Not applicable packet\n");
		 		        sprintf((char*)buf, "Target:  %llx\n", packet[1]);
		 		        simple_uart_putstring(buf);
		 		        sprintf((char*)buf, "Command: %llx\n", packet[0]);
		 		        simple_uart_putstring(buf);
		 		    }
		 		}
		 		if (bump_action()) {
		 			disovery_complete = true;
		 			// uint8_t bump_evt = 1;
		 			// ble_update_bump(&bump_evt)
		 		}
			}

		 	// Deal with radios
	         sd_softdevice_enable(NRF_CLOCK_LFCLKSRC_RC_250_PPM_1000MS_CALIBRATION,app_error_handler);
	       	 // simple_uart_putstring("Enabled soft device\n");
			// END - Deal with radios

	       	// uint8_t bump_val = 1;
	       	// ble_update_bump(&bump_val);
	       	// simple_uart_putstring("They see me bumpin', I see 'em hatin'!\n");
	       	disovery_complete = false;

			// RSVP_App();  //sends MUG information back to app via ble, defined in slip_ble.c

		}
		//debug_ble_ids();
		// vibration_update();
		app_sched_execute();
		nrf_delay_ms(500);

	}
}
Example #19
0
int main(void)
{
    // Start 16 MHz crystal oscillator.
    NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
    NRF_CLOCK->TASKS_HFCLKSTART    = 1;

    // Wait for the external oscillator to start up.
    while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) {}

    // Enable UART comms
    simple_uart_config(0, 23, 0, 22, 0);
    unsigned char buf[32];
    uint8_t uart_data;

    // Enable LED
    nrf_gpio_cfg_output(0);

    // Set radio configuration parameters.
    radio_configure();

    // Print Device ID
    uint64_t this_device_id = (((uint64_t) NRF_FICR->DEVICEID[1] << 32) | ((uint64_t) NRF_FICR->DEVICEID[0]));
    sprintf((char*)buf, "ID: %llx\n",  this_device_id);
    simple_uart_putstring(buf);

    // Initialise LibAlek
    // init_vibration();

    uint8_t availability;

    while(true){
        uint64_t device_id = 0;

        // vibration_update();
        if (receive_packet()){
            device_id = packet[1];
            if (this_device_id == device_id){
                if (packet[0] == 0xcfcf){  // Master init sequence
                    simple_uart_putstring("Got init!\n");
                    packet[0] = 0xaf;  // Send ACK to Master
                    send_packet(1);    // ... 3 times ...

                    simple_uart_putstring("[a]ccept or [r]eject:\n");
                    uart_data = simple_uart_get();
                    switch (uart_data) {
                      case 'a':
                        availability = 0xaa;
                        simple_uart_putstring("Accepted\n");
                        break;
                      case 'r':
                        availability = 0xff;
                        simple_uart_putstring("Rejected\n");
                        break;
                    }
                    uart_data = simple_uart_get();
                } else if (packet[0] == 0xabab) {  // Master poll availability
                    simple_uart_putstring("Sent availability status!\n");
                    packet[0] = availability;  // Send ACK to Master
                    send_packet(1);
                } else if (packet[0] == 0xdede) {  // Master kettle boiling
                    simple_uart_putstring("Got invitation!\n");
                    packet[0] = 0xaf;  // Send ACK to Master
                    send_packet(1);    // ... 3 times ...
                    for (uint8_t i=0; i<100; i++){
                        nrf_gpio_pin_toggle(0);
                        nrf_delay_ms(200);
                    }
                } else {
                    sprintf((char*)buf, "Brocken command: %llx\n", packet[0]);
                    simple_uart_putstring(buf);
                }
            } else {
                simple_uart_putstring("Not applicable packet\n");
                sprintf((char*)buf, "Target:  %llx\n", packet[1]);
                simple_uart_putstring(buf);
                sprintf((char*)buf, "Command: %llx\n", packet[0]);
                simple_uart_putstring(buf);
            }
        }
    }
}
Example #20
0
File: main.c Project: alueger/dAISy
int main(void)
{
	// configure WDT
	WDTCTL = WDTPW | WDTHOLD;				// stop watch dog timer
	_25mhz();


#ifdef TEST
	// when compiled in test mode, use different main
	// disconnect radio when testing to avoid damage!
	test_main();
#endif

	// configure LED1 and turn it off, we'll use that for error and other stuff
	P1DIR |= LED1;
	LED1_OFF;

	P4DIR |= LED2;
	LED2_ON;

	// setup uart
	uart_init();

#ifdef DEBUG_MESSAGES
	uart_send_string("Hola mundo!\r\n");
#endif

	// setup packet handler
	ph_setup();

	// setup an configure radio
	radio_setup();
	radio_configure();

	// self-calibrate image rejection
	radio_calibrate_ir();

	// verify that radio configuration was successful
	radio_get_chip_status(0);
	if (radio_buffer.chip_status.chip_status & RADIO_CMD_ERROR) {	// check for command error
		uart_send_string("Error inicializando radio!!!\r\n");
		while (1) {
			LED1_TOGGLE;
			_delay_cycles(8000000);			// blink LED if there was an error
		}
	}


	// start packet receiving
	ph_start();

#ifdef DEBUG_MESSAGES
	uart_send_string("dAISy 0.2 started\r\n");
	LED2_OFF;
#endif

	while (1) {

		LPM0;	// deep sleep until something worthwhile happens

		__no_operation();

		ph_loop();	// packet handler house-keeping, e.g. channel hopping

#ifdef DEBUG_MESSAGES
		uint8_t channel;
		int16_t rssi;
		// debug code to monitor signal strength (RSSI)
		if (ph_get_state() == PH_STATE_PREFETCH) {											// found preamble and start flag
			// record current channel and signal strength
			channel = ph_get_radio_channel();												// read current channel
			rssi = ph_get_radio_rssi();														// read current RSSI
		}
#endif

		// retrieve last packet handler error
		uint8_t error = ph_get_last_error();
#ifdef DEBUG_MESSAGES
		// report error if packet handler failed
		if (error != PH_ERROR_NONE)	{
			dec_to_str(str_output_buffer, 3, rssi);											// convert to decimal string (reuse radio buffer)
			str_output_buffer[4] = 0;														// terminate string
			uart_send_string("sync ");														// send debug message to UART
			uart_send_byte(channel + 'A');
			uart_send_string(" RSSI=");
			uart_send_string(str_output_buffer);
			uart_send_string("dBm\r\n");
			uart_send_string("error: ");
			switch (error) {
			case PH_ERROR_NOEND:
				uart_send_string("no end flag");
				break;
			case PH_ERROR_STUFFBIT:
				uart_send_string("invalid stuff bit");
				break;
			case PH_ERROR_CRC:
				uart_send_string("CRC error");
				break;
			case PH_ERROR_RSSI_DROP:
				uart_send_string("RSSI drop");
				break;
			}
			uart_send_string("\r\n");
			ph_loop();							// house keeping, sending over UART takes time
		}
#else
		// toggle LED if packet handler failed after finding preamble and start flag
		if (error == PH_ERROR_NOEND || error == PH_ERROR_STUFFBIT || error == PH_ERROR_CRC)
			LED1_TOGGLE;
#endif

		// check if a new valid packet arrived
		uint16_t size = fifo_get_packet();
		if (size > 0) {								// if so, process packet

#ifdef DEBUG_MESSAGES
			dec_to_str(str_output_buffer, 3, rssi);											// convert to decimal string (reuse radio buffer)
			str_output_buffer[4] = 0;														// terminate string
			uart_send_string("sync ");														// send debug message to UART
			uart_send_byte(channel + 'A');
			uart_send_string(" RSSI=");
			uart_send_string(str_output_buffer);
			uart_send_string("dBm\r\n");
#endif
			LED2_ON;
			nmea_process_packet();					// process packet (NMEA message will be sent over UART)
			fifo_remove_packet();					// remove processed packet from FIFO
			LED2_OFF;
		}

		// enter low power mode LPM0 (everything off)
		// TODO: wait for UART to complete transmission

		// TODO: suspend UART
	}
}