Skip to content

Commit 54ec228

Browse files
committed
[NUCLEO_F103RB] add SPI slave and I2C slave
1 parent 569da9a commit 54ec228

File tree

3 files changed

+43
-29
lines changed

3 files changed

+43
-29
lines changed

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/device.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@
4242
#define DEVICE_SERIAL 1
4343

4444
#define DEVICE_I2C 1
45-
#define DEVICE_I2CSLAVE 0
45+
#define DEVICE_I2CSLAVE 1
4646

4747
#define DEVICE_SPI 1
48-
#define DEVICE_SPISLAVE 0
48+
#define DEVICE_SPISLAVE 1
4949

5050
#define DEVICE_RTC 1
5151

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/i2c_api.c

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ inline int i2c_start(i2c_t *obj) {
118118

119119
// Wait the START condition has been correctly sent
120120
timeout = FLAG_TIMEOUT;
121-
//while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_MODE_SELECT) == ERROR) {
122121
while (I2C_GetFlagStatus(i2c, I2C_FLAG_SB) == RESET) {
123122
timeout--;
124123
if (timeout == 0) {
@@ -145,17 +144,6 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
145144

146145
if (length == 0) return 0;
147146

148-
/*
149-
// Wait until the bus is not busy anymore
150-
timeout = LONG_TIMEOUT;
151-
while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
152-
timeout--;
153-
if (timeout == 0) {
154-
return 0;
155-
}
156-
}
157-
*/
158-
159147
i2c_start(obj);
160148

161149
// Send slave address for read
@@ -194,17 +182,6 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
194182
int timeout;
195183
int count;
196184

197-
/*
198-
// Wait until the bus is not busy anymore
199-
timeout = LONG_TIMEOUT;
200-
while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
201-
timeout--;
202-
if (timeout == 0) {
203-
return 0;
204-
}
205-
}
206-
*/
207-
208185
i2c_start(obj);
209186

210187
// Send slave address for write
@@ -269,7 +246,6 @@ int i2c_byte_write(i2c_t *obj, int data) {
269246

270247
// Wait until the byte is transmitted
271248
timeout = FLAG_TIMEOUT;
272-
//while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_BYTE_TRANSMITTED) == ERROR) {
273249
while ((I2C_GetFlagStatus(i2c, I2C_FLAG_TXE) == RESET) &&
274250
(I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) {
275251
timeout--;
@@ -319,8 +295,46 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave) {
319295
#define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
320296

321297
int i2c_slave_receive(i2c_t *obj) {
322-
// TO BE DONE
323-
return(0);
298+
int retValue = NoData;
299+
uint32_t event;
300+
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
301+
302+
event = I2C_GetLastEvent( i2c );
303+
if(event != 0)
304+
{
305+
switch(event){
306+
case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:
307+
retValue = WriteAddressed;
308+
break;
309+
case I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED:
310+
retValue = ReadAddressed;
311+
break;
312+
case I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED:
313+
retValue = WriteGeneral;
314+
break;
315+
default:
316+
retValue = NoData;
317+
break;
318+
}
319+
320+
// clear ADDR
321+
if((retValue == WriteAddressed) || (retValue == ReadAddressed)){
322+
// read SR to clear ADDR flag
323+
i2c->SR1;
324+
i2c->SR2;
325+
}
326+
// clear stopf
327+
if(I2C_GetFlagStatus(i2c, I2C_FLAG_STOPF) == SET) {
328+
// read SR1 and write CR1 to clear STOP flag
329+
i2c->SR1;
330+
I2C_Cmd(i2c, ENABLE);
331+
}
332+
// clear AF
333+
if(I2C_GetFlagStatus(i2c, I2C_FLAG_AF) == SET) {
334+
I2C_ClearFlag(i2c, I2C_FLAG_AF);
335+
}
336+
}
337+
return(retValue);
324338
}
325339

326340
int i2c_slave_read(i2c_t *obj, char *data, int length) {

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/spi_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
126126
else { // Slave
127127
pinmap_pinout(ssel, PinMap_SPI_SSEL);
128128
obj->mode = SPI_Mode_Slave;
129-
obj->nss = SPI_NSS_Soft;
129+
obj->nss = SPI_NSS_Hard;
130130
}
131131

132132
init_spi(obj);

0 commit comments

Comments
 (0)