# Freertos\_twi\_master not work for me

**URL:** <https://forums.freertos.org/t/freertos-twi-master-not-work-for-me/10029>\
**Category:** Kernel\
**Created:** [June 12, 2020, 5:20pm UTC](https://forums.freertos.org/t/freertos-twi-master-not-work-for-me/10029 "2020-06-12T17:20:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjdd88](https://avatars.discourse-cdn.com/v4/letter/j/f04885/32.png) [@jjdd88](https://forums.freertos.org/u/jjdd88)\
**Post date:** [June 12, 2020, 5:20pm UTC](https://forums.freertos.org/t/freertos-twi-master-not-work-for-me/10029/1 "2020-06-12T17:20:49Z")

</div>

I have an Arduino Due board + mcp4728 I2C DAC board. I can do the following from Arduino IDE with Wire Library.

Wire.begin();  
Wire.beginTransmission(0x60);  
Wire.write(0x2C);  
Wire.write(0);  
Wire.write(0);  
error = Wire.endTransmission();

I want to do the something using free freertos\_twi\_master and atmel studio 7

```
const freertos_peripheral_options_t freertos_driver_parameters = {
	.interrupt_priority = 0x0f,
	.operation_mode = TWI_I2C_MASTER,
	.options_flags =(USE_TX_ACCESS_SEM|USE_RX_ACCESS_MUTEX|WAIT_TX_COMPLETE|WAIT_RX_COMPLETE),
	.receive_buffer = NULL,
	.receive_buffer_size = 0
	};
uint8_t buffer[3];
buffer[0] = 0x2C;
buffer[1] = 0;
buffer[2] = 0;	

```

twi\_packet\_t packet = {  
.addr = 0,  
.addr\_length = 1,  
.buffer = buffer,  
.chip = 0x60,  
.length = 3  
};  
freertos\_twi\_if freertos\_twi = freertos\_twi\_master\_init(TWI0, &freertos\_driver\_parameters);  
status\_code\_t status = freertos\_twi\_write\_packet(freertos\_twi, &packet, 200);

But freertos\_twi\_write\_packet never return. Do I missing anything?

Thanks

Joseph

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [June 12, 2020, 6:21pm UTC](https://forums.freertos.org/t/freertos-twi-master-not-work-for-me/10029/2 "2020-06-12T18:21:51Z")

</div>

Does the whole system hang, or just the task calling that function. If the whole system, and you pause on the debugger, what is executing? If just that task, and you step into the function in the debugger, how far does it get?

---

<div class="post-metadata">

**Author:** ![jjdd88](https://avatars.discourse-cdn.com/v4/letter/j/f04885/32.png) [@jjdd88](https://forums.freertos.org/u/jjdd88)\
**Post date:** [June 12, 2020, 6:37pm UTC](https://forums.freertos.org/t/freertos-twi-master-not-work-for-me/10029/3 "2020-06-12T18:37:34Z")

</div>

Following is the call stack when I break,  
Looks like exception happen and not handled.  
maybe more init need to do before call them but not sure what is it.

XPA.elf! Dummy\_Handler Line: 221

XPA.elf! Line: 221

XPA.elf! PendSV\_Handler Line: 307

XPA.elf! Line: 307

XPA.elf! xQueueGenericReceive (xQueueHandle pxQueue, void \* const pvBuffer, void \* const pvBuffer@entry, portTickType xTicksToWait, long xJustPeeking, long xJustPeeking@entry) Line: 1093

XPA.elf! freertos\_optionally\_wait\_transfer\_completion (freertos\_dma\_event\_control\_t \* dma\_event\_control, freertos\_dma\_event\_control\_t \* dma\_event\_control@entry, xSemaphoreHandle notification\_semaphore, xSemaphoreHandle notification\_semaphore@entry, portTickType max\_block\_time\_ticks) Line: 375

XPA.elf! freertos\_twi\_write\_packet\_async (freertos\_twi\_if p\_twi, freertos\_twi\_if p\_twi@entry, twi\_packet\_t \* p\_packet, twi\_packet\_t \* p\_packet@entry, portTickType block\_time\_ticks, portTickType block\_time\_ticks@entry, xSemaphoreHandle notification\_semaphore, xSemaphoreHandle notification\_semaphore@entry) Line: 480

XPA.elf! main Line: 186

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [June 12, 2020, 8:04pm UTC](https://forums.freertos.org/t/freertos-twi-master-not-work-for-me/10029/4 "2020-06-12T20:04:09Z")

</div>

Is the rest of your system functioning as expected (tasks switching, tick count incrementing) if you don’t call freertos\_twi\_write\_packet() anywhere? Do you have the TWI interrupt installed? Do you have configASSERT() defined?

---

<div class="post-metadata">

**Author:** ![jjdd88](https://avatars.discourse-cdn.com/v4/letter/j/f04885/32.png) [@jjdd88](https://forums.freertos.org/u/jjdd88)\
**Post date:** [June 12, 2020, 10:42pm UTC](https://forums.freertos.org/t/freertos-twi-master-not-work-for-me/10029/5 "2020-06-12T22:42:15Z")

</div>

1. Comment out freertos\_twi\_write\_packet() my code runs well.

2. The symptom is waiting for transmission complete forever. I think TWI interrupt is inside freertos\_twi\_master.c. I do add the following code before freertos\_twi\_master\_init but still same result.  
PS: I do change my code from TWI0 to TWI1. Because according to Arduino due, TWI1 has pull up resister but TWI0 no.

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [June 13, 2020, 7:59am UTC](https://forums.freertos.org/t/freertos-twi-master-not-work-for-me/10029/6 "2020-06-13T07:59:42Z")

</div>

Do you really have `TWI1_Handler` defined somewhere ? That’s the required interrupt handler. Usually it‘s set up as interrupt handler (in exception table) by default as weak symbol which is aliased by `Dummy_Handler` if it‘s not defined in application code.

---

<div class="post-metadata">

**Author:** ![jjdd88](https://avatars.discourse-cdn.com/v4/letter/j/f04885/32.png) [@jjdd88](https://forums.freertos.org/u/jjdd88)\
**Post date:** [June 15, 2020, 3:59pm UTC](https://forums.freertos.org/t/freertos-twi-master-not-work-for-me/10029/7 "2020-06-15T15:59:55Z")

</div>

Progress!

It was my fault that I put the code at the beginning of main. It is much better when I put the code in the beginning of a task.

Now the first freertos\_twi\_write\_packet return 0  
But second freertos\_twi\_write\_packet return ERR\_TIMEOUT at freertos\_obtain\_peripheral\_access\_semphore

Do I need to something after call freertos\_twi\_write\_packet? or call freertos\_twi\_master\_init everytime before freertos\_twi\_write\_packet?

Thanks

Joseph

---

<div class="post-metadata">

**Author:** ![jjdd88](https://avatars.discourse-cdn.com/v4/letter/j/f04885/32.png) [@jjdd88](https://forums.freertos.org/u/jjdd88)\
**Post date:** [June 17, 2020, 12:08am UTC](https://forums.freertos.org/t/freertos-twi-master-not-work-for-me/10029/8 "2020-06-17T00:08:16Z")

</div>

I just want to update the status.  
I still cannot make freeRTOS work but my problem solved.  
I add the following into conf\_board.h and my Wire Library port from Arduino IDE to Atmel Studio 7 work.

#define CONF\_BOARD\_TWI1
