Added spi/spi_master_slave. (#101)
Co-authored-by: Michael Stoops <spam@michaelstoops.com>
This commit is contained in:
89
spi/spi_master_slave/spi_master/spi_master.c
Normal file
89
spi/spi_master_slave/spi_master/spi_master.c
Normal file
@@ -0,0 +1,89 @@
|
||||
// Copyright (c) 2021 Michael Stoops. All rights reserved.
|
||||
// Portions copyright (c) 2021 Raspberry Pi (Trading) Ltd.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
|
||||
// following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
|
||||
// disclaimer.
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
|
||||
// following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
|
||||
// products derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
||||
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
//
|
||||
// Example of an SPI bus master using the PL022 SPI interface
|
||||
|
||||
#include <stdio.h>
|
||||
#include "pico/stdlib.h"
|
||||
#include "pico/binary_info.h"
|
||||
#include "hardware/spi.h"
|
||||
|
||||
#define BUF_LEN 0x100
|
||||
|
||||
void printbuf(uint8_t buf[], size_t len) {
|
||||
int i;
|
||||
for (i = 0; i < len; ++i) {
|
||||
if (i % 16 == 15)
|
||||
printf("%02x\n", buf[i]);
|
||||
else
|
||||
printf("%02x ", buf[i]);
|
||||
}
|
||||
|
||||
// append trailing newline if there isn't one
|
||||
if (i % 16) {
|
||||
putchar('\n');
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Enable UART so we can print
|
||||
stdio_init_all();
|
||||
#if !defined(spi_default) || !defined(PICO_DEFAULT_SPI_SCK_PIN) || !defined(PICO_DEFAULT_SPI_TX_PIN) || !defined(PICO_DEFAULT_SPI_RX_PIN) || !defined(PICO_DEFAULT_SPI_CSN_PIN)
|
||||
#warning spi/spi_master example requires a board with SPI pins
|
||||
puts("Default SPI pins were not defined");
|
||||
#else
|
||||
|
||||
printf("SPI master example\n");
|
||||
|
||||
// Enable SPI 0 at 1 MHz and connect to GPIOs
|
||||
spi_init(spi_default, 1000 * 1000);
|
||||
gpio_set_function(PICO_DEFAULT_SPI_RX_PIN, GPIO_FUNC_SPI);
|
||||
gpio_set_function(PICO_DEFAULT_SPI_SCK_PIN, GPIO_FUNC_SPI);
|
||||
gpio_set_function(PICO_DEFAULT_SPI_TX_PIN, GPIO_FUNC_SPI);
|
||||
gpio_set_function(PICO_DEFAULT_SPI_CSN_PIN, GPIO_FUNC_SPI);
|
||||
// Make the SPI pins available to picotool
|
||||
bi_decl(bi_4pins_with_func(PICO_DEFAULT_SPI_RX_PIN, PICO_DEFAULT_SPI_TX_PIN, PICO_DEFAULT_SPI_SCK_PIN, PICO_DEFAULT_SPI_CSN_PIN, GPIO_FUNC_SPI));
|
||||
|
||||
uint8_t out_buf[BUF_LEN], in_buf[BUF_LEN];
|
||||
|
||||
// Initialize output buffer
|
||||
for (size_t i = 0; i < BUF_LEN; ++i) {
|
||||
out_buf[i] = i;
|
||||
}
|
||||
|
||||
printf("SPI master says: The following buffer will be written to MOSI endlessly:\n");
|
||||
printbuf(out_buf, BUF_LEN);
|
||||
|
||||
for (size_t i = 0; ; ++i) {
|
||||
// Write the output buffer to MOSI, and at the same time read from MISO.
|
||||
spi_write_read_blocking(spi_default, out_buf, in_buf, BUF_LEN);
|
||||
|
||||
// Write to stdio whatever came in on the MISO line.
|
||||
printf("SPI master says: read page %d from the MISO line:\n", i);
|
||||
printbuf(in_buf, BUF_LEN);
|
||||
|
||||
// Sleep for ten seconds so you get a chance to read the output.
|
||||
sleep_ms(10 * 1000);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user