[NUCLEO_F103RB] add SPI slave and I2C slave

pull/295/head
dbestm 2014-05-07 10:34:12 +02:00
parent 569da9ae6e
commit 54ec228bd9
3 changed files with 43 additions and 29 deletions

View File

@ -42,10 +42,10 @@
#define DEVICE_SERIAL 1 #define DEVICE_SERIAL 1
#define DEVICE_I2C 1 #define DEVICE_I2C 1
#define DEVICE_I2CSLAVE 0 #define DEVICE_I2CSLAVE 1
#define DEVICE_SPI 1 #define DEVICE_SPI 1
#define DEVICE_SPISLAVE 0 #define DEVICE_SPISLAVE 1
#define DEVICE_RTC 1 #define DEVICE_RTC 1

View File

@ -118,7 +118,6 @@ inline int i2c_start(i2c_t *obj) {
// Wait the START condition has been correctly sent // Wait the START condition has been correctly sent
timeout = FLAG_TIMEOUT; timeout = FLAG_TIMEOUT;
//while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_MODE_SELECT) == ERROR) {
while (I2C_GetFlagStatus(i2c, I2C_FLAG_SB) == RESET) { while (I2C_GetFlagStatus(i2c, I2C_FLAG_SB) == RESET) {
timeout--; timeout--;
if (timeout == 0) { if (timeout == 0) {
@ -145,17 +144,6 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
if (length == 0) return 0; if (length == 0) return 0;
/*
// Wait until the bus is not busy anymore
timeout = LONG_TIMEOUT;
while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
timeout--;
if (timeout == 0) {
return 0;
}
}
*/
i2c_start(obj); i2c_start(obj);
// Send slave address for read // Send slave address for read
@ -194,17 +182,6 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
int timeout; int timeout;
int count; int count;
/*
// Wait until the bus is not busy anymore
timeout = LONG_TIMEOUT;
while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
timeout--;
if (timeout == 0) {
return 0;
}
}
*/
i2c_start(obj); i2c_start(obj);
// Send slave address for write // Send slave address for write
@ -269,7 +246,6 @@ int i2c_byte_write(i2c_t *obj, int data) {
// Wait until the byte is transmitted // Wait until the byte is transmitted
timeout = FLAG_TIMEOUT; timeout = FLAG_TIMEOUT;
//while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_BYTE_TRANSMITTED) == ERROR) {
while ((I2C_GetFlagStatus(i2c, I2C_FLAG_TXE) == RESET) && while ((I2C_GetFlagStatus(i2c, I2C_FLAG_TXE) == RESET) &&
(I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) { (I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) {
timeout--; timeout--;
@ -319,8 +295,46 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave) {
#define WriteAddressed 3 // the master is writing to this slave (slave = receiver) #define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
int i2c_slave_receive(i2c_t *obj) { int i2c_slave_receive(i2c_t *obj) {
// TO BE DONE int retValue = NoData;
return(0); uint32_t event;
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
event = I2C_GetLastEvent( i2c );
if(event != 0)
{
switch(event){
case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:
retValue = WriteAddressed;
break;
case I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED:
retValue = ReadAddressed;
break;
case I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED:
retValue = WriteGeneral;
break;
default:
retValue = NoData;
break;
}
// clear ADDR
if((retValue == WriteAddressed) || (retValue == ReadAddressed)){
// read SR to clear ADDR flag
i2c->SR1;
i2c->SR2;
}
// clear stopf
if(I2C_GetFlagStatus(i2c, I2C_FLAG_STOPF) == SET) {
// read SR1 and write CR1 to clear STOP flag
i2c->SR1;
I2C_Cmd(i2c, ENABLE);
}
// clear AF
if(I2C_GetFlagStatus(i2c, I2C_FLAG_AF) == SET) {
I2C_ClearFlag(i2c, I2C_FLAG_AF);
}
}
return(retValue);
} }
int i2c_slave_read(i2c_t *obj, char *data, int length) { int i2c_slave_read(i2c_t *obj, char *data, int length) {

View File

@ -126,7 +126,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
else { // Slave else { // Slave
pinmap_pinout(ssel, PinMap_SPI_SSEL); pinmap_pinout(ssel, PinMap_SPI_SSEL);
obj->mode = SPI_Mode_Slave; obj->mode = SPI_Mode_Slave;
obj->nss = SPI_NSS_Soft; obj->nss = SPI_NSS_Hard;
} }
init_spi(obj); init_spi(obj);