/** * @fn nm_uart_reconfigure * @brief Reconfigures the UART interface * @param [in] ptr * Pointer to a DWORD containing baudrate at this moment. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure * @author Viswanathan Murugesan * @date 22 OCT 2014 * @version 1.0 */ sint8 nm_uart_reconfigure(void *ptr) { tstrNmUartDefault strUart; sint8 s8Ret = M2M_SUCCESS; uint8 b[HDR_SZ+1]; /*write reg*/ b[0] = 0xa5; b[1] = 5; b[2] = 0; b[3] = 0; b[4] = 0; b[5] = 0; b[6] = 0; b[7] = 0; b[8] = 0; b[9] = (uint8)((*(unsigned long *)ptr) & 0x000000ff); b[10] = (uint8)(((*(unsigned long *)ptr) & 0x0000ff00)>>8); b[11] = (uint8)(((*(unsigned long *)ptr) & 0x00ff0000)>>16); b[12] = (uint8)(((*(unsigned long *)ptr) & 0xff000000)>>24); b[2] = get_cs(&b[1],HDR_SZ); get_cs(&b[1],HDR_SZ); strUart.pu8Buf = b; strUart.u16Sz = sizeof(b); if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_W, &strUart)) { M2M_ERR("write error\n"); s8Ret = M2M_ERR_BUS_FAIL; } else { if(!nm_bus_get_chip_type()) { //check for the ack from the SAMD21 for the packet reception. strUart.u16Sz = 1; if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strUart)) { s8Ret = M2M_ERR_BUS_FAIL; } if(b[0] == 0xAC) { M2M_DBG("Successfully sent the UART reconfigure command\n"); } else { M2M_ERR("write error\n"); s8Ret = M2M_ERR_BUS_FAIL; } } } return s8Ret; }
/* * @fn nm_i2c_write_block * @brief Write block of data * @param [in] u32Addr * Start address * @param [in] puBuf * Pointer to the buffer holding the data to be written * @param [in] u16Sz * Number of bytes to write. The buffer size must be >= u16Sz * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure * @author M. Abdelmawla * @date 11 July 2012 * @version 1.0 */ sint8 nm_i2c_write_block(uint32 u32Addr, uint8 *pu8Buf, uint16 u16Sz) { uint8 au8Buf[7]; tstrNmI2cSpecial strI2c; sint8 s8Ret = M2M_SUCCESS; au8Buf[0] = 0x12; au8Buf[1] = (uint8)(u32Addr >> 24); au8Buf[2] = (uint8)(u32Addr >> 16); au8Buf[3] = (uint8)(u32Addr >> 8); au8Buf[4] = (uint8)(u32Addr); au8Buf[5] = (uint8)(u16Sz >> 8); au8Buf[6] = (uint8)(u16Sz); strI2c.pu8Buf1 = au8Buf; strI2c.pu8Buf2 = pu8Buf; strI2c.u16Sz1 = sizeof(au8Buf); strI2c.u16Sz2 = u16Sz; if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_W_SPECIAL, &strI2c)) { M2M_ERR("write error\n"); s8Ret = M2M_ERR_BUS_FAIL; } return s8Ret; }
/* * @fn nm_i2c_write_reg * @brief write register * @param [in] u32Addr * Register address * @param [in] u32Val * Value to be written to the register * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure * @author M. Abdelmawla * @date 11 July 2012 * @version 1.0 */ sint8 nm_i2c_write_reg(uint32 u32Addr, uint32 u32Val) { tstrNmI2cDefault strI2c; uint8 b[16]; sint8 s8Ret = M2M_SUCCESS; if(u32Addr < 0xff) { /* clockless i2c */ b[0] = 0x19; b[1] = (uint8)(u32Addr); b[2] = (uint8)(u32Val); strI2c.u16Sz = 3; } else { b[0] = 0x90; b[1] = (uint8)(u32Addr >> 24); b[2] = (uint8)(u32Addr >> 16); b[3] = (uint8)(u32Addr >> 8); b[4] = (uint8)u32Addr; b[5] = 0x04; b[6] = (uint8)u32Val; b[7] = (uint8)(u32Val >> 8); b[8] = (uint8)(u32Val >> 16); b[9] = (uint8)(u32Val >> 24); strI2c.u16Sz = 10; } strI2c.pu8Buf = b; if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_W, &strI2c)) { M2M_ERR("write error\n"); s8Ret = M2M_ERR_BUS_FAIL; } return s8Ret; }
/* * @fn nm_i2c_read_reg_with_ret * @brief Read register with error code return * @param [in] u32Addr * Register address * @param [out] pu32RetVal * Pointer to u32 variable used to return the read value * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure * @author M. Abdelmawla * @date 11 July 2012 * @version 1.0 */ sint8 nm_i2c_read_reg_with_ret(uint32 u32Addr, uint32* pu32RetVal) { uint8 b[6]; uint8 rsz; tstrNmI2cDefault strI2c; sint8 s8Ret = M2M_SUCCESS; if(u32Addr < 0xff) { /* clockless i2c */ b[0] = 0x09; b[1] = (uint8)(u32Addr); rsz = 1; strI2c.u16Sz = 2; } else { b[0] = 0x80; b[1] = (uint8)(u32Addr >> 24); b[2] = (uint8)(u32Addr >> 16); b[3] = (uint8)(u32Addr >> 8); b[4] = (uint8)(u32Addr); b[5] = 0x04; rsz = 4; strI2c.u16Sz = 6; } strI2c.pu8Buf = b; if(M2M_SUCCESS == nm_bus_ioctl(NM_BUS_IOCTL_W, &strI2c)) { strI2c.u16Sz = rsz; if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strI2c)) { //M2M_ERR("read error\n"); s8Ret = M2M_ERR_BUS_FAIL; } } else { M2M_ERR("failed to send cfg bytes\n"); s8Ret = M2M_ERR_BUS_FAIL; } if (rsz == 1) { *pu32RetVal = b[0]; } else { *pu32RetVal = b[0] | ((uint32)b[1] << 8) | ((uint32)b[2] << 16) | ((uint32)b[3] << 24); } return s8Ret; }
/* * @fn nm_uart_sync_cmd * @brief Check COM Port * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure * @author Dina El Sissy * @date 13 AUG 2012 * @version 1.0 */ sint8 nm_uart_sync_cmd(void) { tstrNmUartDefault strUart; sint8 s8Ret = -1; uint8 b [HDR_SZ+1]; uint8 rsz; uint8 onchip = 0; /*read reg*/ b[0] = 0x12; rsz = 1; strUart.pu8Buf = b; strUart.u16Sz = 1; if(M2M_SUCCESS == nm_bus_ioctl(NM_BUS_IOCTL_W, &strUart)) { strUart.u16Sz = rsz; if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strUart)) { s8Ret = M2M_ERR_BUS_FAIL; } } else { M2M_ERR("failed to send cfg bytes\n"); s8Ret = M2M_ERR_BUS_FAIL; } if (b[0] == 0x5a) { s8Ret = 0; onchip = 1; M2M_INFO("Built-in WINC1500 UART Found\n"); } else if(b[0] == 0x5b) { s8Ret = 0; onchip = 0; M2M_INFO("WINC1500 Serial Bridge Found\n"); } /*TODO: this should be the way we read the register since the cortus is little endian*/ /**pu32RetVal = b[0] | ((uint32)b[1] << 8) | ((uint32)b[2] << 16) | ((uint32)b[3] << 24);*/ if(s8Ret == M2M_SUCCESS) s8Ret = (sint8)onchip; return s8Ret; }
/* * @fn nm_i2c_read_block * @brief Read block of data * @param [in] u32Addr * Start address * @param [out] puBuf * Pointer to a buffer used to return the read data * @param [in] u16Sz * Number of bytes to read. The buffer size must be >= u16Sz * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure * @author M. Abdelmawla * @date 11 July 2012 * @version 1.0 */ sint8 nm_i2c_read_block(uint32 u32Addr, uint8 *pu8Buf, uint16 u16Sz) { tstrNmI2cDefault strI2c; uint8 au8Buf[7]; sint8 s8Ret = M2M_SUCCESS; au8Buf[0] = 0x02; au8Buf[1] = (uint8)(u32Addr >> 24); au8Buf[2] = (uint8)(u32Addr >> 16); au8Buf[3] = (uint8)(u32Addr >> 8); au8Buf[4] = (uint8)(u32Addr >> 0); au8Buf[5] = (uint8)(u16Sz >> 8); au8Buf[6] = (uint8)(u16Sz); strI2c.pu8Buf = au8Buf; strI2c.u16Sz = sizeof(au8Buf); if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_W, &strI2c)) { M2M_ERR("write error\n"); s8Ret = M2M_ERR_BUS_FAIL; } else { strI2c.pu8Buf = pu8Buf; strI2c.u16Sz = u16Sz; if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strI2c)) { M2M_ERR("read error\n"); s8Ret = M2M_ERR_BUS_FAIL; } } return s8Ret; }
/** * @fn nm_uart_write_block * @brief Write block of data * @param [in] u32Addr * Start address * @param [in] puBuf * Pointer to the buffer holding the data to be written * @param [in] u16Sz * Number of bytes to write. The buffer size must be >= u16Sz * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure * @author Dina El Sissy * @date 13 AUG 2012 * @version 1.0 */ sint8 nm_uart_write_block(uint32 u32Addr, uint8 *puBuf, uint16 u16Sz) { tstrNmUartDefault strUart; sint8 s8Ret = M2M_SUCCESS; static uint8 au8Buf[HDR_SZ+1]; au8Buf[0] = 0xa5; au8Buf[1] = 3; au8Buf[2] = 0; au8Buf[3] = (uint8)(u16Sz & 0x00ff); au8Buf[4] = (uint8)((u16Sz & 0xff00)>>8); au8Buf[5] = (uint8)(u32Addr & 0x000000ff); au8Buf[6] = (uint8)((u32Addr & 0x0000ff00)>>8); au8Buf[7] = (uint8)((u32Addr & 0x00ff0000)>>16); au8Buf[8] = (uint8)((u32Addr & 0xff000000)>>24); au8Buf[9] = 0; au8Buf[10] = 0; au8Buf[11] = 0; au8Buf[12] = 0; au8Buf[2] = get_cs(&au8Buf[1],HDR_SZ); strUart.pu8Buf = au8Buf; strUart.u16Sz = sizeof(au8Buf); if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_W, &strUart)) { M2M_ERR("write error\n"); s8Ret = M2M_ERR_BUS_FAIL; } else { if(!nm_bus_get_chip_type()) { //check for the ack from the SAMD21 for the packet reception. strUart.u16Sz = 1; if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strUart)) { s8Ret = M2M_ERR_BUS_FAIL; } if(au8Buf[0] == 0xAC) { M2M_DBG("Successfully sent the block Write command\n"); strUart.pu8Buf = puBuf; strUart.u16Sz = u16Sz; if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_W, &strUart)) { M2M_ERR("write error\n"); s8Ret = M2M_ERR_BUS_FAIL; } else { //check for the ack from the SAMD21 for the payload reception. strUart.pu8Buf = au8Buf; strUart.u16Sz = 1; if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strUart)) { s8Ret = M2M_ERR_BUS_FAIL; } if(au8Buf[0] == 0xAC) { M2M_DBG("Successfully sent the data payload\n"); } else { M2M_ERR("write error\n"); s8Ret = M2M_ERR_BUS_FAIL; } } } else { M2M_ERR("write error (Error sending the block write command)\n"); s8Ret = M2M_ERR_BUS_FAIL; } } else { strUart.pu8Buf = puBuf; strUart.u16Sz = u16Sz; if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_W, &strUart)) { M2M_ERR("write error\n"); s8Ret = M2M_ERR_BUS_FAIL; } } } return s8Ret; }
sint8 nm_uart_read_reg_with_ret(uint32 u32Addr, uint32* pu32RetVal) { tstrNmUartDefault strUart; sint8 s8Ret = M2M_SUCCESS; uint8 b [HDR_SZ+1]; uint8 rsz; /*read reg*/ b[0] = 0xa5; b[1] = 0; b[2] = 0; b[3] = 0; b[4] = 0; b[5] = (uint8)(u32Addr & 0x000000ff); b[6] = (uint8)((u32Addr & 0x0000ff00)>>8); b[7] = (uint8)((u32Addr & 0x00ff0000)>>16); b[8] = (uint8)((u32Addr & 0xff000000)>>24); b[9] = 0; b[10] = 0; b[11] = 0; b[12] = 0; b[2] = get_cs(&b[1],HDR_SZ); rsz = 4; strUart.pu8Buf = b; strUart.u16Sz = sizeof(b); if(M2M_SUCCESS == nm_bus_ioctl(NM_BUS_IOCTL_W, &strUart)) { if(!nm_bus_get_chip_type()) { strUart.u16Sz = 1; if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strUart)) { s8Ret = M2M_ERR_BUS_FAIL; } if(b[0] == 0xAC) { M2M_DBG("Successfully sent the command\n"); strUart.u16Sz = rsz; if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strUart)) { s8Ret = M2M_ERR_BUS_FAIL; } } else { s8Ret = M2M_ERR_BUS_FAIL; } } else { strUart.u16Sz = rsz; if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strUart)) { s8Ret = M2M_ERR_BUS_FAIL; } } } else { M2M_ERR("failed to send cfg bytes\n"); s8Ret = M2M_ERR_BUS_FAIL; } /*TODO: this should be the way we read the register since the cortus is little endian*/ /**pu32RetVal = b[0] | ((uint32)b[1] << 8) | ((uint32)b[2] << 16) | ((uint32)b[3] << 24);*/ *pu32RetVal = ((uint32)b[0] << 24) | ((uint32)b[1] << 16) | ((uint32)b[2] << 8) | b[3]; return s8Ret; }