From 93625ee7c24d7b5f7f33fee291ce58a6065c7259 Mon Sep 17 00:00:00 2001 From: David Barksdale Date: Mon, 4 Jul 2016 13:39:26 -0500 Subject: Add some radio code --- laser-tag software/CMakeLists.txt | 1 + laser-tag software/main.c | 6 +++ laser-tag software/radio.c | 88 +++++++++++++++++++++++++++++++++++++++ laser-tag software/radio.h | 6 +++ 4 files changed, 101 insertions(+) create mode 100644 laser-tag software/radio.c create mode 100644 laser-tag software/radio.h diff --git a/laser-tag software/CMakeLists.txt b/laser-tag software/CMakeLists.txt index 92bc6d1..fa8937d 100644 --- a/laser-tag software/CMakeLists.txt +++ b/laser-tag software/CMakeLists.txt @@ -101,6 +101,7 @@ ADD_EXECUTABLE(hello_world "${ProjDirPath}/main.c" "${ProjDirPath}/epaper.c" "${ProjDirPath}/text.c" + "${ProjDirPath}/radio.c" ) SET_TARGET_PROPERTIES(hello_world PROPERTIES OUTPUT_NAME "hello_world.elf") diff --git a/laser-tag software/main.c b/laser-tag software/main.c index 0dfbac2..8eee6c5 100644 --- a/laser-tag software/main.c +++ b/laser-tag software/main.c @@ -31,6 +31,7 @@ #include "epaper.h" #include "text.h" +#include "radio.h" #include "fsl_clock_manager.h" #include "fsl_cmp_driver.h" #include "fsl_dac_driver.h" @@ -583,6 +584,11 @@ int main (void) } blank_led = 30; + ret = radio_init(); + if (0 == ret) { + led(0x22, 0x00, 0x22); + } + /* We're done, everything else is triggered through interrupts */ for(;;) { if (cue_next_image) { diff --git a/laser-tag software/radio.c b/laser-tag software/radio.c new file mode 100644 index 0000000..0915a29 --- /dev/null +++ b/laser-tag software/radio.c @@ -0,0 +1,88 @@ +#include "radio.h" +#include "fsl_gpio_driver.h" +#include "fsl_spi_master_driver.h" + +static const gpio_output_pin_user_config_t pinReset = { + .pinName = GPIO_MAKE_PIN(GPIOC_IDX, 8), + .config.outputLogic = 1, +}; + +static const spi_master_user_config_t spiConfig = { + .bitsPerSec = 2000000, /* 2 MHz, max is 10 MHz */ + .polarity = kSpiClockPolarity_ActiveHigh, + .phase = kSpiClockPhase_FirstEdge, + .direction = kSpiMsbFirst, + .bitCount = kSpi16BitMode, +}; + +static spi_master_state_t spiState; + +static spi_status_t SPI_Transfer(const uint8_t *tx, uint8_t *rx, size_t count) +{ + spi_status_t rc; + + rc = SPI_DRV_MasterTransfer(0, NULL, tx, rx, count); + if (rc != kStatus_SPI_Success) { + return rc; + } + int i, timeout = (count + 127) / 128 + 1; + for (i = 0; i < timeout; ++i) { + rc = SPI_DRV_MasterGetTransferStatus(0, NULL); + if (rc == kStatus_SPI_Success) { + return rc; + } + } + return rc; +} + +static uint8_t read_reg(uint8_t reg) +{ + uint8_t tx[2] = { 0, reg & 0x7f }; + uint8_t rx[2] = { 0, 0 }; + + SPI_Transfer(tx, rx, 2); + return rx[0]; +} + +static void write_reg(uint8_t reg, uint8_t val) +{ + uint8_t tx[2] = { val, reg | 0x80 }; + + SPI_Transfer(tx, NULL, 2); +} + +static void delay(uint32_t ms) +{ + for (; ms > 0; --ms) { + /* XXX This is really stupid */ + volatile int i; + for (i = 0; i < 306; ++i); + } +} + +int radio_init() +{ + PORT_HAL_SetMuxMode(g_portBase[GPIOC_IDX], 4, kPortMuxAlt2); + PORT_HAL_SetMuxMode(g_portBase[GPIOC_IDX], 5, kPortMuxAlt2); + PORT_HAL_SetMuxMode(g_portBase[GPIOC_IDX], 6, kPortMuxAlt2); + PORT_HAL_SetMuxMode(g_portBase[GPIOC_IDX], 7, kPortMuxAlt2); + PORT_HAL_SetMuxMode(g_portBase[GPIOC_IDX], 8, kPortMuxAsGpio); + GPIO_DRV_OutputPinInit(&pinReset); + delay(1); + GPIO_DRV_WritePinOutput(pinReset.pinName, 0); + delay(5); + + /* Configure DMA channel */ + SPI_DRV_MasterInit(0, &spiState); + uint32_t calculatedBaudRate; + SPI_DRV_MasterConfigureBus(0, &spiConfig, &calculatedBaudRate); + + write_reg(0x2f, 0xaa); + uint32_t reg = read_reg(0x2f); + if (reg != 0xaa) { + return -1; + } + return 0; +} + +/* vim: set expandtab ts=4 sw=4: */ diff --git a/laser-tag software/radio.h b/laser-tag software/radio.h new file mode 100644 index 0000000..de36dd0 --- /dev/null +++ b/laser-tag software/radio.h @@ -0,0 +1,6 @@ +#ifndef _RADIO_H_ +#define _RADIO_H_ + +int radio_init(); + +#endif -- cgit v1.2.3-18-g5258