Created
January 11, 2020 16:45
-
-
Save a1k0n/74ce30fe5d3f0206b7feda1c10063aef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// read a sample sent to the YM3014B | |
// it's a 13.3 floating point format, 3 MSBs are exponent, | |
// 13 LSBs are mantissa, except only 10 of the mantissa bits are used | |
uint16_t ym3014b_read16(void) { | |
uint16_t data = 0; | |
uint8_t last_port_state = GPIOC_PDIR; | |
// wait for rising clock edge | |
// if we see a falling sync edge, return data | |
while (true) { | |
uint8_t port_state = GPIOC_PDIR; | |
uint8_t change = port_state ^ last_port_state; | |
// rising clock edge, shift in a data bit | |
if ((change & mask_clk) && (port_state & mask_clk)) { | |
data = (data >> 1) | ((port_state & mask_data) << shift_data); | |
} | |
// falling sync edge; we're done | |
if ((change & mask_sync) && (last_port_state & mask_sync)) { | |
return data; | |
} | |
last_port_state = port_state; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment