From 5805e10f74104e3de60470c38d6643d2bdb00fe0 Mon Sep 17 00:00:00 2001 From: Stephane D'Alu Date: Sat, 9 Jul 2016 23:57:48 +0200 Subject: NRF52832 implementation --- demos/NRF52/Classic/main.c | 104 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 demos/NRF52/Classic/main.c (limited to 'demos/NRF52/Classic/main.c') diff --git a/demos/NRF52/Classic/main.c b/demos/NRF52/Classic/main.c new file mode 100644 index 0000000..c55b973 --- /dev/null +++ b/demos/NRF52/Classic/main.c @@ -0,0 +1,104 @@ +#include +#include + +#include "ch.h" +#include "hal.h" +#include "chprintf.h" +#include "shell.h" +#include "ch_test.h" + +#define LED_EXT 14 + +static THD_WORKING_AREA(shell_wa, 1024); + +static const ShellCommand commands[] = { + {NULL, NULL} +}; + +static const ShellConfig shell_cfg1 = { + (BaseSequentialStream *)&SD1, + commands +}; + +static SerialConfig serial_config = { + .speed = 115200, + .tx_pad = UART_TX, + .rx_pad = UART_RX, +#if NRF51_SERIAL_USE_HWFLOWCTRL == TRUE + .rts_pad = UART_RTS, + .cts_pad = UART_CTS, +#endif +}; + + + +static THD_WORKING_AREA(waThread1, 64); +static THD_FUNCTION(Thread1, arg) { + + (void)arg; + uint8_t led = LED4; + + chRegSetThreadName("blinker"); + + + while (1) { + palSetPad(IOPORT1, led); + chThdSleepMilliseconds(100); + palClearPad(IOPORT1, led); + chThdSleepMilliseconds(100); + } +} + + +#define printf(fmt, ...) \ + chprintf((BaseSequentialStream*)&SD1, fmt, ##__VA_ARGS__) + + + + +/**@brief Function for application main entry. + */ +int main(void) +{ + + halInit(); + chSysInit(); + shellInit(); + + sdStart(&SD1, &serial_config); + + palSetPad(IOPORT1, LED1); + palSetPad(IOPORT1, LED2); + palSetPad(IOPORT1, LED3); + palSetPad(IOPORT1, LED4); + + + + chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO+1, + Thread1, NULL); + + + + chThdCreateStatic(shell_wa, sizeof(shell_wa), NORMALPRIO+1, + shellThread, (void *)&shell_cfg1); + + + + printf(PORT_INFO "\r\n"); + chThdSleep(2); + + + + printf("Priority levels %d\r\n", CORTEX_PRIORITY_LEVELS); + + test_execute((BaseSequentialStream *)&SD1); + + while (true) { + chThdSleepMilliseconds(100); + + } + + + +} + -- cgit v1.2.3 From 5259158d1727f3703bc90f88d50938eada316f67 Mon Sep 17 00:00:00 2001 From: Stephane D'Alu Date: Sun, 10 Jul 2016 10:48:04 +0200 Subject: renamed NRF51_* to NRF5_* --- demos/NRF52/Classic/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'demos/NRF52/Classic/main.c') diff --git a/demos/NRF52/Classic/main.c b/demos/NRF52/Classic/main.c index c55b973..c0a63b9 100644 --- a/demos/NRF52/Classic/main.c +++ b/demos/NRF52/Classic/main.c @@ -24,7 +24,7 @@ static SerialConfig serial_config = { .speed = 115200, .tx_pad = UART_TX, .rx_pad = UART_RX, -#if NRF51_SERIAL_USE_HWFLOWCTRL == TRUE +#if NRF5_SERIAL_USE_HWFLOWCTRL == TRUE .rts_pad = UART_RTS, .cts_pad = UART_CTS, #endif -- cgit v1.2.3 From 6423c3dabeba4e4ed9217d71873653bc8af9ae4e Mon Sep 17 00:00:00 2001 From: Stephane D'Alu Date: Sun, 10 Jul 2016 12:04:39 +0200 Subject: moved rng to LLD directory. removed rng power control (doesn't exist in nrf52, wasn't documented in nrf51) renamed peripheral to start at 0 --- demos/NRF52/Classic/main.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'demos/NRF52/Classic/main.c') diff --git a/demos/NRF52/Classic/main.c b/demos/NRF52/Classic/main.c index c0a63b9..efa6591 100644 --- a/demos/NRF52/Classic/main.c +++ b/demos/NRF52/Classic/main.c @@ -9,9 +9,50 @@ #define LED_EXT 14 + + +/* + * Command Random + */ +#define RANDOM_BUFFER_SIZE 1024 +static uint8_t random_buffer[RANDOM_BUFFER_SIZE]; + +static void cmd_random(BaseSequentialStream *chp, int argc, char *argv[]) { + uint16_t size = 16; + uint16_t i = 0; + uint8_t nl = 0; + + if (argc > 0) { + size = atoi(argv[0]); + } + + if (size > RANDOM_BUFFER_SIZE) { + chprintf(chp, "random: maximum size is %d.\r\n", RANDOM_BUFFER_SIZE); + return; + } + + chprintf(chp, "Fetching %d random byte(s):\r\n", size); + + rngStart(&RNGD1, NULL); + rngWrite(&RNGD1, random_buffer, size, TIME_INFINITE); + rngStop(&RNGD1); + + for (i = 0 ; i < size ; i++) { + chprintf(chp, "%02x ", random_buffer[i]); + if ((nl = (((i+1) % 20)) == 0)) + chprintf(chp, "\r\n"); + } + if (!nl) + chprintf(chp, "\r\n"); + +} + + + static THD_WORKING_AREA(shell_wa, 1024); static const ShellCommand commands[] = { + {"random", cmd_random}, {NULL, NULL} }; -- cgit v1.2.3 From 9cf4f9dfc7f6bcd1c46c96d9e388bf677e7148f1 Mon Sep 17 00:00:00 2001 From: Stephane D'Alu Date: Sun, 10 Jul 2016 13:35:31 +0200 Subject: moved wdg to LLD --- demos/NRF52/Classic/main.c | 52 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 5 deletions(-) (limited to 'demos/NRF52/Classic/main.c') diff --git a/demos/NRF52/Classic/main.c b/demos/NRF52/Classic/main.c index efa6591..fde485a 100644 --- a/demos/NRF52/Classic/main.c +++ b/demos/NRF52/Classic/main.c @@ -10,6 +10,21 @@ #define LED_EXT 14 +bool watchdog_started = false; + +void watchdog_callback(void) { + palTogglePad(IOPORT1, LED2); + palTogglePad(IOPORT1, LED3); + palTogglePad(IOPORT1, LED4); +} + +WDGConfig WDG_config = { + .pause_on_sleep = 0, + .pause_on_halt = 0, + .timeout_ms = 5000, + .callback = watchdog_callback, +}; + /* * Command Random @@ -49,10 +64,31 @@ static void cmd_random(BaseSequentialStream *chp, int argc, char *argv[]) { + +static void cmd_watchdog(BaseSequentialStream *chp, int argc, char *argv[]) { + if ((argc == 0) || (strcmp(argv[0], "start"))) { + chprintf(chp, "Usage: watchdog start\r\n"); + return; + } + chprintf(chp, + "Watchdog started\r\n" + "You need to push BTN1 every 5 seconds\r\n"); + + watchdog_started = true; + wdgStart(&WDGD1, &WDG_config); +} + + + + + + + static THD_WORKING_AREA(shell_wa, 1024); static const ShellCommand commands[] = { - {"random", cmd_random}, + {"random", cmd_random }, + {"watchdog", cmd_watchdog }, {NULL, NULL} }; @@ -73,6 +109,10 @@ static SerialConfig serial_config = { + + + + static THD_WORKING_AREA(waThread1, 64); static THD_FUNCTION(Thread1, arg) { @@ -135,11 +175,13 @@ int main(void) test_execute((BaseSequentialStream *)&SD1); while (true) { - chThdSleepMilliseconds(100); - + if (watchdog_started && + (palReadPad(IOPORT1, BTN1) == 0)) { + palTogglePad(IOPORT1, LED1); + wdgReset(&WDGD1); + } + chThdSleepMilliseconds(250); } - - } -- cgit v1.2.3 From ee20f1a67b64eb5548fccdd86c277d6304d9d891 Mon Sep 17 00:00:00 2001 From: Stephane D'Alu Date: Sun, 10 Jul 2016 14:12:04 +0200 Subject: shell commands for info and watchdog --- demos/NRF52/Classic/main.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'demos/NRF52/Classic/main.c') diff --git a/demos/NRF52/Classic/main.c b/demos/NRF52/Classic/main.c index fde485a..1f92bdc 100644 --- a/demos/NRF52/Classic/main.c +++ b/demos/NRF52/Classic/main.c @@ -66,27 +66,44 @@ static void cmd_random(BaseSequentialStream *chp, int argc, char *argv[]) { static void cmd_watchdog(BaseSequentialStream *chp, int argc, char *argv[]) { - if ((argc == 0) || (strcmp(argv[0], "start"))) { - chprintf(chp, "Usage: watchdog start\r\n"); + if ((argc != 2) || (strcmp(argv[0], "start"))) { + usage: + chprintf(chp, "Usage: watchdog start \r\n" + " = 0..%d seconds\r\n", + WDG_MAX_TIMEOUT_MS/1000); return; } + int timeout = atoi(argv[1]); + if ((timeout < 0) || (timeout > (WDG_MAX_TIMEOUT_MS/1000))) + goto usage; + + if (watchdog_started) { + chprintf(chp, "Watchdog already started." + " Can't be modified once activated.\r\n"); + return; + } + chprintf(chp, "Watchdog started\r\n" - "You need to push BTN1 every 5 seconds\r\n"); + "You need to push BTN1 every %d second(s)\r\n", timeout); - watchdog_started = true; + WDG_config.timeout_ms = timeout * 1000; wdgStart(&WDGD1, &WDG_config); + watchdog_started = true; } - +static void cmd_info(BaseSequentialStream *chp, int argc, char *argv[]) { + chprintf(chp, "Watchdog max = %d ms\r\n", WDG_MAX_TIMEOUT_MS); +} static THD_WORKING_AREA(shell_wa, 1024); static const ShellCommand commands[] = { + {"info", cmd_info }, {"random", cmd_random }, {"watchdog", cmd_watchdog }, {NULL, NULL} @@ -179,6 +196,7 @@ int main(void) (palReadPad(IOPORT1, BTN1) == 0)) { palTogglePad(IOPORT1, LED1); wdgReset(&WDGD1); + printf("Watchdog reseted\r\n"); } chThdSleepMilliseconds(250); } -- cgit v1.2.3 From 321ec844af3b1e7a23c94bce65bc8aa13ef1e09e Mon Sep 17 00:00:00 2001 From: Stephane D'Alu Date: Sun, 10 Jul 2016 19:15:46 +0200 Subject: moved GPT to LLD --- demos/NRF52/Classic/main.c | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) (limited to 'demos/NRF52/Classic/main.c') diff --git a/demos/NRF52/Classic/main.c b/demos/NRF52/Classic/main.c index 1f92bdc..28e7505 100644 --- a/demos/NRF52/Classic/main.c +++ b/demos/NRF52/Classic/main.c @@ -26,6 +26,22 @@ WDGConfig WDG_config = { }; +void gpt_callback(GPTDriver *gptp) { + palTogglePad(IOPORT1, LED2); +} + +/* + * GPT configuration + * Frequency: 31250Hz (32us period) + * Resolution: 16 bits + */ +static const GPTConfig gpt_config = { + .frequency = 31250, + .callback = gpt_callback, + .resolution = 16, +}; + + /* * Command Random */ @@ -140,9 +156,7 @@ static THD_FUNCTION(Thread1, arg) { while (1) { - palSetPad(IOPORT1, led); - chThdSleepMilliseconds(100); - palClearPad(IOPORT1, led); + palTogglePad(IOPORT1, led); chThdSleepMilliseconds(100); } } @@ -166,12 +180,14 @@ int main(void) sdStart(&SD1, &serial_config); palSetPad(IOPORT1, LED1); - palSetPad(IOPORT1, LED2); - palSetPad(IOPORT1, LED3); + palClearPad(IOPORT1, LED2); + palClearPad(IOPORT1, LED3); palSetPad(IOPORT1, LED4); + gptStart(&GPTD1, &gpt_config); + gptStartContinuous(&GPTD1, 31250); - + chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO+1, Thread1, NULL); @@ -181,6 +197,7 @@ int main(void) shellThread, (void *)&shell_cfg1); + printf(PORT_INFO "\r\n"); chThdSleep(2); @@ -189,7 +206,9 @@ int main(void) printf("Priority levels %d\r\n", CORTEX_PRIORITY_LEVELS); - test_execute((BaseSequentialStream *)&SD1); + //test_execute((BaseSequentialStream *)&SD1); + + NRF_P0->DETECTMODE = 0; while (true) { if (watchdog_started && -- cgit v1.2.3