Skip to content
This repository was archived by the owner on Aug 2, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions libraries/ms-common/src/x86/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,12 @@ StatusCode i2c_write(I2CPort i2c, I2CAddress addr, uint8_t *tx_data, size_t tx_l

StatusCode i2c_read_reg(I2CPort i2c, I2CAddress addr, uint8_t reg, uint8_t *rx_data,
size_t rx_len) {
if (i2c >= NUM_I2C_PORTS) {
return status_msg(STATUS_CODE_INVALID_ARGS, "Invalid I2C port.");
}
uint8_t reg_to_read = reg;
status_ok_or_return(i2c_write(i2c, addr, &reg_to_read, sizeof(reg)));
status_ok_or_return(i2c_read(i2c, addr, rx_data, rx_len));
rx_data[0] = 0x40;
rx_data[1] = 0x50;
return STATUS_CODE_OK;
}

StatusCode i2c_write_reg(I2CPort i2c, I2CAddress addr, uint8_t reg, uint8_t *tx_data,
size_t tx_len) {
if (i2c >= NUM_I2C_PORTS) {
return status_msg(STATUS_CODE_INVALID_ARGS, "Invalid I2C port.");
}

uint8_t reg_to_write = reg;
status_ok_or_return(i2c_write(i2c, addr, &reg_to_write, sizeof(reg)));
status_ok_or_return(i2c_write(i2c, addr, tx_data, tx_len));
return STATUS_CODE_OK;
}
15 changes: 15 additions & 0 deletions projects/fw_103/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!--
General guidelines
These are just guidelines, not strict rules - document however seems best.
A README for a firmware-only project (e.g. Babydriver, MPXE, bootloader, CAN explorer) should answer the following questions:
- What is it?
- What problem does it solve?
- How do I use it? (with usage examples / example commands, etc)
- How does it work? (architectural overview)
A README for a board project (powering a hardware board, e.g. power distribution, centre console, charger, BMS carrier) should answer the following questions:
- What is the purpose of the board?
- What are all the things that the firmware needs to do?
- How does it fit into the overall system?
- How does it work? (architectural overview, e.g. what each module's purpose is or how data flows through the firmware)
-->
# fw_103
6 changes: 6 additions & 0 deletions projects/fw_103/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"libs": [
"FreeRTOS",
"ms-common"
]
}
45 changes: 45 additions & 0 deletions projects/fw_103/inc/ads1115.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

#include "gpio.h"
#include "i2c.h"
#include "tasks.h"

#define ALRT_EVENT 0

#define ADS1115_I2C_PORT 0

typedef enum {
ADS1115_ADDR_GND = 0x48, // 0b1001000
ADS1115_ADDR_VDD = 0x49, // 0b1001001
ADS1115_ADDR_SDA = 0x4A, // 0b1001010
ADS1115_ADDR_SCL = 0x4B, // 0b1001011
} ADS1115_Address;

typedef enum {
ADS1115_CHANNEL_0 = 0,
ADS1115_CHANNEL_1,
ADS1115_CHANNEL_2,
ADS1115_CHANNEL_3,
} ADS1115_Channel;

typedef enum {
ADS1115_REG_CONVERSION = 0x00,
ADS1115_REG_CONFIG,
ADS1115_REG_LO_THRESH,
ADS1115_REG_HI_THRESH,
} ADS1115_Reg;

typedef struct ADS1115_Config {
I2CPort i2c_port;
I2CAddress i2c_addr;
Task *handler_task;
GpioAddress *ready_pin;
} ADS1115_Config;

StatusCode ads1115_init(ADS1115_Config *config, ADS1115_Address i2c_addr, GpioAddress *ready_pin);

StatusCode ads1115_select_channel(ADS1115_Config *config, ADS1115_Channel channel);

StatusCode ads1115_read_raw(ADS1115_Config *config, ADS1115_Channel channel, uint16_t *reading);

StatusCode ads1115_read_converted(ADS1115_Config *config, ADS1115_Channel channel, float *reading);
81 changes: 81 additions & 0 deletions projects/fw_103/src/ads1115.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "ads1115.h"

#include "gpio_it.h"
#include "i2c.h"
#include "status.h"

// Completed by Ethan Gan
StatusCode ads1115_init(ADS1115_Config *config, ADS1115_Address i2c_addr, GpioAddress *ready_pin) {
if (config == NULL) {
return status_code(STATUS_CODE_INVALID_ARGS);
}

config->i2c_addr = i2c_addr;

uint16_t cmd;

// Write Config register
/* TODO: fill out this value */
cmd = 0x0483;

i2c_write_reg(config->i2c_port, i2c_addr, ADS1115_REG_CONFIG, (uint8_t *)(&cmd), 2);

/* TODO (optional) */
// Set low thresh to zero
cmd = 0x0000;
i2c_write_reg(config->i2c_port, i2c_addr, ADS1115_REG_LO_THRESH, (uint8_t *)(&cmd), 2);

/* TODO (optional) */
// Set high thresh to 1V
cmd = 0x0000;
i2c_write_reg(config->i2c_port, i2c_addr, ADS1115_REG_LO_THRESH, (uint8_t *)(&cmd), 2);

// Register the ALRT pin
/* TODO (optional) */

return STATUS_CODE_OK;
}

StatusCode ads1115_select_channel(ADS1115_Config *config, ADS1115_Channel channel) {
if (config == NULL) {
return status_code(STATUS_CODE_INVALID_ARGS);
}

uint16_t cmd;

// Write Config register
cmd = 0x0483;
i2c_write_reg(config->i2c_port, config->i2c_addr, ADS1115_REG_CONFIG, (uint8_t *)(&cmd), 2);
return STATUS_CODE_OK;
}

StatusCode ads1115_read_raw(ADS1115_Config *config, ADS1115_Channel channel, uint16_t *reading) {
/* TODO: complete function */
// i2c already initialized somewhere else
uint16_t cmd = 0x0483;
// read 2 bytes
StatusCode status = i2c_read_reg(config->i2c_port, config->i2c_addr, ADS1115_REG_CONVERSION, (uint8_t *)reading, 2);
if(status!=STATUS_CODE_OK) {
return status;
}
// this info is stored in reading
return STATUS_CODE_OK;
}

StatusCode ads1115_read_converted(ADS1115_Config *config, ADS1115_Channel channel, float *reading) {
/* TODO: complete function */
uint16_t raw_reading;
uint16_t cmd = 0x0483;
// the reason why it is mapped to int8_t other than the header file calling for it
// is because the rx_data really only cares about pointing to the start of the memory address to deal with the data byte by byte which is why we enter 2 bytes as rx_len.
StatusCode status = i2c_read_reg(config->i2c_port, config->i2c_addr, ADS1115_REG_CONVERSION, (uint8_t *)&raw_reading, 2);

if(status!=STATUS_CODE_OK) {
return status;
}

//voltage = (raw/max)*range - (max magnitude)
// remember to avoid integer division
*reading = ((raw_reading/65535.0f)*4.096f)-2.048;
return STATUS_CODE_OK;
}
80 changes: 80 additions & 0 deletions projects/fw_103/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Project Code for FW 103

Assignment: Create an ADC driver to interface with the ADS1115 Multi-Channel ADC IC.

Requirements:
- Implement the ADC driver functions (set config, select and read from a channel)
- ADC task to periodically measure the voltage of channel 0
- Overvoltage interrupt (configure the interrupt to be on channel 0 with thresholds of 0V - 1V)
*/
// Completed by Ethan Gan
#include "gpio.h"
#include "delay.h"

#include "ads1115.h"

#include <stdio.h>
#include "log.h"
#include "tasks.h"


GpioAddress led_addr = {
// PB5 = Port B, Pin 5
// Green LED from the FW103 picture
.port = GPIO_PORT_B,
.pin = 5,
};

TASK(task1, TASK_STACK_512) {
GpioAddress ready_pin = {
.port = GPIO_PORT_B,
.pin = GPIO_Pin_0,
};
ADS1115_Config config = {
// ig handler task is not "FW103 Final Task: Blink Green LED 1 blink per second",
.handler_task = (Task *)xTaskGetCurrentTaskHandle(),
.i2c_addr = ADS1115_ADDR_GND,
.i2c_port = ADS1115_I2C_PORT,
.ready_pin = &ready_pin,
};
float voltage;
StatusCode status = ads1115_init(&config, config.i2c_addr, config.ready_pin); // start the ads program
// review struct syntax later
if (status!=STATUS_CODE_OK) {
LOG_DEBUG("Failed to start ADS1115\n");
vTaskDelete(NULL);
}

LOG_DEBUG("blink started\n");

while(true) {
ads1115_read_converted(&config, ADS1115_CHANNEL_0, &voltage);
// I still don't get the channel parameter, it looks like it is unused

LOG_DEBUG("Voltage reading: %f V\n", voltage);
gpio_toggle_state(&led_addr);
LOG_DEBUG("blink\n");

delay_ms(1000);
}
vTaskDelete(NULL);

}

int main() {
tasks_init();
log_init();
gpio_init(); // already done

tasks_init_task(task1, TASK_PRIORITY(2), NULL);

LOG_DEBUG("Welcome to FW 103!\n");



tasks_start();

LOG_DEBUG("exiting main?\n");
return 0;
}
15 changes: 15 additions & 0 deletions projects/task1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!--
General guidelines
These are just guidelines, not strict rules - document however seems best.
A README for a firmware-only project (e.g. Babydriver, MPXE, bootloader, CAN explorer) should answer the following questions:
- What is it?
- What problem does it solve?
- How do I use it? (with usage examples / example commands, etc)
- How does it work? (architectural overview)
A README for a board project (powering a hardware board, e.g. power distribution, centre console, charger, BMS carrier) should answer the following questions:
- What is the purpose of the board?
- What are all the things that the firmware needs to do?
- How does it fit into the overall system?
- How does it work? (architectural overview, e.g. what each module's purpose is or how data flows through the firmware)
-->
# task1
6 changes: 6 additions & 0 deletions projects/task1/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"libs": [
"FreeRTOS",
"ms-common"
]
}
86 changes: 86 additions & 0 deletions projects/task1/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Fimware 102 QUEUES - ETHAN GAN
#include <stdbool.h>
#include <stdint.h>

#include "FreeRTOS.h"
#include "tasks.h"
#include "queues.h"
#include "status.h"
#include "delay.h"

#include "log.h"
#include "misc.h"


#define ITEM_SZ 6
#define QUEUE_LEN 5
#define BUF_SIZE (QUEUE_LEN * ITEM_SZ)

static const char s_list[QUEUE_LEN][ITEM_SZ] = {
"Item1",
"Item2",
"Item3",
"Item4",
"Item5"
};

// Task static entities
static uint8_t s_queue1_buf[BUF_SIZE];
static Queue s_queue1 = {
// Add parameters
.num_items = QUEUE_LEN,
.item_size = ITEM_SZ,
.storage_buf = s_queue1_buf, // INTERNAL QUEUE STORAGE (BUFFER IS JUST ALLOCATED MEMORY)
};


TASK(task1, TASK_STACK_512) {
LOG_DEBUG("Task 1 initialized!\n");
StatusCode ret;
while (true) {
// Your code goes here
// Code continually adds the list of strings over and over again
for (long unsigned int i=0; i<sizeof(s_list)/sizeof(s_list[0]); i++) {
ret = queue_send(&s_queue1, &s_list[i], 0);
if (ret != STATUS_CODE_OK) {
LOG_DEBUG("write to queue failed\n");
}
delay_ms(100);
}

}
vTaskDelete(NULL);
}

TASK(task2, TASK_STACK_512) {
LOG_DEBUG("Task 2 initialized!\n");
const char outstr[ITEM_SZ]; // Remember in C and early C++ an array of characters is a string
StatusCode ret;
while (true) {
// Your code goes here

ret = queue_receive(&s_queue1, outstr, 1000);
if (ret == STATUS_CODE_OK) {
LOG_DEBUG("Received: %s\n", outstr);
} else {
LOG_DEBUG("read from queue failed\n");
}
}
vTaskDelete(NULL);
}

int main(void) {
log_init();
// Initialize queues here

tasks_init();
queue_init(&s_queue1);

tasks_init_task(task1, TASK_PRIORITY(2), NULL);
tasks_init_task(task2, TASK_PRIORITY(2), NULL);

LOG_DEBUG("Program start...\n");
tasks_start();

return 0;
}
Loading
Loading