diff --git a/.cproject b/.cproject index 437afc7..a023ea9 100755 --- a/.cproject +++ b/.cproject @@ -146,6 +146,7 @@ make + all true true @@ -153,6 +154,7 @@ make + clean true true @@ -160,7 +162,6 @@ make - distclean true false @@ -216,6 +217,7 @@ make + all true true @@ -247,6 +249,7 @@ make + distclean true true @@ -292,6 +295,14 @@ false true + + make + TEST_APP=rng BOARD=stm32f4-discovery DEBUG=y + test + true + false + true + make APP=example_radio_rx BOARD=msp430-ccrf DEBUG=y @@ -318,6 +329,7 @@ make + all true true @@ -325,6 +337,7 @@ make + clean true true diff --git a/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/include/stm32f4-discovery.h b/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/include/stm32f4-discovery.h index 91ab510..56e96e6 100755 --- a/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/include/stm32f4-discovery.h +++ b/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/include/stm32f4-discovery.h @@ -24,8 +24,35 @@ #include "gpio.h" #include "stm32f4_gpio.h" + +#include "rng.h" +#include "stm32f4_rng.h" + #include "driver.h" +// Random number generator +static RNG_HandleTypeDef stm32f4_rng_handle = { + .Instance = RNG, +}; + +static struct stm32f4_rng stm32f4_rng = { + .rng_handle = &stm32f4_rng_handle, +}; + +static struct rng __rng = { + .arch_dep_device = &stm32f4_rng, + .fp = &rng_fp, +}; + +#ifdef TEST_APP +static const struct driver rng = { +#else +const struct driver rng = { +#endif + .driver_type = DRIVER_TYPE_RNG, + .device_driver = &__rng, +}; + // GPIO_C0 static const GPIO_InitTypeDef port_cfg_c0 = { .Pin = GPIO_PIN_0, diff --git a/source/firmware/arch/stm32f4xx/driver/include/stm32f4_rng.h b/source/firmware/arch/stm32f4xx/driver/include/stm32f4_rng.h new file mode 100644 index 0000000..d555757 --- /dev/null +++ b/source/firmware/arch/stm32f4xx/driver/include/stm32f4_rng.h @@ -0,0 +1,25 @@ +/* + * stm32f4_rng.h + * + * Created on: Aug 30, 2016 + * Author: tkl + */ + +#ifndef SOURCE_FIRMWARE_ARCH_STM32F4XX_INCLUDE_STM32F4_RNG_H_ +#define SOURCE_FIRMWARE_ARCH_STM32F4XX_INCLUDE_STM32F4_RNG_H_ + +struct stm32f4_rng { + RNG_HandleTypeDef *rng_handle; +}; + +int stm32f4_rng_open(const void *this); +int stm32f4_rng_close(const void *this); +unsigned int stm32f4_rng_read(const void *this); + +static const struct rng_fp rng_fp = { + .open = stm32f4_rng_open, + .close = stm32f4_rng_close, + .read = stm32f4_rng_read, +}; + +#endif /* SOURCE_FIRMWARE_ARCH_STM32F4XX_INCLUDE_STM32F4_RNG_H_ */ diff --git a/source/firmware/arch/stm32f4xx/driver/stm32f4_rng.c b/source/firmware/arch/stm32f4xx/driver/stm32f4_rng.c new file mode 100644 index 0000000..71e6460 --- /dev/null +++ b/source/firmware/arch/stm32f4xx/driver/stm32f4_rng.c @@ -0,0 +1,43 @@ +/* + * stm32f4_rng.c + * + * Created on: Aug 30, 2016 + * Author: tkl + */ + +#include + +#include "board.h" + +int stm32f4_rng_open(const void *this) +{ + if(NULL == this) + return -1; + struct stm32f4_rng *dev = (struct stm32f4_rng *)this; + + __HAL_RCC_RNG_CLK_ENABLE(); + HAL_RNG_DeInit(dev->rng_handle); + HAL_RNG_Init(dev->rng_handle); + + return 0; +} + +int stm32f4_rng_close(const void *this) +{ + if(NULL == this) + return -1; + struct stm32f4_rng *dev = (struct stm32f4_rng *)this; + __HAL_RCC_RNG_CLK_DISABLE(); + HAL_RNG_DeInit(dev->rng_handle); + return 0; +} + +unsigned int stm32f4_rng_read(const void *this) +{ + if(NULL == this) + return -1; + struct stm32f4_rng *dev = (struct stm32f4_rng *)this; + uint32_t random = 0; + HAL_RNG_GenerateRandomNumber(dev->rng_handle, &random); + return random; +} diff --git a/source/firmware/kernel/driver/driver.c b/source/firmware/kernel/driver/driver.c index 32d052f..496d440 100644 --- a/source/firmware/kernel/driver/driver.c +++ b/source/firmware/kernel/driver/driver.c @@ -12,7 +12,7 @@ #include "gpio.h" #include "i2c.h" #include "pwm.h" -//#include "rtc.h" +#include "rng.h" #include "spi.h" #include "uart.h" @@ -34,8 +34,10 @@ int drv_open(const struct driver *driver) case DRIVER_TYPE_PWM: ret = pwm_open((const struct pwm *)(driver->device_driver)); break; + case DRIVER_TYPE_RNG: + ret = rng_open((const struct rng *)(driver->device_driver)); + break; case DRIVER_TYPE_RTC: -// ret = rtc_open((const struct rtc *)(driver->device_driver)); break; case DRIVER_TYPE_SPI: ret = spi_open((const struct spi *)(driver->device_driver)); @@ -65,8 +67,10 @@ int drv_close(const struct driver *driver) case DRIVER_TYPE_PWM: ret = pwm_close((const struct pwm *)(driver->device_driver)); break; + case DRIVER_TYPE_RNG: + ret = rng_close((const struct rng *)(driver->device_driver)); + break; case DRIVER_TYPE_RTC: -// ret = rtc_close((const struct rtc *)(driver->device_driver)); break; case DRIVER_TYPE_SPI: ret = spi_close((const struct spi *)(driver->device_driver)); @@ -96,7 +100,9 @@ int drv_read(const struct driver *driver, char *buffer, int len) case DRIVER_TYPE_I2C: break; case DRIVER_TYPE_PWM: - ret = -1; + break; + case DRIVER_TYPE_RNG: + ret = rng_read((const struct rng *)(driver->device_driver)); break; case DRIVER_TYPE_RTC: break; @@ -129,7 +135,8 @@ int drv_write(const struct driver *driver, const char *buffer, int len) case DRIVER_TYPE_I2C: break; case DRIVER_TYPE_PWM: - ret = -1; + break; + case DRIVER_TYPE_RNG: break; case DRIVER_TYPE_RTC: break; @@ -166,6 +173,8 @@ int drv_ioctl(const struct driver *driver, unsigned int cmd, const void *data) return pwm_get_pulse_width_ns((const struct pwm *)(driver->device_driver)); } break; + case DRIVER_TYPE_RNG: + break; case DRIVER_TYPE_RTC: break; case DRIVER_TYPE_SPI: diff --git a/source/firmware/kernel/driver/include/rng.h b/source/firmware/kernel/driver/include/rng.h new file mode 100644 index 0000000..4bc083f --- /dev/null +++ b/source/firmware/kernel/driver/include/rng.h @@ -0,0 +1,30 @@ +/* + * rng.h + * + * Created on: Aug 30, 2016 + * Author: tkl + */ + +#ifndef SOURCE_FIRMWARE_KERNEL_DRIVER_INCLUDE_RNG_H_ +#define SOURCE_FIRMWARE_KERNEL_DRIVER_INCLUDE_RNG_H_ + +typedef int (*rng_fp_open_t)(const void*); +typedef int (*rng_fp_close_t)(const void*); +typedef unsigned int (*rng_fp_read_t)(const void*); + +struct rng_fp { + const rng_fp_open_t open; + const rng_fp_close_t close; + const rng_fp_read_t read; +}; + +struct rng { + const void *arch_dep_device; + const struct rng_fp *fp; +}; + +int rng_open(const struct rng *device); +int rng_close(const struct rng *device); +unsigned int rng_read(const struct rng *device); + +#endif /* SOURCE_FIRMWARE_KERNEL_DRIVER_INCLUDE_RNG_H_ */ diff --git a/source/firmware/kernel/driver/rng.c b/source/firmware/kernel/driver/rng.c new file mode 100644 index 0000000..3822005 --- /dev/null +++ b/source/firmware/kernel/driver/rng.c @@ -0,0 +1,36 @@ +/* + * rng.c + * + * Created on: Aug 30, 2016 + * Author: tkl + */ + +#include +#include "rng.h" + +int rng_open(const struct rng *device) +{ + if(device == NULL) + return -1; + + rng_fp_open_t open = device->fp->open; + return open(device->arch_dep_device); +} + +int rng_close(const struct rng *device) +{ + if(device == NULL) + return -1; + + rng_fp_close_t close = device->fp->close; + return close(device->arch_dep_device); +} + +unsigned int rng_read(const struct rng *device) +{ + if(device == NULL) + return 0; + + rng_fp_read_t read = device->fp->read; + return read(device->arch_dep_device); +} diff --git a/source/firmware/kernel/interface/driver.h b/source/firmware/kernel/interface/driver.h index 6ce5b5d..8af40b4 100644 --- a/source/firmware/kernel/interface/driver.h +++ b/source/firmware/kernel/interface/driver.h @@ -17,6 +17,7 @@ enum driver_type { DRIVER_TYPE_GPIO, DRIVER_TYPE_I2C, DRIVER_TYPE_PWM, + DRIVER_TYPE_RNG, DRIVER_TYPE_RTC, DRIVER_TYPE_SPI, DRIVER_TYPE_UART diff --git a/source/test/rng/main.c b/source/test/rng/main.c new file mode 100644 index 0000000..07c5b7e --- /dev/null +++ b/source/test/rng/main.c @@ -0,0 +1,38 @@ + + +/* + * main.c + * + * Created on: Aug 2, 2016 + * Author: tkl + */ + +#include +#include +#include + +#include "driver.h" +#include "board.h" +#include "stack.h" +#include "queue.h" +#include "kernel.h" +#include "driver.h" +#include "list.h" +#include "shell.h" + +int main(void) +{ + char print_buffer[50]; + shell_init(&uart_1); + drv_open(&rng); + + while(1) { + int random = drv_read(&rng, NULL, 0); + sprintf(print_buffer, "random: %d\r\n", random); + shell_write(print_buffer, strlen(print_buffer)); + sleep_ms(1000); + } + + return 0; +} + diff --git a/source/test/rng/rng.mk b/source/test/rng/rng.mk new file mode 100644 index 0000000..cc0fdcc --- /dev/null +++ b/source/test/rng/rng.mk @@ -0,0 +1 @@ +SRC_DIR += source/test/rng diff --git a/source/test/test.mk b/source/test/test.mk index ba014f1..527eb6b 100644 --- a/source/test/test.mk +++ b/source/test/test.mk @@ -6,6 +6,10 @@ ifeq ($(TEST_APP), pwm) include source/test/pwm/pwm.mk endif +ifeq ($(TEST_APP), rng) +include source/test/rng/rng.mk +endif + ifeq ($(TEST_APP), shell) include source/test/shell/shell.mk endif