void serialSendString(uint8_t id, const char *s) { for (; *s; ++s) { serialSendByte(id, *s); if (*s == '\n') { // HACK: fix the newlines serialSendByte(id, '\r'); } } }
bool amb8420SetDstAddress(uint8_t dstAddress) { bool ok; Handle_t handle; RPRINTF("set dst addr to %#02x\n", dstAddress); // disable radio rx/tx AMB8420ModeContext_t ctx; AMB8420_ENTER_STANDBY_MODE(ctx); // better be safe than sorry - treat 0 as broadcast too if (dstAddress == 0) dstAddress = 0xff; // Wait for device to become ready AMB8420_WAIT_FOR_RTS_READY(ok); if (!ok) goto end; // PRINTF("set dst, tar=%u\n", TAR); // ATOMIC_START_TIMESAVE(handle); AMB8420_SERIAL_CAPTURE(ok); commandInProgress = AMB8420_CMD_SET_DESTADDR_REQ; serialSendByte(AMB8420_UART_ID, AMB8420_START_DELIMITER); serialSendByte(AMB8420_UART_ID, AMB8420_CMD_SET_DESTADDR_REQ); serialSendByte(AMB8420_UART_ID, 0x1); serialSendByte(AMB8420_UART_ID, dstAddress); serialSendByte(AMB8420_UART_ID, AMB8420_START_DELIMITER ^ AMB8420_CMD_SET_DESTADDR_REQ ^ 0x1 ^ dstAddress); AMB8420_SERIAL_FREE(); // ATOMIC_END_TIMESAVE(handle); // PRINTF("end set dst, tar=%u\n", TAR); // wait for reply INTERRUPT_ENABLED_START(handle); BUSYWAIT_UNTIL(!commandInProgress, TIMER_100_MS, ok); // if (commandInProgress) PRINTF("set dst addr: command wait fail\n"); INTERRUPT_ENABLED_END(handle); // if (!ok) goto end; // Wait for device to become ready AMB8420_WAIT_FOR_RTS_READY(ok); end: AMB8420_RESTORE_MODE(ctx); // PRINTF(" ok = %d\n", (int)ok); // if (!ok) amb8420Reset(); return ok; }
int putchar(int c) { if (!spInPutchar) { GET_SP(spInPutchar); } serialSendByte(PRINTF_SERIAL_ID, c); return c; }
int amb8420GetRSSI(void) { int result = 0; bool ok; Handle_t handle; AMB8420ModeContext_t ctx; AMB8420_ENTER_STANDBY_MODE(ctx); // Wait for device to become ready AMB8420_WAIT_FOR_RTS_READY(ok); if (!ok) goto end; // ATOMIC_START_TIMESAVE(handle); AMB8420_SERIAL_CAPTURE(ok); commandInProgress = AMB8420_CMD_RSSI_REQ; serialSendByte(AMB8420_UART_ID, AMB8420_START_DELIMITER); serialSendByte(AMB8420_UART_ID, AMB8420_CMD_RSSI_REQ); serialSendByte(AMB8420_UART_ID, 0x00); serialSendByte(AMB8420_UART_ID, AMB8420_START_DELIMITER ^ AMB8420_CMD_RSSI_REQ); AMB8420_SERIAL_FREE(); // ATOMIC_END_TIMESAVE(handle); // if this code was executed with interrupts disabled, // the rx callback will never be called. so make sure // interrupts are on. INTERRUPT_ENABLED_START(handle); // wait for reply AMB8420_BUSYWAIT_UNTIL_NORET(!commandInProgress, TIMER_100_MS); result = lastRssi; // disable interrups, if required INTERRUPT_ENABLED_END(handle); // Wait for device to become ready AMB8420_WAIT_FOR_RTS_READY(ok); end: AMB8420_RESTORE_MODE(ctx); return result; }
void amb8420SetChannel(int channel) { bool ok; Handle_t handle; AMB8420ModeContext_t ctx; AMB8420_ENTER_STANDBY_MODE(ctx); channel &= 0xff; // one byte channels only RPRINTF("set channel to %d\n", channel); // Wait for device to become ready AMB8420_WAIT_FOR_RTS_READY(ok); if (!ok) goto end; // ATOMIC_START_TIMESAVE(handle); AMB8420_SERIAL_CAPTURE(ok); commandInProgress = AMB8420_CMD_SET_CHANNEL_REQ; serialSendByte(AMB8420_UART_ID, AMB8420_START_DELIMITER); serialSendByte(AMB8420_UART_ID, AMB8420_CMD_SET_CHANNEL_REQ); serialSendByte(AMB8420_UART_ID, 0x1); serialSendByte(AMB8420_UART_ID, channel); serialSendByte(AMB8420_UART_ID, AMB8420_START_DELIMITER ^ AMB8420_CMD_SET_CHANNEL_REQ ^ 0x1 ^ channel); AMB8420_SERIAL_FREE(); // ATOMIC_END_TIMESAVE(handle); // wait for reply INTERRUPT_ENABLED_START(handle); AMB8420_BUSYWAIT_UNTIL(!commandInProgress, TIMER_100_MS, ok); INTERRUPT_ENABLED_END(handle); // Wait for device to become ready AMB8420_WAIT_FOR_RTS_READY(ok); end: AMB8420_RESTORE_MODE(ctx); return; }
// for debugging // static int amb8420GetAll(void) { bool ok; uint8_t crc; Handle_t handle; const uint8_t position = 0; const uint8_t txLen = 0x80; // Wait for device to become ready AMB8420_WAIT_FOR_RTS_READY(ok); if (!ok) goto end; // ATOMIC_START_TIMESAVE(handle); AMB8420_SERIAL_CAPTURE(ok); commandInProgress = AMB8420_CMD_GET_REQ; serialSendByte(AMB8420_UART_ID, AMB8420_START_DELIMITER); serialSendByte(AMB8420_UART_ID, AMB8420_CMD_GET_REQ); serialSendByte(AMB8420_UART_ID, 2); serialSendByte(AMB8420_UART_ID, position); serialSendByte(AMB8420_UART_ID, txLen); crc = AMB8420_START_DELIMITER ^ AMB8420_CMD_GET_REQ ^ 2 ^ position ^ txLen; serialSendByte(AMB8420_UART_ID, crc); AMB8420_SERIAL_FREE(); // ATOMIC_END_TIMESAVE(handle); // wait for reply // if someone grabs access to serial during this waiting, // then it will fail, but that's ok, as the caller will retry. INTERRUPT_ENABLED_START(handle); AMB8420_BUSYWAIT_UNTIL(!commandInProgress, TIMER_100_MS, ok); INTERRUPT_ENABLED_END(handle); #if RADIO_DEBUG PRINTF("recvLength=%u\n", recvLength); if (recvLength > 2 && rxBuffer[0] == position && rxBuffer[1] == txLen) { debugHexdump(rxBuffer + 2, recvLength - 2); } #endif // Wait for device to become ready AMB8420_WAIT_FOR_RTS_READY(ok); end: return ok ? 0 : -EBUSY; }
static int amb8420Set(uint8_t position, uint8_t len, uint8_t *data) { bool ok; uint8_t crc, i; Handle_t handle; // Wait for device to become ready AMB8420_WAIT_FOR_RTS_READY(ok); if (!ok) goto end; // ATOMIC_START_TIMESAVE(handle); AMB8420_SERIAL_CAPTURE(ok); commandInProgress = AMB8420_CMD_SET_REQ; serialSendByte(AMB8420_UART_ID, AMB8420_START_DELIMITER); serialSendByte(AMB8420_UART_ID, AMB8420_CMD_SET_REQ); serialSendByte(AMB8420_UART_ID, len + 2); serialSendByte(AMB8420_UART_ID, position); serialSendByte(AMB8420_UART_ID, len); crc = AMB8420_START_DELIMITER ^ AMB8420_CMD_SET_REQ ^ (len + 2) ^ position ^ len; for (i = 0; i < len; i++) { serialSendByte(AMB8420_UART_ID, data[i]); crc ^= data[i]; } serialSendByte(AMB8420_UART_ID, crc); AMB8420_SERIAL_FREE(); // ATOMIC_END_TIMESAVE(handle); // wait for reply. // if someone grabs access to serial during this waiting, // then it will fail, but that's ok, as the caller will retry. INTERRUPT_ENABLED_START(handle); AMB8420_BUSYWAIT_UNTIL(!commandInProgress, TIMER_100_MS, ok); INTERRUPT_ENABLED_END(handle); // Wait for device to become ready AMB8420_WAIT_FOR_RTS_READY(ok); end: return ok ? 0 : -EBUSY; }
static int amb8420Get1b(uint8_t position, uint8_t *value) { bool ok; uint8_t crc; Handle_t handle; uint8_t rxLen; uint8_t rxPosition; bool hasRxAnswer = false; // const uint8_t position = 0; const uint8_t txLen = 0x1; // Wait for device to become ready AMB8420_WAIT_FOR_RTS_READY(ok); if (!ok) goto end; // ATOMIC_START_TIMESAVE(handle); AMB8420_SERIAL_CAPTURE(ok); commandInProgress = AMB8420_CMD_GET_REQ; serialSendByte(AMB8420_UART_ID, AMB8420_START_DELIMITER); serialSendByte(AMB8420_UART_ID, AMB8420_CMD_GET_REQ); serialSendByte(AMB8420_UART_ID, 2); serialSendByte(AMB8420_UART_ID, position); serialSendByte(AMB8420_UART_ID, txLen); crc = AMB8420_START_DELIMITER ^ AMB8420_CMD_GET_REQ ^ 2 ^ position ^ txLen; serialSendByte(AMB8420_UART_ID, crc); AMB8420_SERIAL_FREE(); // ATOMIC_END_TIMESAVE(handle); // wait for reply // if someone grabs access to serial during this waiting, // then it will fail, but that's ok, as the caller will retry. INTERRUPT_ENABLED_START(handle); AMB8420_BUSYWAIT_UNTIL(!commandInProgress, TIMER_100_MS, ok); INTERRUPT_ENABLED_END(handle); #if RADIO_DEBUG PRINTF("recvLength=%u\n", recvLength); PRINTF("%02x %02x %02x\n", rxBuffer[0], rxBuffer[1], rxBuffer[2]); #endif if (recvLength != 3) { #if RADIO_DEBUG if (recvLength > 3) { debugHexdump(rxBuffer + 2, recvLength - 2); } #endif goto end; } rxPosition = rxBuffer[0]; rxLen = rxBuffer[1]; if (rxPosition != position || rxLen != 1) { goto end; } hasRxAnswer = true; *value = rxBuffer[2]; // Wait for device to become ready AMB8420_WAIT_FOR_RTS_READY(ok); end: if (!ok) return -EBUSY; if (!hasRxAnswer) return -EIO; return 0; }
int amb8420Send(const void *header_, uint16_t headerLen, const void *data_, uint16_t dataLen) { // RPRINTF("amb8420Send, size=%u\n", headerLen + dataLen); // int result = -EBUSY; AMB8420ModeContext_t ctx; AMB8420_ENTER_ACTIVE_MODE(ctx); const uint8_t *header = header_; const uint8_t *data = data_; uint8_t totalLen = headerLen + dataLen; bool ok; // Handle_t h; if (initResult == AMB8420_INIT_HARD_FAIL) { // ok = true; // return as if success to disable retrying // goto end; return -EIO; } AMB8420_WAIT_FOR_RTS_READY(ok); if (!ok) { amb8420Reset(); // XXX: reset the radio in hope it will help // blueLedOn(); goto end; } uint8_t cs = AMB8420_START_DELIMITER ^ AMB8420_CMD_DATA_REQ; cs ^= totalLen; // PRINTF("send, tar=%u\n", TAR); // ATOMIC_START_TIMESAVE(h); AMB8420_SERIAL_CAPTURE(ok); // start delimiter serialSendByte(AMB8420_UART_ID, AMB8420_START_DELIMITER); // command serialSendByte(AMB8420_UART_ID, AMB8420_CMD_DATA_REQ); // data length serialSendByte(AMB8420_UART_ID, totalLen); // data uint16_t i; for (i = 0; i < headerLen; ++i) { cs ^= header[i]; serialSendByte(AMB8420_UART_ID, header[i]); } for (i = 0; i < dataLen; ++i) { cs ^= data[i]; serialSendByte(AMB8420_UART_ID, data[i]); } // checksum serialSendByte(AMB8420_UART_ID, cs); AMB8420_SERIAL_FREE(); // ATOMIC_END_TIMESAVE(h); // PRINTF("end send, tar=%u\n", TAR); AMB8420_RESTORE_MODE(ctx); end: return ok ? 0 : -EBUSY; }
void serialSendData(uint8_t id, const uint8_t *data, uint16_t len) { const uint8_t *p; for (p = data; p < data + len; ++p) { serialSendByte(id, *p); } }