ATCA_STATUS hal_i2c_discover_devices(int busNum, ATCAIfaceCfg cfg[], int *found )
{
	ATCAIfaceCfg *head = cfg;
	uint8_t slaveAddress = 0x01;
	ATCADevice device;
	ATCAIface discoverIface;
	ATCACommand command;
	ATCAPacket packet;
	uint32_t execution_time;
	ATCA_STATUS status;
	uint8_t revs508[1][4] = { { 0x00, 0x00, 0x50, 0x00 } };
	uint8_t revs108[1][4] = { { 0x80, 0x00, 0x10, 0x01 } };
	uint8_t revs204[2][4] = { { 0x00, 0x02, 0x00, 0x08 },
							  { 0x00, 0x04, 0x05, 0x00 } };
	int i;

	/** \brief default configuration, to be reused during discovery process */
	ATCAIfaceCfg discoverCfg = {
		.iface_type				= ATCA_I2C_IFACE,
		.devtype				= ATECC508A,
		.atcai2c.slave_address	= 0x07,
		.atcai2c.bus			= busNum,
		.atcai2c.baud			= 400000,
		//.atcai2c.baud = 100000,
		.wake_delay				= 800,
		.rx_retries				= 3
	};

	// build an info command
	packet.param1 = INFO_MODE_REVISION;
	packet.param2 = 0;

	device = newATCADevice( &discoverCfg );
	discoverIface = atGetIFace( device );
	command = atGetCommands( device );

	// iterate through all addresses on given i2c bus
	// all valid 7-bit addresses go from 0x07 to 0x78
	for ( slaveAddress = 0x07; slaveAddress <= 0x78; slaveAddress++ ) {
		discoverCfg.atcai2c.slave_address = slaveAddress << 1;  // turn it into an 8-bit address which is what the rest of the i2c HAL is expecting when a packet is sent

		// wake up device
		// If it wakes, send it a dev rev command.  Based on that response, determine the device type
		// BTW - this will wake every cryptoauth device living on the same bus (ecc508a, sha204a)

		if ( hal_i2c_wake( discoverIface ) == ATCA_SUCCESS ) {
			(*found)++;
			memcpy( (uint8_t*)head, (uint8_t*)&discoverCfg, sizeof(ATCAIfaceCfg));

			memset( packet.info, 0x00, sizeof(packet.info));

			// get devrev info and set device type accordingly
			atInfo( command, &packet );
			execution_time = atGetExecTime(command, CMD_INFO) + 1;

			// send the command
			if ( (status = atsend( discoverIface, (uint8_t*)&packet, packet.txsize )) != ATCA_SUCCESS ) {
				printf("packet send error\r\n");
				continue;
			}

			// delay the appropriate amount of time for command to execute
			atca_delay_ms(execution_time);

			// receive the response
			if ( (status = atreceive( discoverIface, &(packet.info[0]), &(packet.rxsize) )) != ATCA_SUCCESS )
				continue;

			if ( (status = isATCAError(packet.info)) != ATCA_SUCCESS ) {
				printf("command response error\r\n");
				continue;
			}

			// determine device type from common info and dev rev response byte strings
			for ( i = 0; i < sizeof(revs508) / 4; i++ ) {
				if ( memcmp( &packet.info[1], &revs508[i], 4) == 0 ) {
					discoverCfg.devtype = ATECC508A;
					break;
				}
			}

			for ( i = 0; i < sizeof(revs204) / 4; i++ ) {
				if ( memcmp( &packet.info[1], &revs204[i], 4) == 0 ) {
					discoverCfg.devtype = ATSHA204A;
					break;
				}
			}

			for ( i = 0; i < sizeof(revs108) / 4; i++ ) {
				if ( memcmp( &packet.info[1], &revs108[i], 4) == 0 ) {
					discoverCfg.devtype = ATECC108A;
					break;
				}
			}

			atca_delay_ms(15);
			// now the device type is known, so update the caller's cfg array element with it
			head->devtype = discoverCfg.devtype;
			head++;
		}

		hal_i2c_idle(discoverIface);
	}

	deleteATCADevice(&device);

	return ATCA_SUCCESS;
}


/** \brief
    - this HAL implementation assumes you've included the ASF I2C libraries in your project, otherwise,
    the HAL layer will not compile because the ASF I2C drivers are a dependency *
 */

/** \brief hal_i2c_init manages requests to initialize a physical interface.  it manages use counts so when an interface
 * has released the physical layer, it will disable the interface for some other use.
 * You can have multiple ATCAIFace instances using the same bus, and you can have multiple ATCAIFace instances on
 * multiple i2c buses, so hal_i2c_init manages these things and ATCAIFace is abstracted from the physical details.
 */

/** \brief initialize an I2C interface using given config
 * \param[in] hal - opaque ptr to HAL data
 * \param[in] cfg - interface configuration
 */
ATCA_STATUS hal_i2c_init(void *hal, ATCAIfaceCfg *cfg)
{
	int bus = cfg->atcai2c.bus;   // 0-based logical bus number
	ATCAHAL_t *phal = (ATCAHAL_t*)hal;

	if ( i2c_bus_ref_ct == 0 )     // power up state, no i2c buses will have been used
		for ( int i = 0; i < MAX_I2C_BUSES; i++ )
			i2c_hal_data[i] = NULL;

	i2c_bus_ref_ct++;  // total across buses

	if ( bus >= 0 && bus < MAX_I2C_BUSES ) {
		// if this is the first time this bus and interface has been created, do the physical work of enabling it
		if ( i2c_hal_data[bus] == NULL ) {
			i2c_hal_data[bus] = malloc( sizeof(ATCAI2CMaster_t) );
			i2c_hal_data[bus]->ref_ct = 1;  // buses are shared, this is the first instance

			config_i2c_master.speed = cfg->atcai2c.baud;
			config_i2c_master.chip  = 0x50;
			config_i2c_master.speed_reg = TWI_BAUD(sysclk_get_cpu_hz(), cfg->atcai2c.baud);
			switch (bus) {
			case 0: i2c_hal_data[bus]->i2c_master_instance = &TWIC; break;
			//case 1: i2c_hal_data[bus]->i2c_master_instance = &TWID; break;	// for XMEGA-A1
			case 2: i2c_hal_data[bus]->i2c_master_instance = &TWIE; break;
				//case 3: i2c_hal_data[bus]->i2c_master_instance = &TWIF; break;	// for XMEGA-A1
			}

			twi_master_setup((i2c_hal_data[bus]->i2c_master_instance), &config_i2c_master);
			// store this for use during the release phase
			i2c_hal_data[bus]->bus_index = bus;
			twi_master_enable(i2c_hal_data[bus]->i2c_master_instance);
		}  else{
			// otherwise, another interface already initialized the bus, so this interface will share it and any different
			// cfg parameters will be ignored...first one to initialize this sets the configuration
			i2c_hal_data[bus]->ref_ct++;
		}

		phal->hal_data = i2c_hal_data[bus];

		return ATCA_SUCCESS;
	}

	return ATCA_COMM_FAIL;
}
예제 #2
0
ATCA_STATUS hal_swi_discover_devices(int busNum, ATCAIfaceCfg cfg[], int *found )
{
	ATCAIfaceCfg *head = cfg;
	ATCADevice device;
	ATCAIface discoverIface;
	ATCACommand command;
	ATCAPacket packet;
	uint32_t execution_time;
	ATCA_STATUS status;
	uint8_t revs508[1][4] = { { 0x00, 0x00, 0x50, 0x00 } };
	uint8_t revs108[1][4] = { { 0x80, 0x00, 0x10, 0x01 } };
	uint8_t revs204[2][4] = { { 0x00, 0x02, 0x00, 0x08 },
							  { 0x00, 0x04, 0x05, 0x00 } };
	unsigned int i; i;

	/** \brief default configuration, to be reused during discovery process */
	ATCAIfaceCfg discoverCfg = {
		.iface_type		= ATCA_SWI_IFACE,
		.devtype		= ATECC508A,
		.atcaswi.bus	= busNum,
		.wake_delay		= 800,
		.rx_retries		= 3
	};

	// build an info command
	packet.param1 = INFO_MODE_REVISION;
	packet.param2 = 0;

	device = newATCADevice( &discoverCfg );
	discoverIface = atGetIFace( device );
	command = atGetCommands( device );

	// wake up device
	// If it wakes, send it a dev rev command.  Based on that response, determine the device type
	// BTW - this will wake every cryptoauth device living on the same bus (ecc508a, sha204a)

	if ( hal_swi_wake( discoverIface ) == ATCA_SUCCESS ) {
		(*found)++;
		memcpy( (uint8_t*)head, (uint8_t*)&discoverCfg, sizeof(ATCAIfaceCfg));

		memset( packet.data, 0x00, sizeof(packet.data));

		// get devrev info and set device type accordingly
		atInfo( command, &packet );
		execution_time = atGetExecTime(command, CMD_INFO) + 1;

		// send the command
		if ( (status = atsend( discoverIface, (uint8_t*)&packet, packet.txsize )) != ATCA_SUCCESS )
			printf("packet send error\r\n");

		// delay the appropriate amount of time for command to execute
		atca_delay_ms(execution_time);

		// receive the response
		if ( (status = atreceive( discoverIface, &(packet.data[0]), &(packet.rxsize) )) != ATCA_SUCCESS ) {
		}

		if ( (status = isATCAError(packet.data)) != ATCA_SUCCESS ) {
			printf("command response error\r\n");
			printf("0x%.2X %.2X %.2X %.2X\r\n", packet.data[0], packet.data[1], packet.data[2], packet.data[3]);
		}

		// determine device type from common info and dev rev response byte strings
		for ( i = 0; i < sizeof(revs508) / 4; i++ ) {
			if ( memcmp( &packet.data[1], &revs508[i], 4) == 0 ) {
				discoverCfg.devtype = ATECC508A;
				break;
			}
		}

		for ( i = 0; i < sizeof(revs204) / 4; i++ ) {
			if ( memcmp( &packet.data[1], &revs204[i], 4) == 0 ) {
				discoverCfg.devtype = ATSHA204A;
				break;
			}
		}

		for ( i = 0; i < sizeof(revs108) / 4; i++ ) {
			if ( memcmp( &packet.data[1], &revs108[i], 4) == 0 ) {
				discoverCfg.devtype = ATECC108A;
				break;
			}
		}

		atca_delay_ms(15);
		// now the device type is known, so update the caller's cfg array element with it
		head->devtype = discoverCfg.devtype;

		hal_swi_idle(discoverIface);
	}

	deleteATCADevice(&device);

	return ATCA_SUCCESS;
}

/** \brief hal_swi_init manages requests to initialize a physical interface.  it manages use counts so when an interface
 * has released the physical layer, it will disable the interface for some other use.
 * You can have multiple ATCAIFace instances using the same bus, and you can have multiple ATCAIFace instances on
 * multiple swi buses, so hal_swi_init manages these things and ATCAIFace is abstracted from the physical details.
 */

/** \brief initialize an SWI interface using given config
 * \param[in] hal - opaque ptr to HAL data
 * \param[in] cfg - interface configuration
 */

ATCA_STATUS hal_swi_init(void *hal, ATCAIfaceCfg *cfg)
{
	int bus = cfg->atcaswi.bus;   // 0-based logical bus number
	ATCAHAL_t *phal = (ATCAHAL_t*)hal;

	if ( swi_bus_ref_ct == 0 ) {   // power up state, no swi buses will have been used
		for ( int i = 0; i < MAX_SWI_BUSES; i++ )
			swi_hal_data[i] = NULL;
		swi_bus_ref_ct++;  // total across buses become 1
	}

	if ( bus >= 0 && bus < MAX_SWI_BUSES ) {
		// if this is the first time this bus and interface has been created, do the physical work of enabling it
		if ( swi_hal_data[bus] == NULL ) {
			swi_hal_data[bus] = malloc( sizeof(ATCASWIMaster_t) );
			// store this for use during the release phase
			swi_hal_data[bus]->bus_index = bus;
			// initialize  UART module for SWI interface
			swi_uart_init(swi_hal_data[bus]);
		}  else{
			// otherwise, another interface already initialized the bus, any different
			// cfg parameters will be ignored...first one to initialize this sets the configuration
		}
		phal->hal_data = swi_hal_data[bus];

		return ATCA_SUCCESS;
	}
	return ATCA_COMM_FAIL;
}