/* GECKO3COM
|
/* GECKO3COM
|
*
|
*
|
* Copyright (C) 2008 by
|
* Copyright (C) 2008 by
|
* ___ ____ _ _
|
* ___ ____ _ _
|
* ( _`\ ( __)( ) ( )
|
* ( _`\ ( __)( ) ( )
|
* | (_) )| (_ | |_| | Bern University of Applied Sciences
|
* | (_) )| (_ | |_| | Bern University of Applied Sciences
|
* | _ <'| _) | _ | School of Engineering and
|
* | _ <'| _) | _ | School of Engineering and
|
* | (_) )| | | | | | Information Technology
|
* | (_) )| | | | | | Information Technology
|
* (____/'(_) (_) (_)
|
* (____/'(_) (_) (_)
|
*
|
*
|
*
|
*
|
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
* the Free Software Foundation, either version 3 of the License, or
|
* the Free Software Foundation, either version 3 of the License, or
|
* (at your option) any later version.
|
* (at your option) any later version.
|
*
|
*
|
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
*/
|
*/
|
|
|
/*********************************************************************/
|
/*********************************************************************/
|
/** \file gecko3com_main.c
|
/** \file gecko3com_main.c
|
*********************************************************************
|
*********************************************************************
|
* \brief main file for the GECKO3COM project
|
* \brief main file for the GECKO3COM project
|
*
|
*
|
* \author Christoph Zimmermann bfh.ch
|
* \author Christoph Zimmermann bfh.ch
|
* \date 2009-1-22
|
* \date 2009-1-22
|
*
|
*
|
*/
|
*/
|
|
|
/** enable DFU class support */
|
/** enable DFU class support */
|
#define USB_DFU_SUPPORT
|
#define USB_DFU_SUPPORT
|
|
|
#include <string.h>
|
#include <string.h>
|
#include <stdint.h>
|
#include <stdint.h>
|
|
|
#include "fx2utils.h"
|
#include "fx2utils.h"
|
#include "timer.h"
|
#include "timer.h"
|
#include "spi_flash.h"
|
#include "spi_flash.h"
|
#include "i2c.h"
|
#include "i2c.h"
|
#include "isr.h"
|
#include "isr.h"
|
#include "eeprom_io.h"
|
#include "eeprom_io.h"
|
#include "delay.h"
|
#include "delay.h"
|
|
|
#include "gecko3com_i2c.h"
|
#include "gecko3com_i2c.h"
|
#include "gecko3com_spi.h"
|
#include "gecko3com_spi.h"
|
#include "gecko3com_common.h"
|
#include "gecko3com_common.h"
|
#include "gecko3com_interfaces.h"
|
#include "gecko3com_interfaces.h"
|
#include "gecko3com_commands.h"
|
#include "gecko3com_commands.h"
|
#include "fpga_load.h"
|
#include "fpga_load.h"
|
|
|
#include "gecko3com_gpif.h"
|
#include "gecko3com_gpif.h"
|
#include "gpif_data.h"
|
#include "gpif_data.h"
|
|
|
#include "usb_common.h"
|
#include "usb_common.h"
|
#include "usb_requests.h"
|
#include "usb_requests.h"
|
#include "usb_descriptors.h"
|
#include "usb_descriptors.h"
|
#include "usb_dfu.h"
|
#include "usb_dfu.h"
|
#include "usb_tmc.h"
|
#include "usb_tmc.h"
|
#include "scpi_parser.h"
|
#include "scpi_parser.h"
|
|
|
#include "firmware_version.h"
|
#include "firmware_version.h"
|
#include "debugprint.h"
|
#include "debugprint.h"
|
#ifdef DEBUG_LEVEL_ERROR
|
#ifdef DEBUG_LEVEL_ERROR
|
#include "ser.h"
|
#include "ser.h"
|
#endif
|
#endif
|
|
|
/* -------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------- */
|
|
|
#define WATCHDOG_TIME 100 /**< time until the watchdog times out, 100 equals 1 second */
|
#define WATCHDOG_TIME 100 /**< time until the watchdog times out, 100 equals 1 second */
|
|
|
/* Global variables */
|
/* Global variables */
|
volatile uint8_t watchdog_count = WATCHDOG_TIME;
|
volatile uint8_t watchdog_count = WATCHDOG_TIME;
|
idata int32_t file_size;
|
idata int32_t file_size;
|
xdata uint32_t flash_adress;
|
xdata uint32_t flash_adress;
|
xdata unsigned char *buffer;
|
xdata unsigned char *buffer;
|
xdata TMC_Response_Queue response_queue;
|
xdata TMC_Response_Queue response_queue;
|
|
|
|
|
/** \brief with executing this function, we confirm that we handled the
|
/** \brief with executing this function, we confirm that we handled the
|
* endpoint 0 data and that we are ready to get new data (rearm the endpoint).
|
* endpoint 0 data and that we are ready to get new data (rearm the endpoint).
|
*/
|
*/
|
static void get_ep0_data (void)
|
static void get_ep0_data (void)
|
{
|
{
|
EP0BCL = 0; /* arm EP0 for OUT xfer. This sets the busy bit */
|
EP0BCL = 0; /* arm EP0 for OUT xfer. This sets the busy bit */
|
|
|
while (EP0CS & bmEPBUSY) /* wait for busy to clear */
|
while (EP0CS & bmEPBUSY) /* wait for busy to clear */
|
;
|
;
|
}
|
}
|
|
|
|
|
/*
|
/*
|
* enable debug output through the serial uart
|
* enable debug output through the serial uart
|
*/
|
*/
|
#ifdef DEBUG_LEVEL_ERROR
|
#ifdef DEBUG_LEVEL_ERROR
|
|
|
/** \brief simple wraper to provide putchar function over serial line */
|
/** \brief simple wraper to provide putchar function over serial line */
|
void putchar (char p)
|
void putchar (char p)
|
{
|
{
|
ser_putc((unsigned char) p);
|
ser_putc((unsigned char) p);
|
}
|
}
|
|
|
|
|
/** \brief simple wraper to provide getchar function over serial line */
|
/** \brief simple wraper to provide getchar function over serial line */
|
char getchar (void)
|
char getchar (void)
|
{
|
{
|
return (char) ser_getc();
|
return (char) ser_getc();
|
}
|
}
|
#endif
|
#endif
|
|
|
|
|
#ifdef USB_DFU_SUPPORT
|
#ifdef USB_DFU_SUPPORT
|
/** \brief this function writes the new firmware data in endpoint 0 to the I2C eeprom
|
/** \brief this function writes the new firmware data in endpoint 0 to the I2C eeprom
|
* \note this function is only available when the DFU (device firware upgrade) class
|
* \note this function is only available when the DFU (device firware upgrade) class
|
* support is enabled.
|
* support is enabled.
|
*/
|
*/
|
uint8_t app_firmware_write (void)
|
uint8_t app_firmware_write (void)
|
{
|
{
|
static uint16_t eeprom_offset;
|
static uint16_t eeprom_offset;
|
|
|
get_ep0_data();
|
get_ep0_data();
|
|
|
if(usb_dfu_state == DFU_STATE_dfuIDLE){
|
if(usb_dfu_state == DFU_STATE_dfuIDLE){
|
eeprom_offset = 0;
|
eeprom_offset = 0;
|
}
|
}
|
|
|
// if(!eeprom_write(I2C_ADDR_BOOT, eeprom_offset, EP0BUF, wLengthL)){
|
// if(!eeprom_write(I2C_ADDR_BOOT, eeprom_offset, EP0BUF, wLengthL)){
|
if(!eeprom_write(eeprom_offset, EP0BUF, wLengthL)){
|
if(!eeprom_write(eeprom_offset, EP0BUF, wLengthL)){
|
usb_dfu_status = DFU_STATUS_errWRITE;
|
usb_dfu_status = DFU_STATUS_errWRITE;
|
return 0;
|
return 0;
|
}
|
}
|
|
|
eeprom_offset += wLengthL;
|
eeprom_offset += wLengthL;
|
return 1;
|
return 1;
|
}
|
}
|
#endif
|
#endif
|
|
|
|
|
/** \brief analyze the header from the fpga configuration file and compares the
|
/** \brief analyze the header from the fpga configuration file and compares the
|
* the fpga type with the on board fpga and returs the configuration file size.
|
* the fpga type with the on board fpga and returs the configuration file size.
|
*
|
*
|
* \param[in] xdata unsigned char *buffer pointer to the buffer to read from
|
* \param[in] xdata unsigned char *buffer pointer to the buffer to read from
|
* (normally endpoint buffer)
|
* (normally endpoint buffer)
|
* \param[in] idata uint16_t *offset pointer to the offset, buffer[offset]
|
* \param[in] idata uint16_t *offset pointer to the offset, buffer[offset]
|
* is the current position, anything before this is already consumed.
|
* is the current position, anything before this is already consumed.
|
* \param[in] idata uint16_t *byte_count pointer to the length of the whole
|
* \param[in] idata uint16_t *byte_count pointer to the length of the whole
|
* buffer.
|
* buffer.
|
* \return returns non-zero if successful, else 0
|
* \return returns non-zero if successful, else 0
|
*/
|
*/
|
uint8_t app_check_fpga_type_from_header(/*xdata unsigned char *buffer,*/ \
|
uint8_t app_check_fpga_type_from_header(/*xdata unsigned char *buffer,*/ \
|
idata uint16_t *offset, \
|
idata uint16_t *offset, \
|
idata uint16_t *byte_count)
|
idata uint16_t *byte_count)
|
{
|
{
|
static xdata Fpga_Info fpga_file_header;
|
static xdata Fpga_Info fpga_file_header;
|
xdata char fpga_type[FPGA_TYPE_LEN];
|
xdata char fpga_type[FPGA_TYPE_LEN];
|
static int8_t continue_analyse;
|
static int8_t continue_analyse;
|
|
|
/* check if this is the first attempt to analyse the bit file header*/
|
/* check if this is the first attempt to analyse the bit file header*/
|
if(usb_tmc_transfer.new_transfer == NEWTRANSFER) {
|
if(usb_tmc_transfer.new_transfer == NEWTRANSFER) {
|
continue_analyse = 0;
|
continue_analyse = 0;
|
fpga_file_header.type = FPGA_TYPE;
|
fpga_file_header.type = FPGA_TYPE;
|
}
|
}
|
|
|
/* first value to read from the header file is the fpga type */
|
/* first value to read from the header file is the fpga type */
|
if(fpga_file_header.type == FPGA_TYPE){
|
if(fpga_file_header.type == FPGA_TYPE){
|
if(fpga_scan_file(buffer, offset, byte_count, &fpga_file_header) \
|
if(fpga_scan_file(buffer, offset, byte_count, &fpga_file_header) \
|
== FPGA_INFO_COMPLETE) {
|
== FPGA_INFO_COMPLETE) {
|
|
|
/* compare fpga type from header with value in eeprom */
|
/* compare fpga type from header with value in eeprom */
|
if(!eeprom_read(FPGA_TYPE_OFFSET, fpga_type, FPGA_TYPE_LEN)){
|
if(!eeprom_read(FPGA_TYPE_OFFSET, fpga_type, FPGA_TYPE_LEN)){
|
return 0;
|
return 0;
|
}
|
}
|
|
|
if(strncmp(fpga_file_header.info, fpga_type, FPGA_TYPE_LEN)) {
|
if(strncmp(fpga_file_header.info, fpga_type, FPGA_TYPE_LEN)) {
|
//print_err("!FPGA\n");
|
//print_err("!FPGA\n");
|
return 0;
|
return 0;
|
}
|
}
|
|
|
/* next value to read from the header is the file length */
|
/* next value to read from the header is the file length */
|
fpga_file_header.type = FILE_LENGTH;
|
fpga_file_header.type = FILE_LENGTH;
|
continue_analyse = FPGA_INFO_COMPLETE;
|
continue_analyse = FPGA_INFO_COMPLETE;
|
}
|
}
|
else {
|
else {
|
continue_analyse = FPGA_INFO_NOT_COMPLETE;
|
continue_analyse = FPGA_INFO_NOT_COMPLETE;
|
}
|
}
|
}
|
}
|
|
|
/* second value to read from the header file is the file length */
|
/* second value to read from the header file is the file length */
|
if(fpga_file_header.type == FILE_LENGTH){
|
if(fpga_file_header.type == FILE_LENGTH){
|
if(fpga_scan_file(buffer, offset, byte_count, &fpga_file_header) \
|
if(fpga_scan_file(buffer, offset, byte_count, &fpga_file_header) \
|
== FPGA_INFO_COMPLETE) {
|
== FPGA_INFO_COMPLETE) {
|
|
|
((uint8_t*)&file_size)[0] = fpga_file_header.info[2];
|
((uint8_t*)&file_size)[0] = fpga_file_header.info[2];
|
((uint8_t*)&file_size)[1] = fpga_file_header.info[1];
|
((uint8_t*)&file_size)[1] = fpga_file_header.info[1];
|
((uint8_t*)&file_size)[2] = fpga_file_header.info[0];
|
((uint8_t*)&file_size)[2] = fpga_file_header.info[0];
|
((uint8_t*)&file_size)[3] = 0;
|
((uint8_t*)&file_size)[3] = 0;
|
|
|
continue_analyse = FPGA_INFO_COMPLETE;
|
continue_analyse = FPGA_INFO_COMPLETE;
|
}
|
}
|
else {
|
else {
|
continue_analyse = FPGA_INFO_NOT_COMPLETE;
|
continue_analyse = FPGA_INFO_NOT_COMPLETE;
|
}
|
}
|
}
|
}
|
|
|
/* adjust the offset and byte_count variables to point to the
|
/* adjust the offset and byte_count variables to point to the
|
* binary data after the header */
|
* binary data after the header */
|
usb_tmc_transfer.transfer_size += USB_TMC_HEADER_SIZE;
|
usb_tmc_transfer.transfer_size += USB_TMC_HEADER_SIZE;
|
usb_tmc_transfer.transfer_size -= *offset;
|
usb_tmc_transfer.transfer_size -= *offset;
|
|
|
return continue_analyse;
|
return continue_analyse;
|
}
|
}
|
|
|
|
|
/** \brief function to configure an fpga with data from usb
|
/** \brief function to configure an fpga with data from usb
|
*
|
*
|
* \param[in] xdata unsigned char *buffer pointer to the buffer to read from
|
* \param[in] xdata unsigned char *buffer pointer to the buffer to read from
|
* (normally endpoint buffer)
|
* (normally endpoint buffer)
|
* \param[in] idata uint16_t *offset pointer to the offset, buffer[offset]
|
* \param[in] idata uint16_t *offset pointer to the offset, buffer[offset]
|
* is the current position, anything before this is already consumed.
|
* is the current position, anything before this is already consumed.
|
* \param[in] idata uint16_t *byte_count pointer to the length of the whole
|
* \param[in] idata uint16_t *byte_count pointer to the length of the whole
|
* buffer.
|
* buffer.
|
* \return returns non-zero if successful, else 0
|
* \return returns non-zero if successful, else 0
|
*/
|
*/
|
uint8_t app_configure_fpga(/* xdata unsigned char *buffer,*/ \
|
uint8_t app_configure_fpga(/* xdata unsigned char *buffer,*/ \
|
idata uint16_t *offset, \
|
idata uint16_t *offset, \
|
idata uint16_t *byte_count)
|
idata uint16_t *byte_count)
|
{
|
{
|
/* Is this the first part of configuration? */
|
/* Is this the first part of configuration? */
|
if(usb_tmc_transfer.nbytes_rxd == 0) {
|
if(usb_tmc_transfer.nbytes_rxd == 0) {
|
/* setup all stuff */
|
/* setup all stuff */
|
file_size = 0;
|
file_size = 0;
|
}
|
}
|
|
|
/* do we still analyze the file header? */
|
/* do we still analyze the file header? */
|
if(file_size == 0) {
|
if(file_size == 0) {
|
if(!app_check_fpga_type_from_header(offset, byte_count)) {
|
if(!app_check_fpga_type_from_header(offset, byte_count)) {
|
return 0;
|
return 0;
|
}
|
}
|
|
|
/* are we now finished analyzing? */
|
/* are we now finished analyzing? */
|
if(file_size != 0) {
|
if(file_size != 0) {
|
/* if yes, intialize fpga for configuration */
|
/* if yes, intialize fpga for configuration */
|
//print_info("begin\n");
|
//print_info("begin\n");
|
fpga_load_begin();
|
fpga_load_begin();
|
}
|
}
|
}
|
}
|
|
|
/* anything ready, transfer data to fpga */
|
/* anything ready, transfer data to fpga */
|
if(file_size != 0) {
|
if(file_size != 0) {
|
/* transmitt config data to fpga */
|
/* transmitt config data to fpga */
|
usb_tmc_transfer.transfer_size -= *byte_count;
|
usb_tmc_transfer.transfer_size -= *byte_count;
|
usb_tmc_transfer.transfer_size += *offset;
|
usb_tmc_transfer.transfer_size += *offset;
|
file_size -= *byte_count;
|
file_size -= *byte_count;
|
file_size += *offset;
|
file_size += *offset;
|
|
|
fpga_load_xfer(buffer, offset, byte_count);
|
fpga_load_xfer(buffer, offset, byte_count);
|
|
|
//printf_tiny("buffer[0], %d\n",buffer[0]);
|
//printf_tiny("buffer[0], %d\n",buffer[0]);
|
|
|
/* transfer finished, finishing configuration */
|
/* transfer finished, finishing configuration */
|
if(file_size == 0) {
|
if(file_size == 0) {
|
if(!fpga_load_end()) {
|
if(!fpga_load_end()) {
|
return 0;
|
return 0;
|
}
|
}
|
usb_tmc_state = TMC_STATE_IDLE;
|
usb_tmc_state = TMC_STATE_IDLE;
|
}
|
}
|
}
|
}
|
|
|
return 1;
|
return 1;
|
}
|
}
|
|
|
|
|
/** \brief function to write an fpga configuration from usb to the spi flash
|
/** \brief function to write an fpga configuration from usb to the spi flash
|
*
|
*
|
* \detail The SPI flash is big enough to hold store two different fpga
|
* \detail The SPI flash is big enough to hold store two different fpga
|
* configuration files. To handle this, we split the SPI flash address
|
* configuration files. To handle this, we split the SPI flash address
|
* space simply at the half. \n
|
* space simply at the half. \n
|
* The data structure in the SPI flash is really simple:\n
|
* The data structure in the SPI flash is really simple:\n
|
* \li 32bit file size value (little endian, as used by the fx2)
|
* \li 32bit file size value (little endian, as used by the fx2)
|
* \li binary data from the fpga configuration file
|
* \li binary data from the fpga configuration file
|
*
|
*
|
* \param[in] xdata unsigned char *buffer pointer to the buffer to read from
|
* \param[in] xdata unsigned char *buffer pointer to the buffer to read from
|
* (normally endpoint buffer)
|
* (normally endpoint buffer)
|
* \param[in] idata uint16_t *offset pointer to the offset, buffer[offset]
|
* \param[in] idata uint16_t *offset pointer to the offset, buffer[offset]
|
* is the current position, anything before this is already consumed.
|
* is the current position, anything before this is already consumed.
|
* \param[in] idata uint16_t *byte_count pointer to the length of the whole
|
* \param[in] idata uint16_t *byte_count pointer to the length of the whole
|
* buffer.
|
* buffer.
|
* \return returns non-zero if successful, else 0
|
* \return returns non-zero if successful, else 0
|
*/
|
*/
|
uint8_t app_write_conf_to_flash(/* xdata unsigned char *buffer,*/ \
|
uint8_t app_write_conf_to_flash(/* xdata unsigned char *buffer,*/ \
|
idata uint16_t *offset, \
|
idata uint16_t *offset, \
|
idata uint16_t *byte_count)
|
idata uint16_t *byte_count)
|
{
|
{
|
idata uint16_t length;
|
idata uint16_t length;
|
xdata uint32_t local_uint32_var;
|
xdata uint32_t local_uint32_var;
|
xdata unsigned char *local_buffer_ptr;
|
xdata unsigned char *local_buffer_ptr;
|
|
|
/* Is this the first part of configuration? */
|
/* Is this the first part of configuration? */
|
if(usb_tmc_transfer.nbytes_rxd == 0) {
|
if(usb_tmc_transfer.nbytes_rxd == 0) {
|
/* setup all stuff */
|
/* setup all stuff */
|
file_size = 0;
|
file_size = 0;
|
|
|
/* select which file slot we have to delete */
|
/* select which file slot we have to delete */
|
if(buffer[*offset] == '0') {
|
if(buffer[*offset] == '0') {
|
flash_adress = START_ADRESS_SLOT0;
|
flash_adress = start_adress_slot0(flash_dr);
|
}
|
}
|
else if(buffer[*offset] == '1'){
|
else if(buffer[*offset] == '1'){
|
flash_adress = START_ADRESS_SLOT1;
|
flash_adress = start_adress_slot1(flash_dr);
|
}
|
}
|
else {
|
else {
|
//print_err("slot\n");
|
//print_err("slot\n");
|
ieee488_status.EventStatusRegister |= bmCOMMAND_ERROR;
|
ieee488_status.EventStatusRegister |= bmCOMMAND_ERROR;
|
usb_tmc_state = TMC_STATE_IDLE;
|
usb_tmc_state = TMC_STATE_IDLE;
|
return 0;
|
return 0;
|
}
|
}
|
|
|
*offset += 2;
|
*offset += 2;
|
}
|
}
|
|
|
/* do we still analyze the file header? */
|
/* do we still analyze the file header? */
|
if(file_size == 0) {
|
if(file_size == 0) {
|
if(!app_check_fpga_type_from_header(offset, byte_count)) {
|
if(!app_check_fpga_type_from_header(offset, byte_count)) {
|
return 0;
|
return 0;
|
}
|
}
|
|
|
/* are we now finished analyzing? */
|
/* are we now finished analyzing? */
|
if(file_size != 0) {
|
if(file_size != 0) {
|
/* if yes, write file size information to the SPI flash */
|
/* if yes, write file size information to the SPI flash */
|
//print_info("begin\n");
|
//print_info("begin\n");
|
spiflash_erase(&flash_dr, &flash_adress);
|
spiflash_erase(&flash_dr, &flash_adress);
|
local_uint32_var = file_size;
|
local_uint32_var = file_size;
|
spiflash_write(&flash_dr, &flash_adress, (uint8_t*)&local_uint32_var, \
|
spiflash_write(&flash_dr, &flash_adress, (uint8_t*)&local_uint32_var, \
|
sizeof(file_size));
|
sizeof(file_size));
|
}
|
}
|
}
|
}
|
|
|
/* anything ready, write data to the SPI flash */
|
/* anything ready, write data to the SPI flash */
|
if(file_size != 0) {
|
if(file_size != 0) {
|
//printf_tiny("off: %d\n",*offset);
|
//printf_tiny("off: %d\n",*offset);
|
//printf_tiny("ad: %x,",((uint8_t*)&flash_adress)[3]);
|
//printf_tiny("ad: %x,",((uint8_t*)&flash_adress)[3]);
|
//printf_tiny("%x,",((uint8_t*)&flash_adress)[2]);
|
//printf_tiny("%x,",((uint8_t*)&flash_adress)[2]);
|
//printf_tiny("%x,",((uint8_t*)&flash_adress)[1]);
|
//printf_tiny("%x,",((uint8_t*)&flash_adress)[1]);
|
//printf_tiny("%x\n",((uint8_t*)&flash_adress)[0]);
|
//printf_tiny("%x\n",((uint8_t*)&flash_adress)[0]);
|
|
|
|
|
length = *byte_count - *offset;
|
length = *byte_count - *offset;
|
|
|
/* check if we have data to be written to the next flash sector: */
|
/* check if we have data to be written to the next flash sector: */
|
local_uint32_var = flash_adress + *byte_count;
|
local_uint32_var = flash_adress + *byte_count;
|
if(sectorStart(local_uint32_var) != sectorStart(flash_adress)) {
|
if(sectorStart(local_uint32_var) != sectorStart(flash_adress)) {
|
/* before we can write to the next flash sector, we have to erase it */
|
/* before we can write to the next flash sector, we have to erase it */
|
spiflash_erase(&flash_dr, &local_uint32_var);
|
spiflash_erase(&flash_dr, &local_uint32_var);
|
}
|
}
|
|
|
/* write data to the SPI flash */
|
/* write data to the SPI flash */
|
local_buffer_ptr = buffer;
|
local_buffer_ptr = buffer;
|
local_buffer_ptr += *offset;
|
local_buffer_ptr += *offset;
|
spiflash_write(&flash_dr, &flash_adress, local_buffer_ptr, length);
|
spiflash_write(&flash_dr, &flash_adress, local_buffer_ptr, length);
|
|
|
/* adjust the file- and transfersize */
|
/* adjust the file- and transfersize */
|
usb_tmc_transfer.transfer_size -= length;
|
usb_tmc_transfer.transfer_size -= length;
|
file_size -= length;
|
file_size -= length;
|
|
|
//printf_tiny("le, %d\n",length);
|
//printf_tiny("le, %d\n",length);
|
|
|
/* if transfer finished, back to TMC idle state */
|
/* if transfer finished, back to TMC idle state */
|
if(file_size <= 0) {
|
if(file_size <= 0) {
|
file_size = 0;
|
file_size = 0;
|
usb_tmc_state = TMC_STATE_IDLE;
|
usb_tmc_state = TMC_STATE_IDLE;
|
}
|
}
|
}
|
}
|
|
|
return 1;
|
return 1;
|
}
|
}
|
|
|
|
|
/** \brief erases the desired file slot in spi flash
|
/** \brief erases the desired file slot in spi flash
|
* \detail send the erase command for one spi flash memory block and loop
|
* \detail send the erase command for one spi flash memory block and loop
|
* through the main_loop untill we finished erasing the whole fpga
|
* through the main_loop untill we finished erasing the whole fpga
|
* configuration file slot.
|
* configuration file slot.
|
*
|
*
|
* \param[in] xdata unsigned char *buffer pointer to the buffer to read from
|
* \param[in] xdata unsigned char *buffer pointer to the buffer to read from
|
* (normally endpoint buffer)
|
* (normally endpoint buffer)
|
* \param[in] idata uint16_t *offset pointer to the offset, buffer[offset]
|
* \param[in] idata uint16_t *offset pointer to the offset, buffer[offset]
|
* is the current position, anything before this is already consumed.
|
* is the current position, anything before this is already consumed.
|
* \return returns non-zero if successful, else 0
|
* \return returns non-zero if successful, else 0
|
*
|
*
|
* \todo uncomment this function after finishing debuging, else no space left!
|
* \todo uncomment this function after finishing debuging, else no space left!
|
*/
|
*/
|
uint8_t app_gecko3com_flash_delete(/* uint8_t *buffer,*/ idata uint16_t *offset) {
|
uint8_t app_gecko3com_flash_delete(/* uint8_t *buffer,*/ idata uint16_t *offset) {
|
|
|
xdata uint32_t flash_adress;
|
xdata uint32_t flash_adress;
|
xdata uint32_t local_uint32_var;
|
xdata uint32_t local_uint32_var;
|
char slot;
|
char slot;
|
|
|
/* send the delete command for each block and loop through the main_loop */
|
/* send the delete command for each block and loop through the main_loop */
|
/* check busy and set usb_tmc_state back to idle when finished file delete */
|
/* check busy and set usb_tmc_state back to idle when finished file delete */
|
if(usb_tmc_transfer.new_transfer == NEWTRANSFER) {
|
if(usb_tmc_transfer.new_transfer == NEWTRANSFER) {
|
//print_info("new\n");
|
//print_info("new\n");
|
|
|
/* select which file slot we have to delete */
|
/* select which file slot we have to delete */
|
slot = buffer[*offset];
|
slot = buffer[*offset];
|
if(slot == '0') {
|
if(slot == '0') {
|
flash_adress = START_ADRESS_SLOT0;
|
flash_adress = start_adress_slot0(flash_dr);
|
}
|
}
|
else if(slot == '1'){
|
else if(slot == '1'){
|
flash_adress = START_ADRESS_SLOT1;
|
flash_adress = start_adress_slot1(flash_dr);
|
}
|
}
|
else {
|
else {
|
//print_err("del\n");
|
//print_err("del\n");
|
ieee488_status.EventStatusRegister |= bmCOMMAND_ERROR;
|
ieee488_status.EventStatusRegister |= bmCOMMAND_ERROR;
|
usb_tmc_state = TMC_STATE_IDLE;
|
usb_tmc_state = TMC_STATE_IDLE;
|
return 0;
|
return 0;
|
}
|
}
|
}
|
}
|
|
|
/* to "delete" means to set the file_size at the beginning of the confguration
|
/* to "delete" means to set the file_size at the beginning of the confguration
|
file slot to zero */
|
file slot to zero */
|
local_uint32_var = 0;
|
local_uint32_var = 0;
|
spiflash_write(&flash_dr, &flash_adress, (uint8_t*)&local_uint32_var,4);
|
spiflash_write(&flash_dr, &flash_adress, (uint8_t*)&local_uint32_var,4);
|
usb_tmc_state = TMC_STATE_IDLE;
|
usb_tmc_state = TMC_STATE_IDLE;
|
|
|
return 1;
|
return 1;
|
}
|
}
|
|
|
|
|
/** \brief Handle the class commands on endpoint 0.
|
/** \brief Handle the class commands on endpoint 0.
|
*
|
*
|
* \return If we handle this one, return non-zero.
|
* \return If we handle this one, return non-zero.
|
*/
|
*/
|
unsigned char app_class_cmd (void)
|
unsigned char app_class_cmd (void)
|
{
|
{
|
#ifdef USB_DFU_SUPPORT
|
#ifdef USB_DFU_SUPPORT
|
if (usb_dfu_request()){
|
if (usb_dfu_request()){
|
if(!usb_handle_dfu_packet()){
|
if(!usb_handle_dfu_packet()){
|
//print_err("dfu request\n");
|
//print_err("dfu request\n");
|
return 0;
|
return 0;
|
}
|
}
|
}
|
}
|
|
|
else
|
else
|
#endif
|
#endif
|
if (usb_tmc_request()){
|
if (usb_tmc_request()){
|
if(!usb_handle_tmc_packet()){
|
if(!usb_handle_tmc_packet()){
|
//print_err("tmc request\n");
|
//print_err("tmc request\n");
|
return 0;
|
return 0;
|
}
|
}
|
}
|
}
|
else {
|
else {
|
//print_err("invalid class request\n");
|
//print_err("invalid class request\n");
|
return 0; /* invalid class request */
|
return 0; /* invalid class request */
|
}
|
}
|
|
|
return 1;
|
return 1;
|
}
|
}
|
|
|
|
|
/** \brief Handle our "Vendor Extension" commands on endpoint 0.
|
/** \brief Handle our "Vendor Extension" commands on endpoint 0.
|
*
|
*
|
* \return If we handle this one, return non-zero.
|
* \return If we handle this one, return non-zero.
|
*/
|
*/
|
unsigned char app_vendor_cmd (void)
|
unsigned char app_vendor_cmd (void)
|
{
|
{
|
/* vendor commands are only used after production
|
/* vendor commands are only used after production
|
* starting with firmware version 0.4 we remove the vendor commands
|
* starting with firmware version 0.4 we remove the vendor commands
|
* to save memory for more importand functions!
|
* to save memory for more importand functions!
|
|
|
if (bRequestType == VRT_VENDOR_IN){ */
|
if (bRequestType == VRT_VENDOR_IN){ */
|
/*********************************
|
/*********************************
|
* handle the IN requests
|
* handle the IN requests
|
********************************/
|
********************************/
|
/*
|
/*
|
switch (bRequest){
|
switch (bRequest){
|
|
|
default:
|
default:
|
return 0;
|
return 0;
|
}
|
}
|
}
|
}
|
|
|
else if (bRequestType == VRT_VENDOR_OUT){ */
|
else if (bRequestType == VRT_VENDOR_OUT){ */
|
/***********************************
|
/***********************************
|
* handle the OUT requests
|
* handle the OUT requests
|
**********************************/
|
**********************************/
|
/*
|
/*
|
switch (bRequest){
|
switch (bRequest){
|
case VRQ_SET_SERIAL:
|
case VRQ_SET_SERIAL:
|
get_ep0_data();
|
get_ep0_data();
|
if(wLengthL > SERIAL_NO_LEN){
|
if(wLengthL > SERIAL_NO_LEN){
|
return 0;
|
return 0;
|
}
|
}
|
if(!eeprom_write(I2C_ADDR_BOOT, SERIAL_NO_OFFSET, EP0BUF, wLengthL)){
|
if(!eeprom_write(I2C_ADDR_BOOT, SERIAL_NO_OFFSET, EP0BUF, wLengthL)){
|
return 0;
|
return 0;
|
}
|
}
|
break;
|
break;
|
|
|
case VRQ_SET_HW_REV:
|
case VRQ_SET_HW_REV:
|
get_ep0_data();
|
get_ep0_data();
|
if(!eeprom_write(I2C_ADDR_BOOT, HW_REV_OFFSET, EP0BUF, 1)){
|
if(!eeprom_write(I2C_ADDR_BOOT, HW_REV_OFFSET, EP0BUF, 1)){
|
return 0;
|
return 0;
|
}
|
}
|
break;
|
break;
|
|
|
case VRQ_SET_FPGA_TYPE:
|
case VRQ_SET_FPGA_TYPE:
|
get_ep0_data();
|
get_ep0_data();
|
if(wLengthL > FPGA_TYPE_LEN){
|
if(wLengthL > FPGA_TYPE_LEN){
|
return 0;
|
return 0;
|
}
|
}
|
if(!eeprom_write(I2C_ADDR_BOOT, FPGA_TYPE_OFFSET, EP0BUF, wLengthL)){
|
if(!eeprom_write(I2C_ADDR_BOOT, FPGA_TYPE_OFFSET, EP0BUF, wLengthL)){
|
return 0;
|
return 0;
|
}
|
}
|
break;
|
break;
|
|
|
case VRQ_SET_FPGA_IDCODE:
|
case VRQ_SET_FPGA_IDCODE:
|
get_ep0_data();
|
get_ep0_data();
|
if(!eeprom_write(I2C_ADDR_BOOT, FPGA_IDCODE_OFFSET, EP0BUF, FPGA_IDCODE_LEN)){
|
if(!eeprom_write(I2C_ADDR_BOOT, FPGA_IDCODE_OFFSET, EP0BUF, FPGA_IDCODE_LEN)){
|
return 0;
|
return 0;
|
}
|
}
|
break;
|
break;
|
|
|
default:
|
default:
|
return 0;
|
return 0;
|
|
|
}
|
}
|
}
|
}
|
|
|
else */
|
else */
|
return 0; /* invalid bRequestType */
|
return 0; /* invalid bRequestType */
|
|
|
//return 1;
|
//return 1;
|
}
|
}
|
|
|
|
|
/** \brief Read h/w rev code and serial number out of boot eeprom and
|
/** \brief Read h/w rev code and serial number out of boot eeprom and
|
* patch the usb descriptors with these values.
|
* patch the usb descriptors with these values.
|
*/
|
*/
|
void patch_usb_descriptors(void)
|
void patch_usb_descriptors(void)
|
{
|
{
|
xdata uint8_t hw_rev;
|
xdata uint8_t hw_rev;
|
xdata unsigned char serial_no[SERIAL_NO_LEN];
|
xdata unsigned char serial_no[SERIAL_NO_LEN];
|
unsigned char ch;
|
unsigned char ch;
|
uint8_t i,j;
|
uint8_t i,j;
|
|
|
/* hardware revision */
|
/* hardware revision */
|
eeprom_read(HW_REV_OFFSET, &hw_rev, 1); /* LSB of device id */
|
eeprom_read(HW_REV_OFFSET, &hw_rev, 1); /* LSB of device id */
|
usb_desc_hw_rev_binary_patch_location_0[0] = hw_rev;
|
usb_desc_hw_rev_binary_patch_location_0[0] = hw_rev;
|
usb_desc_hw_rev_binary_patch_location_1[0] = hw_rev;
|
usb_desc_hw_rev_binary_patch_location_1[0] = hw_rev;
|
|
|
/* serial number */
|
/* serial number */
|
eeprom_read(SERIAL_NO_OFFSET, serial_no, SERIAL_NO_LEN);
|
eeprom_read(SERIAL_NO_OFFSET, serial_no, SERIAL_NO_LEN);
|
|
|
for (i = 0; i < SERIAL_NO_LEN; i++){
|
for (i = 0; i < SERIAL_NO_LEN; i++){
|
ch = serial_no[i];
|
ch = serial_no[i];
|
if (ch == 0xff) /* make unprogrammed EEPROM default to '0' */
|
if (ch == 0xff) /* make unprogrammed EEPROM default to '0' */
|
ch = '0';
|
ch = '0';
|
|
|
j = i << 1;
|
j = i << 1;
|
usb_desc_serial_number_ascii[j] = ch;
|
usb_desc_serial_number_ascii[j] = ch;
|
}
|
}
|
}
|
}
|
|
|
|
|
/** \brief we do all the work here. infinite loop */
|
/** \brief we do all the work here. infinite loop */
|
static void main_loop (void)
|
static void main_loop (void)
|
{
|
{
|
tHeader *tmc_header, *tmc_response_header;
|
tHeader *tmc_header, *tmc_response_header;
|
idata uint16_t offset, byte_count;
|
idata uint16_t offset, byte_count;
|
static idata uint32_t transfer_size;
|
static idata uint32_t transfer_size;
|
xdata Scanner scpi_scanner;
|
xdata Scanner scpi_scanner;
|
|
|
uint16_t index;
|
uint16_t index;
|
|
|
buffer = EP2FIFOBUF;
|
buffer = EP2FIFOBUF;
|
scpi_scanner.action = NOACTION;
|
scpi_scanner.action = NOACTION;
|
|
index = 0;
|
|
|
while (1){
|
while (1){
|
|
|
usb_tmc_transfer.new_transfer = 0;
|
usb_tmc_transfer.new_transfer = 0;
|
|
|
/* -------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------- */
|
/* SETUP Package on Endpoint 0. Handle if we received one */
|
/* SETUP Package on Endpoint 0. Handle if we received one */
|
if (usb_setup_packet_avail())
|
if (usb_setup_packet_avail())
|
usb_handle_setup_packet();
|
usb_handle_setup_packet();
|
|
|
/* -------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------- */
|
/* Let's do some work when an Endpoint has data */
|
/* Let's do some work when an Endpoint has data */
|
if (!(EP2468STAT & bmEP2EMPTY) && flLOCAL == GECKO3COM_LOCAL){
|
if (!(EP2468STAT & bmEP2EMPTY) && flLOCAL == GECKO3COM_LOCAL){
|
offset = 0;
|
offset = 0;
|
|
|
if(usb_tmc_state == TMC_STATE_IDLE || usb_tmc_transfer.transfer_size == 0){
|
if(usb_tmc_state == TMC_STATE_IDLE || usb_tmc_transfer.transfer_size == 0){
|
|
|
/* start to analyze the data in Endpoint 2 if it is a correct TMC header */
|
/* start to analyze the data in Endpoint 2 if it is a correct TMC header */
|
tmc_header = (tHeader*)EP2FIFOBUF;
|
tmc_header = (tHeader*)EP2FIFOBUF;
|
|
|
/* bTag sanity check. store bTag for correct IN transfer response */
|
/* bTag sanity check. store bTag for correct IN transfer response */
|
if (tmc_header->bTag == ~tmc_header->bTagInverse) {
|
if (tmc_header->bTag == ~tmc_header->bTagInverse) {
|
usb_tmc_transfer.bTag = tmc_header->bTag;
|
usb_tmc_transfer.bTag = tmc_header->bTag;
|
|
|
/* TMC header is correct. Now find out what we have to do: */
|
/* TMC header is correct. Now find out what we have to do: */
|
|
|
/* check if this transfer is a DEV_DEP_MSG_OUT message */
|
/* check if this transfer is a DEV_DEP_MSG_OUT message */
|
if(tmc_header->MsgID == DEV_DEP_MSG_OUT){
|
if(tmc_header->MsgID == DEV_DEP_MSG_OUT){
|
usb_tmc_transfer.transfer_size = \
|
usb_tmc_transfer.transfer_size = \
|
((DEV_DEP_MSG_OUT_Header*)tmc_header->msg_specific)->TransferSize;
|
((DEV_DEP_MSG_OUT_Header*)tmc_header->msg_specific)->TransferSize;
|
usb_tmc_transfer.new_transfer = NEWTRANSFER;
|
usb_tmc_transfer.new_transfer = NEWTRANSFER;
|
offset = USB_TMC_HEADER_SIZE;
|
offset = USB_TMC_HEADER_SIZE;
|
|
|
/* Decide if we should start the SCPI parser or not
|
/* Decide if we should start the SCPI parser or not
|
* if not IDLE, the transfer size was 0 and we continue
|
* if not IDLE, the transfer size was 0 and we continue
|
* to exectue the action and don't try to parse a new command */
|
* to exectue the action and don't try to parse a new command */
|
if(usb_tmc_state == TMC_STATE_IDLE) {
|
if(usb_tmc_state == TMC_STATE_IDLE) {
|
|
|
/* fresh OUT Transfer: handle device dependent command message */
|
/* fresh OUT Transfer: handle device dependent command message */
|
usb_tmc_state = TMC_STATE_OUT_TRANSFER;
|
usb_tmc_state = TMC_STATE_OUT_TRANSFER;
|
usb_tmc_transfer.nbytes_rxd = 0;
|
usb_tmc_transfer.nbytes_rxd = 0;
|
|
|
/* when we receive an new out message before we sent the response,
|
/* when we receive an new out message before we sent the response,
|
* we have to clear the response queue first*/
|
* we have to clear the response queue first*/
|
IEEE488_clear_mav();
|
IEEE488_clear_mav();
|
usb_tmc_transfer.nbytes_txd = 0;
|
usb_tmc_transfer.nbytes_txd = 0;
|
response_queue.length = 0;
|
response_queue.length = 0;
|
|
|
|
|
/* setup variables for scpi parser.
|
/* setup variables for scpi parser.
|
* offset points to first command byte in endpoint buffer */
|
* offset points to first command byte in endpoint buffer */
|
scpi_scanner.source = EP2FIFOBUF;
|
scpi_scanner.source = EP2FIFOBUF;
|
scpi_scanner.action = NOACTION;
|
scpi_scanner.action = NOACTION;
|
|
|
/* start SCPI parser */
|
/* start SCPI parser */
|
if(!scpi_scan(&offset, &scpi_scanner, &response_queue)){
|
if(!scpi_scan(&offset, &scpi_scanner, &response_queue)){
|
/* the parser returned an error. set flags */
|
/* the parser returned an error. set flags */
|
ieee488_status.EventStatusRegister |= bmCOMMAND_ERROR;
|
ieee488_status.EventStatusRegister |= bmCOMMAND_ERROR;
|
usb_tmc_state = TMC_STATE_IDLE;
|
usb_tmc_state = TMC_STATE_IDLE;
|
scpi_scanner.action = NOACTION;
|
scpi_scanner.action = NOACTION;
|
usb_tmc_transfer.new_transfer = 0;
|
usb_tmc_transfer.new_transfer = 0;
|
//print_err("syntax failure\n");
|
//print_err("syntax failure\n");
|
}
|
}
|
}
|
}
|
}
|
}
|
/* finished handling an DEV_DEP_MSG_OUT message */
|
/* finished handling an DEV_DEP_MSG_OUT message */
|
|
|
/* ---------------------------------------------------------------- */
|
/* ---------------------------------------------------------------- */
|
/* check if this transfer is a IN request and we have a IN response
|
/* check if this transfer is a IN request and we have a IN response
|
* queued */
|
* queued */
|
else if(tmc_header->MsgID == REQUEST_DEV_DEP_MSG_IN \
|
else if(tmc_header->MsgID == REQUEST_DEV_DEP_MSG_IN \
|
&& response_queue.length > 0) {
|
&& response_queue.length > 0) {
|
|
|
/* IN Transfer: Handle response message to a device dependent
|
/* IN Transfer: Handle response message to a device dependent
|
* command message. For this we change the TMC state.
|
* command message. For this we change the TMC state.
|
* Sending the requested data to the IN endpoint
|
* Sending the requested data to the IN endpoint
|
* happens further below */
|
* happens further below */
|
usb_tmc_state = TMC_STATE_IN_TRANSFER;
|
usb_tmc_state = TMC_STATE_IN_TRANSFER;
|
usb_tmc_transfer.transfer_size = \
|
usb_tmc_transfer.transfer_size = \
|
((REQUEST_DEV_DEP_MSG_IN_Header*) tmc_header->msg_specific)\
|
((REQUEST_DEV_DEP_MSG_IN_Header*) tmc_header->msg_specific)\
|
->TransferSize;
|
->TransferSize;
|
usb_tmc_transfer.nbytes_txd = 0;
|
usb_tmc_transfer.nbytes_txd = 0;
|
|
|
}
|
}
|
else {
|
else {
|
/* TMC header error: unknown message ID */
|
/* TMC header error: unknown message ID */
|
EP2CS |= bmEPSTALL;
|
EP2CS |= bmEPSTALL;
|
//print_err("ID\n");
|
//print_err("ID\n");
|
}
|
}
|
}
|
}
|
|
|
else {
|
else {
|
/* TMC header error: bTag and bTagInverse don't match */
|
/* TMC header error: bTag and bTagInverse don't match */
|
EP2CS |= bmEPSTALL;
|
EP2CS |= bmEPSTALL;
|
//print_err("bTag\n");
|
//print_err("bTag\n");
|
}
|
}
|
}
|
}
|
|
|
/* -------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------- */
|
/* OUT Transfer: The SCPI parser has detected a application specific
|
/* OUT Transfer: The SCPI parser has detected a application specific
|
* command. Here we execute the desired functions for these commands: */
|
* command. Here we execute the desired functions for these commands: */
|
if(usb_tmc_state == TMC_STATE_OUT_TRANSFER){
|
if(usb_tmc_state == TMC_STATE_OUT_TRANSFER){
|
|
|
/* set the correct byte_count value */
|
/* set the correct byte_count value */
|
/* read byte counter register of EP2FIFOBUF */
|
/* read byte counter register of EP2FIFOBUF */
|
byte_count = (EP2BCH << 8) + EP2BCL;
|
byte_count = (EP2BCH << 8) + EP2BCL;
|
|
|
/* decide which value is the smaller one */
|
/* decide which value is the smaller one */
|
if((byte_count - offset) > usb_tmc_transfer.transfer_size) {
|
if((byte_count - offset) > usb_tmc_transfer.transfer_size) {
|
byte_count = usb_tmc_transfer.transfer_size;
|
byte_count = usb_tmc_transfer.transfer_size;
|
/* transfer_size does not includ the header length: */
|
/* transfer_size does not includ the header length: */
|
byte_count += offset;
|
byte_count += offset;
|
}
|
}
|
|
|
/* select what we have to to according to the parsed scpi command */
|
/* select what we have to to according to the parsed scpi command */
|
switch (scpi_scanner.action) {
|
switch (scpi_scanner.action) {
|
|
|
case SYSTEM_RESET:
|
case SYSTEM_RESET:
|
/* Send a global reset signal to the FPGA and all connected modules */
|
/* Send a global reset signal to the FPGA and all connected modules */
|
gecko3com_system_reset();
|
gecko3com_system_reset();
|
usb_tmc_state = TMC_STATE_IDLE;
|
usb_tmc_state = TMC_STATE_IDLE;
|
break;
|
break;
|
|
|
case rqFPGA_IDCODE:
|
case rqFPGA_IDCODE:
|
/* Request to read the FPGA JTAG ID code */
|
/* Request to read the FPGA JTAG ID code */
|
eeprom_read(FPGA_IDCODE_OFFSET, response_queue.buf, FPGA_IDCODE_LEN);
|
eeprom_read(FPGA_IDCODE_OFFSET, response_queue.buf, FPGA_IDCODE_LEN);
|
response_queue.buf[FPGA_IDCODE_LEN] = '\n';
|
response_queue.buf[FPGA_IDCODE_LEN] = '\n';
|
response_queue.length = FPGA_IDCODE_LEN+1;
|
response_queue.length = FPGA_IDCODE_LEN+1;
|
IEEE488_set_mav();
|
IEEE488_set_mav();
|
usb_tmc_state = TMC_STATE_IDLE;
|
usb_tmc_state = TMC_STATE_IDLE;
|
break;
|
break;
|
|
|
case rqFPGA_TYPE:
|
case rqFPGA_TYPE:
|
/* Request to read the FPGA type string */
|
/* Request to read the FPGA type string */
|
eeprom_read(FPGA_TYPE_OFFSET, response_queue.buf, FPGA_TYPE_LEN);
|
eeprom_read(FPGA_TYPE_OFFSET, response_queue.buf, FPGA_TYPE_LEN);
|
response_queue.buf[FPGA_TYPE_LEN] = '\n';
|
response_queue.buf[FPGA_TYPE_LEN] = '\n';
|
response_queue.length = FPGA_TYPE_LEN+1;
|
response_queue.length = FPGA_TYPE_LEN+1;
|
IEEE488_set_mav();
|
IEEE488_set_mav();
|
usb_tmc_state = TMC_STATE_IDLE;
|
usb_tmc_state = TMC_STATE_IDLE;
|
break;
|
break;
|
|
|
case rqFPGA_DONE:
|
case rqFPGA_DONE:
|
/* Is the FPGA configured or not? Check the "done" pin*/
|
/* Is the FPGA configured or not? Check the "done" pin*/
|
if(fpga_done()) {
|
if(fpga_done()) {
|
response_queue.buf[0] = '1';
|
response_queue.buf[0] = '1';
|
}
|
}
|
else {
|
else {
|
response_queue.buf[0] = '0';
|
response_queue.buf[0] = '0';
|
}
|
}
|
response_queue.buf[1] = '\n';
|
response_queue.buf[1] = '\n';
|
response_queue.length = 2;
|
response_queue.length = 2;
|
usb_tmc_state = TMC_STATE_IDLE;
|
usb_tmc_state = TMC_STATE_IDLE;
|
break;
|
break;
|
|
|
case FPGA_CONFIGURE:
|
case FPGA_CONFIGURE:
|
/* Configure the FPGA directly */
|
/* Configure the FPGA directly */
|
if(!app_configure_fpga(&offset, &byte_count)) {
|
if(!app_configure_fpga(&offset, &byte_count)) {
|
//print_err("conf\n");
|
//print_err("conf\n");
|
ieee488_status.EventStatusRegister |= bmEXECUTION_ERROR;
|
ieee488_status.EventStatusRegister |= bmEXECUTION_ERROR;
|
usb_tmc_state = TMC_STATE_IDLE;
|
usb_tmc_state = TMC_STATE_IDLE;
|
}
|
}
|
break;
|
break;
|
|
|
case FPGA_COMMUNICATION:
|
case FPGA_COMMUNICATION:
|
/* Switch the context from the FX2 to the FPGA.
|
/* Switch the context from the FX2 to the FPGA.
|
* After this command all endpoint 2 and 6 data goes directly to the
|
* After this command all endpoint 2 and 6 data goes directly to the
|
* FPGA, the FX2 doesn't parse commands anymore. Use endpoint 0 TMC
|
* FPGA, the FX2 doesn't parse commands anymore. Use endpoint 0 TMC
|
* commands to switch back */
|
* commands to switch back */
|
init_gpif();
|
init_gpif();
|
flLOCAL = GECKO3COM_REMOTE;
|
flLOCAL = GECKO3COM_REMOTE;
|
usb_tmc_state = TMC_STATE_IDLE;
|
usb_tmc_state = TMC_STATE_IDLE;
|
break;
|
break;
|
|
|
case SPI_DELETE:
|
case SPI_DELETE:
|
/* Erases one of the file spaces in the SPI flash */
|
/* Erases one of the file spaces in the SPI flash */
|
if(!app_gecko3com_flash_delete(&offset)) {
|
if(!app_gecko3com_flash_delete(&offset)) {
|
ieee488_status.EventStatusRegister |= bmEXECUTION_ERROR;
|
ieee488_status.EventStatusRegister |= bmEXECUTION_ERROR;
|
}
|
}
|
usb_tmc_state = TMC_STATE_IDLE;
|
usb_tmc_state = TMC_STATE_IDLE;
|
break;
|
break;
|
|
|
case SPI_WRITE:
|
case SPI_WRITE:
|
/* Writes a FPGA configuration file into a file space in the
|
/* Writes a FPGA configuration file into a file space in the
|
* SPI flash. */
|
* SPI flash. */
|
if(!app_write_conf_to_flash(&offset, &byte_count)) {
|
if(!app_write_conf_to_flash(&offset, &byte_count)) {
|
ieee488_status.EventStatusRegister |= bmEXECUTION_ERROR;
|
ieee488_status.EventStatusRegister |= bmEXECUTION_ERROR;
|
usb_tmc_state = TMC_STATE_IDLE;
|
usb_tmc_state = TMC_STATE_IDLE;
|
}
|
}
|
break;
|
break;
|
|
|
default:
|
default:
|
|
|
usb_tmc_state = TMC_STATE_IDLE;
|
usb_tmc_state = TMC_STATE_IDLE;
|
}
|
}
|
}
|
}
|
|
|
usb_tmc_transfer.nbytes_rxd += ((EP2BCH << 8) + EP2BCL - USB_TMC_HEADER_SIZE);
|
usb_tmc_transfer.nbytes_rxd += ((EP2BCH << 8) + EP2BCL - USB_TMC_HEADER_SIZE);
|
|
|
/* finished handling usb package.
|
/* finished handling usb package.
|
* rearm OUT endpoint to receive new data */
|
* rearm OUT endpoint to receive new data */
|
OUTPKTEND = bmSKIP | USB_TMC_EP_OUT;
|
OUTPKTEND = bmSKIP | USB_TMC_EP_OUT;
|
|
|
} /* end of OUT Transfer clause */
|
} /* end of OUT Transfer clause */
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------- */
|
/* Let's continue to send data when an Endpoint is free */
|
/* Let's continue to send data when an Endpoint is free */
|
/* IN Transfer: Generate a valid TMC IN header and send the response
|
/* IN Transfer: Generate a valid TMC IN header and send the response
|
* message data to the endpoint */
|
* message data to the endpoint */
|
if (!(EP2468STAT & bmEP6FULL) && usb_tmc_state == TMC_STATE_IN_TRANSFER){
|
if (!(EP2468STAT & bmEP6FULL) && usb_tmc_state == TMC_STATE_IN_TRANSFER){
|
|
|
/* fresh IN transfer, send first header */
|
/* fresh IN transfer, send first header */
|
if(usb_tmc_transfer.nbytes_txd == 0) {
|
if(usb_tmc_transfer.nbytes_txd == 0) {
|
index = 0;
|
index = 0;
|
tmc_response_header = (tHeader*)EP6FIFOBUF;
|
tmc_response_header = (tHeader*)EP6FIFOBUF;
|
tmc_response_header->MsgID = REQUEST_DEV_DEP_MSG_IN;
|
tmc_response_header->MsgID = REQUEST_DEV_DEP_MSG_IN;
|
tmc_response_header->bTag = usb_tmc_transfer.bTag;
|
tmc_response_header->bTag = usb_tmc_transfer.bTag;
|
tmc_response_header->bTagInverse = ~usb_tmc_transfer.bTag;
|
tmc_response_header->bTagInverse = ~usb_tmc_transfer.bTag;
|
tmc_response_header->Reserved = 0;
|
tmc_response_header->Reserved = 0;
|
((DEV_DEP_MSG_IN_Header*)tmc_response_header->msg_specific)-> \
|
((DEV_DEP_MSG_IN_Header*)tmc_response_header->msg_specific)-> \
|
TransferSize = response_queue.length;
|
TransferSize = response_queue.length;
|
((DEV_DEP_MSG_IN_Header*)tmc_response_header->msg_specific)->\
|
((DEV_DEP_MSG_IN_Header*)tmc_response_header->msg_specific)->\
|
Reserved[0] = 0;
|
Reserved[0] = 0;
|
((DEV_DEP_MSG_IN_Header*)tmc_response_header->msg_specific)->\
|
((DEV_DEP_MSG_IN_Header*)tmc_response_header->msg_specific)->\
|
Reserved[1] = 0;
|
Reserved[1] = 0;
|
((DEV_DEP_MSG_IN_Header*)tmc_response_header->msg_specific)->\
|
((DEV_DEP_MSG_IN_Header*)tmc_response_header->msg_specific)->\
|
Reserved[2] = 0;
|
Reserved[2] = 0;
|
|
|
/* if we can send all data in one usb packet,
|
/* if we can send all data in one usb packet,
|
set EOM (end of message) bit */
|
set EOM (end of message) bit */
|
/* WARNING: set EOM bit in the LAST tmc transfer.
|
/* WARNING: set EOM bit in the LAST tmc transfer.
|
* we transmitt anything in one transfer so we set this bit always. */
|
* we transmitt anything in one transfer so we set this bit always. */
|
/*if(USBCS & bmHSM && response_queue.length <= 500 | \
|
/*if(USBCS & bmHSM && response_queue.length <= 500 | \
|
response_queue.length <= 56)*/
|
response_queue.length <= 56)*/
|
((DEV_DEP_MSG_OUT_Header*)tmc_response_header->msg_specific)->\
|
((DEV_DEP_MSG_OUT_Header*)tmc_response_header->msg_specific)->\
|
bmTransferAttributes = bmTA_EOM;
|
bmTransferAttributes = bmTA_EOM;
|
/*else
|
/*else
|
((DEV_DEP_MSG_OUT_Header*)tmc_response_header->msg_specific)->\
|
((DEV_DEP_MSG_OUT_Header*)tmc_response_header->msg_specific)->\
|
bmTransferAttributes = 0;*/
|
bmTransferAttributes = 0;*/
|
|
|
index = USB_TMC_HEADER_SIZE;
|
index = USB_TMC_HEADER_SIZE;
|
} /* finished writing header */
|
} /* finished writing header */
|
|
|
|
|
/* Transmit data */
|
/* Transmit data */
|
for(usb_tmc_transfer.nbytes_txd; \
|
for(usb_tmc_transfer.nbytes_txd; \
|
usb_tmc_transfer.nbytes_txd <= response_queue.length; \
|
usb_tmc_transfer.nbytes_txd <= response_queue.length; \
|
usb_tmc_transfer.nbytes_txd++){
|
usb_tmc_transfer.nbytes_txd++){
|
|
|
/* copy the data from the response queue to the IN endpoint */
|
/* copy the data from the response queue to the IN endpoint */
|
EP6FIFOBUF[index++] = response_queue.buf[usb_tmc_transfer.nbytes_txd];
|
EP6FIFOBUF[index++] = response_queue.buf[usb_tmc_transfer.nbytes_txd];
|
|
|
/* we send any response in one packet so we don't have to check if
|
/* we send any response in one packet so we don't have to check if
|
* the endpoint buffer is full */
|
* the endpoint buffer is full */
|
/*if(!(USBCS & bmHSM) && index == 64 | \
|
/*if(!(USBCS & bmHSM) && index == 64 | \
|
index == 512)
|
index == 512)
|
break;
|
break;
|
*/
|
*/
|
}
|
}
|
|
|
EP6BCH = index >> 8;
|
EP6BCH = index >> 8;
|
EP6BCL = index & 0xFF;
|
EP6BCL = index & 0xFF;
|
index = 0;
|
index = 0;
|
|
|
/* detect end of transfer */
|
/* detect end of transfer */
|
if(usb_tmc_transfer.nbytes_txd >= response_queue.length){
|
if(usb_tmc_transfer.nbytes_txd >= response_queue.length){
|
usb_tmc_state = TMC_STATE_IDLE;
|
usb_tmc_state = TMC_STATE_IDLE;
|
IEEE488_clear_mav();
|
IEEE488_clear_mav();
|
response_queue.length = 0;
|
response_queue.length = 0;
|
}
|
}
|
} /* end of IN Transfer clause */
|
} /* end of IN Transfer clause */
|
|
|
/* if the LED flag is set to off, disable the external LED */
|
/* if the LED flag is set to off, disable the external LED */
|
if(flLED == LEDS_OFF) {
|
if(flLED == LEDS_OFF) {
|
set_led_ext(LEDS_OFF);
|
set_led_ext(LEDS_OFF);
|
}
|
}
|
|
|
/* resets the watchdog timer back to the initial value */
|
/* resets the watchdog timer back to the initial value */
|
watchdog_count = WATCHDOG_TIME;
|
watchdog_count = WATCHDOG_TIME;
|
|
|
} /* end of infinite main loop */
|
} /* end of infinite main loop */
|
}
|
}
|
|
|
|
|
/** \brief ISR called at 100 Hz from timer2 interrupt
|
/** \brief ISR called at 100 Hz from timer2 interrupt
|
*
|
*
|
* Toggle led 0
|
* Toggle led 0
|
*/
|
*/
|
void isr_tick (void) interrupt
|
void isr_tick (void) interrupt
|
{
|
{
|
static uint8_t count = 1;
|
static uint8_t count = 1;
|
|
|
if (--count == 0){
|
if (--count == 0){
|
count = 50;
|
count = 50;
|
toggle_led_0();
|
toggle_led_0();
|
flLED = LEDS_OFF;
|
flLED = LEDS_OFF;
|
}
|
}
|
|
|
// if (--watchdog_count == 0){
|
// if (--watchdog_count == 0){
|
// clear_timer_irq();
|
// clear_timer_irq();
|
// #ifdef DEBUG_LEVEL_ERROR
|
// #ifdef DEBUG_LEVEL_ERROR
|
// print_err("Watchdog timed out! System reset\n");
|
// print_err("Watchdog timed out! System reset\n");
|
//mdelay(100); /* wait 100 ms to give the uart some time to transmit */
|
//mdelay(100); /* wait 100 ms to give the uart some time to transmit */
|
// #endif
|
// #endif
|
|
|
/* simulate CPU reset */ /* FIXME this stuff here does not work. no idea how to simulate an CPU reset instead... */
|
/* simulate CPU reset */ /* FIXME this stuff here does not work. no idea how to simulate an CPU reset instead... */
|
/* _asm
|
/* _asm
|
ljmp __reset_vector
|
ljmp __reset_vector
|
_endasm;*/
|
_endasm;*/
|
//}
|
//}
|
|
|
#ifdef USB_DFU_SUPPORT
|
#ifdef USB_DFU_SUPPORT
|
if (usb_dfu_state == DFU_STATE_appDETACH){
|
if (usb_dfu_state == DFU_STATE_appDETACH){
|
if (--usb_dfu_timeout == 0){
|
if (--usb_dfu_timeout == 0){
|
usb_toggle_dfu_handlers();
|
usb_toggle_dfu_handlers();
|
}
|
}
|
}
|
}
|
#endif
|
#endif
|
|
|
clear_timer_irq();
|
clear_timer_irq();
|
}
|
}
|
|
|
|
|
/** \brief starting point of execution.
|
/** \brief starting point of execution.
|
*
|
*
|
* we initialize all system components here. after that we go to the main_loop
|
* we initialize all system components here. after that we go to the main_loop
|
* function there all the work is done.
|
* function there all the work is done.
|
*/
|
*/
|
void main(void)
|
void main(void)
|
{
|
{
|
/* variables needed for the stand-alone fpga configuration */
|
/* variables needed for the stand-alone fpga configuration */
|
uint8_t led_color;
|
uint8_t led_color;
|
idata uint16_t i, local_offset;
|
idata uint16_t i, local_offset;
|
xdata uint32_t spi_base_adress;
|
xdata uint32_t spi_base_adress;
|
|
|
init_gecko3com();
|
init_gecko3com();
|
init_io_ext();
|
init_io_ext();
|
init_usb_tmc();
|
init_usb_tmc();
|
init_fpga_interface();
|
init_fpga_interface();
|
init_spiflash(&flash_dr);
|
init_spiflash(&flash_dr);
|
|
|
/* disconnect USB, so the host doesn't wait for us during the fpga
|
/* disconnect USB, so the host doesn't wait for us during the fpga
|
*configuration process (takes up to 20s) */
|
*configuration process (takes up to 20s) */
|
USBCS |= bmDISCON;
|
USBCS |= bmDISCON;
|
|
|
#ifdef DEBUG_LEVEL_ERROR
|
#ifdef DEBUG_LEVEL_ERROR
|
ser_init();
|
ser_init();
|
printf_tiny("hi\n");
|
printf_tiny("hi\n");
|
#endif
|
#endif
|
|
|
/* set the context switch flag to local operation, not fpga */
|
/* set the context switch flag to local operation, not fpga */
|
flLOCAL = GECKO3COM_LOCAL;
|
flLOCAL = GECKO3COM_LOCAL;
|
|
|
/* enable GPIF state output for debuging */
|
/* enable GPIF state output for debuging */
|
IFCONFIG |= bmGSTATE;
|
IFCONFIG |= bmGSTATE;
|
|
|
EA = 0; /* disable all interrupts */
|
EA = 0; /* disable all interrupts */
|
|
|
patch_usb_descriptors();
|
patch_usb_descriptors();
|
setup_autovectors();
|
setup_autovectors();
|
usb_install_handlers();
|
usb_install_handlers();
|
hook_timer_tick((unsigned short) isr_tick);
|
hook_timer_tick((unsigned short) isr_tick);
|
|
|
EA = 1; /* global interrupt enable */
|
EA = 1; /* global interrupt enable */
|
|
|
/* finished initializing GECKO3COM system */
|
/* finished initializing GECKO3COM system */
|
/*------------------------------------------------------------------------*/
|
/*------------------------------------------------------------------------*/
|
|
|
/* start to configure the FPGA from the configuration SPI flash */
|
/* start to configure the FPGA from the configuration SPI flash */
|
/* read which configuration, the first or second, we should use */
|
/* read which configuration, the first or second, we should use */
|
if(get_switch()){
|
if(get_switch()){
|
led_color = GREEN;
|
led_color = GREEN;
|
spi_base_adress = START_ADRESS_SLOT0;
|
spi_base_adress = start_adress_slot0(flash_dr);
|
}
|
}
|
else {
|
else {
|
led_color = RED;
|
led_color = RED;
|
spi_base_adress = START_ADRESS_SLOT1;
|
spi_base_adress = start_adress_slot1(flash_dr);
|
}
|
}
|
|
|
/* read the configuration file size from the spi flash */
|
/* read the configuration file size from the spi flash */
|
spiflash_read(&flash_dr, &spi_base_adress, response_queue.buf, 4);
|
spiflash_read(&flash_dr, &spi_base_adress, response_queue.buf, 4);
|
((idata uint8_t*)&file_size)[0] = response_queue.buf[0];
|
((idata uint8_t*)&file_size)[0] = response_queue.buf[0];
|
((idata uint8_t*)&file_size)[1] = response_queue.buf[1];
|
((idata uint8_t*)&file_size)[1] = response_queue.buf[1];
|
((idata uint8_t*)&file_size)[2] = response_queue.buf[2];
|
((idata uint8_t*)&file_size)[2] = response_queue.buf[2];
|
((idata uint8_t*)&file_size)[3] = response_queue.buf[3];
|
((idata uint8_t*)&file_size)[3] = response_queue.buf[3];
|
spi_base_adress += 4;
|
spi_base_adress += 4;
|
|
|
|
/* debug stuff */
|
|
response_queue.buf[0] = init_spiflash(&flash_dr);
|
|
IEEE488_set_mav();
|
|
response_queue.length = 1;
|
|
|
/* there is nothing to configure when the filesize is 0 or 0xFFFFFFFF */
|
/* there is nothing to configure when the filesize is 0 or 0xFFFFFFFF */
|
if(file_size == 0 || file_size == 0xFFFFFFFF) {
|
if(file_size == 0 || file_size == 0xFFFFFFFF) {
|
/* show that we don't load a config */
|
/* show that we don't load a config */
|
set_led_ext(ORANGE);
|
set_led_ext(ORANGE);
|
}
|
}
|
else {
|
else {
|
fpga_load_begin();
|
fpga_load_begin();
|
i = TMC_RESPONSE_QUEUE_LENGTH-1;
|
i = TMC_RESPONSE_QUEUE_LENGTH-1;
|
while(file_size > 0) {
|
while(file_size > 0) {
|
set_led_ext(led_color); /* show which config we load */
|
set_led_ext(led_color); /* show which config we load */
|
|
|
if(i > file_size) {
|
if(i > file_size) {
|
i = (uint8_t)file_size;
|
i = (uint8_t)file_size;
|
}
|
}
|
spiflash_read(&flash_dr, &spi_base_adress, response_queue.buf, i);
|
spiflash_read(&flash_dr, &spi_base_adress, response_queue.buf, i);
|
|
|
local_offset = 0;
|
local_offset = 0;
|
fpga_load_xfer(response_queue.buf, &local_offset, &i);
|
fpga_load_xfer(response_queue.buf, &local_offset, &i);
|
file_size -= i;
|
file_size -= i;
|
spi_base_adress += i;
|
spi_base_adress += i;
|
}
|
}
|
fpga_load_end();
|
fpga_load_end();
|
}
|
}
|
|
|
USBCS &= ~bmDISCON; /* reconnect USB */
|
USBCS &= ~bmDISCON; /* reconnect USB */
|
|
|
main_loop();
|
main_loop();
|
}
|
}
|
|
|