/* * utils.c * * Copyright (C) 2018, 2019 Mind Chasers Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ /* Standard C Included Files */ #include #include #include #include /* SDK Included Files */ #include "board.h" #include "fsl_debug_console.h" #include "fsl_i2c.h" #include "main.h" /* * Stop dumping the data when we msbit is set */ void dump_data(uint8_t * pdata, size_t size, bool i2c_data ) { for (uint32_t i = 0U; i < size; i++) { if (!i2c_data && (i % 8 == 0)) { printf("\r\n"); } // printf("0x%2x ", *pdata++); PUTCHAR( *pdata++ & 0x7f ); if (i2c_data && (*pdata & 0x80) ) { // printf("0x%2x ", *pdata); PUTCHAR( *pdata & 0x7f ); break; } } PRINTF("\r\n"); } uint8_t get_cmd(uint8_t * pdata, size_t size) { int i=0; char buf; if (size <= 1) return 0; buf = (char) GETCHAR(); PUTCHAR(buf); if (buf == 13 ) { PUTCHAR(10); // LF return 0; } *pdata = buf; // capture the cmd for(i=1; i= 48 && buf <= 57) { buf = buf-48; } // Convert A through F ASCII to int base 16 (hex) else if (buf >= 65 && buf <= 70) { buf = buf-55; } // Convert a through f ASCII to int base 16 (hex) else if (buf >= 97 && buf <= 102) { buf = buf-87; } else if (buf == 13) { pdata[i] = buf; break; } else { PUTCHAR(10); // LF return 0; // error } pdata[i] = buf; } PUTCHAR(10); return i+1; } /* convert ASCII hex characters to integers */ uint8_t ascii_to_bin(uint8_t data) { uint8_t temp = 0; if (data > 0x39) { temp = data - 0x57; // a should map to 10 and so on } else { temp = data - 0x30; } return temp; } status_t i2c_write(i2c_master_transfer_t* pxfer, uint8_t devaddr, uint8_t * pdata, size_t size) { status_t result = kStatus_Success; pxfer->slaveAddress = devaddr; pxfer->direction = kI2C_Write; pxfer->data = pdata; pxfer->dataSize = size; return( I2C_MasterTransferBlocking(I2C_MASTER_BASEADDR, pxfer)); if (result!=kStatus_Success) { // printf("\r\n I2C Transfer Error \r\n"); return -1; } else { return 0; } } status_t i2c_read(i2c_master_transfer_t* pxfer, uint8_t devaddr, uint8_t * pdata, size_t size) { status_t result = kStatus_Success; pxfer->slaveAddress = devaddr; pxfer->direction = kI2C_Read; pxfer->data = pdata; pxfer->dataSize = size; result = I2C_MasterTransferBlocking(I2C_MASTER_BASEADDR, pxfer); if (result!=kStatus_Success) { // printf("\r\n I2C Transfer Error \r\n"); return -1; } else { return 0; } }