Demo using input to set 3-bit led colour

This commit is contained in:
Aadi Desai 2023-03-07 20:09:56 +00:00
parent 29e926beba
commit 5ad6a22db5
No known key found for this signature in database
4 changed files with 75 additions and 54 deletions

View file

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
set -e set -e
rm -f demo/*.o demo/*.d demo/demo.bin demo/demo.elf demo/demo.elf.map
python3 demo/demo.py --build-path build/gsd_orangecrab/ --with-cxx python3 demo/demo.py --build-path build/gsd_orangecrab/ --with-cxx
rm -f demo/*.o demo/*.d demo/demo.bin demo/demo.elf demo/demo.elf.map && echo "Cleaned up build artifacts"
rm -f /mnt/c/Users/suple/Desktop/dfu-util-0.9-win64/gsd.bin && echo "Deleted old Win11 BIN" rm -f /mnt/c/Users/suple/Desktop/dfu-util-0.9-win64/gsd.bin && echo "Deleted old Win11 BIN"
mv demo.bin /mnt/c/Users/suple/Desktop/dfu-util-0.9-win64/gsd.bin && echo "Moved new BIN to Win11 Desktop" mv demo.bin /mnt/c/Users/suple/Desktop/dfu-util-0.9-win64/gsd.bin && echo "Moved new BIN to Win11 Desktop"

View file

@ -5,7 +5,7 @@ include $(SOC_DIRECTORY)/software/common.mak
OBJECTS = donut.o helloc.o crt0.o main.o OBJECTS = donut.o helloc.o crt0.o main.o
ifdef WITH_CXX ifdef WITH_CXX
OBJECTS += hellocpp.o OBJECTS += hellocpp.o leds.o
CFLAGS += -DWITH_CXX CFLAGS += -DWITH_CXX
endif endif

7
demo/leds.cpp Normal file
View file

@ -0,0 +1,7 @@
#include <generated/csr.h>
#include <stdio.h>
extern "C" void leds(int);
void leds(int v) {
leds_out_write(v);
}

View file

@ -5,28 +5,27 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <irq.h>
#include <libbase/uart.h>
#include <libbase/console.h>
#include <generated/csr.h> #include <generated/csr.h>
#include <irq.h>
#include <libbase/console.h>
#include <libbase/uart.h>
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* Uart */ /* Uart */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
static char *readstr(void) static char *readstr(void) {
{
char c[2]; char c[2];
static char s[64]; static char s[64];
static int ptr = 0; static int ptr = 0;
if(readchar_nonblock()) { if (readchar_nonblock()) {
c[0] = getchar(); c[0] = getchar();
c[1] = 0; c[1] = 0;
switch(c[0]) { switch (c[0]) {
case 0x7f: case 0x7f:
case 0x08: case 0x08:
if(ptr > 0) { if (ptr > 0) {
ptr--; ptr--;
fputs("\x08 \x08", stdout); fputs("\x08 \x08", stdout);
} }
@ -40,7 +39,7 @@ static char *readstr(void)
ptr = 0; ptr = 0;
return s; return s;
default: default:
if(ptr >= (sizeof(s) - 1)) if (ptr >= (sizeof(s) - 1))
break; break;
fputs(c, stdout); fputs(c, stdout);
s[ptr] = c[0]; s[ptr] = c[0];
@ -52,34 +51,33 @@ static char *readstr(void)
return NULL; return NULL;
} }
static char *get_token(char **str) static char *get_token(char **str) {
{
char *c, *d; char *c, *d;
c = (char *)strchr(*str, ' '); c = (char *)strchr(*str, ' ');
if(c == NULL) { if (c == NULL) {
d = *str; d = *str;
*str = *str+strlen(*str); *str = *str + strlen(*str);
return d; return d;
} }
*c = 0; *c = 0;
d = *str; d = *str;
*str = c+1; *str = c + 1;
return d; return d;
} }
static void prompt(void) static void prompt(void) {
{ printf("\e[92;1mStackSynth\e[0m> ");
printf("\e[92;1mlitex-demo-app\e[0m> ");
} }
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* Help */ /* Help */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
static void help(void) static void help(void) {
{ puts("\nLiteX custom demo app built "__DATE__
puts("\nLiteX minimal demo app built "__DATE__" "__TIME__"\n"); " "__TIME__
"\n");
puts("Available commands:"); puts("Available commands:");
puts("help - Show this command"); puts("help - Show this command");
puts("reboot - Reboot CPU"); puts("reboot - Reboot CPU");
@ -90,6 +88,9 @@ static void help(void)
puts("helloc - Hello C"); puts("helloc - Hello C");
#ifdef WITH_CXX #ifdef WITH_CXX
puts("hellocpp - Hello C++"); puts("hellocpp - Hello C++");
#ifdef CSR_LEDS_BASE
puts("leds - Led set demo");
#endif
#endif #endif
} }
@ -97,35 +98,38 @@ static void help(void)
/* Commands */ /* Commands */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
static void reboot_cmd(void) static void reboot_cmd(void) {
{
ctrl_reset_write(1); ctrl_reset_write(1);
} }
#ifdef CSR_LEDS_BASE #ifdef CSR_LEDS_BASE
static void led_cmd(void) static void led_cmd(void) {
{
int i; int i;
int j;
printf("Led demo...\n"); printf("Led demo...\n");
printf("Counter mode...\n"); printf("Counter mode...\n");
for(i=0; i<32; i++) { for (i = 0; i < 128; i++) {
leds_out_write(i); leds_out_write(i);
busy_wait(100); busy_wait(100);
} }
printf("Shift mode...\n"); printf("Shift mode...\n");
for(i=0; i<4; i++) { for (j = 0; j < 4; j++) {
leds_out_write(1<<i); for (i = 0; i < 4; i++) {
busy_wait(200); leds_out_write(1 << i);
busy_wait(200);
}
} }
for(i=0; i<4; i++) { for (j = 0; j < 4; j++) {
leds_out_write(1<<(3-i)); for (i = 0; i < 4; i++) {
busy_wait(200); leds_out_write(1 << (3 - i));
busy_wait(200);
}
} }
printf("Dance mode...\n"); printf("Dance mode...\n");
for(i=0; i<4; i++) { for (i = 0; i < 16; i++) {
leds_out_write(0x55); leds_out_write(0x55);
busy_wait(200); busy_wait(200);
leds_out_write(0xaa); leds_out_write(0xaa);
@ -134,18 +138,28 @@ static void led_cmd(void)
} }
#endif #endif
#ifdef WITH_CXX
#ifdef CSR_LEDS_BASE
extern void leds(int);
static void leds_cmd(char **val) {
int value = (int)strtol(get_token(val), NULL, 0);
printf("Setting LED to %6x\n", value);
leds(value);
}
#endif
#endif
extern void donut(void); extern void donut(void);
static void donut_cmd(void) static void donut_cmd(void) {
{
printf("Donut demo...\n"); printf("Donut demo...\n");
donut(); donut();
} }
extern void helloc(void); extern void helloc(void);
static void helloc_cmd(void) static void helloc_cmd(void) {
{
printf("Hello C demo...\n"); printf("Hello C demo...\n");
helloc(); helloc();
} }
@ -153,8 +167,7 @@ static void helloc_cmd(void)
#ifdef WITH_CXX #ifdef WITH_CXX
extern void hellocpp(void); extern void hellocpp(void);
static void hellocpp_cmd(void) static void hellocpp_cmd(void) {
{
printf("Hello C++ demo...\n"); printf("Hello C++ demo...\n");
hellocpp(); hellocpp();
} }
@ -164,35 +177,38 @@ static void hellocpp_cmd(void)
/* Console service / Main */ /* Console service / Main */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
static void console_service(void) static void console_service(void) {
{
char *str; char *str;
char *token; char *token;
str = readstr(); str = readstr();
if(str == NULL) return; if (str == NULL)
return;
token = get_token(&str); token = get_token(&str);
if(strcmp(token, "help") == 0) if (strcmp(token, "help") == 0)
help(); help();
else if(strcmp(token, "reboot") == 0) else if (strcmp(token, "reboot") == 0)
reboot_cmd(); reboot_cmd();
#ifdef CSR_LEDS_BASE #ifdef CSR_LEDS_BASE
else if(strcmp(token, "led") == 0) else if (strcmp(token, "led") == 0)
led_cmd(); led_cmd();
#endif #endif
else if(strcmp(token, "donut") == 0) else if (strcmp(token, "donut") == 0)
donut_cmd(); donut_cmd();
else if(strcmp(token, "helloc") == 0) else if (strcmp(token, "helloc") == 0)
helloc_cmd(); helloc_cmd();
#ifdef WITH_CXX #ifdef WITH_CXX
else if(strcmp(token, "hellocpp") == 0) #ifdef CSR_LEDS_BASE
else if (strcmp(token, "hellocpp") == 0)
hellocpp_cmd(); hellocpp_cmd();
else if (strcmp(token, "leds") == 0)
leds_cmd(&str);
#endif
#endif #endif
prompt(); prompt();
} }
int main(void) int main(void) {
{
#ifdef CONFIG_CPU_HAS_INTERRUPT #ifdef CONFIG_CPU_HAS_INTERRUPT
irq_setmask(0); irq_setmask(0);
irq_setie(1); irq_setie(1);
@ -202,9 +218,7 @@ int main(void)
help(); help();
prompt(); prompt();
while(1) { while (1) {
console_service(); console_service();
} }
return 0;
} }