Skip to content

Instantly share code, notes, and snippets.

@ChuckM
Created October 21, 2021 06:20
Show Gist options
  • Save ChuckM/299434f3f3cbea17b8bba48122f87bf4 to your computer and use it in GitHub Desktop.
Save ChuckM/299434f3f3cbea17b8bba48122f87bf4 to your computer and use it in GitHub Desktop.
i2s clock generation and shifting loop
#
# xmit_reg is a Signal(16) as are left_dr and right_dr
#
self.sync += [
mclk.eq(~mclk), # running at 1/2 the clock rate (25MHz)
# @posedge of MCLK
If(mclk == 0,
If(sticks == 15,
sticks.eq(0),
sclk.eq(~sclk),
# @negedge of SCLK
If(sclk == 1,
shift_count.eq(shift_count + 1),
# generate LRCLK
If(shift_count == 15,
shift_count.eq(0),
lrclk.eq(~lrclk),
),
# shift out bits
If(shift_count == 0,
If(lrclk == 1,
xmit_reg.eq(left_dr),
).Else(
xmit_reg.eq(right_dr),
)
).Else(
xmit_reg.eq((xmit_reg[:-1] << 1) | xmit_reg[-1]),
),
),
).Else( sticks.eq(sticks + 1)),
)
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment