test

Dependencies:   BMI160 max32630hsp3 MemoryLCD USBDevice

Committer:
seyhmus.cacina
Date:
Mon Mar 18 10:21:53 2019 +0300
Revision:
0:ac4dea3e2894
ME11B Sample Code First Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
seyhmus.cacina 0:ac4dea3e2894 1 /*
seyhmus.cacina 0:ac4dea3e2894 2 * SHcomm.cpp
seyhmus.cacina 0:ac4dea3e2894 3 *
seyhmus.cacina 0:ac4dea3e2894 4 * Created on: Nov 16, 2018
seyhmus.cacina 0:ac4dea3e2894 5 * Author: Yagmur.Gok
seyhmus.cacina 0:ac4dea3e2894 6 */
seyhmus.cacina 0:ac4dea3e2894 7
seyhmus.cacina 0:ac4dea3e2894 8 /*
seyhmus.cacina 0:ac4dea3e2894 9 #ifdef __cplusplus
seyhmus.cacina 0:ac4dea3e2894 10 extern "C" {
seyhmus.cacina 0:ac4dea3e2894 11 #endif
seyhmus.cacina 0:ac4dea3e2894 12 */
seyhmus.cacina 0:ac4dea3e2894 13
seyhmus.cacina 0:ac4dea3e2894 14 #include <events/mbed_events.h>
seyhmus.cacina 0:ac4dea3e2894 15 #include <mbed.h>
seyhmus.cacina 0:ac4dea3e2894 16 #include "mbed.h"
seyhmus.cacina 0:ac4dea3e2894 17
seyhmus.cacina 0:ac4dea3e2894 18 #include "SHComm.h"
seyhmus.cacina 0:ac4dea3e2894 19
seyhmus.cacina 0:ac4dea3e2894 20
seyhmus.cacina 0:ac4dea3e2894 21 #define SS_I2C_8BIT_SLAVE_ADDR 0xAA
seyhmus.cacina 0:ac4dea3e2894 22 #define SENSORHUB_I2C_ADRESS SS_I2C_8BIT_SLAVE_ADDR
seyhmus.cacina 0:ac4dea3e2894 23
seyhmus.cacina 0:ac4dea3e2894 24 #define ENABLED ((int)(1))
seyhmus.cacina 0:ac4dea3e2894 25 #define DISABLED ((int)(0))
seyhmus.cacina 0:ac4dea3e2894 26
seyhmus.cacina 0:ac4dea3e2894 27 #define SS_DUMP_REG_SLEEP_MS (100)
seyhmus.cacina 0:ac4dea3e2894 28 #define SS_ENABLE_SENSOR_SLEEP_MS (20)
seyhmus.cacina 0:ac4dea3e2894 29 #define SS_DEFAULT_CMD_SLEEP_MS (2)
seyhmus.cacina 0:ac4dea3e2894 30 #define SS_WAIT_BETWEEN_TRIES_MS (2)
seyhmus.cacina 0:ac4dea3e2894 31 #define SS_CMD_WAIT_PULLTRANS_MS (5)
seyhmus.cacina 0:ac4dea3e2894 32 #define SS_FEEDFIFO_CMD_SLEEP_MS (30)
seyhmus.cacina 0:ac4dea3e2894 33
seyhmus.cacina 0:ac4dea3e2894 34
seyhmus.cacina 0:ac4dea3e2894 35 #define SS_DEFAULT_RETRIES ((int) (4))
seyhmus.cacina 0:ac4dea3e2894 36 #define SS_ZERO_DELAY 0
seyhmus.cacina 0:ac4dea3e2894 37 #define SS_ZERO_BYTES 0
seyhmus.cacina 0:ac4dea3e2894 38
seyhmus.cacina 0:ac4dea3e2894 39
seyhmus.cacina 0:ac4dea3e2894 40 /*define sample size of algorithm and raw sensor data in bytes*/
seyhmus.cacina 0:ac4dea3e2894 41 #define SH_ALGO_WHRM_SAMPLE_DATABYTES 4
seyhmus.cacina 0:ac4dea3e2894 42 #define SH_ALGO_SP02_SAMPLE_DATABYTES 4
seyhmus.cacina 0:ac4dea3e2894 43
seyhmus.cacina 0:ac4dea3e2894 44
seyhmus.cacina 0:ac4dea3e2894 45 /*define command sequences given in Maxim ME32664 user manual*/
seyhmus.cacina 0:ac4dea3e2894 46 #define SH_GET_HUB_STATUS_CMDSEQ {0x00,0x00}
seyhmus.cacina 0:ac4dea3e2894 47 #define SH_SET_OPERATING_MODE_CMDSEQ(opMode) {0x01,0x00,opMode}
seyhmus.cacina 0:ac4dea3e2894 48 #define SH_SET_OPERATING_MODE_BOOTLOADER_CMDSEQ {0x02,0x00,0x08}
seyhmus.cacina 0:ac4dea3e2894 49 #define SH_SET_OPERATING_MODE_APPLICATION_CMDSEQ {0x02,0x00,0x00}
seyhmus.cacina 0:ac4dea3e2894 50 #define SH_SET_OPERATING_MODE_RESET_CMDSEQ {0x02,0x00,0x02}
seyhmus.cacina 0:ac4dea3e2894 51 #define SH_GET_OPERATING_MODE_CMDSEQ {0x02,0x00}
seyhmus.cacina 0:ac4dea3e2894 52
seyhmus.cacina 0:ac4dea3e2894 53 #define SH_SET_OUTPUT_MODE_CMDSEQ( outMode) {0x10,0x00, outMode}
seyhmus.cacina 0:ac4dea3e2894 54 #define SH_SET_OUTMODE_NODATA_CMDSEQ {0x10,0x00,0x00}
seyhmus.cacina 0:ac4dea3e2894 55 #define SH_SET_OUTMODE_SENSORDATA_CMDSEQ {0x10,0x00,0x01}
seyhmus.cacina 0:ac4dea3e2894 56 #define SH_SET_OUTMODE_ALGODATA_CMDSEQ {0x10,0x00,0x02}
seyhmus.cacina 0:ac4dea3e2894 57 #define SH_SET_OUTMODE_PAUSE_CMDSEQ {0x10,0x00,0x04}
seyhmus.cacina 0:ac4dea3e2894 58 #define SH_SET_OUTMODE_SENSAMPLECNT_CMDSEQ {0x10,0x00,0x05}
seyhmus.cacina 0:ac4dea3e2894 59 #define SH_SET_OUTMODE_ALGOSAMPLECNT_CMDSEQ {0x10,0x00,0x06}
seyhmus.cacina 0:ac4dea3e2894 60 #define SH_SET_OUTMODE_ALGOSENSAMPLECNT_CMDSEQ {0x10,0x00,0x07}
seyhmus.cacina 0:ac4dea3e2894 61
seyhmus.cacina 0:ac4dea3e2894 62 #define SH_GET_OUTPUT_MODE_CMDSEQ {0x11,0x00}
seyhmus.cacina 0:ac4dea3e2894 63
seyhmus.cacina 0:ac4dea3e2894 64 #define SH_DFIFO_SET_INT_THRESHOLD_CMDSEQ( ucThreshold ) {0x10,0x01,ucThreshold}
seyhmus.cacina 0:ac4dea3e2894 65 #define SH_DFIFO_GET_INT_THRESHOLD_CMDSEQ {0x11,0x01}
seyhmus.cacina 0:ac4dea3e2894 66
seyhmus.cacina 0:ac4dea3e2894 67 #define SH_DFIFO_GET_NSAMPLES_CMDSEQ {0x12,0x00}
seyhmus.cacina 0:ac4dea3e2894 68 #define SH_DFIFO_PULL_SAMPLE_CMDSEQ {0x12,0x01}
seyhmus.cacina 0:ac4dea3e2894 69 #define SH_GET_EXTINPUT_FIFOSZ_CMDSEQ {0x13,0x01}
seyhmus.cacina 0:ac4dea3e2894 70 #define SH_GET_SAMPLEBYTECNT_INPUTFIFO_CMDSEQ {0x13,0x04}
seyhmus.cacina 0:ac4dea3e2894 71 #define SH_FEED_TO_INPUTFIFO_CMDSEQ {0x14,0x00}
seyhmus.cacina 0:ac4dea3e2894 72
seyhmus.cacina 0:ac4dea3e2894 73 #define SH_WRITE_SENSORREG_CMDSEQ( sensorIdx , regAddr ) { 0x40, sensorIdx , regAddr}
seyhmus.cacina 0:ac4dea3e2894 74 #define SH_READ_SENSORREG_CMDSEQ( sensorIdx , regAddr ) { 0x41, sensorIdx , regAddr}
seyhmus.cacina 0:ac4dea3e2894 75 #define SH_READ_AFE_ATTRIBUTES_CMDSEQ(sensorIdx) { 0x42, sensorIdx}
seyhmus.cacina 0:ac4dea3e2894 76 #define SH_READ_ALLREGISTERS_CMDSEQ(sensorIdx) { 0x43, sensorIdx}
seyhmus.cacina 0:ac4dea3e2894 77
seyhmus.cacina 0:ac4dea3e2894 78 #define SH_ENABLE_SENSOR_CMDSEQ(sensorIdx , extMode) {0x44, sensorIdx, 0x01 , extMode }
seyhmus.cacina 0:ac4dea3e2894 79 #define SH_DISABLE_SENSOR_CMDSEQ(sensorIdx) {0x44, sensorIdx, 0x00}
seyhmus.cacina 0:ac4dea3e2894 80
seyhmus.cacina 0:ac4dea3e2894 81
seyhmus.cacina 0:ac4dea3e2894 82 #define SH_AGC_SET_ADCRANGE_CMDSEQ( uiPercentage) {0x50, 0x00, 0x00 , uiPercentage}
seyhmus.cacina 0:ac4dea3e2894 83 #define SH_AGC_SET_STEPSZ_CMDSEQ( uiPercentage) {0x50, 0x00, 0x01 , uiPercentage}
seyhmus.cacina 0:ac4dea3e2894 84 #define SH_AGC_SET_SENSITIVITY_CMDSEQ( uiPercentage) {0x50, 0x00, 0x02 , uiPercentage}
seyhmus.cacina 0:ac4dea3e2894 85 #define SH_AGC_SET_NSAMPLESAVRAGING_CMDSEQ( ucNsamples) {0x50, 0x00, 0x03 , uiNsamples}
seyhmus.cacina 0:ac4dea3e2894 86 #define SH_WHRM_SET_SAMPRATE_CMDSEQ( ucNsamples) {0x50, 0x02, 0x03 , uiNsamples}
seyhmus.cacina 0:ac4dea3e2894 87
seyhmus.cacina 0:ac4dea3e2894 88
seyhmus.cacina 0:ac4dea3e2894 89 #define SH_ENABLE_ALGO_CMDSEQ( algoIdx) { 0x52, algoIdx , 0x01}
seyhmus.cacina 0:ac4dea3e2894 90 #define SH_DISABLE_ALGO_CMDSEQ( algoIdx) { 0x52, algoIdx , 0x00}
seyhmus.cacina 0:ac4dea3e2894 91
seyhmus.cacina 0:ac4dea3e2894 92 #define SH_SET_ALGO_CONFIGURATION_CMDSEQ( algoIdx, cgfIdx) { 0x50 , algoIdx, cgfIdx }
seyhmus.cacina 0:ac4dea3e2894 93 #define SH_GET_ALGO_CONFIGURATION_CMDSEQ( algoIdx, cgfIdx) { 0x51 , algoIdx, cgfIdx }
seyhmus.cacina 0:ac4dea3e2894 94
seyhmus.cacina 0:ac4dea3e2894 95 #define SH_COMM_CHECK_CMDSEQ {0xFF, 0x00}
seyhmus.cacina 0:ac4dea3e2894 96
seyhmus.cacina 0:ac4dea3e2894 97
seyhmus.cacina 0:ac4dea3e2894 98
seyhmus.cacina 0:ac4dea3e2894 99 //phase2 additions
seyhmus.cacina 0:ac4dea3e2894 100 #define SH_CHECKIF_BOOTLDRMODE_CMDSEQ { 0x02, 0x00 }
seyhmus.cacina 0:ac4dea3e2894 101 #define SH_SELFTEST_CMDSEQ(idx) { 0x70, (uint8_t)idx }
seyhmus.cacina 0:ac4dea3e2894 102 #define SH_EXIT_BOOTLDRMODE_CMDSEQ { 0x01, 0x00 }
seyhmus.cacina 0:ac4dea3e2894 103 #define SH_GETLOGSIZE_CMDSEQ { 0x90, 0x01 }
seyhmus.cacina 0:ac4dea3e2894 104 #define SH_READHUBLOGS_CMDSEQ { 0x90, 0x00 }
seyhmus.cacina 0:ac4dea3e2894 105
seyhmus.cacina 0:ac4dea3e2894 106 #define SH_GET_BOOTLDRPAGESIZE_CMDSEQ { 0x81, 0x01 }
seyhmus.cacina 0:ac4dea3e2894 107 #define SH_SET_BOOTLDRPAGECOUNT_CMDSEQ { 0x80, 0x02 }
seyhmus.cacina 0:ac4dea3e2894 108
seyhmus.cacina 0:ac4dea3e2894 109 #define BOOTLOADER_MAX_PAGE_SIZE 8192
seyhmus.cacina 0:ac4dea3e2894 110
seyhmus.cacina 0:ac4dea3e2894 111 /* BOOTLOADER HOST */
seyhmus.cacina 0:ac4dea3e2894 112 #define EBL_CMD_TRIGGER_MODE 0
seyhmus.cacina 0:ac4dea3e2894 113 #define EBL_GPIO_TRIGGER_MODE 1
seyhmus.cacina 0:ac4dea3e2894 114
seyhmus.cacina 0:ac4dea3e2894 115
seyhmus.cacina 0:ac4dea3e2894 116
seyhmus.cacina 0:ac4dea3e2894 117 /*
seyhmus.cacina 0:ac4dea3e2894 118 * define the "platform specific" hardware interface which SSinterface requires:
seyhmus.cacina 0:ac4dea3e2894 119 * 1. master i2c port
seyhmus.cacina 0:ac4dea3e2894 120 * 2. interrupt attachable I/O pin (mfio)
seyhmus.cacina 0:ac4dea3e2894 121 * 3. I/O pin for reset
seyhmus.cacina 0:ac4dea3e2894 122 * Note: Definitions below are for MAX32630FTR Pagasus board . Modify for your platform.
seyhmus.cacina 0:ac4dea3e2894 123 **/
seyhmus.cacina 0:ac4dea3e2894 124
seyhmus.cacina 0:ac4dea3e2894 125 I2C *m_i2cBus; /*i2c bus sensor hub is connected to*/
seyhmus.cacina 0:ac4dea3e2894 126
seyhmus.cacina 0:ac4dea3e2894 127 PinName ss_mfio(P5_4); /* platform specific mfio event pin */
seyhmus.cacina 0:ac4dea3e2894 128 PinName ss_reset(P5_6); /* platform specific sensor hub reset pin */
seyhmus.cacina 0:ac4dea3e2894 129 DigitalInOut mfio_pin(ss_mfio); /* mfio pin mode be I/O */
seyhmus.cacina 0:ac4dea3e2894 130 DigitalInOut reset_pin(ss_reset); /* reset pin mode be I/O */
seyhmus.cacina 0:ac4dea3e2894 131 InterruptIn irq_pin(ss_mfio); /* define mfio pin interrupt attachable*/
seyhmus.cacina 0:ac4dea3e2894 132
seyhmus.cacina 0:ac4dea3e2894 133
seyhmus.cacina 0:ac4dea3e2894 134 /*
seyhmus.cacina 0:ac4dea3e2894 135 * SSI API funcions
seyhmus.cacina 0:ac4dea3e2894 136 * NOTE: Generic functions for any platform.
seyhmus.cacina 0:ac4dea3e2894 137 * exceptions: below needs needs modification according to platform and HAL drivers
seyhmus.cacina 0:ac4dea3e2894 138 * 1. Hard reset function
seyhmus.cacina 0:ac4dea3e2894 139 * 2. Enable/disable mfio event interrput
seyhmus.cacina 0:ac4dea3e2894 140 * 3. mfio pin interrupt routine
seyhmus.cacina 0:ac4dea3e2894 141 *
seyhmus.cacina 0:ac4dea3e2894 142 * **/
seyhmus.cacina 0:ac4dea3e2894 143
seyhmus.cacina 0:ac4dea3e2894 144 /*global buffer for sensor i2c commands+data*/
seyhmus.cacina 0:ac4dea3e2894 145 uint8_t sh_write_buf[512];
seyhmus.cacina 0:ac4dea3e2894 146 static volatile bool m_irq_received_ = false;
seyhmus.cacina 0:ac4dea3e2894 147 static volatile bool mfio_int_happened = false;
seyhmus.cacina 0:ac4dea3e2894 148
seyhmus.cacina 0:ac4dea3e2894 149 /* sensor hub states */
seyhmus.cacina 0:ac4dea3e2894 150 static bool sc_en = false;
seyhmus.cacina 0:ac4dea3e2894 151 static int data_type = 0;
seyhmus.cacina 0:ac4dea3e2894 152 static int is_sensor_enabled[SS_MAX_SUPPORTED_SENSOR_NUM] = {0};
seyhmus.cacina 0:ac4dea3e2894 153 static int is_algo_enabled[SS_MAX_SUPPORTED_ALGO_NUM] = {0};
seyhmus.cacina 0:ac4dea3e2894 154 static int enabled_algo_mode[SS_MAX_SUPPORTED_ALGO_NUM] = {0};
seyhmus.cacina 0:ac4dea3e2894 155 static int sensor_sample_sz[SS_MAX_SUPPORTED_SENSOR_NUM] = {0};
seyhmus.cacina 0:ac4dea3e2894 156 static int algo_sample_sz[SS_MAX_SUPPORTED_ALGO_NUM] = {0};
seyhmus.cacina 0:ac4dea3e2894 157
seyhmus.cacina 0:ac4dea3e2894 158 /* Mode to control sesnor hub resets. ie via GPIO based hard reset or Command based soft reset*/
seyhmus.cacina 0:ac4dea3e2894 159 static uint8_t ebl_mode = EBL_GPIO_TRIGGER_MODE;
seyhmus.cacina 0:ac4dea3e2894 160
seyhmus.cacina 0:ac4dea3e2894 161 /* desc :
seyhmus.cacina 0:ac4dea3e2894 162 * Func to init master i2c hardware comm interface with sennor hub
seyhmus.cacina 0:ac4dea3e2894 163 * init mfio interrupt pin and attach irq to pin
seyhmus.cacina 0:ac4dea3e2894 164 * init reset pin
seyhmus.cacina 0:ac4dea3e2894 165 * params:
seyhmus.cacina 0:ac4dea3e2894 166 * N/A
seyhmus.cacina 0:ac4dea3e2894 167 */
seyhmus.cacina 0:ac4dea3e2894 168
seyhmus.cacina 0:ac4dea3e2894 169
seyhmus.cacina 0:ac4dea3e2894 170 void sh_irq_handler();
seyhmus.cacina 0:ac4dea3e2894 171 void sh_init_hwcomm_interface(){
seyhmus.cacina 0:ac4dea3e2894 172 static I2C ssI2C(P3_4, P3_5); /*set up sensor hub i2c communication at 400 kHz*/
seyhmus.cacina 0:ac4dea3e2894 173 ssI2C.frequency(400000);
seyhmus.cacina 0:ac4dea3e2894 174 m_i2cBus = &ssI2C;
seyhmus.cacina 0:ac4dea3e2894 175
seyhmus.cacina 0:ac4dea3e2894 176 reset_pin.input();
seyhmus.cacina 0:ac4dea3e2894 177 reset_pin.mode(PullUp);
seyhmus.cacina 0:ac4dea3e2894 178 mfio_pin.input(); /*set mfio as input for getting mfio event reporting when sesnor hub is on application mode */
seyhmus.cacina 0:ac4dea3e2894 179 mfio_pin.mode(PullUp);
seyhmus.cacina 0:ac4dea3e2894 180
seyhmus.cacina 0:ac4dea3e2894 181 irq_pin.fall(sh_irq_handler); /*attach falling edge interrupt to mfio pin for mfio event reporting */
seyhmus.cacina 0:ac4dea3e2894 182
seyhmus.cacina 0:ac4dea3e2894 183 return;
seyhmus.cacina 0:ac4dea3e2894 184 }
seyhmus.cacina 0:ac4dea3e2894 185
seyhmus.cacina 0:ac4dea3e2894 186 /* mfio pin event reporting related interrupt functions*/
seyhmus.cacina 0:ac4dea3e2894 187 /*
seyhmus.cacina 0:ac4dea3e2894 188 * data ready event reporting isr from sensor hub
seyhmus.cacina 0:ac4dea3e2894 189 *
seyhmus.cacina 0:ac4dea3e2894 190 * params:
seyhmus.cacina 0:ac4dea3e2894 191 * N/A
seyhmus.cacina 0:ac4dea3e2894 192 * */
seyhmus.cacina 0:ac4dea3e2894 193 void sh_irq_handler()
seyhmus.cacina 0:ac4dea3e2894 194 {
seyhmus.cacina 0:ac4dea3e2894 195 m_irq_received_ = true;
seyhmus.cacina 0:ac4dea3e2894 196 }
seyhmus.cacina 0:ac4dea3e2894 197 void sh_clear_mfio_event_flag(void){
seyhmus.cacina 0:ac4dea3e2894 198 m_irq_received_ = false;
seyhmus.cacina 0:ac4dea3e2894 199 }
seyhmus.cacina 0:ac4dea3e2894 200
seyhmus.cacina 0:ac4dea3e2894 201 bool sh_has_mfio_event(void){
seyhmus.cacina 0:ac4dea3e2894 202 return m_irq_received_;
seyhmus.cacina 0:ac4dea3e2894 203 }
seyhmus.cacina 0:ac4dea3e2894 204
seyhmus.cacina 0:ac4dea3e2894 205 /* desc:
seyhmus.cacina 0:ac4dea3e2894 206 * func to enable event reporting from sensor hub
seyhmus.cacina 0:ac4dea3e2894 207 *
seyhmus.cacina 0:ac4dea3e2894 208 * params:
seyhmus.cacina 0:ac4dea3e2894 209 * N/A
seyhmus.cacina 0:ac4dea3e2894 210 * */
seyhmus.cacina 0:ac4dea3e2894 211 void sh_enable_irq_mfioevent(void)
seyhmus.cacina 0:ac4dea3e2894 212 {
seyhmus.cacina 0:ac4dea3e2894 213 irq_pin.enable_irq();
seyhmus.cacina 0:ac4dea3e2894 214 }
seyhmus.cacina 0:ac4dea3e2894 215
seyhmus.cacina 0:ac4dea3e2894 216 /* desc:
seyhmus.cacina 0:ac4dea3e2894 217 * func to disable event reporting from sensor hub
seyhmus.cacina 0:ac4dea3e2894 218 *
seyhmus.cacina 0:ac4dea3e2894 219 * params:
seyhmus.cacina 0:ac4dea3e2894 220 * N/A
seyhmus.cacina 0:ac4dea3e2894 221 * */
seyhmus.cacina 0:ac4dea3e2894 222 void sh_disable_irq_mfioevent(void)
seyhmus.cacina 0:ac4dea3e2894 223 {
seyhmus.cacina 0:ac4dea3e2894 224 irq_pin.disable_irq();
seyhmus.cacina 0:ac4dea3e2894 225 }
seyhmus.cacina 0:ac4dea3e2894 226
seyhmus.cacina 0:ac4dea3e2894 227 /* desc:
seyhmus.cacina 0:ac4dea3e2894 228 * reset event reporting process from sensor hub, on host side
seyhmus.cacina 0:ac4dea3e2894 229 *
seyhmus.cacina 0:ac4dea3e2894 230 * params:
seyhmus.cacina 0:ac4dea3e2894 231 * N/A
seyhmus.cacina 0:ac4dea3e2894 232 **/
seyhmus.cacina 0:ac4dea3e2894 233 bool sh_reset_mfio_irq(){
seyhmus.cacina 0:ac4dea3e2894 234 bool ret = mfio_int_happened;
seyhmus.cacina 0:ac4dea3e2894 235 mfio_int_happened = false;
seyhmus.cacina 0:ac4dea3e2894 236 sh_disable_irq_mfioevent();
seyhmus.cacina 0:ac4dea3e2894 237 irq_pin.fall(sh_irq_handler);
seyhmus.cacina 0:ac4dea3e2894 238 sh_enable_irq_mfioevent();
seyhmus.cacina 0:ac4dea3e2894 239 return ret;
seyhmus.cacina 0:ac4dea3e2894 240 }
seyhmus.cacina 0:ac4dea3e2894 241
seyhmus.cacina 0:ac4dea3e2894 242
seyhmus.cacina 0:ac4dea3e2894 243 /*
seyhmus.cacina 0:ac4dea3e2894 244 * desc:
seyhmus.cacina 0:ac4dea3e2894 245 * function to reset sensor hub and put to application mode after reset interface and get data format.
seyhmus.cacina 0:ac4dea3e2894 246 *
seyhmus.cacina 0:ac4dea3e2894 247 * params:
seyhmus.cacina 0:ac4dea3e2894 248 *
seyhmus.cacina 0:ac4dea3e2894 249 * __I wakeupMode : 0x00 : application mode
seyhmus.cacina 0:ac4dea3e2894 250 * 0x08 : bootloader mode
seyhmus.cacina 0:ac4dea3e2894 251 * */
seyhmus.cacina 0:ac4dea3e2894 252 int sh_hard_reset(int wakeupMode){
seyhmus.cacina 0:ac4dea3e2894 253
seyhmus.cacina 0:ac4dea3e2894 254 int status;
seyhmus.cacina 0:ac4dea3e2894 255 sh_disable_irq_mfioevent();
seyhmus.cacina 0:ac4dea3e2894 256 reset_pin.output();
seyhmus.cacina 0:ac4dea3e2894 257 mfio_pin.output();
seyhmus.cacina 0:ac4dea3e2894 258
seyhmus.cacina 0:ac4dea3e2894 259 reset_pin.write(0);
seyhmus.cacina 0:ac4dea3e2894 260 wait_ms(SS_RESET_TIME);
seyhmus.cacina 0:ac4dea3e2894 261
seyhmus.cacina 0:ac4dea3e2894 262 if( (wakeupMode & 0xFF) == 0 ) {
seyhmus.cacina 0:ac4dea3e2894 263
seyhmus.cacina 0:ac4dea3e2894 264 mfio_pin.write(1);
seyhmus.cacina 0:ac4dea3e2894 265 reset_pin.write(1);
seyhmus.cacina 0:ac4dea3e2894 266 wait_ms(SS_STARTUP_TO_MAIN_APP_TIME);
seyhmus.cacina 0:ac4dea3e2894 267
seyhmus.cacina 0:ac4dea3e2894 268 }else {
seyhmus.cacina 0:ac4dea3e2894 269
seyhmus.cacina 0:ac4dea3e2894 270 mfio_pin.write(0);
seyhmus.cacina 0:ac4dea3e2894 271 reset_pin.write(1);
seyhmus.cacina 0:ac4dea3e2894 272 wait_ms(SS_STARTUP_TO_BTLDR_TIME);
seyhmus.cacina 0:ac4dea3e2894 273 }
seyhmus.cacina 0:ac4dea3e2894 274 mfio_pin.input();
seyhmus.cacina 0:ac4dea3e2894 275 mfio_pin.mode(PullUp);
seyhmus.cacina 0:ac4dea3e2894 276 reset_pin.input();
seyhmus.cacina 0:ac4dea3e2894 277 sh_enable_irq_mfioevent();
seyhmus.cacina 0:ac4dea3e2894 278
seyhmus.cacina 0:ac4dea3e2894 279
seyhmus.cacina 0:ac4dea3e2894 280 }
seyhmus.cacina 0:ac4dea3e2894 281
seyhmus.cacina 0:ac4dea3e2894 282
seyhmus.cacina 0:ac4dea3e2894 283 int sh_set_ebl_mode(const uint8_t mode)
seyhmus.cacina 0:ac4dea3e2894 284 {
seyhmus.cacina 0:ac4dea3e2894 285 int status;
seyhmus.cacina 0:ac4dea3e2894 286 if (mode == EBL_CMD_TRIGGER_MODE || mode == EBL_GPIO_TRIGGER_MODE) {
seyhmus.cacina 0:ac4dea3e2894 287 ebl_mode = mode;
seyhmus.cacina 0:ac4dea3e2894 288 status = SS_SUCCESS;
seyhmus.cacina 0:ac4dea3e2894 289 } else
seyhmus.cacina 0:ac4dea3e2894 290 status = SS_ERR_INPUT_VALUE;
seyhmus.cacina 0:ac4dea3e2894 291
seyhmus.cacina 0:ac4dea3e2894 292 return status;
seyhmus.cacina 0:ac4dea3e2894 293 }
seyhmus.cacina 0:ac4dea3e2894 294
seyhmus.cacina 0:ac4dea3e2894 295 const int sh_get_ebl_mode(void)
seyhmus.cacina 0:ac4dea3e2894 296 {
seyhmus.cacina 0:ac4dea3e2894 297 return ebl_mode;
seyhmus.cacina 0:ac4dea3e2894 298 }
seyhmus.cacina 0:ac4dea3e2894 299
seyhmus.cacina 0:ac4dea3e2894 300 int sh_reset_to_bootloader(void){
seyhmus.cacina 0:ac4dea3e2894 301
seyhmus.cacina 0:ac4dea3e2894 302 int status;
seyhmus.cacina 0:ac4dea3e2894 303 uint8_t hubMode;
seyhmus.cacina 0:ac4dea3e2894 304
seyhmus.cacina 0:ac4dea3e2894 305 if(ebl_mode == EBL_GPIO_TRIGGER_MODE)
seyhmus.cacina 0:ac4dea3e2894 306 sh_hard_reset(0x08);
seyhmus.cacina 0:ac4dea3e2894 307 if(ebl_mode == EBL_CMD_TRIGGER_MODE)
seyhmus.cacina 0:ac4dea3e2894 308 status = sh_set_sensorhub_operating_mode(0x08);
seyhmus.cacina 0:ac4dea3e2894 309
seyhmus.cacina 0:ac4dea3e2894 310 status = sh_get_sensorhub_operating_mode(&hubMode);
seyhmus.cacina 0:ac4dea3e2894 311 if( status != 0x00 /*SS_SUCCESS*/ || hubMode != 0x08 ){
seyhmus.cacina 0:ac4dea3e2894 312 status = -1;
seyhmus.cacina 0:ac4dea3e2894 313 }
seyhmus.cacina 0:ac4dea3e2894 314
seyhmus.cacina 0:ac4dea3e2894 315 return status;
seyhmus.cacina 0:ac4dea3e2894 316
seyhmus.cacina 0:ac4dea3e2894 317 }
seyhmus.cacina 0:ac4dea3e2894 318
seyhmus.cacina 0:ac4dea3e2894 319 static bool in_bootldr;
seyhmus.cacina 0:ac4dea3e2894 320
seyhmus.cacina 0:ac4dea3e2894 321
seyhmus.cacina 0:ac4dea3e2894 322 int in_bootldr_mode()
seyhmus.cacina 0:ac4dea3e2894 323 {
seyhmus.cacina 0:ac4dea3e2894 324 uint8_t cmd_bytes[] = { SS_FAM_R_MODE, SS_CMDIDX_MODE };
seyhmus.cacina 0:ac4dea3e2894 325 uint8_t rxbuf[2] = { 0 };
seyhmus.cacina 0:ac4dea3e2894 326
seyhmus.cacina 0:ac4dea3e2894 327 int status = sh_read_cmd(&cmd_bytes[0], sizeof(cmd_bytes),
seyhmus.cacina 0:ac4dea3e2894 328 0, 0,
seyhmus.cacina 0:ac4dea3e2894 329 &rxbuf[0], sizeof(rxbuf), SS_DEFAULT_CMD_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 330 if (status != SS_SUCCESS)
seyhmus.cacina 0:ac4dea3e2894 331 return -1;
seyhmus.cacina 0:ac4dea3e2894 332
seyhmus.cacina 0:ac4dea3e2894 333 return (rxbuf[1] & SS_MASK_MODE_BOOTLDR);
seyhmus.cacina 0:ac4dea3e2894 334 }
seyhmus.cacina 0:ac4dea3e2894 335
seyhmus.cacina 0:ac4dea3e2894 336 int exit_from_bootloader(void)
seyhmus.cacina 0:ac4dea3e2894 337 {
seyhmus.cacina 0:ac4dea3e2894 338 uint8_t cmd_bytes[] = { SS_FAM_W_MODE, SS_CMDIDX_MODE };
seyhmus.cacina 0:ac4dea3e2894 339 uint8_t data[] = { 0x00 };
seyhmus.cacina 0:ac4dea3e2894 340
seyhmus.cacina 0:ac4dea3e2894 341 int status = sh_write_cmd_with_data( &cmd_bytes[0], sizeof(cmd_bytes),
seyhmus.cacina 0:ac4dea3e2894 342 &data[0], 1 /*sizeof(data)*/,
seyhmus.cacina 0:ac4dea3e2894 343 10*SS_DEFAULT_CMD_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 344
seyhmus.cacina 0:ac4dea3e2894 345 in_bootldr = (status == SS_SUCCESS) ? true : false;
seyhmus.cacina 0:ac4dea3e2894 346
seyhmus.cacina 0:ac4dea3e2894 347 return status;
seyhmus.cacina 0:ac4dea3e2894 348 }
seyhmus.cacina 0:ac4dea3e2894 349
seyhmus.cacina 0:ac4dea3e2894 350 static int stay_in_bootloader()
seyhmus.cacina 0:ac4dea3e2894 351 {
seyhmus.cacina 0:ac4dea3e2894 352 uint8_t cmd_bytes[] = { SS_FAM_W_MODE, SS_CMDIDX_MODE };
seyhmus.cacina 0:ac4dea3e2894 353 uint8_t data[] = { SS_MASK_MODE_BOOTLDR };
seyhmus.cacina 0:ac4dea3e2894 354
seyhmus.cacina 0:ac4dea3e2894 355 int status = sh_write_cmd_with_data(
seyhmus.cacina 0:ac4dea3e2894 356 &cmd_bytes[0], sizeof(cmd_bytes),
seyhmus.cacina 0:ac4dea3e2894 357 &data[0], sizeof(data), SS_DEFAULT_CMD_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 358
seyhmus.cacina 0:ac4dea3e2894 359 in_bootldr = (status == SS_SUCCESS) ? true : false;
seyhmus.cacina 0:ac4dea3e2894 360 return status;
seyhmus.cacina 0:ac4dea3e2894 361 }
seyhmus.cacina 0:ac4dea3e2894 362
seyhmus.cacina 0:ac4dea3e2894 363
seyhmus.cacina 0:ac4dea3e2894 364 static void cfg_mfio(PinDirection dir)
seyhmus.cacina 0:ac4dea3e2894 365 {
seyhmus.cacina 0:ac4dea3e2894 366 if (dir == PIN_INPUT) {
seyhmus.cacina 0:ac4dea3e2894 367 mfio_pin.input();
seyhmus.cacina 0:ac4dea3e2894 368 mfio_pin.mode(PullUp);
seyhmus.cacina 0:ac4dea3e2894 369 } else {
seyhmus.cacina 0:ac4dea3e2894 370 sh_enable_irq_mfioevent();
seyhmus.cacina 0:ac4dea3e2894 371 mfio_pin.output();
seyhmus.cacina 0:ac4dea3e2894 372 }
seyhmus.cacina 0:ac4dea3e2894 373 }
seyhmus.cacina 0:ac4dea3e2894 374
seyhmus.cacina 0:ac4dea3e2894 375 int sh_debug_reset_to_bootloader(void)
seyhmus.cacina 0:ac4dea3e2894 376 {
seyhmus.cacina 0:ac4dea3e2894 377
seyhmus.cacina 0:ac4dea3e2894 378 int status = -1;
seyhmus.cacina 0:ac4dea3e2894 379
seyhmus.cacina 0:ac4dea3e2894 380 sh_disable_irq_mfioevent();
seyhmus.cacina 0:ac4dea3e2894 381 if (ebl_mode == EBL_GPIO_TRIGGER_MODE) {
seyhmus.cacina 0:ac4dea3e2894 382
seyhmus.cacina 0:ac4dea3e2894 383 reset_pin.output();
seyhmus.cacina 0:ac4dea3e2894 384 cfg_mfio(PIN_OUTPUT);
seyhmus.cacina 0:ac4dea3e2894 385 reset_pin.write(0);
seyhmus.cacina 0:ac4dea3e2894 386 wait_ms(SS_RESET_TIME);
seyhmus.cacina 0:ac4dea3e2894 387 mfio_pin.write(0);
seyhmus.cacina 0:ac4dea3e2894 388 reset_pin.write(1);
seyhmus.cacina 0:ac4dea3e2894 389 wait_ms(SS_STARTUP_TO_BTLDR_TIME);
seyhmus.cacina 0:ac4dea3e2894 390 cfg_mfio(PIN_INPUT);
seyhmus.cacina 0:ac4dea3e2894 391 reset_pin.input();
seyhmus.cacina 0:ac4dea3e2894 392 sh_enable_irq_mfioevent();
seyhmus.cacina 0:ac4dea3e2894 393 stay_in_bootloader();
seyhmus.cacina 0:ac4dea3e2894 394 if (in_bootldr_mode() < 0)
seyhmus.cacina 0:ac4dea3e2894 395 status = SS_ERR_UNKNOWN;
seyhmus.cacina 0:ac4dea3e2894 396 else
seyhmus.cacina 0:ac4dea3e2894 397 status = SS_SUCCESS;
seyhmus.cacina 0:ac4dea3e2894 398
seyhmus.cacina 0:ac4dea3e2894 399 }else{
seyhmus.cacina 0:ac4dea3e2894 400 stay_in_bootloader();
seyhmus.cacina 0:ac4dea3e2894 401 sh_enable_irq_mfioevent();
seyhmus.cacina 0:ac4dea3e2894 402 status = SS_SUCCESS;
seyhmus.cacina 0:ac4dea3e2894 403
seyhmus.cacina 0:ac4dea3e2894 404 }
seyhmus.cacina 0:ac4dea3e2894 405
seyhmus.cacina 0:ac4dea3e2894 406 return status;
seyhmus.cacina 0:ac4dea3e2894 407 }
seyhmus.cacina 0:ac4dea3e2894 408
seyhmus.cacina 0:ac4dea3e2894 409
seyhmus.cacina 0:ac4dea3e2894 410 int sh_reset_to_main_app(void)
seyhmus.cacina 0:ac4dea3e2894 411 {
seyhmus.cacina 0:ac4dea3e2894 412 int status = -1;
seyhmus.cacina 0:ac4dea3e2894 413 sh_disable_irq_mfioevent();
seyhmus.cacina 0:ac4dea3e2894 414 if (ebl_mode == EBL_GPIO_TRIGGER_MODE) {
seyhmus.cacina 0:ac4dea3e2894 415
seyhmus.cacina 0:ac4dea3e2894 416 reset_pin.output();
seyhmus.cacina 0:ac4dea3e2894 417 cfg_mfio(PIN_OUTPUT);
seyhmus.cacina 0:ac4dea3e2894 418 mfio_pin.write(0);
seyhmus.cacina 0:ac4dea3e2894 419 wait_ms(SS_RESET_TIME - 5);
seyhmus.cacina 0:ac4dea3e2894 420 reset_pin.write(0);
seyhmus.cacina 0:ac4dea3e2894 421 wait_ms(SS_RESET_TIME - 5);
seyhmus.cacina 0:ac4dea3e2894 422 mfio_pin.write(1);
seyhmus.cacina 0:ac4dea3e2894 423 wait_ms(SS_RESET_TIME - 5);
seyhmus.cacina 0:ac4dea3e2894 424 reset_pin.write(1);
seyhmus.cacina 0:ac4dea3e2894 425 //wait_ms(50);
seyhmus.cacina 0:ac4dea3e2894 426 //mfio_pin.write(0);
seyhmus.cacina 0:ac4dea3e2894 427 wait_ms(2*SS_STARTUP_TO_MAIN_APP_TIME);
seyhmus.cacina 0:ac4dea3e2894 428 cfg_mfio(PIN_INPUT);
seyhmus.cacina 0:ac4dea3e2894 429 reset_pin.input();
seyhmus.cacina 0:ac4dea3e2894 430
seyhmus.cacina 0:ac4dea3e2894 431 sh_enable_irq_mfioevent();
seyhmus.cacina 0:ac4dea3e2894 432 // Verify we exited bootloader mode
seyhmus.cacina 0:ac4dea3e2894 433 if (in_bootldr_mode() == 0)
seyhmus.cacina 0:ac4dea3e2894 434 status = SS_SUCCESS;
seyhmus.cacina 0:ac4dea3e2894 435 else
seyhmus.cacina 0:ac4dea3e2894 436 status = SS_ERR_UNKNOWN;
seyhmus.cacina 0:ac4dea3e2894 437 }else{
seyhmus.cacina 0:ac4dea3e2894 438 status = exit_from_bootloader();
seyhmus.cacina 0:ac4dea3e2894 439 sh_enable_irq_mfioevent();
seyhmus.cacina 0:ac4dea3e2894 440 }
seyhmus.cacina 0:ac4dea3e2894 441
seyhmus.cacina 0:ac4dea3e2894 442 return status;
seyhmus.cacina 0:ac4dea3e2894 443
seyhmus.cacina 0:ac4dea3e2894 444 }
seyhmus.cacina 0:ac4dea3e2894 445
seyhmus.cacina 0:ac4dea3e2894 446 /*
seyhmus.cacina 0:ac4dea3e2894 447 * desc:
seyhmus.cacina 0:ac4dea3e2894 448 * function to init sensor comm interface and get data format.
seyhmus.cacina 0:ac4dea3e2894 449 *
seyhmus.cacina 0:ac4dea3e2894 450 * */
seyhmus.cacina 0:ac4dea3e2894 451 void sh_init_hubinterface(void){
seyhmus.cacina 0:ac4dea3e2894 452
seyhmus.cacina 0:ac4dea3e2894 453 sh_init_hwcomm_interface();
seyhmus.cacina 0:ac4dea3e2894 454 //sh_get_data_type(&data_type, &sc_en);
seyhmus.cacina 0:ac4dea3e2894 455 return;
seyhmus.cacina 0:ac4dea3e2894 456 }
seyhmus.cacina 0:ac4dea3e2894 457
seyhmus.cacina 0:ac4dea3e2894 458
seyhmus.cacina 0:ac4dea3e2894 459 /*
seyhmus.cacina 0:ac4dea3e2894 460 *
seyhmus.cacina 0:ac4dea3e2894 461 * SENSOR HUB COMMUNICATION INTERFACE ( Defined in MAX32664 User Guide ) API FUNCTIONS
seyhmus.cacina 0:ac4dea3e2894 462 *
seyhmus.cacina 0:ac4dea3e2894 463 *
seyhmus.cacina 0:ac4dea3e2894 464 * */
seyhmus.cacina 0:ac4dea3e2894 465
seyhmus.cacina 0:ac4dea3e2894 466
seyhmus.cacina 0:ac4dea3e2894 467 //PHASE2 ADDITIONS:
seyhmus.cacina 0:ac4dea3e2894 468
seyhmus.cacina 0:ac4dea3e2894 469 int sh_self_test(int idx, uint8_t *result, int sleep_ms){
seyhmus.cacina 0:ac4dea3e2894 470
seyhmus.cacina 0:ac4dea3e2894 471 uint8_t cmd_bytes[] = { SS_FAM_R_SELFTEST, (uint8_t)idx }; // = SH_SELFTEST_CMDSEQ;
seyhmus.cacina 0:ac4dea3e2894 472 uint8_t rxbuf[2];
seyhmus.cacina 0:ac4dea3e2894 473 result[0] = 0xFF;
seyhmus.cacina 0:ac4dea3e2894 474
seyhmus.cacina 0:ac4dea3e2894 475 int status = sh_read_cmd(&cmd_bytes[0],sizeof(cmd_bytes) ,
seyhmus.cacina 0:ac4dea3e2894 476 0, 0,
seyhmus.cacina 0:ac4dea3e2894 477 &rxbuf[0], sizeof(rxbuf),
seyhmus.cacina 0:ac4dea3e2894 478 sleep_ms );
seyhmus.cacina 0:ac4dea3e2894 479
seyhmus.cacina 0:ac4dea3e2894 480 if (status != SS_SUCCESS)
seyhmus.cacina 0:ac4dea3e2894 481 return SS_ERR_TRY_AGAIN;
seyhmus.cacina 0:ac4dea3e2894 482
seyhmus.cacina 0:ac4dea3e2894 483 result[0] = rxbuf[1];
seyhmus.cacina 0:ac4dea3e2894 484 return status;
seyhmus.cacina 0:ac4dea3e2894 485 }
seyhmus.cacina 0:ac4dea3e2894 486
seyhmus.cacina 0:ac4dea3e2894 487 const char* sh_get_hub_fw_version(void)
seyhmus.cacina 0:ac4dea3e2894 488 {
seyhmus.cacina 0:ac4dea3e2894 489 uint8_t cmd_bytes[2];
seyhmus.cacina 0:ac4dea3e2894 490 uint8_t rxbuf[4];
seyhmus.cacina 0:ac4dea3e2894 491
seyhmus.cacina 0:ac4dea3e2894 492 static char fw_version[32] = "SENSORHUB";
seyhmus.cacina 0:ac4dea3e2894 493
seyhmus.cacina 0:ac4dea3e2894 494 int bootldr = sh_checkif_bootldr_mode();
seyhmus.cacina 0:ac4dea3e2894 495
seyhmus.cacina 0:ac4dea3e2894 496 if (bootldr > 0) {
seyhmus.cacina 0:ac4dea3e2894 497 cmd_bytes[0] = SS_FAM_R_BOOTLOADER;
seyhmus.cacina 0:ac4dea3e2894 498 cmd_bytes[1] = SS_CMDIDX_BOOTFWVERSION;
seyhmus.cacina 0:ac4dea3e2894 499 } else if (bootldr == 0) {
seyhmus.cacina 0:ac4dea3e2894 500 cmd_bytes[0] = SS_FAM_R_IDENTITY;
seyhmus.cacina 0:ac4dea3e2894 501 cmd_bytes[1] = SS_CMDIDX_FWVERSION;
seyhmus.cacina 0:ac4dea3e2894 502 } else {
seyhmus.cacina 0:ac4dea3e2894 503
seyhmus.cacina 0:ac4dea3e2894 504 return &fw_version[0];
seyhmus.cacina 0:ac4dea3e2894 505 }
seyhmus.cacina 0:ac4dea3e2894 506
seyhmus.cacina 0:ac4dea3e2894 507 int status = sh_read_cmd( &cmd_bytes[0], sizeof(cmd_bytes),
seyhmus.cacina 0:ac4dea3e2894 508 0, 0,
seyhmus.cacina 0:ac4dea3e2894 509 &rxbuf[0], sizeof(rxbuf),
seyhmus.cacina 0:ac4dea3e2894 510 SS_DEFAULT_CMD_SLEEP_MS );
seyhmus.cacina 0:ac4dea3e2894 511
seyhmus.cacina 0:ac4dea3e2894 512 if (status == SS_SUCCESS) {
seyhmus.cacina 0:ac4dea3e2894 513 snprintf(fw_version, sizeof(fw_version),
seyhmus.cacina 0:ac4dea3e2894 514 "%d.%d.%d", rxbuf[1], rxbuf[2], rxbuf[3]);
seyhmus.cacina 0:ac4dea3e2894 515 }
seyhmus.cacina 0:ac4dea3e2894 516
seyhmus.cacina 0:ac4dea3e2894 517 return &fw_version[0];
seyhmus.cacina 0:ac4dea3e2894 518 }
seyhmus.cacina 0:ac4dea3e2894 519
seyhmus.cacina 0:ac4dea3e2894 520
seyhmus.cacina 0:ac4dea3e2894 521 const char* sh_get_hub_algo_version(void)
seyhmus.cacina 0:ac4dea3e2894 522 {
seyhmus.cacina 0:ac4dea3e2894 523 uint8_t cmd_bytes[3];
seyhmus.cacina 0:ac4dea3e2894 524 uint8_t rxbuf[4];
seyhmus.cacina 0:ac4dea3e2894 525
seyhmus.cacina 0:ac4dea3e2894 526 static char algo_version[64] = "SENSORHUBALGORITHMS";
seyhmus.cacina 0:ac4dea3e2894 527
seyhmus.cacina 0:ac4dea3e2894 528 int bootldr = sh_checkif_bootldr_mode();
seyhmus.cacina 0:ac4dea3e2894 529
seyhmus.cacina 0:ac4dea3e2894 530 if (bootldr > 0) {
seyhmus.cacina 0:ac4dea3e2894 531 cmd_bytes[0] = SS_FAM_R_BOOTLOADER;
seyhmus.cacina 0:ac4dea3e2894 532 cmd_bytes[1] = SS_CMDIDX_BOOTFWVERSION;
seyhmus.cacina 0:ac4dea3e2894 533 cmd_bytes[2] = 0;
seyhmus.cacina 0:ac4dea3e2894 534 } else if (bootldr == 0) {
seyhmus.cacina 0:ac4dea3e2894 535 cmd_bytes[0] = SS_FAM_R_IDENTITY;
seyhmus.cacina 0:ac4dea3e2894 536 cmd_bytes[1] = SS_CMDIDX_ALGOVER;
seyhmus.cacina 0:ac4dea3e2894 537 cmd_bytes[2] = SS_CMDIDX_AVAILSENSORS;
seyhmus.cacina 0:ac4dea3e2894 538 } else {
seyhmus.cacina 0:ac4dea3e2894 539
seyhmus.cacina 0:ac4dea3e2894 540 return &algo_version[0];
seyhmus.cacina 0:ac4dea3e2894 541 }
seyhmus.cacina 0:ac4dea3e2894 542
seyhmus.cacina 0:ac4dea3e2894 543 int status = sh_read_cmd( &cmd_bytes[0], sizeof(cmd_bytes),
seyhmus.cacina 0:ac4dea3e2894 544 0, 0,
seyhmus.cacina 0:ac4dea3e2894 545 &rxbuf[0], sizeof(rxbuf),
seyhmus.cacina 0:ac4dea3e2894 546 SS_DEFAULT_CMD_SLEEP_MS );
seyhmus.cacina 0:ac4dea3e2894 547
seyhmus.cacina 0:ac4dea3e2894 548 if (status == SS_SUCCESS) {
seyhmus.cacina 0:ac4dea3e2894 549 snprintf(algo_version, sizeof(algo_version),
seyhmus.cacina 0:ac4dea3e2894 550 "%d.%d.%d", rxbuf[1], rxbuf[2], rxbuf[3]);
seyhmus.cacina 0:ac4dea3e2894 551
seyhmus.cacina 0:ac4dea3e2894 552 }
seyhmus.cacina 0:ac4dea3e2894 553
seyhmus.cacina 0:ac4dea3e2894 554 return &algo_version[0];
seyhmus.cacina 0:ac4dea3e2894 555 }
seyhmus.cacina 0:ac4dea3e2894 556
seyhmus.cacina 0:ac4dea3e2894 557 int sh_send_raw(uint8_t *rawdata, int rawdata_sz)
seyhmus.cacina 0:ac4dea3e2894 558 {
seyhmus.cacina 0:ac4dea3e2894 559 return sh_write_cmd(&rawdata[0], rawdata_sz, 5 * SS_ENABLE_SENSOR_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 560 }
seyhmus.cacina 0:ac4dea3e2894 561
seyhmus.cacina 0:ac4dea3e2894 562 int sh_get_log_len(int *log_len)
seyhmus.cacina 0:ac4dea3e2894 563 {
seyhmus.cacina 0:ac4dea3e2894 564 uint8_t cmd_bytes[] = { SS_FAM_R_LOG, SS_CMDIDX_R_LOG_LEN }; // = SH_GETLOGSIZE_CMDSEQ;
seyhmus.cacina 0:ac4dea3e2894 565 uint8_t rxbuf[2] = {0};
seyhmus.cacina 0:ac4dea3e2894 566 int logLen = 0;
seyhmus.cacina 0:ac4dea3e2894 567
seyhmus.cacina 0:ac4dea3e2894 568 int status = sh_read_cmd(&cmd_bytes[0], sizeof(cmd_bytes),
seyhmus.cacina 0:ac4dea3e2894 569 0, 0,
seyhmus.cacina 0:ac4dea3e2894 570 &rxbuf[0], sizeof(rxbuf),
seyhmus.cacina 0:ac4dea3e2894 571 SS_DEFAULT_CMD_SLEEP_MS );
seyhmus.cacina 0:ac4dea3e2894 572
seyhmus.cacina 0:ac4dea3e2894 573 if (status == SS_SUCCESS) {
seyhmus.cacina 0:ac4dea3e2894 574 logLen = (rxbuf[1] << 8) | rxbuf[0];
seyhmus.cacina 0:ac4dea3e2894 575 }
seyhmus.cacina 0:ac4dea3e2894 576 *log_len = logLen;
seyhmus.cacina 0:ac4dea3e2894 577
seyhmus.cacina 0:ac4dea3e2894 578 return status;
seyhmus.cacina 0:ac4dea3e2894 579 }
seyhmus.cacina 0:ac4dea3e2894 580
seyhmus.cacina 0:ac4dea3e2894 581 int sh_read_ss_log(int num_bytes, uint8_t *log_buf, int log_buf_sz)
seyhmus.cacina 0:ac4dea3e2894 582 {
seyhmus.cacina 0:ac4dea3e2894 583 int bytes_to_read = num_bytes + 1; //+1 for status byte
seyhmus.cacina 0:ac4dea3e2894 584 //mxm_assert_msg((bytes_to_read <= log_buf_sz), "log_buf too small");
seyhmus.cacina 0:ac4dea3e2894 585
seyhmus.cacina 0:ac4dea3e2894 586 uint8_t cmd_bytes[] = { SS_FAM_R_LOG, SS_CMDIDX_R_LOG_DATA }; // = SH_READHUBLOGS_CMDSEQ;
seyhmus.cacina 0:ac4dea3e2894 587
seyhmus.cacina 0:ac4dea3e2894 588 int status = sh_read_cmd(&cmd_bytes[0], sizeof(cmd_bytes),
seyhmus.cacina 0:ac4dea3e2894 589 0, 0,
seyhmus.cacina 0:ac4dea3e2894 590 log_buf, bytes_to_read,
seyhmus.cacina 0:ac4dea3e2894 591 SS_CMD_WAIT_PULLTRANS_MS );
seyhmus.cacina 0:ac4dea3e2894 592
seyhmus.cacina 0:ac4dea3e2894 593 return status;
seyhmus.cacina 0:ac4dea3e2894 594 }
seyhmus.cacina 0:ac4dea3e2894 595
seyhmus.cacina 0:ac4dea3e2894 596 // END OF PHASE2 ADDITIONS
seyhmus.cacina 0:ac4dea3e2894 597
seyhmus.cacina 0:ac4dea3e2894 598
seyhmus.cacina 0:ac4dea3e2894 599
seyhmus.cacina 0:ac4dea3e2894 600
seyhmus.cacina 0:ac4dea3e2894 601 int sh_write_cmd( uint8_t *tx_buf,
seyhmus.cacina 0:ac4dea3e2894 602 int tx_len,
seyhmus.cacina 0:ac4dea3e2894 603 int sleep_ms)
seyhmus.cacina 0:ac4dea3e2894 604 {
seyhmus.cacina 0:ac4dea3e2894 605 int retries = SS_DEFAULT_RETRIES;
seyhmus.cacina 0:ac4dea3e2894 606 int ret = m_i2cBus->write(SS_I2C_8BIT_SLAVE_ADDR, (char*)tx_buf, tx_len);
seyhmus.cacina 0:ac4dea3e2894 607 while (ret != 0 && retries-- > 0) {
seyhmus.cacina 0:ac4dea3e2894 608
seyhmus.cacina 0:ac4dea3e2894 609 wait_ms(1);
seyhmus.cacina 0:ac4dea3e2894 610 ret = m_i2cBus->write(SS_I2C_8BIT_SLAVE_ADDR, (char*)tx_buf, tx_len);
seyhmus.cacina 0:ac4dea3e2894 611 }
seyhmus.cacina 0:ac4dea3e2894 612 if (ret != 0)
seyhmus.cacina 0:ac4dea3e2894 613 return SS_ERR_UNAVAILABLE;
seyhmus.cacina 0:ac4dea3e2894 614
seyhmus.cacina 0:ac4dea3e2894 615
seyhmus.cacina 0:ac4dea3e2894 616 wait_ms(sleep_ms);
seyhmus.cacina 0:ac4dea3e2894 617
seyhmus.cacina 0:ac4dea3e2894 618 char status_byte;
seyhmus.cacina 0:ac4dea3e2894 619 ret = m_i2cBus->read(SS_I2C_8BIT_SLAVE_ADDR, &status_byte, 1);
seyhmus.cacina 0:ac4dea3e2894 620 bool try_again = (status_byte == SS_ERR_TRY_AGAIN);
seyhmus.cacina 0:ac4dea3e2894 621 while ((ret != 0 || try_again)
seyhmus.cacina 0:ac4dea3e2894 622 && retries-- > 0) {
seyhmus.cacina 0:ac4dea3e2894 623 wait_ms(sleep_ms);
seyhmus.cacina 0:ac4dea3e2894 624 ret = m_i2cBus->read(SS_I2C_8BIT_SLAVE_ADDR, &status_byte, 1);
seyhmus.cacina 0:ac4dea3e2894 625 try_again = (status_byte == SS_ERR_TRY_AGAIN);
seyhmus.cacina 0:ac4dea3e2894 626 }
seyhmus.cacina 0:ac4dea3e2894 627
seyhmus.cacina 0:ac4dea3e2894 628 if (ret != 0 || try_again)
seyhmus.cacina 0:ac4dea3e2894 629 return SS_ERR_UNAVAILABLE;
seyhmus.cacina 0:ac4dea3e2894 630
seyhmus.cacina 0:ac4dea3e2894 631 return (int) (SS_STATUS)status_byte;
seyhmus.cacina 0:ac4dea3e2894 632 }
seyhmus.cacina 0:ac4dea3e2894 633
seyhmus.cacina 0:ac4dea3e2894 634
seyhmus.cacina 0:ac4dea3e2894 635 int sh_write_cmd_with_data(uint8_t *cmd_bytes,
seyhmus.cacina 0:ac4dea3e2894 636 int cmd_bytes_len,
seyhmus.cacina 0:ac4dea3e2894 637 uint8_t *data,
seyhmus.cacina 0:ac4dea3e2894 638 int data_len,
seyhmus.cacina 0:ac4dea3e2894 639 int cmd_delay_ms)
seyhmus.cacina 0:ac4dea3e2894 640 {
seyhmus.cacina 0:ac4dea3e2894 641 memcpy(sh_write_buf, cmd_bytes, cmd_bytes_len);
seyhmus.cacina 0:ac4dea3e2894 642 memcpy(sh_write_buf + cmd_bytes_len, data, data_len);
seyhmus.cacina 0:ac4dea3e2894 643 int status = sh_write_cmd(sh_write_buf,cmd_bytes_len + data_len, cmd_delay_ms);
seyhmus.cacina 0:ac4dea3e2894 644 return status;
seyhmus.cacina 0:ac4dea3e2894 645 }
seyhmus.cacina 0:ac4dea3e2894 646
seyhmus.cacina 0:ac4dea3e2894 647
seyhmus.cacina 0:ac4dea3e2894 648 int sh_read_cmd( uint8_t *cmd_bytes,
seyhmus.cacina 0:ac4dea3e2894 649 int cmd_bytes_len,
seyhmus.cacina 0:ac4dea3e2894 650 uint8_t *data,
seyhmus.cacina 0:ac4dea3e2894 651 int data_len,
seyhmus.cacina 0:ac4dea3e2894 652 uint8_t *rxbuf,
seyhmus.cacina 0:ac4dea3e2894 653 int rxbuf_sz,
seyhmus.cacina 0:ac4dea3e2894 654 int sleep_ms )
seyhmus.cacina 0:ac4dea3e2894 655 {
seyhmus.cacina 0:ac4dea3e2894 656
seyhmus.cacina 0:ac4dea3e2894 657 int retries = SS_DEFAULT_RETRIES;
seyhmus.cacina 0:ac4dea3e2894 658
seyhmus.cacina 0:ac4dea3e2894 659 int ret = m_i2cBus->write(SS_I2C_8BIT_SLAVE_ADDR, (char*)cmd_bytes, cmd_bytes_len, (data_len != 0));
seyhmus.cacina 0:ac4dea3e2894 660 if (data_len != 0)
seyhmus.cacina 0:ac4dea3e2894 661 ret |= m_i2cBus->write(SS_I2C_8BIT_SLAVE_ADDR, (char*)data, data_len, false);
seyhmus.cacina 0:ac4dea3e2894 662
seyhmus.cacina 0:ac4dea3e2894 663
seyhmus.cacina 0:ac4dea3e2894 664 while (ret != 0 && retries-- > 0) {
seyhmus.cacina 0:ac4dea3e2894 665 wait_ms(1);
seyhmus.cacina 0:ac4dea3e2894 666 ret = m_i2cBus->write(SS_I2C_8BIT_SLAVE_ADDR, (char*)cmd_bytes, cmd_bytes_len, (data_len != 0));
seyhmus.cacina 0:ac4dea3e2894 667 if (data_len != 0)
seyhmus.cacina 0:ac4dea3e2894 668 ret |= m_i2cBus->write(SS_I2C_8BIT_SLAVE_ADDR, (char*)data, data_len, false);
seyhmus.cacina 0:ac4dea3e2894 669
seyhmus.cacina 0:ac4dea3e2894 670 }
seyhmus.cacina 0:ac4dea3e2894 671 if (ret != 0)
seyhmus.cacina 0:ac4dea3e2894 672 return SS_ERR_UNAVAILABLE;
seyhmus.cacina 0:ac4dea3e2894 673
seyhmus.cacina 0:ac4dea3e2894 674
seyhmus.cacina 0:ac4dea3e2894 675 wait_ms(sleep_ms);
seyhmus.cacina 0:ac4dea3e2894 676
seyhmus.cacina 0:ac4dea3e2894 677 ret = m_i2cBus->read(SS_I2C_8BIT_SLAVE_ADDR, (char*)rxbuf, rxbuf_sz);
seyhmus.cacina 0:ac4dea3e2894 678 bool try_again = (rxbuf[0] == SS_ERR_TRY_AGAIN);
seyhmus.cacina 0:ac4dea3e2894 679 while ((ret != 0 || try_again) && retries-- > 0) {
seyhmus.cacina 0:ac4dea3e2894 680 wait_ms(sleep_ms);
seyhmus.cacina 0:ac4dea3e2894 681 ret = m_i2cBus->read(SS_I2C_8BIT_SLAVE_ADDR, (char*)rxbuf, rxbuf_sz);
seyhmus.cacina 0:ac4dea3e2894 682 try_again = (rxbuf[0] == SS_ERR_TRY_AGAIN);
seyhmus.cacina 0:ac4dea3e2894 683 }
seyhmus.cacina 0:ac4dea3e2894 684 if (ret != 0 || try_again)
seyhmus.cacina 0:ac4dea3e2894 685 return SS_ERR_UNAVAILABLE;
seyhmus.cacina 0:ac4dea3e2894 686
seyhmus.cacina 0:ac4dea3e2894 687 return (int) ((SS_STATUS)rxbuf[0]);
seyhmus.cacina 0:ac4dea3e2894 688 }
seyhmus.cacina 0:ac4dea3e2894 689
seyhmus.cacina 0:ac4dea3e2894 690
seyhmus.cacina 0:ac4dea3e2894 691
seyhmus.cacina 0:ac4dea3e2894 692 int sh_get_sensorhub_status(uint8_t *hubStatus){
seyhmus.cacina 0:ac4dea3e2894 693
seyhmus.cacina 0:ac4dea3e2894 694 uint8_t ByteSeq[] = SH_GET_HUB_STATUS_CMDSEQ;
seyhmus.cacina 0:ac4dea3e2894 695 uint8_t rxbuf[2] = { 0 };
seyhmus.cacina 0:ac4dea3e2894 696
seyhmus.cacina 0:ac4dea3e2894 697 int status = sh_read_cmd(&ByteSeq[0], sizeof(ByteSeq),
seyhmus.cacina 0:ac4dea3e2894 698 0, 0,
seyhmus.cacina 0:ac4dea3e2894 699 &rxbuf[0], sizeof(rxbuf),
seyhmus.cacina 0:ac4dea3e2894 700 SS_DEFAULT_CMD_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 701
seyhmus.cacina 0:ac4dea3e2894 702 *hubStatus = rxbuf[1];
seyhmus.cacina 0:ac4dea3e2894 703 return status;
seyhmus.cacina 0:ac4dea3e2894 704 }
seyhmus.cacina 0:ac4dea3e2894 705
seyhmus.cacina 0:ac4dea3e2894 706
seyhmus.cacina 0:ac4dea3e2894 707 int sh_get_sensorhub_operating_mode(uint8_t *hubMode){
seyhmus.cacina 0:ac4dea3e2894 708
seyhmus.cacina 0:ac4dea3e2894 709 uint8_t ByteSeq[] = SH_GET_OPERATING_MODE_CMDSEQ;
seyhmus.cacina 0:ac4dea3e2894 710 uint8_t rxbuf[2] = { 0 };
seyhmus.cacina 0:ac4dea3e2894 711
seyhmus.cacina 0:ac4dea3e2894 712 int status = sh_read_cmd(&ByteSeq[0], sizeof(ByteSeq),
seyhmus.cacina 0:ac4dea3e2894 713 0, 0,
seyhmus.cacina 0:ac4dea3e2894 714 &rxbuf[0], sizeof(rxbuf),
seyhmus.cacina 0:ac4dea3e2894 715 SS_DEFAULT_CMD_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 716
seyhmus.cacina 0:ac4dea3e2894 717 *hubMode = rxbuf[1];
seyhmus.cacina 0:ac4dea3e2894 718 return status;
seyhmus.cacina 0:ac4dea3e2894 719 }
seyhmus.cacina 0:ac4dea3e2894 720
seyhmus.cacina 0:ac4dea3e2894 721
seyhmus.cacina 0:ac4dea3e2894 722 int sh_set_sensorhub_operating_mode(uint8_t hubMode){
seyhmus.cacina 0:ac4dea3e2894 723
seyhmus.cacina 0:ac4dea3e2894 724 uint8_t ByteSeq[] = SH_SET_OPERATING_MODE_CMDSEQ(hubMode);
seyhmus.cacina 0:ac4dea3e2894 725 int status = sh_write_cmd( &ByteSeq[0],sizeof(ByteSeq), SS_DEFAULT_CMD_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 726 return status;
seyhmus.cacina 0:ac4dea3e2894 727
seyhmus.cacina 0:ac4dea3e2894 728 }
seyhmus.cacina 0:ac4dea3e2894 729
seyhmus.cacina 0:ac4dea3e2894 730
seyhmus.cacina 0:ac4dea3e2894 731 //int sh_set_data_type( uint8_t outMode)
seyhmus.cacina 0:ac4dea3e2894 732 int sh_set_data_type(int data_type_, bool sc_en_)
seyhmus.cacina 0:ac4dea3e2894 733 {
seyhmus.cacina 0:ac4dea3e2894 734
seyhmus.cacina 0:ac4dea3e2894 735 #if 0
seyhmus.cacina 0:ac4dea3e2894 736 uint8_t dataTypeSc = (uint8_t)((sc_en ? SS_MASK_OUTPUTMODE_SC_EN : 0) | ((data_type << SS_SHIFT_OUTPUTMODE_DATATYPE) & SS_MASK_OUTPUTMODE_DATATYPE));
seyhmus.cacina 0:ac4dea3e2894 737 uint8_t ByteSeq[] = SH_SET_OUTPUT_MODE_CMDSEQ( dataTypeSc);
seyhmus.cacina 0:ac4dea3e2894 738 int status = sh_write_cmd( &ByteSeq[0],sizeof(ByteSeq), SS_DEFAULT_CMD_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 739 if( status == 0x00){
seyhmus.cacina 0:ac4dea3e2894 740 data_type = data_type_;
seyhmus.cacina 0:ac4dea3e2894 741 sc_en = sc_en_;
seyhmus.cacina 0:ac4dea3e2894 742 }
seyhmus.cacina 0:ac4dea3e2894 743 #endif
seyhmus.cacina 0:ac4dea3e2894 744
seyhmus.cacina 0:ac4dea3e2894 745 uint8_t cmd_bytes[] = { SS_FAM_W_COMMCHAN, SS_CMDIDX_OUTPUTMODE };
seyhmus.cacina 0:ac4dea3e2894 746 uint8_t data_bytes[] = { (uint8_t)((sc_en_ ? SS_MASK_OUTPUTMODE_SC_EN : 0) |
seyhmus.cacina 0:ac4dea3e2894 747 ((data_type_ << SS_SHIFT_OUTPUTMODE_DATATYPE) & SS_MASK_OUTPUTMODE_DATATYPE)) };
seyhmus.cacina 0:ac4dea3e2894 748
seyhmus.cacina 0:ac4dea3e2894 749 int status = sh_write_cmd_with_data(&cmd_bytes[0], sizeof(cmd_bytes),
seyhmus.cacina 0:ac4dea3e2894 750 &data_bytes[0], sizeof(data_bytes),
seyhmus.cacina 0:ac4dea3e2894 751 SS_DEFAULT_CMD_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 752 data_type = data_type_;
seyhmus.cacina 0:ac4dea3e2894 753 sc_en = sc_en_;
seyhmus.cacina 0:ac4dea3e2894 754
seyhmus.cacina 0:ac4dea3e2894 755 return status;
seyhmus.cacina 0:ac4dea3e2894 756 }
seyhmus.cacina 0:ac4dea3e2894 757
seyhmus.cacina 0:ac4dea3e2894 758
seyhmus.cacina 0:ac4dea3e2894 759 int sh_get_data_type(int *data_type_, bool *sc_en_){
seyhmus.cacina 0:ac4dea3e2894 760
seyhmus.cacina 0:ac4dea3e2894 761 uint8_t ByteSeq[] = SH_GET_OUTPUT_MODE_CMDSEQ;
seyhmus.cacina 0:ac4dea3e2894 762 uint8_t rxbuf[2] = {0};
seyhmus.cacina 0:ac4dea3e2894 763 int status = sh_read_cmd( &ByteSeq[0], sizeof(ByteSeq),
seyhmus.cacina 0:ac4dea3e2894 764 0, 0,
seyhmus.cacina 0:ac4dea3e2894 765 &rxbuf[0], sizeof(rxbuf),
seyhmus.cacina 0:ac4dea3e2894 766 SS_DEFAULT_CMD_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 767 if (status == 0x00 /*SS_SUCCESS*/) {
seyhmus.cacina 0:ac4dea3e2894 768 *data_type_ =
seyhmus.cacina 0:ac4dea3e2894 769 (rxbuf[1] & SS_MASK_OUTPUTMODE_DATATYPE) >> SS_SHIFT_OUTPUTMODE_DATATYPE;
seyhmus.cacina 0:ac4dea3e2894 770 *sc_en_ =
seyhmus.cacina 0:ac4dea3e2894 771 (bool)((rxbuf[1] & SS_MASK_OUTPUTMODE_SC_EN) >> SS_SHIFT_OUTPUTMODE_SC_EN);
seyhmus.cacina 0:ac4dea3e2894 772
seyhmus.cacina 0:ac4dea3e2894 773 }
seyhmus.cacina 0:ac4dea3e2894 774
seyhmus.cacina 0:ac4dea3e2894 775 return status;
seyhmus.cacina 0:ac4dea3e2894 776
seyhmus.cacina 0:ac4dea3e2894 777 }
seyhmus.cacina 0:ac4dea3e2894 778
seyhmus.cacina 0:ac4dea3e2894 779
seyhmus.cacina 0:ac4dea3e2894 780 int sh_set_fifo_thresh( int threshold ){
seyhmus.cacina 0:ac4dea3e2894 781
seyhmus.cacina 0:ac4dea3e2894 782 #if 0
seyhmus.cacina 0:ac4dea3e2894 783 uint8_t ucThresh = (uint8_t) (threshold & 0xFF);
seyhmus.cacina 0:ac4dea3e2894 784 uint8_t ByteSeq[] = SH_DFIFO_SET_INT_THRESHOLD_CMDSEQ(ucThresh );
seyhmus.cacina 0:ac4dea3e2894 785 int status = sh_write_cmd( &ByteSeq[0],sizeof(ByteSeq), SS_DEFAULT_CMD_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 786 return status;
seyhmus.cacina 0:ac4dea3e2894 787 #endif
seyhmus.cacina 0:ac4dea3e2894 788
seyhmus.cacina 0:ac4dea3e2894 789 uint8_t cmd_bytes[] = { SS_FAM_W_COMMCHAN, SS_CMDIDX_FIFOAFULL };
seyhmus.cacina 0:ac4dea3e2894 790 uint8_t data_bytes[] = { (uint8_t)threshold };
seyhmus.cacina 0:ac4dea3e2894 791
seyhmus.cacina 0:ac4dea3e2894 792 int status = sh_write_cmd_with_data(&cmd_bytes[0], sizeof(cmd_bytes),
seyhmus.cacina 0:ac4dea3e2894 793 &data_bytes[0], sizeof(data_bytes),
seyhmus.cacina 0:ac4dea3e2894 794 SS_DEFAULT_CMD_SLEEP_MS
seyhmus.cacina 0:ac4dea3e2894 795 );
seyhmus.cacina 0:ac4dea3e2894 796 return status;
seyhmus.cacina 0:ac4dea3e2894 797
seyhmus.cacina 0:ac4dea3e2894 798 }
seyhmus.cacina 0:ac4dea3e2894 799
seyhmus.cacina 0:ac4dea3e2894 800
seyhmus.cacina 0:ac4dea3e2894 801 int sh_get_fifo_thresh(int *thresh){
seyhmus.cacina 0:ac4dea3e2894 802
seyhmus.cacina 0:ac4dea3e2894 803 uint8_t ByteSeq[] = SH_DFIFO_GET_INT_THRESHOLD_CMDSEQ;
seyhmus.cacina 0:ac4dea3e2894 804 uint8_t rxbuf[2] = {0};
seyhmus.cacina 0:ac4dea3e2894 805 int status = sh_read_cmd(&ByteSeq[0], sizeof(ByteSeq),
seyhmus.cacina 0:ac4dea3e2894 806 0, 0,
seyhmus.cacina 0:ac4dea3e2894 807 &rxbuf[0], sizeof(rxbuf),
seyhmus.cacina 0:ac4dea3e2894 808 SS_DEFAULT_CMD_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 809
seyhmus.cacina 0:ac4dea3e2894 810 *thresh = (int) rxbuf[1];
seyhmus.cacina 0:ac4dea3e2894 811
seyhmus.cacina 0:ac4dea3e2894 812 return status;
seyhmus.cacina 0:ac4dea3e2894 813
seyhmus.cacina 0:ac4dea3e2894 814 }
seyhmus.cacina 0:ac4dea3e2894 815
seyhmus.cacina 0:ac4dea3e2894 816
seyhmus.cacina 0:ac4dea3e2894 817 int sh_ss_comm_check(void){
seyhmus.cacina 0:ac4dea3e2894 818
seyhmus.cacina 0:ac4dea3e2894 819
seyhmus.cacina 0:ac4dea3e2894 820 uint8_t ByteSeq[] = SH_COMM_CHECK_CMDSEQ;
seyhmus.cacina 0:ac4dea3e2894 821 uint8_t rxbuf[2];
seyhmus.cacina 0:ac4dea3e2894 822
seyhmus.cacina 0:ac4dea3e2894 823 int status = sh_read_cmd( &ByteSeq[0], sizeof(ByteSeq),
seyhmus.cacina 0:ac4dea3e2894 824 0, 0,
seyhmus.cacina 0:ac4dea3e2894 825 &rxbuf[0], sizeof(rxbuf),
seyhmus.cacina 0:ac4dea3e2894 826 SS_DEFAULT_CMD_SLEEP_MS );
seyhmus.cacina 0:ac4dea3e2894 827
seyhmus.cacina 0:ac4dea3e2894 828 int tries = 4;
seyhmus.cacina 0:ac4dea3e2894 829 while (status == SS_ERR_TRY_AGAIN && tries--) {
seyhmus.cacina 0:ac4dea3e2894 830 wait_ms(1000);
seyhmus.cacina 0:ac4dea3e2894 831 status = sh_read_cmd( &ByteSeq[0], sizeof(ByteSeq),
seyhmus.cacina 0:ac4dea3e2894 832 0, 0,
seyhmus.cacina 0:ac4dea3e2894 833 &rxbuf[0], sizeof(rxbuf),
seyhmus.cacina 0:ac4dea3e2894 834 SS_DEFAULT_CMD_SLEEP_MS );
seyhmus.cacina 0:ac4dea3e2894 835
seyhmus.cacina 0:ac4dea3e2894 836 }
seyhmus.cacina 0:ac4dea3e2894 837
seyhmus.cacina 0:ac4dea3e2894 838 return status;
seyhmus.cacina 0:ac4dea3e2894 839 }
seyhmus.cacina 0:ac4dea3e2894 840
seyhmus.cacina 0:ac4dea3e2894 841
seyhmus.cacina 0:ac4dea3e2894 842 int sh_num_avail_samples(int *numSamples) {
seyhmus.cacina 0:ac4dea3e2894 843
seyhmus.cacina 0:ac4dea3e2894 844 uint8_t ByteSeq[] = SH_DFIFO_GET_NSAMPLES_CMDSEQ;
seyhmus.cacina 0:ac4dea3e2894 845 uint8_t rxbuf[2] = {0};
seyhmus.cacina 0:ac4dea3e2894 846
seyhmus.cacina 0:ac4dea3e2894 847 int status = sh_read_cmd(&ByteSeq[0], sizeof(ByteSeq),
seyhmus.cacina 0:ac4dea3e2894 848 0, 0,
seyhmus.cacina 0:ac4dea3e2894 849 &rxbuf[0], sizeof(rxbuf),
seyhmus.cacina 0:ac4dea3e2894 850 1);
seyhmus.cacina 0:ac4dea3e2894 851
seyhmus.cacina 0:ac4dea3e2894 852 *numSamples = (int) rxbuf[1];
seyhmus.cacina 0:ac4dea3e2894 853
seyhmus.cacina 0:ac4dea3e2894 854 return status;
seyhmus.cacina 0:ac4dea3e2894 855 }
seyhmus.cacina 0:ac4dea3e2894 856
seyhmus.cacina 0:ac4dea3e2894 857
seyhmus.cacina 0:ac4dea3e2894 858 int sh_read_fifo_data( int numSamples,
seyhmus.cacina 0:ac4dea3e2894 859 int sampleSize,
seyhmus.cacina 0:ac4dea3e2894 860 uint8_t* databuf,
seyhmus.cacina 0:ac4dea3e2894 861 int databufSz) {
seyhmus.cacina 0:ac4dea3e2894 862
seyhmus.cacina 0:ac4dea3e2894 863 int bytes_to_read = numSamples * sampleSize + 1; //+1 for status byte
seyhmus.cacina 0:ac4dea3e2894 864
seyhmus.cacina 0:ac4dea3e2894 865 uint8_t ByteSeq[] = SH_DFIFO_PULL_SAMPLE_CMDSEQ;
seyhmus.cacina 0:ac4dea3e2894 866 int status = sh_read_cmd(&ByteSeq[0], sizeof(ByteSeq),
seyhmus.cacina 0:ac4dea3e2894 867 0, 0,
seyhmus.cacina 0:ac4dea3e2894 868 databuf, bytes_to_read,
seyhmus.cacina 0:ac4dea3e2894 869 10);
seyhmus.cacina 0:ac4dea3e2894 870
seyhmus.cacina 0:ac4dea3e2894 871 return status;
seyhmus.cacina 0:ac4dea3e2894 872 }
seyhmus.cacina 0:ac4dea3e2894 873
seyhmus.cacina 0:ac4dea3e2894 874
seyhmus.cacina 0:ac4dea3e2894 875 /*
seyhmus.cacina 0:ac4dea3e2894 876 * desc:
seyhmus.cacina 0:ac4dea3e2894 877 * func to read sample size for SmartSensor input FIFO for extrenal accel data
seyhmus.cacina 0:ac4dea3e2894 878 *
seyhmus.cacina 0:ac4dea3e2894 879 * params:
seyhmus.cacina 0:ac4dea3e2894 880 * __O sampSize: size of data sample struct in bytes
seyhmus.cacina 0:ac4dea3e2894 881 * returns:
seyhmus.cacina 0:ac4dea3e2894 882 * 1 byte status (SS_STATUS) : 0x00 (SS_SUCCESS) on success
seyhmus.cacina 0:ac4dea3e2894 883 *
seyhmus.cacina 0:ac4dea3e2894 884 **/
seyhmus.cacina 0:ac4dea3e2894 885 int sh_read_input_fifo_samplesz( int *sampSize){
seyhmus.cacina 0:ac4dea3e2894 886
seyhmus.cacina 0:ac4dea3e2894 887 /* NOT IMPLEMENTED IN SS INTERFACE */
seyhmus.cacina 0:ac4dea3e2894 888
seyhmus.cacina 0:ac4dea3e2894 889 }
seyhmus.cacina 0:ac4dea3e2894 890
seyhmus.cacina 0:ac4dea3e2894 891 /*
seyhmus.cacina 0:ac4dea3e2894 892 * desc:
seyhmus.cacina 0:ac4dea3e2894 893 * func to write data samples to SmartSensor input FIFO for extrenal accel data
seyhmus.cacina 0:ac4dea3e2894 894 *
seyhmus.cacina 0:ac4dea3e2894 895 * params:
seyhmus.cacina 0:ac4dea3e2894 896 ...
seyhmus.cacina 0:ac4dea3e2894 897 * returns:
seyhmus.cacina 0:ac4dea3e2894 898 * 1 byte status (SS_STATUS) : 0x00 (SS_SUCCESS) on success
seyhmus.cacina 0:ac4dea3e2894 899 */
seyhmus.cacina 0:ac4dea3e2894 900 int sh_write_input_fifo( void *arg){
seyhmus.cacina 0:ac4dea3e2894 901
seyhmus.cacina 0:ac4dea3e2894 902 /* NOT IMPLEMENTED IN SS INTERFACE */
seyhmus.cacina 0:ac4dea3e2894 903
seyhmus.cacina 0:ac4dea3e2894 904 }
seyhmus.cacina 0:ac4dea3e2894 905
seyhmus.cacina 0:ac4dea3e2894 906
seyhmus.cacina 0:ac4dea3e2894 907 int sh_set_reg(int idx, uint8_t addr, uint32_t val, int regSz){
seyhmus.cacina 0:ac4dea3e2894 908
seyhmus.cacina 0:ac4dea3e2894 909 uint8_t ByteSeq[] = SH_WRITE_SENSORREG_CMDSEQ( ((uint8_t)idx) , addr );
seyhmus.cacina 0:ac4dea3e2894 910 uint8_t data_bytes[4];
seyhmus.cacina 0:ac4dea3e2894 911 for (int i = 0; i < regSz; i++) {
seyhmus.cacina 0:ac4dea3e2894 912 data_bytes[i] = (val >> (8 * (regSz - 1)) & 0xFF);
seyhmus.cacina 0:ac4dea3e2894 913 }
seyhmus.cacina 0:ac4dea3e2894 914 int status = sh_write_cmd_with_data( &ByteSeq[0], sizeof(ByteSeq),
seyhmus.cacina 0:ac4dea3e2894 915 &data_bytes[0], (uint8_t) regSz,
seyhmus.cacina 0:ac4dea3e2894 916 SS_DEFAULT_CMD_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 917
seyhmus.cacina 0:ac4dea3e2894 918 return status;
seyhmus.cacina 0:ac4dea3e2894 919 }
seyhmus.cacina 0:ac4dea3e2894 920
seyhmus.cacina 0:ac4dea3e2894 921
seyhmus.cacina 0:ac4dea3e2894 922 int sh_get_reg(int idx, uint8_t addr, uint32_t *val){
seyhmus.cacina 0:ac4dea3e2894 923
seyhmus.cacina 0:ac4dea3e2894 924
seyhmus.cacina 0:ac4dea3e2894 925 uint32_t i32tmp;
seyhmus.cacina 0:ac4dea3e2894 926 uint8_t ByteSeq[] = SH_READ_AFE_ATTRIBUTES_CMDSEQ(((uint8_t) idx));
seyhmus.cacina 0:ac4dea3e2894 927 uint8_t rxbuf[3] = {0};
seyhmus.cacina 0:ac4dea3e2894 928
seyhmus.cacina 0:ac4dea3e2894 929 int status = sh_read_cmd(&ByteSeq[0], sizeof(ByteSeq),
seyhmus.cacina 0:ac4dea3e2894 930 0, 0,
seyhmus.cacina 0:ac4dea3e2894 931 &rxbuf[0], sizeof(rxbuf),
seyhmus.cacina 0:ac4dea3e2894 932 SS_DEFAULT_CMD_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 933
seyhmus.cacina 0:ac4dea3e2894 934
seyhmus.cacina 0:ac4dea3e2894 935 if(status == 0x00 /* SS_SUCCESS */) {
seyhmus.cacina 0:ac4dea3e2894 936
seyhmus.cacina 0:ac4dea3e2894 937 int reg_width = rxbuf[1];
seyhmus.cacina 0:ac4dea3e2894 938 uint8_t ByteSeq2[] = SH_READ_SENSORREG_CMDSEQ( ((uint8_t)idx) , addr );
seyhmus.cacina 0:ac4dea3e2894 939 uint8_t rxbuf2[5] = {0};
seyhmus.cacina 0:ac4dea3e2894 940 status = sh_read_cmd(&ByteSeq2[0], sizeof(ByteSeq2),
seyhmus.cacina 0:ac4dea3e2894 941 0, 0,
seyhmus.cacina 0:ac4dea3e2894 942 &rxbuf2[0], reg_width + 1,
seyhmus.cacina 0:ac4dea3e2894 943 SS_DEFAULT_CMD_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 944
seyhmus.cacina 0:ac4dea3e2894 945 if (status == 0x00 /* SS_SUCCESS */) {
seyhmus.cacina 0:ac4dea3e2894 946 i32tmp = 0;
seyhmus.cacina 0:ac4dea3e2894 947 for (int i = 0; i < reg_width; i++) {
seyhmus.cacina 0:ac4dea3e2894 948 i32tmp = (i32tmp << 8) | rxbuf2[i + 1];
seyhmus.cacina 0:ac4dea3e2894 949 }
seyhmus.cacina 0:ac4dea3e2894 950 *val = i32tmp;
seyhmus.cacina 0:ac4dea3e2894 951 }
seyhmus.cacina 0:ac4dea3e2894 952 }
seyhmus.cacina 0:ac4dea3e2894 953
seyhmus.cacina 0:ac4dea3e2894 954 return status;
seyhmus.cacina 0:ac4dea3e2894 955
seyhmus.cacina 0:ac4dea3e2894 956 }
seyhmus.cacina 0:ac4dea3e2894 957
seyhmus.cacina 0:ac4dea3e2894 958
seyhmus.cacina 0:ac4dea3e2894 959 int sh_sensor_enable( int idx , int sensorSampleSz , uint8_t ext_mode ){
seyhmus.cacina 0:ac4dea3e2894 960
seyhmus.cacina 0:ac4dea3e2894 961 uint8_t ByteSeq[] = SH_ENABLE_SENSOR_CMDSEQ( ((uint8_t) idx) , ((uint8_t) ext_mode));
seyhmus.cacina 0:ac4dea3e2894 962 int status = sh_write_cmd( &ByteSeq[0],sizeof(ByteSeq), 5 * SS_ENABLE_SENSOR_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 963 if(status == 0x00){
seyhmus.cacina 0:ac4dea3e2894 964
seyhmus.cacina 0:ac4dea3e2894 965 is_sensor_enabled[idx] = ENABLED;
seyhmus.cacina 0:ac4dea3e2894 966 sensor_sample_sz[idx] = sensorSampleSz;
seyhmus.cacina 0:ac4dea3e2894 967 }
seyhmus.cacina 0:ac4dea3e2894 968 return status;
seyhmus.cacina 0:ac4dea3e2894 969
seyhmus.cacina 0:ac4dea3e2894 970 }
seyhmus.cacina 0:ac4dea3e2894 971
seyhmus.cacina 0:ac4dea3e2894 972
seyhmus.cacina 0:ac4dea3e2894 973 int sh_sensor_disable( int idx ){
seyhmus.cacina 0:ac4dea3e2894 974
seyhmus.cacina 0:ac4dea3e2894 975 uint8_t ByteSeq[] = SH_DISABLE_SENSOR_CMDSEQ( ((uint8_t) idx));
seyhmus.cacina 0:ac4dea3e2894 976 int status = sh_write_cmd( &ByteSeq[0],sizeof(ByteSeq), SS_ENABLE_SENSOR_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 977 if(status == 0x00){
seyhmus.cacina 0:ac4dea3e2894 978
seyhmus.cacina 0:ac4dea3e2894 979 is_sensor_enabled[idx] = DISABLED;
seyhmus.cacina 0:ac4dea3e2894 980 }
seyhmus.cacina 0:ac4dea3e2894 981 return status;
seyhmus.cacina 0:ac4dea3e2894 982
seyhmus.cacina 0:ac4dea3e2894 983 }
seyhmus.cacina 0:ac4dea3e2894 984
seyhmus.cacina 0:ac4dea3e2894 985
seyhmus.cacina 0:ac4dea3e2894 986 int sh_get_input_fifo_size(int *fifo_size)
seyhmus.cacina 0:ac4dea3e2894 987 {
seyhmus.cacina 0:ac4dea3e2894 988
seyhmus.cacina 0:ac4dea3e2894 989 uint8_t ByteSeq[] = SH_GET_EXTINPUT_FIFOSZ_CMDSEQ;
seyhmus.cacina 0:ac4dea3e2894 990 uint8_t rxbuf[3]; /* status + fifo size */
seyhmus.cacina 0:ac4dea3e2894 991
seyhmus.cacina 0:ac4dea3e2894 992
seyhmus.cacina 0:ac4dea3e2894 993 int status = sh_read_cmd(&ByteSeq[0], sizeof(ByteSeq),
seyhmus.cacina 0:ac4dea3e2894 994 0, 0,
seyhmus.cacina 0:ac4dea3e2894 995 rxbuf, sizeof(rxbuf), 2*SS_DEFAULT_CMD_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 996
seyhmus.cacina 0:ac4dea3e2894 997 *fifo_size = rxbuf[1] << 8 | rxbuf[2];
seyhmus.cacina 0:ac4dea3e2894 998 return status;
seyhmus.cacina 0:ac4dea3e2894 999 }
seyhmus.cacina 0:ac4dea3e2894 1000
seyhmus.cacina 0:ac4dea3e2894 1001
seyhmus.cacina 0:ac4dea3e2894 1002 int sh_feed_to_input_fifo(uint8_t *tx_buf, int tx_buf_sz, int *nb_written)
seyhmus.cacina 0:ac4dea3e2894 1003 {
seyhmus.cacina 0:ac4dea3e2894 1004 int status;
seyhmus.cacina 0:ac4dea3e2894 1005
seyhmus.cacina 0:ac4dea3e2894 1006 uint8_t ByteSeq[] = SH_FEED_TO_INPUTFIFO_CMDSEQ;
seyhmus.cacina 0:ac4dea3e2894 1007 uint8_t rxbuf[3];
seyhmus.cacina 0:ac4dea3e2894 1008
seyhmus.cacina 0:ac4dea3e2894 1009 tx_buf[0] = 0x14;
seyhmus.cacina 0:ac4dea3e2894 1010 tx_buf[1] = 0x00;
seyhmus.cacina 0:ac4dea3e2894 1011
seyhmus.cacina 0:ac4dea3e2894 1012 status= sh_read_cmd(tx_buf, tx_buf_sz,
seyhmus.cacina 0:ac4dea3e2894 1013 0, 0,
seyhmus.cacina 0:ac4dea3e2894 1014 rxbuf, sizeof(rxbuf), SS_FEEDFIFO_CMD_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 1015
seyhmus.cacina 0:ac4dea3e2894 1016 *nb_written = rxbuf[1] * 256 + rxbuf[2];
seyhmus.cacina 0:ac4dea3e2894 1017 return status;
seyhmus.cacina 0:ac4dea3e2894 1018 }
seyhmus.cacina 0:ac4dea3e2894 1019
seyhmus.cacina 0:ac4dea3e2894 1020
seyhmus.cacina 0:ac4dea3e2894 1021 int sh_get_num_bytes_in_input_fifo(int *fifo_size)
seyhmus.cacina 0:ac4dea3e2894 1022 {
seyhmus.cacina 0:ac4dea3e2894 1023
seyhmus.cacina 0:ac4dea3e2894 1024 uint8_t ByteSeq[] = SH_GET_SAMPLEBYTECNT_INPUTFIFO_CMDSEQ;
seyhmus.cacina 0:ac4dea3e2894 1025 uint8_t rxbuf[3]; /* status + fifo size */
seyhmus.cacina 0:ac4dea3e2894 1026
seyhmus.cacina 0:ac4dea3e2894 1027
seyhmus.cacina 0:ac4dea3e2894 1028 int status = sh_read_cmd(&ByteSeq[0], sizeof(ByteSeq),
seyhmus.cacina 0:ac4dea3e2894 1029 0, 0,
seyhmus.cacina 0:ac4dea3e2894 1030 rxbuf, sizeof(rxbuf),
seyhmus.cacina 0:ac4dea3e2894 1031 2*SS_DEFAULT_CMD_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 1032
seyhmus.cacina 0:ac4dea3e2894 1033 *fifo_size = rxbuf[1] << 8 | rxbuf[2];
seyhmus.cacina 0:ac4dea3e2894 1034 return status;
seyhmus.cacina 0:ac4dea3e2894 1035 }
seyhmus.cacina 0:ac4dea3e2894 1036
seyhmus.cacina 0:ac4dea3e2894 1037
seyhmus.cacina 0:ac4dea3e2894 1038 /*
seyhmus.cacina 0:ac4dea3e2894 1039 * ALGARITIM RELATED FUNCTIONS :)
seyhmus.cacina 0:ac4dea3e2894 1040 *
seyhmus.cacina 0:ac4dea3e2894 1041 *
seyhmus.cacina 0:ac4dea3e2894 1042 *
seyhmus.cacina 0:ac4dea3e2894 1043 *
seyhmus.cacina 0:ac4dea3e2894 1044 *
seyhmus.cacina 0:ac4dea3e2894 1045 * */
seyhmus.cacina 0:ac4dea3e2894 1046
seyhmus.cacina 0:ac4dea3e2894 1047
seyhmus.cacina 0:ac4dea3e2894 1048 int sh_enable_algo(int idx , int algoSampleSz){
seyhmus.cacina 0:ac4dea3e2894 1049
seyhmus.cacina 0:ac4dea3e2894 1050 uint8_t ByteSeq[] = SH_ENABLE_ALGO_CMDSEQ( ((uint8_t) idx) );
seyhmus.cacina 0:ac4dea3e2894 1051 int status = sh_write_cmd( &ByteSeq[0],sizeof(ByteSeq), 25 * SS_ENABLE_SENSOR_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 1052 if(status == 0x00){
seyhmus.cacina 0:ac4dea3e2894 1053
seyhmus.cacina 0:ac4dea3e2894 1054 is_algo_enabled[idx] = ENABLED;
seyhmus.cacina 0:ac4dea3e2894 1055 algo_sample_sz[idx] = algoSampleSz;
seyhmus.cacina 0:ac4dea3e2894 1056 }
seyhmus.cacina 0:ac4dea3e2894 1057 return status;
seyhmus.cacina 0:ac4dea3e2894 1058
seyhmus.cacina 0:ac4dea3e2894 1059 }
seyhmus.cacina 0:ac4dea3e2894 1060
seyhmus.cacina 0:ac4dea3e2894 1061
seyhmus.cacina 0:ac4dea3e2894 1062 int sh_enable_algo_withmode(int idx, int mode, int algoSampleSz)
seyhmus.cacina 0:ac4dea3e2894 1063 {
seyhmus.cacina 0:ac4dea3e2894 1064
seyhmus.cacina 0:ac4dea3e2894 1065 uint8_t cmd_bytes[] = { SS_FAM_W_ALGOMODE, (uint8_t)idx, (uint8_t)mode };
seyhmus.cacina 0:ac4dea3e2894 1066
seyhmus.cacina 0:ac4dea3e2894 1067 int status = sh_write_cmd_with_data(&cmd_bytes[0], sizeof(cmd_bytes), 0, 0, 25 * SS_ENABLE_SENSOR_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 1068
seyhmus.cacina 0:ac4dea3e2894 1069 if (status == SS_SUCCESS) {
seyhmus.cacina 0:ac4dea3e2894 1070 is_algo_enabled[idx] = ENABLED;
seyhmus.cacina 0:ac4dea3e2894 1071 algo_sample_sz[idx] = algoSampleSz;
seyhmus.cacina 0:ac4dea3e2894 1072 enabled_algo_mode[idx] = mode;
seyhmus.cacina 0:ac4dea3e2894 1073 }
seyhmus.cacina 0:ac4dea3e2894 1074
seyhmus.cacina 0:ac4dea3e2894 1075 return status;
seyhmus.cacina 0:ac4dea3e2894 1076 }
seyhmus.cacina 0:ac4dea3e2894 1077
seyhmus.cacina 0:ac4dea3e2894 1078
seyhmus.cacina 0:ac4dea3e2894 1079
seyhmus.cacina 0:ac4dea3e2894 1080 int sh_disable_algo(int idx){
seyhmus.cacina 0:ac4dea3e2894 1081
seyhmus.cacina 0:ac4dea3e2894 1082 uint8_t ByteSeq[] = SH_DISABLE_ALGO_CMDSEQ( ((uint8_t) idx) );
seyhmus.cacina 0:ac4dea3e2894 1083 int status = sh_write_cmd( &ByteSeq[0],sizeof(ByteSeq), SS_ENABLE_SENSOR_SLEEP_MS );
seyhmus.cacina 0:ac4dea3e2894 1084 if(status == 0x00){
seyhmus.cacina 0:ac4dea3e2894 1085
seyhmus.cacina 0:ac4dea3e2894 1086 is_algo_enabled[idx] = DISABLED;
seyhmus.cacina 0:ac4dea3e2894 1087 }
seyhmus.cacina 0:ac4dea3e2894 1088 return status;
seyhmus.cacina 0:ac4dea3e2894 1089
seyhmus.cacina 0:ac4dea3e2894 1090 }
seyhmus.cacina 0:ac4dea3e2894 1091
seyhmus.cacina 0:ac4dea3e2894 1092
seyhmus.cacina 0:ac4dea3e2894 1093 int sh_set_algo_cfg(int algo_idx, int cfg_idx, uint8_t *cfg, int cfg_sz){
seyhmus.cacina 0:ac4dea3e2894 1094
seyhmus.cacina 0:ac4dea3e2894 1095 uint8_t ByteSeq[] = SH_SET_ALGO_CONFIGURATION_CMDSEQ( ((uint8_t) algo_idx) , ((uint8_t) cfg_idx) );
seyhmus.cacina 0:ac4dea3e2894 1096 int status = sh_write_cmd_with_data( &ByteSeq[0], sizeof(ByteSeq),
seyhmus.cacina 0:ac4dea3e2894 1097 cfg, cfg_sz,
seyhmus.cacina 0:ac4dea3e2894 1098 SS_DEFAULT_CMD_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 1099
seyhmus.cacina 0:ac4dea3e2894 1100 return status;
seyhmus.cacina 0:ac4dea3e2894 1101
seyhmus.cacina 0:ac4dea3e2894 1102 }
seyhmus.cacina 0:ac4dea3e2894 1103
seyhmus.cacina 0:ac4dea3e2894 1104
seyhmus.cacina 0:ac4dea3e2894 1105 int sh_get_algo_cfg(int algo_idx, int cfg_idx, uint8_t *cfg, int cfg_sz){
seyhmus.cacina 0:ac4dea3e2894 1106
seyhmus.cacina 0:ac4dea3e2894 1107 uint8_t ByteSeq[] = SH_GET_ALGO_CONFIGURATION_CMDSEQ( ((uint8_t) algo_idx) , ((uint8_t) cfg_idx) );
seyhmus.cacina 0:ac4dea3e2894 1108 int status = sh_read_cmd(&ByteSeq[0], sizeof(ByteSeq),
seyhmus.cacina 0:ac4dea3e2894 1109 0, 0,
seyhmus.cacina 0:ac4dea3e2894 1110 cfg, cfg_sz,
seyhmus.cacina 0:ac4dea3e2894 1111 SS_DEFAULT_CMD_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 1112 return status;
seyhmus.cacina 0:ac4dea3e2894 1113
seyhmus.cacina 0:ac4dea3e2894 1114 }
seyhmus.cacina 0:ac4dea3e2894 1115
seyhmus.cacina 0:ac4dea3e2894 1116
seyhmus.cacina 0:ac4dea3e2894 1117 /*
seyhmus.cacina 0:ac4dea3e2894 1118 * desc:
seyhmus.cacina 0:ac4dea3e2894 1119 * func to get active cumulative sample size of sensor hub in order to
seyhmus.cacina 0:ac4dea3e2894 1120 * calculate number of bytes to be read from sensor hub report data buffer
seyhmus.cacina 0:ac4dea3e2894 1121 *
seyhmus.cacina 0:ac4dea3e2894 1122 * params:
seyhmus.cacina 0:ac4dea3e2894 1123 * __I data_type : active data type of sensor hub -> no data :0 (SS_DATATYPE_PAUSE)
seyhmus.cacina 0:ac4dea3e2894 1124 * raw sensor data only :1 (SS_DATATYPE_RAW)
seyhmus.cacina 0:ac4dea3e2894 1125 * algo data only :2 (SS_DATATYPE_ALGO)
seyhmus.cacina 0:ac4dea3e2894 1126 * algo+raw data :3 (SS_DATATYPE_BOTH)
seyhmus.cacina 0:ac4dea3e2894 1127 * __O sample_size : calculated active cumulative sample size
seyhmus.cacina 0:ac4dea3e2894 1128
seyhmus.cacina 0:ac4dea3e2894 1129 * returns:
seyhmus.cacina 0:ac4dea3e2894 1130 * N/A
seyhmus.cacina 0:ac4dea3e2894 1131 *
seyhmus.cacina 0:ac4dea3e2894 1132 **/
seyhmus.cacina 0:ac4dea3e2894 1133 static void fifo_sample_size(int data_type_, int *sample_size)
seyhmus.cacina 0:ac4dea3e2894 1134 {
seyhmus.cacina 0:ac4dea3e2894 1135
seyhmus.cacina 0:ac4dea3e2894 1136 int tmpSz = 0;
seyhmus.cacina 0:ac4dea3e2894 1137 //*sample_size = 0;
seyhmus.cacina 0:ac4dea3e2894 1138
seyhmus.cacina 0:ac4dea3e2894 1139 if (data_type_ == SS_DATATYPE_RAW || data_type_ == SS_DATATYPE_BOTH) {
seyhmus.cacina 0:ac4dea3e2894 1140 for (int i = 0; i < SS_MAX_SUPPORTED_SENSOR_NUM; i++) {
seyhmus.cacina 0:ac4dea3e2894 1141 if (is_sensor_enabled[i]) {
seyhmus.cacina 0:ac4dea3e2894 1142 tmpSz += sensor_sample_sz[i];
seyhmus.cacina 0:ac4dea3e2894 1143 //*sample_size += sensor_data_reqs[i]->data_size;
seyhmus.cacina 0:ac4dea3e2894 1144 }
seyhmus.cacina 0:ac4dea3e2894 1145 }
seyhmus.cacina 0:ac4dea3e2894 1146 }
seyhmus.cacina 0:ac4dea3e2894 1147
seyhmus.cacina 0:ac4dea3e2894 1148 if (data_type_ == SS_DATATYPE_ALGO || data_type_ == SS_DATATYPE_BOTH) {
seyhmus.cacina 0:ac4dea3e2894 1149 for (int i = 0; i < SS_MAX_SUPPORTED_ALGO_NUM; i++) {
seyhmus.cacina 0:ac4dea3e2894 1150 if (is_algo_enabled[i]) {
seyhmus.cacina 0:ac4dea3e2894 1151 tmpSz += algo_sample_sz[i];
seyhmus.cacina 0:ac4dea3e2894 1152 //*sample_size += algo_data_reqs[i]->data_size;
seyhmus.cacina 0:ac4dea3e2894 1153 }
seyhmus.cacina 0:ac4dea3e2894 1154 }
seyhmus.cacina 0:ac4dea3e2894 1155 }
seyhmus.cacina 0:ac4dea3e2894 1156
seyhmus.cacina 0:ac4dea3e2894 1157 *sample_size = tmpSz;
seyhmus.cacina 0:ac4dea3e2894 1158 }
seyhmus.cacina 0:ac4dea3e2894 1159
seyhmus.cacina 0:ac4dea3e2894 1160
seyhmus.cacina 0:ac4dea3e2894 1161 int sh_ss_execute_once( uint8_t *databuf , int databufLen , int *nSamplesRead){
seyhmus.cacina 0:ac4dea3e2894 1162
seyhmus.cacina 0:ac4dea3e2894 1163 if(m_irq_received_ == false) {
seyhmus.cacina 0:ac4dea3e2894 1164 *nSamplesRead = 0;
seyhmus.cacina 0:ac4dea3e2894 1165 return -1;
seyhmus.cacina 0:ac4dea3e2894 1166 }
seyhmus.cacina 0:ac4dea3e2894 1167
seyhmus.cacina 0:ac4dea3e2894 1168 uint8_t sample_count;
seyhmus.cacina 0:ac4dea3e2894 1169
seyhmus.cacina 0:ac4dea3e2894 1170 sh_disable_irq_mfioevent();
seyhmus.cacina 0:ac4dea3e2894 1171 sh_clear_mfio_event_flag();
seyhmus.cacina 0:ac4dea3e2894 1172
seyhmus.cacina 0:ac4dea3e2894 1173 uint8_t hubStatus = 0;
seyhmus.cacina 0:ac4dea3e2894 1174 int status = sh_get_sensorhub_status(&hubStatus);
seyhmus.cacina 0:ac4dea3e2894 1175 if(status != 0x00 /*SS_SUCCESS*/){
seyhmus.cacina 0:ac4dea3e2894 1176 *nSamplesRead = 0;
seyhmus.cacina 0:ac4dea3e2894 1177 sh_enable_irq_mfioevent();
seyhmus.cacina 0:ac4dea3e2894 1178 return status;
seyhmus.cacina 0:ac4dea3e2894 1179 }
seyhmus.cacina 0:ac4dea3e2894 1180
seyhmus.cacina 0:ac4dea3e2894 1181 if (hubStatus & SS_MASK_STATUS_DATA_RDY) {
seyhmus.cacina 0:ac4dea3e2894 1182
seyhmus.cacina 0:ac4dea3e2894 1183 int num_samples = 1;
seyhmus.cacina 0:ac4dea3e2894 1184 status = sh_num_avail_samples(&num_samples);
seyhmus.cacina 0:ac4dea3e2894 1185 if (status != 0x00 /*SS_SUCCESS*/){
seyhmus.cacina 0:ac4dea3e2894 1186 *nSamplesRead = 0;
seyhmus.cacina 0:ac4dea3e2894 1187 sh_enable_irq_mfioevent();
seyhmus.cacina 0:ac4dea3e2894 1188 return status;
seyhmus.cacina 0:ac4dea3e2894 1189 }
seyhmus.cacina 0:ac4dea3e2894 1190
seyhmus.cacina 0:ac4dea3e2894 1191
seyhmus.cacina 0:ac4dea3e2894 1192 int sample_size;
seyhmus.cacina 0:ac4dea3e2894 1193 fifo_sample_size(data_type, &sample_size);
seyhmus.cacina 0:ac4dea3e2894 1194 /*DEBUG *///
seyhmus.cacina 0:ac4dea3e2894 1195
seyhmus.cacina 0:ac4dea3e2894 1196
seyhmus.cacina 0:ac4dea3e2894 1197 int bytes_to_read = num_samples * sample_size + 1; //+1 for status byte
seyhmus.cacina 0:ac4dea3e2894 1198 if ( bytes_to_read > databufLen) {
seyhmus.cacina 0:ac4dea3e2894 1199 //Reduce number of samples to read to fit in buffer
seyhmus.cacina 0:ac4dea3e2894 1200 num_samples = (databufLen - 1) / sample_size;
seyhmus.cacina 0:ac4dea3e2894 1201 }
seyhmus.cacina 0:ac4dea3e2894 1202
seyhmus.cacina 0:ac4dea3e2894 1203
seyhmus.cacina 0:ac4dea3e2894 1204 wait_ms(5);
seyhmus.cacina 0:ac4dea3e2894 1205 status = sh_read_fifo_data(num_samples, sample_size, &databuf[0], databufLen);
seyhmus.cacina 0:ac4dea3e2894 1206 if(status != 0x00 /*SS_SUCCESS*/){
seyhmus.cacina 0:ac4dea3e2894 1207 *nSamplesRead = 0;
seyhmus.cacina 0:ac4dea3e2894 1208 sh_enable_irq_mfioevent();
seyhmus.cacina 0:ac4dea3e2894 1209 return status;
seyhmus.cacina 0:ac4dea3e2894 1210 }
seyhmus.cacina 0:ac4dea3e2894 1211 *nSamplesRead = num_samples;
seyhmus.cacina 0:ac4dea3e2894 1212 }
seyhmus.cacina 0:ac4dea3e2894 1213
seyhmus.cacina 0:ac4dea3e2894 1214 sh_enable_irq_mfioevent();
seyhmus.cacina 0:ac4dea3e2894 1215 return status;
seyhmus.cacina 0:ac4dea3e2894 1216 }
seyhmus.cacina 0:ac4dea3e2894 1217
seyhmus.cacina 0:ac4dea3e2894 1218
seyhmus.cacina 0:ac4dea3e2894 1219
seyhmus.cacina 0:ac4dea3e2894 1220 /*
seyhmus.cacina 0:ac4dea3e2894 1221 * BOOTLOADER RELATED FUNCTIONS
seyhmus.cacina 0:ac4dea3e2894 1222 *
seyhmus.cacina 0:ac4dea3e2894 1223 *
seyhmus.cacina 0:ac4dea3e2894 1224 * */
seyhmus.cacina 0:ac4dea3e2894 1225
seyhmus.cacina 0:ac4dea3e2894 1226 static const int aes_nonce_sz = 11;
seyhmus.cacina 0:ac4dea3e2894 1227 static const int aes_auth_sz = 16;
seyhmus.cacina 0:ac4dea3e2894 1228 static int bl_comm_delay_factor = 1;
seyhmus.cacina 0:ac4dea3e2894 1229
seyhmus.cacina 0:ac4dea3e2894 1230
seyhmus.cacina 0:ac4dea3e2894 1231
seyhmus.cacina 0:ac4dea3e2894 1232 int sh_set_bootloader_delayfactor(const int factor ) {
seyhmus.cacina 0:ac4dea3e2894 1233
seyhmus.cacina 0:ac4dea3e2894 1234 int status = -1;
seyhmus.cacina 0:ac4dea3e2894 1235 if( factor >= 1 && factor < 51){
seyhmus.cacina 0:ac4dea3e2894 1236 bl_comm_delay_factor = factor;
seyhmus.cacina 0:ac4dea3e2894 1237 status = 0x00;
seyhmus.cacina 0:ac4dea3e2894 1238 }
seyhmus.cacina 0:ac4dea3e2894 1239
seyhmus.cacina 0:ac4dea3e2894 1240 return status;
seyhmus.cacina 0:ac4dea3e2894 1241
seyhmus.cacina 0:ac4dea3e2894 1242 }
seyhmus.cacina 0:ac4dea3e2894 1243
seyhmus.cacina 0:ac4dea3e2894 1244 const int sh_get_bootloader_delayfactor(void){
seyhmus.cacina 0:ac4dea3e2894 1245
seyhmus.cacina 0:ac4dea3e2894 1246 return bl_comm_delay_factor;
seyhmus.cacina 0:ac4dea3e2894 1247 }
seyhmus.cacina 0:ac4dea3e2894 1248
seyhmus.cacina 0:ac4dea3e2894 1249 int sh_exit_from_bootloader(void)
seyhmus.cacina 0:ac4dea3e2894 1250 {
seyhmus.cacina 0:ac4dea3e2894 1251
seyhmus.cacina 0:ac4dea3e2894 1252 return sh_reset_to_main_app(); //sh_set_sensorhub_operating_mode(0x00);
seyhmus.cacina 0:ac4dea3e2894 1253 }
seyhmus.cacina 0:ac4dea3e2894 1254
seyhmus.cacina 0:ac4dea3e2894 1255 int sh_put_in_bootloader(void)
seyhmus.cacina 0:ac4dea3e2894 1256 {
seyhmus.cacina 0:ac4dea3e2894 1257 return sh_set_sensorhub_operating_mode( 0x08);
seyhmus.cacina 0:ac4dea3e2894 1258 }
seyhmus.cacina 0:ac4dea3e2894 1259
seyhmus.cacina 0:ac4dea3e2894 1260 int sh_checkif_bootldr_mode(void)
seyhmus.cacina 0:ac4dea3e2894 1261 {
seyhmus.cacina 0:ac4dea3e2894 1262 uint8_t hubMode;
seyhmus.cacina 0:ac4dea3e2894 1263 int status = sh_get_sensorhub_operating_mode(&hubMode);
seyhmus.cacina 0:ac4dea3e2894 1264 return (status != SS_SUCCESS)? -1:(hubMode & SS_MASK_MODE_BOOTLDR);
seyhmus.cacina 0:ac4dea3e2894 1265 }
seyhmus.cacina 0:ac4dea3e2894 1266
seyhmus.cacina 0:ac4dea3e2894 1267 int sh_get_bootloader_pagesz(int *pagesz){
seyhmus.cacina 0:ac4dea3e2894 1268
seyhmus.cacina 0:ac4dea3e2894 1269 //uint8_t ByteSeq[]= SH_GET_BOOTLDRPAGESIZE_CMDSEQ;
seyhmus.cacina 0:ac4dea3e2894 1270 uint8_t ByteSeq[]= { SS_FAM_R_BOOTLOADER, SS_CMDIDX_PAGESIZE };
seyhmus.cacina 0:ac4dea3e2894 1271 uint8_t rxbuf[3];
seyhmus.cacina 0:ac4dea3e2894 1272 int sz = 0;
seyhmus.cacina 0:ac4dea3e2894 1273
seyhmus.cacina 0:ac4dea3e2894 1274 int status = sh_read_cmd( &ByteSeq[0], sizeof(ByteSeq),
seyhmus.cacina 0:ac4dea3e2894 1275 0, 0,
seyhmus.cacina 0:ac4dea3e2894 1276 &rxbuf[0], sizeof(rxbuf),
seyhmus.cacina 0:ac4dea3e2894 1277 SS_DEFAULT_CMD_SLEEP_MS);
seyhmus.cacina 0:ac4dea3e2894 1278 if (status == 0x00) {
seyhmus.cacina 0:ac4dea3e2894 1279 //rxbuf holds page size in big-endian format
seyhmus.cacina 0:ac4dea3e2894 1280 sz = (256*(int)rxbuf[1]) + rxbuf[2];
seyhmus.cacina 0:ac4dea3e2894 1281 if(sz > BOOTLOADER_MAX_PAGE_SIZE ) {
seyhmus.cacina 0:ac4dea3e2894 1282 sz = -2;
seyhmus.cacina 0:ac4dea3e2894 1283 }
seyhmus.cacina 0:ac4dea3e2894 1284 }
seyhmus.cacina 0:ac4dea3e2894 1285
seyhmus.cacina 0:ac4dea3e2894 1286 *pagesz = sz;
seyhmus.cacina 0:ac4dea3e2894 1287
seyhmus.cacina 0:ac4dea3e2894 1288 return status;
seyhmus.cacina 0:ac4dea3e2894 1289
seyhmus.cacina 0:ac4dea3e2894 1290 }
seyhmus.cacina 0:ac4dea3e2894 1291
seyhmus.cacina 0:ac4dea3e2894 1292 int sh_set_bootloader_numberofpages(const int pageCount){
seyhmus.cacina 0:ac4dea3e2894 1293
seyhmus.cacina 0:ac4dea3e2894 1294 //uint8_t ByteSeq[] = SH_SET_BOOTLDRPAGECOUNT_CMDSEQ;
seyhmus.cacina 0:ac4dea3e2894 1295 uint8_t ByteSeq[] = { SS_FAM_W_BOOTLOADER, SS_CMDIDX_SETNUMPAGES };
seyhmus.cacina 0:ac4dea3e2894 1296 //num pages = 256*MSB + LSB
seyhmus.cacina 0:ac4dea3e2894 1297 uint8_t data_bytes[] = { (uint8_t)((pageCount >> 8) & 0xFF), (uint8_t)(pageCount & 0xFF) };
seyhmus.cacina 0:ac4dea3e2894 1298
seyhmus.cacina 0:ac4dea3e2894 1299 int status = sh_write_cmd_with_data(&ByteSeq[0], sizeof(ByteSeq),
seyhmus.cacina 0:ac4dea3e2894 1300 &data_bytes[0], sizeof(data_bytes),
seyhmus.cacina 0:ac4dea3e2894 1301 bl_comm_delay_factor * SS_DEFAULT_CMD_SLEEP_MS );
seyhmus.cacina 0:ac4dea3e2894 1302
seyhmus.cacina 0:ac4dea3e2894 1303 return status;
seyhmus.cacina 0:ac4dea3e2894 1304
seyhmus.cacina 0:ac4dea3e2894 1305 }
seyhmus.cacina 0:ac4dea3e2894 1306
seyhmus.cacina 0:ac4dea3e2894 1307 int sh_set_bootloader_iv(uint8_t iv_bytes[aes_nonce_sz]){
seyhmus.cacina 0:ac4dea3e2894 1308
seyhmus.cacina 0:ac4dea3e2894 1309 uint8_t ByteSeq[] = { SS_FAM_W_BOOTLOADER, SS_CMDIDX_SETIV };
seyhmus.cacina 0:ac4dea3e2894 1310 int status = sh_write_cmd_with_data( &ByteSeq[0], sizeof(ByteSeq),
seyhmus.cacina 0:ac4dea3e2894 1311 &iv_bytes[0], aes_nonce_sz /*sizeof(iv_bytes)*/,
seyhmus.cacina 0:ac4dea3e2894 1312 bl_comm_delay_factor * SS_DEFAULT_CMD_SLEEP_MS
seyhmus.cacina 0:ac4dea3e2894 1313 );
seyhmus.cacina 0:ac4dea3e2894 1314
seyhmus.cacina 0:ac4dea3e2894 1315 return status;
seyhmus.cacina 0:ac4dea3e2894 1316
seyhmus.cacina 0:ac4dea3e2894 1317 }
seyhmus.cacina 0:ac4dea3e2894 1318
seyhmus.cacina 0:ac4dea3e2894 1319
seyhmus.cacina 0:ac4dea3e2894 1320 int sh_set_bootloader_auth(uint8_t auth_bytes[aes_auth_sz]){
seyhmus.cacina 0:ac4dea3e2894 1321
seyhmus.cacina 0:ac4dea3e2894 1322 uint8_t ByteSeq[] = { SS_FAM_W_BOOTLOADER, SS_CMDIDX_SETAUTH };
seyhmus.cacina 0:ac4dea3e2894 1323 int status = sh_write_cmd_with_data( &ByteSeq[0], sizeof(ByteSeq),
seyhmus.cacina 0:ac4dea3e2894 1324 &auth_bytes[0], aes_auth_sz /*sizeof(auth_bytes)*/,
seyhmus.cacina 0:ac4dea3e2894 1325 bl_comm_delay_factor * SS_DEFAULT_CMD_SLEEP_MS
seyhmus.cacina 0:ac4dea3e2894 1326 );
seyhmus.cacina 0:ac4dea3e2894 1327
seyhmus.cacina 0:ac4dea3e2894 1328 return status;
seyhmus.cacina 0:ac4dea3e2894 1329
seyhmus.cacina 0:ac4dea3e2894 1330 }
seyhmus.cacina 0:ac4dea3e2894 1331
seyhmus.cacina 0:ac4dea3e2894 1332
seyhmus.cacina 0:ac4dea3e2894 1333 int sh_set_bootloader_erase(void){
seyhmus.cacina 0:ac4dea3e2894 1334
seyhmus.cacina 0:ac4dea3e2894 1335 uint8_t ByteSeq[] = { SS_FAM_W_BOOTLOADER, SS_CMDIDX_ERASE };
seyhmus.cacina 0:ac4dea3e2894 1336
seyhmus.cacina 0:ac4dea3e2894 1337 int status = sh_write_cmd_with_data(&ByteSeq[0], sizeof(ByteSeq),
seyhmus.cacina 0:ac4dea3e2894 1338 0, 0,
seyhmus.cacina 0:ac4dea3e2894 1339 bl_comm_delay_factor * SS_BOOTLOADER_ERASE_DELAY);
seyhmus.cacina 0:ac4dea3e2894 1340
seyhmus.cacina 0:ac4dea3e2894 1341 return status;
seyhmus.cacina 0:ac4dea3e2894 1342
seyhmus.cacina 0:ac4dea3e2894 1343 }
seyhmus.cacina 0:ac4dea3e2894 1344
seyhmus.cacina 0:ac4dea3e2894 1345
seyhmus.cacina 0:ac4dea3e2894 1346 int sh_bootloader_flashpage(uint8_t *flashDataPreceedByCmdBytes , const int page_size){
seyhmus.cacina 0:ac4dea3e2894 1347
seyhmus.cacina 0:ac4dea3e2894 1348 static const int flash_cmdbytes_len = 2;
seyhmus.cacina 0:ac4dea3e2894 1349 static const int check_bytes_len = 16;
seyhmus.cacina 0:ac4dea3e2894 1350 static const int page_write_time_ms = 200;
seyhmus.cacina 0:ac4dea3e2894 1351
seyhmus.cacina 0:ac4dea3e2894 1352 //static const uint8_t ByteSeq[] = { SS_FAM_W_BOOTLOADER, SS_CMDIDX_SENDPAGE };
seyhmus.cacina 0:ac4dea3e2894 1353 int status = -1;
seyhmus.cacina 0:ac4dea3e2894 1354
seyhmus.cacina 0:ac4dea3e2894 1355 if( (*flashDataPreceedByCmdBytes == SS_FAM_W_BOOTLOADER) && ( *(flashDataPreceedByCmdBytes+1) == SS_CMDIDX_SENDPAGE ) ) {
seyhmus.cacina 0:ac4dea3e2894 1356
seyhmus.cacina 0:ac4dea3e2894 1357 /* We do not use sh_write_cmd_with_data function because internal buffers of the function
seyhmus.cacina 0:ac4dea3e2894 1358 is limited to 512 bytes which does not support if flashing page size is bigger */
seyhmus.cacina 0:ac4dea3e2894 1359 status = sh_write_cmd(flashDataPreceedByCmdBytes, page_size + check_bytes_len + flash_cmdbytes_len, bl_comm_delay_factor * page_write_time_ms);
seyhmus.cacina 0:ac4dea3e2894 1360
seyhmus.cacina 0:ac4dea3e2894 1361 }
seyhmus.cacina 0:ac4dea3e2894 1362 return status;
seyhmus.cacina 0:ac4dea3e2894 1363
seyhmus.cacina 0:ac4dea3e2894 1364 }
seyhmus.cacina 0:ac4dea3e2894 1365
seyhmus.cacina 0:ac4dea3e2894 1366
seyhmus.cacina 0:ac4dea3e2894 1367 int sh_get_ss_fw_version(uint8_t *fwDesciptor , uint8_t *descSize)
seyhmus.cacina 0:ac4dea3e2894 1368 {
seyhmus.cacina 0:ac4dea3e2894 1369
seyhmus.cacina 0:ac4dea3e2894 1370 int status = -1;
seyhmus.cacina 0:ac4dea3e2894 1371 uint8_t cmd_bytes[2];
seyhmus.cacina 0:ac4dea3e2894 1372 uint8_t rxbuf[4];
seyhmus.cacina 0:ac4dea3e2894 1373
seyhmus.cacina 0:ac4dea3e2894 1374 int bootldr = in_bootldr_mode();
seyhmus.cacina 0:ac4dea3e2894 1375
seyhmus.cacina 0:ac4dea3e2894 1376 if (bootldr > 0) {
seyhmus.cacina 0:ac4dea3e2894 1377 cmd_bytes[0] = SS_FAM_R_BOOTLOADER;
seyhmus.cacina 0:ac4dea3e2894 1378 cmd_bytes[1] = SS_CMDIDX_BOOTFWVERSION;
seyhmus.cacina 0:ac4dea3e2894 1379 } else if (bootldr == 0) {
seyhmus.cacina 0:ac4dea3e2894 1380 cmd_bytes[0] = SS_FAM_R_IDENTITY;
seyhmus.cacina 0:ac4dea3e2894 1381 cmd_bytes[1] = SS_CMDIDX_FWVERSION;
seyhmus.cacina 0:ac4dea3e2894 1382 } else {
seyhmus.cacina 0:ac4dea3e2894 1383 return -1;
seyhmus.cacina 0:ac4dea3e2894 1384 }
seyhmus.cacina 0:ac4dea3e2894 1385
seyhmus.cacina 0:ac4dea3e2894 1386 status = sh_read_cmd( &cmd_bytes[0], sizeof(cmd_bytes),
seyhmus.cacina 0:ac4dea3e2894 1387 0, 0,
seyhmus.cacina 0:ac4dea3e2894 1388 &rxbuf[0], sizeof(rxbuf) ,
seyhmus.cacina 0:ac4dea3e2894 1389 SS_DEFAULT_CMD_SLEEP_MS );
seyhmus.cacina 0:ac4dea3e2894 1390
seyhmus.cacina 0:ac4dea3e2894 1391 if (status == 0x00 /*SS_SUCCESS*/) {
seyhmus.cacina 0:ac4dea3e2894 1392 *fwDesciptor = rxbuf[1];
seyhmus.cacina 0:ac4dea3e2894 1393 *(fwDesciptor + 1) = rxbuf[2];
seyhmus.cacina 0:ac4dea3e2894 1394 *(fwDesciptor + 2) = rxbuf[3];
seyhmus.cacina 0:ac4dea3e2894 1395 *descSize = 3;
seyhmus.cacina 0:ac4dea3e2894 1396 }else{
seyhmus.cacina 0:ac4dea3e2894 1397 *descSize = 0;
seyhmus.cacina 0:ac4dea3e2894 1398 }
seyhmus.cacina 0:ac4dea3e2894 1399
seyhmus.cacina 0:ac4dea3e2894 1400 return status;
seyhmus.cacina 0:ac4dea3e2894 1401
seyhmus.cacina 0:ac4dea3e2894 1402 }
seyhmus.cacina 0:ac4dea3e2894 1403
seyhmus.cacina 0:ac4dea3e2894 1404
seyhmus.cacina 0:ac4dea3e2894 1405 /*
seyhmus.cacina 0:ac4dea3e2894 1406 #ifdef __cplusplus
seyhmus.cacina 0:ac4dea3e2894 1407 }
seyhmus.cacina 0:ac4dea3e2894 1408 #endif
seyhmus.cacina 0:ac4dea3e2894 1409 */
seyhmus.cacina 0:ac4dea3e2894 1410
seyhmus.cacina 0:ac4dea3e2894 1411