Skip to content

Commit

Permalink
Switch crcmod to crc (#223)
Browse files Browse the repository at this point in the history
* Switch crcmod to crc

Fixed #217 and hopefully makes peoply trying to to bundle/install
Apicual in weird ways a little happier for having to compile less C.

* the stupid thing isn't thread safe

* python scoping rules are dumb
  • Loading branch information
pepijndevos authored Jan 20, 2024
1 parent 96edc2b commit 02520a0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/chipdb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ jobs:
sudo make install
cd ../nextpnr
git checkout ${{ matrix.nextpnr }}
cmake . -DBUILD_PYTHON=OFF -DARCH="gowin;himbaechel" -DHIMBAECHEL_GOWIN_DEVICES="GW1N-1;GW1NZ-1;GW1N-4;GW1N-9;GW1N-9C;GW1NS-4;GW2A-18;GW2A-18C" -DPython3_EXECUTABLE=${{ steps.pysetup.outputs.python-path }}
cmake . -DBUILD_PYTHON=OFF -DARCH="gowin;himbaechel" -DHIMBAECHEL_GOWIN_DEVICES="all" -DPython3_EXECUTABLE=${{ steps.pysetup.outputs.python-path }}
make -j$(nproc)
sudo make install
cd ../examples
Expand Down
10 changes: 1 addition & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,7 @@ RUN curl -so gowin.tgz "http://cdn.gowinsemi.com.cn/Gowin_V1.9.8_linux.tar.gz" &
tar -xf gowin.tgz && \
rm gowin.tgz

RUN mkdir -p "/root/Documents/gowinsemi/" && \
curl -so "/root/Documents/gowinsemi/GW1N-9 Pinout.xlsx" "https://wishfulcoding.nl/gowin/UG114-1.4E_GW1N-9%20Pinout.xlsx" && \
curl -so "/root/Documents/gowinsemi/GW1NR-9 Pinout.xlsx" "https://wishfulcoding.nl/gowin/UG801-1.5E_GW1NR-9%20Pinout.xlsx" && \
curl -so "/root/Documents/gowinsemi/GW1N-4 Pinout.xlsx" "https://wishfulcoding.nl/gowin/UG105-1.6E_GW1N-4%20Pinout.xlsx" && \
curl -so "/root/Documents/gowinsemi/GW1N-1 Pinout.xlsx" "https://wishfulcoding.nl/gowin/UG107-1.09E_GW1N-1%20Pinout.xlsx" && \
curl -so "/root/Documents/gowinsemi/GW1NS-2C Pinout.xlsx" "https://wishfulcoding.nl/gowin/UG825-1.2.1E_GW1NS-2C%20Pinout.xlsx" && \
curl -so "/root/Documents/gowinsemi/GW1NS-2 Pinout.xlsx" "https://wishfulcoding.nl/gowin/UG822-1.2.1E_GW1NS-2%20Pinout.xlsx"

RUN pip install --no-cache-dir numpy crcmod
RUN pip install --no-cache-dir numpy crc

WORKDIR /usr/src/apicula

Expand Down
14 changes: 8 additions & 6 deletions apycula/bslib.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from math import ceil
import numpy as np
from crcmod.predefined import mkPredefinedCrcFun
import crc

crc16arc = mkPredefinedCrcFun('crc-16')
crc16arc = crc.Configuration(width=16, polynomial=0x8005, reverse_input=True, reverse_output=True)

def chunks(l, n):
"""Yield successive n-sized chunks from l."""
Expand All @@ -27,6 +27,7 @@ def read_bitstream(fname):
crcdat = bytearray()
preamble = 3
frames = 0
calc = crc.Calculator(crc16arc)
with open(fname) as inp:
for line in inp:
if line.startswith("//"): continue
Expand Down Expand Up @@ -64,8 +65,8 @@ def read_bitstream(fname):
continue
crcdat.extend(ba[:-8])
crc1 = (ba[-7] << 8) + ba[-8]
crc2 = crc16arc(crcdat)
assert crc1 == crc2, f"Not equal {crc1} {crc2}"
crc2 = calc.checksum(crcdat)
assert crc1 == crc2, f"Not equal {crc1} {crc2} for {crcdat}"
crcdat = ba[-6:]
bitmap.append(bitarr(line, padding))
frames = max(0, frames-1)
Expand Down Expand Up @@ -110,6 +111,7 @@ def write_bitstream(fname, bs, hdr, ftr, compress):

crcdat = bytearray()
preamble = 3
calc = crc.Calculator(crc16arc)
with open(fname, 'w') as f:
for ba in hdr:
if not preamble and ba[0] != 0xd2: # SPI address
Expand All @@ -122,9 +124,9 @@ def write_bitstream(fname, bs, hdr, ftr, compress):
ba = compressLine(ba, key8Z, key4Z, key2Z)
f.write(''.join(f"{b:08b}" for b in ba))
crcdat.extend(ba)
crc = crc16arc(crcdat)
crc_ = calc.checksum(crcdat)
crcdat = bytearray(b'\xff'*6)
f.write(f"{crc&0xff:08b}{crc>>8:08b}")
f.write(f"{crc_&0xff:08b}{crc_>>8:08b}")
f.write('1'*48)
f.write('\n')
for ba in ftr:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
install_requires=[
'numpy',
'crcmod',
'crc',
],
python_requires='>=3.6',
package_data={
Expand Down

0 comments on commit 02520a0

Please sign in to comment.