1 // Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <string.h>
16 #include "diskio_impl.h"
17 #include "ffconf.h"
18 #include "ff.h"
19 #include "esp_log.h"
20 #include "diskio_rawflash.h"
21 #include "esp_compiler.h"
22
23 static const char* TAG = "diskio_rawflash";
24
25 const esp_partition_t* ff_raw_handles[FF_VOLUMES];
26
27
ff_raw_initialize(BYTE pdrv)28 DSTATUS ff_raw_initialize (BYTE pdrv)
29 {
30 return 0;
31 }
32
ff_raw_status(BYTE pdrv)33 DSTATUS ff_raw_status (BYTE pdrv)
34 {
35 return 0;
36 }
37
ff_raw_read(BYTE pdrv,BYTE * buff,DWORD sector,UINT count)38 DRESULT ff_raw_read (BYTE pdrv, BYTE *buff, DWORD sector, UINT count)
39 {
40 ESP_LOGV(TAG, "ff_raw_read - pdrv=%i, sector=%i, count=%in", (unsigned int)pdrv, (unsigned int)sector, (unsigned int)count);
41 const esp_partition_t* part = ff_raw_handles[pdrv];
42 assert(part);
43 esp_err_t err = esp_partition_read(part, sector * SPI_FLASH_SEC_SIZE, buff, count * SPI_FLASH_SEC_SIZE);
44 if (unlikely(err != ESP_OK)) {
45 ESP_LOGE(TAG, "esp_partition_read failed (0x%x)", err);
46 return RES_ERROR;
47 }
48 return RES_OK;
49 }
50
51
ff_raw_write(BYTE pdrv,const BYTE * buff,DWORD sector,UINT count)52 DRESULT ff_raw_write (BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
53 {
54 return RES_ERROR;
55 }
56
ff_raw_ioctl(BYTE pdrv,BYTE cmd,void * buff)57 DRESULT ff_raw_ioctl (BYTE pdrv, BYTE cmd, void *buff)
58 {
59 const esp_partition_t* part = ff_raw_handles[pdrv];
60 ESP_LOGV(TAG, "ff_raw_ioctl: cmd=%in", cmd);
61 assert(part);
62 switch (cmd) {
63 case CTRL_SYNC:
64 return RES_OK;
65 case GET_SECTOR_COUNT:
66 *((DWORD *) buff) = part->size / SPI_FLASH_SEC_SIZE;
67 return RES_OK;
68 case GET_SECTOR_SIZE:
69 *((WORD *) buff) = SPI_FLASH_SEC_SIZE;
70 return RES_OK;
71 case GET_BLOCK_SIZE:
72 return RES_ERROR;
73 }
74 return RES_ERROR;
75 }
76
77
ff_diskio_register_raw_partition(BYTE pdrv,const esp_partition_t * part_handle)78 esp_err_t ff_diskio_register_raw_partition(BYTE pdrv, const esp_partition_t* part_handle)
79 {
80 if (pdrv >= FF_VOLUMES) {
81 return ESP_ERR_INVALID_ARG;
82 }
83 static const ff_diskio_impl_t raw_impl = {
84 .init = &ff_raw_initialize,
85 .status = &ff_raw_status,
86 .read = &ff_raw_read,
87 .write = &ff_raw_write,
88 .ioctl = &ff_raw_ioctl
89 };
90 ff_diskio_register(pdrv, &raw_impl);
91 ff_raw_handles[pdrv] = part_handle;
92 return ESP_OK;
93
94 }
95
96
ff_diskio_get_pdrv_raw(const esp_partition_t * part_handle)97 BYTE ff_diskio_get_pdrv_raw(const esp_partition_t* part_handle)
98 {
99 for (int i = 0; i < FF_VOLUMES; i++) {
100 if (part_handle == ff_raw_handles[i]) {
101 return i;
102 }
103 }
104 return 0xff;
105 }
106