2016-09-02 20:00:30 -04:00
|
|
|
/*
|
2017-08-27 15:02:50 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2016-09-02 20:00:30 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2017-05-02 18:25:06 -04:00
|
|
|
* Copyright (c) 2016, 2017 Scott Shawcroft for Adafruit Industries
|
2016-09-02 20:00:30 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-02-17 03:29:03 -05:00
|
|
|
#include "spi_flash_api.h"
|
2016-09-02 20:00:30 -04:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-02-17 03:29:03 -05:00
|
|
|
#include "external_flash/common_commands.h"
|
2017-11-22 23:07:21 -05:00
|
|
|
#include "peripherals.h"
|
2018-03-09 15:05:12 -05:00
|
|
|
#include "shared_dma.h"
|
2016-09-02 20:00:30 -04:00
|
|
|
|
2017-11-22 23:07:21 -05:00
|
|
|
#include "hal_gpio.h"
|
|
|
|
#include "hal_spi_m_sync.h"
|
2016-10-13 17:53:31 -04:00
|
|
|
|
2017-11-22 23:07:21 -05:00
|
|
|
struct spi_m_sync_descriptor spi_flash_desc;
|
2016-09-02 20:00:30 -04:00
|
|
|
|
2016-10-18 17:56:17 -04:00
|
|
|
// Enable the flash over SPI.
|
2016-11-03 18:50:59 -04:00
|
|
|
static void flash_enable(void) {
|
2017-11-22 23:07:21 -05:00
|
|
|
gpio_set_pin_level(SPI_FLASH_CS_PIN, false);
|
2016-10-14 15:22:06 -04:00
|
|
|
}
|
|
|
|
|
2016-10-18 17:56:17 -04:00
|
|
|
// Disable the flash over SPI.
|
2016-11-03 18:50:59 -04:00
|
|
|
static void flash_disable(void) {
|
2017-11-22 23:07:21 -05:00
|
|
|
gpio_set_pin_level(SPI_FLASH_CS_PIN, true);
|
2016-10-14 15:22:06 -04:00
|
|
|
}
|
|
|
|
|
2018-02-17 03:29:03 -05:00
|
|
|
static bool transfer(uint8_t* command, uint32_t command_length, uint8_t* data_in, uint8_t* data_out, uint32_t data_length) {
|
|
|
|
struct spi_xfer xfer = { command, NULL, command_length };
|
2018-02-16 12:46:34 -05:00
|
|
|
flash_enable();
|
2018-02-17 03:29:03 -05:00
|
|
|
int32_t status = spi_m_sync_transfer(&spi_flash_desc, &xfer);
|
|
|
|
if (status >= 0 && !(data_in == NULL && data_out == NULL)) {
|
|
|
|
struct spi_xfer data_xfer = {data_in, data_out, data_length};
|
|
|
|
status = spi_m_sync_transfer(&spi_flash_desc, &data_xfer);
|
|
|
|
}
|
2018-02-16 12:46:34 -05:00
|
|
|
flash_disable();
|
2018-02-17 03:29:03 -05:00
|
|
|
return status >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool transfer_command(uint8_t command, uint8_t* data_in, uint8_t* data_out, uint32_t data_length) {
|
|
|
|
return transfer(&command, 1, data_in, data_out, data_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool spi_flash_command(uint8_t command) {
|
|
|
|
return transfer_command(command, NULL, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool spi_flash_read_command(uint8_t command, uint8_t* data, uint32_t data_length) {
|
|
|
|
return transfer_command(command, NULL, data, data_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool spi_flash_write_command(uint8_t command, uint8_t* data, uint32_t data_length) {
|
|
|
|
return transfer_command(command, data, NULL, data_length);
|
2018-02-16 12:46:34 -05:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:00:26 -05:00
|
|
|
// Pack the low 24 bits of the address into a uint8_t array.
|
|
|
|
static void address_to_bytes(uint32_t address, uint8_t* bytes) {
|
|
|
|
bytes[0] = (address >> 16) & 0xff;
|
|
|
|
bytes[1] = (address >> 8) & 0xff;
|
|
|
|
bytes[2] = address & 0xff;
|
2018-02-16 12:46:34 -05:00
|
|
|
}
|
|
|
|
|
2018-02-17 03:29:03 -05:00
|
|
|
bool spi_flash_sector_command(uint8_t command, uint32_t address) {
|
2018-02-16 17:00:26 -05:00
|
|
|
uint8_t request[4] = {command, 0x00, 0x00, 0x00};
|
2018-02-17 03:29:03 -05:00
|
|
|
address_to_bytes(address, request + 1);
|
|
|
|
return transfer(request, 4, NULL, NULL, 0);
|
2018-02-16 12:46:34 -05:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:00:26 -05:00
|
|
|
bool spi_flash_write_data(uint32_t address, uint8_t* data, uint32_t data_length) {
|
2018-02-17 03:29:03 -05:00
|
|
|
uint8_t request[4] = {CMD_PAGE_PROGRAM, 0x00, 0x00, 0x00};
|
2018-02-16 12:46:34 -05:00
|
|
|
// Write the SPI flash write address into the bytes following the command byte.
|
2018-02-17 03:29:03 -05:00
|
|
|
address_to_bytes(address, request + 1);
|
2018-03-09 15:05:12 -05:00
|
|
|
struct spi_xfer xfer = { request, NULL, 4 };
|
|
|
|
flash_enable();
|
|
|
|
int32_t status = spi_m_sync_transfer(&spi_flash_desc, &xfer);
|
|
|
|
if (status >= 0) {
|
|
|
|
status = sercom_dma_write(spi_flash_desc.dev.prvt, data, data_length);
|
|
|
|
}
|
|
|
|
flash_disable();
|
|
|
|
return status >= 0;
|
2018-02-16 12:46:34 -05:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:00:26 -05:00
|
|
|
bool spi_flash_read_data(uint32_t address, uint8_t* data, uint32_t data_length) {
|
2018-02-17 03:29:03 -05:00
|
|
|
uint8_t request[4] = {CMD_READ_DATA, 0x00, 0x00, 0x00};
|
|
|
|
// Write the SPI flash write address into the bytes following the command byte.
|
|
|
|
address_to_bytes(address, request + 1);
|
2018-03-09 15:05:12 -05:00
|
|
|
struct spi_xfer xfer = { request, NULL, 4 };
|
|
|
|
flash_enable();
|
|
|
|
int32_t status = spi_m_sync_transfer(&spi_flash_desc, &xfer);
|
|
|
|
if (status >= 0) {
|
|
|
|
status = sercom_dma_read(spi_flash_desc.dev.prvt, data, data_length, 0xff);
|
|
|
|
}
|
|
|
|
flash_disable();
|
|
|
|
return status >= 0;
|
2018-02-16 12:46:34 -05:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:00:26 -05:00
|
|
|
void spi_flash_init(void) {
|
2017-11-22 23:07:21 -05:00
|
|
|
samd_peripherals_sercom_clock_init(SPI_FLASH_SERCOM, SPI_FLASH_SERCOM_INDEX);
|
2016-10-14 15:22:06 -04:00
|
|
|
|
2017-11-22 23:07:21 -05:00
|
|
|
// Set up with defaults, then change.
|
|
|
|
spi_m_sync_init(&spi_flash_desc, SPI_FLASH_SERCOM);
|
2018-01-23 18:58:05 -05:00
|
|
|
|
2017-11-22 23:07:21 -05:00
|
|
|
hri_sercomspi_write_CTRLA_DOPO_bf(SPI_FLASH_SERCOM, SPI_FLASH_DOPO);
|
|
|
|
hri_sercomspi_write_CTRLA_DIPO_bf(SPI_FLASH_SERCOM, SPI_FLASH_DIPO);
|
2016-10-21 14:59:02 -04:00
|
|
|
|
2017-11-22 23:07:21 -05:00
|
|
|
gpio_set_pin_direction(SPI_FLASH_SCK_PIN, GPIO_DIRECTION_OUT);
|
|
|
|
gpio_set_pin_pull_mode(SPI_FLASH_SCK_PIN, GPIO_PULL_OFF);
|
|
|
|
gpio_set_pin_function(SPI_FLASH_SCK_PIN, SPI_FLASH_SCK_PIN_FUNCTION);
|
2016-10-14 15:22:06 -04:00
|
|
|
|
2017-11-22 23:07:21 -05:00
|
|
|
gpio_set_pin_direction(SPI_FLASH_MOSI_PIN, GPIO_DIRECTION_OUT);
|
|
|
|
gpio_set_pin_pull_mode(SPI_FLASH_MOSI_PIN, GPIO_PULL_OFF);
|
|
|
|
gpio_set_pin_function(SPI_FLASH_MOSI_PIN, SPI_FLASH_MOSI_PIN_FUNCTION);
|
2017-05-12 17:47:39 -04:00
|
|
|
|
2017-11-22 23:07:21 -05:00
|
|
|
gpio_set_pin_direction(SPI_FLASH_MISO_PIN, GPIO_DIRECTION_IN);
|
|
|
|
gpio_set_pin_pull_mode(SPI_FLASH_MISO_PIN, GPIO_PULL_OFF);
|
|
|
|
gpio_set_pin_function(SPI_FLASH_MISO_PIN, SPI_FLASH_MISO_PIN_FUNCTION);
|
|
|
|
|
|
|
|
hri_sercomspi_write_CTRLA_DOPO_bf(SPI_FLASH_SERCOM, SPI_FLASH_DOPO);
|
|
|
|
hri_sercomspi_write_CTRLA_DIPO_bf(SPI_FLASH_SERCOM, SPI_FLASH_DIPO);
|
2017-05-02 18:25:06 -04:00
|
|
|
|
2018-01-29 22:52:23 -05:00
|
|
|
spi_m_sync_set_baudrate(&spi_flash_desc, samd_peripherals_spi_baudrate_to_baud_reg_value(SPI_FLASH_BAUDRATE));
|
2017-05-02 18:25:06 -04:00
|
|
|
|
2017-11-22 23:07:21 -05:00
|
|
|
gpio_set_pin_direction(SPI_FLASH_CS_PIN, GPIO_DIRECTION_OUT);
|
|
|
|
// There's already a pull-up on the board.
|
|
|
|
gpio_set_pin_pull_mode(SPI_FLASH_CS_PIN, GPIO_PULL_OFF);
|
|
|
|
gpio_set_pin_function(SPI_FLASH_CS_PIN, GPIO_PIN_FUNCTION_OFF);
|
|
|
|
|
|
|
|
// Set CS high (disabled).
|
|
|
|
flash_disable();
|
2016-10-14 15:22:06 -04:00
|
|
|
|
2017-11-22 23:07:21 -05:00
|
|
|
spi_m_sync_enable(&spi_flash_desc);
|
2018-02-16 12:46:34 -05:00
|
|
|
}
|
2018-03-22 19:42:47 -04:00
|
|
|
|
|
|
|
void spi_flash_init_device(const external_flash_device* device) {
|
|
|
|
|
|
|
|
}
|