DA14580 Bluetooth Smart IC writer library

Dependents:   11u35_usbLocalFilesystem

DA14580.h

Committer:
k4zuki
Date:
2015-08-19
Revision:
1:b2d4a4e2f362
Parent:
0:3bdbabca8a09
Child:
2:043522e836ab

File content as of revision 1:b2d4a4e2f362:

/**
 * mbed library for Dialog Semiconductor DA14580 Bluetooth Smart chip
 * by Kazuki Yamamoto, or _K4ZUKI_
 **/

#ifndef __DA1458X_H__
#define __DA1458X_H__

#include "mbed.h"

#define     LOADER_FILE         "/local/loader.bin"
#define     TARGET_FILE         "/local/target.bin"

/** \class DA14580
 * \brief mbed library for Dialog Semiconductor DA14580 Bluetooth Smart chip
 *
 * Example:
 * @code
 * #include "mbed.h"
 * #include "DA14580.h"
 *
 * DA14580 BLE(P0_18, P0_19, P0_1);
 * Serial pc(USBTX, USBRX);
 *
 * int main()
 * {
 *    int result=0;
 *    pc.baud(115200);
 *
 *   wait_ms(1);
 *   fp = fopen( SOURCE_FILE, "rb" );
 *   result = BLE.load(fp);
 *   fclose(fp);
#if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
 *   free(fp);
#endif
 *   pc.printf("Result = %d \n\r",&result);
 * }
 * @endcode
 */
class DA14580
{
public:
    /** \enum XMODEM_CONST
        \brief Constant number used in XMODEM
        @param SOH = 0x01
        @param STX = 0x02
        @param EOT = 0x04
        @param ACK = 0x06
        @param DLE = 0x10
        @param NAK = 0x15
        @param CAN = 0x18
    */
    enum XMODEM_CONST {
        SOH = (0x01),
        STX = (0x02),
        EOT = (0x04),
        ACK = (0x06),
        DLE = (0x10),
        NAK = (0x15),
        CAN = (0x18),
    };

    /** \enum DA14580_STATUS
        \brief Status flags to show write operation is succeeded 
        SUCCESS =               '0'
        E_NOT_CONNECTED =       '1'
        E_FILE_NOT_FOUND =      '2'
        E_TIMEOUT_STX =         '3'
        E_ACK_NOT_RETURNED =    '4'
        E_CRC_MISMATCH =        '5'
    */
    enum DA14580_STATUS {
        SUCCESS = '0',
        E_NOT_CONNECTED = '1',
        E_FILE_NOT_FOUND = '2',
        E_TIMEOUT_STX = '3',
        E_ACK_NOT_RETURNED = '4',
        E_CRC_MISMATCH = '5'
    };

    /** Constructor
    * @param TX UART data output from mbed
    * @param RX UART data input from device
    * @param RESET LOW-active RESET signal
    */
    DA14580( PinName TX, PinName RX, PinName RESET );

    /** Constructor
    * @param &ble pointer to RawSerial instance
    * @param RESET LOW-active RESET signal
    */
    DA14580( RawSerial &ble, PinName RESET );

    /** Destructor
    */
    ~DA14580();

    /** Initialise internal variables
    */
    void init();

    /** Load binary executable into device through UART
    */
    int load();

    /** Returns file size: copied from ika_shouyu_poppoyaki
    * @param *fp File pointer
    */
    int file_size( FILE *fp );

    RawSerial _ble;

private:
    uint8_t _recieve;
    uint8_t _read;
    int _filesize;
    int _timeout;
    int _status;
    FILE* _fp;
    int _crc;
    DigitalOut _reset;
};

#endif //__DA1458X_H__