Skip to content

Commit 43ea73f

Browse files
committed
pic16bit: Initial version of port to 16-bit PIC family.
Reference MCU is dsPIC33J256GP506 with 256k ROM and 8k RAM, on the dsPIC DSC Starter Kit board. The REPL works, GC works, pyb module has LED and Switch objects. It passes some tests from the test suite (most it can't run because it doesn't have the Python features enabled).
1 parent 12ab9ed commit 43ea73f

12 files changed

Lines changed: 913 additions & 0 deletions

File tree

pic16bit/Makefile

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
include ../py/mkenv.mk
2+
3+
# qstr definitions (must come before including py.mk)
4+
QSTR_DEFS = qstrdefsport.h
5+
6+
# include py core make definitions
7+
include ../py/py.mk
8+
9+
XC16 = /opt/microchip/xc16/v1.24
10+
CROSS_COMPILE = $(XC16)/bin/xc16-
11+
12+
PARTFAMILY = dsPIC33F
13+
PART = 33FJ256GP506
14+
15+
INC = -I.
16+
INC += -I..
17+
INC += -I../lib/mp-readline
18+
INC += -I../stmhal
19+
INC += -I$(BUILD)
20+
INC += -I$(XC16)/include
21+
INC += -I$(XC16)/support/$(PARTFAMILY)/h
22+
23+
CFLAGS_PIC16BIT = -mcpu=$(PART) -mlarge-code
24+
CFLAGS = $(INC) -Wall -Werror -ansi -std=gnu99 -nostdlib $(CFLAGS_PIC16BIT) $(COPT)
25+
26+
#Debugging/Optimization
27+
ifeq ($(DEBUG), 1)
28+
CFLAGS += -O0 -ggdb
29+
else
30+
CFLAGS += -O1 -DNDEBUG
31+
endif
32+
33+
LDFLAGS = --heap=0 -nostdlib -T $(XC16)/support/$(PARTFAMILY)/gld/p$(PART).gld -Map=$@.map --cref -p$(PART)
34+
LIBS = -L$(XC16)/lib -L$(XC16)/lib/$(PARTFAMILY) -lc -lm -lpic30 -lp$(PART)
35+
36+
SRC_C = \
37+
main.c \
38+
board.c \
39+
pic16bit_mphal.c \
40+
modpyb.c \
41+
modpybled.c \
42+
modpybswitch.c \
43+
stmhal/pybstdio.c \
44+
stmhal/pyexec.c \
45+
lib/mp-readline/readline.c \
46+
47+
SRC_S = \
48+
# gchelper.s \
49+
50+
OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(SRC_S:.s=.o))
51+
52+
all: $(BUILD)/firmware.hex
53+
54+
$(BUILD)/firmware.hex: $(BUILD)/firmware.elf
55+
$(ECHO) "Create $@"
56+
$(Q)$(CROSS_COMPILE)bin2hex $<
57+
58+
$(BUILD)/firmware.elf: $(OBJ)
59+
$(ECHO) "LINK $@"
60+
$(Q)$(LD) $(LDFLAGS) -o $@ $(OBJ) $(LIBS)
61+
$(Q)size $@
62+
63+
$(PY_BUILD)/gc.o: CFLAGS += -O1
64+
$(PY_BUILD)/vm.o: CFLAGS += -O1
65+
66+
include ../py/mkrules.mk

pic16bit/board.c

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* This file is part of the Micro Python 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+
27+
#include <p33Fxxxx.h>
28+
#include "board.h"
29+
30+
/********************************************************************/
31+
// CPU
32+
33+
void cpu_init(void) {
34+
// set oscillator to operate at 40MHz
35+
// Fosc = Fin*M/(N1*N2), Fcy = Fosc/2
36+
// Fosc = 7.37M*40/(2*2) = 80Mhz for 7.37M input clock
37+
PLLFBD = 41; // M=39
38+
CLKDIVbits.PLLPOST = 0; // N1=2
39+
CLKDIVbits.PLLPRE = 0; // N2=2
40+
OSCTUN = 0;
41+
42+
// initiate clock switch to FRC with PLL
43+
__builtin_write_OSCCONH(0x01);
44+
__builtin_write_OSCCONL(0x01);
45+
46+
// wait for clock switch to occur
47+
while (OSCCONbits.COSC != 0x01) {
48+
}
49+
while (!OSCCONbits.LOCK) {
50+
}
51+
}
52+
53+
/********************************************************************/
54+
// LEDs
55+
56+
#define RED_LED_TRIS _TRISC15
57+
#define YELLOW_LED_TRIS _TRISC13
58+
#define GREEN_LED_TRIS _TRISC14
59+
60+
#define RED_LED _LATC15
61+
#define YELLOW_LED _LATC13
62+
#define GREEN_LED _LATC14
63+
64+
#define LED_ON (0)
65+
#define LED_OFF (1)
66+
67+
void led_init(void) {
68+
// set led GPIO as outputs
69+
RED_LED_TRIS = 0;
70+
YELLOW_LED_TRIS = 0;
71+
GREEN_LED_TRIS = 0;
72+
73+
// turn off the LEDs
74+
RED_LED = LED_OFF;
75+
YELLOW_LED = LED_OFF;
76+
GREEN_LED = LED_OFF;
77+
}
78+
79+
void led_state(int led, int state) {
80+
int val = state ? LED_ON : LED_OFF;
81+
switch (led) {
82+
case 1: RED_LED = val; break;
83+
case 2: YELLOW_LED = val; break;
84+
case 3: GREEN_LED = val; break;
85+
}
86+
}
87+
88+
void led_toggle(int led) {
89+
switch (led) {
90+
case 1: RED_LED ^= 1; break;
91+
case 2: YELLOW_LED ^= 1; break;
92+
case 3: GREEN_LED ^= 1; break;
93+
}
94+
}
95+
96+
/********************************************************************/
97+
// switches
98+
99+
#define SWITCH_S1_TRIS _TRISD8
100+
#define SWITCH_S2_TRIS _TRISD9
101+
102+
#define SWITCH_S1 _RD8
103+
#define SWITCH_S2 _RD9
104+
105+
void switch_init(void) {
106+
// set switch GPIO as inputs
107+
SWITCH_S1_TRIS = 1;
108+
SWITCH_S2_TRIS = 1;
109+
}
110+
111+
int switch_get(int sw) {
112+
int val = 1;
113+
switch (sw) {
114+
case 1: val = SWITCH_S1; break;
115+
case 2: val = SWITCH_S2; break;
116+
}
117+
return val == 0;
118+
}
119+
120+
/********************************************************************/
121+
// UART
122+
123+
/*
124+
// TODO need an irq
125+
void uart_rx_irq(void) {
126+
if (c == interrupt_char) {
127+
MP_STATE_VM(mp_pending_exception) = MP_STATE_PORT(keyboard_interrupt_obj);
128+
}
129+
}
130+
*/
131+
132+
void uart_init(void) {
133+
// baudrate = F_CY / 16 (uxbrg + 1)
134+
// F_CY = 40MHz for us
135+
UART1.uxbrg = 64; // 38400 baud
136+
UART1.uxmode = 1 << 15; // UARTEN
137+
UART1.uxsta = 1 << 10; // UTXEN
138+
}
139+
140+
int uart_rx_any(void) {
141+
return UART1.uxsta & 1; // URXDA
142+
}
143+
144+
int uart_rx_char(void) {
145+
return UART1.uxrxreg;
146+
}
147+
148+
void uart_tx_char(int chr) {
149+
while (UART1.uxsta & (1 << 9)) {
150+
// tx fifo is full
151+
}
152+
UART1.uxtxreg = chr;
153+
}

pic16bit/board.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* This file is part of the Micro Python 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+
#ifndef __MICROPY_INCLUDED_PIC16BIT_BOARD_H__
27+
#define __MICROPY_INCLUDED_PIC16BIT_BOARD_H__
28+
29+
void cpu_init(void);
30+
31+
void led_init(void);
32+
void led_state(int led, int state);
33+
void led_toggle(int led);
34+
35+
void switch_init(void);
36+
int switch_get(int sw);
37+
38+
void uart_init(void);
39+
int uart_rx_any(void);
40+
int uart_rx_char(void);
41+
void uart_tx_char(int chr);
42+
43+
#endif // __MICROPY_INCLUDED_PIC16BIT_BOARD_H__

pic16bit/main.c

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* This file is part of the Micro Python 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+
27+
#include <stdint.h>
28+
#include <stdio.h>
29+
#include <string.h>
30+
#include <p33Fxxxx.h>
31+
32+
#include "py/compile.h"
33+
#include "py/runtime.h"
34+
#include "py/gc.h"
35+
#include "pyexec.h"
36+
#include "readline.h"
37+
#include MICROPY_HAL_H
38+
#include "board.h"
39+
#include "modpyb.h"
40+
41+
_FGS(GWRP_OFF & GCP_OFF);
42+
_FOSCSEL(FNOSC_FRC);
43+
_FOSC(FCKSM_CSECMD & OSCIOFNC_ON & POSCMD_NONE);
44+
_FWDT(FWDTEN_OFF);
45+
46+
// maximum heap for device with 8k RAM
47+
static char heap[4600];
48+
49+
int main(int argc, char **argv) {
50+
// init the CPU and the peripherals
51+
cpu_init();
52+
led_init();
53+
switch_init();
54+
uart_init();
55+
56+
soft_reset:
57+
58+
// flash green led for 150ms to indicate boot
59+
led_state(1, 0);
60+
led_state(2, 0);
61+
led_state(3, 1);
62+
mp_hal_milli_delay(150);
63+
led_state(3, 0);
64+
65+
// init MicroPython runtime
66+
int stack_dummy;
67+
MP_STATE_VM(stack_top) = (char*)&stack_dummy;
68+
gc_init(heap, heap + sizeof(heap));
69+
mp_init();
70+
mp_hal_init();
71+
readline_init0();
72+
73+
// REPL loop
74+
for (;;) {
75+
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
76+
if (pyexec_raw_repl() != 0) {
77+
break;
78+
}
79+
} else {
80+
if (pyexec_friendly_repl() != 0) {
81+
break;
82+
}
83+
}
84+
}
85+
86+
printf("PYB: soft reboot\n");
87+
mp_deinit();
88+
goto soft_reset;
89+
}
90+
91+
void gc_collect(void) {
92+
// TODO possibly need to trace registers
93+
void *dummy;
94+
gc_collect_start();
95+
// Node: stack is ascending
96+
gc_collect_root(&dummy, ((mp_uint_t)&dummy - (mp_uint_t)MP_STATE_VM(stack_top)) / sizeof(mp_uint_t));
97+
gc_collect_end();
98+
}
99+
100+
mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
101+
return NULL;
102+
}
103+
104+
mp_import_stat_t mp_import_stat(const char *path) {
105+
return MP_IMPORT_STAT_NO_EXIST;
106+
}
107+
108+
mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
109+
return mp_const_none;
110+
}
111+
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
112+
113+
void nlr_jump_fail(void *val) {
114+
}
115+
116+
void NORETURN __fatal_error(const char *msg) {
117+
while (1);
118+
}
119+
120+
#ifndef NDEBUG
121+
void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) {
122+
printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line);
123+
__fatal_error("Assertion failed");
124+
}
125+
#endif

0 commit comments

Comments
 (0)