test
Dependencies: MTS-Serial libmDot mbed-rtos mbed
Fork of mDot_LoRa_Connect_Example by
spi_write.cpp@6:f87d57498fd0, 2017-11-08 (annotated)
- Committer:
- hmiot
- Date:
- Wed Nov 08 12:10:51 2017 +0000
- Revision:
- 6:f87d57498fd0
- Parent:
- 5:f16b067257b6
Updated for Zigo Demo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hariom | 2:20a5d4681abf | 1 | #include "mbed.h" |
hariom | 2:20a5d4681abf | 2 | #include"Thread.h" |
hariom | 2:20a5d4681abf | 3 | #include "rtos.h" |
hariom | 2:20a5d4681abf | 4 | Serial pc(USBTX, USBRX); // tx, rx |
hariom | 2:20a5d4681abf | 5 | |
hariom | 2:20a5d4681abf | 6 | SPI spimaster(SPI1_MOSI, SPI1_MISO, SPI1_SCK); |
hariom | 2:20a5d4681abf | 7 | DigitalOut cs(SPI1_CS); |
hariom | 2:20a5d4681abf | 8 | extern int first_time; |
hariom | 2:20a5d4681abf | 9 | extern int device_buffer[8]; |
hariom | 2:20a5d4681abf | 10 | int ble_flag; |
hariom | 2:20a5d4681abf | 11 | extern Mutex my_mutex; |
hariom | 2:20a5d4681abf | 12 | |
varshaberya123 | 5:f16b067257b6 | 13 | |
varshaberya123 | 5:f16b067257b6 | 14 | // reading data from spi |
varshaberya123 | 5:f16b067257b6 | 15 | // spi is communnication between ble and mdot device |
varshaberya123 | 5:f16b067257b6 | 16 | /* |
varshaberya123 | 5:f16b067257b6 | 17 | int spi_read(int *buff) |
varshaberya123 | 5:f16b067257b6 | 18 | { |
varshaberya123 | 5:f16b067257b6 | 19 | pc.baud(9600); |
varshaberya123 | 5:f16b067257b6 | 20 | printf("...SPI read test ..\n\r "); |
varshaberya123 | 5:f16b067257b6 | 21 | // Chip must be deselected |
varshaberya123 | 5:f16b067257b6 | 22 | cs = 1; |
varshaberya123 | 5:f16b067257b6 | 23 | spi.formate(8,0); |
varshaberya123 | 5:f16b067257b6 | 24 | spimaster.frequency(1000000); |
varshaberya123 | 5:f16b067257b6 | 25 | |
varshaberya123 | 5:f16b067257b6 | 26 | // Chip must selected |
varshaberya123 | 5:f16b067257b6 | 27 | cs = 0; |
varshaberya123 | 5:f16b067257b6 | 28 | int SUCCESS = 1; |
varshaberya123 | 5:f16b067257b6 | 29 | |
varshaberya123 | 5:f16b067257b6 | 30 | //need to figure out how many bytes going to read from ble |
varshaberya123 | 5:f16b067257b6 | 31 | if ( spimaster.read(buff) != SUCCESS) |
varshaberya123 | 5:f16b067257b6 | 32 | { |
varshaberya123 | 5:f16b067257b6 | 33 | printf("reading failed !!!"); |
varshaberya123 | 5:f16b067257b6 | 34 | return 0; |
varshaberya123 | 5:f16b067257b6 | 35 | } |
varshaberya123 | 5:f16b067257b6 | 36 | printf("Reading success read data : %X",buff[0]); |
varshaberya123 | 5:f16b067257b6 | 37 | return 1; |
varshaberya123 | 5:f16b067257b6 | 38 | }*/ |
varshaberya123 | 5:f16b067257b6 | 39 | |
hariom | 2:20a5d4681abf | 40 | int spi_write(int *buf) |
hariom | 2:20a5d4681abf | 41 | { |
hariom | 2:20a5d4681abf | 42 | int response[8]; |
hariom | 2:20a5d4681abf | 43 | cs = 1; |
hariom | 2:20a5d4681abf | 44 | pc.baud(9600); |
hariom | 2:20a5d4681abf | 45 | printf("SPI test..\n\r"); |
hariom | 2:20a5d4681abf | 46 | //int temp; |
hmiot | 6:f87d57498fd0 | 47 | printf("buff[0] = %d\r\n", buf[0]); |
hmiot | 6:f87d57498fd0 | 48 | int size = buf[0] + 2; |
hmiot | 6:f87d57498fd0 | 49 | printf("size = %d\r\n", size); |
hmiot | 6:f87d57498fd0 | 50 | int k, index = 0; |
hmiot | 6:f87d57498fd0 | 51 | int write_buff[16]; |
hariom | 2:20a5d4681abf | 52 | // Chip must be deselected |
hariom | 2:20a5d4681abf | 53 | cs = 1; |
hmiot | 6:f87d57498fd0 | 54 | write_buff[index] = 0x86; |
hmiot | 6:f87d57498fd0 | 55 | index++; |
hmiot | 6:f87d57498fd0 | 56 | for (k =0 ; k< size ; k++, index++) |
hmiot | 6:f87d57498fd0 | 57 | { |
hmiot | 6:f87d57498fd0 | 58 | write_buff[index] = buf[k]; |
hmiot | 6:f87d57498fd0 | 59 | printf("index = %d, size=%d\r\n", index, size); |
hmiot | 6:f87d57498fd0 | 60 | } |
hmiot | 6:f87d57498fd0 | 61 | |
hmiot | 6:f87d57498fd0 | 62 | printf("index = %d, size=%d\r\n", index, size); |
hmiot | 6:f87d57498fd0 | 63 | write_buff[index] = 0x88; |
hmiot | 6:f87d57498fd0 | 64 | printf("index = %d, size=%d\r\n", index, size); |
hmiot | 6:f87d57498fd0 | 65 | printf("index value = %d \r\n", index); |
hmiot | 6:f87d57498fd0 | 66 | |
hariom | 2:20a5d4681abf | 67 | //printf("spi data length is:%d\r\n", size); |
hariom | 2:20a5d4681abf | 68 | // Setup the spi for 8 bit data, high steady state clock, |
hariom | 2:20a5d4681abf | 69 | // second edge capture, with a 1MHz clock rate |
hariom | 2:20a5d4681abf | 70 | spimaster.format(8,0); |
hariom | 2:20a5d4681abf | 71 | spimaster.frequency(1000000); |
hariom | 2:20a5d4681abf | 72 | |
hariom | 2:20a5d4681abf | 73 | int i = 0; |
hmiot | 6:f87d57498fd0 | 74 | |
hmiot | 6:f87d57498fd0 | 75 | while(i <= index) |
hariom | 2:20a5d4681abf | 76 | { |
hariom | 2:20a5d4681abf | 77 | //osDelay(1000); |
hariom | 2:20a5d4681abf | 78 | // Select the device by seting chip select low |
hariom | 2:20a5d4681abf | 79 | cs = 0; |
hmiot | 6:f87d57498fd0 | 80 | response[i] = spimaster.write(write_buff[i]); |
hariom | 2:20a5d4681abf | 81 | cs = 1; |
hmiot | 6:f87d57498fd0 | 82 | printf("buf[%d] = %X\r\n", i ,write_buff[i]); |
hmiot | 6:f87d57498fd0 | 83 | printf("response[%d] = %X\r\n", i , response[i]); |
hariom | 2:20a5d4681abf | 84 | i++; |
hmiot | 6:f87d57498fd0 | 85 | |
hariom | 2:20a5d4681abf | 86 | //printf("\r\nSlave response = 0x%X, 0x%X, 0x%X, 0x%X", response[0], response[1], response[2], response[3]); |
hariom | 2:20a5d4681abf | 87 | } |
hariom | 2:20a5d4681abf | 88 | |
hariom | 2:20a5d4681abf | 89 | //printf("master send: %s\r\n", buf); |
hariom | 2:20a5d4681abf | 90 | //printf("slave response: %s\r\n", response); |
hariom | 2:20a5d4681abf | 91 | printf("exiting from spi write\r\n"); |
hariom | 2:20a5d4681abf | 92 | return response[0]; |
hariom | 2:20a5d4681abf | 93 | } |
hariom | 2:20a5d4681abf | 94 | |
hariom | 2:20a5d4681abf | 95 | |
hariom | 2:20a5d4681abf | 96 | void ble_task(void const *args) |
hariom | 2:20a5d4681abf | 97 | { |
hariom | 2:20a5d4681abf | 98 | printf("started ble task\r\n"); |
hmiot | 6:f87d57498fd0 | 99 | int ble_buffer[16]; |
hariom | 2:20a5d4681abf | 100 | int i; |
hariom | 2:20a5d4681abf | 101 | while (true) { |
hariom | 2:20a5d4681abf | 102 | if (ble_flag) { |
hariom | 2:20a5d4681abf | 103 | my_mutex.lock(); |
hmiot | 6:f87d57498fd0 | 104 | // printf("Device buff Length %d \r\n",); |
hmiot | 6:f87d57498fd0 | 105 | printf("send Size %d\r\n", device_buffer[0]); |
hmiot | 6:f87d57498fd0 | 106 | memset(ble_buffer,'\0', 16); |
hmiot | 6:f87d57498fd0 | 107 | for (i = 0; i <= device_buffer[0] + 2; i++) |
hariom | 2:20a5d4681abf | 108 | ble_buffer[i] = device_buffer[i]; |
hariom | 2:20a5d4681abf | 109 | my_mutex.unlock(); |
hariom | 2:20a5d4681abf | 110 | printf("spi write called\r\n"); |
hmiot | 6:f87d57498fd0 | 111 | |
hariom | 2:20a5d4681abf | 112 | spi_write(ble_buffer); |
hariom | 2:20a5d4681abf | 113 | ble_flag = 0; |
hariom | 2:20a5d4681abf | 114 | } |
hariom | 2:20a5d4681abf | 115 | osDelay(1000); |
hariom | 2:20a5d4681abf | 116 | } |
varshaberya123 | 5:f16b067257b6 | 117 | } |
varshaberya123 | 5:f16b067257b6 | 118 | |
varshaberya123 | 5:f16b067257b6 | 119 | /*void ble_energymeter(void const *argc) |
varshaberya123 | 5:f16b067257b6 | 120 | { |
varshaberya123 | 5:f16b067257b6 | 121 | printf("Starting energymeter ble task \r\n"); |
varshaberya123 | 5:f16b067257b6 | 122 | int buff[8]; |
varshaberya123 | 5:f16b067257b6 | 123 | while(true) |
varshaberya123 | 5:f16b067257b6 | 124 | { |
varshaberya123 | 5:f16b067257b6 | 125 | if(ble_eflag) |
varshaberya123 | 5:f16b067257b6 | 126 | { |
varshaberya123 | 5:f16b067257b6 | 127 | printf("calling spi read \r\n"); |
varshaberya123 | 5:f16b067257b6 | 128 | spi_read(buff); |
varshaberya123 | 5:f16b067257b6 | 129 | ble_eflag = 0; |
varshaberya123 | 5:f16b067257b6 | 130 | } |
varshaberya123 | 5:f16b067257b6 | 131 | osDelay(1000); |
varshaberya123 | 5:f16b067257b6 | 132 | } |
varshaberya123 | 5:f16b067257b6 | 133 | }*/ |