Last active
May 4, 2018 13:38
-
-
Save wizhippo/1f28b72488ef104f8b4365b53d740b94 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Commands for sending messages on an SPI bus | |
// | |
// Copyright (C) 2016 Kevin O'Connor <[email protected]> | |
// | |
// This file may be distributed under the terms of the GNU GPLv3 license. | |
#include <string.h> // memcpy | |
#include "board/gpio.h" // gpio_out_write | |
#include "basecmd.h" // oid_alloc | |
#include "command.h" // DECL_COMMAND | |
#include "sched.h" // sched_add_timer | |
#define SPI_BUFFER_SIZE 32 | |
static struct task_wake spi_wake; | |
struct spi_dev_s { | |
struct spi_config spi_config; | |
struct gpio_out pin; | |
char msg[SPI_BUFFER_SIZE]; | |
uint8_t len; | |
}; | |
void | |
command_config_spi(uint32_t *args) { | |
struct spi_dev_s *spi = | |
oid_alloc(args[0], command_config_spi, sizeof(*spi)); | |
spi->pin = gpio_out_setup(args[1], 1); | |
spi_config_init(&spi->spi_config, args[2], args[3], args[4]); | |
} | |
DECL_COMMAND(command_config_spi, | |
"config_spi oid=%c bus=%u pin=%u mode=%u clock=%u"); | |
void | |
command_spi_transfer(uint32_t *args) | |
{ | |
struct spi_dev_s *spi = oid_lookup(args[0], command_config_spi); | |
if (args[1] > SPI_BUFFER_SIZE) { | |
shutdown("SPI message larger then max size"); | |
} | |
spi->len = args[1]; | |
memcpy(spi->msg, (void*)(size_t)args[2], spi->len); | |
sched_wake_task(&spi_wake); | |
} | |
DECL_COMMAND(command_spi_transfer, "spi_transfer oid=%c data=%*s"); | |
void | |
spi_task(void) | |
{ | |
if (!sched_check_wake(&spi_wake)) | |
return; | |
uint8_t oid; | |
struct spi_dev_s *spi; | |
foreach_oid(oid, spi, command_config_spi) { | |
spi_start(&spi->spi_config); | |
gpio_out_write(spi->pin, 0); | |
spi_exchange(&spi->spi_config, spi->len, spi->msg, spi->msg); | |
gpio_out_write(spi->pin, 1); | |
spi_stop(&spi->spi_config); | |
sendf("spi_transfer_response oid=%c data=%*s", oid, spi->len, spi->msg); | |
} | |
} | |
DECL_TASK(spi_task); | |
void | |
spi_shutdown(void) { | |
uint8_t oid; | |
struct spi_dev_s *spi; | |
foreach_oid(oid, spi, command_config_spi) { | |
gpio_out_write(spi->pin, 1); | |
} | |
} | |
DECL_SHUTDOWN(spi_shutdown); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment