A first try to write a block device wrapper for the BSP_DISCO_F746NG. I can read and write blocks, but I haven't gotten it to work with FATFileSystem yet.
I'm trying to get Block Device to work with the DISCO_F746NG boards built in SD-card-reader using the BSP_DISCO_F746NG library.
I used the HeapBlockDevice as template, and I have gotten read and program to work when I just try to read or program one block and read it back, but I haven't gotten FATFileSystem to work with this class yet. I'm kind of stuck here right now, so any help would be very much appreciated!
BD_DISCO_F746NG.cpp@0:3f8692709f00, 2017-12-14 (annotated)
- Committer:
- Lightsource
- Date:
- Thu Dec 14 19:05:32 2017 +0000
- Revision:
- 0:3f8692709f00
Try to wrap the BSP SD-card functions for blockdevice class, although not functional yet.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Lightsource | 0:3f8692709f00 | 1 | #include "BD_DISCO_F746NG.h" |
Lightsource | 0:3f8692709f00 | 2 | |
Lightsource | 0:3f8692709f00 | 3 | |
Lightsource | 0:3f8692709f00 | 4 | BD_DISCO_F746NG::BD_DISCO_F746NG(bd_size_t block) |
Lightsource | 0:3f8692709f00 | 5 | : _read_size(block), _program_size(block), _erase_size(block) |
Lightsource | 0:3f8692709f00 | 6 | , _blocks(0) |
Lightsource | 0:3f8692709f00 | 7 | { |
Lightsource | 0:3f8692709f00 | 8 | //MBED_ASSERT(_count * _erase_size == size); |
Lightsource | 0:3f8692709f00 | 9 | } |
Lightsource | 0:3f8692709f00 | 10 | |
Lightsource | 0:3f8692709f00 | 11 | BD_DISCO_F746NG::BD_DISCO_F746NG(bd_size_t read, bd_size_t program, bd_size_t erase) |
Lightsource | 0:3f8692709f00 | 12 | : _read_size(read), _program_size(program), _erase_size(erase) |
Lightsource | 0:3f8692709f00 | 13 | , _blocks(0) |
Lightsource | 0:3f8692709f00 | 14 | { |
Lightsource | 0:3f8692709f00 | 15 | //MBED_ASSERT(_count * _erase_size == size); |
Lightsource | 0:3f8692709f00 | 16 | Timeout = 100; |
Lightsource | 0:3f8692709f00 | 17 | } |
Lightsource | 0:3f8692709f00 | 18 | |
Lightsource | 0:3f8692709f00 | 19 | BD_DISCO_F746NG::~BD_DISCO_F746NG() |
Lightsource | 0:3f8692709f00 | 20 | { |
Lightsource | 0:3f8692709f00 | 21 | BSP_SD_DeInit(); |
Lightsource | 0:3f8692709f00 | 22 | /* |
Lightsource | 0:3f8692709f00 | 23 | if (_blocks) { |
Lightsource | 0:3f8692709f00 | 24 | for (size_t i = 0; i < _count; i++) { |
Lightsource | 0:3f8692709f00 | 25 | free(_blocks[i]); |
Lightsource | 0:3f8692709f00 | 26 | } |
Lightsource | 0:3f8692709f00 | 27 | |
Lightsource | 0:3f8692709f00 | 28 | delete[] _blocks; |
Lightsource | 0:3f8692709f00 | 29 | _blocks = 0; |
Lightsource | 0:3f8692709f00 | 30 | } |
Lightsource | 0:3f8692709f00 | 31 | */ |
Lightsource | 0:3f8692709f00 | 32 | } |
Lightsource | 0:3f8692709f00 | 33 | |
Lightsource | 0:3f8692709f00 | 34 | int BD_DISCO_F746NG::init() |
Lightsource | 0:3f8692709f00 | 35 | { |
Lightsource | 0:3f8692709f00 | 36 | Timeout = 100; |
Lightsource | 0:3f8692709f00 | 37 | bool result; |
Lightsource | 0:3f8692709f00 | 38 | |
Lightsource | 0:3f8692709f00 | 39 | _SD_state = BSP_SD_Init(); |
Lightsource | 0:3f8692709f00 | 40 | |
Lightsource | 0:3f8692709f00 | 41 | if(_SD_state != MSD_OK){ |
Lightsource | 0:3f8692709f00 | 42 | if(_SD_state == MSD_ERROR_SD_NOT_PRESENT){ |
Lightsource | 0:3f8692709f00 | 43 | return BD_ERROR_DEVICE_ERROR; |
Lightsource | 0:3f8692709f00 | 44 | } else { |
Lightsource | 0:3f8692709f00 | 45 | return BD_ERROR_DEVICE_ERROR; |
Lightsource | 0:3f8692709f00 | 46 | } |
Lightsource | 0:3f8692709f00 | 47 | } else { |
Lightsource | 0:3f8692709f00 | 48 | BSP_SD_GetCardInfo(&_currentCardInfo); |
Lightsource | 0:3f8692709f00 | 49 | //_blocksize = _currentCardInfo.BlockSize; |
Lightsource | 0:3f8692709f00 | 50 | _blocksize = 512; |
Lightsource | 0:3f8692709f00 | 51 | return BD_ERROR_OK; |
Lightsource | 0:3f8692709f00 | 52 | } |
Lightsource | 0:3f8692709f00 | 53 | /* |
Lightsource | 0:3f8692709f00 | 54 | if (!_blocks) { |
Lightsource | 0:3f8692709f00 | 55 | _blocks = new uint8_t*[_count]; |
Lightsource | 0:3f8692709f00 | 56 | for (size_t i = 0; i < _count; i++) { |
Lightsource | 0:3f8692709f00 | 57 | _blocks[i] = 0; |
Lightsource | 0:3f8692709f00 | 58 | } |
Lightsource | 0:3f8692709f00 | 59 | } |
Lightsource | 0:3f8692709f00 | 60 | */ |
Lightsource | 0:3f8692709f00 | 61 | // return BD_ERROR_OK; |
Lightsource | 0:3f8692709f00 | 62 | } |
Lightsource | 0:3f8692709f00 | 63 | |
Lightsource | 0:3f8692709f00 | 64 | int BD_DISCO_F746NG::deinit() |
Lightsource | 0:3f8692709f00 | 65 | { |
Lightsource | 0:3f8692709f00 | 66 | return BSP_SD_DeInit(); |
Lightsource | 0:3f8692709f00 | 67 | /* |
Lightsource | 0:3f8692709f00 | 68 | MBED_ASSERT(_blocks != NULL); |
Lightsource | 0:3f8692709f00 | 69 | // Memory is lazily cleaned up in destructor to allow |
Lightsource | 0:3f8692709f00 | 70 | // data to live across de/reinitialization |
Lightsource | 0:3f8692709f00 | 71 | */ |
Lightsource | 0:3f8692709f00 | 72 | // return BD_ERROR_OK; |
Lightsource | 0:3f8692709f00 | 73 | } |
Lightsource | 0:3f8692709f00 | 74 | |
Lightsource | 0:3f8692709f00 | 75 | bd_size_t BD_DISCO_F746NG::get_read_size() const |
Lightsource | 0:3f8692709f00 | 76 | { |
Lightsource | 0:3f8692709f00 | 77 | //MBED_ASSERT(_blocks != NULL); |
Lightsource | 0:3f8692709f00 | 78 | return _read_size; |
Lightsource | 0:3f8692709f00 | 79 | } |
Lightsource | 0:3f8692709f00 | 80 | |
Lightsource | 0:3f8692709f00 | 81 | bd_size_t BD_DISCO_F746NG::get_program_size() const |
Lightsource | 0:3f8692709f00 | 82 | { |
Lightsource | 0:3f8692709f00 | 83 | //MBED_ASSERT(_blocks != NULL); |
Lightsource | 0:3f8692709f00 | 84 | return _program_size; |
Lightsource | 0:3f8692709f00 | 85 | } |
Lightsource | 0:3f8692709f00 | 86 | |
Lightsource | 0:3f8692709f00 | 87 | bd_size_t BD_DISCO_F746NG::get_erase_size() const |
Lightsource | 0:3f8692709f00 | 88 | { |
Lightsource | 0:3f8692709f00 | 89 | //MBED_ASSERT(_blocks != NULL); |
Lightsource | 0:3f8692709f00 | 90 | return _erase_size; |
Lightsource | 0:3f8692709f00 | 91 | } |
Lightsource | 0:3f8692709f00 | 92 | |
Lightsource | 0:3f8692709f00 | 93 | bd_size_t BD_DISCO_F746NG::size() const |
Lightsource | 0:3f8692709f00 | 94 | { |
Lightsource | 0:3f8692709f00 | 95 | //MBED_ASSERT(_blocks != NULL); |
Lightsource | 0:3f8692709f00 | 96 | //return (_currentCardInfo.BlockNbr*2) * _blocksize; |
Lightsource | 0:3f8692709f00 | 97 | return 128 * _blocksize; |
Lightsource | 0:3f8692709f00 | 98 | } |
Lightsource | 0:3f8692709f00 | 99 | |
Lightsource | 0:3f8692709f00 | 100 | int BD_DISCO_F746NG::read(void *b, bd_addr_t addr, bd_size_t size) |
Lightsource | 0:3f8692709f00 | 101 | { |
Lightsource | 0:3f8692709f00 | 102 | int result; |
Lightsource | 0:3f8692709f00 | 103 | uint32_t nblocks = (uint32_t)size / _blocksize; |
Lightsource | 0:3f8692709f00 | 104 | result = BSP_SD_ReadBlocks((uint32_t *)b, (uint32_t)addr, nblocks, Timeout); |
Lightsource | 0:3f8692709f00 | 105 | return result; |
Lightsource | 0:3f8692709f00 | 106 | } |
Lightsource | 0:3f8692709f00 | 107 | |
Lightsource | 0:3f8692709f00 | 108 | int BD_DISCO_F746NG::program(const void *b, bd_addr_t addr, bd_size_t size) |
Lightsource | 0:3f8692709f00 | 109 | { |
Lightsource | 0:3f8692709f00 | 110 | int result; |
Lightsource | 0:3f8692709f00 | 111 | uint32_t nblocks = (uint32_t)size / _blocksize; |
Lightsource | 0:3f8692709f00 | 112 | result = BSP_SD_WriteBlocks((uint32_t *)b, (uint32_t)addr, nblocks, Timeout); |
Lightsource | 0:3f8692709f00 | 113 | return result; |
Lightsource | 0:3f8692709f00 | 114 | } |
Lightsource | 0:3f8692709f00 | 115 | |
Lightsource | 0:3f8692709f00 | 116 | int BD_DISCO_F746NG::erase(bd_addr_t addr, bd_size_t size) |
Lightsource | 0:3f8692709f00 | 117 | { |
Lightsource | 0:3f8692709f00 | 118 | int result; |
Lightsource | 0:3f8692709f00 | 119 | uint32_t StartAddr = addr; |
Lightsource | 0:3f8692709f00 | 120 | uint32_t EndAddr = StartAddr + size; |
Lightsource | 0:3f8692709f00 | 121 | result = BSP_SD_Erase(StartAddr, EndAddr); |
Lightsource | 0:3f8692709f00 | 122 | |
Lightsource | 0:3f8692709f00 | 123 | return result; |
Lightsource | 0:3f8692709f00 | 124 | } |