jackknife.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. Copyright (c) 2009 Christopher Hall <hsw@openmoko.com>
  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 3 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 <inttypes.h>
  15. #include "regs.h"
  16. // override the default console bps for faster programming
  17. #define CONSOLE_BPS 115200
  18. #define SAMO_RESTRICTIONS 1
  19. #include "samo.h"
  20. // do not add any static variables
  21. // this program is restricted to 512 bytes .text
  22. // and stack/register variables only
  23. enum {
  24. COMMAND_BOARD_REVISION = 'A',
  25. COMMAND_SPI_WRITE = 'W',
  26. COMMAND_SPI_READ = 'R',
  27. COMMAND_SPI_DESELECT = 'H',
  28. COMMAND_SPI_SELECT = 'L',
  29. };
  30. static void serial_put(uint8_t c);
  31. static uint8_t serial_get(void);
  32. static uint32_t serial_get16(void);
  33. static void spi_put(uint8_t c);
  34. static uint8_t spi_get(void);
  35. int main(void)
  36. {
  37. init_pins();
  38. init_rs232_ch0();
  39. SDCARD_CS_HI();
  40. EEPROM_CS_HI();
  41. EEPROM_WP_HI();
  42. REG_SPI_CTL1 =
  43. //BPT_32_BITS |
  44. //BPT_16_BITS |
  45. BPT_8_BITS |
  46. //BPT_1_BITS |
  47. //CPHA |
  48. //CPOL |
  49. //MCBR_MCLK_DIV_512 |
  50. //MCBR_MCLK_DIV_256 |
  51. //MCBR_MCLK_DIV_128 |
  52. //MCBR_MCLK_DIV_64 |
  53. //MCBR_MCLK_DIV_32 |
  54. //MCBR_MCLK_DIV_16 |
  55. //MCBR_MCLK_DIV_8 |
  56. MCBR_MCLK_DIV_4 |
  57. //TXDE |
  58. //RXDE |
  59. MODE_MASTER |
  60. //MODE_SLAVE |
  61. ENA |
  62. 0;
  63. for (;;) {
  64. uint8_t command = serial_get();
  65. // switch will create jump table and overflow our 512 bytes
  66. if (COMMAND_BOARD_REVISION == command) {
  67. serial_put('A');
  68. serial_put(board_revision() + '0');
  69. } else if (COMMAND_SPI_DESELECT == command) {
  70. EEPROM_CS_HI();
  71. } else if (COMMAND_SPI_SELECT == command) {
  72. EEPROM_CS_LO();
  73. } else if (COMMAND_SPI_WRITE == command) {
  74. uint32_t len = serial_get16();
  75. while (len--) {
  76. spi_put(serial_get());
  77. }
  78. } else if (COMMAND_SPI_READ == command) {
  79. uint32_t len = serial_get16();
  80. while (len--) {
  81. serial_put(spi_get());
  82. }
  83. } else {
  84. serial_put('?');
  85. }
  86. }
  87. }
  88. static void serial_put(uint8_t c)
  89. {
  90. while (0 == (REG_EFSIF0_STATUS & TDBEx)) {
  91. }
  92. REG_EFSIF0_TXD = c;
  93. }
  94. static uint8_t serial_get(void)
  95. {
  96. while (0 == (REG_EFSIF0_STATUS & RDBFx)) {
  97. }
  98. return REG_EFSIF0_RXD;
  99. }
  100. static uint32_t serial_get16(void)
  101. {
  102. uint32_t value = serial_get();
  103. return value | serial_get() << 8;
  104. }
  105. static void spi_put(uint8_t c)
  106. {
  107. while (0 == (REG_SPI_STAT & TDEF)) {
  108. }
  109. REG_SPI_TXD = c;
  110. }
  111. static uint8_t spi_get(void)
  112. {
  113. spi_put(0x00);
  114. while (0 == (REG_SPI_STAT & RDFF)) {
  115. }
  116. return REG_SPI_RXD;
  117. }