/* 	Puts a the ATSHA204's unique, 4-byte serial number in the response array
returns an SHA204 Return code */
uint8_t atsha204Class::getSerialNumber(uint8_t * response)
{
    uint8_t readCommand[READ_COUNT];
    uint8_t readResponse[READ_4_RSP_SIZE];

    /* read from bytes 0->3 of config zone */
    uint8_t returnCode = sha204m_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN03);
    if (!returnCode)	// should return 0 if successful
    {
        for (int i = 0; i<4; i++)	// store bytes 0-3 into respones array
            response[i] = readResponse[SHA204_BUFFER_POS_DATA + i];

        /* read from bytes 8->11 of config zone */
        returnCode = sha204m_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN47);

        for (int i = 4; i<8; i++)	// store bytes 4-7 of SN into response array
            response[i] = readResponse[SHA204_BUFFER_POS_DATA + (i - 4)];

        if (!returnCode)
        {   /* Finally if last two reads were successful, read byte 8 of the SN */
            returnCode = sha204m_read(readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN8);
            response[8] = readResponse[SHA204_BUFFER_POS_DATA];	// Byte 8 of SN should always be 0xEE
        }
    }

    return returnCode;
}
示例#2
0
/**
 * \brief This function runs a SHA204 Read test on all four devices.
 *
 * This test wakes up the devices, reads their TWI addresses from
 * their Config zone, and sends them back to sleep. The test fails if
 * there are communication failures or the TWI addresses do not match.
 *
 * \param test current test case
 */
static void test_all_devices(const struct test_case *test)
{
	test_assert_true(test, success, "Previous test failed.");
	success = false;
	
	uint8_t sha204_status;
	uint8_t i;
	uint8_t twi_address;
	uint8_t twi_address_retrieved = 0;
	uint8_t command[READ_COUNT];
	uint8_t response[READ_4_RSP_SIZE];
	
	sha204_status = sha204p_wakeup();
	test_assert_true(test, sha204_status == SHA204_SUCCESS, "Sending Wakeup token failed.");

	// Address 16 holds the TWI address.
	struct sha204_read_parameters args = 
			{.tx_buffer = command, .rx_buffer = response, .zone = SHA204_ZONE_CONFIG, .address = 16};
	
	// Read TWI address from all four devices that are mounted on the Security Xplained extension board.
	for (i = 0; i < SHA204_DEVICE_COUNT; i++) {
		twi_address = sha204_i2c_address(i);
		sha204p_set_device_id(twi_address);

		memset(response, 0, sizeof(response));
		success = false;
		sha204_status = sha204m_read(&args);
		if (sha204_status != SHA204_SUCCESS)
			break;
		twi_address_retrieved = args.rx_buffer[SHA204_BUFFER_POS_DATA] & 0xFE;
		if (twi_address_retrieved != twi_address)
			break;

		sha204_status = sha204p_sleep();
		if (sha204_status != SHA204_SUCCESS)
			break;

		success = true;
	}
	// Sleep remaining devices in case one of them failed.
	for (; i < SHA204_DEVICE_COUNT; i++) {
		twi_address = sha204_i2c_address(i);
		sha204p_set_device_id(twi_address);
		sha204p_sleep();
	}	
	test_assert_true(test, sha204_status == SHA204_SUCCESS, "Communication error.");
	test_assert_true(test, twi_address_retrieved == twi_address, "TWI address does not match.");

	success = true;
}


//! \name Unit test configuration
//@{
/**
 * \def CONF_TEST_USART
 * \brief USART to redirect STDIO to
 */
/**
 * \def CONF_TEST_BAUDRATE
 * \brief Baudrate of USART
 */
/**
 * \def CONF_TEST_CHARLENGTH
 * \brief Character length (bits) of USART
 */
/**
 * \def CONF_TEST_PARITY
 * \brief Parity mode of USART
 */
/**
 * \def CONF_TEST_STOPBITS
 * \brief Stop bit configuration of USART
 */
//@}

/**
 * \brief This function runs ATSHA204 component unit tests.
 */
int main (void)
{
	const usart_serial_options_t usart_serial_options =
	{
		.baudrate     = CONF_TEST_BAUDRATE,
		.charlength   = CONF_TEST_CHARLENGTH,
		.paritytype   = CONF_TEST_PARITY,
		.stopbits     = CONF_TEST_STOPBITS,
	};

	sysclk_init();
	board_init();
	pmic_init();
	sleepmgr_init();
	stdio_serial_init(CONF_TEST_USART, &usart_serial_options);

	// Enable low level interrupts
	pmic_enable_level(PMIC_LVL_LOW);

	// Enable interrupt requests
	cpu_irq_enable();

	// Define all the test cases
	DEFINE_TEST_CASE(sha204_test1, NULL, test_sha204_wakeup, NULL,
			"Testing Wakeup / Sleep");

	DEFINE_TEST_CASE(sha204_test2, NULL, test_all_devices, NULL,
			"Testing all devices");

	// Put test case addresses in an array
	DEFINE_TEST_ARRAY(sha204_tests) = {
		&sha204_test1,
		&sha204_test2
	};

	// Define the test suite
	DEFINE_TEST_SUITE(sha204_suite, sha204_tests,
			"XMEGA ATSHA204 component test suite");

	// Run all tests in the test suite
	test_suite_run(&sha204_suite);

	while (1) {
		// Loop for infinity
	}
}
示例#3
0
uint8_t sha204_read_config_zone(uint8_t *config_data)
{
    // declared as "volatile" for easier debugging
    volatile uint8_t ret_code;

    uint16_t config_address;

    // Make the command buffer the size of the Read command.
    uint8_t command[READ_COUNT];

    // Make the response buffer the size of the maximum Read response.
    uint8_t response[READ_32_RSP_SIZE];

    // Use this buffer to read the last 24 bytes in 4-byte junks.
    uint8_t response_read_4[READ_4_RSP_SIZE];

    uint8_t *p_response;


    // Read first 32 bytes. Put a breakpoint after the read and inspect "response" to obtain the data.

    memset(response, 0, sizeof(response));
    config_address = 0;
    ret_code = sha204m_read(command, response, SHA204_ZONE_CONFIG | READ_ZONE_MODE_32_BYTES, config_address);
    //sha204p_sleep(); IS THIS REALLY NEED ??????
    if (ret_code != SHA204_SUCCESS)
        return ret_code;

    if (config_data) {
        memcpy(config_data, &response[SHA204_BUFFER_POS_DATA], SHA204_ZONE_ACCESS_32);
        config_data += SHA204_ZONE_ACCESS_32;
    }
    // Read second 32 bytes. Put a breakpoint after the read and inspect "response" to obtain the data.
    config_address += SHA204_ZONE_ACCESS_32;
    memset(response, 0, sizeof(response));
    ret_code = sha204m_read(command, response, SHA204_ZONE_CONFIG | READ_ZONE_MODE_32_BYTES, config_address);
    //sha204p_sleep();IS THIS REALLY NEED ??????
    if (ret_code != SHA204_SUCCESS)
        return ret_code;

    if (config_data) {
        memcpy(config_data, &response[SHA204_BUFFER_POS_DATA], SHA204_ZONE_ACCESS_32);
        config_data += SHA204_ZONE_ACCESS_32;
    }

    // Read last 24 bytes in six four-byte junks.
    memset(response, 0, sizeof(response));
    ret_code = sha204c_wakeup(response); 
	if (ret_code != SHA204_SUCCESS)
		return ret_code;
     
    config_address += SHA204_ZONE_ACCESS_32;
    response[SHA204_BUFFER_POS_COUNT] = 0;
    p_response = &response[SHA204_BUFFER_POS_DATA];
    memset(response, 0, sizeof(response));
    while (config_address < SHA204_CONFIG_SIZE) {
        memset(response_read_4, 0, sizeof(response_read_4));
        ret_code = sha204m_read(command, response_read_4, SHA204_ZONE_CONFIG, config_address);
        if (ret_code != SHA204_SUCCESS) {
            //sha204p_sleep();IS THIS REALLY NEED ??????
            return ret_code;
        }
        memcpy(p_response, &response_read_4[SHA204_BUFFER_POS_DATA], SHA204_ZONE_ACCESS_4);
        p_response += SHA204_ZONE_ACCESS_4;
        response[SHA204_BUFFER_POS_COUNT] += SHA204_ZONE_ACCESS_4; // Update count byte in virtual response packet.
        config_address += SHA204_ZONE_ACCESS_4;
    }
    // Put a breakpoint here and inspect "response" to obtain the data.
    //sha204p_sleep();IS THIS REALLY NEED ??????

    if (ret_code == SHA204_SUCCESS && config_data)
        memcpy(config_data, &response[SHA204_BUFFER_POS_DATA], SHA204_CONFIG_SIZE - 2 * SHA204_ZONE_ACCESS_32);

    return ret_code;
}