circuitpython/ports/atmel-samd/external_flash/spi_flash.c

126 lines
4.7 KiB
C
Raw Normal View History

/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016, 2017 Scott Shawcroft for Adafruit Industries
*
* 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 "spi_flash.h"
#include <stdint.h>
#include <string.h>
#include "peripherals.h"
#include "hal_gpio.h"
#include "hal_spi_m_sync.h"
struct spi_m_sync_descriptor spi_flash_desc;
// Enable the flash over SPI.
static void flash_enable(void) {
gpio_set_pin_level(SPI_FLASH_CS_PIN, false);
}
// Disable the flash over SPI.
static void flash_disable(void) {
gpio_set_pin_level(SPI_FLASH_CS_PIN, true);
}
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();
spi_m_sync_transfer(&spi_flash_desc, &xfer);
2018-02-16 12:46:34 -05:00
flash_disable();
}
// 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
}
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
}
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.
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;
}
bool spi_flash_read_data(uint32_t address, uint8_t* data, uint32_t data_length) {
2018-02-16 12:46:34 -05:00
}
void spi_flash_init(void) {
samd_peripherals_sercom_clock_init(SPI_FLASH_SERCOM, SPI_FLASH_SERCOM_INDEX);
// Set up with defaults, then change.
spi_m_sync_init(&spi_flash_desc, SPI_FLASH_SERCOM);
hri_sercomspi_write_CTRLA_DOPO_bf(SPI_FLASH_SERCOM, SPI_FLASH_DOPO);
hri_sercomspi_write_CTRLA_DIPO_bf(SPI_FLASH_SERCOM, SPI_FLASH_DIPO);
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);
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);
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);
spi_m_sync_set_baudrate(&spi_flash_desc, samd_peripherals_spi_baudrate_to_baud_reg_value(SPI_FLASH_BAUDRATE));
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();
spi_m_sync_enable(&spi_flash_desc);
2018-02-16 12:46:34 -05:00
}