aboutsummaryrefslogtreecommitdiffstats
path: root/sw/spi.c
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2025-08-16 12:19:09 -0230
committerSam Anthony <sam@samanthony.xyz>2025-08-16 12:19:09 -0230
commit4a99ede565fa61005f2a6202669adaa87a8ce0ee (patch)
tree472d4e45858d50b4f719591dc3666b5fdc129549 /sw/spi.c
parent31f8d3b1edbe9dd965dba663e40c62c336fe7ca9 (diff)
downloadcan-gauge-interface-4a99ede565fa61005f2a6202669adaa87a8ce0ee.zip
spi and eeprom modules
Diffstat (limited to 'sw/spi.c')
-rw-r--r--sw/spi.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/sw/spi.c b/sw/spi.c
new file mode 100644
index 0000000..1793c4d
--- /dev/null
+++ b/sw/spi.c
@@ -0,0 +1,30 @@
+#include <xc.h>
+
+#include <stdint.h>
+
+#include "sys.h"
+#include "types.h"
+
+#include "spi.h"
+
+void
+spiInit(void) {
+ U8 junk;
+
+ TRISBbits.TRISB4 = IN; // SDI
+ TRISCbits.TRISC7 = OUT; // SDO
+ TRISBbits.TRISB6 = OUT; // SCK
+
+ SSPSTAT = 0x00;
+ SSPCON1 = 0x01; // FOSC/16 => 3MHz SPI clock
+ SSPCON1bits.SSPEN = 1; // enable
+ junk = SSPBUF; // dummy read to clear BF
+ (void)junk;
+}
+
+U8
+spiTx(U8 c) {
+ SSPBUF = c;
+ while (!SSPSTATbits.BF) {}
+ return SSPBUF;
+}