arch: add function to concurrently send and receive SPI data (ATmega48P)

Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
1 files changed, 15 insertions(+), 1 deletions(-)

M arch/atmega48p_spi.h
M arch/atmega48p_spi.h +15 -1
@@ 1,5 1,5 @@ 
 /*
- * Copyright (c) 2023 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2023-2024 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal

          
@@ 140,6 140,20 @@ static inline void spi_tx_byte(uint8_t b
 		;
 }
 
+/* send & receive a single byte, CS is left up to the caller */
+static inline uint8_t spi_txrx_byte(uint8_t dummy)
+{
+	mmregs->spdr = dummy;
+
+	/* wait for rx to complete */
+	while (!(mmregs->spsr & REG_SPSR_SPIF))
+		;
+
+	return mmregs->spdr;
+}
+
+#define spi_rx_byte() spi_txrx_byte(0)
+
 #endif
 
 #endif