football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Revision:
13:28332f65d14b
Parent:
12:6d313d575f84
Child:
14:76fb883a3cb8
--- a/main.cpp	Tue Jun 16 18:16:49 2015 +0000
+++ b/main.cpp	Sun Jun 21 08:44:24 2015 +0000
@@ -5,6 +5,15 @@
  *   such that what the nRF generates is sent out the serial port,
  *   and what comes in the serial port goes into the nRF.
  *   Maybe could use the now-unused CTS pin for that.
+ *
+ *   Using
+ *     rev 327 of BLE_API  
+ *     rev 102 of nRF51822   w/ modification--See below
+ *     rev 493 of mbed-src   w/ modification to targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/serial_api.c
+ *     so things compile properly.
+ *
+ *   Changed
+ *     nRF51822/nordic/pstorage_platform.h  PSTORAGE_MIN_BLOCK_SIZE  changed from 0x010 to 4.
  */
 
 /* mbed Microcontroller Library
@@ -45,10 +54,12 @@
 #define DEBUG(...) /* nothing */
 #endif /* #if NEED_CONSOLE_OUTPUT */
 
-BLEDevice  ble;
-
 extern "C"
 {
+#include "softdevice_handler.h"  // Attempt to get pstorage enqueued cmd callbacks.
+#include "app_timer.h"
+#include "pstorage.h"
+
     void My_UART0_IRQHandler();
 
     void pin_mode( PinName, PinMode );
@@ -66,6 +77,10 @@
 // Wait to settle.
 int foo = (wait( 0.1 ), 0);
 
+// App timer, app scheduler, and pstorage are already setup by bootloader.
+
+BLEDevice  ble;
+
 
 // Note:  From the datasheet:
 //  PSELRXD, PSELRTS, PSELTRTS and PSELTXD must only be configured when the UART is disabled.
@@ -160,13 +175,6 @@
 {
 }
 
-void periodicCallback( void )
-{
-    led0 = !led0;
-    led1 = !led1;
-//    rts  = !rts;
-}
-
 void toPhoneChk( void )
 {
 //    if( 0 != rts.read() )  pcfc.puts( "\r\n!RTS disengaged.\r\n" );  // When not using HWFC.
@@ -192,6 +200,133 @@
     }
 }
 
+
+static uint32_t boot_cnt_data = 0;
+static uint16_t data_block    = 9*20 +75;   // Last block -- Use for app-start count.
+static uint16_t data_size     = 4;
+static uint16_t data_offset   = 0;          // 12 for 16-byte blocks.
+static bool     data_loaded   = false;
+static bool     data_cleared  = false;
+static bool     data_stored   = false;
+static pstorage_handle_t pstorage_id;
+static uint32_t pstorage_read_test()
+{
+    uint32_t err_code;
+
+    pstorage_handle_t p_block_id;
+
+    do
+    {
+        err_code = pstorage_block_identifier_get( &pstorage_id, data_block, &p_block_id );
+        if( NRF_SUCCESS != err_code )  break;
+
+        err_code = pstorage_load( (uint8_t *)&boot_cnt_data, &p_block_id, data_size, data_offset );
+        if( NRF_SUCCESS != err_code )  break;
+
+    } while( 0 );
+
+    return  err_code;
+}
+static uint32_t pstorage_clear_test()
+{
+    uint32_t err_code;
+
+    pstorage_handle_t p_block_id;
+
+    do
+    {
+        err_code = pstorage_block_identifier_get( &pstorage_id, data_block, &p_block_id );
+        if( NRF_SUCCESS != err_code )  break;
+
+        err_code = pstorage_clear( &p_block_id, PSTORAGE_MIN_BLOCK_SIZE );
+        if( NRF_SUCCESS != err_code )  break;
+
+    } while( 0 );
+
+    return  err_code;
+}
+static uint32_t pstorage_write_test()
+{
+    uint32_t err_code;
+
+    pstorage_handle_t p_block_id;
+
+    do
+    {
+        err_code = pstorage_block_identifier_get( &pstorage_id, data_block, &p_block_id );
+        if( NRF_SUCCESS != err_code )  break;
+
+        err_code = pstorage_store( &p_block_id, (uint8_t *)&boot_cnt_data, data_size, data_offset );
+
+    } while( 0 );
+
+    return  err_code;
+}
+static void pstorage_cb_handler( pstorage_handle_t *handle, uint8_t op_code, uint32_t result, uint8_t *p_data, uint32_t data_len )
+{
+    switch( op_code )
+    {
+      case PSTORAGE_LOAD_OP_CODE:
+        if( NRF_SUCCESS == result )
+        {
+            // Load operation successful.
+            data_loaded = true;  // Flag to signal load is done.
+
+        } else
+          {
+              // Load operation failed.
+              pcfc.printf( "\r\nWarn: pstorage load operation error: %x\r\n", result );
+          }
+        break;       
+      case PSTORAGE_UPDATE_OP_CODE:
+      case PSTORAGE_STORE_OP_CODE:
+        if( NRF_SUCCESS == result )
+        {
+            // Store operation successful.
+            data_stored = true;  // Flag to signal store is done.
+
+        } else
+          {
+              // Store operation failed.
+              pcfc.printf( "\r\nWarn: pstorage store operation error: %x\r\n", result );
+          }
+        // Source memory can now be reused or freed.
+        break;
+      case PSTORAGE_CLEAR_OP_CODE:
+        if( NRF_SUCCESS == result )
+        {
+            // Clear operation successful.
+            data_cleared = true;  // Flag to store to the same data area.
+
+        } else
+          {
+              // Clear operation failed.
+              pcfc.printf( "\r\nWarn: pstorage clear operation error: %x\r\n", result );
+          }
+        break;
+      default:
+        pcfc.printf( "\r\nWarn: pstorage unknown op: %x  error: %x\r\n", op_code, result );
+    }
+}
+static uint32_t pstorage_setup()
+{
+    pstorage_module_param_t pstorage_param;    
+
+    // Setup pstorage with 9*20 +76 blocks of 4 bytes each.  (or 9*20/4 + 19  for 16 byte blocks)
+    pstorage_param.block_size  = 4;         // Recommended to be >= 4 bytes.
+    pstorage_param.block_count = 9*20 +76;  // 9 Sequences x 20 Stations  + 76 blocks to fill out to 1k.
+    pstorage_param.cb          = pstorage_cb_handler;
+
+    return  pstorage_register( &pstorage_param, &pstorage_id );
+}
+
+void periodicCallback( void )
+{
+    led0 = !led0;
+    led1 = !led1;
+//    rts  = !rts;
+}
+
 /*
 void led_thread( void const *args )
 {
@@ -204,6 +339,7 @@
 }
 */
 
+
 int main( void )
 {
     // NVIC_SetVector( UART0_IRQn, (uint32_t)My_UART0_IRQHandler );  // TODO maybe try before instantiating pcfc.
@@ -228,6 +364,7 @@
                                       GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
     ble.setAdvertisingType( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED );
 
+
     // Get MAC addr so we can create a device name using it.
     ble.getAddress( pAdType, macAddr );
     sprintf( deviceName, "T%02X%02X", macAddr[1], macAddr[0] );
@@ -237,6 +374,28 @@
     pcfc.printf( "\r\nIn BLE Loopback mode.\r\n" );
 #endif
 
+    uint32_t p_count;
+    uint32_t pstorageErr = pstorage_init();  // This needs to be called, even though apparently called in bootloader--Check other stuff.
+    pstorage_access_status_get( &p_count );
+//    pcfc.printf( "\r\nInitial pstorage count: %d\r\n", p_count );
+
+    if( NRF_SUCCESS != pstorageErr )  pcfc.printf( "\r\nWarn: pstorage init error: %x\r\n", pstorageErr );
+      else
+      {
+          pstorageErr = pstorage_setup();
+          if( NRF_SUCCESS != pstorageErr )  pcfc.printf( "\r\nWarn: pstorage setup error: %x\r\n", pstorageErr );
+            else
+            {                
+                pstorageErr = pstorage_read_test();
+                if( NRF_SUCCESS != pstorageErr )  pcfc.printf( "\r\nWarn: pstorage read attempt error: %x\r\n", pstorageErr );
+                  else
+                  {
+                      // In this test setup, we use the load callback to signal start to clear,
+                      //  then we use the clear callback to signal start to write.
+                  }
+            }
+      }
+
     ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME,
                                       (const uint8_t *)deviceName, strlen(deviceName) );
     ble.accumulateAdvertisingPayload( GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS,
@@ -277,6 +436,38 @@
         while( 0 <= phone.getchar() );  // Eat input.
 
 //        if( !(loop % 50) )  phone.printf( "Post: %d\r\n", tmr.read_ms() );
+
+        app_sched_execute();  // Attempt to get pstorage enqueued cmd callbacks.
+
+        // Update persistent storage if ready...
+        if( data_loaded )
+        {
+            data_loaded = false;
+
+            boot_cnt_data++;
+            pcfc.printf( "\r\nApp boot #%d\r\n", boot_cnt_data );
+
+            pstorageErr = pstorage_clear_test();
+//            pcfc.printf( "pstorage clear command sent.\r\n" );
+            if( NRF_SUCCESS != pstorageErr )  pcfc.printf( "\r\nWarn: pstorage clear attempt error: %x\r\n", pstorageErr );
+ 
+        } else if( data_cleared )
+          {
+              data_cleared  = false;
+
+//              pcfc.printf( "\r\npstorage clear operation successful.\r\n" );
+
+              pstorageErr = pstorage_write_test();
+//              pcfc.printf( "pstorage write command sent.\r\n" );
+              if( NRF_SUCCESS != pstorageErr )  pcfc.printf( "\r\nWarn: pstorage write attempt error: %x\r\n", pstorageErr );
+
+          } else if( data_stored )
+            {
+                data_stored = false;
+
+//                pcfc.printf( "\r\npstorage store operation successful.\r\n" );
+            }
+
     }
 }