i2c_base.hpp
Go to the documentation of this file.
1 /*
2  * SocialLedge.com - Copyright (C) 2013
3  *
4  * This file is part of free software framework for embedded processors.
5  * You can use it and/or distribute it as long as this copyright header
6  * remains unmodified. The code is free for personal use and requires
7  * permission to use in a commercial product.
8  *
9  * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
10  * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
12  * I SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
13  * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
14  *
15  * You can reach the author of this software at :
16  * p r e e t . w i k i @ g m a i l . c o m
17  */
18 
29 #ifndef I2C_BASE_HPP_
30 #define I2C_BASE_HPP_
31 
32 #include <stdint.h>
33 
34 #include "FreeRTOS.h"
35 #include "task.h" // xTaskGetSchedulerState()
36 #include "semphr.h" // Semaphores used in I2C
37 #include "LPC17xx.h"
38 
39 
40 
46 #define I2C_TIMEOUT_MS 1000
47 
48 
68 class I2C_Base
69 {
70  public:
75  void handleInterrupt();
76 
83  uint8_t readReg(uint8_t deviceAddress, uint8_t registerAddress);
84 
92  bool writeReg(uint8_t deviceAddress, uint8_t registerAddress, uint8_t value);
93 
95  bool readRegisters(uint8_t deviceAddress, uint8_t firstReg, uint8_t* pData, uint32_t transferSize);
96 
98  bool writeRegisters(uint8_t deviceAddress, uint8_t firstReg, uint8_t* pData, uint32_t transferSize);
99 
109  bool checkDeviceResponse(uint8_t deviceAddress);
110 
111 
112 
113  protected:
118  I2C_Base(LPC_I2C_TypeDef* pI2CBaseAddr);
119 
125  bool init(uint32_t pclk, uint32_t busRateInKhz);
126 
132  void disableOperation() { mDisableOperation = true; }
133 
134 
135  private:
136  LPC_I2C_TypeDef* mpI2CRegs;
137  IRQn_Type mIRQ;
138  bool mDisableOperation;
139  SemaphoreHandle_t mI2CMutex;
140  SemaphoreHandle_t mTransferCompleteSignal;
141 
145  typedef enum {
146  busy,
147  readComplete,
148  writeComplete
149  } __attribute__((packed)) mStateMachineStatus_t;
150 
154  typedef struct
155  {
156  uint32_t trxSize;
157  uint8_t slaveAddr;
158  uint8_t firstReg;
159  uint8_t error;
160  uint8_t *pMasterData;
161  } mI2CTransaction_t;
162 
164  mI2CTransaction_t mTransaction;
165 
173  mStateMachineStatus_t i2cStateMachine();
174 
185  bool transfer(uint8_t deviceAddress, uint8_t firstReg, uint8_t* pData, uint32_t transferSize);
186 
194  void i2cKickOffTransfer(uint8_t devAddr, uint8_t regStart, uint8_t* pBytes, uint32_t len);
195 };
196 
197 
198 
199 
200 #endif /* I2C_BASE_HPP_ */
bool writeRegisters(uint8_t deviceAddress, uint8_t firstReg, uint8_t *pData, uint32_t transferSize)
Definition: i2c_base.cpp:66
bool init(uint32_t pclk, uint32_t busRateInKhz)
Definition: i2c_base.cpp:150
uint8_t readReg(uint8_t deviceAddress, uint8_t registerAddress)
Definition: i2c_base.cpp:48
void disableOperation()
Definition: i2c_base.hpp:132
unsigned int unsigned int len
Definition: startup.cpp:197
Definition: i2c_base.hpp:68
Definition: can.h:69
bool readRegisters(uint8_t deviceAddress, uint8_t firstReg, uint8_t *pData, uint32_t transferSize)
Definition: i2c_base.cpp:55
void handleInterrupt()
Definition: i2c_base.cpp:38
bool writeReg(uint8_t deviceAddress, uint8_t registerAddress, uint8_t value)
Definition: i2c_base.cpp:61
bool checkDeviceResponse(uint8_t deviceAddress)
Definition: i2c_base.cpp:111
CMSIS Cortex-M3 Core Peripheral Access Layer Header File for NXP LPC17xx Device Series.
I2C_Base(LPC_I2C_TypeDef *pI2CBaseAddr)
Definition: i2c_base.cpp:122
enum IRQn IRQn_Type
Definition: LPC17xx.h:472
QueueHandle_t SemaphoreHandle_t
Definition: semphr.h:79