HSP Platform firmware evaluating ECG data and hearth rate over PPG data.

Dependencies:   max32630fthr Adafruit_FeatherOLED USBDevice

Committer:
phonemacro
Date:
Thu Mar 25 21:59:17 2021 +0000
Revision:
4:682a4ebb995a
Parent:
1:f60eafbf009a
fix compile errors from mbed OS changes.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gmehmet 1:f60eafbf009a 1 /* mbed Microcontroller Library
gmehmet 1:f60eafbf009a 2 * Copyright (c) 2006-2012 ARM Limited
gmehmet 1:f60eafbf009a 3 *
gmehmet 1:f60eafbf009a 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
gmehmet 1:f60eafbf009a 5 * of this software and associated documentation files (the "Software"), to deal
gmehmet 1:f60eafbf009a 6 * in the Software without restriction, including without limitation the rights
gmehmet 1:f60eafbf009a 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
gmehmet 1:f60eafbf009a 8 * copies of the Software, and to permit persons to whom the Software is
gmehmet 1:f60eafbf009a 9 * furnished to do so, subject to the following conditions:
gmehmet 1:f60eafbf009a 10 *
gmehmet 1:f60eafbf009a 11 * The above copyright notice and this permission notice shall be included in
gmehmet 1:f60eafbf009a 12 * all copies or substantial portions of the Software.
gmehmet 1:f60eafbf009a 13 *
gmehmet 1:f60eafbf009a 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
gmehmet 1:f60eafbf009a 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
gmehmet 1:f60eafbf009a 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
gmehmet 1:f60eafbf009a 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
gmehmet 1:f60eafbf009a 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
gmehmet 1:f60eafbf009a 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
gmehmet 1:f60eafbf009a 20 * SOFTWARE.
gmehmet 1:f60eafbf009a 21 */
gmehmet 1:f60eafbf009a 22 /* Introduction
gmehmet 1:f60eafbf009a 23 * ------------
gmehmet 1:f60eafbf009a 24 * SD and MMC cards support a number of interfaces, but common to them all
gmehmet 1:f60eafbf009a 25 * is one based on SPI. This is the one I'm implmenting because it means
gmehmet 1:f60eafbf009a 26 * it is much more portable even though not so performant, and we already
gmehmet 1:f60eafbf009a 27 * have the mbed SPI Interface!
gmehmet 1:f60eafbf009a 28 *
gmehmet 1:f60eafbf009a 29 * The main reference I'm using is Chapter 7, "SPI Mode" of:
gmehmet 1:f60eafbf009a 30 * http://www.sdcard.org/developers/tech/sdcard/pls/Simplified_Physical_Layer_Spec.pdf
gmehmet 1:f60eafbf009a 31 *
gmehmet 1:f60eafbf009a 32 * SPI Startup
gmehmet 1:f60eafbf009a 33 * -----------
gmehmet 1:f60eafbf009a 34 * The SD card powers up in SD mode. The SPI interface mode is selected by
gmehmet 1:f60eafbf009a 35 * asserting CS low and sending the reset command (CMD0). The card will
gmehmet 1:f60eafbf009a 36 * respond with a (R1) response.
gmehmet 1:f60eafbf009a 37 *
gmehmet 1:f60eafbf009a 38 * CMD8 is optionally sent to determine the voltage range supported, and
gmehmet 1:f60eafbf009a 39 * indirectly determine whether it is a version 1.x SD/non-SD card or
gmehmet 1:f60eafbf009a 40 * version 2.x. I'll just ignore this for now.
gmehmet 1:f60eafbf009a 41 *
gmehmet 1:f60eafbf009a 42 * ACMD41 is repeatedly issued to initialise the card, until "in idle"
gmehmet 1:f60eafbf009a 43 * (bit 0) of the R1 response goes to '0', indicating it is initialised.
gmehmet 1:f60eafbf009a 44 *
gmehmet 1:f60eafbf009a 45 * You should also indicate whether the host supports High Capicity cards,
gmehmet 1:f60eafbf009a 46 * and check whether the card is high capacity - i'll also ignore this
gmehmet 1:f60eafbf009a 47 *
gmehmet 1:f60eafbf009a 48 * SPI Protocol
gmehmet 1:f60eafbf009a 49 * ------------
gmehmet 1:f60eafbf009a 50 * The SD SPI protocol is based on transactions made up of 8-bit words, with
gmehmet 1:f60eafbf009a 51 * the host starting every bus transaction by asserting the CS signal low. The
gmehmet 1:f60eafbf009a 52 * card always responds to commands, data blocks and errors.
gmehmet 1:f60eafbf009a 53 *
gmehmet 1:f60eafbf009a 54 * The protocol supports a CRC, but by default it is off (except for the
gmehmet 1:f60eafbf009a 55 * first reset CMD0, where the CRC can just be pre-calculated, and CMD8)
gmehmet 1:f60eafbf009a 56 * I'll leave the CRC off I think!
gmehmet 1:f60eafbf009a 57 *
gmehmet 1:f60eafbf009a 58 * Standard capacity cards have variable data block sizes, whereas High
gmehmet 1:f60eafbf009a 59 * Capacity cards fix the size of data block to 512 bytes. I'll therefore
gmehmet 1:f60eafbf009a 60 * just always use the Standard Capacity cards with a block size of 512 bytes.
gmehmet 1:f60eafbf009a 61 * This is set with CMD16.
gmehmet 1:f60eafbf009a 62 *
gmehmet 1:f60eafbf009a 63 * You can read and write single blocks (CMD17, CMD25) or multiple blocks
gmehmet 1:f60eafbf009a 64 * (CMD18, CMD25). For simplicity, I'll just use single block accesses. When
gmehmet 1:f60eafbf009a 65 * the card gets a read command, it responds with a response token, and then
gmehmet 1:f60eafbf009a 66 * a data token or an error.
gmehmet 1:f60eafbf009a 67 *
gmehmet 1:f60eafbf009a 68 * SPI Command Format
gmehmet 1:f60eafbf009a 69 * ------------------
gmehmet 1:f60eafbf009a 70 * Commands are 6-bytes long, containing the command, 32-bit argument, and CRC.
gmehmet 1:f60eafbf009a 71 *
gmehmet 1:f60eafbf009a 72 * +---------------+------------+------------+-----------+----------+--------------+
gmehmet 1:f60eafbf009a 73 * | 01 | cmd[5:0] | arg[31:24] | arg[23:16] | arg[15:8] | arg[7:0] | crc[6:0] | 1 |
gmehmet 1:f60eafbf009a 74 * +---------------+------------+------------+-----------+----------+--------------+
gmehmet 1:f60eafbf009a 75 *
gmehmet 1:f60eafbf009a 76 * As I'm not using CRC, I can fix that byte to what is needed for CMD0 (0x95)
gmehmet 1:f60eafbf009a 77 *
gmehmet 1:f60eafbf009a 78 * All Application Specific commands shall be preceded with APP_CMD (CMD55).
gmehmet 1:f60eafbf009a 79 *
gmehmet 1:f60eafbf009a 80 * SPI Response Format
gmehmet 1:f60eafbf009a 81 * -------------------
gmehmet 1:f60eafbf009a 82 * The main response format (R1) is a status byte (normally zero). Key flags:
gmehmet 1:f60eafbf009a 83 * idle - 1 if the card is in an idle state/initialising
gmehmet 1:f60eafbf009a 84 * cmd - 1 if an illegal command code was detected
gmehmet 1:f60eafbf009a 85 *
gmehmet 1:f60eafbf009a 86 * +-------------------------------------------------+
gmehmet 1:f60eafbf009a 87 * R1 | 0 | arg | addr | seq | crc | cmd | erase | idle |
gmehmet 1:f60eafbf009a 88 * +-------------------------------------------------+
gmehmet 1:f60eafbf009a 89 *
gmehmet 1:f60eafbf009a 90 * R1b is the same, except it is followed by a busy signal (zeros) until
gmehmet 1:f60eafbf009a 91 * the first non-zero byte when it is ready again.
gmehmet 1:f60eafbf009a 92 *
gmehmet 1:f60eafbf009a 93 * Data Response Token
gmehmet 1:f60eafbf009a 94 * -------------------
gmehmet 1:f60eafbf009a 95 * Every data block written to the card is acknowledged by a byte
gmehmet 1:f60eafbf009a 96 * response token
gmehmet 1:f60eafbf009a 97 *
gmehmet 1:f60eafbf009a 98 * +----------------------+
gmehmet 1:f60eafbf009a 99 * | xxx | 0 | status | 1 |
gmehmet 1:f60eafbf009a 100 * +----------------------+
gmehmet 1:f60eafbf009a 101 * 010 - OK!
gmehmet 1:f60eafbf009a 102 * 101 - CRC Error
gmehmet 1:f60eafbf009a 103 * 110 - Write Error
gmehmet 1:f60eafbf009a 104 *
gmehmet 1:f60eafbf009a 105 * Single Block Read and Write
gmehmet 1:f60eafbf009a 106 * ---------------------------
gmehmet 1:f60eafbf009a 107 *
gmehmet 1:f60eafbf009a 108 * Block transfers have a byte header, followed by the data, followed
gmehmet 1:f60eafbf009a 109 * by a 16-bit CRC. In our case, the data will always be 512 bytes.
gmehmet 1:f60eafbf009a 110 *
gmehmet 1:f60eafbf009a 111 * +------+---------+---------+- - - -+---------+-----------+----------+
gmehmet 1:f60eafbf009a 112 * | 0xFE | data[0] | data[1] | | data[n] | crc[15:8] | crc[7:0] |
gmehmet 1:f60eafbf009a 113 * +------+---------+---------+- - - -+---------+-----------+----------+
gmehmet 1:f60eafbf009a 114 */
gmehmet 1:f60eafbf009a 115 #include "USBMSD_BD.h"
gmehmet 1:f60eafbf009a 116 #include "mbed_debug.h"
gmehmet 1:f60eafbf009a 117
gmehmet 1:f60eafbf009a 118 #define SD_COMMAND_TIMEOUT 5000
gmehmet 1:f60eafbf009a 119
gmehmet 1:f60eafbf009a 120 #define SD_DBG 0
gmehmet 1:f60eafbf009a 121
gmehmet 1:f60eafbf009a 122 USBMSD_BD::USBMSD_BD(BlockDevice* bd) {
gmehmet 1:f60eafbf009a 123 _bd = bd;
gmehmet 1:f60eafbf009a 124 //no init
gmehmet 1:f60eafbf009a 125 _status = 0x01;
gmehmet 1:f60eafbf009a 126
gmehmet 1:f60eafbf009a 127 // connect();
gmehmet 1:f60eafbf009a 128 }
gmehmet 1:f60eafbf009a 129
gmehmet 1:f60eafbf009a 130 int USBMSD_BD::disk_initialize() {
gmehmet 1:f60eafbf009a 131 _bd->init();
gmehmet 1:f60eafbf009a 132 _ssize = _bd->get_erase_size();
gmehmet 1:f60eafbf009a 133 _sectors = _bd->size() / _ssize;
gmehmet 1:f60eafbf009a 134
gmehmet 1:f60eafbf009a 135 // OK
gmehmet 1:f60eafbf009a 136 _status = 0x00;
gmehmet 1:f60eafbf009a 137
gmehmet 1:f60eafbf009a 138 return 0;
gmehmet 1:f60eafbf009a 139 }
gmehmet 1:f60eafbf009a 140
gmehmet 1:f60eafbf009a 141 int USBMSD_BD::disk_write(const uint8_t* buff, uint64_t sector, uint8_t count)
gmehmet 1:f60eafbf009a 142 {
gmehmet 1:f60eafbf009a 143 bd_addr_t addr = (bd_addr_t)sector*_ssize;
gmehmet 1:f60eafbf009a 144 bd_size_t size = (bd_size_t)count*_ssize;
gmehmet 1:f60eafbf009a 145 int err = _bd->erase(addr, size);
gmehmet 1:f60eafbf009a 146 err = _bd->program(buff, addr, size);
gmehmet 1:f60eafbf009a 147 return err;
gmehmet 1:f60eafbf009a 148 }
gmehmet 1:f60eafbf009a 149
gmehmet 1:f60eafbf009a 150 int USBMSD_BD::disk_read(uint8_t* buff, uint64_t sector, uint8_t count)
gmehmet 1:f60eafbf009a 151 {
gmehmet 1:f60eafbf009a 152 bd_addr_t addr = (bd_addr_t)sector*_ssize;
gmehmet 1:f60eafbf009a 153 bd_size_t size = (bd_size_t)count*_ssize;
gmehmet 1:f60eafbf009a 154 int err = _bd->read(buff, addr, size);
gmehmet 1:f60eafbf009a 155 return err;
gmehmet 1:f60eafbf009a 156 }
gmehmet 1:f60eafbf009a 157
gmehmet 1:f60eafbf009a 158 int USBMSD_BD::disk_status() { return _status; }
gmehmet 1:f60eafbf009a 159 int USBMSD_BD::disk_sync() { return 0; }
gmehmet 1:f60eafbf009a 160 uint64_t USBMSD_BD::disk_sectors() { return _sectors; }
gmehmet 1:f60eafbf009a 161 uint64_t USBMSD_BD::disk_size() { return _bd->size(); }
gmehmet 1:f60eafbf009a 162
gmehmet 1:f60eafbf009a 163
gmehmet 1:f60eafbf009a 164 // PRIVATE FUNCTIONS
gmehmet 1:f60eafbf009a 165