Esempio n. 1
0
s32 sdio_writeN(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8* pbuf)
{
	PADAPTER padapter;
	struct dvobj_priv *psdiodev;
	PSDIO_DATA psdio;
	u8 bMacPwrCtrlOn;
	u8 deviceId;
	u16 offset;
	u32 ftaddr;
	u8 shift;
	s32 err;

_func_enter_;

	padapter = pintfhdl->padapter;
	psdiodev = pintfhdl->pintf_dev;
	psdio = &psdiodev->intf_data;
	err = 0;

	ftaddr = _cvrt2ftaddr(addr, &deviceId, &offset);

	rtw_hal_get_hwreg(padapter, HW_VAR_APFM_ON_MAC, &bMacPwrCtrlOn);
	if (((deviceId == WLAN_IOREG_DEVICE_ID) && (offset < 0x100))
		|| (_FALSE == bMacPwrCtrlOn)
#ifdef CONFIG_LPS_LCLK
		|| (_TRUE == padapter->pwrctrlpriv.bFwCurrentInPSMode)
#endif
		)
	{
		err = sd_cmd52_write(psdio, ftaddr, cnt, pbuf);
		return err;
	}

	shift = ftaddr & 0x3;
	if (shift == 0) {
		err = sd_write(psdio, ftaddr, cnt, pbuf);
	} else {
		u8 *ptmpbuf;
		u32 n;

		ftaddr &= ~(u16)0x3;
		n = cnt + shift;
		ptmpbuf = rtw_malloc(n);
		if (NULL == ptmpbuf) return -1;
		err = sd_read(psdio, ftaddr, 4, ptmpbuf);
		if (err) {
			rtw_mfree(ptmpbuf, n);
			return err;
		}
		_rtw_memcpy(ptmpbuf+shift, pbuf, cnt);
		err = sd_write(psdio, ftaddr, n, ptmpbuf);
		rtw_mfree(ptmpbuf, n);
	}

_func_exit_;

	return err;
}
Esempio n. 2
0
s32 sdio_writeN(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8* pbuf)
{
	PADAPTER padapter;
	u8 bMacPwrCtrlOn;
	u8 deviceId;
	u16 offset;
	u32 ftaddr;
	u8 shift;
	s32 err;

_func_enter_;

	padapter = pintfhdl->padapter;
	err = 0;

	ftaddr = _cvrt2ftaddr(addr, &deviceId, &offset);

//		rtw_hal_get_hwreg(padapter, HW_VAR_APFM_ON_MAC, &bMacPwrCtrlOn);
//		if (((deviceId == WLAN_IOREG_DEVICE_ID) && (offset < 0x100))
//			|| (_FALSE == bMacPwrCtrlOn)
//	#ifdef CONFIG_LPS_LCLK
//			|| (_TRUE == adapter_to_pwrctl(padapter)->bFwCurrentInPSMode)
//	#endif
//			)
//		{
//			err = sd_cmd52_write(pintfhdl, ftaddr, cnt, pbuf);
//			return err;
//		}

	shift = ftaddr & 0x3;
	if (shift == 0) {
		err = sd_write(pintfhdl, ftaddr, cnt, pbuf);
	} else {
		u8 *ptmpbuf;
		u32 n;

		ftaddr &= ~(u16)0x3;
		n = cnt + shift;
		ptmpbuf = rtw_malloc(n);
		if (NULL == ptmpbuf) return -1;
		err = sd_read(pintfhdl, ftaddr, 4, ptmpbuf);
		if (err) {
			rtw_mfree(ptmpbuf, n);
			return err;
		}
		_rtw_memcpy(ptmpbuf+shift, pbuf, cnt);
		err = sd_write(pintfhdl, ftaddr, n, ptmpbuf);
		rtw_mfree(ptmpbuf, n);
	}

_func_exit_;

	return err;
}
Esempio n. 3
0
static void testReadWrite() {
	int32_t ret;
	{
		ret = sd_setBlockSize(sd, SD_BLOCK_SIZE_512B);
		CONFIG_ASSERT(ret == 0);
#if 1
		memset(data, 0x42, ARRAY_SIZE(data));
		/* read 4K from card */
		ret = sd_read(sd, CMD(18), 0, ARRAY_SIZE(data), (uint32_t *) data, 1000 / portTICK_PERIOD_MS);
		CONFIG_ASSERT(ret == 0);
		/* stop transver */
		ret = sd_sendCommand(sd, CMD(12), 0, NULL, 100 / portTICK_PERIOD_MS);
		CONFIG_ASSERT(ret == 0);
#else
		memset(data, 0x00, ARRAY_SIZE(data));
#endif
		/* Write Back test */
		ret = sd_write(sd, CMD(25), 0, ARRAY_SIZE(data), (uint32_t *) data, 1000 / portTICK_PERIOD_MS);
		CONFIG_ASSERT(ret == 0);
		/* stop transver */
		ret = sd_sendCommand(sd, CMD(12), 0, NULL, 100 / portTICK_PERIOD_MS);
		CONFIG_ASSERT(ret == 0);
		vTaskDelay(500 / portTICK_PERIOD_MS);
		/* read 4K from card */
		ret = sd_read(sd, CMD(18), 0, ARRAY_SIZE(data2), (uint32_t *) data2, 1000 / portTICK_PERIOD_MS);
		CONFIG_ASSERT(ret == 0);
		/* stop transver */
		ret = sd_sendCommand(sd, CMD(12), 0, NULL, 100 / portTICK_PERIOD_MS);
		CONFIG_ASSERT(ret == 0);
		ret = memcmp(data, data2, ARRAY_SIZE(data));
		CONFIG_ASSERT(ret == 0);
	}

}
Esempio n. 4
0
/*
 * Description:
 *	Write to TX FIFO
 *	Align write size block size,
 *	and make sure data could be written in one command.
 *
 * Parameters:
 *	pintfhdl	a pointer of intf_hdl
 *	addr		port ID
 *	cnt			size to write
 *	wmem		data pointer to write
 *
 * Return:
 *	_SUCCESS(1)		Success
 *	_FAIL(0)		Fail
 */
static u32 sdio_write_port(
	struct intf_hdl *pintfhdl,
	u32 addr,
	u32 cnt,
	u8 *mem)
{
	PADAPTER padapter;
	PSDIO_DATA psdio;
	s32 err;
	struct xmit_buf *xmitbuf = (struct xmit_buf *)mem;

	padapter = pintfhdl->padapter;
	psdio = &adapter_to_dvobj(padapter)->intf_data;

	if (padapter->hw_init_completed == _FALSE) {
		DBG_871X("%s [addr=0x%x cnt=%d] padapter->hw_init_completed == _FALSE\n",__func__,addr,cnt);
		return _FAIL;
	}

	cnt = _RND4(cnt);
	HalSdioGetCmdAddr8723BSdio(padapter, addr, cnt >> 2, &addr);

	if (cnt > psdio->block_transfer_len)
		cnt = _RND(cnt, psdio->block_transfer_len);
//	cnt = sdio_align_size(cnt);

	err = sd_write(pintfhdl, addr, cnt, xmitbuf->pdata);

	rtw_sctx_done_err(&xmitbuf->sctx,
		err ? RTW_SCTX_DONE_WRITE_PORT_ERR : RTW_SCTX_DONE_SUCCESS);

	if (err) return _FAIL;
	return _SUCCESS;
}
Esempio n. 5
0
/*
 * Description:
 *	Write to TX FIFO
 *	Align write size block size,
 *	and make sure data could be written in one command.
 *
 * Parameters:
 *	pintfhdl	a pointer of intf_hdl
 *	addr		port ID
 *	cnt			size to write
 *	wmem		data pointer to write
 *
 * Return:
 *	_SUCCESS(1)		Success
 *	_FAIL(0)		Fail
 */
static u32 sdio_write_port(
	struct intf_hdl *pintfhdl,
	u32 addr,
	u32 cnt,
	u8 *mem)
{
	PADAPTER padapter;
	PSDIO_DATA psdio;
	s32 err;


	padapter = pintfhdl->padapter;
	psdio = &padapter->dvobjpriv.intf_data;

	cnt = _RND4(cnt);
	HalSdioGetCmdAddr8723ASdio(padapter, addr, cnt >> 2, &addr);

	if (cnt > psdio->block_transfer_len)
		cnt = _RND(cnt, psdio->block_transfer_len);
//	cnt = sdio_align_size(cnt);

	err = sd_write(psdio, addr, cnt, mem);

	if (err) return _FAIL;
	return _SUCCESS;
}
Esempio n. 6
0
static s32 sdio_writeN(struct intf_hdl *intfhdl, u32 addr, u32 cnt, u8 *buf)
{
	struct adapter *adapter;
	u8 mac_pwr_ctrl_on;
	u8 device_id;
	u16 offset;
	u32 ftaddr;
	u8 shift;
	s32 err;

	adapter = intfhdl->padapter;
	err = 0;

	ftaddr = _cvrt2ftaddr(addr, &device_id, &offset);

	rtw_hal_get_hwreg(adapter, HW_VAR_APFM_ON_MAC, &mac_pwr_ctrl_on);
	if (
		((device_id == WLAN_IOREG_DEVICE_ID) && (offset < 0x100)) ||
		(!mac_pwr_ctrl_on) ||
		(adapter_to_pwrctl(adapter)->bFwCurrentInPSMode)
	)
		return sd_cmd52_write(intfhdl, ftaddr, cnt, buf);

	shift = ftaddr & 0x3;
	if (shift == 0) {
		err = sd_write(intfhdl, ftaddr, cnt, buf);
	} else {
		u8 *tmpbuf;
		u32 n;

		ftaddr &= ~(u16)0x3;
		n = cnt + shift;
		tmpbuf = rtw_malloc(n);
		if (!tmpbuf)
			return -1;
		err = sd_read(intfhdl, ftaddr, 4, tmpbuf);
		if (err) {
			kfree(tmpbuf);
			return err;
		}
		memcpy(tmpbuf+shift, buf, cnt);
		err = sd_write(intfhdl, ftaddr, n, tmpbuf);
		kfree(tmpbuf);
	}
	return err;
}
Esempio n. 7
0
/**
 * Send a message to the serial device
 * @param sd The serial device
 * @param message The message to send
 * @return error condition
 * Serial device response is stored in sd->last_response
 */
int sd_send_message(SERIAL_DEVICE sd, string_t message) 
{
	
  int err = SERIAL_DEVICE_OK;
	
  err =  sd_write(sd, message);
  if ( SERIAL_DEVICE_OK == err) {
    err = sd_read(sd) ;
  }
	
  return err;
}
Esempio n. 8
0
void write(void) {
	printf("\nwriting\n");
	uint8_t buf[SD_BLOCKSIZE*2] = {'\0'};
	{
		int i = 0;
		char c;
		do {
			c = getchar();
			putchar(c);
			buf[i++] = c;
		} while (c != '\r');
	}
	if (sd_write(buf, sector, ARR_LEN(buf)/SD_BLOCKSIZE) != 0) printf("ERROR: write error\n");
	printf("\nend write\n");
}
Esempio n. 9
0
/*
 * Todo: align address to 4 bytes.
 */
s32 sdio_local_write(
	PADAPTER	padapter,
	u32		addr,
	u32		cnt,
	u8		*pbuf)
{
	PSDIO_DATA psdio;
	u8 bMacPwrCtrlOn;
	s32 err;
	u8 *ptmpbuf;


#ifdef CONFIG_DEBUG_RTL819X
	if(addr & 0x3)
		DBG_8192C("%s, address must be 4 bytes alignment\n", __FUNCTION__);

	if(cnt  & 0x3)
		DBG_8192C("%s, size must be the multiple of 4 \n", __FUNCTION__);
#endif

	psdio = &padapter->dvobjpriv.intf_data;

	HalSdioGetCmdAddr8723ASdio(padapter, SDIO_LOCAL_DEVICE_ID, addr, &addr);

	padapter->HalFunc.GetHwRegHandler(padapter, HW_VAR_APFM_ON_MAC, &bMacPwrCtrlOn);
	if ((_FALSE == bMacPwrCtrlOn)
#ifdef CONFIG_LPS_LCLK
		|| (_TRUE == padapter->pwrctrlpriv.bFwCurrentInPSMode)
#endif
		)
	{
		err = sd_cmd52_write(psdio, addr, cnt, pbuf);
		return err;
	}

        ptmpbuf = (u8*)rtw_malloc(cnt);
	if(!ptmpbuf)
		return (-1);

	_rtw_memcpy(ptmpbuf, pbuf, cnt);

	err = sd_write(psdio, addr, cnt, ptmpbuf);

	if (ptmpbuf)
		rtw_mfree(ptmpbuf, cnt);

	return err;
}
Esempio n. 10
0
/*
 * Todo: align address to 4 bytes.
 */
s32 sdio_local_write(
	PADAPTER	padapter,
	u32		addr,
	u32		cnt,
	u8		*pbuf)
{
	struct intf_hdl * pintfhdl;
	u8 bMacPwrCtrlOn;
	s32 err;
	u8 *ptmpbuf;

	if(addr & 0x3)
		DBG_8192C("%s, address must be 4 bytes alignment\n", __FUNCTION__);

	if(cnt  & 0x3)
		DBG_8192C("%s, size must be the multiple of 4 \n", __FUNCTION__);

	pintfhdl=&padapter->iopriv.intf;

	HalSdioGetCmdAddr8723BSdio(padapter, SDIO_LOCAL_DEVICE_ID, addr, &addr);

	rtw_hal_get_hwreg(padapter, HW_VAR_APFM_ON_MAC, &bMacPwrCtrlOn);
	if ((_FALSE == bMacPwrCtrlOn)
#ifdef CONFIG_LPS_LCLK
		|| (_TRUE == adapter_to_pwrctl(padapter)->bFwCurrentInPSMode)
#endif
		)
	{
		err = sd_cmd52_write(pintfhdl, addr, cnt, pbuf);
		return err;
	}

	ptmpbuf = (u8*)rtw_malloc(cnt);
	if (!ptmpbuf)
		return (-1);

	_rtw_memcpy(ptmpbuf, pbuf, cnt);

	err = sd_write(pintfhdl, addr, cnt, ptmpbuf);

	if (ptmpbuf)
		rtw_mfree(ptmpbuf, cnt);

	return err;
}
Esempio n. 11
0
DRESULT disk_write (
	BYTE pdrv,			/* Physical drive nmuber (0..) */
	const BYTE *buff,	/* Data to be written */
	DWORD sector,		/* Sector address (LBA) */
	UINT count			/* Number of sectors to write (1..128) */
)
{
	DRESULT res = 0;
	int result;

        result = sd_write(sddevice, buff, count *512, sector);
        
        if (result < 0) res = RES_ERROR;
        else res = RES_OK;

        return res;
        
}
Esempio n. 12
0
PROCESS_THREAD(sd_test, event, data)
{
  static unsigned long iter;
  static unsigned long offset;
  char buf[BUF_SIZE];
  static struct etimer et;
  int r, buflen;

  PROCESS_BEGIN();

  etimer_set(&et, CLOCK_SECOND / 16);

  offset = 0;
  for(iter = 1;; iter++) {
    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));

    memset(buf, 0, sizeof(buf));
    buflen = sprintf(buf, "(%ld) Testing the SD card (%ld)", iter, iter);

    if((iter & 7) == 0) {
      offset = random_rand() & 0xffff;
    } else {
      offset += random_rand() & 0xff;
    }

    r = sd_write(offset, buf, buflen + 1);
    if(r > 0) {
      memset(buf, 0, sizeof(buf));
      r = sd_read(offset, buf, buflen + 1);
      if(r > 0) {
        printf("read %s (offset %lu)\n", buf, offset);
      } else {
        printf("read error: %d (%s)\n", r, sd_error_string(r));
      }
    } else {
      printf("write error: %d (%s)\n", r, sd_error_string(r));
    }
    etimer_restart(&et);
  }

  PROCESS_END();
}
Esempio n. 13
0
int
cfs_coffee_arch_erase(unsigned sector)
{
  char buf[SD_DEFAULT_BLOCK_SIZE];
  sd_offset_t start_offset;
  sd_offset_t end_offset;
  sd_offset_t offset;

  memset(buf, 0, sizeof(buf));

  start_offset = COFFEE_START + sector * COFFEE_SECTOR_SIZE;
  end_offset = start_offset + COFFEE_SECTOR_SIZE;

  for(offset = start_offset; offset < end_offset; offset += SD_DEFAULT_BLOCK_SIZE) {
    if(sd_write(offset, buf, sizeof(buf)) < 0) {
      return -1;
    }
  }
  return 0;
}
Esempio n. 14
0
/*
 * Todo: align address to 4 bytes.
 */
s32 sdio_local_write(
	struct adapter *adapter,
	u32 addr,
	u32 cnt,
	u8 *buf
)
{
	struct intf_hdl *intfhdl;
	u8 mac_pwr_ctrl_on;
	s32 err;
	u8 *tmpbuf;

	if (addr & 0x3)
		DBG_8192C("%s, address must be 4 bytes alignment\n", __func__);

	if (cnt  & 0x3)
		DBG_8192C("%s, size must be the multiple of 4\n", __func__);

	intfhdl = &adapter->iopriv.intf;

	HalSdioGetCmdAddr8723BSdio(adapter, SDIO_LOCAL_DEVICE_ID, addr, &addr);

	rtw_hal_get_hwreg(adapter, HW_VAR_APFM_ON_MAC, &mac_pwr_ctrl_on);
	if (
		(!mac_pwr_ctrl_on) ||
		(adapter_to_pwrctl(adapter)->bFwCurrentInPSMode)
	)
		return sd_cmd52_write(intfhdl, addr, cnt, buf);

	tmpbuf = rtw_malloc(cnt);
	if (!tmpbuf)
		return (-1);

	memcpy(tmpbuf, buf, cnt);

	err = sd_write(intfhdl, addr, cnt, tmpbuf);

	kfree(tmpbuf);

	return err;
}
Esempio n. 15
0
DRESULT disk_write(BYTE drv, const BYTE *buff, DWORD sector, BYTE count)
{
    DSTATUS status = RES_PARERR;

    spi1_lock();
    {
        switch(drv)
        {
            case driveNumFlashMem:
                status = flash_write_sectors((unsigned char*)buff, sector, count);
                break;
            case driveNumSdCard:
                status = sd_write(buff, sector, count);
                break;
            default:
                status = RES_PARERR;
                break;
        }
    }
    spi1_unlock();

    return status;
}
Esempio n. 16
0
int main()
{
    char c;
    char str[200];
    int i;
    //char tmpbuf[512];
    uart0_init();   // ������115200��8N1(8������λ����У��λ��1��ֹͣλ)
    timer_init();
    
    sd_init ();
    //for (i=0; i<512; i++)
    //    tmpbuf[i] = 0;
//
    //sd_block_write (tmpbuf, 0x0, 512);
   
    while (1)
    {   
        u8 buf[BUFFER_SIZE];
        u32 vAddress;
        u32 vSize;
        u8 base = 10;
        int i, ret;
        
        debug ("buf = 0x%08x", buf);
        debug ("__bss_start = 0x%08x", __bss_start);
        debug ("__bss_end = 0x%08x", __bss_end);

        printf("\r\n##### SD Card Read #####\r\n");      
        //printf("Data format: 'year.month.day w hour:min:sec', 'w' is week day\n\r");       
        //printf("eg: 2007.08.30 4 01:16:57\n\r");        
        printf("[R] Read SD card\n\r");       
        printf("[W] Write SD card\n\r");
        printf("Enter your selection: ");

        c = getc();
        printf("%c\n\r", c);
        switch (c)
        {
            case 'r':
            case 'R':
            {
                memset (buf, 0, BUFFER_SIZE);
                printf("Enter Address and Size: ");
                i = 0;
                do
                {
                    c = getc();
                    str[i++] = c;
                    putc(c);
                } while(c != '\n' && c != '\r');
                str[i] = '\0';
                putc ('\n');
                while(--i >= 0)
                {
                    if (str[i] == 'x' || str[i] == 'X') {
                        if ((i-1)>=0 && str[i-1] == '0') {
                            base = 16;
                            str[i-1] = ' ';
                        }
                        else {
                            printf ("Address Error!\r\n");
                            goto LOOP_END;
                        }
                        
                    }
                    if ((str[i] < '0' || str[i] > '9') && (str[i] < 'a' || str[i] > 'f') && (str[i] < 'A' || str[i] > 'F'))
                        str[i] = ' ';
                }
                switch (base)
                {
                    case 10:
                        sscanf (str, "%u %u", &vAddress, &vSize);
                        break;
                    case 16:
                        sscanf (str, "%x %u", &vAddress, &vSize);
                    default:
                        break;
                }

                if (sd_read (vAddress, buf, vSize) < 0){
                    printf ("## Warning: read card fail\r\n");
                    return -1;    
                }

                dumpHEX (buf, vSize, vAddress);
                break;
            }
            
            case 'w':
            case 'W':
            {
                u8 *s_str, *e_str;
                Stream_Buf argv_buf;
                argv_buf.count = 0;
                argv_buf.base = 10;

                memset (buf, 0, BUFFER_SIZE);
                printf("Enter Address and Data: ");
                i = 0;
                do
                {
                    c = getc();
                    str[i++] = c;
                    putc(c);
                } while(c != '\n' && c != '\r');
                str[i] = '\0';
                putc ('\n');
                while(--i >= 0)
                {
                    if (str[i] == 'x' || str[i] == 'X') {
                        if ((i-1)>=0 && str[i-1] == '0') {
                            argv_buf.base = 16;
                            str[i-1] = ' ';
                        }
                        else {
                            printf ("Address Error!\r\n");
                            goto LOOP_END;
                        }
                        
                    }
                    if ((str[i] < '0' || str[i] > '9') && (str[i] < 'a' || str[i] > 'f') && (str[i] < 'A' || str[i] > 'F'))
                        str[i] = ' ';
                }
                    
                s_str = str;
                e_str = str;
                while (*e_str != '\0')
                {
                    s_str = e_str;
                    while (*s_str == ' '){
                        s_str++;
                    }
                    for (e_str=s_str; (*e_str!=' ') && (*e_str!='\0'); e_str++){}
                    if (*e_str == '\0')
                        break;
                    memset (argv_buf.buf[argv_buf.count], 0, 12);
                    memcpy (argv_buf.buf[argv_buf.count], s_str, e_str-s_str);
                    argv_buf.count++;
                }

                switch (argv_buf.base)
                {
                    case 10:
                        sscanf (argv_buf.buf[0], "%u", &vAddress);
                        break;
                    case 16:
                        sscanf (argv_buf.buf[0], "%x", &vAddress);
                    default:
                        break;
                }

                vSize = argv_buf.count - 1;
                for (i=1; i<argv_buf.count; i++){
                    u32 temp;
                    sscanf (argv_buf.buf[i], "%x", &temp);
                    buf[i-1] = (u8)temp;
                }

                printf ("Start write date.\r\n");
                if (sd_write (buf, vAddress, vSize) < 0){
                    printf ("## Warning: write card fail\r\n");
                    return -1;    
                }
                printf ("%dB Data write done.\r\n", argv_buf.count-1);
LOOP_END:
                break;
            }
        }
    }
    
    return 0;
}
Esempio n. 17
0
int main(void)
{
	DDRF = 0;
	char GPS[100];
	char dylos[10];
	char GPS_line = 0;
	char dylos_line = 0;
	char sd_buff[150];
	char buffer[30];
	int GPS_data[15];
	int dylos_data[2];
	int C3H8, CH4, CO, H2S, F;
	char new_GPS_data = 0;
	char new_dylos_data = 0;
	FRESULT fr;

	int ppm2[151];
	ppm2[0] =	10478;
	ppm2[1] =	9706;
	ppm2[2] =	9015;
	ppm2[3] =	8394;
	ppm2[4] =	7834;
	ppm2[5] =	7327;
	ppm2[6] =	6868;
	ppm2[7] =	6449;
	ppm2[8] =	6068;
	ppm2[9] =	5718;
	ppm2[10] =	5398;
	ppm2[11] =	5103;
	ppm2[12] =	4831;
	ppm2[13] =	4581;
	ppm2[14] =	4349;
	ppm2[15] =	4133;
	ppm2[16] =	3934;
	ppm2[17] =	3748;
	ppm2[18] =	3575;
	ppm2[19] =	3413;
	ppm2[20] =	3262;
	ppm2[21] =	3121;
	ppm2[22] =	2988;
	ppm2[23] =	2864;
	ppm2[24] =	2747;
	ppm2[25] =	2637;
	ppm2[26] =	2533;
	ppm2[27] =	2435;
	ppm2[28] =	2343;
	ppm2[29] =	2256;
	ppm2[30] =	2173;
	ppm2[31] =	2095;
	ppm2[32] =	2021;
	ppm2[33] =	1951;
	ppm2[34] =	1884;
	ppm2[35] =	1821;
	ppm2[36] =	1760;
	ppm2[37] =	1703;
	ppm2[38] =	1648;
	ppm2[39] =	1596;
	ppm2[40] =	1546;
	ppm2[41] =	1499;
	ppm2[42] =	1454;
	ppm2[43] =	1410;
	ppm2[44] =	1369;
	ppm2[45] =	1329;
	ppm2[46] =	1291;
	ppm2[47] =	1254;
	ppm2[48] =	1220;
	ppm2[49] =	1186;
	ppm2[50] =	1154;
	ppm2[51] =	1123;
	ppm2[52] =	1093;
	ppm2[53] =	1065;
	ppm2[54] =	1037;
	ppm2[55] =	1011;
	ppm2[56] =	985;
	ppm2[57] =	961;
	ppm2[58] =	937;
	ppm2[59] =	914;
	ppm2[60] =	892;
	ppm2[61] =	871;
	ppm2[62] =	851;
	ppm2[63] =	831;
	ppm2[64] =	812;
	ppm2[65] =	793;
	ppm2[66] =	775;
	ppm2[67] =	758;
	ppm2[68] =	741;
	ppm2[69] =	725;
	ppm2[70] =	710;
	ppm2[71] =	694;
	ppm2[72] =	680;
	ppm2[73] =	667;
	ppm2[74] =	652;
	ppm2[75] =	638;
	ppm2[76] =	625;
	ppm2[77] =	613;
	ppm2[78] =	601;
	ppm2[79] =	589;
	ppm2[80] =	577;
	ppm2[81] =	566;
	ppm2[82] =	555;
	ppm2[83] =	545;
	ppm2[84] =	534;
	ppm2[85] =	524;
	ppm2[86] =	515;
	ppm2[87] =	505;
	ppm2[88] =	496;
	ppm2[89] =	487;
	ppm2[90] =	478;
	ppm2[91] =	470;
	ppm2[92] =	461;
	ppm2[93] =	453;
	ppm2[94] =	446;
	ppm2[95] =	438;
	ppm2[96] =	430;
	ppm2[97] =	423;
	ppm2[98] =	416;
	ppm2[99] =	409;
	ppm2[100] =	402;
	ppm2[101] =	396;
	ppm2[102] =	389;
	ppm2[103] =	383;
	ppm2[104] =	377;
	ppm2[105] =	371;
	ppm2[106] =	365;
	ppm2[107] =	359;
	ppm2[108] =	354;
	ppm2[109] =	348;
	ppm2[110] =	343;
	ppm2[111] =	338;
	ppm2[112] =	333;
	ppm2[113] =	328;
	ppm2[114] =	323;
	ppm2[115] =	318;
	ppm2[116] =	313;
	ppm2[117] =	309;
	ppm2[118] =	304;
	ppm2[119] =	300;
	ppm2[120] =	296;
	ppm2[121] =	292;
	ppm2[122] =	287;
	ppm2[123] =	283;
	ppm2[124] =	279;
	ppm2[125] =	276;
	ppm2[126] =	272;
	ppm2[127] =	268;
	ppm2[128] =	265;
	ppm2[129] =	261;
	ppm2[130] =	257;
	ppm2[131] =	254;
	ppm2[132] =	251;
	ppm2[133] =	247;
	ppm2[134] =	244;
	ppm2[135] =	241;
	ppm2[136] =	238;
	ppm2[137] =	235;
	ppm2[138] =	232;
	ppm2[139] =	229;
	ppm2[140] =	226;
	ppm2[141] =	223;
	ppm2[142] =	220;
	ppm2[143] =	218;
	ppm2[144] =	215;
	ppm2[145] =	212;
	ppm2[146] =	210;
	ppm2[147] =	207;
	ppm2[148] =	205;
	ppm2[149] =	202;
	ppm2[150] =	200;

	int ppm5[151];
	ppm5[0] =	9867;
	ppm5[1] =	9291;
	ppm5[2] =	8760;
	ppm5[3] =	8271;
	ppm5[4] =	7819;
	ppm5[5] =	7400;
	ppm5[6] =	7012;
	ppm5[7] =	6652;
	ppm5[8] =	6317;
	ppm5[9] =	6005;
	ppm5[10] =	5714;
	ppm5[11] =	5443;
	ppm5[12] =	5189;
	ppm5[13] =	4951;
	ppm5[14] =	4728;
	ppm5[15] =	4518;
	ppm5[16] =	4322;
	ppm5[17] =	4137;
	ppm5[18] =	3963;
	ppm5[19] =	3799;
	ppm5[20] =	3644;
	ppm5[21] =	3498;
	ppm5[22] =	3360;
	ppm5[23] =	3229;
	ppm5[24] =	3106;
	ppm5[25] =	2988;
	ppm5[26] =	2772;
	ppm5[27] =	2672;
	ppm5[28] =	2577;
	ppm5[29] =	2486;
	ppm5[30] =	2400;
	ppm5[31] =	2318;
	ppm5[32] =	2240;
	ppm5[33] =	2165;
	ppm5[34] =	2094;
	ppm5[35] =	2026;
	ppm5[36] =	1961;
	ppm5[37] =	1899;
	ppm5[38] =	1839;
	ppm5[39] =	1783;
	ppm5[40] =	1728;
	ppm5[41] =	1676;
	ppm5[42] =	1626;
	ppm5[43] =	1578;
	ppm5[44] =	1532;
	ppm5[45] =	1488;
	ppm5[46] =	1446;
	ppm5[47] =	1405;
	ppm5[48] =	1366;
	ppm5[49] =	1328;
	ppm5[50] =	1292;
	ppm5[51] =	1257;
	ppm5[52] =	1223;
	ppm5[53] =	1191;
	ppm5[54] =	1160;
	ppm5[55] =	1130;
	ppm5[56] =	1101;
	ppm5[57] =	1073;
	ppm5[58] =	1046;
	ppm5[59] =	1020;
	ppm5[60] =	995;
	ppm5[61] =	971;
	ppm5[62] =	947;
	ppm5[63] =	924;
	ppm5[64] =	903;
	ppm5[65] =	881;
	ppm5[66] =	861;
	ppm5[67] =	841;
	ppm5[68] =	822;
	ppm5[69] =	803;
	ppm5[70] =	785;
	ppm5[71] =	768;
	ppm5[72] =	751;
	ppm5[73] =	734;
	ppm5[74] =	718;
	ppm5[75] =	703;
	ppm5[76] =	688;
	ppm5[77] =	673;
	ppm5[78] =	659;
	ppm5[79] =	645;
	ppm5[80] =	632;
	ppm5[81] =	619;
	ppm5[82] =	606;
	ppm5[83] =	594;
	ppm5[84] =	582;
	ppm5[85] =	571;
	ppm5[86] =	559;
	ppm5[87] =	549;
	ppm5[88] =	538;
	ppm5[89] =	528;
	ppm5[90] =	517;
	ppm5[91] =	508;
	ppm5[92] =	498;
	ppm5[93] =	489;
	ppm5[94] =	480;
	ppm5[95] =	471;
	ppm5[96] =	462;
	ppm5[97] =	454;
	ppm5[98] =	446;
	ppm5[99] =	438;
	ppm5[100] =	430;
	ppm5[101] =	422;
	ppm5[102] =	415;
	ppm5[103] =	408;
	ppm5[104] =	401;
	ppm5[105] =	394;
	ppm5[106] =	387;
	ppm5[107] =	380;
	ppm5[108] =	374;
	ppm5[109] =	368;
	ppm5[110] =	362;
	ppm5[111] =	356;
	ppm5[112] =	350;
	ppm5[113] =	344;
	ppm5[114] =	339;
	ppm5[115] =	333;
	ppm5[116] =	328;
	ppm5[117] =	323;
	ppm5[118] =	317;
	ppm5[119] =	312;
	ppm5[120] =	308;
	ppm5[121] =	303;
	ppm5[122] =	298;
	ppm5[123] =	294;
	ppm5[124] =	289;
	ppm5[125] =	285;
	ppm5[126] =	280;
	ppm5[127] =	276;
	ppm5[128] =	272;
	ppm5[129] =	268;
	ppm5[130] =	264;
	ppm5[131] =	260;
	ppm5[132] =	256;
	ppm5[133] =	253;
	ppm5[134] =	249;
	ppm5[135] =	246;
	ppm5[136] =	242;
	ppm5[137] =	239;
	ppm5[138] =	235;
	ppm5[139] =	232;
	ppm5[140] =	229;
	ppm5[141] =	226;
	ppm5[142] =	222;
	ppm5[143] =	219;
	ppm5[144] =	216;
	ppm5[145] =	214;
	ppm5[146] =	211;
	ppm5[147] =	208;
	ppm5[148] =	205;
	ppm5[149] =	202;
	ppm5[150] =	200;	

	int ppm7[151];
	ppm7[0] = 4112;
	ppm7[1] = 3495;
	ppm7[2] = 3017;
	ppm7[3] = 2638;
	ppm7[4] = 2332;
	ppm7[5] = 2080;
	ppm7[6] = 1870;
	ppm7[7] = 1692;
	ppm7[8] = 1541;
	ppm7[9] = 1411;
	ppm7[10] = 1298;
	ppm7[11] = 1199;
	ppm7[12] = 1112;
	ppm7[13] = 1035;
	ppm7[14] = 967;
	ppm7[15] = 905;
	ppm7[16] = 850;
	ppm7[17] = 800;
	ppm7[18] = 755;
	ppm7[19] = 714;
	ppm7[20] = 676;
	ppm7[21] = 641;
	ppm7[22] = 610;
	ppm7[23] = 581;
	ppm7[24] = 554;
	ppm7[25] = 529;
	ppm7[26] = 506;
	ppm7[27] = 484;
	ppm7[28] = 464;
	ppm7[29] = 445;
	ppm7[30] = 428;
	ppm7[31] = 412;
	ppm7[32] = 396;
	ppm7[33] = 382;
	ppm7[34] = 368;
	ppm7[35] = 355;
	ppm7[36] = 343;
	ppm7[37] = 332;
	ppm7[38] = 321;
	ppm7[39] = 311;
	ppm7[40] = 301;
	ppm7[41] = 292;
	ppm7[42] = 283;
	ppm7[43] = 275;
	ppm7[44] = 267;
	ppm7[45] = 259;
	ppm7[46] = 252;
	ppm7[47] = 245;
	ppm7[48] = 238;
	ppm7[49] = 232;
	ppm7[50] = 226;
	ppm7[51] = 220;
	ppm7[52] = 215;
	ppm7[53] = 209;
	ppm7[54] = 204;
	ppm7[55] = 199;
	ppm7[56] = 195;
	ppm7[57] = 190;
	ppm7[58] = 186;
	ppm7[59] = 181;
	ppm7[60] = 177;
	ppm7[61] = 174;
	ppm7[62] = 170;
	ppm7[63] = 166;
	ppm7[64] = 163;
	ppm7[65] = 159;
	ppm7[66] = 156;
	ppm7[67] = 153;
	ppm7[68] = 150;
	ppm7[69] = 147;
	ppm7[70] = 144;
	ppm7[71] = 141;
	ppm7[72] = 139;
	ppm7[73] = 136;
	ppm7[74] = 133;
	ppm7[75] = 131;
	ppm7[76] = 129;
	ppm7[77] = 126;
	ppm7[78] = 124;
	ppm7[79] = 122;
	ppm7[80] = 120;
	ppm7[81] = 118;
	ppm7[82] = 116;
	ppm7[83] = 114;
	ppm7[84] = 112;
	ppm7[85] = 110;
	ppm7[86] = 108;
	ppm7[87] = 107;
	ppm7[88] = 105;
	ppm7[89] = 103;
	ppm7[90] = 102;
	ppm7[91] = 100;
	ppm7[92] = 99;
	ppm7[93] = 97;
	ppm7[94] = 96;
	ppm7[95] = 94;
	ppm7[96] = 93;
	ppm7[97] = 91;
	ppm7[98] = 90;
	ppm7[99] = 89;
	ppm7[100] = 88;
	ppm7[101] = 86;
	ppm7[102] = 85;
	ppm7[103] = 84;
	ppm7[104] = 83;
	ppm7[105] = 82;
	ppm7[106] = 81;
	ppm7[107] = 80;
	ppm7[108] = 79;
	ppm7[109] = 78;
	ppm7[110] = 77;
	ppm7[111] = 76;
	ppm7[112] = 75;
	ppm7[113] = 74;
	ppm7[114] = 73;
	ppm7[115] = 72;
	ppm7[116] = 71;
	ppm7[117] = 70;
	ppm7[118] = 69;
	ppm7[119] = 68;
	ppm7[120] = 68;
	ppm7[121] = 67;
	ppm7[122] = 66;
	ppm7[123] = 65;
	ppm7[124] = 64;
	ppm7[125] = 64;
	ppm7[126] = 63;
	ppm7[127] = 62;
	ppm7[128] = 62;
	ppm7[129] = 61;
	ppm7[130] = 60;
	ppm7[131] = 60;
	ppm7[132] = 59;
	ppm7[133] = 58;
	ppm7[134] = 58;
	ppm7[135] = 57;
	ppm7[136] = 56;
	ppm7[137] = 56;
	ppm7[138] = 55;
	ppm7[139] = 55;
	ppm7[140] = 54;
	ppm7[141] = 54;
	ppm7[142] = 53;
	ppm7[143] = 52;
	ppm7[144] = 52;
	ppm7[145] = 51;
	ppm7[146] = 51;
	ppm7[147] = 50;
	ppm7[148] = 50;
	ppm7[149] = 49;
	ppm7[150] = 49;
	
	int ppm136[161];
	ppm136[0] = 199;
	ppm136[1] = 194;
	ppm136[2] = 188;
	ppm136[3] = 183;
	ppm136[4] = 178;
	ppm136[5] = 173;
	ppm136[6] = 168;
	ppm136[7] = 163;
	ppm136[8] = 159;
	ppm136[9] = 154;
	ppm136[10] = 150;
	ppm136[11] = 146;
	ppm136[12] = 142;
	ppm136[13] = 139;
	ppm136[14] = 135;
	ppm136[15] = 131;
	ppm136[16] = 128;
	ppm136[17] = 125;
	ppm136[18] = 122;
	ppm136[19] = 119;
	ppm136[20] = 116;
	ppm136[21] = 113;
	ppm136[22] = 110;
	ppm136[23] = 107;
	ppm136[24] = 105;
	ppm136[25] = 102;
	ppm136[26] = 100;
	ppm136[27] = 97;
	ppm136[28] = 95;
	ppm136[29] = 93;
	ppm136[30] = 91;
	ppm136[31] = 89;
	ppm136[32] = 86;
	ppm136[33] = 84;
	ppm136[34] = 83;
	ppm136[35] = 81;
	ppm136[36] = 79;
	ppm136[37] = 77;
	ppm136[38] = 75;
	ppm136[39] = 74;
	ppm136[40] = 72;
	ppm136[41] = 71;
	ppm136[42] = 69;
	ppm136[43] = 68;
	ppm136[44] = 66;
	ppm136[45] = 65;
	ppm136[46] = 63;
	ppm136[47] = 62;
	ppm136[48] = 61;
	ppm136[49] = 59;
	ppm136[50] = 58;
	ppm136[51] = 57;
	ppm136[52] = 56;
	ppm136[53] = 55;
	ppm136[54] = 54;
	ppm136[55] = 53;
	ppm136[56] = 52;
	ppm136[57] = 50;
	ppm136[58] = 49;
	ppm136[59] = 49;
	ppm136[60] = 48;
	ppm136[61] = 47;
	ppm136[62] = 46;
	ppm136[63] = 45;
	ppm136[64] = 44;
	ppm136[65] = 43;
	ppm136[66] = 42;
	ppm136[67] = 42;
	ppm136[68] = 41;
	ppm136[69] = 40;
	ppm136[70] = 39;
	ppm136[71] = 39;
	ppm136[72] = 38;
	ppm136[73] = 37;
	ppm136[74] = 37;
	ppm136[75] = 36;
	ppm136[76] = 35;
	ppm136[77] = 35;
	ppm136[78] = 34;
	ppm136[79] = 33;
	ppm136[80] = 33;
	ppm136[81] = 32;
	ppm136[82] = 32;
	ppm136[83] = 31;
	ppm136[84] = 31;
	ppm136[85] = 30;
	ppm136[86] = 30;
	ppm136[87] = 29;
	ppm136[88] = 29;
	ppm136[89] = 28;
	ppm136[90] = 28;
	ppm136[91] = 27;
	ppm136[92] = 27;
	ppm136[93] = 26;
	ppm136[94] = 26;
	ppm136[95] = 25;
	ppm136[96] = 25;
	ppm136[97] = 25;
	ppm136[98] = 24;
	ppm136[99] = 24;
	ppm136[100] = 23;
	ppm136[101] = 23;
	ppm136[102] = 23;
	ppm136[103] = 22;
	ppm136[104] = 22;
	ppm136[105] = 22;
	ppm136[106] = 21;
	ppm136[107] = 21;
	ppm136[108] = 21;
	ppm136[109] = 20;
	ppm136[110] = 20;
	ppm136[111] = 20;
	ppm136[112] = 19;
	ppm136[113] = 19;
	ppm136[114] = 19;
	ppm136[115] = 19;
	ppm136[116] = 18;
	ppm136[117] = 18;
	ppm136[118] = 18;
	ppm136[119] = 17;
	ppm136[120] = 17;
	ppm136[121] = 17;
	ppm136[122] = 17;
	ppm136[123] = 16;
	ppm136[124] = 16;
	ppm136[125] = 16;
	ppm136[126] = 16;
	ppm136[127] = 16;
	ppm136[128] = 15;
	ppm136[129] = 15;
	ppm136[130] = 15;
	ppm136[131] = 15;
	ppm136[132] = 14;
	ppm136[133] = 14;
	ppm136[134] = 14;
	ppm136[135] = 14;
	ppm136[136] = 14;
	ppm136[137] = 14;
	ppm136[138] = 13;
	ppm136[139] = 13;
	ppm136[140] = 13;
	ppm136[141] = 13;
	ppm136[142] = 13;
	ppm136[143] = 12;
	ppm136[144] = 12;
	ppm136[145] = 12;
	ppm136[146] = 12;
	ppm136[147] = 12;
	ppm136[148] = 12;
	ppm136[149] = 12;
	ppm136[150] = 11;
	ppm136[151] = 11;
	ppm136[152] = 11;
	ppm136[153] = 11;
	ppm136[154] = 11;
	ppm136[155] = 11;
	ppm136[156] = 11;
	ppm136[157] = 10;
	ppm136[158] = 10;
	ppm136[159] = 10;
	ppm136[160] = 10;

	TCNT1 = 57723;
	TCCR1B = (1<<CS12) | (1<<CS10);
	TIMSK1 = (1<<TOIE1);
	usart_init(0);
	usart_init(1);
	usart_init(2);
	usart_init(3);
	init_GPS();
	F = 0;
	int f_num = 0;
	char tick = 10;
	char sd_valid = 0;
	f_mount(&FatFs, "", 0);		/* Give a work area to the default drive */
	sei();

    while (1) 
	{
		if(sd_valid == 0)
		{
			sprintf(fil_nm, "data%d.txt",f_num);
			if((fr = f_open(&Fil, fil_nm, FA_WRITE | FA_CREATE_NEW)) && f_num < 9999)
				f_num++;
			else
				sd_valid = 1;
		}

		if(rec_dylos_flag == 1)
		{
			rec_dylos_flag = 0;
			new_dylos_data = 1;
			tick = 0;
			if(rec_dylos[strlen(rec_dylos) - 1] == '\n')
			{
				dylos_line = 1;
				strncpy(dylos, rec_dylos, strlen(rec_dylos));
				strclr(rec_dylos);			
			}
		}

		if(rec_GPS_flag == 1)
		{
			rec_GPS_flag = 0;
			new_GPS_data = 1;
			if(rec_GPS[strlen(rec_GPS) - 1] == '\n')
			{
				GPS_line = 1;
				strncpy(GPS, rec_GPS, strlen(rec_GPS));
				strclr(rec_GPS);
			}
		}
		
		if(dylos_line == 1)
		{
			parse_dylos(dylos, dylos_data);
			dylos_line = 0;
		}
		
		if(GPS_line == 1)
		{
			parse_GPS(GPS, GPS_data);
			GPS_line = 0;
		}
	
		if(ov_flag == 1)
		{
			ov_flag = 0;
			tick++;
			if(new_GPS_data == 1)
			{
				if(GPS_data[9] == 1)
				{
					sprintf(sd_buff, "Date: %.2d/%.2d/%.2d\nTime: %.2d:%.2d:%.2d\nLatitude: %d %d.%d\nLongitude: %d %d.%d\nAltitude %d.%d\n", GPS_data[12], GPS_data[13], GPS_data[14], GPS_data[0], GPS_data[1], GPS_data[2], GPS_data[3], GPS_data[4], GPS_data[5], GPS_data[6], GPS_data[7], GPS_data[8], GPS_data[10], GPS_data[11]);
					sprintf(buffer,"%.2d:%.2d:%.2d,%d,%d.%d,%d,%d.%d,%d.%d,",GPS_data[0], GPS_data[1], GPS_data[2], GPS_data[3], GPS_data[4], GPS_data[5], GPS_data[6], GPS_data[7], GPS_data[8], GPS_data[10], GPS_data[11]);
				}
				else
				{
					sprintf(sd_buff, "Time: %.2d:%.2d:%.2d\nNO GPS DATA AVAILABLE\n", GPS_data[0], GPS_data[1], GPS_data[2]);
					sprintf(buffer,"%.2d:%.2d:%.2d,0,0.0,0,0.0,0.0,", GPS_data[0], GPS_data[1], GPS_data[2]);
				}
				sd_write(sd_buff, &Fil, fil_nm, sd_valid);
				usart_send(3, buffer);
				new_GPS_data = 0;
			}
			else
				usart_send(3,"00:00:00,0,0.0,0,0.0,0.0,");
			if(new_dylos_data == 1 || tick < 8)
			{
				sprintf(sd_buff, "Small: %d\nLarge: %d\n", dylos_data[0], dylos_data[1]);
				sprintf(buffer, "%d,%d,", dylos_data[0], dylos_data[1]);
				usart_send(3, buffer);
				sd_write(sd_buff, &Fil, fil_nm, sd_valid);
				new_dylos_data = 0;
			}
			else
				usart_send(3,"0,0,");			
			F = read_temp();
			sprintf(sd_buff, "Temperature %dF\n", F);
			sprintf(buffer, "%d,", F);
			usart_send(3, buffer);
			sd_write(sd_buff, &Fil, fil_nm, sd_valid);			
			C3H8 = read_MQ2();
			if(C3H8 < -1)
			{
				usart_send(3, "-1,");
				sd_write("C3H8: ERROR - MQ2 reading out of range\n", &Fil, fil_nm, sd_valid);
			}
			else if(C3H8 == -1)
			{
				sprintf(sd_buff, "C3H8: %d ppm \n",0);
				usart_send(3, "0,");
				sd_write(sd_buff, &Fil, fil_nm, sd_valid);
			}
			else
			{
				sprintf(sd_buff, "C3H8: %d ppm \n",ppm2[C3H8]);
				sprintf(buffer, "%d,", ppm2[C3H8]);
				usart_send(3, buffer);
				sd_write(sd_buff, &Fil, fil_nm, sd_valid);
			}
			CH4 = read_MQ5();
			if(CH4 < -1)
			{
				usart_send(3, "-1,");
				sd_write("CH4 : ERROR - MQ5 reading out of range\n", &Fil, fil_nm, sd_valid);
			}
			else if(CH4 == -1)
			{
				sprintf(sd_buff, "CH4 : %d ppm \n",0);
				usart_send(3, "0,");
				sd_write(sd_buff, &Fil, fil_nm, sd_valid);
			}
			else
			{
				sprintf(sd_buff, "CH4 : %d ppm \n",ppm5[CH4]);
				sprintf(buffer, "%d,", ppm5[CH4]);
				usart_send(3, buffer);
				sd_write(sd_buff, &Fil, fil_nm, sd_valid);
			}
			CO = read_MQ7();
			if(CO < -1)
			{
				usart_send(3, "-1,");
				sd_write("CO  : ERROR - MQ7 reading out of range\n", &Fil, fil_nm, sd_valid);
			}
			else if(CO == -1)
			{
				sprintf(sd_buff, "CO  : %d ppm \n",0);
				usart_send(3, "0,");
				sd_write(sd_buff, &Fil, fil_nm, sd_valid);
			}
			else
			{
				sprintf(sd_buff, "CO  : %d ppm \n",ppm7[CO]);
				sprintf(buffer, "%d,", ppm7[CO]);
				usart_send(3, buffer);
				sd_write(sd_buff, &Fil, fil_nm, sd_valid);
			}			
			H2S = read_MQ136();
			if(H2S < -1)
			{
				usart_send(3, "-1\n");
				sd_write("H2S : ERROR - MQ136 reading out of range\n\n", &Fil, fil_nm, sd_valid);
			}
			else if(H2S == -1)
			{
				sprintf(sd_buff, "H2S : %d ppm \n\n",0);
				usart_send(3, "0\n");
				sd_write(sd_buff, &Fil, fil_nm, sd_valid);
			}
			else
			{
				sprintf(sd_buff, "H2S : %d ppm \n\n",ppm136[H2S]);
				sprintf(buffer, "%d\n", ppm136[H2S]);
				usart_send(3, buffer);
				sd_write(sd_buff, &Fil, fil_nm, sd_valid);
			}
		}
	}
	return 0;
}
Esempio n. 18
0
s32 sdio_write32(struct intf_hdl *pintfhdl, u32 addr, u32 val)
{
	PADAPTER padapter;
	u8 bMacPwrCtrlOn;
	u8 deviceId;
	u16 offset;
	u32 ftaddr;
	u8 shift;
	s32 err;

_func_enter_;

	padapter = pintfhdl->padapter;
	err = 0;

	ftaddr = _cvrt2ftaddr(addr, &deviceId, &offset);

	rtw_hal_get_hwreg(padapter, HW_VAR_APFM_ON_MAC, &bMacPwrCtrlOn);
	if (((deviceId == WLAN_IOREG_DEVICE_ID) && (offset < 0x100))
		|| (_FALSE == bMacPwrCtrlOn)
#ifdef CONFIG_LPS_LCLK
		|| (_TRUE == adapter_to_pwrctl(padapter)->bFwCurrentInPSMode)
#endif
		)
	{
		val = cpu_to_le32(val);
		err = sd_cmd52_write(pintfhdl, ftaddr, 4, (u8*)&val);
		return err;
	}

	// 4 bytes alignment
	shift = ftaddr & 0x3;
#if 1
	if (shift == 0)
	{
		sd_write32(pintfhdl, ftaddr, val, &err);
	}
	else
	{
		val = cpu_to_le32(val);
		err = sd_cmd52_write(pintfhdl, ftaddr, 4, (u8*)&val);
	}
#else
	if (shift == 0) {
		sd_write32(pintfhdl, ftaddr, val, &err);
	} else {
		u8 *ptmpbuf;

		ptmpbuf = (u8*)rtw_malloc(8);
		if (NULL == ptmpbuf) return (-1);

		ftaddr &= ~(u16)0x3;
		err = sd_read(pintfhdl, ftaddr, 8, ptmpbuf);
		if (err) {
			rtw_mfree(ptmpbuf, 8);
			return err;
		}
		val = cpu_to_le32(val);
		_rtw_memcpy(ptmpbuf+shift, &val, 4);
		err = sd_write(pintfhdl, ftaddr, 8, ptmpbuf);

		rtw_mfree(ptmpbuf, 8);
	}
#endif

_func_exit_;

	return err;
}