Skip to content

Commit

Permalink
Implement Idle, Reset and Sleep
Browse files Browse the repository at this point in the history
Signed-off-by: Michalis Pappas <mpappas@fastmail.fm>
  • Loading branch information
michpappas authored and jbech-linaro committed May 23, 2018
1 parent 2ec8783 commit a444cec
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 6 deletions.
1 change: 1 addition & 0 deletions s96at/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ set(SRC ${CMAKE_SOURCE_DIR}/src/s96at.c
${CMAKE_SOURCE_DIR}/src/cmd.c
${CMAKE_SOURCE_DIR}/src/crc.c
${CMAKE_SOURCE_DIR}/src/debug.c
${CMAKE_SOURCE_DIR}/src/device.c
${CMAKE_SOURCE_DIR}/src/io.c
${CMAKE_SOURCE_DIR}/src/i2c_linux.c
${CMAKE_SOURCE_DIR}/src/packet.c
Expand Down
6 changes: 0 additions & 6 deletions s96at/include/cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,6 @@ enum {
#define SHA_MODE_INIT 0x00
#define SHA_MODE_COMPUTE 0x01

/* Word address values */
#define PKT_FUNC_RESET 0x0
#define PKT_FUNC_SLEEP 0x1
#define PKT_FUNC_IDLE 0x2
#define PKT_FUNC_COMMAND 0x3

/* OP-codes for each command, see section 8.5.4 in spec */
#define OPCODE_DERIVEKEY 0x1c
#define OPCODE_DEVREV 0x30
Expand Down
14 changes: 14 additions & 0 deletions s96at/include/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,21 @@
#ifndef __DEVICE_H
#define __DEVICE_H

#include <io.h>

/* Default I2C address for the ATSHA204a */
#define ATSHA204A_ADDR 0x64

/* Word address values */
#define PKT_FUNC_RESET 0x0
#define PKT_FUNC_SLEEP 0x1
#define PKT_FUNC_IDLE 0x2
#define PKT_FUNC_COMMAND 0x3

uint8_t device_idle(struct io_interface *ioif);

uint8_t device_reset(struct io_interface *ioif);

uint8_t device_sleep(struct io_interface *ioif);

#endif
28 changes: 28 additions & 0 deletions s96at/include/s96at.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,34 @@ uint8_t s96at_lock_zone(struct s96at_desc *desc, enum s96at_zone zone, uint16_t
*/
uint8_t s96at_read(struct s96at_desc *desc, enum s96at_zone zone, uint8_t id, uint8_t *buf);

/* Put the device into the sleep state
*
* Puts the device in low-power sleep. The device does not respond until the
* next wakeup is sent. The volatile state of the device is reset.
*
* Returns S96AT_STATUS_OK on success, otherwise S96AT_STATUS_EXEC_ERROR.
*/
uint8_t s96at_sleep(struct s96at_desc *desc);

/* Put the device into the idle state
*
* Puts the device into the idle state. The device does not respond until the
* next wakeup is sent. The contents of RNG seed register and TempKey are
* retained.
*
* Returns S96AT_STATUS_OK on success, otherwise S96AT_STATUS_EXEC_ERROR.
*/
uint8_t s96at_idle(struct s96at_desc *desc);

/* Reset the address counter
*
* Resets the address counter. This allows re-reading the device's output
* buffer.
*
* Returns always S96AT_SUCCESS.
*/
uint8_t s96at_reset(struct s96at_desc *desc);

/* Wake up the device
*
* Wakes up the device by sending the wake-up sequence. Upon wake up, a watchdog
Expand Down
1 change: 1 addition & 0 deletions s96at/src/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <cmd.h>
#include <crc.h>
#include <debug.h>
#include <device.h>
#include <packet.h>
#include <status.h>

Expand Down
59 changes: 59 additions & 0 deletions s96at/src/device.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2017, Linaro Ltd and contributors
* SPDX-License-Identifier: Apache-2.0
*/
#include <stddef.h>
#include <stdint.h>

#include <device.h>
#include <io.h>
#include <packet.h>
#include <status.h>

uint8_t device_idle(struct io_interface *ioif)
{
uint8_t ret;
uint8_t data;
uint8_t word_addr = PKT_FUNC_IDLE;

at204_write(ioif, &word_addr, sizeof(word_addr));

/* If idle was successful, we expect a NAK on read */
ret = at204_read(ioif, &data, sizeof(data));

if (ret != STATUS_OK)
ret = STATUS_OK;
else
ret = STATUS_EXEC_ERROR;

return ret;
}

uint8_t device_sleep(struct io_interface *ioif)
{
uint8_t ret;
uint8_t data;
uint8_t word_addr = PKT_FUNC_SLEEP;

at204_write(ioif, &word_addr, sizeof(word_addr));

/* If sleep was successful, we expect a NAK on read */
ret = at204_read(ioif, &data, sizeof(data));

if (ret != STATUS_OK)
ret = STATUS_OK;
else
ret = STATUS_EXEC_ERROR;

return ret;
}

uint8_t device_reset(struct io_interface *ioif)
{
uint8_t word_addr = PKT_FUNC_RESET;

at204_write(ioif, &word_addr, sizeof(word_addr));

return STATUS_OK;
}

16 changes: 16 additions & 0 deletions s96at/src/s96at.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <cmd.h>
#include <crc.h>
#include <device.h>
#include <io.h>
#include <s96at.h>
#include <sha.h>
Expand Down Expand Up @@ -37,6 +38,21 @@ uint8_t s96at_cleanup(struct s96at_desc *desc)
return ret;
}

uint8_t s96at_idle(struct s96at_desc *desc)
{
return device_idle(desc->ioif);
}

uint8_t s96at_reset(struct s96at_desc *desc)
{
return device_reset(desc->ioif);
}

uint8_t s96at_sleep(struct s96at_desc *desc)
{
return device_sleep(desc->ioif);
}

uint8_t s96at_wake(struct s96at_desc *desc)
{
uint8_t ret;
Expand Down
26 changes: 26 additions & 0 deletions s96at/tests/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,23 @@ static int test_read_otp(void)
return memcmp(buf_a, buf_e, ARRAY_LEN(buf_e));
}

static int test_reset(void)
{
uint8_t ret;
uint8_t random1[S96AT_RANDOM_LEN];
uint8_t random2[S96AT_RANDOM_LEN];

ret = s96at_get_random(&desc, S96AT_RANDOM_MODE_UPDATE_SEED, random1);
CHECK_RES("Random 1", ret, random1, ARRAY_LEN(random1));

/* Now send a reset and read the FIFO again */
s96at_reset(&desc);
ret = at204_read(desc.ioif, random2, ARRAY_LEN(random2));
CHECK_RES("Random 2", ret, random2, ARRAY_LEN(random2));

return memcmp(random1, random2, ARRAY_LEN(random2));
}

int main(int argc, char *argv[])
{
uint8_t ret;
Expand All @@ -811,6 +828,7 @@ int main(int argc, char *argv[])
{"Read: Config", test_read_config},
{"Read: Data", test_read_data},
{"Read: OTP", test_read_otp},
{"Reset", test_reset},
{"SHA", test_sha},
{0, NULL}
};
Expand All @@ -832,6 +850,14 @@ int main(int argc, char *argv[])
ret = tests[i].func();
printf("%-30s %s\n", tests[i].name,
ret ? "\x1B[31m[FAIL]\033[0m" : "\x1B[32m[PASS]\033[0m");

/* Force an idle-wake cycle every 4 tests to prevent the watchdog
* from putting the device to sleep during the execution of a test.
*/
if ((i + 1) % 4 == 0) {
s96at_idle(&desc);
while (s96at_wake(&desc) != S96AT_STATUS_READY) {};
}
}
printf("Done\n");
out:
Expand Down

0 comments on commit a444cec

Please sign in to comment.