OTP return value cleanup

This commit is contained in:
Lorenz Meier 2014-01-07 21:41:07 +01:00
parent 4ef7817d96
commit 0ef85c133b
2 changed files with 171 additions and 143 deletions

View File

@ -43,6 +43,8 @@
*
*/
#include <nuttx/config.h>
#include <board_config.h>
#include <stdio.h>
#include <math.h>
#include <unistd.h>
@ -57,9 +59,11 @@ int val_read(void* dest, volatile const void* src, int bytes)
{
int i;
for (i = 0; i < bytes / 4; i++) {
*(((volatile uint32_t *)dest) + i) = *(((volatile uint32_t *)src) + i);
*(((volatile unsigned *)dest) + i) = *(((volatile unsigned *)src) + i);
}
return i * 4;
}
@ -69,23 +73,35 @@ int write_otp(uint8_t id_type, uint32_t vid, uint32_t pid, char* signature)
warnx("write_otp: PX4 / %02X / %02X / %02X / ... etc \n", id_type, vid, pid);
int errors = 0;
// descriptor
F_write_byte( ADDR_OTP_START, 'P'); // write the 'P' from PX4. to first byte in OTP
F_write_byte( ADDR_OTP_START+1, 'X'); // write the 'P' from PX4. to first byte in OTP
F_write_byte( ADDR_OTP_START+2, '4');
F_write_byte( ADDR_OTP_START+3, '\0');
if (F_write_byte(ADDR_OTP_START, 'P'))
errors++;
// write the 'P' from PX4. to first byte in OTP
if (F_write_byte(ADDR_OTP_START + 1, 'X'))
errors++; // write the 'P' from PX4. to first byte in OTP
if (F_write_byte(ADDR_OTP_START + 2, '4'))
errors++;
if (F_write_byte(ADDR_OTP_START + 3, '\0'))
errors++;
//id_type
F_write_byte( ADDR_OTP_START+4, id_type);
if (F_write_byte(ADDR_OTP_START + 4, id_type))
errors++;
// vid and pid are 4 bytes each
F_write_word( ADDR_OTP_START+5, vid);
F_write_word( ADDR_OTP_START+9, pid);
if (F_write_word(ADDR_OTP_START + 5, vid))
errors++;
if (F_write_word(ADDR_OTP_START + 9, pid))
errors++;
// leave some 19 bytes of space, and go to the next block...
// then the auth sig starts
for (int i = 0 ; i < 128 ; i++) {
F_write_byte( ADDR_OTP_START+32+i, signature[i]);
if (F_write_byte(ADDR_OTP_START + 32 + i, signature[i]))
errors++;
}
return errors;
}
int lock_otp(void)
@ -102,23 +118,38 @@ int lock_otp(void)
// val_copy(lock_ptr, &otp_lock_mem, sizeof(otp_lock_mem));
int locksize = 5;
int errors = 0;
// or just realise it's exctly 5x 32byte blocks we need to lock. 1 block for ID,type,vid,pid, and 4 blocks for certificate, which is 128 bytes.
for (int i = 0 ; i < locksize ; i++) {
F_write_byte( ADDR_OTP_LOCK_START+i, OTP_LOCK_LOCKED);
if (F_write_byte(ADDR_OTP_LOCK_START + i, OTP_LOCK_LOCKED))
errors++;
}
return errors;
}
// COMPLETE, BUSY, or other flash error?
uint8_t F_GetStatus(void) {
uint8_t fs = F_COMPLETE;
int F_GetStatus(void)
{
int fs = F_COMPLETE;
if ((FLASH->status & F_BSY) == F_BSY) { fs = F_BUSY; } else {
if ((FLASH->status & F_WRPERR) != (uint32_t)0x00) { fs = F_ERROR_WRP; } else {
if ((FLASH->status & (uint32_t)0xEF) != (uint32_t)0x00) { fs = F_ERROR_PROGRAM; } else {
if ((FLASH->status & F_OPERR) != (uint32_t)0x00) { fs = F_ERROR_OPERATION; } else {
fs = F_COMPLETE; } } } }
fs = F_COMPLETE;
}
}
}
}
return fs;
}
@ -126,8 +157,7 @@ uint8_t F_GetStatus(void) {
// enable FLASH Registers
void F_unlock(void)
{
if((FLASH->control & F_CR_LOCK) != 0)
{
if ((FLASH->control & F_CR_LOCK) != 0) {
FLASH->key = F_KEY1;
FLASH->key = F_KEY2;
}
@ -140,21 +170,24 @@ void F_lock(void)
}
// flash write word.
uint8_t F_write_word(uint32_t Address, uint32_t Data)
int F_write_word(uint32_t Address, uint32_t Data)
{
unsigned char octet[4] = {0, 0, 0, 0};
for (int i=0; i<4; i++)
{
int ret = 0;
for (int i = 0; i < 4; i++) {
octet[i] = (Data >> (i * 8)) & 0xFF;
F_write_byte(Address+i,octet[i]);
ret = F_write_byte(Address + i, octet[i]);
}
return ret;
}
// flash write byte
uint8_t F_write_byte(uint32_t Address, uint8_t Data)
int F_write_byte(uint32_t Address, uint8_t Data)
{
volatile uint8_t status = F_COMPLETE;
volatile int status = F_COMPLETE;
//warnx("F_write_byte: %08X %02d", Address , Data ) ;
@ -163,10 +196,10 @@ uint8_t F_write_byte(uint32_t Address, uint8_t Data)
//Wait for FLASH operation to complete by polling on BUSY flag.
status = F_GetStatus();
while (status == F_BUSY) { status = F_GetStatus();}
if(status == F_COMPLETE)
{
if (status == F_COMPLETE) {
//if the previous operation is completed, proceed to program the new data
FLASH->control &= CR_PSIZE_MASK;
FLASH->control |= F_PSIZE_BYTE;
@ -176,6 +209,7 @@ uint8_t F_write_byte(uint32_t Address, uint8_t Data)
//Wait for FLASH operation to complete by polling on BUSY flag.
status = F_GetStatus();
while (status == F_BUSY) { status = F_GetStatus();}
//if the program operation is completed, disable the PG Bit
@ -183,9 +217,8 @@ uint8_t F_write_byte(uint32_t Address, uint8_t Data)
}
//Return the Program Status
return status;
return !(status == F_COMPLETE);
}

View File

@ -1,9 +1,6 @@
/****************************************************************************
*
* Copyright (C) 2012-2013 PX4 Development Team. All rights reserved.
* Authors:
* Lorenz Meier <lm@inf.ethz.ch>
* David "Buzz" Bussenschutt <davidbuzz@gmail.com>
* Copyright (c) 2012-2014 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -66,8 +63,7 @@
#define F_ERROR_OPERATION 4
#define F_COMPLETE 5
typedef struct
{
typedef struct {
volatile uint32_t accesscontrol; // 0x00
volatile uint32_t key; // 0x04
volatile uint32_t optionkey; // 0x08
@ -124,7 +120,6 @@ typedef struct
};
#pragma pack(pop)
#define UDID_START 0x1FFF7A10
#define ADDR_F_SIZE 0x1FFF7A22
#pragma pack(push, 1)
@ -148,8 +143,8 @@ typedef struct
__EXPORT int lock_otp(void);
__EXPORT uint8_t F_write_byte(uint32_t Address, uint8_t Data);
__EXPORT uint8_t F_write_word(uint32_t Address, uint32_t Data);
__EXPORT int F_write_byte(uint32_t Address, uint8_t Data);
__EXPORT int F_write_word(uint32_t Address, uint32_t Data);
__END_DECLS