Files
barnum/ch9120.c

181 lines
5.1 KiB
C
Raw Normal View History

2025-07-26 21:48:13 -04:00
#include "ch9120.h"
static uint8_t CH9120_Mode = TCP_SERVER;
static uint8_t CH9120_LOCAL_IP[4] = {192, 168, 1, 201};
static uint8_t CH9120_GATEWAY[4] = {192, 168, 1, 1};
static uint8_t CH9120_SUBNET_MASK[4] = {255, 255, 0, 0};
static uint8_t CH9120_TARGET_IP[4] = {192, 168, 1, 10};
static uint16_t CH9120_PORT1 = 1000;
static uint16_t CH9120_TARGET_PORT = 2000;
static uint32_t CH9120_BAUD_RATE = 115200;
static uint8_t tx[8] = {0x57, 0xAB};
static void CH9120_TX_4_bytes(uint8_t data, int command) {
for (int i = 2; i < 4; i++) {
if (i == 2)
tx[i] = command;
else
tx[i] = data;
}
sleep_ms(10);
for (int o = 0; o < 4; o++)
uart_putc(UART_ID1, tx[o]);
sleep_ms(10);
for (int i = 2; i < 4; i++)
tx[i] = 0;
}
static void CH9120_TX_5_bytes(uint16_t data, int command) {
uint8_t Port[2];
Port[0] = data & 0xff;
Port[1] = data >> 8;
for (int i = 2; i < 5; i++) {
if (i == 2)
tx[i] = command;
else
tx[i] = Port[i - 3];
}
sleep_ms(10);
for (int o = 0; o < 5; o++)
uart_putc(UART_ID1, tx[o]);
sleep_ms(10);
for (int i = 2; i < 5; i++)
tx[i] = 0;
}
static void CH9120_TX_7_bytes(uint8_t data[], int command) {
for (int i = 2; i < 7; i++) {
if (i == 2)
tx[i] = command;
else
tx[i] = data[i - 3];
}
sleep_ms(10);
for (int o = 0; o < 7; o++)
uart_putc(UART_ID1, tx[o]);
sleep_ms(10);
for (int i = 2; i < 7; i++)
tx[i] = 0;
}
static void CH9120_TX_BAUD(uint32_t data, int command) {
uint8_t Port[4];
Port[0] = (data & 0xff);
Port[1] = (data >> 8) & 0xff;
Port[2] = (data >> 16) & 0xff;
Port[3] = data >> 24;
for (int i = 2; i < 7; i++) {
if (i == 2)
tx[i] = command;
else
tx[i] = Port[i - 3];
}
sleep_ms(10);
for (int o = 0; o < 7; o++)
uart_putc(UART_ID1, tx[o]);
sleep_ms(10);
for (int i = 2; i < 7; i++)
tx[i] = 0;
}
static void CH9120_Start() {
// Hard reset the CH9120
gpio_put(RES_PIN, 0);
sleep_ms(100);
gpio_put(RES_PIN, 1);
sleep_ms(100);
// Enter configuration mode
gpio_put(CFG_PIN, 0);
sleep_ms(500);
}
static void CH9120_End() {
tx[2] = 0x0d;
uart_write_blocking(UART_ID1, tx, 3);
sleep_ms(200);
tx[2] = 0x0e;
uart_write_blocking(UART_ID1, tx, 3);
sleep_ms(200);
tx[2] = 0x5e;
uart_write_blocking(UART_ID1, tx, 3);
sleep_ms(200);
gpio_put(CFG_PIN, 1);
}
void CH9120_init(void) {
uart_init(UART_ID1, INIT_BAUD_RATE);
gpio_set_function(UART_TX_PIN1, GPIO_FUNC_UART);
gpio_set_function(UART_RX_PIN1, GPIO_FUNC_UART);
gpio_init(CFG_PIN);
gpio_init(RES_PIN);
gpio_set_dir(CFG_PIN, GPIO_OUT);
gpio_set_dir(RES_PIN, GPIO_OUT);
printf("CH9120 Configuration:\n");
printf("Mode: %s\n", (CH9120_Mode == TCP_SERVER) ? "TCP_SERVER" : "TCP_CLIENT");
printf("Local IP: %d.%d.%d.%d\n", CH9120_LOCAL_IP[0], CH9120_LOCAL_IP[1], CH9120_LOCAL_IP[2], CH9120_LOCAL_IP[3]);
printf("Gateway: %d.%d.%d.%d\n", CH9120_GATEWAY[0], CH9120_GATEWAY[1], CH9120_GATEWAY[2], CH9120_GATEWAY[3]);
printf("Subnet: %d.%d.%d.%d\n", CH9120_SUBNET_MASK[0], CH9120_SUBNET_MASK[1], CH9120_SUBNET_MASK[2], CH9120_SUBNET_MASK[3]);
printf("Local Port: %d\n", CH9120_PORT1);
printf("Starting CH9120 configuration...\n");
CH9120_Start();
printf("Setting mode...\n");
CH9120_TX_4_bytes(CH9120_Mode, Mode1);
printf("Setting local IP...\n");
CH9120_TX_7_bytes(CH9120_LOCAL_IP, LOCAL_IP);
printf("Setting subnet mask...\n");
CH9120_TX_7_bytes(CH9120_SUBNET_MASK, SUBNET_MASK);
printf("Setting gateway...\n");
CH9120_TX_7_bytes(CH9120_GATEWAY, GATEWAY);
printf("Setting target IP...\n");
CH9120_TX_7_bytes(CH9120_TARGET_IP, TARGET_IP1);
printf("Setting local port...\n");
CH9120_TX_5_bytes(CH9120_PORT1, LOCAL_PORT1);
printf("Setting target port...\n");
CH9120_TX_5_bytes(CH9120_TARGET_PORT, TARGET_PORT1);
printf("Setting baud rate...\n");
CH9120_TX_BAUD(CH9120_BAUD_RATE, UART1_BAUD1);
printf("Finalizing configuration...\n");
CH9120_End();
uart_set_baudrate(UART_ID1, TRANSPORT_BAUD_RATE);
while (uart_is_readable(UART_ID1)) {
uart_getc(UART_ID1);
}
printf("CH9120 initialization complete\n");
}
void CH9120_send_data(const char *data, size_t len) {
for (size_t i = 0; i < len; i++) {
if (uart_is_writable(UART_ID1)) {
uart_putc(UART_ID1, data[i]);
}
}
}
int CH9120_receive_data(char *buffer, size_t max_len) {
size_t count = 0;
while (uart_is_readable(UART_ID1) && count < max_len) {
buffer[count++] = uart_getc(UART_ID1);
}
return count;
}
void CH9120_process(void) {
char buffer[256];
int received = CH9120_receive_data(buffer, sizeof(buffer) - 1);
if (received > 0) {
buffer[received] = '\0';
printf("Received: %s\n", buffer);
CH9120_send_data("ACK: ", 5);
CH9120_send_data(buffer, received);
}
}