@@ -31,13 +31,13 @@ static bool twi_master_write(uint8_t * data, uint8_t data_length, bool issue_sto
31
31
return false;
32
32
}
33
33
34
- twi -> TXD = * data ++ ;
35
- twi -> TASKS_STARTTX = 1 ;
34
+ twi -> TXD = * data ++ ;
35
+ twi -> TASKS_STARTTX = 1 ;
36
36
37
37
/** @snippet [TWI HW master write] */
38
38
while (true)
39
39
{
40
- while (twi -> EVENTS_TXDSENT == 0 && twi -> EVENTS_ERROR == 0 && (-- timeout ))
40
+ while (twi -> EVENTS_TXDSENT == 0 && twi -> EVENTS_ERROR == 0 && (-- timeout ))
41
41
{
42
42
// Do nothing.
43
43
}
@@ -47,33 +47,33 @@ static bool twi_master_write(uint8_t * data, uint8_t data_length, bool issue_sto
47
47
// Recover the peripheral as indicated by PAN 56: "TWI: TWI module lock-up." found at
48
48
// Product Anomaly Notification document found at
49
49
// https://www.nordicsemi.com/eng/Products/Bluetooth-R-low-energy/nRF51822/#Downloads
50
- twi -> EVENTS_ERROR = 0 ;
51
- twi -> ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos ;
52
- twi -> POWER = 0 ;
50
+ twi -> EVENTS_ERROR = 0 ;
51
+ twi -> ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos ;
52
+ twi -> POWER = 0 ;
53
53
nrf_delay_us (5 );
54
- twi -> POWER = 1 ;
55
- twi -> ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos ;
54
+ twi -> POWER = 1 ;
55
+ twi -> ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos ;
56
56
57
- (void )twi_master_init_and_clear (twi );
57
+ (void )twi_master_init_and_clear (twi );
58
58
59
59
return false;
60
60
}
61
- twi -> EVENTS_TXDSENT = 0 ;
61
+ twi -> EVENTS_TXDSENT = 0 ;
62
62
if (-- data_length == 0 )
63
63
{
64
64
break ;
65
65
}
66
66
67
- twi -> TXD = * data ++ ;
67
+ twi -> TXD = * data ++ ;
68
68
}
69
69
/** @snippet [TWI HW master write] */
70
70
71
71
if (issue_stop_condition )
72
72
{
73
- twi -> EVENTS_STOPPED = 0 ;
74
- twi -> TASKS_STOP = 1 ;
73
+ twi -> EVENTS_STOPPED = 0 ;
74
+ twi -> TASKS_STOP = 1 ;
75
75
/* Wait until stop sequence is sent */
76
- while (twi -> EVENTS_STOPPED == 0 )
76
+ while (twi -> EVENTS_STOPPED == 0 )
77
77
{
78
78
// Do nothing.
79
79
}
@@ -95,39 +95,39 @@ static bool twi_master_read(uint8_t * data, uint8_t data_length, bool issue_stop
95
95
}
96
96
else if (data_length == 1 )
97
97
{
98
- NRF_PPI -> CH [0 ].TEP = (uint32_t )& twi -> TASKS_STOP ;
98
+ NRF_PPI -> CH [0 ].TEP = (uint32_t )& twi -> TASKS_STOP ;
99
99
}
100
100
else
101
101
{
102
- NRF_PPI -> CH [0 ].TEP = (uint32_t )& twi -> TASKS_SUSPEND ;
102
+ NRF_PPI -> CH [0 ].TEP = (uint32_t )& twi -> TASKS_SUSPEND ;
103
103
}
104
104
105
105
NRF_PPI -> CHENSET = PPI_CHENSET_CH0_Msk ;
106
- twi -> EVENTS_RXDREADY = 0 ;
107
- twi -> TASKS_STARTRX = 1 ;
106
+ twi -> EVENTS_RXDREADY = 0 ;
107
+ twi -> TASKS_STARTRX = 1 ;
108
108
109
109
/** @snippet [TWI HW master read] */
110
110
while (true)
111
111
{
112
- while (twi -> EVENTS_RXDREADY == 0 && NRF_TWI1 -> EVENTS_ERROR == 0 && (-- timeout ))
112
+ while (twi -> EVENTS_RXDREADY == 0 && NRF_TWI1 -> EVENTS_ERROR == 0 && (-- timeout ))
113
113
{
114
114
// Do nothing.
115
115
}
116
- twi -> EVENTS_RXDREADY = 0 ;
116
+ twi -> EVENTS_RXDREADY = 0 ;
117
117
118
- if (timeout == 0 || twi -> EVENTS_ERROR != 0 )
118
+ if (timeout == 0 || twi -> EVENTS_ERROR != 0 )
119
119
{
120
120
// Recover the peripheral as indicated by PAN 56: "TWI: TWI module lock-up." found at
121
121
// Product Anomaly Notification document found at
122
122
// https://www.nordicsemi.com/eng/Products/Bluetooth-R-low-energy/nRF51822/#Downloads
123
- twi -> EVENTS_ERROR = 0 ;
124
- twi -> ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos ;
125
- twi -> POWER = 0 ;
123
+ twi -> EVENTS_ERROR = 0 ;
124
+ twi -> ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos ;
125
+ twi -> POWER = 0 ;
126
126
nrf_delay_us (5 );
127
- twi -> POWER = 1 ;
128
- twi -> ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos ;
127
+ twi -> POWER = 1 ;
128
+ twi -> ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos ;
129
129
130
- (void )twi_master_init_and_clear (twi );
130
+ (void )twi_master_init_and_clear (twi );
131
131
132
132
return false;
133
133
}
@@ -149,16 +149,16 @@ static bool twi_master_read(uint8_t * data, uint8_t data_length, bool issue_stop
149
149
// Product Anomaly Notification document found at
150
150
// https://www.nordicsemi.com/eng/Products/Bluetooth-R-low-energy/nRF51822/#Downloads
151
151
nrf_delay_us (20 );
152
- twi -> TASKS_RESUME = 1 ;
152
+ twi -> TASKS_RESUME = 1 ;
153
153
}
154
154
/** @snippet [TWI HW master read] */
155
155
156
156
/* Wait until stop sequence is sent */
157
- while (twi -> EVENTS_STOPPED == 0 )
157
+ while (twi -> EVENTS_STOPPED == 0 )
158
158
{
159
159
// Do nothing.
160
160
}
161
- twi -> EVENTS_STOPPED = 0 ;
161
+ twi -> EVENTS_STOPPED = 0 ;
162
162
163
163
NRF_PPI -> CHENCLR = PPI_CHENCLR_CH0_Msk ;
164
164
return true;
@@ -180,8 +180,8 @@ static bool twi_master_clear_bus(NRF_TWI_Type* twi)
180
180
uint32_t data_pin_config ;
181
181
182
182
// Save and disable TWI hardware so software can take control over the pins.
183
- twi_state = twi -> ENABLE ;
184
- twi -> ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos ;
183
+ twi_state = twi -> ENABLE ;
184
+ twi -> ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos ;
185
185
186
186
clk_pin_config = \
187
187
NRF_GPIO -> PIN_CNF [TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER ];
@@ -234,7 +234,7 @@ static bool twi_master_clear_bus(NRF_TWI_Type* twi)
234
234
NRF_GPIO -> PIN_CNF [TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER ] = clk_pin_config ;
235
235
NRF_GPIO -> PIN_CNF [TWI_MASTER_CONFIG_DATA_PIN_NUMBER ] = data_pin_config ;
236
236
237
- twi -> ENABLE = twi_state ;
237
+ twi -> ENABLE = twi_state ;
238
238
239
239
return bus_clear ;
240
240
}
@@ -262,17 +262,17 @@ bool twi_master_init_and_clear(NRF_TWI_Type* twi)
262
262
| (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos ) \
263
263
| (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos );
264
264
265
- twi -> EVENTS_RXDREADY = 0 ;
266
- twi -> EVENTS_TXDSENT = 0 ;
267
- twi -> PSELSCL = TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER ;
268
- twi -> PSELSDA = TWI_MASTER_CONFIG_DATA_PIN_NUMBER ;
269
- twi -> FREQUENCY = TWI_FREQUENCY_FREQUENCY_K100 << TWI_FREQUENCY_FREQUENCY_Pos ;
270
- NRF_PPI -> CH [0 ].EEP = (uint32_t )& twi -> EVENTS_BB ;
271
- NRF_PPI -> CH [0 ].TEP = (uint32_t )& twi -> TASKS_SUSPEND ;
265
+ twi -> EVENTS_RXDREADY = 0 ;
266
+ twi -> EVENTS_TXDSENT = 0 ;
267
+ twi -> PSELSCL = TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER ;
268
+ twi -> PSELSDA = TWI_MASTER_CONFIG_DATA_PIN_NUMBER ;
269
+ twi -> FREQUENCY = TWI_FREQUENCY_FREQUENCY_K100 << TWI_FREQUENCY_FREQUENCY_Pos ;
270
+ NRF_PPI -> CH [0 ].EEP = (uint32_t )& twi -> EVENTS_BB ;
271
+ NRF_PPI -> CH [0 ].TEP = (uint32_t )& twi -> TASKS_SUSPEND ;
272
272
NRF_PPI -> CHENCLR = PPI_CHENCLR_CH0_Msk ;
273
- twi -> ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos ;
273
+ twi -> ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos ;
274
274
275
- return twi_master_clear_bus (twi );
275
+ return twi_master_clear_bus (twi );
276
276
}
277
277
278
278
@@ -281,21 +281,21 @@ bool twi_master_init_and_clear(NRF_TWI_Type* twi)
281
281
bool twi_master_transfer (uint8_t address ,
282
282
uint8_t * data ,
283
283
uint8_t data_length ,
284
- bool issue_stop_condition ,
285
- NRF_TWI_Type * twi )
284
+ bool issue_stop_condition ,
285
+ NRF_TWI_Type * twi )
286
286
{
287
287
bool transfer_succeeded = false;
288
- if (data_length > 0 && twi_master_clear_bus (twi ))
288
+ if (data_length > 0 && twi_master_clear_bus (twi ))
289
289
{
290
- twi -> ADDRESS = (address >> 1 );
290
+ twi -> ADDRESS = (address >> 1 );
291
291
292
- if ((address & TWI_READ_BIT ))
292
+ if ((address & TWI_READ_BIT ))
293
293
{
294
- transfer_succeeded = twi_master_read (data , data_length , issue_stop_condition , twi );
294
+ transfer_succeeded = twi_master_read (data , data_length , issue_stop_condition , twi );
295
295
}
296
296
else
297
297
{
298
- transfer_succeeded = twi_master_write (data , data_length , issue_stop_condition , twi );
298
+ transfer_succeeded = twi_master_write (data , data_length , issue_stop_condition , twi );
299
299
}
300
300
}
301
301
return transfer_succeeded ;
0 commit comments