FRAM SPI Access Library
Dependents: FRAM_Dump SDFile_Logger
MB85RSxx_SPI.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file MB85RSxx_SPI.cpp 00004 * @author APS Lab 00005 * @version V1.0.0 00006 * @date 26 June 2017 00007 * @brief MB85RSxx_SPI class implementation 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * Permission is hereby granted, free of charge, to any person obtaining a copy 00012 * of this software and associated documentation files (the "Software"), to deal 00013 * in the Software without restriction, including without limitation the rights 00014 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00015 * copies of the Software, and to permit persons to whom the Software is 00016 * furnished to do so, subject to the following conditions: 00017 * 00018 * The above copyright notice and this permission notice shall be included in 00019 * all copies or substantial portions of the Software. 00020 * 00021 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00022 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00023 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00024 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00025 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00026 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00027 * THE SOFTWARE. 00028 */ 00029 #include "mbed.h" 00030 #include "MB85RSxx_SPI.h" 00031 00032 MB85RSxx_SPI::MB85RSxx_SPI(PinName mosi, PinName miso, PinName sclk, PinName nss) 00033 : 00034 _spi_p(new SPI(mosi, miso, sclk)), 00035 _spi(*_spi_p), 00036 _di_p(new DigitalOut(nss)), 00037 _di(*_di_p) 00038 { 00039 _spi.format(8,0); 00040 _spi.frequency(10000000); 00041 } 00042 00043 MB85RSxx_SPI::~MB85RSxx_SPI() 00044 { 00045 if (NULL != _spi_p) 00046 delete _spi_p; 00047 } 00048 00049 void MB85RSxx_SPI::Init(void) 00050 { 00051 char sts; 00052 _di=0; 00053 _spi.write(WRDI); 00054 _di=1; 00055 wait(0.2); 00056 _di=0; 00057 _spi.write(WREN); 00058 _di=1; 00059 wait(0.2); 00060 _di=0; 00061 _spi.write(WRSR); 00062 _spi.write(SR_WEL); 00063 _di=1; 00064 wait(0.2); 00065 _di=0; 00066 _spi.write(RDSR); 00067 sts=_spi.write(0x00); 00068 _di=1; 00069 printf("FRAM Status %x\n", sts); 00070 } 00071 00072 void MB85RSxx_SPI::write(uint32_t address, char data) 00073 { 00074 _di=0; 00075 _spi.write(WREN); 00076 _di=1; 00077 _di=0; 00078 _spi.write(WRITE); 00079 _spi.write((address & 0x3F00) >> 8);//Addr H for 64Kbits 00080 _spi.write((address & 0x00FF)); //Addr L for 64Kbits 00081 _spi.write(data); //Write Data 00082 _di=1; 00083 } 00084 00085 char MB85RSxx_SPI::read(uint32_t address) 00086 { 00087 char ch; 00088 _di=0; 00089 _spi.write(READ); 00090 _spi.write((address & 0x3F00) >> 8);//Addr H for 64Kbits 00091 _spi.write((address & 0x00FF)); //Addr L for 64Kbits 00092 ch = _spi.write(0x00);//Write dummy 0x00, Reade Data 00093 _di=1; 00094 return ch; 00095 } 00096 00097 00098 void MB85RSxx_SPI::write(uint32_t address, char *data, uint32_t length) 00099 { 00100 int idx; 00101 for(idx=0; idx < length; idx++) 00102 { 00103 MB85RSxx_SPI::write(address+idx, *(data+idx)); 00104 } 00105 } 00106 00107 void MB85RSxx_SPI::read(uint32_t address, char *data, uint32_t length) 00108 { 00109 uint32_t idx; 00110 for(idx=0; idx < length; idx++) 00111 { 00112 *(data+idx) = MB85RSxx_SPI::read(address+idx); 00113 } 00114 } 00115
Generated on Fri Jul 15 2022 00:06:59 by 1.7.2