Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

211 lines
6.2 KiB

  1. # Copyright 2021 John-Mark Gurney.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions
  5. # are met:
  6. # 1. Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # 2. Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. #
  12. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  13. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  14. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  15. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  16. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  17. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  18. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  19. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  20. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  21. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  22. # SUCH DAMAGE.
  23. #
  24. ARMOBJDUMP?= arm-none-eabi-objdump
  25. ARMCC?= arm-none-eabi-gcc
  26. ARMTARGET?= -mcpu=cortex-m3 -mthumb -DSTROBE_SINGLE_THREAD=1
  27. # Clang doesn't work due to no-nano libc
  28. #ARMCC?=clang-mp-9.0
  29. #ARMTARGET?= -nostdlib -ffreestanding -target arm-none-eabi -mcpu=cortex-m3 -mfloat-abi=soft -mthumb
  30. PLATFORM != uname -s
  31. .if $(PLATFORM) == "Darwin"
  32. SOEXT=dylib
  33. .else
  34. .error Unsupported platform: $(PLATFORM)
  35. .endif
  36. PROGEXT = .elf
  37. PROGS = lora.gw lora.irr
  38. SRCS.lora.gw = main.c
  39. SRCS.lora.irr = irr_main.c
  40. SRCS+= board.c
  41. SRCS+= misc.c
  42. SRCS+= strobe_rng_init.c
  43. .if 0
  44. SRCS+= rng_save.c
  45. .endif
  46. CFLAGS+= -I$(.CURDIR)
  47. CFLAGS+= -g
  48. #CFLAGS+= -DNDEBUG
  49. # Strobe
  50. .PATH: $(.CURDIR)/strobe
  51. CFLAGS+= -I$(.CURDIR)/strobe
  52. SRCS+= strobe.c \
  53. x25519.c
  54. # LoRamac (SX1276) radio code
  55. LORAMAC_SRC = $(.CURDIR)/loramac/src
  56. .PATH: $(LORAMAC_SRC)/radio/sx1276 $(LORAMAC_SRC)/system $(LORAMAC_SRC)/boards/mcu $(LORAMAC_SRC)/boards/NucleoL152
  57. CFLAGS+= -I$(LORAMAC_SRC)/boards
  58. CFLAGS+= -I$(LORAMAC_SRC)/system
  59. CFLAGS+= -I$(LORAMAC_SRC)/radio
  60. CFLAGS+= -DUSE_HAL_DRIVER -DSX1276MB1LAS
  61. SRCS+= sx1276.c
  62. SRCS+= utilities.c
  63. SRCS+= adc.c timer.c delay.c gpio.c uart.c fifo.c
  64. SRCS+= adc-board.c delay-board.c gpio-board.c rtc-board.c lpm-board.c sx1276mb1las-board.c spi-board.c uart-board.c
  65. # Microcontroller
  66. STM32=$(.CURDIR)/stm32
  67. .PATH: $(STM32)/l151ccux
  68. LINKER_SCRIPT=$(STM32)/l151ccux/STM32L151CCUX_FLASH.ld
  69. SRCS+= \
  70. startup_stm32l151ccux.s \
  71. stm32l1xx_hal.c \
  72. stm32l1xx_hal_adc.c \
  73. stm32l1xx_hal_adc_ex.c \
  74. stm32l1xx_hal_cortex.c \
  75. stm32l1xx_hal_dma.c \
  76. stm32l1xx_hal_flash.c \
  77. stm32l1xx_hal_flash_ex.c \
  78. stm32l1xx_hal_gpio.c \
  79. stm32l1xx_hal_pcd.c \
  80. stm32l1xx_hal_pcd_ex.c \
  81. stm32l1xx_hal_pwr.c \
  82. stm32l1xx_hal_pwr_ex.c \
  83. stm32l1xx_hal_rcc.c \
  84. stm32l1xx_hal_rcc_ex.c \
  85. stm32l1xx_hal_rtc.c \
  86. stm32l1xx_hal_rtc_ex.c \
  87. stm32l1xx_hal_spi.c \
  88. stm32l1xx_hal_uart.c \
  89. system_stm32l1xx.c
  90. SRCS+= \
  91. stm32l1xx_it.c \
  92. stm32l1xx_hal_msp.c
  93. CFLAGS+= -I$(STM32)
  94. CFLAGS+= -I$(STM32)/l151ccux
  95. CFLAGS+= -DSTM32L151xC
  96. # USB
  97. .PATH: $(STM32)/usb
  98. SRCS+= \
  99. stm32l1xx_ll_usb.c \
  100. usb_device.c \
  101. usbd_cdc.c \
  102. usbd_cdc_if.c \
  103. usbd_conf.c \
  104. usbd_core.c \
  105. usbd_ctlreq.c \
  106. usbd_desc.c \
  107. usbd_ioreq.c
  108. CFLAGS+= -I$(STM32)/usb
  109. CFLAGS+= -Werror -Wall
  110. LIBLORA_TEST_SRCS= comms.c strobe.c x25519.c
  111. LIBLORA_TEST_OBJS= $(LIBLORA_TEST_SRCS:C/.c$/.no/)
  112. LIBLORA_TEST = liblora_test.$(SOEXT)
  113. $(LIBLORA_TEST): $(LIBLORA_TEST_OBJS)
  114. $(CC) -shared -o $@ $(.ALLSRC)
  115. .MAIN: all
  116. .PHONY: all
  117. DEPENDS = .arm_deps .test_deps
  118. .PHONY: depend
  119. depend: $(DEPENDS)
  120. .for i in $(DEPENDS)
  121. .sinclude "$i"
  122. .endfor
  123. # Temporarily disabled as OpenOCD can't program EEPROM on the STM32L1.
  124. # This could be used to add the data to flash.
  125. rng_save.c: Makefile
  126. ( echo '#include <strobe_rng_init.h>'; \
  127. echo 'const rng_word_t rng_save[roundup(32, sizeof(rng_word_t)) / sizeof(rng_word_t)] __attribute__ ((section (".eeprom"))) = {'; \
  128. dd if=/dev/random bs=32 count=1 | hexdump -e '4/4 " %3u," "\n"'; \
  129. echo '};' \
  130. ) > $@ || (rm $@ && false)
  131. .arm_deps:
  132. $(ARMCC) $(ARMTARGET) $(CFLAGS) $(.ALLSRC) -MM > $@ || (rm -f $@ && false)
  133. .test_deps: $(LIBLORA_TEST_SRCS)
  134. $(CC) $(CFLAGS) $(.ALLSRC) -MM | sed -e 's/\.o:/\.no:/' > $@ || (rm -f $@ && false)
  135. .for i in $(PROGS)
  136. ALLTGTS+= $(i)$(PROGEXT) $(i).list
  137. ASRCS.$(i) = $(SRCS) $(SRCS.$(i))
  138. OBJS.$(i) = $(ASRCS.$(i):C/.c$/.o/)
  139. .arm_deps: $(ASRCS.$(i))
  140. $(i)$(PROGEXT) $(i).map: $(OBJS.$(i))
  141. $(ARMCC) $(ARMTARGET) -o $(i)$(PROGEXT) $(.ALLSRC) -T$(LINKER_SCRIPT) --specs=nosys.specs -Wl,-Map="$(i).map" -Wl,--gc-sections -static --specs=nano.specs -Wl,--start-group -lc -lm -Wl,--end-group
  142. $(i).list: $(i)$(PROGEXT)
  143. $(ARMOBJDUMP) -h -S $(.ALLSRC) > $@ || (rm -f $@ && false)
  144. .endfor
  145. all: $(ALLTGTS)
  146. .PHONY: runbuild
  147. runbuild: $(SRCS)
  148. for i in $(.MAKEFILE_LIST) $(.ALLSRC) $$(cat $(DEPENDS) | gsed ':x; /\\$$/ { N; s/\\\n//; tx }' | sed -e 's/^[^:]*://'); do if [ "$$i" != ".." ]; then echo $$i; fi; done | entr -d sh -c 'echo starting...; cd $(.CURDIR) && $(MAKE) $(.MAKEFLAGS) depend && $(MAKE) $(.MAKEFLAGS) all'
  149. .PHONY: runtests
  150. runtests: Makefile lora_comms.py lora.py loraserv.py multicast.py $(LIBLORA_TEST) $(LIBLORA_TEST_SRCS)
  151. ls $(.ALLSRC) | entr sh -c '(cd $(.CURDIR) && $(MAKE) $(.MAKEFLAGS) $(LIBLORA_TEST)) && ((PYTHONPATH="$(.CURDIR)" python -m coverage run -m unittest lora multicast loraserv && coverage report --omit=$(.CURDIR)/p/\* -m -i) 2>&1 | head -n 30)'
  152. # native objects
  153. .SUFFIXES: .no
  154. .c.no:
  155. $(CC) $(CFLAGS) -c $< -o $@
  156. .c.o:
  157. $(ARMCC) $(ARMTARGET) $(CFLAGS) -c $< -o $@
  158. STROBE_NAME = strobe
  159. STROBE_REPO = https://git.code.sf.net/p/strobe/code
  160. STROBE_BRANCH = master
  161. LORAMAC_NAME = loramac
  162. LORAMAC_REPO = https://github.com/Lora-net/LoRaMac-node.git
  163. LORAMAC_BRANCH = master
  164. .for module in STROBE LORAMAC
  165. .PHONY: init-$($(module)_NAME)
  166. init-$($(module)_NAME):
  167. git subtree add -P $($(module)_NAME) --squash $($(module)_REPO) $($(module)_BRANCH)
  168. .PHONY: update-$($(module)_NAME)
  169. update-$($(module)_NAME):
  170. git subtree pull -P $($(module)_NAME) --squash $($(module)_REPO) $($(module)_BRANCH)
  171. .endfor