2019-01-01 02:23:52 +00:00
|
|
|
#include <stdio.h>
|
2019-01-01 14:37:14 +00:00
|
|
|
#include <irq.h>
|
2019-01-01 14:10:02 +00:00
|
|
|
#include <printf.h>
|
|
|
|
#include <uart.h>
|
2019-01-01 14:37:14 +00:00
|
|
|
#include <usb.h>
|
2019-01-23 02:25:07 +00:00
|
|
|
#include <time.h>
|
2019-01-01 02:23:52 +00:00
|
|
|
|
2019-03-25 09:39:06 +00:00
|
|
|
#include "spi.h"
|
2019-01-01 14:10:02 +00:00
|
|
|
#include <generated/csr.h>
|
|
|
|
|
2019-03-25 09:39:06 +00:00
|
|
|
struct ff_spi *spi;
|
|
|
|
|
2019-01-23 02:25:07 +00:00
|
|
|
void isr(void)
|
|
|
|
{
|
2019-01-01 14:37:14 +00:00
|
|
|
unsigned int irqs;
|
2019-01-23 02:25:07 +00:00
|
|
|
|
2019-01-01 14:37:14 +00:00
|
|
|
irqs = irq_pending() & irq_getmask();
|
2019-01-23 02:25:07 +00:00
|
|
|
|
|
|
|
if (irqs & (1 << USB_INTERRUPT))
|
|
|
|
usb_isr();
|
2019-01-01 14:37:14 +00:00
|
|
|
|
|
|
|
if (irqs & (1 << UART_INTERRUPT))
|
|
|
|
uart_isr();
|
2019-01-01 14:10:02 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 02:25:07 +00:00
|
|
|
static void rv_putchar(void *ignored, char c)
|
|
|
|
{
|
2019-01-01 14:10:02 +00:00
|
|
|
(void)ignored;
|
2019-02-28 02:48:25 +00:00
|
|
|
if (c == '\n')
|
|
|
|
uart_write('\r');
|
|
|
|
if (c == '\r')
|
|
|
|
return;
|
2019-01-01 14:10:02 +00:00
|
|
|
uart_write(c);
|
|
|
|
}
|
|
|
|
|
2019-01-23 02:25:07 +00:00
|
|
|
static void init(void)
|
|
|
|
{
|
2019-01-25 01:28:03 +00:00
|
|
|
init_printf(NULL, rv_putchar);
|
2019-01-01 14:10:02 +00:00
|
|
|
irq_setmask(0);
|
2019-01-01 14:37:14 +00:00
|
|
|
irq_setie(1);
|
|
|
|
uart_init();
|
2019-01-01 15:03:45 +00:00
|
|
|
usb_init();
|
2019-03-26 01:39:55 +00:00
|
|
|
dfu_init();
|
2019-01-23 02:25:07 +00:00
|
|
|
time_init();
|
2019-03-25 09:39:06 +00:00
|
|
|
|
|
|
|
spi = spiAlloc();
|
|
|
|
spiSetPin(spi, SP_MOSI, 0);
|
|
|
|
spiSetPin(spi, SP_MISO, 1);
|
|
|
|
spiSetPin(spi, SP_WP, 2);
|
|
|
|
spiSetPin(spi, SP_HOLD, 3);
|
|
|
|
spiSetPin(spi, SP_CLK, 4);
|
|
|
|
spiSetPin(spi, SP_CS, 5);
|
|
|
|
spiSetPin(spi, SP_D0, 0);
|
|
|
|
spiSetPin(spi, SP_D1, 1);
|
|
|
|
spiSetPin(spi, SP_D2, 2);
|
|
|
|
spiSetPin(spi, SP_D3, 3);
|
|
|
|
spiInit(spi);
|
2019-01-01 14:10:02 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 01:39:55 +00:00
|
|
|
#if 0
|
2019-02-28 02:48:25 +00:00
|
|
|
static const char *usb_hw_api(void) {
|
2019-01-23 02:25:07 +00:00
|
|
|
#ifdef CSR_USB_EP_0_OUT_EV_PENDING_ADDR
|
2019-02-28 02:48:25 +00:00
|
|
|
return "epfifo";
|
2019-01-23 02:25:07 +00:00
|
|
|
#else
|
2019-02-28 02:48:25 +00:00
|
|
|
#ifdef CSR_USB_OBUF_EMPTY_ADDR
|
|
|
|
return "rawfifo";
|
|
|
|
#else
|
|
|
|
#ifdef CSR_USB_WHATEVER
|
|
|
|
return "whatever";
|
|
|
|
#else
|
|
|
|
return "unrecognized hw api";
|
|
|
|
#endif /* CSR_USB_WHATEVER */
|
|
|
|
#endif /* CSR_USB_OBUF_EMPTY_ADDR */
|
|
|
|
#endif /* CSR_USB_EP_0_OUT_EV_PENDING_ADDR */
|
2019-01-23 02:25:07 +00:00
|
|
|
}
|
2019-03-26 01:39:55 +00:00
|
|
|
#endif
|
2019-01-23 02:25:07 +00:00
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2019-01-01 14:10:02 +00:00
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
|
|
|
|
|
|
|
init();
|
2019-01-23 02:25:07 +00:00
|
|
|
|
2019-03-25 09:39:06 +00:00
|
|
|
// // picospi_cfg_write(0);
|
|
|
|
// picospi_oe_write(0xff);
|
|
|
|
// picospi_do_write(0);
|
|
|
|
// // uint8_t last_value = 0;
|
|
|
|
// printf("\nToggling: %02x ", picospi_oe_read());
|
|
|
|
// while (1) {
|
|
|
|
// // uint8_t new_value = picospi_di_read();
|
|
|
|
// // if (new_value != last_value) {
|
|
|
|
// // printf("SPI %02x -> %02x\n", last_value, new_value);
|
|
|
|
// // last_value = new_value;
|
|
|
|
// // }
|
|
|
|
// // picospi_oe_write(0xff);
|
|
|
|
// // printf("\b0");
|
|
|
|
// // picospi_oe_write(0x00);
|
|
|
|
// // printf("\b1");
|
|
|
|
|
|
|
|
// // picospi_do_write(0x00);
|
|
|
|
// // printf("\b0");
|
|
|
|
// // picospi_do_write(0xff);
|
|
|
|
// // printf("\b1");
|
|
|
|
// }
|
|
|
|
// printf("\nPress any key to read...");
|
|
|
|
// while(1) {
|
|
|
|
// uart_read();
|
|
|
|
// struct spi_id id = spiId(spi);
|
|
|
|
// printf("Manufacturer ID: %s (%02x)\n", id.manufacturer, id.manufacturer_id);
|
|
|
|
// if (id.manufacturer_id != id._manufacturer_id)
|
|
|
|
// printf("!! JEDEC Manufacturer ID: %02x\n",
|
|
|
|
// id._manufacturer_id);
|
|
|
|
// printf("Memory model: %s (%02x)\n", id.model, id.memory_type);
|
|
|
|
// printf("Memory size: %s (%02x)\n", id.capacity, id.memory_size);
|
|
|
|
// printf("Device ID: %02x\n", id.device_id);
|
|
|
|
// if (id.device_id != id.signature)
|
|
|
|
// printf("!! Electronic Signature: %02x\n", id.signature);
|
|
|
|
// printf("Serial number: %02x %02x %02x %02x\n", id.serial[0], id.serial[1], id.serial[2], id.serial[3]);
|
|
|
|
// printf("Status 1: %02x\n", spiReadStatus(spi, 1));
|
|
|
|
// printf("Status 2: %02x\n", spiReadStatus(spi, 2));
|
|
|
|
// printf("Status 3: %02x\n", spiReadStatus(spi, 3));
|
|
|
|
// }
|
|
|
|
// puts("\nPress any key to start...");
|
2019-02-28 02:48:25 +00:00
|
|
|
// uart_read();
|
2019-03-25 09:39:06 +00:00
|
|
|
|
|
|
|
// printf("\n\nUSB API: %s\n", usb_hw_api());
|
|
|
|
// puts("Enabling USB");
|
2019-02-28 02:48:25 +00:00
|
|
|
usb_connect();
|
2019-03-25 09:39:06 +00:00
|
|
|
// printf("USB enabled.\n");
|
2019-03-05 03:54:48 +00:00
|
|
|
// usb_print_status();
|
2019-02-28 02:48:25 +00:00
|
|
|
int last = 0;
|
2019-01-23 02:25:07 +00:00
|
|
|
while (1)
|
|
|
|
{
|
2019-03-26 01:39:55 +00:00
|
|
|
// if (usb_irq_happened() != last) {
|
|
|
|
// last = usb_irq_happened();
|
|
|
|
// // printf("USB %d IRQ happened\n", last);
|
|
|
|
// }
|
2019-03-05 03:54:48 +00:00
|
|
|
usb_poll();
|
2019-03-10 07:25:33 +00:00
|
|
|
/*
|
2019-03-05 12:28:54 +00:00
|
|
|
printf("Press any key to send... ");
|
|
|
|
uart_read();
|
|
|
|
printf("Sending... ");
|
2019-03-10 07:25:33 +00:00
|
|
|
bfr[0] = 0;
|
|
|
|
bfr[1] = ~0;
|
2019-03-05 12:28:54 +00:00
|
|
|
bfr[2] = 0;
|
2019-03-10 07:25:33 +00:00
|
|
|
usb_send(NULL, 0, bfr, 1);
|
2019-03-05 12:28:54 +00:00
|
|
|
printf("Sent\n");
|
2019-03-10 07:25:33 +00:00
|
|
|
*/
|
2019-01-01 02:23:52 +00:00
|
|
|
}
|
2019-01-01 14:10:02 +00:00
|
|
|
return 0;
|
2019-01-01 02:23:52 +00:00
|
|
|
}
|