Skip to content

Commit

Permalink
Updated SPI code for controller and peripheral based on updated firmware
Browse files Browse the repository at this point in the history
  • Loading branch information
sonirohan committed Nov 22, 2024
1 parent 4a3adff commit fd47e1f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 52 deletions.
15 changes: 6 additions & 9 deletions applications/flight_computer/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
import asyncio
import time


from tasks.inter_subsystem_spi import (
inter_subsystem_spi_task,
inter_subsystem_spi_debug_task,
feedbackTask,
spiWriteTask,
sensorReadTask
)

async def gatheredTask():
await asyncio.gather(feedbackTask(), spiWriteTask(), sensorReadTask())

async def gathered_task():
await asyncio.gather(inter_subsystem_spi_task(), inter_subsystem_spi_debug_task())


if __name__ == "__main__":
asyncio.run(gathered_task())
asyncio.run(gatheredTask())
77 changes: 34 additions & 43 deletions applications/testing_board/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,56 @@
import microcontroller
import digitalio
import busio
import spitarget

import asyncio
import time

inter_subsystem_spi_bus = busio.SPI(
clock=board.D12,
MOSI=board.D13,
MISO=board.D11,
SS=board.D10,
slave_mode=True,
inter_subsystem_spi_bus = spitarget.SPITarget(
sck=board.D12,
mosi=board.D13,
miso=board.D11,
ss=board.D10
)
while not inter_subsystem_spi_bus.try_lock():
pass
inter_subsystem_spi_bus.configure() # baudrate, polarity, etc. available here as kw params


async def send_receive(transmit_buffer, receive_buffer):
inter_subsystem_spi_bus.async_transfer_start(transmit_buffer, receive_buffer)
while not inter_subsystem_spi_bus.async_transfer_finished():
inter_subsystem_spi_bus.load_packet(
mosi_packet=receive_buffer,
miso_packet=transmit_buffer
)
while not inter_subsystem_spi_bus.try_transfer():
await asyncio.sleep(0)
inter_subsystem_spi_bus.async_transfer_end()


sensor_value = 0x10203040
spi_read_bytes = bytearray([0] * 4)
spi_write_bytes = bytearray([0] * 4)
sensorValue = 0x10203040
spiReadBytes = bytearray([0] * 4)
spiWriteBytes = bytearray([0] * 4)


async def spi_write_task():
global spi_read_bytes, spi_write_bytes, sensor_value, inter_subsystem_spi_bus
async def spiWriteTask():
global spiReadBytes, spiWriteBytes, sensorValue, inter_subsystem_spi_bus
while True:
# communicates data back to CDH
spi_write_bytes[0] = (sensor_value & 0xFF000000) >> 24
spi_write_bytes[1] = (sensor_value & 0xFF0000) >> 16
spi_write_bytes[2] = (sensor_value & 0xFF00) >> 8
spi_write_bytes[3] = (sensor_value & 0xFF) >> 0
await send_receive(spi_write_bytes, spi_read_bytes)


async def sensor_read_task():
# regularly updates `sensor_value` based on the feedback from the sensor
global sensor_value
# communicates commands with subsystem X
spiWriteBytes[0] = (sensorValue & 0xFF000000) >> 24
spiWriteBytes[1] = (sensorValue & 0xFF0000) >> 16
spiWriteBytes[2] = (sensorValue & 0xFF00) >> 8
spiWriteBytes[3] = (sensorValue & 0xFF) >> 0
await send_receive(spiWriteBytes, spiReadBytes)

async def sensorReadTask():
# regularly updates `sensorValue` based on the feedback from the sensor
global sensorValue
while True:
sensor_value += 1
sensorValue += 1
await asyncio.sleep(0.2)


async def feedback_task():
async def feedbackTask():
# send debug data to the USB serial regularly
global sensor_value, spi_read_bytes, spi_write_bytes
global sensorValue, spiReadBytes, spiWriteBytes
while True:
print("TST wrote", list(bytes(spi_write_bytes)), "to SPI") # fix with [:-1]
print("TST read", list(bytes(spi_read_bytes)), "from SPI") # fix with [:-1]
print("TST wrote", list(bytes(spiWriteBytes)), "to SPI") # fix with [:-1]
print("TST read", list(bytes(spiReadBytes)), "from SPI") # fix with [:-1]
await asyncio.sleep(1)

async def gatheredTask():
await asyncio.gather(feedbackTask(), spiWriteTask(), sensorReadTask())

async def gathered_task():
await asyncio.gather(spi_write_task(), sensor_read_task(), feedback_task())


if __name__ == "__main__":
asyncio.run(gathered_task())
asyncio.run(gatheredTask())

0 comments on commit fd47e1f

Please sign in to comment.