Skip to content

Commit

Permalink
ports/psoc6: Support for WS218B and SK6812.
Browse files Browse the repository at this point in the history
Signed-off-by: IFX-Anusha <Anusha.TR@infineon.com>
  • Loading branch information
IFX-Anusha committed Aug 23, 2024
1 parent f6ab976 commit 57bf683
Showing 1 changed file with 135 additions and 3 deletions.
138 changes: 135 additions & 3 deletions ports/psoc6/machine_bitstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,36 @@
#if MICROPY_PY_MACHINE_BITSTREAM

const uint32_t timing_sequence_1[] = {500, 1125, 800, 750};
const uint32_t timing_sequence_2[] = {400, 850, 800, 450}; // WS2812B 800kHz
const uint32_t timing_sequence_3[] = {300, 900, 600, 600}; // SK6812
const uint32_t timing_sequence_4[] = {800, 1700, 1600, 900}; // WS2812B 400kHz

uint32_t range = 0;

void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len) {

GPIO_PRT_Type *base = CYHAL_GET_PORTADDR(pin);
uint32_t pinNum = CYHAL_GET_PIN(pin);
uint32_t fcpu_mhz = mp_hal_get_cpu_freq() / 1000000;
if (fcpu_mhz != 100) {
mp_raise_ValueError(MP_ERROR_TEXT("Only 100 MHz is supported"));
}

if (memcmp(timing_ns, timing_sequence_1, 4 * sizeof(uint32_t)) == 0) {
range = 0;
} else if (memcmp(timing_ns, timing_sequence_2, 4 * sizeof(uint32_t)) == 0) {
range = 1;
} else if (memcmp(timing_ns, timing_sequence_3, 4 * sizeof(uint32_t)) == 0) {
range = 2;
} else if (memcmp(timing_ns, timing_sequence_4, 4 * sizeof(uint32_t)) == 0) {
range = 3;
} else {
for (size_t i = 0; i < 4; ++i) {
if ((timing_ns[i]) <= 150) {
mp_raise_ValueError(MP_ERROR_TEXT("Timing is not supported"));
if ((timing_ns[i]) <= 1500) {
mp_raise_ValueError(MP_ERROR_TEXT("Given timing is not supported. Use the defined timings if timing is below 1500 ns"));
}
}
range = 1;
range = 4;
}

// Convert ns to cycles [high_time_0, low_time_0, high_time_1, low_time_1].
Expand Down Expand Up @@ -100,6 +113,125 @@ void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const
}
}

break;

case 1:
for (size_t i = 0; i < len; i++)
{

uint8_t b = buf[i];
// Send each bit of the byte
for (size_t j = 0; j < 8; j++)
{
bool bit = (b & 0x80) != 0;
b <<= 1;
switch (bit)
{
// Send a 1-bit
case 1:
// sendOneBit();
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 0);
// Cy_GPIO_Write(base, pinNum, 0);
break;
// Send a 0-bit
case 0:
// sendZeroBit();
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 0);
Cy_GPIO_Write(base, pinNum, 0);
Cy_GPIO_Write(base, pinNum, 0);
break;
}
}
}

break;

case 2:
for (size_t i = 0; i < len; i++)
{

uint8_t b = buf[i];
// Send each bit of the byte
for (size_t j = 0; j < 8; j++)
{
bool bit = (b & 0x80) != 0;
b <<= 1;
switch (bit)
{
// Send a 1-bit
case 1:
// sendOneBit();
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 0);
Cy_GPIO_Write(base, pinNum, 0);
break;
// Send a 0-bit
case 0:
// sendZeroBit();
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 0);
Cy_GPIO_Write(base, pinNum, 0);
Cy_GPIO_Write(base, pinNum, 0);
break;
}
}
}

case 3:
for (size_t i = 0; i < len; i++)
{

uint8_t b = buf[i];
// Send each bit of the byte
for (size_t j = 0; j < 8; j++)
{
bool bit = (b & 0x80) != 0;
b <<= 1;
switch (bit)
{
// Send a 1-bit
case 1:
// sendOneBit();
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 0);
Cy_GPIO_Write(base, pinNum, 0);
Cy_GPIO_Write(base, pinNum, 0);
break;
// Send a 0-bit
case 0:
// sendZeroBit();
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 1);
Cy_GPIO_Write(base, pinNum, 0);
Cy_GPIO_Write(base, pinNum, 0);
Cy_GPIO_Write(base, pinNum, 0);
Cy_GPIO_Write(base, pinNum, 0);
Cy_GPIO_Write(base, pinNum, 0);
Cy_GPIO_Write(base, pinNum, 0);
Cy_GPIO_Write(base, pinNum, 0);
break;
}
}
}

break;
default:
for (size_t i = 0; i < len; ++i) {
Expand Down

0 comments on commit 57bf683

Please sign in to comment.