get qspi work with Feather nrf52840

This commit is contained in:
hathach committed 2019-04-17 13:25:00 +07:00
1 parent 6cb638ca8f
commit 523dee3ba9
14 files changed
+105 -135

No files matched your search

+21 -15
View File
@@ -115,7 +115,7 @@
// DEBUG HELPER
//--------------------------------------------------------------------+
#if CFG_DEBUG == 2
#define malloc_named( name, size ) ({ ::printf("[malloc] %s : %d\r\n", name, size); malloc(size); })
#define malloc_named( name, size ) ({ PRINTF("[malloc] %s : %d\r\n", name, size); malloc(size); })
#else
#define malloc_named( name, size ) malloc ( size )
#endif
@@ -140,38 +140,44 @@ const char* dbg_err_str(int32_t err_id); // TODO move to other place
#if CFG_DEBUG
#define PRINT_LOCATION() ::printf("%s: %d:\n", __PRETTY_FUNCTION__, __LINE__)
#define PRINT_MESS(x) ::printf("%s: %d: %s \n" , __FUNCTION__, __LINE__, (char*)(x))
#define PRTNT_HEAP() if (CFG_DEBUG == 3) ::printf("\n%s: %d: Heap free: %d\n", __FUNCTION__, __LINE__, util_heap_get_free_size())
#define PRINT_STR(x) ::printf("%s: %d: " #x " = %s\n" , __FUNCTION__, __LINE__, (char*)(x) )
#define PRINT_INT(x) ::printf("%s: %d: " #x " = %ld\n" , __FUNCTION__, __LINE__, (uint32_t) (x) )
#if __cplusplus
#define PRINTF ::printf
#else
#define PRINTF printf
#endif
#define PRINT_LOCATION() PRINTF("%s: %d:\n", __PRETTY_FUNCTION__, __LINE__)
#define PRINT_MESS(x) PRINTF("%s: %d: %s \n" , __FUNCTION__, __LINE__, (char*)(x))
#define PRTNT_HEAP() if (CFG_DEBUG == 3) PRINTF("\n%s: %d: Heap free: %d\n", __FUNCTION__, __LINE__, util_heap_get_free_size())
#define PRINT_STR(x) PRINTF("%s: %d: " #x " = %s\n" , __FUNCTION__, __LINE__, (char*)(x) )
#define PRINT_INT(x) PRINTF("%s: %d: " #x " = %ld\n" , __FUNCTION__, __LINE__, (uint32_t) (x) )
#define PRINT_HEX(x) \
do {\
::printf("%s: %d: " #x " = 0x", __PRETTY_FUNCTION__, __LINE__);\
PRINTF("%s: %d: " #x " = 0x", __PRETTY_FUNCTION__, __LINE__);\
char fmt[] = "%00X\n";\
fmt[2] += 2*sizeof(x); /* Hex with correct size */\
::printf(fmt, (x) );\
PRINTF(fmt, (x) );\
}while(0)
#define PRINT_BUFFER(buf, n) \
do {\
uint8_t const* p8 = (uint8_t const*) (buf);\
::printf(#buf ": ");\
for(uint32_t i=0; i<(n); i++) ::printf("%02x ", p8[i]);\
::printf("\n");\
PRINTF(#buf ": ");\
for(uint32_t i=0; i<(n); i++) PRINTF("%02x ", p8[i]);\
PRINTF("\n");\
}while(0)
#define ADALOG(tag, ...) \
do { \
if ( tag ) ::printf("[%-6s] ", tag);\
::printf(__VA_ARGS__);\
::printf("\n");\
if ( tag ) PRINTF("[%-6s] ", tag);\
PRINTF(__VA_ARGS__);\
PRINTF("\n");\
}while(0)
#define ADALOG_BUFFER(_tag, _buf, _n) \
do {\
if ( _tag ) ::printf("%-6s: len = %d\n", _tag, _n);\
if ( _tag ) PRINTF("%-6s: len = %d\n", _tag, _n);\
dbgDumpMemory(_buf, 1, _n, true);\
}while(0)
+12 -8
View File
@@ -1,9 +1,7 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2018 hathach for Adafruit Industries
* Copyright (c) 2019 Ha Thach 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
@@ -37,29 +35,35 @@
typedef struct {
uint32_t total_size;
// Three response bytes to 0x90 JEDEC REMS ID command.
// Response bytes to 0x90 JEDEC REMS ID command.
uint8_t manufacturer_id;
uint8_t device_id;
// Status register value enable quad mode
// Status register value to enable quad mode
uint16_t status_quad_enable;
// Working frequency
uint8_t freq;
} qspi_flash_device_t;
// Settings for the Gigadevice GD25Q16C 2MiB SPI flash.
// Datasheet: http://www.gigadevice.com/wp-content/uploads/2017/12/DS-00086-GD25Q16C-Rev2.6.pdf
// Datasheet: http://www.gigadevice.com/datasheet/gd25q16c/
// Can only work reliably with nrf5x at 16Mhz, even though it should work up to 104 MHz
#define GD25Q16C {\
.total_size = 2*1024*1024, \
.manufacturer_id = 0xc8, \
.device_id = 0x14, \
.status_quad_enable = (1 << 9)\
.status_quad_enable = (1 << 9),\
.freq = NRF_QSPI_FREQ_32MDIV2 \
}
#define MX25R6435F {\
.total_size = 8*1024*1024, \
.manufacturer_id = 0xc2, \
.device_id = 0x17, \
.status_quad_enable = (1 << 6)\
.status_quad_enable = (1 << 6),\
.freq = NRF_QSPI_FREQ_32MDIV1 \
}
#ifdef __cplusplus
+58 -94
View File
@@ -1,38 +1,26 @@
/**************************************************************************/
/*!
@file flash_qspi.c
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2018, Adafruit Industries (adafruit.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**************************************************************************/
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Ha Thach 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.
*/
#ifdef NRF52840_XXAA
@@ -42,14 +30,7 @@
#include "flash_cache.h"
#include "nrfx_qspi.h"
#define _VALID_PIN(n) (defined(PIN_QSPI_DATA##n) && (PIN_QSPI_DATA##n != 0xff))
#define QSPI_FLASH_MODE \
(( _VALID_PIN(0) && _VALID_PIN(1) && _VALID_PIN(2) && _VALID_PIN(3) ) ? 4 : \
( _VALID_PIN(0) && _VALID_PIN(1) ) ? 2 : \
( _VALID_PIN(0) ) ? 1 : 0)
#if QSPI_FLASH_MODE
#ifdef EXTERNAL_FLASH_DEVICES
//--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
@@ -59,20 +40,11 @@ enum
QSPI_CMD_RSTEN = 0x66,
QSPI_CMD_RST = 0x99,
QSPI_CMD_WRSR = 0x01,
QSPI_CMD_READID = 0x90
QSPI_CMD_REMS = 0x90,
};
// If Flash device is not specified, support all devices in flash_devices.h
#ifdef EXTERNAL_FLASH_DEVICES
const qspi_flash_device_t _flash_devices_arr[] = { EXTERNAL_FLASH_DEVICES };
#else
const qspi_flash_device_t _flash_devices_arr[] = { GD25Q16C, MX25R6435F };
#endif
enum
{
FLASH_DEVICE_COUNT = arrcount(_flash_devices_arr)
};
enum { FLASH_DEVICE_COUNT = arrcount(_flash_devices_arr) };
const qspi_flash_device_t* _flash_dev = NULL;
@@ -85,12 +57,12 @@ static bool fal_qspi_verify (uint32_t addr, void const * buf, uint32_t len);
static uint8_t _cache_buffer[FLASH_CACHE_SIZE] __attribute__((aligned(4)));
static flash_cache_t _cache = {
.erase = fal_qspi_erase,
.program = fal_qspi_program,
.read = fal_qspi_read,
.verify = fal_qspi_verify,
.erase = fal_qspi_erase,
.program = fal_qspi_program,
.read = fal_qspi_read,
.verify = fal_qspi_verify,
.cache_addr = FLASH_CACHE_INVALID_ADDR,
.cache_buf = _cache_buffer
.cache_buf = _cache_buffer
};
static SemaphoreHandle_t _qspi_mutex;
@@ -155,38 +127,25 @@ void flash_qspi_init (void)
.sck_pin = g_ADigitalPinMap[PIN_QSPI_SCK],
.csn_pin = g_ADigitalPinMap[PIN_QSPI_CS],
.io0_pin = g_ADigitalPinMap[PIN_QSPI_DATA0],
.io1_pin = NRF_QSPI_PIN_NOT_CONNECTED,
.io2_pin = NRF_QSPI_PIN_NOT_CONNECTED,
.io3_pin = NRF_QSPI_PIN_NOT_CONNECTED,
.io1_pin = g_ADigitalPinMap[PIN_QSPI_DATA1],
.io2_pin = g_ADigitalPinMap[PIN_QSPI_DATA2],
.io3_pin = g_ADigitalPinMap[PIN_QSPI_DATA3],
},
.prot_if = {
.readoc = NRF_QSPI_READOC_FASTREAD,
.writeoc = NRF_QSPI_WRITEOC_PP,
.readoc = NRF_QSPI_READOC_READ4IO,
.writeoc = NRF_QSPI_WRITEOC_PP4O,
.addrmode = NRF_QSPI_ADDRMODE_24BIT,
.dpmconfig = false
},
.phy_if = {
.sck_freq = NRF_QSPI_FREQ_32MDIV1,
.sck_delay = 1, // min time CS must stay high before going low again. in unit of 62.5 ns
.sck_freq = NRF_QSPI_FREQ_32MDIV16, // start with low 2 Mhz speed
.sck_delay = 10, // min time CS must stay high before going low again. in unit of 62.5 ns
.spi_mode = NRF_QSPI_MODE_0,
.dpmen = false
},
.irq_priority = 7,
};
#if QSPI_FLASH_MODE == 2
qspi_cfg.pins.io1_pin = g_ADigitalPinMap[PIN_QSPI_DATA1];
qspi_cfg.prot_if.readoc = NRF_QSPI_READOC_READ2IO;
qspi_cfg.prot_if.writeoc = NRF_QSPI_WRITEOC_PP2O;
#elif QSPI_FLASH_MODE == 4
qspi_cfg.pins.io1_pin = g_ADigitalPinMap[PIN_QSPI_DATA1];
qspi_cfg.pins.io2_pin = g_ADigitalPinMap[PIN_QSPI_DATA2];
qspi_cfg.pins.io3_pin = g_ADigitalPinMap[PIN_QSPI_DATA3];
qspi_cfg.prot_if.readoc = NRF_QSPI_READOC_READ4IO;
qspi_cfg.prot_if.writeoc = NRF_QSPI_WRITEOC_PP4O;
#endif
// No callback for blocking API
nrfx_qspi_init(&qspi_cfg, NULL, NULL);
@@ -212,25 +171,23 @@ void flash_qspi_init (void)
NRFX_DELAY_US(100); // wait for flash device to reset
// Send (Read ID + 3 dummy bytes) + Receive 2 bytes of (Manufacture + Device ID)
uint8_t dummy[6] = { 0 };
uint8_t id_resp[6] = { 0 };
cinstr_cfg.opcode = QSPI_CMD_READID;
uint8_t dummy[5] = { 0 };
uint8_t id_resp[5] = { 0 };
cinstr_cfg.opcode = QSPI_CMD_REMS;
cinstr_cfg.length = 6;
// Bug with -nrf_qspi_cinstrdata_get() didn't combine data.
// https://devzone.nordicsemi.com/f/nordic-q-a/38540/bug-nrf_qspi_cinstrdata_get-didn-t-collect-data-from-both-cinstrdat1-and-cinstrdat0
nrfx_qspi_cinstr_xfer(&cinstr_cfg, dummy, id_resp);
// Due to the bug, we collect data manually
uint8_t dev_id = (uint8_t) NRF_QSPI->CINSTRDAT1;
uint8_t mfgr_id = (uint8_t) ( NRF_QSPI->CINSTRDAT0 >> 24);
uint8_t const mfgr_id = id_resp[3];
uint8_t const dev_id = id_resp[4];
// quick hack
//printf("qspi mfgr id : 0x%02X\n", mfgr_id);
//printf("qspi device id: 0x%02X\n", dev_id);
// printf("qspi mfgr id : 0x%02X\n", mfgr_id);
// printf("qspi device id: 0x%02X\n", dev_id);
// PRINT_BUFFER(id_resp, sizeof(id_resp));
// Look up the flash device in supported array
for ( int i = 0; i < FLASH_DEVICE_COUNT; i++ )
for ( uint32_t i = 0; i < FLASH_DEVICE_COUNT; i++ )
{
// Match ID
if ( _flash_devices_arr[i].manufacturer_id == mfgr_id && _flash_devices_arr[i].device_id == dev_id )
@@ -242,13 +199,20 @@ void flash_qspi_init (void)
if ( _flash_dev )
{
// Enable quad mode if needed
#if QSPI_FLASH_MODE == 4
// Enable quad mode
cinstr_cfg.opcode = QSPI_CMD_WRSR;
cinstr_cfg.length = 3;
cinstr_cfg.wipwait = cinstr_cfg.wren = true;
nrfx_qspi_cinstr_xfer(&cinstr_cfg, &_flash_dev->status_quad_enable, NULL);
#endif
// Speed up frequency
nrf_qspi_phy_conf_t phy_if = {
.sck_freq = _flash_dev->freq,
.sck_delay = 10,
.spi_mode = NRF_QSPI_MODE_0,
.dpmen = false
};
nrf_qspi_ifconfig1_set(NRF_QSPI, &phy_if);
}
// create mutex
@@ -284,6 +248,6 @@ static bool fal_qspi_verify (uint32_t addr, void const * buf, uint32_t len)
return false;
}
#endif // valid pin
#endif // external flash
#endif // nrf52840
+2 -2
View File
@@ -1,8 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018, hathach for Adafruit
* Copyright (c) 2019 Ha Thach 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
@@ -22,6 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifdef NRF52840_XXAA
#include "Adafruit_USBDev_CDC.h"
+1 -1
View File
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018, hathach for Adafruit
* Copyright (c) 2019 Ha Thach 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
+1 -1
View File
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 hathach for Adafruit Industries
* Copyright (c) 2019 Ha Thach 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
+1 -1
View File
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 hathach for Adafruit Industries
* Copyright (c) 2019 Ha Thach 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
@@ -107,7 +107,7 @@ bool tud_msc_set_sense(uint8_t lun, uint8_t sense_key, uint8_t add_sense_code, u
* \retval negative Indicate error e.g reading disk I/O. tinyusb will \b STALL the corresponding
* endpoint and return failed status in command status wrapper phase.
*/
int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize);
ATTR_WEAK int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize);
/**
* Callback invoked when received \ref SCSI_CMD_WRITE_10 command
@@ -126,10 +126,10 @@ int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buf
* \retval negative Indicate error writing disk I/O. Tinyusb will \b STALL the corresponding
* endpoint and return failed status in command status wrapper phase.
*/
int32_t tud_msc_write10_cb (uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize);
ATTR_WEAK int32_t tud_msc_write10_cb (uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize);
// Invoked to determine the disk size
void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size);
ATTR_WEAK void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size);
/**
* Callback invoked when received an SCSI command not in built-in list below.
@@ -148,7 +148,7 @@ void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_siz
* - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE
* - READ10 and WRITE10 has their own callbacks
*/
int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize);
ATTR_WEAK int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize);
/*------------- Optional callbacks : Could be used by application to free up resources -------------*/
-2
View File
@@ -59,8 +59,6 @@
//------------- CLASS -------------//
#define CFG_TUD_CDC 1
// disable msc for feather nrf52840 for now until have a more stable QSPI driver
#define CFG_TUD_MSC 1
#define CFG_TUD_HID 0
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 hathach for Adafruit Industries
* Copyright (c) 2019 Ha Thach 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
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 hathach for Adafruit Industries
* Copyright (c) 2019 Ha Thach 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
@@ -78,9 +78,7 @@ void initVariant()
pinMode(PIN_LED2, OUTPUT);
ledOff(PIN_LED2);
#if 0 // disable QSPI flash for now
// Init External Flash
flash_qspi_init();
#endif
}
+3 -1
View File
@@ -133,9 +133,11 @@ static const uint8_t SCK = PIN_SPI_SCK ;
#define PIN_QSPI_DATA3 32
// On-board QSPI Flash
// If EXTERNAL_FLASH_DEVICES is not defined, all supported devices will be used
#define EXTERNAL_FLASH_DEVICES GD25Q16C
#define USB_MSC_BLOCK_SIZE 512
#define USB_MSC_BLOCK_COUNT ((2*1024*1024) / USB_MSC_BLOCK_SIZE)
#ifdef __cplusplus
}
#endif
-2
View File
@@ -126,10 +126,8 @@ static const uint8_t SCK = PIN_SPI_SCK ;
#define PIN_QSPI_DATA3 23
// On-board QSPI Flash
// If EXTERNAL_FLASH_DEVICES is not defined, all supported devices will be used
#define EXTERNAL_FLASH_DEVICES MX25R6435F
#define USB_MSC_BLOCK_SIZE 512
#define USB_MSC_BLOCK_COUNT ((8*1024*1024) / USB_MSC_BLOCK_SIZE)