Skip to content

Commit

Permalink
USB and ADC drivers, USB-CDC example
Browse files Browse the repository at this point in the history
  • Loading branch information
gl-sergei committed Oct 8, 2017
1 parent 9fa0caf commit 33dc201
Show file tree
Hide file tree
Showing 10 changed files with 2,859 additions and 20 deletions.
4 changes: 3 additions & 1 deletion example-tomu/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ PROJECT = sample

CHOPSTX = ..
LDSCRIPT= sample.ld
CSRC = sample.c
CSRC = sample.c usb-cdc.c

CHIP=efm32
USE_SYS = yes
USE_ADC = yes
USE_USB = yes

###################################
CROSS = arm-none-eabi-
Expand Down
129 changes: 114 additions & 15 deletions example-tomu/sample.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <chopstx.h>
#include <mcu/efm32.h>
#include "board.h"
#include "sys.h" /* for set_led */
#include "usb_lld.h"
#include "adc.h"
#include "tty.h"

#define FEATURE_BUS_POWERED 0x80

Expand All @@ -31,7 +34,6 @@ set_led_pin (uint8_t port, uint8_t pin, int on)
static void
capsense_init (void)
{
CMU->HFPERCLKDIV |= (1 << 8);
CMU->HFPERCLKEN0 |= CMU_HFPERCLKEN0_ACMP0
| CMU_HFPERCLKEN0_TIMER0 | CMU_HFPERCLKEN0_PRS;

Expand All @@ -40,16 +42,14 @@ capsense_init (void)
| (0x0 << 30) /* HALFBIAS */
| (0x7 << 24) /* BIASPROG */
| (0x7 << 8) /* WARMTIME */
| (0x5 << 4) /* HYSTSEL */
;
| (0x5 << 4); /* HYSTSEL */

/* Select capacative sensing mode by selecting a resistor and enabling it */
ACMP0->INPUTSEL= (0x03 << 28) /* CSRESSEL */
| (0x01 << 24) /* CSRESEN */
| (0x00 << 16) /* LPREF */
| (0x3d << 8) /* VDDLEVEL */
| (0x0B << 4) /* NEGSEL = CAPSENSE */
;
| (0x0B << 4);/* NEGSEL = CAPSENSE */

/* Enable ACMP if requested. */
MASKED_WRITE(ACMP0->CTRL, 0x1, 1);
Expand All @@ -74,7 +74,6 @@ capsense_init (void)
PRS->CH[0].CTRL = (1 << 24) /* EDSEL = POSEDGE */
| (2 << 16) /* SOURCESEL = ACMP0 */
| (0 << 0); /* SIGSEL = ACMP0OUT */

}

static void *
Expand All @@ -85,7 +84,6 @@ csn (void *arg)
uint32_t count_max[2] = {0, 0};
uint32_t count;
uint32_t threshold;
uint32_t t[2] = {0, 0};
uint32_t channel;

chopstx_mutex_lock (&mtx);
Expand Down Expand Up @@ -113,11 +111,13 @@ csn (void *arg)

threshold = count_max[channel];
threshold -= count_max[channel] >> 3;
t[channel] = (count > 0 && count < threshold) ? 1 : 0;
if (count > 0 && count < threshold)
touch |= 1 << channel;
else
touch &= ~(1 << channel);
if (count > count_max[channel])
count_max[channel] = count;
}
touch = t[0] | t[1];
}

return NULL;
Expand All @@ -137,11 +137,13 @@ pwm (void *arg)

while (1)
{
set_led (u&v);
set_led_pin (1, 7, touch);
if (!touch)
set_led (u&v);
chopstx_usec_wait (m);
set_led (0);
set_led_pin (1, 7, touch);
if (!touch)
set_led (0);
set_led_pin (0, 0, touch & 1);
set_led_pin (1, 7, (touch & 2) >> 1);
chopstx_usec_wait (100-m);
}

Expand Down Expand Up @@ -185,9 +187,46 @@ extern uint8_t __process3_stack_base__[], __process3_stack_size__[];
#define STACK_ADDR_CSN ((uint32_t)__process3_stack_base__)
#define STACK_SIZE_CSN ((uint32_t)__process3_stack_size__)

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 void
adc_string (char *s)
{
int i;

adc_start_conversion (0, 6);
adc_wait_completion ();

for (i = 0; i < 6; i++)
{
s[i * 8 + 0] = hexchar ((adc_buf[i] >> 0) & 0xf);
s[i * 8 + 1] = hexchar ((adc_buf[i] >> 4) & 0xf);
s[i * 8 + 2] = hexchar ((adc_buf[i] >> 8) & 0xf);
s[i * 8 + 3] = hexchar ((adc_buf[i] >> 12) & 0xf);
s[i * 8 + 4] = hexchar ((adc_buf[i] >> 16) & 0xf);
s[i * 8 + 5] = hexchar ((adc_buf[i] >> 20) & 0xf);
s[i * 8 + 6] = hexchar ((adc_buf[i] >> 24) & 0xf);
s[i * 8 + 7] = hexchar ((adc_buf[i] >> 28) & 0xf);
}
s[i * 8 + 0] = '\r';
s[i * 8 + 1] = '\n';
}

int
main (int argc, const char *argv[])
{
struct tty *tty;
uint8_t count;

(void)argc;
(void)argv;

Expand All @@ -205,6 +244,9 @@ main (int argc, const char *argv[])

capsense_init ();

adc_init ();
adc_start ();

chopstx_usec_wait (200*1000);

chopstx_mutex_lock (&mtx);
Expand All @@ -213,10 +255,67 @@ main (int argc, const char *argv[])
chopstx_cond_signal (&cnd2);
chopstx_mutex_unlock (&mtx);

u = 1;

tty = tty_open ();
tty_wait_configured (tty);

count = 0;
m = 50;
while (1)
{
u ^= 1;
chopstx_usec_wait (200*1000*6);
static char s[LINEBUFSIZE + 4] __attribute__((aligned(4)));
char p[50];

u = 1;
tty_wait_connection (tty);

chopstx_usec_wait (50*1000);

/* Send ZLP at the beginning. */
tty_send (tty, s, 0);

memcpy (s, "xx: You've got Chopstx running on Tomu board!\r\n", 47);
s[0] = hexchar (count >> 4);
s[1] = hexchar (count & 0x0f);
count++;

if (tty_send (tty, s, 47) < 0)
continue;

while (1)
{
int size;
uint32_t usec;

usec = 3000000; /* 3.0 seconds */
size = tty_recv (tty, s + 4, &usec);
if (size < 0)
break;

if (size == 4 && memcmp(s + 4, "adc", 3) == 0)
{
adc_string (p);
if (tty_send (tty, p, 50) < 0)
break;
}

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;
}

u ^= 1;
}
}

return 0;
Expand Down
4 changes: 4 additions & 0 deletions example-tomu/sample.ld
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ __process0_stack_size__ = 0x0300; /* Main program */
__process1_stack_size__ = 0x0200; /* first thread program */
__process2_stack_size__ = 0x0100; /* second thread program */
__process3_stack_size__ = 0x0200; /* third thread program */
__process4_stack_size__ = 0x0200; /* fourth thread program */

MEMORY
{
Expand Down Expand Up @@ -76,6 +77,9 @@ SECTIONS

.process_stack :
{
. = ALIGN(8);
__process4_stack_base__ = .;
. += __process4_stack_size__;
. = ALIGN(8);
__process3_stack_base__ = .;
. += __process3_stack_size__;
Expand Down
9 changes: 9 additions & 0 deletions example-tomu/tty.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#define LINEBUFSIZE 128

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, const char *buf, int count);
int tty_recv (struct tty *tty, char *buf, uint32_t *timeout);
Loading

0 comments on commit 33dc201

Please sign in to comment.