1 // *********************************************************************
    2 //   DPA Autoexec example                                              *
    3 // *********************************************************************
    4 // Copyright (c) IQRF Tech s.r.o.
    5 //
    6 // File:    $RCSfile: DpaAutoexec.c,v $
    7 // Version: $Revision: 1.18 $
    8 // Date:    $Date: 2018/05/28 09:53:23 $
    9 //
   10 // Revision history:
   11 //   2017/03/13  Release for DPA 3.00
   12 //   2015/08/05  Release for DPA 2.20
   13 //   2014/04/30  Release for DPA 2.10
   14 //
   15 // *********************************************************************
   16 
   17 // Online DPA documentation http://www.iqrf.org/DpaTechGuide/
   18 
   19 // Before uploading this example make sure:
   20 // 1. Autoexec is enabled at HWP Configuration
   21 // 2. Used peripherals are enabled at HWP Configuration
   22 // 3. External EEPROM upload is selected at IQRF IDE. Please note external EEPROM cannot be uploaded using RFPGM.
   23 
   24 // ! Important: 
   25 // Updating Custom DPA Handler code using OTA LoadCode command does not allow writing external EEPROM content. 
   26 // Therefore the update of the Autoexec is not possible. It is recommended to avoid Autoexec when OTA is used.
   27 
   28 // Default IQRF include (modify the path according to your setup)
   29 #include "IQRF.h"
   30 
   31 // Do not check for the Custom DPA Handler existence (we only wrote to the external EEPROM)
   32 #define NO_CUSTOM_DPA_HANDLER
   33 
   34 // Uncomment in order to test Example #3 at [C] device (EEEPROM peripheral space starts at different address)
   35 //#define COORDINATOR_CUSTOM_HANDLER
   36 
   37 // Default DPA header (modify the path according to your setup)
   38 #include "DPA.h"
   39 // Default Custom DPA Handler header (modify the path according to your setup)
   40 #include "DPAcustomHandler.h"
   41 
   42 //############################################################################################
   43 
   44 // Choose example number 1-4
   45 #define EXAMPLE 1
   46 
   47 // -------------------------------------------------------------------------------------------
   48 #if EXAMPLE == 1
   49 // Example #1: Switch on both LEDs
   50 #pragma cdata[ __EEESTART + AUTOEXEC_EEEPROM_ADDR ] = \
   51 /* 1. Green LED ON */ \
   52 5, PNUM_LEDG, CMD_LED_SET_ON, 0xff, 0xff, \
   53 /* 2. Red LED ON */ \
   54 5, PNUM_LEDR, CMD_LED_SET_ON, 0xff, 0xff, \
   55 /* 3. Delay 1s (LEDs will be visible at LP mode) */ \
   56 8, PNUM_IO, CMD_IO_SET, 0xff, 0xff, PNUM_IO_DELAY, 0xe8, 0x03, \
   57 /* 4. End of Autoexec  */ \
   58 0
   59 #endif
   60 
   61 // -------------------------------------------------------------------------------------------
   62 #if EXAMPLE == 2
   63 // Example #2: Open UART @ 9600 baud, write "Hello" to UART
   64 #pragma cdata[ __EEESTART + AUTOEXEC_EEEPROM_ADDR ] = \
   65 /* 1. Open UART at 9600 baud  */ \
   66 6, PNUM_UART, CMD_UART_OPEN, 0xff, 0xff, DpaBaud_9600, \
   67 /* 2. Write "Hello" to UART  */ \
   68 11, PNUM_UART, CMD_UART_WRITE_READ, 0xff, 0xff, /* no read */ 0xff, 'H',  'e',  'l',  'l',  'o', \
   69 /* 3. End of Autoexec  */ \
   70 0
   71 #endif
   72 
   73 // -------------------------------------------------------------------------------------------
   74 #if EXAMPLE == 3
   75 // Example #3: Self modifying Autoexec
   76 #pragma cdata[ __EEESTART + AUTOEXEC_EEEPROM_ADDR ] = \
   77 /* 1. Red LED ON, after the 1st boot it will be self-modified to the Green LED ON + end of Autoexec  */ \
   78 5, PNUM_LEDR, CMD_LED_SET_ON, 0xff, 0xff, \
   79 /* 2. Delay 1s (red LED will be visible at LP mode) */ \
   80 8, PNUM_IO, CMD_IO_SET, 0xff, 0xff, PNUM_IO_DELAY, 0xe8, 0x03, \
   81 /* 3. Write to the EEEPROM a modified Autoexec so next time a Green LED will be ON  */ \
   82 7 + 16, PNUM_EEEPROM, CMD_EEEPROM_XWRITE, 0xff, 0xff, AUTOEXEC_EEEPROM_ADDR & 0xFF, AUTOEXEC_EEEPROM_ADDR >> 8, \
   83     /* new 1. Green LED ON   */ \
   84     5, PNUM_LEDG, CMD_LED_SET_ON, 0xff, 0xff, \
   85     /* new 2. Delay 1s (green LED will be visible at LP mode) */ \
   86     8, PNUM_IO, CMD_IO_SET, 0xff, 0xff, PNUM_IO_DELAY, 0xe8, 0x03, \
   87     /* new 3. End of autoexec  */ \
   88     0, \
   89 /* 4. End of Autoexec  */ \
   90 0
   91 #endif
   92 
   93 // -------------------------------------------------------------------------------------------
   94 #if EXAMPLE == 4
   95 // Example #4: set green LED output pin and set LED on for 1s and then off for 1s, works also at LP mode
   96 #pragma cdata[ __EEESTART + AUTOEXEC_EEEPROM_ADDR ] = \
   97 /* 1. Green LED IO is output */ \
   98 8,  PNUM_IO, CMD_IO_DIRECTION, 0xff, 0xff, /* PORTB.7 = LEDG = Output */ PNUM_IO_TRISB, 0x80, 0x00, \
   99 /* 2. Green LED on for 1s, Green LED off for 1s */ \
  100 17, PNUM_IO, CMD_IO_SET, 0xff, 0xff, \
  101   /* PORTB.7 = 1 => LEDG on */ PNUM_IO_PORTB, 0x80, 0x80, \
  102   /* delay 1s */ PNUM_IO_DELAY, 0xe8, 0x03, \
  103   /* PORTB.7 = 0 => LEDG off */ PNUM_IO_PORTB, 0x80, 0x00, \
  104   /* delay 1s */ PNUM_IO_DELAY, 0xe8, 0x03, \
  105 /* 3. End of Autoexec  */ \
  106 0
  107 #endif
  108 
  109 //############################################################################################