monoZ docs
MZ_i2c_example.c
Go to the documentation of this file.
1
8#include "MZ_sys_cmsis_os2.h"
9#include "MZ_i2c_example.h"
10#include "MZ_timer.h"
11#include "MZ_print.h"
12#include "MZ_i2c1_instance.h"
13#include "MZ_error_handler.h"
14#include "MZ_i2c.h"
15#include <stdio.h>
16#include <math.h>
17#include<stdlib.h>
18#include "stm32l4xx_hal_i2c.h"
19
20#define I2C_APP_STACK_SIZE (1024)
21#define I2C_TX_TIME (pdMS_TO_TICKS(60000))
22#define I2C_TX_SIZE (10)
23#define RESP_BUF_LEN 10
24#define SI_SENSOR_BUF_LEN 50
25
27static StaticTask_t i2c_cb_mem;
28static StackType_t i2c_stack[I2C_APP_STACK_SIZE];
36static mzUint8 locstr1[50];
42
48static void timer_cb(TimerHandle_t xTimer)
49{
50 mz_puts("Timer expires\r\n");
52}
53
60static void i2c_app(void * arg)
61{
62 (void)arg;
63
64 // create the timer and start when the starts if main RTOS scheduler started
66 {
67 __NOP(); // Timer create and start failed.
68 }
69 mz_puts("SENSOR Initialization...\r\n");
71 HAL_Delay(100);
72
73 while(1)
74 {
75 // process the read data from I2C
76 //[TODO] condition pending for sensor data read
77 SI_Read_UV();
78
79 }
80
81}
82
83/*
84 * @fn mz_error_t i2c_app_init(void)
85 * @brief creates the App thread and semaphore
86 * @return MZ_OK/MZ_FAIL
87 */
89{
90 mz_error_t _ret = MZ_OK;
91
92 // Initialize I2C 1
93 if(MZ_OK != I2C1_init())
94 return MZ_FAIL;
95
96 // Create the I2c application threads
98 "I2c Scheduler",
99 i2c_app,
100 NULL,
101 osPriorityNormal,
102 i2c_stack,
104 &i2c_cb_mem,
105 sizeof(i2c_cb_mem)))
106 {
108 }
109
110 // Create lock with already locked
111 if(!mz_sem_create(&i2c_sema_id,1U,1U))
112 {
113 _ret = MZ_SEMA_CREATE_FAIL;
114 }
115
116 return _ret;
117}
118
125mz_error_t I2C_Write_Reg(uint8_t addr, uint8_t data)
126{
127 mz_error_t _ret = MZ_OK;
128 uint16_t tlen;
129 tlen = 2;
130 uint8_t I2CLocBuf[5] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
131 I2CLocBuf[0] = SI_REG_RA(addr);
132 I2CLocBuf[1] = data;
133
134 if(MZ_I2C_Master_Transmit(MZ_I2C_INSTANCE,SI_SL_ADD,(uint8_t *) I2CLocBuf,tlen,1000)==MZ_OK)
135 {
136 _ret = 0xFF;
137 }
138 return _ret;
139}
140
141
148mz_error_t I2C_Read_Reg(uint8_t addr, uint32_t *buf)
149{
150 mz_error_t _ret = MZ_OK;
151 uint16_t tlen;
152 uint16_t dlen;
153 tlen = 1;
154 dlen = 2;
155 uint8_t I2CLocBuf[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
156 I2CLocBuf[0] = SI_REG_RA(addr);
157
158 if(MZ_I2C_Master_Transmit(MZ_I2C_INSTANCE,SI_SL_ADD,(uint8_t *) I2CLocBuf,tlen,1000)!=MZ_OK)
159 {
160 _ret = 0XFF;
161 }
162 if(MZ_I2C_Master_Receive(MZ_I2C_INSTANCE,SI_SL_ADD,(uint8_t *) buf,dlen,1000)!=MZ_OK)
163 {
164 _ret= 0XFE;
165 }
166 return _ret;
167}
168
176 {
177 mz_puts("Initialization of SENSOR FAILED\r\n");
178 }
179 osDelay(10);
180}
181
188mz_error_t param_table_write(uint8_t addr, uint8_t data)
189{
190 uint8_t i=0,reg_val;
191 mz_error_t _ret;
192 uint8_t pre_resp;
193
194 err_code=0;
195 write_sucess=0;
197 pre_resp=loc_buf[0];//20
198 resp_buf[0]=pre_resp;
199 pre_resp = pre_resp & 0x1F;
200 do{
202 reg_val=loc_buf[0];
203 }while((reg_val & 0x20) != 0x20);
204 //Do Write
206 addr = addr | 0x80;
208 osDelay(10);
209 do{
210
212 reg_val=loc_buf[0];
213 resp_buf[1]=reg_val;
214 if((reg_val & 0x1F) != pre_resp){
215 break;
216 }else{
217 //Response is NOT received
218 i++;
219 osDelay(10);
220 }
221 }while(i<5);
222 //Check for error code
223 if(reg_val & 0x10 != 0){
224 //Error received
225 _ret = MZ_FAIL;
226 err_code = reg_val & 0xF;
227 }else{
228 //Successful
229 //Read the resonse reg
231 reg_val=loc_buf[0];
232 resp_buf[2]=reg_val;
233 if(reg_val == data){
234 write_sucess=1;
235 _ret = MZ_OK;
236 }else{
237 _ret = MZ_FAIL;
238 }
239 }
240 return _ret;
241}
242
249{
250 mz_error_t _ret = MZ_OK;
251 osDelay(3000);
252 mz_puts("UV Sensor configuration started\r\n");
255 {
256 mz_puts("SENSOR_PARAM_CH_LIST CONFIG FAILED\r\n");
257 return MZ_FAIL;
258 }
259
261 {
262 mz_puts("SENSOR_PARAM_ADCCONFIG0 CONFIG FAILED\r\n");
263 return MZ_FAIL;
264 }
265
267 {
268 mz_puts("SENSOR_PARAM_ADCSENS0 CONFIG FAILED\r\n");
269 return MZ_FAIL;
270 }
271
273 {
274 mz_puts("SENSOR_PARAM_ADCPOST0 CONFIG FAILED\r\n");
275 return MZ_FAIL;
276 }
277
279 {
280 mz_puts("SENSOR_PARAM_ADCCONFIG1 CONFIG FAILED\r\n");
281 return MZ_FAIL;
282 }
283
285 {
286 mz_puts("SENSOR_PARAM_ADCSENS1 CONFIG FAILED\r\n");
287 return MZ_FAIL;
288 }
289
291 {
292 mz_puts("SENSOR_PARAM_ADCPOST1 CONFIG FAILED\r\n");
293 return MZ_FAIL;
294 }
295
297 {
298 mz_puts("SENSOR_PARAM_ADCCONFIG2 CONFIG FAILED\r\n");
299 return MZ_FAIL;
300 }
301
303 {
304 mz_puts("SENSOR_PARAM_ADCSENS2 CONFIG FAILED\r\n");
305 return MZ_FAIL;
306 }
307
309 {
310 mz_puts("SENSOR_PARAM_ADCPOST2 CONFIG FAILED\r\n");
311 return MZ_FAIL;
312 }
313
315 {
316 mz_puts("SENSOR_PARAM_ADCCONFIG3 CONFIG FAILED\r\n");
317 return MZ_FAIL;
318 }
319
321 {
322 mz_puts("SENSOR_PARAM_ADCSENS3 CONFIG FAILED\r\n");
323 return MZ_FAIL;
324 }
325
327 {
328 mz_puts("SENSOR_PARAM_ADCPOST3 CONFIG FAILED\r\n");
329 return MZ_FAIL;
330 }
331 mz_puts("UV Sensor configuration done\r\n");
332
333 return _ret;
334}
341mz_error_t Sensor_cmd_data_conf(uint8_t addr, uint8_t data)
342{
343 mz_error_t ret;
344 for(int i=0;i<2;i++){
345 ret=param_table_write(addr, data);
346 if(MZ_OK == ret){
347 break;
348 }
349 }
350}
351
359{
361 if(buf[0]==0x33){
363 }else{
364 mz_puts("WRONG CHIP ID FOR SENSOR. SKIPPING CONFIGURATION\r\n");
365 }
366
367}
368
375{
376 mz_error_t _ret = MZ_OK;
377 uint16_t tlen;
378 uint16_t dlen;
379 tlen = 1;
380 dlen = 2;
381 uint8_t I2CLocBuf[4] = { 0x00, 0x00, 0x00, 0x00 };
382 I2CLocBuf[0]= SI_ID_REG_PART_ID;
383 if(MZ_OK!= MZ_I2C_Master_Transmit(MZ_I2C_INSTANCE,SI_SL_ADD,(uint8_t *) I2CLocBuf,tlen,1000))
384 {
385 _ret = 0XFF;
386 }
387 if(MZ_OK != MZ_I2C_Master_Receive(MZ_I2C_INSTANCE,SI_SL_ADD,(uint8_t *) buf,dlen,1000))
388 {
389 _ret = 0XFE;
390 }
391 return _ret;
392}
393
401{
402 uint32_t uv_index_val;
403 float uvi;
404 uint16_t len;
405 uint8_t ch1_msb,ch1_lsb,ch1_middle_byte;
406 //single-shot measurement
407 mz_puts("UV Sensor single-shot measurement\r\n");
409
411 ch1_msb = i2cLocBuf[0];
413 ch1_middle_byte = i2cLocBuf[0];
415 ch1_lsb = i2cLocBuf[0];
416 uv_val = ch1_msb << 16;
417 uv_val = uv_val | ch1_middle_byte << 8;
418 uv_val = uv_val | ch1_lsb;
419
420 if(uv_val<0){
422 }
423 else if(uv_val>=0){
424// mz_puts("UV Index:\r\n");
425 mz_puts("UV Sensor cali\r\n");
426 /* Convert the readings to UV index */
427 uvi = (float) SENSOR_getUv(uv_val, uk );
428 uvi = uvi / ( 1 << UV_OUTPUT_FRACTION );
429 calib_uv_index(uvi);
430 if(uvi<10 && uvi>=0){
431 string_copy(locstr1,"0");
433
434 }else if(uvi<100 && uvi>10){
436 }
439 mz_puts("UV Sensor data got\r\n");
441 }
442
443}
444
452int32_t SENSOR_getUv( int32_t uv, SENSOR_Coeff_TypeDef *uk ){
453 int32_t uvi;
455 return uvi;
456}
457
467int32_t SENSOR_calcPolyInner( int32_t input, int8_t fraction, uint16_t mag, int8_t shift )
468 {
469
470 int32_t value;
471
472 if( shift < 0 ) {
473 value = ( ( input << fraction ) / mag ) >> -shift;
474 }
475 else {
476 value = ( ( input << fraction ) / mag ) << shift;
477 }
478
479 return value;
480
481 }
482
493int32_t SENSOR_calcEvalPoly( int32_t x, int32_t y, uint8_t input_fraction, uint8_t output_fraction, uint8_t num_coeff, SENSOR_Coeff_TypeDef *kp ){
494 uint8_t info, x_order, y_order, counter;
495 int8_t sign, shift;
496 uint16_t mag;
497 int32_t output = 0, x1, x2, y1, y2;
498 for( counter = 0; counter < num_coeff; counter++ ) {
499 info = kp->info;
500 x_order = get_x_order( info );
501 y_order = get_y_order( info );
502 shift = ( (uint16_t) kp->info & 0xff00 ) >> 8;
503 shift ^= 0x00ff;
504 shift += 1;
505 shift = -shift;
506 mag = kp->mag;
507 if( get_sign( info ) ) {
508 sign = -1;
509 }else{
510 sign = 1;
511 }
512 if( ( x_order == 0 ) && ( y_order == 0 ) ) {
513 output += sign * mag << output_fraction;
514 }else{
515 if( x_order > 0 ){
516 x1 = SENSOR_calcPolyInner( x, input_fraction, mag, shift );
517 if( x_order > 1 ) {
518 x2 = SENSOR_calcPolyInner( x, input_fraction, mag, shift );
519 }
520 else {
521 x2 = 1;
522 }
523 }
524 else {
525 x1 = 1;
526 x2 = 1;
527 }
528
529 if( y_order > 0 ) {
530 y1 = SENSOR_calcPolyInner( y, input_fraction, mag, shift );
531 if( y_order > 1 ) {
532 y2 = SENSOR_calcPolyInner( y, input_fraction, mag, shift );
533 }
534 else {
535 y2 = 1;
536 }
537 }
538 else {
539 y1 = 1;
540 y2 = 1;
541 }
542
543 output += sign * x1 * x2 * y1 * y2;
544 }
545
546 kp++;
547 }
548
549 if( output < 0 ) {
550 output = -output;
551 }
552 return output;
553 }
554
560void calib_uv_index(float uv_raw_data){
561
562 uint16_t i,j;
563 gcvt(uv_raw_data,10,locstr1);
564 i=0;
565 j=0;
566 while(locstr1[i] != '.' && locstr1[i] != '\0')
567 {
568 localBuf1[j] = locstr1[i];
569 i++;
570 j++;
571 }
572 localBuf1[j] = '\0';
573}
580uint16_t string_copy(uint8_t *dststr,uint8_t *srcstr){
581
582 uint16_t i=0;
583 while(srcstr[i]!='\0' && srcstr[i]!='\r'){
584 dststr[i]=srcstr[i];
585 i++;
586 }
587 dststr[i]='\0';
588 return i;
589}
596uint8_t string_append(uint8_t *srcstr,uint8_t *apstr){
597
598 uint16_t i=0;
599 uint16_t j=0;
600 while(srcstr[i]!='\0' && srcstr[i]!='\r'){
601 i++;
602 }
603 while(apstr[j]!='\0' && apstr[j]!='\r'){
604 srcstr[i]=apstr[j];
605 i++;
606 j++;
607 }
608 srcstr[i]='\0';
609 return i;
610}
611
617uint16_t Get_String_len(uint8_t *buf)
618{
619 uint16_t i=0,len;
620 while(buf[i]!='\r' && buf[i]!='\0'){
621 i++;
622 }
623 len=i;
624 return len;
625}
633void int8buf_to_asciibuf(uint8_t *destbuf,uint8_t *srcbuf,uint16_t len){
634 uint16_t i=0;
635 uint16_t j=0;
636 uint8_t dat=0;
637 uint8_t val=0;
638 for(i=0;i<len;i++){
639 dat = srcbuf[i];
640 dat=dat>>4;
641 val=dat & 0x0F; //dat%16;
642 if(val<0xA){
643 destbuf[j] = val + 48;
644 }else{
645 destbuf[j] = val + 55; //65-10;
646 }
647
648 j++;
649 dat = srcbuf[i];
650 val=dat & 0x0F; //dat%16;
651 if(val<0xA){
652 destbuf[j] = val + 48;
653 }else{
654 destbuf[j] = val + 55; //65-10;
655 }
656 j++;
657 }
658 destbuf[j]='\r';
659 j++;
660 destbuf[j]='\n';
661}
662
#define UV_INPUT_FRACTION
#define UV_NUMCOEFF
#define UV_OUTPUT_FRACTION
#define SLAVE_ADD
unsigned char mzUint8
1 Byte data type
Definition: MZ_common.h:57
unsigned long int mzUint32
4 Byte data type
Definition: MZ_common.h:95
monoZ Error Handler
mz_error_t
Enumeration of monoZ Error Flags.
@ MZ_SEMA_CREATE_FAIL
@ MZ_THREAD_CREATE_FAIL
@ MZ_OK
@ MZ_FAIL
mz_error_t I2C1_init(void)
#define MZ_I2C_INSTANCE
This file contains all the functionalities for I2C protocol.
static mzUint8 localBuf1[SI_SENSOR_BUF_LEN]
mz_error_t I2C_Read_Reg(uint8_t addr, uint32_t *buf)
Function to read from the specific register.
void int8buf_to_asciibuf(uint8_t *destbuf, uint8_t *srcbuf, uint16_t len)
Function to Conversion of Int to ascii data.
static mz_thread_t i2c_thread_id
uint16_t string_copy(uint8_t *dststr, uint8_t *srcstr)
Function to Copy the data from source to destination.
int32_t SENSOR_calcEvalPoly(int32_t x, int32_t y, uint8_t input_fraction, uint8_t output_fraction, uint8_t num_coeff, SENSOR_Coeff_TypeDef *kp)
Function to Conversion of Raw Data.
SISER_RCVD SI_Read_UV()
Function to read the data from SENSOR.
static mz_semaphore_t i2c_sema_id
#define SI_SENSOR_BUF_LEN
uint16_t Get_String_len(uint8_t *buf)
Function to get string length.
static mzUint8 loc_buf[RESP_BUF_LEN]
#define RESP_BUF_LEN
mz_error_t Sensor_cmd_data_conf(uint8_t addr, uint8_t data)
Function to write data for Sensor Configuration.
static mzUint8 err_code
#define I2C_TX_TIME
static StackType_t i2c_stack[I2C_APP_STACK_SIZE]
void reset_resp_counter()
Reset register.
mzUint32 uv_val
mz_error_t param_table_write(uint8_t addr, uint8_t data)
Function to read from the specific register.
#define I2C_TX_SIZE
mzUint8 buf[10]
int32_t SENSOR_calcPolyInner(int32_t input, int8_t fraction, uint16_t mag, int8_t shift)
Function to Conversion of Raw Data.
uint8_t string_append(uint8_t *srcstr, uint8_t *apstr)
Function to append the string.
mzUint32 als_val1
mz_error_t I2C_Write_Reg(uint8_t addr, uint8_t data)
Function to write into the specific register.
static mzUint8 send_data[I2C_TX_SIZE]
static mzUint8 write_sucess
mz_error_t Sensor_ReadID(uint8_t *buf)
Function to read the content from ID register of SENSOR.
static void timer_cb(TimerHandle_t xTimer)
Timer callback function for transmission of the data.
static StaticTask_t i2c_cb_mem
static mzUint8 i2cLocBuf[SI_SENSOR_BUF_LEN]
void calib_uv_index(float uv_raw_data)
Function to calibrate the raw data.
mzUint32 als_val3
mzUint32 als_val2
mz_error_t i2c_app_init(void)
#define I2C_APP_STACK_SIZE
void SENSOR_Init()
Function Sensor Initialisation.
int32_t SENSOR_getUv(int32_t uv, SENSOR_Coeff_TypeDef *uk)
Function to Conversion of Raw Data.
static void i2c_app(void *arg)
create the timer and starts if main RTOS scheduler started
static mzUint8 resp_buf[RESP_BUF_LEN]
mz_error_t SENSOR_Config()
Function to config the sensor.
static mzUint8 locstr1[50]
SISER_RCVD SI_resp_rcvd
mzUint8 cnt
#define SENSOR_REG_HOSTOUT1
#define SENSOR_PARAM_ADCCONFIG1
#define RESET_CMD_CTR
#define SENSOR_PARAM_ADCSENS1
#define SENSOR_PARAM_ADCSENS2
#define get_x_order(m)
#define SENSOR_PARAM_ADCCONFIG2
SISER_RCVD
@ SISER_NONE_RCVD
@ SISER_INVALID_RESP_RCVD
@ SISER_VALID_RESP_RCVD
PAYLOAD_ST SENSOR_payload
static SENSOR_Coeff_TypeDef uk[2]
#define SENSOR_PARAM_ADCPOST2
#define SENSOR_REG_RESPONSE0
#define SENSOR_PARAM_CH_LIST
#define SENSOR_PARAM_ADCCONFIG0
#define SI_REG_RA(x)
#define SENSOR_PARAM_ADCSENS0
#define SENSOR_REG_HOSTOUT2
#define SENSOR_REG_HOSTOUT0
#define SENSOR_PARAM_ADCPOST0
#define SENSOR_PARAM_ADCPOST1
#define SENSOR_PARAM_ADCPOST3
#define get_y_order(m)
#define SENSOR_REG_COMMAND
#define FORCE_CMD
#define SENSOR_REG_HOSTIN0
#define SENSOR_PARAM_ADCSENS3
#define SENSOR_PARAM_ADCCONFIG3
#define SENSOR_REG_RESPONSE1
#define SI_ID_REG_PART_ID
#define get_sign(m)
#define SI_SL_ADD
monoZ Print
monoZ system CMSIS OS2 This is an abstraction layer and includes FreeRTOS configuration,...
osSemaphoreId_t mz_semaphore_t
mz_semaphore_t variable of type osSemaphoreId_t
osThreadId_t mz_thread_t
mz_thread_t variable of type osThreadId_t
This file contains APIs for create,delete,start and stop of timers. Maximum 5 timers can be created....
mz_error_t MZ_I2C_Master_Receive(uint8_t i2c_no, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
This function is used to receive data using I2C in interrupt mode.
mz_error_t MZ_I2C_Master_Transmit_IT(uint8_t i2c_no, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
This function is used to transmit data using I2C in polling mode.
mz_error_t MZ_I2C_Master_Transmit(uint8_t i2c_no, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
This function is used to transmit data using I2C in interrupt mode.
int mz_puts(void *__ch)
This function prints the given string.
uint8_t mz_sem_create(mz_semaphore_t *c, uint8_t cnt, uint8_t init)
This function is used to create and initialize the semaphore object Create and Initialize a Semaphore...
uint8_t mz_thread_create(mz_thread_t *t, const char *name, mz_fn thread_func, void *const arg, mz_thread_prio_t prio, StackType_t *stack_mem, size_t stack_size, StaticTask_t *cb_mem, uint32_t cb_size)
This function is used to check the message queue object validity Create a thread and add it to Active...
mz_error_t mz_tm_create_start_recursive(char *_name, TickType_t _tick, mz_tm_cb _cb)
Function to Create and Start One Shot Timers.
uint8_t UV_Data[50]