]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/arm_atsam/spi.c
Bringing Massdrop keyboard hardware configuration to keyboard level (#4593)
[qmk_firmware.git] / tmk_core / protocol / arm_atsam / spi.c
1 /*
2 Copyright 2018 Massdrop Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "arm_atsam_protocol.h"
19
20 sr_exp_t sr_exp_data;
21
22 void SR_EXP_WriteData(void)
23 {
24     SR_EXP_RCLK_LO;
25
26     while (!(SR_EXP_SERCOM->SPI.INTFLAG.bit.DRE)) { DBGC(DC_SPI_WRITE_DRE); }
27
28     SR_EXP_SERCOM->SPI.DATA.bit.DATA = sr_exp_data.reg & 0xFF; //Shift in bits 7-0
29     while (!(SR_EXP_SERCOM->SPI.INTFLAG.bit.TXC)) { DBGC(DC_SPI_WRITE_TXC_1); }
30
31     SR_EXP_SERCOM->SPI.DATA.bit.DATA = (sr_exp_data.reg >> 8) & 0xFF; //Shift in bits 15-8
32     while (!(SR_EXP_SERCOM->SPI.INTFLAG.bit.TXC)) { DBGC(DC_SPI_WRITE_TXC_2); }
33
34     SR_EXP_RCLK_HI;
35 }
36
37 void SR_EXP_Init(void)
38 {
39     DBGC(DC_SPI_INIT_BEGIN);
40
41     CLK_set_spi_freq(CHAN_SERCOM_SPI, FREQ_SPI_DEFAULT);
42
43     //Set up MCU Shift Register pins
44     PORT->Group[SR_EXP_RCLK_PORT].DIRSET.reg = (1 << SR_EXP_RCLK_PIN);
45     PORT->Group[SR_EXP_OE_N_PORT].DIRSET.reg = (1 << SR_EXP_OE_N_PIN);
46     
47     //Set up MCU SPI pins
48     PORT->Group[SR_EXP_DATAOUT_PORT].PMUX[SR_EXP_DATAOUT_PIN / 2].bit.SR_EXP_DATAOUT_MUX_SEL = SR_EXP_DATAOUT_MUX; //MUX select for sercom
49     PORT->Group[SR_EXP_SCLK_PORT].PMUX[SR_EXP_SCLK_PIN / 2].bit.SR_EXP_SCLK_MUX_SEL = SR_EXP_SCLK_MUX; //MUX select for sercom
50     PORT->Group[SR_EXP_DATAOUT_PORT].PINCFG[SR_EXP_DATAOUT_PIN].bit.PMUXEN = 1; //MUX Enable
51     PORT->Group[SR_EXP_SCLK_PORT].PINCFG[SR_EXP_SCLK_PIN].bit.PMUXEN = 1; //MUX Enable
52
53     //Initialize Shift Register
54     SR_EXP_OE_N_DIS;
55     SR_EXP_RCLK_HI;
56
57     SR_EXP_SERCOM->SPI.CTRLA.bit.DORD = 1; //Data Order - LSB is transferred first
58     SR_EXP_SERCOM->SPI.CTRLA.bit.CPOL = 1; //Clock Polarity - SCK high when idle. Leading edge of cycle is falling. Trailing rising.
59     SR_EXP_SERCOM->SPI.CTRLA.bit.CPHA = 1; //Clock Phase - Leading Edge Falling, change, Trailing Edge - Rising, sample
60     SR_EXP_SERCOM->SPI.CTRLA.bit.DIPO = 3; //Data In Pinout - SERCOM PAD[3] is used as data input (Configure away from DOPO. Not using input.)
61     SR_EXP_SERCOM->SPI.CTRLA.bit.DOPO = 0; //Data Output PAD[0], Serial Clock PAD[1]
62     SR_EXP_SERCOM->SPI.CTRLA.bit.MODE = 3; //Operating Mode - Master operation
63
64     SR_EXP_SERCOM->SPI.CTRLA.bit.ENABLE = 1; //Enable - Peripheral is enabled or being enabled
65     while (SR_EXP_SERCOM->SPI.SYNCBUSY.bit.ENABLE) { DBGC(DC_SPI_SYNC_ENABLING); }
66
67     sr_exp_data.reg = 0;
68     sr_exp_data.bit.HUB_CONNECT = 0;
69     sr_exp_data.bit.HUB_RESET_N = 0;
70     sr_exp_data.bit.S_UP = 0;
71     sr_exp_data.bit.E_UP_N = 1;
72     sr_exp_data.bit.S_DN1 = 1;
73     sr_exp_data.bit.E_DN1_N = 1;
74     sr_exp_data.bit.E_VBUS_1 = 0;
75     sr_exp_data.bit.E_VBUS_2 = 0;
76     sr_exp_data.bit.SRC_1 = 1;
77     sr_exp_data.bit.SRC_2 = 1;
78     sr_exp_data.bit.IRST = 1;
79     sr_exp_data.bit.SDB_N = 0;
80     SR_EXP_WriteData();
81
82     //Enable Shift Register output
83     SR_EXP_OE_N_ENA;
84
85     DBGC(DC_SPI_INIT_COMPLETE);
86 }
87