for OV5642
Dependencies: EthernetInterface FATFileSystem GR-PEACH_video GraphicsFramework HttpServer_snapshot R_BSP mbed-rpc mbed-rtos mbed
i2c_setting.cpp@6:7ec90cc47dc4, 2015-11-02 (annotated)
- Committer:
- 1050186
- Date:
- Mon Nov 02 01:11:12 2015 +0000
- Revision:
- 6:7ec90cc47dc4
- Parent:
- 5:34d84609dd60
- Child:
- 7:c45ecff1b44d
The IP address is acquired from DHCP Server.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
1050186 | 5:34d84609dd60 | 1 | /* |
1050186 | 5:34d84609dd60 | 2 | Permission is hereby granted, free of charge, to any person obtaining a copy |
1050186 | 5:34d84609dd60 | 3 | of this software and associated documentation files (the "Software"), to deal |
1050186 | 5:34d84609dd60 | 4 | in the Software without restriction, including without limitation the rights |
1050186 | 5:34d84609dd60 | 5 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
1050186 | 5:34d84609dd60 | 6 | copies of the Software, and to permit persons to whom the Software is |
1050186 | 5:34d84609dd60 | 7 | furnished to do so, subject to the following conditions: |
1050186 | 5:34d84609dd60 | 8 | |
1050186 | 5:34d84609dd60 | 9 | The above copyright notice and this permission notice shall be included in |
1050186 | 5:34d84609dd60 | 10 | all copies or substantial portions of the Software. |
1050186 | 5:34d84609dd60 | 11 | |
1050186 | 5:34d84609dd60 | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
1050186 | 5:34d84609dd60 | 13 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
1050186 | 5:34d84609dd60 | 14 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
1050186 | 5:34d84609dd60 | 15 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
1050186 | 5:34d84609dd60 | 16 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
1050186 | 5:34d84609dd60 | 17 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
1050186 | 5:34d84609dd60 | 18 | THE SOFTWARE. |
1050186 | 5:34d84609dd60 | 19 | */ |
1050186 | 5:34d84609dd60 | 20 | |
1050186 | 5:34d84609dd60 | 21 | #include "mbed.h" |
1050186 | 5:34d84609dd60 | 22 | #include "i2c_setting.h" |
1050186 | 5:34d84609dd60 | 23 | |
1050186 | 5:34d84609dd60 | 24 | I2C i2c(I2C_SDA, I2C_SCL); |
1050186 | 5:34d84609dd60 | 25 | Serial terminal(USBTX, USBRX); |
1050186 | 5:34d84609dd60 | 26 | |
1050186 | 5:34d84609dd60 | 27 | static char recv_term_buffer[RECV_BUF_SIZE]; |
1050186 | 5:34d84609dd60 | 28 | static char reg_req_buffer[REG_REQ_BUF_SIZE]; |
1050186 | 5:34d84609dd60 | 29 | static char reg_arg_buf[ARG_MAX_NUM]; |
1050186 | 5:34d84609dd60 | 30 | static char wr_data[DATA_MAX_SIZE]; |
1050186 | 5:34d84609dd60 | 31 | static char rd_data[DATA_MAX_SIZE]; |
1050186 | 5:34d84609dd60 | 32 | static char recv_data; |
1050186 | 5:34d84609dd60 | 33 | static int32_t term_buf_offset = 0; |
1050186 | 5:34d84609dd60 | 34 | |
1050186 | 5:34d84609dd60 | 35 | static char hex_to_char_tbl[] = { |
1050186 | 5:34d84609dd60 | 36 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
1050186 | 5:34d84609dd60 | 37 | 'A', 'B', 'C', 'D', 'E', 'F' |
1050186 | 5:34d84609dd60 | 38 | }; |
1050186 | 5:34d84609dd60 | 39 | |
1050186 | 5:34d84609dd60 | 40 | static int str_to_hex(char * psrcbuf, char * pdestbuf, int cnt) { |
1050186 | 5:34d84609dd60 | 41 | int retval = false; |
1050186 | 5:34d84609dd60 | 42 | int32_t tmp_hex; |
1050186 | 5:34d84609dd60 | 43 | |
1050186 | 5:34d84609dd60 | 44 | if ((psrcbuf != NULL) && (pdestbuf != NULL)) { |
1050186 | 5:34d84609dd60 | 45 | retval = true; |
1050186 | 5:34d84609dd60 | 46 | if ((((int32_t)*psrcbuf) >= '0') && (((int32_t)*psrcbuf) <= '9')) { |
1050186 | 5:34d84609dd60 | 47 | tmp_hex = NUM_STR_TO_HEX; |
1050186 | 5:34d84609dd60 | 48 | } else if ((((int32_t)*psrcbuf) >= 'A') && (((int32_t)*psrcbuf) <= 'F')) { |
1050186 | 5:34d84609dd60 | 49 | tmp_hex = BIG_STR_TO_HEX; |
1050186 | 5:34d84609dd60 | 50 | } else if ((((int32_t)*psrcbuf) >= 'a') && (((int32_t)*psrcbuf) <= 'f')) { |
1050186 | 5:34d84609dd60 | 51 | tmp_hex = SMA_STR_TO_HEX; |
1050186 | 5:34d84609dd60 | 52 | } else { |
1050186 | 5:34d84609dd60 | 53 | retval = false; |
1050186 | 5:34d84609dd60 | 54 | } |
1050186 | 5:34d84609dd60 | 55 | if (retval == true) { |
1050186 | 5:34d84609dd60 | 56 | *pdestbuf += ((int32_t)*psrcbuf) - tmp_hex; |
1050186 | 5:34d84609dd60 | 57 | if (cnt == 0) { |
1050186 | 5:34d84609dd60 | 58 | *pdestbuf *= MASK_HEX10; |
1050186 | 5:34d84609dd60 | 59 | } |
1050186 | 5:34d84609dd60 | 60 | } |
1050186 | 5:34d84609dd60 | 61 | } |
1050186 | 5:34d84609dd60 | 62 | |
1050186 | 5:34d84609dd60 | 63 | return retval; |
1050186 | 5:34d84609dd60 | 64 | } |
1050186 | 5:34d84609dd60 | 65 | |
1050186 | 5:34d84609dd60 | 66 | static void char_to_16char(char * pdestbuf, char * psrcbuf, int length) { |
1050186 | 5:34d84609dd60 | 67 | if ((pdestbuf != NULL) && (psrcbuf != NULL)) { |
1050186 | 5:34d84609dd60 | 68 | while(length != 0) { |
1050186 | 5:34d84609dd60 | 69 | *pdestbuf = hex_to_char_tbl[((int32_t)*psrcbuf) / MASK_HEX10]; |
1050186 | 5:34d84609dd60 | 70 | pdestbuf++; |
1050186 | 5:34d84609dd60 | 71 | *pdestbuf = hex_to_char_tbl[((int32_t)*psrcbuf) % MASK_HEX10]; |
1050186 | 5:34d84609dd60 | 72 | pdestbuf++; |
1050186 | 5:34d84609dd60 | 73 | |
1050186 | 5:34d84609dd60 | 74 | psrcbuf++; |
1050186 | 5:34d84609dd60 | 75 | length--; |
1050186 | 5:34d84609dd60 | 76 | } |
1050186 | 5:34d84609dd60 | 77 | *pdestbuf = CODE_NULL; |
1050186 | 5:34d84609dd60 | 78 | } |
1050186 | 5:34d84609dd60 | 79 | } |
1050186 | 5:34d84609dd60 | 80 | static int receive_term(void) { |
1050186 | 5:34d84609dd60 | 81 | int ret = false; |
1050186 | 5:34d84609dd60 | 82 | |
1050186 | 5:34d84609dd60 | 83 | recv_data = terminal.getc(); |
1050186 | 5:34d84609dd60 | 84 | /* echo back */ |
1050186 | 5:34d84609dd60 | 85 | printf("%c", recv_data); |
1050186 | 5:34d84609dd60 | 86 | switch ((int32_t)recv_data) { |
1050186 | 5:34d84609dd60 | 87 | case 0x0A : |
1050186 | 5:34d84609dd60 | 88 | recv_term_buffer[term_buf_offset] = CODE_NULL; |
1050186 | 5:34d84609dd60 | 89 | term_buf_offset = 0; |
1050186 | 5:34d84609dd60 | 90 | ret = true; |
1050186 | 5:34d84609dd60 | 91 | break; |
1050186 | 5:34d84609dd60 | 92 | case 0x0D : |
1050186 | 5:34d84609dd60 | 93 | /* Do Nothing */ |
1050186 | 5:34d84609dd60 | 94 | break; |
1050186 | 5:34d84609dd60 | 95 | default : |
1050186 | 5:34d84609dd60 | 96 | /* check data_buffer size */ |
1050186 | 5:34d84609dd60 | 97 | if (term_buf_offset < RECV_BUF_SIZE) { |
1050186 | 5:34d84609dd60 | 98 | recv_term_buffer[term_buf_offset] = recv_data; |
1050186 | 5:34d84609dd60 | 99 | term_buf_offset++; |
1050186 | 5:34d84609dd60 | 100 | } |
1050186 | 5:34d84609dd60 | 101 | break; |
1050186 | 5:34d84609dd60 | 102 | } |
1050186 | 5:34d84609dd60 | 103 | |
1050186 | 5:34d84609dd60 | 104 | return ret; |
1050186 | 5:34d84609dd60 | 105 | |
1050186 | 5:34d84609dd60 | 106 | } |
1050186 | 5:34d84609dd60 | 107 | |
1050186 | 5:34d84609dd60 | 108 | static int analysis_cmd(char * buf) { |
1050186 | 5:34d84609dd60 | 109 | int arg_cnt; |
1050186 | 5:34d84609dd60 | 110 | int byte_cnt; |
1050186 | 5:34d84609dd60 | 111 | int retval; |
1050186 | 5:34d84609dd60 | 112 | char * psrcbuf; |
1050186 | 5:34d84609dd60 | 113 | char * pdestbuf; |
1050186 | 5:34d84609dd60 | 114 | |
1050186 | 5:34d84609dd60 | 115 | arg_cnt = 0; |
1050186 | 5:34d84609dd60 | 116 | byte_cnt = 0; |
1050186 | 5:34d84609dd60 | 117 | /* get reg req */ |
1050186 | 5:34d84609dd60 | 118 | memset(®_req_buffer[0], 0, REG_REQ_BUF_SIZE); |
1050186 | 5:34d84609dd60 | 119 | psrcbuf = buf; |
1050186 | 5:34d84609dd60 | 120 | pdestbuf = ®_req_buffer[0]; |
1050186 | 5:34d84609dd60 | 121 | while (((int32_t)*psrcbuf) != ':') { |
1050186 | 5:34d84609dd60 | 122 | if (byte_cnt >= REG_REQ_BUF_SIZE) { |
1050186 | 5:34d84609dd60 | 123 | arg_cnt = DATA_ANALY_ERROR; |
1050186 | 5:34d84609dd60 | 124 | goto err_out; |
1050186 | 5:34d84609dd60 | 125 | } else { |
1050186 | 5:34d84609dd60 | 126 | *pdestbuf = *psrcbuf; |
1050186 | 5:34d84609dd60 | 127 | byte_cnt++; |
1050186 | 5:34d84609dd60 | 128 | pdestbuf++; |
1050186 | 5:34d84609dd60 | 129 | psrcbuf++; |
1050186 | 5:34d84609dd60 | 130 | } |
1050186 | 5:34d84609dd60 | 131 | } |
1050186 | 5:34d84609dd60 | 132 | *pdestbuf = *psrcbuf; |
1050186 | 5:34d84609dd60 | 133 | if ((strncmp(®_req_buffer[0], "Wr:", REG_REQ_BUF_SIZE) == 0) || (strncmp(®_req_buffer[0], "Rd:", REG_REQ_BUF_SIZE) == 0) || |
1050186 | 5:34d84609dd60 | 134 | (strncmp(®_req_buffer[0], "WrNoP:", REG_REQ_BUF_SIZE) == 0) || (strncmp(®_req_buffer[0], "RdNoP:", REG_REQ_BUF_SIZE) == 0)) { |
1050186 | 5:34d84609dd60 | 135 | /* get argument(I2C addr, len, data1, data2, data3, ...) */ |
1050186 | 5:34d84609dd60 | 136 | byte_cnt = 0; |
1050186 | 5:34d84609dd60 | 137 | memset(®_arg_buf[0], 0, sizeof(reg_arg_buf)); |
1050186 | 5:34d84609dd60 | 138 | psrcbuf++; |
1050186 | 5:34d84609dd60 | 139 | while (((int32_t)*psrcbuf) != CODE_NULL) { |
1050186 | 5:34d84609dd60 | 140 | retval = str_to_hex(psrcbuf, ®_arg_buf[arg_cnt], byte_cnt); |
1050186 | 5:34d84609dd60 | 141 | if (retval == true) { |
1050186 | 5:34d84609dd60 | 142 | byte_cnt++; |
1050186 | 5:34d84609dd60 | 143 | psrcbuf++; |
1050186 | 5:34d84609dd60 | 144 | if (byte_cnt >= ARG_MAX_SIZE) { |
1050186 | 5:34d84609dd60 | 145 | if ((arg_cnt + 1) >= ARG_MAX_NUM) { |
1050186 | 5:34d84609dd60 | 146 | arg_cnt = DATA_ANALY_ERROR; |
1050186 | 5:34d84609dd60 | 147 | goto err_out; |
1050186 | 5:34d84609dd60 | 148 | } else { |
1050186 | 5:34d84609dd60 | 149 | arg_cnt++; |
1050186 | 5:34d84609dd60 | 150 | byte_cnt = 0; |
1050186 | 5:34d84609dd60 | 151 | } |
1050186 | 5:34d84609dd60 | 152 | } |
1050186 | 5:34d84609dd60 | 153 | } else { |
1050186 | 5:34d84609dd60 | 154 | psrcbuf++; |
1050186 | 5:34d84609dd60 | 155 | } |
1050186 | 5:34d84609dd60 | 156 | } |
1050186 | 5:34d84609dd60 | 157 | } else { |
1050186 | 5:34d84609dd60 | 158 | arg_cnt = DATA_ANALY_ERROR; |
1050186 | 5:34d84609dd60 | 159 | goto err_out; |
1050186 | 5:34d84609dd60 | 160 | } |
1050186 | 5:34d84609dd60 | 161 | |
1050186 | 5:34d84609dd60 | 162 | err_out: |
1050186 | 5:34d84609dd60 | 163 | |
1050186 | 5:34d84609dd60 | 164 | return arg_cnt; |
1050186 | 5:34d84609dd60 | 165 | } |
1050186 | 5:34d84609dd60 | 166 | |
1050186 | 5:34d84609dd60 | 167 | static void execute_cmd(void) { |
1050186 | 5:34d84609dd60 | 168 | char convert_data[(DATA_MAX_SIZE * 2) + NULL_SIZE]; |
1050186 | 5:34d84609dd60 | 169 | |
1050186 | 5:34d84609dd60 | 170 | /* check length */ |
1050186 | 5:34d84609dd60 | 171 | if (reg_arg_buf[1] >= DATA_MAX_SIZE) { |
1050186 | 5:34d84609dd60 | 172 | reg_arg_buf[1] = DATA_MAX_SIZE; |
1050186 | 5:34d84609dd60 | 173 | } |
1050186 | 5:34d84609dd60 | 174 | /* check request */ |
1050186 | 5:34d84609dd60 | 175 | if (strncmp(®_req_buffer[0], "Wr:", REG_REQ_BUF_SIZE) == 0) { |
1050186 | 5:34d84609dd60 | 176 | /* write */ |
1050186 | 5:34d84609dd60 | 177 | memcpy(&wr_data[0], ®_arg_buf[2], reg_arg_buf[1]); |
1050186 | 5:34d84609dd60 | 178 | (void)i2c.write(reg_arg_buf[0], wr_data, reg_arg_buf[1], 0); // write(I2C Addr, write data, len, restart flg(=0)) |
1050186 | 5:34d84609dd60 | 179 | } else if (strncmp(®_req_buffer[0], "Rd:", REG_REQ_BUF_SIZE) == 0) { |
1050186 | 5:34d84609dd60 | 180 | /* read */ |
1050186 | 5:34d84609dd60 | 181 | memset(&rd_data[0], 0, reg_arg_buf[1]); |
1050186 | 5:34d84609dd60 | 182 | (void)i2c.read(reg_arg_buf[0], rd_data, reg_arg_buf[1], 0); // read(I2C Addr, read data, len, restart flg(=0)) |
1050186 | 5:34d84609dd60 | 183 | |
1050186 | 5:34d84609dd60 | 184 | char_to_16char(&convert_data[0], &rd_data[0], reg_arg_buf[1]); |
1050186 | 5:34d84609dd60 | 185 | DEBUGPRINT(" Read Data is %s\r\n", &convert_data[0]); |
1050186 | 5:34d84609dd60 | 186 | } else if (strncmp(®_req_buffer[0], "WrNoP:", REG_REQ_BUF_SIZE) == 0) { |
1050186 | 5:34d84609dd60 | 187 | /* write(non stop condition) */ |
1050186 | 5:34d84609dd60 | 188 | memcpy(&wr_data[0], ®_arg_buf[2], reg_arg_buf[1]); |
1050186 | 5:34d84609dd60 | 189 | (void)i2c.write(reg_arg_buf[0], wr_data, reg_arg_buf[1], 1); // write(I2C Addr, write data, len, restart flg(=1)) |
1050186 | 5:34d84609dd60 | 190 | } else if (strncmp(®_req_buffer[0], "RdNoP:", REG_REQ_BUF_SIZE) == 0) { |
1050186 | 5:34d84609dd60 | 191 | /* read(non stop condition) */ |
1050186 | 5:34d84609dd60 | 192 | memset(&rd_data[0], 0, reg_arg_buf[1]); |
1050186 | 5:34d84609dd60 | 193 | (void)i2c.read(reg_arg_buf[0], rd_data, reg_arg_buf[1], 1); // read(I2C Addr, read data, len, restart flg(=1)) |
1050186 | 5:34d84609dd60 | 194 | |
1050186 | 5:34d84609dd60 | 195 | char_to_16char(&convert_data[0], &rd_data[0], reg_arg_buf[1]); |
1050186 | 5:34d84609dd60 | 196 | DEBUGPRINT(" Read Data is %s\r\n", &convert_data[0]); |
1050186 | 5:34d84609dd60 | 197 | } |
1050186 | 5:34d84609dd60 | 198 | } |
1050186 | 5:34d84609dd60 | 199 | |
1050186 | 5:34d84609dd60 | 200 | void analy_and_exe(char * buf) { |
1050186 | 5:34d84609dd60 | 201 | int reg_arg_cnt; |
1050186 | 5:34d84609dd60 | 202 | |
1050186 | 5:34d84609dd60 | 203 | /* analysis command */ |
1050186 | 5:34d84609dd60 | 204 | reg_arg_cnt = analysis_cmd(buf); |
1050186 | 5:34d84609dd60 | 205 | if (reg_arg_cnt != DATA_ANALY_ERROR) { |
1050186 | 5:34d84609dd60 | 206 | /* execute command */ |
1050186 | 5:34d84609dd60 | 207 | execute_cmd(); |
1050186 | 5:34d84609dd60 | 208 | } |
1050186 | 5:34d84609dd60 | 209 | } |
1050186 | 5:34d84609dd60 | 210 | |
1050186 | 5:34d84609dd60 | 211 | void SetI2CfromTerm(void const *argument) { |
1050186 | 5:34d84609dd60 | 212 | int ret; |
1050186 | 5:34d84609dd60 | 213 | |
1050186 | 5:34d84609dd60 | 214 | while (1) { |
1050186 | 5:34d84609dd60 | 215 | ret = receive_term(); |
1050186 | 5:34d84609dd60 | 216 | if (ret == true) { |
1050186 | 5:34d84609dd60 | 217 | /* command analysis and execute */ |
1050186 | 5:34d84609dd60 | 218 | analy_and_exe(&recv_term_buffer[0]); |
1050186 | 5:34d84609dd60 | 219 | } |
1050186 | 5:34d84609dd60 | 220 | } |
1050186 | 5:34d84609dd60 | 221 | } |
1050186 | 5:34d84609dd60 | 222 |