161006_BDK_SPIslave
Dependencies: mbed
Fork of Nucleo_SPIslave_F303K8_KSS by
main.cpp@1:3a338e553a54, 2016-10-05 (annotated)
- Committer:
- bcup
- Date:
- Wed Oct 05 07:04:31 2016 +0000
- Revision:
- 1:3a338e553a54
- Parent:
- 0:24e90e3ca3f4
- Child:
- 2:5311ad7c83e6
SPImaster_F042K6
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 | |
bcup | 1:3a338e553a54 | 9 | SPI spi(PA_7,PA_6,PA_5); // MOSI, MISO, SCLK(CLK,SCK) |
bcup | 1:3a338e553a54 | 10 | DigitalOut cs(PA_4); |
bcup | 0:24e90e3ca3f4 | 11 | |
bcup | 1:3a338e553a54 | 12 | void SPI_INIT() |
bcup | 0:24e90e3ca3f4 | 13 | { |
bcup | 1:3a338e553a54 | 14 | PRINTD("Set SPI init..\n"); |
bcup | 1:3a338e553a54 | 15 | PRINTD("Set SPI format..\n"); |
bcup | 1:3a338e553a54 | 16 | spi.format(8,0); |
bcup | 1:3a338e553a54 | 17 | PRINTD("Set frequency to default..\n"); |
bcup | 1:3a338e553a54 | 18 | spi.frequency(1000000); // default 1MHz |
bcup | 0:24e90e3ca3f4 | 19 | } |
bcup | 0:24e90e3ca3f4 | 20 | |
bcup | 1:3a338e553a54 | 21 | void SPI_Write() |
bcup | 0:24e90e3ca3f4 | 22 | { |
bcup | 1:3a338e553a54 | 23 | char temp; |
bcup | 1:3a338e553a54 | 24 | int i,value; |
bcup | 1:3a338e553a54 | 25 | char response; |
bcup | 1:3a338e553a54 | 26 | char tx_cnt = -1; |
bcup | 1:3a338e553a54 | 27 | char rx_cnt = -1; |
bcup | 0:24e90e3ca3f4 | 28 | char tx_buffer[255]={0}; |
bcup | 1:3a338e553a54 | 29 | char rx_buffer[255]={0}; |
bcup | 1:3a338e553a54 | 30 | PRINTD("\n==========MASTER==========\n"); |
bcup | 1:3a338e553a54 | 31 | PRINTD("DATA SEND START...\n"); |
bcup | 1:3a338e553a54 | 32 | PRINTD("Lock SPI BUS..\n"); |
bcup | 1:3a338e553a54 | 33 | |
bcup | 0:24e90e3ca3f4 | 34 | while(1) |
bcup | 0:24e90e3ca3f4 | 35 | { |
bcup | 0:24e90e3ca3f4 | 36 | |
bcup | 0:24e90e3ca3f4 | 37 | temp=getchar(); |
bcup | 1:3a338e553a54 | 38 | tx_buffer[++tx_cnt]=temp; |
bcup | 0:24e90e3ca3f4 | 39 | if(temp==0x0d) |
bcup | 0:24e90e3ca3f4 | 40 | { |
bcup | 0:24e90e3ca3f4 | 41 | tx_buffer[tx_cnt]=0; |
bcup | 0:24e90e3ca3f4 | 42 | PRINTD("\nData send Finish...\n"); |
bcup | 0:24e90e3ca3f4 | 43 | for(i=0;i<=tx_cnt;++i) |
bcup | 0:24e90e3ca3f4 | 44 | { |
bcup | 0:24e90e3ca3f4 | 45 | PRINTD("%c[%02x]",tx_buffer[i],tx_buffer[i]); |
bcup | 0:24e90e3ca3f4 | 46 | } |
bcup | 0:24e90e3ca3f4 | 47 | PRINTD("\n\n"); |
bcup | 1:3a338e553a54 | 48 | spi.lock(); |
bcup | 0:24e90e3ca3f4 | 49 | for(i=0;i<=tx_cnt;++i) |
bcup | 0:24e90e3ca3f4 | 50 | { |
bcup | 0:24e90e3ca3f4 | 51 | value=tx_buffer[i]; |
bcup | 1:3a338e553a54 | 52 | PRINTD("[M]write[%d]=%c[%02x]\n",i,value,value); |
bcup | 1:3a338e553a54 | 53 | cs=0; |
bcup | 1:3a338e553a54 | 54 | response=spi.write(value); |
bcup | 1:3a338e553a54 | 55 | cs=-1; |
bcup | 1:3a338e553a54 | 56 | PRINTD("[M]receive=%c[%x]\n",response,response); |
bcup | 1:3a338e553a54 | 57 | rx_buffer[++rx_cnt]=response; |
bcup | 0:24e90e3ca3f4 | 58 | } |
bcup | 1:3a338e553a54 | 59 | spi.unlock(); |
bcup | 1:3a338e553a54 | 60 | for(i=0;i<255;++i) |
bcup | 0:24e90e3ca3f4 | 61 | { |
bcup | 0:24e90e3ca3f4 | 62 | tx_buffer[i]=0; |
bcup | 0:24e90e3ca3f4 | 63 | } |
bcup | 1:3a338e553a54 | 64 | for(i=0;i<=tx_cnt;i++) |
bcup | 1:3a338e553a54 | 65 | { |
bcup | 1:3a338e553a54 | 66 | PRINTD("init_tx_buffer[%d]=%c\n",i,tx_buffer[i]); |
bcup | 1:3a338e553a54 | 67 | } |
bcup | 1:3a338e553a54 | 68 | tx_cnt=-1; |
bcup | 0:24e90e3ca3f4 | 69 | } |
bcup | 0:24e90e3ca3f4 | 70 | else |
bcup | 0:24e90e3ca3f4 | 71 | { |
bcup | 0:24e90e3ca3f4 | 72 | PRINTD("%c[%02x]",tx_buffer[tx_cnt],tx_buffer[tx_cnt]); |
bcup | 0:24e90e3ca3f4 | 73 | } |
bcup | 0:24e90e3ca3f4 | 74 | } |
bcup | 0:24e90e3ca3f4 | 75 | } |
bcup | 0:24e90e3ca3f4 | 76 | |
bcup | 0:24e90e3ca3f4 | 77 | int main() |
bcup | 0:24e90e3ca3f4 | 78 | { |
bcup | 1:3a338e553a54 | 79 | int send_data; |
bcup | 1:3a338e553a54 | 80 | SPI_INIT(); |
bcup | 1:3a338e553a54 | 81 | |
bcup | 0:24e90e3ca3f4 | 82 | while(1) |
bcup | 0:24e90e3ca3f4 | 83 | { |
bcup | 1:3a338e553a54 | 84 | SPI_Write(); |
bcup | 1:3a338e553a54 | 85 | } |
bcup | 1:3a338e553a54 | 86 | } |