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.
|
|
|
|
*/
|
2016-10-28 23:16:39 -04:00
|
|
|
#include "spi_flash.h"
|
2016-09-02 20:00:30 -04:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-11-22 23:07:21 -05:00
|
|
|
#include "peripherals.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-16 17:00:26 -05:00
|
|
|
void spi_flash_command(uint8_t* request, uint8_t* response, uint32_t length) {
|
2018-02-16 12:46:34 -05:00
|
|
|
struct spi_xfer xfer = { request, response, length };
|
|
|
|
flash_enable();
|
2018-02-16 17:00:26 -05:00
|
|
|
spi_m_sync_transfer(&spi_flash_desc, &xfer);
|
2018-02-16 12:46:34 -05:00
|
|
|
flash_disable();
|
|
|
|
}
|
|
|
|
|
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-16 17:00:26 -05:00
|
|
|
void spi_flash_sector_command(uint8_t command, uint32_t address) {
|
|
|
|
uint8_t request[4] = {command, 0x00, 0x00, 0x00};
|
|
|
|
address_to_bytes(address, page_program_request + 1);
|
|
|
|
struct spi_xfer xfer = { request, NULL, 4 };
|
|
|
|
flash_enable();
|
|
|
|
spi_m_sync_transfer(&spi_flash_desc, &xfer);
|
|
|
|
flash_disable();
|
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-16 12:46:34 -05:00
|
|
|
flash_enable();
|
|
|
|
uint8_t page_program_request[4] = {CMD_PAGE_PROGRAM, 0x00, 0x00, 0x00};
|
|
|
|
// Write the SPI flash write address into the bytes following the command byte.
|
2018-02-16 17:00:26 -05:00
|
|
|
address_to_bytes(address, page_program_request + 1);
|
2018-02-16 12:46:34 -05:00
|
|
|
struct spi_xfer page_program_xfer = {request, 0, request_length};
|
|
|
|
int32_t status = spi_m_sync_transfer(&spi_flash_desc, &page_program_xfer);
|
|
|
|
if (status >= 0) {
|
|
|
|
struct spi_xfer write_data_buffer_xfer = {data, 0, data_length};
|
|
|
|
status = spi_m_sync_transfer(&spi_flash_desc, &write_data_buffer_xfer);
|
|
|
|
}
|
|
|
|
flash_disable();
|
|
|
|
return status >= 0;
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
}
|