Beispiel #1
0
/*! \brief Main function. Execution starts here.
 */
int main(void)
{

	irq_initialize_vectors();
	cpu_irq_enable();

	// Initialize the sleep manager
	sleepmgr_init();

#if !SAMD21 && !SAMR21
	sysclk_init();
	board_init();
#else
	system_init();
#endif
	ui_init();
	ui_powerdown();

	// Start USB stack to authorize VBus monitoring
	udc_start();
	// The main loop manages only the power mode
	// because the USB management is done by interrupt
	PORTC_DIR=0b00000011; // USTAWIENIE NA PORCIE C DWÓCH PINÓW WYJŒCIOWYCH
	while (true) {
		char ch;
		
		if (udi_cdc_is_rx_ready())
		{
			
			ch = udi_cdc_getc();
			switch(ch)
			{
				case '0'       : 
								PORTC.OUT=PIN1_bm; 
								_delay_ms(1000);
								udi_cdc_write_buf("START \n\r", 14);
								PORTC.OUTTGL=PIN1_bm;
								
								break;// USTAWIENIE 0 I 1
				case '1'       : 
								PORTC.OUT=PIN0_bm;
								_delay_ms(1000);
								udi_cdc_write_buf("ZAWORY \n\r", 14);
								PORTC.OUTTGL=PIN0_bm; break; // NEGACJA PORTÓW
				
				default        : udi_cdc_write_buf("'S' TO START A 'Z' TO ZMIANA", 26); break;
				
				
				
			};
		}
	}
}
Beispiel #2
0
int mon_data(int argc, char **argv){
  FIL fil;
  power_pkt pkt; 
  FRESULT fr; 
  UINT r;
  if(argc!=2){
    printf("specify [read] or [erase]\n");
    return -1;
  }
  if(strcmp(argv[1],"read")==0){
    //print the data out to STDOUT
    fr = f_open(&fil, DATA_FILE, FA_READ);
    if(fr){
      printf("error reading data: %d\n",(int)fr);
      return -1;
    }
    while(f_read(&fil, &pkt, sizeof(pkt), &r)==0){
      if(r==0) //all done
	break;
      if(r!=sizeof(pkt)){
	core_log("corrupt data buffer");
	break;
      }
      while(!udi_cdc_is_tx_ready()); //wait for buffer
      //data is binary so write directly to the USB buffer
      udi_cdc_write_buf(&pkt,sizeof(pkt));
    }
    f_close(&fil);

    memset(&pkt,'x',sizeof(pkt));  //set up termination string
    while(!udi_cdc_is_tx_ready()); //wait for buffer
    udi_cdc_write_buf(&pkt,sizeof(pkt)); //send it

    return 0;
  }
  else if(strcmp(argv[1],"erase")==0){
    fs_erase_data();
    if(wemo_config.echo)
      printf("erased data\n");
    return 0;
  }
  else{
    printf("specify [read] or [erase]\n");
    return -1;
  }
  //shouldn't get here
  return 0;
}
Beispiel #3
0
void at86rfx_tal_tx_status_cb(uint8_t status)
{
	if (status == AT86RFX_SUCCESS) {
		/* LED indication for successful frame transmission. */
		LED_Toggle(LED1);
		udi_cdc_write_buf((const int *)&tx_buffer[1],
				tx_buffer[0] - FCS_LEN);
		tx_state = TX_IDLE;
	} else if (status == AT86RFX_CHANNEL_ACCESS_FAILURE) {
		/*
		 * Channel access failure is the only transmission failure that is handled
		 * within this application.
		 *
		 * In case of channel access failure the frame is retransmitted
		 */
		at86rfx_tx_frame(tx_buffer);
	} else {
		/*
		 * Other transmission status codes are not handled
		 * within this application.
		 * The transmission is considered as been completed for this frame.
		 */
		tx_state = TX_IDLE;
	}
}
Beispiel #4
0
static void UsbConnectionLost ( void )
{
  SerialPrintStr( "Connection lost on the native USB port." EOL );

  BusPirateConnection_Terminate();
  ResetBuffers();

  // Note that at this point there may still be outgoing data in the USB buffer inside the Atmel Software Framework
  // (or may be that is directly the chip's USB hardware buffer). If the USB cable was not removed, this data
  // will not be lost. I have tested that data written here with udi_cdc_write_buf() gets received twice
  // by the next program that connects to the CDC serial port, which seems strange. I guess the host side (Linux)
  // will buffer up any received data and then deliver it to the next client that connects to CDC the serial port,
  // as long as the USB cable remains put. I could not find any ASF API near udi_cdc_write_buf() in order
  // to discard any outgoing data from the ASF buffer.
  //
  // For the reasons above, I guess that any serial port client on the host side (Linux) should read and
  // discard all stale data upon connect. After all, the Bus Pirate protocol is of the master/slave type,
  // so the client should have nothing to say until the master sends the first command.
  //
  // Enabling the condition below will generate stale data for test purposes, but I am not sure
  // whether this is an ASF bug. After all, any data written after getting the connection lost notification
  // should be automatically discarded.
  //
  // Another way to generate stale data is with this bash command:
  //     printf "help\r">/dev/jtagdue1
  // The next time you connect to /dev/jtagdue1 you will get the help text,
  // you can test it with this bash command:
  //     cat /dev/jtagdue1

  if ( false )
  {
    udi_cdc_write_buf( "stale-test-data", 15 );
  }
}
Beispiel #5
0
static bool SendData ( void )
{
  bool wasAtLeastOneByteTransferred = false;

  for ( ; ; )
  {
    uint32_t availableByteCount;
    const uint8_t * const readPtr = s_usbTxBuffer.GetReadPtr( &availableByteCount );

    if ( availableByteCount == 0 )
      break;

    const uint32_t remainingCount = udi_cdc_write_buf( readPtr, availableByteCount );

    assert( remainingCount <= availableByteCount );

    const uint32_t writtenCount = availableByteCount - remainingCount;

    if ( writtenCount == 0 )
    {
      break;
    }

    if ( false )
    {
      SerialPrintStr( "Data sent:" EOL );
      SerialPrintHexDump( readPtr, writtenCount, EOL );
    }

    s_usbTxBuffer.ConsumeReadElements( writtenCount );
    wasAtLeastOneByteTransferred = true;
  }

  return wasAtLeastOneByteTransferred;
}
Beispiel #6
0
void at86rfx_tal_rx_frame_cb(uint8_t * rx_frame_array)
{
	/* Frame received from transceiver is sent to terminal through USB. */
	udi_cdc_write_buf((const int *)&rx_frame_array[1],
			rx_frame_array[0] - FCS_LEN);
	/* LED indication for frame reception. */
	LED_Toggle(LED0);
}
Beispiel #7
0
uint8_t sio2host_tx(uint8_t *data, uint8_t length)
{
    uint16_t bytes_transmitted, bytes_remaining;
    bytes_remaining = udi_cdc_write_buf((const int *)data, length);

    bytes_transmitted = length - bytes_remaining;
    return(bytes_transmitted);
}
Beispiel #8
0
void cdc_rx_notify(uint8_t port) {
  l("cdc_rx_notify [%d]", port);

  uint8_t b = udi_cdc_getc();
  if (b != 0x08) {
    l("Protocol desync");
  }
  l("First byte ok");
  uint32_t offset = 0;
  do {
    buffer[offset++] = b;
    b = udi_cdc_getc();
    l("-> 0x%02x", b);
  } while (b & 0x80);
  buffer[offset++] = b;
  // Now we have enough to know the size
  l("Length read, decoding...");
  l("... 0x%02x 0x%02x", buffer[0], buffer[1]);

  pb_istream_t istream = pb_istream_from_buffer(buffer + 1, USB_BUFFER_SIZE);
  l("istream bytes_left before %d", istream.bytes_left);
  uint64_t len = 0;
  pb_decode_varint(&istream, &len);
  l("message_length %d", (uint32_t)len);
  l("offset %d", offset);
  udi_cdc_read_buf(buffer + offset, len);
  l("decode message");
  istream = pb_istream_from_buffer(buffer + offset, len);
  DonglePiRequest request = {0};
  request.config.i2c.funcs.decode = handle_i2c_config_cb;
  request.config.uart.funcs.decode = handle_uart_config_cb;
  request.config.spi.funcs.decode = handle_spi_config_cb;
  request.config.gpio.pins.funcs.decode = handle_gpio_pin_config_cb;
  request.data.i2c.writes.funcs.decode = handle_i2c_write_cb;

  if (!pb_decode(&istream, DonglePiRequest_fields, &request)) {
    l("failed to decode the packet, wait for more data");
    return;
  }

  l("Request #%d received", request.message_nb);

  if (request.has_data && request.data.has_gpio) {
    handle_gpio_write(request.data.gpio);
  }

  pb_ostream_t ostream = pb_ostream_from_buffer(buffer, USB_BUFFER_SIZE);
  DonglePiResponse response = {};
  response.message_nb = request.message_nb;
  l("Create response for #%d", response.message_nb);

  handle_gpio_read(&response);

  pb_encode_delimited(&ostream, DonglePiResponse_fields, &response);
  l("Write response nb_bytes = %d", ostream.bytes_written);
  uint32_t wrote = udi_cdc_write_buf(buffer, ostream.bytes_written);
  l("Done. wrote %d bytes", wrote);
}
Beispiel #9
0
// Non-blocking write to USB. Returns number of bytes written. If we are not connected, pretend that all bytes hacve been written.
size_t SerialCDC::write(const uint8_t *buffer, size_t size)
{
	if (isConnected && size != 0)
	{
		const size_t remaining = udi_cdc_write_buf(buffer, size);
		return size - remaining;
	}
	return size;
}
Beispiel #10
0
void sendDataToUsb(const char *data, bool checkBufferSize) {

	// Check if data can be sent
	uint8_t length = strlen(data);
	if(!checkBufferSize || udi_cdc_get_free_tx_buffer() >= length)
	
		// Send data
		udi_cdc_write_buf(data, length);
}
Beispiel #11
0
int usb_serial_write( const void *buffer, uint32_t size )
{
	// Make sure CDC is enabled
	if ( !usb_serial_enabled )
	{
		return -1;
	}

	// TODO (HaaTa): Possibly queue up buffer rather than stall?
	while ( udi_cdc_write_buf( buffer, size ) > 0 );

	return 0;
}
Beispiel #12
0
void cdc_cli_output(const uint8_t const *message_string)
{
	if (cdc_connected == true) {
		/* This call is outside of the CLI task, so ensure mutually exclusive
		access is obtained. */
		xSemaphoreTake(access_mutex, portMAX_DELAY);

		udi_cdc_write_buf((void *) message_string,
				strlen((char *) message_string));

		/* Finished writing to the CDC. */
		xSemaphoreGive(access_mutex);
	}
}
Beispiel #13
0
void core_putc(void* stream, char c){
  //only wait 2 systicks and then give up
  static uint8_t locked = false; //prevent re-entrant
  int prev_tick; //prevent deadlock when USB isn't connected (bad disconnect)
  if(locked)
    return; //udc spinlock is not re-entrant
  prev_tick = sys_tick; //grab the current sys_tick
  if(b_usb_enabled && wemo_config.echo){
    locked = true; //enter spin
    while(!udi_cdc_is_tx_ready()){
      if(sys_tick>(prev_tick+1)){ //check if we timeout
	core_usb_enable(0,false); //turn off USB
	locked = false;
	return;
      }
    }
    //success, got USB TX ready signal
    udi_cdc_write_buf(&c,1);
    locked = false; //release
  }
}
Beispiel #14
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);
}
Beispiel #15
0
static void usb_cdc_command_console_task(void *pvParameters)
{
	uint8_t received_char, input_index = 0, *output_string;
	static int8_t input_string[MAX_INPUT_SIZE],
			last_input_string[MAX_INPUT_SIZE];
	portBASE_TYPE returned_value;

	/* Just to remove compiler warnings. */
	(void) pvParameters;

	udc_start();

	if (udc_include_vbus_monitoring() == false) {
		/* VBUS monitoring is not available on this product.  Assume VBUS is
		present. */
		cli_vbus_event(true);
	}

	/* Obtain the address of the output buffer.  Note there is no mutual
	exclusion on this buffer as it is assumed only one command console
	interface will be used at any one time. */
	output_string = (uint8_t *) FreeRTOS_CLIGetOutputBuffer();

	for (;;) {
		/* Wait for new data. */
		xSemaphoreTake(cdc_new_data_semaphore, portMAX_DELAY);

		/* Ensure mutually exclusive access is obtained as other tasks can write
		to the CLI. */
		xSemaphoreTake(access_mutex, portMAX_DELAY);

		/* While there are input characters. */
		while (udi_cdc_is_rx_ready() == true) {
			received_char = (uint8_t) udi_cdc_getc();

			/* Echo the character. */
			udi_cdc_putc(received_char);

			if (received_char == '\r') {
				/* Transmit a line separator, just to make the output easier to
				read. */
				udi_cdc_write_buf((void *) new_line,
						strlen((char *) new_line));

				/* See if the command is empty, indicating that the last command
				is to be executed again. */
				if (input_index == 0) {
					strcpy((char *) input_string,
							(char *) last_input_string);
				}

				/* Pass the received command to the command interpreter.  The
				command interpreter is called repeatedly until it returns pdFALSE as
				it might generate more than one string. */
				do {
					/* Get the string to write to the UART from the command
					interpreter. */
					returned_value = FreeRTOS_CLIProcessCommand(
							input_string,
							(int8_t *) output_string,
							configCOMMAND_INT_MAX_OUTPUT_SIZE);

					/* Transmit the generated string. */
					udi_cdc_write_buf((void *) output_string, strlen(
							(char *) output_string));
				} while (returned_value != pdFALSE);

				/* All the strings generated by the input command have been sent.
				Clear the input	string ready to receive the next command.
				Remember the command that was just processed first in case it is
				to be processed again. */
				strcpy((char *) last_input_string,
						(char *) input_string);
				input_index = 0;
				memset(input_string, 0x00, MAX_INPUT_SIZE);

				/* Start to transmit a line separator, just to make the output
				easier to read. */
				udi_cdc_write_buf((void *) line_separator, strlen(
						(char *) line_separator));
			} else {
				if (received_char == '\n') {
					/* Ignore the character. */
				} else if (received_char == '\b') {
					/* Backspace was pressed.  Erase the last character in the
					string - if any. */
					if (input_index > 0) {
						input_index--;
						input_string[input_index]
							= '\0';
					}
				} else {
					/* A character was entered.  Add it to the string
					entered so far.  When a \n is entered the complete
					string will be passed to the command interpreter. */
					if (input_index < MAX_INPUT_SIZE) {
						input_string[input_index] = received_char;
						input_index++;
					}
				}
			}
		}

		/* Finished with the CDC port, return the mutex until more characters
		arrive. */
		xSemaphoreGive(access_mutex);
	}
}