Example for updating the MTi-1's firmware. Uses a platform independent, retargetable pure C implementation of the firmware updater protocol.

Dependencies:   mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mtssp_spi_driver.cpp Source File

mtssp_spi_driver.cpp

Go to the documentation of this file.
00001 /*!
00002  * \file
00003  * \copyright Copyright (C) Xsens Technologies B.V., 2015.
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00006  * use this file except in compliance with the License. You may obtain a copy
00007  * of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00014  * License for the specific language governing permissions and limitations
00015  * under the License.
00016  */
00017 #include "mtssp_spi_driver.h "
00018 #include <string.h>
00019 #include "board.h "
00020 
00021 
00022 /*! \class MtsspSpiDriver
00023     \brief Implementation of MtsspDriver for communicating over SPI.
00024     
00025     The low level SPI driver is provided by the mbed platform.
00026 */
00027 
00028 
00029 /*! \brief Constructor
00030 */
00031 MtsspSpiDriver::MtsspSpiDriver()
00032 {
00033     m_chipSelect = new DigitalOut(MT_nCS, 1);
00034     *m_chipSelect = 1;
00035     wait(0.1);
00036     m_spi = new SPI(MT_MOSI, MT_MISO, MT_SCLK);
00037     
00038     m_spi->frequency(1000000);
00039     m_spi->format(8, 3);
00040 }
00041 
00042 
00043 /*! \brief Destructor
00044 */
00045 MtsspSpiDriver::~MtsspSpiDriver()
00046 {
00047     delete m_chipSelect;
00048     delete m_spi;
00049 }
00050 
00051 
00052 /*! \brief Perform a blocking write transfer over SPI
00053     \param opcode Opcode to use
00054     \param data Pointer to data to be written
00055     \param dataLength Number of data bytes to write
00056 */
00057 void MtsspSpiDriver::write(uint8_t opcode, uint8_t const* data, int dataLength)
00058 {
00059     *m_chipSelect = 0;
00060     m_spi->write(opcode);
00061     m_spi->write(0);
00062     m_spi->write(0);
00063     m_spi->write(0);
00064 
00065     for (int i = 0; i < dataLength; i++)
00066     {
00067         m_spi->write(data[i]);
00068     }
00069 
00070     *m_chipSelect = 1;
00071 }
00072 
00073 
00074 /*! \brief Perform a blocking read transfer over SPI
00075     \param opcode Opcode to use
00076     \param data Pointer to result buffer
00077     \param dataLength Number of data bytes to read
00078 */
00079 void MtsspSpiDriver::read(uint8_t opcode, uint8_t* data, int dataLength)
00080 {
00081     *m_chipSelect = 0;
00082 
00083     m_spi->write(opcode);
00084     m_spi->write(0);
00085     m_spi->write(0);
00086     m_spi->write(0);
00087 
00088     for (int i = 0; i < dataLength; i++)
00089     {
00090         data[i] = m_spi->write(0);
00091     }
00092     *m_chipSelect = 1;
00093 }
00094 
00095 
00096 /*! \brief Perform a blocking write transfer over SPI
00097     \param data Pointer to data to be written
00098     \param dataLength Number of data bytes to write
00099 */
00100 void MtsspSpiDriver::writeRaw(uint8_t const* data, int dataLength)
00101 {
00102     *m_chipSelect = 0;
00103     for (int i = 0; i < dataLength; i++)
00104     {
00105         m_spi->write(data[i]);
00106     }
00107     *m_chipSelect = 1;
00108 }