aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/lib/tinyusb/hw/bsp/pic32mz/boards/olimex_emz64/olimex_emz64.c
blob: e47ec5f31be11e5d4141667f42111b334c857733 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* 
 * The MIT License (MIT)
 *
 * Copyright (c) 2022 Jerzy Kasenberg
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * This file is part of the TinyUSB stack.
 */

#include <stdint.h>
#include <stdbool.h>
#include <xc.h>
#include "tusb.h"

/* JTAG on, WDT off */
#pragma config FDMTEN=0, FSOSCEN=0, DMTCNT=1
#pragma config DEBUG=ON
#pragma config JTAGEN=ON
#pragma config FSLEEP=OFF
#pragma config TRCEN=OFF
#pragma config ICESEL=ICS_PGx2

#pragma config POSCMOD = EC
#pragma config FNOSC = SPLL
/* 24MHz posc input to pll, div by 3, multiply by 50, div by 2 -> 200mhz*/
#pragma config FPLLICLK=0, FPLLIDIV=DIV_3, FPLLRNG=RANGE_5_10_MHZ, FPLLMULT=MUL_50, FPLLODIV=DIV_2
#pragma config FUSBIDIO=1
#pragma config WINDIS=NORMAL
#pragma config WDTSPGM=1
#pragma config WDTPS=15
#pragma config FWDTEN=OFF

void button_init(void)
{
  // RB12 - button
  // ANSELB B12 not analog
  ANSELBCLR = TU_BIT(12);
  // TRISB B12 input
  TRISBSET = TU_BIT(12);
  // Pull-up
  CNPUBSET = TU_BIT(12);
}

void led_init(void)
{
  // RB8 - LED
  // ANASELB RB8 not analog
  ANSELBCLR = TU_BIT(8);
  // TRISH RH2 output
  TRISBCLR = TU_BIT(8);
  // Initial value 0, LED off
  LATBCLR = TU_BIT(8);
}

void uart_init(void)
{
  // RD4/RD0 Uart4 TX/RX
  // ANSELD - not present on 64 pin device

  /* Unlock system for PPS configuration */
  SYSKEY = 0x00000000;
  SYSKEY = 0xAA996655;
  SYSKEY = 0x556699AA;
  CFGCONbits.IOLOCK = 0;

  // PPS Input Remapping
  // U4RX -> RD0
  U4RXR = 3;

  // PPS Output Remapping
  // RD4 -> U4TX
  RPD4R = 2;

  // Lock back the system after PPS configuration
  CFGCONbits.IOLOCK = 1;
  SYSKEY = 0x00000000;

  // UART4
  // High speed mode
  // 8 bits, no parity, no RTS/CTS, no flow control
  U4MODE = 0x0;

  // Enable UART2 Receiver and Transmitter
  U4STASET = (_U4STA_UTXEN_MASK | _U4STA_URXEN_MASK | _U4STA_UTXISEL1_MASK);

  // BAUD Rate register Setup
  U4BRG = 100000000 / (16 * 115200) + 1;

  // Disable Interrupts
  IEC4CLR = _IEC5_U4EIE_MASK | _IEC5_U4RXIE_MASK | _IEC5_U4TXIE_MASK;

  // Turn ON UART2
  U4MODESET = _U4MODE_ON_MASK;
}

//--------------------------------------------------------------------+
// Board porting API
//--------------------------------------------------------------------+

void board_led_write(bool state)
{
  if (state)
  {
    LATBSET = TU_BIT(8);
  }
  else
  {
    LATBCLR = TU_BIT(8);
  }
}

uint32_t board_button_read(void)
{
  return ((PORTB >> 12) & 1) == 0;
}

int board_uart_write(void const * buf, int len)
{
  int i = len;
  uint8_t const * data = buf;
  while (i--)
  {
    while (U4STAbits.UTXBF) ;
    U4TXREG = *data++;
  }
  return len;
}