Commit 1648f85cd6d2cf3e0d85a54e2184c98d0e52f890

Authored by Imanol-Mikel Barba Sabariego
1 parent 0af9274f

I2C Finished

Project/applications/smartcities/i2c.c 0 → 100644
  1 +#include <stm32f10x.h>
  2 +#include <stm32f10x_i2c.h>
  3 +
  4 +void I2C1_init()
  5 +{
  6 + GPIO_InitTypeDef GPIO_InitStruct;
  7 + I2C_InitTypeDef I2C_InitStruct;
  8 +
  9 + RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
  10 + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  11 +
  12 + /*
  13 + * 1. SCL on PB6 or PB8
  14 + * 2. SDA on PB7 or PB9
  15 + */
  16 +
  17 + GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_9;
  18 + GPIO_InitStruct.GPIO_Mode = GPIO_Mode_A
  19 + GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  20 + GPIO_InitStruct.GPIO_OType = GPIO_OType_OD; // set output to open drain
  21 + GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; // enable pull up resistors
  22 + GPIO_Init(GPIOB, &GPIO_InitStruct); // init GPIOB
  23 +
  24 + /* WARNING
  25 + *
  26 + * Check consistency with above configuration
  27 + *
  28 + * --Imanol
  29 + */
  30 +
  31 + // Connect I2C1 pins to AF
  32 + GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1); // SCL
  33 + GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1); // SDA
  34 +
  35 + // configure I2C1
  36 + I2C_InitStruct.I2C_ClockSpeed = 100000; // 100kHz - STANDARD MODE
  37 + I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
  38 + I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
  39 + I2C_InitStruct.I2C_OwnAddress1 = 0x00;
  40 + I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
  41 + I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  42 + I2C_Init(I2C1, &I2C_InitStruct);
  43 + I2C_Cmd(I2C1, ENABLE);
  44 +}
  45 +
  46 +void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction)
  47 +{
  48 + while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY))
  49 + {
  50 + // wait until I2C1 is not busy any more
  51 + }
  52 + I2C_GenerateSTART(I2Cx, ENABLE);
  53 +
  54 + while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))
  55 + {
  56 + // wait for I2C1 EV5 --> Slave has acknowledged start condition
  57 + }
  58 + I2C_Send7bitAddress(I2Cx, address, direction);
  59 +
  60 + if(direction == I2C_Direction_Transmitter)
  61 + {
  62 + while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
  63 + {
  64 + // wait for I2Cx EV6, check if Slave has acknowledged Master transmitter mode
  65 + }
  66 + }
  67 + else if(direction == I2C_Direction_Receiver)
  68 + {
  69 + while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
  70 + {
  71 + // wait for I2Cx EV6, check if Slave has acknowledged Master receiver mode
  72 + }
  73 + }
  74 +}
  75 +
  76 +void I2C_write(I2C_TypeDef* I2Cx, uint8_t data)
  77 +{
  78 + /* WARNING
  79 + *
  80 + * OJITO QUE ESTÁ ESPERANDO EL EV8 Y NO EL EV8_2
  81 + *
  82 + * --Imanol
  83 + */
  84 + while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTING))
  85 + {
  86 + // wait for I2C1 EV8 --> last byte is still being transmitted (last byte in SR, buffer empty), next byte can already be written
  87 + }
  88 + I2C_SendData(I2Cx, data);
  89 +}
  90 +
  91 +uint8_t I2C_read_ack(I2C_TypeDef* I2Cx)
  92 +{
  93 + I2C_AcknowledgeConfig(I2Cx, ENABLE);
  94 + while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED))
  95 + {
  96 + // wait until one byte has been received
  97 + }
  98 + return I2C_ReceiveData(I2Cx);
  99 +}
  100 +
  101 +uint8_t I2C_read_nack(I2C_TypeDef* I2Cx)
  102 +{
  103 + I2C_AcknowledgeConfig(I2Cx, DISABLE);
  104 + I2C_GenerateSTOP(I2Cx, ENABLE);
  105 + while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED))
  106 + {
  107 + // wait until one byte has been received
  108 + }
  109 + return I2C_ReceiveData(I2Cx);
  110 +}
  111 +
  112 +void I2C_stop(I2C_TypeDef* I2Cx)
  113 +{
  114 + I2C_GenerateSTOP(I2Cx, ENABLE);
  115 + while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
  116 + {
  117 + // wait for I2C1 EV8_2 --> byte has been transmitted
  118 + }
  119 +}
  120 +
  121 +/* SAMPLE CODE
  122 + *
  123 + * int main(void)
  124 + * {
  125 + * uint8_t received_data;
  126 + * I2C1_init(); // initialize I2C peripheral
  127 + * I2C_start(I2C1, SLAVE_ADDRESS<<1, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode
  128 + * I2C_write(I2C1, 0x01); // write one byte to the slave
  129 + * I2C_stop(I2C1); // stop the transmission
  130 + * I2C_start(I2C1, SLAVE_ADDRESS<<1, I2C_Direction_Receiver); // start a transmission in Master receiver mode
  131 + * received_data = I2C_read_nack(I2C1); // read one byte and don't request another byte, stop transmission
  132 + * while(1);
  133 + * return 0;
  134 + * }
  135 + * --Imanol
  136 + */
Project/applications/smartcities/include/i2c.h 0 → 100644
  1 +#ifndef I2C_H
  2 +#define I2C_H
  3 +
  4 +void I2C1_init();
  5 +void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction);
  6 +void I2C_stop(I2C_TypeDef* I2Cx);
  7 +void I2C_write(I2C_TypeDef* I2Cx, uint8_t data);
  8 +uint8_t I2C_read_ack(I2C_TypeDef* I2Cx);
  9 +uint8_t I2C_read_nack(I2C_TypeDef* I2Cx);
  10 +
  11 +#endif
  12 +