Gowin Vol.3 第3部第3章 リスト2

from polyphony import testbench, module, is_worker_running
from polyphony.timing import clksleep, wait_value, clkfence
from polyphony.io import Port
from polyphony.typing import bit, bit16

from data import ov2640_data

RGB565_MODE = 0x08
RAW10_MODE = 0x04

@module
class ov2640:
def __init__(self):
self.resend = Port(bit, ‘in’)
self.advance = Port(bit, ‘in’)

self.command = Port(bit16, ‘out’)
self.finished = Port(bit, ‘out’, 0)

self.append_worker(self.main)

def send_all_data(self):
for data in ov2640_data:
self.command.wr(data)
wait_value(1, self.advance)
wait_value(0, self.advance)
#clksleep(40)

def send_mode(self, mode_data):
self.command.wr(0xDA00 | mode_data)
wait_value(1, self.advance)
wait_value(0, self.advance)

def send_last_data(self):
self.command.wr(0xFFFF)
wait_value(1, self.advance)
wait_value(0, self.advance)

def main(self):
while is_worker_running():

self.finished.wr(0)
self.send_all_data()
self.send_mode(RGB565_MODE)
self.send_last_data()
self.finished.wr(1)

wait_value(1, self.resend)

m = ov2640()

@testbench
def test(ov2640_m):
pass

test(m)