Skip to content

Commit

Permalink
Covert the master side to use the SPI driver.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimitris Papavasiliou committed Feb 12, 2021
1 parent d34251d commit 1ee8ef0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
5 changes: 4 additions & 1 deletion keyboards/handwired/lagrange/lagrange.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

#include "quantum.h"

#define SPI_SS_PIN B0
#if !defined(SPI_SS_PIN)
# define SPI_SS_PIN B0
#endif

#define SPI_SCK_PIN B1
#define SPI_MOSI_PIN B2
#define SPI_MISO_PIN B3
Expand Down
2 changes: 1 addition & 1 deletion keyboards/handwired/lagrange/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ UNICODE_ENABLE = yes
SPLIT_KEYBOARD = yes
SPLIT_TRANSPORT = custom

SRC += transport.c
SRC += transport.c spi_master.c
28 changes: 20 additions & 8 deletions keyboards/handwired/lagrange/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include <spi_master.h>

#include "quantum.h"
#include "split_util.h"
#include "timer.h"
Expand Down Expand Up @@ -78,14 +80,23 @@ bool transport_master(matrix_row_t matrix[]) {
* while transmitting LED and layer states. */

shake_hands(true);
writePinLow(SPI_SS_PIN);

spi_start(SPI_SS_PIN, 0, 0, 4);

for (i = 0 ; i < sizeof(matrix_row_t[MATRIX_ROWS / 2]) ; i += 1) {
((uint8_t *)matrix)[i] = transceive(i < sizeof(struct led_context) ?
((uint8_t *)&context)[i] : 0);
spi_status_t x;

x = spi_write(i < sizeof(struct led_context) ?
((uint8_t *)&context)[i] : 0);

if (x == SPI_STATUS_TIMEOUT) {
return false;
}

((uint8_t *)matrix)[i] = (uint8_t)x;
}

writePinHigh(SPI_SS_PIN);
spi_stop();

return true;
}
Expand Down Expand Up @@ -130,13 +141,14 @@ void transport_slave(matrix_row_t matrix[]) {
}

void transport_master_init(void) {
/* We need to set the SS pin as output as the handshake logic
* above depends on it and the SPI master driver won't do it
* before we call spi_start(). */

writePinHigh(SPI_SS_PIN);
setPinOutput(SPI_SS_PIN);
setPinOutput(SPI_SCK_PIN);
setPinOutput(SPI_MOSI_PIN);
setPinInput(SPI_MISO_PIN);

SPCR = (_BV(SPE) | _BV(MSTR));
spi_init();

shake_hands(true);
}
Expand Down

0 comments on commit 1ee8ef0

Please sign in to comment.