diff options
author | NIIBE Yutaka <gniibe@fsij.org> | 2016-05-24 17:34:20 +0900 |
---|---|---|
committer | NIIBE Yutaka <gniibe@fsij.org> | 2016-05-24 17:34:20 +0900 |
commit | 2578fc0f303b97ab0fd1b1935cd00f6f45a353e9 (patch) | |
tree | 18160939bc9fde9fadf999f73b001b523813fc29 /example-fs-bb48 | |
parent | 2704416c384d5e2990621b15be01f37f4f9c49e0 (diff) |
Update example-fs-bb48
Diffstat (limited to 'example-fs-bb48')
-rw-r--r-- | example-fs-bb48/Makefile | 2 | ||||
-rw-r--r-- | example-fs-bb48/adc.h | 2 | ||||
-rw-r--r-- | example-fs-bb48/adc_kl27z.c | 68 | ||||
-rw-r--r-- | example-fs-bb48/command.c | 322 | ||||
-rw-r--r-- | example-fs-bb48/command.h | 1 | ||||
-rw-r--r-- | example-fs-bb48/config.h | 2 | ||||
-rw-r--r-- | example-fs-bb48/sample.c | 95 | ||||
-rw-r--r-- | example-fs-bb48/tty.h | 2 | ||||
-rw-r--r-- | example-fs-bb48/usb-cdc.c | 9 |
9 files changed, 390 insertions, 113 deletions
diff --git a/example-fs-bb48/Makefile b/example-fs-bb48/Makefile index 0978cdc..c806c9c 100644 --- a/example-fs-bb48/Makefile +++ b/example-fs-bb48/Makefile @@ -6,7 +6,7 @@ PROJECT = sample CHOPSTX = .. LDSCRIPT= sample.ld -CSRC = sample.c first-pages.c usb_kl27z.c usb-cdc.c adc_kl27z.c +CSRC = sample.c first-pages.c usb_kl27z.c usb-cdc.c adc_kl27z.c command.c ################################### CROSS = arm-none-eabi- diff --git a/example-fs-bb48/adc.h b/example-fs-bb48/adc.h index 54e1f4e..85a49b0 100644 --- a/example-fs-bb48/adc.h +++ b/example-fs-bb48/adc.h @@ -1,4 +1,4 @@ -void adc_init (void); +int adc_init (void); void adc_start (void); void adc_stop (void); diff --git a/example-fs-bb48/adc_kl27z.c b/example-fs-bb48/adc_kl27z.c index 9c30fd2..7ad9517 100644 --- a/example-fs-bb48/adc_kl27z.c +++ b/example-fs-bb48/adc_kl27z.c @@ -81,48 +81,53 @@ static struct ADC *const ADC = (struct ADC *const)0x4003B000; #define ADC_SC1_ADCSTOP 31 /* CFG1 */ -#define ADC_CLOCK_SOURCE_ASYNCH 3 -#define ADC_RESOLUTION_16BIT 3 -#define ADC_ADLSMP_SHORT 0 -#define ADC_ADLSMP_LONG 1 -#define ADC_ADIV_1 0 -#define ADC_ADIV_8 3 -#define ADC_ADLPC_NORMAL 1 +#define ADC_CLOCK_SOURCE_ASYNCH (3 << 0) +#define ADC_MODE_16BIT (3 << 2) +#define ADC_ADLSMP_SHORT (0 << 4) +#define ADC_ADLSMP_LONG (1 << 4) +#define ADC_ADIV_1 (0 << 5) +#define ADC_ADIV_8 (3 << 5) +#define ADC_ADLPC_NORMAL (0 << 7) +#define ADC_ADLPC_LOWPOWER (1 << 7) /**/ #define ADC_CLOCK_SOURCE ADC_CLOCK_SOURCE_ASYNCH -#define ADC_MODE (ADC_RESOLUTION_16BIT << 2) -#define ADC_ADLSMP (ADC_ADLSMP_SHORT << 4) -#define ADC_ADIV (ADC_ADIV_8 << 5) -#define ADC_ADLPC (ADC_ADLPC_NORMAL << 7) +#define ADC_MODE ADC_MODE_16BIT +#if 0 +#define ADC_ADLSMP ADC_ADLSMP_SHORT +#else +#define ADC_ADLSMP ADC_ADLSMP_LONG +#endif +#define ADC_ADIV ADC_ADIV_8 +#define ADC_ADLPC ADC_ADLPC_LOWPOWER /* CFG2 */ #define ADC_ADLSTS_DEFAULT 0 /* 24 cycles if CFG1.ADLSMP=1, 4 if not. */ -#define ADC_ADHSC_NORMAL 0 -#define ADC_ADHSC_HIGHSPEED 1 -#define ADC_ADACK_ENABLE 1 -#define ADC_ADACK_DISABLE 0 -#define ADC_MUXSEL_A 0 -#define ADC_MUXSEL_B 1 +#define ADC_ADHSC_NORMAL (0 << 2) +#define ADC_ADHSC_HIGHSPEED (1 << 2) +#define ADC_ADACK_DISABLE (0 << 3) +#define ADC_ADACK_ENABLE (1 << 3) +#define ADC_MUXSEL_A (0 << 4) +#define ADC_MUXSEL_B (1 << 4) /**/ #define ADC_ADLSTS ADC_ADLSTS_DEFAULT -#define ADC_ADHSC (ADC_ADHSC_NORMAL << 2) -#define ADC_ADACKEN (ADC_ADACK_ENABLE << 3) -#define ADC_MUXSEL (ADC_MUXSEL_A << 4) +#define ADC_ADHSC ADC_ADHSC_NORMAL +#define ADC_ADACKEN ADC_ADACK_ENABLE +#define ADC_MUXSEL ADC_MUXSEL_A /* SC2 */ -#define ADC_SC2_ADTRG (1 << 6) /* For hardware trigger */ -#define ADC_SC2_ACFE (1 << 5) -#define ADC_SC2_ACFGT (1 << 4) -#define ADC_SC2_ACREN (1 << 3) +#define ADC_SC2_REFSEL_DEFAULT 1 /* Internal Voltage Reference??? */ #define ADC_SC2_DMAEN (1 << 2) -#define ADC_SC2_REFSEL_DEFAULT 0 +#define ADC_SC2_ACREN (1 << 3) +#define ADC_SC2_ACFGT (1 << 4) +#define ADC_SC2_ACFE (1 << 5) +#define ADC_SC2_ADTRG (1 << 6) /* For hardware trigger */ /* SC3 */ -#define ADC_SC3_CAL (1 << 7) -#define ADC_SC3_CALF (1 << 6) -#define ADC_SC3_ADCO (1 << 3) -#define ADC_SC3_AVGE (1 << 2) #define ADC_SC3_AVGS11 0x03 +#define ADC_SC3_AVGE (1 << 2) +#define ADC_SC3_ADCO (1 << 3) +#define ADC_SC3_CALF (1 << 6) +#define ADC_SC3_CAL (1 << 7) /* * Initialize ADC module, do calibration. @@ -138,9 +143,8 @@ adc_init (void) ADC->CFG1 = ADC_CLOCK_SOURCE | ADC_MODE | ADC_ADLSMP | ADC_ADIV | ADC_ADLPC; ADC->CFG2 = ADC_ADLSTS | ADC_ADHSC | ADC_ADACKEN | ADC_MUXSEL; - ADC->SC2 = 0; + ADC->SC2 = ADC_SC2_REFSEL_DEFAULT; ADC->SC3 = ADC_SC3_CAL | ADC_SC3_CALF | ADC_SC3_AVGE | ADC_SC3_AVGS11; - ADC->SC1[0] = ADC_SC1_TEMPSENSOR; /* Wait ADC completion */ while ((ADC->SC1[0] & ADC_SC1_COCO) == 0) @@ -171,7 +175,7 @@ adc_start (void) { ADC->CFG1 = ADC_CLOCK_SOURCE | ADC_MODE | ADC_ADLSMP | ADC_ADIV | ADC_ADLPC; ADC->CFG2 = ADC_ADLSTS | ADC_ADHSC | ADC_ADACKEN | ADC_MUXSEL; - ADC->SC2 = 0; + ADC->SC2 = ADC_SC2_REFSEL_DEFAULT; ADC->SC3 = 0; } diff --git a/example-fs-bb48/command.c b/example-fs-bb48/command.c new file mode 100644 index 0000000..480cb2e --- /dev/null +++ b/example-fs-bb48/command.c @@ -0,0 +1,322 @@ +#include <string.h> +#include <stdint.h> +#include <chopstx.h> +#include "tty.h" +#include "config.h" +#ifdef TEMPERATURE_SUPPORT +#include "adc.h" +static int adc_initialized = 0; +#endif + +struct command_table +{ + const char *name; + void (*handler) (struct tty *tty, const char *line); +}; + +/* + * Put a line (or lines) to TTY. + * LINE should be terminated with newline. + */ +static void +put_line (struct tty *tty, const char *line) +{ + tty_send (tty, (uint8_t *)line, strlen (line)); +} + +static const char *help_string = + "mdw ADDR [COUNT]\r\n" + "mww ADDR VALUE [COUNT]\r\n" +#ifdef CRC32_SUPPORT + "crc32 string\r\n" +#endif +#ifdef TEMPERATURE_SUPPORT + "temp\r\n" +#endif + "help\r\n"; + +static char hexchar (uint8_t x) +{ + x &= 0x0f; + if (x <= 0x09) + return '0' + x; + else if (x <= 0x0f) + return 'a' + x - 10; + else + return '?'; +} + +static char * +compose_decimal (char *s, int value) +{ + uint32_t v; + int col = 1000000000; + int d; + int digit_output = 0; + + if (value < 0) + { + *s++ = '-'; + v = 1 + ~((uint32_t)value); + } + else + v = (uint32_t)value; + + while (col >= 10) + { + if (v >= (uint32_t)col) + { + d = v / col; + v = v - d * col; + *s++ = d + '0'; + digit_output = 1; + } + else if (digit_output) + *s++ = '0'; + + col = col / 10; + } + + *s++ = v + '0'; + + return s; +} + +static char * +compose_hex (char *s, uint32_t v) +{ + s[0] = hexchar (v >> 28); + s[1] = hexchar (v >> 24); + s[2] = hexchar (v >> 20); + s[3] = hexchar (v >> 16); + s[4] = hexchar (v >> 12); + s[5] = hexchar (v >> 8); + s[6] = hexchar (v >> 4); + s[7] = hexchar (v); + return s+8; +} + +static void +cmd_mdw (struct tty *tty, const char *line) +{ + int i; + uint32_t addr = 0; + int count = 0; + char c; + + if (line[0] == '0' && line[1] == 'x') + line = line + 2; + while (1) + { + c = *line++; + + if (c == 0) + { + count = 1; + break; + } + + if (c == ' ') + break; + + addr = (addr << 4); + if (c >= '0' && c <= '9') + addr += (c - '0'); + else if (c >= 'a' && c <= 'f') + addr += (c - 'a') + 10; + else if (c >= 'A' && c <= 'F') + addr += (c - 'A') + 10; + else + { + put_line (tty, "mdw error\r\n"); + return; + } + } + + addr &= ~3; + + if (count == 0) + { + while (1) + { + c = *line++; + + if (c == 0 || c == ' ') + break; + + count = count * 10; + if (c >= '0' && c <= '9') + count += c - '0'; + else + { + put_line (tty, "mdw error\r\n"); + return; + } + } + } + + i = 0; + while (i < count) + { + uint32_t v; + char output[48]; + char *s; + + s = compose_hex (output, addr); + *s++ = ':'; + *s++ = ' '; + + while (1) + { + v = *(uint32_t *)addr; + s = compose_hex (s, v); + i++; + addr += 4; + if (i >= count || (i % 4) == 0) + break; + *s++ = ' '; + } + + *s++ = '\r'; + *s++ = '\n'; + tty_send (tty, (uint8_t *)output, s - output); + } +} + +static void +cmd_mww (struct tty *tty, const char *line) +{ + (void)tty; + (void)line; + put_line (tty, "mww not yet supported\r\n"); +} + + +#ifdef CRC32_SUPPORT +#include "crc32.h" + +static void +cmd_crc32 (struct tty *tty, const char *line) +{ + uint32_t v; + char string[10]; + char *s; + + crc32_init (); + while (*line) + crc32_u8 (*line++); + v = crc32_value () ^ 0xffffffff; + + s = compose_hex (string, v); + *s++ = '\r'; + *s++ = '\n'; + tty_send (tty, (uint8_t *)string, sizeof (string)); +} +#endif + +#ifdef TEMPERATURE_SUPPORT +#define ADCV_TEMP25 14800 /* TYP: 716mV=14219 */ +#define ADCV_100M 3217 + +/* m = 1.62 mV/C_degree */ + +static void +cmd_temperature (struct tty *tty, const char *line) +{ + uint32_t v; + int t; + char string[13]; + char *s; + + (void)line; + + if (!adc_initialized) + { + if (adc_init ()) + { + put_line (tty, "adc_init error\r\n"); + return; + } + else + { + adc_start (); + adc_initialized = 1; + } + } + + adc_start_conversion (0, 1); + adc_wait_completion (NULL); + v = adc_buf[0]; + + t = 25 - (((int)v - ADCV_TEMP25) * 100 / ADCV_100M); + + s = compose_hex (string, v); + *s++ = '\r'; + *s++ = '\n'; + tty_send (tty, (uint8_t *)string, s - string); + + s = compose_decimal (string, t); + *s++ = '\r'; + *s++ = '\n'; + tty_send (tty, (uint8_t *)string, s - string); +} +#endif + +static void +cmd_help (struct tty *tty, const char *line) +{ + (void)line; + put_line (tty, help_string); +} + + +struct command_table command_table[] = { + { "mdw", cmd_mdw }, + { "mww", cmd_mww }, +#ifdef CRC32_SUPPORT + { "crc32", cmd_crc32 }, +#endif +#ifdef TEMPERATURE_SUPPORT + { "temp", cmd_temperature }, +#endif + { "help", cmd_help }, +}; + +#define N_CMDS (int)(sizeof (command_table) / sizeof (struct command_table)) + + +/* + * Dispatch a command parsing LINE. + * Line is NULL terminated with no newline. + */ +void +cmd_dispatch (struct tty *tty, const char *line) +{ + int i; + const char *p; + unsigned int n = 0; + + p = line; + while (*p) + { + if (*p++ == ' ') + break; + n++; + } + + for (i = 0; i < N_CMDS; i++) + if (n == strlen (command_table[i].name) + && strncmp ((const char *)line, command_table[i].name, n) == 0) + break; + + if (i != N_CMDS) + (*command_table[i].handler) (tty, p); + else + { + uint8_t crlf[] = { '\r', '\n' }; + + put_line (tty, "No such command: "); + tty_send (tty, (const uint8_t *)line, n); + tty_send (tty, crlf, sizeof (crlf)); + } +} diff --git a/example-fs-bb48/command.h b/example-fs-bb48/command.h new file mode 100644 index 0000000..139ab4a --- /dev/null +++ b/example-fs-bb48/command.h @@ -0,0 +1 @@ +void cmd_dispatch (struct tty *tty, const char *line); diff --git a/example-fs-bb48/config.h b/example-fs-bb48/config.h new file mode 100644 index 0000000..b04454f --- /dev/null +++ b/example-fs-bb48/config.h @@ -0,0 +1,2 @@ +#define CRC32_SUPPORT +#define TEMPERATURE_SUPPORT diff --git a/example-fs-bb48/sample.c b/example-fs-bb48/sample.c index 5966577..db8aac5 100644 --- a/example-fs-bb48/sample.c +++ b/example-fs-bb48/sample.c @@ -6,9 +6,7 @@ #include "usb_lld.h" #include "tty.h" #include "board.h" - -#include "crc32.h" -#include "adc.h" +#include "command.h" struct GPIO { volatile uint32_t PDOR; /* Port Data Output Register */ @@ -128,9 +126,6 @@ main (int argc, const char *argv[]) (void)argc; (void)argv; - adc_init (); - adc_start (); - chopstx_mutex_init (&mtx); chopstx_cond_init (&cnd0); chopstx_cond_init (&cnd1); @@ -158,6 +153,7 @@ main (int argc, const char *argv[]) { uint8_t s[LINEBUFSIZE]; + connection_loop: u = 1; tty_wait_connection (tty); @@ -176,85 +172,32 @@ main (int argc, const char *argv[]) while (1) { - int size; uint32_t usec; - usec = 3000000; /* 3.0 seconds */ - size = tty_recv (tty, s + 4, &usec); - if (size < 0) + /* Prompt */ + if (tty_send (tty, (uint8_t *)"> ", 2) < 0) break; - if (size) + usec = 3000000; /* 3.0 seconds */ + while (1) { - unsigned int value; + int size = tty_recv (tty, s, &usec); + u ^= 1; - if (s[4] == 't') - { - s[0] = 'T'; - s[1] = 'M'; - - adc_start_conversion (0, 1); - adc_wait_completion (NULL); - value = adc_buf[0]; - - s[4] = hexchar (value >> 28); - s[5] = hexchar (value >> 24); - s[6] = hexchar (value >> 20); - s[7] = hexchar (value >> 16); - s[8] = hexchar (value >> 12); - s[9] = hexchar (value >> 8); - s[10] = hexchar (value >> 4); - s[11] = hexchar (value); - s[12] = '\r'; - s[13] = '\n'; - - if (tty_send (tty, s, 14) < 0) - break; - } - else if (s[4] == 'c') - { - int i; - - size--; - - crc32_init (); - s[0] = hexchar (size >> 4); - s[1] = hexchar (size & 0x0f); - - for (i = 0; i < size; i++) - crc32_u8 (s[4 + i]); - value = crc32_value () ^ 0xffffffff; - - s[4] = hexchar (value >> 28); - s[5] = hexchar (value >> 24); - s[6] = hexchar (value >> 20); - s[7] = hexchar (value >> 16); - s[8] = hexchar (value >> 12); - s[9] = hexchar (value >> 8); - s[10] = hexchar (value >> 4); - s[11] = hexchar (value); - s[12] = '\r'; - s[13] = '\n'; - - if (tty_send (tty, s, 14) < 0) - break; - } - else + if (size < 0) + goto connection_loop; + + if (size == 1) + /* Do nothing but prompt again. */ + break; + else if (size) { - size--; - - s[0] = hexchar (size >> 4); - s[1] = hexchar (size & 0x0f); - s[2] = ':'; - s[3] = ' '; - s[size + 4] = '\r'; - s[size + 5] = '\n'; - if (tty_send (tty, s, size + 6) < 0) - break; + /* Newline into NUL */ + s[size - 1] = 0; + cmd_dispatch (tty, (char *)s); + break; } } - - u ^= 1; } } diff --git a/example-fs-bb48/tty.h b/example-fs-bb48/tty.h index bd0f070..55e97da 100644 --- a/example-fs-bb48/tty.h +++ b/example-fs-bb48/tty.h @@ -5,5 +5,5 @@ struct tty; struct tty *tty_open (void); void tty_wait_configured (struct tty *tty); void tty_wait_connection (struct tty *tty); -int tty_send (struct tty *tty, uint8_t *buf, int count); +int tty_send (struct tty *tty, const uint8_t *buf, int count); int tty_recv (struct tty *tty, uint8_t *buf, uint32_t *timeout); diff --git a/example-fs-bb48/usb-cdc.c b/example-fs-bb48/usb-cdc.c index afbaff3..c950362 100644 --- a/example-fs-bb48/usb-cdc.c +++ b/example-fs-bb48/usb-cdc.c @@ -735,10 +735,10 @@ check_tx (struct tty *t) } int -tty_send (struct tty *t, uint8_t *buf, int len) +tty_send (struct tty *t, const uint8_t *buf, int len) { int r; - uint8_t *p; + const uint8_t *p; int count; p = buf; @@ -767,6 +767,11 @@ tty_send (struct tty *t, uint8_t *buf, int len) count = len >= 64 ? 64 : len; } + /* Wait until all sent. */ + chopstx_mutex_lock (&t->mtx); + while ((r = check_tx (t)) == 0) + chopstx_cond_wait (&t->cnd, &t->mtx); + chopstx_mutex_unlock (&t->mtx); return r; } |