You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.5 KiB
C

/*
* i2c_slave.c
*
* Created on: Apr 19, 2024
* Author: NikmaOS-W
*/
#include "i2c_slave.h"
#define CTLR1_SWRST_Set ((uint16_t)0x8000)
void I2C_Slave_Init(void){
//扭抉技快投忘找抆 扭抉扼抖快 我扶我扯我忘抖我戒忘扯我我 SPI 找忘抗 抗忘抗 找忘技 批忪快 志抗抖攻折快扶 扭抉把找 GPIO
GPIO_InitTypeDef GPIO_InitStructure={0};
I2C_InitTypeDef I2C_InitStruct={0};
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOC, &GPIO_InitStructure);
I2C_InitStruct.I2C_ClockSpeed = 100000;
I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStruct.I2C_OwnAddress1 = 96;
I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C1,&I2C_InitStruct);
I2C_Cmd(I2C1, ENABLE);
I2C_AcknowledgeConfig( I2C1, ENABLE );
}
void I2C_slave_rcv(I2C_TypeDef * I2Cx,u8 * buf, u8 size){
while( I2C_CheckEvent(I2Cx, I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED)!=READY){
__NOP();
};
__NOP();
for (u8 i=0;i<size;i++){
while( I2C_CheckEvent(I2Cx,I2C_EVENT_SLAVE_BYTE_RECEIVED)!=READY){
};
buf[i]=I2C_ReceiveData(I2Cx);}
while( I2C_CheckEvent(I2Cx,I2C_EVENT_SLAVE_STOP_DETECTED)!=READY){
};
I2C_Cmd(I2Cx, ENABLE);
}