spi.c 3.5 KB

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