/** * Allocates a new thread ID. This depends on the scheduler lock. */ static scheduler_tid_t scheduler_new_tid(void) { mutex_take_spin(&scheduler_lock); next_tid++; mutex_give(&scheduler_lock); return next_tid; }
bool i2c_eeprom_master_write(Twi *twi, const uint16_t internal_address, const char *data, const uint16_t length) { uint32_t timeout; mutex_take(mutex_twi_bricklet, MUTEX_BLOCKING); // Start write TWI_StartWrite(twi, bricklet_eeprom_address, internal_address, I2C_EEPROM_INTERNAL_ADDRESS_BYTES, data[0]); for(uint16_t i = 1; i < length; i++) { timeout = 0; // Wait until byte is sent, otherwise return false while(!TWI_ByteSent(twi) && (++timeout < I2C_EEPROM_TIMEOUT)) {} if(timeout == I2C_EEPROM_TIMEOUT) { logieew("write timeout (nothing sent)\n\r"); mutex_give(mutex_twi_bricklet); return false; } TWI_WriteByte(twi, data[i]); } // Send STOP TWI_SendSTOPCondition(twi); timeout = 0; // Wait for transfer to be complete while(!TWI_TransferComplete(twi) && (++timeout < I2C_EEPROM_TIMEOUT)) {} if (timeout == I2C_EEPROM_TIMEOUT) { logieew("write timeout (transfer incomplete)\n\r"); mutex_give(mutex_twi_bricklet); return false; } // Wait at least 5ms between writes (see m24128-bw.pdf) SLEEP_MS(5); mutex_give(mutex_twi_bricklet); return true; }
// i2c_eeprom_master_read/write are based on twid.c from atmels at91lib and // adapted for better handling when there is no eeprom present. // This handling is needed for the bricklet initialization bool i2c_eeprom_master_read(Twi *twi, const uint16_t internal_address, char *data, const uint16_t length) { uint32_t timeout; mutex_take(mutex_twi_bricklet, MUTEX_BLOCKING); // Start read TWI_StartRead(twi, bricklet_eeprom_address, internal_address, I2C_EEPROM_INTERNAL_ADDRESS_BYTES); for(uint16_t i = 0; i < length; i++) { // If last Byte -> send STOP if(i == length-1) { TWI_Stop(twi); } uint32_t timeout = 0; // Wait until byte is received, otherwise return false while(!TWI_ByteReceived(twi) && (++timeout < I2C_EEPROM_TIMEOUT)); if(timeout == I2C_EEPROM_TIMEOUT) { logieew("read timeout (nothing received)\n\r"); mutex_give(mutex_twi_bricklet); return false; } data[i] = TWI_ReadByte(twi); } timeout = 0; // Wait for transfer to be complete while(!TWI_TransferComplete(twi) && (++timeout < I2C_EEPROM_TIMEOUT)); if (timeout == I2C_EEPROM_TIMEOUT) { logieew("read timeout (transfer incomplete)\n\r"); mutex_give(mutex_twi_bricklet); return false; } mutex_give(mutex_twi_bricklet); return true; }
void bmo_read_registers(const uint8_t reg, uint8_t *data, const uint8_t length) { mutex_take(mutex_twi_bricklet, MUTEX_BLOCKING); TWID_Read(&twid, BMO055_ADDRESS_HIGH, reg, 1, data, length, NULL); mutex_give(mutex_twi_bricklet); }
void bmo_write_register(const uint8_t reg, uint8_t const value) { mutex_take(mutex_twi_bricklet, MUTEX_BLOCKING); TWID_Write(&twid, BMO055_ADDRESS_HIGH, reg, 1, (uint8_t *)&value, 1, NULL); mutex_give(mutex_twi_bricklet); }
int32 EventLoop::processOps() { SDK_LOG(LOG_LEVEL_TRACE, "EventLoop::processOps"); Op *op = NULL; char buf[1024]; int32 loop = sizeof(buf); while (m_ops.size()){ mutex_take(&m_ops_mtx); if (m_ops.size() >= (int32)sizeof(op)){ m_ops.read((uint8*)&op, sizeof(op)); mutex_give(&m_ops_mtx); } else{ mutex_give(&m_ops_mtx); break; } if (op) { SmartOp sp(op); sp->process(this); if (sp.release() > 0 && sp.get()) { CliConnMap::iterator it = m_conns.find(sp->getCid()); if (it != m_conns.end()) { CliConn* conn = it->second; conn->addTimer(sp->getSN(), sp); } } } } while (loop >= (int32)sizeof(buf)){ loop = recv(m_ctlfdr, buf, sizeof(buf), 0); } return 0; }
int32 EventLoop::asynAddOp(Op* op) { if (m_ctlfdw != INVALID_SOCKET) { if (op) { mutex_take(&m_ops_mtx); m_ops.auto_resize_write((const uint8*)&op, sizeof(op)); mutex_give(&m_ops_mtx); } char ctl = 0; int32 ret = send(m_ctlfdw, &ctl, sizeof(ctl), 0); return ret; } return -1; }