gadget-securevault/main.c
Daniele Lacamera c958c4becb Added USB-MSC
2019-11-04 15:38:49 +01:00

142 lines
3.2 KiB
C

/*
* (c) danielinux 2019
* GPLv.2
*
* See LICENSE for details
*/
#include <string.h>
#include <stdio.h>
#include "system.h"
#include "usecfs.h"
#include "led.h"
#include "display.h"
#include <wolfssl/wolfcrypt/random.h>
#include "spi_flash.h"
#include "uart.h"
#include "systick.h"
#include "ui.h"
#include "sdcard.h"
#include "button.h"
#include "usb.h"
extern uint32_t cpu_freq;
#define BLINK_INTERVAL 500
static int sdcard_detected = 0;
static void bootlevel_0(void)
{
WC_RNG hwrng;
clock_pll_on(0);
uart2_setup(115200, 8, 0, 1);
printf("Console on USART2 Enabled.\r\n");
printf("\r\n=====================\r\n");
printf("Entering bootlevel 0.\r\n");
// led_setup(RED_LED);
// led_setup(GREEN_LED);
led_setup(YELLOW_LED);
led_setup(BLUE_LED);
wc_InitRng(&hwrng);
printf("RNG enabled");
systick_enable();
printf("System clock started.\r\n");
button_init();
printf("Buttons initialized.\r\n");
}
static void bootlevel_1(void)
{
uint16_t spiflash_vendor;
int sdcard_present;
printf("=====================\r\n");
printf("Entering bootlevel 1.\r\n");
printf("Probing SPI flash...\r\n");
spi_init(0,0);
spiflash_vendor = spi_flash_probe();
if ((spiflash_vendor & 0xFF00) >> 8 != 0xBF) {
printf("FATAL: Unexpected SPI flash detected.\r\n");
panic();
} else {
printf("SPI flash detected (Vendor: %02X Model: %02X)\r\n", (spiflash_vendor & 0xFF00) >> 8, (spiflash_vendor & 0xFF));
}
printf("Initializing SDIO module\r\n");
sdcard_hw_init();
printf("Probing SD card slot...\r\n");
if (sdcard_detect() != 0) {
printf("SD Card not found.\r\n");
} else {
printf("SD Card detected.\r\n");
led_on(YELLOW_LED);
sdcard_detected = 1;
}
printf("Activating USB mass storage device\r\n");
printf("Activating I2C bus\r\n");
i2c_display_init();
printf("Display initialized.\r\n");
printf("Displaying splash screen...\r\n");
ui_init();
printf("UI initialized.\r\n");
printf("System up and running.\r\n\n\n");
}
void process_button(uint16_t press, int hold)
{
char c = bu2c(press);
ui_button_press(press);
}
int main(void) {
int hb = 1;
int fs_on = 0;
int ret;
volatile uint32_t poll_time = 0;
uint32_t bstatus = 0;
const uint32_t hb_len = 5000;
const uint32_t hb_pulse = 5;
bootlevel_0();
bootlevel_1();
while(1) {
hb = 1;
led_on(BLUE_LED);
poll_time = jiffies;
/* SD Card detection */
if (!sdcard_detected) {
sdcard_detected = !!(0 == sdcard_detect());
if (sdcard_detected) {
printf("Inserted SD Card");
ui_sdcard_insert();
led_on(YELLOW_LED);
}
}
ui_keepalive(hb_len);
do {
WFI();
usb_poll();
ret = button_poll(process_button);
if (jiffies - poll_time > hb_pulse) {
if (hb) {
hb = 0;
led_off(BLUE_LED);
}
}
} while ((jiffies - poll_time) < hb_len);
}
return 0;
}