Thanks for that.
Yes, it was supposed to be SET Y, 5, im using that to count the number of SLOAD low bits before a frame end on SLOAD high.
pio_gpio_init(pio, pin + 2), was what i was using to init the SLOAD pin. I have renamed it to make it a little easier to read.
i'll try this and see if it works.
Yes, it was supposed to be SET Y, 5, im using that to count the number of SLOAD low bits before a frame end on SLOAD high.
pio_gpio_init(pio, pin + 2), was what i was using to init the SLOAD pin. I have renamed it to make it a little easier to read.
i'll try this and see if it works.
Code:
#include <stdio.h>#include "pico/stdlib.h"#include "hardware/i2c.h"#include "hardware/pio.h"#include "hardware/irq.h"#include "pico/multicore.h"#include "sgpio.pio.h"// I2C defines#define I2C_PORT i2c0#define I2C_SDA 16#define I2C_SCL 17// SGPIO IN defines#define SDATA_PIN 6#define SCLK_PIN 7#define SLOAD_PIN 8// SGPIO OUT defines#define SCLK1_PIN 3#define SLOAD1_PIN 4#define SDOUT1_PIN 5PIO pio = pio0;uint sm = 0;int sgpio_speed = 1000;// Interrupt handler for PIO RX FIFOvoid pio0_irq_handler() { uint32_t sgpio_data = pio_sm_get(pio, sm); // Read data from RX FIFO printf("Recieved: %b \n", sgpio_data); }void core1_main(){ while (1) // send dummy SGPIO data { gpio_put(SCLK1_PIN, 1); gpio_put(SLOAD1_PIN, 1); gpio_put(SDOUT1_PIN, 1); sleep_us(sgpio_speed); gpio_put(SCLK1_PIN, 0); sleep_us(sgpio_speed); gpio_put(SCLK1_PIN, 1); gpio_put(SLOAD1_PIN, 0); gpio_put(SDOUT1_PIN, 1); sleep_us(sgpio_speed); gpio_put(SCLK1_PIN, 0); sleep_us(sgpio_speed); gpio_put(SCLK1_PIN, 1); gpio_put(SLOAD1_PIN, 1); gpio_put(SDOUT1_PIN, 1); sleep_us(sgpio_speed); gpio_put(SCLK1_PIN, 0); sleep_us(sgpio_speed); gpio_put(SCLK1_PIN, 1); gpio_put(SLOAD1_PIN, 0); gpio_put(SDOUT1_PIN, 1); sleep_us(sgpio_speed); gpio_put(SCLK1_PIN, 0); sleep_us(sgpio_speed); gpio_put(SCLK1_PIN, 1); gpio_put(SLOAD1_PIN, 1); gpio_put(SDOUT1_PIN, 1); sleep_us(sgpio_speed); gpio_put(SCLK1_PIN, 0); sleep_us(sgpio_speed); for (int i = 0; i < 5; i++) { gpio_put(SCLK1_PIN, 1); gpio_put(SLOAD1_PIN, 0); gpio_put(SDOUT1_PIN, 1); sleep_us(sgpio_speed); gpio_put(SCLK1_PIN, 0); sleep_us(sgpio_speed); } } }int main(){ stdio_init_all(); // I2C Initialisation. Using it at 400Khz. i2c_init(I2C_PORT, 400*1000); gpio_set_function(I2C_SDA, GPIO_FUNC_I2C); gpio_set_function(I2C_SCL, GPIO_FUNC_I2C); gpio_pull_up(I2C_SDA); gpio_pull_up(I2C_SCL); // SGPIO pin Init gpio_init(SCLK1_PIN); gpio_init(SLOAD1_PIN); gpio_init(SDOUT1_PIN); gpio_set_dir(SCLK1_PIN, GPIO_OUT); gpio_set_dir(SLOAD1_PIN, GPIO_OUT); gpio_set_dir(SDOUT1_PIN, GPIO_OUT); // setup pio uint offset = pio_add_program(pio, &sgpio_program); sgpio_program_init(pio, sm, offset, SDATA_PIN, SCLK_PIN, SLOAD_PIN); pio_sm_set_enabled(pio, sm, true); // Enable interrupt when RX FIFO is not empty pio_set_irq0_source_enabled(pio, pis_sm0_rx_fifo_not_empty, true); irq_set_exclusive_handler(PIO0_IRQ_0, pio0_irq_handler); irq_set_enabled(PIO0_IRQ_0, true); printf("Loaded program at %d\n", offset); multicore_launch_core1(core1_main);// Main Loop while (true) { sleep_ms(1000); }}
Code:
;; Copyright (c) 2020 Raspberry Pi (Trading) Ltd.;; SPDX-License-Identifier: BSD-3-Clause;; SET pin 0 should be mapped to your LED GPIO.program sgpiosyncreset: set y, 5 ; set y = 5syncloop: wait 1 pin 1 wait 0 pin 1 ; wait for clock falling edge jmp !y synccomplete ; if y == 0, EOF expected jmp pin syncreset ; if SLOAD high, reset counter y and keep waiting jmp y-- syncloop ; if SLOAD low, decrement counter y and keep waitingsynccomplete: set y, 5 wait 1 pin 1 wait 0 pin 1 ; wait for clock falling edge jmp pin loop1 ; if SLOAD high jump to main recieve loop jmp synccomplete ; if SLOAD low, keep waiting.wrap_targetloop1: wait 1 pin 1 wait 0 pin 1 ; wait for clock falling edge in pins, 1 ; read DOUT into ISR jmp pin sloadhigh ; if SLOAD high, check for EOF jmp y-- loop1 ; get next bitsloadhigh: jmp !y dataout ; if counter y is 0, EOF. set y, 5 ; reset counter y jmp loop1 ; if not EOF, get next bitdataout: push ; send frame to RX FIFO set y, 5 ; reset counter y jmp loop1 ; get next bit.wrap % c-sdk {// this is a raw helper function for use by the user which sets up the GPIO output, and configures the SM to output on a particular pinvoid sgpio_program_init(PIO pio, uint sm, uint offset, uint data_pin, uint sclk_pin, uint sload_pin) { pio_sm_config c = sgpio_program_get_default_config(offset); sm_config_set_in_pins(&c, data_pin); sm_config_set_jmp_pin(&c, sload_pin); pio_sm_set_consecutive_pindirs(pio, sm, data_pin, 3, false); // set 3 input pins pio_gpio_init(pio, data_pin); pio_gpio_init(pio, sclk_pin); pio_gpio_init(pio, sload_pin); pio_sm_init(pio, sm, offset, &c); }%}
Statistics: Posted by Sprog007 — Sun Nov 24, 2024 9:38 pm