diff --git a/Makefile b/Makefile index 0512353cd3..7a231199e6 100644 --- a/Makefile +++ b/Makefile @@ -276,6 +276,7 @@ clean: $(Q)$(MAKE) -C $(EXT) clean $(Q)find . -name '*~' -exec rm -f {} \; $(Q)find . -name '*.pyc' -exec rm -f {} \; + $(Q)find . -name 'Cargo.lock' -exec rm -f {} \; cleanspaces: find sw -path sw/ext -prune -o -type f -name '*.[ch]' -exec sed -i {} -e 's/[ \t]*$$//' \; diff --git a/sw/airborne/arch/chibios/mcu_periph/rng_arch.c b/sw/airborne/arch/chibios/mcu_periph/rng_arch.c new file mode 100644 index 0000000000..aed9cbb5c1 --- /dev/null +++ b/sw/airborne/arch/chibios/mcu_periph/rng_arch.c @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2021 Gautier Hattenberger + * + * This file is part of paparazzi. + * + * paparazzi is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * paparazzi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with paparazzi; see the file COPYING. If not, see + * . + */ + +/** \file rng_arch.c + * \brief arch specific Random Number Generator API + * + */ + +#include "mcu_periph/rng.h" +#include + +/*********** + * RNG API * + ***********/ + +// FIXME remove and use API from ChibiOS after updating to newer version + +static void TRNGStart(void) +{ + rccEnableAHB2(RCC_AHB2ENR_RNGEN, 0); + RNG->CR |= RNG_CR_RNGEN; +} + +static void TRNGStop(void) +{ + RNG->CR &= ~RNG_CR_RNGEN; + rccDisableAHB2(RCC_AHB2ENR_RNGEN); +} + +static uint32_t TRNGGet(void) +{ + // waiting for data ready + while ((RNG->SR & RNG_SR_DRDY) == 0) {}; + return RNG->DR; +} + +static bool TRNGGetErrors(void) +{ + return (RNG->SR & RNG_SR_CECS) || (RNG->SR & RNG_SR_SECS); +} + +static void TRNGClearErrors(void) +{ + RNG->SR &= ~(RNG_SR_SEIS | RNG_SR_CEIS); +} + +static bool TRNGGenerate(size_t size, uint32_t *out) +{ + while (size) { + out[--size] = TRNGGet(); + } + return TRNGGetErrors(); +} + + +/************ + * PPRZ API * + ************/ + +void rng_init(void) +{ + TRNGStart(); +} + +void rng_deinit(void) +{ + TRNGStop(); +} + +// Return true only if we got a new number +// that is different from the previous one +bool rng_get(uint32_t *rand_nr) +{ + bool err = TRNGGenerate(1, rand_nr); + if (err) { + TRNGClearErrors(); + return false; + } + else { + return true; + } +} + +// Wait until we get a new number that is different +// from the previous one. We can wait forever here if +// the clocks are not setup properly. +uint32_t rng_wait_and_get(void) +{ + uint32_t tmp = 0; + bool err = true; + do { + err = TRNGGenerate(1, &tmp); + } while (err); + return tmp; +} + diff --git a/sw/airborne/modules/datalink/gec_dl.c b/sw/airborne/modules/datalink/gec_dl.c index 777134bca2..26ac1b682b 100644 --- a/sw/airborne/modules/datalink/gec_dl.c +++ b/sw/airborne/modules/datalink/gec_dl.c @@ -145,7 +145,7 @@ static void start_message(struct pprzlink_msg *msg, uint8_t payload_len __attribute__((unused))) { PPRZ_MUTEX_LOCK(get_trans(msg)->mtx_tx); // lock mutex - memset(get_trans(msg)->tx_msg, _FD, TRANSPORT_PAYLOAD_LEN);// erase message data + memset(get_trans(msg)->tx_msg, 0, TRANSPORT_PAYLOAD_LEN);// erase message data get_trans(msg)->tx_msg_idx = 0;// reset index } @@ -197,6 +197,7 @@ static void end_message(struct pprzlink_msg *msg, long fd) case CRYPTO_OK: if (gec_encrypt_message(gec_tp.tx_msg, &gec_tp.tx_msg_idx)) { gec_encapsulate_and_send_msg(msg, fd); + return; } break; default: @@ -214,10 +215,11 @@ static void end_message(struct pprzlink_msg *msg, long fd) */ void gec_encapsulate_and_send_msg(struct pprzlink_msg *msg, long fd) { - get_trans(msg)->pprz_tp.trans_tx.start_message(msg, fd, - get_trans(msg)->tx_msg_idx); - get_trans(msg)->pprz_tp.trans_tx.put_bytes(msg, _FD, DL_TYPE_UINT8, + get_trans(msg)->pprz_tp.trans_tx.start_message(msg, fd, get_trans(msg)->tx_msg_idx); + get_trans(msg)->pprz_tp.trans_tx.put_bytes(msg, fd, DL_TYPE_UINT8, DL_FORMAT_SCALAR, get_trans(msg)->tx_msg, get_trans(msg)->tx_msg_idx); + // unlock mutex + PPRZ_MUTEX_UNLOCK(get_trans(msg)->mtx_tx); get_trans(msg)->pprz_tp.trans_tx.end_message(msg, fd); } @@ -262,7 +264,7 @@ static void start_message(struct gec_transport *trans, uint8_t payload_len __attribute__((unused))) { PPRZ_MUTEX_LOCK(trans->mtx_tx); // lock mutex - memset(trans->tx_msg, _FD, TRANSPORT_PAYLOAD_LEN); // erase message data + memset(trans->tx_msg, 0, TRANSPORT_PAYLOAD_LEN); // erase message data trans->tx_msg_idx = 0; // reset index } @@ -273,6 +275,7 @@ static void end_message(struct gec_transport *trans, struct link_device *dev, case CRYPTO_OK: if (gec_encrypt_message(gec_tp.tx_msg, &gec_tp.tx_msg_idx)) { gec_encapsulate_and_send_msg(trans, dev, fd); + return; } break; default: @@ -287,8 +290,10 @@ void gec_encapsulate_and_send_msg(struct gec_transport *trans, struct link_device *dev, long fd) { trans->pprz_tp.trans_tx.start_message(trans, dev, fd, trans->tx_msg_idx); - trans->pprz_tp.trans_tx.put_bytes(trans, dev, _FD, DL_TYPE_UINT8, + trans->pprz_tp.trans_tx.put_bytes(trans, dev, fd, DL_TYPE_UINT8, DL_FORMAT_SCALAR, trans->tx_msg, trans->tx_msg_idx); + // unlock mutex + PPRZ_MUTEX_UNLOCK(trans->mtx_tx); trans->pprz_tp.trans_tx.end_message(trans, dev, fd); }