Skip to content

Commit ca173a1

Browse files
committed
[NUCLEO_F030R8] Add SPI api, corrections in I2C
1 parent 08941aa commit ca173a1

File tree

3 files changed

+307
-63
lines changed

3 files changed

+307
-63
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#define DEVICE_I2C 1
4545
#define DEVICE_I2CSLAVE 0
4646

47-
#define DEVICE_SPI 0
47+
#define DEVICE_SPI 1
4848
#define DEVICE_SPISLAVE 0
4949

5050
#define DEVICE_RTC 1

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

Lines changed: 48 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@
4242
#define LONG_TIMEOUT ((int)0x8000)
4343

4444
static const PinMap PinMap_I2C_SDA[] = {
45-
{PB_9, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_NOPULL, GPIO_AF_1)},
45+
{PB_9, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_1)},
4646
{NC, NC, 0}
4747
};
4848

4949
static const PinMap PinMap_I2C_SCL[] = {
50-
{PB_8, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_NOPULL, GPIO_AF_1)},
50+
{PB_8, I2C_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP, GPIO_AF_1)},
5151
{NC, NC, 0}
5252
};
5353

@@ -71,10 +71,10 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
7171
//}
7272

7373
// Configure I2C pins
74-
pinmap_pinout(sda, PinMap_I2C_SDA);
7574
pinmap_pinout(scl, PinMap_I2C_SCL);
76-
pin_mode(sda, OpenDrain);
7775
pin_mode(scl, OpenDrain);
76+
pinmap_pinout(sda, PinMap_I2C_SDA);
77+
pin_mode(sda, OpenDrain);
7878

7979
// Reset to clear pending flags if any
8080
i2c_reset(obj);
@@ -120,12 +120,15 @@ void i2c_frequency(i2c_t *obj, int hz) {
120120

121121
inline int i2c_start(i2c_t *obj) {
122122
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
123-
int timeout = LONG_TIMEOUT;
123+
int timeout;
124124

125125
// Test BUSY Flag
126-
while (I2C_GetFlagStatus(i2c, I2C_ISR_BUSY) != RESET)
127-
{
128-
if ((timeout--) == 0) return 0;
126+
timeout = LONG_TIMEOUT;
127+
while (I2C_GetFlagStatus(i2c, I2C_ISR_BUSY) != RESET) {
128+
timeout--;
129+
if (timeout == 0) {
130+
return 0;
131+
}
129132
}
130133

131134
I2C_GenerateSTART(i2c, ENABLE);
@@ -135,68 +138,47 @@ inline int i2c_start(i2c_t *obj) {
135138

136139
inline int i2c_stop(i2c_t *obj) {
137140
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
138-
141+
139142
I2C_GenerateSTOP(i2c, ENABLE);
140-
143+
141144
return 0;
142145
}
143146

144147
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
145148
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
146-
int timeout;
147149
int count;
148150
int value;
149-
151+
150152
if (length == 0) return 0;
151153

152154
// Configure slave address, nbytes, reload, end mode and start or stop generation
153-
if (stop) {
154-
I2C_TransferHandling(i2c, address, length, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);
155-
}
156-
else {
157-
I2C_TransferHandling(i2c, address, length, I2C_Reload_Mode, I2C_Generate_Start_Read);
158-
}
155+
I2C_TransferHandling(i2c, address, length, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);
159156

160157
// Read all bytes
161158
for (count = 0; count < length; count++) {
162159
value = i2c_byte_read(obj, 0);
163160
data[count] = (char)value;
164161
}
165162

166-
if (stop) {
167-
// Wait until STOPF flag is set
168-
timeout = LONG_TIMEOUT;
169-
while (I2C_GetFlagStatus(i2c, I2C_ISR_STOPF) == RESET)
170-
{
171-
if ((timeout--) == 0) return 0;
172-
}
173-
174-
// Clear STOPF flag
175-
I2C_ClearFlag(i2c, I2C_ICR_STOPCF);
176-
}
177-
178163
return length;
179164
}
180165

181166
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
182167
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
183-
int timeout;
168+
//int timeout;
184169
int count;
170+
171+
if (length == 0) return 0;
172+
173+
// TODO: the stop is always sent even with I2C_SoftEnd_Mode. To be corrected.
185174

186-
// Test BUSY Flag
187-
//timeout = LONG_TIMEOUT;
188-
//while (I2C_GetFlagStatus(i2c, I2C_ISR_BUSY) != RESET)
189-
//{
190-
// if((timeout--) == 0) return 0;
191-
//}
192-
193175
// Configure slave address, nbytes, reload, end mode and start or stop generation
194-
if (stop) {
176+
//if (stop) {
195177
I2C_TransferHandling(i2c, address, length, I2C_AutoEnd_Mode, I2C_Generate_Start_Write);
196-
}
197-
else {
198-
I2C_TransferHandling(i2c, address, length, I2C_Reload_Mode, I2C_Generate_Start_Write);
199-
}
178+
//}
179+
//else {
180+
// I2C_TransferHandling(i2c, address, length, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);
181+
//}
200182

201183
// Write all bytes
202184
for (count = 0; count < length; count++) {
@@ -206,29 +188,36 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
206188
}
207189
}
208190

191+
/*
209192
if (stop) {
210193
// Wait until STOPF flag is set
211194
timeout = LONG_TIMEOUT;
212-
while (I2C_GetFlagStatus(i2c, I2C_ISR_STOPF) == RESET)
213-
{
214-
if ((timeout--) == 0) return 0;
215-
}
216-
195+
while (I2C_GetFlagStatus(i2c, I2C_ISR_STOPF) == RESET) {
196+
timeout--;
197+
if (timeout == 0) {
198+
return 0;
199+
}
200+
}
217201
// Clear STOPF flag
218202
I2C_ClearFlag(i2c, I2C_ICR_STOPCF);
219203
}
204+
*/
220205

221206
return count;
222207
}
223208

224209
int i2c_byte_read(i2c_t *obj, int last) {
225210
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
226211
uint8_t data;
227-
int timeout = FLAG_TIMEOUT;
212+
int timeout;
228213

229214
// Wait until the byte is received
215+
timeout = FLAG_TIMEOUT;
230216
while (I2C_GetFlagStatus(i2c, I2C_ISR_RXNE) == RESET) {
231-
if ((timeout--) == 0) return 0;
217+
timeout--;
218+
if (timeout == 0) {
219+
return 0;
220+
}
232221
}
233222

234223
data = I2C_ReceiveData(i2c);
@@ -238,21 +227,18 @@ int i2c_byte_read(i2c_t *obj, int last) {
238227

239228
int i2c_byte_write(i2c_t *obj, int data) {
240229
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
241-
int timeout = FLAG_TIMEOUT;
230+
int timeout;
242231

243-
// Wait until TXIS flag is set
244-
timeout = LONG_TIMEOUT;
245-
while (I2C_GetFlagStatus(i2c, I2C_ISR_TXIS) == RESET)
246-
{
247-
if ((timeout--) == 0) return 0;
232+
// Wait until the previous byte is transmitted
233+
timeout = FLAG_TIMEOUT;
234+
while (I2C_GetFlagStatus(i2c, I2C_ISR_TXIS) == RESET) {
235+
timeout--;
236+
if (timeout == 0) {
237+
return 0;
238+
}
248239
}
249-
240+
250241
I2C_SendData(i2c, (uint8_t)data);
251-
252-
// Wait until the byte is transmitted
253-
//while (I2C_GetFlagStatus(i2c, I2C_ISR_TCR) == RESET) {
254-
// if ((timeout--) == 0) return 0;
255-
//}
256242

257243
return 1;
258244
}

0 commit comments

Comments
 (0)