Skip to content

Commit 06edaf7

Browse files
committed
Merge pull request #294 from mazgch/master
increase timeout for slow I2C slaves that make use of extensive clock stretching
2 parents 7751e75 + 94ac072 commit 06edaf7

File tree

5 files changed

+35
-45
lines changed

5 files changed

+35
-45
lines changed

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_K20D5M/i2c_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ int i2c_stop(i2c_t *obj) {
102102
}
103103

104104
static int timeout_status_poll(i2c_t *obj, uint32_t mask) {
105-
uint32_t i, timeout = 1000;
105+
uint32_t i, timeout = 100000;
106106

107107
for (i = 0; i < timeout; i++) {
108108
if (obj->i2c->S & mask)

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KLXX/i2c_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ int i2c_stop(i2c_t *obj) {
9797
}
9898

9999
static int timeout_status_poll(i2c_t *obj, uint32_t mask) {
100-
uint32_t i, timeout = 1000;
100+
uint32_t i, timeout = 100000;
101101

102102
for (i = 0; i < timeout; i++) {
103103
if (obj->i2c->S & mask)

libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_K64F/i2c_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ int i2c_stop(i2c_t *obj) {
8484
}
8585

8686
static int timeout_status_poll(i2c_t *obj, uint32_t mask) {
87-
uint32_t i, timeout = 1000;
87+
uint32_t i, timeout = 100000;
8888

8989
for (i = 0; i < timeout; i++) {
9090
if (HW_I2C_S_RD(obj->instance) & mask)

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC15XX/i2c_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
138138
LPC_I2C0->MSTCTL = (1 << 0);
139139
data[count] = (LPC_I2C0->MSTDAT & 0xFF);
140140
status = ((LPC_I2C0->STAT >> 1) & (0x07));
141-
if (status != 0x00) {
141+
if (status != 0x01) {
142142
i2c_stop(obj);
143143
return count;
144144
}

libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC15XX/serial_api.c

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ static const SWM_Map SWM_UART_RX[] = {
4242
static const SWM_Map SWM_UART_RTS[] = {
4343
{0, 16},
4444
{1, 24},
45-
{3, 0},
45+
{3, 0}, // not available
4646
};
4747

4848
static const SWM_Map SWM_UART_CTS[] = {
4949
{0, 24},
5050
{2, 0},
51-
{3, 8}
51+
{3, 8} // not available
5252
};
5353

5454
// bit flags for used UARTs
@@ -82,9 +82,29 @@ static uart_irq_handler irq_handler;
8282
int stdio_uart_inited = 0;
8383
serial_t stdio_uart;
8484

85+
static void switch_pin(const SWM_Map *swm, PinName pn)
86+
{
87+
uint32_t regVal;
88+
if (pn != NC)
89+
{
90+
// check if we have any function mapped to this pin already and remove it
91+
for (int n = 0; n < sizeof(LPC_SWM->PINASSIGN)/sizeof(*LPC_SWM->PINASSIGN); n ++) {
92+
regVal = LPC_SWM->PINASSIGN[n];
93+
for (int j = 0; j <= 24; j += 8) {
94+
if (((regVal >> j) & 0xFF) == pn)
95+
regVal |= (0xFF << j);
96+
}
97+
LPC_SWM->PINASSIGN[n] = regVal;
98+
}
99+
}
100+
// now map it
101+
regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
102+
LPC_SWM->PINASSIGN[swm->n] = regVal | (pn << swm->offset);
103+
}
104+
85105
void serial_init(serial_t *obj, PinName tx, PinName rx) {
86106
int is_stdio_uart = 0;
87-
107+
88108
int uart_n = get_available_uart();
89109
if (uart_n == -1) {
90110
error("No available UART");
@@ -93,16 +113,8 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
93113
obj->uart = (LPC_USART0_Type *)(LPC_USART0_BASE + (0x4000 * uart_n));
94114
uart_used |= (1 << uart_n);
95115

96-
const SWM_Map *swm;
97-
uint32_t regVal;
98-
99-
swm = &SWM_UART_TX[uart_n];
100-
regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
101-
LPC_SWM->PINASSIGN[swm->n] = regVal | (tx << swm->offset);
102-
103-
swm = &SWM_UART_RX[uart_n];
104-
regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
105-
LPC_SWM->PINASSIGN[swm->n] = regVal | (rx << swm->offset);
116+
switch_pin(&SWM_UART_TX[uart_n], tx);
117+
switch_pin(&SWM_UART_RX[uart_n], rx);
106118

107119
/* uart clock divided by 6 */
108120
LPC_SYSCON->UARTCLKDIV =6;
@@ -296,33 +308,11 @@ void serial_break_clear(serial_t *obj) {
296308
}
297309

298310
void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow) {
299-
const SWM_Map *swm_rts, *swm_cts;
300-
uint32_t regVal_rts, regVal_cts;
301-
302-
swm_rts = &SWM_UART_RTS[obj->index];
303-
swm_cts = &SWM_UART_CTS[obj->index];
304-
regVal_rts = LPC_SWM->PINASSIGN[swm_rts->n] & ~(0xFF << swm_rts->offset);
305-
regVal_cts = LPC_SWM->PINASSIGN[swm_cts->n] & ~(0xFF << swm_cts->offset);
306-
307-
if (FlowControlNone == type) {
308-
LPC_SWM->PINASSIGN[swm_rts->n] = regVal_rts | (0xFF << swm_rts->offset);
309-
LPC_SWM->PINASSIGN[swm_cts->n] = regVal_cts | (0xFF << swm_cts->offset);
310-
obj->uart->CFG &= ~CTSEN;
311-
return;
312-
}
313-
if ((FlowControlRTS == type || FlowControlRTSCTS == type) && (rxflow != NC)) {
314-
LPC_SWM->PINASSIGN[swm_rts->n] = regVal_rts | (rxflow << swm_rts->offset);
315-
if (FlowControlRTS == type) {
316-
LPC_SWM->PINASSIGN[swm_cts->n] = regVal_cts | (0xFF << swm_cts->offset);
317-
obj->uart->CFG &= ~CTSEN;
318-
}
319-
}
320-
if ((FlowControlCTS == type || FlowControlRTSCTS == type) && (txflow != NC)) {
321-
LPC_SWM->PINASSIGN[swm_cts->n] = regVal_cts | (txflow << swm_cts->offset);
322-
obj->uart->CFG |= CTSEN;
323-
if (FlowControlCTS == type) {
324-
LPC_SWM->PINASSIGN[swm_rts->n] = regVal_rts | (0xFF << swm_rts->offset);
325-
}
326-
}
311+
if ((FlowControlNone == type || FlowControlRTS == type)) txflow = NC;
312+
if ((FlowControlNone == type || FlowControlCTS == type)) rxflow = NC;
313+
switch_pin(&SWM_UART_RTS[obj->index], rxflow);
314+
switch_pin(&SWM_UART_CTS[obj->index], txflow);
315+
if (txflow == NC) obj->uart->CFG &= ~CTSEN;
316+
else obj->uart->CFG |= CTSEN;
327317
}
328318

0 commit comments

Comments
 (0)