board.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2015 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <p33Fxxxx.h>
  27. #include "board.h"
  28. /********************************************************************/
  29. // CPU
  30. void cpu_init(void) {
  31. // set oscillator to operate at 40MHz
  32. // Fosc = Fin*M/(N1*N2), Fcy = Fosc/2
  33. // Fosc = 7.37M*40/(2*2) = 80Mhz for 7.37M input clock
  34. PLLFBD = 41; // M=39
  35. CLKDIVbits.PLLPOST = 0; // N1=2
  36. CLKDIVbits.PLLPRE = 0; // N2=2
  37. OSCTUN = 0;
  38. // initiate clock switch to FRC with PLL
  39. __builtin_write_OSCCONH(0x01);
  40. __builtin_write_OSCCONL(0x01);
  41. // wait for clock switch to occur
  42. while (OSCCONbits.COSC != 0x01) {
  43. }
  44. while (!OSCCONbits.LOCK) {
  45. }
  46. }
  47. /********************************************************************/
  48. // LEDs
  49. #define RED_LED_TRIS _TRISC15
  50. #define YELLOW_LED_TRIS _TRISC13
  51. #define GREEN_LED_TRIS _TRISC14
  52. #define RED_LED _LATC15
  53. #define YELLOW_LED _LATC13
  54. #define GREEN_LED _LATC14
  55. #define LED_ON (0)
  56. #define LED_OFF (1)
  57. void led_init(void) {
  58. // set led GPIO as outputs
  59. RED_LED_TRIS = 0;
  60. YELLOW_LED_TRIS = 0;
  61. GREEN_LED_TRIS = 0;
  62. // turn off the LEDs
  63. RED_LED = LED_OFF;
  64. YELLOW_LED = LED_OFF;
  65. GREEN_LED = LED_OFF;
  66. }
  67. void led_state(int led, int state) {
  68. int val = state ? LED_ON : LED_OFF;
  69. switch (led) {
  70. case 1: RED_LED = val; break;
  71. case 2: YELLOW_LED = val; break;
  72. case 3: GREEN_LED = val; break;
  73. }
  74. }
  75. void led_toggle(int led) {
  76. switch (led) {
  77. case 1: RED_LED ^= 1; break;
  78. case 2: YELLOW_LED ^= 1; break;
  79. case 3: GREEN_LED ^= 1; break;
  80. }
  81. }
  82. /********************************************************************/
  83. // switches
  84. #define SWITCH_S1_TRIS _TRISD8
  85. #define SWITCH_S2_TRIS _TRISD9
  86. #define SWITCH_S1 _RD8
  87. #define SWITCH_S2 _RD9
  88. void switch_init(void) {
  89. // set switch GPIO as inputs
  90. SWITCH_S1_TRIS = 1;
  91. SWITCH_S2_TRIS = 1;
  92. }
  93. int switch_get(int sw) {
  94. int val = 1;
  95. switch (sw) {
  96. case 1: val = SWITCH_S1; break;
  97. case 2: val = SWITCH_S2; break;
  98. }
  99. return val == 0;
  100. }
  101. /********************************************************************/
  102. // UART
  103. /*
  104. // TODO need an irq
  105. void uart_rx_irq(void) {
  106. if (c == interrupt_char) {
  107. MP_STATE_VM(mp_pending_exception) = MP_STATE_PORT(keyboard_interrupt_obj);
  108. }
  109. }
  110. */
  111. void uart_init(void) {
  112. // baudrate = F_CY / 16 (uxbrg + 1)
  113. // F_CY = 40MHz for us
  114. UART1.uxbrg = 64; // 38400 baud
  115. UART1.uxmode = 1 << 15; // UARTEN
  116. UART1.uxsta = 1 << 10; // UTXEN
  117. }
  118. int uart_rx_any(void) {
  119. return UART1.uxsta & 1; // URXDA
  120. }
  121. int uart_rx_char(void) {
  122. return UART1.uxrxreg;
  123. }
  124. void uart_tx_char(int chr) {
  125. while (UART1.uxsta & (1 << 9)) {
  126. // tx fifo is full
  127. }
  128. UART1.uxtxreg = chr;
  129. }