/** * Connects to a cheetah spi interface. * @param port * The port if the interface. * @param mode * The spi mode (0-3) * @param baudrate * The baudrate of the interface (kHz). * @return * True on success. */ bool CheetahSpi::connectToDevice(quint32 port, qint16 mode, quint32 baudrate) { bool result = true; if (m_handle > 0) { ch_close(m_handle); m_handle = 0; } m_handle = ch_open(port); if (m_handle > 0) { ch_spi_configure(m_handle, (CheetahSpiPolarity)(mode >> 1), (CheetahSpiPhase)(mode & 1), CH_SPI_BITORDER_MSB, 0x0); // Power the flash using the Cheetah adapter's power supply. ch_target_power(m_handle, CH_TARGET_POWER_ON); ch_sleep_ms(100); int currentBaudrate = ch_spi_bitrate(m_handle, (int)baudrate); if (currentBaudrate != (int)baudrate) { ch_close(m_handle); m_handle = 0; } else { ch_spi_queue_ss(m_handle, 0); } }
static int fill(struct channel *ch, struct buffer *bu) { ssize_t res; if (!ch->connected) return -1; if (bu->max == bu->cur) return -1; if (bu->cur) return 0; do { assert(ch->fd > 0); assert(bu->cur < bu->max); res = recv(ch->fd, bu->data + bu->cur, bu->max - bu->cur, 0); } while (res < 0 && errno == EINTR); if (res <= 0) { if (res < 0) sys_error(); ch_close(ch); return -1; } bu->cur += res; return 0; }
/** * Disconnects from the cheetah spi interface. */ void CheetahSpi::disconnect(void) { if (m_handle > 0) { ch_close(m_handle); m_handle = 0; } }
//========================================================================= // MAIN PROGRAM //========================================================================= int main (int argc, char *argv[]) { Cheetah handle; int port = 0; int bitrate = 0; int mode = 0; if (argc < 4) { printf("usage: timing PORT BITRATE MODE\n"); printf("\n"); printf("MODE possibilities are:\n"); printf(" mode 0 : pol = 0, phase = 0\n"); printf(" mode 1 : pol = 0, phase = 1\n"); printf(" mode 2 : pol = 1, phase = 0\n"); printf(" mode 3 : pol = 1, phase = 1\n"); return 1; } port = atoi(argv[1]); bitrate = atoi(argv[2]); mode = atoi(argv[3]); // Open the device handle = ch_open(port); if (handle <= 0) { printf("Unable to open Cheetah device on port %d\n", port); printf("Error code = %d (%s)\n", handle, ch_status_string(handle)); return 1; } printf("Opened Cheetah device on port %d\n", port); printf("Host interface is %s\n", (ch_host_ifce_speed(handle)) ? "high speed" : "full speed"); fflush(stdout); // Ensure that the SPI subsystem is configured ch_spi_configure(handle, (mode >> 1), mode & 1, CH_SPI_BITORDER_MSB, 0x0); printf("SPI configuration set to mode %d, MSB shift, SS[2:0] active low\n", mode); fflush(stdout); // Power the target using the Cheetah adapter's power supply ch_target_power(handle, CH_TARGET_POWER_ON); ch_sleep_ms(100); // Set the bitrate bitrate = ch_spi_bitrate(handle, bitrate); printf("Bitrate set to %d kHz\n", bitrate); fflush(stdout); _timing(handle); // Close and exit ch_close(handle); return 0; }
static void *worker_loop(void *p) { struct work_info *wi = p; struct channel ch; while (wi->active) { if (net_accept(wi->li, &ch)) break; ht_process(&ch); ch_close(&ch); } return NULL; }
static void *worker_loop(void *p) { struct work_info *wi = p; struct channel ch; char desc[40]; struct net_socket sock; while (wi->active) { if (net_accept(&wi->li->sock, &sock, sizeof(desc), desc)) break; ch_init(&ch, sock, desc); ht_process(&ch); ch_close(&ch); } return NULL; }
static int ht_method(struct channel *ch, struct buffer *bu, size_t method_len, char *method_str, size_t uri_len, char *uri_str, size_t version_len, char *version_str) { struct methodline_state ms; struct buffer method, uri, version; buffer_init(&method, method_str, method_len); buffer_init(&uri, uri_str, uri_len); buffer_init(&version, version_str, version_len); ms_init(&ms); while (!ch_fill(ch)) { if (methodline_parse(bu, &ms, &method, &uri, &version)) { ch_close(ch); return -1; } if (ms.done) break; } return 0; }
static int ht_headers(struct channel *ch, struct buffer *bu, struct env *env) { struct headers_state hs; char name_str[80]; char value_str[2048]; struct buffer name = buffer_wrap(name_str); struct buffer value = buffer_wrap(value_str); buffer_reset(&name); buffer_reset(&value); env_init(env); hs_init(&hs); while (!ch_fill(ch)) { if (headers_parse(bu, &hs, &name, &value, env)) { ch_close(ch); return -1; } if (hs.done) break; } return 0; }