8000 atmel-samd: Rework SPI flash defines for clarity. · sparkfun/circuitpython@e1a6f6d · GitHub
[go: up one dir, main page]

Skip to content

Commit e1a6f6d

Browse files
committed
atmel-samd: Rework SPI flash defines for clarity.
Also add sector protection support for the SAMD Xplained board.
1 parent 7ea76e0 commit e1a6f6d

File tree

11 files changed

+205
-83
lines changed

11 files changed

+205
-83
lines changed

atmel-samd/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ INC += $(addprefix -Iasf/sam0/,\
7373
utils/preprocessor \
7474
)
7575
INC += -Iasf/thirdparty/CMSIS/Include
76+
INC += -Iboards
7677
INC += -Iboards/$(BOARD)/
7778
INC += -Ifreetouch/
7879
INC += -I$(BUILD)

atmel-samd/access_vfs.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ bool vfs_unload(bool unload)
126126
//!
127127
Ctrl_status vfs_usb_read_10(uint32_t addr, volatile uint16_t nb_sector)
128128
{
129-
uint8_t sector_buffer[FLASH_BLOCK_SIZE];
129+
uint8_t sector_buffer[FILESYSTEM_BLOCK_SIZE];
130130
for (uint16_t sector = 0; sector < nb_sector; sector++) {
131131
DRESULT result = disk_read(VFS_INDEX, sector_buffer, addr + sector, 1);
132132
if (result == RES_PARERR) {
@@ -135,7 +135,7 @@ Ctrl_status vfs_usb_read_10(uint32_t addr, volatile uint16_t nb_sector)
135135
if (result == RES_ERROR) {
136136
return CTRL_FAIL;
137137
}
138-
if (!udi_msc_trans_block(true, sector_buffer, FLASH_BLOCK_SIZE, NULL)) {
138+
if (!udi_msc_trans_block(true, sector_buffer, FILESYSTEM_BLOCK_SIZE, NULL)) {
139139
return CTRL_FAIL; // transfer aborted
140140
}
141141
}
@@ -155,9 +155,9 @@ Ctrl_status vfs_usb_read_10(uint32_t addr, volatile uint16_t nb_sector)
155155
//!
156156
Ctrl_status vfs_usb_write_10(uint32_t addr, volatile uint16_t nb_sector)
157157
{
158-
uint8_t sector_buffer[FLASH_BLOCK_SIZE];
158+
uint8_t sector_buffer[FILESYSTEM_BLOCK_SIZE];
159159
for (uint16_t sector = 0; sector < nb_sector; sector++) {
160-
if (!udi_msc_trans_block(false, sector_buffer, FLASH_BLOCK_SIZE, NULL)) {
160+
if (!udi_msc_trans_block(false, sector_buffer, FILESYSTEM_BLOCK_SIZE, NULL)) {
161161
return CTRL_FAIL; // transfer aborted
162162
}
163163
uint32_t sector_address = addr + sector;
@@ -174,13 +174,13 @@ Ctrl_status vfs_usb_write_10(uint32_t addr, volatile uint16_t nb_sector)
174174
volatile uint16_t x = addr;
175175
(void) x;
176176
#if _MAX_SS != _MIN_SS
177-
if (vfs->ssize == FLASH_BLOCK_SIZE) {
177+
if (vfs->ssize == FILESYSTEM_BLOCK_SIZE) {
178178
#else
179179
// The compiler can optimize this away.
180-
if (_MAX_SS == FLASH_BLOCK_SIZE) {
180+
if (_MAX_SS == FILESYSTEM_BLOCK_SIZE) {
181181
#endif
182182
if (sector_address == vfs->fatfs.winsect && sector_address > 0) {
183-
memcpy(vfs->fatfs.win, sector_buffer, FLASH_BLOCK_SIZE);
183+
memcpy(vfs->fatfs.win, sector_buffer, FILESYSTEM_BLOCK_SIZE);
184184
}
185185
}
186186
}

atmel-samd/boards/circuitplayground_express/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@
3838

3939
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000)
4040

41+
#include "flash_S25FL216K.h"
42+
4143
#define CALIBRATE_CRYSTALLESS 1

atmel-samd/boards/feather_m0_express/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@
2626
#include "spi_flash.h"
2727

2828
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000)
29+
30+
#include "flash_S25FL216K.h"

atmel-samd/boards/flash_AT25DF081A.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_AT25DF081A_H__
28+
#define __MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_AT25DF081A_H__
29+
30+
// Settings for the Adesto Tech AT25DF081A 1MiB SPI flash. Its on the SAMD21
31+
// Xplained board.
32+
// Datasheet: https://www.adestotech.com/wp-content/uploads/doc8715.pdf
33+
34+
// The total flash size in bytes.
35+
#define SPI_FLASH_TOTAL_SIZE (1 << 20) // 2 MiB
36+
37+
// The size of the smallest erase unit thats erased with command 0x20.
38+
#define SPI_FLASH_ERASE_SIZE (1 << 12) // 4 KiB
39+
40+
// The size of a page that is programmed with page program command 0x02.
41+
#define SPI_FLASH_PAGE_SIZE (256) // 256 bytes
42+
43+
// These are the first three response bytes to the JEDEC ID command 0x9f that is
44+
// used to confirm we're talking to the flash we expect. The data sheet calls
45+
// them something else so we just match it byte for byte.
46+
#define SPI_FLASH_JEDEC_MANUFACTURER 0x1F
47+
#define SPI_FLASH_JEDEC_MEMORY_TYPE 0x45
48+
#define SPI_FLASH_JEDEC_CAPACITY 0x01
49+
50+
#define SPI_FLASH_SECTOR_PROTECTION
51+
52+
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_AT25DF081A_H__

atmel-samd/boards/flash_S25FL216K.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_S25FL216K_H__
28+
#define __MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_S25FL216K_H__
29+
30+
// Settings for the Cypress (was Spansion) S25FL216K 2MiB SPI flash.
31+
// Datasheet: http://www.cypress.com/file/197346/download
32+
33+
// The total flash size in bytes.
34+
#define SPI_FLASH_TOTAL_SIZE (1 << 21) // 2 MiB
35+
36+
// The size of the smallest erase unit thats erased with command 0x20.
37+
#define SPI_FLASH_ERASE_SIZE (1 << 12) // 4 KiB
38+
39+
// The size of a page that is programmed with page program command 0x02.
40+
#define SPI_FLASH_PAGE_SIZE (256) // 256 bytes
41+
42+
// These are the first three response bytes to the JEDEC ID command 0x9f that is
43+
// used to confirm we're talking to the flash we expect.
44+
#define SPI_FLASH_JEDEC_MANUFACTURER 0x01
45+
#define SPI_FLASH_JEDEC_MEMORY_TYPE 0x40
46+
#define SPI_FLASH_JEDEC_CAPACITY 0x15
47+
48+
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_S25FL216K_H__

atmel-samd/boards/metro_m0_express/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@
2828
#include "spi_flash.h"
2929

3030
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000)
31+
32+
#include "flash_S25FL216K.h"

atmel-samd/internal_flash.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
#define INTERNAL_FLASH_MEM_SEG1_START_ADDR (0x00040000 - TOTAL_INTERNAL_FLASH_SIZE)
4545
#define INTERNAL_FLASH_PART1_START_BLOCK (0x1)
46-
#define INTERNAL_FLASH_PART1_NUM_BLOCKS (TOTAL_INTERNAL_FLASH_SIZE / INTERNAL_FLASH_BLOCK_SIZE)
46+
#define INTERNAL_FLASH_PART1_NUM_BLOCKS (TOTAL_INTERNAL_FLASH_SIZE / FILESYSTEM_BLOCK_SIZE)
4747

4848
void internal_flash_init(void) {
4949
// Activity LED for flash writes.
@@ -58,7 +58,7 @@ void internal_flash_init(void) {
5858
}
5959

6060
uint32_t internal_flash_get_block_size(void) {
61-
return INTERNAL_FLASH_BLOCK_SIZE;
61+
return FILESYSTEM_BLOCK_SIZE;
6262
}
6363

6464
uint32_t internal_flash_get_block_count(void) {
@@ -112,7 +112,7 @@ static int32_t convert_block_to_flash_addr(uint32_t block) {
112112
if (INTERNAL_FLASH_PART1_START_BLOCK <= block && block < INTERNAL_FLASH_PART1_START_BLOCK + INTERNAL_FLASH_PART1_NUM_BLOCKS) {
113113
// a block in partition 1
114114
block -= INTERNAL_FLASH_PART1_START_BLOCK;
115-
return INTERNAL_FLASH_MEM_SEG1_START_ADDR + block * INTERNAL_FLASH_BLOCK_SIZE;
115+
return INTERNAL_FLASH_MEM_SEG1_START_ADDR + block * FILESYSTEM_BLOCK_SIZE;
116116
}
117117
// bad block
118118
return -1;
@@ -147,7 +147,7 @@ bool internal_flash_read_block(uint8_t *dest, uint32_t block) {
147147
enum status_code error_code;
148148
// A block is made up of multiple pages. Read each page
149149
// sequentially.
150-
for (int i = 0; i < INTERNAL_FLASH_BLOCK_SIZE / NVMCTRL_PAGE_SIZE; i++) {
150+
for (int i = 0; i < FILESYSTEM_BLOCK_SIZE / NVMCTRL_PAGE_SIZE; i++) {
151151
do
152152
{
153153
error_code = nvm_read_buffer(src + i * NVMCTRL_PAGE_SIZE,
@@ -195,7 +195,7 @@ bool internal_flash_write_block(const uint8_t *src, uint32_t block) {
195195

196196
// A block is made up of multiple pages. Write each page
197197
// sequentially.
198-
for (int i = 0; i < INTERNAL_FLASH_BLOCK_SIZE / NVMCTRL_PAGE_SIZE; i++) {
198+
for (int i = 0; i < FILESYSTEM_BLOCK_SIZE / NVMCTRL_PAGE_SIZE; i++) {
199199
do
200200
{
201201
error_code = nvm_write_buffer(dest + i * NVMCTRL_PAGE_SIZE,
@@ -216,7 +216,7 @@ bool internal_flash_write_block(const uint8_t *src, uint32_t block) {
216216

217217
mp_uint_t internal_flash_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {
218218
for (size_t i = 0; i < num_blocks; i++) {
219-
if (!internal_flash_read_block(dest + i * INTERNAL_FLASH_BLOCK_SIZE, block_num + i)) {
219+
if (!internal_flash_read_block(dest + i * FILESYSTEM_BLOCK_SIZE, block_num + i)) {
220220
return 1; // error
221221
}
222222
}
@@ -225,7 +225,7 @@ mp_uint_t internal_flash_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t
225225

226226
mp_uint_t internal_flash_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks) {
227227
for (size_t i = 0; i < num_blocks; i++) {
228-
if (!internal_flash_write_block(src + i * INTERNAL_FLASH_BLOCK_SIZE, block_num + i)) {
228+
if (!internal_flash_write_block(src + i * FILESYSTEM_BLOCK_SIZE, block_num + i)) {
229229
return 1; // error
230230
}
231231
}
@@ -251,15 +251,15 @@ STATIC mp_obj_t internal_flash_obj_make_new(const mp_obj_type_t *type, size_t n_
251251
STATIC mp_obj_t internal_flash_obj_readblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
252252
mp_buffer_info_t bufinfo;
253253
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE);
254-
mp_uint_t ret = internal_flash_read_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FLASH_BLOCK_SIZE);
254+
mp_uint_t ret = internal_flash_read_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FILESYSTEM_BLOCK_SIZE);
255255
return MP_OBJ_NEW_SMALL_INT(ret);
256256
}
257257
STATIC MP_DEFINE_CONST_FUN_OBJ_3(internal_flash_obj_readblocks_obj, internal_flash_obj_readblocks);
258258

259259
STATIC mp_obj_t internal_flash_obj_writeblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
260260
mp_buffer_info_t bufinfo;
261261
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
262-
mp_uint_t ret = internal_flash_write_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FLASH_BLOCK_SIZE);
262+
mp_uint_t ret = internal_flash_write_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FILESYSTEM_BLOCK_SIZE);
263263
return MP_OBJ_NEW_SMALL_INT(ret);
264264
}
265265
STATIC MP_DEFINE_CONST_FUN_OBJ_3(internal_flash_obj_writeblocks_obj, internal_flash_obj_writeblocks);

atmel-samd/internal_flash.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232

3333
#define FLASH_ROOT_POINTERS
3434

35-
#define INTERNAL_FLASH_BLOCK_SIZE (512)
36-
3735
#define INTERNAL_FLASH_SYSTICK_MASK (0x1ff) // 512ms
3836
#define INTERNAL_FLASH_IDLE_TICK(tick) (((tick) & INTERNAL_FLASH_SYSTICK_MASK) == 2)
3937

atmel-samd/mpconfigport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
// size.
7777
// #define MICROPY_FATFS_MAX_SS (4096)
7878

79-
#define FLASH_BLOCK_SIZE (512)
79+
#define FILESYSTEM_BLOCK_SIZE (512)
8080

8181
#define MICROPY_VFS_FAT (1)
8282
#define MICROPY_PY_MACHINE (1)

0 commit comments

Comments
 (0)
0