bool sha204_init(void) {
    uint8_t dataPage[SHA204_RSP_SIZE_MIN];
    int i;
    
    for (i=0; i++; i<SHA204_RSP_SIZE_MIN)
        dataPage[i]=0;
        
    bool ret=true;
    ret = sha204c_wakeup(dataPage)==SHA204_SUCCESS;

    if  (ret)
        ret = sha204_read_config_zone(config_data);

    return ret;
}
uint8_t atsha204Class::sha204c_resync(uint8_t size, uint8_t *response)
{
    // Try to re-synchronize without sending a Wake token
    // (step 1 of the re-synchronization process).
    uint8_t ret_code = sha204p_resync(size, response);
    if (ret_code == SHA204_SUCCESS)
        return ret_code;

    // We lost communication. Send a Wake pulse and try
    // to receive a response (steps 2 and 3 of the
    // re-synchronization process).
    (void)sha204p_sleep();
    ret_code = sha204c_wakeup(response);

    // Translate a return value of success into one
    // that indicates that the device had to be woken up
    // and might have lost its TempKey.
    return (ret_code == SHA204_SUCCESS ? SHA204_RESYNC_WITH_WAKEUP : ret_code);
}
Exemple #3
0
/**
 * \brief This function runs a SHA204 Wakeup / Sleep test.
 *
 * This test wakes up the device, reads its Wakeup response,
 * and sends a Sleep command. It then sends a command and
 * expects a response timeout to verify that the device has
 * gone to sleep.
 *
 * \param test current test case
 */
static void test_sha204_wakeup(const struct test_case *test)
{
	uint8_t sha204_status = SHA204_SUCCESS;

	// Catch watchdog timeout in case no Security Xplained board is connected.
	if ((reset_cause_get_causes() & CHIP_RESET_CAUSE_WDT) != CHIP_RESET_CAUSE_WDT) {
		reset_cause_clear_causes(CHIP_RESET_CAUSE_POR |
				CHIP_RESET_CAUSE_EXTRST |
				CHIP_RESET_CAUSE_BOD_CPU |
				CHIP_RESET_CAUSE_OCD |
				CHIP_RESET_CAUSE_SOFT | CHIP_RESET_CAUSE_SPIKE);
	} else {
		wdt_disable();
		reset_cause_clear_causes(CHIP_RESET_CAUSE_WDT);
		test_assert_false(test, sha204_status == SHA204_SUCCESS, "No Device.");
		return;
	}

	/* Start watchdog timer. */
	wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_250CLK); // 250 ms
	wdt_enable();
			
	uint8_t response[DEVREV_RSP_SIZE];
	
	success = false;
	
	sha204_status = sha204c_wakeup(response);
	// The TWI_M driver is not hanging. We can disable the watchdog now.
	wdt_disable();
	test_assert_true(test, sha204_status == SHA204_SUCCESS, "Sending Wakeup token failed.");
	
	sha204_status = sha204p_sleep();
	test_assert_true(test, sha204_status == SHA204_SUCCESS, "Sending Sleep command failed.");
	
	// Make sure the device is asleep. The code below works only for TWI, not for SWI,
	// because there is no acknowledging of a TWI address for SWI.
	uint8_t command[DEVREV_COUNT] = {DEVREV_COUNT, SHA204_DEVREV, 0, 0, 0, 0x03, 0x5d};
	sha204_status = sha204p_send_command(sizeof(command), command);
	test_assert_false(test, sha204_status == SHA204_SUCCESS, "Device is not asleep.");

	success = true;
}
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;
}