This is sample code for interfacing ROHM's SENSORSHLD1-EVK-101 with Nordic Semiconductor's nRF51-DK Development Kit Host BTLE Board
Dependencies: BLE_API mbed nRF51822
Fork of Nordic_UART_TEMPLATE_ROHM by
Code Example for ROHM Mutli-Sensor Shield on the Nordic Semiconductor nRF51-DK
This code was written to be used with the Nordic Semiconductor nRF51-DK.
This is the basic example code for interfacing ROHM's Multi-sensor Shield Board onto this board.
Additional information about the ROHM MultiSensor Shield Board can be found at the following link: https://github.com/ROHMUSDC/ROHM_SensorPlatform_Multi-Sensor-Shield
For code example for the ROHM SENSORSHLD0-EVK-101, please see the following link: https://developer.mbed.org/teams/ROHMUSDC/code/Nordic_UART_TEMPLATE_ROHM/
Operation
Ultimately, this code will initialize all the sensors on the Multi-sensor shield board and then poll the sensors. The sensor data will then be returned to the BTLE COM port link and will be view-able on any BTLE enabled phone that can connect to the Nordic UART Application.
Supported ROHM Sensor Devices
- BDE0600G Temperature Sensor
- BM1383GLV Pressure Sensor
- BU52014 Hall Sensor
- ML8511 UV Sensor
- RPR-0521 ALS/PROX Sensor
- BH1745NUC Color Sensor
- KMX62 Accel/Mag Sensor
- KX122 Accel Sensor
- BM1422 MI Magnetometer Sensor
- KXG03 Gyro/Accel Sensor
Updates from SHLD0 to SHLD1
- Pressure Sensor Changes: Fixed Register Map Changes for BM1383AGLV, See Pressure Sensor Datasheet for more details - TEMP and PRES output switched
- Added new #ifdef section for Magnetometer
- Changed Gyro Device Address (7bit addr now 0x4F, not 0x4E)
Sensor Applicable Code Sections
- Added a Section in "Main" to act as initialization
- Added to the "Periodic Callback" to read sensor data and return to Phone/Host
Questions/Feedback
Please feel free to let us know any questions/feedback/comments/concerns on the ROHM shield implementation by contacting the following e-mail:
Diff: main.cpp
- Revision:
- 2:c7b9d588c80f
- Parent:
- 1:2c0ab5cd1a7f
- Child:
- 3:c3ee9d663fb8
--- a/main.cpp Sun Jul 19 23:14:07 2015 +0000 +++ b/main.cpp Wed Jul 22 01:05:56 2015 +0000 @@ -13,19 +13,38 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + + /* + * Added Functions for ROHM's Multi-Sensor Shield Board + * Supports the following Sensor Devices + * > BDE0600G Temperature Sensor + * > BM1383GLV Pressure Sensor + * > BU52014 Hall Sensor + * > ML8511 UV Sensor + * > RPR-0521 ALS/PROX Sensor + * > BH1745NUC Color Sensor + * > KMX62 Accel/Mag Sensor + * > KX122 Accel Sensor + * > KXG03 Gyro (Currently Unavailable as IC hasn't docked yet) + * + * New Code: + * Added a Section in "Main" to act as initialization + * Added to the "Periodic Callback" to read sensor data and return to Phone/Host + */ + -#define AnalogALS //BH1620 //Change 0: Remove this completely +//#define AnalogALS //BH1620 //Change 0: Remove this completely #define AnalogTemp //BDE0600 #define AnalogUV //ML8511 #define HallSensor //BU52011 //Change 1: Change to use GPIO for BU52014 -#define DigitalALS //BH1721 //Change 2: Remove This and add in the RPR-0521 +#define RPR0521 //RPR0521 //Change 2: Remove This and add in the RPR-0521 //Change 3: Add Code For BH1745, KX022, BM1383GLV, KMX62 - #include "mbed.h" #include "BLEDevice.h" #include "UARTService.h" #include "nrf_temp.h" +#include "I2C.h" #define MAX_REPLY_LEN (UARTService::BLE_UART_SERVICE_MAX_DATA_LEN) //Actually equal to 20 #define SENSOR_READ_INTERVAL_S (2.0F) @@ -50,29 +69,45 @@ I2C i2c(p30,p7); //Sensor Variables -AnalogIn BH1620_ALS(p1); +/* +AnalogIn BH1620_ALS(p1); //No Analog ALS on the shield uint16_t BH1620_ALS_value; float BH1620_output; +*/ -AnalogIn BDE0600_Temp(p2); +AnalogIn BDE0600_Temp(p3); //p2 on the prior evk, p3 on the shield uint16_t BDE0600_Temp_value; float BDE0600_output; -AnalogIn ML8511_UV(p3); +AnalogIn ML8511_UV(p5); //p3 on prior EVK, p5 on the shield uint16_t ML8511_UV_value; float ML8511_output; -DigitalIn Hall_GPIO0(p28); -DigitalIn Hall_GPIO1(p29); +DigitalIn Hall_GPIO0(p14); // +DigitalIn Hall_GPIO1(p15); // int Hall_Return1; int Hall_Return0; -int ALS_addr_w = 0x46; //7bit addr = 0x23, with write bit 0 -int ALS_addr_r = 0x47; //7bit addr = 0x23, with read bit 1 -char ALS_PwrOn_cmd = 0x01; -char ALS_ContAuto_cmd = 0x10; -char ALS_ReturnData_raw[2]; -float ALS_Return = 0; +bool RepStart = true; +bool NoRepStart = false; + +#ifdef RPR0521 +int RPR0521_addr_w = 0x70; //7bit addr = 0x38, with write bit 0 +int RPR0521_addr_r = 0x71; //7bit addr = 0x38, with read bit 1 + +char RPR0521_ModeControl[2] = {0x41, 0xE6}; +char RPR0521_ALSPSControl[2] = {0x42, 0x03}; +char RPR0521_Persist[2] = {0x43, 0x20}; +char RPR0521_Addr_ReadData = 0x44; +char RPR0521_Content_ReadData[6]; + +int RPR0521_PS_RAWOUT = 0; +float RPR0521_PS_OUT = 0; +int RPR0521_ALS_D0_RAWOUT = 0; +int RPR0521_ALS_D1_RAWOUT = 0; +float RPR0521_ALS_DataRatio = 0; +float RPR0521_ALS_OUT = 0; +#endif /** * This callback is used whenever a disconnection occurs. @@ -117,10 +152,10 @@ len = snprintf((char*) buf, MAX_REPLY_LEN, "OK... LED OFF"); break; case 'a': - len = snprintf((char*) buf, MAX_REPLY_LEN, "ALSRaw = %d", BH1620_ALS_value); + //len = snprintf((char*) buf, MAX_REPLY_LEN, "ALSRaw = %d", BH1620_ALS_value); break; case 'b': - len = snprintf((char*) buf, MAX_REPLY_LEN, "ALS = %.2f lx", BH1620_output); + //len = snprintf((char*) buf, MAX_REPLY_LEN, "ALS = %.2f lx", BH1620_output); break; default: len = snprintf((char*) buf, MAX_REPLY_LEN, "ERROR"); @@ -157,8 +192,9 @@ uint8_t buf[MAX_REPLY_LEN]; uint32_t len = 0; - -#ifdef AnalogALS + +/* +#ifdef AnalogALS if (m_ble.getGapState().connected) { BH1620_ALS_value = BH1620_ALS.read_u16(); BH1620_output = (float)BH1620_ALS_value * 1.543; @@ -167,6 +203,7 @@ m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); } #endif +*/ #ifdef AnalogTemp if (m_ble.getGapState().connected) { @@ -212,14 +249,42 @@ } #endif - - +#ifdef RPR0521 + if (m_ble.getGapState().connected) { + + i2c.write(RPR0521_addr_w, &RPR0521_Addr_ReadData, 1, RepStart); + i2c.read(RPR0521_addr_r, &RPR0521_Content_ReadData[0], 6, NoRepStart); + + RPR0521_PS_RAWOUT = (RPR0521_Content_ReadData[1]<<8) | (RPR0521_Content_ReadData[0]); + RPR0521_ALS_D0_RAWOUT = (RPR0521_Content_ReadData[3]<<8) | (RPR0521_Content_ReadData[2]); + RPR0521_ALS_D1_RAWOUT = (RPR0521_Content_ReadData[5]<<8) | (RPR0521_Content_ReadData[4]); + RPR0521_ALS_DataRatio = (float)RPR0521_ALS_D1_RAWOUT / (float)RPR0521_ALS_D0_RAWOUT; + + if(RPR0521_ALS_DataRatio < 0.595){ + RPR0521_ALS_OUT = (1.682*(float)RPR0521_ALS_D0_RAWOUT - 1.877*(float)RPR0521_ALS_D1_RAWOUT); + } + else if(RPR0521_ALS_DataRatio < 1.015){ + RPR0521_ALS_OUT = (0.644*(float)RPR0521_ALS_D0_RAWOUT - 0.132*(float)RPR0521_ALS_D1_RAWOUT); + } + else if(RPR0521_ALS_DataRatio < 1.352){ + RPR0521_ALS_OUT = (0.756*(float)RPR0521_ALS_D0_RAWOUT - 0.243*(float)RPR0521_ALS_D1_RAWOUT); + } + else if(RPR0521_ALS_DataRatio < 3.053){ + RPR0521_ALS_OUT = (0.766*(float)RPR0521_ALS_D0_RAWOUT - 0.25*(float)RPR0521_ALS_D1_RAWOUT); + } + else{ + RPR0521_ALS_OUT = 0; + } + + len = snprintf((char*) buf, MAX_REPLY_LEN, "DALS= %0.2f lx", RPR0521_ALS_OUT); + m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); + } +#endif if (m_ble.getGapState().connected) { len = snprintf((char*) buf, MAX_REPLY_LEN, " "); //Print and Extra Line to show new data m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); } - } @@ -236,12 +301,15 @@ m_cmd_led = !m_cmd_led; + if (m_ble.getGapState().connected) { + /* BH1620_ALS_value = BH1620_ALS.read_u16(); BH1620_output = (float)BH1620_ALS_value * 1.543; len = snprintf((char*) buf, MAX_REPLY_LEN, "ALS = %.2f lx", BH1620_output); m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); + */ } } @@ -252,21 +320,26 @@ m_serial_port.baud(UART_BAUD_RATE); - DEBUG("Initialising\n\r"); + DEBUG("Initialising...\n\r"); m_cmd_led = 0; m_error_led = 0; - BH1620_ALS_value = 0; + //BH1620_ALS_value = 0; ticker.attach(periodicCallback, SENSOR_READ_INTERVAL_S); sw4Press.fall(&PBTrigger); -#ifdef DigitalALS - i2c.write(ALS_addr_w, &ALS_PwrOn_cmd, 1); - i2c.write(ALS_addr_w, &ALS_ContAuto_cmd, 1); +#ifdef RPR0521 + // 1. Mode Control (0x41), write (0xC6): ALS EN, PS EN, 100ms measurement for ALS and PS, PS_PULSE=1 + // 2. ALS_PS_CONTROL (0x42), write (0x03): LED Current = 200mA + // 3. PERSIST (0x43), write (0x20): PS Gain x4 + i2c.write(RPR0521_addr_w, &RPR0521_ModeControl[0], 2, false); + i2c.write(RPR0521_addr_w, &RPR0521_ALSPSControl[0], 2, false); + i2c.write(RPR0521_addr_w, &RPR0521_Persist[0], 2, false); #endif +//Start BTLE Initialization Section m_ble.init(); m_ble.onDisconnection(disconnectionCallback); m_ble.onDataWritten(dataWrittenCallback);