1 /* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
4 * 2019/7/25 - ROBIN KRENS
16 #include <sys/robsys.h>
18 #include <lib/regfunc.h>
19 #include <lib/string.h>
20 #include <lib/stdio.h>
21 #include <lib/tinyprintf.h>
23 #include <drivers/at24c.h>
28 #define WRITE_CMD 0xA0
31 /* STM32F1 microcontrollers do not provide the ability to pull-up SDA and SCL lines. Their
32 GPIOs must be configured as open-drain. So, you have to add two additional resistors to
33 pull-up I2C lines. Something between 4K and 10K is a proven value.
38 /* Program the peripheral input clock in I2C_CR2 Register in order to generate correct timings
39 Configure the clock control registers CCR
40 Configure the rise time register TRIS
41 Program the I2C_CR1 register to enable the peripheral
43 ENABLE GPIOB6 and B7*/
45 rsetbit(RCC_APB1ENR, 21);
46 rsetbit(RCC_APB2ENR, 3);
47 // //regw_u8(AFIO_EVCR, 0x89, 0, SETBIT);// set event control register, output on ?
49 rwrite(GPIOB_CRL, 0xEE444444);
51 rsetbitsfrom(I2C_CR2, 0, 0x2); // 36 MHz
52 rwrite(I2C_TRISE, 0x3); // MAX = 1000ns, TPCLK1 = 500ns (+1)
53 rwrite(I2C_CCR, 0x000A); // standard modeļ¼ output 100 kHz (100hz* / perip)
55 rsetbit(I2C_CR1, 10); // send ack if receive
57 rsetbit(I2C_CR1, 0); // enable
61 static void start_condition() {
63 rsetbit(I2C_CR1, 8); //start
67 static void stop_condition() {
69 rsetbit(I2C_CR1, 9); //stop
72 static int ack_recv() {
75 while(!(*I2C_SR1 & 0x2)) {
86 static int buf_empty() {
88 while(!(*I2C_SR1 & 0x80)) {
97 static void data_recv() {
99 while(!(*I2C_SR1 & 0x4)) {
101 // if (cnt > TIMEOUT)
110 for (int i = 0; i < 0xFFFF; i++)
116 // regw_u32(I2C_CR1, 0x1, 8, SETBIT);
117 // uint32_t read_status = *I2C_SR1;
119 // regw_u32(I2C_DR, DATASET, 0, OWRITE);
121 // read_status = *I2C_SR1;
122 // read_status = *I2C_SR2;
127 //uint32_t statusr = *I2C_SR1; // clear start_signal
128 rwrite(I2C_DR, 0xA0); // write to address CMD
130 cputs("CAN'T REACH DEVICE");
132 rwrite(I2C_DR, 0x00);
135 rwrite(I2C_DR, 0x00);
138 //rwrite(I2C_DR, 0x61);
142 //statusr = *I2C_SR1;
143 //statusr = *I2C_SR2;
146 // start_condition();
147 // rwrite(I2C_DR, 0xA0); // dummy write
149 // cputs("CAN'T REACH DEVICE");
151 // rwrite(I2C_DR, 0x00);
154 // rwrite(I2C_DR, 0x00);
160 start_condition(); // restart condition
161 rwrite(I2C_DR, 0xA1); // read? to address CMD
163 cputs("COULDN'T START READ CMD");
167 //cputs("NO RESPONSE");
169 char a = (char) *I2C_DR;
170 printf("DATA %c\n", a);
176 //statusr = *I2C_SR1; // clear start_signal
177 //regw_u32(I2C_DR, 0xC1, 0, OWRITE);
179 // cputs("TIMEOUT2!");
180 //statusr = *I2C_SR1;
181 //statusr = *I2C_SR2;
182 //regw_u32(I2C_DR, 0x7D, 0, OWRITE);
184 // cputs("TIMEOUT3!");
191 regw_u32(I2C_DR, DISPLAY_ON, 0, OWRITE);
197 /* regw_u32(I2C_CR1, 0x1, 8, SETBIT); //start
198 uint32_t read_status = *I2C_SR1;
199 regw_u32(I2C_DR, 0x40, 0, OWRITE); // write to address CMD
200 read_status = *I2C_SR1;
201 read_status = *I2C_SR2;
202 regw_u32(I2C_CR1, 0x1, 9, SETBIT); //stop
203 read_status = *I2C_SR1;
205 regw_u32(I2C_CR1, 0x1, 8, SETBIT); //start
206 read_status = *I2C_SR1;
207 regw_u32(I2C_DR, 0xC1, 0, OWRITE); // segment address
208 read_status = *I2C_SR1;
209 read_status = *I2C_SR2;
210 regw_u32(I2C_DR, 0x7D, 0, OWRITE); // write a six
212 regw_u32(I2C_CR1, 0x1, 9, SETBIT); //stop
213 read_status = *I2C_SR1;
215 regw_u32(I2C_CR1, 0x1, 8, SETBIT); //start
216 read_status = *I2C_SR1;
218 regw_u32(I2C_DR, DISPLAY_ON, 0, OWRITE);
219 read_status = *I2C_SR1;
220 regw_u32(I2C_CR1, 0x1, 9, SETBIT); //stop */