161006_BDK_SPImaster
Dependencies: mbed
Fork of Nucleo_SPImaster_F042K6_KSS by
main.cpp@4:a9fa91152c17, 2016-10-05 (annotated)
- Committer:
- gandol2
- Date:
- Wed Oct 05 08:58:51 2016 +0000
- Revision:
- 4:a9fa91152c17
- Parent:
- 3:c6f8271b2d4e
- Child:
- 5:47353da6bca4
161005_F0426K_SPIMaster
Who changed what in which revision?
User | Revision | Line number | New 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 | { |
gandol2 | 4:a9fa91152c17 | 91 | int spiTxCnt = 0; |
gandol2 | 4:a9fa91152c17 | 92 | char spiTxReadyFlag = 0; |
gandol2 | 4:a9fa91152c17 | 93 | |
gandol2 | 4:a9fa91152c17 | 94 | char serialRxBuf[255]; |
gandol2 | 4:a9fa91152c17 | 95 | int serialRxLen = 0; |
gandol2 | 4:a9fa91152c17 | 96 | |
gandol2 | 4:a9fa91152c17 | 97 | pc_serial.printf("\n\n========== KSS SPI Master [Start] ==========\n"); |
gandol2 | 4:a9fa91152c17 | 98 | |
gandol2 | 4:a9fa91152c17 | 99 | |
gandol2 | 4:a9fa91152c17 | 100 | while(1) |
gandol2 | 4:a9fa91152c17 | 101 | { |
gandol2 | 4:a9fa91152c17 | 102 | if(0 != pc_serial.readable()) // wait serial input.. |
gandol2 | 4:a9fa91152c17 | 103 | { |
gandol2 | 4:a9fa91152c17 | 104 | pc_serial.scanf("%s", serialRxBuf); |
gandol2 | 4:a9fa91152c17 | 105 | serialRxLen = strlen(serialRxBuf); |
gandol2 | 4:a9fa91152c17 | 106 | pc_serial.printf("len=[%d] %s\n", serialRxLen, serialRxBuf); |
gandol2 | 4:a9fa91152c17 | 107 | spiTxReadyFlag = 1; |
gandol2 | 4:a9fa91152c17 | 108 | } |
gandol2 | 4:a9fa91152c17 | 109 | if(1 == spiTxReadyFlag) |
gandol2 | 4:a9fa91152c17 | 110 | { |
gandol2 | 4:a9fa91152c17 | 111 | // SPI Send Start |
gandol2 | 4:a9fa91152c17 | 112 | spi_master.lock(); |
gandol2 | 4:a9fa91152c17 | 113 | cs = 0; |
gandol2 | 4:a9fa91152c17 | 114 | for(spiTxCnt = 0 ; spiTxCnt < serialRxLen + 1 ; ++spiTxCnt) |
gandol2 | 4:a9fa91152c17 | 115 | { |
gandol2 | 4:a9fa91152c17 | 116 | //printf("send Cnt[%d] [0x%02X]\n", spiTxCnt, serialRxBuf[spiTxCnt]); |
gandol2 | 4:a9fa91152c17 | 117 | spi_master.write(serialRxBuf[spiTxCnt]); |
gandol2 | 4:a9fa91152c17 | 118 | } |
gandol2 | 4:a9fa91152c17 | 119 | cs = 1; |
gandol2 | 4:a9fa91152c17 | 120 | spi_master.unlock(); |
gandol2 | 4:a9fa91152c17 | 121 | spiTxReadyFlag = 0; |
gandol2 | 4:a9fa91152c17 | 122 | } |
gandol2 | 4:a9fa91152c17 | 123 | |
gandol2 | 4:a9fa91152c17 | 124 | |
gandol2 | 4:a9fa91152c17 | 125 | |
gandol2 | 4:a9fa91152c17 | 126 | /* TODO " ----(SPI)-----> master --> serial " |
gandol2 | 4:a9fa91152c17 | 127 | |
gandol2 | 4:a9fa91152c17 | 128 | */ |
gandol2 | 4:a9fa91152c17 | 129 | |
gandol2 | 4:a9fa91152c17 | 130 | } |
gandol2 | 4:a9fa91152c17 | 131 | |
gandol2 | 4:a9fa91152c17 | 132 | |
gandol2 | 4:a9fa91152c17 | 133 | |
gandol2 | 4:a9fa91152c17 | 134 | |
gandol2 | 4:a9fa91152c17 | 135 | |
gandol2 | 4:a9fa91152c17 | 136 | |
gandol2 | 4:a9fa91152c17 | 137 | |
gandol2 | 4:a9fa91152c17 | 138 | |
gandol2 | 4:a9fa91152c17 | 139 | |
gandol2 | 4:a9fa91152c17 | 140 | |
gandol2 | 4:a9fa91152c17 | 141 | |
gandol2 | 4:a9fa91152c17 | 142 | |
gandol2 | 4:a9fa91152c17 | 143 | |
gandol2 | 4:a9fa91152c17 | 144 | |
gandol2 | 4:a9fa91152c17 | 145 | |
gandol2 | 4:a9fa91152c17 | 146 | |
gandol2 | 4:a9fa91152c17 | 147 | |
gandol2 | 4:a9fa91152c17 | 148 | |
gandol2 | 4:a9fa91152c17 | 149 | |
gandol2 | 4:a9fa91152c17 | 150 | |
gandol2 | 4:a9fa91152c17 | 151 | |
gandol2 | 4:a9fa91152c17 | 152 | |
gandol2 | 4:a9fa91152c17 | 153 | #if 0 // 161005_BDK_backup2 start |
bcup | 3:c6f8271b2d4e | 154 | int send_data; |
gandol2 | 4:a9fa91152c17 | 155 | SPI_INIT(); |
bcup | 0:24e90e3ca3f4 | 156 | while(1) |
bcup | 0:24e90e3ca3f4 | 157 | { |
bcup | 3:c6f8271b2d4e | 158 | SPI_Write(); |
bcup | 3:c6f8271b2d4e | 159 | } |
gandol2 | 4:a9fa91152c17 | 160 | #endif // 161005_BDK_backup2 end |
gandol2 | 4:a9fa91152c17 | 161 | return 0; |
bcup | 3:c6f8271b2d4e | 162 | } |