diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2025-10-02 14:45:56 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2025-10-02 14:45:56 -0400 |
| commit | 99be520563834d51eb3ddd32b757a3dcd2486632 (patch) | |
| tree | 7c31bcc262950d431a1676b62624b6ac82f30df3 | |
| parent | c91e3d96c90cd1c9b3b6155bc5e37954011cf0af (diff) | |
| download | can-gauge-interface-99be520563834d51eb3ddd32b757a3dcd2486632.zip | |
spi systest
| -rw-r--r-- | fw/eeprom.c | 2 | ||||
| -rw-r--r-- | fw/init.c | 17 | ||||
| -rw-r--r-- | fw/init.h | 11 | ||||
| -rw-r--r-- | fw/main.c | 10 | ||||
| -rw-r--r-- | fw/spi.c | 5 | ||||
| -rw-r--r-- | fw/sys.h | 9 | ||||
| -rw-r--r-- | fw/system.c | 15 | ||||
| -rw-r--r-- | fw/system.h | 8 | ||||
| -rw-r--r-- | fw/tests/system/eeprom_systest.c | 31 | ||||
| -rw-r--r-- | fw/tests/system/spi_systest.c | 22 | ||||
| -rw-r--r-- | fw/usb.c | 56 | ||||
| -rw-r--r-- | fw/usb.h | 2 |
12 files changed, 115 insertions, 73 deletions
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 <stdbool.h> #include <stdint.h> -#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 <xc.h> - -#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 <xc8.h> - * #include "init.h" - */ - -void clockInit(void); -void pinsInit(void); @@ -2,22 +2,18 @@ #include <stdint.h> -#include <usb_device.h> - +#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(); @@ -2,7 +2,7 @@ #include <stdint.h> -#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 <xc.h> + +#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 <stdint.h> +#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 <xc.h> + +#include <stdint.h> + +#include "system.h" +#include "types.h" +#include "spi.h" + +void +main(void) { + sysInit(); + spiInit(); + + for (;;) { + (void)spiTx(0x05); // 0b0000101 + } +} + +void +__interrupt() isr(void) { + +} @@ -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) { @@ -1 +1,3 @@ +void usbInit(void); void usbTask(void); +void usbPrint(char *s); |