Skip to content

Commit

Permalink
Merge pull request #19 from boochow/rpi
Browse files Browse the repository at this point in the history
from rpi branch
  • Loading branch information
boochow authored Nov 23, 2018
2 parents cc1b22e + 5b2cbc9 commit b1c11c9
Show file tree
Hide file tree
Showing 30 changed files with 1,269 additions and 92 deletions.
2 changes: 1 addition & 1 deletion micropython
Submodule micropython updated 297 files
13 changes: 13 additions & 0 deletions raspberrypi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ CROSS_COMPILE = arm-none-eabi-

OBJCPY=$(CROSS_COMPILE)objcopy

ZIP = /usr/bin/zip
RELEASE=`/bin/date +%Y%m%d`-rpi
RELFILES=fs/

INC += -I.
INC += -I$(TOP)
Expand Down Expand Up @@ -71,6 +74,10 @@ SRC_C = \
machine_spi.c \
vc_property.c \
gpu_vc_property.c \
bcm283x_clockmgr.c \
pwm.c \
machine_pwm.c \
machine_clockmgr.c \

SRC_LIB = $(addprefix lib/,\
utils/stdout_helpers.c \
Expand Down Expand Up @@ -114,6 +121,7 @@ SRC_S = \

# USB
ifneq ($(MICROPY_HW_USBHOST),0)
RELEASE=$(RELEASE)-usb
USBHOST_DIR=csud
CSUDLIB=libcsud.a
INC += -I$(USBHOST_DIR)/include
Expand Down Expand Up @@ -181,6 +189,11 @@ $(BUILD)/config.txt: config-template.txt
$(Q)$(CP) $< $@
$(Q)$(ECHO) "kernel=firmware.img" >> $@

release: $(BUILD)/firmware.img $(BUILD)/config.txt
$(ECHO) "GEN $@"
$(Q)$(ZIP) $(BUILD)/$(RELEASE) -j $^
$(Q)cd $(RELFILES); $(ZIP) ../$(BUILD)/$(RELEASE) -r .

$(BUILD)/firmware-qemu.img: $(OBJ) $(USBLIB)
$(ECHO) "LINK $@"
$(Q)$(LD) --defsym=_load_addr=0x10000 $(LDFLAGS) -o $@.elf $^ $(LIBS)
Expand Down
23 changes: 23 additions & 0 deletions raspberrypi/bcm283x_clockmgr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdint.h>
#include "bcm283x_clockmgr.h"

void clockmgr_pause(clockmgr_t *clk) {
if (clk->CTL & CM_CTL_ENAB) {
clockmgr_set_ctl(clk, (clk->CTL & ~CM_CTL_ENAB));
while(clk->CTL & CM_CTL_BUSY) {};
}
}

void clockmgr_config_ctl(clockmgr_t *clk, int32_t flags) {
clockmgr_pause(clk);
clk->CTL = CM_PASSWORD | flags;
}

void clockmgr_config_div(clockmgr_t *clk, int32_t divi, int32_t divf) {
uint32_t save = clk->CTL;
clockmgr_pause(clk);
clockmgr_set_div(clk, divi, divf);
if (save & CM_CTL_ENAB) {
clockmgr_set_ctl(clk, save);
}
}
53 changes: 53 additions & 0 deletions raspberrypi/bcm283x_clockmgr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#ifndef MICROPY_INCLUDED_RPI_BCM283X_CLOCKMGR_H
#define MICROPY_INCLUDED_RPI_BCM283X_CLOCKMGR_H

#define CM_GP0 (IO_BASE + 0x101070)
#define CM_GP1 (IO_BASE + 0x101078)
#define CM_GP2 (IO_BASE + 0x101080)
#define CM_PCM (IO_BASE + 0x101098)
#define CM_PWM (IO_BASE + 0x1010a0)

typedef volatile struct _clockmgr_t {
uint32_t CTL;
uint32_t DIV;
} clockmgr_t;


#define CM_PASSWORD (0x5a000000)

#define CM_CTL_MASH_MASK (3U<<9)
#define CM_CTL_MASH_IDIV (0U<<9)
#define CM_CTL_MASH_1STG (1U<<9)
#define CM_CTL_MASH_2STG (2U<<9)
#define CM_CTL_MASH_3STG (3U<<9)

#define CM_CTL_FLIP (1U<<8)
#define CM_CTL_BUSY (1U<<7)
#define CM_CTL_KILL (1U<<5)
#define CM_CTL_ENAB (1U<<4)

#define CM_CTL_SRC_MASK (0xfU)
#define CM_CTL_SRC_GND (0U)
#define CM_CTL_SRC_OSC (1U)
#define CM_CTL_SRC_PLLA (4U)
#define CM_CTL_SRC_PLLC (5U)
#define CM_CTL_SRC_PLLD (6U)
#define CM_CTL_SRC_HDMI (7U)

#define CM_DIV_MASK (0x00ffffffU)

__attribute__(( always_inline )) inline void clockmgr_set_ctl(clockmgr_t *clk, uint32_t ctl) {
clk->CTL = CM_PASSWORD | (ctl & 0xffffff);
}

__attribute__(( always_inline )) inline void clockmgr_set_div(clockmgr_t *clk, uint32_t divi, uint32_t divf) {
clk->DIV = CM_PASSWORD | (divi & 0xfff) << 12 | (divf & 0xfff);
}

void clockmgr_pause(clockmgr_t *clk);

void clockmgr_config_ctl(clockmgr_t *clk, int32_t flags);

void clockmgr_config_div(clockmgr_t *clk, int32_t divi, int32_t divf);

#endif // MICROPY_INCLUDED_RPI_BCM283X_CLOCKMGR_H
55 changes: 55 additions & 0 deletions raspberrypi/bcm283x_pwm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef MICROPY_INCLUDED_RPI_BCM283X_PWM_H
#define MICROPY_INCLUDED_RPI_BCM283X_PWM_H

#include "bcm283x.h"

#define PWM (IO_BASE + 0x20C000)

typedef volatile struct _pwm_t {
uint32_t CTL;
uint32_t STA;
uint32_t DMAC;
uint32_t undef1;
uint32_t RNG1;
uint32_t DAT1;
uint32_t FIF1;
uint32_t undef2;
uint32_t RNG2;
uint32_t DAT2;
} pwm_t;

#define CTL_MSEN2 (1<<15)
#define CTL_USEF2 (1<<13)
#define CTL_POLA2 (1<<12)
#define CTL_SBIT2 (1<<11)
#define CTL_RPTL2 (1<<10)
#define CTL_MODE2 (1<<9)
#define CTL_PWEN2 (1<<8)
#define CTL_MSEN1 (1<<7)
#define CTL_CLRF1 (1<<6)
#define CTL_USEF1 (1<<5)
#define CTL_POLA1 (1<<4)
#define CTL_SBIT1 (1<<3)
#define CTL_RPTL1 (1<<2)
#define CTL_MODE1 (1<<1)
#define CTL_PWEN1 (1)

#define STA_STA4 (1<<12)
#define STA_STA3 (1<<11)
#define STA_STA2 (1<<10)
#define STA_STA1 (1<<9)
#define STA_BERR (1<<8)
#define STA_GAPO4 (1<<7)
#define STA_GAPO3 (1<<6)
#define STA_GAPO2 (1<<5)
#define STA_GAPO1 (1<<4)
#define STA_RERR1 (1<<3)
#define STA_WERR1 (1<<2)
#define STA_EMPT1 (1<<1)
#define STA_FULL1 (1)

#define DMAC_ENAB (1<<31)
#define DMAC_PANIC (255<<8)
#define DMAC_DREQ (255)

#endif // MICROPY_INCLUDED_RPI_BCM283X_PWM_H
5 changes: 5 additions & 0 deletions raspberrypi/fs/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
copy these files to the root of your SD card.

uncomment lines in main.py to use HDMI display as console screen.

more info: https://github.com/boochow/micropython-raspberrypi
1 change: 1 addition & 0 deletions raspberrypi/fs/boot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# exec(open("./lib/ramdisk.py").read())
Original file line number Diff line number Diff line change
@@ -1,43 +1,33 @@
import gpu
import framebuf
import uio

class RPiScreen(framebuf.FrameBuffer):
def __init__(self, width, height):
self.width = width
self.height = height
gpu.fb_init(width,height,screen_w=1920,screen_h=1080)
super().__init__(gpu.fb_data(),width,height,framebuf.RGB565)
self

def show(self):
pass

class FBConsole(uio.IOBase):
def __init__(self, framebuf, bgcolor=0, fgcolor=-1, width=-1, height=-1, readobj=None):
def __init__(self, fb, bgcolor=0, fgcolor=-1, width=-1, height=-1, readobj=None):
self.readobj = readobj
self.fb = framebuf
self.fb = fb
if width > 0:
self.width=width
else:
try:
self.width=framebuf.width
self.width=fb.width
except:
raise ValueError
if height > 0:
self.height=height
else:
try:
self.height=framebuf.height
self.height=fb.height
except:
raise ValueError
self.bgcolor = bgcolor
self.fgcolor = fgcolor
self.line_height(8)
self.cls()

def cls(self):
self.x = 0
self.y = 0
self.y_end = 0
self.fb.fill(self.bgcolor)
try:
self.fb.show()
Expand All @@ -48,57 +38,69 @@ def line_height(self, px):
self.lineheight = px
self.w = self.width // px
self.h = self.height // px
self.cls()
self._draw_cursor()

def _putc(self, c):
c = chr(c)
if c == '\n':
self.x = 0
self._newline()
elif c == '\x08':
self._backspace()
elif c >= ' ':
self.fb.fill_rect(self.x * 8, self.y * self.lineheight, 8, self.lineheight, self.bgcolor)
self.fb.text(c, self.x * 8, self.y * self.lineheight, self.fgcolor)
self.x += 1
if self.x >= self.w:
self._newline()
self.x = 0

def _esq_read_num(self, buf, pos):
digit = 1
n = 0
while buf[pos] != 0x5b:
n += digit * (buf[pos] - 0x30)
pos -= 1
digit *= 10
return n

def write(self, buf):
self._erase_current()
self._draw_cursor(self.bgcolor)
i = 0
while i < len(buf):
c = buf[i]
if c == 0x1b: # skip escape sequence
if c == 0x1b:
i += 1
esc = i
while chr(buf[i]) in '[;0123456789':
i += 1
c = buf[i]
if c == 0x4b and i == esc + 1: # ESC [ K
self._clear_cursor_eol()
elif c == 0x44: # ESC [ n D
for _ in range(self._esq_read_num(buf, i - 1)):
self._backspace()
else:
self._putc(c)
i += 1
self._draw_cursor()
self._draw_cursor(self.fgcolor)
try:
self.fb.show()
except:
pass
return len(buf)

def readinto(self, buf, nbytes=0):
if readobj != None:
return readobj.readinto(buf, nbytes)
if self.readobj != None:
return self.readobj.readinto(buf, nbytes)
else:
return None

def _newline(self):
self.x = 0
self.y += 1
if self.y >= self.h:
self.fb.scroll(0, -8)
self.fb.fill_rect(0, self.height - self.lineheight, self.width, self.lineheight, self.bgcolor)
self.y = self.h - 1

def _erase_current(self):
self.fb.fill_rect(self.x * 8, self.y * self.lineheight, 8, self.lineheight, self.bgcolor)
self.y_end = self.y

def _backspace(self):
if self.x == 0:
Expand All @@ -107,7 +109,12 @@ def _backspace(self):
self.x = self.w - 1
else:
self.x -= 1
self.fb.fill_rect(self.x * 8, self.y * self.lineheight, 8, self.lineheight, self.bgcolor)

def _draw_cursor(self):
self.fb.hline(self.x * 8, self.y * self.lineheight + 7, 8, self.fgcolor)
def _clear_cursor_eol(self):
self.fb.fill_rect(self.x * 8, self.y * self.lineheight, self.width, self.lineheight, self.bgcolor)
for l in range(self.y + 1, self.y_end + 1):
self.fb.fill_rect(0, l * self.lineheight, self.width, self.lineheight, self.bgcolor)
self.y_end = self.y

def _draw_cursor(self, color):
self.fb.hline(self.x * 8, self.y * self.lineheight + 7, 8, color)
35 changes: 8 additions & 27 deletions raspberrypi/modules/_boot.py → raspberrypi/fs/lib/ramdisk.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,24 @@ def ioctl(self, op, arg):
if op == 5: # get block size
return self.block_size

def _mount_initrd(num_blocks):
def mount_initrd(num_blocks, path):
try:
import uos
bdev = RAMBlockDev(512, num_blocks)
uos.VfsFat.mkfs(bdev)
vfs = uos.VfsFat(bdev)
uos.mount(vfs, '/')
uos.mount(vfs, path)
except:
print("error: _mount_initrd")
print("error: mount_initrd")

def _create_ramdisk():
_mount_initrd(2048)
f=open('RAM disk.txt', 'w')
def create_ramdisk():
mount_initrd(2048, '/tmp')
f=open('/tmp/RAM disk.txt', 'w')
f.write('''
Size = 512 bytes * 2048 blocks
Format = FAT16
''')
f.close()

def set_fb_console(on=True):
global theScreen
from FBConsole import FBConsole, RPiScreen
import os
if on:
theScreen = FBConsole(RPiScreen(480,270))
os.dupterm(theScreen)
else:
theScreen = os.dupterm(None)

def _boot_main():
_create_ramdisk()
import machine
try:
if (machine.usb_mode() == 'host'):
set_fb_console()
except:
pass

print("_boot.py")
_boot_main()
if __name__ == '__main__':
create_ramdisk()
13 changes: 13 additions & 0 deletions raspberrypi/fs/lib/rpi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import gpu
import framebuf

class RPiScreen(framebuf.FrameBuffer):
def __init__(self, width, height):
self.width = width
self.height = height
gpu.fb_init(width,height,screen_w=1920,screen_h=1080)
super().__init__(gpu.fb_data(),width,height,framebuf.RGB565)
self

def show(self):
pass
Loading

0 comments on commit b1c11c9

Please sign in to comment.