Skip to content

Commit 976405d

Browse files
authored
Merge pull request hathach#1270 from kasjer/kasjer/pic32mz
Driver for Microchip PIC32MZ family
2 parents 5dab0e8 + c722133 commit 976405d

File tree

10 files changed

+2101
-0
lines changed

10 files changed

+2101
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
JLINK_DEVICE=PIC32MZ2048EFH064
2+
JLINK_IF=ICSP
3+
4+
CFLAGS += \
5+
-mprocessor=32MZ2048EFH064 \
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2022 Jerzy Kasenberg
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
* This file is part of the TinyUSB stack.
25+
*/
26+
27+
#include <stdint.h>
28+
#include <stdbool.h>
29+
#include <xc.h>
30+
#include "tusb.h"
31+
32+
/* JTAG on, WDT off */
33+
#pragma config FDMTEN=0, FSOSCEN=0, DMTCNT=1
34+
#pragma config DEBUG=ON
35+
#pragma config JTAGEN=ON
36+
#pragma config FSLEEP=OFF
37+
#pragma config TRCEN=OFF
38+
#pragma config ICESEL=ICS_PGx2
39+
40+
#pragma config POSCMOD = EC
41+
#pragma config FNOSC = SPLL
42+
/* 24MHz posc input to pll, div by 3, multiply by 50, div by 2 -> 200mhz*/
43+
#pragma config FPLLICLK=0, FPLLIDIV=DIV_3, FPLLRNG=RANGE_5_10_MHZ, FPLLMULT=MUL_50, FPLLODIV=DIV_2
44+
#pragma config FUSBIDIO=1
45+
#pragma config WINDIS=NORMAL
46+
#pragma config WDTSPGM=1
47+
#pragma config WDTPS=15
48+
#pragma config FWDTEN=OFF
49+
50+
void button_init(void)
51+
{
52+
// RB12 - button
53+
// ANSELB B12 not analog
54+
ANSELBCLR = TU_BIT(12);
55+
// TRISB B12 input
56+
TRISBSET = TU_BIT(12);
57+
// Pull-up
58+
CNPUBSET = TU_BIT(12);
59+
}
60+
61+
void led_init(void)
62+
{
63+
// RB8 - LED
64+
// ANASELB RB8 not analog
65+
ANSELBCLR = TU_BIT(8);
66+
// TRISH RH2 input
67+
TRISBCLR = TU_BIT(8);
68+
// Initial value 0, LED off
69+
LATBCLR = TU_BIT(8);
70+
}
71+
72+
void uart_init(void)
73+
{
74+
// RD4/RD0 Uart4 TX/RX
75+
// ANSELD - not present on 64 pin device
76+
77+
/* Unlock system for PPS configuration */
78+
SYSKEY = 0x00000000;
79+
SYSKEY = 0xAA996655;
80+
SYSKEY = 0x556699AA;
81+
CFGCONbits.IOLOCK = 0;
82+
83+
// PPS Input Remapping
84+
// U4RX -> RD0
85+
U4RXR = 3;
86+
87+
// PPS Output Remapping
88+
// RD4 -> U4TX
89+
RPD4R = 2;
90+
91+
// Lock back the system after PPS configuration
92+
CFGCONbits.IOLOCK = 1;
93+
SYSKEY = 0x00000000;
94+
95+
// UART4
96+
// High speed mode
97+
// 8 bits, no parity, no RTS/CTS, no flow control
98+
U4MODE = 0x0;
99+
100+
// Enable UART2 Receiver and Transmitter
101+
U4STASET = (_U4STA_UTXEN_MASK | _U4STA_URXEN_MASK | _U4STA_UTXISEL1_MASK);
102+
103+
// BAUD Rate register Setup
104+
U4BRG = 100000000 / (16 * 115200) + 1;
105+
106+
// Disable Interrupts
107+
IEC4CLR = _IEC5_U4EIE_MASK | _IEC5_U4RXIE_MASK | _IEC5_U4TXIE_MASK;
108+
109+
// Turn ON UART2
110+
U4MODESET = _U4MODE_ON_MASK;
111+
}
112+
113+
//--------------------------------------------------------------------+
114+
// Board porting API
115+
//--------------------------------------------------------------------+
116+
117+
void board_led_write(bool state)
118+
{
119+
if (state)
120+
{
121+
LATBSET = TU_BIT(8);
122+
}
123+
else
124+
{
125+
LATBCLR = TU_BIT(8);
126+
}
127+
}
128+
129+
uint32_t board_button_read(void)
130+
{
131+
return (PORTB >> 12) & 1;
132+
}
133+
134+
int board_uart_write(void const * buf, int len)
135+
{
136+
int i = len;
137+
uint8_t const * data = buf;
138+
while (i--)
139+
{
140+
while (U4STAbits.UTXBF) ;
141+
U4TXREG = *data++;
142+
}
143+
return len;
144+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
JLINK_DEVICE=PIC32MZ2048EFM144
2+
JLINK_IF=ICSP
3+
4+
CFLAGS += \
5+
-mprocessor=32MZ2048EFM144 \
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2022 Jerzy Kasenberg
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
* This file is part of the TinyUSB stack.
25+
*/
26+
27+
#include <stdint.h>
28+
#include <stdbool.h>
29+
#include <xc.h>
30+
#include "tusb.h"
31+
32+
/* JTAG on, WDT off */
33+
#pragma config FDMTEN=0, FSOSCEN=0, DMTCNT=1
34+
#pragma config DEBUG=ON
35+
#pragma config JTAGEN=ON
36+
#pragma config FSLEEP=OFF
37+
#pragma config TRCEN=OFF
38+
#pragma config ICESEL=ICS_PGx2
39+
40+
#pragma config POSCMOD = HS
41+
#pragma config FNOSC = SPLL
42+
/* 24MHz posc input to pll, div by 3, multiply by 50, div by 2 -> 200mhz*/
43+
#pragma config FPLLICLK=0, FPLLIDIV=DIV_3, FPLLRNG=RANGE_5_10_MHZ, FPLLMULT=MUL_50, FPLLODIV=DIV_2
44+
#pragma config FUSBIDIO=1
45+
#pragma config WINDIS=NORMAL
46+
#pragma config WDTSPGM=1
47+
#pragma config WDTPS=15
48+
#pragma config FWDTEN=OFF
49+
50+
void button_init(void)
51+
{
52+
// RB12 - button
53+
// ANSELB B12 not analog
54+
ANSELBCLR = TU_BIT(12);
55+
// TRISB B12 input
56+
TRISBSET = TU_BIT(12);
57+
}
58+
59+
void led_init(void)
60+
{
61+
// RH2 - LED
62+
// ANASELH no analog function on RH2
63+
// TRISH RH2 input
64+
TRISHCLR = TU_BIT(2);
65+
// Initial value 0, LED off
66+
LATHCLR = TU_BIT(2);
67+
}
68+
69+
void uart_init(void)
70+
{
71+
// RE8/RE9 Uart2 TX/RX
72+
// ANSELE - TX/RX not analog
73+
ANSELECLR = TU_BIT(8) | TU_BIT(9);
74+
75+
/* Unlock system for PPS configuration */
76+
SYSKEY = 0x00000000;
77+
SYSKEY = 0xAA996655;
78+
SYSKEY = 0x556699AA;
79+
CFGCONbits.IOLOCK = 0;
80+
81+
// PPS Input Remapping
82+
// U2RX -> RE9
83+
U2RXR = 13;
84+
85+
// PPS Output Remapping
86+
// RE8 -> U2TX
87+
RPE8R = 2;
88+
89+
// Lock back the system after PPS configuration
90+
CFGCONbits.IOLOCK = 1;
91+
SYSKEY = 0x00000000;
92+
93+
// UART2
94+
// High speed mode
95+
// 8 bits, no parity, no RTS/CTS, no flow control
96+
U2MODE = 0x0;
97+
98+
// Enable UART2 Receiver and Transmitter
99+
U2STASET = (_U2STA_UTXEN_MASK | _U2STA_URXEN_MASK | _U2STA_UTXISEL1_MASK);
100+
101+
// BAUD Rate register Setup
102+
U2BRG = 100000000 / (16 * 115200) + 1;
103+
104+
// Disable Interrupts
105+
IEC4CLR = _IEC4_U2EIE_MASK | _IEC4_U2RXIE_MASK | _IEC4_U2TXIE_MASK;
106+
107+
// Turn ON UART2
108+
U2MODESET = _U2MODE_ON_MASK;
109+
}
110+
111+
//--------------------------------------------------------------------+
112+
// Board porting API
113+
//--------------------------------------------------------------------+
114+
115+
void board_led_write(bool state)
116+
{
117+
if (state)
118+
{
119+
LATHSET = TU_BIT(2);
120+
}
121+
else
122+
{
123+
LATHCLR = TU_BIT(2);
124+
}
125+
}
126+
127+
uint32_t board_button_read(void)
128+
{
129+
return (PORTB >> 12) & 1;
130+
}
131+
132+
int board_uart_write(void const * buf, int len)
133+
{
134+
int i = len;
135+
uint8_t const * data = buf;
136+
while (i--)
137+
{
138+
while (U2STAbits.UTXBF) ;
139+
U2TXREG = *data++;
140+
}
141+
return len;
142+
}

0 commit comments

Comments
 (0)