From 523a5d2f0b3f0d79b16784470870935313dd5775 Mon Sep 17 00:00:00 2001 From: Joey Castillo Date: Wed, 26 Jan 2022 22:39:09 -0500 Subject: implement SPI flash chip --- apps/spi-test/app.c | 28 +++++++++++++++------------- apps/spi-test/watch_spi.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 apps/spi-test/watch_spi.c (limited to 'apps') diff --git a/apps/spi-test/app.c b/apps/spi-test/app.c index 54b484ad..2ce2c1f7 100644 --- a/apps/spi-test/app.c +++ b/apps/spi-test/app.c @@ -2,11 +2,20 @@ #include #include #include "watch.h" +#include "spiflash.h" // this is a very basic app to confirm that SPI is working, tested with board OSO-MISC-21-017 and a GD25Q16C Flash chip. void app_init(void) { - watch_enable_spi(); + spi_flash_init(); + + uint8_t buf[3] = {1, 2, 3}; + flash_enable(); + spi_flash_command(CMD_ENABLE_WRITE); + // note that you will need to erase the sector to write different values later: + // spi_flash_sector_command(CMD_SECTOR_ERASE, 0); + spi_flash_write_data(0, buf, 3); + flash_disable(); } void app_wake_from_backup(void) { @@ -23,20 +32,13 @@ void app_wake_from_standby(void) { bool app_loop(void) { - uint8_t get_id_command[1] = {0x9F}; uint8_t buf[3] = {0}; - watch_set_pin_level(A3, false); - - // should print 0, 0, 0 - watch_spi_send(get_id_command, 1); - printf("blank: %x, %x, %x\n", buf[0], buf[1], buf[2]); - - // should print c8, 40, 15 - watch_spi_receive(buf, 3); - printf("ident: %x, %x, %x\n", buf[0], buf[1], buf[2]); - - watch_set_pin_level(A3, true); + flash_enable(); + // should print 1, 2, 3 + spi_flash_read_data(0, buf, 3); + printf("data: %x, %x, %x\n", buf[0], buf[1], buf[2]); + flash_disable(); delay_ms(100); diff --git a/apps/spi-test/watch_spi.c b/apps/spi-test/watch_spi.c new file mode 100644 index 00000000..9227f36a --- /dev/null +++ b/apps/spi-test/watch_spi.c @@ -0,0 +1,46 @@ +/* + * MIT License + * + * Copyright (c) 2022 Joey Castillo + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "watch_spi.h" + +struct io_descriptor *spi_io; + +void watch_enable_spi(void) { + SPI_0_init(); + spi_m_sync_get_io_descriptor(&SPI_0, &spi_io); + spi_m_sync_enable(&SPI_0); +} + +void watch_disable_spi(void) { + spi_m_sync_disable(&SPI_0); + spi_io = NULL; +} + +void watch_spi_read(const uint8_t *buf, uint16_t length) { + io_write(spi_io, buf, length); +} + +void watch_spi_read(uint8_t *buf, uint16_t length) { + io_read(spi_io, buf, length); +} -- cgit v1.2.3