Exemple #1
0
void usart_debug_init(const struct UsartConfig * config)
{
    /* Set up circular buffer for rx */
    circularBufferInit(&rxcb, usart_debug_buffer, sizeof(usart_debug_buffer) / sizeof(char));
    configure_usart(config);
    configure_usart_callbacks();
}
int main(void)
{
	system_init();

//! [setup_init]
	configure_usart();
	configure_usart_callbacks();
//! [setup_init]

//! [main]
//! [enable_global_interrupts]
	system_interrupt_enable_global();
//! [enable_global_interrupts]

//! [main_send_string]
	uint8_t string[] = "Hello World!\r\n";
	usart_write_buffer_job(&usart_instance, string, sizeof(string));
//! [main_send_string]

//! [main_loop]
	while (true) {
//! [main_loop]
//! [main_read]
		usart_read_buffer_job(&usart_instance,
				(uint8_t *)rx_buffer, MAX_RX_BUFFER_LENGTH);
//! [main_read]
	}
//! [main]
}
Exemple #3
0
/**
 * \brief The main function.
 */
int main(void)
{
//! [setup_init]
	/* Initialize the system and console*/
	system_init();
	configure_usart();

//! [setup_config]
	aes_get_config_defaults(&g_aes_cfg);
//! [setup_config]

//! [setup_config_defaults]
	aes_init(&aes_instance,AES, &g_aes_cfg);
//! [setup_config_defaults]
//! [module_enable]
	aes_enable(&aes_instance);
//! [module_enable]

//! [setup_init]

//! [encryption_decryption]
	/* Run ECB mode test*/
	ecb_mode_test();
//! [encryption_decryption]

	while (true) {
		/* Infinite loop */
	}
}
Exemple #4
0
int main (void)
{
	system_init();

  configure_port_pins();

  configure_usart();

  printf("--TEST prepare: ATMEL SAMD10 watchdog callback!\r\n");
  
  configure_wdt();
  configure_wdt_callbacks();  
  
  printf("--TEST prepare: Config WDT:4096 ms!\r\n");
  printf("--TEST prepare: Config WDT:Early Warning,2048ms!\r\n");
  
  system_interrupt_enable_global();

  printf("--TEST prepare: Enable gobale Interrupt!\r\n");
  
  printf("--TEST information: Waitting for WDT Early Warning Interrupt!\r\n");  
  printf("\r\n\r\n\r\n");    

  while(1)
  {
    ;
  }
	/* Insert application code here, after the board has been initialized. */
}
Exemple #5
0
int main(void)
{
	system_init();

//! [setup_init]
	configure_usart();
//! [setup_init]

//! [main]
//! [main_send_string]
	uint8_t string[] = "Hello World!\r\n";
	usart_write_buffer_wait(&usart_instance, string, sizeof(string));
//! [main_send_string]

//! [main_rec_var]
	uint16_t temp;
//! [main_rec_var]

//! [main_loop]
	while (true) {
//! [main_read]
		if (usart_read_wait(&usart_instance, &temp) == STATUS_OK) {
//! [main_read]
//! [main_write]
			while (usart_write_wait(&usart_instance, temp) != STATUS_OK) {
			}
//! [main_write]
		}
	}
//! [main_loop]
//! [main]
}
Exemple #6
0
int main(void)
{
	system_init();
	configure_usart();
	configure_usart_callbacks();
	
	struct port_config pin_conf;
	port_get_config_defaults(&pin_conf);

	/* Configure LEDs as outputs, turn them off */
	pin_conf.direction  = PORT_PIN_DIR_OUTPUT;
	port_pin_set_config(LED_1_PIN, &pin_conf);
	port_pin_set_output_level(LED_1_PIN, LED_1_INACTIVE);

	/* Set buttons as inputs */
	pin_conf.direction  = PORT_PIN_DIR_INPUT;
	pin_conf.input_pull = PORT_PIN_PULL_UP;
	port_pin_set_config(BUTTON_1_PIN, &pin_conf);


#if USE_EIC == true
	configure_extint();
#endif

#if USE_INTERRUPTS == true
#  if USE_EIC == false
	configure_systick_handler();
#  else
	configure_eic_callback();
#  endif

	system_interrupt_enable_global();

	uint16_t temp;
	while (true) {
		/* Do nothing - use interrupts */
		//if (usart_read_wait(&usart_instance, &temp) == STATUS_OK)
		//{
			//while (usart_write_wait(&usart_instance, temp) != STATUS_OK);
		//}
		usart_read_buffer_job(&usart_instance, (uint8_t *)rx_buffer, 1);
		//sleepmgr_sleep(SLEEPMGR_STANDBY);
	}
#else
#  if USE_EIC == false
	while (true) {
		update_led_state();
	}
#  else
	while (true) {
		if (extint_chan_is_detected(BUTTON_1_EIC_LINE)) {
			extint_chan_clear_detected(BUTTON_1_EIC_LINE);

			update_led_state();
		}
	}
#  endif
#endif
}
Exemple #7
0
/**
 *  \brief usart_hard_handshaking_example application entry point.
 *
 *  \return Unused (ANSI-C compatibility).
 */
int main(void)
{
	uint8_t uc_char;
	uint8_t uc_flag;

	/* Initialize the system. */
	sysclk_init();
	board_init();

	/* Configure UART for debug message output. */
	configure_console();

	/* Output example information. */
	puts(STRING_HEADER);

	/* Initialize board USART. */
	configure_usart();

	/* Initialize TC0. */
	configure_tc();

	/* Get board USART PDC base address and enable receiver. */
	g_p_pdc = usart_get_pdc_base(BOARD_USART);
	g_st_packet.ul_addr = (uint32_t)gs_puc_buffer;
	g_st_packet.ul_size = BUFFER_SIZE;
	g_st_nextpacket.ul_addr = (uint32_t)gs_puc_nextbuffer;
	g_st_nextpacket.ul_size = BUFFER_SIZE;
	pdc_rx_init(g_p_pdc, &g_st_packet, &g_st_nextpacket);
	pdc_enable_transfer(g_p_pdc, PERIPH_PTCR_RXTEN);

	usart_enable_interrupt(BOARD_USART, US_IER_RXBUFF);

	/* Start the Timer counter. */
	tc_start(TC0, 0);

	while (1) {
		uc_char = 0;
		uc_flag = uart_read(CONSOLE_UART, &uc_char);
		if (!uc_flag) {
			switch (uc_char) {
			case 'd':
			case 'D':
				usart_write_line(BOARD_USART, (char *)gs_dump_buffer);
				break;
			default:
				break;
			}
		}
	}
}
Exemple #8
0
int main (void)
{
	system_init();

	configure_usart();

	xTaskCreate(led_task, (signed char *) "LED task#1",
				configMINIMAL_STACK_SIZE+100,NULL, 2,& worker1_id);

	vTaskStartScheduler();

	// Insert application code here, after the board has been initialized.
	while(1);
}
/**
 * \brief The main function.
 */
int main(void)
{
//! [setup_init]

	/* Initialize the system and console*/
	system_init();
	configure_usart();

//! [setup_config]
	aes_get_config_defaults(&g_aes_cfg);
//! [setup_config]
//! [setup_config_defaults]
	aes_init(&aes_instance,AES, &g_aes_cfg);
//! [setup_config_defaults]

//! [module_enable]
	aes_enable(&aes_instance);
//! [module_enable]

//! [module_enable_register]
	/* Enable AES interrupt. */
	aes_register_callback(aes_callback,AES_CALLBACK_ENCRYPTION_COMPLETE);
	aes_enable_callback(&aes_instance,AES_CALLBACK_ENCRYPTION_COMPLETE);
//! [module_enable_register]

//! [setup_init]
	printf("Start test\r\n");
//! [encryption_decryption]
//![ECB_MODE]
	ecb_mode_test();
//![ECB_MODE]
//![CBC_MODE]
	cbc_mode_test();
//![CBC_MODE]
//![CFB_MODE]
	cfb128_mode_test();
//![CFB_MODE]
//![OFB_MODE]
	ofb_mode_test();
//![OFB_MODE]
//![CTR_MODE]
	ctr_mode_test();
//![CTR_MODE]

//! [encryption_decryption]

	while(1) ;

}
Exemple #10
0
/**
 * \brief Run AES driver unit tests.
 */
int main(void)
{
	/* Initialize the system and console*/
	system_init();
	configure_usart();

	/*Initialize the delay driver*/
	delay_init();

	/* Enable the AES module. */
	aes_get_config_defaults(&g_aes_cfg);
	aes_init(&aes_instance,AES, &g_aes_cfg);
	aes_enable(&aes_instance);

	/* Define all the test cases. */
	DEFINE_TEST_CASE(ecb_mode_test, NULL, run_ecb_mode_test, NULL,
			"SAM AES ECB mode encryption and decryption test.");
	DEFINE_TEST_CASE(cbc_mode_test, NULL, run_cbc_mode_test, NULL,
			"SAM AES CBC mode encryption and decryption test.");
	DEFINE_TEST_CASE(cfb128_mode_test, NULL, run_cfb128_mode_test, NULL,
			"SAM AES CFB128 mode encryption and decryption test.");
	DEFINE_TEST_CASE(ofb_mode_test, NULL, run_ofb_mode_test, NULL,
			"SAM AES OFB mode encryption and decryption test.");
	DEFINE_TEST_CASE(ctr_mode_test, NULL, run_ctr_mode_test, NULL,
			"SAM AES CTR mode encryption and decryption test.");
	DEFINE_TEST_CASE(ecb_mode_test_dma, NULL, run_ecb_mode_test_dma, NULL,
			"SAM AES ECB mode encryption and decryption with DMA test.");

	/* Put test case addresses in an array. */
	DEFINE_TEST_ARRAY(aes_tests) = {
		&ecb_mode_test,
		&cbc_mode_test,
		&cfb128_mode_test,
		&ofb_mode_test,
		&ctr_mode_test,
		&ecb_mode_test_dma,
	};

	/* Define the test suite. */
	DEFINE_TEST_SUITE(aes_suite, aes_tests, "SAM AES driver test suite");

	/* Run all tests in the test suite. */
	test_suite_run(&aes_suite);

	while (1) {
		/* Busy-wait forever. */
	}
}
/**
 * \brief The main function.
 */
int main(void)
{
//! [setup_init]

	/* Initialize the system and console*/
	system_init();
	configure_usart();

//! [setup_dma]
	/* Configure AES DMA and enable callback */
	configure_dma_aes_wr();
	configure_dma_aes_rd();

	dma_register_callback(&example_resource_tx, transfer_tx_rx_done,
			DMA_CALLBACK_TRANSFER_DONE);
	dma_enable_callback(&example_resource_tx, DMA_CALLBACK_TRANSFER_DONE);

	dma_register_callback(&example_resource_rx, transfer_tx_rx_done,
			DMA_CALLBACK_TRANSFER_DONE);
	dma_enable_callback(&example_resource_rx, DMA_CALLBACK_TRANSFER_DONE);
//! [setup_dma]

//! [setup_config]
	aes_get_config_defaults(&g_aes_cfg);
//! [setup_config]
//! [setup_config_defaults]
	aes_init(&aes_instance,AES, &g_aes_cfg);
//! [setup_config_defaults]

//! [module_enable]
	aes_enable(&aes_instance);
//! [module_enable]

//! [setup_init]

//! [encryption_decryption]
	/* ECB mode encryption test with DMA */
	ecb_mode_test_dma();
//! [encryption_decryption]

	while(1) ;

}
Exemple #12
0
int main (void)
{
	system_init();
	//delay_init();
	configure_port_pins();
	configure_usart();
	
	uint8_t string[] = "Testing usart\n";
	
	usart_write_buffer_wait(&usart_instance, string,sizeof(string));
	port_pin_set_output_level(_RESET, 1);
	port_pin_set_output_level(_RESET, 0);
	delay_ms(100);
	port_pin_set_output_level(_RESET, 1);
	delay_ms(1000);
	
	port_pin_set_output_level(PIN_PA02, !false);
	
	uint8_t ndef_data[] = COSY_DEFAULT_DATA;
	
	rf430_init();
	rf430_write_ndef(ndef_data);
	
	port_pin_set_output_level(PIN_PA02, false);

	// Insert application code here, after the board has been initialized.
	
	//example application with cosytech data. (This data should be fetched from web/bluetooth)
	

	
	while(1){
		delay_ms(500);
		port_pin_set_output_level(PIN_PA02, true);
		delay_ms(500);
		port_pin_set_output_level(PIN_PA02, false);
	}
}
Exemple #13
0
/**
 * \brief Application entry point for usart_dmac_serial example.
 *
 * \return Unused (ANSI-C compatibility).
 */
int main(void)
{
	/* Initialize the SAM system. */
	sysclk_init();
	board_init();

	/* Configure UART for debug message output. */
	configure_console();

	/* Output example information. */
	puts(STRING_HEADER);

	/* Configure USART. */
	configure_usart();

	/* Configure DMAC. */
	configure_dmac();

	/* Configure DMAC RX channel. */
	configure_dmac_rx();

	while (1) {
	}
}
Exemple #14
0
int main(void)
{
	
//! [setup_init]
	system_init();

	//延时、串口初始化
	delay_init();
	configure_usart();
	configure_usart1();
	
	//配置输入输出变量
	port_get_config_defaults(&pini);
	port_get_config_defaults(&pinc);
	pinc.direction = PORT_PIN_DIR_OUTPUT;
	
	//延时20s,使MQ2、MQ7充分初始化
	delay_s(20);
	
	//MQ2、MQ7初始化输出模拟电压值
	mq2_init_value = mq_run(MQ2);
	mq7_init_value = mq_run(MQ7);
	
	//对ESP8266进行WIFI配置初始化
	esp_init();
//! [setup_init]

	//port_group_set_output_level(LED_0_PIN, LED_0_ACTIVE);
	
//! [main]
	//定义串口接收变量,用户端口号
	uint16_t temp='H', user = '******';
	
//! [main_loop]
	while (true) {
		if(usart_read_wait(&usart_instance, &temp) == STATUS_OK)
		{
			delay_us(8);
			if(temp == 'D')
			{
				while(usart_write_wait(&usart_inst1, 'T')!=STATUS_OK){}
				if(usart_read_wait(&usart_instance, &temp) == STATUS_OK)
				{
					
					if(temp == ',')
					{
						while(usart_write_wait(&usart_inst1, 'E')!=STATUS_OK){}
						if(usart_read_wait(&usart_instance, &temp) == STATUS_OK)
						{
							user = temp;
							while(usart_write_wait(&usart_inst1, 'S')!=STATUS_OK){}
							while(true)
							{
								if(usart_read_wait(&usart_instance, &temp) == STATUS_OK)
								{
									if(islower(temp))
									{
										while(usart_write_wait(&usart_inst1, 'T')!=STATUS_OK){}
										Do(temp, user);
										break;
									}
								}
							}
						}
					}
				}
			}
		}
	}
	//! [main]
}
Exemple #15
0
/*! \brief Main function. Execution starts here.
 */
int main(void)
{

    uint8_t write_data[PATTERN_TEST_LENGTH];
    uint8_t read_data[PATTERN_TEST_LENGTH];
    uint32_t file_size = 0,remaining_len = 0;;

    struct i2c_master_packet tx_buf = {
        .address     = SLAVE_ADDRESS,
        .data_length = PATTERN_TEST_LENGTH,
        .data        = write_data,
        .ten_bit_address = false,
        .high_speed      = false,
        .hs_master_code  = 0x0,
    };
    struct i2c_master_packet rx_buf = {
        .address     = SLAVE_ADDRESS,
        .data_length = 1,
        .data        = read_data,
        .ten_bit_address = false,
        .high_speed      = false,
        .hs_master_code  = 0x0,
    };
    uint8_t nb_twi_packets_sent;
    uint16_t cdc_rx_size;

    irq_initialize_vectors();
    cpu_irq_enable();

    sleepmgr_init();
    system_init();
    configure_usart();

    ui_init();
    ui_powerdown();

    udc_start();
    printf("Start application\r\n");
    while (true) {

        if (!b_com_port_opened) {
            continue;
        }
        if (b_cdc_data_rx == true) {
            b_cdc_data_rx = false;
            cdc_rx_size = udi_cdc_get_nb_received_data();
            udi_cdc_read_buf((void *)cdc_data, cdc_rx_size);
            if (file_size == 0 && cdc_rx_size == 4) {
                MSB0W(file_size) = cdc_data[0];
                MSB1W(file_size) = cdc_data[1];
                MSB2W(file_size) = cdc_data[2];
                MSB3W(file_size) = cdc_data[3];
                printf("File size :%ld\r\n",file_size);
            }
            remaining_len += cdc_rx_size;

            if (cdc_rx_size == TARGET_PAGE_SIZE/2) {
                if (!b_wait) {
                    memcpy((void *)(write_data), (const void *)cdc_data, cdc_rx_size);
                    b_wait = true;
                    if (file_size + 4 == remaining_len) {
                        tx_buf.data_length = TARGET_PAGE_SIZE/2;
                        while (i2c_master_write_packet_wait(&i2c_master_instance, &tx_buf) != STATUS_OK) ;
                    }
                }
                else {
                    memcpy((void *)(write_data + (TARGET_PAGE_SIZE/2)), (const void *)cdc_data, cdc_rx_size);
                    tx_buf.data_length = TARGET_PAGE_SIZE;
                    while (i2c_master_write_packet_wait(&i2c_master_instance, &tx_buf) != STATUS_OK) ;
                    b_wait = false;
                }

            } else {
                if ((cdc_rx_size) <= PATTERN_TEST_LENGTH) {
                    tx_buf.data_length = cdc_rx_size;
                    memcpy((void *)(write_data), (const void *)cdc_data, cdc_rx_size);
                    while (i2c_master_write_packet_wait(&i2c_master_instance, &tx_buf) != STATUS_OK) ;
                } else {
                    nb_twi_packets_sent = 0;
                    while(cdc_rx_size / PATTERN_TEST_LENGTH) {
                        tx_buf.data_length = PATTERN_TEST_LENGTH;
                        memcpy((void *)(write_data), (const void *)(&cdc_data[(nb_twi_packets_sent++) * (PATTERN_TEST_LENGTH)]), PATTERN_TEST_LENGTH);
                        while (i2c_master_write_packet_wait(&i2c_master_instance, &tx_buf) != STATUS_OK) ;
                        cdc_rx_size -= (PATTERN_TEST_LENGTH);
                    }
                    if(cdc_rx_size) {
                        tx_buf.data_length = cdc_rx_size;
                        memcpy((void *)(write_data), (const void *)(&cdc_data[(nb_twi_packets_sent) * (PATTERN_TEST_LENGTH)]), cdc_rx_size);
                        while (i2c_master_write_packet_wait(&i2c_master_instance, &tx_buf) != STATUS_OK) ;
                        cdc_rx_size = 0;
                    }
                }
            }
        }
        if (i2c_master_read_packet_wait(&i2c_master_instance, &rx_buf) == STATUS_OK) {
            udi_cdc_write_buf((const void *)(rx_buf.data),(iram_size_t)read_data[0]);
            if (file_size + 4 == remaining_len) {
                printf("File transfer successfully, file size:%ld, transefer size :%ld\r\n",file_size,remaining_len-4);
            }
        }

    }
}

void main_suspend_action(void)
{
    ui_powerdown();
}

void main_resume_action(void)
{
    ui_wakeup();
}

void main_sof_action(void)
{
    if (!main_b_cdc_enable)
        return;
    ui_process(udd_get_frame_number());
}

#ifdef USB_DEVICE_LPM_SUPPORT
void main_suspend_lpm_action(void)
{
    ui_powerdown();
}

void main_remotewakeup_lpm_disable(void)
{
    ui_wakeup_disable();
}

void main_remotewakeup_lpm_enable(void)
{
    ui_wakeup_enable();
}
#endif

bool main_cdc_enable(uint8_t port)
{
    main_b_cdc_enable = true;
    configure_i2c_master();
    return true;
}

void main_cdc_disable(uint8_t port)
{
    main_b_cdc_enable = false;
    b_com_port_opened = false;
    i2c_master_disable(&i2c_master_instance);
}
/**
 *  \brief usart_rs485 Application entry point.
 *
 *  Configure USART in RS485 mode. If the application starts earlier, it acts
 *  as a receiver. Otherwise, it should be a transmitter.
 *
 *  \return Unused (ANSI-C compatibility).
 */
int main(void)
{
	uint8_t uc_receive, uc_send = SYNC_CHAR;
	uint32_t time_elapsed = 0;
	uint32_t i;

	/* Initialize the SAM system. */
	sysclk_init();
	board_init();

	/* Configure UART for debug message output. */
	configure_console();

	/* Output example information. */
	puts(STRING_HEADER);

	/* 1ms tick. */
	configure_systick();

	/* Configure USART. */
	configure_usart();

	/* Initialize receiving buffer to distinguish with the sent frame. */
	memset(g_uc_receive_buffer, 0x0, BUFFER_SIZE);

	/*
	 * Enable transmitter here, and disable receiver first, to avoid receiving
	 * characters sent by itself. It's necessary for half duplex RS485.
	 */
	usart_enable_tx(BOARD_USART);
	usart_disable_rx(BOARD_USART);

	/* Enable PDCA module clock */
	pdca_enable(PDCA);

	/* Init PDCA channel with the pdca_options.*/
	pdca_channel_set_config(PDCA_RX_CHANNEL, &PDCA_RX_OPTIONS);
	pdca_channel_set_config(PDCA_TX_CHANNEL, &PDCA_TX_OPTIONS);

	/* Send a sync character XON (0x11). */
	pdca_channel_write_load(PDCA_TX_CHANNEL, &uc_send, 1);
	/* Enable transfer PDCA channel */
	pdca_channel_enable(PDCA_TX_CHANNEL);

	/* Delay until the line is cleared, an estimated time used. */
	wait(50);

	/* Then enable receiver. */
	usart_enable_rx(BOARD_USART);

	/* Read the acknowledgement. */
	pdca_channel_write_load(PDCA_RX_CHANNEL, &uc_receive, 1);
	/* Enable PDCA channel */
	pdca_channel_enable(PDCA_RX_CHANNEL);


	/* Wait until time out or acknowledgement is received. */
	time_elapsed = get_tick_count();
	while (pdca_get_channel_status(PDCA_RX_CHANNEL) !=
			PDCA_CH_TRANSFER_COMPLETED) {
		if (get_tick_count() - time_elapsed > TIMEOUT) {
			break;
		}
	}

	/* If acknowledgement received in a short time. */
	if (pdca_get_channel_status(PDCA_RX_CHANNEL) ==
			PDCA_CH_TRANSFER_COMPLETED) {
		/* Acknowledgement. */
		if (uc_receive == ACK_CHAR) {
			/* Act as transmitter, start transmitting. */
			puts("-I- Act as transmitter.\r");

			g_state = TRANSMITTING;
			puts("-I- Start transmitting!\r");
			pdca_channel_write_load(PDCA_TX_CHANNEL, g_uc_transmit_buffer,
					BUFFER_SIZE);

			/* Enable PDCA interrupt */
			pdca_channel_set_callback(PDCA_TX_CHANNEL, PDCA_TX_Handler,
					PDCA_1_IRQn, 1, PDCA_IER_TRC);

			while (g_state != TRANSMITTED) {
			}
			puts("-I- Transmit done!\r");

			while (1) {
			}
		}
	} else {
		/* Start receiving, act as receiver. */
		puts("-I- Act as receiver.\r");
		puts("-I- Receiving sync character.\r");

		while (pdca_get_channel_status(PDCA_RX_CHANNEL) !=
			PDCA_CH_TRANSFER_COMPLETED) {
		}

		/* Sync character is received. */
		if (uc_receive == SYNC_CHAR) {
			puts("-I- Received sync character.\r");
			/* SEND XOff as acknowledgement. */
			uc_send = ACK_CHAR;

			/*
			 * Delay to prevent the character from being discarded by
			 * transmitter due to responding too soon.
			 */
			wait(100);
			ioport_set_pin_level(RS485_USART_CTS_PIN, 1);
			pdca_channel_write_load(PDCA_TX_CHANNEL, &uc_send, 1);

			g_state = RECEIVING;
			puts("-I- Start receiving buffer!\r");

			pdca_channel_write_load(PDCA_RX_CHANNEL, g_uc_receive_buffer,
					BUFFER_SIZE);

			/* Enable PDCA interrupt */
			pdca_channel_set_callback(PDCA_RX_CHANNEL, PDCA_RX_Handler,
					PDCA_0_IRQn, 1, PDCA_IER_TRC);

			ioport_set_pin_level(RS485_USART_CTS_PIN, 0);

			while (g_state != RECEIVED) {
			}
		}
	}

	i = 0;
	/* Check received frame. */
	while (i < BUFFER_SIZE) {
		if (g_uc_transmit_buffer[i] != g_uc_receive_buffer[i]) {
			puts("-E- Error occurred while receiving!\r");
			/* Infinite loop here. */
			while (1) {
			}
		}

		i++;
	}
	puts("-I- Received buffer successfully!\r");

	while (1) {
	}
}
/**
 * \brief Application entry point.
 *
 * Configure USART in synchronous master/slave mode to start a transmission
 * between two boards.
 * \return Unused.
 */
int main(void)
{
	uint8_t uc_char;

	/* Initialize the SAM system. */
	sysclk_init();
	board_init();

	/* Configure UART for debug message output. */
	configure_console();

	/* Output example information. */
	puts(STRING_HEADER);

	/* Display main menu. */
	display_main_menu();

	/* Configure USART. */
	configure_usart(SYNC_MASTER, g_ul_freq[g_uc_freq_idx]);

	/* Get board USART PDC base address and enable receiver and transmitter. */
	g_p_pdc = usart_get_pdc_base(BOARD_USART);
	pdc_enable_transfer(g_p_pdc, PERIPH_PTCR_RXTEN | PERIPH_PTCR_TXTEN);

	g_uc_transfer_mode = SYNC_MASTER;

	g_uc_state = STATE_WRITE;

	puts("-- USART in MASTER mode --\r");

	while (1) {
		uc_char = 0;
		scanf("%c", (char *)&uc_char);
		switch (uc_char) {
		case '0':
		case '1':
		case '2':
		case '3':
			g_uc_freq_idx = uc_char - '0';
			printf("-- The clock freq is: %luHz.\r\n",
				(unsigned long)g_ul_freq[g_uc_freq_idx]);
			configure_usart(SYNC_MASTER, g_ul_freq[g_uc_freq_idx]);
			break;
		case 'i':
		case 'I':
			if (g_uc_transfer_mode == SYNC_MASTER) {
				printf("-- USART is MASTER at %luHz.\r\n",
					(unsigned long)g_ul_freq[g_uc_freq_idx]);
			} else {
				puts("-- USART is SLAVE \r");
			}
			break;
		case 's':
		case 'S':
			if (g_uc_transfer_mode == SYNC_MASTER) {
				g_uc_transfer_mode = SYNC_SLAVE;
				configure_usart(SYNC_SLAVE, g_ul_freq[g_uc_freq_idx]);
				puts("-- USART in SLAVE mode --\r");
			} else {
				if (g_uc_transfer_mode == SYNC_SLAVE) {
					g_uc_transfer_mode = SYNC_MASTER;
					configure_usart(SYNC_MASTER, g_ul_freq[g_uc_freq_idx]);
					puts("-- USART in MASTER mode --\r");
				}
			}
			break;
		case 'w':
		case 'W':
			g_uc_state = STATE_WRITE;
			g_st_packet.ul_addr = (uint32_t)tran_buff;
			g_st_packet.ul_size = BUFFER_SIZE;
			pdc_tx_init(g_p_pdc, &g_st_packet, NULL);
			usart_enable_interrupt(BOARD_USART, US_IER_TXBUFE);
			while (!g_ul_sent_done) {
			}
			if (g_ul_sent_done) {
				printf("-- %s sent done --\r\n",
						g_uc_transfer_mode ? "MASTER" :
						"SLAVE");
			}
			break;
		case 'r':
		case 'R':
			g_uc_state = STATE_READ;
			if (g_uc_transfer_mode == SYNC_MASTER) {
				puts("----USART MASTER Read----\r");
			} else {
				puts("----USART SLAVE Read----\r");
			}
			g_st_packet.ul_addr = (uint32_t)g_c_recv_buff;
			g_st_packet.ul_size = BUFFER_SIZE;
			pdc_rx_init(g_p_pdc, &g_st_packet, NULL);
			usart_enable_interrupt(BOARD_USART, US_IER_RXBUFF);
			while (!g_ul_recv_done) {
			}
			if (g_ul_recv_done) {
				if (strncmp((char*)g_c_recv_buff, tran_buff, BUFFER_SIZE)) {
					puts(" -F-: Failed!\r");
				} else {
					/* successfully received */
					dump_info((char*)g_c_recv_buff, BUFFER_SIZE);
				}
				puts("----END of read----\r");
				memset(g_c_recv_buff, 0, sizeof(g_c_recv_buff));
				g_ul_recv_done = false;
			}
			break;
		case 'm':
		case 'M':
			display_main_menu();
			break;
		default:
			break;
		}
	}
}
Exemple #18
0
/**
 * \brief Main Application Routine                              \n
 * - Initialize the system clocks                               \n
 * NOTE: The clock should be configured in conf_clock.h         \n
 * - Configure port pins (PA14 and PA16) are used here          \n
 * - Enable Global Interrupt                                    \n
 * - Configure and enable USART                                 \n
 * - Configure and enable ADC                                   \n
 * - Configure and enable DMAC and EVSYS if DMAC mode is chosen \n
 * - Start first ADC conversion                                 \n
 * - Count idle loop count in forever loop                      \n
 */
int main(void)
{

	/* Initialize system clocks */
	system_init();

#if defined(ENABLE_PORT_TOGGLE)
	/* Configure PORT pins PA14 and PA16 are configured here
	 * NOTE: Use oscilloscope to probe the pin.
	 */
	configure_port();
#endif

	/* ENable Global interrupt */
	system_interrupt_enable_global();

	/* Start SysTick Timer */
	systick_init();
 
	/* Configure SERCOM - USART */
	configure_usart();

	/* Configure and enable ADC */
	configure_adc();

	/* Configure and enable EVSYS */
	configure_event();

	/* Configure and enable DMA channel and descriptor */
	configure_dma();

	/* Get the time stamp 1 before starting ADC transfer */
	time_stamp1 = SysTick->VAL;
	

	/*
	 * Trigger first ADC conversion through software.
	 * NOTE: In case of using DMA, further conversions are triggered through
	 * event generated when previous ADC result is transferred to destination 
	 * (can be USART DATA register [or] RAM buffer).
	 * When DMA is not used, further conversions are triggered via software in 
	 * ADC handler after each result ready.
	 */
	adc_start_conversion(&adc_instance);

	while (1){

		#if defined (ENABLE_PORT_TOGGLE)
			/* 	 Use oscilloscope to probe the pin. */
			port_base->OUTTGL.reg = (1UL << PIN_PA16 % 32 );
		#endif
		
		/* Increment idle count whenever application reached while(1) loop */
		idle_loop_count++;

		/*
		 * Check if 1024 bytes transfer is done in either case (I.e. with or without
		 * using DMA.
		 * 'adc_conv_done' flag is set to true in the ADC handler once 
		 * 'adc_sample_count' reaches BLOCK_COUNT.
		 * 'adc_dma_transfer_is_done' is set to true once DMA transfer is done 
		 * in DMA call back for channel zero when 'ADC_DMAC_USART' is chosen.
		 * When choosing ADC_DMAC_MEM_MEM_USART mode, 'adc_dma_transfer_is_done' 
		 * is set to true in DMA channel call back for channel 2.
		 * DMA channel is disabled once reaching BLOCK_COUNT (with DMA cases).
		 * ADC is disabled once reaching BLOBK_COUNT samples (without DMA cases).
		 */
		if (adc_dma_transfer_is_done == true){
			
			/*
			 * Calculate number of cycles taken from the time stamp 
			 * taken before start of the conversion and after 1024 transfer
			 * is completed.
			 * NOTE: This value in relation to the idle_loop_count is 
			 * used in calculating CPU usage.
			 */
			cycles_taken = calculate_cycles_taken(time_stamp1,time_stamp2);

			/* Write the CPU cycles taken on USART */
			usart_write_buffer_wait(&usart_instance, (uint8_t *)&cycles_taken, sizeof(cycles_taken));
			/* Print idle loop count on USART */
			usart_write_buffer_wait(&usart_instance,(uint8_t *)&idle_loop_count, sizeof(idle_loop_count));

			/*
			 * Enter into forever loop as all transfers are completed and 
			 * DMAC/ADC is disabled 
			 */
			while(1);
		}
	}

}//end of main
/**
 * \brief Application entry point.
 *
 * Configure USART in synchronous master/slave mode to start a transmission
 * between two boards.
 * \return Unused.
 */
int main(void)
{
	uint8_t uc_char;
	uint8_t *p_data;

	/* Initialize the SAM system. */
	sysclk_init();
	board_init();

	/* Configure UART for debug message output. */
	configure_console();

	/* Output example information. */
	puts(STRING_HEADER);

	/* Display main menu. */
	display_main_menu();

	/* Configure USART. */
	configure_usart(SYNC_MASTER, g_ul_freq[g_uc_freq_idx]);

	g_uc_transfer_mode = SYNC_MASTER;

	g_uc_state = STATE_WRITE;

	puts("-- USART in MASTER mode --\r");

	while (1) {
		uc_char = 0;
		scanf("%c", (char *)&uc_char);
		switch (uc_char) {
		case '0':
		case '1':
		case '2':
		case '3':
			g_uc_freq_idx = uc_char - '0';
			printf("-- The clock freq is: %luHz.\r\n",
				(unsigned long)g_ul_freq[g_uc_freq_idx]);
			configure_usart(SYNC_MASTER, g_ul_freq[g_uc_freq_idx]);
			break;
		case 'i':
		case 'I':
			if (g_uc_transfer_mode == SYNC_MASTER) {
				printf("-- USART is MASTER at %luHz.\r\n",
					(unsigned long)g_ul_freq[g_uc_freq_idx]);
			} else {
				puts("-- USART is SLAVE \r");
			}
			break;
		case 's':
		case 'S':
			if (g_uc_transfer_mode == SYNC_MASTER) {
				g_uc_transfer_mode = SYNC_SLAVE;
				configure_usart(SYNC_SLAVE, g_ul_freq[g_uc_freq_idx]);
				puts("-- USART in SLAVE mode --\r");
			} else {
				if (g_uc_transfer_mode == SYNC_SLAVE) {
					g_uc_transfer_mode = SYNC_MASTER;
					configure_usart(SYNC_MASTER, g_ul_freq[g_uc_freq_idx]);
					puts("-- USART in MASTER mode --\r");
				}
			}
			break;
		case 'w':
		case 'W':
			g_uc_state = STATE_WRITE;
			p_data = (uint8_t *)&tran_buff[0];
			transmit_mode_sync(p_data, BUFFER_SIZE);

			while (!g_ul_sent_done) {
			}
			if (g_ul_sent_done) {
				printf("-- %s sent done --\r\n",
						g_uc_transfer_mode ? "MASTER" :
						"SLAVE");
			}
			break;
		case 'r':
		case 'R':
			g_uc_state = STATE_READ;
			g_ulcount = 0;
			p_revdata = &g_c_recv_buff[0];
			if (g_uc_transfer_mode == SYNC_MASTER) {
				puts("----USART MASTER Read----\r");
			} else {
				puts("----USART SLAVE Read----\r");
			}

			usart_enable_interrupt(BOARD_USART, US_IER_RXRDY);

			while (!g_ul_recv_done) {
			}
			if (g_ul_recv_done) {
				if (strncmp((char*)g_c_recv_buff, tran_buff, BUFFER_SIZE)) {
					puts(" -F-: Failed!\r");
				} else {
					/* successfully received */
					dump_info((char*)g_c_recv_buff, BUFFER_SIZE);
				}
				puts("----END of read----\r");
				memset(g_c_recv_buff, 0, sizeof(g_c_recv_buff));
				g_ul_recv_done = false;
			}
			break;
		case 'm':
		case 'M':
			display_main_menu();
			break;
		default:
			break;
		}
	}
}
Exemple #20
0
/**
 * \brief Application entry point.
 *
 * Initialize the IrDA and start the main loop.
 *
 * \return Unused (ANSI-C compatibility).
 */
int main(void)
{
	uint8_t uc_char;

	/* Initialize the SAM system. */
	sysclk_init();
	board_init();

	/* Configure UART for debug message output. */
	configure_console();

	/* Output example information. */
	puts(STRING_HEADER);

	/* Initialize board USART. */
	configure_usart();

	/* Get board USART PDC base address and enable receiver and transmitter. */
	g_p_pdc = usart_get_pdc_base(BOARD_USART);
	pdc_enable_transfer(g_p_pdc, PERIPH_PTCR_RXTEN | PERIPH_PTCR_TXTEN);

	puts("-I- Press t to transmit, press r to receive...\r");

	/* Main loop. */
	while (1) {
		uc_char = 0;
		uart_read(CONSOLE_UART, (uint8_t *)&uc_char);

		switch (uc_char) {
		case 't':
		case 'T':
			g_uc_state = STATE_TRANSMIT;
			/* Enable transmitter. */
			func_transmitter();
			g_st_packet.ul_addr = (uint32_t)g_uc_send_data;
			g_st_packet.ul_size = BUFFER_SIZE;
			pdc_tx_init(g_p_pdc, &g_st_packet, NULL);
			usart_enable_interrupt(BOARD_USART, US_IER_TXBUFE);
			while (!g_ul_sent_done) {
			}

			puts("-I- Sent Done!\r");
			g_ul_sent_done = false;
			g_uc_state = STATE_IDLE;
			puts("-I- Press t to transmit, press r to receive...\r");
			break;

		case 'r':
		case 'R':
			g_uc_state = STATE_RECEIVE;

			/* Enable receiver. */
			puts("-I- IrDA receive mode\r");
			func_receiver();
			g_st_packet.ul_addr = (uint32_t)g_uc_recv_data;
			g_st_packet.ul_size = BUFFER_SIZE;
			pdc_rx_init(g_p_pdc, &g_st_packet, NULL);
			usart_enable_interrupt(BOARD_USART, US_IER_RXBUFF);

			while (!g_ul_recv_done) {
			}

			puts("-I- Received Done! \r");
			dump_recv_buf(g_uc_recv_data, BUFFER_SIZE);
			memset(g_uc_recv_data, 0, BUFFER_SIZE);
			g_uc_state = STATE_IDLE;
			g_ul_recv_done = false;
			puts("-I- Press t to transmit, press r to receive...\r");
			break;

		default:
			break;
		}
	}
}
/*!
 * \brief Main application function.                              \n
 * -> Initialize clock                                            \n
 * -> Initialize USART for print functions                        \n
 * -> Initialize AES to generate Key schedule for AES-128         \n
 * -> Based on the AES mode enabled in conf_example.h file,       \n
 *    execute encryption and decryption of a message and          \n
 *    compare them against input data to check its functionality. \n
 * -> The decrypted message can be viewed on the COM port terminal \n
 */
int main(void)
{

	/*
	 * Initialize the System clock 
	 * Note: Clock should be configured in conf_clock.h
	 */
	system_init();

	/* Configure EDBG SERCOM UART to print messages */
	configure_usart();

	/* Generate key schedule for AES-128 from the Cipher Key */
	aes_init(key_vectors); 

	/* Print status messages */
	printf("AES key generated successfully!..\r\n");
	/* Print Input message for user */
	printf("\n The message to be encrypted is:\r\n");
	printf("\n %s \r\n",pText);

	/*
	 * Perform ECB, CFB, OFB, CTR and CBC Encryption and Decryption
	 * based on the mode enabled in conf_example.h. User can choose
	 * the mode that he wants to evaluate. By default, all modes are
	 * enabled.
	 * The decrypted message is printed to EDBG virtual COM port.
	 * If the decrypted message is same as the input plain text,
	 * it ensures the working of each mode.
	 */
#if (AES_ECB == true)
        
     	//Perform ECB Encryption
	ecb_encrypt( pText, cText, sizeof(pText) );
	for (volatile int i = 0; i < 1000; i++);
	//Perform ECB Decryption
	ecb_decrypt( cText, pText1, sizeof(cText));
	//Print decrypted message
	printf("\n Decrypted message using AES-ECB mode : \r\n");
	printf("\n %s \r\n",pText1);

#endif

#if (AES_CFB == true)

	//Perform CFB Encryption
	cfb_encrypt(pText, cText, init_vector, CFB_MODE_128, sizeof(pText));
	for (volatile int i = 0; i < 1000; i++);
	//Perform CFB Decryption
	cfb_decrypt(cText, pText1, init_vector, CFB_MODE_128, sizeof(cText));
	//Print decrypted message
	printf("\n Decrypted message using AES-CFB mode : \r\n");
	printf("\n %s \r\n",pText1);
 
#endif

#if (AES_OFB == true)

	//Perform OFB Encryption
	ofb_encrypt(pText, cText, init_vector, sizeof(pText));
	for (volatile int i = 0; i < 1000; i++);
	//Perform OFB Decryption
	ofb_encrypt(cText, pText1, init_vector, sizeof(cText));
	//Print decrytped message
	printf("\n Decrypted message using AES-OFB mode : \r\n");
	printf("\n %s \r\n",pText1);

#endif

#if (AES_CTR == true)

	/* Initialize Counter block with initialization vector, 
	 * nonce and counter value
	 */
	ctr_blk_t counter_vector = {
		.i_vector = AES_CTR_IVECTOR,
		.nonce = AES_CTR_NONCE,
		.counter = AES_CTR_COUNTER
	};
	//Perform CTR Encryption
	ctr_encrypt_decrypt(pText, cText, &counter_vector, sizeof(pText));
	//Send Counter block value to decryptor
	for (volatile int i = 0; i < 1000; i++);
	counter_vector.i_vector = AES_CTR_IVECTOR;
	counter_vector.nonce = AES_CTR_NONCE;
	counter_vector.counter = AES_CTR_COUNTER;
	//Perform CTR Decryption
	ctr_encrypt_decrypt(cText, pText1, &counter_vector, sizeof(pText1));
	//Print decrypted message
	printf("\n Decrypted message using AES-CTR mode : \r\n");
	printf("\n %s \r\n",pText1);

#endif

/*! \warning CBC mode is done at the last as it process input plain text 
 * during encryption and so the plain text value is not retained.
 * For testing purpose, to preserve the input plan text for testing with
 * other modes, this mode is added at the last.
 */
#if (AES_CBC == true)

	//Perform CBC Encryption
	cbc_encrypt(pText, cText, init_vector, sizeof(pText));
	for (volatile int i = 0; i < 1000; i++);
	//Perform CBC Decryption
	cbc_decrypt(cText, pText1, init_vector, sizeof(cText));
	//Print decrypted message
	printf("\n Decrypted message using AES-CBC mode : \r\n");
	printf("\n %s \r\n",pText1);

#endif

	/* Forever loop */
	while(1);

}
/**
 *  \brief usart_rs485 Application entry point.
 *
 *  Configure USART in RS485 mode. If the application starts earlier, it acts
 *  as a receiver. Otherwise, it should be a transmitter.
 *
 *  \return Unused (ANSI-C compatibility).
 */
int main(void)
{
	static uint8_t uc_sync = SYNC_CHAR;
	uint32_t time_elapsed = 0;
	uint32_t ul_i;

	/* Initialize the SAM system. */
	sysclk_init();
	board_init();

	/* Configure UART for debug message output. */
	configure_console();

	/* Output example information. */
	puts(STRING_HEADER);

	/* Configure USART. */
	configure_usart();

	/* Get board USART PDC base address and enable receiver and transmitter. */
	g_p_pdc = usart_get_pdc_base(BOARD_USART);
	pdc_enable_transfer(g_p_pdc, PERIPH_PTCR_RXTEN | PERIPH_PTCR_TXTEN);

	/* 1ms tick. */
	configure_systick();

	/* Initialize receiving buffer to distinguish with the sent frame. */
	memset(g_uc_receive_buffer, 0x0, BUFFER_SIZE);

	/*
	 * Enable transmitter here, and disable receiver first, to avoid receiving
	 * characters sent by itself. It's necessary for half duplex RS485.
	 */
	usart_enable_tx(BOARD_USART);
	usart_disable_rx(BOARD_USART);

	/* Send a sync character XON (0x11). */
	g_st_packet.ul_addr = (uint32_t)&uc_sync;
	g_st_packet.ul_size = 1;
	pdc_tx_init(g_p_pdc, &g_st_packet, NULL);

	/* Delay until the line is cleared, an estimated time used. */
	wait(50);

	/* Read the acknowledgement. */
	g_st_packet.ul_addr = (uint32_t)&uc_sync;
	g_st_packet.ul_size = 1;
	pdc_rx_init(g_p_pdc, &g_st_packet, NULL);

	/* Then enable receiver. */
	usart_enable_rx(BOARD_USART);

	/* Wait until time out or acknowledgement is received. */
	time_elapsed = get_tick_count();
	while (!usart_is_rx_buf_end(BOARD_USART)) {
		if (get_tick_count() - time_elapsed > TIMEOUT) {
			break;
		}
	}

	/* If acknowledgement received in a short time. */
	if (usart_is_rx_buf_end(BOARD_USART)) {
		/* Acknowledgement. */
		if (uc_sync == ACK_CHAR) {
			/* Act as transmitter, start transmitting. */
			g_state = TRANSMITTING;
			puts("-I- Start transmitting!\r");
			g_st_packet.ul_addr = (uint32_t)g_uc_transmit_buffer;
			g_st_packet.ul_size = PDC_BUF_SIZE;
			pdc_tx_init(g_p_pdc, &g_st_packet, NULL);

			/* Enable transmitting interrupt. */
			usart_enable_interrupt(BOARD_USART, US_IER_ENDTX);
		}
	} else {
		/* Start receiving, act as receiver. */
		g_st_packet.ul_addr = (uint32_t)&uc_sync;
		g_st_packet.ul_size = 1;
		pdc_rx_init(g_p_pdc, &g_st_packet, NULL);
		puts("-I- Receiving sync character.\r");
		while (!usart_is_rx_buf_end(BOARD_USART)) {
		}

		/* Sync character is received. */
		if (uc_sync == SYNC_CHAR) {
			/* SEND XOff as acknowledgement. */
			uc_sync = ACK_CHAR;

			/*
			 * Delay to prevent the character from being discarded by
			 * transmitter due to responding too soon.
			 */
			wait(100);
			pio_set_pin_high(PIN_RE_IDX);
			g_st_packet.ul_addr = (uint32_t)&uc_sync;
			g_st_packet.ul_size = 1;
			pdc_tx_init(g_p_pdc, &g_st_packet, NULL);

			g_state = RECEIVING;
			puts("-I- Start receiving!\r");
			g_st_packet.ul_addr = (uint32_t)g_uc_receive_buffer;
			g_st_packet.ul_size = PDC_BUF_SIZE;
			pdc_rx_init(g_p_pdc, &g_st_packet, NULL);
			pio_set_pin_low(PIN_RE_IDX);
			/* Enable receiving interrupt. */
			usart_enable_interrupt(BOARD_USART, US_IER_ENDRX);
		}
	}
	while (g_state != RECEIVED) {
	}

	ul_i = 0;
	/* Print received frame out. */
	while ((ul_i < BUFFER_SIZE) && (g_uc_receive_buffer[ul_i] != '\0')) {
		if (g_uc_transmit_buffer[ul_i] != g_uc_receive_buffer[ul_i]) {
			puts("-E- Error occurred while receiving!\r");
			/* Infinite loop here. */
			while (1) {
			}
		}
		ul_i++;
	}
	puts("-I- Received successfully!\r");

	while (1) {
	}
}
void usart_i2c_init()
{
    delay_init();
    configure_usart();
    configure_i2c_master();
}
Exemple #24
0
int main (void)
{
	system_init();

	// Initialize local variables used in main
	int failed = 0;
	uint8_t debug_string[24] = "y:     x:     z:   c:   ";
	uint8_t jx, jy;
	char z, c, lz, lc;
	jx = jy = z = c = lz = lc = 0;
	int cycle_index = 0;
	uint8_t cycle = 0;
	uint8_t light_mode = 0; 
	int light_modes = 1;
	uint16_t head, brake = 0;
	uint8_t LIGHTS_ON = 0;
	uint8_t FWD = 0;
	uint8_t HEADLIGHTS = 0;
	uint8_t DEBUG_BLE = 1;
	uint16_t BLE_temp = '0';
	//uint16_t light_sens;

	// Configure Devices
	configure_port_pins();
	configure_LED_PWM();
	configure_usart();
	configure_i2c_slave();
	initIMU();
	failed = !beginIMU();
	configure_i2c_slave_callbacks();
	//configure_ADC();

	// Set the BLE module name to "long-itude"
	uint8_t string[17] = "AT+NAMElong-itude";
	usart_write_buffer_wait(&usart_instance, string, sizeof(string));
	for(int i = 0; i < 50000; ++i);

	// Switch LED direction to FWD
	setFWD();

	while(1)
	{
		//readAccel();
		//readGyro();
		//readMag();
		//light_sens = getLightSens();

		// Read data from BLE
		if (usart_read_wait(&usart_instance, &BLE_temp) == STATUS_OK) {
			switch(BLE_temp)
			{
				case '0':
					LIGHTS_ON = 0;
					break;
				case '1':
					LIGHTS_ON = 1;
					break;
				case '2':
					DEBUG_BLE = 0;
					break;
				case '3':
					DEBUG_BLE = 1;
					break;
				case '4':
					setFWD();
					break;
				case '5':
					setREV();
					break;
			}
		}

		if(DEBUG_BLE)
		{
				uint8_t temp = I2C_slave_read_buffer[0];
				debug_string[4] = '0' + temp%10;
				temp = (temp - temp%10)/10;
				debug_string[3] = '0' + temp%10;
				temp = (temp - temp%10)/10;
				debug_string[2] = '0' + temp%10;
				temp = (temp - temp%10)/10;
				temp = I2C_slave_read_buffer[1];
				debug_string[11] = '0' + temp%10;
				temp = (temp - temp%10)/10;
				debug_string[10] = '0' + temp%10;
				temp = (temp - temp%10)/10;
				debug_string[9] = '0' + temp%10;
				temp = (temp - temp%10)/10;
				debug_string[16] = '0' + I2C_slave_read_buffer[2];
				debug_string[21] = '0' + I2C_slave_read_buffer[3];
				debug_string[22] = '\r';
				debug_string[23] = '\n'; 
				usart_write_buffer_wait(&usart_instance, debug_string, sizeof(debug_string));
		}

		jx = I2C_slave_read_buffer[0];
		jy = I2C_slave_read_buffer[1];
		z = I2C_slave_read_buffer[2];
		c = I2C_slave_read_buffer[3];

		if(z == 0 && lz != 0)
		{
			if(jy > 200)
			{
				setFWD();
				if(HEADLIGHTS && FWD)
					HEADLIGHTS = 0;
				else if(!HEADLIGHTS && FWD)
					HEADLIGHTS = 1;
				else if(HEADLIGHTS && !FWD)
				{
					FWD = 1;
				}
				else if(!HEADLIGHTS && !FWD)
				{
					HEADLIGHTS = 1;
					FWD = 1;
				}
			}
			else if(jy < 55)
			{
				setREV();
				if(HEADLIGHTS && !FWD)
					HEADLIGHTS = 0;
				else if(!HEADLIGHTS && !FWD)
					HEADLIGHTS = 1;
				else if(HEADLIGHTS && FWD)
				{
					FWD = 0;
				}
				else if(!HEADLIGHTS && FWD)
				{
					HEADLIGHTS = 1;
					FWD = 0;
				}
			}
			else if(jy > 110 && jy < 150 && jx > 110 && jx < 150)
			{
				LIGHTS_ON = !LIGHTS_ON;
			}
			else if(jx > 200)
			{
				light_mode++;
				if(light_mode >= light_modes)
					light_mode = 0;
			}
			else if(jx < 55)
			{
				light_mode--;
				if(light_mode <= -1)
					light_mode = light_modes-1;
			}
		}

		if(LIGHTS_ON)
		{
			if(light_mode == 0)
			{
				if(cycle == 0)
				{
					setLeftRGB(cycle_index,0,0xCFFF-cycle_index);
					setRightRGB(cycle_index,0,0xCFFF-cycle_index);
				}
				if(cycle == 1)
				{
					setLeftRGB(0xCFFF-cycle_index,cycle_index,0);
					setRightRGB(0xCFFF-cycle_index,cycle_index,0);
				}
				if(cycle == 2)
				{
					setLeftRGB(0,0xCFFF-cycle_index,cycle_index);
					setRightRGB(0,0xCFFF-cycle_index,cycle_index);
				}

				cycle_index += 250;
				if(cycle_index >= 0xCFFF)
				{
					cycle_index = 0;
					cycle += 1;
					if(cycle == 3)
						cycle = 0;
				}
			}

			if(HEADLIGHTS)
			{
				head = 0xFFFF;
				setWhite(head);
				if(jy < 120)
				{
					brake = (0xFFFF/120)*(120-jy);
					setRed(brake);
				}
				else
					setRed(0);
			}
			else
			{
				setWhite(0);
				setRed(0);
			}
		}
		else
		{
			setWhite(0);
			setRed(0);
			setLeftRGB(0,0,0);
			setRightRGB(0,0,0);
		}

		lc = c;
		lz = z;
	}
}