Serial communication protocol generic implementation
Dependents: ClassFRDM ClassLPC
DataComm.cpp@0:60c4436f7667, 2015-03-19 (annotated)
- Committer:
- askksa12543
- Date:
- Thu Mar 19 02:36:11 2015 +0000
- Revision:
- 0:60c4436f7667
- Child:
- 1:9cfb17f74dcd
initial class version of data comm project.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
askksa12543 | 0:60c4436f7667 | 1 | #include "DataComm.h" |
askksa12543 | 0:60c4436f7667 | 2 | |
askksa12543 | 0:60c4436f7667 | 3 | void DataComm::setClockOut(PinName pin) |
askksa12543 | 0:60c4436f7667 | 4 | { |
askksa12543 | 0:60c4436f7667 | 5 | gpio_init_out_ex(&clock_out, pin, 0); |
askksa12543 | 0:60c4436f7667 | 6 | } |
askksa12543 | 0:60c4436f7667 | 7 | |
askksa12543 | 0:60c4436f7667 | 8 | void DataComm::setClockIn(PinName pin) |
askksa12543 | 0:60c4436f7667 | 9 | { |
askksa12543 | 0:60c4436f7667 | 10 | gpio_init_in(&clock_in, pin); |
askksa12543 | 0:60c4436f7667 | 11 | } |
askksa12543 | 0:60c4436f7667 | 12 | |
askksa12543 | 0:60c4436f7667 | 13 | void DataComm::setSerialOut(PinName pin) |
askksa12543 | 0:60c4436f7667 | 14 | { |
askksa12543 | 0:60c4436f7667 | 15 | gpio_init_out_ex(&serial_out, pin, 0); |
askksa12543 | 0:60c4436f7667 | 16 | } |
askksa12543 | 0:60c4436f7667 | 17 | |
askksa12543 | 0:60c4436f7667 | 18 | void DataComm::setSerialIn(PinName pin) |
askksa12543 | 0:60c4436f7667 | 19 | { |
askksa12543 | 0:60c4436f7667 | 20 | gpio_init_in(&serial_in, pin); |
askksa12543 | 0:60c4436f7667 | 21 | } |
askksa12543 | 0:60c4436f7667 | 22 | |
askksa12543 | 0:60c4436f7667 | 23 | void DataComm::listen() |
askksa12543 | 0:60c4436f7667 | 24 | { |
askksa12543 | 0:60c4436f7667 | 25 | int pre = 0, data_flag = 1; |
askksa12543 | 0:60c4436f7667 | 26 | char temp = 0; |
askksa12543 | 0:60c4436f7667 | 27 | |
askksa12543 | 0:60c4436f7667 | 28 | while(!pre) |
askksa12543 | 0:60c4436f7667 | 29 | { |
askksa12543 | 0:60c4436f7667 | 30 | //read in data if clock is 1 and data flag is 1 |
askksa12543 | 0:60c4436f7667 | 31 | if(gpio_read(&clock_in) && data_flag) |
askksa12543 | 0:60c4436f7667 | 32 | { |
askksa12543 | 0:60c4436f7667 | 33 | //data is left shifted into our temporary variable. |
askksa12543 | 0:60c4436f7667 | 34 | //each new data bit is moved into the least significant bit after the rest of the bits are shifted to the left |
askksa12543 | 0:60c4436f7667 | 35 | //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit. |
askksa12543 | 0:60c4436f7667 | 36 | temp = (temp << 1) + gpio_read(&serial_in); |
askksa12543 | 0:60c4436f7667 | 37 | data_flag = 0; |
askksa12543 | 0:60c4436f7667 | 38 | if(temp == PREAMBLE) |
askksa12543 | 0:60c4436f7667 | 39 | { |
askksa12543 | 0:60c4436f7667 | 40 | pre = 1; |
askksa12543 | 0:60c4436f7667 | 41 | } |
askksa12543 | 0:60c4436f7667 | 42 | } |
askksa12543 | 0:60c4436f7667 | 43 | //when clock returns to low - reset data flag to accept the next bit. |
askksa12543 | 0:60c4436f7667 | 44 | if(!gpio_read(&clock_in) && !data_flag) |
askksa12543 | 0:60c4436f7667 | 45 | { |
askksa12543 | 0:60c4436f7667 | 46 | data_flag = 1; |
askksa12543 | 0:60c4436f7667 | 47 | } |
askksa12543 | 0:60c4436f7667 | 48 | } |
askksa12543 | 0:60c4436f7667 | 49 | } |
askksa12543 | 0:60c4436f7667 | 50 | |
askksa12543 | 0:60c4436f7667 | 51 | void DataComm::setClock(int clock) |
askksa12543 | 0:60c4436f7667 | 52 | { |
askksa12543 | 0:60c4436f7667 | 53 | clock_time = clock; |
askksa12543 | 0:60c4436f7667 | 54 | skew_time = (clock * 9)/10; |
askksa12543 | 0:60c4436f7667 | 55 | } |
askksa12543 | 0:60c4436f7667 | 56 | |
askksa12543 | 0:60c4436f7667 | 57 | void DataComm::initiate_connection() |
askksa12543 | 0:60c4436f7667 | 58 | { |
askksa12543 | 0:60c4436f7667 | 59 | int j=128, done=0, skew_flag=0; |
askksa12543 | 0:60c4436f7667 | 60 | |
askksa12543 | 0:60c4436f7667 | 61 | //output preamble |
askksa12543 | 0:60c4436f7667 | 62 | while(!done) |
askksa12543 | 0:60c4436f7667 | 63 | { |
askksa12543 | 0:60c4436f7667 | 64 | //start timer for clock |
askksa12543 | 0:60c4436f7667 | 65 | t.start(); |
askksa12543 | 0:60c4436f7667 | 66 | //wait until the timer has reached the set time. |
askksa12543 | 0:60c4436f7667 | 67 | while(t.read_ms() < clock_time) |
askksa12543 | 0:60c4436f7667 | 68 | { |
askksa12543 | 0:60c4436f7667 | 69 | //extract data just before clock goes high |
askksa12543 | 0:60c4436f7667 | 70 | if(!gpio_read(&clock_out) && skew_flag && t.read_ms() > skew_time) |
askksa12543 | 0:60c4436f7667 | 71 | { |
askksa12543 | 0:60c4436f7667 | 72 | //extract data bit |
askksa12543 | 0:60c4436f7667 | 73 | gpio_write(&serial_out, ((PREAMBLE / j) % 2)); |
askksa12543 | 0:60c4436f7667 | 74 | skew_flag = 0; |
askksa12543 | 0:60c4436f7667 | 75 | j /= 2; //decrement j to get to next bit location |
askksa12543 | 0:60c4436f7667 | 76 | } |
askksa12543 | 0:60c4436f7667 | 77 | } |
askksa12543 | 0:60c4436f7667 | 78 | //stop and reset the timer |
askksa12543 | 0:60c4436f7667 | 79 | t.stop(); |
askksa12543 | 0:60c4436f7667 | 80 | t.reset(); |
askksa12543 | 0:60c4436f7667 | 81 | //switch clock signal |
askksa12543 | 0:60c4436f7667 | 82 | gpio_write(&clock_out, !gpio_read(&clock_out)); |
askksa12543 | 0:60c4436f7667 | 83 | //reset skew flag |
askksa12543 | 0:60c4436f7667 | 84 | skew_flag = 1; |
askksa12543 | 0:60c4436f7667 | 85 | //last preamble bit sent - return from function |
askksa12543 | 0:60c4436f7667 | 86 | if(j==0) |
askksa12543 | 0:60c4436f7667 | 87 | { |
askksa12543 | 0:60c4436f7667 | 88 | done = 1; |
askksa12543 | 0:60c4436f7667 | 89 | } |
askksa12543 | 0:60c4436f7667 | 90 | } |
askksa12543 | 0:60c4436f7667 | 91 | } |
askksa12543 | 0:60c4436f7667 | 92 | |
askksa12543 | 0:60c4436f7667 | 93 | void DataComm::setDataSize(int size) |
askksa12543 | 0:60c4436f7667 | 94 | { |
askksa12543 | 0:60c4436f7667 | 95 | data_size = size; |
askksa12543 | 0:60c4436f7667 | 96 | } |
askksa12543 | 0:60c4436f7667 | 97 | |
askksa12543 | 0:60c4436f7667 | 98 | void DataComm::send_data(char data[]) |
askksa12543 | 0:60c4436f7667 | 99 | { |
askksa12543 | 0:60c4436f7667 | 100 | int i=0, j=128, done=0, skew_flag=0; |
askksa12543 | 0:60c4436f7667 | 101 | |
askksa12543 | 0:60c4436f7667 | 102 | //output data |
askksa12543 | 0:60c4436f7667 | 103 | while(!done) |
askksa12543 | 0:60c4436f7667 | 104 | { |
askksa12543 | 0:60c4436f7667 | 105 | //start timer for clock |
askksa12543 | 0:60c4436f7667 | 106 | t.start(); |
askksa12543 | 0:60c4436f7667 | 107 | //wait until the timer has reached the set time. |
askksa12543 | 0:60c4436f7667 | 108 | while(t.read_ms() < clock_time) |
askksa12543 | 0:60c4436f7667 | 109 | { |
askksa12543 | 0:60c4436f7667 | 110 | //extract data just before clock goes high |
askksa12543 | 0:60c4436f7667 | 111 | if(!gpio_read(&clock_out) && skew_flag && t.read_ms() > skew_time) |
askksa12543 | 0:60c4436f7667 | 112 | { |
askksa12543 | 0:60c4436f7667 | 113 | //extract data bit |
askksa12543 | 0:60c4436f7667 | 114 | gpio_write(&serial_out, ((data[i] / j) % 2)); |
askksa12543 | 0:60c4436f7667 | 115 | skew_flag = 0; |
askksa12543 | 0:60c4436f7667 | 116 | j /= 2; //decrement j to get to next bit location |
askksa12543 | 0:60c4436f7667 | 117 | } |
askksa12543 | 0:60c4436f7667 | 118 | } |
askksa12543 | 0:60c4436f7667 | 119 | //stop and reset the timer |
askksa12543 | 0:60c4436f7667 | 120 | t.stop(); |
askksa12543 | 0:60c4436f7667 | 121 | t.reset(); |
askksa12543 | 0:60c4436f7667 | 122 | //switch clock signal |
askksa12543 | 0:60c4436f7667 | 123 | gpio_write(&clock_out, !gpio_read(&clock_out)); |
askksa12543 | 0:60c4436f7667 | 124 | //reset skew flag |
askksa12543 | 0:60c4436f7667 | 125 | skew_flag = 1; |
askksa12543 | 0:60c4436f7667 | 126 | //last preamble bit sent - return from function |
askksa12543 | 0:60c4436f7667 | 127 | if(j==0) |
askksa12543 | 0:60c4436f7667 | 128 | { |
askksa12543 | 0:60c4436f7667 | 129 | i++; |
askksa12543 | 0:60c4436f7667 | 130 | if(i>data_size) |
askksa12543 | 0:60c4436f7667 | 131 | done = 1; |
askksa12543 | 0:60c4436f7667 | 132 | j=128; |
askksa12543 | 0:60c4436f7667 | 133 | } |
askksa12543 | 0:60c4436f7667 | 134 | } |
askksa12543 | 0:60c4436f7667 | 135 | } |
askksa12543 | 0:60c4436f7667 | 136 | |
askksa12543 | 0:60c4436f7667 | 137 | char* DataComm::receive_data() |
askksa12543 | 0:60c4436f7667 | 138 | { |
askksa12543 | 0:60c4436f7667 | 139 | int i=0, j=0, done = 0, data_flag = 1; |
askksa12543 | 0:60c4436f7667 | 140 | char temp[data_size]; |
askksa12543 | 0:60c4436f7667 | 141 | |
askksa12543 | 0:60c4436f7667 | 142 | while(!done) |
askksa12543 | 0:60c4436f7667 | 143 | { |
askksa12543 | 0:60c4436f7667 | 144 | //read in data if clock is 1 and data flag is 1 |
askksa12543 | 0:60c4436f7667 | 145 | if(gpio_read(&clock_in) && data_flag) |
askksa12543 | 0:60c4436f7667 | 146 | { |
askksa12543 | 0:60c4436f7667 | 147 | //data is left shifted into our temporary variable. |
askksa12543 | 0:60c4436f7667 | 148 | //each new data bit is moved into the least significant bit after the rest of the bits are shifted to the left |
askksa12543 | 0:60c4436f7667 | 149 | //data must be sent from the other microcontroller shifted out from the most significant bit to the least significant bit. |
askksa12543 | 0:60c4436f7667 | 150 | temp[i] = (temp[i] << 1) + gpio_read(&serial_in); |
askksa12543 | 0:60c4436f7667 | 151 | data_flag = 0; |
askksa12543 | 0:60c4436f7667 | 152 | j++; |
askksa12543 | 0:60c4436f7667 | 153 | } |
askksa12543 | 0:60c4436f7667 | 154 | //when clock returns to low - reset data flag to accept the next bit. |
askksa12543 | 0:60c4436f7667 | 155 | if(!gpio_read(&clock_in) && !data_flag) |
askksa12543 | 0:60c4436f7667 | 156 | { |
askksa12543 | 0:60c4436f7667 | 157 | data_flag = 1; |
askksa12543 | 0:60c4436f7667 | 158 | } |
askksa12543 | 0:60c4436f7667 | 159 | if(j>7) |
askksa12543 | 0:60c4436f7667 | 160 | { |
askksa12543 | 0:60c4436f7667 | 161 | i++; |
askksa12543 | 0:60c4436f7667 | 162 | if(i>data_size) |
askksa12543 | 0:60c4436f7667 | 163 | done=1; |
askksa12543 | 0:60c4436f7667 | 164 | j=0; |
askksa12543 | 0:60c4436f7667 | 165 | } |
askksa12543 | 0:60c4436f7667 | 166 | } |
askksa12543 | 0:60c4436f7667 | 167 | return temp; |
askksa12543 | 0:60c4436f7667 | 168 | } |