# MSP430 LMIC library porting

**URL:** <https://www.thethingsnetwork.org/forum/t/msp430-lmic-library-porting/58151>\
**Category:** LMIC\
**Tags:** lmic\
**Created:** [July 24, 2022, 5:38pm UTC](https://www.thethingsnetwork.org/forum/t/msp430-lmic-library-porting/58151 "2022-07-24T17:38:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mirko-newcastledemo](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/mirko-newcastledemo/32/35697_2.png) [@mirko-newcastledemo](https://www.thethingsnetwork.org/forum/u/mirko-newcastledemo)\
**Post date:** [July 24, 2022, 5:38pm UTC](https://www.thethingsnetwork.org/forum/t/msp430-lmic-library-porting/58151/1 "2022-07-24T17:38:46Z")

</div>

Hi there,

I’m trying to port this [IBM LMIC library](https://github.com/mcci-catena/ibm-lmic) to the MSP430 platform.  
I have implemented the HAL but I’m struggling to get the timing correctly. The library is somehow working, I can see some “Receive uplink message” on the gateway dashboard once in a while or if I use the debug on the MSP430.

To count the ticks, I set up a timer using the 1MHz SMCLK with a divider of 8 and further divider of 2 in order to fire the interrupt every 1s. This way every tick is approximately 16us and `OSTICKS_PER_SEC` is 62500. My problem is that I don’t have the micros() function so I’m quite lost on how to proceed. I read [this](https://www.thethingsnetwork.org/forum/t/use-32khz-clock-source-for-hal-ticks/52369) other topic but still no solutions.

```auto
static void hal_time_init () {
  TA0CCTL0 |= CCIE;                     
  TA0EX0 = TAIDEX_1; // TAIDEX_1 is dividing the clock by 2
  TA0CTL |= TASSEL __SMCLK | ID__ 8 | MC__CONTINUOUS; // SMCLK/8, continuous mode
   __bis_SR_register(GIE);
}

```

```auto

#if defined( __TI_COMPILER_VERSION__ ) || defined( __IAR_SYSTEMS_ICC__ )
#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer0_A0_ISR(void)
#elif defined( __GNUC__ )
void __attribute__ ((interrupt(TIMER1_A0_VECTOR))) Timer0_A0_ISR (void)
#else
#error Compiler not supported!
#endif
{
  counter_tick+=1;
  TA0CCR0 += 62500; 
}

```

I need some help implementing the hal\_ticks function. This is what I did so far:

```auto
u4_t hal_ticks () {
  volatile u4_t seconds = sec2osticks(counter_tick); 
  return (u4_t)(sec);
}

```

Using the debugger I can see that after the initialisation my program keep going into the hal\_io\_check function forever.

```auto
static void hal_io_check() {
    uint8_t GPIO_0 = (P8IN >> 0) & 0x01; // equivalent of DigitalRead(pin)
    uint8_t GPIO_1 = (P7IN >> 5) & 0x01;
   
    if (dio_states[0] != GPIO_0)
    {
        dio_states[0] = !dio_states[0];
        if (dio_states[0])
            radio_irq_handler(0);
    }

    if (dio_states[1] != GPIO_1)
    {
        dio_states[1] = !dio_states[1];
        if (dio_states[1])
            radio_irq_handler(1);
    }
}

```

All the other functions are pretty much standard SPI stuff and equal to the Arduino versions so I’m sure that the problem is in how I’m counting the ticks. I’ve been working on this porting for over 2 weeks now and at this point, I’m completely out of ideas. I would be very glad if you could give me some directions!

---

<div class="post-metadata">

**Author:** ![descartes](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/descartes/32/47220_2.png) [@descartes](https://www.thethingsnetwork.org/forum/u/descartes)\
**Post date:** [July 24, 2022, 6:15pm UTC](https://www.thethingsnetwork.org/forum/t/msp430-lmic-library-porting/58151/2 "2022-07-24T18:15:31Z")

</div>

> [@mirko-newcastledemo](#):
>
> I would be very glad if you could give me some directions!

Stop and move on to porting the MCCI LMIC because the 6 year old IBM library is barely able to talk LoRaWAN, so even if you do get it working it will be largely incompatible with most Network Servers expectations.

Apart from that, it would seem the internal state machine is constantly polling the radio status to see if something has finished or happened - bit hard to know if this is down to the age of the library or something to do with the porting.

---

<div class="post-metadata">

**Author:** ![mirko-newcastledemo](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/mirko-newcastledemo/32/35697_2.png) [@mirko-newcastledemo](https://www.thethingsnetwork.org/forum/u/mirko-newcastledemo)\
**Post date:** [July 24, 2022, 7:39pm UTC](https://www.thethingsnetwork.org/forum/t/msp430-lmic-library-porting/58151/3 "2022-07-24T19:39:12Z")

</div>

But If I use this library on Arduino it works ok. This is why I think there must be something wrong in my implementation. But I get you, there is no point in wasting time with an old library. Is [this](https://github.com/mcci-catena/arduino-lorawan) the MCCI LMIC library?

---

<div class="post-metadata">

**Author:** ![descartes](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/descartes/32/47220_2.png) [@descartes](https://www.thethingsnetwork.org/forum/u/descartes)\
**Post date:** [July 24, 2022, 10:46pm UTC](https://www.thethingsnetwork.org/forum/t/msp430-lmic-library-porting/58151/4 "2022-07-24T22:46:27Z")

</div>

> [@mirko-newcastledemo](#):
>
> Is [this](https://github.com/mcci-catena/arduino-lorawan) the MCCI LMIC library?

Does that have the core LMIC and HAL etc?

---

<div class="post-metadata">

**Author:** ![DavidTaylor\_DPI](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/davidtaylor_dpi/32/35697_2.png) [@DavidTaylor\_DPI](https://www.thethingsnetwork.org/forum/u/DavidTaylor_DPI)\
**Post date:** [July 28, 2022, 3:25am UTC](https://www.thethingsnetwork.org/forum/t/msp430-lmic-library-porting/58151/5 "2022-07-28T03:25:51Z")

</div>

I have replaced the default HAL time code with a 32768 Hz timer - that’s the frequency MCCI LMIC uses by default.

See the post [A fork to add SAMD21 standby support to LMIC](https://www.thethingsnetwork.org/forum/t/a-fork-to-add-samd21-standby-support-to-lmic/56700)

The timer initialisation code won’t help you, but you should be able to get the idea - initialise a timer at the correct frequency and make a couple of other changes.

It seems to work well.

Regards, David.

---

<div class="post-metadata">

**Author:** ![mirko-newcastledemo](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/mirko-newcastledemo/32/35697_2.png) [@mirko-newcastledemo](https://www.thethingsnetwork.org/forum/u/mirko-newcastledemo)\
**Post date:** [August 4, 2022, 11:26am UTC](https://www.thethingsnetwork.org/forum/t/msp430-lmic-library-porting/58151/6 "2022-08-04T11:26:55Z")

</div>

Quick update, I managed to write my own library implementation and now I can at least see the massages coming at the Gateway. My problem now is that the messages are not forwarded to the application side (live data on the TTN console is empty). At the moment I’m not listening for any downlink (the code is very simple, I just prepare the Lora packet and send it). Do I need to implement the Rx and Rx2 windows to complete a class A device and be able to see the data on the TTN console?  
Best,  
Mirko

---

<div class="post-metadata">

**Author:** ![kersing](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/kersing/32/44777_2.png) [@kersing](https://www.thethingsnetwork.org/forum/u/kersing)\
**Post date:** [August 4, 2022, 1:50pm UTC](https://www.thethingsnetwork.org/forum/t/msp430-lmic-library-porting/58151/7 "2022-08-04T13:50:46Z")

</div>

> [@mirko-newcastledemo](#):
>
> Do I need to implement the Rx and Rx2 windows to complete a class A device

You need to implement the receive windows as well as the MAC commands for a device to work correctly with TTN (v3). Anything less will result in semi DoS for the gateway(s) that receive your uplinks.  
Have you looked at Loramac-node in stead of LMIC to port to your controller?
