123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- /*
- * fw_app.c
- *
- * Copyright (C) 2019 Sylvain Munaut
- * All rights reserved.
- *
- * LGPL v3+, see LICENSE.lgpl3
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- #include <stdint.h>
- #include <stdbool.h>
- #include <string.h>
- #include "console.h"
- #include "led.h"
- #include "mini-printf.h"
- #include "spi.h"
- #include <no2usb/usb.h>
- #include <no2usb/usb_dfu_rt.h>
- #include "utils.h"
- #include "registers.h"
- extern const struct usb_stack_descriptors app_stack_desc;
- /*
- double dupa;
- float kupa;
- */
- static void
- serial_no_init()
- {
- uint8_t buf[8];
- char *id, *desc;
- int i;
- flash_manuf_id(buf);
- printf("Flash Manufacturer : %s\n", hexstr(buf, 3, true));
- flash_unique_id(buf);
- printf("Flash Unique ID : %s\n", hexstr(buf, 8, true));
- //uint32_t old = *(volatile uint32_t *)(MAILBOX_BASE + 0x00);
- //printf("Old=0x%x\n", old);
- //*(volatile uint32_t *)(MAILBOX_BASE + 0x00) = 0x00000004;
- //uint32_t new = *(volatile uint32_t *)(MAILBOX_BASE + 0x00);
- //printf("New=0x%x\n", new);
- //printf("Old=0x%x, New=0x%x\n", old, new);
- // Print Mailbox contents just to be sure */
- //printf("Mailbox period1: %d\n", mailbox_regs->regs.period1);
- //printf("Mailbox delay1: %d\n", mailbox_regs->regs.delay1);
- //// Debug print all mailbox memory
- //for (i = 0; i < 64; ++i) {
- // //printf("%x ", *((int*)MAILBOX_BASE+i));
- // printf("%x ", *((int*)MAILBOX_BASE+i));
- // if (i != 0 && i % 16)
- // printf("\n");
- //}
- //mailbox_regs->regs.delay3= 0x1;
- //printf("Mailbox delay1: %d\n", mailbox_regs->regs.delay3);
- /* Testing floats TODO: test if calculation is correct
- dupa = 3.1415;
- kupa = 2.74f;
- dupa *= kupa;
- printf("Pi * e: %.3f\n", dupa);
- */
- /* Overwrite descriptor string */
- /* In theory in rodata ... but nothing is ro here */
- id = hexstr(buf, 8, false);
- desc = (char*)app_stack_desc.str[1];
- for (i=0; i<16; i++)
- desc[2 + (i << 1)] = id[i];
- }
- static char _printf_buf[128];
- static void write_period1()
- {
- printf("Writing period1 0x1\n");
- //uint8_t *strMem = (uint8_t *)&_printf_buf;
- //for (int i=0; i<32; i++) {
- // printf("%x ", strMem[i]);
- //}
- mailbox_regs->regs.period1 = 0x1;
- printf("Done writing\n");
- //uint8_t * strMem = (uint8_t *)&_printf_buf;
- //for (int i=0; i<32; i++) {
- // printf("%x ", strMem[i]);
- //}
- printf("\n");
- }
- static void write_period1_2()
- {
- printf("Writing period1 0x2\n");
- //uint8_t *strMem = (uint8_t *)&_printf_buf;
- //for (int i=0; i<32; i++) {
- // printf("%x ", strMem[i]);
- //}
- mailbox_regs->regs.period1 = 0x2;
- printf("Done writing\n");
- //uint8_t * strMem = (uint8_t *)&_printf_buf;
- //for (int i=0; i<32; i++) {
- // printf("%x ", strMem[i]);
- //}
- }
- static void write_period1_4()
- {
- printf("Writing period1 0x4\n");
- mailbox_regs->regs.period1 = 0x4;
- printf("Done writing\n");
- printf("Reading now \n");
- printf("value: %d\n", mailbox_regs->regs.period1);
- }
- static void clear_period1()
- {
- printf("Clearing period1 val: %d\n", mailbox_regs->regs.period1);
- mailbox_regs->regs.period1 = 0x0;
- printf("Done clearing\n");
- }
- static void write_delay1()
- {
- printf("Writing delay1 0x1234\n");
- mailbox_regs->regs.delay1 = 0x1234;
- printf("Done writing\n");
- }
- static void clear_delay1()
- {
- printf("Clearing delay1 val: %d\n", mailbox_regs->regs.delay1);
- mailbox_regs->regs.delay1 = 0x0;
- printf("Done clearing\n");
- }
- static void
- boot_dfu(void)
- {
- /* Force re-enumeration */
- usb_disconnect();
- /* Boot firmware */
- volatile uint32_t *boot = (void*)0x80000000;
- *boot = (1 << 2) | (1 << 0);
- }
- void
- usb_dfu_rt_cb_reboot(void)
- {
- boot_dfu();
- }
- void main()
- {
- int cmd = 0;
- /* Init console IO */
- console_init();
- puts("Booting App image..\n");
- /* LED */
- led_init();
- led_color(48, 96, 5);
- led_blink(true, 200, 1000);
- led_breathe(true, 100, 200);
- led_state(true);
- /* SPI */
- spi_init();
- /* Enable USB directly */
- serial_no_init();
- usb_init(&app_stack_desc);
- usb_dfu_rt_init();
- /* Main loop */
- while (1)
- {
- /* Prompt ? */
- if (cmd >= 0)
- printf("Command> ");
- /* Poll for command */
- cmd = getchar_nowait();
- if (cmd >= 0) {
- if (cmd > 32 && cmd < 127) {
- putchar(cmd);
- putchar('\r');
- putchar('\n');
- }
- switch (cmd)
- {
- case 'p':
- usb_debug_print();
- break;
- case 'b':
- boot_dfu();
- break;
- case 'c':
- usb_connect();
- break;
- case 'd':
- usb_disconnect();
- break;
- case 'w':
- write_period1();
- break;
- case 'k':
- clear_period1();
- break;
- case 'a':
- write_period1_2();
- break;
- case 's':
- write_period1_4();
- break;
- case 'o':
- write_delay1();
- break;
- case 'm':
- clear_delay1();
- break;
- default:
- break;
- }
- }
- /* USB poll */
- usb_poll();
- }
- }
|