Pão de Queijo Development Board. This repo has all libraries from pqdb shield.
Pão de Queijo development board is a shield for add-on on any arduino compatible board.
This project presents the libraries to control the board peripherals using mbed framework. The libraries works as interfaces to keep the main code compatible with the examples from the book "Programação de sistemas embarcados". The peripherals on the board are presented on the folowing picture
The main objective of the project is to develop a low cost, easy to assemble board with a group of peripheral that allows one to start learning embedded systems. The board was routed on one side copper PCB to make easier to buid it yourself.
The source code for Arduino boards (wiring) can be found on: https://github.com/projetopqdb/PQDB-Arduino
The source code for freedom frdm KL05 board (using direct register access) can be found on: https://github.com/projetopqdb/PQDB-KL05Z
The source code from the board schematics and layout can be found on: https://github.com/projetopqdb/PQDB-Hardware
i2c.cpp@6:3fb450ba1e95, 2017-10-03 (annotated)
- Committer:
- rmaalmeida
- Date:
- Tue Oct 03 01:27:24 2017 +0000
- Revision:
- 6:3fb450ba1e95
- Parent:
- 3:6ca4d7dd8bea
timer driver added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rmaalmeida | 2:4b7f5a060c22 | 1 | #include "i2c.h" |
rmaalmeida | 2:4b7f5a060c22 | 2 | //#include "io.h" |
rmaalmeida | 2:4b7f5a060c22 | 3 | #include "mbed.h" |
rmaalmeida | 2:4b7f5a060c22 | 4 | |
rmaalmeida | 2:4b7f5a060c22 | 5 | /* |
rmaalmeida | 2:4b7f5a060c22 | 6 | #define SDA_OFF() digitalWrite(SDA_PIN,LOW) |
rmaalmeida | 2:4b7f5a060c22 | 7 | #define SDA() digitalRead(SDA_PIN) |
rmaalmeida | 2:4b7f5a060c22 | 8 | #define SDA_IN() pinMode(SDA_PIN,INPUT) |
rmaalmeida | 2:4b7f5a060c22 | 9 | #define SDA_OUT() pinMode(SDA_PIN,OUTPUT) |
rmaalmeida | 2:4b7f5a060c22 | 10 | |
rmaalmeida | 2:4b7f5a060c22 | 11 | #define SCL_OFF() digitalWrite(SCL_PIN,LOW) |
rmaalmeida | 2:4b7f5a060c22 | 12 | #define SCL() digitalRead(SCL_PIN) |
rmaalmeida | 2:4b7f5a060c22 | 13 | #define SCL_IN() pinMode(SCL_PIN,INPUT) |
rmaalmeida | 2:4b7f5a060c22 | 14 | #define SCL_OUT() pinMode(SCL_PIN,OUTPUT) |
rmaalmeida | 2:4b7f5a060c22 | 15 | */ |
rmaalmeida | 2:4b7f5a060c22 | 16 | |
rmaalmeida | 2:4b7f5a060c22 | 17 | DigitalInOut SDA(A4); |
rmaalmeida | 2:4b7f5a060c22 | 18 | DigitalInOut SCL(A5); |
rmaalmeida | 2:4b7f5a060c22 | 19 | |
rmaalmeida | 2:4b7f5a060c22 | 20 | #define SDA_OFF() (SDA.write(0)) |
rmaalmeida | 2:4b7f5a060c22 | 21 | #define SDA() (SDA.read()) |
rmaalmeida | 2:4b7f5a060c22 | 22 | #define SDA_IN() (SDA.input()) |
rmaalmeida | 2:4b7f5a060c22 | 23 | #define SDA_OUT() (SDA.output()) |
rmaalmeida | 2:4b7f5a060c22 | 24 | |
rmaalmeida | 2:4b7f5a060c22 | 25 | #define SCL_OFF() (SCL.write(0)) |
rmaalmeida | 2:4b7f5a060c22 | 26 | #define SCL() (SCL.read()) |
rmaalmeida | 2:4b7f5a060c22 | 27 | #define SCL_IN() (SCL.input()) |
rmaalmeida | 2:4b7f5a060c22 | 28 | #define SCL_OUT() (SCL.output()) |
rmaalmeida | 2:4b7f5a060c22 | 29 | |
rmaalmeida | 2:4b7f5a060c22 | 30 | |
rmaalmeida | 2:4b7f5a060c22 | 31 | |
rmaalmeida | 2:4b7f5a060c22 | 32 | void i2cInit(void) { |
rmaalmeida | 2:4b7f5a060c22 | 33 | |
rmaalmeida | 2:4b7f5a060c22 | 34 | SDA_IN(); |
rmaalmeida | 2:4b7f5a060c22 | 35 | SCL_IN(); |
rmaalmeida | 2:4b7f5a060c22 | 36 | } |
rmaalmeida | 2:4b7f5a060c22 | 37 | |
rmaalmeida | 2:4b7f5a060c22 | 38 | // Hardware-specific support functions that MUST be customized: |
rmaalmeida | 2:4b7f5a060c22 | 39 | #define I2CSPEED 10 |
rmaalmeida | 2:4b7f5a060c22 | 40 | void I2C_delay(void) { |
rmaalmeida | 3:6ca4d7dd8bea | 41 | /*volatile int v; |
rmaalmeida | 2:4b7f5a060c22 | 42 | int i; |
rmaalmeida | 2:4b7f5a060c22 | 43 | for (i = 0; i < I2CSPEED / 2; i++) |
rmaalmeida | 3:6ca4d7dd8bea | 44 | v;*/ |
rmaalmeida | 3:6ca4d7dd8bea | 45 | wait_us(10); |
rmaalmeida | 2:4b7f5a060c22 | 46 | } |
rmaalmeida | 2:4b7f5a060c22 | 47 | |
rmaalmeida | 2:4b7f5a060c22 | 48 | unsigned char read_SCL(void) // Set SCL as input and return current level of line, 0 or 1 |
rmaalmeida | 2:4b7f5a060c22 | 49 | { |
rmaalmeida | 2:4b7f5a060c22 | 50 | SCL_IN(); |
rmaalmeida | 2:4b7f5a060c22 | 51 | return !!SCL(); |
rmaalmeida | 2:4b7f5a060c22 | 52 | } |
rmaalmeida | 2:4b7f5a060c22 | 53 | unsigned char read_SDA(void) // Set SDA as input and return current level of line, 0 or 1 |
rmaalmeida | 2:4b7f5a060c22 | 54 | { |
rmaalmeida | 2:4b7f5a060c22 | 55 | SDA_IN(); |
rmaalmeida | 2:4b7f5a060c22 | 56 | return !!SDA(); |
rmaalmeida | 2:4b7f5a060c22 | 57 | |
rmaalmeida | 2:4b7f5a060c22 | 58 | } |
rmaalmeida | 2:4b7f5a060c22 | 59 | void clear_SCL(void) // Actively drive SCL signal low |
rmaalmeida | 2:4b7f5a060c22 | 60 | { |
rmaalmeida | 2:4b7f5a060c22 | 61 | SCL_OUT(); |
rmaalmeida | 2:4b7f5a060c22 | 62 | SCL_OFF(); |
rmaalmeida | 2:4b7f5a060c22 | 63 | } |
rmaalmeida | 2:4b7f5a060c22 | 64 | |
rmaalmeida | 2:4b7f5a060c22 | 65 | void clear_SDA(void) // Actively drive SDA signal low |
rmaalmeida | 2:4b7f5a060c22 | 66 | { |
rmaalmeida | 2:4b7f5a060c22 | 67 | SDA_OUT(); |
rmaalmeida | 2:4b7f5a060c22 | 68 | SDA_OFF(); |
rmaalmeida | 2:4b7f5a060c22 | 69 | } |
rmaalmeida | 2:4b7f5a060c22 | 70 | |
rmaalmeida | 2:4b7f5a060c22 | 71 | unsigned char started = 0; // global data |
rmaalmeida | 2:4b7f5a060c22 | 72 | void i2c_start(void) { |
rmaalmeida | 2:4b7f5a060c22 | 73 | //se j� estiver iniciado, prepara para reenviar o bit de start |
rmaalmeida | 2:4b7f5a060c22 | 74 | if (started) { |
rmaalmeida | 2:4b7f5a060c22 | 75 | read_SDA(); |
rmaalmeida | 2:4b7f5a060c22 | 76 | I2C_delay(); |
rmaalmeida | 2:4b7f5a060c22 | 77 | while (read_SCL() == 0); // Clock stretching |
rmaalmeida | 2:4b7f5a060c22 | 78 | // Repeated start setup time, minimum 4.7us |
rmaalmeida | 2:4b7f5a060c22 | 79 | I2C_delay(); |
rmaalmeida | 2:4b7f5a060c22 | 80 | } |
rmaalmeida | 2:4b7f5a060c22 | 81 | if (read_SDA() == 0) { |
rmaalmeida | 2:4b7f5a060c22 | 82 | //Houve problema de comunica��o |
rmaalmeida | 2:4b7f5a060c22 | 83 | } |
rmaalmeida | 2:4b7f5a060c22 | 84 | // SCL is high, set SDA from 1 to 0. |
rmaalmeida | 2:4b7f5a060c22 | 85 | clear_SDA(); |
rmaalmeida | 2:4b7f5a060c22 | 86 | I2C_delay(); |
rmaalmeida | 2:4b7f5a060c22 | 87 | clear_SCL(); |
rmaalmeida | 2:4b7f5a060c22 | 88 | started = 1; |
rmaalmeida | 2:4b7f5a060c22 | 89 | } |
rmaalmeida | 2:4b7f5a060c22 | 90 | |
rmaalmeida | 2:4b7f5a060c22 | 91 | void i2c_stop(void) { |
rmaalmeida | 2:4b7f5a060c22 | 92 | // set SDA to 0 |
rmaalmeida | 2:4b7f5a060c22 | 93 | clear_SDA(); |
rmaalmeida | 2:4b7f5a060c22 | 94 | I2C_delay(); |
rmaalmeida | 2:4b7f5a060c22 | 95 | while (read_SCL() == 0); // Clock stretching |
rmaalmeida | 2:4b7f5a060c22 | 96 | I2C_delay(); |
rmaalmeida | 2:4b7f5a060c22 | 97 | if (read_SDA() == 0) { |
rmaalmeida | 2:4b7f5a060c22 | 98 | // arbitration_lost(); |
rmaalmeida | 2:4b7f5a060c22 | 99 | } |
rmaalmeida | 2:4b7f5a060c22 | 100 | I2C_delay(); |
rmaalmeida | 2:4b7f5a060c22 | 101 | started = 0; |
rmaalmeida | 2:4b7f5a060c22 | 102 | } |
rmaalmeida | 2:4b7f5a060c22 | 103 | |
rmaalmeida | 2:4b7f5a060c22 | 104 | void i2c_write_bit(unsigned char bit) { |
rmaalmeida | 2:4b7f5a060c22 | 105 | if (bit) { |
rmaalmeida | 2:4b7f5a060c22 | 106 | read_SDA(); |
rmaalmeida | 2:4b7f5a060c22 | 107 | } else { |
rmaalmeida | 2:4b7f5a060c22 | 108 | clear_SDA(); |
rmaalmeida | 2:4b7f5a060c22 | 109 | } |
rmaalmeida | 2:4b7f5a060c22 | 110 | I2C_delay(); |
rmaalmeida | 2:4b7f5a060c22 | 111 | while (read_SCL() == 0); // Clock stretching |
rmaalmeida | 2:4b7f5a060c22 | 112 | if (bit && read_SDA() == 0) { |
rmaalmeida | 2:4b7f5a060c22 | 113 | //Houve problema de comunica��o |
rmaalmeida | 2:4b7f5a060c22 | 114 | } |
rmaalmeida | 2:4b7f5a060c22 | 115 | I2C_delay(); |
rmaalmeida | 2:4b7f5a060c22 | 116 | clear_SCL(); |
rmaalmeida | 2:4b7f5a060c22 | 117 | I2C_delay(); |
rmaalmeida | 2:4b7f5a060c22 | 118 | } |
rmaalmeida | 2:4b7f5a060c22 | 119 | |
rmaalmeida | 2:4b7f5a060c22 | 120 | unsigned char i2c_read_bit(void) { |
rmaalmeida | 2:4b7f5a060c22 | 121 | unsigned char bit; |
rmaalmeida | 2:4b7f5a060c22 | 122 | read_SDA(); |
rmaalmeida | 2:4b7f5a060c22 | 123 | I2C_delay(); |
rmaalmeida | 2:4b7f5a060c22 | 124 | while (read_SCL() == 0); // Clock stretching |
rmaalmeida | 2:4b7f5a060c22 | 125 | bit = read_SDA(); |
rmaalmeida | 2:4b7f5a060c22 | 126 | I2C_delay(); |
rmaalmeida | 2:4b7f5a060c22 | 127 | clear_SCL(); |
rmaalmeida | 2:4b7f5a060c22 | 128 | I2C_delay(); |
rmaalmeida | 2:4b7f5a060c22 | 129 | return bit; |
rmaalmeida | 2:4b7f5a060c22 | 130 | } |
rmaalmeida | 2:4b7f5a060c22 | 131 | |
rmaalmeida | 2:4b7f5a060c22 | 132 | // Write a byte to I2C bus. Return 0 if ack by the slave. |
rmaalmeida | 2:4b7f5a060c22 | 133 | unsigned char i2cWriteByte(unsigned char send_start, unsigned char send_stop, unsigned char byte) { |
rmaalmeida | 2:4b7f5a060c22 | 134 | unsigned char bit; |
rmaalmeida | 2:4b7f5a060c22 | 135 | unsigned char nack; |
rmaalmeida | 2:4b7f5a060c22 | 136 | if (send_start) { |
rmaalmeida | 2:4b7f5a060c22 | 137 | i2c_start(); |
rmaalmeida | 2:4b7f5a060c22 | 138 | } |
rmaalmeida | 2:4b7f5a060c22 | 139 | for (bit = 0; bit < 8; bit++) { |
rmaalmeida | 2:4b7f5a060c22 | 140 | i2c_write_bit((byte & 0x80) != 0); |
rmaalmeida | 2:4b7f5a060c22 | 141 | byte <<= 1; |
rmaalmeida | 2:4b7f5a060c22 | 142 | } |
rmaalmeida | 2:4b7f5a060c22 | 143 | nack = i2c_read_bit(); |
rmaalmeida | 2:4b7f5a060c22 | 144 | if (send_stop) { |
rmaalmeida | 2:4b7f5a060c22 | 145 | i2c_stop(); |
rmaalmeida | 2:4b7f5a060c22 | 146 | } |
rmaalmeida | 2:4b7f5a060c22 | 147 | return nack; |
rmaalmeida | 2:4b7f5a060c22 | 148 | } |
rmaalmeida | 2:4b7f5a060c22 | 149 | |
rmaalmeida | 2:4b7f5a060c22 | 150 | // Read a byte from I2C bus |
rmaalmeida | 2:4b7f5a060c22 | 151 | unsigned char i2cReadByte(unsigned char nack, unsigned char send_stop) { |
rmaalmeida | 2:4b7f5a060c22 | 152 | unsigned char byte = 0; |
rmaalmeida | 2:4b7f5a060c22 | 153 | unsigned bit; |
rmaalmeida | 2:4b7f5a060c22 | 154 | for (bit = 0; bit < 8; bit++) { |
rmaalmeida | 2:4b7f5a060c22 | 155 | byte = (byte << 1) | i2c_read_bit(); |
rmaalmeida | 2:4b7f5a060c22 | 156 | } |
rmaalmeida | 2:4b7f5a060c22 | 157 | i2c_write_bit(nack); |
rmaalmeida | 2:4b7f5a060c22 | 158 | if (send_stop) { |
rmaalmeida | 2:4b7f5a060c22 | 159 | i2c_stop(); |
rmaalmeida | 2:4b7f5a060c22 | 160 | } |
rmaalmeida | 2:4b7f5a060c22 | 161 | return byte; |
rmaalmeida | 2:4b7f5a060c22 | 162 | } |
rmaalmeida | 2:4b7f5a060c22 | 163 |