CEBF746 Module.. PC <--> This Module (SPI Master) <-->

Dependencies:   cube_CEBF746 cube_crc_16 mbed

main.cpp

Committer:
gandol2
Date:
2016-10-14
Revision:
5:589ab465ed00
Parent:
0:9a61123385e9
Child:
6:a98f9d63f8a2

File content as of revision 5:589ab465ed00:

// firmware for NUCLEO-F746ZG
// ======== SPI Master Pin
// PA5  - SCK
// PA6  - MISO
// PA7  - MOSI
// PA14 - SEL

#include "mbed.h"

#define DEBUG_SPI
#ifdef DEBUG_SPI
#define PRINTD(arg1,arg2...)    printf(arg1,##arg2)
#endif

// ======== SPI Master Pin define
#define SPI_SCLK    PA_5
#define SPI_MISO    PA_6
#define SPI_MOSI    PA_7
#define SPI_SSEL    PA_4

// ======== SPI Func define
#define EEPROM_WREN         0x06        // Write Enable
#define EEPROM_WRDI         0x04        // Write Disable
#define EEPROM_RDSR         0x05        // Read Status Register
#define EEPROM_WRSR         0x01        // Write Status Register
#define EEPROM_READ         0x03        // Read EEPROM Memory
#define EEPROM_WRITE        0x02        // Write EEPROM Memory
#define EEPROM_MULTIREAD    0x07        // Write EEPROM Memory

#define SPI_CONFIG_BIT          8
#define SPI_CONFIG_FREQUENCY    1000000         // 1Mhz
//#define SPI_CONFIG_FREQUENCY    50000000      // 50Mhz

Serial pc_serial(USBTX, USBRX);
SPI spi_master(SPI_MOSI, SPI_MISO, SPI_SCLK); // MOSI, MISO, SCLK(CLK,SCK)
DigitalOut cs(SPI_SSEL);

void SPI_InitMaster()
{
    spi_master.format(SPI_CONFIG_BIT, 0);
    spi_master.frequency(SPI_CONFIG_FREQUENCY);
    PRINTD("SPI Init.. Packet bit size=[%d] Frequency=[%d]Mhz\n", SPI_CONFIG_BIT, (SPI_CONFIG_FREQUENCY/1000000));
}

enum cubeMenu
{
    MENU_EEPROM_Write_Enable = 1,
    MENU_EEPROM_Write_Disable,
    MENU_EEPROM_Read_Status,
    MENU_EEPROM_Write_Status,
    MENU_EEPROM_Write_Memory,
    MENU_EEPROM_Read_Memory,
    MENU_TEST_Multi_Byte_Read,
    MENU_EXIT_Program = 9
};

/** @brief print menu
 *
 */
void print_menu(void)
{
    pc_serial.printf("\n\n\n");
    pc_serial.printf("-----------[MENU]----------\n");
// EEPROM func ###
    pc_serial.printf("%d) Write Enable\n", MENU_EEPROM_Write_Enable);
    pc_serial.printf("%d) Write Disable\n", MENU_EEPROM_Write_Disable);
    pc_serial.printf("%d) Read Status Register\n", MENU_EEPROM_Read_Status);
    pc_serial.printf("%d) Write Status Register\n", MENU_EEPROM_Write_Status);
    pc_serial.printf("%d) Write Memory\n", MENU_EEPROM_Write_Memory);
    pc_serial.printf("%d) Read Memory\n", MENU_EEPROM_Read_Memory);
// cubebite test func ###
    pc_serial.printf("%d) multi byte read\n", MENU_TEST_Multi_Byte_Read);

// exit func ###
    pc_serial.printf("%d) Exit program\n", MENU_EXIT_Program);
    pc_serial.printf("---------------------------\n");
    pc_serial.printf("input menu : ");
}

/** @brief spi master start
 *
 */
void spi_start(void)
{
    spi_master.lock();
    cs = 0;
    wait_us(0);
}

/** @brief spi master end
 *
 */
void spi_end(void)
{
    wait_us(10);
    cs = 1;
    spi_master.unlock();
}

/** @brief spi master dummy write
 *
 */
void spi_dummy_write(void)
{
    spi_start();
    spi_master.write(0x00);
    spi_end();
}


/** @brief convert ASCII to hex
 *
 *  @param convertedData converted Data
 *  @param sourceBuf ASCII data array
 *  @param lenght sourceBuf length
 *  @return 0 is valid hex format, other is error
 */
int converHex(char * convertedData, char * sourceBuf, int length)
{
    int ret = 0;
    char tempBuf[length];
    *convertedData = 0;

    for (int iCnt = 0; iCnt < length; iCnt++)
    {
        if (('0' <= *(sourceBuf + iCnt)) && ('9' >= *(sourceBuf + iCnt)))
        {
            tempBuf[iCnt] = (*(sourceBuf + iCnt) - 48);
        }
        else if (('A' <= *(sourceBuf + iCnt)) && ('F' >= *(sourceBuf + iCnt)))
        {
            tempBuf[iCnt] = (*(sourceBuf + iCnt) - 55);
        }
        else if (('a' <= *(sourceBuf + iCnt)) && ('f' >= *(sourceBuf + iCnt)))
        {
            tempBuf[iCnt] = (*(sourceBuf + iCnt) - 87);
        }
        else    // error
        {
            ret = -1;
            return ret;
        }
    }

    //pc_serial.printf("[TEST] tempBuf[0] = 0x%02X\n", tempBuf[0]);
    //pc_serial.printf("[TEST] tempBuf[1] = 0x%02X\n", tempBuf[1]);
    *convertedData = ((tempBuf[0] & 0x0F) << 4) | (tempBuf[1] & 0x0F);
    //pc_serial.printf("[TEST] convertedData = 0x%02X\n", *convertedData);
    return ret;
}

/** @brief main function ###################################################################
 *
 */
int main()
{
    char inputMenuNum = 0;
    char statusReg = 0x00;
    char test_data[10] = { 0 };

    char eepAddrBuf[2] = { 0 };
    char eepAddr = 0;

    char eepDataBuf[2] = { 0 };
    char eepData = 0;
    int eepReadData = 0;
    int maxInputCnt = 0;

    int ret;

    SPI_InitMaster();
    spi_dummy_write();

    pc_serial.printf("\n\n========== SPI Master for EEPROM [Start] ==========\n");

    while (1)
    {
        print_menu();
        inputMenuNum = pc_serial.getc();

        pc_serial.printf("[DEBUG] input menu number = %c\n", inputMenuNum);

        if (('0' < inputMenuNum) && ('9' >= inputMenuNum))
        {
            inputMenuNum = inputMenuNum - 48;
        }
        else
        {
            pc_serial.printf("[INFO] correct input menu number.. please retry..\n");
            wait(1);
            continue;
        }

        spi_start();
        switch (inputMenuNum)
        {
        case MENU_EEPROM_Write_Enable:        // Write Enable =================================[OK]
            pc_serial.printf("[DEBUG] Write Enable progress..\n");
            spi_master.write(EEPROM_WREN);
            break;

        case MENU_EEPROM_Write_Disable:        // Write Disable =================================[OK]
            pc_serial.printf("[DEBUG] Write Disable progress..\n");
            spi_master.write(EEPROM_WRDI);
            break;

        case MENU_EEPROM_Read_Status:        // Read Status Register =================================[OK]
            pc_serial.printf("[DEBUG] Read Status Register progress..\n");
            spi_master.write(EEPROM_RDSR);
            statusReg = spi_master.write(EEPROM_RDSR);

            // TODO : print statusReg data to serial
            pc_serial.printf("[INFO] Status Register Data : 0x%02X\n", statusReg);

            break;

        case MENU_EEPROM_Write_Status:        // Write Status Register =================================
            pc_serial.printf("[DEBUG] Write Status Register progress..\n");
            // TODO : input data for serial

            spi_master.write(EEPROM_WRSR);
            spi_master.write(statusReg);
            break;

        case MENU_EEPROM_Write_Memory:        // Write Memory =================================
            pc_serial.printf("[DEBUG] Write Memory progress..\n");

            // EEPROM Address input

            maxInputCnt = 3;
            while (maxInputCnt--)
            {
                pc_serial.printf("input Address (hex format : 00~FF) : ");
                for (int iCnt = 0; iCnt < 2; ++iCnt)
                {
                    eepAddrBuf[iCnt] = pc_serial.getc();
                    pc_serial.printf("%c", eepAddrBuf[iCnt]);
                }

                ret = converHex(&eepAddr, eepAddrBuf, 2);

                if (0 == ret)
                {
                    //pc_serial.printf("\n[DEBUG] before convert hex [0x%02x] \n", eepAddr);
                    break;
                }
                else        // error
                {
                    pc_serial.printf("\n[INFO] hex formet error!\n");
                    continue;
                }
            }

            if (0 == ret)
            {
                // EEPROM data input
                maxInputCnt = 3;
                while (maxInputCnt--)
                {
                    pc_serial.printf("\ninput Data (hex format : 00~FF) : ");
                    for (int iCnt = 0; iCnt < 2; ++iCnt)
                    {
                        eepDataBuf[iCnt] = pc_serial.getc();
                        pc_serial.printf("%c", eepDataBuf[iCnt]);
                    }

                    int ret = converHex(&eepData, eepDataBuf, 2);
                    if (0 == ret)
                    {

                        pc_serial.printf("\n[INFO] SPI Write Memory.... Start! \n");
                        //pc_serial.printf("\n[DEBUG] Addr[0x%02X] Data[0x%02X] \n", eepAddr, eepData);

                        spi_master.write(EEPROM_RDSR);
                        if (!(spi_master.write(EEPROM_RDSR) & 0x01))
                        {
                            spi_master.write(EEPROM_WRITE);
                            spi_master.write(eepAddr);
                            spi_master.write(eepData);
                            pc_serial.printf("\n[INFO] SPI Write success \n");
                        }
                        else
                        {
                            pc_serial.printf("\n[INFO] SPI Write fail.. device is busy! \n");
                        }

                        break;
                    }
                    else        // error
                    {
                        pc_serial.printf("\n[INFO] hex formet error!\n");
                        continue;
                    }
                }
            }

            break;

        case MENU_EEPROM_Read_Memory:        // Read Memory =================================
            pc_serial.printf("[DEBUG] Read Memory progress..\n");

            // EEPROM Address input

            maxInputCnt = 3;
            while (maxInputCnt--)
            {
                pc_serial.printf("input Address (hex format : 00~FF) : ");
                for (int iCnt = 0; iCnt < 2; ++iCnt)
                {
                    eepAddrBuf[iCnt] = pc_serial.getc();
                    pc_serial.printf("%c", eepAddrBuf[iCnt]);
                }

                ret = converHex(&eepAddr, eepAddrBuf, 2);

                if (0 == ret)
                {
                    //pc_serial.printf("\n[DEBUG] before convert hex [0x%02x] \n", eepAddr);
                    spi_master.write(EEPROM_READ);
                    spi_master.write(eepAddr);

                    eepReadData = spi_master.write(0);

                    pc_serial.printf("\n[DEBUG] Read EEPROM Addr[0x%02X] Data[0x%02X] \n", eepAddr, eepReadData);
                    break;
                }
                else        // error
                {
                    pc_serial.printf("\n[INFO] hex formet error!\n");
                    continue;
                }
            }

            break;

        case MENU_TEST_Multi_Byte_Read:
            pc_serial.printf("\n[DEBUG] TEST_Multi_Byte_Read\n");

            maxInputCnt = 3;
            while (maxInputCnt--)
            {
                pc_serial.printf("input Address (hex format : 00~FF) : ");
                for (int iCnt = 0; iCnt < 2; ++iCnt)
                {
                    eepAddrBuf[iCnt] = pc_serial.getc();
                    pc_serial.printf("%c", eepAddrBuf[iCnt]);
                }

                ret = converHex(&eepAddr, eepAddrBuf, 2);

                if (0 == ret)
                {
                    spi_master.write(EEPROM_MULTIREAD);
                    spi_master.write(eepAddr);

                    for (int i = 0; i < 5; i++)
                    {
                        wait_ms(10);
                        //test_data[i] = spi_master.write(0);
                        //spi_master.write(++eepAddr);
                        test_data[i] = spi_master.write(0);     // dummy write

                    }

                    for (int i = 0; i < 5; i++)
                    {
                        printf("\n[DEBUG] Read EEPROM Data[0x%02X]\n", test_data[i]);
                    }

                    //pc_serial.printf("\n[DEBUG] Read EEPROM Addr[0x%02X]\n", eepAddr);
                    break;
                }
                else        // error
                {
                    pc_serial.printf("\n[INFO] hex formet error!\n");
                    continue;
                }
            }

            break;

        case MENU_EXIT_Program:
            spi_end();
            pc_serial.printf("exit program... thank you\n");
            //return 0;

        }
        spi_end();

    }

    return 0;
}