Skip to content

Commit da605b8

Browse files
committed
Merge pull request #126 from bcostm/master
[NUCLEO_F103RB] Update PWM IOs used + I2C cleanup
2 parents f5e1f70 + 18136e3 commit da605b8

File tree

3 files changed

+77
-71
lines changed

3 files changed

+77
-71
lines changed

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

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,13 @@
2727
#define FLAG_TIMEOUT ((int)0x1000)
2828
#define LONG_TIMEOUT ((int)0x8000)
2929

30-
// Functions exit codes
31-
#define EXIT_OK (0)
32-
#define EXIT_FAIL (1)
33-
#define EXIT_TIMEOUT (0xFFFFFFFF)
34-
3530
static const PinMap PinMap_I2C_SDA[] = {
36-
//{PB_7, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 0)}, // Cannot be used due to TIM4
37-
{PB_9, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 7)}, // GPIO_Remap_I2C1
31+
{PB_9, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 8)}, // GPIO_Remap_I2C1
3832
{NC, NC, 0}
3933
};
4034

4135
static const PinMap PinMap_I2C_SCL[] = {
42-
//{PB_6, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 0)}, // // Cannot be used due to TIM4
43-
{PB_8, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 7)}, // GPIO_Remap_I2C1
36+
{PB_8, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 8)}, // GPIO_Remap_I2C1
4437
{NC, NC, 0}
4538
};
4639

@@ -107,17 +100,19 @@ inline int i2c_start(i2c_t *obj) {
107100
//while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_MODE_SELECT) == ERROR) {
108101
while (I2C_GetFlagStatus(i2c, I2C_FLAG_SB) == RESET) {
109102
if ((timeout--) == 0) {
110-
return EXIT_TIMEOUT;
103+
return 1;
111104
}
112105
}
113106

114-
return EXIT_OK;
107+
return 0;
115108
}
116109

117110
inline int i2c_stop(i2c_t *obj) {
118111
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
112+
119113
I2C_GenerateSTOP(i2c, ENABLE);
120-
return EXIT_OK;
114+
115+
return 0;
121116
}
122117

123118
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
@@ -133,7 +128,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
133128
timeout = LONG_TIMEOUT;
134129
while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
135130
if ((timeout--) == 0) {
136-
return EXIT_TIMEOUT;
131+
return 0;
137132
}
138133
}
139134
*/
@@ -147,7 +142,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
147142
timeout = FLAG_TIMEOUT;
148143
while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) == ERROR) {
149144
if ((timeout--) == 0) {
150-
return EXIT_TIMEOUT;
145+
return 0;
151146
}
152147
}
153148

@@ -180,7 +175,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
180175
timeout = LONG_TIMEOUT;
181176
while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
182177
if ((timeout--) == 0) {
183-
return EXIT_TIMEOUT;
178+
return 0;
184179
}
185180
}
186181
*/
@@ -194,13 +189,14 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
194189
timeout = FLAG_TIMEOUT;
195190
while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == ERROR) {
196191
if ((timeout--) == 0) {
197-
return EXIT_TIMEOUT;
192+
return 0;
198193
}
199194
}
200195

201196
for (count = 0; count < length; count++) {
202-
if (i2c_byte_write(obj, data[count]) != EXIT_OK) {
203-
return EXIT_FAIL;
197+
if (i2c_byte_write(obj, data[count]) != 1) {
198+
i2c_stop(obj);
199+
return 0;
204200
}
205201
}
206202

@@ -229,7 +225,7 @@ int i2c_byte_read(i2c_t *obj, int last) {
229225
timeout = FLAG_TIMEOUT;
230226
while (I2C_GetFlagStatus(i2c, I2C_FLAG_RXNE) == RESET) {
231227
if ((timeout--) == 0) {
232-
return EXIT_TIMEOUT;
228+
return 0;
233229
}
234230
}
235231

@@ -250,11 +246,11 @@ int i2c_byte_write(i2c_t *obj, int data) {
250246
while ((I2C_GetFlagStatus(i2c, I2C_FLAG_TXE) == RESET) &&
251247
(I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) {
252248
if ((timeout--) == 0) {
253-
return EXIT_TIMEOUT;
249+
return 0;
254250
}
255251
}
256252

257-
return EXIT_OK;
253+
return 1;
258254
}
259255

260256
void i2c_reset(i2c_t *obj) {
@@ -288,29 +284,37 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave) {
288284
// Nothing to do
289285
}
290286

291-
#define NoData 0
292-
#define ReadAddressed 1
293-
#define WriteGeneral 2
294-
#define WriteAddressed 3
287+
// See I2CSlave.h
288+
#define NoData 0 // the slave has not been addressed
289+
#define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
290+
#define WriteGeneral 2 // the master is writing to all slave
291+
#define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
295292

296293
int i2c_slave_receive(i2c_t *obj) {
297-
//I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
298-
int retval = NoData;
299-
//int status;
300-
301-
//if (I2C_GetFlagStatus(i2c, I2C_FLAG_GENCALL) == SET) retval = WriteGeneral;
302-
303-
//status = I2C_GetLastEvent(i2c);
304-
305-
return(retval);
294+
// TO BE DONE
295+
return(0);
306296
}
307297

308298
int i2c_slave_read(i2c_t *obj, char *data, int length) {
309-
return 0;
299+
int count = 0;
300+
301+
// Read all bytes
302+
for (count = 0; count < length; count++) {
303+
data[count] = i2c_byte_read(obj, 0);
304+
}
305+
306+
return count;
310307
}
311308

312309
int i2c_slave_write(i2c_t *obj, const char *data, int length) {
313-
return 0;
310+
int count = 0;
311+
312+
// Write all bytes
313+
for (count = 0; count < length; count++) {
314+
i2c_byte_write(obj, data[count]);
315+
}
316+
317+
return count;
314318
}
315319

316320

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818

1919
// Alternate-function mapping
2020
static const uint32_t AF_mapping[] = {
21-
0, // 0 = No AF
22-
GPIO_Remap_SPI1, // 1
23-
GPIO_Remap_I2C1, // 2
24-
GPIO_Remap_USART1, // 3
25-
GPIO_Remap_USART2, // 4
26-
GPIO_FullRemap_TIM2, // 5
27-
GPIO_FullRemap_TIM3, // 6
28-
GPIO_Remap_I2C1 // 7
21+
0, // 0 = No AF
22+
GPIO_Remap_SPI1, // 1
23+
GPIO_Remap_I2C1, // 2
24+
GPIO_Remap_USART1, // 3
25+
GPIO_Remap_USART2, // 4
26+
GPIO_FullRemap_TIM2, // 5
27+
GPIO_FullRemap_TIM3, // 6
28+
GPIO_PartialRemap_TIM3, // 7
29+
GPIO_Remap_I2C1 // 8
2930
};
3031

3132
/**

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

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@
2121

2222
// Only TIM2 and TIM3 can be used (TIM1 and TIM4 are used by the us_ticker)
2323
static const PinMap PinMap_PWM[] = {
24-
// TIM2
25-
{PA_2, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM2_CH3 - ARDUINO D1 (extra)
26-
{PA_3, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM2_CH4 - ARDUINO D0 (extra)
27-
// TIM2 remap
28-
{PB_3, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 5)}, // TIM2r_CH2 - ARDUINO D3
29-
{PB_10, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 5)}, // TIM2r_CH3 - ARDUINO D6
30-
// TIM3
31-
{PA_6, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH1 - ARDUINO D12 (extra)
32-
{PA_7, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH2 - ARDUINO D11
33-
// TIM3 remap
34-
{PB_4, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM3r_CH1 - ARDUINO D5
35-
{PC_7, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM3r_CH2 - ARDUINO D9
24+
// TIM2 default
25+
//{PA_2, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM2_CH3 - ARDUINO D1
26+
//{PA_3, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM2_CH4 - ARDUINO D0
27+
// TIM2 full remap
28+
{PB_3, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 5)}, // TIM2fr_CH2 - ARDUINO D3
29+
//{PB_10, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 5)}, // TIM2fr_CH3 - ARDUINO D6
30+
// TIM3 default
31+
//{PA_6, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH1 - ARDUINO D12
32+
//{PA_7, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH2 - ARDUINO D11
33+
// TIM3 full remap
34+
//{PC_7, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM3fr_CH2 - ARDUINO D9
35+
// TIM3 partial remap
36+
{PB_4, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 7)}, // TIM3pr_CH1 - ARDUINO D5
3637
{NC, NC, 0}
3738
};
3839

@@ -83,29 +84,29 @@ void pwmout_write(pwmout_t* obj, float value) {
8384
TIM_OCInitStructure.TIM_Pulse = obj->pulse;
8485
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
8586

86-
// TIM Channel 1
87-
if ((obj->pin == PA_6) || (obj->pin == PB_4)) {
87+
// Configure channel 1
88+
if (obj->pin == PB_4) {
8889
TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable);
8990
TIM_OC1Init(tim, &TIM_OCInitStructure);
9091
}
9192

92-
// TIM Channel 2
93-
if ((obj->pin == PA_7) || (obj->pin == PB_3) || (obj->pin == PC_7)) {
93+
// Configure channel 2
94+
if (obj->pin == PB_3) {
9495
TIM_OC2PreloadConfig(tim, TIM_OCPreload_Enable);
9596
TIM_OC2Init(tim, &TIM_OCInitStructure);
9697
}
9798

98-
// TIM Channel 3
99-
if ((obj->pin == PA_2) || (obj->pin == PB_10)) {
100-
TIM_OC3PreloadConfig(tim, TIM_OCPreload_Enable);
101-
TIM_OC3Init(tim, &TIM_OCInitStructure);
102-
}
103-
104-
// TIM Channel 4
105-
if (obj->pin == PA_3) {
106-
TIM_OC4PreloadConfig(tim, TIM_OCPreload_Enable);
107-
TIM_OC4Init(tim, &TIM_OCInitStructure);
108-
}
99+
// Configure channel 3
100+
//if (obj->pin == PB_10) {
101+
// TIM_OC3PreloadConfig(tim, TIM_OCPreload_Enable);
102+
// TIM_OC3Init(tim, &TIM_OCInitStructure);
103+
//}
104+
105+
// Configure channel 4
106+
//if (obj->pin == PA_3) {
107+
// TIM_OC4PreloadConfig(tim, TIM_OCPreload_Enable);
108+
// TIM_OC4Init(tim, &TIM_OCInitStructure);
109+
//}
109110
}
110111

111112
float pwmout_read(pwmout_t* obj) {

0 commit comments

Comments
 (0)