68hw_set_bits(&pio->input_sync_bypass,1u<< pin_miso);6970pio_sm_init(pio, sm, prog_offs, &c);71pio_sm_set_enabled(pio, sm, true);72 }The state machine will now immediately begin to shift out any data appearing in the TX FIFO, and push received datainto the RX FIFO.Pico Examples:Lines 18 - 3418void__time_critical_func(pio_spi_write8_blocking)(constpio_spi_inst_t *spi,const uint8_t*src,size_tlen) {19size_ttx_remain = len, rx_remain = len;20// Do 8 bit accesses on FIFO, so that write data is byte-replicated. This21// gets us the left-justification for free (for MSB-first shift-out)22io_rw_8 *txfifo = (io_rw_8 *) &spi->pio->txf[spi->sm];23io_rw_8 *rxfifo = (io_rw_8 *) &spi->pio->rxf[spi->sm];24while(tx_remain || rx_remain) {25if(tx_remain && !pio_sm_is_tx_fifo_full(spi->pio, spi->sm)) {26*txfifo = *src++;27--tx_remain;RP2040 Datasheet3.6. Examples344
28}29if(rx_remain && !pio_sm_is_rx_fifo_empty(spi->pio, spi->sm)) {30(void) *rxfifo;31--rx_remain;32}33}34 }Putting this all together, this complete C program will loop back some data through a PIO SPI at 1MHz, with all fourCPOL/CPHA combinations:Pico Examples:Lines 1 - 77 1/** 2* Copyright (c) 2020 Raspberry Pi (Trading) Ltd. 3* 4* SPDX-License-Identifier: BSD-3-Clause 5*/ 6 7#include<stdlib.h> 8#include<stdio.h> 910#include"pico/stdlib.h"11#include"pio_spi.h"1213// This program instantiates a PIO SPI with each of the four possible14// CPOL/CPHA combinations, with the serial input and output pin mapped to the15// same GPIO. Any data written into the state machine's TX FIFO should then be16// serialised, deserialised, and reappear in the state machine's RX FIFO.1718#define PIN_SCK 1819#define PIN_MOSI 1620#define PIN_MISO 16 // same as MOSI, so we get loopback2122#define BUF_SIZE 202324voidtest(constpio_spi_inst_t *spi) {25static uint8_ttxbuf[BUF_SIZE];26static uint8_trxbuf[BUF_SIZE];27printf("TX:");28for(inti =0; i < BUF_SIZE; ++i) {29txbuf[i] = rand() >>16;30rxbuf[i] =0;31printf(" %02x", (int) txbuf[i]);32}33printf("\n");3435pio_spi_write8_read8_blocking(spi, txbuf, rxbuf, BUF_SIZE);3637printf("RX:");38bool mismatch = false;39for(inti =0; i < BUF_SIZE; ++i) {40printf(" %02x", (int) rxbuf[i]);41mismatch = mismatch || rxbuf[i] != txbuf[i];42}43if(mismatch)44printf("\nNope\n");45else46printf("\nOK\n");47 }4849intmain() {RP2040 Datasheet3.6. Examples345
50stdio_init_all();5152pio_spi_inst_t spi = {53.pio = pio0,54.sm =055};56floatclkdiv =31.25f;// 1 MHz @ 125 clk_sys57uint cpha0_prog_offs = pio_add_program(spi.pio, &spi_cpha0_program);58uint cpha1_prog_offs = pio_add_program(spi.pio, &spi_cpha1_program);5960for(intcpha =0; cpha <=1; ++cpha) {61for(intcpol =0; cpol <=1; ++cpol) {62printf("CPHA = %d, CPOL = %d\n", cpha, cpol);63pio_spi_init(spi.pio, spi.sm,64cpha ? cpha1_prog_offs : cpha0_prog_offs,658,// 8 bits per SPI frame66clkdiv,67cpha,68cpol,69PIN_SCK,70PIN_MOSI,71PIN_MISO72);73test(&spi);74sleep_ms(10);75}76}77 }3.6.2. WS2812 LEDsWS2812 LEDs are driven by a proprietary pulse-width serial format, with a wide positive pulse representing a "1" bit, andnarrow positive pulse a "0". Each LED has a serial input and a serial output; LEDs are connected in a chain, with eachserial input connected to the previous LED’s serial output.
Upload your study docs or become a
Course Hero member to access this document
Upload your study docs or become a
Course Hero member to access this document
End of preview. Want to read all 634 pages?
Upload your study docs or become a
Course Hero member to access this document
Term
Fall
Professor
SudeepPasricha
Tags
Implied warranty