Skip to content

Commit b15f1e5

Browse files
committed
[NUCLEO_L152RE] add SPI slave and I2C slave
1 parent ae75c78 commit b15f1e5

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
@@ -314,7 +314,44 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave) {
314314
#define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
315315

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

320357
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
@@ -139,7 +139,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
139139
else { // Slave
140140
pinmap_pinout(ssel, PinMap_SPI_SSEL);
141141
obj->mode = SPI_Mode_Slave;
142-
obj->nss = SPI_NSS_Soft;
142+
obj->nss = SPI_NSS_Hard;
143143
}
144144

145145
init_spi(obj);
@@ -288,7 +288,8 @@ int spi_master_write(spi_t *obj, int value) {
288288
}
289289

290290
int spi_slave_receive(spi_t *obj) {
291-
return (ssp_readable(obj) && !ssp_busy(obj)) ? (1) : (0);
291+
//return (ssp_readable(obj) && !ssp_busy(obj)) ? (1) : (0); // initial code
292+
return (ssp_readable(obj)) ? (1) : (0); // works better like this
292293
};
293294

294295
int spi_slave_read(spi_t *obj) {

0 commit comments

Comments
 (0)