161007_BDK_EEPROM

Dependencies:   mbed

Fork of 161006_BDK_SPImaster by CUBEBITE

Committer:
bcup
Date:
Thu Oct 06 00:11:07 2016 +0000
Revision:
5:47353da6bca4
Parent:
4:a9fa91152c17
Child:
6:50376de8d756
161006_BDK_SPImaster

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcup 0:24e90e3ca3f4 1 #include "mbed.h"
bcup 0:24e90e3ca3f4 2
bcup 0:24e90e3ca3f4 3 #define DEBUG_SPI
bcup 0:24e90e3ca3f4 4
bcup 0:24e90e3ca3f4 5 #ifdef DEBUG_SPI
bcup 0:24e90e3ca3f4 6 #define PRINTD(arg1,arg2...) printf(arg1,##arg2)
bcup 0:24e90e3ca3f4 7 #endif
bcup 0:24e90e3ca3f4 8
gandol2 4:a9fa91152c17 9
gandol2 4:a9fa91152c17 10 #define SPI_MOSI PA_7
gandol2 4:a9fa91152c17 11 #define SPI_MISO PA_6
gandol2 4:a9fa91152c17 12 #define SPI_SCLK PA_5
gandol2 4:a9fa91152c17 13 #define SPI_SSEL PA_4
gandol2 4:a9fa91152c17 14
gandol2 4:a9fa91152c17 15 SPI spi_master(SPI_MOSI,SPI_MISO,SPI_SCLK); // MOSI, MISO, SCLK(CLK,SCK)
gandol2 4:a9fa91152c17 16 DigitalOut cs(SPI_SSEL);
gandol2 4:a9fa91152c17 17
gandol2 4:a9fa91152c17 18 Serial pc_serial(USBTX, USBRX);
gandol2 4:a9fa91152c17 19
bcup 0:24e90e3ca3f4 20
bcup 3:c6f8271b2d4e 21 void SPI_INIT()
bcup 0:24e90e3ca3f4 22 {
bcup 3:c6f8271b2d4e 23 PRINTD("Set SPI init..\n");
bcup 3:c6f8271b2d4e 24 PRINTD("Set SPI format..\n");
gandol2 4:a9fa91152c17 25 spi_master.format(8,0);
bcup 3:c6f8271b2d4e 26 PRINTD("Set frequency to default..\n");
gandol2 4:a9fa91152c17 27 spi_master.frequency(1000000); // default 1MHz
bcup 0:24e90e3ca3f4 28 }
gandol2 4:a9fa91152c17 29 /*
bcup 3:c6f8271b2d4e 30 void SPI_Write()
bcup 0:24e90e3ca3f4 31 {
bcup 3:c6f8271b2d4e 32 char temp;
bcup 3:c6f8271b2d4e 33 int i,value;
bcup 3:c6f8271b2d4e 34 char response;
bcup 3:c6f8271b2d4e 35 char tx_cnt = -1;
bcup 3:c6f8271b2d4e 36 char rx_cnt = -1;
bcup 2:5311ad7c83e6 37 char tx_buffer[255]={0};
bcup 3:c6f8271b2d4e 38 char rx_buffer[255]={0};
bcup 3:c6f8271b2d4e 39 PRINTD("\n==========MASTER==========\n");
bcup 3:c6f8271b2d4e 40 PRINTD("DATA SEND START...\n");
bcup 3:c6f8271b2d4e 41 PRINTD("Lock SPI BUS..\n");
bcup 3:c6f8271b2d4e 42
bcup 0:24e90e3ca3f4 43 while(1)
bcup 0:24e90e3ca3f4 44 {
bcup 0:24e90e3ca3f4 45
bcup 0:24e90e3ca3f4 46 temp=getchar();
bcup 3:c6f8271b2d4e 47 tx_buffer[++tx_cnt]=temp;
bcup 0:24e90e3ca3f4 48 if(temp==0x0d)
bcup 0:24e90e3ca3f4 49 {
bcup 0:24e90e3ca3f4 50 tx_buffer[tx_cnt]=0;
bcup 0:24e90e3ca3f4 51 PRINTD("\nData send Finish...\n");
bcup 0:24e90e3ca3f4 52 for(i=0;i<=tx_cnt;++i)
bcup 0:24e90e3ca3f4 53 {
bcup 0:24e90e3ca3f4 54 PRINTD("%c[%02x]",tx_buffer[i],tx_buffer[i]);
bcup 0:24e90e3ca3f4 55 }
bcup 0:24e90e3ca3f4 56 PRINTD("\n\n");
gandol2 4:a9fa91152c17 57 spi_master.lock();
bcup 0:24e90e3ca3f4 58 for(i=0;i<=tx_cnt;++i)
bcup 0:24e90e3ca3f4 59 {
bcup 0:24e90e3ca3f4 60 value=tx_buffer[i];
bcup 3:c6f8271b2d4e 61 PRINTD("[M]write[%d]=%c[%02x]\n",i,value,value);
bcup 3:c6f8271b2d4e 62 cs=0;
gandol2 4:a9fa91152c17 63 response=spi_master.write(value);
bcup 3:c6f8271b2d4e 64 cs=-1;
bcup 3:c6f8271b2d4e 65 PRINTD("[M]receive=%c[%x]\n",response,response);
bcup 3:c6f8271b2d4e 66 rx_buffer[++rx_cnt]=response;
bcup 0:24e90e3ca3f4 67 }
gandol2 4:a9fa91152c17 68 spi_master.unlock();
bcup 3:c6f8271b2d4e 69 for(i=0;i<255;++i)
bcup 0:24e90e3ca3f4 70 {
bcup 0:24e90e3ca3f4 71 tx_buffer[i]=0;
bcup 0:24e90e3ca3f4 72 }
bcup 3:c6f8271b2d4e 73 for(i=0;i<=tx_cnt;i++)
bcup 3:c6f8271b2d4e 74 {
bcup 3:c6f8271b2d4e 75 PRINTD("init_tx_buffer[%d]=%c\n",i,tx_buffer[i]);
bcup 3:c6f8271b2d4e 76 }
bcup 3:c6f8271b2d4e 77 tx_cnt=-1;
bcup 0:24e90e3ca3f4 78 }
bcup 0:24e90e3ca3f4 79 else
bcup 0:24e90e3ca3f4 80 {
bcup 0:24e90e3ca3f4 81 PRINTD("%c[%02x]",tx_buffer[tx_cnt],tx_buffer[tx_cnt]);
bcup 0:24e90e3ca3f4 82 }
bcup 0:24e90e3ca3f4 83 }
bcup 0:24e90e3ca3f4 84 }
gandol2 4:a9fa91152c17 85 */
bcup 0:24e90e3ca3f4 86
gandol2 4:a9fa91152c17 87
gandol2 4:a9fa91152c17 88 // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ main();
bcup 0:24e90e3ca3f4 89 int main()
bcup 0:24e90e3ca3f4 90 {
bcup 5:47353da6bca4 91 int spiTxCnt = 0;
bcup 5:47353da6bca4 92 int spiRxCnt = 0;
gandol2 4:a9fa91152c17 93 char spiTxReadyFlag = 0;
bcup 5:47353da6bca4 94 int res;
bcup 5:47353da6bca4 95 int spiRxTempBuf = 0;
bcup 5:47353da6bca4 96 char serialRxBuf[255];
bcup 5:47353da6bca4 97 char spiRxBuf[255];
bcup 5:47353da6bca4 98 int serialRxLen = 0;
gandol2 4:a9fa91152c17 99
gandol2 4:a9fa91152c17 100
gandol2 4:a9fa91152c17 101 pc_serial.printf("\n\n========== KSS SPI Master [Start] ==========\n");
gandol2 4:a9fa91152c17 102
gandol2 4:a9fa91152c17 103
gandol2 4:a9fa91152c17 104 while(1)
gandol2 4:a9fa91152c17 105 {
gandol2 4:a9fa91152c17 106 if(0 != pc_serial.readable()) // wait serial input..
gandol2 4:a9fa91152c17 107 {
gandol2 4:a9fa91152c17 108 pc_serial.scanf("%s", serialRxBuf);
gandol2 4:a9fa91152c17 109 serialRxLen = strlen(serialRxBuf);
gandol2 4:a9fa91152c17 110 pc_serial.printf("len=[%d] %s\n", serialRxLen, serialRxBuf);
gandol2 4:a9fa91152c17 111 spiTxReadyFlag = 1;
gandol2 4:a9fa91152c17 112 }
gandol2 4:a9fa91152c17 113 if(1 == spiTxReadyFlag)
gandol2 4:a9fa91152c17 114 {
gandol2 4:a9fa91152c17 115 // SPI Send Start
gandol2 4:a9fa91152c17 116 spi_master.lock();
gandol2 4:a9fa91152c17 117 cs = 0;
gandol2 4:a9fa91152c17 118 for(spiTxCnt = 0 ; spiTxCnt < serialRxLen + 1 ; ++spiTxCnt)
gandol2 4:a9fa91152c17 119 {
gandol2 4:a9fa91152c17 120 //printf("send Cnt[%d] [0x%02X]\n", spiTxCnt, serialRxBuf[spiTxCnt]);
bcup 5:47353da6bca4 121 res=spi_master.write(serialRxBuf[spiTxCnt]);
bcup 5:47353da6bca4 122 PRINTD("res=%c 0x[%02x]\n",res,res);
bcup 5:47353da6bca4 123 spiRxBuf[spiRxCnt++]=res;
gandol2 4:a9fa91152c17 124 }
gandol2 4:a9fa91152c17 125 cs = 1;
gandol2 4:a9fa91152c17 126 spi_master.unlock();
gandol2 4:a9fa91152c17 127 spiTxReadyFlag = 0;
gandol2 4:a9fa91152c17 128 }
gandol2 4:a9fa91152c17 129
gandol2 4:a9fa91152c17 130
gandol2 4:a9fa91152c17 131
bcup 5:47353da6bca4 132 /* TODO " ----(SPI)-----> master --> serial " */
bcup 5:47353da6bca4 133
gandol2 4:a9fa91152c17 134
bcup 5:47353da6bca4 135 #if 0
bcup 5:47353da6bca4 136 if(res!=0)
bcup 5:47353da6bca4 137 {
bcup 5:47353da6bca4 138 PRINTD("res=%d\n",res);
bcup 5:47353da6bca4 139 PRINTD("res=%s\n",spiRxBuf);
bcup 5:47353da6bca4 140 }
bcup 5:47353da6bca4 141 #endif
bcup 5:47353da6bca4 142
bcup 5:47353da6bca4 143
bcup 5:47353da6bca4 144
bcup 5:47353da6bca4 145
bcup 5:47353da6bca4 146
bcup 5:47353da6bca4 147
bcup 5:47353da6bca4 148
bcup 5:47353da6bca4 149
bcup 5:47353da6bca4 150
gandol2 4:a9fa91152c17 151
gandol2 4:a9fa91152c17 152 }
gandol2 4:a9fa91152c17 153
gandol2 4:a9fa91152c17 154
gandol2 4:a9fa91152c17 155
gandol2 4:a9fa91152c17 156
gandol2 4:a9fa91152c17 157
gandol2 4:a9fa91152c17 158
gandol2 4:a9fa91152c17 159
gandol2 4:a9fa91152c17 160
gandol2 4:a9fa91152c17 161
gandol2 4:a9fa91152c17 162
gandol2 4:a9fa91152c17 163
gandol2 4:a9fa91152c17 164
gandol2 4:a9fa91152c17 165
gandol2 4:a9fa91152c17 166
gandol2 4:a9fa91152c17 167
gandol2 4:a9fa91152c17 168
gandol2 4:a9fa91152c17 169
gandol2 4:a9fa91152c17 170
gandol2 4:a9fa91152c17 171
gandol2 4:a9fa91152c17 172
gandol2 4:a9fa91152c17 173
gandol2 4:a9fa91152c17 174
gandol2 4:a9fa91152c17 175 #if 0 // 161005_BDK_backup2 start
bcup 3:c6f8271b2d4e 176 int send_data;
gandol2 4:a9fa91152c17 177 SPI_INIT();
bcup 0:24e90e3ca3f4 178 while(1)
bcup 0:24e90e3ca3f4 179 {
bcup 3:c6f8271b2d4e 180 SPI_Write();
bcup 3:c6f8271b2d4e 181 }
gandol2 4:a9fa91152c17 182 #endif // 161005_BDK_backup2 end
gandol2 4:a9fa91152c17 183 return 0;
bcup 3:c6f8271b2d4e 184 }