NetBurner 3.5.6
PDF Version
i2c_class.h
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
5#ifndef _I2C_CLASS_H
6#define _I2C_CLASS_H
7#include <basictypes.h>
8#include <nbrtos.h>
9#include <sim.h>
10
11/* defined I2C Timeout values will be used if user does not include the 'ticks_to_wait'
12 parameter when calling I2C functions */
13#define I2C_RX_TX_TIMEOUT (5) // Ticks allowed before timeout of a single byte transmission
14#define I2C_START_TIMEOUT (20) // Ticks allowed before timeout when attempting start on I2C bus
15
16#define NUM_I2C_MODULES (6)
17
18enum e_i2cstatus
19{
20 OKAY = 0,
21 NEXT_WRITE_OK,
22 NEXT_READ_OK,
23 MASTER_OK,
24 TIMEOUT,
25 BUS_NOT_AVAIL,
26 NOT_READY,
27 LOST_ARB,
28 LOST_ARB_ADD,
29 NO_LINK_RX_ACK
30};
31
32// //Struct that contains I2C Slave I/O Buffers
33// struct I2C_Slave_Record
34// {
35// OS_SEM I2C_Slave_RX_Semaphore; //semaphore used to determine when a slave RX interrupt occurs
36
37// volatile uint8_t * pI2CRxbuf;
38// volatile uint8_t * pI2CTxbuf;
39
40// volatile uint32_t I2Crx_put;
41// volatile uint32_t I2Crx_get;
42
43// volatile uint32_t I2Ctx_put;
44// volatile uint32_t I2Ctx_get;
45// };
46
47class i2c_master
48{
49 private:
50 volatile i2cstruct *i2cRegs;
51
52 protected:
53 inline bool busBusy() { return (0x20 & i2cRegs->i2sr) == 0x20; };
54 inline bool slaveMode() { return (0x20 & i2cRegs->i2cr) == 0x00; };
55 inline bool arbLost() { return (0x10 & i2cRegs->i2sr) == 0x10; };
56 inline bool addressedAsSlave() { return (0x40 & i2cRegs->i2sr) == 0x40; };
57 inline bool addressedSlaveTX() { return (0x04 & i2cRegs->i2sr) == 0x04; };
58 inline bool transmitting() { return (0x10 & i2cRegs->i2cr) == 0x10; };
59 inline bool receivedRXAck() { return (0x01 & i2cRegs->i2sr) == 0x00; };
60 inline bool sentRXAck() { return (0x08 & i2cRegs->i2cr) == 0x00; };
61 inline void setAck() { i2cRegs->i2cr &= 0xF7; };
62 inline void setRX() { i2cRegs->i2cr &= 0xEF; };
63 inline void setRepeatStart() { i2cRegs->i2cr |= 0x04; };
64 inline void clrArbLost() { i2cRegs->i2sr &= 0xEF; };
65 inline void clrIntFlag() { i2cRegs->i2sr &= 0xFD; };
66 inline void sendStop() { i2cRegs->i2cr &= 0xDF; }
67
68 inline void sendData(uint8_t data) { i2cRegs->i2dr = data; };
69 inline void setFreq(uint8_t freq) { i2cRegs->i2fdr = freq; }
70
71 public:
72 inline void setTX() { i2cRegs->i2cr |= 0x10; };
73 inline void setNoAck() { i2cRegs->i2cr |= 0x08; };
74 inline uint8_t readData() { return i2cRegs->i2dr; };
75 void isr();
76 OS_SEM I2C_Semaphore; // semaphore for when interrupt occurs
77 volatile e_i2cstatus status;
78 i2c_master();
79 void init(uint8_t moduleNum, uint8_t freqdiv);
80 void stop(uint32_t ticks_to_wait);
81 void disable();
82 void reset();
83 void read8(uint8_t *val, uint32_t ticks_to_wait);
84 void write8(uint8_t, uint32_t);
85 void start(uint8_t, bool, uint32_t);
86 void restart(uint8_t, bool, uint32_t);
87 volatile bool bLastTX; // boolean to tell when we have completed a master-TX
88 volatile uint8_t RnW; // 2 = Last TX was not address, 1 = RX request, 0 = TX request
89 volatile bool bRestarted; // Used in buf RX, TX functions to identify if we restarted
90};
91
92extern i2c_master I2C[NUM_I2C_MODULES];
93// i2c_master I2C0(0, 0x3C);
94
95#endif
I2C Peripheral Class.
Definition i2c.h:213
void init()
System initialization. Ideally called at the beginning of all applications, since the easiest Recover...
NetBurner Real-Time Operating System (NBRTOS) API.
Semaphores are used to control access to shared resources or or to communicate between tasks in a mul...
Definition nbrtos.h:411