From f9cd3449f374ab7f3f492607aa9b51a0de15098f Mon Sep 17 00:00:00 2001 From: Atis Elsts Date: Tue, 18 Dec 2018 16:21:15 +0000 Subject: [PATCH 1/7] add tsch_get_network_uptime_ticks() function --- os/net/mac/tsch/tsch-slot-operation.c | 48 ++++++++++++++++++++++++--- os/net/mac/tsch/tsch-slot-operation.h | 2 +- os/net/mac/tsch/tsch.h | 6 ++++ 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/os/net/mac/tsch/tsch-slot-operation.c b/os/net/mac/tsch/tsch-slot-operation.c index a801bc92e..4397a4805 100644 --- a/os/net/mac/tsch/tsch-slot-operation.c +++ b/os/net/mac/tsch/tsch-slot-operation.c @@ -52,6 +52,7 @@ #include "net/queuebuf.h" #include "net/mac/framer/framer-802154.h" #include "net/mac/tsch/tsch.h" +#include "sys/critical.h" #include "sys/log.h" /* TSCH debug macros, i.e. to set LEDs or GPIOs on various TSCH @@ -126,9 +127,10 @@ struct tsch_packet *dequeued_array[TSCH_DEQUEUED_ARRAY_SIZE]; struct ringbufindex input_ringbuf; struct input_packet input_array[TSCH_MAX_INCOMING_PACKETS]; +/* Updates and reads of the next two variables must be atomic (i.e. both together) */ /* Last time we received Sync-IE (ACK or data packet from a time source) */ static struct tsch_asn_t last_sync_asn; -clock_time_t last_sync_time; /* Same info, in clock_time_t units */ +clock_time_t tsch_last_sync_time; /* Same info, in clock_time_t units */ /* A global lock for manipulating data structures safely from outside of interrupt */ static volatile int tsch_locked = 0; @@ -347,6 +349,33 @@ get_packet_and_neighbor_for_link(struct tsch_link *link, struct tsch_neighbor ** return p; } /*---------------------------------------------------------------------------*/ +uint64_t +tsch_get_network_uptime_ticks(void) +{ + uint64_t uptime_asn; + uint64_t uptime_ticks; + int_master_status_t status; + + if(!tsch_is_associated) { + /* not associated, network uptime is not known */ + return (uint64_t)-1; + } + + status = critical_enter(); + + uptime_asn = last_sync_asn.ls4b + ((uint64_t)last_sync_asn.ms1b << 32); + /* first calculate the at the uptime at the last sync in rtimer ticks */ + uptime_ticks = uptime_asn * tsch_timing[tsch_ts_timeslot_length]; + /* then convert to clock ticks (assume that CLOCK_SECOND divides RTIMER_ARCH_SECOND) */ + uptime_ticks /= (RTIMER_ARCH_SECOND / CLOCK_SECOND); + /* then add the ticks passed since the last timesync */ + uptime_ticks += (clock_time() - tsch_last_sync_time); + + critical_exit(status); + + return uptime_ticks; +} +/*---------------------------------------------------------------------------*/ /** * This function turns on the radio. Its semantics is dependent on * the value of TSCH_RADIO_ON_DURING_TIMESLOT constant: @@ -623,7 +652,7 @@ PT_THREAD(tsch_tx_slot(struct pt *pt, struct rtimer *t)) tsch_timesync_update(current_neighbor, since_last_timesync, drift_correction); /* Keep track of sync time */ last_sync_asn = tsch_current_asn; - last_sync_time = clock_time(); + tsch_last_sync_time = clock_time(); tsch_schedule_keepalive(); } mac_tx_status = MAC_TX_OK; @@ -882,7 +911,7 @@ PT_THREAD(tsch_rx_slot(struct pt *pt, struct rtimer *t)) int32_t since_last_timesync = TSCH_ASN_DIFF(tsch_current_asn, last_sync_asn); /* Keep track of last sync time */ last_sync_asn = tsch_current_asn; - last_sync_time = clock_time(); + tsch_last_sync_time = clock_time(); /* Save estimated drift */ drift_correction = -estimated_drift; is_drift_correction_used = 1; @@ -1014,6 +1043,13 @@ PT_THREAD(tsch_slot_operation(struct rtimer *t, void *ptr)) /* End of slot operation, schedule next slot or resynchronize */ + if(tsch_is_coordinator) { + /* Update the `last_sync_*` variables to avoid large errors + * in the application-level time synchronization */ + last_sync_asn = tsch_current_asn; + tsch_last_sync_time = clock_time(); + } + /* Do we need to resynchronize? i.e., wait for EB again */ if(!tsch_is_coordinator && (TSCH_ASN_DIFF(tsch_current_asn, last_sync_asn) > (100 * TSCH_CLOCK_TO_SLOTS(TSCH_DESYNC_THRESHOLD / 100, tsch_timing[tsch_ts_timeslot_length])))) { @@ -1115,10 +1151,14 @@ void tsch_slot_operation_sync(rtimer_clock_t next_slot_start, struct tsch_asn_t *next_slot_asn) { + int_master_status_t status; + current_slot_start = next_slot_start; tsch_current_asn = *next_slot_asn; + status = critical_enter(); last_sync_asn = tsch_current_asn; - last_sync_time = clock_time(); + tsch_last_sync_time = clock_time(); + critical_exit(status); current_link = NULL; } /*---------------------------------------------------------------------------*/ diff --git a/os/net/mac/tsch/tsch-slot-operation.h b/os/net/mac/tsch/tsch-slot-operation.h index b1c05697b..22547a2c9 100644 --- a/os/net/mac/tsch/tsch-slot-operation.h +++ b/os/net/mac/tsch/tsch-slot-operation.h @@ -56,7 +56,7 @@ extern struct tsch_packet *dequeued_array[TSCH_DEQUEUED_ARRAY_SIZE]; extern struct ringbufindex input_ringbuf; extern struct input_packet input_array[TSCH_MAX_INCOMING_PACKETS]; /* Last clock_time_t where synchronization happened */ -extern clock_time_t last_sync_time; +extern clock_time_t tsch_last_sync_time; /* Counts the length of the current burst */ extern int tsch_current_burst_count; diff --git a/os/net/mac/tsch/tsch.h b/os/net/mac/tsch/tsch.h index db23b4851..8674ee31a 100644 --- a/os/net/mac/tsch/tsch.h +++ b/os/net/mac/tsch/tsch.h @@ -226,6 +226,12 @@ void tsch_schedule_keepalive(void); * Schedule a keep-alive immediately */ void tsch_schedule_keepalive_immediately(void); +/** + * Get the time, in clock ticks, since the TSCH network was started. + * + * \return The network uptime, or -1 if the node is not part of a TSCH network. + */ +uint64_t tsch_get_network_uptime_ticks(void); /** * Leave the TSCH network we are currently in */ From 08cbbe224b7ec3148f4d55ea3afe95f66717b38f Mon Sep 17 00:00:00 2001 From: Atis Elsts Date: Tue, 18 Dec 2018 16:21:43 +0000 Subject: [PATCH 2/7] minor shell update: extend the tsch-status command --- os/services/shell/shell-commands.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/os/services/shell/shell-commands.c b/os/services/shell/shell-commands.c index c8b7d862e..2d0bdfed0 100644 --- a/os/services/shell/shell-commands.c +++ b/os/services/shell/shell-commands.c @@ -584,8 +584,12 @@ PT_THREAD(cmd_tsch_status(struct pt *pt, shell_output_func output, char *args)) } else { SHELL_OUTPUT(output, "none\n"); } - SHELL_OUTPUT(output, "-- Last synchronized: %lu seconds ago\n", (clock_time() - last_sync_time) / CLOCK_SECOND); - SHELL_OUTPUT(output, "-- Drift w.r.t. coordinator: %ld ppm\n", tsch_adaptive_timesync_get_drift_ppm()); + SHELL_OUTPUT(output, "-- Last synchronized: %lu seconds ago\n", + (clock_time() - tsch_last_sync_time) / CLOCK_SECOND); + SHELL_OUTPUT(output, "-- Drift w.r.t. coordinator: %ld ppm\n", + tsch_adaptive_timesync_get_drift_ppm()); + SHELL_OUTPUT(output, "-- Network uptime: %lu seconds\n", + (unsigned long)(tsch_get_network_uptime_ticks() / CLOCK_SECOND)); } PT_END(pt); From 99a68e84f193ac6a70d13c12234dda793e0c698e Mon Sep 17 00:00:00 2001 From: Atis Elsts Date: Tue, 18 Dec 2018 16:22:37 +0000 Subject: [PATCH 3/7] add the TSCH application-level timesync example --- examples/6tisch/timesync-demo/Makefile | 24 ++++ examples/6tisch/timesync-demo/README.md | 18 +++ examples/6tisch/timesync-demo/node.c | 139 +++++++++++++++++++ examples/6tisch/timesync-demo/project-conf.h | 83 +++++++++++ 4 files changed, 264 insertions(+) create mode 100644 examples/6tisch/timesync-demo/Makefile create mode 100644 examples/6tisch/timesync-demo/README.md create mode 100644 examples/6tisch/timesync-demo/node.c create mode 100644 examples/6tisch/timesync-demo/project-conf.h diff --git a/examples/6tisch/timesync-demo/Makefile b/examples/6tisch/timesync-demo/Makefile new file mode 100644 index 000000000..cd18192d0 --- /dev/null +++ b/examples/6tisch/timesync-demo/Makefile @@ -0,0 +1,24 @@ +CONTIKI_PROJECT = node +all: $(CONTIKI_PROJECT) + +CONTIKI=../../.. + +PLATFORMS_EXCLUDE = sky nrf52dk native + +# force Orchestra from command line +MAKE_WITH_ORCHESTRA ?= 0 +# force Security from command line +MAKE_WITH_SECURITY ?= 0 + +MAKE_MAC = MAKE_MAC_TSCH +MODULES += os/services/shell + +ifeq ($(MAKE_WITH_ORCHESTRA),1) +MODULES += os/services/orchestra +endif + +ifeq ($(MAKE_WITH_SECURITY),1) +CFLAGS += -DWITH_SECURITY=1 +endif + +include $(CONTIKI)/Makefile.include diff --git a/examples/6tisch/timesync-demo/README.md b/examples/6tisch/timesync-demo/README.md new file mode 100644 index 000000000..cf130ee73 --- /dev/null +++ b/examples/6tisch/timesync-demo/README.md @@ -0,0 +1,18 @@ +This is a demonstration of application-level time synchronization using TSCH. + +In a TSCH network, all nodes are synchronized to a global time counter maintained +by the coordinator. This fact can be exploited to measure properties such as latency. + +The modes periodically send their detected network uptime to the coordinator. +The coordinator receives these packets, prints its local network uptime +and the time difference. This time difference is equal to the end-to-end latency, +which includes both the time to prepare the packet, the time until an appropriate +slot in the TSCH schedule, the over-the-air time (negligible). If the packet +does not arrive with the first attempt, it also includes the retransmission time. + +The nodes in this example do not have any notion of the wall-clock time. +That would need additional synchronization between the TSCH network and an external clock source. +For example, one can periodically distribute UNIX timestamps over the UART interface +on the border router to implement this feature. Alternatively, the data collected from +the TSCH network can be timestamped with just TSCH timestamps, and them on the external gateway +these timestamps could be converted to wall-clock time. \ No newline at end of file diff --git a/examples/6tisch/timesync-demo/node.c b/examples/6tisch/timesync-demo/node.c new file mode 100644 index 000000000..408421cec --- /dev/null +++ b/examples/6tisch/timesync-demo/node.c @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2015, SICS Swedish ICT. + * Copyright (c) 2018, University of Bristol - http://www.bristol.ac.uk + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ +/** + * \file + * A RPL+TSCH node demonstrating application-level time syncrhonization. + * + * \author Atis Elsts + * Simon Duquennoy + */ + +#include "contiki.h" +#include "net/routing/routing.h" +#include "net/netstack.h" +#include "net/ipv6/simple-udp.h" +#include "net/mac/tsch/tsch.h" +#include "lib/random.h" +#include "sys/node-id.h" + +#include "sys/log.h" +#define LOG_MODULE "App" +#define LOG_LEVEL LOG_LEVEL_INFO + +#define UDP_CLIENT_PORT 8765 +#define UDP_SERVER_PORT 5678 + +#define SEND_INTERVAL (60 * CLOCK_SECOND) +/*---------------------------------------------------------------------------*/ +static struct simple_udp_connection client_conn, server_conn; + +PROCESS(node_process, "RPL Node"); +AUTOSTART_PROCESSES(&node_process); +/*---------------------------------------------------------------------------*/ +static void +udp_rx_callback(struct simple_udp_connection *c, + const uip_ipaddr_t *sender_addr, + uint16_t sender_port, + const uip_ipaddr_t *receiver_addr, + uint16_t receiver_port, + const uint8_t *data, + uint16_t datalen) +{ + uint64_t local_time_clock_ticks = tsch_get_network_uptime_ticks(); + uint64_t remote_time_clock_ticks; + + if(datalen >= sizeof(remote_time_clock_ticks)) { + memcpy(&remote_time_clock_ticks, data, sizeof(remote_time_clock_ticks)); + + LOG_INFO("Received from "); + LOG_INFO_6ADDR(sender_addr); + LOG_INFO_(", created at %lu, now %lu, latency %lu clock ticks\n", + (unsigned long)remote_time_clock_ticks, + (unsigned long)local_time_clock_ticks, + (unsigned long)(local_time_clock_ticks - remote_time_clock_ticks)); + } +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(node_process, ev, data) +{ + static struct etimer periodic_timer; + int is_coordinator; + uip_ipaddr_t dest_ipaddr; + + PROCESS_BEGIN(); + + is_coordinator = 0; + +#if CONTIKI_TARGET_COOJA + is_coordinator = (node_id == 1); +#endif + + if(is_coordinator) { + NETSTACK_ROUTING.root_start(); + } + + /* Initialize UDP connections */ + simple_udp_register(&server_conn, UDP_SERVER_PORT, NULL, + UDP_CLIENT_PORT, udp_rx_callback); + simple_udp_register(&client_conn, UDP_CLIENT_PORT, NULL, + UDP_SERVER_PORT, NULL); + + NETSTACK_MAC.on(); + + etimer_set(&periodic_timer, random_rand() % SEND_INTERVAL); + + while(1) { + PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer)); + + if(tsch_is_coordinator) { + break; + } + + if(NETSTACK_ROUTING.node_is_reachable() && NETSTACK_ROUTING.get_root_ipaddr(&dest_ipaddr)) { + /* Send network uptime timestamp to the DAG root */ + uint64_t network_uptime; + network_uptime = tsch_get_network_uptime_ticks(); + simple_udp_sendto(&client_conn, &network_uptime, sizeof(network_uptime), &dest_ipaddr); + LOG_INFO("Sent network uptime timestamp %lu to ", (unsigned long)network_uptime); + LOG_INFO_6ADDR(&dest_ipaddr); + LOG_INFO_("\n"); + } else { + LOG_INFO("Not reachable yet\n"); + } + + /* Add some jitter */ + etimer_set(&periodic_timer, SEND_INTERVAL + - CLOCK_SECOND + (random_rand() % (2 * CLOCK_SECOND))); + } + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ diff --git a/examples/6tisch/timesync-demo/project-conf.h b/examples/6tisch/timesync-demo/project-conf.h new file mode 100644 index 000000000..f787b8dfe --- /dev/null +++ b/examples/6tisch/timesync-demo/project-conf.h @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2015, SICS Swedish ICT. + * Copyright (c) 2018, University of Bristol - http://www.bristol.ac.uk + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/** + * \author Simon Duquennoy + * Atis Elsts + */ + +#ifndef __PROJECT_CONF_H__ +#define __PROJECT_CONF_H__ + +/* Set to enable TSCH security */ +#ifndef WITH_SECURITY +#define WITH_SECURITY 0 +#endif /* WITH_SECURITY */ + +/* USB serial takes space, free more space elsewhere */ +#define SICSLOWPAN_CONF_FRAG 0 +#define UIP_CONF_BUFFER_SIZE 160 + +/*******************************************************/ +/******************* Configure TSCH ********************/ +/*******************************************************/ + +/* IEEE802.15.4 PANID */ +#define IEEE802154_CONF_PANID 0x81a5 + +/* Do not start TSCH at init, wait for NETSTACK_MAC.on() */ +#define TSCH_CONF_AUTOSTART 0 + +/* 6TiSCH minimal schedule length. + * Larger values result in less frequent active slots: reduces capacity and saves energy. */ +#define TSCH_SCHEDULE_CONF_DEFAULT_LENGTH 3 + +#if WITH_SECURITY + +/* Enable security */ +#define LLSEC802154_CONF_ENABLED 1 + +#endif /* WITH_SECURITY */ + +/*******************************************************/ +/************* Other system configuration **************/ +/*******************************************************/ + +/* Logging */ +#define LOG_CONF_LEVEL_RPL LOG_LEVEL_WARN +#define LOG_CONF_LEVEL_TCPIP LOG_LEVEL_WARN +#define LOG_CONF_LEVEL_IPV6 LOG_LEVEL_WARN +#define LOG_CONF_LEVEL_6LOWPAN LOG_LEVEL_WARN +#define LOG_CONF_LEVEL_MAC LOG_LEVEL_WARN +#define LOG_CONF_LEVEL_FRAMER LOG_LEVEL_WARN +#define TSCH_LOG_CONF_PER_SLOT 0 + +#endif /* __PROJECT_CONF_H__ */ From c01d3d7d227845721d66b9ec157acf68a41bcf1b Mon Sep 17 00:00:00 2001 From: Atis Elsts Date: Tue, 18 Dec 2018 17:14:18 +0000 Subject: [PATCH 4/7] Do not allow to 'trick' the platform and board exclude checks --- Makefile.include | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile.include b/Makefile.include index 2e5a31d1a..930378c01 100644 --- a/Makefile.include +++ b/Makefile.include @@ -478,6 +478,8 @@ ifeq ($(PLATFORM_ACTION),skip) # Skip this target. $(CONTIKI_PROJECT): @echo "Skipping $@: not for the '$(TARGET)/$(BOARD)' platform!" +%.$(TARGET): + @echo "Skipping $@: not for the '$(TARGET)/$(BOARD)' platform!" else # Build this target. # Match-anything pattern rule to allow the project makefiles to From cd1b23ff38ee3117268ded59ed5b6f1575786911 Mon Sep 17 00:00:00 2001 From: Zhitao He Date: Wed, 9 Jan 2019 14:02:01 +0100 Subject: [PATCH 5/7] fix temperature sensor reading in nrf52dk platform, which was stuck at zero --- arch/platform/nrf52dk/dev/temperature-sensor.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/arch/platform/nrf52dk/dev/temperature-sensor.c b/arch/platform/nrf52dk/dev/temperature-sensor.c index 5aac8b3ef..3af30c4af 100644 --- a/arch/platform/nrf52dk/dev/temperature-sensor.c +++ b/arch/platform/nrf52dk/dev/temperature-sensor.c @@ -64,7 +64,15 @@ static int value(int type) { #ifndef SOFTDEVICE_PRESENT - return nrf_temp_read(); + int32_t volatile temp; + + NRF_TEMP->TASKS_START = 1; + while(NRF_TEMP->EVENTS_DATARDY == 0); + NRF_TEMP->EVENTS_DATARDY = 0; + temp = nrf_temp_read(); + NRF_TEMP->TASKS_STOP = 1; + + return temp; #else int32_t temp; sd_temp_get(&temp); From 0b31b090aa425c9b5d4967f0a3b1c8fbe32c35e2 Mon Sep 17 00:00:00 2001 From: Atis Elsts Date: Wed, 9 Jan 2019 16:53:26 +0200 Subject: [PATCH 6/7] Add AES-128 driver for CC26x0/CC13x0 --- arch/cpu/cc26x0-cc13x0/Makefile.cc26x0-cc13x0 | 1 + arch/cpu/cc26x0-cc13x0/cc13xx-cc26xx-conf.h | 5 + arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.c | 127 ++++++++++++++++++ arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.h | 80 +++++++++++ arch/cpu/cc26x0-cc13x0/ti-lib.h | 8 ++ 5 files changed, 221 insertions(+) create mode 100755 arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.c create mode 100755 arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.h diff --git a/arch/cpu/cc26x0-cc13x0/Makefile.cc26x0-cc13x0 b/arch/cpu/cc26x0-cc13x0/Makefile.cc26x0-cc13x0 index c0fb8eeed..d3900b9fb 100644 --- a/arch/cpu/cc26x0-cc13x0/Makefile.cc26x0-cc13x0 +++ b/arch/cpu/cc26x0-cc13x0/Makefile.cc26x0-cc13x0 @@ -44,6 +44,7 @@ CONTIKI_CPU_SOURCEFILES += rf-core.c rf-ble.c ieee-mode.c CONTIKI_CPU_SOURCEFILES += ble-cc2650.c ble-hal-cc26xx.c ble-addr.c rf-ble-cmd.c CONTIKI_CPU_SOURCEFILES += random.c soc-trng.c int-master.c CONTIKI_CPU_SOURCEFILES += spi-arch.c +CONTIKI_CPU_SOURCEFILES += cc26xx-aes.c CONTIKI_SOURCEFILES += $(CONTIKI_CPU_SOURCEFILES) diff --git a/arch/cpu/cc26x0-cc13x0/cc13xx-cc26xx-conf.h b/arch/cpu/cc26x0-cc13x0/cc13xx-cc26xx-conf.h index 7400c5ad9..0b553fb90 100644 --- a/arch/cpu/cc26x0-cc13x0/cc13xx-cc26xx-conf.h +++ b/arch/cpu/cc26x0-cc13x0/cc13xx-cc26xx-conf.h @@ -102,6 +102,11 @@ #define NETSTACK_RADIO_MAX_PAYLOAD_LEN 125 +/* Platform-specific (H/W) AES implementation */ +#ifndef AES_128_CONF +#define AES_128_CONF cc26xx_aes_128_driver +#endif /* AES_128_CONF */ + /** @} */ /*---------------------------------------------------------------------------*/ /** diff --git a/arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.c b/arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.c new file mode 100755 index 000000000..cacdfc61f --- /dev/null +++ b/arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.c @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2016, University of Bristol - http://www.bristol.ac.uk + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/** + * \addtogroup cc26xx-aes + * @{ + * + * \file + * Implementation of the AES driver for the CC26x0/CC13x0 SoC + * \author + * Atis Elsts + */ +#include "contiki.h" +#include "dev/cc26xx-aes.h" +#include "ti-lib.h" +/*---------------------------------------------------------------------------*/ +#include "sys/log.h" +#define LOG_MODULE "cc26xx-aes" +#define LOG_LEVEL LOG_LEVEL_MAIN +/*---------------------------------------------------------------------------*/ +static uint32_t skey[AES_128_KEY_LENGTH / sizeof(uint32_t)]; + +/*---------------------------------------------------------------------------*/ +void +cc26xx_aes_set_key(const uint8_t *key) +{ + memcpy(skey, key, AES_128_KEY_LENGTH); +} +/*---------------------------------------------------------------------------*/ +static void +encrypt_decrypt(uint8_t *plaintext_and_result, bool do_encrypt) +{ + uint32_t result[AES_128_BLOCK_SIZE / sizeof(uint32_t)]; + unsigned status; + int i; + + /* First, make sure the PERIPH PD is on */ + ti_lib_prcm_power_domain_on(PRCM_DOMAIN_PERIPH); + while((ti_lib_prcm_power_domain_status(PRCM_DOMAIN_PERIPH) + != PRCM_DOMAIN_POWER_ON)); + + /* Enable CRYPTO peripheral */ + ti_lib_prcm_peripheral_run_enable(PRCM_PERIPH_CRYPTO); + ti_lib_prcm_load_set(); + while(!ti_lib_prcm_load_get()); + + status = ti_lib_crypto_aes_load_key(skey, CRYPTO_KEY_AREA_0); + if(status != AES_SUCCESS) { + LOG_WARN("load key failed: %u\n", status); + } else { + + status = ti_lib_crypto_aes_ecb((uint32_t *)plaintext_and_result, result, CRYPTO_KEY_AREA_0, do_encrypt, false); + if(status != AES_SUCCESS) { + LOG_WARN("ecb failed: %u\n", status); + } else { + + for(i = 0; i < 100; ++i) { + ti_lib_cpu_delay(10); + status = ti_lib_crypto_aes_ecb_status(); + if(status != AES_DMA_BSY) { + break; + } + } + + ti_lib_crypto_aes_ecb_finish(); + + if(status != AES_SUCCESS) { + LOG_WARN("ecb get result failed: %u\n", status); + } + } + } + + ti_lib_prcm_peripheral_run_disable(PRCM_PERIPH_CRYPTO); + ti_lib_prcm_load_set(); + while(!ti_lib_prcm_load_get()); + + if(status == AES_SUCCESS) { + memcpy(plaintext_and_result, result, AES_128_BLOCK_SIZE); + } else { + /* corrupt the result */ + plaintext_and_result[0] ^= 1; + } +} +/*---------------------------------------------------------------------------*/ +void +cc26xx_aes_encrypt(uint8_t *plaintext_and_result) +{ + encrypt_decrypt(plaintext_and_result, true); +} +/*---------------------------------------------------------------------------*/ +void +cc26xx_aes_decrypt(uint8_t *cyphertext_and_result) +{ + encrypt_decrypt(cyphertext_and_result, false); +} +/*---------------------------------------------------------------------------*/ +const struct aes_128_driver cc26xx_aes_128_driver = { + cc26xx_aes_set_key, + cc26xx_aes_encrypt +}; + +/** @} */ diff --git a/arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.h b/arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.h new file mode 100755 index 000000000..a979cc562 --- /dev/null +++ b/arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.h @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2016, University of Bristol - http://www.bristol.ac.uk + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/** + * \addtogroup cc26xx + * @{ + * + * \defgroup cc26xx-aes CC26x0/CC13x0 AES-128 + * + * AES-128 driver for the CC26x0/CC13x0 SoC + * @{ + * + * \file + * Header file of the AES-128 driver for the CC26xx SoC + * \author + * Atis Elsts + */ +#ifndef CC2538_AES_H_ +#define CC2538_AES_H_ + +#include "lib/aes-128.h" + +/** + * \brief Set a key to use in subsequent encryption & decryption operations. + * \param key The key to use + * + * The size of the key must be AES_128_KEY_LENGTH. + */ +void cc26xx_aes_set_key(const uint8_t *key); + +/** + * \brief Encrypt a message using the SoC AES-128 hardware implementation + * \param plaintext_and_result In: message to encrypt, out: the encrypted message. + * + * The size of the message must be AES_128_BLOCK_SIZE. + * The key to use in the encryption must be set before calling this function. + */ +void cc26xx_aes_encrypt(uint8_t *plaintext_and_result); + +/** + * \brief Decrypt a message using the SoC AES-128 hardware implementation + * \param cyphertext_and_result In: message to decrypt, out: the decrypted message. + * + * The size of the message must be AES_128_BLOCK_SIZE. + * The key to use in the decryption must be set before calling this function. + */ +void cc26xx_aes_decrypt(uint8_t *cyphertext_and_result); + +extern const struct aes_128_driver cc26xx_aes_128_driver; + +#endif /* CC2538_AES_H_ */ +/** + * @} + * @} + */ diff --git a/arch/cpu/cc26x0-cc13x0/ti-lib.h b/arch/cpu/cc26x0-cc13x0/ti-lib.h index d6c7363ee..0d8400813 100644 --- a/arch/cpu/cc26x0-cc13x0/ti-lib.h +++ b/arch/cpu/cc26x0-cc13x0/ti-lib.h @@ -572,6 +572,14 @@ #define ti_lib_watchdog_stall_enable(...) WatchdogStallEnable(__VA_ARGS__) #define ti_lib_watchdog_stall_disable(...) WatchdogStallDisable(__VA_ARGS__) /*---------------------------------------------------------------------------*/ +/* crypto.h */ +#include "driverlib/crypto.h" + +#define ti_lib_crypto_aes_load_key(...) CRYPTOAesLoadKey(__VA_ARGS__) +#define ti_lib_crypto_aes_ecb(...) CRYPTOAesEcb(__VA_ARGS__) +#define ti_lib_crypto_aes_ecb_status(...) CRYPTOAesEcbStatus(__VA_ARGS__) +#define ti_lib_crypto_aes_ecb_finish(...) CRYPTOAesEcbFinish(__VA_ARGS__) +/*---------------------------------------------------------------------------*/ #endif /* TI_LIB_H_ */ /*---------------------------------------------------------------------------*/ /** From d0a0a2a568eff152ed1f3b964d53f8b3eee12434 Mon Sep 17 00:00:00 2001 From: Zhitao He Date: Thu, 10 Jan 2019 18:56:27 +0100 Subject: [PATCH 7/7] busy wait maximum 72 us (typically 36 us) for taking a measurement from the on-chip temperature sensor --- arch/platform/nrf52dk/dev/temperature-sensor.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/platform/nrf52dk/dev/temperature-sensor.c b/arch/platform/nrf52dk/dev/temperature-sensor.c index 3af30c4af..f42fd3fe3 100644 --- a/arch/platform/nrf52dk/dev/temperature-sensor.c +++ b/arch/platform/nrf52dk/dev/temperature-sensor.c @@ -67,7 +67,8 @@ value(int type) int32_t volatile temp; NRF_TEMP->TASKS_START = 1; - while(NRF_TEMP->EVENTS_DATARDY == 0); + /* nRF52832 datasheet: one temperature measurement takes typically 36 us */ + RTIMER_BUSYWAIT_UNTIL(NRF_TEMP->EVENTS_DATARDY, RTIMER_SECOND * 72 / 1000000); NRF_TEMP->EVENTS_DATARDY = 0; temp = nrf_temp_read(); NRF_TEMP->TASKS_STOP = 1;