2021-06-28 23:03:02 +00:00
|
|
|
// vim: set et:
|
2021-06-06 03:10:36 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <hardware/spi.h>
|
2021-06-28 23:03:02 +00:00
|
|
|
#include <pico/binary_info.h>
|
|
|
|
#include <pico/stdlib.h>
|
2021-06-06 03:10:36 +00:00
|
|
|
|
2021-06-14 00:01:35 +00:00
|
|
|
#include "pinout.h"
|
2021-06-06 03:10:36 +00:00
|
|
|
#include "protos.h"
|
|
|
|
#include "serprog.h"
|
|
|
|
|
|
|
|
static bool cs_asserted;
|
|
|
|
|
|
|
|
void sp_spi_init(void) {
|
2021-06-28 23:03:02 +00:00
|
|
|
cs_asserted = false;
|
2021-06-06 03:10:36 +00:00
|
|
|
|
2021-06-28 23:03:02 +00:00
|
|
|
spi_init(PINOUT_SPI_DEV, 512 * 1000); // default to 512 kHz
|
2021-06-06 03:10:36 +00:00
|
|
|
|
2021-06-28 23:03:02 +00:00
|
|
|
gpio_set_function(PINOUT_SPI_MISO, GPIO_FUNC_SPI);
|
|
|
|
gpio_set_function(PINOUT_SPI_MOSI, GPIO_FUNC_SPI);
|
|
|
|
gpio_set_function(PINOUT_SPI_SCLK, GPIO_FUNC_SPI);
|
2021-06-06 03:10:36 +00:00
|
|
|
|
2021-06-28 23:03:02 +00:00
|
|
|
// gpio_set_function(PINOUT_SPI_nCS, GPIO_FUNC_SIO);
|
|
|
|
gpio_init(PINOUT_SPI_nCS);
|
|
|
|
gpio_put(PINOUT_SPI_nCS, 1);
|
|
|
|
gpio_set_dir(PINOUT_SPI_nCS, GPIO_OUT);
|
2021-06-08 00:23:02 +00:00
|
|
|
|
2021-06-28 23:03:02 +00:00
|
|
|
bi_decl(bi_3pins_with_func(PINOUT_SPI_MISO, PINOUT_SPI_MOSI, PINOUT_SPI_SCLK, GPIO_FUNC_SPI));
|
|
|
|
bi_decl(bi_1pin_with_name(PINOUT_SPI_nCS, "SPI #CS"));
|
2021-06-06 03:10:36 +00:00
|
|
|
}
|
|
|
|
uint32_t __not_in_flash_func(sp_spi_set_freq)(uint32_t freq_wanted) {
|
2021-06-28 23:03:02 +00:00
|
|
|
return spi_set_baudrate(PINOUT_SPI_DEV, freq_wanted);
|
2021-06-06 03:10:36 +00:00
|
|
|
}
|
|
|
|
void __not_in_flash_func(sp_spi_cs_deselect)(void) {
|
2021-06-28 23:03:02 +00:00
|
|
|
asm volatile("nop\nnop\nnop"); // idk if this is needed
|
|
|
|
gpio_put(PINOUT_SPI_nCS, 1);
|
|
|
|
asm volatile("nop\nnop\nnop"); // idk if this is needed
|
|
|
|
cs_asserted = false;
|
2021-06-06 03:10:36 +00:00
|
|
|
}
|
|
|
|
void __not_in_flash_func(sp_spi_cs_select)(void) {
|
2021-06-28 23:03:02 +00:00
|
|
|
asm volatile("nop\nnop\nnop"); // idk if this is needed
|
|
|
|
gpio_put(PINOUT_SPI_nCS, 0);
|
|
|
|
asm volatile("nop\nnop\nnop"); // idk if this is needed
|
|
|
|
cs_asserted = true;
|
2021-06-06 03:10:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void __not_in_flash_func(sp_spi_op_begin)(void) {
|
2021-06-28 23:03:02 +00:00
|
|
|
// sp_spi_cs_select();
|
|
|
|
if (!cs_asserted) {
|
|
|
|
asm volatile("nop\nnop\nnop"); // idk if this is needed
|
|
|
|
gpio_put(PINOUT_SPI_nCS, 0);
|
|
|
|
asm volatile("nop\nnop\nnop"); // idk if this is needed
|
|
|
|
}
|
2021-06-06 03:10:36 +00:00
|
|
|
}
|
|
|
|
void __not_in_flash_func(sp_spi_op_end)(void) {
|
2021-06-28 23:03:02 +00:00
|
|
|
// sp_spi_cs_deselect();
|
|
|
|
if (!cs_asserted) { // YES, this condition is the intended one!
|
|
|
|
asm volatile("nop\nnop\nnop"); // idk if this is needed
|
|
|
|
gpio_put(PINOUT_SPI_nCS, 1);
|
|
|
|
asm volatile("nop\nnop\nnop"); // idk if this is needed
|
|
|
|
}
|
2021-06-06 03:10:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: use dma?
|
|
|
|
void __not_in_flash_func(sp_spi_op_write)(uint32_t write_len, const uint8_t* write_data) {
|
2021-06-28 23:03:02 +00:00
|
|
|
spi_write_blocking(PINOUT_SPI_DEV, write_data, write_len);
|
2021-06-06 03:10:36 +00:00
|
|
|
}
|
|
|
|
void __not_in_flash_func(sp_spi_op_read)(uint32_t read_len, uint8_t* read_data) {
|
2021-06-28 23:03:02 +00:00
|
|
|
spi_read_blocking(PINOUT_SPI_DEV, 0, read_data, read_len);
|
2021-06-06 03:10:36 +00:00
|
|
|
}
|
|
|
|
|