void menu_i2c_write_to_eeprom_func(const struct menu_item *m, bool checked) { int i; size_t wr_status = 0; HW_I2C_ABORT_SOURCE abrt_src = HW_I2C_ABORT_NONE; uint8_t addr[2] = {0x00, 0x00}; static uint8_t write_buffer[65]; // 64 bytes of data + '\0' set_target_address(EEPROM_ADDRESS); /* * Generate random data (some pattern). */ for (i = 0; i < sizeof(write_buffer) - 1; i++) { write_buffer[i] = rand() % 96 + 32; } /* * Acquire I2C resource and write data to EEPROM. First two bytes determine memory address * from which data will be written. First byte is high byte address and the second one is * low byte address. In that case data will be stored starting from address 0. Next bytes * are data. Despite 2 separate write calls below, writing is still goes as one transfer * on the bus because it is done via FIFO and as long as FIFO is non-empty controller will * continue to send data and will generate stop condition only when FIFO is empty. Notice * also the use of flags between the 2 calls that ensure that no STOP condition will be * generated between them. * After writing operation release I2C resource. */ resource_acquire(RES_MASK(RES_ID_I2C1), RES_WAIT_FOREVER); wr_status = hw_i2c_write_buffer_sync(HW_I2C1, addr, sizeof(addr), &abrt_src, HW_I2C_F_NONE); if ((wr_status < sizeof(addr)) || (abrt_src != HW_I2C_ABORT_NONE)) { printf("EEPROM address write during write failed: %u" NEWLINE, abrt_src); } else { wr_status = hw_i2c_write_buffer_sync(HW_I2C1, write_buffer, sizeof(write_buffer) - 1, NULL, HW_I2C_F_WAIT_FOR_STOP); if ((wr_status < (ssize_t)sizeof(write_buffer)) || (abrt_src != HW_I2C_ABORT_NONE)) { printf("EEPROM write failure: %u" NEWLINE, abrt_src); } else { /* * Wait for ACK from EEPROM. */ eeprom_poll_ack(HW_I2C1); /* * Print on UART what was generated and written in EEPROM. */ printf("written to EEPROM: %s" NEWLINE, write_buffer); } } resource_release(RES_MASK(RES_ID_I2C1)); }
void task_i2c_get_temp_init(const struct task_item *task) { static const uint8_t conf_reg = 0x00; static const uint8_t hyst_reg[2] = {0x4B, 0x00}; static const uint8_t os_reg[2] = {0x50, 0x00}; /* * Create event to wait on, when task should not read temperature from sensor. */ OS_EVENT_CREATE(event); /* * Set FM75 with initial values which are at the same time default values of configuration * register of the sensor after power up. */ set_target_address(FM75_ADDRESS); fm75_write_reg(FM75_REG_CONF, &conf_reg, sizeof(conf_reg)); fm75_write_reg(FM75_REG_THYST, hyst_reg, sizeof(hyst_reg)); fm75_write_reg(FM75_REG_TOS, os_reg, sizeof(os_reg)); }
void task_i2c_get_temp_func(const struct task_item *task) { uint8_t temp[2]; int t_out, fract; /* * Wait in OS friendly way for request to run. */ if (!read_temp_enabled) { OS_EVENT_WAIT(event, OS_EVENT_FOREVER); } /* * Require I2C for exclusively reading temperature from FM75 and release it after operation. */ resource_acquire(RES_MASK(RES_ID_I2C1), RES_WAIT_FOREVER); set_target_address(FM75_ADDRESS); /* * Read actual temperature values from FM75. */ fm75_read_reg(FM75_REG_TEMP, temp, sizeof(temp)); resource_release(RES_MASK(RES_ID_I2C1)); /* * Send results to UART. */ t_out = convert_temp(temp, &fract); printf("current temperature: %d.%04d C" NEWLINE, t_out, fract); /* * Wait 1 second to get temperature again. */ OS_DELAY(1000); }
//! The method sets the address of the remote host where log records will be sent to. BOOST_LOG_API void syslog_backend::set_target_address(std::string const& addr, unsigned short port) { #if !defined(BOOST_LOG_NO_THREADS) typedef implementation::udp_socket_based udp_socket_based_impl; if (udp_socket_based_impl* impl = dynamic_cast< udp_socket_based_impl* >(m_pImpl)) { char service_name[std::numeric_limits< int >::digits10 + 3]; boost::log::aux::snprintf(service_name, sizeof(service_name), "%d", static_cast< int >(port)); asio::ip::udp::resolver::query q(impl->m_Protocol, addr, service_name, asio::ip::resolver_query_base::address_configured); asio::ip::udp::endpoint remote_address; { lock_guard< mutex > _(impl->m_pService->m_Mutex); remote_address = *impl->m_pService->m_HostNameResolver.resolve(q); } impl->m_TargetHost = remote_address; } #else // Boost.ASIO requires threads for the host name resolver, // so without threads we simply assume the string already contains IP address set_target_address(boost::asio::ip::address::from_string(addr), port); #endif // !defined(BOOST_LOG_NO_THREADS) }
void menu_i2c_read_from_eeprom_func(const struct menu_item *m, bool checked) { size_t rd_status, wr_status; HW_I2C_ABORT_SOURCE abrt_src = HW_I2C_ABORT_NONE; uint8_t addr[2] = {0x00, 0x00}; static uint8_t read_buffer[65]; // 64 bytes of data + '\0' set_target_address(EEPROM_ADDRESS); /* * Acquire I2C resource and read data back from EEPROM and release it after operation. * Before reading data we need to set address from which reading will start. This is done * by writing two bytes indicating address in EEPROM before reading from it. I2C controller * will automatically generate proper write and read commands on I2C bus. */ resource_acquire(RES_MASK(RES_ID_I2C1), RES_WAIT_FOREVER); wr_status = hw_i2c_write_buffer_sync(HW_I2C1, addr, sizeof(addr), &abrt_src, HW_I2C_F_NONE); if ((wr_status < sizeof(addr)) || (abrt_src != HW_I2C_ABORT_NONE)) { printf("EEPROM address write during read failed: %u" NEWLINE, abrt_src); } else { rd_status = hw_i2c_read_buffer_sync(HW_I2C1, read_buffer, sizeof(read_buffer) - 1, &abrt_src, HW_I2C_F_NONE); /* * Print on UART what was read from EEPROM. */ if ((rd_status < sizeof(read_buffer)) || (abrt_src != HW_I2C_ABORT_NONE)) { printf("EEPROM read failure: %u" NEWLINE, abrt_src); } else { printf("read from EEPROM: %s" NEWLINE, read_buffer); } } resource_release(RES_MASK(RES_ID_I2C1)); }