From 99be520563834d51eb3ddd32b757a3dcd2486632 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Thu, 2 Oct 2025 14:45:56 -0400 Subject: spi systest --- fw/eeprom.c | 2 +- fw/init.c | 17 ------------ fw/init.h | 11 -------- fw/main.c | 10 +++---- fw/spi.c | 5 ++-- fw/sys.h | 9 ------- fw/system.c | 15 +++++++++++ fw/system.h | 8 ++++++ fw/tests/system/eeprom_systest.c | 31 +++++++++++++++++++--- fw/tests/system/spi_systest.c | 22 ++++++++++++++++ fw/usb.c | 56 +++++++++++++++++++++++++--------------- fw/usb.h | 2 ++ 12 files changed, 115 insertions(+), 73 deletions(-) delete mode 100644 fw/init.c delete mode 100644 fw/init.h delete mode 100644 fw/sys.h create mode 100644 fw/tests/system/spi_systest.c (limited to 'fw') diff --git a/fw/eeprom.c b/fw/eeprom.c index 686be40..567cdc1 100644 --- a/fw/eeprom.c +++ b/fw/eeprom.c @@ -3,7 +3,7 @@ #include #include -#include "sys.h" +#include "system.h" #include "types.h" #include "spi.h" diff --git a/fw/init.c b/fw/init.c deleted file mode 100644 index 9c89bee..0000000 --- a/fw/init.c +++ /dev/null @@ -1,17 +0,0 @@ -#include - -#include "init.h" - -void -clockInit(void) { - OSCCON = 0xFC; // HFINTOSC @ 16MHz, 3x PLL, PLL enabled - ACTCON = 0x90; // active clock tuning enabled for USB -} - -void -pinsInit(void) { - // Disable all analog pin functions - ANSELA = 0; - ANSELB = 0; - ANSELC = 0; -} diff --git a/fw/init.h b/fw/init.h deleted file mode 100644 index 0dc8dc3..0000000 --- a/fw/init.h +++ /dev/null @@ -1,11 +0,0 @@ -/* Device: PIC16F1459 - * Compiler: XC8 v3.00 - * - * Usage: - * - * #include - * #include "init.h" - */ - -void clockInit(void); -void pinsInit(void); diff --git a/fw/main.c b/fw/main.c index 3c54179..8bf4f80 100644 --- a/fw/main.c +++ b/fw/main.c @@ -2,22 +2,18 @@ #include -#include - +#include "system.h" #include "types.h" -#include "init.h" #include "spi.h" #include "eeprom.h" #include "usb.h" void main(void) { - clockInit(); - pinsInit(); + sysInit(); spiInit(); eepromInit(); - USBDeviceInit(); - USBDeviceAttach(); + usbInit(); for (;;) { usbTask(); diff --git a/fw/spi.c b/fw/spi.c index 1793c4d..9198ab1 100644 --- a/fw/spi.c +++ b/fw/spi.c @@ -2,7 +2,7 @@ #include -#include "sys.h" +#include "system.h" #include "types.h" #include "spi.h" @@ -16,8 +16,7 @@ spiInit(void) { TRISBbits.TRISB6 = OUT; // SCK SSPSTAT = 0x00; - SSPCON1 = 0x01; // FOSC/16 => 3MHz SPI clock - SSPCON1bits.SSPEN = 1; // enable + SSPCON1 = 0x22; // FOSC/64 => 750kHz SPI clock junk = SSPBUF; // dummy read to clear BF (void)junk; } diff --git a/fw/sys.h b/fw/sys.h deleted file mode 100644 index 8a71edb..0000000 --- a/fw/sys.h +++ /dev/null @@ -1,9 +0,0 @@ -/* Device: PIC16F1459 - * Compiler: XC8 v3.00 - */ - -// TRIS -enum { - OUT = 0, - IN = 1, -}; diff --git a/fw/system.c b/fw/system.c index f369673..720f456 100644 --- a/fw/system.c +++ b/fw/system.c @@ -1,3 +1,7 @@ +#include + +#include "system.h" + // CONFIG1 #pragma config FOSC = INTOSC // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin) #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) @@ -19,3 +23,14 @@ #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config LPBOR = OFF // Low-Power Brown Out Reset (Low-Power BOR is disabled) #pragma config LVP = ON // Low-Voltage Programming Enable + +void +sysInit(void) { + OSCCON = 0xFC; // HFINTOSC @ 16MHz, 3x PLL, PLL enabled + ACTCON = 0x90; // active clock tuning enabled for USB + + // Disable all analog pin functions + ANSELA = 0; + ANSELB = 0; + ANSELC = 0; +} diff --git a/fw/system.h b/fw/system.h index cfb2cfb..acd59df 100644 --- a/fw/system.h +++ b/fw/system.h @@ -3,4 +3,12 @@ #include "fixed_address_memory.h" +// TRIS +enum { + OUT = 0, + IN = 1, +}; + +void sysInit(void); + #endif // SYSTEM_H diff --git a/fw/tests/system/eeprom_systest.c b/fw/tests/system/eeprom_systest.c index 9099345..81fa189 100644 --- a/fw/tests/system/eeprom_systest.c +++ b/fw/tests/system/eeprom_systest.c @@ -2,20 +2,43 @@ #include +#include "system.h" #include "types.h" -#include "init.h" #include "spi.h" #include "eeprom.h" -#include "config.h" +#include "usb.h" void main(void) { - clockInit(); - pinsInit(); + sysInit(); spiInit(); eepromInit(); + usbInit(); + + T1CON = 0; + T1CONbits.TMR1CS = 0x0; // FOSC/4 + T1CONbits.T1CKPS = 0x3; // 1:8 prescaler + T1CONbits.TMR1ON = 1; + + TMR1IE = 1; + TMR1IF = 0; + PEIE = 1; + GIE = 1; for (;;) { } } + +void +__interrupt() isr(void) { + static U8 ctr = 0u; + + if (TMR1IF) { + if (ctr == 23u) { // 1s period + + } + ctr = (ctr+1u) % 23u; + TMR1IF = 0; + } +} diff --git a/fw/tests/system/spi_systest.c b/fw/tests/system/spi_systest.c new file mode 100644 index 0000000..1adc624 --- /dev/null +++ b/fw/tests/system/spi_systest.c @@ -0,0 +1,22 @@ +#include + +#include + +#include "system.h" +#include "types.h" +#include "spi.h" + +void +main(void) { + sysInit(); + spiInit(); + + for (;;) { + (void)spiTx(0x05); // 0b0000101 + } +} + +void +__interrupt() isr(void) { + +} diff --git a/fw/usb.c b/fw/usb.c index d793d31..ad78d79 100644 --- a/fw/usb.c +++ b/fw/usb.c @@ -59,6 +59,41 @@ static U8 txBuf[CDC_DATA_IN_EP_SIZE]; /***** Function Definitions *****/ +void +usbInit(void) { + USBDeviceInit(); + USBDeviceAttach(); +} + +void +usbTask(void) { + static State state = {idleState}; + + USBDeviceTasks(); + + if (USBGetDeviceState() < CONFIGURED_STATE) { + return; + } + if (USBIsDeviceSuspended()) { + return; + } + + if (USBUSARTIsTxTrfReady()) { + // Execute action and transition to next state + state = *state.next(); + } + + CDCTxService(); +} + +void +usbPrint(char *s) { + while (!USBUSARTIsTxTrfReady()) { + usbTask(); + } + putsUSBUSART(s); +} + // Rx a char from USB. // Returns FAIL if no data. static Status @@ -95,27 +130,6 @@ getcharBlock(char *c, U8 retries) { return FAIL; } -void -usbTask(void) { - static State state = {idleState}; - - USBDeviceTasks(); - - if (USBGetDeviceState() < CONFIGURED_STATE) { - return; - } - if (USBIsDeviceSuspended()) { - return; - } - - if (USBUSARTIsTxTrfReady()) { - // Execute action and transition to next state - state = *state.next(); - } - - CDCTxService(); -} - // Read (the start of) a command from USB. static State * idleState(void) { diff --git a/fw/usb.h b/fw/usb.h index 7ceae36..14b9bf7 100644 --- a/fw/usb.h +++ b/fw/usb.h @@ -1 +1,3 @@ +void usbInit(void); void usbTask(void); +void usbPrint(char *s); -- cgit v1.2.3