Skip to content

Commit 6bc9b93

Browse files
author
Bogdan Marinescu
committed
Merge branch 'dbestm-master'
2 parents 1ac1ba4 + e6f6f73 commit 6bc9b93

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/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 // Not yet supported
45+
#define DEVICE_I2CSLAVE 1
4646

4747
#define DEVICE_SPI 1
48-
#define DEVICE_SPISLAVE 0 // Not yet supported
48+
#define DEVICE_SPISLAVE 1
4949

5050
#define DEVICE_RTC 1
5151

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,44 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave) {
313313
#define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
314314

315315
int i2c_slave_receive(i2c_t *obj) {
316-
return (0);
316+
int retValue = NoData;
317+
uint32_t event;
318+
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
319+
320+
event = I2C_GetLastEvent( i2c );
321+
if(event != 0)
322+
{
323+
switch(event){
324+
case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:
325+
retValue = WriteAddressed;
326+
break;
327+
case I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED:
328+
retValue = ReadAddressed;
329+
break;
330+
case I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED:
331+
retValue = WriteGeneral;
332+
break;
333+
default:
334+
retValue = NoData;
335+
break;
336+
}
337+
338+
// clear ADDR
339+
if((retValue == WriteAddressed) || (retValue == ReadAddressed)){
340+
i2c->SR1;// read status register 1
341+
i2c->SR2;// read status register 2
342+
}
343+
// clear stopf
344+
if(I2C_GetFlagStatus(i2c, I2C_FLAG_STOPF) == SET) {
345+
i2c->SR1;// read status register 1
346+
I2C_Cmd(i2c, ENABLE);
347+
}
348+
// clear AF
349+
if(I2C_GetFlagStatus(i2c, I2C_FLAG_AF) == SET) {
350+
I2C_ClearFlag(i2c, I2C_FLAG_AF);
351+
}
352+
}
353+
return(retValue);
317354
}
318355

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

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
138138
} else { // Slave
139139
pinmap_pinout(ssel, PinMap_SPI_SSEL);
140140
obj->mode = SPI_Mode_Slave;
141-
obj->nss = SPI_NSS_Soft;
141+
obj->nss = SPI_NSS_Hard;
142142
}
143143

144144
init_spi(obj);
@@ -270,7 +270,8 @@ int spi_master_write(spi_t *obj, int value) {
270270
}
271271

272272
int spi_slave_receive(spi_t *obj) {
273-
return (ssp_readable(obj) && !ssp_busy(obj)) ? (1) : (0);
273+
//return (ssp_readable(obj) && !ssp_busy(obj)) ? (1) : (0); // initial code
274+
return (ssp_readable(obj)) ? (1) : (0); // works better like this
274275
};
275276

276277
int spi_slave_read(spi_t *obj) {

0 commit comments

Comments
 (0)