Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
utils/i2cconfig.cpp@15:5433f9d94cd7, 2015-08-28 (annotated)
- Committer:
- dflet
- Date:
- Fri Aug 28 21:04:44 2015 +0000
- Revision:
- 15:5433f9d94cd7
- Child:
- 18:3f1b52616d00
Cam ov2640 working correctly, didnt know it also gave a jpeg header. App still not stable. Still a lot of work needed.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| dflet | 15:5433f9d94cd7 | 1 | //***************************************************************************** |
| dflet | 15:5433f9d94cd7 | 2 | // i2cconfig.c |
| dflet | 15:5433f9d94cd7 | 3 | // |
| dflet | 15:5433f9d94cd7 | 4 | // I2C features APIs |
| dflet | 15:5433f9d94cd7 | 5 | // |
| dflet | 15:5433f9d94cd7 | 6 | // Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/ |
| dflet | 15:5433f9d94cd7 | 7 | // |
| dflet | 15:5433f9d94cd7 | 8 | // |
| dflet | 15:5433f9d94cd7 | 9 | // Redistribution and use in source and binary forms, with or without |
| dflet | 15:5433f9d94cd7 | 10 | // modification, are permitted provided that the following conditions |
| dflet | 15:5433f9d94cd7 | 11 | // are met: |
| dflet | 15:5433f9d94cd7 | 12 | // |
| dflet | 15:5433f9d94cd7 | 13 | // Redistributions of source code must retain the above copyright |
| dflet | 15:5433f9d94cd7 | 14 | // notice, this list of conditions and the following disclaimer. |
| dflet | 15:5433f9d94cd7 | 15 | // |
| dflet | 15:5433f9d94cd7 | 16 | // Redistributions in binary form must reproduce the above copyright |
| dflet | 15:5433f9d94cd7 | 17 | // notice, this list of conditions and the following disclaimer in the |
| dflet | 15:5433f9d94cd7 | 18 | // documentation and/or other materials provided with the |
| dflet | 15:5433f9d94cd7 | 19 | // distribution. |
| dflet | 15:5433f9d94cd7 | 20 | // |
| dflet | 15:5433f9d94cd7 | 21 | // Neither the name of Texas Instruments Incorporated nor the names of |
| dflet | 15:5433f9d94cd7 | 22 | // its contributors may be used to endorse or promote products derived |
| dflet | 15:5433f9d94cd7 | 23 | // from this software without specific prior written permission. |
| dflet | 15:5433f9d94cd7 | 24 | // |
| dflet | 15:5433f9d94cd7 | 25 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| dflet | 15:5433f9d94cd7 | 26 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| dflet | 15:5433f9d94cd7 | 27 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| dflet | 15:5433f9d94cd7 | 28 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| dflet | 15:5433f9d94cd7 | 29 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| dflet | 15:5433f9d94cd7 | 30 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| dflet | 15:5433f9d94cd7 | 31 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| dflet | 15:5433f9d94cd7 | 32 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| dflet | 15:5433f9d94cd7 | 33 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| dflet | 15:5433f9d94cd7 | 34 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| dflet | 15:5433f9d94cd7 | 35 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| dflet | 15:5433f9d94cd7 | 36 | // |
| dflet | 15:5433f9d94cd7 | 37 | //***************************************************************************** |
| dflet | 15:5433f9d94cd7 | 38 | //***************************************************************************** |
| dflet | 15:5433f9d94cd7 | 39 | // |
| dflet | 15:5433f9d94cd7 | 40 | //! \addtogroup i2cconfig |
| dflet | 15:5433f9d94cd7 | 41 | //! @{ |
| dflet | 15:5433f9d94cd7 | 42 | // |
| dflet | 15:5433f9d94cd7 | 43 | //***************************************************************************** |
| dflet | 15:5433f9d94cd7 | 44 | #include <stdbool.h> |
| dflet | 15:5433f9d94cd7 | 45 | #include <stdint.h> |
| dflet | 15:5433f9d94cd7 | 46 | #include "mbed.h" |
| dflet | 15:5433f9d94cd7 | 47 | |
| dflet | 15:5433f9d94cd7 | 48 | #include "i2cconfig.h" |
| dflet | 15:5433f9d94cd7 | 49 | #include "cli_uart.h" |
| dflet | 15:5433f9d94cd7 | 50 | #include "myBoardInit.h" |
| dflet | 15:5433f9d94cd7 | 51 | |
| dflet | 15:5433f9d94cd7 | 52 | |
| dflet | 15:5433f9d94cd7 | 53 | |
| dflet | 15:5433f9d94cd7 | 54 | I2C i2c(PB_9, PB_8); |
| dflet | 15:5433f9d94cd7 | 55 | |
| dflet | 15:5433f9d94cd7 | 56 | |
| dflet | 15:5433f9d94cd7 | 57 | //***************************************************************************** |
| dflet | 15:5433f9d94cd7 | 58 | // |
| dflet | 15:5433f9d94cd7 | 59 | //! I2CInit |
| dflet | 15:5433f9d94cd7 | 60 | //! |
| dflet | 15:5433f9d94cd7 | 61 | //! \param Delay |
| dflet | 15:5433f9d94cd7 | 62 | //! \return None |
| dflet | 15:5433f9d94cd7 | 63 | // |
| dflet | 15:5433f9d94cd7 | 64 | //***************************************************************************** |
| dflet | 15:5433f9d94cd7 | 65 | uint32_t I2CInit() |
| dflet | 15:5433f9d94cd7 | 66 | { |
| dflet | 15:5433f9d94cd7 | 67 | |
| dflet | 15:5433f9d94cd7 | 68 | i2c.frequency (100000); |
| dflet | 15:5433f9d94cd7 | 69 | |
| dflet | 15:5433f9d94cd7 | 70 | return 0; |
| dflet | 15:5433f9d94cd7 | 71 | } |
| dflet | 15:5433f9d94cd7 | 72 | //**************************************************************************** |
| dflet | 15:5433f9d94cd7 | 73 | // |
| dflet | 15:5433f9d94cd7 | 74 | //! Invokes the I2C driver APIs to read from the device. This assumes the |
| dflet | 15:5433f9d94cd7 | 75 | //! device local address to read from is set using the I2CWrite API. |
| dflet | 15:5433f9d94cd7 | 76 | //! |
| dflet | 15:5433f9d94cd7 | 77 | //! \param ucDevAddr is the device I2C slave address |
| dflet | 15:5433f9d94cd7 | 78 | //! \param ucBuffer is the pointer to the read data to be placed |
| dflet | 15:5433f9d94cd7 | 79 | //! \param ulSize is the length of data to be read |
| dflet | 15:5433f9d94cd7 | 80 | //! \param ucFlags Flag |
| dflet | 15:5433f9d94cd7 | 81 | //! |
| dflet | 15:5433f9d94cd7 | 82 | //! This function works in a polling mode, |
| dflet | 15:5433f9d94cd7 | 83 | //! 1. Writes the device register address to be written to. |
| dflet | 15:5433f9d94cd7 | 84 | //! 2. In a loop, reads all the bytes over I2C |
| dflet | 15:5433f9d94cd7 | 85 | //! |
| dflet | 15:5433f9d94cd7 | 86 | //! \return 0: Success, < 0: Failure. |
| dflet | 15:5433f9d94cd7 | 87 | // |
| dflet | 15:5433f9d94cd7 | 88 | //**************************************************************************** |
| dflet | 15:5433f9d94cd7 | 89 | int32_t I2CBufferRead(int32_t ucDevAddr, uint8_t *ucBuffer, |
| dflet | 15:5433f9d94cd7 | 90 | int32_t ulSize, unsigned char ucFlags) |
| dflet | 15:5433f9d94cd7 | 91 | { |
| dflet | 15:5433f9d94cd7 | 92 | // Uart_Write((uint8_t*)"I2CBufferRead \n\r"); |
| dflet | 15:5433f9d94cd7 | 93 | /* |
| dflet | 15:5433f9d94cd7 | 94 | i2c.start(); |
| dflet | 15:5433f9d94cd7 | 95 | |
| dflet | 15:5433f9d94cd7 | 96 | // Set I2C slave read address |
| dflet | 15:5433f9d94cd7 | 97 | i2c.write(ucDevAddr); |
| dflet | 15:5433f9d94cd7 | 98 | if(ulSize == 1){ |
| dflet | 15:5433f9d94cd7 | 99 | ucBuffer[0] = i2c.read(0); |
| dflet | 15:5433f9d94cd7 | 100 | }else{ |
| dflet | 15:5433f9d94cd7 | 101 | for(int i=0;i<ulSize;i++){ |
| dflet | 15:5433f9d94cd7 | 102 | ucBuffer[i] = i2c.read(0); |
| dflet | 15:5433f9d94cd7 | 103 | } |
| dflet | 15:5433f9d94cd7 | 104 | } |
| dflet | 15:5433f9d94cd7 | 105 | |
| dflet | 15:5433f9d94cd7 | 106 | i2c.stop(); |
| dflet | 15:5433f9d94cd7 | 107 | */ |
| dflet | 15:5433f9d94cd7 | 108 | int32_t err = 0; |
| dflet | 15:5433f9d94cd7 | 109 | |
| dflet | 15:5433f9d94cd7 | 110 | err = i2c.read(ucDevAddr,(char*)ucBuffer,ulSize); |
| dflet | 15:5433f9d94cd7 | 111 | if(err == 1){ |
| dflet | 15:5433f9d94cd7 | 112 | Uart_Write((uint8_t*)"Return error I2C read\n\r"); |
| dflet | 15:5433f9d94cd7 | 113 | return -1; |
| dflet | 15:5433f9d94cd7 | 114 | } |
| dflet | 15:5433f9d94cd7 | 115 | // wait_ms(1); |
| dflet | 15:5433f9d94cd7 | 116 | return 0; |
| dflet | 15:5433f9d94cd7 | 117 | } |
| dflet | 15:5433f9d94cd7 | 118 | //**************************************************************************** |
| dflet | 15:5433f9d94cd7 | 119 | // |
| dflet | 15:5433f9d94cd7 | 120 | //! Invokes the I2C driver APIs to write to the specified address |
| dflet | 15:5433f9d94cd7 | 121 | //! |
| dflet | 15:5433f9d94cd7 | 122 | //! \param ucDevAddr is the device I2C slave address |
| dflet | 15:5433f9d94cd7 | 123 | //! \param ucBuffer is the pointer to the data to be written |
| dflet | 15:5433f9d94cd7 | 124 | //! \param ulSize is the length of data to be written |
| dflet | 15:5433f9d94cd7 | 125 | //! \param ucFlags |
| dflet | 15:5433f9d94cd7 | 126 | //! |
| dflet | 15:5433f9d94cd7 | 127 | //! This function works in a polling mode, |
| dflet | 15:5433f9d94cd7 | 128 | //! 1. Writes the device register address to be written to. |
| dflet | 15:5433f9d94cd7 | 129 | //! 2. In a loop, writes all the bytes over I2C |
| dflet | 15:5433f9d94cd7 | 130 | //! |
| dflet | 15:5433f9d94cd7 | 131 | //! \return 0: Success, < 0: Failure. |
| dflet | 15:5433f9d94cd7 | 132 | // |
| dflet | 15:5433f9d94cd7 | 133 | //**************************************************************************** |
| dflet | 15:5433f9d94cd7 | 134 | |
| dflet | 15:5433f9d94cd7 | 135 | int32_t I2CBufferWrite(int32_t ucDevAddr, uint8_t *ucBuffer, |
| dflet | 15:5433f9d94cd7 | 136 | int32_t ulSize,unsigned char ucFlags) |
| dflet | 15:5433f9d94cd7 | 137 | { |
| dflet | 15:5433f9d94cd7 | 138 | // Uart_Write((uint8_t*)"I2CBufferWrite \n\r"); |
| dflet | 15:5433f9d94cd7 | 139 | /* |
| dflet | 15:5433f9d94cd7 | 140 | i2c.start(); |
| dflet | 15:5433f9d94cd7 | 141 | |
| dflet | 15:5433f9d94cd7 | 142 | // Set I2C slave write address |
| dflet | 15:5433f9d94cd7 | 143 | i2c.write(ucDevAddr); |
| dflet | 15:5433f9d94cd7 | 144 | |
| dflet | 15:5433f9d94cd7 | 145 | if(ulSize == 1){ |
| dflet | 15:5433f9d94cd7 | 146 | i2c.write(ucBuffer[0]); |
| dflet | 15:5433f9d94cd7 | 147 | }else{ |
| dflet | 15:5433f9d94cd7 | 148 | for(int i=0;i<ulSize;i++){ |
| dflet | 15:5433f9d94cd7 | 149 | i2c.write(ucBuffer[i]); |
| dflet | 15:5433f9d94cd7 | 150 | } |
| dflet | 15:5433f9d94cd7 | 151 | } |
| dflet | 15:5433f9d94cd7 | 152 | |
| dflet | 15:5433f9d94cd7 | 153 | i2c.stop(); |
| dflet | 15:5433f9d94cd7 | 154 | */ |
| dflet | 15:5433f9d94cd7 | 155 | int32_t err = 0; |
| dflet | 15:5433f9d94cd7 | 156 | |
| dflet | 15:5433f9d94cd7 | 157 | err = i2c.write(ucDevAddr,(char*)ucBuffer,ulSize); |
| dflet | 15:5433f9d94cd7 | 158 | if(err == 1){ |
| dflet | 15:5433f9d94cd7 | 159 | Uart_Write((uint8_t*)"Return error I2C write\n\r"); |
| dflet | 15:5433f9d94cd7 | 160 | return -1; |
| dflet | 15:5433f9d94cd7 | 161 | } |
| dflet | 15:5433f9d94cd7 | 162 | // wait_ms(1); |
| dflet | 15:5433f9d94cd7 | 163 | return 0; |
| dflet | 15:5433f9d94cd7 | 164 | } |
| dflet | 15:5433f9d94cd7 | 165 | |
| dflet | 15:5433f9d94cd7 | 166 | //***************************************************************************** |
| dflet | 15:5433f9d94cd7 | 167 | // |
| dflet | 15:5433f9d94cd7 | 168 | // Close the Doxygen group. |
| dflet | 15:5433f9d94cd7 | 169 | //! @} |
| dflet | 15:5433f9d94cd7 | 170 | // |
| dflet | 15:5433f9d94cd7 | 171 | //***************************************************************************** |
| dflet | 15:5433f9d94cd7 | 172 | |
| dflet | 15:5433f9d94cd7 | 173 |