TX comes out with distortions possibly due to spi transfer buffer overflow/underflows on PIC.
TX comes out with distortions possibly due to spi transfer buffer overflow/underflows on PIC.

--- a/src/BLE_TNC_ADC_DAC.X/.gitignore
+++ b/src/BLE_TNC_ADC_DAC.X/.gitignore
@@ -1,3 +1,4 @@
 dist
 bin
+build
 

--- a/src/BLE_TNC_ADC_DAC.X/funclist
+++ b/src/BLE_TNC_ADC_DAC.X/funclist
@@ -1,4 +1,4 @@
-_main: CODE, 270 0 128
-__initialization: CODE, 251 0 15
-_isr: CODE, 4 0 245
-Total: 388
+_main: CODE, 261 0 128
+__initialization: CODE, 242 0 15
+_isr: CODE, 4 0 236
+Total: 379

 Binary files a/src/BLE_TNC_ADC_DAC.X/l.obj and b/src/BLE_TNC_ADC_DAC.X/l.obj differ
--- a/src/BLE_TNC_ADC_DAC.X/main.c
+++ b/src/BLE_TNC_ADC_DAC.X/main.c
@@ -181,8 +181,10 @@
             if(!dac_started);
             else if(data_buffer_read_pos == data_buffer_write_pos){
 
+                // STALL DAC FOR TESTING
+                
                 overflow = true;
-
+                /*
                 mode = ADC_MODE;
 
                 // Stop DAC
@@ -195,6 +197,7 @@
                 // Reset buffer
                 data_buffer_write_pos = 0;
                 data_buffer_read_pos = 0;
+                */
 
             } else {
 

--- a/src/BLE_TNC_ADC_DAC.X/nbproject/Makefile-default.mk
+++ b/src/BLE_TNC_ADC_DAC.X/nbproject/Makefile-default.mk
@@ -72,6 +72,9 @@
 FIXDEPS=fixDeps
 
 .build-conf:  ${BUILD_SUBPROJECTS}
+ifneq ($(INFORMATION_MESSAGE), )
+	@echo $(INFORMATION_MESSAGE)
+endif
 	${MAKE}  -f nbproject/Makefile-default.mk dist/${CND_CONF}/${IMAGE_TYPE}/BLE_TNC_ADC_DAC.X.${IMAGE_TYPE}.${OUTPUT_SUFFIX}
 
 MP_PROCESSOR_OPTION=16LF1704
@@ -79,7 +82,7 @@
 # Rules for buildStep: compile
 ifeq ($(TYPE_IMAGE), DEBUG_RUN)
 ${OBJECTDIR}/main.p1: main.c  nbproject/Makefile-${CND_CONF}.mk
-	@${MKDIR} ${OBJECTDIR} 
+	@${MKDIR} "${OBJECTDIR}" 
 	@${RM} ${OBJECTDIR}/main.p1.d 
 	@${RM} ${OBJECTDIR}/main.p1 
 	${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G  -D__DEBUG=1 --debugger=pickit3  --double=24 --float=24 --opt=default,+asm,+asmfile,-speed,+space,-debug --addrqual=ignore --mode=free -P -N255 --warn=0 --asmlist --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,+osccal,-resetbits,-download,-stackcall,+clib --output=-mcof,+elf:multilocs --stack=compiled:auto:auto "--errformat=%f:%l: error: (%n) %s" "--warnformat=%f:%l: warning: (%n) %s" "--msgformat=%f:%l: advisory: (%n) %s"    -o${OBJECTDIR}/main.p1  main.c 
@@ -88,7 +91,7 @@
 	
 else
 ${OBJECTDIR}/main.p1: main.c  nbproject/Makefile-${CND_CONF}.mk
-	@${MKDIR} ${OBJECTDIR} 
+	@${MKDIR} "${OBJECTDIR}" 
 	@${RM} ${OBJECTDIR}/main.p1.d 
 	@${RM} ${OBJECTDIR}/main.p1 
 	${MP_CC} --pass1 $(MP_EXTRA_CC_PRE) --chip=$(MP_PROCESSOR_OPTION) -Q -G  --double=24 --float=24 --opt=default,+asm,+asmfile,-speed,+space,-debug --addrqual=ignore --mode=free -P -N255 --warn=0 --asmlist --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,+osccal,-resetbits,-download,-stackcall,+clib --output=-mcof,+elf:multilocs --stack=compiled:auto:auto "--errformat=%f:%l: error: (%n) %s" "--warnformat=%f:%l: warning: (%n) %s" "--msgformat=%f:%l: advisory: (%n) %s"    -o${OBJECTDIR}/main.p1  main.c 

--- a/src/BLE_TNC_ADC_DAC.X/nbproject/Makefile-genesis.properties
+++ b/src/BLE_TNC_ADC_DAC.X/nbproject/Makefile-genesis.properties
@@ -1,9 +1,9 @@
 #
-#Mon Aug 25 20:13:09 MDT 2014
-default.languagetoolchain.dir=/opt/microchip/xc8/v1.32/bin
-com-microchip-mplab-nbide-embedded-makeproject-MakeProject.md5=1f98a0eed69cb2a45c12981fa9470927
-default.languagetoolchain.version=1.32
+#Wed Mar 25 13:20:35 MDT 2015
+default.languagetoolchain.dir=/home/microchip/xc8/v1.33/bin
+com-microchip-mplab-nbide-embedded-makeproject-MakeProject.md5=6c4dcf44e1ed7eded46cf7c7ad3c6db0
+default.languagetoolchain.version=1.33
 host.platform=linux
 conf.ids=default
-default.com-microchip-mplab-nbide-toolchainXC8-XC8LanguageToolchain.md5=52258db7536b2d1fec300cefc7ed9230
+default.com-microchip-mplab-nbide-toolchainXC8-XC8LanguageToolchain.md5=3dd5e13579ba684c0895d122f577e24e
 

--- a/src/BLE_TNC_ADC_DAC.X/nbproject/Makefile-local-default.mk
+++ b/src/BLE_TNC_ADC_DAC.X/nbproject/Makefile-local-default.mk
@@ -14,23 +14,23 @@
 # You can invoke make with the values of the macros:
 # $ makeMP_CC="/opt/microchip/mplabc30/v3.30c/bin/pic30-gcc" ...  
 #
-PATH_TO_IDE_BIN=/opt/microchip/mplabx/mplab_ide/mplab_ide/modules/../../bin/
+PATH_TO_IDE_BIN=/home/microchip/mplabx/mplab_ide/mplab_ide/modules/../../bin/
 # Adding MPLAB X bin directory to path.
-PATH:=/opt/microchip/mplabx/mplab_ide/mplab_ide/modules/../../bin/:$(PATH)
+PATH:=/home/microchip/mplabx/mplab_ide/mplab_ide/modules/../../bin/:$(PATH)
 # Path to java used to run MPLAB X when this makefile was created
-MP_JAVA_PATH="/opt/microchip/mplabx/sys/java/jre1.7.0_25/bin/"
+MP_JAVA_PATH="/home/microchip/mplabx/sys/java/jre1.7.0_67/bin/"
 OS_CURRENT="$(shell uname -s)"
-MP_CC="/opt/microchip/xc8/v1.32/bin/xc8"
+MP_CC="/home/microchip/xc8/v1.33/bin/xc8"
 # MP_CPPC is not defined
 # MP_BC is not defined
-# MP_AS is not defined
+MP_AS="/home/microchip/xc8/v1.33/bin/xc8"
 # MP_LD is not defined
 # MP_AR is not defined
-DEP_GEN=${MP_JAVA_PATH}java -jar "/opt/microchip/mplabx/mplab_ide/mplab_ide/modules/../../bin/extractobjectdependencies.jar" 
-MP_CC_DIR="/opt/microchip/xc8/v1.32/bin"
+DEP_GEN=${MP_JAVA_PATH}java -jar "/home/microchip/mplabx/mplab_ide/mplab_ide/modules/../../bin/extractobjectdependencies.jar"
+MP_CC_DIR="/home/microchip/xc8/v1.33/bin"
 # MP_CPPC_DIR is not defined
 # MP_BC_DIR is not defined
-# MP_AS_DIR is not defined
+MP_AS_DIR="/home/microchip/xc8/v1.33/bin"
 # MP_LD_DIR is not defined
 # MP_AR_DIR is not defined
 # MP_BC_DIR is not defined

--- a/src/BLE_TNC_ADC_DAC.X/nbproject/configurations.xml
+++ b/src/BLE_TNC_ADC_DAC.X/nbproject/configurations.xml
@@ -30,7 +30,7 @@
         <targetPluginBoard></targetPluginBoard>
         <platformTool>PICkit3PlatformTool</platformTool>
         <languageToolchain>XC8</languageToolchain>
-        <languageToolchainVersion>1.32</languageToolchainVersion>
+        <languageToolchainVersion>1.33</languageToolchainVersion>
         <platform>2</platform>
       </toolsSet>
       <compileType>
@@ -38,8 +38,11 @@
           <linkerLibItems>
           </linkerLibItems>
         </linkerTool>
+        <archiverTool>
+        </archiverTool>
         <loading>
           <useAlternateLoadableFile>false</useAlternateLoadableFile>
+          <parseOnProdLoad>false</parseOnProdLoad>
           <alternateLoadableFile></alternateLoadableFile>
         </loading>
       </compileType>
@@ -63,6 +66,8 @@
         <property key="optimization-assembler-files" value="true"/>
         <property key="optimization-debug" value="false"/>
         <property key="optimization-global" value="true"/>
+        <property key="optimization-invariant-enable" value="false"/>
+        <property key="optimization-invariant-value" value="16"/>
         <property key="optimization-level" value="9"/>
         <property key="optimization-set" value="default"/>
         <property key="optimization-speed" value="false"/>
@@ -125,21 +130,32 @@
         <property key="memories.aux" value="false"/>
         <property key="memories.bootflash" value="true"/>
         <property key="memories.configurationmemory" value="true"/>
+        <property key="memories.configurationmemory2" value="true"/>
+        <property key="memories.dataflash" value="true"/>
         <property key="memories.eeprom" value="true"/>
         <property key="memories.flashdata" value="true"/>
         <property key="memories.id" value="true"/>
         <property key="memories.programmemory" value="true"/>
         <property key="memories.programmemory.end" value="0xfff"/>
+        <property key="memories.programmemory.partition2" value="true"/>
+        <property key="memories.programmemory.partition2.end"
+                  value="${memories.programmemory.partition2.end.value}"/>
+        <property key="memories.programmemory.partition2.start"
+                  value="${memories.programmemory.partition2.start.value}"/>
         <property key="memories.programmemory.start" value="0x0"/>
-        <property key="poweroptions.powerenable" value="true"/>
+        <property key="poweroptions.powerenable" value="false"/>
         <property key="programmertogo.imagename" value=""/>
+        <property key="programoptions.donoteraseauxmem" value="false"/>
         <property key="programoptions.eraseb4program" value="true"/>
         <property key="programoptions.pgmspeed" value="2"/>
+        <property key="programoptions.preservedataflash" value="false"/>
         <property key="programoptions.preserveeeprom" value="false"/>
         <property key="programoptions.preserveprogramrange" value="false"/>
         <property key="programoptions.preserveprogramrange.end" value="0xfff"/>
         <property key="programoptions.preserveprogramrange.start" value="0x0"/>
         <property key="programoptions.preserveuserid" value="false"/>
+        <property key="programoptions.programcalmem" value="false"/>
+        <property key="programoptions.programuserotp" value="false"/>
         <property key="programoptions.testmodeentrymethod" value="VPPFirst"/>
         <property key="programoptions.usehighvoltageonmclr" value="false"/>
         <property key="programoptions.uselvpprogramming" value="false"/>

--- /dev/null
+++ b/src/BLE_TNC_ADC_DAC.X/nbproject/private/SuppressibleMessageMemo.properties
@@ -1,1 +1,4 @@
+#
+#Thu Mar 26 10:03:00 MDT 2015
+pk3/CHECK_4_HIGH_VOLTAGE_VPP=true
 

--- a/src/BLE_TNC_ADC_DAC.X/nbproject/private/configurations.xml
+++ b/src/BLE_TNC_ADC_DAC.X/nbproject/private/configurations.xml
@@ -4,8 +4,8 @@
   <defaultConf>0</defaultConf>
   <confs>
     <conf name="default" type="2">
-      <platformToolSN>:=MPLABComm-USB-Microchip:=&lt;vid>04D8:=&lt;pid>900A:=&lt;rev>0002:=&lt;man>Microchip Technology Inc.:=&lt;prod>PICkit 3:=&lt;sn>BUR114180848:=&lt;drv>x:=end</platformToolSN>
-      <languageToolchainDir>/opt/microchip/xc8/v1.32/bin</languageToolchainDir>
+      <platformToolSN>:=MPLABComm-USB-Microchip:=&lt;vid>04D8:=&lt;pid>900A:=&lt;rev>0002:=&lt;man>Microchip Technology Inc.:=&lt;prod>PICkit 3:=&lt;sn>BUR114180848:=&lt;drv>x:=&lt;xpt>h:=end</platformToolSN>
+      <languageToolchainDir>/home/microchip/xc8/v1.33/bin</languageToolchainDir>
       <mdbdebugger version="1">
         <placeholder1>place holder 1</placeholder1>
         <placeholder2>place holder 2</placeholder2>

--- a/src/RFDuino_TNC/AFSK_Modulator.c
+++ b/src/RFDuino_TNC/AFSK_Modulator.c
@@ -21,7 +21,7 @@
   self.frequency_0 = frequency_0;
   self.frequency_1 = frequency_1;
 
-  self.current_frequency = frequency_0;
+  self.current_frequency = 0;
 
   self.len = len;
   memcpy(self.data, data, len);
@@ -29,6 +29,9 @@
   self.same_count = 0;
   self.last_bit = 0;
   self.sample_counter = 0;
+
+  self.freq_0_phi_adjust = 256 * frequency_0 / MODULATOR_SAMPLE_RATE;
+  self.freq_1_phi_adjust = 256 * frequency_1 / MODULATOR_SAMPLE_RATE;
 
 }
 
@@ -42,17 +45,20 @@
 
 static inline void AFSK_Modulator_change_frequency(){
 
-  if(self.current_frequency == self.frequency_0)
-    self.current_frequency = self.frequency_1;
+  if(self.current_frequency == 0)
+    self.current_frequency = 1;
 
   else
-    self.current_frequency = self.frequency_0;
+    self.current_frequency = 0;
 
 }
 
-static int8_t AFSK_Modulator_next(const uint16_t freq){
+static int8_t AFSK_Modulator_next(const uint8_t freq){
 
-  self.phi += 256 * freq / MODULATOR_SAMPLE_RATE;
+  if(freq == 0)
+    self.phi += self.freq_0_phi_adjust;
+  else
+    self.phi += self.freq_1_phi_adjust;
   self.sample_counter++;
 
   return sin_table[self.phi];

--- a/src/RFDuino_TNC/AFSK_Modulator.h
+++ b/src/RFDuino_TNC/AFSK_Modulator.h
@@ -27,12 +27,15 @@
   uint16_t frequency_0;
   uint16_t frequency_1;
 
-  uint16_t current_frequency;
+  uint8_t current_frequency;
 
   uint8_t same_count;
   uint16_t last_bit;
   uint32_t sample_counter;
   uint8_t phi;
+
+  uint8_t freq_0_phi_adjust;
+  uint8_t freq_1_phi_adjust;
 
 } AFSK_Modulator;
 

--- a/src/RFDuino_TNC/RFDuino_TNC.ino
+++ b/src/RFDuino_TNC/RFDuino_TNC.ino
@@ -135,6 +135,8 @@
   ADC_Setup();
   pinMode(BATTERY_MEASURE_PIN, INPUT);
   pinMode(6, OUTPUT);
+
+  //AFSK_Modulator_init(ARISS_data, ARISS_data_len, 1200, 1200, 2200, 120, 60);
 
 }
 
@@ -194,8 +196,21 @@
 
 }
 
+// Persistent sample so we can run sample calculation during SPI transfer
+//uint16_t sample;
+
 void loop() {
-
+/*
+  // Code for testing sample caluculation time
+  NRF_GPIO->OUTSET = (1 << 6);
+  uint16_t sample = AFSK_Modulator_next_sample();
+  NRF_GPIO->OUTCLR = (1 << 6);
+
+  if(sample == 0x100)
+    AFSK_Modulator_init(ARISS_data, ARISS_data_len, 1200, 1200, 2200, 120, 60);
+
+  return;
+*/
   // Start SPI request for ADC Sample
   if (spi_wait_flag) {
 
@@ -214,14 +229,20 @@
 
       // if starting send 0x7E to switch to DAC mode
       if (stage == TX_STAGE_1) {
+
         NRF_SPI0->TXD = 0x7E;
         stage = TX_STAGE_2;
+        // While TXing byte to switch to DAC mode
+        // Start calculating first sample
+        //sample = AFSK_Modulator_next_sample();
+
       }
       // else send next tx byte from modulator
       else if (stage == TX_STAGE_2) {
 
-        uint16_t sample = AFSK_Modulator_next_sample();
-
+        int16_t sample = AFSK_Modulator_next_sample();
+
+        // process previously calculated sample
         // if modulator 0x100 recived finished sending wait for tx buffer to empty
         if (sample == AFSK_MODULATOR_PACKET_FINISHED) {
           NRF_SPI0->TXD = 0x7E; // NULL data character
@@ -234,6 +255,9 @@
             NRF_SPI0->TXD = 0x7D;
           else
             NRF_SPI0->TXD = (uint8_t)(sample & 0xFF);
+
+          // start calculating next sample during SPI Xfer
+          //sample = AFSK_Modulator_next_sample();
 
         }
       }